]> andersk Git - moira.git/blob - clients/moira/quota.c
8319fa446c9b1bebc034527ec9786b025f6bd443
[moira.git] / clients / moira / quota.c
1 #if (!defined(lint) && !defined(SABER))
2   static char rcsid_module_c[] = "$Header$";
3 #endif lint
4
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.
8  *      
9  *      Created:        7/10/88
10  *      By:             Chris D. Peterson
11  *
12  *      $Source$
13  *      $Author$
14  *      $Header$
15  *      
16  *      Copyright 1988 by the Massachusetts Institute of Technology.
17  *
18  *      For further information on copyright and distribution 
19  *      see the file mit-copyright.h
20  */
21
22 #include <stdio.h>
23 #include <strings.h>
24 #include <sms.h>
25 #include <sms_app.h>
26 #include <menu.h>
27
28 #include "mit-copyright.h"
29 #include "defs.h"
30 #include "f_defs.h"
31 #include "globals.h"
32
33 static char * def_quota = NULL; 
34   
35 #define DEFAULT_FILESYS DEFAULT_NONE
36 #define DEFAULT_USER user       /* this is the user who started sms. */
37 #define NOBODY  "\\[nobody\\]"
38
39
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.
43  *      Returns: none.
44  *      NOTE: Using a queue here is pretty useless, but StoreInfo expects
45  *            one, and it works, so why fuck with it.
46  */
47
48 static char *
49 GetDefaultUserQuota(override)
50 Bool override;
51 {
52   register int status;
53   char ** info;
54   struct qelem * top = NULL;
55   static char *val[] = {"def_quota", NULL};
56
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");
64         }
65         else
66           Put_message("No default Quota Found, retaining old value.");
67       }
68     else {
69       top = QueueTop(top);
70       info = (char **) top->q_data;
71       FreeAndClear(&def_quota, TRUE);
72       def_quota = Strsave(info[0]);
73       FreeQueue(top);
74     }
75   }
76   return(def_quota);
77 }
78     
79 /*      Function Name: PrintDefaultQuota
80  *      Description: Prints default quota info in a meaningful way.
81  *      Arguments: value of the default quota.
82  *      Returns: none.
83  */
84
85 static void
86 PrintDefaultQuota(quota)
87 char * quota;
88 {
89     char temp_buf[BUFSIZ];
90     Put_message("");
91     sprintf(temp_buf,"The default quota is %s Kb.", quota);
92     Put_message(temp_buf);
93 }
94
95 /*      Function Name: PrintQuota
96  *      Description: Prints a users quota information.
97  *      Arguments: info - a pointer to the quota information:
98  *      Returns: none.
99  */
100
101 static char *
102 PrintQuota(info)
103 char ** info;
104 {
105     char buf[BUFSIZ], *user;
106
107     user = info[Q_LOGIN];
108     if (!strcmp(user, "[nobody]"))
109       user = "[anybody]";
110     Put_message("");
111     sprintf(buf, "Filsystem: %-45s User: %s",info[Q_FILESYS], user);
112     Put_message(buf);
113     sprintf(buf, "Machine: %-20s Directory: %-15s",
114                    info[Q_MACHINE], info[Q_DIRECTORY]);
115     Put_message(buf);
116     sprintf(buf, "Quota: %s", info[Q_QUOTA]);
117     Put_message(buf);
118     sprintf(buf, MOD_FORMAT, info[Q_MODBY], info[Q_MODTIME], info[Q_MODWITH]);
119     Put_message(buf);
120     return(info[Q_FILESYS]);
121 }
122
123
124 afsfilsyshelper(argc, argv, hint)
125 int argc;
126 char **argv;
127 int *hint;
128 {
129     *hint = !strcmp(argv[FS_TYPE], "AFS");
130     return(0);
131 }
132
133
134 int afsfilesys(name)
135 char *name;
136 {
137     int status, ret = 0;
138     char *argv[1];
139
140     if (index(name, '*') || index(name, '?') || index(name, '\\'))
141       return(0);
142     argv[0] = name;
143     status = do_sms_query("get_filesys_by_label", 1, argv,
144                           afsfilsyshelper, &ret);
145     if (status == SMS_SUCCESS)
146       return(ret);
147     return(0);
148     
149 }
150
151
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.
156  */
157
158 static char **
159 GetQuotaArgs(quota)
160 Bool quota;
161 {
162   char ** args = (char **) malloc( MAX_ARGS_SIZE * sizeof(char *) );
163   
164   if (args == NULL) {
165     Put_message("Could not allocate memory in GetQuotaArgs.");
166     return(NULL);
167   }
168
169   args[Q_FILESYS] = Strsave(DEFAULT_FILESYS);
170   args[Q_LOGIN] = Strsave(DEFAULT_USER);
171   if (quota) {
172     args[Q_QUOTA] = Strsave(GetDefaultUserQuota(FALSE));
173     args[3] = NULL;             /* NULL terminate. */
174   }
175   else 
176     args[2] = NULL;             /* NULL terminate. */
177
178   /* Get filesystem. */
179
180   GetValueFromUser("Filesystem", &args[Q_FILESYS]);
181   if (quota && !ValidName(args[Q_FILESYS]))
182     return(NULL);
183
184   /* Get and check username. */
185   if (afsfilesys(args[Q_FILESYS])) {
186       args[Q_LOGIN] = strsave(NOBODY);
187   } else {
188       GetValueFromUser("Username", &args[Q_LOGIN]);
189       if (!ValidName(args[Q_LOGIN])) return(NULL);
190   }
191
192   if (quota) {                  /* Get and check quota. */
193     GetValueFromUser("Quota", &args[Q_QUOTA]);
194     if (!ValidName(args[Q_QUOTA])) return(NULL);
195   }
196   return(args);
197 }  
198
199 /* ------------------------- Show Quota Info ------------------------- */
200
201 /*      Function Name: ShowDefaultQuota
202  *      Description: This prints out a default quota for the system.
203  *      Arguments: none
204  *      Returns: DM_NORMAL.
205  */
206
207 int
208 ShowDefaultQuota()
209 {
210   PrintDefaultQuota(GetDefaultUserQuota(TRUE));
211   return (DM_NORMAL);
212 }
213
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.
218  */
219
220 /*ARGSUSED*/
221 int
222 ChangeDefaultQuota(argc, argv)
223 int argc;
224 char *argv[];
225 {
226     register int status;
227     char temp_buf[BUFSIZ];
228     static char *newval[] = {
229         "def_quota", NULL, NULL
230     };
231
232     if (!ValidName(argv[1]))
233         return(DM_NORMAL);
234
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)) {
238         newval[1] = argv[1];
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]);
243         }
244         else
245             com_err(program_name, status, " in update_value");
246     }
247     else
248         Put_message("Quota not changed.");
249
250     return (DM_NORMAL);
251 }
252
253 /* ------------------------- User Quotas ------------------------- */
254
255 /*      Function Name: ShowUserQuota
256  *      Description: Shows the quota of a user.
257  *      Arguments: none
258  *      Returns: DM_NORMAL
259  */
260
261 int
262 ShowUserQuota()
263 {
264   struct qelem *top = NULL;
265   register int status;
266   char ** args;
267
268   if ( (args = GetQuotaArgs(FALSE) ) == NULL)
269     return(DM_NORMAL);
270
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");
274   
275   FreeInfo(args);               /* done with args free them. */
276   free(args);
277
278   top = QueueTop(top);
279   Loop(top, (void *) PrintQuota);
280   
281   FreeQueue(top);
282   return (DM_NORMAL);
283 }
284
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].
288  *      Returns: DM_NORMAL
289  */
290
291 int
292 AddUserQuota()
293 {
294   char ** args;
295   register int status;
296   
297   if ( (args = GetQuotaArgs(TRUE) ) == NULL)
298     return(DM_NORMAL);
299
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");
303   
304   FreeInfo(args);
305   free(args);
306   return(DM_NORMAL);
307 }
308
309 /*      Function Name: RealUpdateUser
310  *      Description: Performs the actual update of the user information.
311  *      Arguments: info - the information nesc. to update the user.
312  *      Returns: none.
313  */
314
315 static void
316 RealUpdateUser(info)
317 char ** info;
318 {
319   register int status;
320   char temp_buf[BUFSIZ];
321
322   sprintf(temp_buf, "New quota for filesystem %s (in KB)", info[Q_FILESYS]);
323   GetValueFromUser(temp_buf, &info[Q_QUOTA]);
324   
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",
329             info[Q_FILESYS]); 
330     Put_message(temp_buf);
331   }
332 }
333   
334 /*      Function Name: ChangeUserQuota
335  *      Description: This function allows quotas to be updated.
336  *      Arguments: none.
337  *      Returns: DM_NORMAL.
338  */
339
340 int
341 ChangeUserQuota()
342 {
343   int status;
344   char **args;
345   struct qelem *top = NULL;
346   
347   if ( (args = GetQuotaArgs(FALSE) ) == NULL)
348     return(DM_NORMAL);
349
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");
353   
354   FreeInfo(args);               /* done with args, free them. */
355   free(args);
356   top = QueueTop(top);
357   Loop(top, RealUpdateUser);
358
359   FreeQueue(top);
360   return (DM_NORMAL);
361 }
362   
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
367  *                            we should confirm.
368  *      Returns: none.
369  */
370
371 static void
372 RealRemoveUserQuota(info, one_item)
373 char ** info;
374 Bool one_item;
375 {
376   register int status;
377   char temp_buf[BUFSIZ];
378
379   sprintf(temp_buf,
380           "Do you really want to delete the user %s's quota on filesystem %s",
381           info[Q_LOGIN], info[Q_FILESYS]);
382
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");
387     else
388       Put_message("Quota sucessfully removed.");
389   }
390   else
391     Put_message("Aborted.");
392 }
393
394 /*      Function Name: RemoveUserQuota
395  *      Description: Removes a users quota on a given filsystem
396  *      Arguments: none.
397  *      Returns: DM_NORMAL.
398  */
399
400 int
401 RemoveUserQuota()
402 {
403   register int status;
404   char **args;
405   struct qelem *top = NULL;
406
407   if ( (args = GetQuotaArgs(FALSE) ) == NULL)
408     return(DM_NORMAL);
409
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");
413
414   FreeInfo(args);
415   free(args);
416   top = QueueTop(top);
417   QueryLoop(top, PrintQuota, RealRemoveUserQuota,
418             "Delete This users quota on filesystem");
419
420   FreeQueue(top);
421   return(DM_NORMAL);
422 }
This page took 0.055585 seconds and 3 git commands to generate.