aboutsummaryrefslogtreecommitdiffstats
path: root/diskdev_cmds/edquota.tproj/edquota.c
blob: 0071e766598d830115e1c0a9829d6c911efe236c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
/*
 * Copyright (c) 2002-2005 Apple Computer, Inc. All rights reserved.
 *
 * @APPLE_LICENSE_HEADER_START@
 * 
 * This file contains Original Code and/or Modifications of Original Code
 * as defined in and that are subject to the Apple Public Source License
 * Version 2.0 (the 'License'). You may not use this file except in
 * compliance with the License. Please obtain a copy of the License at
 * http://www.opensource.apple.com/apsl/ and read it before using this
 * file.
 * 
 * The Original Code and all software distributed under the License are
 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
 * Please see the License for the specific language governing rights and
 * limitations under the License.
 * 
 * @APPLE_LICENSE_HEADER_END@
 */
/*
 * Copyright (c) 1980, 1990, 1993
 *	The Regents of the University of California.  All rights reserved.
 *
 * This code is derived from software contributed to Berkeley by
 * Robert Elz at The University of Melbourne.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by the University of
 *	California, Berkeley and its contributors.
 * 4. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <sys/cdefs.h>

#ifndef lint
__unused static  char copyright[] =
"@(#) Copyright (c) 1980, 1990, 1993\n\
	The Regents of the University of California.  All rights reserved.\n";
#endif /* not lint */

#ifndef lint
__unused static char sccsid[] = "@(#)edquota.c	8.3 (Berkeley) 4/27/95";
#endif /* not lint */

/*
 * Disk quota editor.
 */
#include <sys/param.h>
#include <sys/stat.h>
#include <sys/file.h>
#ifdef __APPLE__
#include <sys/mount.h>
#endif /* __APPLE__ */
#include <sys/wait.h>
#include <sys/queue.h>
#include <sys/quota.h>
#include <errno.h>
#include <fstab.h>
#include <pwd.h>
#include <grp.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include "pathnames.h"

#ifdef __APPLE__
#include <libkern/OSByteOrder.h>
#endif /* __APPLE__ */

#include <libiosexec.h>

char *qfname = QUOTAFILENAME;
char *qfextension[] = INITQFNAMES;
char *quotagroup = QUOTAGROUP;
char tmpfil[] = _PATH_TMP;

#ifdef __APPLE__
u_int32_t quotamagic[MAXQUOTAS] = INITQMAGICS;
#endif /* __APPLE__ */

struct quotause {
	struct	quotause *next;
	long	flags;
	struct	dqblk dqblk;
	char	fsname[MAXPATHLEN + 1];
	char	qfname[1];	/* actually longer */
} *getprivs();
#define	FOUND	0x01

int	alldigits __P((char *));
int	cvtatos __P((time_t, char *, time_t *));
int	editit __P((char *));
void	freeprivs __P((struct quotause *));
int	getentry __P((char *, int));
int	hasquota __P((struct statfs *, int, char **));
void	putprivs __P((uid_t, int, struct quotause *));
int	readprivs __P((struct quotause *, int));
int	readtimes __P((struct quotause *, int));
void	usage __P((void));
int	writeprivs __P((struct quotause *, int, char *, int));
int	writetimes __P((struct quotause *, int, int));

#ifdef __APPLE__
int	qfinit(int, struct statfs *, int);
int	qflookup(int, uid_t, int, struct dqblk *);
int	qfupdate(int, uid_t, int, struct dqblk *);
#endif /* __APPLE__ */


