1 #if (!defined(lint) && !defined(SABER))
2 static char rcsid_module_c[] = "$Header$";
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.
7 * It Contains: Functions for manipulating the quota information.
10 * By: Chris D. Peterson
16 * Copyright 1988 by the Massachusetts Institute of Technology.
18 * For further information on copyright and distribution
19 * see the file mit-copyright.h
25 #include <moira_site.h>
28 #include "mit-copyright.h"
33 static char * def_quota = NULL;
35 #define DEFAULT_FILESYS DEFAULT_NONE
36 #define DEFAULT_USER user /* this is the user who started moira. */
37 #define NOBODY "\\[nobody\\]"
38 #define DEFAULT_QTYPE "USER"
41 /* Function Name: GetDefaultUserQuota
42 * Description: gets the user quota from moira, and caches the value.
43 * Arguments: override - if true, go to moira and override the cache.
45 * NOTE: Using a queue here is pretty useless, but StoreInfo expects
46 * one, and it works, so why fuck with it.
50 GetDefaultUserQuota(override)
55 struct qelem * top = NULL;
56 static char *val[] = {"def_quota", NULL};
58 if (override || (def_quota == NULL)) {
59 if ( (status = do_mr_query("get_value", CountArgs(val), val,
60 StoreInfo, (char *) &top)) != MR_SUCCESS) {
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");
67 Put_message("No default Quota Found, retaining old value.");
71 info = (char **) top->q_data;
72 FreeAndClear(&def_quota, TRUE);
73 def_quota = Strsave(info[0]);
80 /* Function Name: PrintDefaultQuota
81 * Description: Prints default quota info in a meaningful way.
82 * Arguments: value of the default quota.
87 PrintDefaultQuota(quota)
90 char temp_buf[BUFSIZ];
92 sprintf(temp_buf,"The default quota is %s Kb.", quota);
93 Put_message(temp_buf);
96 /* Function Name: PrintQuota
97 * Description: Prints a users quota information.
98 * Arguments: info - a pointer to the quota information:
110 if (!strcmp(info[Q_TYPE], "ANY"))
111 sprintf(buf, "Filesystem: %s", info[Q_FILESYS]);
113 sprintf(buf, "Filesystem: %-45s %s %s", info[Q_FILESYS],
114 info[Q_TYPE], info[Q_NAME]);
116 sprintf(buf, "Machine: %-20s Directory: %-15s",
117 info[Q_MACHINE], info[Q_DIRECTORY]);
119 sprintf(buf, "Quota: %s", info[Q_QUOTA]);
121 sprintf(buf, MOD_FORMAT, info[Q_MODBY], info[Q_MODTIME], info[Q_MODWITH]);
123 return(info[Q_FILESYS]);
127 afsfilsyshelper(argc, argv, hint)
132 *hint = !strcmp(argv[FS_TYPE], "AFS");
143 if (strchr(name, '*') || strchr(name, '?') || strchr(name, '\\'))
146 status = do_mr_query("get_filesys_by_label", 1, argv,
147 afsfilsyshelper, &ret);
148 if (status == MR_SUCCESS)
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.
165 char ** args = (char **) malloc( MAX_ARGS_SIZE * sizeof(char *) );
169 Put_message("Could not allocate memory in GetQuotaArgs.");
173 args[Q_FILESYS] = Strsave(DEFAULT_FILESYS);
174 args[Q_TYPE] = Strsave(DEFAULT_QTYPE);
175 args[Q_NAME] = Strsave(DEFAULT_USER);
177 args[Q_QUOTA] = Strsave(GetDefaultUserQuota(FALSE));
178 args[Q_QUOTA+1] = NULL; /* NULL terminate. */
181 args[Q_NAME+1] = NULL; /* NULL terminate. */
183 /* Get filesystem. */
185 if (GetValueFromUser("Filesystem", &args[Q_FILESYS]) == SUB_ERROR)
187 if (quota && !ValidName(args[Q_FILESYS]))
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.");
195 com_err(program_name, af, " in afsfilesys");
199 args[Q_TYPE] = strsave("ANY");
200 args[Q_NAME] = strsave(NOBODY);
203 if (GetTypeFromUser("Quota type", "quota_type", &args[Q_TYPE]) ==
206 if (GetValueFromUser("Name", &args[Q_NAME]) == SUB_ERROR)
208 if (!ValidName(args[Q_NAME])) return(NULL);
211 if (quota) { /* Get and check quota. */
212 if (GetValueFromUser("Quota", &args[Q_QUOTA]) == SUB_ERROR)
214 if (!ValidName(args[Q_QUOTA])) return(NULL);
219 /* Function Name: ShowDefaultQuota
220 * Description: This prints out a default quota for the system.
222 * Returns: DM_NORMAL.
228 PrintDefaultQuota(GetDefaultUserQuota(TRUE));
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.
240 ChangeDefaultQuota(argc, argv)
245 char temp_buf[BUFSIZ];
246 static char *newval[] = {
247 "def_quota", NULL, NULL
250 if (!ValidName(argv[1]))
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)) {
257 if ( (status = do_mr_query("update_value", CountArgs(newval),
258 newval, Scream, NULL)) == MR_SUCCESS ) {
259 FreeAndClear(&def_quota, TRUE);
260 def_quota = Strsave(argv[1]);
263 com_err(program_name, status, " in update_value");
266 Put_message("Quota not changed.");
271 /* ------------------------ Filesystem Quotas -------------------- */
273 /* Function Name: GetQuota
274 * Description: Shows the quota of a filesystem w.r.t.
275 * a group, user, or anybody (AFS)
283 struct qelem *top = NULL;
287 if ( (args = GetQuotaArgs(FALSE) ) == NULL)
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");
298 Loop(top, (void *) PrintQuota);
304 /* Function Name: GetQuotaByFilesys
305 * Description: Shows all quotas associated with the
314 struct qelem *top = NULL;
316 char **args = (char**)malloc(2*sizeof(char*));
319 Put_message("Could not allocate memory in GetQuotaByFilesys.");
323 args[0] = Strsave("");
325 if (GetValueFromUser("Filesystem", &args[0]) == SUB_ERROR)
328 if (( status = do_mr_query("get_quota_by_filesys", 1, args,
329 StoreInfo, (char *) &top)) != MR_SUCCESS)
330 com_err(program_name, status, " in get_quota_by_filesys");
336 Loop(top, (void *) PrintQuota);
342 /* Function Name: AddQuota
343 * Description: Adds a new quota record for a filesystem
344 * w.r.t. a user, group, or anybody (AFS).
354 if ( (args = GetQuotaArgs(TRUE) ) == NULL)
357 if ( (status = do_mr_query("add_quota", CountArgs(args), args,
358 Scream, (char *) NULL)) != MR_SUCCESS)
359 com_err(program_name, status, " in add_quota");
366 /* Function Name: RealUpdateQuota
367 * Description: Performs the actual update of the quota
368 * Arguments: info - the information nesc. to update the quota.
373 RealUpdateQuota(info)
377 char temp_buf[BUFSIZ];
379 sprintf(temp_buf, "New quota for filesystem %s (in KB)", info[Q_FILESYS]);
380 if (GetValueFromUser(temp_buf, &info[Q_QUOTA]) == SUB_ERROR) {
381 Put_message("Not changed.");
385 if (status = do_mr_query("update_quota", 4, info,
386 Scream, (char *) NULL) != MR_SUCCESS) {
387 com_err(program_name, status, " in update_quota");
388 sprintf(temp_buf,"Could not perform quota change on %s",
390 Put_message(temp_buf);
394 /* Function Name: UpdateQuota
395 * Description: Updates an existing quota for a filesytem
396 * w.r.t. a user, group, or anybody.
406 struct qelem *top = NULL;
408 if ( (args = GetQuotaArgs(FALSE) ) == NULL)
411 if ( (status = do_mr_query("get_quota", CountArgs(args), args,
412 StoreInfo, (char *) &top)) != MR_SUCCESS)
413 com_err(program_name, status, " in get_quota");
415 FreeInfo(args); /* done with args, free them. */
418 Loop(top, RealUpdateQuota);
425 /* Function Name: RealDeleteQuota
426 * Description: Actually removes the quota
427 * Arguments: info - all information about this quota.
428 * one_item - true if there is only one item in the queue, and
434 RealDeleteQuota(info, one_item)
439 char temp_buf[BUFSIZ];
441 if (!strcmp(info[Q_TYPE], "ANY"))
443 "Do you really want to delete the quota on %s",
447 "Do you really want to delete the %s %s's quota on %s",
448 (strcmp(info[Q_TYPE], "USER") ? "group" : "user"),
449 info[Q_NAME], info[Q_FILESYS]);
451 if (!one_item || Confirm(temp_buf)) {
452 if ( (status = do_mr_query("delete_quota", 3, info,
453 Scream, (char *) NULL)) != MR_SUCCESS)
454 com_err(program_name, status, " in delete_quota");
456 Put_message("Quota sucessfully removed.");
459 Put_message("Aborted.");
462 /* Function Name: DeleteQuota
463 * Description: Removes the quota record for a filesystem
464 * w.r.t. a user, group, or anybody.
474 struct qelem *top = NULL;
476 if ( (args = GetQuotaArgs(FALSE) ) == NULL)
479 if ( (status = do_mr_query("get_quota", 3, args,
480 StoreInfo, (char *) &top)) != MR_SUCCESS)
481 com_err(program_name, status, " in get_quota");
486 QueryLoop(top, PrintQuota, RealDeleteQuota,
487 "Delete this quota on filesystem");