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
26 #include <moira_site.h>
29 #include "mit-copyright.h"
34 static char *def_quota = NULL;
36 #define DEFAULT_FILESYS DEFAULT_NONE
37 #define DEFAULT_USER user /* this is the user who started moira. */
38 #define NOBODY "[nobody]"
39 #define DEFAULT_QTYPE "USER"
42 /* Function Name: GetDefaultUserQuota
43 * Description: gets the user quota from moira, and caches the value.
44 * Arguments: override - if true, go to moira and override the cache.
46 * NOTE: Using a queue here is pretty useless, but StoreInfo expects
47 * one, and it works, so why fuck with it.
50 static char *GetDefaultUserQuota(Bool override)
54 struct qelem *top = NULL;
55 static char *val[] = {"def_quota", NULL};
57 if (override || (def_quota == NULL))
59 if ((status = do_mr_query("get_value", CountArgs(val), val,
60 StoreInfo, (char *) &top)))
62 com_err(program_name, status, " in ShowDefaultQuota");
65 Put_message("No default Quota Found, setting default to 0.");
66 def_quota = Strsave("0");
69 Put_message("No default Quota Found, retaining old value.");
74 info = (char **) top->q_data;
75 FreeAndClear(&def_quota, TRUE);
76 def_quota = Strsave(info[0]);
83 /* Function Name: PrintDefaultQuota
84 * Description: Prints default quota info in a meaningful way.
85 * Arguments: value of the default quota.
89 static void PrintDefaultQuota(char *quota)
91 char temp_buf[BUFSIZ];
93 sprintf(temp_buf, "The default quota is %s Kb.", quota);
94 Put_message(temp_buf);
97 /* Function Name: PrintQuota
98 * Description: Prints a users quota information.
99 * Arguments: info - a pointer to the quota information:
103 static char *PrintQuota(char **info)
109 if (!strcmp(info[Q_TYPE], "ANY"))
110 sprintf(buf, "Filesystem: %s", info[Q_FILESYS]);
112 sprintf(buf, "Filesystem: %-45s %s %s", info[Q_FILESYS],
113 info[Q_TYPE], info[Q_NAME]);
115 sprintf(buf, "Machine: %-20s Directory: %-15s",
116 info[Q_MACHINE], info[Q_DIRECTORY]);
118 sprintf(buf, "Quota: %s", info[Q_QUOTA]);
120 sprintf(buf, MOD_FORMAT, info[Q_MODBY], info[Q_MODTIME], info[Q_MODWITH]);
122 return info[Q_FILESYS];
126 int afsfilsyshelper(int argc, char **argv, int *hint)
128 *hint = !strcmp(argv[FS_TYPE], "AFS");
133 int afsfilesys(char *name)
138 if (strchr(name, '*') || strchr(name, '?') || strchr(name, '\\'))
141 status = do_mr_query("get_filesys_by_label", 1, argv, afsfilsyshelper, &ret);
142 if (status == MR_SUCCESS)
148 /* Function Name: GetQuotaArgs
149 * Description: gets quota args from the user
150 * Arguments: quota - if TRUE the get quota too.
151 * Returns: the arguments.
154 static char **GetQuotaArgs(Bool quota)
156 char **args = malloc(MAX_ARGS_SIZE * sizeof(char *));
161 Put_message("Could not allocate memory in GetQuotaArgs.");
165 args[Q_FILESYS] = Strsave(DEFAULT_FILESYS);
166 args[Q_TYPE] = Strsave(DEFAULT_QTYPE);
167 args[Q_NAME] = Strsave(DEFAULT_USER);
170 args[Q_QUOTA] = Strsave(GetDefaultUserQuota(FALSE));
171 args[Q_QUOTA + 1] = NULL; /* NULL terminate. */
174 args[Q_NAME + 1] = NULL; /* NULL terminate. */
176 /* Get filesystem. */
178 if (GetValueFromUser("Filesystem", &args[Q_FILESYS]) == SUB_ERROR)
180 if (quota && !ValidName(args[Q_FILESYS]))
183 af = afsfilesys(args[Q_FILESYS]);
184 if (af != 0 && af != 1)
186 if (af == MR_NO_MATCH)
187 Put_message("That filesystem does not exist.");
189 com_err(program_name, af, " in afsfilesys");
194 args[Q_TYPE] = strsave("ANY");
195 args[Q_NAME] = strsave(NOBODY);
199 if (GetTypeFromUser("Quota type", "quota_type", &args[Q_TYPE]) ==
202 if (GetValueFromUser("Name", &args[Q_NAME]) == SUB_ERROR)
204 if (!ValidName(args[Q_NAME]))
208 if (quota) /* Get and check quota. */
210 if (GetValueFromUser("Quota", &args[Q_QUOTA]) == SUB_ERROR)
212 if (!ValidName(args[Q_QUOTA]))
218 /* Function Name: ShowDefaultQuota
219 * Description: This prints out a default quota for the system.
221 * Returns: DM_NORMAL.
224 int ShowDefaultQuota(void)
226 PrintDefaultQuota(GetDefaultUserQuota(TRUE));
230 /* Function Name: ChangeDefaultQuota
231 * Description: Changes the System Wide default quota.
232 * Arguments: argc, argv - New quota in argv[1].
233 * Returns: DM_NORMAL.
236 int ChangeDefaultQuota(int argc, char *argv[])
239 char temp_buf[BUFSIZ];
240 static char *newval[] = {
241 "def_quota", NULL, NULL
244 if (!ValidName(argv[1]))
247 sprintf(temp_buf, "%s %s", "Are you sure that you want to",
248 "change the default quota for all new users");
249 if (Confirm(temp_buf))
252 if ((status = do_mr_query("update_value", CountArgs(newval),
253 newval, Scream, NULL)) == MR_SUCCESS)
255 FreeAndClear(&def_quota, TRUE);
256 def_quota = Strsave(argv[1]);
259 com_err(program_name, status, " in update_value");
262 Put_message("Quota not changed.");
267 /* ------------------------ Filesystem Quotas -------------------- */
269 /* Function Name: GetQuota
270 * Description: Shows the quota of a filesystem w.r.t.
271 * a group, user, or anybody (AFS)
278 struct qelem *top = NULL;
282 if (!(args = GetQuotaArgs(FALSE)))
285 if ((status = do_mr_query("get_quota", CountArgs(args), args,
286 StoreInfo, (char *) &top)) != MR_SUCCESS)
287 com_err(program_name, status, " in get_quota");
293 Loop(top, (void *) PrintQuota);
299 /* Function Name: GetQuotaByFilesys
300 * Description: Shows all quotas associated with the
306 int GetQuotaByFilesys(void)
308 struct qelem *top = NULL;
310 char **args = malloc(2 * sizeof(char *));
314 Put_message("Could not allocate memory in GetQuotaByFilesys.");
318 args[0] = Strsave("");
320 if (GetValueFromUser("Filesystem", &args[0]) == SUB_ERROR)
323 if ((status = do_mr_query("get_quota_by_filesys", 1, args,
324 StoreInfo, (char *) &top)) != MR_SUCCESS)
325 com_err(program_name, status, " in get_quota_by_filesys");
331 Loop(top, (void *) PrintQuota);
337 /* Function Name: AddQuota
338 * Description: Adds a new quota record for a filesystem
339 * w.r.t. a user, group, or anybody (AFS).
348 if (!(args = GetQuotaArgs(TRUE)))
351 if ((status = do_mr_query("add_quota", CountArgs(args), args,
352 Scream, NULL)) != MR_SUCCESS)
353 com_err(program_name, status, " in add_quota");
360 /* Function Name: RealUpdateQuota
361 * Description: Performs the actual update of the quota
362 * Arguments: info - the information nesc. to update the quota.
366 static void RealUpdateQuota(char **info)
369 char temp_buf[BUFSIZ];
371 sprintf(temp_buf, "New quota for filesystem %s (in KB)", info[Q_FILESYS]);
372 if (GetValueFromUser(temp_buf, &info[Q_QUOTA]) == SUB_ERROR)
374 Put_message("Not changed.");
378 if (status = do_mr_query("update_quota", 4, info,
379 Scream, NULL) != MR_SUCCESS)
381 com_err(program_name, status, " in update_quota");
382 sprintf(temp_buf, "Could not perform quota change on %s",
384 Put_message(temp_buf);
388 /* Function Name: UpdateQuota
389 * Description: Updates an existing quota for a filesytem
390 * w.r.t. a user, group, or anybody.
395 int UpdateQuota(void)
399 struct qelem *top = NULL;
401 if (!(args = GetQuotaArgs(FALSE)))
404 if ((status = do_mr_query("get_quota", CountArgs(args), args,
405 StoreInfo, (char *) &top)) != MR_SUCCESS)
406 com_err(program_name, status, " in get_quota");
408 FreeInfo(args); /* done with args, free them. */
411 Loop(top, RealUpdateQuota);
418 /* Function Name: RealDeleteQuota
419 * Description: Actually removes the quota
420 * Arguments: info - all information about this quota.
421 * one_item - true if there is only one item in the queue, and
426 static void RealDeleteQuota(char **info, Bool one_item)
429 char temp_buf[BUFSIZ];
431 if (!strcmp(info[Q_TYPE], "ANY"))
433 sprintf(temp_buf, "Do you really want to delete the quota on %s",
438 sprintf(temp_buf, "Do you really want to delete the %s %s's quota on %s",
439 (strcmp(info[Q_TYPE], "USER") ? "group" : "user"), info[Q_NAME],
443 if (!one_item || Confirm(temp_buf))
445 if ((status = do_mr_query("delete_quota", 3, info,
446 Scream, NULL)) != MR_SUCCESS)
447 com_err(program_name, status, " in delete_quota");
449 Put_message("Quota sucessfully removed.");
452 Put_message("Aborted.");
455 /* Function Name: DeleteQuota
456 * Description: Removes the quota record for a filesystem
457 * w.r.t. a user, group, or anybody.
462 int DeleteQuota(void)
466 struct qelem *top = NULL;
468 if (!(args = GetQuotaArgs(FALSE)))
471 if ((status = do_mr_query("get_quota", 3, args,
472 StoreInfo, (char *) &top)))
473 com_err(program_name, status, " in get_quota");
478 QueryLoop(top, PrintQuota, RealDeleteQuota,
479 "Delete this quota on filesystem");