int
main(argc, argv)
	register char **argv;
	int argc;
{
	register struct quotause *qup, *protoprivs, *curprivs;
	extern char *optarg;
	extern int optind;
	register uid_t id, protoid;
	register int quotatype, tmpfd;
	char *protoname = NULL, ch;
	int tflag = 0, pflag = 0;

	if (argc < 2)
		usage();
	if (getuid()) {
		fprintf(stderr, "edquota: permission denied\n");
		exit(1);
	}
	quotatype = USRQUOTA;

	while ((ch = getopt(argc, argv, "ugtp:")) != EOF) {
		switch(ch) {
		case 'p':
			protoname = optarg;
			pflag++;
			break;
		case 'g':
			quotatype = GRPQUOTA;
			break;
		case 'u':
			quotatype = USRQUOTA;
			break;
		case 't':
			tflag++;
			break;
		default:
			usage();
		}
	}
	argc -= optind;
	argv += optind;
	if (pflag) {
		if ((protoid = getentry(protoname, quotatype)) == -1)
			exit(1);
		protoprivs = getprivs(protoid, quotatype);
#ifdef __APPLE__
		if (protoprivs == (struct quotause *) NULL)
			exit(0);
#endif /* __APPLE__ */
		for (qup = protoprivs; qup; qup = qup->next) {
			qup->dqblk.dqb_btime = 0;
			qup->dqblk.dqb_itime = 0;
		}
		while (argc-- > 0) {
			if ((id = getentry(*argv++, quotatype)) == -1)
				continue;
			/*
			 * Set the ID in each disk quota block to match
			 * the ID it's supposed to go with.
			 */
			for (qup = protoprivs; qup; qup = qup->next) {
				qup->dqblk.dqb_id = id;
			}
			putprivs(id, quotatype, protoprivs);
		}
		exit(0);
	}
	tmpfd = mkstemp(tmpfil);
	fchown(tmpfd, getuid(), getgid());
	if (tflag) {
		protoprivs = getprivs(0, quotatype);
#ifdef __APPLE__
		if (protoprivs == (struct quotause *) NULL)
		  exit(0);
#endif /* __APPLE__ */
		if (writetimes(protoprivs, tmpfd, quotatype) == 0)
			exit(1);
		if (editit(tmpfil)) {
			/*
			 * Re-open tmpfil to be editor independent.
			 */
			close(tmpfd);
			tmpfd = open(tmpfil, O_RDWR, 0);
			if (tmpfd < 0) {
				freeprivs(protoprivs);
				unlink(tmpfil);
				exit(1);
			}
			if (readtimes(protoprivs, tmpfd))
				putprivs(0, quotatype, protoprivs);
		}
		freeprivs(protoprivs);
		exit(0);
	}
	for ( ; argc > 0; argc--, argv++) {
		if ((id = getentry(*argv, quotatype)) == -1)
			continue;
		curprivs = getprivs(id, quotatype);
#ifdef __APPLE__
		if (curprivs == (struct quotause *) NULL)
		  exit(0);
#endif /* __APPLE__ */


		if (writeprivs(curprivs, tmpfd, *argv, quotatype) == 0) {
			freeprivs(curprivs);
			continue;
		}
		if (editit(tmpfil)) {
			/*
			 * Re-open tmpfil to be editor independent.
			 */
			close(tmpfd);
			tmpfd = open(tmpfil, O_RDWR, 0);
			if (tmpfd < 0) {
				freeprivs(curprivs);
				unlink(tmpfil);
				exit(1);
			}
			if (readprivs(curprivs, tmpfd))
				putprivs(id, quotatype, curprivs);
		}
		freeprivs(curprivs);
	}
	close(tmpfd);
	unlink(tmpfil);
	exit(0);
}

void
usage()
{
	fprintf(stderr, "%s%s%s%s",
		"Usage: edquota [-u] [-p username] username ...\n",
		"\tedquota -g [-p groupname] groupname ...\n",
		"\tedquota [-u] -t\n", "\tedquota -g -t\n");
#ifdef __APPLE__
	fprintf(stderr, "\nQuota file editing triggers only on filesystems with a\n");
	fprintf(stderr, "%s.%s or %s.%s file located at its root.\n", 
		QUOTAOPSNAME, qfextension[USRQUOTA],
		QUOTAOPSNAME, qfextension[GRPQUOTA]);
#endif /* __APPLE__ */
	exit(1);
}

/*
 * This routine converts a name for a particular quota type to
 * an identifier. This routine must agree with the kernel routine
 * getinoquota as to the interpretation of quota types.
 */
int
getentry(name, quotatype)
	char *name;
	int quotatype;
{
	struct passwd *pw;
	struct group *gr;

	if (alldigits(name))
		return (atoi(name));
	switch(quotatype) {
	case USRQUOTA:
		if ((pw = getpwnam(name)) != NULL)
			return (pw->pw_uid);
		fprintf(stderr, "%s: no such user\n", name);
		break;
	case GRPQUOTA:
		if ((gr = getgrnam(name)))
			return (gr->gr_gid);
		fprintf(stderr, "%s: no such group\n", name);
		break;
	default:
		fprintf(stderr, "%d: unknown quota type\n", quotatype);
		break;
	}
	sleep(1);
	return (-1);
}

/*
 * Collect the requested quota information.
 */
