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