]> andersk Git - moira.git/blob - clients/moira/quota.c
Diane Delgado's changes for a fixed table-locking order
[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 MOIRA Client, which allows a nieve
6  *      user to quickly and easily maintain most parts of the MOIRA 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 <string.h>
24 #include <moira.h>
25 #include <moira_site.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 moira. */
37 #define NOBODY  "\\[nobody\\]"
38 #define DEFAULT_QTYPE   "USER"
39
40
41 /*      Function Name: GetDefaultUserQuota
42  *      Description: gets the user quota from moira, and caches the value.
43  *      Arguments: override - if true, go to moira and override the cache.
44  *      Returns: none.
45  *      NOTE: Using a queue here is pretty useless, but StoreInfo expects
46  *            one, and it works, so why fuck with it.
47  */
48
49 static char *
50 GetDefaultUserQuota(override)
51 Bool override;
52 {
53   register int status;
54   char ** info;
55   struct qelem * top = NULL;
56   static char *val[] = {"def_quota", NULL};
57
58   if (override || (def_quota == NULL)) {
59     if ( (status = do_mr_query("get_value", CountArgs(val), val,
60                                 StoreInfo, (char *) &top)) != MR_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");
65         }
66         else
67           Put_message("No default Quota Found, retaining old value.");
68       }
69     else {
70       top = QueueTop(top);
71       info = (char **) top->q_data;
72       FreeAndClear(&def_quota, TRUE);
73       def_quota = Strsave(info[0]);
74       FreeQueue(top);
75     }
76   }
77   return(def_quota);
78 }
79     
80 /*      Function Name: PrintDefaultQuota
81  *      Description: Prints default quota info in a meaningful way.
82  *      Arguments: value of the default quota.
83  *      Returns: none.
84  */
85
86 static void
87 PrintDefaultQuota(quota)
88 char * quota;
89 {
90     char temp_buf[BUFSIZ];
91     Put_message("");
92     sprintf(temp_buf,"The default quota is %s Kb.", quota);
93     Put_message(temp_buf);
94 }
95
96 /*      Function Name: PrintQuota
97  *      Description: Prints a users quota information.
98  *      Arguments: info - a pointer to the quota information:
99  *      Returns: none.
100  */
101
102 static char *
103 PrintQuota(info)
104 char ** info;
105 {
106     char buf[BUFSIZ];
107
108     Put_message("");
109
110     if (!strcmp(info[Q_TYPE], "ANY"))
111       sprintf(buf, "Filesystem: %s", info[Q_FILESYS]);
112     else
113       sprintf(buf, "Filesystem: %-45s %s %s", info[Q_FILESYS],
114               info[Q_TYPE], info[Q_NAME]);
115     Put_message(buf);
116     sprintf(buf, "Machine: %-20s Directory: %-15s",
117                    info[Q_MACHINE], info[Q_DIRECTORY]);
118     Put_message(buf);
119     sprintf(buf, "Quota: %s", info[Q_QUOTA]);
120     Put_message(buf);
121     sprintf(buf, MOD_FORMAT, info[Q_MODBY], info[Q_MODTIME], info[Q_MODWITH]);
122     Put_message(buf);
123     return(info[Q_FILESYS]);
124 }
125
126
127 afsfilsyshelper(argc, argv, hint)
128 int argc;
129 char **argv;
130 int *hint;
131 {
132     *hint = !strcmp(argv[FS_TYPE], "AFS");
133     return(0);
134 }
135
136
137 int afsfilesys(name)
138 char *name;
139 {
140     int status, ret = 0;
141     char *argv[1];
142
143     if (strchr(name, '*') || strchr(name, '?') || strchr(name, '\\'))
144       return(0);
145     argv[0] = name;
146     status = do_mr_query("get_filesys_by_label", 1, argv,
147                           afsfilsyshelper, &ret);
148     if (status == MR_SUCCESS)
149       return(ret);
150     return(status);
151     
152 }
153
154
155 /*      Function Name: GetQuotaArgs
156  *      Description: gets quota args from the user
157  *      Arguments: quota - if TRUE the get quota too.
158  *      Returns: the arguments.
159  */
160
161 static char **
162 GetQuotaArgs(quota)
163 Bool quota;
164 {
165   char ** args = (char **) malloc( MAX_ARGS_SIZE * sizeof(char *) );
166   int af;
167   
168   if (args == NULL) {
169     Put_message("Could not allocate memory in GetQuotaArgs.");
170     return(NULL);
171   }
172
173   args[Q_FILESYS] = Strsave(DEFAULT_FILESYS);
174   args[Q_TYPE] = Strsave(DEFAULT_QTYPE);
175   args[Q_NAME] = Strsave(DEFAULT_USER);
176   if (quota) {
177     args[Q_QUOTA] = Strsave(GetDefaultUserQuota(FALSE));
178     args[Q_QUOTA+1] = NULL;     /* NULL terminate. */
179   }
180   else 
181     args[Q_NAME+1] = NULL;      /* NULL terminate. */
182
183   /* Get filesystem. */
184
185   if (GetValueFromUser("Filesystem", &args[Q_FILESYS]) == SUB_ERROR)
186     return(NULL);
187   if (quota && !ValidName(args[Q_FILESYS]))
188     return(NULL);
189  
190   af = afsfilesys(args[Q_FILESYS]);
191   if (af != 0 && af != (1 == 1)) {
192       if (af == MR_NO_MATCH)
193         Put_message("That filesystem does not exist.");
194       else
195         com_err(program_name, af, " in afsfilesys");
196       return(NULL);
197   }
198   if (af) {
199     args[Q_TYPE] = strsave("ANY");
200     args[Q_NAME] = strsave(NOBODY);
201   }
202   else {
203       if (GetTypeFromUser("Quota type", "quota_type", &args[Q_TYPE]) ==
204           SUB_ERROR)
205         return(NULL);
206       if (GetValueFromUser("Name", &args[Q_NAME]) == SUB_ERROR)
207         return(NULL);
208       if (!ValidName(args[Q_NAME])) return(NULL);
209   }
210
211   if (quota) {                  /* Get and check quota. */
212       if (GetValueFromUser("Quota", &args[Q_QUOTA]) == SUB_ERROR)
213         return(NULL);
214       if (!ValidName(args[Q_QUOTA])) return(NULL);
215   }
216   return(args);
217 }  
218
219 /*      Function Name: ShowDefaultQuota
220  *      Description: This prints out a default quota for the system.
221  *      Arguments: none
222  *      Returns: DM_NORMAL.
223  */
224
225 int
226 ShowDefaultQuota()
227 {
228   PrintDefaultQuota(GetDefaultUserQuota(TRUE));
229   return (DM_NORMAL);
230 }
231
232 /*      Function Name: ChangeDefaultQuota
233  *      Description: Changes the System Wide default quota.
234  *      Arguments: argc, argv - New quota in argv[1].
235  *      Returns: DM_NORMAL.
236  */
237
238 /*ARGSUSED*/
239 int
240 ChangeDefaultQuota(argc, argv)
241 int argc;
242 char *argv[];
243 {
244     register int status;
245     char temp_buf[BUFSIZ];
246     static char *newval[] = {
247         "def_quota", NULL, NULL
248     };
249
250     if (!ValidName(argv[1]))
251         return(DM_NORMAL);
252
253     sprintf(temp_buf,"%s %s","Are you sure that you want to",
254             "change the default quota for all new users");
255     if(Confirm(temp_buf)) {
256         newval[1] = argv[1];
257         if ( (status = do_mr_query("update_value", CountArgs(newval), 
258                                     newval, Scream, NULL)) == MR_SUCCESS ) {
259           FreeAndClear(&def_quota, TRUE);
260           def_quota = Strsave(argv[1]);
261         }
262         else
263             com_err(program_name, status, " in update_value");
264     }
265     else
266         Put_message("Quota not changed.");
267
268     return (DM_NORMAL);
269 }
270
271 /* ------------------------ Filesystem Quotas -------------------- */
272
273 /*      Function Name: GetQuota
274  *      Description: Shows the quota of a filesystem w.r.t.
275  *                   a group, user, or anybody (AFS)
276  *      Arguments: none
277  *      Returns: DM_NORMAL
278  */
279
280 int
281 GetQuota()
282 {
283   struct qelem *top = NULL;
284   register int status;
285   char ** args;
286
287   if ( (args = GetQuotaArgs(FALSE) ) == NULL)
288     return(DM_NORMAL);
289
290   if (( status = do_mr_query("get_quota", CountArgs(args), args,
291                              StoreInfo, (char *) &top)) != MR_SUCCESS)
292     com_err(program_name, status, " in get_quota");
293
294   FreeInfo(args);
295   free(args);
296
297   top = QueueTop(top);
298   Loop(top, (void *) PrintQuota);
299
300   FreeQueue(top);
301   return(DM_NORMAL);
302 }
303
304 /*        Function Name: GetQuotaByFilesys
305  *        Description: Shows all quotas associated with the
306  *                     given filesystem
307  *        Arguments: none
308  *        Returns: DM_NORMAL
309  */
310
311 int
312 GetQuotaByFilesys()
313 {
314   struct qelem *top = NULL;
315   register int status;
316   char **args = (char**)malloc(2*sizeof(char*));
317
318   if (args == NULL) {
319     Put_message("Could not allocate memory in GetQuotaByFilesys.");
320     return(DM_NORMAL);
321   }
322
323   args[0] = Strsave("");
324   args[1] = NULL;
325   if (GetValueFromUser("Filesystem", &args[0]) == SUB_ERROR)
326     return(DM_NORMAL);
327
328   if (( status = do_mr_query("get_quota_by_filesys", 1, args,
329                              StoreInfo, (char *) &top)) != MR_SUCCESS)
330     com_err(program_name, status, " in get_quota_by_filesys");
331
332   FreeInfo(args);
333   free(args);
334
335   top = QueueTop(top);
336   Loop(top, (void *) PrintQuota);
337
338   FreeQueue(top);
339   return(DM_NORMAL);
340 }
341
342 /*        Function Name: AddQuota
343  *        Description: Adds a new quota record for a filesystem
344  *                     w.r.t. a user, group, or anybody (AFS).
345  *        Arguments: None
346  *        Returns: DM_NORMAL
347  */
348 int
349 AddQuota()
350 {
351   char ** args;
352   register int status;
353   
354   if ( (args = GetQuotaArgs(TRUE) ) == NULL)
355     return(DM_NORMAL);
356
357   if ( (status = do_mr_query("add_quota", CountArgs(args), args,
358                               Scream, (char *) NULL)) != MR_SUCCESS)
359     com_err(program_name, status, " in add_quota");
360   
361   FreeInfo(args);
362   free(args);
363   return(DM_NORMAL);
364 }
365
366 /*      Function Name: RealUpdateQuota
367  *      Description: Performs the actual update of the quota
368  *      Arguments: info - the information nesc. to update the quota.
369  *      Returns: none.
370  */
371
372 static void
373 RealUpdateQuota(info)
374 char ** info;
375 {
376   register int status;
377   char temp_buf[BUFSIZ];
378
379   sprintf(temp_buf, "New quota for filesystem %s (in KB)", info[Q_FILESYS]);
380   if (GetValueFromUser(temp_buf, &info[Q_QUOTA]) == SUB_ERROR) {
381       Put_message("Not changed.");
382       return;
383   }
384
385   if (status = do_mr_query("update_quota", 4, info,
386                             Scream, (char *) NULL) != MR_SUCCESS) {
387     com_err(program_name, status, " in update_quota");
388     sprintf(temp_buf,"Could not perform quota change on %s",
389             info[Q_FILESYS]); 
390     Put_message(temp_buf);
391   }
392 }
393
394 /*        Function Name: UpdateQuota
395  *        Description: Updates an existing quota for a filesytem
396  *                     w.r.t. a user, group, or anybody.
397  *        Arguments: None
398  *        Returns: DM_NORMAL
399  */
400
401 int
402 UpdateQuota()
403 {
404   int status;
405   char **args;
406   struct qelem *top = NULL;
407   
408   if ( (args = GetQuotaArgs(FALSE) ) == NULL)
409     return(DM_NORMAL);
410
411   if ( (status = do_mr_query("get_quota", CountArgs(args), args,
412                               StoreInfo, (char *) &top)) != MR_SUCCESS)
413     com_err(program_name, status, " in get_quota");
414   
415   FreeInfo(args);               /* done with args, free them. */
416   free(args);
417   top = QueueTop(top);
418   Loop(top, RealUpdateQuota);
419
420   FreeQueue(top);
421   return (DM_NORMAL);
422 }
423
424
425 /*      Function Name: RealDeleteQuota
426  *      Description: Actually removes the quota
427  *      Arguments: info - all information about this quota.
428  *                 one_item - true if there is only one item in the queue, and
429  *                            we should confirm.
430  *      Returns: none.
431  */
432
433 static void
434 RealDeleteQuota(info, one_item)
435 char ** info;
436 Bool one_item;
437 {
438   register int status;
439   char temp_buf[BUFSIZ];
440
441   if (!strcmp(info[Q_TYPE], "ANY"))
442     sprintf(temp_buf,
443             "Do you really want to delete the quota on %s",
444             info[Q_FILESYS]);
445   else
446     sprintf(temp_buf,
447             "Do you really want to delete the %s %s's quota on %s",
448             (strcmp(info[Q_TYPE], "USER") ? "group" : "user"),
449             info[Q_NAME], info[Q_FILESYS]);
450
451   if (!one_item || Confirm(temp_buf)) {
452     if ( (status = do_mr_query("delete_quota", 3, info,
453                                 Scream, (char *) NULL)) != MR_SUCCESS)
454       com_err(program_name, status, " in delete_quota");
455     else
456       Put_message("Quota sucessfully removed.");
457   }
458   else
459     Put_message("Aborted.");
460 }
461
462 /*        Function Name: DeleteQuota
463  *        Description: Removes the quota record for a filesystem
464  *                     w.r.t. a user, group, or anybody.
465  *        Arguments: None
466  *        Returns: DM_NORMAL
467  */
468
469 int
470 DeleteQuota()
471 {
472   register int status;
473   char **args;
474   struct qelem *top = NULL;
475
476   if ( (args = GetQuotaArgs(FALSE) ) == NULL)
477     return(DM_NORMAL);
478
479   if ( (status = do_mr_query("get_quota", 3, args,
480                               StoreInfo, (char *) &top)) != MR_SUCCESS)
481     com_err(program_name, status, " in get_quota");
482
483   FreeInfo(args);
484   free(args);
485   top = QueueTop(top);
486   QueryLoop(top, PrintQuota, RealDeleteQuota,
487             "Delete this quota on filesystem");
488
489   FreeQueue(top);
490   return(DM_NORMAL);
491 }
492
493   
This page took 0.178578 seconds and 5 git commands to generate.