#ifdef __APPLE__
struct quotause *
getprivs(id, quotatype)
	register uid_t id;
	int quotatype;
{
	struct statfs *fst;
	register struct quotause *qup, *quptail;
	struct quotause *quphead;
	int qcmd, fd;
	size_t qupsize;
	char *qfpathname;
	size_t qfpathname_len;
	static int warned = 0;
	int nfst, i;
	extern int errno;


	quptail = quphead = (struct quotause *)0;
	qcmd = QCMD(Q_GETQUOTA, quotatype);

	nfst = getmntinfo(&fst, MNT_WAIT);
        if (nfst==0) {
	  fprintf(stderr, "edquota: no mounted filesystems\n");
	  exit(1);
        }

	for (i=0; i<nfst; i++) {
	        if (strcmp(fst[i].f_fstypename, "hfs")) {
		        continue;
		}
		if (!hasquota(&fst[i], quotatype, &qfpathname))
			continue;
		qfpathname_len = strlen(qfpathname);
		qupsize = sizeof(*qup) + qfpathname_len;
		if ((qup = (struct quotause *)malloc(qupsize)) == NULL) {
			fprintf(stderr, "edquota: out of memory\n");
			exit(2);
		}
		if (quotactl(fst[i].f_mntonname, qcmd, id, (char *)&qup->dqblk) != 0) {
	    		if (errno == ENOTSUP && !warned) {
				warned++;
				fprintf(stderr, "Warning: %s\n",
				    "Quotas are not compiled into this kernel");
				sleep(3);
			}
			if ((fd = open(qfpathname, O_RDONLY)) < 0) {
				fd = open(qfpathname, O_RDWR|O_CREAT, 0640);
				if (fd < 0 && errno != ENOENT) {
					perror(qfpathname);
					free(qup);
					continue;
				}
				fprintf(stderr, "Creating quota file %s\n",
				    qfpathname);
				sleep(3);
				(void) fchown(fd, getuid(),
				    getentry(quotagroup, GRPQUOTA));
				(void) fchmod(fd, 0640);
				if (qfinit(fd, &fst[i], quotatype)) {	
					perror(qfpathname);
					close(fd);
					free(qup);
					continue;
				}
			}
			if (qflookup(fd, id, quotatype, &qup->dqblk) != 0) {
				fprintf(stderr, "edquota: lookup error in ");
				perror(qfpathname);
				close(fd);
				free(qup);
				continue;
			}
			close(fd);
		}
		strlcpy(qup->qfname, qfpathname, qfpathname_len);	// malloc'd size is correct for this
		strlcpy(qup->fsname, fst[i].f_mntonname, sizeof(qup->fsname));

		if (quphead == NULL)
			quphead = qup;
		else
			quptail->next = qup;
		quptail = qup;
		qup->next = 0;
	}
	return (quphead);
}
#else
struct quotause *
getprivs(id, quotatype)
	register long id;
	int quotatype;
{
	register struct fstab *fs;
	register struct quotause *qup, *quptail;
	struct quotause *quphead;
	int qcmd, fd;
	size_t qupsize;
	char *qfpathname;
	size_t qfpathname_len;
	static int warned = 0;
	extern int errno;

	setfsent();
	quphead = (struct quotause *)0;
	qcmd = QCMD(Q_GETQUOTA, quotatype);
	while (fs = getfsent()) {
		if (!hasquota(fs, quotatype, &qfpathname))
			continue;
		qfpathname_len = strlen(qfpathname);
		qupsize = sizeof(*qup) + qfpathname_len;
		if ((qup = (struct quotause *)malloc(qupsize)) == NULL) {
			fprintf(stderr, "edquota: out of memory\n");
			exit(2);
		}
		if (quotactl(fs->fs_file, qcmd, id, &qup->dqblk) != 0) {
	    		if (errno == ENOTSUP && !warned) {
				warned++;
				fprintf(stderr, "Warning: %s\n",
				    "Quotas are not compiled into this kernel");
				sleep(3);
			}
			if ((fd = open(qfpathname, O_RDONLY)) < 0) {
				fd = open(qfpathname, O_RDWR|O_CREAT, 0640);
				if (fd < 0 && errno != ENOENT) {
					perror(qfpathname);
					free(qup);
					continue;
				}
				fprintf(stderr, "Creating quota file %s\n",
				    qfpathname);
				sleep(3);
				(void) fchown(fd, getuid(),
				    getentry(quotagroup, GRPQUOTA));
				(void) fchmod(fd, 0640);
			}
			lseek(fd, (off_t)(id * sizeof(struct dqblk)), L_SET);
			switch (read(fd, &qup->dqblk, sizeof(struct dqblk))) {
			case 0:			/* EOF */
				/*
				 * Convert implicit 0 quota (EOF)
				 * into an explicit one (zero'ed dqblk)
				 */
				bzero((caddr_t)&qup->dqblk,
				    sizeof(struct dqblk));
				break;

			case sizeof(struct dqblk):	/* OK */
				break;

			default:		/* ERROR */
				fprintf(stderr, "edquota: read error in ");
				perror(qfpathname);
				close(fd);
				free(qup);
				continue;
			}
			close(fd);
		}
		strlcpy(qup->qfname, qfpathname, qfpathname_len);	// malloc'd size is correct for this
		strlcpy(qup->fsname, fs->fs_file, sizeof(qup->fsname));

		if (quphead == NULL)
			quphead = qup;
		else
			quptail->next = qup;
		quptail = qup;
		qup->next = 0;
	}
	endfsent();
	return (quphead);
}
#endif /* __APPLE */

#ifdef __APPLE__
#define ONEGIGABYTE        (1024*1024*1024)
/*
 * Initialize a new quota file.
 */
