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