edquota.c revision 1.30 1 1.30 bouyer /* $NetBSD: edquota.c,v 1.30 2011/03/06 17:08:42 bouyer Exp $ */
2 1.1 cgd /*
3 1.5 mycroft * Copyright (c) 1980, 1990, 1993
4 1.5 mycroft * The Regents of the University of California. All rights reserved.
5 1.1 cgd *
6 1.1 cgd * This code is derived from software contributed to Berkeley by
7 1.1 cgd * Robert Elz at The University of Melbourne.
8 1.1 cgd *
9 1.1 cgd * Redistribution and use in source and binary forms, with or without
10 1.1 cgd * modification, are permitted provided that the following conditions
11 1.1 cgd * are met:
12 1.1 cgd * 1. Redistributions of source code must retain the above copyright
13 1.1 cgd * notice, this list of conditions and the following disclaimer.
14 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
15 1.1 cgd * notice, this list of conditions and the following disclaimer in the
16 1.1 cgd * documentation and/or other materials provided with the distribution.
17 1.24 agc * 3. Neither the name of the University nor the names of its contributors
18 1.1 cgd * may be used to endorse or promote products derived from this software
19 1.1 cgd * without specific prior written permission.
20 1.1 cgd *
21 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 1.1 cgd * SUCH DAMAGE.
32 1.1 cgd */
33 1.1 cgd
34 1.15 lukem #include <sys/cdefs.h>
35 1.1 cgd #ifndef lint
36 1.29 lukem __COPYRIGHT("@(#) Copyright (c) 1980, 1990, 1993\
37 1.29 lukem The Regents of the University of California. All rights reserved.");
38 1.1 cgd #endif /* not lint */
39 1.1 cgd
40 1.1 cgd #ifndef lint
41 1.15 lukem #if 0
42 1.15 lukem static char sccsid[] = "from: @(#)edquota.c 8.3 (Berkeley) 4/27/95";
43 1.15 lukem #else
44 1.30 bouyer __RCSID("$NetBSD: edquota.c,v 1.30 2011/03/06 17:08:42 bouyer Exp $");
45 1.15 lukem #endif
46 1.1 cgd #endif /* not lint */
47 1.1 cgd
48 1.1 cgd /*
49 1.1 cgd * Disk quota editor.
50 1.1 cgd */
51 1.1 cgd #include <sys/param.h>
52 1.1 cgd #include <sys/stat.h>
53 1.1 cgd #include <sys/file.h>
54 1.1 cgd #include <sys/wait.h>
55 1.12 mikel #include <sys/queue.h>
56 1.30 bouyer #include <sys/types.h>
57 1.30 bouyer #include <sys/statvfs.h>
58 1.30 bouyer
59 1.30 bouyer #include <ufs/ufs/quota2_prop.h>
60 1.30 bouyer #include <ufs/ufs/quota1.h>
61 1.30 bouyer #include <sys/quota.h>
62 1.30 bouyer
63 1.30 bouyer #include <assert.h>
64 1.15 lukem #include <err.h>
65 1.1 cgd #include <errno.h>
66 1.1 cgd #include <fstab.h>
67 1.1 cgd #include <pwd.h>
68 1.1 cgd #include <grp.h>
69 1.1 cgd #include <ctype.h>
70 1.15 lukem #include <signal.h>
71 1.1 cgd #include <stdio.h>
72 1.9 cgd #include <stdlib.h>
73 1.1 cgd #include <string.h>
74 1.5 mycroft #include <unistd.h>
75 1.30 bouyer
76 1.30 bouyer #include <printquota.h>
77 1.30 bouyer #include <getvfsquota.h>
78 1.30 bouyer
79 1.1 cgd #include "pathnames.h"
80 1.1 cgd
81 1.28 xtraeme const char *qfname = QUOTAFILENAME;
82 1.28 xtraeme const char *quotagroup = QUOTAGROUP;
83 1.1 cgd char tmpfil[] = _PATH_TMP;
84 1.1 cgd
85 1.1 cgd struct quotause {
86 1.1 cgd struct quotause *next;
87 1.1 cgd long flags;
88 1.30 bouyer struct quota2_entry q2e;
89 1.1 cgd char fsname[MAXPATHLEN + 1];
90 1.30 bouyer char *qfname;
91 1.15 lukem };
92 1.1 cgd #define FOUND 0x01
93 1.30 bouyer #define QUOTA2 0x02
94 1.30 bouyer #define DEFAULT 0x04
95 1.13 mikel
96 1.23 jonb #define MAX_TMPSTR (100+MAXPATHLEN)
97 1.23 jonb
98 1.30 bouyer int main(int, char **);
99 1.30 bouyer void usage(void);
100 1.30 bouyer int getentry(const char *, int);
101 1.30 bouyer struct quotause * getprivs(long, int, const char *, int);
102 1.30 bouyer struct quotause * getprivs2(long, int, const char *, int);
103 1.30 bouyer struct quotause * getprivs1(long, int, const char *);
104 1.30 bouyer void putprivs(long, int, struct quotause *);
105 1.30 bouyer void putprivs2(long, int, struct quotause *);
106 1.30 bouyer void putprivs1(long, int, struct quotause *);
107 1.30 bouyer int editit(char *);
108 1.30 bouyer int writeprivs(struct quotause *, int, char *, int);
109 1.30 bouyer int readprivs(struct quotause *, int);
110 1.30 bouyer int writetimes(struct quotause *, int, int);
111 1.30 bouyer int readtimes(struct quotause *, int);
112 1.30 bouyer void freeq(struct quotause *);
113 1.30 bouyer void freeprivs(struct quotause *);
114 1.30 bouyer int alldigits(const char *);
115 1.30 bouyer int hasquota(struct fstab *, int, char **);
116 1.30 bouyer static void clearpriv(int, char **, const char *, int);
117 1.30 bouyer
118 1.30 bouyer int Hflag = 0;
119 1.30 bouyer int Dflag = 0;
120 1.30 bouyer int dflag = 0;
121 1.1 cgd
122 1.12 mikel int
123 1.1 cgd main(argc, argv)
124 1.1 cgd int argc;
125 1.15 lukem char **argv;
126 1.1 cgd {
127 1.15 lukem struct quotause *qup, *protoprivs, *curprivs;
128 1.15 lukem long id, protoid;
129 1.15 lukem int quotatype, tmpfd;
130 1.11 mark char *protoname;
131 1.30 bouyer char *soft = NULL, *hard = NULL, *grace = NULL;
132 1.22 bouyer char *fs = NULL;
133 1.11 mark int ch;
134 1.30 bouyer int pflag = 0;
135 1.30 bouyer int cflag = 0;
136 1.1 cgd
137 1.1 cgd if (argc < 2)
138 1.1 cgd usage();
139 1.15 lukem if (getuid())
140 1.15 lukem errx(1, "permission denied");
141 1.15 lukem protoname = NULL;
142 1.1 cgd quotatype = USRQUOTA;
143 1.30 bouyer while ((ch = getopt(argc, argv, "DHcdugp:s:h:t:f:")) != -1) {
144 1.1 cgd switch(ch) {
145 1.30 bouyer case 'D':
146 1.30 bouyer Dflag++;
147 1.30 bouyer break;
148 1.30 bouyer case 'H':
149 1.30 bouyer Hflag++;
150 1.30 bouyer break;
151 1.30 bouyer case 'c':
152 1.30 bouyer cflag++;
153 1.30 bouyer break;
154 1.30 bouyer case 'd':
155 1.30 bouyer dflag++;
156 1.30 bouyer break;
157 1.1 cgd case 'p':
158 1.1 cgd protoname = optarg;
159 1.1 cgd pflag++;
160 1.1 cgd break;
161 1.1 cgd case 'g':
162 1.1 cgd quotatype = GRPQUOTA;
163 1.1 cgd break;
164 1.1 cgd case 'u':
165 1.1 cgd quotatype = USRQUOTA;
166 1.1 cgd break;
167 1.22 bouyer case 's':
168 1.22 bouyer soft = optarg;
169 1.22 bouyer break;
170 1.22 bouyer case 'h':
171 1.22 bouyer hard = optarg;
172 1.22 bouyer break;
173 1.30 bouyer case 't':
174 1.30 bouyer grace = optarg;
175 1.30 bouyer break;
176 1.22 bouyer case 'f':
177 1.22 bouyer fs = optarg;
178 1.22 bouyer break;
179 1.1 cgd default:
180 1.1 cgd usage();
181 1.1 cgd }
182 1.1 cgd }
183 1.1 cgd argc -= optind;
184 1.1 cgd argv += optind;
185 1.30 bouyer
186 1.1 cgd if (pflag) {
187 1.30 bouyer if (soft || hard || grace || dflag || cflag)
188 1.22 bouyer usage();
189 1.1 cgd if ((protoid = getentry(protoname, quotatype)) == -1)
190 1.1 cgd exit(1);
191 1.30 bouyer protoprivs = getprivs(protoid, quotatype, fs, 0);
192 1.1 cgd for (qup = protoprivs; qup; qup = qup->next) {
193 1.30 bouyer qup->q2e.q2e_val[QL_BLOCK].q2v_time = 0;
194 1.30 bouyer qup->q2e.q2e_val[QL_FILE].q2v_time = 0;
195 1.1 cgd }
196 1.1 cgd while (argc-- > 0) {
197 1.1 cgd if ((id = getentry(*argv++, quotatype)) < 0)
198 1.1 cgd continue;
199 1.1 cgd putprivs(id, quotatype, protoprivs);
200 1.1 cgd }
201 1.1 cgd exit(0);
202 1.1 cgd }
203 1.30 bouyer if (soft || hard || grace) {
204 1.28 xtraeme struct quotause *lqup;
205 1.30 bouyer u_int64_t softb, hardb, softi, hardi;
206 1.30 bouyer time_t graceb, gracei;
207 1.30 bouyer char *str;
208 1.30 bouyer
209 1.30 bouyer if (cflag)
210 1.22 bouyer usage();
211 1.22 bouyer if (soft) {
212 1.30 bouyer str = strsep(&soft, "/");
213 1.30 bouyer if (str[0] == '\0' || soft == NULL || soft[0] == '\0')
214 1.22 bouyer usage();
215 1.30 bouyer
216 1.30 bouyer if (intrd(str, &softb, HN_B) != 0)
217 1.30 bouyer errx(1, "%s: bad number", str);
218 1.30 bouyer if (intrd(soft, &softi, 0) != 0)
219 1.30 bouyer errx(1, "%s: bad number", soft);
220 1.22 bouyer }
221 1.22 bouyer if (hard) {
222 1.30 bouyer str = strsep(&hard, "/");
223 1.30 bouyer if (str[0] == '\0' || hard == NULL || hard[0] == '\0')
224 1.30 bouyer usage();
225 1.30 bouyer
226 1.30 bouyer if (intrd(str, &hardb, HN_B) != 0)
227 1.30 bouyer errx(1, "%s: bad number", str);
228 1.30 bouyer if (intrd(hard, &hardi, 0) != 0)
229 1.30 bouyer errx(1, "%s: bad number", hard);
230 1.30 bouyer }
231 1.30 bouyer if (grace) {
232 1.30 bouyer str = strsep(&grace, "/");
233 1.30 bouyer if (str[0] == '\0' || grace == NULL || grace[0] == '\0')
234 1.22 bouyer usage();
235 1.30 bouyer
236 1.30 bouyer if (timeprd(str, &graceb) != 0)
237 1.30 bouyer errx(1, "%s: bad number", str);
238 1.30 bouyer if (timeprd(grace, &gracei) != 0)
239 1.30 bouyer errx(1, "%s: bad number", grace);
240 1.30 bouyer }
241 1.30 bouyer if (dflag) {
242 1.30 bouyer curprivs = getprivs(0, quotatype, fs, 1);
243 1.30 bouyer for (lqup = curprivs; lqup; lqup = lqup->next) {
244 1.30 bouyer if (soft) {
245 1.30 bouyer lqup->q2e.q2e_val[QL_BLOCK].q2v_softlimit = softb;
246 1.30 bouyer lqup->q2e.q2e_val[QL_FILE].q2v_softlimit = softi;
247 1.30 bouyer }
248 1.30 bouyer if (hard) {
249 1.30 bouyer lqup->q2e.q2e_val[QL_BLOCK].q2v_hardlimit = hardb;
250 1.30 bouyer lqup->q2e.q2e_val[QL_FILE].q2v_hardlimit = hardi;
251 1.30 bouyer }
252 1.30 bouyer if (grace) {
253 1.30 bouyer lqup->q2e.q2e_val[QL_BLOCK].q2v_grace = graceb;
254 1.30 bouyer lqup->q2e.q2e_val[QL_FILE].q2v_grace = gracei;
255 1.30 bouyer }
256 1.30 bouyer }
257 1.30 bouyer putprivs(0, quotatype, curprivs);
258 1.30 bouyer freeprivs(curprivs);
259 1.30 bouyer exit(0);
260 1.22 bouyer }
261 1.22 bouyer for ( ; argc > 0; argc--, argv++) {
262 1.22 bouyer if ((id = getentry(*argv, quotatype)) == -1)
263 1.22 bouyer continue;
264 1.30 bouyer curprivs = getprivs(id, quotatype, fs, 0);
265 1.28 xtraeme for (lqup = curprivs; lqup; lqup = lqup->next) {
266 1.22 bouyer if (soft) {
267 1.22 bouyer if (softb &&
268 1.30 bouyer lqup->q2e.q2e_val[QL_BLOCK].q2v_cur >= softb &&
269 1.30 bouyer (lqup->q2e.q2e_val[QL_BLOCK].q2v_softlimit == 0 ||
270 1.30 bouyer lqup->q2e.q2e_val[QL_BLOCK].q2v_cur < lqup->q2e.q2e_val[QL_BLOCK].q2v_softlimit))
271 1.30 bouyer lqup->q2e.q2e_val[QL_BLOCK].q2v_time = 0;
272 1.22 bouyer if (softi &&
273 1.30 bouyer lqup->q2e.q2e_val[QL_FILE].q2v_cur >= softb &&
274 1.30 bouyer (lqup->q2e.q2e_val[QL_FILE].q2v_softlimit == 0 ||
275 1.30 bouyer lqup->q2e.q2e_val[QL_FILE].q2v_cur < lqup->q2e.q2e_val[QL_FILE].q2v_softlimit))
276 1.30 bouyer lqup->q2e.q2e_val[QL_FILE].q2v_time = 0;
277 1.30 bouyer lqup->q2e.q2e_val[QL_BLOCK].q2v_softlimit = softb;
278 1.30 bouyer lqup->q2e.q2e_val[QL_FILE].q2v_softlimit = softi;
279 1.22 bouyer }
280 1.22 bouyer if (hard) {
281 1.30 bouyer lqup->q2e.q2e_val[QL_BLOCK].q2v_hardlimit = hardb;
282 1.30 bouyer lqup->q2e.q2e_val[QL_FILE].q2v_hardlimit = hardi;
283 1.30 bouyer }
284 1.30 bouyer if (grace) {
285 1.30 bouyer lqup->q2e.q2e_val[QL_BLOCK].q2v_grace = graceb;
286 1.30 bouyer lqup->q2e.q2e_val[QL_FILE].q2v_grace = gracei;
287 1.22 bouyer }
288 1.22 bouyer }
289 1.22 bouyer putprivs(id, quotatype, curprivs);
290 1.22 bouyer freeprivs(curprivs);
291 1.22 bouyer }
292 1.22 bouyer exit(0);
293 1.22 bouyer }
294 1.30 bouyer if (cflag) {
295 1.30 bouyer if (dflag)
296 1.30 bouyer usage();
297 1.30 bouyer clearpriv(argc, argv, fs, quotatype);
298 1.30 bouyer exit(0);
299 1.30 bouyer }
300 1.1 cgd tmpfd = mkstemp(tmpfil);
301 1.1 cgd fchown(tmpfd, getuid(), getgid());
302 1.30 bouyer if (dflag) {
303 1.30 bouyer curprivs = getprivs(0, quotatype, fs, 1);
304 1.30 bouyer if (writeprivs(curprivs, tmpfd, NULL, quotatype) &&
305 1.30 bouyer editit(tmpfil) && readprivs(curprivs, tmpfd))
306 1.30 bouyer putprivs(0, quotatype, curprivs);
307 1.30 bouyer freeprivs(curprivs);
308 1.1 cgd }
309 1.1 cgd for ( ; argc > 0; argc--, argv++) {
310 1.1 cgd if ((id = getentry(*argv, quotatype)) == -1)
311 1.1 cgd continue;
312 1.30 bouyer curprivs = getprivs(id, quotatype, fs, 0);
313 1.1 cgd if (writeprivs(curprivs, tmpfd, *argv, quotatype) == 0)
314 1.1 cgd continue;
315 1.1 cgd if (editit(tmpfil) && readprivs(curprivs, tmpfd))
316 1.1 cgd putprivs(id, quotatype, curprivs);
317 1.1 cgd freeprivs(curprivs);
318 1.1 cgd }
319 1.1 cgd close(tmpfd);
320 1.1 cgd unlink(tmpfil);
321 1.1 cgd exit(0);
322 1.1 cgd }
323 1.1 cgd
324 1.12 mikel void
325 1.1 cgd usage()
326 1.1 cgd {
327 1.22 bouyer fprintf(stderr,
328 1.30 bouyer "usage:\n"
329 1.30 bouyer " edquota [-D] [-H] [-u] [-p username] [-f filesystem] -d | username ...\n"
330 1.30 bouyer " edquota [-D] [-H] -g [-p groupname] [-f filesystem] -d | groupname ...\n"
331 1.30 bouyer " edquota [-D] [-u] [-f filesystem] [-s b#/i#] [-h b#/i#] [-t t#/t#] \\\n\t-d | username ...\n"
332 1.30 bouyer " edquota [-D] -g [-f filesystem] [-s b#/i#] [-h b#/i#] [-t t#/t#] \\\n\t-d | groupname ...\n"
333 1.30 bouyer " edquota [-D] [-H] [-u] -c [-f filesystem] username ...\n"
334 1.30 bouyer " edquota [-D] [-H] -g -c [-f filesystem] groupname ...\n"
335 1.22 bouyer );
336 1.1 cgd exit(1);
337 1.1 cgd }
338 1.1 cgd
339 1.1 cgd /*
340 1.1 cgd * This routine converts a name for a particular quota type to
341 1.1 cgd * an identifier. This routine must agree with the kernel routine
342 1.1 cgd * getinoquota as to the interpretation of quota types.
343 1.1 cgd */
344 1.12 mikel int
345 1.1 cgd getentry(name, quotatype)
346 1.28 xtraeme const char *name;
347 1.1 cgd int quotatype;
348 1.1 cgd {
349 1.1 cgd struct passwd *pw;
350 1.1 cgd struct group *gr;
351 1.1 cgd
352 1.1 cgd if (alldigits(name))
353 1.1 cgd return (atoi(name));
354 1.1 cgd switch(quotatype) {
355 1.1 cgd case USRQUOTA:
356 1.15 lukem if ((pw = getpwnam(name)) != NULL)
357 1.1 cgd return (pw->pw_uid);
358 1.15 lukem warnx("%s: no such user", name);
359 1.1 cgd break;
360 1.1 cgd case GRPQUOTA:
361 1.15 lukem if ((gr = getgrnam(name)) != NULL)
362 1.1 cgd return (gr->gr_gid);
363 1.15 lukem warnx("%s: no such group", name);
364 1.1 cgd break;
365 1.1 cgd default:
366 1.15 lukem warnx("%d: unknown quota type", quotatype);
367 1.1 cgd break;
368 1.1 cgd }
369 1.1 cgd sleep(1);
370 1.1 cgd return (-1);
371 1.1 cgd }
372 1.1 cgd
373 1.1 cgd /*
374 1.1 cgd * Collect the requested quota information.
375 1.1 cgd */
376 1.1 cgd struct quotause *
377 1.30 bouyer getprivs(long id, int quotatype, const char *filesys, int defaultq)
378 1.1 cgd {
379 1.30 bouyer struct statvfs *fst;
380 1.30 bouyer int nfst, i;
381 1.30 bouyer struct quotause *qup, *quptail = NULL;
382 1.30 bouyer struct quotause *quphead = NULL;
383 1.30 bouyer
384 1.30 bouyer nfst = getmntinfo(&fst, MNT_WAIT);
385 1.30 bouyer if (nfst == 0)
386 1.30 bouyer errx(2, "no filesystems mounted!");
387 1.1 cgd
388 1.30 bouyer for (i = 0; i < nfst; i++) {
389 1.30 bouyer if ((fst[i].f_flag & ST_QUOTA) == 0)
390 1.22 bouyer continue;
391 1.30 bouyer if (filesys && strcmp(fst[i].f_mntonname, filesys) != 0 &&
392 1.30 bouyer strcmp(fst[i].f_mntfromname, filesys) != 0)
393 1.1 cgd continue;
394 1.30 bouyer qup = getprivs2(id, quotatype, fst[i].f_mntonname, defaultq);
395 1.30 bouyer if (qup == NULL)
396 1.30 bouyer return NULL;
397 1.30 bouyer if (quphead == NULL)
398 1.30 bouyer quphead = qup;
399 1.30 bouyer else
400 1.30 bouyer quptail->next = qup;
401 1.30 bouyer quptail = qup;
402 1.30 bouyer qup->next = 0;
403 1.30 bouyer }
404 1.1 cgd
405 1.30 bouyer if (filesys && quphead == NULL) {
406 1.30 bouyer if (defaultq)
407 1.30 bouyer errx(1, "no default quota for version 1");
408 1.30 bouyer /* if we get there, filesys is not mounted. try the old way */
409 1.30 bouyer qup = getprivs1(id, quotatype, filesys);
410 1.30 bouyer if (qup == NULL)
411 1.30 bouyer return NULL;
412 1.1 cgd if (quphead == NULL)
413 1.1 cgd quphead = qup;
414 1.1 cgd else
415 1.1 cgd quptail->next = qup;
416 1.1 cgd quptail = qup;
417 1.1 cgd qup->next = 0;
418 1.1 cgd }
419 1.30 bouyer return quphead;
420 1.30 bouyer }
421 1.30 bouyer
422 1.30 bouyer struct quotause *
423 1.30 bouyer getprivs2(long id, int quotatype, const char *filesys, int defaultq)
424 1.30 bouyer {
425 1.30 bouyer struct quotause *qup;
426 1.30 bouyer int8_t version;
427 1.30 bouyer
428 1.30 bouyer if ((qup = (struct quotause *)malloc(sizeof(*qup))) == NULL)
429 1.30 bouyer errx(2, "out of memory");
430 1.30 bouyer memset(qup, 0, sizeof(*qup));
431 1.30 bouyer strcpy(qup->fsname, filesys);
432 1.30 bouyer if (defaultq)
433 1.30 bouyer qup->flags |= DEFAULT;
434 1.30 bouyer if (!getvfsquota(filesys, &qup->q2e, &version,
435 1.30 bouyer id, quotatype, defaultq, Dflag)) {
436 1.30 bouyer /* no entry, get default entry */
437 1.30 bouyer if (!getvfsquota(filesys, &qup->q2e, &version,
438 1.30 bouyer id, quotatype, 1, Dflag)) {
439 1.30 bouyer free(qup);
440 1.30 bouyer return NULL;
441 1.30 bouyer }
442 1.30 bouyer }
443 1.30 bouyer if (version == 2)
444 1.30 bouyer qup->flags |= QUOTA2;
445 1.30 bouyer qup->q2e.q2e_uid = id;
446 1.30 bouyer return qup;
447 1.30 bouyer }
448 1.30 bouyer
449 1.30 bouyer struct quotause *
450 1.30 bouyer getprivs1(long id, int quotatype, const char *filesys)
451 1.30 bouyer {
452 1.30 bouyer struct fstab *fs;
453 1.30 bouyer char *qfpathname;
454 1.30 bouyer struct quotause *qup;
455 1.30 bouyer struct dqblk dqblk;
456 1.30 bouyer int fd;
457 1.30 bouyer
458 1.30 bouyer setfsent();
459 1.30 bouyer while ((fs = getfsent()) != NULL) {
460 1.30 bouyer if (strcmp(fs->fs_vfstype, "ffs"))
461 1.30 bouyer continue;
462 1.30 bouyer if (strcmp(fs->fs_spec, filesys) == 0 ||
463 1.30 bouyer strcmp(fs->fs_file, filesys) == 0)
464 1.30 bouyer break;
465 1.30 bouyer }
466 1.30 bouyer if (fs == NULL)
467 1.30 bouyer return NULL;
468 1.30 bouyer
469 1.30 bouyer if (!hasquota(fs, quotatype, &qfpathname))
470 1.30 bouyer return NULL;
471 1.30 bouyer if ((qup = (struct quotause *)malloc(sizeof(*qup))) == NULL)
472 1.30 bouyer errx(2, "out of memory");
473 1.30 bouyer strcpy(qup->fsname, fs->fs_file);
474 1.30 bouyer if ((fd = open(qfpathname, O_RDONLY)) < 0) {
475 1.30 bouyer fd = open(qfpathname, O_RDWR|O_CREAT, 0640);
476 1.30 bouyer if (fd < 0 && errno != ENOENT) {
477 1.30 bouyer warnx("open `%s'", qfpathname);
478 1.30 bouyer freeq(qup);
479 1.30 bouyer return NULL;
480 1.30 bouyer }
481 1.30 bouyer warnx("Creating quota file %s", qfpathname);
482 1.30 bouyer sleep(3);
483 1.30 bouyer (void) fchown(fd, getuid(),
484 1.30 bouyer getentry(quotagroup, GRPQUOTA));
485 1.30 bouyer (void) fchmod(fd, 0640);
486 1.30 bouyer }
487 1.30 bouyer (void)lseek(fd, (off_t)(id * sizeof(struct dqblk)),
488 1.30 bouyer SEEK_SET);
489 1.30 bouyer switch (read(fd, &dqblk, sizeof(struct dqblk))) {
490 1.30 bouyer case 0: /* EOF */
491 1.30 bouyer /*
492 1.30 bouyer * Convert implicit 0 quota (EOF)
493 1.30 bouyer * into an explicit one (zero'ed dqblk)
494 1.30 bouyer */
495 1.30 bouyer memset((caddr_t)&dqblk, 0,
496 1.30 bouyer sizeof(struct dqblk));
497 1.30 bouyer break;
498 1.30 bouyer
499 1.30 bouyer case sizeof(struct dqblk): /* OK */
500 1.30 bouyer break;
501 1.30 bouyer
502 1.30 bouyer default: /* ERROR */
503 1.30 bouyer warn("read error in `%s'", qfpathname);
504 1.30 bouyer close(fd);
505 1.30 bouyer freeq(qup);
506 1.30 bouyer return NULL;
507 1.30 bouyer }
508 1.30 bouyer close(fd);
509 1.30 bouyer qup->qfname = qfpathname;
510 1.1 cgd endfsent();
511 1.30 bouyer dqblk2q2e(&dqblk, &qup->q2e);
512 1.30 bouyer return (qup);
513 1.1 cgd }
514 1.1 cgd
515 1.1 cgd /*
516 1.1 cgd * Store the requested quota information.
517 1.1 cgd */
518 1.12 mikel void
519 1.30 bouyer putprivs(long id, int quotatype, struct quotause *quplist)
520 1.1 cgd {
521 1.15 lukem struct quotause *qup;
522 1.1 cgd
523 1.30 bouyer for (qup = quplist; qup; qup = qup->next) {
524 1.30 bouyer if (qup->qfname == NULL)
525 1.30 bouyer putprivs2(id, quotatype, qup);
526 1.30 bouyer else
527 1.30 bouyer putprivs1(id, quotatype, qup);
528 1.30 bouyer }
529 1.30 bouyer }
530 1.30 bouyer
531 1.30 bouyer void
532 1.30 bouyer putprivs2(long id, int quotatype, struct quotause *qup)
533 1.30 bouyer {
534 1.30 bouyer
535 1.30 bouyer prop_dictionary_t dict, data, cmd;
536 1.30 bouyer prop_array_t cmds, datas;
537 1.30 bouyer struct plistref pref;
538 1.30 bouyer int error;
539 1.30 bouyer int8_t error8;
540 1.30 bouyer
541 1.30 bouyer qup->q2e.q2e_uid = id;
542 1.30 bouyer data = q2etoprop(&qup->q2e, (qup->flags & DEFAULT) ? 1 : 0);
543 1.30 bouyer
544 1.30 bouyer if (data == NULL)
545 1.30 bouyer err(1, "q2etoprop(id)");
546 1.30 bouyer
547 1.30 bouyer dict = quota2_prop_create();
548 1.30 bouyer cmds = prop_array_create();
549 1.30 bouyer datas = prop_array_create();
550 1.30 bouyer
551 1.30 bouyer if (dict == NULL || cmds == NULL || datas == NULL) {
552 1.30 bouyer errx(1, "can't allocate proplist");
553 1.30 bouyer }
554 1.30 bouyer
555 1.30 bouyer if (!prop_array_add_and_rel(datas, data))
556 1.30 bouyer err(1, "prop_array_add(data)");
557 1.30 bouyer
558 1.30 bouyer if (!quota2_prop_add_command(cmds, "set",
559 1.30 bouyer qfextension[quotatype], datas))
560 1.30 bouyer err(1, "prop_add_command");
561 1.30 bouyer if (!prop_dictionary_set(dict, "commands", cmds))
562 1.30 bouyer err(1, "prop_dictionary_set(command)");
563 1.30 bouyer if (Dflag)
564 1.30 bouyer printf("message to kernel:\n%s\n",
565 1.30 bouyer prop_dictionary_externalize(dict));
566 1.30 bouyer
567 1.30 bouyer if (!prop_dictionary_send_syscall(dict, &pref))
568 1.30 bouyer err(1, "prop_dictionary_send_syscall");
569 1.30 bouyer prop_object_release(dict);
570 1.30 bouyer
571 1.30 bouyer if (quotactl(qup->fsname, &pref) != 0)
572 1.30 bouyer err(1, "quotactl");
573 1.30 bouyer
574 1.30 bouyer if ((error = prop_dictionary_recv_syscall(&pref, &dict)) != 0) {
575 1.30 bouyer errx(1, "prop_dictionary_recv_syscall: %s\n",
576 1.30 bouyer strerror(error));
577 1.30 bouyer }
578 1.30 bouyer
579 1.30 bouyer if (Dflag)
580 1.30 bouyer printf("reply from kernel:\n%s\n",
581 1.30 bouyer prop_dictionary_externalize(dict));
582 1.30 bouyer
583 1.30 bouyer if ((error = quota2_get_cmds(dict, &cmds)) != 0) {
584 1.30 bouyer errx(1, "quota2_get_cmds: %s\n",
585 1.30 bouyer strerror(error));
586 1.30 bouyer }
587 1.30 bouyer /* only one command, no need to iter */
588 1.30 bouyer cmd = prop_array_get(cmds, 0);
589 1.30 bouyer if (cmd == NULL)
590 1.30 bouyer err(1, "prop_array_get(cmd)");
591 1.30 bouyer
592 1.30 bouyer if (!prop_dictionary_get_int8(cmd, "return", &error8))
593 1.30 bouyer err(1, "prop_get(return)");
594 1.30 bouyer
595 1.30 bouyer if (error8) {
596 1.30 bouyer if (qup->flags & DEFAULT)
597 1.30 bouyer fprintf(stderr, "set default %s quota: %s\n",
598 1.30 bouyer qfextension[quotatype], strerror(error8));
599 1.30 bouyer else
600 1.30 bouyer fprintf(stderr, "set %s quota for %ld: %s\n",
601 1.30 bouyer qfextension[quotatype], id, strerror(error8));
602 1.30 bouyer }
603 1.30 bouyer prop_object_release(dict);
604 1.30 bouyer }
605 1.30 bouyer
606 1.30 bouyer void
607 1.30 bouyer putprivs1(long id, int quotatype, struct quotause *qup)
608 1.30 bouyer {
609 1.30 bouyer struct dqblk dqblk;
610 1.30 bouyer int fd;
611 1.30 bouyer
612 1.30 bouyer q2e2dqblk(&qup->q2e, &dqblk);
613 1.30 bouyer assert((qup->flags & DEFAULT) == 0);
614 1.30 bouyer
615 1.30 bouyer if ((fd = open(qup->qfname, O_WRONLY)) < 0) {
616 1.30 bouyer warnx("open `%s'", qup->qfname);
617 1.30 bouyer } else {
618 1.30 bouyer (void)lseek(fd,
619 1.30 bouyer (off_t)(id * (long)sizeof (struct dqblk)),
620 1.30 bouyer SEEK_SET);
621 1.30 bouyer if (write(fd, &dqblk, sizeof (struct dqblk)) !=
622 1.30 bouyer sizeof (struct dqblk))
623 1.30 bouyer warnx("writing `%s'", qup->qfname);
624 1.30 bouyer close(fd);
625 1.1 cgd }
626 1.1 cgd }
627 1.1 cgd
628 1.1 cgd /*
629 1.18 simonb * Take a list of privileges and get it edited.
630 1.1 cgd */
631 1.12 mikel int
632 1.28 xtraeme editit(ltmpfile)
633 1.28 xtraeme char *ltmpfile;
634 1.1 cgd {
635 1.1 cgd long omask;
636 1.28 xtraeme int pid, lst;
637 1.23 jonb char p[MAX_TMPSTR];
638 1.1 cgd
639 1.1 cgd omask = sigblock(sigmask(SIGINT)|sigmask(SIGQUIT)|sigmask(SIGHUP));
640 1.1 cgd top:
641 1.1 cgd if ((pid = fork()) < 0) {
642 1.1 cgd
643 1.1 cgd if (errno == EPROCLIM) {
644 1.15 lukem warnx("You have too many processes");
645 1.1 cgd return(0);
646 1.1 cgd }
647 1.1 cgd if (errno == EAGAIN) {
648 1.1 cgd sleep(1);
649 1.1 cgd goto top;
650 1.1 cgd }
651 1.15 lukem warn("fork");
652 1.1 cgd return (0);
653 1.1 cgd }
654 1.1 cgd if (pid == 0) {
655 1.28 xtraeme const char *ed;
656 1.1 cgd
657 1.1 cgd sigsetmask(omask);
658 1.1 cgd setgid(getgid());
659 1.1 cgd setuid(getuid());
660 1.1 cgd if ((ed = getenv("EDITOR")) == (char *)0)
661 1.1 cgd ed = _PATH_VI;
662 1.28 xtraeme if (strlen(ed) + strlen(ltmpfile) + 2 >= MAX_TMPSTR) {
663 1.23 jonb err (1, "%s", "editor or filename too long");
664 1.23 jonb }
665 1.28 xtraeme snprintf (p, MAX_TMPSTR, "%s %s", ed, ltmpfile);
666 1.23 jonb execlp(_PATH_BSHELL, _PATH_BSHELL, "-c", p, NULL);
667 1.15 lukem err(1, "%s", ed);
668 1.1 cgd }
669 1.28 xtraeme waitpid(pid, &lst, 0);
670 1.1 cgd sigsetmask(omask);
671 1.28 xtraeme if (!WIFEXITED(lst) || WEXITSTATUS(lst) != 0)
672 1.1 cgd return (0);
673 1.1 cgd return (1);
674 1.1 cgd }
675 1.1 cgd
676 1.1 cgd /*
677 1.1 cgd * Convert a quotause list to an ASCII file.
678 1.1 cgd */
679 1.12 mikel int
680 1.1 cgd writeprivs(quplist, outfd, name, quotatype)
681 1.1 cgd struct quotause *quplist;
682 1.1 cgd int outfd;
683 1.1 cgd char *name;
684 1.1 cgd int quotatype;
685 1.1 cgd {
686 1.15 lukem struct quotause *qup;
687 1.1 cgd FILE *fd;
688 1.1 cgd
689 1.1 cgd ftruncate(outfd, 0);
690 1.14 kleink (void)lseek(outfd, (off_t)0, SEEK_SET);
691 1.15 lukem if ((fd = fdopen(dup(outfd), "w")) == NULL)
692 1.15 lukem errx(1, "fdopen `%s'", tmpfil);
693 1.30 bouyer if (dflag) {
694 1.30 bouyer fprintf(fd, "Default %s quotas:\n", qfextension[quotatype]);
695 1.30 bouyer } else {
696 1.30 bouyer fprintf(fd, "Quotas for %s %s:\n",
697 1.30 bouyer qfextension[quotatype], name);
698 1.30 bouyer }
699 1.1 cgd for (qup = quplist; qup; qup = qup->next) {
700 1.30 bouyer fprintf(fd, "%s (version %d):\n",
701 1.30 bouyer qup->fsname, (qup->flags & QUOTA2) ? 2 : 1);
702 1.30 bouyer if ((qup->flags & DEFAULT) == 0 || (qup->flags & QUOTA2) != 0) {
703 1.30 bouyer fprintf(fd, "\tblocks in use: %s, "
704 1.30 bouyer "limits (soft = %s, hard = %s",
705 1.30 bouyer intprt(qup->q2e.q2e_val[QL_BLOCK].q2v_cur,
706 1.30 bouyer HN_NOSPACE | HN_B, Hflag, 20),
707 1.30 bouyer intprt(qup->q2e.q2e_val[QL_BLOCK].q2v_softlimit,
708 1.30 bouyer HN_NOSPACE | HN_B, Hflag, 20),
709 1.30 bouyer intprt(qup->q2e.q2e_val[QL_BLOCK].q2v_hardlimit,
710 1.30 bouyer HN_NOSPACE | HN_B, Hflag, 20));
711 1.30 bouyer if (qup->flags & QUOTA2)
712 1.30 bouyer fprintf(fd, ", ");
713 1.30 bouyer } else
714 1.30 bouyer fprintf(fd, "\tblocks: (");
715 1.30 bouyer
716 1.30 bouyer if (qup->flags & (QUOTA2|DEFAULT)) {
717 1.30 bouyer fprintf(fd, "grace = %s",
718 1.30 bouyer timepprt(qup->q2e.q2e_val[QL_BLOCK].q2v_grace,
719 1.30 bouyer Hflag, 20));
720 1.30 bouyer }
721 1.30 bouyer fprintf(fd, ")\n");
722 1.30 bouyer if ((qup->flags & DEFAULT) == 0 || (qup->flags & QUOTA2) != 0) {
723 1.30 bouyer fprintf(fd, "\tinodes in use: %s, "
724 1.30 bouyer "limits (soft = %s, hard = %s",
725 1.30 bouyer intprt(qup->q2e.q2e_val[QL_FILE].q2v_cur,
726 1.30 bouyer HN_NOSPACE, Hflag, 20),
727 1.30 bouyer intprt(qup->q2e.q2e_val[QL_FILE].q2v_softlimit,
728 1.30 bouyer HN_NOSPACE, Hflag, 20),
729 1.30 bouyer intprt(qup->q2e.q2e_val[QL_FILE].q2v_hardlimit,
730 1.30 bouyer HN_NOSPACE, Hflag, 20));
731 1.30 bouyer if (qup->flags & QUOTA2)
732 1.30 bouyer fprintf(fd, ", ");
733 1.30 bouyer } else
734 1.30 bouyer fprintf(fd, "\tinodes: (");
735 1.30 bouyer
736 1.30 bouyer if (qup->flags & (QUOTA2|DEFAULT)) {
737 1.30 bouyer fprintf(fd, "grace = %s",
738 1.30 bouyer timepprt(qup->q2e.q2e_val[QL_FILE].q2v_grace,
739 1.30 bouyer Hflag, 20));
740 1.30 bouyer }
741 1.30 bouyer fprintf(fd, ")\n");
742 1.1 cgd }
743 1.1 cgd fclose(fd);
744 1.1 cgd return (1);
745 1.1 cgd }
746 1.1 cgd
747 1.1 cgd /*
748 1.1 cgd * Merge changes to an ASCII file into a quotause list.
749 1.1 cgd */
750 1.12 mikel int
751 1.1 cgd readprivs(quplist, infd)
752 1.1 cgd struct quotause *quplist;
753 1.1 cgd int infd;
754 1.1 cgd {
755 1.15 lukem struct quotause *qup;
756 1.1 cgd FILE *fd;
757 1.1 cgd int cnt;
758 1.30 bouyer char fsp[BUFSIZ];
759 1.30 bouyer static char line0[BUFSIZ], line1[BUFSIZ], line2[BUFSIZ];
760 1.30 bouyer static char scurb[BUFSIZ], scuri[BUFSIZ], ssoft[BUFSIZ], shard[BUFSIZ];
761 1.30 bouyer static char stime[BUFSIZ];
762 1.30 bouyer uint64_t softb, hardb, softi, hardi;
763 1.30 bouyer time_t graceb = -1, gracei = -1;
764 1.30 bouyer int version;
765 1.1 cgd
766 1.14 kleink (void)lseek(infd, (off_t)0, SEEK_SET);
767 1.1 cgd fd = fdopen(dup(infd), "r");
768 1.1 cgd if (fd == NULL) {
769 1.15 lukem warn("Can't re-read temp file");
770 1.1 cgd return (0);
771 1.1 cgd }
772 1.1 cgd /*
773 1.1 cgd * Discard title line, then read pairs of lines to process.
774 1.1 cgd */
775 1.1 cgd (void) fgets(line1, sizeof (line1), fd);
776 1.30 bouyer while (fgets(line0, sizeof (line0), fd) != NULL &&
777 1.30 bouyer fgets(line1, sizeof (line2), fd) != NULL &&
778 1.1 cgd fgets(line2, sizeof (line2), fd) != NULL) {
779 1.30 bouyer if (sscanf(line0, "%s (version %d):\n", fsp, &version) != 2) {
780 1.30 bouyer warnx("%s: bad format", line0);
781 1.26 christos goto out;
782 1.1 cgd }
783 1.30 bouyer #define last_char(str) ((str)[strlen(str) - 1])
784 1.30 bouyer if (last_char(line1) != '\n') {
785 1.30 bouyer warnx("%s:%s: bad format", fsp, line1);
786 1.26 christos goto out;
787 1.1 cgd }
788 1.30 bouyer last_char(line1) = '\0';
789 1.30 bouyer if (last_char(line2) != '\n') {
790 1.30 bouyer warnx("%s:%s: bad format", fsp, line2);
791 1.26 christos goto out;
792 1.1 cgd }
793 1.30 bouyer last_char(line2) = '\0';
794 1.30 bouyer if (dflag && version == 1) {
795 1.30 bouyer if (sscanf(line1,
796 1.30 bouyer "\tblocks: (grace = %s\n", stime) != 1) {
797 1.30 bouyer warnx("%s:%s: bad format", fsp, line1);
798 1.30 bouyer goto out;
799 1.30 bouyer }
800 1.30 bouyer if (last_char(stime) != ')') {
801 1.30 bouyer warnx("%s:%s: bad format", fsp, line1);
802 1.30 bouyer goto out;
803 1.30 bouyer }
804 1.30 bouyer last_char(stime) = '\0';
805 1.30 bouyer if (timeprd(stime, &graceb) != 0) {
806 1.30 bouyer warnx("%s:%s: bad number", fsp, stime);
807 1.30 bouyer goto out;
808 1.30 bouyer }
809 1.30 bouyer if (sscanf(line2,
810 1.30 bouyer "\tinodes: (grace = %s\n", stime) != 1) {
811 1.30 bouyer warnx("%s:%s: bad format", fsp, line2);
812 1.30 bouyer goto out;
813 1.30 bouyer }
814 1.30 bouyer if (last_char(stime) != ')') {
815 1.30 bouyer warnx("%s:%s: bad format", fsp, line2);
816 1.30 bouyer goto out;
817 1.30 bouyer }
818 1.30 bouyer last_char(stime) = '\0';
819 1.30 bouyer if (timeprd(stime, &gracei) != 0) {
820 1.30 bouyer warnx("%s:%s: bad number", fsp, stime);
821 1.30 bouyer goto out;
822 1.30 bouyer }
823 1.30 bouyer } else {
824 1.30 bouyer cnt = sscanf(line1,
825 1.30 bouyer "\tblocks in use: %s limits (soft = %s hard = %s "
826 1.30 bouyer "grace = %s", scurb, ssoft, shard, stime);
827 1.30 bouyer if (cnt == 3) {
828 1.30 bouyer if (version != 1 ||
829 1.30 bouyer last_char(scurb) != ',' ||
830 1.30 bouyer last_char(ssoft) != ',' ||
831 1.30 bouyer last_char(shard) != ')') {
832 1.30 bouyer warnx("%s:%s: bad format %d",
833 1.30 bouyer fsp, line1, cnt);
834 1.30 bouyer goto out;
835 1.30 bouyer }
836 1.30 bouyer stime[0] = '\0';
837 1.30 bouyer } else if (cnt == 4) {
838 1.30 bouyer if (version < 2 ||
839 1.30 bouyer last_char(scurb) != ',' ||
840 1.30 bouyer last_char(ssoft) != ',' ||
841 1.30 bouyer last_char(shard) != ',' ||
842 1.30 bouyer last_char(stime) != ')') {
843 1.30 bouyer warnx("%s:%s: bad format %d",
844 1.30 bouyer fsp, line1, cnt);
845 1.30 bouyer goto out;
846 1.30 bouyer }
847 1.30 bouyer } else {
848 1.30 bouyer warnx("%s: %s: bad format cnt %d", fsp, line1, cnt);
849 1.30 bouyer goto out;
850 1.30 bouyer }
851 1.30 bouyer /* drop last char which is ',' or ')' */
852 1.30 bouyer last_char(scurb) = '\0';
853 1.30 bouyer last_char(ssoft) = '\0';
854 1.30 bouyer last_char(shard) = '\0';
855 1.30 bouyer last_char(stime) = '\0';
856 1.30 bouyer
857 1.30 bouyer if (intrd(ssoft, &softb, HN_B) != 0) {
858 1.30 bouyer warnx("%s:%s: bad number", fsp, ssoft);
859 1.30 bouyer goto out;
860 1.30 bouyer }
861 1.30 bouyer if (intrd(shard, &hardb, HN_B) != 0) {
862 1.30 bouyer warnx("%s:%s: bad number", fsp, shard);
863 1.30 bouyer goto out;
864 1.30 bouyer }
865 1.30 bouyer if (cnt == 4) {
866 1.30 bouyer if (timeprd(stime, &graceb) != 0) {
867 1.30 bouyer warnx("%s:%s: bad number", fsp, stime);
868 1.30 bouyer goto out;
869 1.30 bouyer }
870 1.30 bouyer }
871 1.30 bouyer
872 1.30 bouyer cnt = sscanf(line2,
873 1.30 bouyer "\tinodes in use: %s limits (soft = %s hard = %s "
874 1.30 bouyer "grace = %s", scuri, ssoft, shard, stime);
875 1.30 bouyer if (cnt == 3) {
876 1.30 bouyer if (version != 1 ||
877 1.30 bouyer last_char(scuri) != ',' ||
878 1.30 bouyer last_char(ssoft) != ',' ||
879 1.30 bouyer last_char(shard) != ')') {
880 1.30 bouyer warnx("%s:%s: bad format %d",
881 1.30 bouyer fsp, line2, cnt);
882 1.30 bouyer goto out;
883 1.30 bouyer }
884 1.30 bouyer stime[0] = '\0';
885 1.30 bouyer } else if (cnt == 4) {
886 1.30 bouyer if (version < 2 ||
887 1.30 bouyer last_char(scuri) != ',' ||
888 1.30 bouyer last_char(ssoft) != ',' ||
889 1.30 bouyer last_char(shard) != ',' ||
890 1.30 bouyer last_char(stime) != ')') {
891 1.30 bouyer warnx("%s:%s: bad format %d",
892 1.30 bouyer fsp, line2, cnt);
893 1.30 bouyer goto out;
894 1.30 bouyer }
895 1.30 bouyer } else {
896 1.30 bouyer warnx("%s: %s: bad format", fsp, line2);
897 1.30 bouyer goto out;
898 1.30 bouyer }
899 1.30 bouyer /* drop last char which is ',' or ')' */
900 1.30 bouyer last_char(scuri) = '\0';
901 1.30 bouyer last_char(ssoft) = '\0';
902 1.30 bouyer last_char(shard) = '\0';
903 1.30 bouyer last_char(stime) = '\0';
904 1.30 bouyer if (intrd(ssoft, &softi, 0) != 0) {
905 1.30 bouyer warnx("%s:%s: bad number", fsp, ssoft);
906 1.30 bouyer goto out;
907 1.30 bouyer }
908 1.30 bouyer if (intrd(shard, &hardi, 0) != 0) {
909 1.30 bouyer warnx("%s:%s: bad number", fsp, shard);
910 1.30 bouyer goto out;
911 1.30 bouyer }
912 1.30 bouyer if (cnt == 4) {
913 1.30 bouyer if (timeprd(stime, &gracei) != 0) {
914 1.30 bouyer warnx("%s:%s: bad number", fsp, stime);
915 1.30 bouyer goto out;
916 1.30 bouyer }
917 1.30 bouyer }
918 1.1 cgd }
919 1.1 cgd for (qup = quplist; qup; qup = qup->next) {
920 1.1 cgd if (strcmp(fsp, qup->fsname))
921 1.1 cgd continue;
922 1.30 bouyer if (version == 1 && dflag) {
923 1.30 bouyer qup->q2e.q2e_val[QL_BLOCK].q2v_grace = graceb;
924 1.30 bouyer qup->q2e.q2e_val[QL_FILE].q2v_grace = gracei;
925 1.30 bouyer qup->flags |= FOUND;
926 1.30 bouyer continue;
927 1.30 bouyer }
928 1.30 bouyer
929 1.30 bouyer if (strcmp(intprt(qup->q2e.q2e_val[QL_BLOCK].q2v_cur,
930 1.30 bouyer HN_NOSPACE | HN_B, Hflag, 20),
931 1.30 bouyer scurb) != 0 ||
932 1.30 bouyer strcmp(intprt(qup->q2e.q2e_val[QL_FILE].q2v_cur,
933 1.30 bouyer HN_NOSPACE, Hflag, 20),
934 1.30 bouyer scuri) != 0) {
935 1.30 bouyer warnx("%s: cannot change current allocation",
936 1.30 bouyer fsp);
937 1.30 bouyer break;
938 1.30 bouyer }
939 1.1 cgd /*
940 1.1 cgd * Cause time limit to be reset when the quota
941 1.1 cgd * is next used if previously had no soft limit
942 1.1 cgd * or were under it, but now have a soft limit
943 1.1 cgd * and are over it.
944 1.1 cgd */
945 1.30 bouyer if (qup->q2e.q2e_val[QL_BLOCK].q2v_cur &&
946 1.30 bouyer qup->q2e.q2e_val[QL_BLOCK].q2v_cur >= softb &&
947 1.30 bouyer (qup->q2e.q2e_val[QL_BLOCK].q2v_softlimit == 0 ||
948 1.30 bouyer qup->q2e.q2e_val[QL_BLOCK].q2v_cur <
949 1.30 bouyer qup->q2e.q2e_val[QL_BLOCK].q2v_softlimit))
950 1.30 bouyer qup->q2e.q2e_val[QL_BLOCK].q2v_time = 0;
951 1.30 bouyer if (qup->q2e.q2e_val[QL_FILE].q2v_cur &&
952 1.30 bouyer qup->q2e.q2e_val[QL_FILE].q2v_cur >= softi &&
953 1.30 bouyer (qup->q2e.q2e_val[QL_FILE].q2v_softlimit == 0 ||
954 1.30 bouyer qup->q2e.q2e_val[QL_FILE].q2v_cur <
955 1.30 bouyer qup->q2e.q2e_val[QL_FILE].q2v_softlimit))
956 1.30 bouyer qup->q2e.q2e_val[QL_FILE].q2v_time = 0;
957 1.30 bouyer qup->q2e.q2e_val[QL_BLOCK].q2v_softlimit = softb;
958 1.30 bouyer qup->q2e.q2e_val[QL_BLOCK].q2v_hardlimit = hardb;
959 1.30 bouyer if (version == 2)
960 1.30 bouyer qup->q2e.q2e_val[QL_BLOCK].q2v_grace = graceb;
961 1.30 bouyer qup->q2e.q2e_val[QL_FILE].q2v_softlimit = softi;
962 1.30 bouyer qup->q2e.q2e_val[QL_FILE].q2v_hardlimit = hardi;
963 1.30 bouyer if (version == 2)
964 1.30 bouyer qup->q2e.q2e_val[QL_FILE].q2v_grace = gracei;
965 1.1 cgd qup->flags |= FOUND;
966 1.1 cgd }
967 1.1 cgd }
968 1.27 jnemeth out:
969 1.1 cgd fclose(fd);
970 1.1 cgd /*
971 1.1 cgd * Disable quotas for any filesystems that have not been found.
972 1.1 cgd */
973 1.1 cgd for (qup = quplist; qup; qup = qup->next) {
974 1.1 cgd if (qup->flags & FOUND) {
975 1.1 cgd qup->flags &= ~FOUND;
976 1.1 cgd continue;
977 1.1 cgd }
978 1.30 bouyer qup->q2e.q2e_val[QL_BLOCK].q2v_softlimit = UQUAD_MAX;
979 1.30 bouyer qup->q2e.q2e_val[QL_BLOCK].q2v_hardlimit = UQUAD_MAX;
980 1.30 bouyer qup->q2e.q2e_val[QL_BLOCK].q2v_grace = 0;
981 1.30 bouyer qup->q2e.q2e_val[QL_FILE].q2v_softlimit = UQUAD_MAX;
982 1.30 bouyer qup->q2e.q2e_val[QL_FILE].q2v_hardlimit = UQUAD_MAX;
983 1.30 bouyer qup->q2e.q2e_val[QL_FILE].q2v_grace = 0;
984 1.1 cgd }
985 1.1 cgd return (1);
986 1.1 cgd }
987 1.1 cgd
988 1.1 cgd /*
989 1.30 bouyer * Free a quotause structure.
990 1.1 cgd */
991 1.30 bouyer void
992 1.30 bouyer freeq(struct quotause *qup)
993 1.1 cgd {
994 1.30 bouyer if (qup->qfname)
995 1.30 bouyer free(qup->qfname);
996 1.30 bouyer free(qup);
997 1.1 cgd }
998 1.1 cgd
999 1.1 cgd /*
1000 1.1 cgd * Free a list of quotause structures.
1001 1.1 cgd */
1002 1.12 mikel void
1003 1.1 cgd freeprivs(quplist)
1004 1.1 cgd struct quotause *quplist;
1005 1.1 cgd {
1006 1.15 lukem struct quotause *qup, *nextqup;
1007 1.1 cgd
1008 1.1 cgd for (qup = quplist; qup; qup = nextqup) {
1009 1.1 cgd nextqup = qup->next;
1010 1.30 bouyer freeq(qup);
1011 1.1 cgd }
1012 1.1 cgd }
1013 1.1 cgd
1014 1.1 cgd /*
1015 1.1 cgd * Check whether a string is completely composed of digits.
1016 1.1 cgd */
1017 1.12 mikel int
1018 1.1 cgd alldigits(s)
1019 1.28 xtraeme const char *s;
1020 1.1 cgd {
1021 1.15 lukem int c;
1022 1.1 cgd
1023 1.1 cgd c = *s++;
1024 1.1 cgd do {
1025 1.1 cgd if (!isdigit(c))
1026 1.1 cgd return (0);
1027 1.15 lukem } while ((c = *s++) != 0);
1028 1.1 cgd return (1);
1029 1.1 cgd }
1030 1.1 cgd
1031 1.1 cgd /*
1032 1.30 bouyer * Check to see if a particular legacy quota is to be enabled in fstab
1033 1.1 cgd */
1034 1.12 mikel int
1035 1.1 cgd hasquota(fs, type, qfnamep)
1036 1.15 lukem struct fstab *fs;
1037 1.1 cgd int type;
1038 1.1 cgd char **qfnamep;
1039 1.1 cgd {
1040 1.15 lukem char *opt;
1041 1.15 lukem char *cp;
1042 1.1 cgd static char initname, usrname[100], grpname[100];
1043 1.30 bouyer char *buf;
1044 1.1 cgd
1045 1.1 cgd if (!initname) {
1046 1.1 cgd sprintf(usrname, "%s%s", qfextension[USRQUOTA], qfname);
1047 1.1 cgd sprintf(grpname, "%s%s", qfextension[GRPQUOTA], qfname);
1048 1.1 cgd initname = 1;
1049 1.1 cgd }
1050 1.30 bouyer buf = fs->fs_mntops;
1051 1.16 mrg cp = NULL;
1052 1.1 cgd for (opt = strtok(buf, ","); opt; opt = strtok(NULL, ",")) {
1053 1.15 lukem if ((cp = strchr(opt, '=')) != NULL)
1054 1.1 cgd *cp++ = '\0';
1055 1.1 cgd if (type == USRQUOTA && strcmp(opt, usrname) == 0)
1056 1.1 cgd break;
1057 1.1 cgd if (type == GRPQUOTA && strcmp(opt, grpname) == 0)
1058 1.1 cgd break;
1059 1.1 cgd }
1060 1.30 bouyer if (!opt) {
1061 1.30 bouyer *qfnamep = NULL;
1062 1.1 cgd return (0);
1063 1.30 bouyer }
1064 1.1 cgd if (cp) {
1065 1.30 bouyer *qfnamep = malloc(strlen(cp) + 1);
1066 1.30 bouyer if (*qfnamep == NULL)
1067 1.30 bouyer err(1, "malloc");
1068 1.30 bouyer strcpy(*qfnamep, cp);
1069 1.1 cgd return (1);
1070 1.1 cgd }
1071 1.30 bouyer *qfnamep = malloc(BUFSIZ);
1072 1.30 bouyer if (*qfnamep == NULL)
1073 1.30 bouyer err(1, "malloc");
1074 1.30 bouyer (void) sprintf(*qfnamep, "%s/%s.%s", fs->fs_file, qfname,
1075 1.30 bouyer qfextension[type]);
1076 1.1 cgd return (1);
1077 1.1 cgd }
1078 1.30 bouyer
1079 1.30 bouyer static void
1080 1.30 bouyer clearpriv(int argc, char **argv, const char *filesys, int quotatype)
1081 1.30 bouyer {
1082 1.30 bouyer prop_array_t cmds, datas;
1083 1.30 bouyer prop_dictionary_t protodict, dict, data, cmd;
1084 1.30 bouyer struct plistref pref;
1085 1.30 bouyer bool ret;
1086 1.30 bouyer struct statvfs *fst;
1087 1.30 bouyer int nfst, i, error;
1088 1.30 bouyer int8_t error8;
1089 1.30 bouyer int id;
1090 1.30 bouyer
1091 1.30 bouyer /* build a generic command */
1092 1.30 bouyer protodict = quota2_prop_create();
1093 1.30 bouyer cmds = prop_array_create();
1094 1.30 bouyer datas = prop_array_create();
1095 1.30 bouyer if (protodict == NULL || cmds == NULL || datas == NULL) {
1096 1.30 bouyer errx(1, "can't allocate proplist");
1097 1.30 bouyer }
1098 1.30 bouyer
1099 1.30 bouyer for ( ; argc > 0; argc--, argv++) {
1100 1.30 bouyer if ((id = getentry(*argv, quotatype)) == -1)
1101 1.30 bouyer continue;
1102 1.30 bouyer data = prop_dictionary_create();
1103 1.30 bouyer if (data == NULL)
1104 1.30 bouyer errx(1, "can't allocate proplist");
1105 1.30 bouyer
1106 1.30 bouyer ret = prop_dictionary_set_uint32(data, "id", id);
1107 1.30 bouyer if (!ret)
1108 1.30 bouyer err(1, "prop_dictionary_set(id)");
1109 1.30 bouyer if (!prop_array_add_and_rel(datas, data))
1110 1.30 bouyer err(1, "prop_array_add(data)");
1111 1.30 bouyer }
1112 1.30 bouyer if (!quota2_prop_add_command(cmds, "clear", qfextension[quotatype],
1113 1.30 bouyer datas))
1114 1.30 bouyer err(1, "prop_add_command");
1115 1.30 bouyer
1116 1.30 bouyer if (!prop_dictionary_set(protodict, "commands", cmds))
1117 1.30 bouyer err(1, "prop_dictionary_set(command)");
1118 1.30 bouyer
1119 1.30 bouyer /* now loop over quota-enabled filesystems */
1120 1.30 bouyer nfst = getmntinfo(&fst, MNT_WAIT);
1121 1.30 bouyer if (nfst == 0)
1122 1.30 bouyer errx(2, "no filesystems mounted!");
1123 1.30 bouyer
1124 1.30 bouyer for (i = 0; i < nfst; i++) {
1125 1.30 bouyer if ((fst[i].f_flag & ST_QUOTA) == 0)
1126 1.30 bouyer continue;
1127 1.30 bouyer if (filesys && strcmp(fst[i].f_mntonname, filesys) != 0 &&
1128 1.30 bouyer strcmp(fst[i].f_mntfromname, filesys) != 0)
1129 1.30 bouyer continue;
1130 1.30 bouyer if (Dflag) {
1131 1.30 bouyer fprintf(stderr, "message to kernel for %s:\n%s\n",
1132 1.30 bouyer fst[i].f_mntonname,
1133 1.30 bouyer prop_dictionary_externalize(protodict));
1134 1.30 bouyer }
1135 1.30 bouyer
1136 1.30 bouyer if (!prop_dictionary_send_syscall(protodict, &pref))
1137 1.30 bouyer err(1, "prop_dictionary_send_syscall");
1138 1.30 bouyer if (quotactl(fst[i].f_mntonname, &pref) != 0)
1139 1.30 bouyer err(1, "quotactl");
1140 1.30 bouyer
1141 1.30 bouyer if ((error = prop_dictionary_recv_syscall(&pref, &dict)) != 0) {
1142 1.30 bouyer errx(1, "prop_dictionary_recv_syscall: %s\n",
1143 1.30 bouyer strerror(error));
1144 1.30 bouyer }
1145 1.30 bouyer
1146 1.30 bouyer if (Dflag) {
1147 1.30 bouyer fprintf(stderr, "reply from kernel for %s:\n%s\n",
1148 1.30 bouyer fst[i].f_mntonname,
1149 1.30 bouyer prop_dictionary_externalize(dict));
1150 1.30 bouyer }
1151 1.30 bouyer if ((error = quota2_get_cmds(dict, &cmds)) != 0) {
1152 1.30 bouyer errx(1, "quota2_get_cmds: %s\n",
1153 1.30 bouyer strerror(error));
1154 1.30 bouyer }
1155 1.30 bouyer /* only one command, no need to iter */
1156 1.30 bouyer cmd = prop_array_get(cmds, 0);
1157 1.30 bouyer if (cmd == NULL)
1158 1.30 bouyer err(1, "prop_array_get(cmd)");
1159 1.30 bouyer
1160 1.30 bouyer if (!prop_dictionary_get_int8(cmd, "return", &error8))
1161 1.30 bouyer err(1, "prop_get(return)");
1162 1.30 bouyer if (error8) {
1163 1.30 bouyer fprintf(stderr, "clear %s quota entries on %s: %s\n",
1164 1.30 bouyer qfextension[quotatype], fst[i].f_mntonname,
1165 1.30 bouyer strerror(error8));
1166 1.30 bouyer }
1167 1.30 bouyer prop_object_release(dict);
1168 1.30 bouyer }
1169 1.30 bouyer prop_object_release(protodict);
1170 1.30 bouyer }
1171