edquota.c revision 1.29.16.8 1 /* $NetBSD: edquota.c,v 1.29.16.8 2011/02/06 19:24:20 bouyer 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.29.16.8 2011/02/06 19:24:20 bouyer 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 <ufs/ufs/quota2_prop.h>
60 #include <ufs/ufs/quota1.h>
61 #include <sys/quota.h>
62
63 #include <assert.h>
64 #include <err.h>
65 #include <errno.h>
66 #include <fstab.h>
67 #include <pwd.h>
68 #include <grp.h>
69 #include <ctype.h>
70 #include <signal.h>
71 #include <stdio.h>
72 #include <stdlib.h>
73 #include <string.h>
74 #include <unistd.h>
75
76 #include <printquota.h>
77 #include <getvfsquota.h>
78
79 #include "pathnames.h"
80
81 const char *qfname = QUOTAFILENAME;
82 const char *quotagroup = QUOTAGROUP;
83 char tmpfil[] = _PATH_TMP;
84
85 struct quotause {
86 struct quotause *next;
87 long flags;
88 struct quota2_entry q2e;
89 char fsname[MAXPATHLEN + 1];
90 char *qfname;
91 };
92 #define FOUND 0x01
93 #define QUOTA2 0x02
94 #define DEFAULT 0x04
95
96 #define MAX_TMPSTR (100+MAXPATHLEN)
97
98 int main(int, char **);
99 void usage(void);
100 int getentry(const char *, int);
101 struct quotause * getprivs(long, int, const char *, int);
102 struct quotause * getprivs2(long, int, const char *, int);
103 struct quotause * getprivs1(long, int, const char *);
104 void putprivs(long, int, struct quotause *);
105 void putprivs2(long, int, struct quotause *);
106 void putprivs1(long, int, struct quotause *);
107 int editit(char *);
108 int writeprivs(struct quotause *, int, char *, int);
109 int readprivs(struct quotause *, int);
110 int writetimes(struct quotause *, int, int);
111 int readtimes(struct quotause *, int);
112 void freeq(struct quotause *);
113 void freeprivs(struct quotause *);
114 int alldigits(const char *);
115 int hasquota(struct fstab *, int, char **);
116
117 int Hflag = 0;
118 int Dflag = 0;
119 int dflag = 0;
120
121 int
122 main(argc, argv)
123 int argc;
124 char **argv;
125 {
126 struct quotause *qup, *protoprivs, *curprivs;
127 long id, protoid;
128 int quotatype, tmpfd;
129 char *protoname;
130 char *soft = NULL, *hard = NULL, *grace = NULL;
131 char *fs = NULL;
132 int ch;
133 int pflag = 0;
134
135 if (argc < 2)
136 usage();
137 if (getuid())
138 errx(1, "permission denied");
139 protoname = NULL;
140 quotatype = USRQUOTA;
141 while ((ch = getopt(argc, argv, "DHdugp:s:h:t:f:")) != -1) {
142 switch(ch) {
143 case 'D':
144 Dflag++;
145 break;
146 case 'H':
147 Hflag++;
148 break;
149 case 'd':
150 dflag++;
151 break;
152 case 'p':
153 protoname = optarg;
154 pflag++;
155 break;
156 case 'g':
157 quotatype = GRPQUOTA;
158 break;
159 case 'u':
160 quotatype = USRQUOTA;
161 break;
162 case 's':
163 soft = optarg;
164 break;
165 case 'h':
166 hard = optarg;
167 break;
168 case 't':
169 grace = optarg;
170 break;
171 case 'f':
172 fs = optarg;
173 break;
174 default:
175 usage();
176 }
177 }
178 argc -= optind;
179 argv += optind;
180
181 if (pflag) {
182 if (soft || hard || grace || dflag)
183 usage();
184 if ((protoid = getentry(protoname, quotatype)) == -1)
185 exit(1);
186 protoprivs = getprivs(protoid, quotatype, fs, 0);
187 for (qup = protoprivs; qup; qup = qup->next) {
188 qup->q2e.q2e_val[QL_BLOCK].q2v_time = 0;
189 qup->q2e.q2e_val[QL_FILE].q2v_time = 0;
190 }
191 while (argc-- > 0) {
192 if ((id = getentry(*argv++, quotatype)) < 0)
193 continue;
194 putprivs(id, quotatype, protoprivs);
195 }
196 exit(0);
197 }
198 if (soft || hard || grace) {
199 struct quotause *lqup;
200 u_int64_t softb, hardb, softi, hardi;
201 time_t graceb, gracei;
202 char *str;
203 if (soft) {
204 str = strsep(&soft, "/");
205 if (str[0] == '\0' || soft == NULL || soft[0] == '\0')
206 usage();
207
208 if (intrd(str, &softb, HN_B) != 0)
209 errx(1, "%s: bad number", str);
210 if (intrd(soft, &softi, 0) != 0)
211 errx(1, "%s: bad number", soft);
212 }
213 if (hard) {
214 str = strsep(&hard, "/");
215 if (str[0] == '\0' || hard == NULL || hard[0] == '\0')
216 usage();
217
218 if (intrd(str, &hardb, HN_B) != 0)
219 errx(1, "%s: bad number", str);
220 if (intrd(hard, &hardi, 0) != 0)
221 errx(1, "%s: bad number", hard);
222 }
223 if (grace) {
224 str = strsep(&grace, "/");
225 if (str[0] == '\0' || grace == NULL || grace[0] == '\0')
226 usage();
227
228 if (timeprd(str, &graceb) != 0)
229 errx(1, "%s: bad number", str);
230 if (timeprd(grace, &gracei) != 0)
231 errx(1, "%s: bad number", grace);
232 }
233 if (dflag) {
234 curprivs = getprivs(0, quotatype, fs, 1);
235 for (lqup = curprivs; lqup; lqup = lqup->next) {
236 if (soft) {
237 lqup->q2e.q2e_val[QL_BLOCK].q2v_softlimit = softb;
238 lqup->q2e.q2e_val[QL_FILE].q2v_softlimit = softi;
239 }
240 if (hard) {
241 lqup->q2e.q2e_val[QL_BLOCK].q2v_hardlimit = hardb;
242 lqup->q2e.q2e_val[QL_FILE].q2v_hardlimit = hardi;
243 }
244 if (grace) {
245 lqup->q2e.q2e_val[QL_BLOCK].q2v_grace = graceb;
246 lqup->q2e.q2e_val[QL_FILE].q2v_grace = gracei;
247 }
248 }
249 putprivs(0, quotatype, curprivs);
250 freeprivs(curprivs);
251 exit(0);
252 }
253 for ( ; argc > 0; argc--, argv++) {
254 if ((id = getentry(*argv, quotatype)) == -1)
255 continue;
256 curprivs = getprivs(id, quotatype, fs, 0);
257 for (lqup = curprivs; lqup; lqup = lqup->next) {
258 if (soft) {
259 if (softb &&
260 lqup->q2e.q2e_val[QL_BLOCK].q2v_cur >= softb &&
261 (lqup->q2e.q2e_val[QL_BLOCK].q2v_softlimit == 0 ||
262 lqup->q2e.q2e_val[QL_BLOCK].q2v_cur < lqup->q2e.q2e_val[QL_BLOCK].q2v_softlimit))
263 lqup->q2e.q2e_val[QL_BLOCK].q2v_time = 0;
264 if (softi &&
265 lqup->q2e.q2e_val[QL_FILE].q2v_cur >= softb &&
266 (lqup->q2e.q2e_val[QL_FILE].q2v_softlimit == 0 ||
267 lqup->q2e.q2e_val[QL_FILE].q2v_cur < lqup->q2e.q2e_val[QL_FILE].q2v_softlimit))
268 lqup->q2e.q2e_val[QL_FILE].q2v_time = 0;
269 lqup->q2e.q2e_val[QL_BLOCK].q2v_softlimit = softb;
270 lqup->q2e.q2e_val[QL_FILE].q2v_softlimit = softi;
271 }
272 if (hard) {
273 lqup->q2e.q2e_val[QL_BLOCK].q2v_hardlimit = hardb;
274 lqup->q2e.q2e_val[QL_FILE].q2v_hardlimit = hardi;
275 }
276 if (grace) {
277 lqup->q2e.q2e_val[QL_BLOCK].q2v_grace = graceb;
278 lqup->q2e.q2e_val[QL_FILE].q2v_grace = gracei;
279 }
280 }
281 putprivs(id, quotatype, curprivs);
282 freeprivs(curprivs);
283 }
284 exit(0);
285 }
286 tmpfd = mkstemp(tmpfil);
287 fchown(tmpfd, getuid(), getgid());
288 if (dflag) {
289 curprivs = getprivs(0, quotatype, fs, 1);
290 if (writeprivs(curprivs, tmpfd, NULL, quotatype) &&
291 editit(tmpfil) && readprivs(curprivs, tmpfd))
292 putprivs(0, quotatype, curprivs);
293 freeprivs(curprivs);
294 }
295 for ( ; argc > 0; argc--, argv++) {
296 if ((id = getentry(*argv, quotatype)) == -1)
297 continue;
298 curprivs = getprivs(id, quotatype, fs, 0);
299 if (writeprivs(curprivs, tmpfd, *argv, quotatype) == 0)
300 continue;
301 if (editit(tmpfil) && readprivs(curprivs, tmpfd))
302 putprivs(id, quotatype, curprivs);
303 freeprivs(curprivs);
304 }
305 close(tmpfd);
306 unlink(tmpfil);
307 exit(0);
308 }
309
310 void
311 usage()
312 {
313 fprintf(stderr,
314 "usage:\n"
315 " edquota [-D] [-H] [-u] [-p username] [-f filesystem] [-d] username ...\n"
316 " edquota [-D] [-H] -g [-p groupname] [-f filesystem] [-d] groupname ...\n"
317 " edquota [-D] [-u] [-f filesystem] [-s b#/i#] [-h b#/i#] [-t t#/t#] \\\n\t[-d] username ...\n"
318 " edquota [-D] -g [-f filesystem] [-s b#/i#] [-h b#/i#] [-t t#/t#] \\\n\t[-d] groupname ...\n"
319 );
320 exit(1);
321 }
322
323 /*
324 * This routine converts a name for a particular quota type to
325 * an identifier. This routine must agree with the kernel routine
326 * getinoquota as to the interpretation of quota types.
327 */
328 int
329 getentry(name, quotatype)
330 const char *name;
331 int quotatype;
332 {
333 struct passwd *pw;
334 struct group *gr;
335
336 if (alldigits(name))
337 return (atoi(name));
338 switch(quotatype) {
339 case USRQUOTA:
340 if ((pw = getpwnam(name)) != NULL)
341 return (pw->pw_uid);
342 warnx("%s: no such user", name);
343 break;
344 case GRPQUOTA:
345 if ((gr = getgrnam(name)) != NULL)
346 return (gr->gr_gid);
347 warnx("%s: no such group", name);
348 break;
349 default:
350 warnx("%d: unknown quota type", quotatype);
351 break;
352 }
353 sleep(1);
354 return (-1);
355 }
356
357 /*
358 * Collect the requested quota information.
359 */
360 struct quotause *
361 getprivs(long id, int quotatype, const char *filesys, int defaultq)
362 {
363 struct statvfs *fst;
364 int nfst, i;
365 struct quotause *qup, *quptail = NULL;
366 struct quotause *quphead = NULL;
367
368 nfst = getmntinfo(&fst, MNT_WAIT);
369 if (nfst == 0)
370 errx(2, "no filesystems mounted!");
371
372 for (i = 0; i < nfst; i++) {
373 if (strcmp(fst[i].f_fstypename, "ffs") != 0 ||
374 (fst[i].f_flag & ST_QUOTA) == 0)
375 continue;
376 if (filesys && strcmp(fst[i].f_mntonname, filesys) != 0 &&
377 strcmp(fst[i].f_mntfromname, filesys) != 0)
378 continue;
379 qup = getprivs2(id, quotatype, fst[i].f_mntonname, defaultq);
380 if (qup == NULL)
381 return NULL;
382 if (quphead == NULL)
383 quphead = qup;
384 else
385 quptail->next = qup;
386 quptail = qup;
387 qup->next = 0;
388 }
389
390 if (filesys && quphead == NULL) {
391 if (defaultq)
392 errx(1, "no default quota for version 1");
393 /* if we get there, filesys is not mounted. try the old way */
394 qup = getprivs1(id, quotatype, filesys);
395 if (qup == NULL)
396 return NULL;
397 if (quphead == NULL)
398 quphead = qup;
399 else
400 quptail->next = qup;
401 quptail = qup;
402 qup->next = 0;
403 }
404 return quphead;
405 }
406
407 struct quotause *
408 getprivs2(long id, int quotatype, const char *filesys, int defaultq)
409 {
410 struct quotause *qup;
411 int8_t version;
412
413 if ((qup = (struct quotause *)malloc(sizeof(*qup))) == NULL)
414 errx(2, "out of memory");
415 memset(qup, 0, sizeof(*qup));
416 strcpy(qup->fsname, filesys);
417 if (defaultq)
418 qup->flags |= DEFAULT;
419 if (!getvfsquota(filesys, &qup->q2e, &version,
420 id, quotatype, defaultq, Dflag)) {
421 /* no entry, get default entry */
422 if (!getvfsquota(filesys, &qup->q2e, &version,
423 id, quotatype, 1, Dflag)) {
424 free(qup);
425 return NULL;
426 }
427 }
428 if (version == 2)
429 qup->flags |= QUOTA2;
430 qup->q2e.q2e_uid = id;
431 return qup;
432 }
433
434 struct quotause *
435 getprivs1(long id, int quotatype, const char *filesys)
436 {
437 struct fstab *fs;
438 char *qfpathname;
439 struct quotause *qup;
440 struct dqblk dqblk;
441 int fd;
442
443 setfsent();
444 while ((fs = getfsent()) != NULL) {
445 if (strcmp(fs->fs_vfstype, "ffs"))
446 continue;
447 if (strcmp(fs->fs_spec, filesys) == 0 ||
448 strcmp(fs->fs_file, filesys) == 0)
449 break;
450 }
451 if (fs == NULL)
452 return NULL;
453
454 if (!hasquota(fs, quotatype, &qfpathname))
455 return NULL;
456 if ((qup = (struct quotause *)malloc(sizeof(*qup))) == NULL)
457 errx(2, "out of memory");
458 strcpy(qup->fsname, fs->fs_file);
459 if ((fd = open(qfpathname, O_RDONLY)) < 0) {
460 fd = open(qfpathname, O_RDWR|O_CREAT, 0640);
461 if (fd < 0 && errno != ENOENT) {
462 warnx("open `%s'", qfpathname);
463 freeq(qup);
464 return NULL;
465 }
466 warnx("Creating quota file %s", qfpathname);
467 sleep(3);
468 (void) fchown(fd, getuid(),
469 getentry(quotagroup, GRPQUOTA));
470 (void) fchmod(fd, 0640);
471 }
472 (void)lseek(fd, (off_t)(id * sizeof(struct dqblk)),
473 SEEK_SET);
474 switch (read(fd, &dqblk, sizeof(struct dqblk))) {
475 case 0: /* EOF */
476 /*
477 * Convert implicit 0 quota (EOF)
478 * into an explicit one (zero'ed dqblk)
479 */
480 memset((caddr_t)&dqblk, 0,
481 sizeof(struct dqblk));
482 break;
483
484 case sizeof(struct dqblk): /* OK */
485 break;
486
487 default: /* ERROR */
488 warn("read error in `%s'", qfpathname);
489 close(fd);
490 freeq(qup);
491 return NULL;
492 }
493 close(fd);
494 qup->qfname = qfpathname;
495 endfsent();
496 dqblk2q2e(&dqblk, &qup->q2e);
497 return (qup);
498 }
499
500 /*
501 * Store the requested quota information.
502 */
503 void
504 putprivs(long id, int quotatype, struct quotause *quplist)
505 {
506 struct quotause *qup;
507
508 for (qup = quplist; qup; qup = qup->next) {
509 if (qup->qfname == NULL)
510 putprivs2(id, quotatype, qup);
511 else
512 putprivs1(id, quotatype, qup);
513 }
514 }
515
516 void
517 putprivs2(long id, int quotatype, struct quotause *qup)
518 {
519
520 prop_dictionary_t dict, data, cmd;
521 prop_array_t cmds, datas;
522 struct plistref pref;
523 int error;
524 int8_t error8;
525
526 qup->q2e.q2e_uid = id;
527 data = q2etoprop(&qup->q2e, (qup->flags & DEFAULT) ? 1 : 0);
528
529 if (data == NULL)
530 err(1, "q2etoprop(id)");
531
532 dict = quota2_prop_create();
533 cmds = prop_array_create();
534 datas = prop_array_create();
535
536 if (dict == NULL || cmds == NULL || datas == NULL) {
537 errx(1, "can't allocate proplist");
538 }
539
540 if (!prop_array_add_and_rel(datas, data))
541 err(1, "prop_array_add(data)");
542
543 if (!quota2_prop_add_command(cmds, "set",
544 qfextension[quotatype], datas))
545 err(1, "prop_add_command");
546 if (!prop_dictionary_set(dict, "commands", cmds))
547 err(1, "prop_dictionary_set(command)");
548 if (Dflag)
549 printf("message to kernel:\n%s\n",
550 prop_dictionary_externalize(dict));
551
552 if (!prop_dictionary_send_syscall(dict, &pref))
553 err(1, "prop_dictionary_send_syscall");
554 prop_object_release(dict);
555
556 if (quotactl(qup->fsname, &pref) != 0)
557 err(1, "quotactl");
558
559 if ((error = prop_dictionary_recv_syscall(&pref, &dict)) != 0) {
560 errx(1, "prop_dictionary_recv_syscall: %s\n",
561 strerror(error));
562 }
563
564 if (Dflag)
565 printf("reply from kernel:\n%s\n",
566 prop_dictionary_externalize(dict));
567
568 if ((error = quota2_get_cmds(dict, &cmds)) != 0) {
569 errx(1, "quota2_get_cmds: %s\n",
570 strerror(error));
571 }
572 /* only one command, no need to iter */
573 cmd = prop_array_get(cmds, 0);
574 if (cmd == NULL)
575 err(1, "prop_array_get(cmd)");
576
577 if (!prop_dictionary_get_int8(cmd, "return", &error8))
578 err(1, "prop_get(return)");
579
580 if (error8) {
581 if (qup->flags & DEFAULT)
582 fprintf(stderr, "set default %s quota: %s\n",
583 qfextension[quotatype], strerror(error8));
584 else
585 fprintf(stderr, "set %s quota for %ld: %s\n",
586 qfextension[quotatype], id, strerror(error8));
587 }
588 prop_object_release(dict);
589 }
590
591 void
592 putprivs1(long id, int quotatype, struct quotause *qup)
593 {
594 struct dqblk dqblk;
595 int fd;
596
597 q2e2dqblk(&qup->q2e, &dqblk);
598 assert((qup->flags & DEFAULT) == 0);
599
600 if ((fd = open(qup->qfname, O_WRONLY)) < 0) {
601 warnx("open `%s'", qup->qfname);
602 } else {
603 (void)lseek(fd,
604 (off_t)(id * (long)sizeof (struct dqblk)),
605 SEEK_SET);
606 if (write(fd, &dqblk, sizeof (struct dqblk)) !=
607 sizeof (struct dqblk))
608 warnx("writing `%s'", qup->qfname);
609 close(fd);
610 }
611 }
612
613 /*
614 * Take a list of privileges and get it edited.
615 */
616 int
617 editit(ltmpfile)
618 char *ltmpfile;
619 {
620 long omask;
621 int pid, lst;
622 char p[MAX_TMPSTR];
623
624 omask = sigblock(sigmask(SIGINT)|sigmask(SIGQUIT)|sigmask(SIGHUP));
625 top:
626 if ((pid = fork()) < 0) {
627
628 if (errno == EPROCLIM) {
629 warnx("You have too many processes");
630 return(0);
631 }
632 if (errno == EAGAIN) {
633 sleep(1);
634 goto top;
635 }
636 warn("fork");
637 return (0);
638 }
639 if (pid == 0) {
640 const char *ed;
641
642 sigsetmask(omask);
643 setgid(getgid());
644 setuid(getuid());
645 if ((ed = getenv("EDITOR")) == (char *)0)
646 ed = _PATH_VI;
647 if (strlen(ed) + strlen(ltmpfile) + 2 >= MAX_TMPSTR) {
648 err (1, "%s", "editor or filename too long");
649 }
650 snprintf (p, MAX_TMPSTR, "%s %s", ed, ltmpfile);
651 execlp(_PATH_BSHELL, _PATH_BSHELL, "-c", p, NULL);
652 err(1, "%s", ed);
653 }
654 waitpid(pid, &lst, 0);
655 sigsetmask(omask);
656 if (!WIFEXITED(lst) || WEXITSTATUS(lst) != 0)
657 return (0);
658 return (1);
659 }
660
661 /*
662 * Convert a quotause list to an ASCII file.
663 */
664 int
665 writeprivs(quplist, outfd, name, quotatype)
666 struct quotause *quplist;
667 int outfd;
668 char *name;
669 int quotatype;
670 {
671 struct quotause *qup;
672 FILE *fd;
673
674 ftruncate(outfd, 0);
675 (void)lseek(outfd, (off_t)0, SEEK_SET);
676 if ((fd = fdopen(dup(outfd), "w")) == NULL)
677 errx(1, "fdopen `%s'", tmpfil);
678 if (dflag) {
679 fprintf(fd, "Default %s quotas:\n", qfextension[quotatype]);
680 } else {
681 fprintf(fd, "Quotas for %s %s:\n",
682 qfextension[quotatype], name);
683 }
684 for (qup = quplist; qup; qup = qup->next) {
685 fprintf(fd, "%s (version %d):\n",
686 qup->fsname, (qup->flags & QUOTA2) ? 2 : 1);
687 if ((qup->flags & DEFAULT) == 0 || (qup->flags & QUOTA2) != 0) {
688 fprintf(fd, "\tblocks in use: %s, "
689 "limits (soft = %s, hard = %s",
690 intprt(qup->q2e.q2e_val[QL_BLOCK].q2v_cur,
691 HN_NOSPACE | HN_B, Hflag, 20),
692 intprt(qup->q2e.q2e_val[QL_BLOCK].q2v_softlimit,
693 HN_NOSPACE | HN_B, Hflag, 20),
694 intprt(qup->q2e.q2e_val[QL_BLOCK].q2v_hardlimit,
695 HN_NOSPACE | HN_B, Hflag, 20));
696 if (qup->flags & QUOTA2)
697 fprintf(fd, ", ");
698 } else
699 fprintf(fd, "\tblocks: (");
700
701 if (qup->flags & (QUOTA2|DEFAULT)) {
702 fprintf(fd, "grace = %s",
703 timepprt(qup->q2e.q2e_val[QL_BLOCK].q2v_grace,
704 Hflag, 20));
705 }
706 fprintf(fd, ")\n");
707 if ((qup->flags & DEFAULT) == 0 || (qup->flags & QUOTA2) != 0) {
708 fprintf(fd, "\tinodes in use: %s, "
709 "limits (soft = %s, hard = %s",
710 intprt(qup->q2e.q2e_val[QL_FILE].q2v_cur,
711 HN_NOSPACE, Hflag, 20),
712 intprt(qup->q2e.q2e_val[QL_FILE].q2v_softlimit,
713 HN_NOSPACE, Hflag, 20),
714 intprt(qup->q2e.q2e_val[QL_FILE].q2v_hardlimit,
715 HN_NOSPACE, Hflag, 20));
716 if (qup->flags & QUOTA2)
717 fprintf(fd, ", ");
718 } else
719 fprintf(fd, "\tinodes: (");
720
721 if (qup->flags & (QUOTA2|DEFAULT)) {
722 fprintf(fd, "grace = %s",
723 timepprt(qup->q2e.q2e_val[QL_FILE].q2v_grace,
724 Hflag, 20));
725 }
726 fprintf(fd, ")\n");
727 }
728 fclose(fd);
729 return (1);
730 }
731
732 /*
733 * Merge changes to an ASCII file into a quotause list.
734 */
735 int
736 readprivs(quplist, infd)
737 struct quotause *quplist;
738 int infd;
739 {
740 struct quotause *qup;
741 FILE *fd;
742 int cnt;
743 char fsp[BUFSIZ];
744 static char line0[BUFSIZ], line1[BUFSIZ], line2[BUFSIZ];
745 static char scurb[BUFSIZ], scuri[BUFSIZ], ssoft[BUFSIZ], shard[BUFSIZ];
746 static char stime[BUFSIZ];
747 uint64_t softb, hardb, softi, hardi;
748 time_t graceb = -1, gracei = -1;
749 int version;
750
751 (void)lseek(infd, (off_t)0, SEEK_SET);
752 fd = fdopen(dup(infd), "r");
753 if (fd == NULL) {
754 warn("Can't re-read temp file");
755 return (0);
756 }
757 /*
758 * Discard title line, then read pairs of lines to process.
759 */
760 (void) fgets(line1, sizeof (line1), fd);
761 while (fgets(line0, sizeof (line0), fd) != NULL &&
762 fgets(line1, sizeof (line2), fd) != NULL &&
763 fgets(line2, sizeof (line2), fd) != NULL) {
764 if (sscanf(line0, "%s (version %d):\n", fsp, &version) != 2) {
765 warnx("%s: bad format", line0);
766 goto out;
767 }
768 #define last_char(str) ((str)[strlen(str) - 1])
769 if (last_char(line1) != '\n') {
770 warnx("%s:%s: bad format", fsp, line1);
771 goto out;
772 }
773 last_char(line1) = '\0';
774 if (last_char(line2) != '\n') {
775 warnx("%s:%s: bad format", fsp, line2);
776 goto out;
777 }
778 last_char(line2) = '\0';
779 if (dflag && version == 1) {
780 if (sscanf(line1,
781 "\tblocks:(grace = %s\n", stime) != 1) {
782 warnx("%s:%s: bad format", fsp, line1);
783 goto out;
784 }
785 if (last_char(stime) != ')') {
786 warnx("%s:%s: bad format", fsp, line1);
787 goto out;
788 }
789 last_char(stime) = '\0';
790 if (timeprd(stime, &graceb) != 0) {
791 warnx("%s:%s: bad number", fsp, stime);
792 goto out;
793 }
794 if (sscanf(line2,
795 "\tinodes:(grace = %s\n", stime) != 1) {
796 warnx("%s:%s: bad format", fsp, line2);
797 goto out;
798 }
799 if (last_char(stime) != ')') {
800 warnx("%s:%s: bad format", fsp, line2);
801 goto out;
802 }
803 last_char(stime) = '\0';
804 if (timeprd(stime, &gracei) != 0) {
805 warnx("%s:%s: bad number", fsp, stime);
806 goto out;
807 }
808 } else {
809 cnt = sscanf(line1,
810 "\tblocks in use: %s limits (soft = %s hard = %s "
811 "grace = %s", scurb, ssoft, shard, stime);
812 if (cnt == 3) {
813 if (version != 1 ||
814 last_char(scurb) != ',' ||
815 last_char(ssoft) != ',' ||
816 last_char(shard) != ')') {
817 warnx("%s:%s: bad format %d",
818 fsp, line1, cnt);
819 goto out;
820 }
821 stime[0] = '\0';
822 } else if (cnt == 4) {
823 if (version < 2 ||
824 last_char(scurb) != ',' ||
825 last_char(ssoft) != ',' ||
826 last_char(shard) != ',' ||
827 last_char(stime) != ')') {
828 warnx("%s:%s: bad format %d",
829 fsp, line1, cnt);
830 goto out;
831 }
832 } else {
833 warnx("%s: %s: bad format cnt %d", fsp, line1, cnt);
834 goto out;
835 }
836 /* drop last char which is ',' or ')' */
837 last_char(scurb) = '\0';
838 last_char(ssoft) = '\0';
839 last_char(shard) = '\0';
840 last_char(stime) = '\0';
841
842 if (intrd(ssoft, &softb, HN_B) != 0) {
843 warnx("%s:%s: bad number", fsp, ssoft);
844 goto out;
845 }
846 if (intrd(shard, &hardb, HN_B) != 0) {
847 warnx("%s:%s: bad number", fsp, shard);
848 goto out;
849 }
850 if (cnt == 4) {
851 if (timeprd(stime, &graceb) != 0) {
852 warnx("%s:%s: bad number", fsp, stime);
853 goto out;
854 }
855 }
856
857 cnt = sscanf(line2,
858 "\tinodes in use: %s limits (soft = %s hard = %s "
859 "grace = %s", scuri, ssoft, shard, stime);
860 if (cnt == 3) {
861 if (version != 1 ||
862 last_char(scuri) != ',' ||
863 last_char(ssoft) != ',' ||
864 last_char(shard) != ')') {
865 warnx("%s:%s: bad format %d",
866 fsp, line2, cnt);
867 goto out;
868 }
869 stime[0] = '\0';
870 } else if (cnt == 4) {
871 if (version < 2 ||
872 last_char(scuri) != ',' ||
873 last_char(ssoft) != ',' ||
874 last_char(shard) != ',' ||
875 last_char(stime) != ')') {
876 warnx("%s:%s: bad format %d",
877 fsp, line2, cnt);
878 goto out;
879 }
880 } else {
881 warnx("%s: %s: bad format", fsp, line2);
882 goto out;
883 }
884 /* drop last char which is ',' or ')' */
885 last_char(scuri) = '\0';
886 last_char(ssoft) = '\0';
887 last_char(shard) = '\0';
888 last_char(stime) = '\0';
889 if (intrd(ssoft, &softi, 0) != 0) {
890 warnx("%s:%s: bad number", fsp, ssoft);
891 goto out;
892 }
893 if (intrd(shard, &hardi, 0) != 0) {
894 warnx("%s:%s: bad number", fsp, shard);
895 goto out;
896 }
897 if (cnt == 4) {
898 if (timeprd(stime, &gracei) != 0) {
899 warnx("%s:%s: bad number", fsp, stime);
900 goto out;
901 }
902 }
903 }
904 for (qup = quplist; qup; qup = qup->next) {
905 if (strcmp(fsp, qup->fsname))
906 continue;
907 if (version == 1 && dflag) {
908 qup->q2e.q2e_val[QL_BLOCK].q2v_grace = graceb;
909 qup->q2e.q2e_val[QL_FILE].q2v_grace = gracei;
910 qup->flags |= FOUND;
911 continue;
912 }
913
914 if (strcmp(intprt(qup->q2e.q2e_val[QL_BLOCK].q2v_cur,
915 HN_NOSPACE | HN_B, Hflag, 20),
916 scurb) != 0 ||
917 strcmp(intprt(qup->q2e.q2e_val[QL_FILE].q2v_cur,
918 HN_NOSPACE, Hflag, 20),
919 scuri) != 0) {
920 warnx("%s: cannot change current allocation",
921 fsp);
922 break;
923 }
924 /*
925 * Cause time limit to be reset when the quota
926 * is next used if previously had no soft limit
927 * or were under it, but now have a soft limit
928 * and are over it.
929 */
930 if (qup->q2e.q2e_val[QL_BLOCK].q2v_cur &&
931 qup->q2e.q2e_val[QL_BLOCK].q2v_cur >= softb &&
932 (qup->q2e.q2e_val[QL_BLOCK].q2v_softlimit == 0 ||
933 qup->q2e.q2e_val[QL_BLOCK].q2v_cur <
934 qup->q2e.q2e_val[QL_BLOCK].q2v_softlimit))
935 qup->q2e.q2e_val[QL_BLOCK].q2v_time = 0;
936 if (qup->q2e.q2e_val[QL_FILE].q2v_cur &&
937 qup->q2e.q2e_val[QL_FILE].q2v_cur >= softi &&
938 (qup->q2e.q2e_val[QL_FILE].q2v_softlimit == 0 ||
939 qup->q2e.q2e_val[QL_FILE].q2v_cur <
940 qup->q2e.q2e_val[QL_FILE].q2v_softlimit))
941 qup->q2e.q2e_val[QL_FILE].q2v_time = 0;
942 qup->q2e.q2e_val[QL_BLOCK].q2v_softlimit = softb;
943 qup->q2e.q2e_val[QL_BLOCK].q2v_hardlimit = hardb;
944 if (version == 2)
945 qup->q2e.q2e_val[QL_BLOCK].q2v_grace = graceb;
946 qup->q2e.q2e_val[QL_FILE].q2v_softlimit = softi;
947 qup->q2e.q2e_val[QL_FILE].q2v_hardlimit = hardi;
948 if (version == 2)
949 qup->q2e.q2e_val[QL_FILE].q2v_grace = gracei;
950 qup->flags |= FOUND;
951 }
952 }
953 out:
954 fclose(fd);
955 /*
956 * Disable quotas for any filesystems that have not been found.
957 */
958 for (qup = quplist; qup; qup = qup->next) {
959 if (qup->flags & FOUND) {
960 qup->flags &= ~FOUND;
961 continue;
962 }
963 qup->q2e.q2e_val[QL_BLOCK].q2v_softlimit = UQUAD_MAX;
964 qup->q2e.q2e_val[QL_BLOCK].q2v_hardlimit = UQUAD_MAX;
965 qup->q2e.q2e_val[QL_BLOCK].q2v_grace = 0;
966 qup->q2e.q2e_val[QL_FILE].q2v_softlimit = UQUAD_MAX;
967 qup->q2e.q2e_val[QL_FILE].q2v_hardlimit = UQUAD_MAX;
968 qup->q2e.q2e_val[QL_FILE].q2v_grace = 0;
969 }
970 return (1);
971 }
972
973 /*
974 * Free a quotause structure.
975 */
976 void
977 freeq(struct quotause *qup)
978 {
979 if (qup->qfname)
980 free(qup->qfname);
981 free(qup);
982 }
983
984 /*
985 * Free a list of quotause structures.
986 */
987 void
988 freeprivs(quplist)
989 struct quotause *quplist;
990 {
991 struct quotause *qup, *nextqup;
992
993 for (qup = quplist; qup; qup = nextqup) {
994 nextqup = qup->next;
995 freeq(qup);
996 }
997 }
998
999 /*
1000 * Check whether a string is completely composed of digits.
1001 */
1002 int
1003 alldigits(s)
1004 const char *s;
1005 {
1006 int c;
1007
1008 c = *s++;
1009 do {
1010 if (!isdigit(c))
1011 return (0);
1012 } while ((c = *s++) != 0);
1013 return (1);
1014 }
1015
1016 /*
1017 * Check to see if a particular legacy quota is to be enabled in fstab
1018 */
1019 int
1020 hasquota(fs, type, qfnamep)
1021 struct fstab *fs;
1022 int type;
1023 char **qfnamep;
1024 {
1025 char *opt;
1026 char *cp;
1027 static char initname, usrname[100], grpname[100];
1028 char *buf;
1029
1030 if (!initname) {
1031 sprintf(usrname, "%s%s", qfextension[USRQUOTA], qfname);
1032 sprintf(grpname, "%s%s", qfextension[GRPQUOTA], qfname);
1033 initname = 1;
1034 }
1035 buf = fs->fs_mntops;
1036 cp = NULL;
1037 for (opt = strtok(buf, ","); opt; opt = strtok(NULL, ",")) {
1038 if ((cp = strchr(opt, '=')) != NULL)
1039 *cp++ = '\0';
1040 if (type == USRQUOTA && strcmp(opt, usrname) == 0)
1041 break;
1042 if (type == GRPQUOTA && strcmp(opt, grpname) == 0)
1043 break;
1044 }
1045 if (!opt) {
1046 *qfnamep = NULL;
1047 return (0);
1048 }
1049 if (cp) {
1050 *qfnamep = malloc(strlen(cp) + 1);
1051 if (*qfnamep == NULL)
1052 err(1, "malloc");
1053 strcpy(*qfnamep, cp);
1054 return (1);
1055 }
1056 *qfnamep = malloc(BUFSIZ);
1057 if (*qfnamep == NULL)
1058 err(1, "malloc");
1059 (void) sprintf(*qfnamep, "%s/%s.%s", fs->fs_file, qfname,
1060 qfextension[type]);
1061 return (1);
1062 }
1063