]> andersk Git - moira.git/blame - clients/moira/quota.c
ANSIfy
[moira.git] / clients / moira / quota.c
CommitLineData
73c83e3d 1#if (!defined(lint) && !defined(SABER))
2 static char rcsid_module_c[] = "$Header$";
3#endif lint
4
8defc06b 5/* This is the file quota.c for the MOIRA Client, which allows a nieve
6 * user to quickly and easily maintain most parts of the MOIRA database.
0a2c64cb 7 * It Contains: Functions for manipulating the quota information.
73c83e3d 8 *
0a2c64cb 9 * Created: 7/10/88
10 * By: Chris D. Peterson
73c83e3d 11 *
12 * $Source$
13 * $Author$
14 * $Header$
15 *
0a2c64cb 16 * Copyright 1988 by the Massachusetts Institute of Technology.
73c83e3d 17 *
18 * For further information on copyright and distribution
19 * see the file mit-copyright.h
20 */
21
73c83e3d 22#include <stdio.h>
23#include <strings.h>
8defc06b 24#include <moira.h>
25#include <moira_site.h>
73c83e3d 26#include <menu.h>
27
28#include "mit-copyright.h"
0a2c64cb 29#include "defs.h"
30#include "f_defs.h"
73c83e3d 31#include "globals.h"
73c83e3d 32
33static char * def_quota = NULL;
34
35#define DEFAULT_FILESYS DEFAULT_NONE
8defc06b 36#define DEFAULT_USER user /* this is the user who started moira. */
bf96ab95 37#define NOBODY "\\[nobody\\]"
b124ed18 38#define DEFAULT_QTYPE "USER"
bf96ab95 39
73c83e3d 40
41/* Function Name: GetDefaultUserQuota
8defc06b 42 * Description: gets the user quota from moira, and caches the value.
43 * Arguments: override - if true, go to moira and override the cache.
73c83e3d 44 * Returns: none.
45 * NOTE: Using a queue here is pretty useless, but StoreInfo expects
46 * one, and it works, so why fuck with it.
47 */
48
49static char *
50GetDefaultUserQuota(override)
51Bool override;
52{
53 register int status;
54 char ** info;
55 struct qelem * top = NULL;
56 static char *val[] = {"def_quota", NULL};
57
58 if (override || (def_quota == NULL)) {
8defc06b 59 if ( (status = do_mr_query("get_value", CountArgs(val), val,
60 StoreInfo, (char *) &top)) != MR_SUCCESS) {
73c83e3d 61 com_err(program_name, status, " in ShowDefaultQuota");
62 if (def_quota == NULL) {
63 Put_message("No default Quota Found, setting default to 0.");
64 def_quota = Strsave("0");
65 }
66 else
67 Put_message("No default Quota Found, retaining old value.");
68 }
69 else {
70 top = QueueTop(top);
71 info = (char **) top->q_data;
72 FreeAndClear(&def_quota, TRUE);
73 def_quota = Strsave(info[0]);
74 FreeQueue(top);
75 }
76 }
77 return(def_quota);
78}
79
80/* Function Name: PrintDefaultQuota
81 * Description: Prints default quota info in a meaningful way.
82 * Arguments: value of the default quota.
83 * Returns: none.
84 */
85
86static void
87PrintDefaultQuota(quota)
88char * quota;
89{
90 char temp_buf[BUFSIZ];
91 Put_message("");
92 sprintf(temp_buf,"The default quota is %s Kb.", quota);
93 Put_message(temp_buf);
94}
95
96/* Function Name: PrintQuota
97 * Description: Prints a users quota information.
98 * Arguments: info - a pointer to the quota information:
99 * Returns: none.
100 */
101
102static char *
103PrintQuota(info)
104char ** info;
105{
b124ed18 106 char buf[BUFSIZ];
bf96ab95 107
73c83e3d 108 Put_message("");
b124ed18 109
110 if (!strcmp(info[Q_TYPE], "ANY"))
111 sprintf(buf, "Filesystem: %s", info[Q_FILESYS]);
112 else
113 sprintf(buf, "Filesystem: %-45s %s %s", info[Q_FILESYS],
114 info[Q_TYPE], info[Q_NAME]);
73c83e3d 115 Put_message(buf);
bf96ab95 116 sprintf(buf, "Machine: %-20s Directory: %-15s",
117 info[Q_MACHINE], info[Q_DIRECTORY]);
118 Put_message(buf);
119 sprintf(buf, "Quota: %s", info[Q_QUOTA]);
73c83e3d 120 Put_message(buf);
70aa4030 121 sprintf(buf, MOD_FORMAT, info[Q_MODBY], info[Q_MODTIME], info[Q_MODWITH]);
122 Put_message(buf);
73c83e3d 123 return(info[Q_FILESYS]);
124}
125
bf96ab95 126
127afsfilsyshelper(argc, argv, hint)
128int argc;
129char **argv;
130int *hint;
131{
132 *hint = !strcmp(argv[FS_TYPE], "AFS");
133 return(0);
134}
135
136
137int afsfilesys(name)
138char *name;
139{
140 int status, ret = 0;
141 char *argv[1];
142
d950adaf 143 if (index(name, '*') || index(name, '?') || index(name, '\\'))
144 return(0);
bf96ab95 145 argv[0] = name;
8defc06b 146 status = do_mr_query("get_filesys_by_label", 1, argv,
bf96ab95 147 afsfilsyshelper, &ret);
8defc06b 148 if (status == MR_SUCCESS)
bf96ab95 149 return(ret);
6d8848f6 150 return(status);
bf96ab95 151
152}
153
154
73c83e3d 155/* Function Name: GetQuotaArgs
156 * Description: gets quota args from the user
157 * Arguments: quota - if TRUE the get quota too.
158 * Returns: the arguments.
159 */
160
161static char **
162GetQuotaArgs(quota)
163Bool quota;
164{
165 char ** args = (char **) malloc( MAX_ARGS_SIZE * sizeof(char *) );
6d8848f6 166 int af;
73c83e3d 167
168 if (args == NULL) {
169 Put_message("Could not allocate memory in GetQuotaArgs.");
170 return(NULL);
171 }
172
173 args[Q_FILESYS] = Strsave(DEFAULT_FILESYS);
b124ed18 174 args[Q_TYPE] = Strsave(DEFAULT_QTYPE);
175 args[Q_NAME] = Strsave(DEFAULT_USER);
73c83e3d 176 if (quota) {
177 args[Q_QUOTA] = Strsave(GetDefaultUserQuota(FALSE));
b124ed18 178 args[Q_QUOTA+1] = NULL; /* NULL terminate. */
73c83e3d 179 }
180 else
b124ed18 181 args[Q_NAME+1] = NULL; /* NULL terminate. */
73c83e3d 182
183 /* Get filesystem. */
184
245b2337 185 if (GetValueFromUser("Filesystem", &args[Q_FILESYS]) == SUB_ERROR)
186 return(NULL);
bf96ab95 187 if (quota && !ValidName(args[Q_FILESYS]))
188 return(NULL);
b124ed18 189
6d8848f6 190 af = afsfilesys(args[Q_FILESYS]);
191 if (af != 0 && af != (1 == 1)) {
192 if (af == MR_NO_MATCH)
193 Put_message("That filesystem does not exist.");
194 else
195 com_err(program_name, af, " in afsfilesys");
196 return(NULL);
197 }
198 if (af) {
b124ed18 199 args[Q_TYPE] = strsave("ANY");
200 args[Q_NAME] = strsave(NOBODY);
201 }
202 else {
245b2337 203 if (GetTypeFromUser("Quota type", "quota_type", &args[Q_TYPE]) ==
204 SUB_ERROR)
205 return(NULL);
206 if (GetValueFromUser("Name", &args[Q_NAME]) == SUB_ERROR)
207 return(NULL);
208 if (!ValidName(args[Q_NAME])) return(NULL);
bf96ab95 209 }
73c83e3d 210
211 if (quota) { /* Get and check quota. */
245b2337 212 if (GetValueFromUser("Quota", &args[Q_QUOTA]) == SUB_ERROR)
213 return(NULL);
214 if (!ValidName(args[Q_QUOTA])) return(NULL);
73c83e3d 215 }
216 return(args);
217}
218
73c83e3d 219/* Function Name: ShowDefaultQuota
220 * Description: This prints out a default quota for the system.
221 * Arguments: none
222 * Returns: DM_NORMAL.
223 */
224
225int
226ShowDefaultQuota()
227{
228 PrintDefaultQuota(GetDefaultUserQuota(TRUE));
229 return (DM_NORMAL);
230}
231
232/* Function Name: ChangeDefaultQuota
233 * Description: Changes the System Wide default quota.
234 * Arguments: argc, argv - New quota in argv[1].
235 * Returns: DM_NORMAL.
236 */
237
238/*ARGSUSED*/
239int
240ChangeDefaultQuota(argc, argv)
241int argc;
242char *argv[];
243{
244 register int status;
245 char temp_buf[BUFSIZ];
246 static char *newval[] = {
247 "def_quota", NULL, NULL
248 };
249
250 if (!ValidName(argv[1]))
251 return(DM_NORMAL);
252
253 sprintf(temp_buf,"%s %s","Are you sure that you want to",
254 "change the default quota for all new users");
255 if(Confirm(temp_buf)) {
256 newval[1] = argv[1];
8defc06b 257 if ( (status = do_mr_query("update_value", CountArgs(newval),
258 newval, Scream, NULL)) == MR_SUCCESS ) {
73c83e3d 259 FreeAndClear(&def_quota, TRUE);
260 def_quota = Strsave(argv[1]);
261 }
262 else
263 com_err(program_name, status, " in update_value");
264 }
265 else
266 Put_message("Quota not changed.");
267
268 return (DM_NORMAL);
269}
270
b124ed18 271/* ------------------------ Filesystem Quotas -------------------- */
73c83e3d 272
b124ed18 273/* Function Name: GetQuota
274 * Description: Shows the quota of a filesystem w.r.t.
275 * a group, user, or anybody (AFS)
276 * Arguments: none
277 * Returns: DM_NORMAL
73c83e3d 278 */
279
280int
b124ed18 281GetQuota()
73c83e3d 282{
283 struct qelem *top = NULL;
284 register int status;
285 char ** args;
286
287 if ( (args = GetQuotaArgs(FALSE) ) == NULL)
288 return(DM_NORMAL);
289
b124ed18 290 if (( status = do_mr_query("get_quota", CountArgs(args), args,
291 StoreInfo, (char *) &top)) != MR_SUCCESS)
292 com_err(program_name, status, " in get_quota");
293
294 FreeInfo(args);
73c83e3d 295 free(args);
296
297 top = QueueTop(top);
298 Loop(top, (void *) PrintQuota);
b124ed18 299
73c83e3d 300 FreeQueue(top);
b124ed18 301 return(DM_NORMAL);
73c83e3d 302}
303
b124ed18 304/* Function Name: GetQuotaByFilesys
305 * Description: Shows all quotas associated with the
306 * given filesystem
307 * Arguments: none
308 * Returns: DM_NORMAL
73c83e3d 309 */
310
311int
b124ed18 312GetQuotaByFilesys()
313{
314 struct qelem *top = NULL;
315 register int status;
316 char **args = (char**)malloc(2*sizeof(char*));
317
318 if (args == NULL) {
319 Put_message("Could not allocate memory in GetQuotaByFilesys.");
320 return(DM_NORMAL);
321 }
322
323 args[0] = args[1] = NULL;
245b2337 324 if (GetValueFromUser("Filesystem", &args[0]) == SUB_ERROR)
325 return(DM_NORMAL);
b124ed18 326
327 if (( status = do_mr_query("get_quota_by_filesys", 1, args,
328 StoreInfo, (char *) &top)) != MR_SUCCESS)
329 com_err(program_name, status, " in get_quota_by_filesys");
330
331 FreeInfo(args);
332 free(args);
333
334 top = QueueTop(top);
335 Loop(top, (void *) PrintQuota);
336
337 FreeQueue(top);
338 return(DM_NORMAL);
339}
340
341/* Function Name: AddQuota
342 * Description: Adds a new quota record for a filesystem
343 * w.r.t. a user, group, or anybody (AFS).
344 * Arguments: None
345 * Returns: DM_NORMAL
346 */
347int
348AddQuota()
73c83e3d 349{
350 char ** args;
351 register int status;
352
353 if ( (args = GetQuotaArgs(TRUE) ) == NULL)
354 return(DM_NORMAL);
355
b124ed18 356 if ( (status = do_mr_query("add_quota", CountArgs(args), args,
8defc06b 357 Scream, (char *) NULL)) != MR_SUCCESS)
b124ed18 358 com_err(program_name, status, " in add_quota");
73c83e3d 359
360 FreeInfo(args);
361 free(args);
362 return(DM_NORMAL);
363}
364
b124ed18 365/* Function Name: RealUpdateQuota
366 * Description: Performs the actual update of the quota
367 * Arguments: info - the information nesc. to update the quota.
73c83e3d 368 * Returns: none.
369 */
370
371static void
b124ed18 372RealUpdateQuota(info)
73c83e3d 373char ** info;
374{
375 register int status;
376 char temp_buf[BUFSIZ];
377
378 sprintf(temp_buf, "New quota for filesystem %s (in KB)", info[Q_FILESYS]);
245b2337 379 if (GetValueFromUser(temp_buf, &info[Q_QUOTA]) == SUB_ERROR) {
380 Put_message("Not changed.");
381 return;
382 }
b124ed18 383
384 if (status = do_mr_query("update_quota", 4, info,
8defc06b 385 Scream, (char *) NULL) != MR_SUCCESS) {
b124ed18 386 com_err(program_name, status, " in update_quota");
73c83e3d 387 sprintf(temp_buf,"Could not perform quota change on %s",
388 info[Q_FILESYS]);
389 Put_message(temp_buf);
390 }
391}
b124ed18 392
393/* Function Name: UpdateQuota
394 * Description: Updates an existing quota for a filesytem
395 * w.r.t. a user, group, or anybody.
396 * Arguments: None
397 * Returns: DM_NORMAL
73c83e3d 398 */
399
400int
b124ed18 401UpdateQuota()
73c83e3d 402{
403 int status;
404 char **args;
405 struct qelem *top = NULL;
406
407 if ( (args = GetQuotaArgs(FALSE) ) == NULL)
408 return(DM_NORMAL);
409
b124ed18 410 if ( (status = do_mr_query("get_quota", CountArgs(args), args,
8defc06b 411 StoreInfo, (char *) &top)) != MR_SUCCESS)
b124ed18 412 com_err(program_name, status, " in get_quota");
73c83e3d 413
414 FreeInfo(args); /* done with args, free them. */
415 free(args);
416 top = QueueTop(top);
b124ed18 417 Loop(top, RealUpdateQuota);
73c83e3d 418
419 FreeQueue(top);
420 return (DM_NORMAL);
421}
b124ed18 422
423
424/* Function Name: RealDeleteQuota
425 * Description: Actually removes the quota
426 * Arguments: info - all information about this quota.
73c83e3d 427 * one_item - true if there is only one item in the queue, and
428 * we should confirm.
429 * Returns: none.
430 */
431
432static void
b124ed18 433RealDeleteQuota(info, one_item)
73c83e3d 434char ** info;
435Bool one_item;
436{
437 register int status;
438 char temp_buf[BUFSIZ];
439
b124ed18 440 if (!strcmp(info[Q_TYPE], "ANY"))
441 sprintf(temp_buf,
442 "Do you really want to delete the quota on %s",
443 info[Q_FILESYS]);
444 else
445 sprintf(temp_buf,
446 "Do you really want to delete the %s %s's quota on %s",
447 (strcmp(info[Q_TYPE], "USER") ? "group" : "user"),
448 info[Q_NAME], info[Q_FILESYS]);
73c83e3d 449
450 if (!one_item || Confirm(temp_buf)) {
b124ed18 451 if ( (status = do_mr_query("delete_quota", 3, info,
8defc06b 452 Scream, (char *) NULL)) != MR_SUCCESS)
b124ed18 453 com_err(program_name, status, " in delete_quota");
73c83e3d 454 else
455 Put_message("Quota sucessfully removed.");
456 }
457 else
458 Put_message("Aborted.");
459}
460
b124ed18 461/* Function Name: DeleteQuota
462 * Description: Removes the quota record for a filesystem
463 * w.r.t. a user, group, or anybody.
464 * Arguments: None
465 * Returns: DM_NORMAL
73c83e3d 466 */
467
468int
b124ed18 469DeleteQuota()
73c83e3d 470{
471 register int status;
472 char **args;
473 struct qelem *top = NULL;
474
475 if ( (args = GetQuotaArgs(FALSE) ) == NULL)
476 return(DM_NORMAL);
477
b124ed18 478 if ( (status = do_mr_query("get_quota", 3, args,
8defc06b 479 StoreInfo, (char *) &top)) != MR_SUCCESS)
b124ed18 480 com_err(program_name, status, " in get_quota");
73c83e3d 481
482 FreeInfo(args);
483 free(args);
484 top = QueueTop(top);
b124ed18 485 QueryLoop(top, PrintQuota, RealDeleteQuota,
486 "Delete this quota on filesystem");
73c83e3d 487
488 FreeQueue(top);
489 return(DM_NORMAL);
490}
b124ed18 491
492
This page took 0.139282 seconds and 5 git commands to generate.