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