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