]> andersk Git - moira.git/blob - clients/moira/quota.c
Deal with AFS quotas in a sensible manner
[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     return(info[Q_FILESYS]);
119 }
120
121
122 afsfilsyshelper(argc, argv, hint)
123 int argc;
124 char **argv;
125 int *hint;
126 {
127     *hint = !strcmp(argv[FS_TYPE], "AFS");
128     return(0);
129 }
130
131
132 int afsfilesys(name)
133 char *name;
134 {
135     int status, ret = 0;
136     char *argv[1];
137
138     argv[0] = name;
139     status = do_sms_query("get_filesys_by_label", 1, argv,
140                           afsfilsyshelper, &ret);
141     if (status == SMS_SUCCESS)
142       return(ret);
143     return(0);
144     
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 **
155 GetQuotaArgs(quota)
156 Bool quota;
157 {
158   char ** args = (char **) malloc( MAX_ARGS_SIZE * sizeof(char *) );
159   
160   if (args == NULL) {
161     Put_message("Could not allocate memory in GetQuotaArgs.");
162     return(NULL);
163   }
164
165   args[Q_FILESYS] = Strsave(DEFAULT_FILESYS);
166   args[Q_LOGIN] = Strsave(DEFAULT_USER);
167   if (quota) {
168     args[Q_QUOTA] = Strsave(GetDefaultUserQuota(FALSE));
169     args[3] = NULL;             /* NULL terminate. */
170   }
171   else 
172     args[2] = NULL;             /* NULL terminate. */
173
174   /* Get filesystem. */
175
176   GetValueFromUser("Filesystem", &args[Q_FILESYS]);
177   if (quota && !ValidName(args[Q_FILESYS]))
178     return(NULL);
179
180   /* Get and check username. */
181   if (afsfilesys(args[Q_FILESYS])) {
182       args[Q_LOGIN] = strsave(NOBODY);
183   } else {
184       GetValueFromUser("Username", &args[Q_LOGIN]);
185       if (!ValidName(args[Q_LOGIN])) return(NULL);
186   }
187
188   if (quota) {                  /* Get and check quota. */
189     GetValueFromUser("Quota", &args[Q_QUOTA]);
190     if (!ValidName(args[Q_QUOTA])) return(NULL);
191   }
192   return(args);
193 }  
194
195 /* ------------------------- Show Quota Info ------------------------- */
196
197 /*      Function Name: ShowDefaultQuota
198  *      Description: This prints out a default quota for the system.
199  *      Arguments: none
200  *      Returns: DM_NORMAL.
201  */
202
203 int
204 ShowDefaultQuota()
205 {
206   PrintDefaultQuota(GetDefaultUserQuota(TRUE));
207   return (DM_NORMAL);
208 }
209
210 /*      Function Name: ChangeDefaultQuota
211  *      Description: Changes the System Wide default quota.
212  *      Arguments: argc, argv - New quota in argv[1].
213  *      Returns: DM_NORMAL.
214  */
215
216 /*ARGSUSED*/
217 int
218 ChangeDefaultQuota(argc, argv)
219 int argc;
220 char *argv[];
221 {
222     register int status;
223     char temp_buf[BUFSIZ];
224     static char *newval[] = {
225         "def_quota", NULL, NULL
226     };
227
228     if (!ValidName(argv[1]))
229         return(DM_NORMAL);
230
231     sprintf(temp_buf,"%s %s","Are you sure that you want to",
232             "change the default quota for all new users");
233     if(Confirm(temp_buf)) {
234         newval[1] = argv[1];
235         if ( (status = do_sms_query("update_value", CountArgs(newval), 
236                                     newval, Scream, NULL)) == SMS_SUCCESS ) {
237           FreeAndClear(&def_quota, TRUE);
238           def_quota = Strsave(argv[1]);
239         }
240         else
241             com_err(program_name, status, " in update_value");
242     }
243     else
244         Put_message("Quota not changed.");
245
246     return (DM_NORMAL);
247 }
248
249 /* ------------------------- User Quotas ------------------------- */
250
251 /*      Function Name: ShowUserQuota
252  *      Description: Shows the quota of a user.
253  *      Arguments: none
254  *      Returns: DM_NORMAL
255  */
256
257 int
258 ShowUserQuota()
259 {
260   struct qelem *top = NULL;
261   register int status;
262   char ** args;
263
264   if ( (args = GetQuotaArgs(FALSE) ) == NULL)
265     return(DM_NORMAL);
266
267   if ( (status = do_sms_query("get_nfs_quota", CountArgs(args), args,
268                               StoreInfo, (char *) &top)) != SMS_SUCCESS)
269     com_err(program_name, status, " in get_nfs_quota");
270   
271   FreeInfo(args);               /* done with args free them. */
272   free(args);
273
274   top = QueueTop(top);
275   Loop(top, (void *) PrintQuota);
276   
277   FreeQueue(top);
278   return (DM_NORMAL);
279 }
280
281 /*      Function Name: AddUserQuota
282  *      Description: Adds a new quota entry to the database.
283  *      Arguments: argc, argv - name of the filesystem in argv[1].
284  *      Returns: DM_NORMAL
285  */
286
287 int
288 AddUserQuota()
289 {
290   char ** args;
291   register int status;
292   
293   if ( (args = GetQuotaArgs(TRUE) ) == NULL)
294     return(DM_NORMAL);
295
296   if ( (status = do_sms_query("add_nfs_quota", CountArgs(args), args,
297                               Scream, (char *) NULL)) != SMS_SUCCESS)
298     com_err(program_name, status, " in get_nfs_quota");
299   
300   FreeInfo(args);
301   free(args);
302   return(DM_NORMAL);
303 }
304
305 /*      Function Name: RealUpdateUser
306  *      Description: Performs the actual update of the user information.
307  *      Arguments: info - the information nesc. to update the user.
308  *      Returns: none.
309  */
310
311 static void
312 RealUpdateUser(info)
313 char ** info;
314 {
315   register int status;
316   char temp_buf[BUFSIZ];
317
318   sprintf(temp_buf, "New quota for filesystem %s (in KB)", info[Q_FILESYS]);
319   GetValueFromUser(temp_buf, &info[Q_QUOTA]);
320   
321   if (status = do_sms_query("update_nfs_quota", 3, info,
322                             Scream, (char *) NULL) != SMS_SUCCESS) {
323     com_err(program_name, status, " in update_nfs_quota");
324     sprintf(temp_buf,"Could not perform quota change on %s",
325             info[Q_FILESYS]); 
326     Put_message(temp_buf);
327   }
328 }
329   
330 /*      Function Name: ChangeUserQuota
331  *      Description: This function allows quotas to be updated.
332  *      Arguments: none.
333  *      Returns: DM_NORMAL.
334  */
335
336 int
337 ChangeUserQuota()
338 {
339   int status;
340   char **args;
341   struct qelem *top = NULL;
342   
343   if ( (args = GetQuotaArgs(FALSE) ) == NULL)
344     return(DM_NORMAL);
345
346   if ( (status = do_sms_query("get_nfs_quota", 2, args,
347                               StoreInfo, (char *) &top)) != SMS_SUCCESS)
348     com_err(program_name, status, " in get_nfs_quota");
349   
350   FreeInfo(args);               /* done with args, free them. */
351   free(args);
352   top = QueueTop(top);
353   Loop(top, RealUpdateUser);
354
355   FreeQueue(top);
356   return (DM_NORMAL);
357 }
358   
359 /*      Function Name: RealRemoveUserQuota
360  *      Description: Actually removes the user quota.
361  *      Arguments: info - all information about this user quota.
362  *                 one_item - true if there is only one item in the queue, and
363  *                            we should confirm.
364  *      Returns: none.
365  */
366
367 static void
368 RealRemoveUserQuota(info, one_item)
369 char ** info;
370 Bool one_item;
371 {
372   register int status;
373   char temp_buf[BUFSIZ];
374
375   sprintf(temp_buf,
376           "Do you really want to delete the user %s's quota on filesystem %s",
377           info[Q_LOGIN], info[Q_FILESYS]);
378
379   if (!one_item || Confirm(temp_buf)) {
380     if ( (status = do_sms_query("delete_nfs_quota", 2, info,
381                                 Scream, (char *) NULL)) != SMS_SUCCESS)
382       com_err(program_name, status, " in delete_nfs_quota");
383     else
384       Put_message("Quota sucessfully removed.");
385   }
386   else
387     Put_message("Aborted.");
388 }
389
390 /*      Function Name: RemoveUserQuota
391  *      Description: Removes a users quota on a given filsystem
392  *      Arguments: none.
393  *      Returns: DM_NORMAL.
394  */
395
396 int
397 RemoveUserQuota()
398 {
399   register int status;
400   char **args;
401   struct qelem *top = NULL;
402
403   if ( (args = GetQuotaArgs(FALSE) ) == NULL)
404     return(DM_NORMAL);
405
406   if ( (status = do_sms_query("get_nfs_quota", 2, args,
407                               StoreInfo, (char *) &top)) != SMS_SUCCESS)
408     com_err(program_name, status, " in get_nfs_quota");
409
410   FreeInfo(args);
411   free(args);
412   top = QueueTop(top);
413   QueryLoop(top, PrintQuota, RealRemoveUserQuota,
414             "Delete This users quota on filesystem");
415
416   FreeQueue(top);
417   return(DM_NORMAL);
418 }
This page took 0.066452 seconds and 5 git commands to generate.