Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[dragonfly.git] / usr.sbin / edquota / edquota.c
1 /*
2  * Copyright (c) 1980, 1990, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Robert Elz at The University of Melbourne.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *      This product includes software developed by the University of
19  *      California, Berkeley and its contributors.
20  * 4. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  *
36  * @(#) Copyright (c) 1980, 1990, 1993 The Regents of the University of California.  All rights reserved.
37  * @(#)edquota.c        8.1 (Berkeley) 6/6/93
38  * $FreeBSD: src/usr.sbin/edquota/edquota.c,v 1.9.2.6 2002/10/31 22:38:43 iedowse Exp $
39  * $DragonFly: src/usr.sbin/edquota/edquota.c,v 1.2 2003/06/17 04:29:53 dillon Exp $
40  */
41
42 /*
43  * Disk quota editor.
44  */
45 #include <sys/param.h>
46 #include <sys/stat.h>
47 #include <sys/file.h>
48 #include <sys/wait.h>
49 #include <ufs/ufs/quota.h>
50 #include <ctype.h>
51 #include <err.h>
52 #include <errno.h>
53 #include <fstab.h>
54 #include <grp.h>
55 #include <pwd.h>
56 #include <signal.h>
57 #include <stdio.h>
58 #include <stdlib.h>
59 #include <string.h>
60 #include <unistd.h>
61 #include "pathnames.h"
62
63 const char *qfname = QUOTAFILENAME;
64 const char *qfextension[] = INITQFNAMES;
65 const char *quotagroup = QUOTAGROUP;
66 char tmpfil[] = _PATH_TMP;
67
68 struct quotause {
69         struct  quotause *next;
70         long    flags;
71         struct  dqblk dqblk;
72         char    fsname[MAXPATHLEN + 1];
73         char    qfname[1];      /* actually longer */
74 };
75 #define FOUND   0x01
76
77 int alldigits(const char *s);
78 int cvtatos(time_t, char *, time_t *);
79 char *cvtstoa(time_t);
80 int editit(char *);
81 void freeprivs(struct quotause *);
82 int getentry(const char *, int);
83 struct quotause *getprivs(long, int, char *);
84 int hasquota(struct fstab *, int, char **);
85 void putprivs(long, int, struct quotause *);
86 int readprivs(struct quotause *, char *);
87 int readtimes(struct quotause *, char *);
88 static void usage(void);
89 int writetimes(struct quotause *, int, int);
90 int writeprivs(struct quotause *, int, char *, int);
91
92 int
93 main(int argc, char **argv)
94 {
95         struct quotause *qup, *protoprivs, *curprivs;
96         long id, protoid;
97         long long lim;
98         int i, quotatype, range, tmpfd;
99         uid_t startuid, enduid;
100         u_int32_t *limp;
101         char *protoname, *cp, *oldoptarg, ch;
102         int eflag = 0, tflag = 0, pflag = 0;
103         char *fspath = NULL;
104         char buf[30];
105
106         if (argc < 2)
107                 usage();
108         if (getuid())
109                 errx(1, "permission denied");
110         quotatype = USRQUOTA;
111         protoprivs = NULL;
112         while ((ch = getopt(argc, argv, "ugtf:p:e:")) != -1) {
113                 switch(ch) {
114                 case 'f':
115                         fspath = optarg;
116                         break;
117                 case 'p':
118                         protoname = optarg;
119                         pflag++;
120                         break;
121                 case 'g':
122                         quotatype = GRPQUOTA;
123                         break;
124                 case 'u':
125                         quotatype = USRQUOTA;
126                         break;
127                 case 't':
128                         tflag++;
129                         break;
130                 case 'e':
131                         if ((qup = malloc(sizeof(*qup))) == NULL)
132                                 errx(2, "out of memory");
133                         bzero(qup, sizeof(*qup));
134                         i = 0;
135                         oldoptarg = optarg;
136                         for (cp = optarg; (cp = strsep(&optarg, ":")) != NULL;
137                             i++) {
138                                 if (cp != oldoptarg)
139                                         *(cp - 1) = ':';
140                                 limp = NULL;
141                                 switch (i) {
142                                 case 0:
143                                         strlcpy(qup->fsname, cp,
144                                             sizeof(qup->fsname));
145                                         break;
146                                 case 1:
147                                         limp = &qup->dqblk.dqb_bsoftlimit;
148                                         break;
149                                 case 2:
150                                         limp = &qup->dqblk.dqb_bhardlimit;
151                                         break;
152                                 case 3:
153                                         limp = &qup->dqblk.dqb_isoftlimit;
154                                         break;
155                                 case 4:
156                                         limp = &qup->dqblk.dqb_ihardlimit;
157                                         break;
158                                 default:
159                                         warnx("incorrect quota specification: "
160                                             "%s", oldoptarg);
161                                         usage();
162                                         break; /* XXX: report an error */
163                                 }
164                                 if (limp != NULL) {
165                                         lim = strtoll(cp, NULL, 10);
166                                         if (lim < 0 || lim > UINT_MAX)
167                                                 errx(1, "invalid limit value: "
168                                                     "%lld", lim);
169                                         *limp = (u_int32_t)lim;
170                                 }
171                         }
172                         qup->dqblk.dqb_bsoftlimit =
173                             btodb((off_t)qup->dqblk.dqb_bsoftlimit * 1024);
174                         qup->dqblk.dqb_bhardlimit =
175                             btodb((off_t)qup->dqblk.dqb_bhardlimit * 1024);
176                         if (protoprivs == NULL) {
177                                 protoprivs = curprivs = qup;
178                         } else {
179                                 curprivs->next = qup;
180                                 curprivs = qup;
181                         }
182                         eflag++;
183                         pflag++;
184                         break;
185                 default:
186                         usage();
187                 }
188         }
189         argc -= optind;
190         argv += optind;
191         if (pflag) {
192                 if (protoprivs == NULL) {
193                         if ((protoid = getentry(protoname, quotatype)) == -1)
194                                 exit(1);
195                         protoprivs = getprivs(protoid, quotatype, fspath);
196                         for (qup = protoprivs; qup; qup = qup->next) {
197                                 qup->dqblk.dqb_btime = 0;
198                                 qup->dqblk.dqb_itime = 0;
199                         }
200                 }
201                 for (; argc-- > 0; argv++) {
202                         if (strspn(*argv, "0123456789-") == strlen(*argv) &&
203                             (cp = strchr(*argv, '-')) != NULL) {
204                                 *cp++ = '\0';
205                                 startuid = atoi(*argv);
206                                 enduid = atoi(cp);
207                                 if (enduid < startuid)
208                                         errx(1,
209         "ending uid (%d) must be >= starting uid (%d) when using uid ranges",
210                                                 enduid, startuid);
211                                 range = 1;
212                         } else {
213                                 startuid = enduid = 0;
214                                 range = 0;
215                         }
216                         for ( ; startuid <= enduid; startuid++) {
217                                 if (range)
218                                         snprintf(buf, sizeof(buf), "%d",
219                                             startuid);
220                                 else
221                                         snprintf(buf, sizeof(buf), "%s",
222                                                 *argv);
223                                 if ((id = getentry(buf, quotatype)) < 0)
224                                         continue;
225                                 if (eflag) {
226                                         for (qup = protoprivs; qup;
227                                             qup = qup->next) {
228                                                 curprivs = getprivs(id,
229                                                     quotatype, qup->fsname);
230                                                 if (curprivs == NULL)
231                                                         continue;
232                                                 strcpy(qup->qfname,
233                                                     curprivs->qfname);
234                                                 strcpy(qup->fsname,
235                                                     curprivs->fsname);
236                                         }
237                                 }
238                                 putprivs(id, quotatype, protoprivs);                                            
239                         }
240                 }
241                 exit(0);
242         }
243         tmpfd = mkstemp(tmpfil);
244         fchown(tmpfd, getuid(), getgid());
245         if (tflag) {
246                 protoprivs = getprivs(0, quotatype, fspath);
247                 if (writetimes(protoprivs, tmpfd, quotatype) == 0)
248                         exit(1);
249                 if (editit(tmpfil) && readtimes(protoprivs, tmpfil))
250                         putprivs(0, quotatype, protoprivs);
251                 freeprivs(protoprivs);
252                 close(tmpfd);
253                 unlink(tmpfil);
254                 exit(0);
255         }
256         for ( ; argc > 0; argc--, argv++) {
257                 if ((id = getentry(*argv, quotatype)) == -1)
258                         continue;
259                 curprivs = getprivs(id, quotatype, fspath);
260                 if (writeprivs(curprivs, tmpfd, *argv, quotatype) == 0)
261                         continue;
262                 if (editit(tmpfil) && readprivs(curprivs, tmpfil))
263                         putprivs(id, quotatype, curprivs);
264                 freeprivs(curprivs);
265         }
266         close(tmpfd);
267         unlink(tmpfil);
268         exit(0);
269 }
270
271 static void
272 usage()
273 {
274         fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
275                 "usage: edquota [-u] [-f fspath] [-p username] username ...",
276                 "       edquota [-u] -e fspath[:bslim[:bhlim[:islim[:ihlim]]]] [-e ...]",
277                 "               username ...",
278                 "       edquota -g [-f fspath] [-p groupname] groupname ...",
279                 "       edquota -g -e fspath[:bslim[:bhlim[:islim[:ihlim]]]] [-e ...]",
280                 "               groupname ...",
281                 "       edquota [-u] -t [-f fspath]",
282                 "       edquota -g -t [-f fspath]");
283         exit(1);
284 }
285
286 /*
287  * This routine converts a name for a particular quota type to
288  * an identifier. This routine must agree with the kernel routine
289  * getinoquota as to the interpretation of quota types.
290  */
291 int
292 getentry(name, quotatype)
293         const char *name;
294         int quotatype;
295 {
296         struct passwd *pw;
297         struct group *gr;
298
299         if (alldigits(name))
300                 return (atoi(name));
301         switch(quotatype) {
302         case USRQUOTA:
303                 if ((pw = getpwnam(name)))
304                         return (pw->pw_uid);
305                 warnx("%s: no such user", name);
306                 break;
307         case GRPQUOTA:
308                 if ((gr = getgrnam(name)))
309                         return (gr->gr_gid);
310                 warnx("%s: no such group", name);
311                 break;
312         default:
313                 warnx("%d: unknown quota type", quotatype);
314                 break;
315         }
316         sleep(1);
317         return (-1);
318 }
319
320 /*
321  * Collect the requested quota information.
322  */
323 struct quotause *
324 getprivs(id, quotatype, fspath)
325         register long id;
326         int quotatype;
327         char *fspath;
328 {
329         register struct fstab *fs;
330         register struct quotause *qup, *quptail;
331         struct quotause *quphead;
332         int qcmd, qupsize, fd;
333         char *qfpathname;
334         static int warned = 0;
335
336         setfsent();
337         quphead = (struct quotause *)0;
338         qcmd = QCMD(Q_GETQUOTA, quotatype);
339         while ((fs = getfsent())) {
340                 if (fspath && *fspath && strcmp(fspath, fs->fs_spec) &&
341                     strcmp(fspath, fs->fs_file))
342                         continue;
343                 if (strcmp(fs->fs_vfstype, "ufs"))
344                         continue;
345                 if (!hasquota(fs, quotatype, &qfpathname))
346                         continue;
347                 qupsize = sizeof(*qup) + strlen(qfpathname);
348                 if ((qup = (struct quotause *)malloc(qupsize)) == NULL)
349                         errx(2, "out of memory");
350                 if (quotactl(fs->fs_file, qcmd, id, &qup->dqblk) != 0) {
351                         if (errno == EOPNOTSUPP && !warned) {
352                                 warned++;
353                 warnx("warning: quotas are not compiled into this kernel");
354                                 sleep(3);
355                         }
356                         if ((fd = open(qfpathname, O_RDONLY)) < 0) {
357                                 fd = open(qfpathname, O_RDWR|O_CREAT, 0640);
358                                 if (fd < 0 && errno != ENOENT) {
359                                         warn("%s", qfpathname);
360                                         free(qup);
361                                         continue;
362                                 }
363                                 warnx("creating quota file %s", qfpathname);
364                                 sleep(3);
365                                 (void) fchown(fd, getuid(),
366                                     getentry(quotagroup, GRPQUOTA));
367                                 (void) fchmod(fd, 0640);
368                         }
369                         lseek(fd, (long)(id * sizeof(struct dqblk)), L_SET);
370                         switch (read(fd, &qup->dqblk, sizeof(struct dqblk))) {
371                         case 0:                 /* EOF */
372                                 /*
373                                  * Convert implicit 0 quota (EOF)
374                                  * into an explicit one (zero'ed dqblk)
375                                  */
376                                 bzero((caddr_t)&qup->dqblk,
377                                     sizeof(struct dqblk));
378                                 break;
379
380                         case sizeof(struct dqblk):      /* OK */
381                                 break;
382
383                         default:                /* ERROR */
384                                 warn("read error in %s", qfpathname);
385                                 close(fd);
386                                 free(qup);
387                                 continue;
388                         }
389                         close(fd);
390                 }
391                 strcpy(qup->qfname, qfpathname);
392                 strcpy(qup->fsname, fs->fs_file);
393                 if (quphead == NULL)
394                         quphead = qup;
395                 else
396                         quptail->next = qup;
397                 quptail = qup;
398                 qup->next = 0;
399         }
400         endfsent();
401         return (quphead);
402 }
403
404 /*
405  * Store the requested quota information.
406  */
407 void
408 putprivs(id, quotatype, quplist)
409         long id;
410         int quotatype;
411         struct quotause *quplist;
412 {
413         register struct quotause *qup;
414         int qcmd, fd;
415
416         qcmd = QCMD(Q_SETQUOTA, quotatype);
417         for (qup = quplist; qup; qup = qup->next) {
418                 if (quotactl(qup->fsname, qcmd, id, &qup->dqblk) == 0)
419                         continue;
420                 if ((fd = open(qup->qfname, O_WRONLY)) < 0) {
421                         warn("%s", qup->qfname);
422                 } else {
423                         lseek(fd, (long)id * (long)sizeof (struct dqblk), 0);
424                         if (write(fd, &qup->dqblk, sizeof (struct dqblk)) !=
425                             sizeof (struct dqblk)) {
426                                 warn("%s", qup->qfname);
427                         }
428                         close(fd);
429                 }
430         }
431 }
432
433 /*
434  * Take a list of priviledges and get it edited.
435  */
436 int
437 editit(tmpf)
438         char *tmpf;
439 {
440         long omask;
441         int pid, status;
442
443         omask = sigblock(sigmask(SIGINT)|sigmask(SIGQUIT)|sigmask(SIGHUP));
444  top:
445         if ((pid = fork()) < 0) {
446
447                 if (errno == EPROCLIM) {
448                         warnx("you have too many processes");
449                         return(0);
450                 }
451                 if (errno == EAGAIN) {
452                         sleep(1);
453                         goto top;
454                 }
455                 warn("fork");
456                 return (0);
457         }
458         if (pid == 0) {
459                 register const char *ed;
460
461                 sigsetmask(omask);
462                 setgid(getgid());
463                 setuid(getuid());
464                 if ((ed = getenv("EDITOR")) == (char *)0)
465                         ed = _PATH_VI;
466                 execlp(ed, ed, tmpf, (char *)0);
467                 err(1, "%s", ed);
468         }
469         waitpid(pid, &status, 0);
470         sigsetmask(omask);
471         if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
472                 return (0);
473         return (1);
474 }
475
476 /*
477  * Convert a quotause list to an ASCII file.
478  */
479 int
480 writeprivs(quplist, outfd, name, quotatype)
481         struct quotause *quplist;
482         int outfd;
483         char *name;
484         int quotatype;
485 {
486         register struct quotause *qup;
487         FILE *fd;
488
489         ftruncate(outfd, 0);
490         lseek(outfd, 0, L_SET);
491         if ((fd = fdopen(dup(outfd), "w")) == NULL)
492                 err(1, "%s", tmpfil);
493         fprintf(fd, "Quotas for %s %s:\n", qfextension[quotatype], name);
494         for (qup = quplist; qup; qup = qup->next) {
495                 fprintf(fd, "%s: %s %lu, limits (soft = %lu, hard = %lu)\n",
496                     qup->fsname, "kbytes in use:",
497                     (unsigned long)(dbtob(qup->dqblk.dqb_curblocks) / 1024),
498                     (unsigned long)(dbtob(qup->dqblk.dqb_bsoftlimit) / 1024),
499                     (unsigned long)(dbtob(qup->dqblk.dqb_bhardlimit) / 1024));
500                 fprintf(fd, "%s %lu, limits (soft = %lu, hard = %lu)\n",
501                     "\tinodes in use:",
502                     (unsigned long)qup->dqblk.dqb_curinodes,
503                     (unsigned long)qup->dqblk.dqb_isoftlimit,
504                     (unsigned long)qup->dqblk.dqb_ihardlimit);
505         }
506         fclose(fd);
507         return (1);
508 }
509
510 /*
511  * Merge changes to an ASCII file into a quotause list.
512  */
513 int
514 readprivs(quplist, inname)
515         struct quotause *quplist;
516         char *inname;
517 {
518         register struct quotause *qup;
519         FILE *fd;
520         unsigned long bhardlimit, bsoftlimit, curblocks;
521         unsigned long ihardlimit, isoftlimit, curinodes;
522         int cnt;
523         register char *cp;
524         struct dqblk dqblk;
525         char *fsp, line1[BUFSIZ], line2[BUFSIZ];
526
527         fd = fopen(inname, "r");
528         if (fd == NULL) {
529                 warnx("can't re-read temp file!!");
530                 return (0);
531         }
532         /*
533          * Discard title line, then read pairs of lines to process.
534          */
535         (void) fgets(line1, sizeof (line1), fd);
536         while (fgets(line1, sizeof (line1), fd) != NULL &&
537                fgets(line2, sizeof (line2), fd) != NULL) {
538                 if ((fsp = strtok(line1, " \t:")) == NULL) {
539                         warnx("%s: bad format", line1);
540                         return (0);
541                 }
542                 if ((cp = strtok((char *)0, "\n")) == NULL) {
543                         warnx("%s: %s: bad format", fsp, &fsp[strlen(fsp) + 1]);
544                         return (0);
545                 }
546                 cnt = sscanf(cp,
547                     " kbytes in use: %lu, limits (soft = %lu, hard = %lu)",
548                     &curblocks, &bsoftlimit, &bhardlimit);
549                 if (cnt != 3) {
550                         warnx("%s:%s: bad format", fsp, cp);
551                         return (0);
552                 }
553                 dqblk.dqb_curblocks = btodb((off_t)curblocks * 1024);
554                 dqblk.dqb_bsoftlimit = btodb((off_t)bsoftlimit * 1024);
555                 dqblk.dqb_bhardlimit = btodb((off_t)bhardlimit * 1024);
556                 if ((cp = strtok(line2, "\n")) == NULL) {
557                         warnx("%s: %s: bad format", fsp, line2);
558                         return (0);
559                 }
560                 cnt = sscanf(cp,
561                     "\tinodes in use: %lu, limits (soft = %lu, hard = %lu)",
562                     &curinodes, &isoftlimit, &ihardlimit);
563                 if (cnt != 3) {
564                         warnx("%s: %s: bad format", fsp, line2);
565                         return (0);
566                 }
567                 dqblk.dqb_curinodes = curinodes;
568                 dqblk.dqb_isoftlimit = isoftlimit;
569                 dqblk.dqb_ihardlimit = ihardlimit;
570                 for (qup = quplist; qup; qup = qup->next) {
571                         if (strcmp(fsp, qup->fsname))
572                                 continue;
573                         /*
574                          * Cause time limit to be reset when the quota
575                          * is next used if previously had no soft limit
576                          * or were under it, but now have a soft limit
577                          * and are over it.
578                          */
579                         if (dqblk.dqb_bsoftlimit &&
580                             qup->dqblk.dqb_curblocks >= dqblk.dqb_bsoftlimit &&
581                             (qup->dqblk.dqb_bsoftlimit == 0 ||
582                              qup->dqblk.dqb_curblocks <
583                              qup->dqblk.dqb_bsoftlimit))
584                                 qup->dqblk.dqb_btime = 0;
585                         if (dqblk.dqb_isoftlimit &&
586                             qup->dqblk.dqb_curinodes >= dqblk.dqb_isoftlimit &&
587                             (qup->dqblk.dqb_isoftlimit == 0 ||
588                              qup->dqblk.dqb_curinodes <
589                              qup->dqblk.dqb_isoftlimit))
590                                 qup->dqblk.dqb_itime = 0;
591                         qup->dqblk.dqb_bsoftlimit = dqblk.dqb_bsoftlimit;
592                         qup->dqblk.dqb_bhardlimit = dqblk.dqb_bhardlimit;
593                         qup->dqblk.dqb_isoftlimit = dqblk.dqb_isoftlimit;
594                         qup->dqblk.dqb_ihardlimit = dqblk.dqb_ihardlimit;
595                         qup->flags |= FOUND;
596                         if (dqblk.dqb_curblocks == qup->dqblk.dqb_curblocks &&
597                             dqblk.dqb_curinodes == qup->dqblk.dqb_curinodes)
598                                 break;
599                         warnx("%s: cannot change current allocation", fsp);
600                         break;
601                 }
602         }
603         fclose(fd);
604         /*
605          * Disable quotas for any filesystems that have not been found.
606          */
607         for (qup = quplist; qup; qup = qup->next) {
608                 if (qup->flags & FOUND) {
609                         qup->flags &= ~FOUND;
610                         continue;
611                 }
612                 qup->dqblk.dqb_bsoftlimit = 0;
613                 qup->dqblk.dqb_bhardlimit = 0;
614                 qup->dqblk.dqb_isoftlimit = 0;
615                 qup->dqblk.dqb_ihardlimit = 0;
616         }
617         return (1);
618 }
619
620 /*
621  * Convert a quotause list to an ASCII file of grace times.
622  */
623 int
624 writetimes(quplist, outfd, quotatype)
625         struct quotause *quplist;
626         int outfd;
627         int quotatype;
628 {
629         register struct quotause *qup;
630         FILE *fd;
631
632         ftruncate(outfd, 0);
633         lseek(outfd, 0, L_SET);
634         if ((fd = fdopen(dup(outfd), "w")) == NULL)
635                 err(1, "%s", tmpfil);
636         fprintf(fd, "Time units may be: days, hours, minutes, or seconds\n");
637         fprintf(fd, "Grace period before enforcing soft limits for %ss:\n",
638             qfextension[quotatype]);
639         for (qup = quplist; qup; qup = qup->next) {
640                 fprintf(fd, "%s: block grace period: %s, ",
641                     qup->fsname, cvtstoa(qup->dqblk.dqb_btime));
642                 fprintf(fd, "file grace period: %s\n",
643                     cvtstoa(qup->dqblk.dqb_itime));
644         }
645         fclose(fd);
646         return (1);
647 }
648
649 /*
650  * Merge changes of grace times in an ASCII file into a quotause list.
651  */
652 int
653 readtimes(quplist, inname)
654         struct quotause *quplist;
655         char *inname;
656 {
657         register struct quotause *qup;
658         FILE *fd;
659         int cnt;
660         register char *cp;
661         time_t itime, btime, iseconds, bseconds;
662         long l_itime, l_btime;
663         char *fsp, bunits[10], iunits[10], line1[BUFSIZ];
664
665         fd = fopen(inname, "r");
666         if (fd == NULL) {
667                 warnx("can't re-read temp file!!");
668                 return (0);
669         }
670         /*
671          * Discard two title lines, then read lines to process.
672          */
673         (void) fgets(line1, sizeof (line1), fd);
674         (void) fgets(line1, sizeof (line1), fd);
675         while (fgets(line1, sizeof (line1), fd) != NULL) {
676                 if ((fsp = strtok(line1, " \t:")) == NULL) {
677                         warnx("%s: bad format", line1);
678                         return (0);
679                 }
680                 if ((cp = strtok((char *)0, "\n")) == NULL) {
681                         warnx("%s: %s: bad format", fsp, &fsp[strlen(fsp) + 1]);
682                         return (0);
683                 }
684                 cnt = sscanf(cp,
685                     " block grace period: %ld %s file grace period: %ld %s",
686                     &l_btime, bunits, &l_itime, iunits);
687                 if (cnt != 4) {
688                         warnx("%s:%s: bad format", fsp, cp);
689                         return (0);
690                 }
691                 btime = l_btime;
692                 itime = l_itime;
693                 if (cvtatos(btime, bunits, &bseconds) == 0)
694                         return (0);
695                 if (cvtatos(itime, iunits, &iseconds) == 0)
696                         return (0);
697                 for (qup = quplist; qup; qup = qup->next) {
698                         if (strcmp(fsp, qup->fsname))
699                                 continue;
700                         qup->dqblk.dqb_btime = bseconds;
701                         qup->dqblk.dqb_itime = iseconds;
702                         qup->flags |= FOUND;
703                         break;
704                 }
705         }
706         fclose(fd);
707         /*
708          * reset default grace periods for any filesystems
709          * that have not been found.
710          */
711         for (qup = quplist; qup; qup = qup->next) {
712                 if (qup->flags & FOUND) {
713                         qup->flags &= ~FOUND;
714                         continue;
715                 }
716                 qup->dqblk.dqb_btime = 0;
717                 qup->dqblk.dqb_itime = 0;
718         }
719         return (1);
720 }
721
722 /*
723  * Convert seconds to ASCII times.
724  */
725 char *
726 cvtstoa(secs)
727         time_t secs;
728 {
729         static char buf[20];
730
731         if (secs % (24 * 60 * 60) == 0) {
732                 secs /= 24 * 60 * 60;
733                 sprintf(buf, "%ld day%s", (long)secs, secs == 1 ? "" : "s");
734         } else if (secs % (60 * 60) == 0) {
735                 secs /= 60 * 60;
736                 sprintf(buf, "%ld hour%s", (long)secs, secs == 1 ? "" : "s");
737         } else if (secs % 60 == 0) {
738                 secs /= 60;
739                 sprintf(buf, "%ld minute%s", (long)secs, secs == 1 ? "" : "s");
740         } else
741                 sprintf(buf, "%ld second%s", (long)secs, secs == 1 ? "" : "s");
742         return (buf);
743 }
744
745 /*
746  * Convert ASCII input times to seconds.
747  */
748 int
749 cvtatos(period, units, seconds)
750         time_t period;
751         char *units;
752         time_t *seconds;
753 {
754
755         if (bcmp(units, "second", 6) == 0)
756                 *seconds = period;
757         else if (bcmp(units, "minute", 6) == 0)
758                 *seconds = period * 60;
759         else if (bcmp(units, "hour", 4) == 0)
760                 *seconds = period * 60 * 60;
761         else if (bcmp(units, "day", 3) == 0)
762                 *seconds = period * 24 * 60 * 60;
763         else {
764                 printf("%s: bad units, specify %s\n", units,
765                     "days, hours, minutes, or seconds");
766                 return (0);
767         }
768         return (1);
769 }
770
771 /*
772  * Free a list of quotause structures.
773  */
774 void
775 freeprivs(quplist)
776         struct quotause *quplist;
777 {
778         register struct quotause *qup, *nextqup;
779
780         for (qup = quplist; qup; qup = nextqup) {
781                 nextqup = qup->next;
782                 free(qup);
783         }
784 }
785
786 /*
787  * Check whether a string is completely composed of digits.
788  */
789 int
790 alldigits(s)
791         register const char *s;
792 {
793         register int c;
794
795         c = *s++;
796         do {
797                 if (!isdigit(c))
798                         return (0);
799         } while ((c = *s++));
800         return (1);
801 }
802
803 /*
804  * Check to see if a particular quota is to be enabled.
805  */
806 int
807 hasquota(fs, type, qfnamep)
808         register struct fstab *fs;
809         int type;
810         char **qfnamep;
811 {
812         register char *opt;
813         char *cp;
814         static char initname, usrname[100], grpname[100];
815         static char buf[BUFSIZ];
816
817         if (!initname) {
818                 sprintf(usrname, "%s%s", qfextension[USRQUOTA], qfname);
819                 sprintf(grpname, "%s%s", qfextension[GRPQUOTA], qfname);
820                 initname = 1;
821         }
822         strcpy(buf, fs->fs_mntops);
823         for (opt = strtok(buf, ","); opt; opt = strtok(NULL, ",")) {
824                 if ((cp = index(opt, '=')))
825                         *cp++ = '\0';
826                 if (type == USRQUOTA && strcmp(opt, usrname) == 0)
827                         break;
828                 if (type == GRPQUOTA && strcmp(opt, grpname) == 0)
829                         break;
830         }
831         if (!opt)
832                 return (0);
833         if (cp) {
834                 *qfnamep = cp;
835                 return (1);
836         }
837         (void) sprintf(buf, "%s/%s.%s", fs->fs_file, qfname, qfextension[type]);
838         *qfnamep = buf;
839         return (1);
840 }