1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
|
/*
* Copyright (c) 1980, 1990 Regents of the University of California. All
* rights reserved.
*
* This code is derived from software contributed to Berkeley by Robert Elz at
* The University of Melbourne.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met: 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer. 2.
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution. 3. All advertising
* materials mentioning features or use of this software must display the
* following acknowledgement: This product includes software developed by the
* University of California, Berkeley and its contributors. 4. Neither the
* name of the University nor the names of its contributors may be used to
* endorse or promote products derived from this software without specific
* prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include "config.h"
/*
* Disk quota editor.
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <pwd.h>
#include <grp.h>
#include <ctype.h>
#include <stdio.h>
#include <string.h>
#include <signal.h>
#include <unistd.h>
#include <paths.h>
#include <stdlib.h>
#include <fcntl.h>
#include <getopt.h>
#include "pot.h"
#include "quotaops.h"
#include "quotasys.h"
#include "quotaio.h"
#include "common.h"
#define FL_EDIT_PERIOD 1
#define FL_EDIT_TIMES 2
#define FL_REMOTE 4
#define FL_NUMNAMES 8
#define FL_NO_MIXED_PATHS 16
char *progname;
static int flags, quotatype;
static int fmt = -1;
static char *protoname;
static char *dirname;
static void usage(void)
{
#if defined(RPC_SETQUOTA)
char *rpcflag = "[-rm] ";
#else
char *rpcflag = "";
#endif
errstr(_("Usage:\n\tedquota %1$s[-u] [-F formatname] [-p username] [-f filesystem] username ...\n\
\tedquota %1$s-g [-F formatname] [-p groupname] [-f filesystem] groupname ...\n\
\tedquota [-u|g] [-F formatname] [-f filesystem] -t\n\
\tedquota [-u|g] [-F formatname] [-f filesystem] -T username|groupname ...\n"), rpcflag);
fputs(_("\n\
-u, --user edit user data\n\
-g, --group edit group data\n"), stderr);
#if defined(RPC_SETQUOTA)
fputs(_("-r, --remote edit remote quota (via RPC)\n\
-m, --no-mixed-pathnames trim leading slashes from NFSv4 mountpoints\n"), stderr);
#endif
fputs(_("-F, --format=formatname edit quotas of a specific format\n\
-p, --prototype=name copy data from a prototype user/group\n\
--always-resolve always try to resolve name, even if it is\n\
composed only of digits\n\
-f, --filesystem=filesystem edit data only on a specific filesystem\n\
-t, --edit-period edit grace period\n\
-T, --edit-times edit grace time of a user/group\n\
-h, --help display this help text and exit\n\
-V, --version display version information and exit\n\n"), stderr);
fprintf(stderr, _("Bugs to: %s\n"), MY_EMAIL);
exit(1);
}
static int parse_options(int argc, char **argv)
{
int ret;
struct option long_opts[] = {
{ "help", 0, NULL, 'h' },
{ "version", 0, NULL, 'V' },
{ "prototype", 1, NULL, 'p' },
{ "user", 0, NULL, 'u' },
{ "group", 0, NULL, 'g' },
{ "format", 1, NULL, 'F' },
{ "filesystem", 1, NULL, 'f' },
#if defined(RPC_SETQUOTA)
{ "remote", 0, NULL, 'r' },
{ "no-mixed-pathnames", 0, NULL, 'm' },
#endif
{ "always-resolve", 0, NULL, 256 },
{ "edit-period", 0, NULL, 't' },
{ "edit-times", 0, NULL, 'T' },
{ NULL, 0, NULL, 0 }
};
if (argc < 2)
usage();
quotatype = USRQUOTA;
#if defined(RPC_SETQUOTA)
while ((ret = getopt_long(argc, argv, "ughrmntTVp:F:f:", long_opts, NULL)) != -1) {
#else
while ((ret = getopt_long(argc, argv, "ughtTVp:F:f:", long_opts, NULL)) != -1) {
#endif
switch (ret) {
case 'p':
protoname = optarg;
break;
case 'g':
quotatype = GRPQUOTA;
break;
#if defined(RPC_SETQUOTA)
case 'n':
case 'r':
flags |= FL_REMOTE;
break;
case 'm':
flags |= FL_NO_MIXED_PATHS;
break;
#endif
case 'u':
quotatype = USRQUOTA;
break;
case 't':
flags |= FL_EDIT_PERIOD;
break;
case 'T':
flags |= FL_EDIT_TIMES;
break;
case 'F':
if ((fmt = name2fmt(optarg)) == QF_ERROR) /* Error? */
exit(1);
break;
case 'f':
dirname = optarg;
break;
case 256:
flags |= FL_NUMNAMES;
break;
case 'V':
version();
exit(0);
case 'h':
default:
usage();
}
}
argc -= optind;
if (((flags & FL_EDIT_PERIOD) && argc != 0) || ((flags & FL_EDIT_TIMES) && argc < 1))
usage();
if ((flags & (FL_EDIT_PERIOD | FL_EDIT_TIMES)) && protoname) {
errstr(_("Prototype name does not make sense when editing grace period or times.\n"));
usage();
}
if (flags & FL_REMOTE && (flags & (FL_EDIT_TIMES | FL_EDIT_PERIOD))) {
errstr(_("Cannot change grace times over RPC protocol.\n"));
usage();
}
return optind;
}
static void copy_prototype(int argc, char **argv, struct quota_handle **handles)
{
int ret, protoid, id;
struct dquot *protoprivs, *curprivs, *pprivs, *cprivs;
ret = 0;
protoid = name2id(protoname, quotatype, !!(flags & FL_NUMNAMES), NULL);
protoprivs = getprivs(protoid, handles, 0);
while (argc-- > 0) {
id = name2id(*argv, quotatype, !!(flags & FL_NUMNAMES), NULL);
curprivs = getprivs(id, handles, 0);
if (!curprivs)
die(1, _("Cannot get quota information for user %s\n"), *argv);
argv++;
for (pprivs = protoprivs, cprivs = curprivs; pprivs && cprivs;
pprivs = pprivs->dq_next, cprivs = cprivs->dq_next) {
if (!devcmp_handles(pprivs->dq_h, cprivs->dq_h)) {
errstr(_("fsname mismatch\n"));
continue;
}
cprivs->dq_dqb.dqb_bsoftlimit =
pprivs->dq_dqb.dqb_bsoftlimit;
cprivs->dq_dqb.dqb_bhardlimit =
pprivs->dq_dqb.dqb_bhardlimit;
cprivs->dq_dqb.dqb_isoftlimit =
pprivs->dq_dqb.dqb_isoftlimit;
cprivs->dq_dqb.dqb_ihardlimit =
pprivs->dq_dqb.dqb_ihardlimit;
update_grace_times(cprivs);
}
if (putprivs(curprivs, COMMIT_LIMITS) == -1)
ret = -1;
freeprivs(curprivs);
}
if (dispose_handle_list(handles) == -1)
ret = -1;
freeprivs(protoprivs);
exit(ret ? 1 : 0);
}
int main(int argc, char **argv)
{
struct dquot *curprivs;
int tmpfd, ret, id;
struct quota_handle **handles;
char *tmpfil, *tmpdir = NULL;
gettexton();
progname = basename(argv[0]);
ret = parse_options(argc, argv);
argc -= ret;
argv += ret;
init_kernel_interface();
handles = create_handle_list(dirname ? 1 : 0, dirname ? &dirname : NULL, quotatype, fmt,
(flags & FL_NO_MIXED_PATHS) ? 0 : IOI_NFS_MIXED_PATHS,
(flags & FL_REMOTE) ? 0 : MS_LOCALONLY);
if (!handles[0]) {
dispose_handle_list(handles);
fputs(_("No filesystems with quota detected.\n"), stderr);
return 0;
}
if (protoname)
copy_prototype(argc, argv, handles);
umask(077);
if (getuid() == geteuid() && getgid() == getegid())
tmpdir = getenv("TMPDIR");
if (!tmpdir)
tmpdir = _PATH_TMP;
tmpfil = smalloc(strlen(tmpdir) + strlen("/EdP.aXXXXXX") + 1);
strcpy(tmpfil, tmpdir);
strcat(tmpfil, "/EdP.aXXXXXX");
tmpfd = mkstemp(tmpfil);
if (tmpfd < 0) {
errstr(_("Cannot create temporary file: %s\n"), strerror(errno));
ret = -1;
goto out;
}
if (fchown(tmpfd, getuid(), getgid()) < 0) {
errstr(_("Cannot change owner of temporary file: %s\n"), strerror(errno));
ret = -1;
goto out;
}
ret = 0;
if (flags & FL_EDIT_PERIOD) {
if (writetimes(handles, tmpfd) < 0) {
errstr(_("Cannot write grace times to file.\n"));
ret = -1;
}
if (editprivs(tmpfil) < 0) {
errstr(_("Error while editing grace times.\n"));
ret = -1;
}
if (readtimes(handles, tmpfd) < 0) {
errstr(_("Failed to parse grace times file.\n"));
ret = -1;
}
}
else if (flags & FL_EDIT_TIMES) {
for (; argc > 0; argc--, argv++) {
id = name2id(*argv, quotatype, !!(flags & FL_NUMNAMES), NULL);
curprivs = getprivs(id, handles, 0);
if (!curprivs)
die(1, _("Cannot get quota information for user %s.\n"), *argv);
if (writeindividualtimes(curprivs, tmpfd, *argv, quotatype) < 0) {
errstr(_("Cannot write individual grace times to file.\n"));
ret = -1;
continue;
}
if (editprivs(tmpfil) < 0) {
errstr(_("Error while editing individual grace times.\n"));
ret = -1;
continue;
}
if (readindividualtimes(curprivs, tmpfd) < 0) {
errstr(_("Cannot read individual grace times from file.\n"));
ret = -1;
continue;
}
if (putprivs(curprivs, COMMIT_TIMES) == -1)
ret = -1;
freeprivs(curprivs);
}
}
else {
for (; argc > 0; argc--, argv++) {
id = name2id(*argv, quotatype, !!(flags & FL_NUMNAMES), NULL);
curprivs = getprivs(id, handles, 0);
if (!curprivs)
die(1, _("Cannot get quota information for user %s.\n"), *argv);
if (writeprivs(curprivs, tmpfd, *argv, quotatype) < 0) {
errstr(_("Cannot write quotas to file.\n"));
ret = -1;
continue;
}
if (editprivs(tmpfil) < 0) {
errstr(_("Error while editing quotas.\n"));
ret = -1;
continue;
}
close(tmpfd);
/*
* Open in rw mode because we can reuse the file for
* editting next user as well.
*/
if ((tmpfd = open(tmpfil, O_RDWR)) < 0)
die(1, _("Cannot reopen!"));
if (readprivs(curprivs, tmpfd) < 0) {
errstr(_("Cannot read quotas from file.\n"));
ret = -1;
continue;
}
if (putprivs(curprivs, COMMIT_LIMITS) == -1)
ret = -1;
freeprivs(curprivs);
}
}
out:
if (dispose_handle_list(handles) == -1)
ret = -1;
close(tmpfd);
unlink(tmpfil);
free(tmpfil);
return ret ? 1 : 0;
}
|