]> andersk Git - moira.git/blame - clients/moira/quota.c
Added comments and did some cleaning up in preparation for rewrite.
[moira.git] / clients / moira / quota.c
CommitLineData
73c83e3d 1#if (!defined(lint) && !defined(SABER))
2 static char rcsid_module_c[] = "$Header$";
3#endif lint
4
5/* This is the file ***FILE for allmaint, the SMS client that allows
6 * a user to maintaint most important parts of the SMS database.
7 * It Contains:
8 *
9 * Created:
10 * By:
11 *
12 * $Source$
13 * $Author$
14 * $Header$
15 *
16 * Copyright 1987, 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
23#include <stdio.h>
24#include <strings.h>
25#include <sms.h>
26#include <menu.h>
27
28#include "mit-copyright.h"
29#include "allmaint.h"
30#include "allmaint_funcs.h"
31#include "globals.h"
32#include "infodefs.h"
33
34static char * def_quota = NULL;
35
36#define DEFAULT_FILESYS DEFAULT_NONE
37#define DEFAULT_USER user /* this is the user who started sms. */
38
39/* Function Name: GetDefaultUserQuota
40 * Description: gets the user quota from sms, and caches the value.
41 * Arguments: override - if true, go to sms and override the cache.
42 * Returns: none.
43 * NOTE: Using a queue here is pretty useless, but StoreInfo expects
44 * one, and it works, so why fuck with it.
45 */
46
47static char *
48GetDefaultUserQuota(override)
49Bool override;
50{
51 register int status;
52 char ** info;
53 struct qelem * top = NULL;
54 static char *val[] = {"def_quota", NULL};
55
56 if (override || (def_quota == NULL)) {
57 if ( (status = sms_query("get_value", CountArgs(val),
58 val, StoreInfo, (char *) &top)) != SMS_SUCCESS) {
59 com_err(program_name, status, " in ShowDefaultQuota");
60 if (def_quota == NULL) {
61 Put_message("No default Quota Found, setting default to 0.");
62 def_quota = Strsave("0");
63 }
64 else
65 Put_message("No default Quota Found, retaining old value.");
66 }
67 else {
68 top = QueueTop(top);
69 info = (char **) top->q_data;
70 FreeAndClear(&def_quota, TRUE);
71 def_quota = Strsave(info[0]);
72 FreeQueue(top);
73 }
74 }
75 return(def_quota);
76}
77
78/* Function Name: PrintDefaultQuota
79 * Description: Prints default quota info in a meaningful way.
80 * Arguments: value of the default quota.
81 * Returns: none.
82 */
83
84static void
85PrintDefaultQuota(quota)
86char * quota;
87{
88 char temp_buf[BUFSIZ];
89 Put_message("");
90 sprintf(temp_buf,"The default quota is %s Kb.", quota);
91 Put_message(temp_buf);
92}
93
94/* Function Name: PrintQuota
95 * Description: Prints a users quota information.
96 * Arguments: info - a pointer to the quota information:
97 * Returns: none.
98 */
99
100static char *
101PrintQuota(info)
102char ** info;
103{
104 char buf[BUFSIZ];
105 Put_message("");
106 sprintf(buf, "Filsystem: %-45s User: %s",info[Q_FILESYS], info[Q_LOGIN]);
107 Put_message(buf);
108 sprintf(buf, "Machine: %-20s Directory: %-15s Quota: %s",
109 info[Q_MACHINE], info[Q_DIRECTORY], info[Q_QUOTA]);
110 Put_message(buf);
111 return(info[Q_FILESYS]);
112}
113
114/* Function Name: GetQuotaArgs
115 * Description: gets quota args from the user
116 * Arguments: quota - if TRUE the get quota too.
117 * Returns: the arguments.
118 */
119
120static char **
121GetQuotaArgs(quota)
122Bool quota;
123{
124 char ** args = (char **) malloc( MAX_ARGS_SIZE * sizeof(char *) );
125
126 if (args == NULL) {
127 Put_message("Could not allocate memory in GetQuotaArgs.");
128 return(NULL);
129 }
130
131 args[Q_FILESYS] = Strsave(DEFAULT_FILESYS);
132 args[Q_LOGIN] = Strsave(DEFAULT_USER);
133 if (quota) {
134 args[Q_QUOTA] = Strsave(GetDefaultUserQuota(FALSE));
135 args[3] = NULL; /* NULL terminate. */
136 }
137 else
138 args[2] = NULL; /* NULL terminate. */
139
140 /* Get filesystem. */
141
142 GetValueFromUser("Filesystem", &args[Q_FILESYS]);
143 if (quota) /* We need an exact entry. */
144 if (!ValidName(args[Q_FILESYS]))
145 return(NULL);
146
147 /* Get and check username. */
148
149 GetValueFromUser("Username", &args[Q_LOGIN]);
150 if (!ValidName(args[Q_LOGIN])) return(NULL);
151
152 if (quota) { /* Get and check quota. */
153 GetValueFromUser("Quota", &args[Q_QUOTA]);
154 if (!ValidName(args[Q_QUOTA])) return(NULL);
155 }
156 return(args);
157}
158
159/* ------------------------- Show Quota Info ------------------------- */
160
161/* Function Name: ShowDefaultQuota
162 * Description: This prints out a default quota for the system.
163 * Arguments: none
164 * Returns: DM_NORMAL.
165 */
166
167int
168ShowDefaultQuota()
169{
170 PrintDefaultQuota(GetDefaultUserQuota(TRUE));
171 return (DM_NORMAL);
172}
173
174/* Function Name: ChangeDefaultQuota
175 * Description: Changes the System Wide default quota.
176 * Arguments: argc, argv - New quota in argv[1].
177 * Returns: DM_NORMAL.
178 */
179
180/*ARGSUSED*/
181int
182ChangeDefaultQuota(argc, argv)
183int argc;
184char *argv[];
185{
186 register int status;
187 char temp_buf[BUFSIZ];
188 static char *newval[] = {
189 "def_quota", NULL, NULL
190 };
191
192 if (!ValidName(argv[1]))
193 return(DM_NORMAL);
194
195 sprintf(temp_buf,"%s %s","Are you sure that you want to",
196 "change the default quota for all new users");
197 if(Confirm(temp_buf)) {
198 newval[1] = argv[1];
199 if ( (status = sms_query("update_value", CountArgs(newval),
200 newval, Scream, NULL)) == SMS_SUCCESS ) {
201 FreeAndClear(&def_quota, TRUE);
202 def_quota = Strsave(argv[1]);
203 }
204 else
205 com_err(program_name, status, " in update_value");
206 }
207 else
208 Put_message("Quota not changed.");
209
210 return (DM_NORMAL);
211}
212
213/* ------------------------- User Quotas ------------------------- */
214
215/* Function Name: ShowUserQuota
216 * Description: Shows the quota of a user.
217 * Arguments: none
218 * Returns: DM_NORMAL
219 */
220
221int
222ShowUserQuota()
223{
224 struct qelem *top = NULL;
225 register int status;
226 char ** args;
227
228 if ( (args = GetQuotaArgs(FALSE) ) == NULL)
229 return(DM_NORMAL);
230
231 if ( (status = sms_query("get_nfs_quota", CountArgs(args), args,
232 StoreInfo, (char *) &top)) != SMS_SUCCESS)
233 com_err(program_name, status, " in get_nfs_quota");
234
235 FreeInfo(args); /* done with args free them. */
236 free(args);
237
238 top = QueueTop(top);
239 Loop(top, (void *) PrintQuota);
240
241 FreeQueue(top);
242 return (DM_NORMAL);
243}
244
245/* Function Name: AddUserQuota
246 * Description: Adds a new quota entry to the database.
247 * Arguments: argc, argv - name of the filesystem in argv[1].
248 * Returns: DM_NORMAL
249 */
250
251int
252AddUserQuota()
253{
254 char ** args;
255 register int status;
256
257 if ( (args = GetQuotaArgs(TRUE) ) == NULL)
258 return(DM_NORMAL);
259
260 if ( (status = sms_query("add_nfs_quota", CountArgs(args), args,
261 Scream, (char *) NULL)) != SMS_SUCCESS)
262 com_err(program_name, status, " in get_nfs_quota");
263
264 FreeInfo(args);
265 free(args);
266 return(DM_NORMAL);
267}
268
269/* Function Name: RealUpdateUser
270 * Description: Performs the actual update of the user information.
271 * Arguments: info - the information nesc. to update the user.
272 * Returns: none.
273 */
274
275static void
276RealUpdateUser(info)
277char ** info;
278{
279 register int status;
280 char temp_buf[BUFSIZ];
281
282 sprintf(temp_buf, "New quota for filesystem %s (in KB)", info[Q_FILESYS]);
283 GetValueFromUser(temp_buf, &info[Q_QUOTA]);
284
285 if (status = sms_query("update_nfs_quota", 3, info,
286 Scream, (char *) NULL) != SMS_SUCCESS) {
287 com_err(program_name, status, " in update_nfs_quota");
288 sprintf(temp_buf,"Could not perform quota change on %s",
289 info[Q_FILESYS]);
290 Put_message(temp_buf);
291 }
292}
293
294/* Function Name: ChangeUserQuota
295 * Description: This function allows quotas to be updated.
296 * Arguments: none.
297 * Returns: DM_NORMAL.
298 */
299
300int
301ChangeUserQuota()
302{
303 int status;
304 char **args;
305 struct qelem *top = NULL;
306
307 if ( (args = GetQuotaArgs(FALSE) ) == NULL)
308 return(DM_NORMAL);
309
310 if ( (status = sms_query("get_nfs_quota", 2, args,
311 StoreInfo, (char *) &top)) != SMS_SUCCESS)
312 com_err(program_name, status, " in get_nfs_quota");
313
314 FreeInfo(args); /* done with args, free them. */
315 free(args);
316 top = QueueTop(top);
317 Loop(top, RealUpdateUser);
318
319 FreeQueue(top);
320 return (DM_NORMAL);
321}
322
323/* Function Name: RealRemoveUserQuota
324 * Description: Actually removes the user quota.
325 * Arguments: info - all information about this user quota.
326 * one_item - true if there is only one item in the queue, and
327 * we should confirm.
328 * Returns: none.
329 */
330
331static void
332RealRemoveUserQuota(info, one_item)
333char ** info;
334Bool one_item;
335{
336 register int status;
337 char temp_buf[BUFSIZ];
338
339 sprintf(temp_buf,
340 "Do you really want to delete the user %s's quota on filesystem %s",
341 info[Q_LOGIN], info[Q_FILESYS]);
342
343 if (!one_item || Confirm(temp_buf)) {
344 if ( (status = sms_query("delete_nfs_quota", 2, info,
345 Scream, (char *) NULL)) != SMS_SUCCESS)
346 com_err(program_name, status, " in delete_nfs_quota");
347 else
348 Put_message("Quota sucessfully removed.");
349 }
350 else
351 Put_message("Aborted.");
352}
353
354/* Function Name: RemoveUserQuota
355 * Description: Removes a users quota on a given filsystem
356 * Arguments: none.
357 * Returns: DM_NORMAL.
358 */
359
360int
361RemoveUserQuota()
362{
363 register int status;
364 char **args;
365 struct qelem *top = NULL;
366
367 if ( (args = GetQuotaArgs(FALSE) ) == NULL)
368 return(DM_NORMAL);
369
370 if ( (status = sms_query("get_nfs_quota", 2, args,
371 StoreInfo, (char *) &top)) != SMS_SUCCESS)
372 com_err(program_name, status, " in get_nfs_quota");
373
374 FreeInfo(args);
375 free(args);
376 top = QueueTop(top);
377 QueryLoop(top, PrintQuota, RealRemoveUserQuota,
378 "Delete This users quota on filesystem");
379
380 FreeQueue(top);
381 return(DM_NORMAL);
382}
This page took 0.09498 seconds and 5 git commands to generate.