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