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