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