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\\]"
38 #define OLDNOBODY "[nobody]"
41 /* Function Name: GetDefaultUserQuota
42 * Description: gets the user quota from sms, and caches the value.
43 * Arguments: override - if true, go to sms 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_sms_query("get_value", CountArgs(val), val,
60 StoreInfo, (char *) &top)) != SMS_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:
106 char buf[BUFSIZ], *user;
108 user = info[Q_LOGIN];
109 if (!strcmp(user, "[nobody]"))
112 sprintf(buf, "Filsystem: %-45s User: %s",info[Q_FILESYS], user);
114 sprintf(buf, "Machine: %-20s Directory: %-15s",
115 info[Q_MACHINE], info[Q_DIRECTORY]);
117 sprintf(buf, "Quota: %s", info[Q_QUOTA]);
119 sprintf(buf, MOD_FORMAT, info[Q_MODBY], info[Q_MODTIME], info[Q_MODWITH]);
121 return(info[Q_FILESYS]);
125 afsfilsyshelper(argc, argv, hint)
130 *hint = !strcmp(argv[FS_TYPE], "AFS");
141 if (index(name, '*') || index(name, '?') || index(name, '\\'))
144 status = do_sms_query("get_filesys_by_label", 1, argv,
145 afsfilsyshelper, &ret);
146 if (status == SMS_SUCCESS)
153 /* Function Name: GetQuotaArgs
154 * Description: gets quota args from the user
155 * Arguments: quota - if TRUE the get quota too.
156 * Returns: the arguments.
163 char ** args = (char **) malloc( MAX_ARGS_SIZE * sizeof(char *) );
166 Put_message("Could not allocate memory in GetQuotaArgs.");
170 args[Q_FILESYS] = Strsave(DEFAULT_FILESYS);
171 args[Q_LOGIN] = Strsave(DEFAULT_USER);
173 args[Q_QUOTA] = Strsave(GetDefaultUserQuota(FALSE));
174 args[3] = NULL; /* NULL terminate. */
177 args[2] = NULL; /* NULL terminate. */
179 /* Get filesystem. */
181 GetValueFromUser("Filesystem", &args[Q_FILESYS]);
182 if (quota && !ValidName(args[Q_FILESYS]))
185 /* Get and check username. */
186 if (afsfilesys(args[Q_FILESYS])) {
187 args[Q_LOGIN] = strsave(NOBODY);
189 GetValueFromUser("Username", &args[Q_LOGIN]);
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]);
324 if (!strcmp(info[Q_LOGIN], OLDNOBODY)) {
326 info[Q_LOGIN] = strsave(NOBODY);
329 if (status = do_sms_query("update_nfs_quota", 3, info,
330 Scream, (char *) NULL) != SMS_SUCCESS) {
331 com_err(program_name, status, " in update_nfs_quota");
332 sprintf(temp_buf,"Could not perform quota change on %s",
334 Put_message(temp_buf);
338 /* Function Name: ChangeUserQuota
339 * Description: This function allows quotas to be updated.
341 * Returns: DM_NORMAL.
349 struct qelem *top = NULL;
351 if ( (args = GetQuotaArgs(FALSE) ) == NULL)
354 if ( (status = do_sms_query("get_nfs_quota", 2, args,
355 StoreInfo, (char *) &top)) != SMS_SUCCESS)
356 com_err(program_name, status, " in get_nfs_quota");
358 FreeInfo(args); /* done with args, free them. */
361 Loop(top, RealUpdateUser);
367 /* Function Name: RealRemoveUserQuota
368 * Description: Actually removes the user quota.
369 * Arguments: info - all information about this user quota.
370 * one_item - true if there is only one item in the queue, and
376 RealRemoveUserQuota(info, one_item)
381 char temp_buf[BUFSIZ];
384 "Do you really want to delete the user %s's quota on filesystem %s",
385 info[Q_LOGIN], info[Q_FILESYS]);
387 if (!one_item || Confirm(temp_buf)) {
388 if ( (status = do_sms_query("delete_nfs_quota", 2, info,
389 Scream, (char *) NULL)) != SMS_SUCCESS)
390 com_err(program_name, status, " in delete_nfs_quota");
392 Put_message("Quota sucessfully removed.");
395 Put_message("Aborted.");
398 /* Function Name: RemoveUserQuota
399 * Description: Removes a users quota on a given filsystem
401 * Returns: DM_NORMAL.
409 struct qelem *top = NULL;
411 if ( (args = GetQuotaArgs(FALSE) ) == NULL)
414 if ( (status = do_sms_query("get_nfs_quota", 2, args,
415 StoreInfo, (char *) &top)) != SMS_SUCCESS)
416 com_err(program_name, status, " in get_nfs_quota");
421 QueryLoop(top, PrintQuota, RealRemoveUserQuota,
422 "Delete This users quota on filesystem");