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