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