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