]>
Commit | Line | Data |
---|---|---|
73c83e3d | 1 | #if (!defined(lint) && !defined(SABER)) |
2 | static char rcsid_module_c[] = "$Header$"; | |
3 | #endif lint | |
4 | ||
8defc06b | 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. | |
0a2c64cb | 7 | * It Contains: Functions for manipulating the quota information. |
73c83e3d | 8 | * |
0a2c64cb | 9 | * Created: 7/10/88 |
10 | * By: Chris D. Peterson | |
73c83e3d | 11 | * |
12 | * $Source$ | |
13 | * $Author$ | |
14 | * $Header$ | |
15 | * | |
0a2c64cb | 16 | * Copyright 1988 by the Massachusetts Institute of Technology. |
73c83e3d | 17 | * |
18 | * For further information on copyright and distribution | |
19 | * see the file mit-copyright.h | |
20 | */ | |
21 | ||
73c83e3d | 22 | #include <stdio.h> |
f071d8a7 | 23 | #include <string.h> |
8defc06b | 24 | #include <moira.h> |
25 | #include <moira_site.h> | |
73c83e3d | 26 | #include <menu.h> |
27 | ||
28 | #include "mit-copyright.h" | |
0a2c64cb | 29 | #include "defs.h" |
30 | #include "f_defs.h" | |
73c83e3d | 31 | #include "globals.h" |
73c83e3d | 32 | |
33 | static char * def_quota = NULL; | |
34 | ||
35 | #define DEFAULT_FILESYS DEFAULT_NONE | |
8defc06b | 36 | #define DEFAULT_USER user /* this is the user who started moira. */ |
bf96ab95 | 37 | #define NOBODY "\\[nobody\\]" |
b124ed18 | 38 | #define DEFAULT_QTYPE "USER" |
bf96ab95 | 39 | |
73c83e3d | 40 | |
41 | /* Function Name: GetDefaultUserQuota | |
8defc06b | 42 | * Description: gets the user quota from moira, and caches the value. |
43 | * Arguments: override - if true, go to moira and override the cache. | |
73c83e3d | 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)) { | |
8defc06b | 59 | if ( (status = do_mr_query("get_value", CountArgs(val), val, |
60 | StoreInfo, (char *) &top)) != MR_SUCCESS) { | |
73c83e3d | 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 | { | |
b124ed18 | 106 | char buf[BUFSIZ]; |
bf96ab95 | 107 | |
73c83e3d | 108 | Put_message(""); |
b124ed18 | 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]); | |
73c83e3d | 115 | Put_message(buf); |
bf96ab95 | 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]); | |
73c83e3d | 120 | Put_message(buf); |
70aa4030 | 121 | sprintf(buf, MOD_FORMAT, info[Q_MODBY], info[Q_MODTIME], info[Q_MODWITH]); |
122 | Put_message(buf); | |
73c83e3d | 123 | return(info[Q_FILESYS]); |
124 | } | |
125 | ||
bf96ab95 | 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 | ||
f071d8a7 | 143 | if (strchr(name, '*') || strchr(name, '?') || strchr(name, '\\')) |
d950adaf | 144 | return(0); |
bf96ab95 | 145 | argv[0] = name; |
8defc06b | 146 | status = do_mr_query("get_filesys_by_label", 1, argv, |
bf96ab95 | 147 | afsfilsyshelper, &ret); |
8defc06b | 148 | if (status == MR_SUCCESS) |
bf96ab95 | 149 | return(ret); |
6d8848f6 | 150 | return(status); |
bf96ab95 | 151 | |
152 | } | |
153 | ||
154 | ||
73c83e3d | 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 *) ); | |
6d8848f6 | 166 | int af; |
73c83e3d | 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); | |
b124ed18 | 174 | args[Q_TYPE] = Strsave(DEFAULT_QTYPE); |
175 | args[Q_NAME] = Strsave(DEFAULT_USER); | |
73c83e3d | 176 | if (quota) { |
177 | args[Q_QUOTA] = Strsave(GetDefaultUserQuota(FALSE)); | |
b124ed18 | 178 | args[Q_QUOTA+1] = NULL; /* NULL terminate. */ |
73c83e3d | 179 | } |
180 | else | |
b124ed18 | 181 | args[Q_NAME+1] = NULL; /* NULL terminate. */ |
73c83e3d | 182 | |
183 | /* Get filesystem. */ | |
184 | ||
245b2337 | 185 | if (GetValueFromUser("Filesystem", &args[Q_FILESYS]) == SUB_ERROR) |
186 | return(NULL); | |
bf96ab95 | 187 | if (quota && !ValidName(args[Q_FILESYS])) |
188 | return(NULL); | |
b124ed18 | 189 | |
6d8848f6 | 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) { | |
b124ed18 | 199 | args[Q_TYPE] = strsave("ANY"); |
200 | args[Q_NAME] = strsave(NOBODY); | |
201 | } | |
202 | else { | |
245b2337 | 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); | |
bf96ab95 | 209 | } |
73c83e3d | 210 | |
211 | if (quota) { /* Get and check quota. */ | |
245b2337 | 212 | if (GetValueFromUser("Quota", &args[Q_QUOTA]) == SUB_ERROR) |
213 | return(NULL); | |
214 | if (!ValidName(args[Q_QUOTA])) return(NULL); | |
73c83e3d | 215 | } |
216 | return(args); | |
217 | } | |
218 | ||
73c83e3d | 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]; | |
8defc06b | 257 | if ( (status = do_mr_query("update_value", CountArgs(newval), |
258 | newval, Scream, NULL)) == MR_SUCCESS ) { | |
73c83e3d | 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 | ||
b124ed18 | 271 | /* ------------------------ Filesystem Quotas -------------------- */ |
73c83e3d | 272 | |
b124ed18 | 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 | |
73c83e3d | 278 | */ |
279 | ||
280 | int | |
b124ed18 | 281 | GetQuota() |
73c83e3d | 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 | ||
b124ed18 | 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); | |
73c83e3d | 295 | free(args); |
296 | ||
297 | top = QueueTop(top); | |
298 | Loop(top, (void *) PrintQuota); | |
b124ed18 | 299 | |
73c83e3d | 300 | FreeQueue(top); |
b124ed18 | 301 | return(DM_NORMAL); |
73c83e3d | 302 | } |
303 | ||
b124ed18 | 304 | /* Function Name: GetQuotaByFilesys |
305 | * Description: Shows all quotas associated with the | |
306 | * given filesystem | |
307 | * Arguments: none | |
308 | * Returns: DM_NORMAL | |
73c83e3d | 309 | */ |
310 | ||
311 | int | |
b124ed18 | 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 | ||
0b48ba68 | 323 | args[0] = Strsave(""); |
324 | args[1] = NULL; | |
245b2337 | 325 | if (GetValueFromUser("Filesystem", &args[0]) == SUB_ERROR) |
326 | return(DM_NORMAL); | |
b124ed18 | 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() | |
73c83e3d | 350 | { |
351 | char ** args; | |
352 | register int status; | |
353 | ||
354 | if ( (args = GetQuotaArgs(TRUE) ) == NULL) | |
355 | return(DM_NORMAL); | |
356 | ||
b124ed18 | 357 | if ( (status = do_mr_query("add_quota", CountArgs(args), args, |
8defc06b | 358 | Scream, (char *) NULL)) != MR_SUCCESS) |
b124ed18 | 359 | com_err(program_name, status, " in add_quota"); |
73c83e3d | 360 | |
361 | FreeInfo(args); | |
362 | free(args); | |
363 | return(DM_NORMAL); | |
364 | } | |
365 | ||
b124ed18 | 366 | /* Function Name: RealUpdateQuota |
367 | * Description: Performs the actual update of the quota | |
368 | * Arguments: info - the information nesc. to update the quota. | |
73c83e3d | 369 | * Returns: none. |
370 | */ | |
371 | ||
372 | static void | |
b124ed18 | 373 | RealUpdateQuota(info) |
73c83e3d | 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]); | |
245b2337 | 380 | if (GetValueFromUser(temp_buf, &info[Q_QUOTA]) == SUB_ERROR) { |
381 | Put_message("Not changed."); | |
382 | return; | |
383 | } | |
b124ed18 | 384 | |
385 | if (status = do_mr_query("update_quota", 4, info, | |
8defc06b | 386 | Scream, (char *) NULL) != MR_SUCCESS) { |
b124ed18 | 387 | com_err(program_name, status, " in update_quota"); |
73c83e3d | 388 | sprintf(temp_buf,"Could not perform quota change on %s", |
389 | info[Q_FILESYS]); | |
390 | Put_message(temp_buf); | |
391 | } | |
392 | } | |
b124ed18 | 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 | |
73c83e3d | 399 | */ |
400 | ||
401 | int | |
b124ed18 | 402 | UpdateQuota() |
73c83e3d | 403 | { |
404 | int status; | |
405 | char **args; | |
406 | struct qelem *top = NULL; | |
407 | ||
408 | if ( (args = GetQuotaArgs(FALSE) ) == NULL) | |
409 | return(DM_NORMAL); | |
410 | ||
b124ed18 | 411 | if ( (status = do_mr_query("get_quota", CountArgs(args), args, |
8defc06b | 412 | StoreInfo, (char *) &top)) != MR_SUCCESS) |
b124ed18 | 413 | com_err(program_name, status, " in get_quota"); |
73c83e3d | 414 | |
415 | FreeInfo(args); /* done with args, free them. */ | |
416 | free(args); | |
417 | top = QueueTop(top); | |
b124ed18 | 418 | Loop(top, RealUpdateQuota); |
73c83e3d | 419 | |
420 | FreeQueue(top); | |
421 | return (DM_NORMAL); | |
422 | } | |
b124ed18 | 423 | |
424 | ||
425 | /* Function Name: RealDeleteQuota | |
426 | * Description: Actually removes the quota | |
427 | * Arguments: info - all information about this quota. | |
73c83e3d | 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 | |
b124ed18 | 434 | RealDeleteQuota(info, one_item) |
73c83e3d | 435 | char ** info; |
436 | Bool one_item; | |
437 | { | |
438 | register int status; | |
439 | char temp_buf[BUFSIZ]; | |
440 | ||
b124ed18 | 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]); | |
73c83e3d | 450 | |
451 | if (!one_item || Confirm(temp_buf)) { | |
b124ed18 | 452 | if ( (status = do_mr_query("delete_quota", 3, info, |
8defc06b | 453 | Scream, (char *) NULL)) != MR_SUCCESS) |
b124ed18 | 454 | com_err(program_name, status, " in delete_quota"); |
73c83e3d | 455 | else |
456 | Put_message("Quota sucessfully removed."); | |
457 | } | |
458 | else | |
459 | Put_message("Aborted."); | |
460 | } | |
461 | ||
b124ed18 | 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 | |
73c83e3d | 467 | */ |
468 | ||
469 | int | |
b124ed18 | 470 | DeleteQuota() |
73c83e3d | 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 | ||
b124ed18 | 479 | if ( (status = do_mr_query("get_quota", 3, args, |
8defc06b | 480 | StoreInfo, (char *) &top)) != MR_SUCCESS) |
b124ed18 | 481 | com_err(program_name, status, " in get_quota"); |
73c83e3d | 482 | |
483 | FreeInfo(args); | |
484 | free(args); | |
485 | top = QueueTop(top); | |
b124ed18 | 486 | QueryLoop(top, PrintQuota, RealDeleteQuota, |
487 | "Delete this quota on filesystem"); | |
73c83e3d | 488 | |
489 | FreeQueue(top); | |
490 | return(DM_NORMAL); | |
491 | } | |
b124ed18 | 492 | |
493 |