]> git.cameronkatri.com Git - apple_cmds.git/blob - diskdev_cmds/edquota.tproj/edquota.c
Merge branch 'apple'
[apple_cmds.git] / diskdev_cmds / edquota.tproj / edquota.c
1 /*
2 * Copyright (c) 2002-2005 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23 /*
24 * Copyright (c) 1980, 1990, 1993
25 * The Regents of the University of California. All rights reserved.
26 *
27 * This code is derived from software contributed to Berkeley by
28 * Robert Elz at The University of Melbourne.
29 *
30 * Redistribution and use in source and binary forms, with or without
31 * modification, are permitted provided that the following conditions
32 * are met:
33 * 1. Redistributions of source code must retain the above copyright
34 * notice, this list of conditions and the following disclaimer.
35 * 2. Redistributions in binary form must reproduce the above copyright
36 * notice, this list of conditions and the following disclaimer in the
37 * documentation and/or other materials provided with the distribution.
38 * 3. All advertising materials mentioning features or use of this software
39 * must display the following acknowledgement:
40 * This product includes software developed by the University of
41 * California, Berkeley and its contributors.
42 * 4. Neither the name of the University nor the names of its contributors
43 * may be used to endorse or promote products derived from this software
44 * without specific prior written permission.
45 *
46 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
47 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
48 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
49 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
50 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
51 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
52 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
53 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
54 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
55 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
56 * SUCH DAMAGE.
57 */
58
59 #include <sys/cdefs.h>
60
61 #ifndef lint
62 __unused static char copyright[] =
63 "@(#) Copyright (c) 1980, 1990, 1993\n\
64 The Regents of the University of California. All rights reserved.\n";
65 #endif /* not lint */
66
67 #ifndef lint
68 __unused static char sccsid[] = "@(#)edquota.c 8.3 (Berkeley) 4/27/95";
69 #endif /* not lint */
70
71 /*
72 * Disk quota editor.
73 */
74 #include <sys/param.h>
75 #include <sys/stat.h>
76 #include <sys/file.h>
77 #ifdef __APPLE__
78 #include <sys/mount.h>
79 #endif /* __APPLE__ */
80 #include <sys/wait.h>
81 #include <sys/queue.h>
82 #include <sys/quota.h>
83 #include <errno.h>
84 #include <fstab.h>
85 #include <pwd.h>
86 #include <grp.h>
87 #include <ctype.h>
88 #include <stdio.h>
89 #include <stdlib.h>
90 #include <string.h>
91 #include <unistd.h>
92 #include <signal.h>
93 #include "pathnames.h"
94
95 #ifdef __APPLE__
96 #include <libkern/OSByteOrder.h>
97 #endif /* __APPLE__ */
98
99 #include <libiosexec.h>
100
101 char *qfname = QUOTAFILENAME;
102 char *qfextension[] = INITQFNAMES;
103 char *quotagroup = QUOTAGROUP;
104 char tmpfil[] = _PATH_TMP;
105
106 #ifdef __APPLE__
107 u_int32_t quotamagic[MAXQUOTAS] = INITQMAGICS;
108 #endif /* __APPLE__ */
109
110 struct quotause {
111 struct quotause *next;
112 long flags;
113 struct dqblk dqblk;
114 char fsname[MAXPATHLEN + 1];
115 char qfname[1]; /* actually longer */
116 } *getprivs();
117 #define FOUND 0x01
118
119 int alldigits __P((char *));
120 int cvtatos __P((time_t, char *, time_t *));
121 int editit __P((char *));
122 void freeprivs __P((struct quotause *));
123 int getentry __P((char *, int));
124 int hasquota __P((struct statfs *, int, char **));
125 void putprivs __P((uid_t, int, struct quotause *));
126 int readprivs __P((struct quotause *, int));
127 int readtimes __P((struct quotause *, int));
128 void usage __P((void));
129 int writeprivs __P((struct quotause *, int, char *, int));
130 int writetimes __P((struct quotause *, int, int));
131
132 #ifdef __APPLE__
133 int qfinit(int, struct statfs *, int);
134 int qflookup(int, uid_t, int, struct dqblk *);
135 int qfupdate(int, uid_t, int, struct dqblk *);
136 #endif /* __APPLE__ */
137
138
139 int
140 main(argc, argv)
141 register char **argv;
142 int argc;
143 {
144 register struct quotause *qup, *protoprivs, *curprivs;
145 extern char *optarg;
146 extern int optind;
147 register uid_t id, protoid;
148 register int quotatype, tmpfd;
149 char *protoname = NULL, ch;
150 int tflag = 0, pflag = 0;
151
152 if (argc < 2)
153 usage();
154 if (getuid()) {
155 fprintf(stderr, "edquota: permission denied\n");
156 exit(1);
157 }
158 quotatype = USRQUOTA;
159
160 while ((ch = getopt(argc, argv, "ugtp:")) != EOF) {
161 switch(ch) {
162 case 'p':
163 protoname = optarg;
164 pflag++;
165 break;
166 case 'g':
167 quotatype = GRPQUOTA;
168 break;
169 case 'u':
170 quotatype = USRQUOTA;
171 break;
172 case 't':
173 tflag++;
174 break;
175 default:
176 usage();
177 }
178 }
179 argc -= optind;
180 argv += optind;
181 if (pflag) {
182 if ((protoid = getentry(protoname, quotatype)) == -1)
183 exit(1);
184 protoprivs = getprivs(protoid, quotatype);
185 #ifdef __APPLE__
186 if (protoprivs == (struct quotause *) NULL)
187 exit(0);
188 #endif /* __APPLE__ */
189 for (qup = protoprivs; qup; qup = qup->next) {
190 qup->dqblk.dqb_btime = 0;
191 qup->dqblk.dqb_itime = 0;
192 }
193 while (argc-- > 0) {
194 if ((id = getentry(*argv++, quotatype)) == -1)
195 continue;
196 /*
197 * Set the ID in each disk quota block to match
198 * the ID it's supposed to go with.
199 */
200 for (qup = protoprivs; qup; qup = qup->next) {
201 qup->dqblk.dqb_id = id;
202 }
203 putprivs(id, quotatype, protoprivs);
204 }
205 exit(0);
206 }
207 tmpfd = mkstemp(tmpfil);
208 fchown(tmpfd, getuid(), getgid());
209 if (tflag) {
210 protoprivs = getprivs(0, quotatype);
211 #ifdef __APPLE__
212 if (protoprivs == (struct quotause *) NULL)
213 exit(0);
214 #endif /* __APPLE__ */
215 if (writetimes(protoprivs, tmpfd, quotatype) == 0)
216 exit(1);
217 if (editit(tmpfil)) {
218 /*
219 * Re-open tmpfil to be editor independent.
220 */
221 close(tmpfd);
222 tmpfd = open(tmpfil, O_RDWR, 0);
223 if (tmpfd < 0) {
224 freeprivs(protoprivs);
225 unlink(tmpfil);
226 exit(1);
227 }
228 if (readtimes(protoprivs, tmpfd))
229 putprivs(0, quotatype, protoprivs);
230 }
231 freeprivs(protoprivs);
232 exit(0);
233 }
234 for ( ; argc > 0; argc--, argv++) {
235 if ((id = getentry(*argv, quotatype)) == -1)
236 continue;
237 curprivs = getprivs(id, quotatype);
238 #ifdef __APPLE__
239 if (curprivs == (struct quotause *) NULL)
240 exit(0);
241 #endif /* __APPLE__ */
242
243
244 if (writeprivs(curprivs, tmpfd, *argv, quotatype) == 0) {
245 freeprivs(curprivs);
246 continue;
247 }
248 if (editit(tmpfil)) {
249 /*
250 * Re-open tmpfil to be editor independent.
251 */
252 close(tmpfd);
253 tmpfd = open(tmpfil, O_RDWR, 0);
254 if (tmpfd < 0) {
255 freeprivs(curprivs);
256 unlink(tmpfil);
257 exit(1);
258 }
259 if (readprivs(curprivs, tmpfd))
260 putprivs(id, quotatype, curprivs);
261 }
262 freeprivs(curprivs);
263 }
264 close(tmpfd);
265 unlink(tmpfil);
266 exit(0);
267 }
268
269 void
270 usage()
271 {
272 fprintf(stderr, "%s%s%s%s",
273 "Usage: edquota [-u] [-p username] username ...\n",
274 "\tedquota -g [-p groupname] groupname ...\n",
275 "\tedquota [-u] -t\n", "\tedquota -g -t\n");
276 #ifdef __APPLE__
277 fprintf(stderr, "\nQuota file editing triggers only on filesystems with a\n");
278 fprintf(stderr, "%s.%s or %s.%s file located at its root.\n",
279 QUOTAOPSNAME, qfextension[USRQUOTA],
280 QUOTAOPSNAME, qfextension[GRPQUOTA]);
281 #endif /* __APPLE__ */
282 exit(1);
283 }
284
285 /*
286 * This routine converts a name for a particular quota type to
287 * an identifier. This routine must agree with the kernel routine
288 * getinoquota as to the interpretation of quota types.
289 */
290 int
291 getentry(name, quotatype)
292 char *name;
293 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)) != NULL)
303 return (pw->pw_uid);
304 fprintf(stderr, "%s: no such user\n", name);
305 break;
306 case GRPQUOTA:
307 if ((gr = getgrnam(name)))
308 return (gr->gr_gid);
309 fprintf(stderr, "%s: no such group\n", name);
310 break;
311 default:
312 fprintf(stderr, "%d: unknown quota type\n", quotatype);
313 break;
314 }
315 sleep(1);
316 return (-1);
317 }
318
319 /*
320 * Collect the requested quota information.
321 */
322 #ifdef __APPLE__
323 struct quotause *
324 getprivs(id, quotatype)
325 register uid_t id;
326 int quotatype;
327 {
328 struct statfs *fst;
329 register struct quotause *qup, *quptail;
330 struct quotause *quphead;
331 int qcmd, fd;
332 size_t qupsize;
333 char *qfpathname;
334 size_t qfpathname_len;
335 static int warned = 0;
336 int nfst, i;
337 extern int errno;
338
339
340 quptail = quphead = (struct quotause *)0;
341 qcmd = QCMD(Q_GETQUOTA, quotatype);
342
343 nfst = getmntinfo(&fst, MNT_WAIT);
344 if (nfst==0) {
345 fprintf(stderr, "edquota: no mounted filesystems\n");
346 exit(1);
347 }
348
349 for (i=0; i<nfst; i++) {
350 if (strcmp(fst[i].f_fstypename, "hfs")) {
351 continue;
352 }
353 if (!hasquota(&fst[i], quotatype, &qfpathname))
354 continue;
355 qfpathname_len = strlen(qfpathname);
356 qupsize = sizeof(*qup) + qfpathname_len;
357 if ((qup = (struct quotause *)malloc(qupsize)) == NULL) {
358 fprintf(stderr, "edquota: out of memory\n");
359 exit(2);
360 }
361 if (quotactl(fst[i].f_mntonname, qcmd, id, (char *)&qup->dqblk) != 0) {
362 if (errno == ENOTSUP && !warned) {
363 warned++;
364 fprintf(stderr, "Warning: %s\n",
365 "Quotas are not compiled into this kernel");
366 sleep(3);
367 }
368 if ((fd = open(qfpathname, O_RDONLY)) < 0) {
369 fd = open(qfpathname, O_RDWR|O_CREAT, 0640);
370 if (fd < 0 && errno != ENOENT) {
371 perror(qfpathname);
372 free(qup);
373 continue;
374 }
375 fprintf(stderr, "Creating quota file %s\n",
376 qfpathname);
377 sleep(3);
378 (void) fchown(fd, getuid(),
379 getentry(quotagroup, GRPQUOTA));
380 (void) fchmod(fd, 0640);
381 if (qfinit(fd, &fst[i], quotatype)) {
382 perror(qfpathname);
383 close(fd);
384 free(qup);
385 continue;
386 }
387 }
388 if (qflookup(fd, id, quotatype, &qup->dqblk) != 0) {
389 fprintf(stderr, "edquota: lookup error in ");
390 perror(qfpathname);
391 close(fd);
392 free(qup);
393 continue;
394 }
395 close(fd);
396 }
397 strlcpy(qup->qfname, qfpathname, qfpathname_len); // malloc'd size is correct for this
398 strlcpy(qup->fsname, fst[i].f_mntonname, sizeof(qup->fsname));
399
400 if (quphead == NULL)
401 quphead = qup;
402 else
403 quptail->next = qup;
404 quptail = qup;
405 qup->next = 0;
406 }
407 return (quphead);
408 }
409 #else
410 struct quotause *
411 getprivs(id, quotatype)
412 register long id;
413 int quotatype;
414 {
415 register struct fstab *fs;
416 register struct quotause *qup, *quptail;
417 struct quotause *quphead;
418 int qcmd, fd;
419 size_t qupsize;
420 char *qfpathname;
421 size_t qfpathname_len;
422 static int warned = 0;
423 extern int errno;
424
425 setfsent();
426 quphead = (struct quotause *)0;
427 qcmd = QCMD(Q_GETQUOTA, quotatype);
428 while (fs = getfsent()) {
429 if (!hasquota(fs, quotatype, &qfpathname))
430 continue;
431 qfpathname_len = strlen(qfpathname);
432 qupsize = sizeof(*qup) + qfpathname_len;
433 if ((qup = (struct quotause *)malloc(qupsize)) == NULL) {
434 fprintf(stderr, "edquota: out of memory\n");
435 exit(2);
436 }
437 if (quotactl(fs->fs_file, qcmd, id, &qup->dqblk) != 0) {
438 if (errno == ENOTSUP && !warned) {
439 warned++;
440 fprintf(stderr, "Warning: %s\n",
441 "Quotas are not compiled into this kernel");
442 sleep(3);
443 }
444 if ((fd = open(qfpathname, O_RDONLY)) < 0) {
445 fd = open(qfpathname, O_RDWR|O_CREAT, 0640);
446 if (fd < 0 && errno != ENOENT) {
447 perror(qfpathname);
448 free(qup);
449 continue;
450 }
451 fprintf(stderr, "Creating quota file %s\n",
452 qfpathname);
453 sleep(3);
454 (void) fchown(fd, getuid(),
455 getentry(quotagroup, GRPQUOTA));
456 (void) fchmod(fd, 0640);
457 }
458 lseek(fd, (off_t)(id * sizeof(struct dqblk)), L_SET);
459 switch (read(fd, &qup->dqblk, sizeof(struct dqblk))) {
460 case 0: /* EOF */
461 /*
462 * Convert implicit 0 quota (EOF)
463 * into an explicit one (zero'ed dqblk)
464 */
465 bzero((caddr_t)&qup->dqblk,
466 sizeof(struct dqblk));
467 break;
468
469 case sizeof(struct dqblk): /* OK */
470 break;
471
472 default: /* ERROR */
473 fprintf(stderr, "edquota: read error in ");
474 perror(qfpathname);
475 close(fd);
476 free(qup);
477 continue;
478 }
479 close(fd);
480 }
481 strlcpy(qup->qfname, qfpathname, qfpathname_len); // malloc'd size is correct for this
482 strlcpy(qup->fsname, fs->fs_file, sizeof(qup->fsname));
483
484 if (quphead == NULL)
485 quphead = qup;
486 else
487 quptail->next = qup;
488 quptail = qup;
489 qup->next = 0;
490 }
491 endfsent();
492 return (quphead);
493 }
494 #endif /* __APPLE */
495
496 #ifdef __APPLE__
497 #define ONEGIGABYTE (1024*1024*1024)
498 /*
499 * Initialize a new quota file.
500 */
501 int
502 qfinit(fd, fst, type)
503 int fd;
504 struct statfs *fst;
505 int type;
506 {
507 struct dqfilehdr dqhdr = {0};
508 u_int64_t fs_size;
509 int64_t max = 0;
510
511 /*
512 * Calculate the size of the hash table from the size of
513 * the file system. Note that the open addressing hashing
514 * used by the quota file assumes that this table will not
515 * be more than 90% full.
516 */
517 fs_size = (u_int64_t)fst->f_blocks * (u_int64_t)fst->f_bsize;
518
519 if (type == USRQUOTA) {
520 max = QF_USERS_PER_GB * (fs_size / ONEGIGABYTE);
521
522 if (max < QF_MIN_USERS)
523 max = QF_MIN_USERS;
524 else if (max > QF_MAX_USERS)
525 max = QF_MAX_USERS;
526 } else if (type == GRPQUOTA) {
527 max = QF_GROUPS_PER_GB * (fs_size / ONEGIGABYTE);
528
529 if (max < QF_MIN_GROUPS)
530 max = QF_MIN_GROUPS;
531 else if (max > QF_MAX_GROUPS)
532 max = QF_MAX_GROUPS;
533 }
534 /* Round up to a power of 2 */
535 if (max && !powerof2(max)) {
536 int64_t x = max;
537 max = 4;
538 while (x>>1 != 1) {
539 x = x >> 1;
540 max = max << 1;
541 }
542 }
543
544 (void) ftruncate(fd, (off_t)((max + 1) * sizeof(struct dqblk)));
545 dqhdr.dqh_magic = OSSwapHostToBigInt32(quotamagic[type]);
546 dqhdr.dqh_version = OSSwapHostToBigConstInt32(QF_VERSION);
547 dqhdr.dqh_maxentries = OSSwapHostToBigInt32((int32_t)max);
548 dqhdr.dqh_btime = OSSwapHostToBigConstInt32(MAX_DQ_TIME);
549 dqhdr.dqh_itime = OSSwapHostToBigConstInt32(MAX_IQ_TIME);
550 memmove(dqhdr.dqh_string, QF_STRING_TAG, strlen(QF_STRING_TAG));
551 (void) lseek(fd, 0, L_SET);
552 (void) write(fd, &dqhdr, sizeof(dqhdr));
553
554 return (0);
555 }
556
557 /*
558 * Lookup an entry in a quota file.
559 */
560 int
561 qflookup(fd, id, type, dqbp)
562 int fd;
563 uid_t id;
564 int type;
565 struct dqblk *dqbp;
566 {
567 struct dqfilehdr dqhdr;
568 int i, skip, last, m;
569 int mask;
570
571 bzero(dqbp, sizeof(struct dqblk));
572
573 if (id == 0)
574 return (0);
575
576 lseek(fd, 0, L_SET);
577 if (read(fd, &dqhdr, sizeof(struct dqfilehdr)) != sizeof(struct dqfilehdr))
578 return (-1);
579
580 /* Sanity check the quota file header. */
581 if ((OSSwapBigToHostInt32(dqhdr.dqh_magic) != quotamagic[type]) ||
582 (OSSwapBigToHostInt32(dqhdr.dqh_version) > 1) ||
583 (!powerof2(OSSwapBigToHostInt32(dqhdr.dqh_maxentries)))) {
584 fprintf(stderr, "quota: invalid quota file header\n");
585 return (-1);
586 }
587
588 m = OSSwapBigToHostInt32(dqhdr.dqh_maxentries);
589 mask = m - 1;
590 i = dqhash1(id, dqhashshift(m), mask);
591 skip = dqhash2(id, mask);
592
593 for (last = (i + (m-1) * skip) & mask;
594 i != last;
595 i = (i + skip) & mask) {
596 lseek(fd, dqoffset(i), L_SET);
597 if (read(fd, dqbp, sizeof(struct dqblk)) < sizeof(struct dqblk))
598 return (-1);
599 /*
600 * Stop when an empty entry is found
601 * or we encounter a matching id.
602 */
603 if (dqbp->dqb_id == 0 || OSSwapBigToHostInt32(dqbp->dqb_id) == id)
604 break;
605 }
606 /* Put data in host native byte order. */
607 dqbp->dqb_bhardlimit = OSSwapBigToHostInt64(dqbp->dqb_bhardlimit);
608 dqbp->dqb_bsoftlimit = OSSwapBigToHostInt64(dqbp->dqb_bsoftlimit);
609 dqbp->dqb_curbytes = OSSwapBigToHostInt64(dqbp->dqb_curbytes);
610 dqbp->dqb_ihardlimit = OSSwapBigToHostInt32(dqbp->dqb_ihardlimit);
611 dqbp->dqb_isoftlimit = OSSwapBigToHostInt32(dqbp->dqb_isoftlimit);
612 dqbp->dqb_curinodes = OSSwapBigToHostInt32(dqbp->dqb_curinodes);
613 dqbp->dqb_btime = OSSwapBigToHostInt32(dqbp->dqb_btime);
614 dqbp->dqb_itime = OSSwapBigToHostInt32(dqbp->dqb_itime);
615 dqbp->dqb_id = OSSwapBigToHostInt32(dqbp->dqb_id);
616
617 return (0);
618 }
619 #endif /* __APPLE */
620
621
622 /*
623 * Store the requested quota information.
624 */
625 void
626 putprivs(id, quotatype, quplist)
627 uid_t id;
628 int quotatype;
629 struct quotause *quplist;
630 {
631 register struct quotause *qup;
632 int qcmd, fd;
633
634 qcmd = QCMD(Q_SETQUOTA, quotatype);
635 for (qup = quplist; qup; qup = qup->next) {
636 if (quotactl(qup->fsname, qcmd, id, (char *)&qup->dqblk) == 0)
637 continue;
638 #ifdef __APPLE__
639 if ((fd = open(qup->qfname, O_RDWR)) < 0) {
640 perror(qup->qfname);
641 } else {
642 if (qfupdate(fd, id, quotatype, &qup->dqblk) != 0) {
643 fprintf(stderr, "edquota: ");
644 perror(qup->qfname);
645 }
646 #else
647 if ((fd = open(qup->qfname, O_WRONLY)) < 0) {
648 perror(qup->qfname);
649 } else {
650 lseek(fd,
651 (off_t)(id * (long)sizeof (struct dqblk)), L_SET);
652 if (write(fd, &qup->dqblk, sizeof (struct dqblk)) !=
653 sizeof (struct dqblk)) {
654 fprintf(stderr, "edquota: ");
655 perror(qup->qfname);
656 }
657 #endif /* __APPLE */
658 close(fd);
659 }
660 }
661 }
662
663 #ifdef __APPLE__
664 /*
665 * Update an entry in a quota file.
666 */
667 int
668 qfupdate(fd, id, type, dqbp)
669 int fd;
670 uid_t id;
671 int type;
672 struct dqblk *dqbp;
673 {
674 struct dqblk dqbuf;
675 struct dqfilehdr dqhdr;
676 int i, skip, last, m;
677 unsigned int mask;
678
679 if (id == 0)
680 return (0);
681
682 lseek(fd, 0, L_SET);
683 if (read(fd, &dqhdr, sizeof(struct dqfilehdr)) != sizeof(struct dqfilehdr))
684 return (-1);
685
686 /* Sanity check the quota file header. */
687 if ((OSSwapBigToHostInt32(dqhdr.dqh_magic) != quotamagic[type]) ||
688 (OSSwapBigToHostInt32(dqhdr.dqh_version) > QF_VERSION) ||
689 (!powerof2(OSSwapBigToHostInt32(dqhdr.dqh_maxentries)))) {
690 fprintf(stderr, "quota: invalid quota file header\n");
691 return (EINVAL);
692 }
693
694 m = OSSwapBigToHostInt32(dqhdr.dqh_maxentries);
695 mask = m - 1;
696 i = dqhash1(id, dqhashshift(m), mask);
697 skip = dqhash2(id, mask);
698
699 for (last = (i + (m-1) * skip) & mask;
700 i != last;
701 i = (i + skip) & mask) {
702 lseek(fd, dqoffset(i), L_SET);
703 if (read(fd, &dqbuf, sizeof(struct dqblk)) < sizeof(struct dqblk))
704 return (-1);
705 /*
706 * Stop when an empty entry is found
707 * or we encounter a matching id.
708 */
709 if (dqbuf.dqb_id == 0 || OSSwapBigToHostInt32(dqbuf.dqb_id) == id) {
710 /* Convert buffer to big endian before writing. */
711 struct dqblk tblk;
712
713 tblk.dqb_bhardlimit = OSSwapHostToBigInt64(dqbp->dqb_bhardlimit);
714 tblk.dqb_bsoftlimit = OSSwapHostToBigInt64(dqbp->dqb_bsoftlimit);
715 tblk.dqb_curbytes = OSSwapHostToBigInt64(dqbp->dqb_curbytes);
716 tblk.dqb_ihardlimit = OSSwapHostToBigInt32(dqbp->dqb_ihardlimit);
717 tblk.dqb_isoftlimit = OSSwapHostToBigInt32(dqbp->dqb_isoftlimit);
718 tblk.dqb_curinodes = OSSwapHostToBigInt32(dqbp->dqb_curinodes);
719 tblk.dqb_btime = OSSwapHostToBigInt32((int)dqbp->dqb_btime);
720 tblk.dqb_itime = OSSwapHostToBigInt32((int)dqbp->dqb_itime);
721 tblk.dqb_id = OSSwapHostToBigInt32(id);
722
723 lseek(fd, dqoffset(i), L_SET);
724 if (write(fd, &tblk, sizeof (struct dqblk)) !=
725 sizeof (struct dqblk)) {
726 return (-1);
727 }
728 return (0);
729 }
730 }
731 errno = ENOSPC;
732 return (-1);
733 }
734 #endif /* __APPLE__ */
735
736 /*
737 * Take a list of priviledges and get it edited.
738 */
739 int
740 editit(tmpfile)
741 char *tmpfile;
742 {
743 int omask;
744 int pid, stat;
745 extern char *getenv();
746
747 omask = sigblock(sigmask(SIGINT)|sigmask(SIGQUIT)|sigmask(SIGHUP));
748 top:
749 if ((pid = fork()) < 0) {
750 extern int errno;
751
752 if (errno == EPROCLIM) {
753 fprintf(stderr, "You have too many processes\n");
754 return(0);
755 }
756 if (errno == EAGAIN) {
757 sleep(1);
758 goto top;
759 }
760 perror("fork");
761 return (0);
762 }
763 if (pid == 0) {
764 register char *ed;
765 struct passwd *pwd = getpwuid(getuid());
766 gid_t newgid = getgid();
767
768 setgid(newgid);
769 if (pwd) initgroups(pwd->pw_name, newgid);
770 setuid(getuid());
771 sigsetmask(omask);
772
773 if ((ed = getenv("EDITOR")) == (char *)0)
774 ed = _PATH_VI;
775 execlp(ed, ed, tmpfile, NULL);
776 perror(ed);
777 exit(1);
778 }
779 waitpid(pid, &stat, 0);
780 sigsetmask(omask);
781 if (!WIFEXITED(stat) || WEXITSTATUS(stat) != 0)
782 return (0);
783 return (1);
784 }
785
786 /*
787 * Convert a quotause list to an ASCII file.
788 */
789 int
790 writeprivs(quplist, outfd, name, quotatype)
791 struct quotause *quplist;
792 int outfd;
793 char *name;
794 int quotatype;
795 {
796 register struct quotause *qup;
797 FILE *fd;
798
799 ftruncate(outfd, 0);
800 lseek(outfd, 0, L_SET);
801 if ((fd = fdopen(dup(outfd), "w")) == NULL) {
802 fprintf(stderr, "edquota: ");
803 perror(tmpfil);
804 exit(1);
805 }
806 fprintf(fd, "Quotas for %s %s:\n", qfextension[quotatype], name);
807 for (qup = quplist; qup; qup = qup->next) {
808 #ifdef __APPLE__
809 fprintf(fd, "%s: %s %qd, limits (soft = %qd, hard = %qd)\n",
810 qup->fsname, "1K blocks in use:",
811 qup->dqblk.dqb_curbytes / 1024,
812 qup->dqblk.dqb_bsoftlimit / 1024,
813 qup->dqblk.dqb_bhardlimit / 1024);
814 #else
815 fprintf(fd, "%s: %s %d, limits (soft = %d, hard = %d)\n",
816 qup->fsname, "blocks in use:",
817 dbtob(qup->dqblk.dqb_curblocks) / 1024,
818 dbtob(qup->dqblk.dqb_bsoftlimit) / 1024,
819 dbtob(qup->dqblk.dqb_bhardlimit) / 1024);
820 #endif /* __APPLE__ */
821
822 fprintf(fd, "%s %d, limits (soft = %d, hard = %d)\n",
823 "\tinodes in use:", qup->dqblk.dqb_curinodes,
824 qup->dqblk.dqb_isoftlimit, qup->dqblk.dqb_ihardlimit);
825 }
826 fclose(fd);
827 return (1);
828 }
829
830 /*
831 * Merge changes to an ASCII file into a quotause list.
832 */
833 int
834 readprivs(quplist, infd)
835 struct quotause *quplist;
836 int infd;
837 {
838 register struct quotause *qup;
839 FILE *fd;
840 int cnt;
841 register char *cp;
842 struct dqblk dqblk;
843 char fsp[BUFSIZ], line1[BUFSIZ], line2[BUFSIZ];
844
845 lseek(infd, 0, L_SET);
846 fd = fdopen(dup(infd), "r");
847 if (fd == NULL) {
848 fprintf(stderr, "Can't re-read temp file!!\n");
849 return (0);
850 }
851 /*
852 * Discard title line, then read pairs of lines to process.
853 */
854 (void) fgets(line1, sizeof (line1), fd);
855 while (fgets(line1, sizeof (line1), fd) != NULL &&
856 fgets(line2, sizeof (line2), fd) != NULL) {
857 cp = strstr(line1, ": 1K blocks in use:");
858 if (cp == NULL) {
859 fprintf(stderr, "%s: bad format for line\n", line1);
860 return 0;
861 }
862 /* Copy up to the template text */
863 strlcpy(fsp, line1, cp - line1 + 1);
864 /* And point cp right after it. */
865 cp = line1 + strlen(fsp) + 1;
866 #ifdef __APPLE__
867 /* We expect input to be in 1K blocks */
868 cnt = sscanf(cp,
869 " 1K blocks in use: %qd, limits (soft = %qd, hard = %qd)",
870 &dqblk.dqb_curbytes, &dqblk.dqb_bsoftlimit,
871 &dqblk.dqb_bhardlimit);
872 if (cnt != 3) {
873 fprintf(stderr, "%s:%s: bad format\n", fsp, cp);
874 return (0);
875 }
876
877 /* convert default 1K blocks to byte count */
878 dqblk.dqb_curbytes = dqblk.dqb_curbytes * 1024;
879 dqblk.dqb_bsoftlimit = dqblk.dqb_bsoftlimit * 1024;
880 dqblk.dqb_bhardlimit = dqblk.dqb_bhardlimit * 1024;
881 #else
882 cnt = sscanf(cp,
883 " blocks in use: %d, limits (soft = %d, hard = %d)",
884 &dqblk.dqb_curblocks, &dqblk.dqb_bsoftlimit,
885 &dqblk.dqb_bhardlimit);
886 if (cnt != 3) {
887 fprintf(stderr, "%s:%s: bad format\n", fsp, cp);
888 return (0);
889 }
890 dqblk.dqb_curblocks = btodb(dqblk.dqb_curblocks * 1024);
891 dqblk.dqb_bsoftlimit = btodb(dqblk.dqb_bsoftlimit * 1024);
892 dqblk.dqb_bhardlimit = btodb(dqblk.dqb_bhardlimit * 1024);
893 #endif /* __APPLE__ */
894
895 if ((cp = strtok(line2, "\n")) == NULL) {
896 fprintf(stderr, "%s: %s: bad format\n", fsp, line2);
897 return (0);
898 }
899 cnt = sscanf(cp,
900 "\tinodes in use: %d, limits (soft = %d, hard = %d)",
901 &dqblk.dqb_curinodes, &dqblk.dqb_isoftlimit,
902 &dqblk.dqb_ihardlimit);
903 if (cnt != 3) {
904 fprintf(stderr, "%s: %s: bad format\n", fsp, line2);
905 return (0);
906 }
907 for (qup = quplist; qup; qup = qup->next) {
908 if (strcmp(fsp, qup->fsname))
909 continue;
910 /*
911 * Cause time limit to be reset when the quota
912 * is next used if previously had no soft limit
913 * or were under it, but now have a soft limit
914 * and are over it.
915 */
916 #ifdef __APPLE__
917 if (dqblk.dqb_bsoftlimit &&
918 qup->dqblk.dqb_curbytes >= dqblk.dqb_bsoftlimit &&
919 (qup->dqblk.dqb_bsoftlimit == 0 ||
920 qup->dqblk.dqb_curbytes <
921 qup->dqblk.dqb_bsoftlimit))
922 qup->dqblk.dqb_btime = 0;
923 #else
924 if (dqblk.dqb_bsoftlimit &&
925 qup->dqblk.dqb_curblocks >= dqblk.dqb_bsoftlimit &&
926 (qup->dqblk.dqb_bsoftlimit == 0 ||
927 qup->dqblk.dqb_curblocks <
928 qup->dqblk.dqb_bsoftlimit))
929 qup->dqblk.dqb_btime = 0;
930 #endif /* __APPLE__ */
931 if (dqblk.dqb_isoftlimit &&
932 qup->dqblk.dqb_curinodes >= dqblk.dqb_isoftlimit &&
933 (qup->dqblk.dqb_isoftlimit == 0 ||
934 qup->dqblk.dqb_curinodes <
935 qup->dqblk.dqb_isoftlimit))
936 qup->dqblk.dqb_itime = 0;
937 qup->dqblk.dqb_bsoftlimit = dqblk.dqb_bsoftlimit;
938 qup->dqblk.dqb_bhardlimit = dqblk.dqb_bhardlimit;
939 qup->dqblk.dqb_isoftlimit = dqblk.dqb_isoftlimit;
940 qup->dqblk.dqb_ihardlimit = dqblk.dqb_ihardlimit;
941 qup->flags |= FOUND;
942 #ifdef __APPLE__
943 if (dqblk.dqb_curbytes == qup->dqblk.dqb_curbytes &&
944 dqblk.dqb_curinodes == qup->dqblk.dqb_curinodes)
945 break;
946 #else
947 if (dqblk.dqb_curblocks == qup->dqblk.dqb_curblocks &&
948 dqblk.dqb_curinodes == qup->dqblk.dqb_curinodes)
949 break;
950 #endif /* __APPLE__ */
951 fprintf(stderr,
952 "%s: cannot change current allocation\n", fsp);
953 break;
954 }
955 }
956 fclose(fd);
957 /*
958 * Disable quotas for any filesystems that have not been found.
959 */
960 for (qup = quplist; qup; qup = qup->next) {
961 if (qup->flags & FOUND) {
962 qup->flags &= ~FOUND;
963 continue;
964 }
965 qup->dqblk.dqb_bsoftlimit = 0;
966 qup->dqblk.dqb_bhardlimit = 0;
967 qup->dqblk.dqb_isoftlimit = 0;
968 qup->dqblk.dqb_ihardlimit = 0;
969 }
970 return (1);
971 }
972
973 /*
974 * Convert a quotause list to an ASCII file of grace times.
975 */
976 int
977 writetimes(quplist, outfd, quotatype)
978 struct quotause *quplist;
979 int outfd;
980 int quotatype;
981 {
982 register struct quotause *qup;
983 char *cvtstoa();
984 FILE *fd;
985
986 ftruncate(outfd, 0);
987 lseek(outfd, 0, L_SET);
988 if ((fd = fdopen(dup(outfd), "w")) == NULL) {
989 fprintf(stderr, "edquota: ");
990 perror(tmpfil);
991 exit(1);
992 }
993 fprintf(fd, "Time units may be: days, hours, minutes, or seconds\n");
994 fprintf(fd, "Grace period before enforcing soft limits for %ss:\n",
995 qfextension[quotatype]);
996 for (qup = quplist; qup; qup = qup->next) {
997 fprintf(fd, "%s: block grace period: %s, ",
998 qup->fsname, cvtstoa(qup->dqblk.dqb_btime));
999 fprintf(fd, "file grace period: %s\n",
1000 cvtstoa(qup->dqblk.dqb_itime));
1001 }
1002 fclose(fd);
1003 return (1);
1004 }
1005
1006 /*
1007 * Merge changes of grace times in an ASCII file into a quotause list.
1008 */
1009 int
1010 readtimes(quplist, infd)
1011 struct quotause *quplist;
1012 int infd;
1013 {
1014 register struct quotause *qup;
1015 FILE *fd;
1016 int cnt;
1017 register char *cp;
1018 time_t itime, btime, iseconds, bseconds;
1019 char *fsp, bunits[10], iunits[10], line1[BUFSIZ];
1020
1021 lseek(infd, 0, L_SET);
1022 fd = fdopen(dup(infd), "r");
1023 if (fd == NULL) {
1024 fprintf(stderr, "Can't re-read temp file!!\n");
1025 return (0);
1026 }
1027 /*
1028 * Discard two title lines, then read lines to process.
1029 */
1030 (void) fgets(line1, sizeof (line1), fd);
1031 (void) fgets(line1, sizeof (line1), fd);
1032 while (fgets(line1, sizeof (line1), fd) != NULL) {
1033 if ((fsp = strtok(line1, " \t:")) == NULL) {
1034 fprintf(stderr, "%s: bad format\n", line1);
1035 return (0);
1036 }
1037 if ((cp = strtok((char *)0, "\n")) == NULL) {
1038 fprintf(stderr, "%s: %s: bad format\n", fsp,
1039 &fsp[strlen(fsp) + 1]);
1040 return (0);
1041 }
1042 cnt = sscanf(cp,
1043 " block grace period: %ld %s file grace period: %ld %s",
1044 &btime, bunits, &itime, iunits);
1045 if (cnt != 4) {
1046 fprintf(stderr, "%s:%s: bad format\n", fsp, cp);
1047 return (0);
1048 }
1049 if (cvtatos(btime, bunits, &bseconds) == 0)
1050 return (0);
1051 if (cvtatos(itime, iunits, &iseconds) == 0)
1052 return (0);
1053 for (qup = quplist; qup; qup = qup->next) {
1054 if (strcmp(fsp, qup->fsname))
1055 continue;
1056 qup->dqblk.dqb_btime = (uint32_t) bseconds;
1057 qup->dqblk.dqb_itime = (uint32_t) iseconds;
1058 qup->flags |= FOUND;
1059 break;
1060 }
1061 }
1062 fclose(fd);
1063 /*
1064 * reset default grace periods for any filesystems
1065 * that have not been found.
1066 */
1067 for (qup = quplist; qup; qup = qup->next) {
1068 if (qup->flags & FOUND) {
1069 qup->flags &= ~FOUND;
1070 continue;
1071 }
1072 qup->dqblk.dqb_btime = 0;
1073 qup->dqblk.dqb_itime = 0;
1074 }
1075 return (1);
1076 }
1077
1078 /*
1079 * Convert seconds to ASCII times.
1080 */
1081 char *
1082 cvtstoa(time)
1083 time_t time;
1084 {
1085 static char buf[20];
1086
1087 if (time % (24 * 60 * 60) == 0) {
1088 time /= 24 * 60 * 60;
1089 snprintf(buf, sizeof(buf), "%d day%s", (int)time, time == 1 ? "" : "s");
1090 } else if (time % (60 * 60) == 0) {
1091 time /= 60 * 60;
1092 snprintf(buf, sizeof(buf), "%d hour%s", (int)time, time == 1 ? "" : "s");
1093 } else if (time % 60 == 0) {
1094 time /= 60;
1095 snprintf(buf, sizeof(buf), "%d minute%s", (int)time, time == 1 ? "" : "s");
1096 } else
1097 snprintf(buf, sizeof(buf), "%d second%s", (int)time, time == 1 ? "" : "s");
1098 return (buf);
1099 }
1100
1101 /*
1102 * Convert ASCII input times to seconds.
1103 */
1104 int
1105 cvtatos(time, units, seconds)
1106 time_t time;
1107 char *units;
1108 time_t *seconds;
1109 {
1110
1111 if (bcmp(units, "second", 6) == 0)
1112 *seconds = time;
1113 else if (bcmp(units, "minute", 6) == 0)
1114 *seconds = time * 60;
1115 else if (bcmp(units, "hour", 4) == 0)
1116 *seconds = time * 60 * 60;
1117 else if (bcmp(units, "day", 3) == 0)
1118 *seconds = time * 24 * 60 * 60;
1119 else {
1120 printf("%s: bad units, specify %s\n", units,
1121 "days, hours, minutes, or seconds");
1122 return (0);
1123 }
1124 return (1);
1125 }
1126
1127 /*
1128 * Free a list of quotause structures.
1129 */
1130 void
1131 freeprivs(quplist)
1132 struct quotause *quplist;
1133 {
1134 register struct quotause *qup, *nextqup;
1135
1136 for (qup = quplist; qup; qup = nextqup) {
1137 nextqup = qup->next;
1138 free(qup);
1139 }
1140 }
1141
1142 /*
1143 * Check whether a string is completely composed of digits.
1144 */
1145 int
1146 alldigits(s)
1147 register char *s;
1148 {
1149 register int c;
1150
1151 c = *s++;
1152 do {
1153 if (!isdigit(c))
1154 return (0);
1155 } while ((c = *s++));
1156 return (1);
1157 }
1158
1159 /*
1160 * Check to see if a particular quota is to be enabled.
1161 */
1162 #ifdef __APPLE__
1163 int
1164 hasquota(fst, type, qfnamep)
1165 register struct statfs *fst;
1166 int type;
1167 char **qfnamep;
1168 {
1169 struct stat sb;
1170 static char initname, usrname[100], grpname[100];
1171 static char buf[BUFSIZ];
1172
1173 if (!initname) {
1174 snprintf(usrname, sizeof(usrname), "%s%s", qfextension[USRQUOTA], qfname);
1175 snprintf(grpname, sizeof(grpname), "%s%s", qfextension[GRPQUOTA], qfname);
1176 initname = 1;
1177 }
1178
1179 /*
1180 We only support the default path to the
1181 on disk quota files.
1182 */
1183
1184 (void)snprintf(buf, sizeof(buf), "%s/%s.%s", fst->f_mntonname,
1185 QUOTAOPSNAME, qfextension[type] );
1186 if (stat(buf, &sb) != 0) {
1187 /* There appears to be no mount option file */
1188 return(0);
1189 }
1190
1191 (void) snprintf(buf, sizeof(buf), "%s/%s.%s", fst->f_mntonname, qfname, qfextension[type]);
1192 *qfnamep = buf;
1193 return (1);
1194 }
1195 #else
1196 hasquota(fs, type, qfnamep)
1197 register struct fstab *fs;
1198 int type;
1199 char **qfnamep;
1200 {
1201 register char *opt;
1202 char *cp, *index(), *strtok();
1203 static char initname, usrname[100], grpname[100];
1204 static char buf[BUFSIZ];
1205
1206 if (!initname) {
1207 snprintf(usrname, sizeof(usrname), "%s%s", qfextension[USRQUOTA], qfname);
1208 snprintf(grpname, sizeof(grpname), "%s%s", qfextension[GRPQUOTA], qfname);
1209 initname = 1;
1210 }
1211 strlcpy(buf, fs->fs_mntops, sizeof(buf));
1212 for (opt = strtok(buf, ","); opt; opt = strtok(NULL, ",")) {
1213 if (cp = index(opt, '='))
1214 *cp++ = '\0';
1215 if (type == USRQUOTA && strcmp(opt, usrname) == 0)
1216 break;
1217 if (type == GRPQUOTA && strcmp(opt, grpname) == 0)
1218 break;
1219 }
1220 if (!opt)
1221 return (0);
1222 if (cp) {
1223 *qfnamep = cp;
1224 return (1);
1225 }
1226 (void) snprintf(buf, sizeof(buf), "%s/%s.%s", fs->fs_file, qfname, qfextension[type]);
1227 *qfnamep = buf;
1228 return (1);
1229 }
1230 #endif /* __APPLE */