]> andersk Git - moira.git/blame - clients/moira/attach.c
Added comments and did some cleaning up in preparation for rewrite.
[moira.git] / clients / moira / attach.c
CommitLineData
402461ad 1#if (!defined(lint) && !defined(SABER))
08345b74 2 static char rcsid_module_c[] = "$Header$";
402461ad 3#endif
08345b74 4
5/* This is the file attach.c for allmaint, the SMS client that allows
6 * a user to maintaint most important parts of the SMS database.
7 * It Contains: Functions for maintaining data used by Hesiod
8 * to map courses/projects/users to their file systems,
9 * and maintain filesys info.
10 *
11 * Created: 5/4/88
12 * By: Chris D. Peterson
13 * Based Upon: attach.c 87/07/24 marcus
14 *
15 * $Source$
16 * $Author$
17 * $Header$
18 *
19 * Copyright 1987, 1988 by the Massachusetts Institute of Technology.
20 *
21 * For further information on copyright and distribution
22 * see the file mit-copyright.h
23 */
24
08345b74 25#include <stdio.h>
26#include <strings.h>
27#include <sms.h>
28#include <menu.h>
29
461c03b6 30#include "mit-copyright.h"
31#include "allmaint.h"
32#include "allmaint_funcs.h"
33#include "globals.h"
34#include "infodefs.h"
35
08345b74 36#define FS_ALIAS_TYPE "FILESYS"
37
461c03b6 38#define LABEL 0
39#define MACHINE 1
40#define GROUP 2
41#define ALIAS 3
08345b74 42
85ca828a 43#define DEFAULT_TYPE ("NFS")
44#define DEFAULT_MACHINE DEFAULT_NONE
45#define DEFAULT_PACK DEFAULT_NONE
46#define DEFAULT_M_POINT DEFAULT_NONE
47#define DEFAULT_ACCESS ("r")
48#define DEFAULT_COMMENTS DEFAULT_COMMENT
49#define DEFAULT_OWNER (user)
50#define DEFAULT_OWNERS (user)
51#define DEFAULT_CREATE DEFAULT_YES
52#define DEFAULT_L_TYPE ("HOMEDIR")
53
54/* Function Name: SetDefaults
55 * Description: sets the default values for filesystem additions.
56 * Arguments: info - an array of char pointers to recieve defaults.
57 * Returns: char ** (this array, now filled).
58 */
59
60static char **
61SetDefaults(info, name)
62char ** info;
63char * name;
64{
65 info[FS_NAME] = Strsave(name);
66 info[FS_TYPE] = Strsave(DEFAULT_TYPE);
67 info[FS_MACHINE] = Strsave(DEFAULT_MACHINE);
68 info[FS_PACK] = Strsave(DEFAULT_PACK);
69 info[FS_M_POINT] = Strsave(DEFAULT_M_POINT);
70 info[FS_ACCESS] = Strsave(DEFAULT_ACCESS);
71 info[FS_COMMENTS] = Strsave(DEFAULT_COMMENTS);
72 info[FS_OWNER] = Strsave(DEFAULT_OWNER);
73 info[FS_OWNERS] = Strsave(DEFAULT_OWNERS);
74 info[FS_CREATE] = Strsave(DEFAULT_CREATE);
75 info[FS_L_TYPE] = Strsave(DEFAULT_L_TYPE);
76 info[FS_MODTIME] = info[FS_MODBY] = info[FS_MODWITH] = info[FS_END] = NULL;
77 return(info);
78}
79
08345b74 80/* Function Name: GetFSInfo
81 * Description: Stores the info in a queue.
82 * Arguments: type - type of information to get.
83 * name - name of the item to get information on.
84 * Returns: a pointer to the first element in the queue.
85 */
86
402461ad 87static struct qelem *
08345b74 88GetFSInfo(type, name)
89int type;
90char *name;
91{
92 int stat;
93 struct qelem * elem = NULL;
85ca828a 94 char * args[5];
08345b74 95
96 switch (type) {
461c03b6 97 case LABEL:
08345b74 98 if ( (stat = sms_query("get_filesys_by_label", 1, &name,
99 StoreInfo, &elem)) != 0) {
461c03b6 100 com_err(program_name, stat, NULL);
08345b74 101 return(NULL);
102 }
103 break;
461c03b6 104 case MACHINE:
105 if ( (stat = sms_query("get_filesys_by_machine", 1, &name,
08345b74 106 StoreInfo, &elem)) != 0) {
461c03b6 107 com_err(program_name, stat, NULL);
08345b74 108 return(NULL);
109 }
110 break;
461c03b6 111 case GROUP:
112 if ( (stat = sms_query("get_filesys_by_group", 1, &name,
08345b74 113 StoreInfo, &elem)) != 0) {
461c03b6 114 com_err(program_name, stat, NULL);
08345b74 115 return(NULL);
116 }
117 break;
461c03b6 118 case ALIAS:
85ca828a 119 args[ALIAS_NAME] = name;
120 args[ALIAS_TYPE] = FS_ALIAS_TYPE;
121 args[ALIAS_TRANS] = "*";
122 if ( (stat = sms_query("get_alias", 3, args, StoreInfo, &elem)) != 0) {
461c03b6 123 com_err(program_name, stat, " in get_alias.");
08345b74 124 return(NULL);
125 }
126 }
127
128 return(QueueTop(elem));
129}
130
85ca828a 131/* Function Name: PrintFSAlias
132 * Description: Prints a filesystem alias
133 * Arguments: info - an array contains the strings of info.
402461ad 134 * Returns: the name of the filesys - used be QueryLoop().
85ca828a 135 */
136
402461ad 137static char *
85ca828a 138PrintFSAlias(info)
139char ** info;
140{
141 char buf[BUFSIZ];
142
143 sprintf(buf,"Alias: %-25s Filesystem: %s",info[ALIAS_NAME],
144 info[ALIAS_TRANS]);
145 Put_message(buf);
402461ad 146 return(info[ALIAS_NAME]);
85ca828a 147}
148
08345b74 149/* Function Name: PrintFSInfo
150 * Description: Prints the filesystem information.
151 * Arguments: info - a pointer to the filesystem information.
152 * Returns: none.
153 */
154
402461ad 155static char *
461c03b6 156PrintFSInfo(info)
08345b74 157char ** info;
158{
159 char print_buf[BUFSIZ];
85ca828a 160 FORMFEED;
161 sprintf(print_buf,"%20s Filesystem: %s",
162 " ",info[FS_NAME]);
08345b74 163 Put_message(print_buf);
85ca828a 164 sprintf(print_buf,"Type: %-40s Machine: %-15s",
165 info[FS_TYPE], info[FS_MACHINE]);
08345b74 166 Put_message(print_buf);
85ca828a 167 sprintf(print_buf,"Default Access: %-2s Packname: %-17s Mountpoint %s ",
168 info[FS_ACCESS], info[FS_PACK], info[FS_M_POINT]);
08345b74 169 Put_message(print_buf);
170 sprintf(print_buf,"Comments; %s",info[FS_COMMENTS]);
171 Put_message(print_buf);
85ca828a 172 sprintf(print_buf, "User Ownership: %-30s Group Ownership: %s",
08345b74 173 info[FS_OWNER], info[FS_OWNERS]);
174 Put_message(print_buf);
075fe5bb 175 sprintf(print_buf, "Auto Create: %-34s Locker Type: %s",
176 atoi(info[FS_CREATE]) ? "ON" : "OFF",
08345b74 177 info[FS_L_TYPE]);
178 Put_message(print_buf);
075fe5bb 179 sprintf(print_buf, MOD_FORMAT, info[FS_MODBY], info[FS_MODTIME],
180 info[FS_MODWITH]);
08345b74 181 Put_message(print_buf);
402461ad 182 return(info[FS_NAME]);
08345b74 183}
184
08345b74 185/* Function Name: AskFSInfo.
186 * Description: This function askes the user for information about a
187 * machine and saves it into a structure.
188 * Arguments: info - a pointer the the structure to put the
189 * info into.
190 * name - add a newname field? (T/F)
191 * Returns: none.
192 */
193
402461ad 194static char **
461c03b6 195AskFSInfo(info, name)
08345b74 196char ** info;
197Bool name;
198{
461c03b6 199 char temp_buf[BUFSIZ], *newname;
08345b74 200
402461ad 201 Put_message("");
202 sprintf(temp_buf, "Changing Attributes of filesystem %s.",
85ca828a 203 info[FS_NAME]);
08345b74 204 Put_message(temp_buf);
402461ad 205 Put_message("");
08345b74 206
207 if (name) {
208 newname = Strsave(info[FS_NAME]);
402461ad 209 GetValueFromUser("The new name for this filesystem",
08345b74 210 &newname);
211 }
212
402461ad 213 GetValueFromUser("Filesystem's Type", &info[FS_TYPE]);
214 GetValueFromUser("Filesystem's Machine", &info[FS_MACHINE]);
85ca828a 215 strcpy(temp_buf, CanonicalizeHostname(info[FS_MACHINE]));
216 free(info[FS_MACHINE]);
217 info[FS_MACHINE] = Strsave(temp_buf);
402461ad 218 GetValueFromUser("Filesystem's Pack Name", &info[FS_PACK]);
219 GetValueFromUser("Filesystem's Mount Point", &info[FS_M_POINT]);
220 GetValueFromUser("Filesystem's Default Access", &info[FS_ACCESS]);
221 GetValueFromUser("Comments about this Filesystem", &info[FS_COMMENTS]);
222 GetValueFromUser("Filesystem's owner (user)", &info[FS_OWNER]);
223 GetValueFromUser("Filesystem's owners (group)", &info[FS_OWNERS]);
075fe5bb 224 GetYesNoValueFromUser("Automatically create this filsystem",
85ca828a 225 &info[FS_CREATE]);
402461ad 226 GetValueFromUser("Filesystem's lockertype", &info[FS_L_TYPE]);
08345b74 227
228 FreeAndClear(&info[FS_MODTIME], TRUE);
229 FreeAndClear(&info[FS_MODBY], TRUE);
230 FreeAndClear(&info[FS_MODWITH], TRUE);
231
232 if (name) /* slide the newname into the #2 slot. */
233 SlipInNewName(info, newname);
234
235 return(info);
236}
237
238/* --------------- Filesystem Menu ------------- */
239
240/* Function Name: GetFS
241 * Description: Get Filesystem information by name.
242 * Arguments: argc, argv - name of filsys in argv[1].
243 * Returns: DM_NORMAL.
244 */
245
246/* ARGSUSED */
247int
248GetFS(argc, argv)
249int argc;
250char **argv;
251{
85ca828a 252 struct qelem *top, *elem;
08345b74 253
85ca828a 254 top = elem = GetFSInfo(LABEL, argv[1]); /* get info. */
255 while(elem != NULL) {
256 char ** info = (char **) elem->q_data;
402461ad 257 (void) PrintFSInfo(info);
85ca828a 258 elem = elem->q_forw;
259 }
260 FreeQueue(top); /* clean the queue. */
08345b74 261 return (DM_NORMAL);
262}
263
402461ad 264/* Function Name: RealDeleteFS
265 * Description: Does the real deletion work.
266 * Arguments: info - array of char *'s containing all useful info.
267 * one_item - a Boolean that is true if only one item
268 * in queue that dumped us here.
269 * Returns: none.
270 */
271
272void
273RealDeleteFS(info, one_item)
274char ** info;
275Bool one_item;
276{
277 int stat;
278 char temp_buf[BUFSIZ];
279
280/*
281 * Deletetions are performed if the user hits 'y' on a list of multiple
282 * filesystem, or if the user confirms on a unique alias.
283 */
284 sprintf(temp_buf, "Are you sure that you want to delete filesystem %s",
285 info[FS_NAME]);
286 if(!one_item || Confirm(temp_buf)) {
287 if ( (stat = sms_query("delete_filesys", 1,
288 &info[FS_NAME], Scream, NULL)) != 0)
289 com_err(program_name, stat, " filesystem not deleted.");
290 else
291 Put_message("Filesystem deleted.");
292 }
293 else
294 Put_message("Filesystem not deleted.");
295}
296
08345b74 297/* Function Name: DeleteFS
298 * Description: Delete a filesystem give its name.
299 * Arguments: argc, argv - argv[1] is the name of the filesystem.
300 * Returns: none.
301 */
302
303/* ARGSUSED */
304
305int
306DeleteFS(argc, argv)
307int argc;
308char **argv;
309{
402461ad 310 struct qelem *elem = GetFSInfo(LABEL, argv[1]);
311 QueryLoop(elem, PrintFSInfo, RealDeleteFS, "Delete the Filesystem");
08345b74 312
402461ad 313 FreeQueue(elem);
08345b74 314 return (DM_NORMAL);
315}
316
402461ad 317/* Function Name: RealChangeFS
318 * Description: performs the actual change to the filesys.
319 * Arguments: info - the information
320 * junk - an unused boolean.
321 * Returns: none.
322 */
323
324/* ARGSUSED. */
325static void
326RealChangeFS(info, junk)
327char ** info;
328Bool junk;
329{
330 int stat;
331 char ** args = AskFSInfo(info, TRUE);
332
333 if ( (stat = sms_query("update_filesys", CountArgs(args),
334 args, NullFunc, NULL)) != 0)
335 com_err(program_name, stat, ", filesystem not updated");
336 else
337 Put_message("filesystem sucessfully updated.");
338}
339
08345b74 340/* Function Name: ChangeFS
341 * Description: change the information in a filesys record.
342 * Arguments: arc, argv - value of filsys in argv[1].
343 * Returns: DM_NORMAL.
344 */
345
346/* ARGSUSED */
347int
348ChangeFS(argc, argv)
349char **argv;
350int argc;
351{
402461ad 352 struct qelem *elem = GetFSInfo(LABEL, argv[1]);
353 QueryLoop(elem, NullPrint, RealChangeFS, "Update the Filesystem");
08345b74 354
08345b74 355 FreeQueue(elem);
356 return (DM_NORMAL);
357}
358
359/* Function Name: AddFS
360 * Description: change the information in a filesys record.
361 * Arguments: arc, argv - name of filsys in argv[1].
362 * Returns: DM_NORMAL.
363 */
364
365/* ARGSUSED */
366int
367AddFS(argc, argv)
368char **argv;
369int argc;
370{
371 char *info[MAX_ARGS_SIZE], **args;
85ca828a 372 int stat;
08345b74 373
374 if ( !ValidName(argv[1]) )
375 return(DM_NORMAL);
376
377 if ( (stat = sms_query("get_filesys_by_label", 1, argv + 1,
378 NullFunc, NULL)) == 0) {
379 Put_message ("A Filesystem by that name already exists.");
380 return(DM_NORMAL);
461c03b6 381 } else if (stat != SMS_NO_MATCH) {
382 com_err(program_name, stat, " in AddFS");
08345b74 383 return(DM_NORMAL);
384 }
385
85ca828a 386 args = AskFSInfo(SetDefaults(info, argv[1]), FALSE );
461c03b6 387
85ca828a 388 if ( (stat = sms_query("add_filesys", CountArgs(args), args,
389 NullFunc, NULL)) != 0)
461c03b6 390 com_err(program_name, stat, " in AddFS");
08345b74 391
392 FreeInfo(info);
393 return (DM_NORMAL);
394}
395
396/* -------------- Top Level Menu ---------------- */
397
398/* Function Name: GetFSAlias
399 * Description: Gets the value for a Filesystem Alias.
400 * Arguments: argc, argv - name of alias in argv[1].
401 * Returns: DM_NORMAL.
402 * NOTES: There should only be one filesystem per alias, thus
403 * this will work correctly.
404 */
405
406/* ARGSUSED */
407int
408GetFSAlias(argc, argv)
409int argc;
410char **argv;
411{
08345b74 412 struct qelem *top, *elem;
413
461c03b6 414 top = elem = GetFSInfo(ALIAS, argv[1]);
08345b74 415
85ca828a 416 Put_message(" "); /* blank line. */
08345b74 417 while (elem != NULL) {
85ca828a 418 char **info = (char **) elem->q_data;
419 PrintFSAlias(info);
08345b74 420 elem = elem->q_forw;
421 }
422
423 FreeQueue(top);
424 return(DM_NORMAL);
425}
426
427/* Function Name: CreateFSAlias
428 * Description: Create an alias name for a filsystem
429 * Arguments: argc, argv - name of alias in argv[1].
430 * Returns: DM_NORMAL.
431 * NOTES: This requires (name, type, transl) I get {name, translation}
432 * from the user. I provide type, which is well-known.
433 */
434
435/* ARGSUSED */
436int
437CreateFSAlias(argc, argv)
438int argc;
439char **argv;
440{
461c03b6 441 register int stat;
08345b74 442 struct qelem *elem, *top;
461c03b6 443 char *args[MAX_ARGS_SIZE], buf[BUFSIZ], **info;
08345b74 444
445 elem = NULL;
446
447 if (!ValidName(argv[1]))
448 return(DM_NORMAL);
449
461c03b6 450 args[ALIAS_NAME] = Strsave(argv[1]);
451 args[ALIAS_TYPE] = Strsave(FS_ALIAS_TYPE);
85ca828a 452 args[ALIAS_TRANS] = Strsave("*");
08345b74 453
454/*
455 * Check to see if this alias already exists in the database, if so then
456 * print out values, free memory used and then exit.
457 */
458
85ca828a 459 if ( (stat = sms_query("get_alias", 3, args, StoreInfo, &elem)) == 0) {
460 top = elem = QueueTop(elem);
08345b74 461 while (elem != NULL) {
462 info = (char **) elem->q_data;
85ca828a 463 sprintf(buf,"The alias: %s currently describes the filesystem %s",
461c03b6 464 info[ALIAS_NAME], info[ALIAS_TRANS]);
08345b74 465 Put_message(buf);
466 elem = elem->q_forw;
467 }
468 FreeQueue(top);
469 return(DM_NORMAL);
470 }
471 else if ( stat != SMS_NO_MATCH) {
461c03b6 472 com_err(program_name, stat, " in CreateFSAlias.");
08345b74 473 return(DM_NORMAL);
474 }
475
461c03b6 476 args[ALIAS_TRANS]= args[ALIAS_END] = NULL; /* set to NULL initially. */
08345b74 477 GetValueFromUser("Which filesystem will this alias point to?",
461c03b6 478 &args[ALIAS_TRANS]);
08345b74 479
480 if ( (stat = sms_query("add_alias", 3, args, NullFunc, NULL)) != 0)
461c03b6 481 com_err(program_name, stat, " in CreateFSAlias.");
08345b74 482
461c03b6 483 FreeInfo(args);
08345b74 484 return (DM_NORMAL);
485}
402461ad 486
487/* Function Name: RealDeleteFSAlias
488 * Description: Does the real deletion work.
489 * Arguments: info - array of char *'s containing all useful info.
490 * one_item - a Boolean that is true if only one item
491 * in queue that dumped us here.
492 * Returns: none.
493 */
494
495void
496RealDeleteFSAlias(info, one_item)
497char ** info;
498Bool one_item;
499{
500 int stat;
501 char temp_buf[BUFSIZ];
502
503/*
504 * Deletetions are performed if the user hits 'y' on a list of multiple
505 * filesystem, or if the user confirms on a unique alias.
506 */
507 sprintf(temp_buf,
508 "Are you sure that you want to delete the filesystem alias %s",
509 info[ALIAS_NAME]);
510 if(!one_item || Confirm(temp_buf)) {
511 if ( (stat = sms_query("delete_alias", CountArgs(info),
512 info, Scream, NULL)) != 0 )
513 com_err(program_name, stat, " filesystem alias not deleted.");
514 else
515 Put_message("Filesystem alias deleted.");
516 }
517 else
518 Put_message("Filesystem alias not deleted.");
519}
08345b74 520
521/* Function Name: DeleteFSAlias
522 * Description: Delete an alias name for a filsystem
523 * Arguments: argc, argv - name of alias in argv[1].
524 * Returns: DM_NORMAL.
525 * NOTES: This requires (name, type, transl) I get {name, translation}
526 * from the user. I provide type, which is well-known.
527 */
528
529/* ARGSUSED */
530int
531DeleteFSAlias(argc, argv)
532int argc;
533char **argv;
534{
402461ad 535 struct qelem *elem = GetFSInfo(ALIAS, argv[1]);
536 QueryLoop(elem, PrintFSAlias, RealDeleteFSAlias,
537 "Delete the Filesystem Alias");
538 FreeQueue(elem);
08345b74 539 return (DM_NORMAL);
540}
541
542/* Function Name: AttachHelp
543 * Description: Print help info on attachmaint.
544 * Arguments: none
545 * Returns: DM_NORMAL.
546 */
547
548int
549AttachHelp()
550{
551 static char *message[] = {
85ca828a 552 "These are the options:\n\n",
553 "get - get information about a filesystem.\n",
554 "add - add a new filesystem to the data base.\n",
555 "update - update the information in the database on a filesystem.\n",
556 "delete - delete a filesystem from the database.\n",
557 "check - check information about association of a name and a filesys.\n",
558 "alias - associate a name with a filsystem.\n",
559 "unalias - disassociate a name with a filesystem.\n",
560 "verbose - toggle the request for delete confirmation.\n",
08345b74 561 NULL,
562 };
563
564 return(PrintHelp(message));
565}
566
567/*
568 * Local Variables:
569 * mode: c
570 * c-indent-level: 4
571 * c-continued-statement-offset: 4
572 * c-brace-offset: -4
573 * c-argdecl-indent: 4
574 * c-label-offset: -4
575 * End:
576 */
This page took 0.135226 seconds and 5 git commands to generate.