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