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