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