edquota.c revision 1.29.16.6 1 /* $NetBSD: edquota.c,v 1.29.16.6 2011/01/31 15:26:31 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.6 2011/01/31 15:26:31 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) {
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 (quphead == NULL)
410 quphead = qup;
411 else
412 quptail->next = qup;
413 quptail = qup;
414 qup->next = 0;
415 }
416 return quphead;
417 }
418
419 struct quotause *
420 getprivs2(long id, int quotatype, const char *filesys, int defaultq)
421 {
422 struct quotause *qup;
423 int8_t version;
424
425 if ((qup = (struct quotause *)malloc(sizeof(*qup))) == NULL)
426 errx(2, "out of memory");
427 memset(qup, 0, sizeof(*qup));
428 strcpy(qup->fsname, filesys);
429 if (defaultq)
430 qup->flags |= DEFAULT;
431 if (!getvfsquota(filesys, &qup->q2e, &version,
432 id, quotatype, defaultq, Dflag)) {
433 /* no entry, get default entry */
434 if (!getvfsquota(filesys, &qup->q2e, &version,
435 id, quotatype, 1, Dflag)) {
436 free(qup);
437 return NULL;
438 }
439 }
440 if (version == 2)
441 qup->flags |= QUOTA2;
442 qup->q2e.q2e_uid = id;
443 return qup;
444 }
445
446 struct quotause *
447 getprivs1(long id, int quotatype, const char *filesys)
448 {
449 struct fstab *fs;
450 char *qfpathname;
451 struct quotause *qup;
452 struct dqblk dqblk;
453 int fd;
454
455 setfsent();
456 while ((fs = getfsent()) != NULL) {
457 if (strcmp(fs->fs_vfstype, "ffs"))
458 continue;
459 if (strcmp(fs->fs_spec, filesys) == 0 ||
460 strcmp(fs->fs_file, filesys) == 0)
461 break;
462 }
463 if (fs == NULL)
464 return NULL;
465
466 if (!hasquota(fs, quotatype, &qfpathname))
467 return NULL;
468 if ((qup = (struct quotause *)malloc(sizeof(*qup))) == NULL)
469 errx(2, "out of memory");
470 strcpy(qup->fsname, fs->fs_file);
471 if ((fd = open(qfpathname, O_RDONLY)) < 0) {
472 fd = open(qfpathname, O_RDWR|O_CREAT, 0640);
473 if (fd < 0 && errno != ENOENT) {
474 warnx("open `%s'", qfpathname);
475 freeq(qup);
476 return NULL;
477 }
478 warnx("Creating quota file %s", qfpathname);
479 sleep(3);
480 (void) fchown(fd, getuid(),
481 getentry(quotagroup, GRPQUOTA));
482 (void) fchmod(fd, 0640);
483 }
484 (void)lseek(fd, (off_t)(id * sizeof(struct dqblk)),
485 SEEK_SET);
486 switch (read(fd, &dqblk, sizeof(struct dqblk))) {
487 case 0: /* EOF */
488 /*
489 * Convert implicit 0 quota (EOF)
490 * into an explicit one (zero'ed dqblk)
491 */
492 memset((caddr_t)&dqblk, 0,
493 sizeof(struct dqblk));
494 break;
495
496 case sizeof(struct dqblk): /* OK */
497 break;
498
499 default: /* ERROR */
500 warn("read error in `%s'", qfpathname);
501 close(fd);
502 freeq(qup);
503 return NULL;
504 }
505 close(fd);
506 qup->qfname = qfpathname;
507 endfsent();
508 dqblk2q2e(&dqblk, &qup->q2e);
509 return (qup);
510 }
511
512 /*
513 * Store the requested quota information.
514 */
515 void
516 putprivs(long id, int quotatype, struct quotause *quplist)
517 {
518 struct quotause *qup;
519
520 for (qup = quplist; qup; qup = qup->next) {
521 if (qup->qfname == NULL)
522 putprivs2(id, quotatype, qup);
523 else
524 putprivs1(id, quotatype, qup);
525 }
526 }
527
528 void
529 putprivs2(long id, int quotatype, struct quotause *qup)
530 {
531
532 prop_dictionary_t dict, data, cmd;
533 prop_array_t cmds, datas;
534 struct plistref pref;
535 int error;
536 int8_t error8;
537
538 qup->q2e.q2e_uid = id;
539 data = q2etoprop(&qup->q2e, (qup->flags & DEFAULT) ? 1 : 0);
540
541 if (data == NULL)
542 err(1, "q2etoprop(id)");
543
544 dict = quota2_prop_create();
545 cmds = prop_array_create();
546 datas = prop_array_create();
547
548 if (dict == NULL || cmds == NULL || datas == NULL) {
549 errx(1, "can't allocate proplist");
550 }
551
552 if (!prop_array_add_and_rel(datas, data))
553 err(1, "prop_array_add(data)");
554
555 if (!quota2_prop_add_command(cmds, "set",
556 qfextension[quotatype], datas))
557 err(1, "prop_add_command");
558 if (!prop_dictionary_set(dict, "commands", cmds))
559 err(1, "prop_dictionary_set(command)");
560 if (Dflag)
561 printf("message to kernel:\n%s\n",
562 prop_dictionary_externalize(dict));
563
564 if (!prop_dictionary_send_syscall(dict, &pref))
565 err(1, "prop_dictionary_send_syscall");
566 prop_object_release(dict);
567
568 if (quotactl(qup->fsname, &pref) != 0)
569 err(1, "quotactl");
570
571 if ((error = prop_dictionary_recv_syscall(&pref, &dict)) != 0) {
572 errx(1, "prop_dictionary_recv_syscall: %s\n",
573 strerror(error));
574 }
575
576 if (Dflag)
577 printf("reply from kernel:\n%s\n",
578 prop_dictionary_externalize(dict));
579
580 if ((error = quota2_get_cmds(dict, &cmds)) != 0) {
581 errx(1, "quota2_get_cmds: %s\n",
582 strerror(error));
583 }
584 /* only one command, no need to iter */
585 cmd = prop_array_get(cmds, 0);
586 if (cmd == NULL)
587 err(1, "prop_array_get(cmd)");
588
589 if (!prop_dictionary_get_int8(cmd, "return", &error8))
590 err(1, "prop_get(return)");
591
592 if (error8) {
593 if (qup->flags & DEFAULT)
594 fprintf(stderr, "set default %s quota: %s\n",
595 qfextension[quotatype], strerror(error8));
596 else
597 fprintf(stderr, "set %s quota for %ld: %s\n",
598 qfextension[quotatype], id, strerror(error8));
599 }
600 prop_object_release(dict);
601 }
602
603 void
604 putprivs1(long id, int quotatype, struct quotause *qup)
605 {
606 struct dqblk dqblk;
607 int fd;
608
609 q2e2dqblk(&qup->q2e, &dqblk);
610 assert((qup->flags & DEFAULT) == 0);
611
612 if ((fd = open(qup->qfname, O_WRONLY)) < 0) {
613 warnx("open `%s'", qup->qfname);
614 } else {
615 (void)lseek(fd,
616 (off_t)(id * (long)sizeof (struct dqblk)),
617 SEEK_SET);
618 if (write(fd, &dqblk, sizeof (struct dqblk)) !=
619 sizeof (struct dqblk))
620 warnx("writing `%s'", qup->qfname);
621 close(fd);
622 }
623 }
624
625 /*
626 * Take a list of privileges and get it edited.
627 */
628 int
629 editit(ltmpfile)
630 char *ltmpfile;
631 {
632 long omask;
633 int pid, lst;
634 char p[MAX_TMPSTR];
635
636 omask = sigblock(sigmask(SIGINT)|sigmask(SIGQUIT)|sigmask(SIGHUP));
637 top:
638 if ((pid = fork()) < 0) {
639
640 if (errno == EPROCLIM) {
641 warnx("You have too many processes");
642 return(0);
643 }
644 if (errno == EAGAIN) {
645 sleep(1);
646 goto top;
647 }
648 warn("fork");
649 return (0);
650 }
651 if (pid == 0) {
652 const char *ed;
653
654 sigsetmask(omask);
655 setgid(getgid());
656 setuid(getuid());
657 if ((ed = getenv("EDITOR")) == (char *)0)
658 ed = _PATH_VI;
659 if (strlen(ed) + strlen(ltmpfile) + 2 >= MAX_TMPSTR) {
660 err (1, "%s", "editor or filename too long");
661 }
662 snprintf (p, MAX_TMPSTR, "%s %s", ed, ltmpfile);
663 execlp(_PATH_BSHELL, _PATH_BSHELL, "-c", p, NULL);
664 err(1, "%s", ed);
665 }
666 waitpid(pid, &lst, 0);
667 sigsetmask(omask);
668 if (!WIFEXITED(lst) || WEXITSTATUS(lst) != 0)
669 return (0);
670 return (1);
671 }
672
673 /*
674 * Convert a quotause list to an ASCII file.
675 */
676 int
677 writeprivs(quplist, outfd, name, quotatype)
678 struct quotause *quplist;
679 int outfd;
680 char *name;
681 int quotatype;
682 {
683 struct quotause *qup;
684 FILE *fd;
685
686 ftruncate(outfd, 0);
687 (void)lseek(outfd, (off_t)0, SEEK_SET);
688 if ((fd = fdopen(dup(outfd), "w")) == NULL)
689 errx(1, "fdopen `%s'", tmpfil);
690 if (dflag) {
691 fprintf(fd, "Default %s quotas:\n", qfextension[quotatype]);
692 } else {
693 fprintf(fd, "Quotas for %s %s:\n",
694 qfextension[quotatype], name);
695 }
696 for (qup = quplist; qup; qup = qup->next) {
697 fprintf(fd, "%s: %s %s, limits (soft = %s, hard = %s",
698 qup->fsname, "blocks in use:",
699 intprt(qup->q2e.q2e_val[QL_BLOCK].q2v_cur,
700 HN_NOSPACE | HN_B, Hflag, 20),
701 intprt(qup->q2e.q2e_val[QL_BLOCK].q2v_softlimit,
702 HN_NOSPACE | HN_B, Hflag, 20),
703 intprt(qup->q2e.q2e_val[QL_BLOCK].q2v_hardlimit,
704 HN_NOSPACE | HN_B, Hflag, 20));
705 if (qup->flags & (QUOTA2|DEFAULT)) {
706 fprintf(fd, ", grace = %s",
707 timepprt(qup->q2e.q2e_val[QL_BLOCK].q2v_grace,
708 Hflag, 20));
709 }
710 fprintf(fd, ")\n");
711 fprintf(fd, "%s %s, limits (soft = %s, hard = %s",
712 "\tinodes in use:",
713 intprt(qup->q2e.q2e_val[QL_FILE].q2v_cur,
714 HN_NOSPACE, Hflag, 20),
715 intprt(qup->q2e.q2e_val[QL_FILE].q2v_softlimit,
716 HN_NOSPACE, Hflag, 20),
717 intprt(qup->q2e.q2e_val[QL_FILE].q2v_hardlimit,
718 HN_NOSPACE, Hflag, 20));
719 if (qup->flags & (QUOTA2|DEFAULT)) {
720 fprintf(fd, ", grace = %s",
721 timepprt(qup->q2e.q2e_val[QL_FILE].q2v_grace,
722 Hflag, 20));
723 }
724 fprintf(fd, ")\n");
725 }
726 fclose(fd);
727 return (1);
728 }
729
730 /*
731 * Merge changes to an ASCII file into a quotause list.
732 */
733 int
734 readprivs(quplist, infd)
735 struct quotause *quplist;
736 int infd;
737 {
738 struct quotause *qup;
739 FILE *fd;
740 int cnt;
741 char *cp;
742 char *fsp;
743 static char line1[BUFSIZ], line2[BUFSIZ];
744 static char scurb[BUFSIZ], scuri[BUFSIZ], ssoft[BUFSIZ], shard[BUFSIZ];
745 static char stime[BUFSIZ];
746 uint64_t softb, hardb, softi, hardi;
747 time_t graceb = -1, gracei = -1;
748
749 (void)lseek(infd, (off_t)0, SEEK_SET);
750 fd = fdopen(dup(infd), "r");
751 if (fd == NULL) {
752 warn("Can't re-read temp file");
753 return (0);
754 }
755 /*
756 * Discard title line, then read pairs of lines to process.
757 */
758 (void) fgets(line1, sizeof (line1), fd);
759 while (fgets(line1, sizeof (line1), fd) != NULL &&
760 fgets(line2, sizeof (line2), fd) != NULL) {
761 if ((fsp = strtok(line1, " \t:")) == NULL) {
762 warnx("%s: bad format", line1);
763 goto out;
764 }
765 if ((cp = strtok((char *)0, "\n")) == NULL) {
766 warnx("%s: %s: bad format", fsp,
767 &fsp[strlen(fsp) + 1]);
768 goto out;
769 }
770 #define last_char(str) ((str)[strlen(str) - 1])
771 cnt = sscanf(cp,
772 " blocks in use: %s limits (soft = %s hard = %s "
773 "grace = %s\n", scurb, ssoft, shard, stime);
774 if (cnt == 3) {
775 if (last_char(scurb) != ',' ||
776 last_char(ssoft) != ',' ||
777 last_char(shard) != ')') {
778 warnx("%s:%s: bad format %d", fsp, cp, cnt);
779 goto out;
780 }
781 stime[0] = '\0';
782 } else if (cnt == 4) {
783 if (last_char(scurb) != ',' ||
784 last_char(ssoft) != ',' ||
785 last_char(shard) != ',' ||
786 last_char(stime) != ')') {
787 warnx("%s:%s: bad format %d", fsp, cp, cnt);
788 goto out;
789 }
790 } else {
791 warnx("%s: %s: bad format", fsp, line2);
792 goto out;
793 }
794 /* drop last char which is ',' or ')' */
795 last_char(scurb) = '\0';
796 last_char(ssoft) = '\0';
797 last_char(shard) = '\0';
798 last_char(stime) = '\0';
799
800 if (intrd(ssoft, &softb, HN_B) != 0) {
801 warnx("%s:%s: bad number", fsp, ssoft);
802 goto out;
803 }
804 if (intrd(shard, &hardb, HN_B) != 0) {
805 warnx("%s:%s: bad number", fsp, shard);
806 goto out;
807 }
808 if (cnt == 4) {
809 if (timeprd(stime, &graceb) != 0) {
810 warnx("%s:%s: bad number", fsp, shard);
811 goto out;
812 }
813 }
814
815 if ((cp = strtok(line2, "\n")) == NULL) {
816 warnx("%s: %s: bad format", fsp, line2);
817 goto out;
818 }
819 cnt = sscanf(cp,
820 "\tinodes in use: %s limits (soft = %s hard = %s "
821 "grace = %s\n", scuri, ssoft, shard, stime);
822 if (cnt == 3) {
823 if (last_char(scuri) != ',' ||
824 last_char(ssoft) != ',' ||
825 last_char(shard) != ')') {
826 warnx("%s:%s: bad format %d", fsp, cp, cnt);
827 goto out;
828 }
829 stime[0] = '\0';
830 } else if (cnt == 4) {
831 if (last_char(scuri) != ',' ||
832 last_char(ssoft) != ',' ||
833 last_char(shard) != ',' ||
834 last_char(stime) != ')') {
835 warnx("%s:%s: bad format %d", fsp, cp, cnt);
836 goto out;
837 }
838 } else {
839 warnx("%s: %s: bad format", fsp, line2);
840 goto out;
841 }
842 /* drop last char which is ',' or ')' */
843 last_char(scuri) = '\0';
844 last_char(ssoft) = '\0';
845 last_char(shard) = '\0';
846 last_char(stime) = '\0';
847 if (intrd(ssoft, &softi, 0) != 0) {
848 warnx("%s:%s: bad number", fsp, ssoft);
849 goto out;
850 }
851 if (intrd(shard, &hardi, 0) != 0) {
852 warnx("%s:%s: bad number", fsp, shard);
853 goto out;
854 }
855 if (cnt == 4) {
856 if (timeprd(stime, &gracei) != 0) {
857 warnx("%s:%s: bad number", fsp, shard);
858 goto out;
859 }
860 }
861 for (qup = quplist; qup; qup = qup->next) {
862 if (strcmp(fsp, qup->fsname))
863 continue;
864 if (strcmp(intprt(qup->q2e.q2e_val[QL_BLOCK].q2v_cur,
865 HN_NOSPACE | HN_B, Hflag, 20),
866 scurb) != 0 ||
867 strcmp(intprt(qup->q2e.q2e_val[QL_FILE].q2v_cur,
868 HN_NOSPACE, Hflag, 20),
869 scuri) != 0) {
870 warnx("%s: cannot change current allocation",
871 fsp);
872 break;
873 }
874 /*
875 * Cause time limit to be reset when the quota
876 * is next used if previously had no soft limit
877 * or were under it, but now have a soft limit
878 * and are over it.
879 */
880 if (qup->q2e.q2e_val[QL_BLOCK].q2v_cur &&
881 qup->q2e.q2e_val[QL_BLOCK].q2v_cur >= softb &&
882 (qup->q2e.q2e_val[QL_BLOCK].q2v_softlimit == 0 ||
883 qup->q2e.q2e_val[QL_BLOCK].q2v_cur <
884 qup->q2e.q2e_val[QL_BLOCK].q2v_softlimit))
885 qup->q2e.q2e_val[QL_BLOCK].q2v_time = 0;
886 if (qup->q2e.q2e_val[QL_FILE].q2v_cur &&
887 qup->q2e.q2e_val[QL_FILE].q2v_cur >= softi &&
888 (qup->q2e.q2e_val[QL_FILE].q2v_softlimit == 0 ||
889 qup->q2e.q2e_val[QL_FILE].q2v_cur <
890 qup->q2e.q2e_val[QL_FILE].q2v_softlimit))
891 qup->q2e.q2e_val[QL_FILE].q2v_time = 0;
892 qup->q2e.q2e_val[QL_BLOCK].q2v_softlimit = softb;
893 qup->q2e.q2e_val[QL_BLOCK].q2v_hardlimit = hardb;
894 if (graceb >= 0)
895 qup->q2e.q2e_val[QL_BLOCK].q2v_grace = graceb;
896 qup->q2e.q2e_val[QL_FILE].q2v_softlimit = softi;
897 qup->q2e.q2e_val[QL_FILE].q2v_hardlimit = hardi;
898 if (gracei >= 0)
899 qup->q2e.q2e_val[QL_FILE].q2v_grace = gracei;
900 qup->flags |= FOUND;
901 }
902 }
903 out:
904 fclose(fd);
905 /*
906 * Disable quotas for any filesystems that have not been found.
907 */
908 for (qup = quplist; qup; qup = qup->next) {
909 if (qup->flags & FOUND) {
910 qup->flags &= ~FOUND;
911 continue;
912 }
913 qup->q2e.q2e_val[QL_BLOCK].q2v_softlimit = UQUAD_MAX;
914 qup->q2e.q2e_val[QL_BLOCK].q2v_hardlimit = UQUAD_MAX;
915 qup->q2e.q2e_val[QL_FILE].q2v_softlimit = UQUAD_MAX;
916 qup->q2e.q2e_val[QL_FILE].q2v_hardlimit = UQUAD_MAX;
917 }
918 return (1);
919 }
920
921 /*
922 * Convert a quotause list to an ASCII file of grace times.
923 */
924 int
925 writetimes(quplist, outfd, quotatype)
926 struct quotause *quplist;
927 int outfd;
928 int quotatype;
929 {
930 struct quotause *qup;
931 FILE *fd;
932
933 ftruncate(outfd, 0);
934 (void)lseek(outfd, (off_t)0, SEEK_SET);
935 if ((fd = fdopen(dup(outfd), "w")) == NULL)
936 err(1, "fdopen `%s'", tmpfil);
937 fprintf(fd, "Time units may be: days, hours, minutes, or seconds\n");
938 fprintf(fd, "Grace period before enforcing soft limits for %ss:\n",
939 qfextension[quotatype]);
940 for (qup = quplist; qup; qup = qup->next) {
941 fprintf(fd, "%s: block grace period: %s, ",
942 qup->fsname, cvtstoa(qup->q2e.q2e_val[QL_BLOCK].q2v_time));
943 fprintf(fd, "file grace period: %s\n",
944 cvtstoa(qup->q2e.q2e_val[QL_FILE].q2v_time));
945 }
946 fclose(fd);
947 return (1);
948 }
949
950 /*
951 * Merge changes of grace times in an ASCII file into a quotause list.
952 */
953 int
954 readtimes(quplist, infd)
955 struct quotause *quplist;
956 int infd;
957 {
958 struct quotause *qup;
959 FILE *fd;
960 int cnt;
961 char *cp;
962 long litime, lbtime;
963 time_t itime, btime, iseconds, bseconds;
964 char *fsp, bunits[10], iunits[10], line1[BUFSIZ];
965
966 (void)lseek(infd, (off_t)0, SEEK_SET);
967 fd = fdopen(dup(infd), "r");
968 if (fd == NULL) {
969 warnx("Can't re-read temp file!!");
970 return (0);
971 }
972 /*
973 * Discard two title lines, then read lines to process.
974 */
975 (void) fgets(line1, sizeof (line1), fd);
976 (void) fgets(line1, sizeof (line1), fd);
977 while (fgets(line1, sizeof (line1), fd) != NULL) {
978 if ((fsp = strtok(line1, " \t:")) == NULL) {
979 warnx("%s: bad format", line1);
980 goto bad;
981 }
982 if ((cp = strtok((char *)0, "\n")) == NULL) {
983 warnx("%s: %s: bad format", fsp,
984 &fsp[strlen(fsp) + 1]);
985 goto bad;
986 }
987 cnt = sscanf(cp,
988 " block grace period: %ld %s file grace period: %ld %s",
989 &lbtime, bunits, &litime, iunits);
990 if (cnt != 4) {
991 warnx("%s:%s: bad format", fsp, cp);
992 goto bad;
993 }
994 itime = (time_t)litime;
995 btime = (time_t)lbtime;
996 if (cvtatos(btime, bunits, &bseconds) == 0)
997 goto bad;
998 if (cvtatos(itime, iunits, &iseconds) == 0) {
999 bad:
1000 (void)fclose(fd);
1001 return (0);
1002 }
1003 for (qup = quplist; qup; qup = qup->next) {
1004 if (strcmp(fsp, qup->fsname))
1005 continue;
1006 qup->q2e.q2e_val[QL_BLOCK].q2v_time = bseconds;
1007 qup->q2e.q2e_val[QL_FILE].q2v_time = iseconds;
1008 qup->flags |= FOUND;
1009 break;
1010 }
1011 }
1012 (void)fclose(fd);
1013 /*
1014 * reset default grace periods for any filesystems
1015 * that have not been found.
1016 */
1017 for (qup = quplist; qup; qup = qup->next) {
1018 if (qup->flags & FOUND) {
1019 qup->flags &= ~FOUND;
1020 continue;
1021 }
1022 qup->q2e.q2e_val[QL_BLOCK].q2v_time = 0;
1023 qup->q2e.q2e_val[QL_FILE].q2v_time = 0;
1024 }
1025 return (1);
1026 }
1027
1028 /*
1029 * Convert seconds to ASCII times.
1030 */
1031 char *
1032 cvtstoa(ltime)
1033 time_t ltime;
1034 {
1035 static char buf[20];
1036
1037 if (ltime % (24 * 60 * 60) == 0) {
1038 ltime /= 24 * 60 * 60;
1039 snprintf(buf, sizeof buf, "%ld day%s", (long)ltime,
1040 ltime == 1 ? "" : "s");
1041 } else if (ltime % (60 * 60) == 0) {
1042 ltime /= 60 * 60;
1043 sprintf(buf, "%ld hour%s", (long)ltime, ltime == 1 ? "" : "s");
1044 } else if (ltime % 60 == 0) {
1045 ltime /= 60;
1046 sprintf(buf, "%ld minute%s", (long)ltime, ltime == 1 ? "" : "s");
1047 } else
1048 sprintf(buf, "%ld second%s", (long)ltime, ltime == 1 ? "" : "s");
1049 return (buf);
1050 }
1051
1052 /*
1053 * Convert ASCII input times to seconds.
1054 */
1055 int
1056 cvtatos(ltime, units, seconds)
1057 time_t ltime;
1058 char *units;
1059 time_t *seconds;
1060 {
1061
1062 if (memcmp(units, "second", 6) == 0)
1063 *seconds = ltime;
1064 else if (memcmp(units, "minute", 6) == 0)
1065 *seconds = ltime * 60;
1066 else if (memcmp(units, "hour", 4) == 0)
1067 *seconds = ltime * 60 * 60;
1068 else if (memcmp(units, "day", 3) == 0)
1069 *seconds = ltime * 24 * 60 * 60;
1070 else {
1071 printf("%s: bad units, specify %s\n", units,
1072 "days, hours, minutes, or seconds");
1073 return (0);
1074 }
1075 return (1);
1076 }
1077
1078 /*
1079 * Free a quotause structure.
1080 */
1081 void
1082 freeq(struct quotause *qup)
1083 {
1084 if (qup->qfname)
1085 free(qup->qfname);
1086 free(qup);
1087 }
1088
1089 /*
1090 * Free a list of quotause structures.
1091 */
1092 void
1093 freeprivs(quplist)
1094 struct quotause *quplist;
1095 {
1096 struct quotause *qup, *nextqup;
1097
1098 for (qup = quplist; qup; qup = nextqup) {
1099 nextqup = qup->next;
1100 freeq(qup);
1101 }
1102 }
1103
1104 /*
1105 * Check whether a string is completely composed of digits.
1106 */
1107 int
1108 alldigits(s)
1109 const char *s;
1110 {
1111 int c;
1112
1113 c = *s++;
1114 do {
1115 if (!isdigit(c))
1116 return (0);
1117 } while ((c = *s++) != 0);
1118 return (1);
1119 }
1120
1121 /*
1122 * Check to see if a particular legacy quota is to be enabled in fstab
1123 */
1124 int
1125 hasquota(fs, type, qfnamep)
1126 struct fstab *fs;
1127 int type;
1128 char **qfnamep;
1129 {
1130 char *opt;
1131 char *cp;
1132 static char initname, usrname[100], grpname[100];
1133 char *buf;
1134
1135 if (!initname) {
1136 sprintf(usrname, "%s%s", qfextension[USRQUOTA], qfname);
1137 sprintf(grpname, "%s%s", qfextension[GRPQUOTA], qfname);
1138 initname = 1;
1139 }
1140 buf = fs->fs_mntops;
1141 cp = NULL;
1142 for (opt = strtok(buf, ","); opt; opt = strtok(NULL, ",")) {
1143 if ((cp = strchr(opt, '=')) != NULL)
1144 *cp++ = '\0';
1145 if (type == USRQUOTA && strcmp(opt, usrname) == 0)
1146 break;
1147 if (type == GRPQUOTA && strcmp(opt, grpname) == 0)
1148 break;
1149 }
1150 if (!opt) {
1151 *qfnamep = NULL;
1152 return (0);
1153 }
1154 if (cp) {
1155 *qfnamep = malloc(strlen(cp) + 1);
1156 if (*qfnamep == NULL)
1157 err(1, "malloc");
1158 strcpy(*qfnamep, cp);
1159 return (1);
1160 }
1161 *qfnamep = malloc(BUFSIZ);
1162 if (*qfnamep == NULL)
1163 err(1, "malloc");
1164 (void) sprintf(*qfnamep, "%s/%s.%s", fs->fs_file, qfname,
1165 qfextension[type]);
1166 return (1);
1167 }
1168