xref: /freebsd/libexec/rpc.rquotad/rquotad.c (revision a3e8fd0b7f663db7eafff527d5c3ca3bcfa8a537)
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$";
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 <rpcsvc/rquota.h>
36 #include <arpa/inet.h>
37 #include <netdb.h>
38 
39 void rquota_service(struct svc_req *request, SVCXPRT *transp);
40 void sendquota(struct svc_req *request, SVCXPRT *transp);
41 void printerr_reply(SVCXPRT *transp);
42 void initfs(void);
43 int getfsquota(long id, char *path, struct dqblk *dqblk);
44 int hasquota(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(int sig)
62 {
63 	(void) rpcb_unset(RQUOTAPROG, RQUOTAVERS, NULL);
64 	exit(0);
65 }
66 
67 int
68 main(int argc, char *argv[])
69 {
70 	SVCXPRT *transp;
71 	int ok;
72 	struct sockaddr_storage from;
73 	int fromlen;
74 
75 	fromlen = sizeof(from);
76 	if (getsockname(0, (struct sockaddr *)&from, &fromlen) < 0) {
77 		from_inetd = 0;
78 	}
79 
80 	if (!from_inetd) {
81 		daemon(0, 0);
82 
83 		(void) rpcb_unset(RQUOTAPROG, RQUOTAVERS, NULL);
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 	if (from_inetd) {
94 		transp = svc_tli_create(0, NULL, NULL, 0, 0);
95 		if (transp == NULL) {
96 			syslog(LOG_ERR, "couldn't create udp service.");
97 			exit(1);
98 		}
99 		ok = svc_reg(transp, RQUOTAPROG, RQUOTAVERS,
100 			     rquota_service, NULL);
101 	} else
102 		ok = svc_create(rquota_service,
103 				RQUOTAPROG, RQUOTAVERS, "udp");
104 	if (!ok) {
105 		syslog(LOG_ERR, "unable to register (RQUOTAPROG, RQUOTAVERS, %s)", (!from_inetd)?"udp":"(inetd)");
106 		exit(1);
107 	}
108 
109 	initfs();		/* init the fs_stat list */
110 	svc_run();
111 	syslog(LOG_ERR, "svc_run returned");
112 	exit(1);
113 }
114 
115 void
116 rquota_service(struct svc_req *request, SVCXPRT *transp)
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 = malloc(sizeof(char) * (strlen(fs->fs_file) + 1));
227 		strcpy(fs_current->fs_file, fs->fs_file);
228 
229 		fs_current->qfpathname = malloc(sizeof(char) * (strlen(qfpathname) + 1));
230 		strcpy(fs_current->qfpathname, qfpathname);
231 
232 		stat(fs_current->fs_file, &st);
233 		fs_current->st_dev = st.st_dev;
234 
235 		fs_next = fs_current;
236 	}
237 	endfsent();
238 	fs_begin = fs_current;
239 }
240 
241 /*
242  * gets the quotas for id, filesystem path.
243  * Return 0 if fail, 1 otherwise
244  */
245 int
246 getfsquota(long id, char   *path, struct dqblk *dqblk)
247 {
248 	struct stat st_path;
249 	struct fs_stat *fs;
250 	int	qcmd, fd, ret = 0;
251 
252 	if (stat(path, &st_path) < 0)
253 		return (0);
254 
255 	qcmd = QCMD(Q_GETQUOTA, USRQUOTA);
256 
257 	for (fs = fs_begin; fs != NULL; fs = fs->fs_next) {
258 		/* where the devise is the same as path */
259 		if (fs->st_dev != st_path.st_dev)
260 			continue;
261 
262 		/* find the specified filesystem. get and return quota */
263 		if (quotactl(fs->fs_file, qcmd, id, dqblk) == 0)
264 			return (1);
265 
266 		if ((fd = open(fs->qfpathname, O_RDONLY)) < 0) {
267 			syslog(LOG_ERR, "open error: %s: %m", fs->qfpathname);
268 			return (0);
269 		}
270 		if (lseek(fd, (off_t)(id * sizeof(struct dqblk)), L_SET) == (off_t)-1) {
271 			close(fd);
272 			return (1);
273 		}
274 		switch (read(fd, dqblk, sizeof(struct dqblk))) {
275 		case 0:
276 			/*
277                          * Convert implicit 0 quota (EOF)
278                          * into an explicit one (zero'ed dqblk)
279                          */
280 			bzero(dqblk, sizeof(struct dqblk));
281 			ret = 1;
282 			break;
283 		case sizeof(struct dqblk):	/* OK */
284 			ret = 1;
285 			break;
286 		default:	/* ERROR */
287 			syslog(LOG_ERR, "read error: %s: %m", fs->qfpathname);
288 			close(fd);
289 			return (0);
290 		}
291 		close(fd);
292 	}
293 	return (ret);
294 }
295 
296 /*
297  * Check to see if a particular quota is to be enabled.
298  * Comes from quota.c, NetBSD 0.9
299  */
300 int
301 hasquota(struct fstab *fs, char  **qfnamep)
302 {
303 	static char initname, usrname[100];
304 	static char buf[BUFSIZ];
305 	char	*opt, *cp;
306 	char	*qfextension[] = INITQFNAMES;
307 
308 	if (!initname) {
309 		sprintf(usrname, "%s%s", qfextension[USRQUOTA], QUOTAFILENAME);
310 		initname = 1;
311 	}
312 	strcpy(buf, fs->fs_mntops);
313 	for (opt = strtok(buf, ","); opt; opt = strtok(NULL, ",")) {
314 		if ((cp = index(opt, '=')))
315 			*cp++ = '\0';
316 		if (strcmp(opt, usrname) == 0)
317 			break;
318 	}
319 	if (!opt)
320 		return (0);
321 	if (cp) {
322 		*qfnamep = cp;
323 		return (1);
324 	}
325 	sprintf(buf, "%s/%s.%s", fs->fs_file, QUOTAFILENAME, qfextension[USRQUOTA]);
326 	*qfnamep = buf;
327 	return (1);
328 }
329