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