edquota.c revision 1.39 1 /* $NetBSD: edquota.c,v 1.39 2011/11/25 16:55:05 dholland Exp $ */
2 /*
3 * Copyright (c) 1980, 1990, 1993
4 * The Regents of the University of California. All rights reserved.
5 *
6 * This code is derived from software contributed to Berkeley by
7 * Robert Elz at The University of Melbourne.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34 #include <sys/cdefs.h>
35 #ifndef lint
36 __COPYRIGHT("@(#) Copyright (c) 1980, 1990, 1993\
37 The Regents of the University of California. All rights reserved.");
38 #endif /* not lint */
39
40 #ifndef lint
41 #if 0
42 static char sccsid[] = "from: @(#)edquota.c 8.3 (Berkeley) 4/27/95";
43 #else
44 __RCSID("$NetBSD: edquota.c,v 1.39 2011/11/25 16:55:05 dholland Exp $");
45 #endif
46 #endif /* not lint */
47
48 /*
49 * Disk quota editor.
50 */
51 #include <sys/param.h>
52 #include <sys/stat.h>
53 #include <sys/file.h>
54 #include <sys/wait.h>
55 #include <sys/queue.h>
56 #include <sys/types.h>
57 #include <sys/statvfs.h>
58
59 #include <quota/quotaprop.h>
60 #include <quota/quota.h>
61 #include <ufs/ufs/quota1.h>
62 #include <sys/quota.h>
63
64 #include <assert.h>
65 #include <err.h>
66 #include <errno.h>
67 #include <fstab.h>
68 #include <pwd.h>
69 #include <grp.h>
70 #include <ctype.h>
71 #include <signal.h>
72 #include <stdbool.h>
73 #include <stdio.h>
74 #include <stdlib.h>
75 #include <string.h>
76 #include <unistd.h>
77
78 #include "printquota.h"
79 #include "getvfsquota.h"
80 #include "quotautil.h"
81
82 #include "pathnames.h"
83
84 static const char *quotagroup = QUOTAGROUP;
85
86 #define MAX_TMPSTR (100+MAXPATHLEN)
87
88 /* flags for quotause */
89 #define FOUND 0x01
90 #define QUOTA2 0x02
91 #define DEFAULT 0x04
92
93 struct quotause {
94 struct quotause *next;
95 long flags;
96 struct quotaval qv[QUOTA_NLIMITS];
97 char fsname[MAXPATHLEN + 1];
98 char *qfname;
99 };
100
101 struct quotalist {
102 struct quotause *head;
103 struct quotause *tail;
104 };
105
106 static void usage(void) __dead;
107
108 static int Hflag = 0;
109 static int Dflag = 0;
110
111 /* more compact form of constants */
112 #define QL_BLK QUOTA_LIMIT_BLOCK
113 #define QL_FL QUOTA_LIMIT_FILE
114
115 ////////////////////////////////////////////////////////////
116 // support code
117
118 /*
119 * This routine converts a name for a particular quota class to
120 * an identifier. This routine must agree with the kernel routine
121 * getinoquota as to the interpretation of quota classes.
122 */
123 static int
124 getidbyname(const char *name, int quotaclass)
125 {
126 struct passwd *pw;
127 struct group *gr;
128
129 if (alldigits(name))
130 return atoi(name);
131 switch(quotaclass) {
132 case QUOTA_CLASS_USER:
133 if ((pw = getpwnam(name)) != NULL)
134 return pw->pw_uid;
135 warnx("%s: no such user", name);
136 break;
137 case QUOTA_CLASS_GROUP:
138 if ((gr = getgrnam(name)) != NULL)
139 return gr->gr_gid;
140 warnx("%s: no such group", name);
141 break;
142 default:
143 warnx("%d: unknown quota type", quotaclass);
144 break;
145 }
146 sleep(1);
147 return -1;
148 }
149
150 ////////////////////////////////////////////////////////////
151 // quotause operations
152
153 /*
154 * Create an empty quotause structure.
155 */
156 static struct quotause *
157 quotause_create(void)
158 {
159 struct quotause *qup;
160
161 qup = malloc(sizeof(*qup));
162 if (qup == NULL) {
163 err(1, "malloc");
164 }
165
166 qup->next = NULL;
167 qup->flags = 0;
168 memset(qup->qv, 0, sizeof(qup->qv));
169 qup->fsname[0] = '\0';
170 qup->qfname = NULL;
171
172 return qup;
173 }
174
175 /*
176 * Free a quotause structure.
177 */
178 static void
179 quotause_destroy(struct quotause *qup)
180 {
181 free(qup->qfname);
182 free(qup);
183 }
184
185 ////////////////////////////////////////////////////////////
186 // quotalist operations
187
188 /*
189 * Create a quotause list.
190 */
191 static struct quotalist *
192 quotalist_create(void)
193 {
194 struct quotalist *qlist;
195
196 qlist = malloc(sizeof(*qlist));
197 if (qlist == NULL) {
198 err(1, "malloc");
199 }
200
201 qlist->head = NULL;
202 qlist->tail = NULL;
203
204 return qlist;
205 }
206
207 /*
208 * Free a list of quotause structures.
209 */
210 static void
211 quotalist_destroy(struct quotalist *qlist)
212 {
213 struct quotause *qup, *nextqup;
214
215 for (qup = qlist->head; qup; qup = nextqup) {
216 nextqup = qup->next;
217 quotause_destroy(qup);
218 }
219 free(qlist);
220 }
221
222 static bool
223 quotalist_empty(struct quotalist *qlist)
224 {
225 return qlist->head == NULL;
226 }
227
228 static void
229 quotalist_append(struct quotalist *qlist, struct quotause *qup)
230 {
231 /* should not already be on a list */
232 assert(qup->next == NULL);
233
234 if (qlist->head == NULL) {
235 qlist->head = qup;
236 } else {
237 qlist->tail->next = qup;
238 }
239 qlist->tail = qup;
240 }
241
242 ////////////////////////////////////////////////////////////
243 // ffs quota v1
244
245 static void
246 putprivs1(uint32_t id, int quotaclass, struct quotause *qup)
247 {
248 struct dqblk dqblk;
249 int fd;
250
251 quotaval_to_dqblk(qup->qv, &dqblk);
252 assert((qup->flags & DEFAULT) == 0);
253
254 if ((fd = open(qup->qfname, O_WRONLY)) < 0) {
255 warnx("open `%s'", qup->qfname);
256 } else {
257 (void)lseek(fd,
258 (off_t)(id * (long)sizeof (struct dqblk)),
259 SEEK_SET);
260 if (write(fd, &dqblk, sizeof (struct dqblk)) !=
261 sizeof (struct dqblk))
262 warnx("writing `%s'", qup->qfname);
263 close(fd);
264 }
265 }
266
267 static struct quotause *
268 getprivs1(long id, int quotaclass, const char *filesys)
269 {
270 struct fstab *fs;
271 char qfpathname[MAXPATHLEN];
272 struct quotause *qup;
273 struct dqblk dqblk;
274 int fd;
275
276 setfsent();
277 while ((fs = getfsent()) != NULL) {
278 if (strcmp(fs->fs_vfstype, "ffs"))
279 continue;
280 if (strcmp(fs->fs_spec, filesys) == 0 ||
281 strcmp(fs->fs_file, filesys) == 0)
282 break;
283 }
284 if (fs == NULL)
285 return NULL;
286
287 if (!hasquota(qfpathname, sizeof(qfpathname), fs,
288 ufsclass2qtype(quotaclass)))
289 return NULL;
290
291 qup = quotause_create();
292 strcpy(qup->fsname, fs->fs_file);
293 if ((fd = open(qfpathname, O_RDONLY)) < 0) {
294 fd = open(qfpathname, O_RDWR|O_CREAT, 0640);
295 if (fd < 0 && errno != ENOENT) {
296 warnx("open `%s'", qfpathname);
297 quotause_destroy(qup);
298 return NULL;
299 }
300 warnx("Creating quota file %s", qfpathname);
301 sleep(3);
302 (void)fchown(fd, getuid(),
303 getidbyname(quotagroup, QUOTA_CLASS_GROUP));
304 (void)fchmod(fd, 0640);
305 }
306 (void)lseek(fd, (off_t)(id * sizeof(struct dqblk)),
307 SEEK_SET);
308 switch (read(fd, &dqblk, sizeof(struct dqblk))) {
309 case 0: /* EOF */
310 /*
311 * Convert implicit 0 quota (EOF)
312 * into an explicit one (zero'ed dqblk)
313 */
314 memset(&dqblk, 0, sizeof(struct dqblk));
315 break;
316
317 case sizeof(struct dqblk): /* OK */
318 break;
319
320 default: /* ERROR */
321 warn("read error in `%s'", qfpathname);
322 close(fd);
323 quotause_destroy(qup);
324 return NULL;
325 }
326 close(fd);
327 qup->qfname = qfpathname;
328 endfsent();
329 dqblk_to_quotaval(&dqblk, qup->qv);
330 return qup;
331 }
332
333 ////////////////////////////////////////////////////////////
334 // ffs quota v2
335
336 static struct quotause *
337 getprivs2(long id, int quotaclass, const char *filesys, int defaultq)
338 {
339 struct quotause *qup;
340 int8_t version;
341
342 qup = quotause_create();
343 strcpy(qup->fsname, filesys);
344 if (defaultq)
345 qup->flags |= DEFAULT;
346 if (!getvfsquota(filesys, qup->qv, &version,
347 id, quotaclass, defaultq, Dflag)) {
348 /* no entry, get default entry */
349 if (!getvfsquota(filesys, qup->qv, &version,
350 id, quotaclass, 1, Dflag)) {
351 free(qup);
352 return NULL;
353 }
354 }
355 if (version == 2)
356 qup->flags |= QUOTA2;
357 return qup;
358 }
359
360 static void
361 putprivs2(uint32_t id, int quotaclass, struct quotause *qup)
362 {
363
364 prop_dictionary_t dict, data, cmd;
365 prop_array_t cmds, datas;
366 struct plistref pref;
367 int8_t error8;
368 uint64_t *valuesp[QUOTA_NLIMITS];
369
370 valuesp[QL_BLK] =
371 &qup->qv[QL_BLK].qv_hardlimit;
372 valuesp[QL_FL] =
373 &qup->qv[QL_FL].qv_hardlimit;
374
375 data = quota64toprop(id, (qup->flags & DEFAULT) ? 1 : 0,
376 valuesp, ufs_quota_entry_names, UFS_QUOTA_NENTRIES,
377 ufs_quota_limit_names, QUOTA_NLIMITS);
378
379 if (data == NULL)
380 err(1, "quota64toprop(id)");
381
382 dict = quota_prop_create();
383 cmds = prop_array_create();
384 datas = prop_array_create();
385
386 if (dict == NULL || cmds == NULL || datas == NULL) {
387 errx(1, "can't allocate proplist");
388 }
389
390 if (!prop_array_add_and_rel(datas, data))
391 err(1, "prop_array_add(data)");
392
393 if (!quota_prop_add_command(cmds, "set",
394 ufs_quota_class_names[quotaclass], datas))
395 err(1, "prop_add_command");
396 if (!prop_dictionary_set(dict, "commands", cmds))
397 err(1, "prop_dictionary_set(command)");
398 if (Dflag)
399 printf("message to kernel:\n%s\n",
400 prop_dictionary_externalize(dict));
401
402 if (prop_dictionary_send_syscall(dict, &pref) != 0)
403 err(1, "prop_dictionary_send_syscall");
404 prop_object_release(dict);
405
406 if (quotactl(qup->fsname, &pref) != 0)
407 err(1, "quotactl");
408
409 if (prop_dictionary_recv_syscall(&pref, &dict) != 0) {
410 err(1, "prop_dictionary_recv_syscall");
411 }
412
413 if (Dflag)
414 printf("reply from kernel:\n%s\n",
415 prop_dictionary_externalize(dict));
416
417 if ((errno = quota_get_cmds(dict, &cmds)) != 0) {
418 err(1, "quota_get_cmds");
419 }
420 /* only one command, no need to iter */
421 cmd = prop_array_get(cmds, 0);
422 if (cmd == NULL)
423 err(1, "prop_array_get(cmd)");
424
425 if (!prop_dictionary_get_int8(cmd, "return", &error8))
426 err(1, "prop_get(return)");
427
428 if (error8) {
429 errno = error8;
430 if (qup->flags & DEFAULT)
431 warn("set default %s quota",
432 ufs_quota_class_names[quotaclass]);
433 else
434 warn("set %s quota for %u",
435 ufs_quota_class_names[quotaclass], id);
436 }
437 prop_object_release(dict);
438 }
439
440 ////////////////////////////////////////////////////////////
441 // quota format switch
442
443 /*
444 * Collect the requested quota information.
445 */
446 static struct quotalist *
447 getprivs(long id, int defaultq, int quotaclass, const char *filesys)
448 {
449 struct statvfs *fst;
450 int nfst, i;
451 struct quotalist *qlist;
452 struct quotause *qup;
453
454 qlist = quotalist_create();
455
456 nfst = getmntinfo(&fst, MNT_WAIT);
457 if (nfst == 0)
458 errx(1, "no filesystems mounted!");
459
460 for (i = 0; i < nfst; i++) {
461 if ((fst[i].f_flag & ST_QUOTA) == 0)
462 continue;
463 if (filesys &&
464 strcmp(fst[i].f_mntonname, filesys) != 0 &&
465 strcmp(fst[i].f_mntfromname, filesys) != 0)
466 continue;
467 qup = getprivs2(id, quotaclass, fst[i].f_mntonname, defaultq);
468 if (qup == NULL) {
469 /*
470 * XXX: returning NULL is totally wrong. On
471 * serious error, abort; on minor error, warn
472 * and continue.
473 *
474 * Note: we cannot warn unconditionally here
475 * because this case apparently includes "no
476 * quota entry on this volume" and that causes
477 * the atf tests to fail. Bletch.
478 */
479 /*return NULL;*/
480 /*warnx("getprivs2 failed");*/
481 continue;
482 }
483
484 quotalist_append(qlist, qup);
485 }
486
487 if (filesys && quotalist_empty(qlist)) {
488 if (defaultq)
489 errx(1, "no default quota for version 1");
490 /* if we get there, filesys is not mounted. try the old way */
491 qup = getprivs1(id, quotaclass, filesys);
492 if (qup == NULL) {
493 /* XXX. see above */
494 /*return NULL;*/
495 /*warnx("getprivs1 failed");*/
496 return qlist;
497 }
498 quotalist_append(qlist, qup);
499 }
500 return qlist;
501 }
502
503 /*
504 * Store the requested quota information.
505 */
506 static void
507 putprivs(uint32_t id, int quotaclass, struct quotalist *qlist)
508 {
509 struct quotause *qup;
510
511 for (qup = qlist->head; qup; qup = qup->next) {
512 if (qup->qfname == NULL)
513 putprivs2(id, quotaclass, qup);
514 else
515 putprivs1(id, quotaclass, qup);
516 }
517 }
518
519 static void
520 clearpriv(int argc, char **argv, const char *filesys, int quotaclass)
521 {
522 prop_array_t cmds, datas;
523 prop_dictionary_t protodict, dict, data, cmd;
524 struct plistref pref;
525 bool ret;
526 struct statvfs *fst;
527 int nfst, i;
528 int8_t error8;
529 int id;
530
531 /* build a generic command */
532 protodict = quota_prop_create();
533 cmds = prop_array_create();
534 datas = prop_array_create();
535 if (protodict == NULL || cmds == NULL || datas == NULL) {
536 errx(1, "can't allocate proplist");
537 }
538
539 for ( ; argc > 0; argc--, argv++) {
540 if ((id = getidbyname(*argv, quotaclass)) == -1)
541 continue;
542 data = prop_dictionary_create();
543 if (data == NULL)
544 errx(1, "can't allocate proplist");
545
546 ret = prop_dictionary_set_uint32(data, "id", id);
547 if (!ret)
548 err(1, "prop_dictionary_set(id)");
549 if (!prop_array_add_and_rel(datas, data))
550 err(1, "prop_array_add(data)");
551 }
552 if (!quota_prop_add_command(cmds, "clear",
553 ufs_quota_class_names[quotaclass], datas))
554 err(1, "prop_add_command");
555
556 if (!prop_dictionary_set(protodict, "commands", cmds))
557 err(1, "prop_dictionary_set(command)");
558
559 /* now loop over quota-enabled filesystems */
560 nfst = getmntinfo(&fst, MNT_WAIT);
561 if (nfst == 0)
562 errx(1, "no filesystems mounted!");
563
564 for (i = 0; i < nfst; i++) {
565 if ((fst[i].f_flag & ST_QUOTA) == 0)
566 continue;
567 if (filesys && strcmp(fst[i].f_mntonname, filesys) != 0 &&
568 strcmp(fst[i].f_mntfromname, filesys) != 0)
569 continue;
570 if (Dflag) {
571 fprintf(stderr, "message to kernel for %s:\n%s\n",
572 fst[i].f_mntonname,
573 prop_dictionary_externalize(protodict));
574 }
575
576 if (prop_dictionary_send_syscall(protodict, &pref) != 0)
577 err(1, "prop_dictionary_send_syscall");
578 if (quotactl(fst[i].f_mntonname, &pref) != 0)
579 err(1, "quotactl");
580
581 if (prop_dictionary_recv_syscall(&pref, &dict) != 0) {
582 err(1, "prop_dictionary_recv_syscall");
583 }
584
585 if (Dflag) {
586 fprintf(stderr, "reply from kernel for %s:\n%s\n",
587 fst[i].f_mntonname,
588 prop_dictionary_externalize(dict));
589 }
590 if ((errno = quota_get_cmds(dict, &cmds)) != 0) {
591 err(1, "quota_get_cmds");
592 }
593 /* only one command, no need to iter */
594 cmd = prop_array_get(cmds, 0);
595 if (cmd == NULL)
596 err(1, "prop_array_get(cmd)");
597
598 if (!prop_dictionary_get_int8(cmd, "return", &error8))
599 err(1, "prop_get(return)");
600 if (error8) {
601 errno = error8;
602 warn("clear %s quota entries on %s",
603 ufs_quota_class_names[quotaclass],
604 fst[i].f_mntonname);
605 }
606 prop_object_release(dict);
607 }
608 prop_object_release(protodict);
609 }
610
611 ////////////////////////////////////////////////////////////
612 // editor
613
614 /*
615 * Take a list of privileges and get it edited.
616 */
617 static int
618 editit(const char *ltmpfile)
619 {
620 pid_t pid;
621 int lst;
622 char p[MAX_TMPSTR];
623 const char *ed;
624 sigset_t s, os;
625
626 sigemptyset(&s);
627 sigaddset(&s, SIGINT);
628 sigaddset(&s, SIGQUIT);
629 sigaddset(&s, SIGHUP);
630 if (sigprocmask(SIG_BLOCK, &s, &os) == -1)
631 err(1, "sigprocmask");
632 top:
633 switch ((pid = fork())) {
634 case -1:
635 if (errno == EPROCLIM) {
636 warnx("You have too many processes");
637 return 0;
638 }
639 if (errno == EAGAIN) {
640 sleep(1);
641 goto top;
642 }
643 warn("fork");
644 return 0;
645 case 0:
646 if (sigprocmask(SIG_SETMASK, &os, NULL) == -1)
647 err(1, "sigprocmask");
648 setgid(getgid());
649 setuid(getuid());
650 if ((ed = getenv("EDITOR")) == (char *)0)
651 ed = _PATH_VI;
652 if (strlen(ed) + strlen(ltmpfile) + 2 >= MAX_TMPSTR) {
653 errx(1, "%s", "editor or filename too long");
654 }
655 snprintf(p, sizeof(p), "%s %s", ed, ltmpfile);
656 execlp(_PATH_BSHELL, _PATH_BSHELL, "-c", p, NULL);
657 err(1, "%s", ed);
658 default:
659 if (waitpid(pid, &lst, 0) == -1)
660 err(1, "waitpid");
661 if (sigprocmask(SIG_SETMASK, &os, NULL) == -1)
662 err(1, "sigprocmask");
663 if (!WIFEXITED(lst) || WEXITSTATUS(lst) != 0)
664 return 0;
665 return 1;
666 }
667 }
668
669 /*
670 * Convert a quotause list to an ASCII file.
671 */
672 static int
673 writeprivs(struct quotalist *qlist, int outfd, const char *name,
674 int quotaclass)
675 {
676 struct quotause *qup;
677 FILE *fd;
678 char b0[32], b1[32], b2[32], b3[32];
679
680 (void)ftruncate(outfd, 0);
681 (void)lseek(outfd, (off_t)0, SEEK_SET);
682 if ((fd = fdopen(dup(outfd), "w")) == NULL)
683 errx(1, "fdopen");
684 if (name == NULL) {
685 fprintf(fd, "Default %s quotas:\n",
686 ufs_quota_class_names[quotaclass]);
687 } else {
688 fprintf(fd, "Quotas for %s %s:\n",
689 ufs_quota_class_names[quotaclass], name);
690 }
691 for (qup = qlist->head; qup; qup = qup->next) {
692 struct quotaval *q = qup->qv;
693 fprintf(fd, "%s (version %d):\n",
694 qup->fsname, (qup->flags & QUOTA2) ? 2 : 1);
695 if ((qup->flags & DEFAULT) == 0 || (qup->flags & QUOTA2) != 0) {
696 fprintf(fd, "\tblocks in use: %s, "
697 "limits (soft = %s, hard = %s",
698 intprt(b1, 21, q[QL_BLK].qv_usage,
699 HN_NOSPACE | HN_B, Hflag),
700 intprt(b2, 21, q[QL_BLK].qv_softlimit,
701 HN_NOSPACE | HN_B, Hflag),
702 intprt(b3, 21, q[QL_BLK].qv_hardlimit,
703 HN_NOSPACE | HN_B, Hflag));
704 if (qup->flags & QUOTA2)
705 fprintf(fd, ", ");
706 } else
707 fprintf(fd, "\tblocks: (");
708
709 if (qup->flags & (QUOTA2|DEFAULT)) {
710 fprintf(fd, "grace = %s",
711 timepprt(b0, 21, q[QL_BLK].qv_grace, Hflag));
712 }
713 fprintf(fd, ")\n");
714 if ((qup->flags & DEFAULT) == 0 || (qup->flags & QUOTA2) != 0) {
715 fprintf(fd, "\tinodes in use: %s, "
716 "limits (soft = %s, hard = %s",
717 intprt(b1, 21, q[QL_FL].qv_usage,
718 HN_NOSPACE, Hflag),
719 intprt(b2, 21, q[QL_FL].qv_softlimit,
720 HN_NOSPACE, Hflag),
721 intprt(b3, 21, q[QL_FL].qv_hardlimit,
722 HN_NOSPACE, Hflag));
723 if (qup->flags & QUOTA2)
724 fprintf(fd, ", ");
725 } else
726 fprintf(fd, "\tinodes: (");
727
728 if (qup->flags & (QUOTA2|DEFAULT)) {
729 fprintf(fd, "grace = %s",
730 timepprt(b0, 21, q[QL_FL].qv_grace, Hflag));
731 }
732 fprintf(fd, ")\n");
733 }
734 fclose(fd);
735 return 1;
736 }
737
738 /*
739 * Merge changes to an ASCII file into a quotause list.
740 */
741 static int
742 readprivs(struct quotalist *qlist, int infd, int dflag)
743 {
744 struct quotause *qup;
745 FILE *fd;
746 int cnt;
747 char fsp[BUFSIZ];
748 static char line0[BUFSIZ], line1[BUFSIZ], line2[BUFSIZ];
749 static char scurb[BUFSIZ], scuri[BUFSIZ], ssoft[BUFSIZ], shard[BUFSIZ];
750 static char stime[BUFSIZ];
751 uint64_t softb, hardb, softi, hardi;
752 time_t graceb = -1, gracei = -1;
753 int version;
754
755 (void)lseek(infd, (off_t)0, SEEK_SET);
756 fd = fdopen(dup(infd), "r");
757 if (fd == NULL) {
758 warn("Can't re-read temp file");
759 return 0;
760 }
761 /*
762 * Discard title line, then read pairs of lines to process.
763 */
764 (void) fgets(line1, sizeof (line1), fd);
765 while (fgets(line0, sizeof (line0), fd) != NULL &&
766 fgets(line1, sizeof (line2), fd) != NULL &&
767 fgets(line2, sizeof (line2), fd) != NULL) {
768 if (sscanf(line0, "%s (version %d):\n", fsp, &version) != 2) {
769 warnx("%s: bad format", line0);
770 goto out;
771 }
772 #define last_char(str) ((str)[strlen(str) - 1])
773 if (last_char(line1) != '\n') {
774 warnx("%s:%s: bad format", fsp, line1);
775 goto out;
776 }
777 last_char(line1) = '\0';
778 if (last_char(line2) != '\n') {
779 warnx("%s:%s: bad format", fsp, line2);
780 goto out;
781 }
782 last_char(line2) = '\0';
783 if (dflag && version == 1) {
784 if (sscanf(line1,
785 "\tblocks: (grace = %s\n", stime) != 1) {
786 warnx("%s:%s: bad format", fsp, line1);
787 goto out;
788 }
789 if (last_char(stime) != ')') {
790 warnx("%s:%s: bad format", fsp, line1);
791 goto out;
792 }
793 last_char(stime) = '\0';
794 if (timeprd(stime, &graceb) != 0) {
795 warnx("%s:%s: bad number", fsp, stime);
796 goto out;
797 }
798 if (sscanf(line2,
799 "\tinodes: (grace = %s\n", stime) != 1) {
800 warnx("%s:%s: bad format", fsp, line2);
801 goto out;
802 }
803 if (last_char(stime) != ')') {
804 warnx("%s:%s: bad format", fsp, line2);
805 goto out;
806 }
807 last_char(stime) = '\0';
808 if (timeprd(stime, &gracei) != 0) {
809 warnx("%s:%s: bad number", fsp, stime);
810 goto out;
811 }
812 } else {
813 cnt = sscanf(line1,
814 "\tblocks in use: %s limits (soft = %s hard = %s "
815 "grace = %s", scurb, ssoft, shard, stime);
816 if (cnt == 3) {
817 if (version != 1 ||
818 last_char(scurb) != ',' ||
819 last_char(ssoft) != ',' ||
820 last_char(shard) != ')') {
821 warnx("%s:%s: bad format %d",
822 fsp, line1, cnt);
823 goto out;
824 }
825 stime[0] = '\0';
826 } else if (cnt == 4) {
827 if (version < 2 ||
828 last_char(scurb) != ',' ||
829 last_char(ssoft) != ',' ||
830 last_char(shard) != ',' ||
831 last_char(stime) != ')') {
832 warnx("%s:%s: bad format %d",
833 fsp, line1, cnt);
834 goto out;
835 }
836 } else {
837 warnx("%s: %s: bad format cnt %d", fsp, line1,
838 cnt);
839 goto out;
840 }
841 /* drop last char which is ',' or ')' */
842 last_char(scurb) = '\0';
843 last_char(ssoft) = '\0';
844 last_char(shard) = '\0';
845 last_char(stime) = '\0';
846
847 if (intrd(ssoft, &softb, HN_B) != 0) {
848 warnx("%s:%s: bad number", fsp, ssoft);
849 goto out;
850 }
851 if (intrd(shard, &hardb, HN_B) != 0) {
852 warnx("%s:%s: bad number", fsp, shard);
853 goto out;
854 }
855 if (cnt == 4) {
856 if (timeprd(stime, &graceb) != 0) {
857 warnx("%s:%s: bad number", fsp, stime);
858 goto out;
859 }
860 }
861
862 cnt = sscanf(line2,
863 "\tinodes in use: %s limits (soft = %s hard = %s "
864 "grace = %s", scuri, ssoft, shard, stime);
865 if (cnt == 3) {
866 if (version != 1 ||
867 last_char(scuri) != ',' ||
868 last_char(ssoft) != ',' ||
869 last_char(shard) != ')') {
870 warnx("%s:%s: bad format %d",
871 fsp, line2, cnt);
872 goto out;
873 }
874 stime[0] = '\0';
875 } else if (cnt == 4) {
876 if (version < 2 ||
877 last_char(scuri) != ',' ||
878 last_char(ssoft) != ',' ||
879 last_char(shard) != ',' ||
880 last_char(stime) != ')') {
881 warnx("%s:%s: bad format %d",
882 fsp, line2, cnt);
883 goto out;
884 }
885 } else {
886 warnx("%s: %s: bad format", fsp, line2);
887 goto out;
888 }
889 /* drop last char which is ',' or ')' */
890 last_char(scuri) = '\0';
891 last_char(ssoft) = '\0';
892 last_char(shard) = '\0';
893 last_char(stime) = '\0';
894 if (intrd(ssoft, &softi, 0) != 0) {
895 warnx("%s:%s: bad number", fsp, ssoft);
896 goto out;
897 }
898 if (intrd(shard, &hardi, 0) != 0) {
899 warnx("%s:%s: bad number", fsp, shard);
900 goto out;
901 }
902 if (cnt == 4) {
903 if (timeprd(stime, &gracei) != 0) {
904 warnx("%s:%s: bad number", fsp, stime);
905 goto out;
906 }
907 }
908 }
909 for (qup = qlist->head; qup; qup = qup->next) {
910 struct quotaval *q = qup->qv;
911 char b1[32], b2[32];
912 if (strcmp(fsp, qup->fsname))
913 continue;
914 if (version == 1 && dflag) {
915 q[QL_BLK].qv_grace = graceb;
916 q[QL_FL].qv_grace = gracei;
917 qup->flags |= FOUND;
918 continue;
919 }
920
921 if (strcmp(intprt(b1, 21, q[QL_BLK].qv_usage,
922 HN_NOSPACE | HN_B, Hflag),
923 scurb) != 0 ||
924 strcmp(intprt(b2, 21, q[QL_FL].qv_usage,
925 HN_NOSPACE, Hflag),
926 scuri) != 0) {
927 warnx("%s: cannot change current allocation",
928 fsp);
929 break;
930 }
931 /*
932 * Cause time limit to be reset when the quota
933 * is next used if previously had no soft limit
934 * or were under it, but now have a soft limit
935 * and are over it.
936 */
937 if (q[QL_BLK].qv_usage &&
938 q[QL_BLK].qv_usage >= softb &&
939 (q[QL_BLK].qv_softlimit == 0 ||
940 q[QL_BLK].qv_usage < q[QL_BLK].qv_softlimit))
941 q[QL_BLK].qv_expiretime = 0;
942 if (q[QL_FL].qv_usage &&
943 q[QL_FL].qv_usage >= softi &&
944 (q[QL_FL].qv_softlimit == 0 ||
945 q[QL_FL].qv_usage < q[QL_FL].qv_softlimit))
946 q[QL_FL].qv_expiretime = 0;
947 q[QL_BLK].qv_softlimit = softb;
948 q[QL_BLK].qv_hardlimit = hardb;
949 if (version == 2)
950 q[QL_BLK].qv_grace = graceb;
951 q[QL_FL].qv_softlimit = softi;
952 q[QL_FL].qv_hardlimit = hardi;
953 if (version == 2)
954 q[QL_FL].qv_grace = gracei;
955 qup->flags |= FOUND;
956 }
957 }
958 out:
959 fclose(fd);
960 /*
961 * Disable quotas for any filesystems that have not been found.
962 */
963 for (qup = qlist->head; qup; qup = qup->next) {
964 struct quotaval *q = qup->qv;
965 if (qup->flags & FOUND) {
966 qup->flags &= ~FOUND;
967 continue;
968 }
969 q[QL_BLK].qv_softlimit = UQUAD_MAX;
970 q[QL_BLK].qv_hardlimit = UQUAD_MAX;
971 q[QL_BLK].qv_grace = 0;
972 q[QL_FL].qv_softlimit = UQUAD_MAX;
973 q[QL_FL].qv_hardlimit = UQUAD_MAX;
974 q[QL_FL].qv_grace = 0;
975 }
976 return 1;
977 }
978
979 ////////////////////////////////////////////////////////////
980 // actions
981
982 static void
983 replicate(const char *fs, int quotaclass, const char *protoname,
984 char **names, int numnames)
985 {
986 long protoid, id;
987 struct quotalist *protoprivs;
988 struct quotause *qup;
989 int i;
990
991 if ((protoid = getidbyname(protoname, quotaclass)) == -1)
992 exit(1);
993 protoprivs = getprivs(protoid, 0, quotaclass, fs);
994 for (qup = protoprivs->head; qup; qup = qup->next) {
995 qup->qv[QL_BLK].qv_expiretime = 0;
996 qup->qv[QL_FL].qv_expiretime = 0;
997 }
998 for (i=0; i<numnames; i++) {
999 id = getidbyname(names[i], quotaclass);
1000 if (id < 0)
1001 continue;
1002 putprivs(id, quotaclass, protoprivs);
1003 }
1004 /* XXX */
1005 /* quotalist_destroy(protoprivs); */
1006 }
1007
1008 static void
1009 assign(const char *fs, int quotaclass,
1010 char *soft, char *hard, char *grace,
1011 char **names, int numnames)
1012 {
1013 struct quotalist *curprivs;
1014 struct quotause *lqup;
1015 u_int64_t softb, hardb, softi, hardi;
1016 time_t graceb, gracei;
1017 char *str;
1018 long id;
1019 int dflag;
1020 int i;
1021
1022 if (soft) {
1023 str = strsep(&soft, "/");
1024 if (str[0] == '\0' || soft == NULL || soft[0] == '\0')
1025 usage();
1026
1027 if (intrd(str, &softb, HN_B) != 0)
1028 errx(1, "%s: bad number", str);
1029 if (intrd(soft, &softi, 0) != 0)
1030 errx(1, "%s: bad number", soft);
1031 }
1032 if (hard) {
1033 str = strsep(&hard, "/");
1034 if (str[0] == '\0' || hard == NULL || hard[0] == '\0')
1035 usage();
1036
1037 if (intrd(str, &hardb, HN_B) != 0)
1038 errx(1, "%s: bad number", str);
1039 if (intrd(hard, &hardi, 0) != 0)
1040 errx(1, "%s: bad number", hard);
1041 }
1042 if (grace) {
1043 str = strsep(&grace, "/");
1044 if (str[0] == '\0' || grace == NULL || grace[0] == '\0')
1045 usage();
1046
1047 if (timeprd(str, &graceb) != 0)
1048 errx(1, "%s: bad number", str);
1049 if (timeprd(grace, &gracei) != 0)
1050 errx(1, "%s: bad number", grace);
1051 }
1052 for (i=0; i<numnames; i++) {
1053 if (names[i] == NULL) {
1054 id = 0;
1055 dflag = 1;
1056 } else {
1057 id = getidbyname(names[i], quotaclass);
1058 if (id == -1)
1059 continue;
1060 dflag = 0;
1061 }
1062
1063 curprivs = getprivs(id, dflag, quotaclass, fs);
1064 for (lqup = curprivs->head; lqup; lqup = lqup->next) {
1065 struct quotaval *q = lqup->qv;
1066 if (soft) {
1067 if (!dflag && softb &&
1068 q[QL_BLK].qv_usage >= softb &&
1069 (q[QL_BLK].qv_softlimit == 0 ||
1070 q[QL_BLK].qv_usage <
1071 q[QL_BLK].qv_softlimit))
1072 q[QL_BLK].qv_expiretime = 0;
1073 if (!dflag && softi &&
1074 q[QL_FL].qv_usage >= softb &&
1075 (q[QL_FL].qv_softlimit == 0 ||
1076 q[QL_FL].qv_usage <
1077 q[QL_FL].qv_softlimit))
1078 q[QL_FL].qv_expiretime = 0;
1079 q[QL_BLK].qv_softlimit = softb;
1080 q[QL_FL].qv_softlimit = softi;
1081 }
1082 if (hard) {
1083 q[QL_BLK].qv_hardlimit = hardb;
1084 q[QL_FL].qv_hardlimit = hardi;
1085 }
1086 if (grace) {
1087 q[QL_BLK].qv_grace = graceb;
1088 q[QL_FL].qv_grace = gracei;
1089 }
1090 }
1091 putprivs(id, quotaclass, curprivs);
1092 quotalist_destroy(curprivs);
1093 }
1094 }
1095
1096 static void
1097 clear(const char *fs, int quotaclass, char **names, int numnames)
1098 {
1099 clearpriv(numnames, names, fs, quotaclass);
1100 }
1101
1102 static void
1103 editone(const char *fs, int quotaclass, const char *name,
1104 int tmpfd, const char *tmppath)
1105 {
1106 struct quotalist *curprivs;
1107 long id;
1108 int dflag;
1109
1110 if (name == NULL) {
1111 id = 0;
1112 dflag = 1;
1113 } else {
1114 id = getidbyname(name, quotaclass);
1115 if (id == -1)
1116 return;
1117 dflag = 0;
1118 }
1119 curprivs = getprivs(id, dflag, quotaclass, fs);
1120
1121 if (writeprivs(curprivs, tmpfd, name, quotaclass) == 0)
1122 goto fail;
1123
1124 if (editit(tmppath) == 0)
1125 goto fail;
1126
1127 if (readprivs(curprivs, tmpfd, dflag) == 0)
1128 goto fail;
1129
1130 putprivs(id, quotaclass, curprivs);
1131 fail:
1132 quotalist_destroy(curprivs);
1133 }
1134
1135 static void
1136 edit(const char *fs, int quotaclass, char **names, int numnames)
1137 {
1138 char tmppath[] = _PATH_TMPFILE;
1139 int tmpfd, i;
1140
1141 tmpfd = mkstemp(tmppath);
1142 fchown(tmpfd, getuid(), getgid());
1143
1144 for (i=0; i<numnames; i++) {
1145 editone(fs, quotaclass, names[i], tmpfd, tmppath);
1146 }
1147
1148 close(tmpfd);
1149 unlink(tmppath);
1150 }
1151
1152 ////////////////////////////////////////////////////////////
1153 // main
1154
1155 static void
1156 usage(void)
1157 {
1158 const char *p = getprogname();
1159 fprintf(stderr,
1160 "Usage: %s [-D] [-H] [-u] [-p <username>] [-f <filesystem>] "
1161 "-d | <username> ...\n"
1162 "\t%s [-D] [-H] -g [-p <groupname>] [-f <filesystem>] "
1163 "-d | <groupname> ...\n"
1164 "\t%s [-D] [-u] [-f <filesystem>] [-s b#/i#] [-h b#/i#] [-t t#/t#] "
1165 "-d | <username> ...\n"
1166 "\t%s [-D] -g [-f <filesystem>] [-s b#/i#] [-h b#/i#] [-t t#/t#] "
1167 "-d | <groupname> ...\n"
1168 "\t%s [-D] [-H] [-u] -c [-f <filesystem>] username ...\n"
1169 "\t%s [-D] [-H] -g -c [-f <filesystem>] groupname ...\n",
1170 p, p, p, p, p, p);
1171 exit(1);
1172 }
1173
1174 int
1175 main(int argc, char *argv[])
1176 {
1177 int quotaclass;
1178 char *protoname;
1179 char *soft = NULL, *hard = NULL, *grace = NULL;
1180 char *fs = NULL;
1181 int ch;
1182 int pflag = 0;
1183 int cflag = 0;
1184 int dflag = 0;
1185
1186 if (argc < 2)
1187 usage();
1188 if (getuid())
1189 errx(1, "permission denied");
1190 protoname = NULL;
1191 quotaclass = QUOTA_CLASS_USER;
1192 while ((ch = getopt(argc, argv, "DHcdugp:s:h:t:f:")) != -1) {
1193 switch(ch) {
1194 case 'D':
1195 Dflag++;
1196 break;
1197 case 'H':
1198 Hflag++;
1199 break;
1200 case 'c':
1201 cflag++;
1202 break;
1203 case 'd':
1204 dflag++;
1205 break;
1206 case 'p':
1207 protoname = optarg;
1208 pflag++;
1209 break;
1210 case 'g':
1211 quotaclass = QUOTA_CLASS_GROUP;
1212 break;
1213 case 'u':
1214 quotaclass = QUOTA_CLASS_USER;
1215 break;
1216 case 's':
1217 soft = optarg;
1218 break;
1219 case 'h':
1220 hard = optarg;
1221 break;
1222 case 't':
1223 grace = optarg;
1224 break;
1225 case 'f':
1226 fs = optarg;
1227 break;
1228 default:
1229 usage();
1230 }
1231 }
1232 argc -= optind;
1233 argv += optind;
1234
1235 if (pflag) {
1236 if (soft || hard || grace || dflag || cflag)
1237 usage();
1238 replicate(fs, quotaclass, protoname, argv, argc);
1239 } else if (soft || hard || grace) {
1240 if (cflag)
1241 usage();
1242 if (dflag) {
1243 /* use argv[argc], which is null, to mean 'default' */
1244 argc++;
1245 }
1246 assign(fs, quotaclass, soft, hard, grace, argv, argc);
1247 } else if (cflag) {
1248 if (dflag)
1249 usage();
1250 clear(fs, quotaclass, argv, argc);
1251 } else {
1252 if (dflag) {
1253 /* use argv[argc], which is null, to mean 'default' */
1254 argc++;
1255 }
1256 edit(fs, quotaclass, argv, argc);
1257 }
1258 return 0;
1259 }
1260