rquotad.c revision 1.5 1 /*
2 * by Manuel Bouyer (bouyer (at) ensta.fr)
3 *
4 * There is no copyright, you can use it as you want.
5 */
6
7 #include <sys/param.h>
8 #include <sys/types.h>
9 #include <sys/mount.h>
10 #include <sys/file.h>
11 #include <sys/stat.h>
12 #include <signal.h>
13
14 #include <stdio.h>
15 #include <fstab.h>
16 #include <ctype.h>
17 #include <stdlib.h>
18 #include <string.h>
19 #include <pwd.h>
20 #include <grp.h>
21 #include <errno.h>
22
23 #include <syslog.h>
24 #include <varargs.h>
25
26 #include <ufs/ufs/quota.h>
27 #include <rpc/rpc.h>
28 #include <rpcsvc/rquota.h>
29 #include <arpa/inet.h>
30
31 void rquota_service __P((struct svc_req *request, SVCXPRT *transp));
32 void sendquota __P((struct svc_req *request, SVCXPRT *transp));
33 void printerr_reply __P((SVCXPRT *transp));
34 void initfs __P((void));
35 int getfsquota __P((long id, char *path, struct dqblk *dqblk));
36 int hasquota __P((struct fstab *fs, char **qfnamep));
37
38 /*
39 * structure containing informations about ufs filesystems
40 * initialised by initfs()
41 */
42 struct fs_stat {
43 struct fs_stat *fs_next; /* next element */
44 char *fs_file; /* mount point of the filesystem */
45 char *qfpathname; /* pathname of the quota file */
46 dev_t st_dev; /* device of the filesystem */
47 } fs_stat;
48 struct fs_stat *fs_begin = NULL;
49
50 int from_inetd = 1;
51
52 void
53 cleanup()
54 {
55 (void) pmap_unset(RQUOTAPROG, RQUOTAVERS);
56 exit(0);
57 }
58
59 int
60 main(argc, argv)
61 int argc;
62 char *argv[];
63 {
64 SVCXPRT *transp;
65 int sock = 0;
66 int proto = 0;
67 struct sockaddr_in from;
68 int fromlen;
69
70 fromlen = sizeof(from);
71 if (getsockname(0, (struct sockaddr *)&from, &fromlen) < 0) {
72 from_inetd = 0;
73 sock = RPC_ANYSOCK;
74 proto = IPPROTO_UDP;
75 }
76
77 if (!from_inetd) {
78 daemon(0, 0);
79
80 (void) pmap_unset(RQUOTAPROG, RQUOTAVERS);
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 transp = svcudp_create(sock);
91 if (transp == NULL) {
92 syslog(LOG_ERR, "couldn't create udp service.");
93 exit(1);
94 }
95 if (!svc_register(transp, RQUOTAPROG, RQUOTAVERS, rquota_service, proto)) {
96 syslog(LOG_ERR, "unable to register (RQUOTAPROG, RQUOTAVERS, %s).", proto?"udp":"(inetd)");
97 exit(1);
98 }
99
100 initfs(); /* init the fs_stat list */
101 svc_run();
102 syslog(LOG_ERR, "svc_run returned");
103 exit(1);
104 }
105
106 void
107 rquota_service(request, transp)
108 struct svc_req *request;
109 SVCXPRT *transp;
110 {
111 switch (request->rq_proc) {
112 case NULLPROC:
113 (void)svc_sendreply(transp, xdr_void, (char *)NULL);
114 break;
115
116 case RQUOTAPROC_GETQUOTA:
117 case RQUOTAPROC_GETACTIVEQUOTA:
118 sendquota(request, transp);
119 break;
120
121 default:
122 svcerr_noproc(transp);
123 break;
124 }
125 if (from_inetd)
126 exit(0);
127 }
128
129 /* read quota for the specified id, and send it */
130 void
131 sendquota(request, transp)
132 struct svc_req *request;
133 SVCXPRT *transp;
134 {
135 struct getquota_args getq_args;
136 struct getquota_rslt getq_rslt;
137 struct dqblk dqblk;
138 struct timeval timev;
139
140 bzero((char *)&getq_args, sizeof(getq_args));
141 if (!svc_getargs(transp, xdr_getquota_args, (caddr_t)&getq_args)) {
142 svcerr_decode(transp);
143 return;
144 }
145 if (request->rq_cred.oa_flavor != AUTH_UNIX) {
146 /* bad auth */
147 getq_rslt.status = Q_EPERM;
148 } else if (!getfsquota(getq_args.gqa_uid, getq_args.gqa_pathp, &dqblk)) {
149 /* failed, return noquota */
150 getq_rslt.status = Q_NOQUOTA;
151 } else {
152 gettimeofday(&timev, NULL);
153 getq_rslt.status = Q_OK;
154 getq_rslt.getquota_rslt_u.gqr_rquota.rq_active = TRUE;
155 getq_rslt.getquota_rslt_u.gqr_rquota.rq_bsize = DEV_BSIZE;
156 getq_rslt.getquota_rslt_u.gqr_rquota.rq_bhardlimit =
157 dqblk.dqb_bhardlimit;
158 getq_rslt.getquota_rslt_u.gqr_rquota.rq_bsoftlimit =
159 dqblk.dqb_bsoftlimit;
160 getq_rslt.getquota_rslt_u.gqr_rquota.rq_curblocks =
161 dqblk.dqb_curblocks;
162 getq_rslt.getquota_rslt_u.gqr_rquota.rq_fhardlimit =
163 dqblk.dqb_ihardlimit;
164 getq_rslt.getquota_rslt_u.gqr_rquota.rq_fsoftlimit =
165 dqblk.dqb_isoftlimit;
166 getq_rslt.getquota_rslt_u.gqr_rquota.rq_curfiles =
167 dqblk.dqb_curinodes;
168 getq_rslt.getquota_rslt_u.gqr_rquota.rq_btimeleft =
169 dqblk.dqb_btime - timev.tv_sec;
170 getq_rslt.getquota_rslt_u.gqr_rquota.rq_ftimeleft =
171 dqblk.dqb_itime - timev.tv_sec;
172 }
173 if (!svc_sendreply(transp, xdr_getquota_rslt, (char *)&getq_rslt)) {
174 svcerr_systemerr(transp);
175 }
176 if (!svc_freeargs(transp, xdr_getquota_args, (caddr_t)&getq_args)) {
177 syslog(LOG_ERR, "unable to free arguments");
178 exit(1);
179 }
180 }
181
182 void
183 printerr_reply(transp) /* when a reply to a request failed */
184 SVCXPRT *transp;
185 {
186 char *name;
187 struct sockaddr_in *caller;
188 int save_errno;
189
190 save_errno = errno;
191
192 caller = svc_getcaller(transp);
193 name = (char *)inet_ntoa(caller->sin_addr);
194 errno = save_errno;
195 if (errno == 0)
196 syslog(LOG_ERR, "couldn't send reply to %s", name);
197 else
198 syslog(LOG_ERR, "couldn't send reply to %s: %m", name);
199 }
200
201 /* initialise the fs_tab list from entries in /etc/fstab */
202 void
203 initfs()
204 {
205 struct fs_stat *fs_current = NULL;
206 struct fs_stat *fs_next = NULL;
207 char *qfpathname;
208 struct fstab *fs;
209 struct stat st;
210 char *qfextension[] = INITQFNAMES;
211
212 setfsent();
213 while (fs = getfsent()) {
214 if (strcmp(fs->fs_vfstype, "ufs"))
215 continue;
216 if (!hasquota(fs, &qfpathname))
217 continue;
218
219 fs_current = (struct fs_stat *) malloc(sizeof(struct fs_stat));
220 fs_current->fs_next = fs_next; /* next element */
221
222 fs_current->fs_file = malloc(sizeof(char) * (strlen(fs->fs_file) + 1));
223 strcpy(fs_current->fs_file, fs->fs_file);
224
225 fs_current->qfpathname = malloc(sizeof(char) * (strlen(qfpathname) + 1));
226 strcpy(fs_current->qfpathname, qfpathname);
227
228 stat(qfpathname, &st);
229 fs_current->st_dev = st.st_dev;
230
231 fs_next = fs_current;
232 }
233 endfsent();
234 fs_begin = fs_current;
235 }
236
237 /*
238 * gets the quotas for id, filesystem path.
239 * Return 0 if fail, 1 otherwise
240 */
241 int
242 getfsquota(id, path, dqblk)
243 long id;
244 char *path;
245 struct dqblk *dqblk;
246 {
247 struct stat st_path;
248 struct fs_stat *fs;
249 int qcmd, fd, ret = 0;
250 char *qfextension[] = INITQFNAMES;
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((caddr_t) 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(fs, qfnamep)
302 struct fstab *fs;
303 char **qfnamep;
304 {
305 static char initname, usrname[100];
306 static char buf[BUFSIZ];
307 char *opt, *cp;
308 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, qfextension[USRQUOTA]);
328 *qfnamep = buf;
329 return (1);
330 }
331