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
27 #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. */
38 /* Function Name: GetDefaultUserQuota
39 * Description: gets the user quota from sms, and caches the value.
40 * Arguments: override - if true, go to sms and override the cache.
42 * NOTE: Using a queue here is pretty useless, but StoreInfo expects
43 * one, and it works, so why fuck with it.
47 GetDefaultUserQuota(override)
52 struct qelem * top = NULL;
53 static char *val[] = {"def_quota", NULL};
55 if (override || (def_quota == NULL)) {
56 if ( (status = sms_query("get_value", CountArgs(val),
57 val, StoreInfo, (char *) &top)) != SMS_SUCCESS) {
58 com_err(program_name, status, " in ShowDefaultQuota");
59 if (def_quota == NULL) {
60 Put_message("No default Quota Found, setting default to 0.");
61 def_quota = Strsave("0");
64 Put_message("No default Quota Found, retaining old value.");
68 info = (char **) top->q_data;
69 FreeAndClear(&def_quota, TRUE);
70 def_quota = Strsave(info[0]);
77 /* Function Name: PrintDefaultQuota
78 * Description: Prints default quota info in a meaningful way.
79 * Arguments: value of the default quota.
84 PrintDefaultQuota(quota)
87 char temp_buf[BUFSIZ];
89 sprintf(temp_buf,"The default quota is %s Kb.", quota);
90 Put_message(temp_buf);
93 /* Function Name: PrintQuota
94 * Description: Prints a users quota information.
95 * Arguments: info - a pointer to the quota information:
105 sprintf(buf, "Filsystem: %-45s User: %s",info[Q_FILESYS], info[Q_LOGIN]);
107 sprintf(buf, "Machine: %-20s Directory: %-15s Quota: %s",
108 info[Q_MACHINE], info[Q_DIRECTORY], info[Q_QUOTA]);
110 return(info[Q_FILESYS]);
113 /* Function Name: GetQuotaArgs
114 * Description: gets quota args from the user
115 * Arguments: quota - if TRUE the get quota too.
116 * Returns: the arguments.
123 char ** args = (char **) malloc( MAX_ARGS_SIZE * sizeof(char *) );
126 Put_message("Could not allocate memory in GetQuotaArgs.");
130 args[Q_FILESYS] = Strsave(DEFAULT_FILESYS);
131 args[Q_LOGIN] = Strsave(DEFAULT_USER);
133 args[Q_QUOTA] = Strsave(GetDefaultUserQuota(FALSE));
134 args[3] = NULL; /* NULL terminate. */
137 args[2] = NULL; /* NULL terminate. */
139 /* Get filesystem. */
141 GetValueFromUser("Filesystem", &args[Q_FILESYS]);
142 if (quota) /* We need an exact entry. */
143 if (!ValidName(args[Q_FILESYS]))
146 /* Get and check username. */
148 GetValueFromUser("Username", &args[Q_LOGIN]);
149 if (!ValidName(args[Q_LOGIN])) return(NULL);
151 if (quota) { /* Get and check quota. */
152 GetValueFromUser("Quota", &args[Q_QUOTA]);
153 if (!ValidName(args[Q_QUOTA])) return(NULL);
158 /* ------------------------- Show Quota Info ------------------------- */
160 /* Function Name: ShowDefaultQuota
161 * Description: This prints out a default quota for the system.
163 * Returns: DM_NORMAL.
169 PrintDefaultQuota(GetDefaultUserQuota(TRUE));
173 /* Function Name: ChangeDefaultQuota
174 * Description: Changes the System Wide default quota.
175 * Arguments: argc, argv - New quota in argv[1].
176 * Returns: DM_NORMAL.
181 ChangeDefaultQuota(argc, argv)
186 char temp_buf[BUFSIZ];
187 static char *newval[] = {
188 "def_quota", NULL, NULL
191 if (!ValidName(argv[1]))
194 sprintf(temp_buf,"%s %s","Are you sure that you want to",
195 "change the default quota for all new users");
196 if(Confirm(temp_buf)) {
198 if ( (status = sms_query("update_value", CountArgs(newval),
199 newval, Scream, NULL)) == SMS_SUCCESS ) {
200 FreeAndClear(&def_quota, TRUE);
201 def_quota = Strsave(argv[1]);
204 com_err(program_name, status, " in update_value");
207 Put_message("Quota not changed.");
212 /* ------------------------- User Quotas ------------------------- */
214 /* Function Name: ShowUserQuota
215 * Description: Shows the quota of a user.
223 struct qelem *top = NULL;
227 if ( (args = GetQuotaArgs(FALSE) ) == NULL)
230 if ( (status = sms_query("get_nfs_quota", CountArgs(args), args,
231 StoreInfo, (char *) &top)) != SMS_SUCCESS)
232 com_err(program_name, status, " in get_nfs_quota");
234 FreeInfo(args); /* done with args free them. */
238 Loop(top, (void *) PrintQuota);
244 /* Function Name: AddUserQuota
245 * Description: Adds a new quota entry to the database.
246 * Arguments: argc, argv - name of the filesystem in argv[1].
256 if ( (args = GetQuotaArgs(TRUE) ) == NULL)
259 if ( (status = sms_query("add_nfs_quota", CountArgs(args), args,
260 Scream, (char *) NULL)) != SMS_SUCCESS)
261 com_err(program_name, status, " in get_nfs_quota");
268 /* Function Name: RealUpdateUser
269 * Description: Performs the actual update of the user information.
270 * Arguments: info - the information nesc. to update the user.
279 char temp_buf[BUFSIZ];
281 sprintf(temp_buf, "New quota for filesystem %s (in KB)", info[Q_FILESYS]);
282 GetValueFromUser(temp_buf, &info[Q_QUOTA]);
284 if (status = sms_query("update_nfs_quota", 3, info,
285 Scream, (char *) NULL) != SMS_SUCCESS) {
286 com_err(program_name, status, " in update_nfs_quota");
287 sprintf(temp_buf,"Could not perform quota change on %s",
289 Put_message(temp_buf);
293 /* Function Name: ChangeUserQuota
294 * Description: This function allows quotas to be updated.
296 * Returns: DM_NORMAL.
304 struct qelem *top = NULL;
306 if ( (args = GetQuotaArgs(FALSE) ) == NULL)
309 if ( (status = sms_query("get_nfs_quota", 2, args,
310 StoreInfo, (char *) &top)) != SMS_SUCCESS)
311 com_err(program_name, status, " in get_nfs_quota");
313 FreeInfo(args); /* done with args, free them. */
316 Loop(top, RealUpdateUser);
322 /* Function Name: RealRemoveUserQuota
323 * Description: Actually removes the user quota.
324 * Arguments: info - all information about this user quota.
325 * one_item - true if there is only one item in the queue, and
331 RealRemoveUserQuota(info, one_item)
336 char temp_buf[BUFSIZ];
339 "Do you really want to delete the user %s's quota on filesystem %s",
340 info[Q_LOGIN], info[Q_FILESYS]);
342 if (!one_item || Confirm(temp_buf)) {
343 if ( (status = sms_query("delete_nfs_quota", 2, info,
344 Scream, (char *) NULL)) != SMS_SUCCESS)
345 com_err(program_name, status, " in delete_nfs_quota");
347 Put_message("Quota sucessfully removed.");
350 Put_message("Aborted.");
353 /* Function Name: RemoveUserQuota
354 * Description: Removes a users quota on a given filsystem
356 * Returns: DM_NORMAL.
364 struct qelem *top = NULL;
366 if ( (args = GetQuotaArgs(FALSE) ) == NULL)
369 if ( (status = sms_query("get_nfs_quota", 2, args,
370 StoreInfo, (char *) &top)) != SMS_SUCCESS)
371 com_err(program_name, status, " in get_nfs_quota");
376 QueryLoop(top, PrintQuota, RealRemoveUserQuota,
377 "Delete This users quota on filesystem");