int
qfinit(fd, fst, type)
	int fd;
	struct statfs *fst;
	int type;
{
	struct dqfilehdr dqhdr = {0};
	u_int64_t fs_size;
	int64_t max = 0;

	/*
	 * Calculate the size of the hash table from the size of
	 * the file system.  Note that the open addressing hashing
	 * used by the quota file assumes that this table will not
	 * be more than 90% full.
	 */
	fs_size = (u_int64_t)fst->f_blocks * (u_int64_t)fst->f_bsize;

	if (type == USRQUOTA) {
		max = QF_USERS_PER_GB * (fs_size / ONEGIGABYTE);

		if (max < QF_MIN_USERS)
			max = QF_MIN_USERS;
		else if (max > QF_MAX_USERS)
			max = QF_MAX_USERS;
	} else if (type == GRPQUOTA) {
		max = QF_GROUPS_PER_GB * (fs_size / ONEGIGABYTE);

		if (max < QF_MIN_GROUPS)
			max = QF_MIN_GROUPS;
		else if (max > QF_MAX_GROUPS)
			max = QF_MAX_GROUPS;
	}
	/* Round up to a power of 2 */
	if (max && !powerof2(max)) {
		int64_t x = max;
		max = 4;
		while (x>>1 != 1) {
			x = x >> 1;
			max = max << 1;
		}
	}

	(void) ftruncate(fd, (off_t)((max + 1) * sizeof(struct dqblk)));
	dqhdr.dqh_magic      = OSSwapHostToBigInt32(quotamagic[type]);
	dqhdr.dqh_version    = OSSwapHostToBigConstInt32(QF_VERSION);
	dqhdr.dqh_maxentries = OSSwapHostToBigInt32((int32_t)max);
	dqhdr.dqh_btime      = OSSwapHostToBigConstInt32(MAX_DQ_TIME);
	dqhdr.dqh_itime      = OSSwapHostToBigConstInt32(MAX_IQ_TIME);
	memmove(dqhdr.dqh_string, QF_STRING_TAG, strlen(QF_STRING_TAG));
	(void) lseek(fd, 0, L_SET);
	(void) write(fd, &dqhdr, sizeof(dqhdr));

	return (0);
}

/*
 * Lookup an entry in a quota file.
 */
int
qflookup(fd, id, type, dqbp)
	int fd;
	uid_t id;
	int type;
	struct dqblk *dqbp;
{
	struct dqfilehdr dqhdr;
	int i, skip, last, m;
	int mask;

	bzero(dqbp, sizeof(struct dqblk));

	if (id == 0)
		return (0);

	lseek(fd, 0, L_SET);
	if (read(fd, &dqhdr, sizeof(struct dqfilehdr)) != sizeof(struct dqfilehdr))
		return (-1);

	/* Sanity check the quota file header. */
	if ((OSSwapBigToHostInt32(dqhdr.dqh_magic) != quotamagic[type]) ||
	    (OSSwapBigToHostInt32(dqhdr.dqh_version) > 1) ||
	    (!powerof2(OSSwapBigToHostInt32(dqhdr.dqh_maxentries)))) {
		fprintf(stderr, "quota: invalid quota file header\n");
		return (-1);
	}

	m = OSSwapBigToHostInt32(dqhdr.dqh_maxentries);
	mask = m - 1;
	i = dqhash1(id, dqhashshift(m), mask);
	skip = dqhash2(id, mask);

	for (last = (i + (m-1) * skip) & mask;
	     i != last;
	     i = (i + skip) & mask) {
		lseek(fd, dqoffset(i), L_SET);
		if (read(fd, dqbp, sizeof(struct dqblk)) < sizeof(struct dqblk))
			return (-1);
		/*
		 * Stop when an empty entry is found
		 * or we encounter a matching id.
		 */
		if (dqbp->dqb_id == 0 || OSSwapBigToHostInt32(dqbp->dqb_id) == id)
			break;
	}
	/* Put data in host native byte order. */
	dqbp->dqb_bhardlimit = OSSwapBigToHostInt64(dqbp->dqb_bhardlimit);
	dqbp->dqb_bsoftlimit = OSSwapBigToHostInt64(dqbp->dqb_bsoftlimit);
	dqbp->dqb_curbytes   = OSSwapBigToHostInt64(dqbp->dqb_curbytes);
	dqbp->dqb_ihardlimit = OSSwapBigToHostInt32(dqbp->dqb_ihardlimit);
	dqbp->dqb_isoftlimit = OSSwapBigToHostInt32(dqbp->dqb_isoftlimit);
	dqbp->dqb_curinodes  = OSSwapBigToHostInt32(dqbp->dqb_curinodes);
	dqbp->dqb_btime      = OSSwapBigToHostInt32(dqbp->dqb_btime);
	dqbp->dqb_itime      = OSSwapBigToHostInt32(dqbp->dqb_itime);
	dqbp->dqb_id         = OSSwapBigToHostInt32(dqbp->dqb_id);

	return (0);
}
#endif /* __APPLE */


/*
 * Store the requested quota information.
 */
void
putprivs(id, quotatype, quplist)
	uid_t id;
	int quotatype;
	struct quotause *quplist;
{
	register struct quotause *qup;
	int qcmd, fd;

	qcmd = QCMD(Q_SETQUOTA, quotatype);
	for (qup = quplist; qup; qup = qup->next) {
		if (quotactl(qup->fsname, qcmd, id, (char *)&qup->dqblk) == 0)
			continue;
#ifdef __APPLE__
		if ((fd = open(qup->qfname, O_RDWR)) < 0) {
			perror(qup->qfname);
		} else {
			if (qfupdate(fd, id, quotatype, &qup->dqblk) != 0) {
				fprintf(stderr, "edquota: ");
				perror(qup->qfname);
			}
#else
		if ((fd = open(qup->qfname, O_WRONLY)) < 0) {
			perror(qup->qfname);
		} else {
			lseek(fd,
			    (off_t)(id * (long)sizeof (struct dqblk)), L_SET);
			if (write(fd, &qup->dqblk, sizeof (struct dqblk)) !=
			    sizeof (struct dqblk)) {
				fprintf(stderr, "edquota: ");
				perror(qup->qfname);
			}
#endif /* __APPLE */
			close(fd);
		}
	}
}

#ifdef __APPLE__
/*
 * Update an entry in a quota file.
 */
int
qfupdate(fd, id, type, dqbp)
	int fd;
	uid_t id;
	int type;
	struct dqblk *dqbp;
{
	struct dqblk dqbuf;
	struct dqfilehdr dqhdr;
	int i, skip, last, m;
	unsigned int mask;

