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 return(info[Q_FILESYS]);
122 afsfilsyshelper(argc, argv, hint)
127 *hint = !strcmp(argv[FS_TYPE], "AFS");
139 status = do_sms_query("get_filesys_by_label", 1, argv,
140 afsfilsyshelper, &ret);
141 if (status == SMS_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.
158 char ** args = (char **) 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_LOGIN] = Strsave(DEFAULT_USER);
168 args[Q_QUOTA] = Strsave(GetDefaultUserQuota(FALSE));
169 args[3] = NULL; /* NULL terminate. */
172 args[2] = NULL; /* NULL terminate. */
174 /* Get filesystem. */
176 GetValueFromUser("Filesystem", &args[Q_FILESYS]);
177 if (quota && !ValidName(args[Q_FILESYS]))
180 /* Get and check username. */
181 if (afsfilesys(args[Q_FILESYS])) {
182 args[Q_LOGIN] = strsave(NOBODY);
184 GetValueFromUser("Username", &args[Q_LOGIN]);
185 if (!ValidName(args[Q_LOGIN])) return(NULL);
188 if (quota) { /* Get and check quota. */
189 GetValueFromUser("Quota", &args[Q_QUOTA]);
190 if (!ValidName(args[Q_QUOTA])) return(NULL);
195 /* ------------------------- Show Quota Info ------------------------- */
197 /* Function Name: ShowDefaultQuota
198 * Description: This prints out a default quota for the system.
200 * Returns: DM_NORMAL.
206 PrintDefaultQuota(GetDefaultUserQuota(TRUE));
210 /* Function Name: ChangeDefaultQuota
211 * Description: Changes the System Wide default quota.
212 * Arguments: argc, argv - New quota in argv[1].
213 * Returns: DM_NORMAL.
218 ChangeDefaultQuota(argc, argv)
223 char temp_buf[BUFSIZ];
224 static char *newval[] = {
225 "def_quota", NULL, NULL
228 if (!ValidName(argv[1]))
231 sprintf(temp_buf,"%s %s","Are you sure that you want to",
232 "change the default quota for all new users");
233 if(Confirm(temp_buf)) {
235 if ( (status = do_sms_query("update_value", CountArgs(newval),
236 newval, Scream, NULL)) == SMS_SUCCESS ) {
237 FreeAndClear(&def_quota, TRUE);
238 def_quota = Strsave(argv[1]);
241 com_err(program_name, status, " in update_value");
244 Put_message("Quota not changed.");
249 /* ------------------------- User Quotas ------------------------- */
251 /* Function Name: ShowUserQuota
252 * Description: Shows the quota of a user.
260 struct qelem *top = NULL;
264 if ( (args = GetQuotaArgs(FALSE) ) == NULL)
267 if ( (status = do_sms_query("get_nfs_quota", CountArgs(args), args,
268 StoreInfo, (char *) &top)) != SMS_SUCCESS)
269 com_err(program_name, status, " in get_nfs_quota");
271 FreeInfo(args); /* done with args free them. */
275 Loop(top, (void *) PrintQuota);
281 /* Function Name: AddUserQuota
282 * Description: Adds a new quota entry to the database.
283 * Arguments: argc, argv - name of the filesystem in argv[1].
293 if ( (args = GetQuotaArgs(TRUE) ) == NULL)
296 if ( (status = do_sms_query("add_nfs_quota", CountArgs(args), args,
297 Scream, (char *) NULL)) != SMS_SUCCESS)
298 com_err(program_name, status, " in get_nfs_quota");
305 /* Function Name: RealUpdateUser
306 * Description: Performs the actual update of the user information.
307 * Arguments: info - the information nesc. to update the user.
316 char temp_buf[BUFSIZ];
318 sprintf(temp_buf, "New quota for filesystem %s (in KB)", info[Q_FILESYS]);
319 GetValueFromUser(temp_buf, &info[Q_QUOTA]);
321 if (status = do_sms_query("update_nfs_quota", 3, info,
322 Scream, (char *) NULL) != SMS_SUCCESS) {
323 com_err(program_name, status, " in update_nfs_quota");
324 sprintf(temp_buf,"Could not perform quota change on %s",
326 Put_message(temp_buf);
330 /* Function Name: ChangeUserQuota
331 * Description: This function allows quotas to be updated.
333 * Returns: DM_NORMAL.
341 struct qelem *top = NULL;
343 if ( (args = GetQuotaArgs(FALSE) ) == NULL)
346 if ( (status = do_sms_query("get_nfs_quota", 2, args,
347 StoreInfo, (char *) &top)) != SMS_SUCCESS)
348 com_err(program_name, status, " in get_nfs_quota");
350 FreeInfo(args); /* done with args, free them. */
353 Loop(top, RealUpdateUser);
359 /* Function Name: RealRemoveUserQuota
360 * Description: Actually removes the user quota.
361 * Arguments: info - all information about this user quota.
362 * one_item - true if there is only one item in the queue, and
368 RealRemoveUserQuota(info, one_item)
373 char temp_buf[BUFSIZ];
376 "Do you really want to delete the user %s's quota on filesystem %s",
377 info[Q_LOGIN], info[Q_FILESYS]);
379 if (!one_item || Confirm(temp_buf)) {
380 if ( (status = do_sms_query("delete_nfs_quota", 2, info,
381 Scream, (char *) NULL)) != SMS_SUCCESS)
382 com_err(program_name, status, " in delete_nfs_quota");
384 Put_message("Quota sucessfully removed.");
387 Put_message("Aborted.");
390 /* Function Name: RemoveUserQuota
391 * Description: Removes a users quota on a given filsystem
393 * Returns: DM_NORMAL.
401 struct qelem *top = NULL;
403 if ( (args = GetQuotaArgs(FALSE) ) == NULL)
406 if ( (status = do_sms_query("get_nfs_quota", 2, args,
407 StoreInfo, (char *) &top)) != SMS_SUCCESS)
408 com_err(program_name, status, " in get_nfs_quota");
413 QueryLoop(top, PrintQuota, RealRemoveUserQuota,
414 "Delete This users quota on filesystem");