1 #if (!defined(lint) && !defined(SABER))
2 static char rcsid_module_c[] = "$Header$";
5 /* This is the file quota.c for the SMS Client, which allows a nieve
6 * user to quickly and easily maintain most parts of the SMS 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
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 sms. */
37 #define NOBODY "\\[nobody\\]"
40 /* Function Name: GetDefaultUserQuota
41 * Description: gets the user quota from sms, and caches the value.
42 * Arguments: override - if true, go to sms and override the cache.
44 * NOTE: Using a queue here is pretty useless, but StoreInfo expects
45 * one, and it works, so why fuck with it.
49 GetDefaultUserQuota(override)
54 struct qelem * top = NULL;
55 static char *val[] = {"def_quota", NULL};
57 if (override || (def_quota == NULL)) {
58 if ( (status = do_sms_query("get_value", CountArgs(val), val,
59 StoreInfo, (char *) &top)) != SMS_SUCCESS) {
60 com_err(program_name, status, " in ShowDefaultQuota");
61 if (def_quota == NULL) {
62 Put_message("No default Quota Found, setting default to 0.");
63 def_quota = Strsave("0");
66 Put_message("No default Quota Found, retaining old value.");
70 info = (char **) top->q_data;
71 FreeAndClear(&def_quota, TRUE);
72 def_quota = Strsave(info[0]);
79 /* Function Name: PrintDefaultQuota
80 * Description: Prints default quota info in a meaningful way.
81 * Arguments: value of the default quota.
86 PrintDefaultQuota(quota)
89 char temp_buf[BUFSIZ];
91 sprintf(temp_buf,"The default quota is %s Kb.", quota);
92 Put_message(temp_buf);
95 /* Function Name: PrintQuota
96 * Description: Prints a users quota information.
97 * Arguments: info - a pointer to the quota information:
105 char buf[BUFSIZ], *user;
107 user = info[Q_LOGIN];
108 if (!strcmp(user, "[nobody]"))
111 sprintf(buf, "Filsystem: %-45s User: %s",info[Q_FILESYS], user);
113 sprintf(buf, "Machine: %-20s Directory: %-15s",
114 info[Q_MACHINE], info[Q_DIRECTORY]);
116 sprintf(buf, "Quota: %s", info[Q_QUOTA]);
118 sprintf(buf, MOD_FORMAT, info[Q_MODBY], info[Q_MODTIME], info[Q_MODWITH]);
120 return(info[Q_FILESYS]);
124 afsfilsyshelper(argc, argv, hint)
129 *hint = !strcmp(argv[FS_TYPE], "AFS");
140 if (index(name, '*') || index(name, '?') || index(name, '\\'))
143 status = do_sms_query("get_filesys_by_label", 1, argv,
144 afsfilsyshelper, &ret);
145 if (status == SMS_SUCCESS)
152 /* Function Name: GetQuotaArgs
153 * Description: gets quota args from the user
154 * Arguments: quota - if TRUE the get quota too.
155 * Returns: the arguments.
162 char ** args = (char **) malloc( MAX_ARGS_SIZE * sizeof(char *) );
165 Put_message("Could not allocate memory in GetQuotaArgs.");
169 args[Q_FILESYS] = Strsave(DEFAULT_FILESYS);
170 args[Q_LOGIN] = Strsave(DEFAULT_USER);
172 args[Q_QUOTA] = Strsave(GetDefaultUserQuota(FALSE));
173 args[3] = NULL; /* NULL terminate. */
176 args[2] = NULL; /* NULL terminate. */
178 /* Get filesystem. */
180 GetValueFromUser("Filesystem", &args[Q_FILESYS]);
181 if (quota && !ValidName(args[Q_FILESYS]))
184 /* Get and check username. */
185 if (afsfilesys(args[Q_FILESYS])) {
186 args[Q_LOGIN] = strsave(NOBODY);
188 GetValueFromUser("Username", &args[Q_LOGIN]);
189 if (!ValidName(args[Q_LOGIN])) return(NULL);
192 if (quota) { /* Get and check quota. */
193 GetValueFromUser("Quota", &args[Q_QUOTA]);
194 if (!ValidName(args[Q_QUOTA])) return(NULL);
199 /* ------------------------- Show Quota Info ------------------------- */
201 /* Function Name: ShowDefaultQuota
202 * Description: This prints out a default quota for the system.
204 * Returns: DM_NORMAL.
210 PrintDefaultQuota(GetDefaultUserQuota(TRUE));
214 /* Function Name: ChangeDefaultQuota
215 * Description: Changes the System Wide default quota.
216 * Arguments: argc, argv - New quota in argv[1].
217 * Returns: DM_NORMAL.
222 ChangeDefaultQuota(argc, argv)
227 char temp_buf[BUFSIZ];
228 static char *newval[] = {
229 "def_quota", NULL, NULL
232 if (!ValidName(argv[1]))
235 sprintf(temp_buf,"%s %s","Are you sure that you want to",
236 "change the default quota for all new users");
237 if(Confirm(temp_buf)) {
239 if ( (status = do_sms_query("update_value", CountArgs(newval),
240 newval, Scream, NULL)) == SMS_SUCCESS ) {
241 FreeAndClear(&def_quota, TRUE);
242 def_quota = Strsave(argv[1]);
245 com_err(program_name, status, " in update_value");
248 Put_message("Quota not changed.");
253 /* ------------------------- User Quotas ------------------------- */
255 /* Function Name: ShowUserQuota
256 * Description: Shows the quota of a user.
264 struct qelem *top = NULL;
268 if ( (args = GetQuotaArgs(FALSE) ) == NULL)
271 if ( (status = do_sms_query("get_nfs_quota", CountArgs(args), args,
272 StoreInfo, (char *) &top)) != SMS_SUCCESS)
273 com_err(program_name, status, " in get_nfs_quota");
275 FreeInfo(args); /* done with args free them. */
279 Loop(top, (void *) PrintQuota);
285 /* Function Name: AddUserQuota
286 * Description: Adds a new quota entry to the database.
287 * Arguments: argc, argv - name of the filesystem in argv[1].
297 if ( (args = GetQuotaArgs(TRUE) ) == NULL)
300 if ( (status = do_sms_query("add_nfs_quota", CountArgs(args), args,
301 Scream, (char *) NULL)) != SMS_SUCCESS)
302 com_err(program_name, status, " in get_nfs_quota");
309 /* Function Name: RealUpdateUser
310 * Description: Performs the actual update of the user information.
311 * Arguments: info - the information nesc. to update the user.
320 char temp_buf[BUFSIZ];
322 sprintf(temp_buf, "New quota for filesystem %s (in KB)", info[Q_FILESYS]);
323 GetValueFromUser(temp_buf, &info[Q_QUOTA]);
325 if (status = do_sms_query("update_nfs_quota", 3, info,
326 Scream, (char *) NULL) != SMS_SUCCESS) {
327 com_err(program_name, status, " in update_nfs_quota");
328 sprintf(temp_buf,"Could not perform quota change on %s",
330 Put_message(temp_buf);
334 /* Function Name: ChangeUserQuota
335 * Description: This function allows quotas to be updated.
337 * Returns: DM_NORMAL.
345 struct qelem *top = NULL;
347 if ( (args = GetQuotaArgs(FALSE) ) == NULL)
350 if ( (status = do_sms_query("get_nfs_quota", 2, args,
351 StoreInfo, (char *) &top)) != SMS_SUCCESS)
352 com_err(program_name, status, " in get_nfs_quota");
354 FreeInfo(args); /* done with args, free them. */
357 Loop(top, RealUpdateUser);
363 /* Function Name: RealRemoveUserQuota
364 * Description: Actually removes the user quota.
365 * Arguments: info - all information about this user quota.
366 * one_item - true if there is only one item in the queue, and
372 RealRemoveUserQuota(info, one_item)
377 char temp_buf[BUFSIZ];
380 "Do you really want to delete the user %s's quota on filesystem %s",
381 info[Q_LOGIN], info[Q_FILESYS]);
383 if (!one_item || Confirm(temp_buf)) {
384 if ( (status = do_sms_query("delete_nfs_quota", 2, info,
385 Scream, (char *) NULL)) != SMS_SUCCESS)
386 com_err(program_name, status, " in delete_nfs_quota");
388 Put_message("Quota sucessfully removed.");
391 Put_message("Aborted.");
394 /* Function Name: RemoveUserQuota
395 * Description: Removes a users quota on a given filsystem
397 * Returns: DM_NORMAL.
405 struct qelem *top = NULL;
407 if ( (args = GetQuotaArgs(FALSE) ) == NULL)
410 if ( (status = do_sms_query("get_nfs_quota", 2, args,
411 StoreInfo, (char *) &top)) != SMS_SUCCESS)
412 com_err(program_name, status, " in get_nfs_quota");
417 QueryLoop(top, PrintQuota, RealRemoveUserQuota,
418 "Delete This users quota on filesystem");