edquota.c revision 24428
11553Srgrimes/* 21553Srgrimes * Copyright (c) 1980, 1990, 1993 31553Srgrimes * The Regents of the University of California. All rights reserved. 41553Srgrimes * 51553Srgrimes * This code is derived from software contributed to Berkeley by 61553Srgrimes * Robert Elz at The University of Melbourne. 71553Srgrimes * 81553Srgrimes * Redistribution and use in source and binary forms, with or without 91553Srgrimes * modification, are permitted provided that the following conditions 101553Srgrimes * are met: 111553Srgrimes * 1. Redistributions of source code must retain the above copyright 121553Srgrimes * notice, this list of conditions and the following disclaimer. 131553Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 141553Srgrimes * notice, this list of conditions and the following disclaimer in the 151553Srgrimes * documentation and/or other materials provided with the distribution. 161553Srgrimes * 3. All advertising materials mentioning features or use of this software 171553Srgrimes * must display the following acknowledgement: 181553Srgrimes * This product includes software developed by the University of 191553Srgrimes * California, Berkeley and its contributors. 201553Srgrimes * 4. Neither the name of the University nor the names of its contributors 211553Srgrimes * may be used to endorse or promote products derived from this software 221553Srgrimes * without specific prior written permission. 231553Srgrimes * 241553Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 251553Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 261553Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 271553Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 281553Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 291553Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 301553Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 311553Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 321553Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 331553Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 341553Srgrimes * SUCH DAMAGE. 351553Srgrimes */ 361553Srgrimes 371553Srgrimes#ifndef lint 381553Srgrimesstatic char copyright[] = 391553Srgrimes"@(#) Copyright (c) 1980, 1990, 1993\n\ 401553Srgrimes The Regents of the University of California. All rights reserved.\n"; 411553Srgrimes#endif /* not lint */ 421553Srgrimes 431553Srgrimes#ifndef lint 441553Srgrimesstatic char sccsid[] = "@(#)edquota.c 8.1 (Berkeley) 6/6/93"; 451553Srgrimes#endif /* not lint */ 461553Srgrimes 471553Srgrimes/* 481553Srgrimes * Disk quota editor. 491553Srgrimes */ 501553Srgrimes#include <sys/param.h> 511553Srgrimes#include <sys/stat.h> 521553Srgrimes#include <sys/file.h> 531553Srgrimes#include <sys/wait.h> 541553Srgrimes#include <ufs/ufs/quota.h> 551553Srgrimes#include <errno.h> 561553Srgrimes#include <fstab.h> 571553Srgrimes#include <pwd.h> 581553Srgrimes#include <grp.h> 591553Srgrimes#include <ctype.h> 601553Srgrimes#include <stdio.h> 611553Srgrimes#include <string.h> 621553Srgrimes#include <unistd.h> 631553Srgrimes#include "pathnames.h" 641553Srgrimes 651553Srgrimeschar *qfname = QUOTAFILENAME; 661553Srgrimeschar *qfextension[] = INITQFNAMES; 671553Srgrimeschar *quotagroup = QUOTAGROUP; 681553Srgrimeschar tmpfil[] = _PATH_TMP; 691553Srgrimes 701553Srgrimesstruct quotause { 711553Srgrimes struct quotause *next; 721553Srgrimes long flags; 731553Srgrimes struct dqblk dqblk; 741553Srgrimes char fsname[MAXPATHLEN + 1]; 751553Srgrimes char qfname[1]; /* actually longer */ 761553Srgrimes} *getprivs(); 771553Srgrimes#define FOUND 0x01 781553Srgrimes 791553Srgrimesmain(argc, argv) 801553Srgrimes register char **argv; 811553Srgrimes int argc; 821553Srgrimes{ 831553Srgrimes register struct quotause *qup, *protoprivs, *curprivs; 841553Srgrimes extern char *optarg; 851553Srgrimes extern int optind; 861553Srgrimes register long id, protoid; 871553Srgrimes register int quotatype, tmpfd; 8814961Smpp register uid_t startuid, enduid; 8914961Smpp char *protoname, *cp, ch; 901553Srgrimes int tflag = 0, pflag = 0; 9114961Smpp char buf[30]; 921553Srgrimes 931553Srgrimes if (argc < 2) 941553Srgrimes usage(); 951553Srgrimes if (getuid()) { 961553Srgrimes fprintf(stderr, "edquota: permission denied\n"); 971553Srgrimes exit(1); 981553Srgrimes } 991553Srgrimes quotatype = USRQUOTA; 10024428Simp while ((ch = getopt(argc, argv, "ugtp:")) != -1) { 1011553Srgrimes switch(ch) { 1021553Srgrimes case 'p': 1031553Srgrimes protoname = optarg; 1041553Srgrimes pflag++; 1051553Srgrimes break; 1061553Srgrimes case 'g': 1071553Srgrimes quotatype = GRPQUOTA; 1081553Srgrimes break; 1091553Srgrimes case 'u': 1101553Srgrimes quotatype = USRQUOTA; 1111553Srgrimes break; 1121553Srgrimes case 't': 1131553Srgrimes tflag++; 1141553Srgrimes break; 1151553Srgrimes default: 1161553Srgrimes usage(); 1171553Srgrimes } 1181553Srgrimes } 1191553Srgrimes argc -= optind; 1201553Srgrimes argv += optind; 1211553Srgrimes if (pflag) { 1221553Srgrimes if ((protoid = getentry(protoname, quotatype)) == -1) 1231553Srgrimes exit(1); 1241553Srgrimes protoprivs = getprivs(protoid, quotatype); 1251553Srgrimes for (qup = protoprivs; qup; qup = qup->next) { 1261553Srgrimes qup->dqblk.dqb_btime = 0; 1271553Srgrimes qup->dqblk.dqb_itime = 0; 1281553Srgrimes } 1291553Srgrimes while (argc-- > 0) { 13014961Smpp if (isdigit(*argv[0]) && 13114961Smpp (cp = strchr(*argv, '-')) != NULL) { 13214961Smpp *cp++ = '\0'; 13314961Smpp startuid = atoi(*argv); 13414961Smpp enduid = atoi(cp); 13514961Smpp if (enduid < startuid) { 13614961Smpp fprintf(stderr, "edquota: ending uid (%d) must be >= starting uid (%d) when using uid ranges\n", 13714961Smpp enduid, startuid); 13814961Smpp exit(1); 13914961Smpp } 14014961Smpp for ( ; startuid <= enduid; startuid++) { 14114961Smpp snprintf(buf, sizeof(buf), "%d", 14214961Smpp startuid); 14314961Smpp if ((id = getentry(buf, quotatype)) < 0) 14414961Smpp continue; 14514961Smpp putprivs(id, quotatype, protoprivs); 14614961Smpp } 14714961Smpp continue; 14814961Smpp } 1491553Srgrimes if ((id = getentry(*argv++, quotatype)) < 0) 1501553Srgrimes continue; 1511553Srgrimes putprivs(id, quotatype, protoprivs); 1521553Srgrimes } 1531553Srgrimes exit(0); 1541553Srgrimes } 1551553Srgrimes tmpfd = mkstemp(tmpfil); 1561553Srgrimes fchown(tmpfd, getuid(), getgid()); 1571553Srgrimes if (tflag) { 1581553Srgrimes protoprivs = getprivs(0, quotatype); 1591553Srgrimes if (writetimes(protoprivs, tmpfd, quotatype) == 0) 1601553Srgrimes exit(1); 1614782Sache if (editit(tmpfil) && readtimes(protoprivs, tmpfil)) 1621553Srgrimes putprivs(0, quotatype, protoprivs); 1631553Srgrimes freeprivs(protoprivs); 1641553Srgrimes exit(0); 1651553Srgrimes } 1661553Srgrimes for ( ; argc > 0; argc--, argv++) { 1671553Srgrimes if ((id = getentry(*argv, quotatype)) == -1) 1681553Srgrimes continue; 1691553Srgrimes curprivs = getprivs(id, quotatype); 1701553Srgrimes if (writeprivs(curprivs, tmpfd, *argv, quotatype) == 0) 1711553Srgrimes continue; 1724782Sache if (editit(tmpfil) && readprivs(curprivs, tmpfil)) 1731553Srgrimes putprivs(id, quotatype, curprivs); 1741553Srgrimes freeprivs(curprivs); 1751553Srgrimes } 1761553Srgrimes close(tmpfd); 1771553Srgrimes unlink(tmpfil); 1781553Srgrimes exit(0); 1791553Srgrimes} 1801553Srgrimes 1811553Srgrimesusage() 1821553Srgrimes{ 1831553Srgrimes fprintf(stderr, "%s%s%s%s", 1841553Srgrimes "Usage: edquota [-u] [-p username] username ...\n", 1851553Srgrimes "\tedquota -g [-p groupname] groupname ...\n", 1861553Srgrimes "\tedquota [-u] -t\n", "\tedquota -g -t\n"); 1871553Srgrimes exit(1); 1881553Srgrimes} 1891553Srgrimes 1901553Srgrimes/* 1911553Srgrimes * This routine converts a name for a particular quota type to 1921553Srgrimes * an identifier. This routine must agree with the kernel routine 1931553Srgrimes * getinoquota as to the interpretation of quota types. 1941553Srgrimes */ 1951553Srgrimesgetentry(name, quotatype) 1961553Srgrimes char *name; 1971553Srgrimes int quotatype; 1981553Srgrimes{ 1991553Srgrimes struct passwd *pw; 2001553Srgrimes struct group *gr; 2011553Srgrimes 2021553Srgrimes if (alldigits(name)) 2031553Srgrimes return (atoi(name)); 2041553Srgrimes switch(quotatype) { 2051553Srgrimes case USRQUOTA: 2061553Srgrimes if (pw = getpwnam(name)) 2071553Srgrimes return (pw->pw_uid); 2081553Srgrimes fprintf(stderr, "%s: no such user\n", name); 2091553Srgrimes break; 2101553Srgrimes case GRPQUOTA: 2111553Srgrimes if (gr = getgrnam(name)) 2121553Srgrimes return (gr->gr_gid); 2131553Srgrimes fprintf(stderr, "%s: no such group\n", name); 2141553Srgrimes break; 2151553Srgrimes default: 2161553Srgrimes fprintf(stderr, "%d: unknown quota type\n", quotatype); 2171553Srgrimes break; 2181553Srgrimes } 2191553Srgrimes sleep(1); 2201553Srgrimes return (-1); 2211553Srgrimes} 2221553Srgrimes 2231553Srgrimes/* 2241553Srgrimes * Collect the requested quota information. 2251553Srgrimes */ 2261553Srgrimesstruct quotause * 2271553Srgrimesgetprivs(id, quotatype) 2281553Srgrimes register long id; 2291553Srgrimes int quotatype; 2301553Srgrimes{ 2311553Srgrimes register struct fstab *fs; 2321553Srgrimes register struct quotause *qup, *quptail; 2331553Srgrimes struct quotause *quphead; 2341553Srgrimes int qcmd, qupsize, fd; 2351553Srgrimes char *qfpathname; 2361553Srgrimes static int warned = 0; 2371553Srgrimes extern int errno; 2381553Srgrimes 2391553Srgrimes setfsent(); 2401553Srgrimes quphead = (struct quotause *)0; 2411553Srgrimes qcmd = QCMD(Q_GETQUOTA, quotatype); 2421553Srgrimes while (fs = getfsent()) { 2431553Srgrimes if (strcmp(fs->fs_vfstype, "ufs")) 2441553Srgrimes continue; 2451553Srgrimes if (!hasquota(fs, quotatype, &qfpathname)) 2461553Srgrimes continue; 2471553Srgrimes qupsize = sizeof(*qup) + strlen(qfpathname); 2481553Srgrimes if ((qup = (struct quotause *)malloc(qupsize)) == NULL) { 2491553Srgrimes fprintf(stderr, "edquota: out of memory\n"); 2501553Srgrimes exit(2); 2511553Srgrimes } 2521553Srgrimes if (quotactl(fs->fs_file, qcmd, id, &qup->dqblk) != 0) { 2531553Srgrimes if (errno == EOPNOTSUPP && !warned) { 2541553Srgrimes warned++; 2551553Srgrimes fprintf(stderr, "Warning: %s\n", 2561553Srgrimes "Quotas are not compiled into this kernel"); 2571553Srgrimes sleep(3); 2581553Srgrimes } 2591553Srgrimes if ((fd = open(qfpathname, O_RDONLY)) < 0) { 2601553Srgrimes fd = open(qfpathname, O_RDWR|O_CREAT, 0640); 2611553Srgrimes if (fd < 0 && errno != ENOENT) { 2621553Srgrimes perror(qfpathname); 2631553Srgrimes free(qup); 2641553Srgrimes continue; 2651553Srgrimes } 2661553Srgrimes fprintf(stderr, "Creating quota file %s\n", 2671553Srgrimes qfpathname); 2681553Srgrimes sleep(3); 2691553Srgrimes (void) fchown(fd, getuid(), 2701553Srgrimes getentry(quotagroup, GRPQUOTA)); 2711553Srgrimes (void) fchmod(fd, 0640); 2721553Srgrimes } 2731553Srgrimes lseek(fd, (long)(id * sizeof(struct dqblk)), L_SET); 2741553Srgrimes switch (read(fd, &qup->dqblk, sizeof(struct dqblk))) { 2751553Srgrimes case 0: /* EOF */ 2761553Srgrimes /* 2771553Srgrimes * Convert implicit 0 quota (EOF) 2781553Srgrimes * into an explicit one (zero'ed dqblk) 2791553Srgrimes */ 2801553Srgrimes bzero((caddr_t)&qup->dqblk, 2811553Srgrimes sizeof(struct dqblk)); 2821553Srgrimes break; 2831553Srgrimes 2841553Srgrimes case sizeof(struct dqblk): /* OK */ 2851553Srgrimes break; 2861553Srgrimes 2871553Srgrimes default: /* ERROR */ 2881553Srgrimes fprintf(stderr, "edquota: read error in "); 2891553Srgrimes perror(qfpathname); 2901553Srgrimes close(fd); 2911553Srgrimes free(qup); 2921553Srgrimes continue; 2931553Srgrimes } 2941553Srgrimes close(fd); 2951553Srgrimes } 2961553Srgrimes strcpy(qup->qfname, qfpathname); 2971553Srgrimes strcpy(qup->fsname, fs->fs_file); 2981553Srgrimes if (quphead == NULL) 2991553Srgrimes quphead = qup; 3001553Srgrimes else 3011553Srgrimes quptail->next = qup; 3021553Srgrimes quptail = qup; 3031553Srgrimes qup->next = 0; 3041553Srgrimes } 3051553Srgrimes endfsent(); 3061553Srgrimes return (quphead); 3071553Srgrimes} 3081553Srgrimes 3091553Srgrimes/* 3101553Srgrimes * Store the requested quota information. 3111553Srgrimes */ 3121553Srgrimesputprivs(id, quotatype, quplist) 3131553Srgrimes long id; 3141553Srgrimes int quotatype; 3151553Srgrimes struct quotause *quplist; 3161553Srgrimes{ 3171553Srgrimes register struct quotause *qup; 3181553Srgrimes int qcmd, fd; 3191553Srgrimes 3201553Srgrimes qcmd = QCMD(Q_SETQUOTA, quotatype); 3211553Srgrimes for (qup = quplist; qup; qup = qup->next) { 3221553Srgrimes if (quotactl(qup->fsname, qcmd, id, &qup->dqblk) == 0) 3231553Srgrimes continue; 3241553Srgrimes if ((fd = open(qup->qfname, O_WRONLY)) < 0) { 3251553Srgrimes perror(qup->qfname); 3261553Srgrimes } else { 3271553Srgrimes lseek(fd, (long)id * (long)sizeof (struct dqblk), 0); 3281553Srgrimes if (write(fd, &qup->dqblk, sizeof (struct dqblk)) != 3291553Srgrimes sizeof (struct dqblk)) { 3301553Srgrimes fprintf(stderr, "edquota: "); 3311553Srgrimes perror(qup->qfname); 3321553Srgrimes } 3331553Srgrimes close(fd); 3341553Srgrimes } 3351553Srgrimes } 3361553Srgrimes} 3371553Srgrimes 3381553Srgrimes/* 3391553Srgrimes * Take a list of priviledges and get it edited. 3401553Srgrimes */ 3411553Srgrimeseditit(tmpfile) 3421553Srgrimes char *tmpfile; 3431553Srgrimes{ 3441553Srgrimes long omask; 3451553Srgrimes int pid, stat; 3461553Srgrimes extern char *getenv(); 3471553Srgrimes 3481553Srgrimes omask = sigblock(sigmask(SIGINT)|sigmask(SIGQUIT)|sigmask(SIGHUP)); 3491553Srgrimes top: 3501553Srgrimes if ((pid = fork()) < 0) { 3511553Srgrimes extern errno; 3521553Srgrimes 3531553Srgrimes if (errno == EPROCLIM) { 3541553Srgrimes fprintf(stderr, "You have too many processes\n"); 3551553Srgrimes return(0); 3561553Srgrimes } 3571553Srgrimes if (errno == EAGAIN) { 3581553Srgrimes sleep(1); 3591553Srgrimes goto top; 3601553Srgrimes } 3611553Srgrimes perror("fork"); 3621553Srgrimes return (0); 3631553Srgrimes } 3641553Srgrimes if (pid == 0) { 3651553Srgrimes register char *ed; 3661553Srgrimes 3671553Srgrimes sigsetmask(omask); 3681553Srgrimes setgid(getgid()); 3691553Srgrimes setuid(getuid()); 3701553Srgrimes if ((ed = getenv("EDITOR")) == (char *)0) 3711553Srgrimes ed = _PATH_VI; 3721553Srgrimes execlp(ed, ed, tmpfile, 0); 3731553Srgrimes perror(ed); 3741553Srgrimes exit(1); 3751553Srgrimes } 3761553Srgrimes waitpid(pid, &stat, 0); 3771553Srgrimes sigsetmask(omask); 3781553Srgrimes if (!WIFEXITED(stat) || WEXITSTATUS(stat) != 0) 3791553Srgrimes return (0); 3801553Srgrimes return (1); 3811553Srgrimes} 3821553Srgrimes 3831553Srgrimes/* 3841553Srgrimes * Convert a quotause list to an ASCII file. 3851553Srgrimes */ 3861553Srgrimeswriteprivs(quplist, outfd, name, quotatype) 3871553Srgrimes struct quotause *quplist; 3881553Srgrimes int outfd; 3891553Srgrimes char *name; 3901553Srgrimes int quotatype; 3911553Srgrimes{ 3921553Srgrimes register struct quotause *qup; 3931553Srgrimes FILE *fd; 3941553Srgrimes 3951553Srgrimes ftruncate(outfd, 0); 3961553Srgrimes lseek(outfd, 0, L_SET); 3971553Srgrimes if ((fd = fdopen(dup(outfd), "w")) == NULL) { 3981553Srgrimes fprintf(stderr, "edquota: "); 3991553Srgrimes perror(tmpfil); 4001553Srgrimes exit(1); 4011553Srgrimes } 4021553Srgrimes fprintf(fd, "Quotas for %s %s:\n", qfextension[quotatype], name); 4031553Srgrimes for (qup = quplist; qup; qup = qup->next) { 4048325Sbde fprintf(fd, "%s: %s %lu, limits (soft = %lu, hard = %lu)\n", 4051553Srgrimes qup->fsname, "blocks in use:", 4068325Sbde (unsigned long)(dbtob(qup->dqblk.dqb_curblocks) / 1024), 4078325Sbde (unsigned long)(dbtob(qup->dqblk.dqb_bsoftlimit) / 1024), 4088325Sbde (unsigned long)(dbtob(qup->dqblk.dqb_bhardlimit) / 1024)); 4098325Sbde fprintf(fd, "%s %lu, limits (soft = %lu, hard = %lu)\n", 4101553Srgrimes "\tinodes in use:", qup->dqblk.dqb_curinodes, 4111553Srgrimes qup->dqblk.dqb_isoftlimit, qup->dqblk.dqb_ihardlimit); 4121553Srgrimes } 4131553Srgrimes fclose(fd); 4141553Srgrimes return (1); 4151553Srgrimes} 4161553Srgrimes 4171553Srgrimes/* 4181553Srgrimes * Merge changes to an ASCII file into a quotause list. 4191553Srgrimes */ 4204782Sachereadprivs(quplist, inname) 4211553Srgrimes struct quotause *quplist; 4224782Sache char *inname; 4231553Srgrimes{ 4241553Srgrimes register struct quotause *qup; 4251553Srgrimes FILE *fd; 4261553Srgrimes int cnt; 4271553Srgrimes register char *cp; 4281553Srgrimes struct dqblk dqblk; 4291553Srgrimes char *fsp, line1[BUFSIZ], line2[BUFSIZ]; 4301553Srgrimes 4314782Sache fd = fopen(inname, "r"); 4321553Srgrimes if (fd == NULL) { 4331553Srgrimes fprintf(stderr, "Can't re-read temp file!!\n"); 4341553Srgrimes return (0); 4351553Srgrimes } 4361553Srgrimes /* 4371553Srgrimes * Discard title line, then read pairs of lines to process. 4381553Srgrimes */ 4391553Srgrimes (void) fgets(line1, sizeof (line1), fd); 4401553Srgrimes while (fgets(line1, sizeof (line1), fd) != NULL && 4411553Srgrimes fgets(line2, sizeof (line2), fd) != NULL) { 4421553Srgrimes if ((fsp = strtok(line1, " \t:")) == NULL) { 4431553Srgrimes fprintf(stderr, "%s: bad format\n", line1); 4441553Srgrimes return (0); 4451553Srgrimes } 4461553Srgrimes if ((cp = strtok((char *)0, "\n")) == NULL) { 4471553Srgrimes fprintf(stderr, "%s: %s: bad format\n", fsp, 4481553Srgrimes &fsp[strlen(fsp) + 1]); 4491553Srgrimes return (0); 4501553Srgrimes } 4511553Srgrimes cnt = sscanf(cp, 4528325Sbde " blocks in use: %lu, limits (soft = %lu, hard = %lu)", 4531553Srgrimes &dqblk.dqb_curblocks, &dqblk.dqb_bsoftlimit, 4541553Srgrimes &dqblk.dqb_bhardlimit); 4551553Srgrimes if (cnt != 3) { 4561553Srgrimes fprintf(stderr, "%s:%s: bad format\n", fsp, cp); 4571553Srgrimes return (0); 4581553Srgrimes } 4591553Srgrimes dqblk.dqb_curblocks = btodb(dqblk.dqb_curblocks * 1024); 4601553Srgrimes dqblk.dqb_bsoftlimit = btodb(dqblk.dqb_bsoftlimit * 1024); 4611553Srgrimes dqblk.dqb_bhardlimit = btodb(dqblk.dqb_bhardlimit * 1024); 4621553Srgrimes if ((cp = strtok(line2, "\n")) == NULL) { 4631553Srgrimes fprintf(stderr, "%s: %s: bad format\n", fsp, line2); 4641553Srgrimes return (0); 4651553Srgrimes } 4661553Srgrimes cnt = sscanf(cp, 4678325Sbde "\tinodes in use: %lu, limits (soft = %lu, hard = %lu)", 4681553Srgrimes &dqblk.dqb_curinodes, &dqblk.dqb_isoftlimit, 4691553Srgrimes &dqblk.dqb_ihardlimit); 4701553Srgrimes if (cnt != 3) { 4711553Srgrimes fprintf(stderr, "%s: %s: bad format\n", fsp, line2); 4721553Srgrimes return (0); 4731553Srgrimes } 4741553Srgrimes for (qup = quplist; qup; qup = qup->next) { 4751553Srgrimes if (strcmp(fsp, qup->fsname)) 4761553Srgrimes continue; 4771553Srgrimes /* 4781553Srgrimes * Cause time limit to be reset when the quota 4791553Srgrimes * is next used if previously had no soft limit 4801553Srgrimes * or were under it, but now have a soft limit 4811553Srgrimes * and are over it. 4821553Srgrimes */ 4831553Srgrimes if (dqblk.dqb_bsoftlimit && 4841553Srgrimes qup->dqblk.dqb_curblocks >= dqblk.dqb_bsoftlimit && 4851553Srgrimes (qup->dqblk.dqb_bsoftlimit == 0 || 4861553Srgrimes qup->dqblk.dqb_curblocks < 4871553Srgrimes qup->dqblk.dqb_bsoftlimit)) 4881553Srgrimes qup->dqblk.dqb_btime = 0; 4891553Srgrimes if (dqblk.dqb_isoftlimit && 4901553Srgrimes qup->dqblk.dqb_curinodes >= dqblk.dqb_isoftlimit && 4911553Srgrimes (qup->dqblk.dqb_isoftlimit == 0 || 4921553Srgrimes qup->dqblk.dqb_curinodes < 4931553Srgrimes qup->dqblk.dqb_isoftlimit)) 4941553Srgrimes qup->dqblk.dqb_itime = 0; 4951553Srgrimes qup->dqblk.dqb_bsoftlimit = dqblk.dqb_bsoftlimit; 4961553Srgrimes qup->dqblk.dqb_bhardlimit = dqblk.dqb_bhardlimit; 4971553Srgrimes qup->dqblk.dqb_isoftlimit = dqblk.dqb_isoftlimit; 4981553Srgrimes qup->dqblk.dqb_ihardlimit = dqblk.dqb_ihardlimit; 4991553Srgrimes qup->flags |= FOUND; 5001553Srgrimes if (dqblk.dqb_curblocks == qup->dqblk.dqb_curblocks && 5011553Srgrimes dqblk.dqb_curinodes == qup->dqblk.dqb_curinodes) 5021553Srgrimes break; 5031553Srgrimes fprintf(stderr, 5041553Srgrimes "%s: cannot change current allocation\n", fsp); 5051553Srgrimes break; 5061553Srgrimes } 5071553Srgrimes } 5081553Srgrimes fclose(fd); 5091553Srgrimes /* 5101553Srgrimes * Disable quotas for any filesystems that have not been found. 5111553Srgrimes */ 5121553Srgrimes for (qup = quplist; qup; qup = qup->next) { 5131553Srgrimes if (qup->flags & FOUND) { 5141553Srgrimes qup->flags &= ~FOUND; 5151553Srgrimes continue; 5161553Srgrimes } 5171553Srgrimes qup->dqblk.dqb_bsoftlimit = 0; 5181553Srgrimes qup->dqblk.dqb_bhardlimit = 0; 5191553Srgrimes qup->dqblk.dqb_isoftlimit = 0; 5201553Srgrimes qup->dqblk.dqb_ihardlimit = 0; 5211553Srgrimes } 5221553Srgrimes return (1); 5231553Srgrimes} 5241553Srgrimes 5251553Srgrimes/* 5261553Srgrimes * Convert a quotause list to an ASCII file of grace times. 5271553Srgrimes */ 5281553Srgrimeswritetimes(quplist, outfd, quotatype) 5291553Srgrimes struct quotause *quplist; 5301553Srgrimes int outfd; 5311553Srgrimes int quotatype; 5321553Srgrimes{ 5331553Srgrimes register struct quotause *qup; 5341553Srgrimes char *cvtstoa(); 5351553Srgrimes FILE *fd; 5361553Srgrimes 5371553Srgrimes ftruncate(outfd, 0); 5381553Srgrimes lseek(outfd, 0, L_SET); 5391553Srgrimes if ((fd = fdopen(dup(outfd), "w")) == NULL) { 5401553Srgrimes fprintf(stderr, "edquota: "); 5411553Srgrimes perror(tmpfil); 5421553Srgrimes exit(1); 5431553Srgrimes } 5441553Srgrimes fprintf(fd, "Time units may be: days, hours, minutes, or seconds\n"); 5451553Srgrimes fprintf(fd, "Grace period before enforcing soft limits for %ss:\n", 5461553Srgrimes qfextension[quotatype]); 5471553Srgrimes for (qup = quplist; qup; qup = qup->next) { 5481553Srgrimes fprintf(fd, "%s: block grace period: %s, ", 5491553Srgrimes qup->fsname, cvtstoa(qup->dqblk.dqb_btime)); 5501553Srgrimes fprintf(fd, "file grace period: %s\n", 5511553Srgrimes cvtstoa(qup->dqblk.dqb_itime)); 5521553Srgrimes } 5531553Srgrimes fclose(fd); 5541553Srgrimes return (1); 5551553Srgrimes} 5561553Srgrimes 5571553Srgrimes/* 5581553Srgrimes * Merge changes of grace times in an ASCII file into a quotause list. 5591553Srgrimes */ 5604782Sachereadtimes(quplist, inname) 5611553Srgrimes struct quotause *quplist; 5624782Sache char *inname; 5631553Srgrimes{ 5641553Srgrimes register struct quotause *qup; 5651553Srgrimes FILE *fd; 5661553Srgrimes int cnt; 5671553Srgrimes register char *cp; 5681553Srgrimes time_t itime, btime, iseconds, bseconds; 5691553Srgrimes char *fsp, bunits[10], iunits[10], line1[BUFSIZ]; 5701553Srgrimes 5714782Sache fd = fopen(inname, "r"); 5721553Srgrimes if (fd == NULL) { 5731553Srgrimes fprintf(stderr, "Can't re-read temp file!!\n"); 5741553Srgrimes return (0); 5751553Srgrimes } 5761553Srgrimes /* 5771553Srgrimes * Discard two title lines, then read lines to process. 5781553Srgrimes */ 5791553Srgrimes (void) fgets(line1, sizeof (line1), fd); 5801553Srgrimes (void) fgets(line1, sizeof (line1), fd); 5811553Srgrimes while (fgets(line1, sizeof (line1), fd) != NULL) { 5821553Srgrimes if ((fsp = strtok(line1, " \t:")) == NULL) { 5831553Srgrimes fprintf(stderr, "%s: bad format\n", line1); 5841553Srgrimes return (0); 5851553Srgrimes } 5861553Srgrimes if ((cp = strtok((char *)0, "\n")) == NULL) { 5871553Srgrimes fprintf(stderr, "%s: %s: bad format\n", fsp, 5881553Srgrimes &fsp[strlen(fsp) + 1]); 5891553Srgrimes return (0); 5901553Srgrimes } 5911553Srgrimes cnt = sscanf(cp, 5928325Sbde " block grace period: %ld %s file grace period: %ld %s", 5931553Srgrimes &btime, bunits, &itime, iunits); 5941553Srgrimes if (cnt != 4) { 5951553Srgrimes fprintf(stderr, "%s:%s: bad format\n", fsp, cp); 5961553Srgrimes return (0); 5971553Srgrimes } 5981553Srgrimes if (cvtatos(btime, bunits, &bseconds) == 0) 5991553Srgrimes return (0); 6001553Srgrimes if (cvtatos(itime, iunits, &iseconds) == 0) 6011553Srgrimes return (0); 6021553Srgrimes for (qup = quplist; qup; qup = qup->next) { 6031553Srgrimes if (strcmp(fsp, qup->fsname)) 6041553Srgrimes continue; 6051553Srgrimes qup->dqblk.dqb_btime = bseconds; 6061553Srgrimes qup->dqblk.dqb_itime = iseconds; 6071553Srgrimes qup->flags |= FOUND; 6081553Srgrimes break; 6091553Srgrimes } 6101553Srgrimes } 6111553Srgrimes fclose(fd); 6121553Srgrimes /* 6131553Srgrimes * reset default grace periods for any filesystems 6141553Srgrimes * that have not been found. 6151553Srgrimes */ 6161553Srgrimes for (qup = quplist; qup; qup = qup->next) { 6171553Srgrimes if (qup->flags & FOUND) { 6181553Srgrimes qup->flags &= ~FOUND; 6191553Srgrimes continue; 6201553Srgrimes } 6211553Srgrimes qup->dqblk.dqb_btime = 0; 6221553Srgrimes qup->dqblk.dqb_itime = 0; 6231553Srgrimes } 6241553Srgrimes return (1); 6251553Srgrimes} 6261553Srgrimes 6271553Srgrimes/* 6281553Srgrimes * Convert seconds to ASCII times. 6291553Srgrimes */ 6301553Srgrimeschar * 6311553Srgrimescvtstoa(time) 6321553Srgrimes time_t time; 6331553Srgrimes{ 6341553Srgrimes static char buf[20]; 6351553Srgrimes 6361553Srgrimes if (time % (24 * 60 * 60) == 0) { 6371553Srgrimes time /= 24 * 60 * 60; 6388325Sbde sprintf(buf, "%ld day%s", time, time == 1 ? "" : "s"); 6391553Srgrimes } else if (time % (60 * 60) == 0) { 6401553Srgrimes time /= 60 * 60; 6418325Sbde sprintf(buf, "%ld hour%s", time, time == 1 ? "" : "s"); 6421553Srgrimes } else if (time % 60 == 0) { 6431553Srgrimes time /= 60; 6448325Sbde sprintf(buf, "%ld minute%s", time, time == 1 ? "" : "s"); 6451553Srgrimes } else 6468325Sbde sprintf(buf, "%ld second%s", time, time == 1 ? "" : "s"); 6471553Srgrimes return (buf); 6481553Srgrimes} 6491553Srgrimes 6501553Srgrimes/* 6511553Srgrimes * Convert ASCII input times to seconds. 6521553Srgrimes */ 6531553Srgrimescvtatos(time, units, seconds) 6541553Srgrimes time_t time; 6551553Srgrimes char *units; 6561553Srgrimes time_t *seconds; 6571553Srgrimes{ 6581553Srgrimes 6591553Srgrimes if (bcmp(units, "second", 6) == 0) 6601553Srgrimes *seconds = time; 6611553Srgrimes else if (bcmp(units, "minute", 6) == 0) 6621553Srgrimes *seconds = time * 60; 6631553Srgrimes else if (bcmp(units, "hour", 4) == 0) 6641553Srgrimes *seconds = time * 60 * 60; 6651553Srgrimes else if (bcmp(units, "day", 3) == 0) 6661553Srgrimes *seconds = time * 24 * 60 * 60; 6671553Srgrimes else { 6681553Srgrimes printf("%s: bad units, specify %s\n", units, 6691553Srgrimes "days, hours, minutes, or seconds"); 6701553Srgrimes return (0); 6711553Srgrimes } 6721553Srgrimes return (1); 6731553Srgrimes} 6741553Srgrimes 6751553Srgrimes/* 6761553Srgrimes * Free a list of quotause structures. 6771553Srgrimes */ 6781553Srgrimesfreeprivs(quplist) 6791553Srgrimes struct quotause *quplist; 6801553Srgrimes{ 6811553Srgrimes register struct quotause *qup, *nextqup; 6821553Srgrimes 6831553Srgrimes for (qup = quplist; qup; qup = nextqup) { 6841553Srgrimes nextqup = qup->next; 6851553Srgrimes free(qup); 6861553Srgrimes } 6871553Srgrimes} 6881553Srgrimes 6891553Srgrimes/* 6901553Srgrimes * Check whether a string is completely composed of digits. 6911553Srgrimes */ 6921553Srgrimesalldigits(s) 6931553Srgrimes register char *s; 6941553Srgrimes{ 6951553Srgrimes register c; 6961553Srgrimes 6971553Srgrimes c = *s++; 6981553Srgrimes do { 6991553Srgrimes if (!isdigit(c)) 7001553Srgrimes return (0); 7011553Srgrimes } while (c = *s++); 7021553Srgrimes return (1); 7031553Srgrimes} 7041553Srgrimes 7051553Srgrimes/* 7061553Srgrimes * Check to see if a particular quota is to be enabled. 7071553Srgrimes */ 7081553Srgrimeshasquota(fs, type, qfnamep) 7091553Srgrimes register struct fstab *fs; 7101553Srgrimes int type; 7111553Srgrimes char **qfnamep; 7121553Srgrimes{ 7131553Srgrimes register char *opt; 7141553Srgrimes char *cp, *index(), *strtok(); 7151553Srgrimes static char initname, usrname[100], grpname[100]; 7161553Srgrimes static char buf[BUFSIZ]; 7171553Srgrimes 7181553Srgrimes if (!initname) { 7191553Srgrimes sprintf(usrname, "%s%s", qfextension[USRQUOTA], qfname); 7201553Srgrimes sprintf(grpname, "%s%s", qfextension[GRPQUOTA], qfname); 7211553Srgrimes initname = 1; 7221553Srgrimes } 7231553Srgrimes strcpy(buf, fs->fs_mntops); 7241553Srgrimes for (opt = strtok(buf, ","); opt; opt = strtok(NULL, ",")) { 7251553Srgrimes if (cp = index(opt, '=')) 7261553Srgrimes *cp++ = '\0'; 7271553Srgrimes if (type == USRQUOTA && strcmp(opt, usrname) == 0) 7281553Srgrimes break; 7291553Srgrimes if (type == GRPQUOTA && strcmp(opt, grpname) == 0) 7301553Srgrimes break; 7311553Srgrimes } 7321553Srgrimes if (!opt) 7331553Srgrimes return (0); 7341553Srgrimes if (cp) { 7351553Srgrimes *qfnamep = cp; 7361553Srgrimes return (1); 7371553Srgrimes } 7381553Srgrimes (void) sprintf(buf, "%s/%s.%s", fs->fs_file, qfname, qfextension[type]); 7391553Srgrimes *qfnamep = buf; 7401553Srgrimes return (1); 7411553Srgrimes} 742