edquota.c revision 1.48 1 1.48 dholland /* $NetBSD: edquota.c,v 1.48 2012/08/13 22:21:05 dholland 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.48 dholland __RCSID("$NetBSD: edquota.c,v 1.48 2012/08/13 22:21:05 dholland 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.40 dholland #include <quota.h>
60 1.30 bouyer
61 1.30 bouyer #include <assert.h>
62 1.15 lukem #include <err.h>
63 1.1 cgd #include <errno.h>
64 1.1 cgd #include <fstab.h>
65 1.1 cgd #include <pwd.h>
66 1.1 cgd #include <grp.h>
67 1.1 cgd #include <ctype.h>
68 1.15 lukem #include <signal.h>
69 1.37 dholland #include <stdbool.h>
70 1.1 cgd #include <stdio.h>
71 1.9 cgd #include <stdlib.h>
72 1.1 cgd #include <string.h>
73 1.5 mycroft #include <unistd.h>
74 1.47 christos #include <util.h>
75 1.30 bouyer
76 1.31 christos #include "printquota.h"
77 1.30 bouyer
78 1.1 cgd #include "pathnames.h"
79 1.1 cgd
80 1.46 dholland /*
81 1.46 dholland * XXX. Ideally we shouldn't compile this in, but it'll take some
82 1.46 dholland * reworking to avoid it and it'll be ok for now.
83 1.46 dholland */
84 1.46 dholland #define EDQUOTA_NUMOBJTYPES 2
85 1.46 dholland
86 1.45 dholland #if 0
87 1.31 christos static const char *quotagroup = QUOTAGROUP;
88 1.45 dholland #endif
89 1.1 cgd
90 1.37 dholland #define MAX_TMPSTR (100+MAXPATHLEN)
91 1.37 dholland
92 1.1 cgd struct quotause {
93 1.1 cgd struct quotause *next;
94 1.48 dholland unsigned found:1, /* found after running editor */
95 1.48 dholland xgrace:1, /* grace periods are per-id */
96 1.48 dholland isdefault:1;
97 1.48 dholland
98 1.46 dholland struct quotaval qv[EDQUOTA_NUMOBJTYPES];
99 1.1 cgd char fsname[MAXPATHLEN + 1];
100 1.44 dholland char implementation[32];
101 1.15 lukem };
102 1.13 mikel
103 1.37 dholland struct quotalist {
104 1.37 dholland struct quotause *head;
105 1.37 dholland struct quotause *tail;
106 1.46 dholland char *idtypename;
107 1.37 dholland };
108 1.23 jonb
109 1.33 dholland static void usage(void) __dead;
110 1.30 bouyer
111 1.31 christos static int Hflag = 0;
112 1.1 cgd
113 1.32 bouyer /* more compact form of constants */
114 1.46 dholland #define QO_BLK QUOTA_OBJTYPE_BLOCKS
115 1.46 dholland #define QO_FL QUOTA_OBJTYPE_FILES
116 1.32 bouyer
117 1.34 dholland ////////////////////////////////////////////////////////////
118 1.34 dholland // support code
119 1.1 cgd
120 1.1 cgd /*
121 1.35 dholland * This routine converts a name for a particular quota class to
122 1.1 cgd * an identifier. This routine must agree with the kernel routine
123 1.35 dholland * getinoquota as to the interpretation of quota classes.
124 1.1 cgd */
125 1.31 christos static int
126 1.41 dholland getidbyname(const char *name, int idtype)
127 1.1 cgd {
128 1.1 cgd struct passwd *pw;
129 1.1 cgd struct group *gr;
130 1.1 cgd
131 1.1 cgd if (alldigits(name))
132 1.31 christos return atoi(name);
133 1.41 dholland switch (idtype) {
134 1.41 dholland case QUOTA_IDTYPE_USER:
135 1.15 lukem if ((pw = getpwnam(name)) != NULL)
136 1.31 christos return pw->pw_uid;
137 1.15 lukem warnx("%s: no such user", name);
138 1.1 cgd break;
139 1.41 dholland case QUOTA_IDTYPE_GROUP:
140 1.15 lukem if ((gr = getgrnam(name)) != NULL)
141 1.31 christos return gr->gr_gid;
142 1.15 lukem warnx("%s: no such group", name);
143 1.1 cgd break;
144 1.1 cgd default:
145 1.41 dholland warnx("%d: unknown quota type", idtype);
146 1.1 cgd break;
147 1.1 cgd }
148 1.1 cgd sleep(1);
149 1.31 christos return -1;
150 1.1 cgd }
151 1.1 cgd
152 1.34 dholland ////////////////////////////////////////////////////////////
153 1.34 dholland // quotause operations
154 1.34 dholland
155 1.34 dholland /*
156 1.37 dholland * Create an empty quotause structure.
157 1.37 dholland */
158 1.37 dholland static struct quotause *
159 1.37 dholland quotause_create(void)
160 1.37 dholland {
161 1.37 dholland struct quotause *qup;
162 1.37 dholland
163 1.37 dholland qup = malloc(sizeof(*qup));
164 1.37 dholland if (qup == NULL) {
165 1.37 dholland err(1, "malloc");
166 1.37 dholland }
167 1.37 dholland
168 1.37 dholland qup->next = NULL;
169 1.48 dholland qup->found = 0;
170 1.48 dholland qup->xgrace = 0;
171 1.48 dholland qup->isdefault = 0;
172 1.39 dholland memset(qup->qv, 0, sizeof(qup->qv));
173 1.37 dholland qup->fsname[0] = '\0';
174 1.37 dholland
175 1.37 dholland return qup;
176 1.37 dholland }
177 1.37 dholland
178 1.37 dholland /*
179 1.34 dholland * Free a quotause structure.
180 1.34 dholland */
181 1.34 dholland static void
182 1.35 dholland quotause_destroy(struct quotause *qup)
183 1.34 dholland {
184 1.34 dholland free(qup);
185 1.34 dholland }
186 1.34 dholland
187 1.37 dholland ////////////////////////////////////////////////////////////
188 1.37 dholland // quotalist operations
189 1.37 dholland
190 1.37 dholland /*
191 1.37 dholland * Create a quotause list.
192 1.37 dholland */
193 1.37 dholland static struct quotalist *
194 1.37 dholland quotalist_create(void)
195 1.37 dholland {
196 1.37 dholland struct quotalist *qlist;
197 1.37 dholland
198 1.37 dholland qlist = malloc(sizeof(*qlist));
199 1.37 dholland if (qlist == NULL) {
200 1.37 dholland err(1, "malloc");
201 1.37 dholland }
202 1.37 dholland
203 1.37 dholland qlist->head = NULL;
204 1.37 dholland qlist->tail = NULL;
205 1.46 dholland qlist->idtypename = NULL;
206 1.37 dholland
207 1.37 dholland return qlist;
208 1.37 dholland }
209 1.37 dholland
210 1.1 cgd /*
211 1.34 dholland * Free a list of quotause structures.
212 1.1 cgd */
213 1.34 dholland static void
214 1.37 dholland quotalist_destroy(struct quotalist *qlist)
215 1.1 cgd {
216 1.34 dholland struct quotause *qup, *nextqup;
217 1.34 dholland
218 1.37 dholland for (qup = qlist->head; qup; qup = nextqup) {
219 1.34 dholland nextqup = qup->next;
220 1.35 dholland quotause_destroy(qup);
221 1.34 dholland }
222 1.46 dholland free(qlist->idtypename);
223 1.37 dholland free(qlist);
224 1.37 dholland }
225 1.37 dholland
226 1.45 dholland #if 0
227 1.37 dholland static bool
228 1.37 dholland quotalist_empty(struct quotalist *qlist)
229 1.37 dholland {
230 1.37 dholland return qlist->head == NULL;
231 1.37 dholland }
232 1.45 dholland #endif
233 1.37 dholland
234 1.37 dholland static void
235 1.37 dholland quotalist_append(struct quotalist *qlist, struct quotause *qup)
236 1.37 dholland {
237 1.37 dholland /* should not already be on a list */
238 1.37 dholland assert(qup->next == NULL);
239 1.37 dholland
240 1.37 dholland if (qlist->head == NULL) {
241 1.37 dholland qlist->head = qup;
242 1.37 dholland } else {
243 1.37 dholland qlist->tail->next = qup;
244 1.37 dholland }
245 1.37 dholland qlist->tail = qup;
246 1.34 dholland }
247 1.30 bouyer
248 1.34 dholland ////////////////////////////////////////////////////////////
249 1.34 dholland // ffs quota v1
250 1.1 cgd
251 1.45 dholland #if 0
252 1.34 dholland static void
253 1.41 dholland putprivs1(uint32_t id, int idtype, struct quotause *qup)
254 1.34 dholland {
255 1.34 dholland struct dqblk dqblk;
256 1.34 dholland int fd;
257 1.1 cgd
258 1.42 dholland quotavals_to_dqblk(&qup->qv[QUOTA_LIMIT_BLOCK],
259 1.42 dholland &qup->qv[QUOTA_LIMIT_FILE],
260 1.42 dholland &dqblk);
261 1.34 dholland assert((qup->flags & DEFAULT) == 0);
262 1.30 bouyer
263 1.34 dholland if ((fd = open(qup->qfname, O_WRONLY)) < 0) {
264 1.34 dholland warnx("open `%s'", qup->qfname);
265 1.34 dholland } else {
266 1.34 dholland (void)lseek(fd,
267 1.34 dholland (off_t)(id * (long)sizeof (struct dqblk)),
268 1.34 dholland SEEK_SET);
269 1.34 dholland if (write(fd, &dqblk, sizeof (struct dqblk)) !=
270 1.34 dholland sizeof (struct dqblk))
271 1.34 dholland warnx("writing `%s'", qup->qfname);
272 1.34 dholland close(fd);
273 1.30 bouyer }
274 1.30 bouyer }
275 1.30 bouyer
276 1.31 christos static struct quotause *
277 1.41 dholland getprivs1(long id, int idtype, const char *filesys)
278 1.30 bouyer {
279 1.30 bouyer struct fstab *fs;
280 1.47 christos char qfpathname[MAXPATHLEN], xbuf[MAXPATHLEN];
281 1.47 christos const char *fsspec;
282 1.30 bouyer struct quotause *qup;
283 1.30 bouyer struct dqblk dqblk;
284 1.30 bouyer int fd;
285 1.30 bouyer
286 1.30 bouyer setfsent();
287 1.30 bouyer while ((fs = getfsent()) != NULL) {
288 1.30 bouyer if (strcmp(fs->fs_vfstype, "ffs"))
289 1.30 bouyer continue;
290 1.47 christos fsspec = getfsspecname(xbuf, sizeof(xbuf), fs->fs_spec);
291 1.47 christos if (fsspec == NULL) {
292 1.47 christos warn("%s", xbuf);
293 1.47 christos continue;
294 1.47 christos }
295 1.47 christos if (strcmp(fsspec, filesys) == 0 ||
296 1.30 bouyer strcmp(fs->fs_file, filesys) == 0)
297 1.30 bouyer break;
298 1.30 bouyer }
299 1.30 bouyer if (fs == NULL)
300 1.30 bouyer return NULL;
301 1.30 bouyer
302 1.32 bouyer if (!hasquota(qfpathname, sizeof(qfpathname), fs,
303 1.43 dholland quota_idtype_to_ufs(idtype)))
304 1.30 bouyer return NULL;
305 1.37 dholland
306 1.37 dholland qup = quotause_create();
307 1.30 bouyer strcpy(qup->fsname, fs->fs_file);
308 1.30 bouyer if ((fd = open(qfpathname, O_RDONLY)) < 0) {
309 1.30 bouyer fd = open(qfpathname, O_RDWR|O_CREAT, 0640);
310 1.30 bouyer if (fd < 0 && errno != ENOENT) {
311 1.30 bouyer warnx("open `%s'", qfpathname);
312 1.35 dholland quotause_destroy(qup);
313 1.30 bouyer return NULL;
314 1.30 bouyer }
315 1.30 bouyer warnx("Creating quota file %s", qfpathname);
316 1.30 bouyer sleep(3);
317 1.32 bouyer (void)fchown(fd, getuid(),
318 1.35 dholland getidbyname(quotagroup, QUOTA_CLASS_GROUP));
319 1.31 christos (void)fchmod(fd, 0640);
320 1.30 bouyer }
321 1.30 bouyer (void)lseek(fd, (off_t)(id * sizeof(struct dqblk)),
322 1.30 bouyer SEEK_SET);
323 1.30 bouyer switch (read(fd, &dqblk, sizeof(struct dqblk))) {
324 1.30 bouyer case 0: /* EOF */
325 1.30 bouyer /*
326 1.30 bouyer * Convert implicit 0 quota (EOF)
327 1.30 bouyer * into an explicit one (zero'ed dqblk)
328 1.30 bouyer */
329 1.31 christos memset(&dqblk, 0, sizeof(struct dqblk));
330 1.30 bouyer break;
331 1.30 bouyer
332 1.30 bouyer case sizeof(struct dqblk): /* OK */
333 1.30 bouyer break;
334 1.30 bouyer
335 1.30 bouyer default: /* ERROR */
336 1.30 bouyer warn("read error in `%s'", qfpathname);
337 1.30 bouyer close(fd);
338 1.35 dholland quotause_destroy(qup);
339 1.30 bouyer return NULL;
340 1.30 bouyer }
341 1.30 bouyer close(fd);
342 1.30 bouyer qup->qfname = qfpathname;
343 1.1 cgd endfsent();
344 1.42 dholland dqblk_to_quotavals(&dqblk,
345 1.42 dholland &qup->qv[QUOTA_LIMIT_BLOCK],
346 1.42 dholland &qup->qv[QUOTA_LIMIT_FILE]);
347 1.31 christos return qup;
348 1.1 cgd }
349 1.45 dholland #endif
350 1.1 cgd
351 1.34 dholland ////////////////////////////////////////////////////////////
352 1.48 dholland // generic quota interface
353 1.34 dholland
354 1.44 dholland static int
355 1.44 dholland dogetprivs2(struct quotahandle *qh, int idtype, id_t id, int defaultq,
356 1.44 dholland int objtype, struct quotause *qup)
357 1.44 dholland {
358 1.44 dholland struct quotakey qk;
359 1.44 dholland
360 1.44 dholland qk.qk_idtype = idtype;
361 1.44 dholland qk.qk_id = defaultq ? QUOTA_DEFAULTID : id;
362 1.44 dholland qk.qk_objtype = objtype;
363 1.44 dholland if (quota_get(qh, &qk, &qup->qv[objtype])) {
364 1.44 dholland /* no entry, get default entry */
365 1.44 dholland qk.qk_id = QUOTA_DEFAULTID;
366 1.44 dholland if (quota_get(qh, &qk, &qup->qv[objtype])) {
367 1.44 dholland return -1;
368 1.44 dholland }
369 1.44 dholland }
370 1.44 dholland return 0;
371 1.44 dholland }
372 1.44 dholland
373 1.34 dholland static struct quotause *
374 1.46 dholland getprivs2(long id, int idtype, const char *filesys, int defaultq,
375 1.46 dholland char **idtypename_p)
376 1.1 cgd {
377 1.15 lukem struct quotause *qup;
378 1.44 dholland struct quotahandle *qh;
379 1.44 dholland const char *impl;
380 1.44 dholland unsigned restrictions;
381 1.46 dholland const char *idtypename;
382 1.1 cgd
383 1.37 dholland qup = quotause_create();
384 1.34 dholland strcpy(qup->fsname, filesys);
385 1.34 dholland if (defaultq)
386 1.48 dholland qup->isdefault = 1;
387 1.44 dholland
388 1.44 dholland qh = quota_open(filesys);
389 1.44 dholland if (qh == NULL) {
390 1.44 dholland quotause_destroy(qup);
391 1.44 dholland return NULL;
392 1.44 dholland }
393 1.44 dholland
394 1.44 dholland impl = quota_getimplname(qh);
395 1.44 dholland if (impl == NULL) {
396 1.44 dholland impl = "???";
397 1.44 dholland }
398 1.44 dholland strlcpy(qup->implementation, impl, sizeof(qup->implementation));
399 1.44 dholland
400 1.44 dholland restrictions = quota_getrestrictions(qh);
401 1.44 dholland if ((restrictions & QUOTA_RESTRICT_UNIFORMGRACE) == 0) {
402 1.48 dholland qup->xgrace = 1;
403 1.44 dholland }
404 1.44 dholland
405 1.46 dholland if (*idtypename_p == NULL) {
406 1.46 dholland idtypename = quota_idtype_getname(qh, idtype);
407 1.46 dholland *idtypename_p = strdup(idtypename);
408 1.46 dholland if (*idtypename_p == NULL) {
409 1.46 dholland errx(1, "Out of memory");
410 1.46 dholland }
411 1.46 dholland }
412 1.46 dholland
413 1.44 dholland if (dogetprivs2(qh, idtype, id, defaultq, QUOTA_OBJTYPE_BLOCKS, qup)) {
414 1.44 dholland quota_close(qh);
415 1.44 dholland quotause_destroy(qup);
416 1.44 dholland return NULL;
417 1.44 dholland }
418 1.44 dholland
419 1.44 dholland if (dogetprivs2(qh, idtype, id, defaultq, QUOTA_OBJTYPE_FILES, qup)) {
420 1.44 dholland quota_close(qh);
421 1.44 dholland quotause_destroy(qup);
422 1.44 dholland return NULL;
423 1.30 bouyer }
424 1.44 dholland
425 1.44 dholland quota_close(qh);
426 1.44 dholland
427 1.34 dholland return qup;
428 1.30 bouyer }
429 1.30 bouyer
430 1.31 christos static void
431 1.41 dholland putprivs2(uint32_t id, int idtype, struct quotause *qup)
432 1.30 bouyer {
433 1.40 dholland struct quotahandle *qh;
434 1.40 dholland struct quotakey qk;
435 1.40 dholland char idname[32];
436 1.40 dholland
437 1.48 dholland if (qup->isdefault) {
438 1.40 dholland snprintf(idname, sizeof(idname), "%s default",
439 1.41 dholland idtype == QUOTA_IDTYPE_USER ? "user" : "group");
440 1.40 dholland id = QUOTA_DEFAULTID;
441 1.40 dholland } else {
442 1.40 dholland snprintf(idname, sizeof(idname), "%s %u",
443 1.41 dholland idtype == QUOTA_IDTYPE_USER ? "uid" : "gid", id);
444 1.40 dholland }
445 1.40 dholland
446 1.40 dholland qh = quota_open(qup->fsname);
447 1.40 dholland if (qh == NULL) {
448 1.40 dholland err(1, "%s: quota_open", qup->fsname);
449 1.40 dholland }
450 1.40 dholland
451 1.41 dholland qk.qk_idtype = idtype;
452 1.40 dholland qk.qk_id = id;
453 1.40 dholland qk.qk_objtype = QUOTA_OBJTYPE_BLOCKS;
454 1.46 dholland if (quota_put(qh, &qk, &qup->qv[QO_BLK])) {
455 1.40 dholland err(1, "%s: quota_put (%s blocks)", qup->fsname, idname);
456 1.40 dholland }
457 1.30 bouyer
458 1.41 dholland qk.qk_idtype = idtype;
459 1.40 dholland qk.qk_id = id;
460 1.40 dholland qk.qk_objtype = QUOTA_OBJTYPE_FILES;
461 1.46 dholland if (quota_put(qh, &qk, &qup->qv[QO_FL])) {
462 1.40 dholland err(1, "%s: quota_put (%s files)", qup->fsname, idname);
463 1.30 bouyer }
464 1.40 dholland
465 1.40 dholland quota_close(qh);
466 1.30 bouyer }
467 1.30 bouyer
468 1.34 dholland ////////////////////////////////////////////////////////////
469 1.34 dholland // quota format switch
470 1.34 dholland
471 1.34 dholland /*
472 1.34 dholland * Collect the requested quota information.
473 1.34 dholland */
474 1.37 dholland static struct quotalist *
475 1.41 dholland getprivs(long id, int defaultq, int idtype, const char *filesys)
476 1.30 bouyer {
477 1.34 dholland struct statvfs *fst;
478 1.34 dholland int nfst, i;
479 1.37 dholland struct quotalist *qlist;
480 1.37 dholland struct quotause *qup;
481 1.37 dholland
482 1.37 dholland qlist = quotalist_create();
483 1.34 dholland
484 1.34 dholland nfst = getmntinfo(&fst, MNT_WAIT);
485 1.34 dholland if (nfst == 0)
486 1.34 dholland errx(1, "no filesystems mounted!");
487 1.30 bouyer
488 1.34 dholland for (i = 0; i < nfst; i++) {
489 1.34 dholland if ((fst[i].f_flag & ST_QUOTA) == 0)
490 1.34 dholland continue;
491 1.37 dholland if (filesys &&
492 1.37 dholland strcmp(fst[i].f_mntonname, filesys) != 0 &&
493 1.34 dholland strcmp(fst[i].f_mntfromname, filesys) != 0)
494 1.34 dholland continue;
495 1.46 dholland qup = getprivs2(id, idtype, fst[i].f_mntonname, defaultq,
496 1.46 dholland &qlist->idtypename);
497 1.37 dholland if (qup == NULL) {
498 1.37 dholland /*
499 1.37 dholland * XXX: returning NULL is totally wrong. On
500 1.37 dholland * serious error, abort; on minor error, warn
501 1.37 dholland * and continue.
502 1.37 dholland *
503 1.37 dholland * Note: we cannot warn unconditionally here
504 1.37 dholland * because this case apparently includes "no
505 1.37 dholland * quota entry on this volume" and that causes
506 1.37 dholland * the atf tests to fail. Bletch.
507 1.37 dholland */
508 1.37 dholland /*return NULL;*/
509 1.37 dholland /*warnx("getprivs2 failed");*/
510 1.37 dholland continue;
511 1.37 dholland }
512 1.37 dholland
513 1.37 dholland quotalist_append(qlist, qup);
514 1.34 dholland }
515 1.30 bouyer
516 1.45 dholland #if 0
517 1.37 dholland if (filesys && quotalist_empty(qlist)) {
518 1.34 dholland if (defaultq)
519 1.34 dholland errx(1, "no default quota for version 1");
520 1.34 dholland /* if we get there, filesys is not mounted. try the old way */
521 1.41 dholland qup = getprivs1(id, idtype, filesys);
522 1.37 dholland if (qup == NULL) {
523 1.37 dholland /* XXX. see above */
524 1.37 dholland /*return NULL;*/
525 1.37 dholland /*warnx("getprivs1 failed");*/
526 1.37 dholland return qlist;
527 1.37 dholland }
528 1.37 dholland quotalist_append(qlist, qup);
529 1.1 cgd }
530 1.45 dholland #endif
531 1.37 dholland return qlist;
532 1.1 cgd }
533 1.1 cgd
534 1.1 cgd /*
535 1.34 dholland * Store the requested quota information.
536 1.1 cgd */
537 1.37 dholland static void
538 1.41 dholland putprivs(uint32_t id, int idtype, struct quotalist *qlist)
539 1.1 cgd {
540 1.34 dholland struct quotause *qup;
541 1.1 cgd
542 1.37 dholland for (qup = qlist->head; qup; qup = qup->next) {
543 1.45 dholland #if 0
544 1.48 dholland if (qup->qfname != NULL)
545 1.48 dholland putprivs1(id, idtype, qup);
546 1.34 dholland else
547 1.45 dholland #endif
548 1.48 dholland putprivs2(id, idtype, qup);
549 1.1 cgd }
550 1.1 cgd }
551 1.1 cgd
552 1.34 dholland static void
553 1.41 dholland clearpriv(int argc, char **argv, const char *filesys, int idtype)
554 1.1 cgd {
555 1.34 dholland struct statvfs *fst;
556 1.34 dholland int nfst, i;
557 1.34 dholland int id;
558 1.40 dholland id_t *ids;
559 1.40 dholland unsigned nids, maxids, j;
560 1.40 dholland struct quotahandle *qh;
561 1.40 dholland struct quotakey qk;
562 1.40 dholland char idname[32];
563 1.40 dholland
564 1.40 dholland maxids = 4;
565 1.40 dholland nids = 0;
566 1.40 dholland ids = malloc(maxids * sizeof(ids[0]));
567 1.40 dholland if (ids == NULL) {
568 1.40 dholland err(1, "malloc");
569 1.34 dholland }
570 1.34 dholland
571 1.34 dholland for ( ; argc > 0; argc--, argv++) {
572 1.41 dholland if ((id = getidbyname(*argv, idtype)) == -1)
573 1.34 dholland continue;
574 1.34 dholland
575 1.40 dholland if (nids + 1 > maxids) {
576 1.40 dholland maxids *= 2;
577 1.40 dholland ids = realloc(ids, maxids * sizeof(ids[0]));
578 1.40 dholland if (ids == NULL) {
579 1.40 dholland err(1, "realloc");
580 1.40 dholland }
581 1.40 dholland }
582 1.40 dholland ids[nids++] = id;
583 1.40 dholland }
584 1.34 dholland
585 1.34 dholland /* now loop over quota-enabled filesystems */
586 1.34 dholland nfst = getmntinfo(&fst, MNT_WAIT);
587 1.34 dholland if (nfst == 0)
588 1.34 dholland errx(1, "no filesystems mounted!");
589 1.34 dholland
590 1.34 dholland for (i = 0; i < nfst; i++) {
591 1.34 dholland if ((fst[i].f_flag & ST_QUOTA) == 0)
592 1.34 dholland continue;
593 1.34 dholland if (filesys && strcmp(fst[i].f_mntonname, filesys) != 0 &&
594 1.34 dholland strcmp(fst[i].f_mntfromname, filesys) != 0)
595 1.34 dholland continue;
596 1.34 dholland
597 1.40 dholland qh = quota_open(fst[i].f_mntonname);
598 1.40 dholland if (qh == NULL) {
599 1.40 dholland err(1, "%s: quota_open", fst[i].f_mntonname);
600 1.40 dholland }
601 1.40 dholland
602 1.40 dholland for (j = 0; j < nids; j++) {
603 1.40 dholland snprintf(idname, sizeof(idname), "%s %u",
604 1.41 dholland idtype == QUOTA_IDTYPE_USER ?
605 1.40 dholland "uid" : "gid", ids[j]);
606 1.40 dholland
607 1.41 dholland qk.qk_idtype = idtype;
608 1.40 dholland qk.qk_id = ids[j];
609 1.40 dholland qk.qk_objtype = QUOTA_OBJTYPE_BLOCKS;
610 1.40 dholland if (quota_delete(qh, &qk)) {
611 1.40 dholland err(1, "%s: quota_delete (%s blocks)",
612 1.40 dholland fst[i].f_mntonname, idname);
613 1.40 dholland }
614 1.34 dholland
615 1.41 dholland qk.qk_idtype = idtype;
616 1.40 dholland qk.qk_id = ids[j];
617 1.40 dholland qk.qk_objtype = QUOTA_OBJTYPE_FILES;
618 1.40 dholland if (quota_delete(qh, &qk)) {
619 1.40 dholland if (errno == ENOENT) {
620 1.40 dholland /*
621 1.40 dholland * XXX ignore this case; due
622 1.40 dholland * to a weakness in the quota
623 1.40 dholland * proplib interface it can
624 1.40 dholland * appear spuriously.
625 1.40 dholland */
626 1.40 dholland } else {
627 1.40 dholland err(1, "%s: quota_delete (%s files)",
628 1.40 dholland fst[i].f_mntonname, idname);
629 1.40 dholland }
630 1.40 dholland }
631 1.34 dholland }
632 1.34 dholland
633 1.40 dholland quota_close(qh);
634 1.40 dholland }
635 1.34 dholland
636 1.40 dholland free(ids);
637 1.34 dholland }
638 1.34 dholland
639 1.34 dholland ////////////////////////////////////////////////////////////
640 1.34 dholland // editor
641 1.34 dholland
642 1.34 dholland /*
643 1.34 dholland * Take a list of privileges and get it edited.
644 1.34 dholland */
645 1.34 dholland static int
646 1.34 dholland editit(const char *ltmpfile)
647 1.34 dholland {
648 1.34 dholland pid_t pid;
649 1.34 dholland int lst;
650 1.34 dholland char p[MAX_TMPSTR];
651 1.34 dholland const char *ed;
652 1.34 dholland sigset_t s, os;
653 1.34 dholland
654 1.34 dholland sigemptyset(&s);
655 1.34 dholland sigaddset(&s, SIGINT);
656 1.34 dholland sigaddset(&s, SIGQUIT);
657 1.34 dholland sigaddset(&s, SIGHUP);
658 1.34 dholland if (sigprocmask(SIG_BLOCK, &s, &os) == -1)
659 1.34 dholland err(1, "sigprocmask");
660 1.34 dholland top:
661 1.34 dholland switch ((pid = fork())) {
662 1.34 dholland case -1:
663 1.34 dholland if (errno == EPROCLIM) {
664 1.34 dholland warnx("You have too many processes");
665 1.34 dholland return 0;
666 1.34 dholland }
667 1.34 dholland if (errno == EAGAIN) {
668 1.34 dholland sleep(1);
669 1.34 dholland goto top;
670 1.34 dholland }
671 1.34 dholland warn("fork");
672 1.34 dholland return 0;
673 1.34 dholland case 0:
674 1.34 dholland if (sigprocmask(SIG_SETMASK, &os, NULL) == -1)
675 1.34 dholland err(1, "sigprocmask");
676 1.34 dholland setgid(getgid());
677 1.34 dholland setuid(getuid());
678 1.34 dholland if ((ed = getenv("EDITOR")) == (char *)0)
679 1.34 dholland ed = _PATH_VI;
680 1.34 dholland if (strlen(ed) + strlen(ltmpfile) + 2 >= MAX_TMPSTR) {
681 1.34 dholland errx(1, "%s", "editor or filename too long");
682 1.34 dholland }
683 1.34 dholland snprintf(p, sizeof(p), "%s %s", ed, ltmpfile);
684 1.34 dholland execlp(_PATH_BSHELL, _PATH_BSHELL, "-c", p, NULL);
685 1.34 dholland err(1, "%s", ed);
686 1.34 dholland default:
687 1.34 dholland if (waitpid(pid, &lst, 0) == -1)
688 1.34 dholland err(1, "waitpid");
689 1.34 dholland if (sigprocmask(SIG_SETMASK, &os, NULL) == -1)
690 1.34 dholland err(1, "sigprocmask");
691 1.34 dholland if (!WIFEXITED(lst) || WEXITSTATUS(lst) != 0)
692 1.34 dholland return 0;
693 1.34 dholland return 1;
694 1.34 dholland }
695 1.34 dholland }
696 1.34 dholland
697 1.34 dholland /*
698 1.34 dholland * Convert a quotause list to an ASCII file.
699 1.34 dholland */
700 1.34 dholland static int
701 1.37 dholland writeprivs(struct quotalist *qlist, int outfd, const char *name,
702 1.46 dholland int idtype, const char *idtypename)
703 1.34 dholland {
704 1.34 dholland struct quotause *qup;
705 1.34 dholland FILE *fd;
706 1.34 dholland char b0[32], b1[32], b2[32], b3[32];
707 1.34 dholland
708 1.34 dholland (void)ftruncate(outfd, 0);
709 1.34 dholland (void)lseek(outfd, (off_t)0, SEEK_SET);
710 1.34 dholland if ((fd = fdopen(dup(outfd), "w")) == NULL)
711 1.38 dholland errx(1, "fdopen");
712 1.38 dholland if (name == NULL) {
713 1.46 dholland fprintf(fd, "Default %s quotas:\n", idtypename);
714 1.34 dholland } else {
715 1.46 dholland fprintf(fd, "Quotas for %s %s:\n", idtypename, name);
716 1.30 bouyer }
717 1.37 dholland for (qup = qlist->head; qup; qup = qup->next) {
718 1.39 dholland struct quotaval *q = qup->qv;
719 1.44 dholland fprintf(fd, "%s (%s):\n",
720 1.44 dholland qup->fsname, qup->implementation);
721 1.48 dholland if (!qup->isdefault || qup->xgrace) {
722 1.30 bouyer fprintf(fd, "\tblocks in use: %s, "
723 1.30 bouyer "limits (soft = %s, hard = %s",
724 1.46 dholland intprt(b1, 21, q[QO_BLK].qv_usage,
725 1.31 christos HN_NOSPACE | HN_B, Hflag),
726 1.46 dholland intprt(b2, 21, q[QO_BLK].qv_softlimit,
727 1.31 christos HN_NOSPACE | HN_B, Hflag),
728 1.46 dholland intprt(b3, 21, q[QO_BLK].qv_hardlimit,
729 1.31 christos HN_NOSPACE | HN_B, Hflag));
730 1.48 dholland if (qup->xgrace)
731 1.30 bouyer fprintf(fd, ", ");
732 1.30 bouyer } else
733 1.30 bouyer fprintf(fd, "\tblocks: (");
734 1.30 bouyer
735 1.48 dholland if (qup->xgrace || qup->isdefault) {
736 1.30 bouyer fprintf(fd, "grace = %s",
737 1.46 dholland timepprt(b0, 21, q[QO_BLK].qv_grace, Hflag));
738 1.30 bouyer }
739 1.30 bouyer fprintf(fd, ")\n");
740 1.48 dholland if (!qup->isdefault || qup->xgrace) {
741 1.30 bouyer fprintf(fd, "\tinodes in use: %s, "
742 1.30 bouyer "limits (soft = %s, hard = %s",
743 1.46 dholland intprt(b1, 21, q[QO_FL].qv_usage,
744 1.31 christos HN_NOSPACE, Hflag),
745 1.46 dholland intprt(b2, 21, q[QO_FL].qv_softlimit,
746 1.31 christos HN_NOSPACE, Hflag),
747 1.46 dholland intprt(b3, 21, q[QO_FL].qv_hardlimit,
748 1.31 christos HN_NOSPACE, Hflag));
749 1.48 dholland if (qup->xgrace)
750 1.30 bouyer fprintf(fd, ", ");
751 1.30 bouyer } else
752 1.30 bouyer fprintf(fd, "\tinodes: (");
753 1.30 bouyer
754 1.48 dholland if (qup->xgrace || qup->isdefault) {
755 1.30 bouyer fprintf(fd, "grace = %s",
756 1.46 dholland timepprt(b0, 21, q[QO_FL].qv_grace, Hflag));
757 1.30 bouyer }
758 1.30 bouyer fprintf(fd, ")\n");
759 1.1 cgd }
760 1.1 cgd fclose(fd);
761 1.31 christos return 1;
762 1.1 cgd }
763 1.1 cgd
764 1.1 cgd /*
765 1.1 cgd * Merge changes to an ASCII file into a quotause list.
766 1.1 cgd */
767 1.31 christos static int
768 1.38 dholland readprivs(struct quotalist *qlist, int infd, int dflag)
769 1.1 cgd {
770 1.15 lukem struct quotause *qup;
771 1.1 cgd FILE *fd;
772 1.1 cgd int cnt;
773 1.30 bouyer char fsp[BUFSIZ];
774 1.30 bouyer static char line0[BUFSIZ], line1[BUFSIZ], line2[BUFSIZ];
775 1.30 bouyer static char scurb[BUFSIZ], scuri[BUFSIZ], ssoft[BUFSIZ], shard[BUFSIZ];
776 1.30 bouyer static char stime[BUFSIZ];
777 1.30 bouyer uint64_t softb, hardb, softi, hardi;
778 1.30 bouyer time_t graceb = -1, gracei = -1;
779 1.30 bouyer int version;
780 1.1 cgd
781 1.14 kleink (void)lseek(infd, (off_t)0, SEEK_SET);
782 1.1 cgd fd = fdopen(dup(infd), "r");
783 1.1 cgd if (fd == NULL) {
784 1.15 lukem warn("Can't re-read temp file");
785 1.31 christos return 0;
786 1.1 cgd }
787 1.1 cgd /*
788 1.1 cgd * Discard title line, then read pairs of lines to process.
789 1.1 cgd */
790 1.1 cgd (void) fgets(line1, sizeof (line1), fd);
791 1.30 bouyer while (fgets(line0, sizeof (line0), fd) != NULL &&
792 1.30 bouyer fgets(line1, sizeof (line2), fd) != NULL &&
793 1.1 cgd fgets(line2, sizeof (line2), fd) != NULL) {
794 1.30 bouyer if (sscanf(line0, "%s (version %d):\n", fsp, &version) != 2) {
795 1.30 bouyer warnx("%s: bad format", line0);
796 1.26 christos goto out;
797 1.1 cgd }
798 1.30 bouyer #define last_char(str) ((str)[strlen(str) - 1])
799 1.30 bouyer if (last_char(line1) != '\n') {
800 1.30 bouyer warnx("%s:%s: bad format", fsp, line1);
801 1.26 christos goto out;
802 1.1 cgd }
803 1.30 bouyer last_char(line1) = '\0';
804 1.30 bouyer if (last_char(line2) != '\n') {
805 1.30 bouyer warnx("%s:%s: bad format", fsp, line2);
806 1.26 christos goto out;
807 1.1 cgd }
808 1.30 bouyer last_char(line2) = '\0';
809 1.30 bouyer if (dflag && version == 1) {
810 1.30 bouyer if (sscanf(line1,
811 1.30 bouyer "\tblocks: (grace = %s\n", stime) != 1) {
812 1.30 bouyer warnx("%s:%s: bad format", fsp, line1);
813 1.30 bouyer goto out;
814 1.30 bouyer }
815 1.30 bouyer if (last_char(stime) != ')') {
816 1.30 bouyer warnx("%s:%s: bad format", fsp, line1);
817 1.30 bouyer goto out;
818 1.30 bouyer }
819 1.30 bouyer last_char(stime) = '\0';
820 1.30 bouyer if (timeprd(stime, &graceb) != 0) {
821 1.30 bouyer warnx("%s:%s: bad number", fsp, stime);
822 1.30 bouyer goto out;
823 1.30 bouyer }
824 1.30 bouyer if (sscanf(line2,
825 1.30 bouyer "\tinodes: (grace = %s\n", stime) != 1) {
826 1.30 bouyer warnx("%s:%s: bad format", fsp, line2);
827 1.30 bouyer goto out;
828 1.30 bouyer }
829 1.30 bouyer if (last_char(stime) != ')') {
830 1.30 bouyer warnx("%s:%s: bad format", fsp, line2);
831 1.30 bouyer goto out;
832 1.30 bouyer }
833 1.30 bouyer last_char(stime) = '\0';
834 1.30 bouyer if (timeprd(stime, &gracei) != 0) {
835 1.30 bouyer warnx("%s:%s: bad number", fsp, stime);
836 1.30 bouyer goto out;
837 1.30 bouyer }
838 1.30 bouyer } else {
839 1.30 bouyer cnt = sscanf(line1,
840 1.30 bouyer "\tblocks in use: %s limits (soft = %s hard = %s "
841 1.30 bouyer "grace = %s", scurb, ssoft, shard, stime);
842 1.30 bouyer if (cnt == 3) {
843 1.30 bouyer if (version != 1 ||
844 1.30 bouyer last_char(scurb) != ',' ||
845 1.30 bouyer last_char(ssoft) != ',' ||
846 1.30 bouyer last_char(shard) != ')') {
847 1.30 bouyer warnx("%s:%s: bad format %d",
848 1.30 bouyer fsp, line1, cnt);
849 1.30 bouyer goto out;
850 1.30 bouyer }
851 1.30 bouyer stime[0] = '\0';
852 1.30 bouyer } else if (cnt == 4) {
853 1.30 bouyer if (version < 2 ||
854 1.30 bouyer last_char(scurb) != ',' ||
855 1.30 bouyer last_char(ssoft) != ',' ||
856 1.30 bouyer last_char(shard) != ',' ||
857 1.30 bouyer last_char(stime) != ')') {
858 1.30 bouyer warnx("%s:%s: bad format %d",
859 1.30 bouyer fsp, line1, cnt);
860 1.30 bouyer goto out;
861 1.30 bouyer }
862 1.30 bouyer } else {
863 1.31 christos warnx("%s: %s: bad format cnt %d", fsp, line1,
864 1.31 christos cnt);
865 1.30 bouyer goto out;
866 1.30 bouyer }
867 1.30 bouyer /* drop last char which is ',' or ')' */
868 1.30 bouyer last_char(scurb) = '\0';
869 1.30 bouyer last_char(ssoft) = '\0';
870 1.30 bouyer last_char(shard) = '\0';
871 1.30 bouyer last_char(stime) = '\0';
872 1.30 bouyer
873 1.30 bouyer if (intrd(ssoft, &softb, HN_B) != 0) {
874 1.30 bouyer warnx("%s:%s: bad number", fsp, ssoft);
875 1.30 bouyer goto out;
876 1.30 bouyer }
877 1.30 bouyer if (intrd(shard, &hardb, HN_B) != 0) {
878 1.30 bouyer warnx("%s:%s: bad number", fsp, shard);
879 1.30 bouyer goto out;
880 1.30 bouyer }
881 1.30 bouyer if (cnt == 4) {
882 1.30 bouyer if (timeprd(stime, &graceb) != 0) {
883 1.30 bouyer warnx("%s:%s: bad number", fsp, stime);
884 1.30 bouyer goto out;
885 1.30 bouyer }
886 1.30 bouyer }
887 1.30 bouyer
888 1.30 bouyer cnt = sscanf(line2,
889 1.30 bouyer "\tinodes in use: %s limits (soft = %s hard = %s "
890 1.30 bouyer "grace = %s", scuri, ssoft, shard, stime);
891 1.30 bouyer if (cnt == 3) {
892 1.30 bouyer if (version != 1 ||
893 1.30 bouyer last_char(scuri) != ',' ||
894 1.30 bouyer last_char(ssoft) != ',' ||
895 1.30 bouyer last_char(shard) != ')') {
896 1.30 bouyer warnx("%s:%s: bad format %d",
897 1.30 bouyer fsp, line2, cnt);
898 1.30 bouyer goto out;
899 1.30 bouyer }
900 1.30 bouyer stime[0] = '\0';
901 1.30 bouyer } else if (cnt == 4) {
902 1.30 bouyer if (version < 2 ||
903 1.30 bouyer last_char(scuri) != ',' ||
904 1.30 bouyer last_char(ssoft) != ',' ||
905 1.30 bouyer last_char(shard) != ',' ||
906 1.30 bouyer last_char(stime) != ')') {
907 1.30 bouyer warnx("%s:%s: bad format %d",
908 1.30 bouyer fsp, line2, cnt);
909 1.30 bouyer goto out;
910 1.30 bouyer }
911 1.30 bouyer } else {
912 1.30 bouyer warnx("%s: %s: bad format", fsp, line2);
913 1.30 bouyer goto out;
914 1.30 bouyer }
915 1.30 bouyer /* drop last char which is ',' or ')' */
916 1.30 bouyer last_char(scuri) = '\0';
917 1.30 bouyer last_char(ssoft) = '\0';
918 1.30 bouyer last_char(shard) = '\0';
919 1.30 bouyer last_char(stime) = '\0';
920 1.30 bouyer if (intrd(ssoft, &softi, 0) != 0) {
921 1.30 bouyer warnx("%s:%s: bad number", fsp, ssoft);
922 1.30 bouyer goto out;
923 1.30 bouyer }
924 1.30 bouyer if (intrd(shard, &hardi, 0) != 0) {
925 1.30 bouyer warnx("%s:%s: bad number", fsp, shard);
926 1.30 bouyer goto out;
927 1.30 bouyer }
928 1.30 bouyer if (cnt == 4) {
929 1.30 bouyer if (timeprd(stime, &gracei) != 0) {
930 1.30 bouyer warnx("%s:%s: bad number", fsp, stime);
931 1.30 bouyer goto out;
932 1.30 bouyer }
933 1.30 bouyer }
934 1.1 cgd }
935 1.37 dholland for (qup = qlist->head; qup; qup = qup->next) {
936 1.39 dholland struct quotaval *q = qup->qv;
937 1.31 christos char b1[32], b2[32];
938 1.1 cgd if (strcmp(fsp, qup->fsname))
939 1.1 cgd continue;
940 1.30 bouyer if (version == 1 && dflag) {
941 1.46 dholland q[QO_BLK].qv_grace = graceb;
942 1.46 dholland q[QO_FL].qv_grace = gracei;
943 1.48 dholland qup->found = 1;
944 1.30 bouyer continue;
945 1.30 bouyer }
946 1.30 bouyer
947 1.46 dholland if (strcmp(intprt(b1, 21, q[QO_BLK].qv_usage,
948 1.31 christos HN_NOSPACE | HN_B, Hflag),
949 1.30 bouyer scurb) != 0 ||
950 1.46 dholland strcmp(intprt(b2, 21, q[QO_FL].qv_usage,
951 1.31 christos HN_NOSPACE, Hflag),
952 1.30 bouyer scuri) != 0) {
953 1.30 bouyer warnx("%s: cannot change current allocation",
954 1.30 bouyer fsp);
955 1.30 bouyer break;
956 1.30 bouyer }
957 1.1 cgd /*
958 1.1 cgd * Cause time limit to be reset when the quota
959 1.1 cgd * is next used if previously had no soft limit
960 1.1 cgd * or were under it, but now have a soft limit
961 1.1 cgd * and are over it.
962 1.1 cgd */
963 1.46 dholland if (q[QO_BLK].qv_usage &&
964 1.46 dholland q[QO_BLK].qv_usage >= softb &&
965 1.46 dholland (q[QO_BLK].qv_softlimit == 0 ||
966 1.46 dholland q[QO_BLK].qv_usage < q[QO_BLK].qv_softlimit))
967 1.46 dholland q[QO_BLK].qv_expiretime = 0;
968 1.46 dholland if (q[QO_FL].qv_usage &&
969 1.46 dholland q[QO_FL].qv_usage >= softi &&
970 1.46 dholland (q[QO_FL].qv_softlimit == 0 ||
971 1.46 dholland q[QO_FL].qv_usage < q[QO_FL].qv_softlimit))
972 1.46 dholland q[QO_FL].qv_expiretime = 0;
973 1.46 dholland q[QO_BLK].qv_softlimit = softb;
974 1.46 dholland q[QO_BLK].qv_hardlimit = hardb;
975 1.30 bouyer if (version == 2)
976 1.46 dholland q[QO_BLK].qv_grace = graceb;
977 1.46 dholland q[QO_FL].qv_softlimit = softi;
978 1.46 dholland q[QO_FL].qv_hardlimit = hardi;
979 1.30 bouyer if (version == 2)
980 1.46 dholland q[QO_FL].qv_grace = gracei;
981 1.48 dholland qup->found = 1;
982 1.1 cgd }
983 1.1 cgd }
984 1.27 jnemeth out:
985 1.1 cgd fclose(fd);
986 1.1 cgd /*
987 1.1 cgd * Disable quotas for any filesystems that have not been found.
988 1.1 cgd */
989 1.37 dholland for (qup = qlist->head; qup; qup = qup->next) {
990 1.39 dholland struct quotaval *q = qup->qv;
991 1.48 dholland if (qup->found) {
992 1.48 dholland qup->found = 0;
993 1.1 cgd continue;
994 1.1 cgd }
995 1.46 dholland q[QO_BLK].qv_softlimit = UQUAD_MAX;
996 1.46 dholland q[QO_BLK].qv_hardlimit = UQUAD_MAX;
997 1.46 dholland q[QO_BLK].qv_grace = 0;
998 1.46 dholland q[QO_FL].qv_softlimit = UQUAD_MAX;
999 1.46 dholland q[QO_FL].qv_hardlimit = UQUAD_MAX;
1000 1.46 dholland q[QO_FL].qv_grace = 0;
1001 1.1 cgd }
1002 1.31 christos return 1;
1003 1.1 cgd }
1004 1.1 cgd
1005 1.34 dholland ////////////////////////////////////////////////////////////
1006 1.38 dholland // actions
1007 1.38 dholland
1008 1.38 dholland static void
1009 1.41 dholland replicate(const char *fs, int idtype, const char *protoname,
1010 1.38 dholland char **names, int numnames)
1011 1.38 dholland {
1012 1.38 dholland long protoid, id;
1013 1.38 dholland struct quotalist *protoprivs;
1014 1.38 dholland struct quotause *qup;
1015 1.38 dholland int i;
1016 1.38 dholland
1017 1.41 dholland if ((protoid = getidbyname(protoname, idtype)) == -1)
1018 1.38 dholland exit(1);
1019 1.41 dholland protoprivs = getprivs(protoid, 0, idtype, fs);
1020 1.38 dholland for (qup = protoprivs->head; qup; qup = qup->next) {
1021 1.46 dholland qup->qv[QO_BLK].qv_expiretime = 0;
1022 1.46 dholland qup->qv[QO_FL].qv_expiretime = 0;
1023 1.38 dholland }
1024 1.38 dholland for (i=0; i<numnames; i++) {
1025 1.41 dholland id = getidbyname(names[i], idtype);
1026 1.41 dholland if (id == -1)
1027 1.38 dholland continue;
1028 1.41 dholland putprivs(id, idtype, protoprivs);
1029 1.38 dholland }
1030 1.38 dholland /* XXX */
1031 1.38 dholland /* quotalist_destroy(protoprivs); */
1032 1.38 dholland }
1033 1.38 dholland
1034 1.38 dholland static void
1035 1.41 dholland assign(const char *fs, int idtype,
1036 1.38 dholland char *soft, char *hard, char *grace,
1037 1.38 dholland char **names, int numnames)
1038 1.38 dholland {
1039 1.38 dholland struct quotalist *curprivs;
1040 1.38 dholland struct quotause *lqup;
1041 1.38 dholland u_int64_t softb, hardb, softi, hardi;
1042 1.38 dholland time_t graceb, gracei;
1043 1.38 dholland char *str;
1044 1.38 dholland long id;
1045 1.38 dholland int dflag;
1046 1.38 dholland int i;
1047 1.38 dholland
1048 1.38 dholland if (soft) {
1049 1.38 dholland str = strsep(&soft, "/");
1050 1.38 dholland if (str[0] == '\0' || soft == NULL || soft[0] == '\0')
1051 1.38 dholland usage();
1052 1.38 dholland
1053 1.38 dholland if (intrd(str, &softb, HN_B) != 0)
1054 1.38 dholland errx(1, "%s: bad number", str);
1055 1.38 dholland if (intrd(soft, &softi, 0) != 0)
1056 1.38 dholland errx(1, "%s: bad number", soft);
1057 1.38 dholland }
1058 1.38 dholland if (hard) {
1059 1.38 dholland str = strsep(&hard, "/");
1060 1.38 dholland if (str[0] == '\0' || hard == NULL || hard[0] == '\0')
1061 1.38 dholland usage();
1062 1.38 dholland
1063 1.38 dholland if (intrd(str, &hardb, HN_B) != 0)
1064 1.38 dholland errx(1, "%s: bad number", str);
1065 1.38 dholland if (intrd(hard, &hardi, 0) != 0)
1066 1.38 dholland errx(1, "%s: bad number", hard);
1067 1.38 dholland }
1068 1.38 dholland if (grace) {
1069 1.38 dholland str = strsep(&grace, "/");
1070 1.38 dholland if (str[0] == '\0' || grace == NULL || grace[0] == '\0')
1071 1.38 dholland usage();
1072 1.38 dholland
1073 1.38 dholland if (timeprd(str, &graceb) != 0)
1074 1.38 dholland errx(1, "%s: bad number", str);
1075 1.38 dholland if (timeprd(grace, &gracei) != 0)
1076 1.38 dholland errx(1, "%s: bad number", grace);
1077 1.38 dholland }
1078 1.38 dholland for (i=0; i<numnames; i++) {
1079 1.38 dholland if (names[i] == NULL) {
1080 1.38 dholland id = 0;
1081 1.38 dholland dflag = 1;
1082 1.38 dholland } else {
1083 1.41 dholland id = getidbyname(names[i], idtype);
1084 1.38 dholland if (id == -1)
1085 1.38 dholland continue;
1086 1.38 dholland dflag = 0;
1087 1.38 dholland }
1088 1.38 dholland
1089 1.41 dholland curprivs = getprivs(id, dflag, idtype, fs);
1090 1.38 dholland for (lqup = curprivs->head; lqup; lqup = lqup->next) {
1091 1.39 dholland struct quotaval *q = lqup->qv;
1092 1.38 dholland if (soft) {
1093 1.38 dholland if (!dflag && softb &&
1094 1.46 dholland q[QO_BLK].qv_usage >= softb &&
1095 1.46 dholland (q[QO_BLK].qv_softlimit == 0 ||
1096 1.46 dholland q[QO_BLK].qv_usage <
1097 1.46 dholland q[QO_BLK].qv_softlimit))
1098 1.46 dholland q[QO_BLK].qv_expiretime = 0;
1099 1.38 dholland if (!dflag && softi &&
1100 1.46 dholland q[QO_FL].qv_usage >= softb &&
1101 1.46 dholland (q[QO_FL].qv_softlimit == 0 ||
1102 1.46 dholland q[QO_FL].qv_usage <
1103 1.46 dholland q[QO_FL].qv_softlimit))
1104 1.46 dholland q[QO_FL].qv_expiretime = 0;
1105 1.46 dholland q[QO_BLK].qv_softlimit = softb;
1106 1.46 dholland q[QO_FL].qv_softlimit = softi;
1107 1.38 dholland }
1108 1.38 dholland if (hard) {
1109 1.46 dholland q[QO_BLK].qv_hardlimit = hardb;
1110 1.46 dholland q[QO_FL].qv_hardlimit = hardi;
1111 1.38 dholland }
1112 1.38 dholland if (grace) {
1113 1.46 dholland q[QO_BLK].qv_grace = graceb;
1114 1.46 dholland q[QO_FL].qv_grace = gracei;
1115 1.38 dholland }
1116 1.38 dholland }
1117 1.41 dholland putprivs(id, idtype, curprivs);
1118 1.38 dholland quotalist_destroy(curprivs);
1119 1.38 dholland }
1120 1.38 dholland }
1121 1.38 dholland
1122 1.38 dholland static void
1123 1.41 dholland clear(const char *fs, int idtype, char **names, int numnames)
1124 1.38 dholland {
1125 1.41 dholland clearpriv(numnames, names, fs, idtype);
1126 1.38 dholland }
1127 1.38 dholland
1128 1.38 dholland static void
1129 1.41 dholland editone(const char *fs, int idtype, const char *name,
1130 1.38 dholland int tmpfd, const char *tmppath)
1131 1.38 dholland {
1132 1.38 dholland struct quotalist *curprivs;
1133 1.38 dholland long id;
1134 1.38 dholland int dflag;
1135 1.38 dholland
1136 1.38 dholland if (name == NULL) {
1137 1.38 dholland id = 0;
1138 1.38 dholland dflag = 1;
1139 1.38 dholland } else {
1140 1.41 dholland id = getidbyname(name, idtype);
1141 1.38 dholland if (id == -1)
1142 1.38 dholland return;
1143 1.38 dholland dflag = 0;
1144 1.38 dholland }
1145 1.41 dholland curprivs = getprivs(id, dflag, idtype, fs);
1146 1.38 dholland
1147 1.46 dholland if (writeprivs(curprivs, tmpfd, name, idtype,
1148 1.46 dholland curprivs->idtypename) == 0)
1149 1.38 dholland goto fail;
1150 1.38 dholland
1151 1.38 dholland if (editit(tmppath) == 0)
1152 1.38 dholland goto fail;
1153 1.38 dholland
1154 1.38 dholland if (readprivs(curprivs, tmpfd, dflag) == 0)
1155 1.38 dholland goto fail;
1156 1.38 dholland
1157 1.41 dholland putprivs(id, idtype, curprivs);
1158 1.38 dholland fail:
1159 1.38 dholland quotalist_destroy(curprivs);
1160 1.38 dholland }
1161 1.38 dholland
1162 1.38 dholland static void
1163 1.41 dholland edit(const char *fs, int idtype, char **names, int numnames)
1164 1.38 dholland {
1165 1.38 dholland char tmppath[] = _PATH_TMPFILE;
1166 1.38 dholland int tmpfd, i;
1167 1.38 dholland
1168 1.38 dholland tmpfd = mkstemp(tmppath);
1169 1.38 dholland fchown(tmpfd, getuid(), getgid());
1170 1.38 dholland
1171 1.38 dholland for (i=0; i<numnames; i++) {
1172 1.41 dholland editone(fs, idtype, names[i], tmpfd, tmppath);
1173 1.38 dholland }
1174 1.38 dholland
1175 1.38 dholland close(tmpfd);
1176 1.38 dholland unlink(tmppath);
1177 1.38 dholland }
1178 1.38 dholland
1179 1.38 dholland ////////////////////////////////////////////////////////////
1180 1.34 dholland // main
1181 1.1 cgd
1182 1.31 christos static void
1183 1.34 dholland usage(void)
1184 1.1 cgd {
1185 1.34 dholland const char *p = getprogname();
1186 1.34 dholland fprintf(stderr,
1187 1.34 dholland "Usage: %s [-D] [-H] [-u] [-p <username>] [-f <filesystem>] "
1188 1.34 dholland "-d | <username> ...\n"
1189 1.34 dholland "\t%s [-D] [-H] -g [-p <groupname>] [-f <filesystem>] "
1190 1.34 dholland "-d | <groupname> ...\n"
1191 1.34 dholland "\t%s [-D] [-u] [-f <filesystem>] [-s b#/i#] [-h b#/i#] [-t t#/t#] "
1192 1.34 dholland "-d | <username> ...\n"
1193 1.34 dholland "\t%s [-D] -g [-f <filesystem>] [-s b#/i#] [-h b#/i#] [-t t#/t#] "
1194 1.34 dholland "-d | <groupname> ...\n"
1195 1.34 dholland "\t%s [-D] [-H] [-u] -c [-f <filesystem>] username ...\n"
1196 1.34 dholland "\t%s [-D] [-H] -g -c [-f <filesystem>] groupname ...\n",
1197 1.34 dholland p, p, p, p, p, p);
1198 1.34 dholland exit(1);
1199 1.1 cgd }
1200 1.1 cgd
1201 1.34 dholland int
1202 1.34 dholland main(int argc, char *argv[])
1203 1.30 bouyer {
1204 1.41 dholland int idtype;
1205 1.34 dholland char *protoname;
1206 1.34 dholland char *soft = NULL, *hard = NULL, *grace = NULL;
1207 1.34 dholland char *fs = NULL;
1208 1.34 dholland int ch;
1209 1.34 dholland int pflag = 0;
1210 1.34 dholland int cflag = 0;
1211 1.38 dholland int dflag = 0;
1212 1.30 bouyer
1213 1.34 dholland if (argc < 2)
1214 1.34 dholland usage();
1215 1.34 dholland if (getuid())
1216 1.34 dholland errx(1, "permission denied");
1217 1.34 dholland protoname = NULL;
1218 1.41 dholland idtype = QUOTA_IDTYPE_USER;
1219 1.44 dholland while ((ch = getopt(argc, argv, "Hcdugp:s:h:t:f:")) != -1) {
1220 1.34 dholland switch(ch) {
1221 1.34 dholland case 'H':
1222 1.34 dholland Hflag++;
1223 1.34 dholland break;
1224 1.34 dholland case 'c':
1225 1.34 dholland cflag++;
1226 1.34 dholland break;
1227 1.34 dholland case 'd':
1228 1.34 dholland dflag++;
1229 1.34 dholland break;
1230 1.34 dholland case 'p':
1231 1.34 dholland protoname = optarg;
1232 1.34 dholland pflag++;
1233 1.34 dholland break;
1234 1.34 dholland case 'g':
1235 1.41 dholland idtype = QUOTA_IDTYPE_GROUP;
1236 1.34 dholland break;
1237 1.34 dholland case 'u':
1238 1.41 dholland idtype = QUOTA_IDTYPE_USER;
1239 1.34 dholland break;
1240 1.34 dholland case 's':
1241 1.34 dholland soft = optarg;
1242 1.34 dholland break;
1243 1.34 dholland case 'h':
1244 1.34 dholland hard = optarg;
1245 1.34 dholland break;
1246 1.34 dholland case 't':
1247 1.34 dholland grace = optarg;
1248 1.34 dholland break;
1249 1.34 dholland case 'f':
1250 1.34 dholland fs = optarg;
1251 1.34 dholland break;
1252 1.34 dholland default:
1253 1.34 dholland usage();
1254 1.34 dholland }
1255 1.30 bouyer }
1256 1.34 dholland argc -= optind;
1257 1.34 dholland argv += optind;
1258 1.30 bouyer
1259 1.34 dholland if (pflag) {
1260 1.34 dholland if (soft || hard || grace || dflag || cflag)
1261 1.34 dholland usage();
1262 1.41 dholland replicate(fs, idtype, protoname, argv, argc);
1263 1.38 dholland } else if (soft || hard || grace) {
1264 1.34 dholland if (cflag)
1265 1.34 dholland usage();
1266 1.34 dholland if (dflag) {
1267 1.38 dholland /* use argv[argc], which is null, to mean 'default' */
1268 1.38 dholland argc++;
1269 1.30 bouyer }
1270 1.41 dholland assign(fs, idtype, soft, hard, grace, argv, argc);
1271 1.38 dholland } else if (cflag) {
1272 1.34 dholland if (dflag)
1273 1.34 dholland usage();
1274 1.41 dholland clear(fs, idtype, argv, argc);
1275 1.38 dholland } else {
1276 1.38 dholland if (dflag) {
1277 1.38 dholland /* use argv[argc], which is null, to mean 'default' */
1278 1.38 dholland argc++;
1279 1.38 dholland }
1280 1.41 dholland edit(fs, idtype, argv, argc);
1281 1.34 dholland }
1282 1.34 dholland return 0;
1283 1.30 bouyer }
1284