edquota.c revision 1.51 1 1.51 dholland /* $NetBSD: edquota.c,v 1.51 2012/08/14 04:48:42 dholland Exp $ */
2 1.1 cgd /*
3 1.5 mycroft * Copyright (c) 1980, 1990, 1993
4 1.5 mycroft * The Regents of the University of California. All rights reserved.
5 1.1 cgd *
6 1.1 cgd * This code is derived from software contributed to Berkeley by
7 1.1 cgd * Robert Elz at The University of Melbourne.
8 1.1 cgd *
9 1.1 cgd * Redistribution and use in source and binary forms, with or without
10 1.1 cgd * modification, are permitted provided that the following conditions
11 1.1 cgd * are met:
12 1.1 cgd * 1. Redistributions of source code must retain the above copyright
13 1.1 cgd * notice, this list of conditions and the following disclaimer.
14 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
15 1.1 cgd * notice, this list of conditions and the following disclaimer in the
16 1.1 cgd * documentation and/or other materials provided with the distribution.
17 1.24 agc * 3. Neither the name of the University nor the names of its contributors
18 1.1 cgd * may be used to endorse or promote products derived from this software
19 1.1 cgd * without specific prior written permission.
20 1.1 cgd *
21 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 1.1 cgd * SUCH DAMAGE.
32 1.1 cgd */
33 1.1 cgd
34 1.15 lukem #include <sys/cdefs.h>
35 1.1 cgd #ifndef lint
36 1.29 lukem __COPYRIGHT("@(#) Copyright (c) 1980, 1990, 1993\
37 1.29 lukem The Regents of the University of California. All rights reserved.");
38 1.1 cgd #endif /* not lint */
39 1.1 cgd
40 1.1 cgd #ifndef lint
41 1.15 lukem #if 0
42 1.15 lukem static char sccsid[] = "from: @(#)edquota.c 8.3 (Berkeley) 4/27/95";
43 1.15 lukem #else
44 1.51 dholland __RCSID("$NetBSD: edquota.c,v 1.51 2012/08/14 04:48:42 dholland Exp $");
45 1.15 lukem #endif
46 1.1 cgd #endif /* not lint */
47 1.1 cgd
48 1.1 cgd /*
49 1.1 cgd * Disk quota editor.
50 1.1 cgd */
51 1.1 cgd #include <sys/param.h>
52 1.1 cgd #include <sys/stat.h>
53 1.1 cgd #include <sys/file.h>
54 1.1 cgd #include <sys/wait.h>
55 1.12 mikel #include <sys/queue.h>
56 1.30 bouyer #include <sys/types.h>
57 1.30 bouyer #include <sys/statvfs.h>
58 1.30 bouyer
59 1.40 dholland #include <quota.h>
60 1.30 bouyer
61 1.30 bouyer #include <assert.h>
62 1.15 lukem #include <err.h>
63 1.1 cgd #include <errno.h>
64 1.1 cgd #include <fstab.h>
65 1.1 cgd #include <pwd.h>
66 1.1 cgd #include <grp.h>
67 1.1 cgd #include <ctype.h>
68 1.15 lukem #include <signal.h>
69 1.37 dholland #include <stdbool.h>
70 1.1 cgd #include <stdio.h>
71 1.9 cgd #include <stdlib.h>
72 1.1 cgd #include <string.h>
73 1.5 mycroft #include <unistd.h>
74 1.47 christos #include <util.h>
75 1.30 bouyer
76 1.31 christos #include "printquota.h"
77 1.30 bouyer
78 1.1 cgd #include "pathnames.h"
79 1.1 cgd
80 1.46 dholland /*
81 1.46 dholland * XXX. Ideally we shouldn't compile this in, but it'll take some
82 1.46 dholland * reworking to avoid it and it'll be ok for now.
83 1.46 dholland */
84 1.46 dholland #define EDQUOTA_NUMOBJTYPES 2
85 1.46 dholland
86 1.45 dholland #if 0
87 1.31 christos static const char *quotagroup = QUOTAGROUP;
88 1.45 dholland #endif
89 1.1 cgd
90 1.37 dholland #define MAX_TMPSTR (100+MAXPATHLEN)
91 1.37 dholland
92 1.50 dholland enum sources {
93 1.50 dholland SRC_EDITED, /* values came from user */
94 1.50 dholland SRC_QUOTA, /* values came from a specific quota entry */
95 1.50 dholland SRC_DEFAULT, /* values were copied from the default quota entry */
96 1.50 dholland SRC_CLEAR, /* values arose by zeroing out a quota entry */
97 1.50 dholland };
98 1.50 dholland
99 1.1 cgd struct quotause {
100 1.1 cgd struct quotause *next;
101 1.48 dholland unsigned found:1, /* found after running editor */
102 1.48 dholland xgrace:1, /* grace periods are per-id */
103 1.48 dholland isdefault:1;
104 1.48 dholland
105 1.46 dholland struct quotaval qv[EDQUOTA_NUMOBJTYPES];
106 1.50 dholland enum sources source[EDQUOTA_NUMOBJTYPES];
107 1.1 cgd char fsname[MAXPATHLEN + 1];
108 1.44 dholland char implementation[32];
109 1.15 lukem };
110 1.13 mikel
111 1.37 dholland struct quotalist {
112 1.37 dholland struct quotause *head;
113 1.37 dholland struct quotause *tail;
114 1.46 dholland char *idtypename;
115 1.37 dholland };
116 1.23 jonb
117 1.33 dholland static void usage(void) __dead;
118 1.30 bouyer
119 1.31 christos static int Hflag = 0;
120 1.1 cgd
121 1.32 bouyer /* more compact form of constants */
122 1.46 dholland #define QO_BLK QUOTA_OBJTYPE_BLOCKS
123 1.46 dholland #define QO_FL QUOTA_OBJTYPE_FILES
124 1.32 bouyer
125 1.34 dholland ////////////////////////////////////////////////////////////
126 1.34 dholland // support code
127 1.1 cgd
128 1.1 cgd /*
129 1.35 dholland * This routine converts a name for a particular quota class to
130 1.1 cgd * an identifier. This routine must agree with the kernel routine
131 1.35 dholland * getinoquota as to the interpretation of quota classes.
132 1.1 cgd */
133 1.31 christos static int
134 1.41 dholland getidbyname(const char *name, int idtype)
135 1.1 cgd {
136 1.1 cgd struct passwd *pw;
137 1.1 cgd struct group *gr;
138 1.1 cgd
139 1.1 cgd if (alldigits(name))
140 1.31 christos return atoi(name);
141 1.41 dholland switch (idtype) {
142 1.41 dholland case QUOTA_IDTYPE_USER:
143 1.15 lukem if ((pw = getpwnam(name)) != NULL)
144 1.31 christos return pw->pw_uid;
145 1.15 lukem warnx("%s: no such user", name);
146 1.1 cgd break;
147 1.41 dholland case QUOTA_IDTYPE_GROUP:
148 1.15 lukem if ((gr = getgrnam(name)) != NULL)
149 1.31 christos return gr->gr_gid;
150 1.15 lukem warnx("%s: no such group", name);
151 1.1 cgd break;
152 1.1 cgd default:
153 1.41 dholland warnx("%d: unknown quota type", idtype);
154 1.1 cgd break;
155 1.1 cgd }
156 1.1 cgd sleep(1);
157 1.31 christos return -1;
158 1.1 cgd }
159 1.1 cgd
160 1.50 dholland /*
161 1.50 dholland * check if a source is "real" (reflects actual data) or not
162 1.50 dholland */
163 1.50 dholland static bool
164 1.50 dholland source_is_real(enum sources source)
165 1.50 dholland {
166 1.50 dholland switch (source) {
167 1.50 dholland case SRC_EDITED:
168 1.50 dholland case SRC_QUOTA:
169 1.50 dholland return true;
170 1.50 dholland case SRC_DEFAULT:
171 1.50 dholland case SRC_CLEAR:
172 1.50 dholland return false;
173 1.50 dholland }
174 1.50 dholland assert(!"encountered invalid source");
175 1.50 dholland return false;
176 1.50 dholland }
177 1.50 dholland
178 1.50 dholland /*
179 1.50 dholland * some simple string tools
180 1.50 dholland */
181 1.50 dholland
182 1.50 dholland static /*const*/ char *
183 1.50 dholland skipws(/*const*/ char *s)
184 1.50 dholland {
185 1.50 dholland while (*s == ' ' || *s == '\t') {
186 1.50 dholland s++;
187 1.50 dholland }
188 1.50 dholland return s;
189 1.50 dholland }
190 1.50 dholland
191 1.50 dholland static /*const*/ char *
192 1.50 dholland skipword(/*const*/ char *s)
193 1.50 dholland {
194 1.50 dholland while (*s != '\0' && *s != '\n' && *s != ' ' && *s != '\t') {
195 1.50 dholland s++;
196 1.50 dholland }
197 1.50 dholland return s;
198 1.50 dholland }
199 1.50 dholland
200 1.34 dholland ////////////////////////////////////////////////////////////
201 1.34 dholland // quotause operations
202 1.34 dholland
203 1.34 dholland /*
204 1.37 dholland * Create an empty quotause structure.
205 1.37 dholland */
206 1.37 dholland static struct quotause *
207 1.37 dholland quotause_create(void)
208 1.37 dholland {
209 1.37 dholland struct quotause *qup;
210 1.50 dholland unsigned i;
211 1.37 dholland
212 1.37 dholland qup = malloc(sizeof(*qup));
213 1.37 dholland if (qup == NULL) {
214 1.37 dholland err(1, "malloc");
215 1.37 dholland }
216 1.37 dholland
217 1.37 dholland qup->next = NULL;
218 1.48 dholland qup->found = 0;
219 1.48 dholland qup->xgrace = 0;
220 1.48 dholland qup->isdefault = 0;
221 1.50 dholland for (i=0; i<EDQUOTA_NUMOBJTYPES; i++) {
222 1.50 dholland quotaval_clear(&qup->qv[i]);
223 1.50 dholland qup->source[i] = SRC_CLEAR;
224 1.50 dholland }
225 1.37 dholland qup->fsname[0] = '\0';
226 1.37 dholland
227 1.37 dholland return qup;
228 1.37 dholland }
229 1.37 dholland
230 1.37 dholland /*
231 1.34 dholland * Free a quotause structure.
232 1.34 dholland */
233 1.34 dholland static void
234 1.35 dholland quotause_destroy(struct quotause *qup)
235 1.34 dholland {
236 1.34 dholland free(qup);
237 1.34 dholland }
238 1.34 dholland
239 1.37 dholland ////////////////////////////////////////////////////////////
240 1.37 dholland // quotalist operations
241 1.37 dholland
242 1.37 dholland /*
243 1.37 dholland * Create a quotause list.
244 1.37 dholland */
245 1.37 dholland static struct quotalist *
246 1.37 dholland quotalist_create(void)
247 1.37 dholland {
248 1.37 dholland struct quotalist *qlist;
249 1.37 dholland
250 1.37 dholland qlist = malloc(sizeof(*qlist));
251 1.37 dholland if (qlist == NULL) {
252 1.37 dholland err(1, "malloc");
253 1.37 dholland }
254 1.37 dholland
255 1.37 dholland qlist->head = NULL;
256 1.37 dholland qlist->tail = NULL;
257 1.46 dholland qlist->idtypename = NULL;
258 1.37 dholland
259 1.37 dholland return qlist;
260 1.37 dholland }
261 1.37 dholland
262 1.1 cgd /*
263 1.34 dholland * Free a list of quotause structures.
264 1.1 cgd */
265 1.34 dholland static void
266 1.37 dholland quotalist_destroy(struct quotalist *qlist)
267 1.1 cgd {
268 1.34 dholland struct quotause *qup, *nextqup;
269 1.34 dholland
270 1.37 dholland for (qup = qlist->head; qup; qup = nextqup) {
271 1.34 dholland nextqup = qup->next;
272 1.35 dholland quotause_destroy(qup);
273 1.34 dholland }
274 1.46 dholland free(qlist->idtypename);
275 1.37 dholland free(qlist);
276 1.37 dholland }
277 1.37 dholland
278 1.45 dholland #if 0
279 1.37 dholland static bool
280 1.37 dholland quotalist_empty(struct quotalist *qlist)
281 1.37 dholland {
282 1.37 dholland return qlist->head == NULL;
283 1.37 dholland }
284 1.45 dholland #endif
285 1.37 dholland
286 1.37 dholland static void
287 1.37 dholland quotalist_append(struct quotalist *qlist, struct quotause *qup)
288 1.37 dholland {
289 1.37 dholland /* should not already be on a list */
290 1.37 dholland assert(qup->next == NULL);
291 1.37 dholland
292 1.37 dholland if (qlist->head == NULL) {
293 1.37 dholland qlist->head = qup;
294 1.37 dholland } else {
295 1.37 dholland qlist->tail->next = qup;
296 1.37 dholland }
297 1.37 dholland qlist->tail = qup;
298 1.34 dholland }
299 1.30 bouyer
300 1.34 dholland ////////////////////////////////////////////////////////////
301 1.34 dholland // ffs quota v1
302 1.1 cgd
303 1.45 dholland #if 0
304 1.34 dholland static void
305 1.41 dholland putprivs1(uint32_t id, int idtype, struct quotause *qup)
306 1.34 dholland {
307 1.34 dholland struct dqblk dqblk;
308 1.34 dholland int fd;
309 1.1 cgd
310 1.42 dholland quotavals_to_dqblk(&qup->qv[QUOTA_LIMIT_BLOCK],
311 1.42 dholland &qup->qv[QUOTA_LIMIT_FILE],
312 1.42 dholland &dqblk);
313 1.34 dholland assert((qup->flags & DEFAULT) == 0);
314 1.30 bouyer
315 1.34 dholland if ((fd = open(qup->qfname, O_WRONLY)) < 0) {
316 1.34 dholland warnx("open `%s'", qup->qfname);
317 1.34 dholland } else {
318 1.34 dholland (void)lseek(fd,
319 1.34 dholland (off_t)(id * (long)sizeof (struct dqblk)),
320 1.34 dholland SEEK_SET);
321 1.34 dholland if (write(fd, &dqblk, sizeof (struct dqblk)) !=
322 1.34 dholland sizeof (struct dqblk))
323 1.34 dholland warnx("writing `%s'", qup->qfname);
324 1.34 dholland close(fd);
325 1.30 bouyer }
326 1.30 bouyer }
327 1.30 bouyer
328 1.31 christos static struct quotause *
329 1.41 dholland getprivs1(long id, int idtype, const char *filesys)
330 1.30 bouyer {
331 1.30 bouyer struct fstab *fs;
332 1.47 christos char qfpathname[MAXPATHLEN], xbuf[MAXPATHLEN];
333 1.47 christos const char *fsspec;
334 1.30 bouyer struct quotause *qup;
335 1.30 bouyer struct dqblk dqblk;
336 1.30 bouyer int fd;
337 1.30 bouyer
338 1.30 bouyer setfsent();
339 1.30 bouyer while ((fs = getfsent()) != NULL) {
340 1.30 bouyer if (strcmp(fs->fs_vfstype, "ffs"))
341 1.30 bouyer continue;
342 1.47 christos fsspec = getfsspecname(xbuf, sizeof(xbuf), fs->fs_spec);
343 1.47 christos if (fsspec == NULL) {
344 1.47 christos warn("%s", xbuf);
345 1.47 christos continue;
346 1.47 christos }
347 1.47 christos if (strcmp(fsspec, filesys) == 0 ||
348 1.30 bouyer strcmp(fs->fs_file, filesys) == 0)
349 1.30 bouyer break;
350 1.30 bouyer }
351 1.30 bouyer if (fs == NULL)
352 1.30 bouyer return NULL;
353 1.30 bouyer
354 1.32 bouyer if (!hasquota(qfpathname, sizeof(qfpathname), fs,
355 1.43 dholland quota_idtype_to_ufs(idtype)))
356 1.30 bouyer return NULL;
357 1.37 dholland
358 1.37 dholland qup = quotause_create();
359 1.30 bouyer strcpy(qup->fsname, fs->fs_file);
360 1.30 bouyer if ((fd = open(qfpathname, O_RDONLY)) < 0) {
361 1.30 bouyer fd = open(qfpathname, O_RDWR|O_CREAT, 0640);
362 1.30 bouyer if (fd < 0 && errno != ENOENT) {
363 1.30 bouyer warnx("open `%s'", qfpathname);
364 1.35 dholland quotause_destroy(qup);
365 1.30 bouyer return NULL;
366 1.30 bouyer }
367 1.30 bouyer warnx("Creating quota file %s", qfpathname);
368 1.30 bouyer sleep(3);
369 1.32 bouyer (void)fchown(fd, getuid(),
370 1.35 dholland getidbyname(quotagroup, QUOTA_CLASS_GROUP));
371 1.31 christos (void)fchmod(fd, 0640);
372 1.30 bouyer }
373 1.30 bouyer (void)lseek(fd, (off_t)(id * sizeof(struct dqblk)),
374 1.30 bouyer SEEK_SET);
375 1.30 bouyer switch (read(fd, &dqblk, sizeof(struct dqblk))) {
376 1.30 bouyer case 0: /* EOF */
377 1.30 bouyer /*
378 1.30 bouyer * Convert implicit 0 quota (EOF)
379 1.30 bouyer * into an explicit one (zero'ed dqblk)
380 1.30 bouyer */
381 1.31 christos memset(&dqblk, 0, sizeof(struct dqblk));
382 1.30 bouyer break;
383 1.30 bouyer
384 1.30 bouyer case sizeof(struct dqblk): /* OK */
385 1.30 bouyer break;
386 1.30 bouyer
387 1.30 bouyer default: /* ERROR */
388 1.30 bouyer warn("read error in `%s'", qfpathname);
389 1.30 bouyer close(fd);
390 1.35 dholland quotause_destroy(qup);
391 1.30 bouyer return NULL;
392 1.30 bouyer }
393 1.30 bouyer close(fd);
394 1.30 bouyer qup->qfname = qfpathname;
395 1.1 cgd endfsent();
396 1.42 dholland dqblk_to_quotavals(&dqblk,
397 1.42 dholland &qup->qv[QUOTA_LIMIT_BLOCK],
398 1.42 dholland &qup->qv[QUOTA_LIMIT_FILE]);
399 1.31 christos return qup;
400 1.1 cgd }
401 1.45 dholland #endif
402 1.1 cgd
403 1.34 dholland ////////////////////////////////////////////////////////////
404 1.48 dholland // generic quota interface
405 1.34 dholland
406 1.44 dholland static int
407 1.44 dholland dogetprivs2(struct quotahandle *qh, int idtype, id_t id, int defaultq,
408 1.44 dholland int objtype, struct quotause *qup)
409 1.44 dholland {
410 1.44 dholland struct quotakey qk;
411 1.44 dholland
412 1.44 dholland qk.qk_idtype = idtype;
413 1.44 dholland qk.qk_id = defaultq ? QUOTA_DEFAULTID : id;
414 1.44 dholland qk.qk_objtype = objtype;
415 1.50 dholland if (quota_get(qh, &qk, &qup->qv[objtype]) == 0) {
416 1.50 dholland /* succeeded */
417 1.50 dholland qup->source[objtype] = SRC_QUOTA;
418 1.50 dholland return 0;
419 1.50 dholland }
420 1.50 dholland if (errno != ENOENT) {
421 1.50 dholland /* serious failure */
422 1.50 dholland return -1;
423 1.50 dholland }
424 1.50 dholland
425 1.50 dholland /* no entry, get default entry */
426 1.50 dholland qk.qk_id = QUOTA_DEFAULTID;
427 1.50 dholland if (quota_get(qh, &qk, &qup->qv[objtype]) == 0) {
428 1.50 dholland /* succeeded */
429 1.50 dholland qup->source[objtype] = SRC_DEFAULT;
430 1.50 dholland return 0;
431 1.50 dholland }
432 1.50 dholland if (errno != ENOENT) {
433 1.50 dholland return -1;
434 1.44 dholland }
435 1.50 dholland
436 1.50 dholland /* use a zeroed-out entry */
437 1.50 dholland quotaval_clear(&qup->qv[objtype]);
438 1.50 dholland qup->source[objtype] = SRC_CLEAR;
439 1.44 dholland return 0;
440 1.44 dholland }
441 1.44 dholland
442 1.34 dholland static struct quotause *
443 1.46 dholland getprivs2(long id, int idtype, const char *filesys, int defaultq,
444 1.46 dholland char **idtypename_p)
445 1.1 cgd {
446 1.15 lukem struct quotause *qup;
447 1.44 dholland struct quotahandle *qh;
448 1.44 dholland const char *impl;
449 1.44 dholland unsigned restrictions;
450 1.46 dholland const char *idtypename;
451 1.51 dholland int serrno;
452 1.1 cgd
453 1.37 dholland qup = quotause_create();
454 1.34 dholland strcpy(qup->fsname, filesys);
455 1.34 dholland if (defaultq)
456 1.48 dholland qup->isdefault = 1;
457 1.44 dholland
458 1.44 dholland qh = quota_open(filesys);
459 1.44 dholland if (qh == NULL) {
460 1.51 dholland serrno = errno;
461 1.44 dholland quotause_destroy(qup);
462 1.51 dholland errno = serrno;
463 1.44 dholland return NULL;
464 1.44 dholland }
465 1.44 dholland
466 1.44 dholland impl = quota_getimplname(qh);
467 1.44 dholland if (impl == NULL) {
468 1.44 dholland impl = "???";
469 1.44 dholland }
470 1.44 dholland strlcpy(qup->implementation, impl, sizeof(qup->implementation));
471 1.44 dholland
472 1.44 dholland restrictions = quota_getrestrictions(qh);
473 1.44 dholland if ((restrictions & QUOTA_RESTRICT_UNIFORMGRACE) == 0) {
474 1.48 dholland qup->xgrace = 1;
475 1.44 dholland }
476 1.44 dholland
477 1.46 dholland if (*idtypename_p == NULL) {
478 1.46 dholland idtypename = quota_idtype_getname(qh, idtype);
479 1.46 dholland *idtypename_p = strdup(idtypename);
480 1.46 dholland if (*idtypename_p == NULL) {
481 1.46 dholland errx(1, "Out of memory");
482 1.46 dholland }
483 1.46 dholland }
484 1.46 dholland
485 1.44 dholland if (dogetprivs2(qh, idtype, id, defaultq, QUOTA_OBJTYPE_BLOCKS, qup)) {
486 1.51 dholland serrno = errno;
487 1.44 dholland quota_close(qh);
488 1.44 dholland quotause_destroy(qup);
489 1.51 dholland errno = serrno;
490 1.44 dholland return NULL;
491 1.44 dholland }
492 1.44 dholland
493 1.44 dholland if (dogetprivs2(qh, idtype, id, defaultq, QUOTA_OBJTYPE_FILES, qup)) {
494 1.51 dholland serrno = errno;
495 1.44 dholland quota_close(qh);
496 1.44 dholland quotause_destroy(qup);
497 1.51 dholland errno = serrno;
498 1.44 dholland return NULL;
499 1.30 bouyer }
500 1.44 dholland
501 1.44 dholland quota_close(qh);
502 1.44 dholland
503 1.34 dholland return qup;
504 1.30 bouyer }
505 1.30 bouyer
506 1.31 christos static void
507 1.41 dholland putprivs2(uint32_t id, int idtype, struct quotause *qup)
508 1.30 bouyer {
509 1.40 dholland struct quotahandle *qh;
510 1.40 dholland struct quotakey qk;
511 1.40 dholland char idname[32];
512 1.40 dholland
513 1.48 dholland if (qup->isdefault) {
514 1.40 dholland snprintf(idname, sizeof(idname), "%s default",
515 1.41 dholland idtype == QUOTA_IDTYPE_USER ? "user" : "group");
516 1.40 dholland id = QUOTA_DEFAULTID;
517 1.40 dholland } else {
518 1.40 dholland snprintf(idname, sizeof(idname), "%s %u",
519 1.41 dholland idtype == QUOTA_IDTYPE_USER ? "uid" : "gid", id);
520 1.40 dholland }
521 1.40 dholland
522 1.40 dholland qh = quota_open(qup->fsname);
523 1.40 dholland if (qh == NULL) {
524 1.40 dholland err(1, "%s: quota_open", qup->fsname);
525 1.40 dholland }
526 1.40 dholland
527 1.50 dholland if (source_is_real(qup->source[QUOTA_OBJTYPE_BLOCKS])) {
528 1.50 dholland qk.qk_idtype = idtype;
529 1.50 dholland qk.qk_id = id;
530 1.50 dholland qk.qk_objtype = QUOTA_OBJTYPE_BLOCKS;
531 1.50 dholland if (quota_put(qh, &qk, &qup->qv[QO_BLK])) {
532 1.50 dholland err(1, "%s: quota_put (%s blocks)", qup->fsname,
533 1.50 dholland idname);
534 1.50 dholland }
535 1.40 dholland }
536 1.30 bouyer
537 1.50 dholland if (source_is_real(qup->source[QUOTA_OBJTYPE_FILES])) {
538 1.50 dholland qk.qk_idtype = idtype;
539 1.50 dholland qk.qk_id = id;
540 1.50 dholland qk.qk_objtype = QUOTA_OBJTYPE_FILES;
541 1.50 dholland if (quota_put(qh, &qk, &qup->qv[QO_FL])) {
542 1.50 dholland err(1, "%s: quota_put (%s files)", qup->fsname,
543 1.50 dholland idname);
544 1.50 dholland }
545 1.30 bouyer }
546 1.40 dholland
547 1.40 dholland quota_close(qh);
548 1.30 bouyer }
549 1.30 bouyer
550 1.34 dholland ////////////////////////////////////////////////////////////
551 1.34 dholland // quota format switch
552 1.34 dholland
553 1.34 dholland /*
554 1.34 dholland * Collect the requested quota information.
555 1.34 dholland */
556 1.37 dholland static struct quotalist *
557 1.41 dholland getprivs(long id, int defaultq, int idtype, const char *filesys)
558 1.30 bouyer {
559 1.34 dholland struct statvfs *fst;
560 1.34 dholland int nfst, i;
561 1.37 dholland struct quotalist *qlist;
562 1.37 dholland struct quotause *qup;
563 1.49 dholland int seenany = 0;
564 1.37 dholland
565 1.37 dholland qlist = quotalist_create();
566 1.34 dholland
567 1.34 dholland nfst = getmntinfo(&fst, MNT_WAIT);
568 1.34 dholland if (nfst == 0)
569 1.34 dholland errx(1, "no filesystems mounted!");
570 1.30 bouyer
571 1.34 dholland for (i = 0; i < nfst; i++) {
572 1.34 dholland if ((fst[i].f_flag & ST_QUOTA) == 0)
573 1.34 dholland continue;
574 1.49 dholland seenany = 1;
575 1.37 dholland if (filesys &&
576 1.37 dholland strcmp(fst[i].f_mntonname, filesys) != 0 &&
577 1.34 dholland strcmp(fst[i].f_mntfromname, filesys) != 0)
578 1.34 dholland continue;
579 1.46 dholland qup = getprivs2(id, idtype, fst[i].f_mntonname, defaultq,
580 1.46 dholland &qlist->idtypename);
581 1.37 dholland if (qup == NULL) {
582 1.51 dholland warn("Reading quotas failed for id %ld", id);
583 1.37 dholland continue;
584 1.37 dholland }
585 1.37 dholland
586 1.37 dholland quotalist_append(qlist, qup);
587 1.34 dholland }
588 1.30 bouyer
589 1.49 dholland if (!seenany) {
590 1.49 dholland errx(1, "No mounted filesystems have quota support");
591 1.49 dholland }
592 1.49 dholland
593 1.45 dholland #if 0
594 1.37 dholland if (filesys && quotalist_empty(qlist)) {
595 1.34 dholland if (defaultq)
596 1.34 dholland errx(1, "no default quota for version 1");
597 1.34 dholland /* if we get there, filesys is not mounted. try the old way */
598 1.41 dholland qup = getprivs1(id, idtype, filesys);
599 1.37 dholland if (qup == NULL) {
600 1.50 dholland warnx("getprivs1 failed");
601 1.37 dholland return qlist;
602 1.37 dholland }
603 1.37 dholland quotalist_append(qlist, qup);
604 1.1 cgd }
605 1.45 dholland #endif
606 1.37 dholland return qlist;
607 1.1 cgd }
608 1.1 cgd
609 1.1 cgd /*
610 1.34 dholland * Store the requested quota information.
611 1.1 cgd */
612 1.37 dholland static void
613 1.41 dholland putprivs(uint32_t id, int idtype, struct quotalist *qlist)
614 1.1 cgd {
615 1.34 dholland struct quotause *qup;
616 1.1 cgd
617 1.37 dholland for (qup = qlist->head; qup; qup = qup->next) {
618 1.45 dholland #if 0
619 1.48 dholland if (qup->qfname != NULL)
620 1.48 dholland putprivs1(id, idtype, qup);
621 1.34 dholland else
622 1.45 dholland #endif
623 1.48 dholland putprivs2(id, idtype, qup);
624 1.1 cgd }
625 1.1 cgd }
626 1.1 cgd
627 1.34 dholland static void
628 1.41 dholland clearpriv(int argc, char **argv, const char *filesys, int idtype)
629 1.1 cgd {
630 1.34 dholland struct statvfs *fst;
631 1.34 dholland int nfst, i;
632 1.34 dholland int id;
633 1.40 dholland id_t *ids;
634 1.40 dholland unsigned nids, maxids, j;
635 1.40 dholland struct quotahandle *qh;
636 1.40 dholland struct quotakey qk;
637 1.40 dholland char idname[32];
638 1.40 dholland
639 1.40 dholland maxids = 4;
640 1.40 dholland nids = 0;
641 1.40 dholland ids = malloc(maxids * sizeof(ids[0]));
642 1.40 dholland if (ids == NULL) {
643 1.40 dholland err(1, "malloc");
644 1.34 dholland }
645 1.34 dholland
646 1.34 dholland for ( ; argc > 0; argc--, argv++) {
647 1.41 dholland if ((id = getidbyname(*argv, idtype)) == -1)
648 1.34 dholland continue;
649 1.34 dholland
650 1.40 dholland if (nids + 1 > maxids) {
651 1.40 dholland maxids *= 2;
652 1.40 dholland ids = realloc(ids, maxids * sizeof(ids[0]));
653 1.40 dholland if (ids == NULL) {
654 1.40 dholland err(1, "realloc");
655 1.40 dholland }
656 1.40 dholland }
657 1.40 dholland ids[nids++] = id;
658 1.40 dholland }
659 1.34 dholland
660 1.34 dholland /* now loop over quota-enabled filesystems */
661 1.34 dholland nfst = getmntinfo(&fst, MNT_WAIT);
662 1.34 dholland if (nfst == 0)
663 1.34 dholland errx(1, "no filesystems mounted!");
664 1.34 dholland
665 1.34 dholland for (i = 0; i < nfst; i++) {
666 1.34 dholland if ((fst[i].f_flag & ST_QUOTA) == 0)
667 1.34 dholland continue;
668 1.34 dholland if (filesys && strcmp(fst[i].f_mntonname, filesys) != 0 &&
669 1.34 dholland strcmp(fst[i].f_mntfromname, filesys) != 0)
670 1.34 dholland continue;
671 1.34 dholland
672 1.40 dholland qh = quota_open(fst[i].f_mntonname);
673 1.40 dholland if (qh == NULL) {
674 1.40 dholland err(1, "%s: quota_open", fst[i].f_mntonname);
675 1.40 dholland }
676 1.40 dholland
677 1.40 dholland for (j = 0; j < nids; j++) {
678 1.40 dholland snprintf(idname, sizeof(idname), "%s %u",
679 1.41 dholland idtype == QUOTA_IDTYPE_USER ?
680 1.40 dholland "uid" : "gid", ids[j]);
681 1.40 dholland
682 1.41 dholland qk.qk_idtype = idtype;
683 1.40 dholland qk.qk_id = ids[j];
684 1.40 dholland qk.qk_objtype = QUOTA_OBJTYPE_BLOCKS;
685 1.40 dholland if (quota_delete(qh, &qk)) {
686 1.40 dholland err(1, "%s: quota_delete (%s blocks)",
687 1.40 dholland fst[i].f_mntonname, idname);
688 1.40 dholland }
689 1.34 dholland
690 1.41 dholland qk.qk_idtype = idtype;
691 1.40 dholland qk.qk_id = ids[j];
692 1.40 dholland qk.qk_objtype = QUOTA_OBJTYPE_FILES;
693 1.40 dholland if (quota_delete(qh, &qk)) {
694 1.40 dholland if (errno == ENOENT) {
695 1.40 dholland /*
696 1.40 dholland * XXX ignore this case; due
697 1.40 dholland * to a weakness in the quota
698 1.40 dholland * proplib interface it can
699 1.40 dholland * appear spuriously.
700 1.40 dholland */
701 1.40 dholland } else {
702 1.40 dholland err(1, "%s: quota_delete (%s files)",
703 1.40 dholland fst[i].f_mntonname, idname);
704 1.40 dholland }
705 1.40 dholland }
706 1.34 dholland }
707 1.34 dholland
708 1.40 dholland quota_close(qh);
709 1.40 dholland }
710 1.34 dholland
711 1.40 dholland free(ids);
712 1.34 dholland }
713 1.34 dholland
714 1.34 dholland ////////////////////////////////////////////////////////////
715 1.34 dholland // editor
716 1.34 dholland
717 1.34 dholland /*
718 1.34 dholland * Take a list of privileges and get it edited.
719 1.34 dholland */
720 1.34 dholland static int
721 1.34 dholland editit(const char *ltmpfile)
722 1.34 dholland {
723 1.34 dholland pid_t pid;
724 1.34 dholland int lst;
725 1.34 dholland char p[MAX_TMPSTR];
726 1.34 dholland const char *ed;
727 1.34 dholland sigset_t s, os;
728 1.34 dholland
729 1.34 dholland sigemptyset(&s);
730 1.34 dholland sigaddset(&s, SIGINT);
731 1.34 dholland sigaddset(&s, SIGQUIT);
732 1.34 dholland sigaddset(&s, SIGHUP);
733 1.34 dholland if (sigprocmask(SIG_BLOCK, &s, &os) == -1)
734 1.34 dholland err(1, "sigprocmask");
735 1.34 dholland top:
736 1.34 dholland switch ((pid = fork())) {
737 1.34 dholland case -1:
738 1.34 dholland if (errno == EPROCLIM) {
739 1.34 dholland warnx("You have too many processes");
740 1.34 dholland return 0;
741 1.34 dholland }
742 1.34 dholland if (errno == EAGAIN) {
743 1.34 dholland sleep(1);
744 1.34 dholland goto top;
745 1.34 dholland }
746 1.34 dholland warn("fork");
747 1.34 dholland return 0;
748 1.34 dholland case 0:
749 1.34 dholland if (sigprocmask(SIG_SETMASK, &os, NULL) == -1)
750 1.34 dholland err(1, "sigprocmask");
751 1.34 dholland setgid(getgid());
752 1.34 dholland setuid(getuid());
753 1.34 dholland if ((ed = getenv("EDITOR")) == (char *)0)
754 1.34 dholland ed = _PATH_VI;
755 1.34 dholland if (strlen(ed) + strlen(ltmpfile) + 2 >= MAX_TMPSTR) {
756 1.34 dholland errx(1, "%s", "editor or filename too long");
757 1.34 dholland }
758 1.34 dholland snprintf(p, sizeof(p), "%s %s", ed, ltmpfile);
759 1.34 dholland execlp(_PATH_BSHELL, _PATH_BSHELL, "-c", p, NULL);
760 1.34 dholland err(1, "%s", ed);
761 1.34 dholland default:
762 1.34 dholland if (waitpid(pid, &lst, 0) == -1)
763 1.34 dholland err(1, "waitpid");
764 1.34 dholland if (sigprocmask(SIG_SETMASK, &os, NULL) == -1)
765 1.34 dholland err(1, "sigprocmask");
766 1.34 dholland if (!WIFEXITED(lst) || WEXITSTATUS(lst) != 0)
767 1.34 dholland return 0;
768 1.34 dholland return 1;
769 1.34 dholland }
770 1.34 dholland }
771 1.34 dholland
772 1.34 dholland /*
773 1.34 dholland * Convert a quotause list to an ASCII file.
774 1.34 dholland */
775 1.34 dholland static int
776 1.37 dholland writeprivs(struct quotalist *qlist, int outfd, const char *name,
777 1.46 dholland int idtype, const char *idtypename)
778 1.34 dholland {
779 1.34 dholland struct quotause *qup;
780 1.34 dholland FILE *fd;
781 1.34 dholland char b0[32], b1[32], b2[32], b3[32];
782 1.50 dholland const char *comm;
783 1.34 dholland
784 1.34 dholland (void)ftruncate(outfd, 0);
785 1.34 dholland (void)lseek(outfd, (off_t)0, SEEK_SET);
786 1.34 dholland if ((fd = fdopen(dup(outfd), "w")) == NULL)
787 1.38 dholland errx(1, "fdopen");
788 1.38 dholland if (name == NULL) {
789 1.46 dholland fprintf(fd, "Default %s quotas:\n", idtypename);
790 1.34 dholland } else {
791 1.46 dholland fprintf(fd, "Quotas for %s %s:\n", idtypename, name);
792 1.30 bouyer }
793 1.37 dholland for (qup = qlist->head; qup; qup = qup->next) {
794 1.39 dholland struct quotaval *q = qup->qv;
795 1.50 dholland
796 1.50 dholland fprintf(fd, "%s (%s):\n", qup->fsname, qup->implementation);
797 1.50 dholland
798 1.50 dholland comm = source_is_real(qup->source[QO_BLK]) ? "" : "#";
799 1.51 dholland fprintf(fd, "\tblocks:%s\n",
800 1.51 dholland Hflag ? "" : " (sizes in 1K-blocks)");
801 1.50 dholland fprintf(fd, "\t\t%susage: %s\n", comm,
802 1.50 dholland intprt(b1, 21, q[QO_BLK].qv_usage,
803 1.50 dholland HN_NOSPACE | HN_B, Hflag));
804 1.50 dholland fprintf(fd, "\t\t%slimits: soft %s, hard %s\n", comm,
805 1.50 dholland intprt(b2, 21, q[QO_BLK].qv_softlimit,
806 1.50 dholland HN_NOSPACE | HN_B, Hflag),
807 1.50 dholland intprt(b3, 21, q[QO_BLK].qv_hardlimit,
808 1.50 dholland HN_NOSPACE | HN_B, Hflag));
809 1.48 dholland if (qup->xgrace || qup->isdefault) {
810 1.50 dholland fprintf(fd, "\t\t%sgrace: %s\n", comm,
811 1.50 dholland timepprt(b0, 21, q[QO_BLK].qv_grace, Hflag));
812 1.30 bouyer }
813 1.30 bouyer
814 1.50 dholland comm = source_is_real(qup->source[QO_FL]) ? "" : "#";
815 1.50 dholland fprintf(fd, "\tinodes:\n");
816 1.50 dholland fprintf(fd, "\t\t%susage: %s\n", comm,
817 1.50 dholland intprt(b1, 21, q[QO_FL].qv_usage,
818 1.50 dholland HN_NOSPACE, Hflag));
819 1.50 dholland fprintf(fd, "\t\t%slimits: soft %s, hard %s\n", comm,
820 1.50 dholland intprt(b2, 21, q[QO_FL].qv_softlimit,
821 1.50 dholland HN_NOSPACE, Hflag),
822 1.50 dholland intprt(b3, 21, q[QO_FL].qv_hardlimit,
823 1.50 dholland HN_NOSPACE, Hflag));
824 1.48 dholland if (qup->xgrace || qup->isdefault) {
825 1.50 dholland fprintf(fd, "\t\t%sgrace: %s\n", comm,
826 1.50 dholland timepprt(b0, 21, q[QO_FL].qv_grace, Hflag));
827 1.30 bouyer }
828 1.1 cgd }
829 1.1 cgd fclose(fd);
830 1.31 christos return 1;
831 1.1 cgd }
832 1.1 cgd
833 1.1 cgd /*
834 1.1 cgd * Merge changes to an ASCII file into a quotause list.
835 1.1 cgd */
836 1.31 christos static int
837 1.38 dholland readprivs(struct quotalist *qlist, int infd, int dflag)
838 1.1 cgd {
839 1.50 dholland FILE *fd; /* file */
840 1.50 dholland unsigned lineno; /* line number in file */
841 1.50 dholland char line[BUFSIZ]; /* input buffer */
842 1.50 dholland size_t len; /* length of input buffer */
843 1.50 dholland bool seenheader; /* true if past the file header */
844 1.50 dholland struct quotause *qup; /* current filesystem */
845 1.50 dholland bool haveobjtype; /* true if objtype is valid */
846 1.50 dholland unsigned objtype; /* current object type */
847 1.50 dholland int objtypeflags; /* humanize flags */
848 1.50 dholland /* XXX make following const later (requires non-local cleanup) */
849 1.50 dholland /*const*/ char *text, *text2, *t; /* scratch values */
850 1.50 dholland char b0[32], b1[32];
851 1.50 dholland uint64_t soft, hard, current;
852 1.50 dholland time_t grace;
853 1.50 dholland struct quotaval *qv;
854 1.50 dholland
855 1.50 dholland lineno = 0;
856 1.50 dholland seenheader = false;
857 1.50 dholland qup = NULL;
858 1.50 dholland haveobjtype = false;
859 1.50 dholland objtype = QUOTA_OBJTYPE_BLOCKS; /* for gcc 4.5 */
860 1.50 dholland objtypeflags = HN_B;
861 1.1 cgd
862 1.14 kleink (void)lseek(infd, (off_t)0, SEEK_SET);
863 1.1 cgd fd = fdopen(dup(infd), "r");
864 1.1 cgd if (fd == NULL) {
865 1.15 lukem warn("Can't re-read temp file");
866 1.50 dholland return -1;
867 1.1 cgd }
868 1.50 dholland
869 1.1 cgd /*
870 1.50 dholland * Read back the same format we wrote.
871 1.1 cgd */
872 1.50 dholland
873 1.50 dholland while (fgets(line, sizeof(line), fd)) {
874 1.50 dholland lineno++;
875 1.50 dholland if (!seenheader) {
876 1.50 dholland if ((!strncmp(line, "Default ", 8) && dflag) ||
877 1.50 dholland (!strncmp(line, "Quotas for ", 11) && !dflag)) {
878 1.50 dholland /* ok. */
879 1.50 dholland seenheader = 1;
880 1.50 dholland continue;
881 1.30 bouyer }
882 1.50 dholland warnx("Header line missing");
883 1.50 dholland goto fail;
884 1.50 dholland }
885 1.50 dholland len = strlen(line);
886 1.50 dholland if (len == 0) {
887 1.50 dholland /* ? */
888 1.50 dholland continue;
889 1.50 dholland }
890 1.50 dholland if (line[len - 1] != '\n') {
891 1.50 dholland warnx("Line %u too long", lineno);
892 1.50 dholland goto fail;
893 1.50 dholland }
894 1.50 dholland line[--len] = '\0';
895 1.50 dholland if (line[len - 1] == '\r') {
896 1.50 dholland line[--len] = '\0';
897 1.50 dholland }
898 1.50 dholland if (len == 0) {
899 1.50 dholland /* blank line */
900 1.50 dholland continue;
901 1.50 dholland }
902 1.50 dholland
903 1.50 dholland /*
904 1.50 dholland * If the line has: it is:
905 1.50 dholland * two tabs values
906 1.50 dholland * one tab the next objtype
907 1.50 dholland * no tabs the next filesystem
908 1.50 dholland */
909 1.50 dholland if (line[0] == '\t' && line[1] == '\t') {
910 1.50 dholland if (qup == NULL) {
911 1.50 dholland warnx("Line %u: values with no filesystem",
912 1.50 dholland lineno);
913 1.50 dholland goto fail;
914 1.50 dholland }
915 1.50 dholland if (!haveobjtype) {
916 1.50 dholland warnx("Line %u: values with no object type",
917 1.50 dholland lineno);
918 1.50 dholland goto fail;
919 1.50 dholland }
920 1.50 dholland qv = &qup->qv[objtype];
921 1.50 dholland
922 1.50 dholland text = line + 2;
923 1.50 dholland if (*text == '#') {
924 1.50 dholland /* commented out; ignore */
925 1.50 dholland continue;
926 1.30 bouyer }
927 1.50 dholland else if (!strncmp(text, "usage:", 6)) {
928 1.50 dholland
929 1.50 dholland /* usage: %llu */
930 1.50 dholland text += 6;
931 1.50 dholland t = skipws(text);
932 1.50 dholland if (intrd(t, ¤t, objtypeflags) != 0) {
933 1.50 dholland warnx("Line %u: Bad number %s",
934 1.50 dholland lineno, t);
935 1.50 dholland goto fail;
936 1.50 dholland }
937 1.50 dholland
938 1.50 dholland /*
939 1.50 dholland * Because the humanization can lead
940 1.50 dholland * to roundoff, check if the two
941 1.50 dholland * values produce the same humanized
942 1.50 dholland * string, rather than if they're the
943 1.50 dholland * same number. Sigh.
944 1.50 dholland */
945 1.50 dholland intprt(b0, 21, current,
946 1.51 dholland HN_NOSPACE | objtypeflags, Hflag);
947 1.50 dholland intprt(b1, 21, qv->qv_usage,
948 1.51 dholland HN_NOSPACE | objtypeflags, Hflag);
949 1.50 dholland if (strcmp(b0, b1)) {
950 1.50 dholland warnx("Line %u: cannot change usage",
951 1.50 dholland lineno);
952 1.50 dholland }
953 1.50 dholland continue;
954 1.50 dholland
955 1.50 dholland } else if (!strncmp(text, "limits:", 7)) {
956 1.50 dholland
957 1.50 dholland /* limits: soft %llu, hard %llu */
958 1.50 dholland text += 7;
959 1.50 dholland text2 = strchr(text, ',');
960 1.50 dholland if (text2 == NULL) {
961 1.50 dholland warnx("Line %u: expected comma",
962 1.50 dholland lineno);
963 1.50 dholland goto fail;
964 1.50 dholland }
965 1.50 dholland *text2 = '\0';
966 1.50 dholland text2++;
967 1.50 dholland
968 1.50 dholland t = skipws(text);
969 1.50 dholland t = skipword(t);
970 1.50 dholland t = skipws(t);
971 1.50 dholland if (intrd(t, &soft, objtypeflags) != 0) {
972 1.50 dholland warnx("Line %u: Bad number %s",
973 1.50 dholland lineno, t);
974 1.50 dholland goto fail;
975 1.50 dholland }
976 1.50 dholland t = skipws(text2);
977 1.50 dholland t = skipword(t);
978 1.50 dholland t = skipws(t);
979 1.50 dholland if (intrd(t, &hard, objtypeflags) != 0) {
980 1.50 dholland warnx("Line %u: Bad number %s",
981 1.50 dholland lineno, t);
982 1.50 dholland goto fail;
983 1.30 bouyer }
984 1.50 dholland
985 1.50 dholland /*
986 1.50 dholland * Cause time limit to be reset when the quota
987 1.50 dholland * is next used if previously had no soft limit
988 1.50 dholland * or were under it, but now have a soft limit
989 1.50 dholland * and are over it.
990 1.50 dholland */
991 1.50 dholland if (qv->qv_usage && qv->qv_usage >= soft &&
992 1.50 dholland (qv->qv_softlimit == 0 ||
993 1.50 dholland qv->qv_usage < qv->qv_softlimit)) {
994 1.50 dholland qv->qv_expiretime = 0;
995 1.30 bouyer }
996 1.50 dholland if (soft != qv->qv_softlimit ||
997 1.50 dholland hard != qv->qv_hardlimit) {
998 1.50 dholland qv->qv_softlimit = soft;
999 1.50 dholland qv->qv_hardlimit = hard;
1000 1.50 dholland qup->source[objtype] = SRC_EDITED;
1001 1.30 bouyer }
1002 1.50 dholland qup->found = 1;
1003 1.50 dholland
1004 1.50 dholland } else if (!strncmp(text, "grace:", 6)) {
1005 1.30 bouyer
1006 1.50 dholland text += 6;
1007 1.50 dholland /* grace: %llu */
1008 1.50 dholland t = skipws(text);
1009 1.50 dholland if (timeprd(t, &grace) != 0) {
1010 1.50 dholland warnx("Line %u: Bad number %s",
1011 1.50 dholland lineno, t);
1012 1.50 dholland goto fail;
1013 1.30 bouyer }
1014 1.50 dholland if (qup->isdefault || qup->xgrace) {
1015 1.50 dholland if (grace != qv->qv_grace) {
1016 1.50 dholland qv->qv_grace = grace;
1017 1.50 dholland qup->source[objtype] =
1018 1.50 dholland SRC_EDITED;
1019 1.50 dholland }
1020 1.50 dholland qup->found = 1;
1021 1.50 dholland } else {
1022 1.50 dholland warnx("Line %u: Cannot set individual "
1023 1.50 dholland "grace time on this filesystem",
1024 1.50 dholland lineno);
1025 1.50 dholland goto fail;
1026 1.30 bouyer }
1027 1.50 dholland
1028 1.30 bouyer } else {
1029 1.50 dholland warnx("Line %u: Unknown/unexpected value line",
1030 1.50 dholland lineno);
1031 1.50 dholland goto fail;
1032 1.50 dholland }
1033 1.50 dholland } else if (line[0] == '\t') {
1034 1.50 dholland text = line + 1;
1035 1.50 dholland if (*text == '#') {
1036 1.50 dholland /* commented out; ignore */
1037 1.50 dholland continue;
1038 1.30 bouyer }
1039 1.50 dholland else if (!strncmp(text, "blocks:", 7)) {
1040 1.50 dholland objtype = QUOTA_OBJTYPE_BLOCKS;
1041 1.50 dholland objtypeflags = HN_B;
1042 1.50 dholland haveobjtype = true;
1043 1.50 dholland } else if (!strncmp(text, "inodes:", 7)) {
1044 1.50 dholland objtype = QUOTA_OBJTYPE_FILES;
1045 1.50 dholland objtypeflags = 0;
1046 1.50 dholland haveobjtype = true;
1047 1.50 dholland } else {
1048 1.50 dholland warnx("Line %u: Unknown/unexpected object "
1049 1.50 dholland "type (%s)", lineno, text);
1050 1.50 dholland goto fail;
1051 1.30 bouyer }
1052 1.50 dholland } else {
1053 1.50 dholland t = strchr(line, ' ');
1054 1.50 dholland if (t == NULL) {
1055 1.50 dholland t = strchr(line, ':');
1056 1.50 dholland if (t == NULL) {
1057 1.50 dholland t = line + len;
1058 1.30 bouyer }
1059 1.30 bouyer }
1060 1.50 dholland *t = '\0';
1061 1.50 dholland
1062 1.50 dholland if (*line == '#') {
1063 1.50 dholland /* commented out; ignore */
1064 1.30 bouyer continue;
1065 1.30 bouyer }
1066 1.30 bouyer
1067 1.50 dholland for (qup = qlist->head; qup; qup = qup->next) {
1068 1.50 dholland if (!strcmp(line, qup->fsname))
1069 1.50 dholland break;
1070 1.50 dholland }
1071 1.50 dholland if (qup == NULL) {
1072 1.50 dholland warnx("Line %u: Filesystem %s invalid or has "
1073 1.50 dholland "no quota support", lineno, line);
1074 1.50 dholland goto fail;
1075 1.30 bouyer }
1076 1.50 dholland haveobjtype = false;
1077 1.1 cgd }
1078 1.1 cgd }
1079 1.50 dholland
1080 1.1 cgd fclose(fd);
1081 1.50 dholland
1082 1.1 cgd /*
1083 1.50 dholland * Disable quotas for any filesystems that we didn't see,
1084 1.50 dholland * because they must have been deleted in the editor.
1085 1.50 dholland *
1086 1.50 dholland * XXX this should be improved so it results in
1087 1.50 dholland * quota_delete(), not just writing out a blank quotaval.
1088 1.1 cgd */
1089 1.37 dholland for (qup = qlist->head; qup; qup = qup->next) {
1090 1.48 dholland if (qup->found) {
1091 1.48 dholland qup->found = 0;
1092 1.1 cgd continue;
1093 1.1 cgd }
1094 1.50 dholland
1095 1.50 dholland if (source_is_real(qup->source[QUOTA_OBJTYPE_BLOCKS])) {
1096 1.50 dholland quotaval_clear(&qup->qv[QUOTA_OBJTYPE_BLOCKS]);
1097 1.50 dholland qup->source[QUOTA_OBJTYPE_BLOCKS] = SRC_EDITED;
1098 1.50 dholland }
1099 1.50 dholland
1100 1.50 dholland if (source_is_real(qup->source[QUOTA_OBJTYPE_FILES])) {
1101 1.50 dholland quotaval_clear(&qup->qv[QUOTA_OBJTYPE_FILES]);
1102 1.50 dholland qup->source[QUOTA_OBJTYPE_FILES] = SRC_EDITED;
1103 1.50 dholland }
1104 1.1 cgd }
1105 1.50 dholland return 0;
1106 1.50 dholland
1107 1.50 dholland fail:
1108 1.50 dholland sleep(3);
1109 1.50 dholland fclose(fd);
1110 1.50 dholland return -1;
1111 1.1 cgd }
1112 1.1 cgd
1113 1.34 dholland ////////////////////////////////////////////////////////////
1114 1.38 dholland // actions
1115 1.38 dholland
1116 1.38 dholland static void
1117 1.41 dholland replicate(const char *fs, int idtype, const char *protoname,
1118 1.38 dholland char **names, int numnames)
1119 1.38 dholland {
1120 1.38 dholland long protoid, id;
1121 1.38 dholland struct quotalist *protoprivs;
1122 1.38 dholland struct quotause *qup;
1123 1.38 dholland int i;
1124 1.38 dholland
1125 1.41 dholland if ((protoid = getidbyname(protoname, idtype)) == -1)
1126 1.38 dholland exit(1);
1127 1.41 dholland protoprivs = getprivs(protoid, 0, idtype, fs);
1128 1.38 dholland for (qup = protoprivs->head; qup; qup = qup->next) {
1129 1.46 dholland qup->qv[QO_BLK].qv_expiretime = 0;
1130 1.46 dholland qup->qv[QO_FL].qv_expiretime = 0;
1131 1.50 dholland qup->source[QO_BLK] = SRC_EDITED;
1132 1.50 dholland qup->source[QO_FL] = SRC_EDITED;
1133 1.38 dholland }
1134 1.38 dholland for (i=0; i<numnames; i++) {
1135 1.41 dholland id = getidbyname(names[i], idtype);
1136 1.41 dholland if (id == -1)
1137 1.38 dholland continue;
1138 1.41 dholland putprivs(id, idtype, protoprivs);
1139 1.38 dholland }
1140 1.38 dholland /* XXX */
1141 1.38 dholland /* quotalist_destroy(protoprivs); */
1142 1.38 dholland }
1143 1.38 dholland
1144 1.38 dholland static void
1145 1.41 dholland assign(const char *fs, int idtype,
1146 1.38 dholland char *soft, char *hard, char *grace,
1147 1.38 dholland char **names, int numnames)
1148 1.38 dholland {
1149 1.38 dholland struct quotalist *curprivs;
1150 1.38 dholland struct quotause *lqup;
1151 1.38 dholland u_int64_t softb, hardb, softi, hardi;
1152 1.38 dholland time_t graceb, gracei;
1153 1.38 dholland char *str;
1154 1.38 dholland long id;
1155 1.38 dholland int dflag;
1156 1.38 dholland int i;
1157 1.38 dholland
1158 1.38 dholland if (soft) {
1159 1.38 dholland str = strsep(&soft, "/");
1160 1.38 dholland if (str[0] == '\0' || soft == NULL || soft[0] == '\0')
1161 1.38 dholland usage();
1162 1.38 dholland
1163 1.38 dholland if (intrd(str, &softb, HN_B) != 0)
1164 1.38 dholland errx(1, "%s: bad number", str);
1165 1.38 dholland if (intrd(soft, &softi, 0) != 0)
1166 1.38 dholland errx(1, "%s: bad number", soft);
1167 1.38 dholland }
1168 1.38 dholland if (hard) {
1169 1.38 dholland str = strsep(&hard, "/");
1170 1.38 dholland if (str[0] == '\0' || hard == NULL || hard[0] == '\0')
1171 1.38 dholland usage();
1172 1.38 dholland
1173 1.38 dholland if (intrd(str, &hardb, HN_B) != 0)
1174 1.38 dholland errx(1, "%s: bad number", str);
1175 1.38 dholland if (intrd(hard, &hardi, 0) != 0)
1176 1.38 dholland errx(1, "%s: bad number", hard);
1177 1.38 dholland }
1178 1.38 dholland if (grace) {
1179 1.38 dholland str = strsep(&grace, "/");
1180 1.38 dholland if (str[0] == '\0' || grace == NULL || grace[0] == '\0')
1181 1.38 dholland usage();
1182 1.38 dholland
1183 1.38 dholland if (timeprd(str, &graceb) != 0)
1184 1.38 dholland errx(1, "%s: bad number", str);
1185 1.38 dholland if (timeprd(grace, &gracei) != 0)
1186 1.38 dholland errx(1, "%s: bad number", grace);
1187 1.38 dholland }
1188 1.38 dholland for (i=0; i<numnames; i++) {
1189 1.38 dholland if (names[i] == NULL) {
1190 1.38 dholland id = 0;
1191 1.38 dholland dflag = 1;
1192 1.38 dholland } else {
1193 1.41 dholland id = getidbyname(names[i], idtype);
1194 1.38 dholland if (id == -1)
1195 1.38 dholland continue;
1196 1.38 dholland dflag = 0;
1197 1.38 dholland }
1198 1.38 dholland
1199 1.41 dholland curprivs = getprivs(id, dflag, idtype, fs);
1200 1.38 dholland for (lqup = curprivs->head; lqup; lqup = lqup->next) {
1201 1.39 dholland struct quotaval *q = lqup->qv;
1202 1.38 dholland if (soft) {
1203 1.38 dholland if (!dflag && softb &&
1204 1.46 dholland q[QO_BLK].qv_usage >= softb &&
1205 1.46 dholland (q[QO_BLK].qv_softlimit == 0 ||
1206 1.46 dholland q[QO_BLK].qv_usage <
1207 1.46 dholland q[QO_BLK].qv_softlimit))
1208 1.46 dholland q[QO_BLK].qv_expiretime = 0;
1209 1.38 dholland if (!dflag && softi &&
1210 1.46 dholland q[QO_FL].qv_usage >= softb &&
1211 1.46 dholland (q[QO_FL].qv_softlimit == 0 ||
1212 1.46 dholland q[QO_FL].qv_usage <
1213 1.46 dholland q[QO_FL].qv_softlimit))
1214 1.46 dholland q[QO_FL].qv_expiretime = 0;
1215 1.46 dholland q[QO_BLK].qv_softlimit = softb;
1216 1.46 dholland q[QO_FL].qv_softlimit = softi;
1217 1.38 dholland }
1218 1.38 dholland if (hard) {
1219 1.46 dholland q[QO_BLK].qv_hardlimit = hardb;
1220 1.46 dholland q[QO_FL].qv_hardlimit = hardi;
1221 1.38 dholland }
1222 1.38 dholland if (grace) {
1223 1.46 dholland q[QO_BLK].qv_grace = graceb;
1224 1.46 dholland q[QO_FL].qv_grace = gracei;
1225 1.38 dholland }
1226 1.50 dholland lqup->source[QO_BLK] = SRC_EDITED;
1227 1.50 dholland lqup->source[QO_FL] = SRC_EDITED;
1228 1.38 dholland }
1229 1.41 dholland putprivs(id, idtype, curprivs);
1230 1.38 dholland quotalist_destroy(curprivs);
1231 1.38 dholland }
1232 1.38 dholland }
1233 1.38 dholland
1234 1.38 dholland static void
1235 1.41 dholland clear(const char *fs, int idtype, char **names, int numnames)
1236 1.38 dholland {
1237 1.41 dholland clearpriv(numnames, names, fs, idtype);
1238 1.38 dholland }
1239 1.38 dholland
1240 1.38 dholland static void
1241 1.41 dholland editone(const char *fs, int idtype, const char *name,
1242 1.38 dholland int tmpfd, const char *tmppath)
1243 1.38 dholland {
1244 1.38 dholland struct quotalist *curprivs;
1245 1.38 dholland long id;
1246 1.38 dholland int dflag;
1247 1.38 dholland
1248 1.38 dholland if (name == NULL) {
1249 1.38 dholland id = 0;
1250 1.38 dholland dflag = 1;
1251 1.38 dholland } else {
1252 1.41 dholland id = getidbyname(name, idtype);
1253 1.38 dholland if (id == -1)
1254 1.38 dholland return;
1255 1.38 dholland dflag = 0;
1256 1.38 dholland }
1257 1.41 dholland curprivs = getprivs(id, dflag, idtype, fs);
1258 1.38 dholland
1259 1.46 dholland if (writeprivs(curprivs, tmpfd, name, idtype,
1260 1.46 dholland curprivs->idtypename) == 0)
1261 1.38 dholland goto fail;
1262 1.38 dholland
1263 1.38 dholland if (editit(tmppath) == 0)
1264 1.38 dholland goto fail;
1265 1.38 dholland
1266 1.50 dholland if (readprivs(curprivs, tmpfd, dflag) < 0)
1267 1.38 dholland goto fail;
1268 1.38 dholland
1269 1.41 dholland putprivs(id, idtype, curprivs);
1270 1.38 dholland fail:
1271 1.38 dholland quotalist_destroy(curprivs);
1272 1.38 dholland }
1273 1.38 dholland
1274 1.38 dholland static void
1275 1.41 dholland edit(const char *fs, int idtype, char **names, int numnames)
1276 1.38 dholland {
1277 1.38 dholland char tmppath[] = _PATH_TMPFILE;
1278 1.38 dholland int tmpfd, i;
1279 1.38 dholland
1280 1.38 dholland tmpfd = mkstemp(tmppath);
1281 1.38 dholland fchown(tmpfd, getuid(), getgid());
1282 1.38 dholland
1283 1.38 dholland for (i=0; i<numnames; i++) {
1284 1.41 dholland editone(fs, idtype, names[i], tmpfd, tmppath);
1285 1.38 dholland }
1286 1.38 dholland
1287 1.38 dholland close(tmpfd);
1288 1.38 dholland unlink(tmppath);
1289 1.38 dholland }
1290 1.38 dholland
1291 1.38 dholland ////////////////////////////////////////////////////////////
1292 1.34 dholland // main
1293 1.1 cgd
1294 1.31 christos static void
1295 1.34 dholland usage(void)
1296 1.1 cgd {
1297 1.34 dholland const char *p = getprogname();
1298 1.34 dholland fprintf(stderr,
1299 1.34 dholland "Usage: %s [-D] [-H] [-u] [-p <username>] [-f <filesystem>] "
1300 1.34 dholland "-d | <username> ...\n"
1301 1.34 dholland "\t%s [-D] [-H] -g [-p <groupname>] [-f <filesystem>] "
1302 1.34 dholland "-d | <groupname> ...\n"
1303 1.34 dholland "\t%s [-D] [-u] [-f <filesystem>] [-s b#/i#] [-h b#/i#] [-t t#/t#] "
1304 1.34 dholland "-d | <username> ...\n"
1305 1.34 dholland "\t%s [-D] -g [-f <filesystem>] [-s b#/i#] [-h b#/i#] [-t t#/t#] "
1306 1.34 dholland "-d | <groupname> ...\n"
1307 1.34 dholland "\t%s [-D] [-H] [-u] -c [-f <filesystem>] username ...\n"
1308 1.34 dholland "\t%s [-D] [-H] -g -c [-f <filesystem>] groupname ...\n",
1309 1.34 dholland p, p, p, p, p, p);
1310 1.34 dholland exit(1);
1311 1.1 cgd }
1312 1.1 cgd
1313 1.34 dholland int
1314 1.34 dholland main(int argc, char *argv[])
1315 1.30 bouyer {
1316 1.41 dholland int idtype;
1317 1.34 dholland char *protoname;
1318 1.34 dholland char *soft = NULL, *hard = NULL, *grace = NULL;
1319 1.34 dholland char *fs = NULL;
1320 1.34 dholland int ch;
1321 1.34 dholland int pflag = 0;
1322 1.34 dholland int cflag = 0;
1323 1.38 dholland int dflag = 0;
1324 1.30 bouyer
1325 1.34 dholland if (argc < 2)
1326 1.34 dholland usage();
1327 1.34 dholland if (getuid())
1328 1.34 dholland errx(1, "permission denied");
1329 1.34 dholland protoname = NULL;
1330 1.41 dholland idtype = QUOTA_IDTYPE_USER;
1331 1.44 dholland while ((ch = getopt(argc, argv, "Hcdugp:s:h:t:f:")) != -1) {
1332 1.34 dholland switch(ch) {
1333 1.34 dholland case 'H':
1334 1.34 dholland Hflag++;
1335 1.34 dholland break;
1336 1.34 dholland case 'c':
1337 1.34 dholland cflag++;
1338 1.34 dholland break;
1339 1.34 dholland case 'd':
1340 1.34 dholland dflag++;
1341 1.34 dholland break;
1342 1.34 dholland case 'p':
1343 1.34 dholland protoname = optarg;
1344 1.34 dholland pflag++;
1345 1.34 dholland break;
1346 1.34 dholland case 'g':
1347 1.41 dholland idtype = QUOTA_IDTYPE_GROUP;
1348 1.34 dholland break;
1349 1.34 dholland case 'u':
1350 1.41 dholland idtype = QUOTA_IDTYPE_USER;
1351 1.34 dholland break;
1352 1.34 dholland case 's':
1353 1.34 dholland soft = optarg;
1354 1.34 dholland break;
1355 1.34 dholland case 'h':
1356 1.34 dholland hard = optarg;
1357 1.34 dholland break;
1358 1.34 dholland case 't':
1359 1.34 dholland grace = optarg;
1360 1.34 dholland break;
1361 1.34 dholland case 'f':
1362 1.34 dholland fs = optarg;
1363 1.34 dholland break;
1364 1.34 dholland default:
1365 1.34 dholland usage();
1366 1.34 dholland }
1367 1.30 bouyer }
1368 1.34 dholland argc -= optind;
1369 1.34 dholland argv += optind;
1370 1.30 bouyer
1371 1.34 dholland if (pflag) {
1372 1.34 dholland if (soft || hard || grace || dflag || cflag)
1373 1.34 dholland usage();
1374 1.41 dholland replicate(fs, idtype, protoname, argv, argc);
1375 1.38 dholland } else if (soft || hard || grace) {
1376 1.34 dholland if (cflag)
1377 1.34 dholland usage();
1378 1.34 dholland if (dflag) {
1379 1.38 dholland /* use argv[argc], which is null, to mean 'default' */
1380 1.38 dholland argc++;
1381 1.30 bouyer }
1382 1.41 dholland assign(fs, idtype, soft, hard, grace, argv, argc);
1383 1.38 dholland } else if (cflag) {
1384 1.34 dholland if (dflag)
1385 1.34 dholland usage();
1386 1.41 dholland clear(fs, idtype, argv, argc);
1387 1.38 dholland } else {
1388 1.38 dholland if (dflag) {
1389 1.38 dholland /* use argv[argc], which is null, to mean 'default' */
1390 1.38 dholland argc++;
1391 1.38 dholland }
1392 1.41 dholland edit(fs, idtype, argv, argc);
1393 1.34 dholland }
1394 1.34 dholland return 0;
1395 1.30 bouyer }
1396