xref: /freebsd/libexec/rpc.rquotad/rquotad.c (revision e627b39baccd1ec9129690167cf5e6d860509655)
1 /*
2  * by Manuel Bouyer (bouyer@ensta.fr)
3  *
4  * There is no copyright, you can use it as you want.
5  */
6 
7 #include <sys/param.h>
8 #include <sys/types.h>
9 #include <sys/mount.h>
10 #include <sys/file.h>
11 #include <sys/stat.h>
12 #include <sys/socket.h>
13 #include <signal.h>
14 
15 #include <stdio.h>
16 #include <fstab.h>
17 #include <ctype.h>
18 #include <stdlib.h>
19 #include <unistd.h>
20 #include <string.h>
21 #include <pwd.h>
22 #include <grp.h>
23 #include <errno.h>
24 
25 #include <syslog.h>
26 #include <varargs.h>
27 
28 #include <ufs/ufs/quota.h>
29 #include <rpc/rpc.h>
30 #include <rpc/pmap_clnt.h>
31 #include <rpcsvc/rquota.h>
32 #include <arpa/inet.h>
33 
34 void rquota_service __P((struct svc_req *request, SVCXPRT *transp));
35 void sendquota __P((struct svc_req *request, SVCXPRT *transp));
36 void printerr_reply __P((SVCXPRT *transp));
37 void initfs __P((void));
38 int getfsquota __P((long id, char *path, struct dqblk *dqblk));
39 int hasquota __P((struct fstab *fs, char **qfnamep));
40 
41 /*
42  * structure containing informations about ufs filesystems
43  * initialised by initfs()
44  */
45 struct fs_stat {
46 	struct fs_stat *fs_next;	/* next element */
47 	char   *fs_file;		/* mount point of the filesystem */
48 	char   *qfpathname;		/* pathname of the quota file */
49 	dev_t   st_dev;			/* device of the filesystem */
50 } fs_stat;
51 struct fs_stat *fs_begin = NULL;
52 
53 int from_inetd = 1;
54 
55 void
56 cleanup()
57 {
58 	(void) pmap_unset(RQUOTAPROG, RQUOTAVERS);
59 	exit(0);
60 }
61 
62 int
63 main(argc, argv)
64 	int     argc;
65 	char   *argv[];
66 {
67 	SVCXPRT *transp;
68 	int sock = 0;
69 	int proto = 0;
70 	struct sockaddr_in from;
71 	int fromlen;
72 
73 	fromlen = sizeof(from);
74 	if (getsockname(0, (struct sockaddr *)&from, &fromlen) < 0) {
75 		from_inetd = 0;
76 		sock = RPC_ANYSOCK;
77 		proto = IPPROTO_UDP;
78 	}
79 
80 	if (!from_inetd) {
81 		daemon(0, 0);
82 
83 		(void) pmap_unset(RQUOTAPROG, RQUOTAVERS);
84 
85 		(void) signal(SIGINT, cleanup);
86 		(void) signal(SIGTERM, cleanup);
87 		(void) signal(SIGHUP, cleanup);
88 	}
89 
90 	openlog("rpc.rquotad", LOG_CONS|LOG_PID, LOG_DAEMON);
91 
92 	/* create and register the service */
93 	transp = svcudp_create(sock);
94 	if (transp == NULL) {
95 		syslog(LOG_ERR, "couldn't create udp service.");
96 		exit(1);
97 	}
98 	if (!svc_register(transp, RQUOTAPROG, RQUOTAVERS, rquota_service, proto)) {
99 		syslog(LOG_ERR, "unable to register (RQUOTAPROG, RQUOTAVERS, %s).", proto?"udp":"(inetd)");
100 		exit(1);
101 	}
102 
103 	initfs();		/* init the fs_stat list */
104 	svc_run();
105 	syslog(LOG_ERR, "svc_run returned");
106 	exit(1);
107 }
108 
109 void
110 rquota_service(request, transp)
111 	struct svc_req *request;
112 	SVCXPRT *transp;
113 {
114 	switch (request->rq_proc) {
115 	case NULLPROC:
116 		(void)svc_sendreply(transp, xdr_void, (char *)NULL);
117 		break;
118 
119 	case RQUOTAPROC_GETQUOTA:
120 	case RQUOTAPROC_GETACTIVEQUOTA:
121 		sendquota(request, transp);
122 		break;
123 
124 	default:
125 		svcerr_noproc(transp);
126 		break;
127 	}
128 	if (from_inetd)
129 		exit(0);
130 }
131 
132 /* read quota for the specified id, and send it */
133 void
134 sendquota(request, transp)
135 	struct svc_req *request;
136 	SVCXPRT *transp;
137 {
138 	struct getquota_args getq_args;
139 	struct getquota_rslt getq_rslt;
140 	struct dqblk dqblk;
141 	struct timeval timev;
142 
143 	bzero((char *)&getq_args, sizeof(getq_args));
144 	if (!svc_getargs(transp, xdr_getquota_args, (caddr_t)&getq_args)) {
145 		svcerr_decode(transp);
146 		return;
147 	}
148 	if (request->rq_cred.oa_flavor != AUTH_UNIX) {
149 		/* bad auth */
150 		getq_rslt.status = Q_EPERM;
151 	} else if (!getfsquota(getq_args.gqa_uid, getq_args.gqa_pathp, &dqblk)) {
152 		/* failed, return noquota */
153 		getq_rslt.status = Q_NOQUOTA;
154 	} else {
155 		gettimeofday(&timev, NULL);
156 		getq_rslt.status = Q_OK;
157 		getq_rslt.getquota_rslt_u.gqr_rquota.rq_active = TRUE;
158 		getq_rslt.getquota_rslt_u.gqr_rquota.rq_bsize = DEV_BSIZE;
159 		getq_rslt.getquota_rslt_u.gqr_rquota.rq_bhardlimit =
160 		    dqblk.dqb_bhardlimit;
161 		getq_rslt.getquota_rslt_u.gqr_rquota.rq_bsoftlimit =
162 		    dqblk.dqb_bsoftlimit;
163 		getq_rslt.getquota_rslt_u.gqr_rquota.rq_curblocks =
164 		    dqblk.dqb_curblocks;
165 		getq_rslt.getquota_rslt_u.gqr_rquota.rq_fhardlimit =
166 		    dqblk.dqb_ihardlimit;
167 		getq_rslt.getquota_rslt_u.gqr_rquota.rq_fsoftlimit =
168 		    dqblk.dqb_isoftlimit;
169 		getq_rslt.getquota_rslt_u.gqr_rquota.rq_curfiles =
170 		    dqblk.dqb_curinodes;
171 		getq_rslt.getquota_rslt_u.gqr_rquota.rq_btimeleft =
172 		    dqblk.dqb_btime - timev.tv_sec;
173 		getq_rslt.getquota_rslt_u.gqr_rquota.rq_ftimeleft =
174 		    dqblk.dqb_itime - timev.tv_sec;
175 	}
176 	if (!svc_sendreply(transp, xdr_getquota_rslt, (char *)&getq_rslt)) {
177 		svcerr_systemerr(transp);
178 	}
179 	if (!svc_freeargs(transp, xdr_getquota_args, (caddr_t)&getq_args)) {
180 		syslog(LOG_ERR, "unable to free arguments");
181 		exit(1);
182 	}
183 }
184 
185 void
186 printerr_reply(transp)	/* when a reply to a request failed */
187 	SVCXPRT *transp;
188 {
189 	char   *name;
190 	struct sockaddr_in *caller;
191 	int     save_errno;
192 
193 	save_errno = errno;
194 
195 	caller = svc_getcaller(transp);
196 	name = (char *)inet_ntoa(caller->sin_addr);
197 	errno = save_errno;
198 	if (errno == 0)
199 		syslog(LOG_ERR, "couldn't send reply to %s", name);
200 	else
201 		syslog(LOG_ERR, "couldn't send reply to %s: %m", name);
202 }
203 
204 /* initialise the fs_tab list from entries in /etc/fstab */
205 void
206 initfs()
207 {
208 	struct fs_stat *fs_current = NULL;
209 	struct fs_stat *fs_next = NULL;
210 	char *qfpathname;
211 	struct fstab *fs;
212 	struct stat st;
213 
214 	setfsent();
215 	while ((fs = getfsent())) {
216 		if (strcmp(fs->fs_vfstype, "ufs"))
217 			continue;
218 		if (!hasquota(fs, &qfpathname))
219 			continue;
220 
221 		fs_current = (struct fs_stat *) malloc(sizeof(struct fs_stat));
222 		fs_current->fs_next = fs_next;	/* next element */
223 
224 		fs_current->fs_file = malloc(sizeof(char) * (strlen(fs->fs_file) + 1));
225 		strcpy(fs_current->fs_file, fs->fs_file);
226 
227 		fs_current->qfpathname = malloc(sizeof(char) * (strlen(qfpathname) + 1));
228 		strcpy(fs_current->qfpathname, qfpathname);
229 
230 		stat(qfpathname, &st);
231 		fs_current->st_dev = st.st_dev;
232 
233 		fs_next = fs_current;
234 	}
235 	endfsent();
236 	fs_begin = fs_current;
237 }
238 
239 /*
240  * gets the quotas for id, filesystem path.
241  * Return 0 if fail, 1 otherwise
242  */
243 int
244 getfsquota(id, path, dqblk)
245 	long id;
246 	char   *path;
247 	struct dqblk *dqblk;
248 {
249 	struct stat st_path;
250 	struct fs_stat *fs;
251 	int	qcmd, fd, ret = 0;
252 
253 	if (stat(path, &st_path) < 0)
254 		return (0);
255 
256 	qcmd = QCMD(Q_GETQUOTA, USRQUOTA);
257 
258 	for (fs = fs_begin; fs != NULL; fs = fs->fs_next) {
259 		/* where the devise is the same as path */
260 		if (fs->st_dev != st_path.st_dev)
261 			continue;
262 
263 		/* find the specified filesystem. get and return quota */
264 		if (quotactl(fs->fs_file, qcmd, id, dqblk) == 0)
265 			return (1);
266 
267 		if ((fd = open(fs->qfpathname, O_RDONLY)) < 0) {
268 			syslog(LOG_ERR, "open error: %s: %m", fs->qfpathname);
269 			return (0);
270 		}
271 		if (lseek(fd, (off_t)(id * sizeof(struct dqblk)), L_SET) == (off_t)-1) {
272 			close(fd);
273 			return (1);
274 		}
275 		switch (read(fd, dqblk, sizeof(struct dqblk))) {
276 		case 0:
277 			/*
278                          * Convert implicit 0 quota (EOF)
279                          * into an explicit one (zero'ed dqblk)
280                          */
281 			bzero((caddr_t) dqblk, sizeof(struct dqblk));
282 			ret = 1;
283 			break;
284 		case sizeof(struct dqblk):	/* OK */
285 			ret = 1;
286 			break;
287 		default:	/* ERROR */
288 			syslog(LOG_ERR, "read error: %s: %m", fs->qfpathname);
289 			close(fd);
290 			return (0);
291 		}
292 		close(fd);
293 	}
294 	return (ret);
295 }
296 
297 /*
298  * Check to see if a particular quota is to be enabled.
299  * Comes from quota.c, NetBSD 0.9
300  */
301 int
302 hasquota(fs, qfnamep)
303 	struct fstab *fs;
304 	char  **qfnamep;
305 {
306 	static char initname, usrname[100];
307 	static char buf[BUFSIZ];
308 	char	*opt, *cp;
309 	char	*qfextension[] = INITQFNAMES;
310 
311 	if (!initname) {
312 		sprintf(usrname, "%s%s", qfextension[USRQUOTA], QUOTAFILENAME);
313 		initname = 1;
314 	}
315 	strcpy(buf, fs->fs_mntops);
316 	for (opt = strtok(buf, ","); opt; opt = strtok(NULL, ",")) {
317 		if ((cp = index(opt, '=')))
318 			*cp++ = '\0';
319 		if (strcmp(opt, usrname) == 0)
320 			break;
321 	}
322 	if (!opt)
323 		return (0);
324 	if (cp) {
325 		*qfnamep = cp;
326 		return (1);
327 	}
328 	sprintf(buf, "%s/%s.%s", fs->fs_file, QUOTAFILENAME, qfextension[USRQUOTA]);
329 	*qfnamep = buf;
330 	return (1);
331 }
332