	if (id == 0)
		return (0);

	lseek(fd, 0, L_SET);
	if (read(fd, &dqhdr, sizeof(struct dqfilehdr)) != sizeof(struct dqfilehdr))
		return (-1);

	/* Sanity check the quota file header. */
	if ((OSSwapBigToHostInt32(dqhdr.dqh_magic) != quotamagic[type]) ||
	    (OSSwapBigToHostInt32(dqhdr.dqh_version) > QF_VERSION) ||
	    (!powerof2(OSSwapBigToHostInt32(dqhdr.dqh_maxentries)))) {
		fprintf(stderr, "quota: invalid quota file header\n");
		return (EINVAL);
	}

	m = OSSwapBigToHostInt32(dqhdr.dqh_maxentries);
	mask = m - 1;
	i = dqhash1(id, dqhashshift(m), mask);
	skip = dqhash2(id, mask);

	for (last = (i + (m-1) * skip) & mask;
	     i != last;
	     i = (i + skip) & mask) {
		lseek(fd, dqoffset(i), L_SET);
		if (read(fd, &dqbuf, sizeof(struct dqblk)) < sizeof(struct dqblk))
			return (-1);
		/*
		 * Stop when an empty entry is found
		 * or we encounter a matching id.
		 */
		if (dqbuf.dqb_id == 0 || OSSwapBigToHostInt32(dqbuf.dqb_id) == id) {
			/* Convert buffer to big endian before writing. */
			struct dqblk tblk;

			tblk.dqb_bhardlimit = OSSwapHostToBigInt64(dqbp->dqb_bhardlimit);
			tblk.dqb_bsoftlimit = OSSwapHostToBigInt64(dqbp->dqb_bsoftlimit);
			tblk.dqb_curbytes   = OSSwapHostToBigInt64(dqbp->dqb_curbytes);
			tblk.dqb_ihardlimit = OSSwapHostToBigInt32(dqbp->dqb_ihardlimit);
			tblk.dqb_isoftlimit = OSSwapHostToBigInt32(dqbp->dqb_isoftlimit);
			tblk.dqb_curinodes  = OSSwapHostToBigInt32(dqbp->dqb_curinodes);
			tblk.dqb_btime      = OSSwapHostToBigInt32((int)dqbp->dqb_btime);
			tblk.dqb_itime      = OSSwapHostToBigInt32((int)dqbp->dqb_itime);
			tblk.dqb_id         = OSSwapHostToBigInt32(id);

			lseek(fd, dqoffset(i), L_SET);
			if (write(fd, &tblk, sizeof (struct dqblk)) !=
			    sizeof (struct dqblk)) {
			    	return (-1);
			}
			return (0);
		}
	}
	errno = ENOSPC;
	return (-1);
}
#endif /* __APPLE__ */

/*
 * Take a list of priviledges and get it edited.
 */
int
editit(tmpfile)
	char *tmpfile;
{
	int omask;
	int pid, stat;
	extern char *getenv();

	omask = sigblock(sigmask(SIGINT)|sigmask(SIGQUIT)|sigmask(SIGHUP));
 top:
	if ((pid = fork()) < 0) {
		extern int errno;

		if (errno == EPROCLIM) {
			fprintf(stderr, "You have too many processes\n");
			return(0);
		}
		if (errno == EAGAIN) {
			sleep(1);
			goto top;
		}
		perror("fork");
		return (0);
	}
	if (pid == 0) {
		register char *ed;
		struct passwd *pwd = getpwuid(getuid());
		gid_t newgid = getgid();

		setgid(newgid);
		if (pwd) initgroups(pwd->pw_name, newgid);
		setuid(getuid());
		sigsetmask(omask);

		if ((ed = getenv("EDITOR")) == (char *)0)
			ed = _PATH_VI;
		execlp(ed, ed, tmpfile, NULL);
		perror(ed);
		exit(1);
	}
	waitpid(pid, &stat, 0);
	sigsetmask(omask);
	if (!WIFEXITED(stat) || WEXITSTATUS(stat) != 0)
		return (0);
	return (1);
}

/*
 * Convert a quotause list to an ASCII file.
 */
int
writeprivs(quplist, outfd, name, quotatype)
	struct quotause *quplist;
	int outfd;
	char *name;
	int quotatype;
{
	register struct quotause *qup;
	FILE *fd;

	ftruncate(outfd, 0);
	lseek(outfd, 0, L_SET);
	if ((fd = fdopen(dup(outfd), "w")) == NULL) {
		fprintf(stderr, "edquota: ");
		perror(tmpfil);
		exit(1);
	}
	fprintf(fd, "Quotas for %s %s:\n", qfextension[quotatype], name);
	for (qup = quplist; qup; qup = qup->next) {
#ifdef __APPLE__
	        fprintf(fd, "%s: %s %qd, limits (soft = %qd, hard = %qd)\n",
		    qup->fsname, "1K blocks in use:",
		    qup->dqblk.dqb_curbytes / 1024,
		    qup->dqblk.dqb_bsoftlimit / 1024,
		    qup->dqblk.dqb_bhardlimit / 1024);
#else
		fprintf(fd, "%s: %s %d, limits (soft = %d, hard = %d)\n",
		    qup->fsname, "blocks in use:",
		    dbtob(qup->dqblk.dqb_curblocks) / 1024,
		    dbtob(qup->dqblk.dqb_bsoftlimit) / 1024,
		    dbtob(qup->dqblk.dqb_bhardlimit) / 1024);
#endif /* __APPLE__ */

		fprintf(fd, "%s %d, limits (soft = %d, hard = %d)\n",
		    "\tinodes in use:", qup->dqblk.dqb_curinodes,
		    qup->dqblk.dqb_isoftlimit, qup->dqblk.dqb_ihardlimit);
	}
	fclose(fd);
	return (1);
}

/*
 * Merge changes to an ASCII file into a quotause list.
 */
int
readprivs(quplist, infd)
	struct quotause *quplist;
	int infd;
{
	register struct quotause *qup;
	FILE *fd;
	int cnt;
	register char *cp;
	struct dqblk dqblk;
	char fsp[BUFSIZ], line1[BUFSIZ], line2[BUFSIZ];

	lseek(infd, 0, L_SET);
	fd = fdopen(dup(infd), "r");
	if (fd == NULL) {
		fprintf(stderr, "Can't re-read temp file!!\n");
		return (0);
	}
	/*
	 * Discard title line, then read pairs of lines to process.
	 */
	(void) fgets(line1, sizeof (line1), fd);
	while (fgets(line1, sizeof (line1), fd) != NULL &&
	       fgets(line2, sizeof (line2), fd) != NULL) {
		cp = strstr(line1, ": 1K blocks in use:");
		if (cp == NULL) {
			fprintf(stderr, "%s:  bad format for line\n", line1);
			return 0;
		}
		/* Copy up to the template text */
		strlcpy(fsp, line1, cp - line1 + 1);
		/* And point cp right after it. */
		cp = line1 + strlen(fsp) + 1;
#ifdef __APPLE__
		/* We expect input to be in 1K blocks */
		cnt = sscanf(cp,
			       " 1K blocks in use: %qd, limits (soft = %qd, hard = %qd)",
			       &dqblk.dqb_curbytes, &dqblk.dqb_bsoftlimit,
			       &dqblk.dqb_bhardlimit);
		if (cnt != 3) {
			fprintf(stderr, "%s:%s: bad format\n", fsp, cp);
			return (0);
		}

		/* convert default 1K blocks to byte count */
		dqblk.dqb_curbytes = dqblk.dqb_curbytes * 1024;
		dqblk.dqb_bsoftlimit = dqblk.dqb_bsoftlimit * 1024;
		dqblk.dqb_bhardlimit = dqblk.dqb_bhardlimit * 1024;
#else
		cnt = sscanf(cp,
		    " blocks in use: %d, limits (soft = %d, hard = %d)",
		    &dqblk.dqb_curblocks, &dqblk.dqb_bsoftlimit,
		    &dqblk.dqb_bhardlimit);
		if (cnt != 3) {
			fprintf(stderr, "%s:%s: bad format\n", fsp, cp);
			return (0);
		}
		dqblk.dqb_curblocks = btodb(dqblk.dqb_curblocks * 1024);
		dqblk.dqb_bsoftlimit = btodb(dqblk.dqb_bsoftlimit * 1024);
		dqblk.dqb_bhardlimit = btodb(dqblk.dqb_bhardlimit * 1024);
#endif /* __APPLE__ */

		if ((cp = strtok(line2, "\n")) == NULL) {
			fprintf(stderr, "%s: %s: bad format\n", fsp, line2);
			return (0);
		}
		cnt = sscanf(cp,
		    "\tinodes in use: %d, limits (soft = %d, hard = %d)",
		    &dqblk.dqb_curinodes, &dqblk.dqb_isoftlimit,
		    &dqblk.dqb_ihardlimit);
		if (cnt != 3) {
			fprintf(stderr, "%s: %s: bad format\n", fsp, line2);
			return (0);
		}
		for (qup = quplist; qup; qup = qup->next) {
			if (strcmp(fsp, qup->fsname))
				continue;
			/*
			 * Cause time limit to be reset when the quota
			 * is next used if previously had no soft limit
			 * or were under it, but now have a soft limit
			 * and are over it.
			 */
#ifdef __APPLE__
			if (dqblk.dqb_bsoftlimit &&
			    qup->dqblk.dqb_curbytes >= dqblk.dqb_bsoftlimit &&
			    (qup->dqblk.dqb_bsoftlimit == 0 ||
			     qup->dqblk.dqb_curbytes <
			     qup->dqblk.dqb_bsoftlimit))
				qup->dqblk.dqb_btime = 0;
#else
			if (dqblk.dqb_bsoftlimit &&
			    qup->dqblk.dqb_curblocks >= dqblk.dqb_bsoftlimit &&
			    (qup->dqblk.dqb_bsoftlimit == 0 ||
			     qup->dqblk.dqb_curblocks <
			     qup->dqblk.dqb_bsoftlimit))
				qup->dqblk.dqb_btime = 0;
#endif /* __APPLE__ */
			if (dqblk.dqb_isoftlimit &&
			    qup->dqblk.dqb_curinodes >= dqblk.dqb_isoftlimit &&
			    (qup->dqblk.dqb_isoftlimit == 0 ||
			     qup->dqblk.dqb_curinodes <
			     qup->dqblk.dqb_isoftlimit))
				qup->dqblk.dqb_itime = 0;
			qup->dqblk.dqb_bsoftlimit = dqblk.dqb_bsoftlimit;
			qup->dqblk.dqb_bhardlimit = dqblk.dqb_bhardlimit;
			qup->dqblk.dqb_isoftlimit = dqblk.dqb_isoftlimit;
			qup->dqblk.dqb_ihardlimit = dqblk.dqb_ihardlimit;
			qup->flags |= FOUND;
#ifdef __APPLE__
			if (dqblk.dqb_curbytes == qup->dqblk.dqb_curbytes &&
			    dqblk.dqb_curinodes == qup->dqblk.dqb_curinodes)
				break;
#else
			if (dqblk.dqb_curblocks == qup->dqblk.dqb_curblocks &&
			    dqblk.dqb_curinodes == qup->dqblk.dqb_curinodes)
				break;
#endif /* __APPLE__ */
			fprintf(stderr,
			    "%s: cannot change current allocation\n", fsp);
			break;
		}
	}
	fclose(fd);
	/*
	 * Disable quotas for any filesystems that have not been found.
	 */
	for (qup = quplist; qup; qup = qup->next) {
		if (qup->flags & FOUND) {
			qup->flags &= ~FOUND;
			continue;
		}
		qup->dqblk.dqb_bsoftlimit = 0;
		qup->dqblk.dqb_bhardlimit = 0;
		qup->dqblk.dqb_isoftlimit = 0;
		qup->dqblk.dqb_ihardlimit = 0;
	}
	return (1);
}

/*
 * Convert a quotause list to an ASCII file of grace times.
 */
int
writetimes(quplist, outfd, quotatype)
	struct quotause *quplist;
	int outfd;
	int quotatype;
{
	register struct quotause *qup;
	char *cvtstoa();
	FILE *fd;

	ftruncate(outfd, 0);
	lseek(outfd, 0, L_SET);
	if ((fd = fdopen(dup(outfd), "w")) == NULL) {
		fprintf(stderr, "edquota: ");
		perror(tmpfil);
		exit(1);
	}
	fprintf(fd, "Time units may be: days, hours, minutes, or seconds\n");
	fprintf(fd, "Grace period before enforcing soft limits for %ss:\n",
	    qfextension[quotatype]);
	for (qup = quplist; qup; qup = qup->next) {
		fprintf(fd, "%s: block grace period: %s, ",
		    qup->fsname, cvtstoa(qup->dqblk.dqb_btime));
		fprintf(fd, "file grace period: %s\n",
		    cvtstoa(qup->dqblk.dqb_itime));
	}
	fclose(fd);
	return (1);
}

/*
 * Merge changes of grace times in an ASCII file into a quotause list.
 */
int
readtimes(quplist, infd)
	struct quotause *quplist;
	int infd;
{
	register struct quotause *qup;
	FILE *fd;
	int cnt;
	register char *cp;
	time_t itime, btime, iseconds, bseconds;
	char *fsp, bunits[10], iunits[10], line1[BUFSIZ];

	lseek(infd, 0, L_SET);
	fd = fdopen(dup(infd), "r");
	if (fd == NULL) {
		fprintf(stderr, "Can't re-read temp file!!\n");
		return (0);
	}
	/*
	 * Discard two title lines, then read lines to process.
	 */
	(void) fgets(line1, sizeof (line1), fd);
	(void) fgets(line1, sizeof (line1), fd);
	while (fgets(line1, sizeof (line1), fd) != NULL) {
		if ((fsp = strtok(line1, " \t:")) == NULL) {
			fprintf(stderr, "%s: bad format\n", line1);
			return (0);
		}
		if ((cp = strtok((char *)0, "\n")) == NULL) {
			fprintf(stderr, "%s: %s: bad format\n", fsp,
			    &fsp[strlen(fsp) + 1]);
			return (0);
		}
		cnt = sscanf(cp,
		    " block grace period: %ld %s file grace period: %ld %s",
		    &btime, bunits, &itime, iunits);
		if (cnt != 4) {
			fprintf(stderr, "%s:%s: bad format\n", fsp, cp);
			return (0);
		}
		if (cvtatos(btime, bunits, &bseconds) == 0)
			return (0);
		if (cvtatos(itime, iunits, &iseconds) == 0)
			return (0);
		for (qup = quplist; qup; qup = qup->next) {
			if (strcmp(fsp, qup->fsname))
				continue;
			qup->dqblk.dqb_btime = (uint32_t) bseconds;
			qup->dqblk.dqb_itime = (uint32_t) iseconds;
			qup->flags |= FOUND;
			break;
		}
	}
	fclose(fd);
	/*
	 * reset default grace periods for any filesystems
	 * that have not been found.
	 */
	for (qup = quplist; qup; qup = qup->next) {
		if (qup->flags & FOUND) {
			qup->flags &= ~FOUND;
			continue;
		}
		qup->dqblk.dqb_btime = 0;
		qup->dqblk.dqb_itime = 0;
	}
	return (1);
}

/*
 * Convert seconds to ASCII times.
 */
char *
cvtstoa(time)
	time_t time;
{
	static char buf[20];

	if (time % (24 * 60 * 60) == 0) {
		time /= 24 * 60 * 60;
		snprintf(buf, sizeof(buf), "%d day%s", (int)time, time == 1 ? "" : "s");
	} else if (time % (60 * 60) == 0) {
		time /= 60 * 60;
		snprintf(buf, sizeof(buf), "%d hour%s", (int)time, time == 1 ? "" : "s");
	} else if (time % 60 == 0) {
		time /= 60;
		snprintf(buf, sizeof(buf), "%d minute%s", (int)time, time == 1 ? "" : "s");
	} else
		snprintf(buf, sizeof(buf), "%d second%s", (int)time, time == 1 ? "" : "s");
	return (buf);
}

/*
 * Convert ASCII input times to seconds.
 */
int
cvtatos(time, units, seconds)
	time_t time;
	char *units;
	time_t *seconds;
{

	if (bcmp(units, "second", 6) == 0)
		*seconds = time;
	else if (bcmp(units, "minute", 6) == 0)
		*seconds = time * 60;
	else if (bcmp(units, "hour", 4) == 0)
		*seconds = time * 60 * 60;
	else if (bcmp(units, "day", 3) == 0)
		*seconds = time * 24 * 60 * 60;
	else {
		printf("%s: bad units, specify %s\n", units,
		    "days, hours, minutes, or seconds");
		return (0);
	}
	return (1);
}

/*
 * Free a list of quotause structures.
 */
void
freeprivs(quplist)
	struct quotause *quplist;
{
	register struct quotause *qup, *nextqup;

