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