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