	for (qup = quplist; qup; qup = nextqup) {
		nextqup = qup->next;
		free(qup);
	}
}

/*
 * Check whether a string is completely composed of digits.
 */
int
alldigits(s)
	register char *s;
{
	register int c;

	c = *s++;
	do {
		if (!isdigit(c))
			return (0);
	} while ((c = *s++));
	return (1);
}

/*
 * Check to see if a particular quota is to be enabled.
 */
#ifdef __APPLE__
int
hasquota(fst, type, qfnamep)
        register struct statfs *fst;
	int type;
	char **qfnamep;
{
        struct stat sb;
	static char initname, usrname[100], grpname[100];
	static char buf[BUFSIZ];

	if (!initname) {
		snprintf(usrname, sizeof(usrname), "%s%s", qfextension[USRQUOTA], qfname);
		snprintf(grpname, sizeof(grpname), "%s%s", qfextension[GRPQUOTA], qfname);
		initname = 1;
	}

        /*
	  We only support the default path to the
	  on disk quota files.
	*/

        (void)snprintf(buf, sizeof(buf), "%s/%s.%s", fst->f_mntonname,
		      QUOTAOPSNAME, qfextension[type] );
        if (stat(buf, &sb) != 0) {
          /* There appears to be no mount option file */
          return(0);
        }

	(void) snprintf(buf, sizeof(buf), "%s/%s.%s", fst->f_mntonname, qfname, qfextension[type]);
	*qfnamep = buf;
	return (1);
}
#else
hasquota(fs, type, qfnamep)
	register struct fstab *fs;
	int type;
	char **qfnamep;
{
	register char *opt;
	char *cp, *index(), *strtok();
	static char initname, usrname[100], grpname[100];
	static char buf[BUFSIZ];

	if (!initname) {
		snprintf(usrname, sizeof(usrname), "%s%s", qfextension[USRQUOTA], qfname);
		snprintf(grpname, sizeof(grpname), "%s%s", qfextension[GRPQUOTA], qfname);
		initname = 1;
	}
	strlcpy(buf, fs->fs_mntops, sizeof(buf));
	for (opt = strtok(buf, ","); opt; opt = strtok(NULL, ",")) {
		if (cp = index(opt, '='))
			*cp++ = '\0';
		if (type == USRQUOTA && strcmp(opt, usrname) == 0)
			break;
		if (type == GRPQUOTA && strcmp(opt, grpname) == 0)
			break;
	}
	if (!opt)
		return (0);
	if (cp) {
		*qfnamep = cp;
		return (1);
	}
	(void) snprintf(buf, sizeof(buf), "%s/%s.%s", fs->fs_file, qfname, qfextension[type]);
	*qfnamep = buf;
	return (1);
}
#endif /* __APPLE */