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