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