]> andersk Git - moira.git/blob - clients/moira/nfs.c
Change strings.h to string.h (we weren't using any BSD-isms anyway!)
[moira.git] / clients / moira / nfs.c
1 #if (!defined(lint) && !defined(SABER))
2   static char rcsid_module_c[] = "$Header$";
3 #endif lint
4
5 /*      This is the file nfs.c for the MOIRA Client, which allows a nieve
6  *      user to quickly and easily maintain most parts of the MOIRA database.
7  *      It Contains: All functions for manipulating NFS Physical directories.
8  *      
9  *      Created:        5/6/88
10  *      By:             Chris D. Peterson
11  *
12  *      $Source$
13  *      $Author$
14  *      $Header$
15  *      
16  *      Copyright 1988 by the Massachusetts Institute of Technology.
17  *
18  *      For further information on copyright and distribution 
19  *      see the file mit-copyright.h
20  */
21
22 #include <stdio.h>
23 #include <string.h>
24 #include <moira.h>
25 #include <moira_site.h>
26 #include <menu.h>
27
28 #include "mit-copyright.h"
29 #include "defs.h"
30 #include "f_defs.h"
31 #include "globals.h"
32
33
34 #define TYPE_NFS    "NFS"
35
36 #define DEFAULT_DIR    "/u1/lockers"
37 #define DEFAULT_DEVICE "/dev/ra1c"
38 #define DEFAULT_STATUS DEFAULT_YES /* active. */
39 #define DEFAULT_ALLOC  "0"
40 #define DEFAULT_SIZE   "0"
41
42 /*      Function Name: UpdatePrint
43  *      Description: store a useful string for updates to print.
44  *      Arguments: info - info about NFS service stored in array of strings.
45  *      Returns: useful string.
46  */
47
48 static char *
49 UpdatePrint(info)
50 char ** info;
51 {
52     char temp_buf[BUFSIZ];
53     sprintf(temp_buf, "Machine %s Directory %s", 
54             info[NFS_NAME], info[NFS_DIR]);
55     return(Strsave(temp_buf));  /* Small memory leak here, but no good way
56                                    to avoid it that I see. */
57 }
58
59 /*      Function Name: PrintNFSInfo
60  *      Description: Prints NFS Physical service information.
61  *      Arguments: info - the information.
62  *      Returns: directory of this nfs server, for DeleteNFSService().
63  */
64
65 static char *
66 PrintNFSInfo(info)
67 char ** info;
68 {
69     char buf[BUFSIZ], status_buf[BUFSIZ];
70     int status = atoi(info[NFS_STATUS]);
71     Bool is_one = FALSE;
72     
73     status_buf[0] = '\0';       /* clear string. */
74
75     if (status & MR_FS_STUDENT) {
76         strcat(status_buf, "Student");
77         is_one = TRUE;
78     }
79     if (status & MR_FS_FACULTY) {
80         if (is_one)
81             strcat(status_buf, " and ");
82         strcat(status_buf, "Faculty");
83         is_one = TRUE;
84     }
85     if (status & MR_FS_STAFF) {
86         if (is_one)
87             strcat(status_buf, " and ");
88         strcat(status_buf, "Staff");
89         is_one = TRUE;
90     }
91     if (status & MR_FS_MISC) {
92         if (is_one)
93             strcat(status_buf, " and ");
94         strcat(status_buf, "Miscellaneous");
95     }
96     /* Add another type here. */
97     if (status & MR_FS_GROUPQUOTA) {
98         if (is_one)
99             strcat(status_buf, " / ");
100         strcat(status_buf, "Group Quotas Enabled");
101     }
102
103     if (status_buf[0] == '\0')
104         strcat(status_buf, "-- None --");
105     
106     Put_message("");
107     sprintf(buf,"Machine: %-20s Directory: %-15s Device: %s",
108             info[NFS_NAME], info[NFS_DIR], info[NFS_DEVICE]);
109     Put_message(buf);
110     sprintf(buf, "Status: %s", status_buf);
111     Put_message(buf);
112     sprintf(buf, "Quota Allocated: %-17s Size: %s",
113             info[NFS_ALLOC], info[NFS_SIZE]);
114     Put_message(buf);
115     sprintf(buf, MOD_FORMAT, info[NFS_MODBY], info[NFS_MODTIME],
116             info[NFS_MODWITH]);
117     Put_message(buf);
118     return(info[NFS_DIR]);
119 }
120
121 /*      Function Name: AskNFSInfo.
122  *      Description: This function askes the user for information about a 
123  *                   machine and saves it into a structure.
124  *      Arguments: info - a pointer the the structure to put the
125  *                             info into.
126  *      Returns: the arglist to make the update call with.
127  */
128
129 char **
130 AskNFSInfo(info)
131 char ** info;
132 {
133     /* Also need name of the machine in this structure. */
134
135     if (GetValueFromUser("Device for this filsystem", &info[NFS_DEVICE]) ==
136         SUB_ERROR)
137       return(NULL);
138     if (GetFSTypes(&info[NFS_STATUS], TRUE) == SUB_ERROR)
139       return(NULL);
140     if (GetValueFromUser("Allocated Space for this filsystem:",
141                          &info[NFS_ALLOC]) == SUB_ERROR)
142       return(NULL);
143     if (GetValueFromUser("Size of this Filsystem:", &info[NFS_SIZE]) ==
144         SUB_ERROR)
145       return(NULL);
146
147     FreeAndClear(&info[NFS_MODTIME], TRUE);
148     FreeAndClear(&info[NFS_MODBY], TRUE);
149     FreeAndClear(&info[NFS_MODWITH], TRUE);
150     
151     return(info);
152 }
153
154
155 /*      Function Name: ShowNFSService
156  *      Description: This function prints all exported partitions.
157  *      Arguments: argc, argv - argv[1] - name of machine.
158  *      Returns: DM_NORMAL.
159  */
160
161 /* ARGSUSED */
162 int 
163 ShowNFSService(argc, argv)
164 int argc;
165 char **argv;   
166 {
167     register int stat;
168     struct qelem *elem = NULL;
169     char *args[10];
170
171     if (!ValidName(argv[1]))
172         return(DM_NORMAL);
173     
174     args[0] = canonicalize_hostname(strsave(argv[1]));
175     args[1] = strsave(DEFAULT_DIR);
176     if (GetValueFromUser("Directory:", &args[1]) == SUB_ERROR)
177       return(DM_NORMAL);
178     
179     if ( (stat = do_mr_query("get_nfsphys", 2, args,
180                               StoreInfo, (char *)  &elem)) != MR_SUCCESS)
181         com_err(program_name, stat, " in ShowNFSServices.");
182     free(args[0]);
183     free(args[1]);              /* prevents memory leaks. */
184
185     elem = QueueTop(elem);
186     Loop(elem, (void *) PrintNFSInfo);
187
188     FreeQueue(elem);
189     return (DM_NORMAL);
190 }
191
192 /*      Function Name: AddNFSService
193  *      Description: Adds a new partition to the nfsphys relation
194  *      Arguments: arc, argv - argv[1] - machine name.
195  *      Returns: DM_NORMAL.
196  */
197
198 /* ARGSUSED */
199 int
200 AddNFSService(argc, argv)
201 char **argv;
202 int argc;
203 {
204     char **add_args, *args[10];
205     char *info[MAX_ARGS_SIZE];
206     int stat;
207
208     args[0] = canonicalize_hostname(strsave(argv[1]));
209     args[1] = strsave(DEFAULT_DIR);
210     if (GetValueFromUser("Directory:", &args[1]) == SUB_ERROR)
211       return(DM_NORMAL);
212     
213     if (!ValidName(args[0]) || !ValidName(args[1]))
214         return(DM_NORMAL);
215     
216     if ( (stat = do_mr_query("get_nfsphys", 2, args,
217                               NullFunc, (char *) NULL)) == MR_SUCCESS)
218       stat = MR_EXISTS;
219     if (stat != MR_NO_MATCH) {
220         com_err(program_name, stat, " in get_nfsphys.");
221         return(DM_NORMAL);
222     }
223     
224     info[NFS_NAME]   = Strsave(args[0]);
225     info[NFS_DIR]    = args[1]; /* already saved. */
226     info[NFS_DEVICE] = Strsave(DEFAULT_DEVICE);
227     info[NFS_STATUS] = Strsave(DEFAULT_STATUS);
228     info[NFS_ALLOC]  = Strsave(DEFAULT_ALLOC);
229     info[NFS_SIZE]   = Strsave(DEFAULT_SIZE);
230     info[NFS_MODBY] = info[NFS_MODWITH] = info[NFS_MODTIME] = NULL;
231     info[NFS_END] = NULL;       
232
233     if ((add_args = AskNFSInfo(info)) == NULL) {
234         Put_message("Aborted.");
235         return(DM_NORMAL);
236     }
237     
238     if ((stat = do_mr_query("add_nfsphys", CountArgs(add_args), add_args,
239                              Scream, (char *) NULL)) != 0) 
240         com_err(program_name, stat, " in AdsNFSService");
241     
242     FreeInfo(info);
243     free(args[0]);
244     return (DM_NORMAL);
245 }
246
247 /*      Function Name: RealUpdateNFSService
248  *      Description: performs the actual update of the nfs service.
249  *      Arguments: info - info about NFS service stored in array of strings.
250  *                 junk - an unused boolean.
251  *      Returns: none.
252  */
253
254 /* ARGSUSED */
255 static void
256 RealUpdateNFSService(info, junk)
257 char ** info;
258 Bool junk;
259 {
260     char ** args;
261     register int stat;
262     
263     if ((args = AskNFSInfo(info)) == NULL) {
264         Put_message("Aborted.");
265         return;
266     }
267
268     if ((stat = do_mr_query("update_nfsphys", CountArgs(args), args,
269                              Scream, (char *)NULL)) != MR_SUCCESS) 
270         com_err(program_name, stat, " in RealUpdateNFSService");
271 }
272
273 /*      Function Name: UpdateNFSService
274  *      Description: Update the values for an nfsphys entry.
275  *      Arguments: argc, argv - argv[1] - machine name.
276  *      Returns: DM_NORMAL.
277  */
278
279 /* ARGSUSED. */
280 int
281 UpdateNFSService(argc, argv)
282 char **argv;
283 int argc;
284 {
285     register int stat;
286     struct qelem *elem = NULL;
287     char * args[10];
288
289     if (!ValidName(argv[1]))
290         return(DM_NORMAL);
291
292     args[0] = canonicalize_hostname(strsave(argv[1]));
293     args[1] = strsave(DEFAULT_DIR);
294     if (GetValueFromUser("Directory:", &args[1]) == SUB_ERROR)
295       return(DM_NORMAL);
296
297     if ( (stat = do_mr_query("get_nfsphys", 2, args,
298                               StoreInfo, (char *) &elem)) != MR_SUCCESS) {
299         com_err(program_name, stat, " in UpdateNFSService.");
300         return (DM_NORMAL);
301     }
302     free(args[0]);
303     free(args[1]);              /* stop memory leaks. */
304
305     elem = QueueTop(elem);
306     QueryLoop(elem, UpdatePrint, RealUpdateNFSService, 
307               "Update NFS Service for");
308
309     FreeQueue(elem);
310     return (DM_NORMAL);
311 }
312
313 /*      Function Name: FSPartPrint
314  *      Description: print filesystem partition usage.
315  *      Arguments: info - the filesystem information.
316  *      Returns: none.
317  */
318
319 static void
320 FSPartPrint(info)
321 char ** info;
322 {
323     char buf[BUFSIZ];
324     sprintf(buf, "NFS Filesystem %s uses that partition.", info[FS_NAME]);
325     Put_message(buf);
326 }
327
328 /*      Function Name: RealDeleteNFSService
329  *      Description: Actually Deletes the filesystem (some checks are made).
330  *      Arguments: info - info about NFS service stored in array of strings.
331  *                 one_item - if TRUE then only one item on the queue, and
332  *                            we should confirm.
333  *      Returns: none.
334  */
335
336 static void
337 RealDeleteNFSService(info, one_item)
338 char ** info;
339 Bool one_item;
340 {
341     char temp_buf[BUFSIZ], *args[10];
342     struct qelem *elem= NULL;
343     register int stat;
344     
345     sprintf(temp_buf,
346             "Are you sure that you want to delete the %s directory on %s",
347             info[NFS_DIR],info[NFS_NAME]);
348
349 /* 
350  * Check to be sure that it is not used by any of the nfs packs.
351  */
352
353     if (!one_item || Confirm(temp_buf)) {
354         args[0] = info[NFS_NAME];
355         args[1] = info[NFS_DIR];
356         args[2] = NULL;
357         switch(stat = do_mr_query("get_filesys_by_nfsphys", CountArgs(args), 
358                                    args, StoreInfo, (char *)&elem)) {
359         case MR_NO_MATCH:       /* it is unused, delete it. */
360             if ( (stat = do_mr_query("delete_nfsphys", 2, info, Scream, 
361                                       (char *) NULL )) != MR_SUCCESS)
362                 com_err(program_name, stat, " in DeleteNFSService");
363             else
364                 Put_message("Physical Filesystem Deleted.");
365             break;
366         case MR_SUCCESS:        /* it is used, print filesys's that use it. */
367             elem = QueueTop(elem);
368             Put_message("The following fileystems are using this partition,");
369             Put_message("and must be removed before it can be deleted.");
370             Put_message("");
371             Loop(elem, FSPartPrint);
372
373             FreeQueue(elem);
374             Put_message("");
375             break;
376         default:
377             com_err(program_name, stat, " while checking usage of partition");
378         }
379     }
380     else
381         Put_message("Physical filesystem not deleted.");
382 }
383
384 /*      Function Name: DeleteNFSService
385  *      Description: Delete an nfsphys entry.
386  *      Arguments: argc, argv - name of the machine in argv[1].
387  *      Returns: DM_NORMAL.
388  */
389
390 /* ARGSUSED */
391 int
392 DeleteNFSService(argc, argv)
393 int argc;
394 char **argv;
395 {
396     register int stat;
397     struct qelem *elem = NULL;
398     char * args[10];
399
400     if (!ValidName(argv[1]))
401         return(DM_NORMAL);
402
403     args[0] = canonicalize_hostname(strsave(argv[1]));
404     args[1] = strsave(DEFAULT_DIR);
405     if (GetValueFromUser("Directory:", &args[1]) == SUB_ERROR)
406       return(DM_NORMAL);
407
408     switch(stat = do_mr_query("get_nfsphys", 2, args, 
409                                StoreInfo, (char *) &elem)) {
410     case MR_NO_MATCH:
411         Put_message("This filsystem does not exist!");
412         return(DM_NORMAL);
413     case MR_SUCCESS:
414         break;
415     default:
416         com_err(program_name, stat, " in DeleteNFSService");
417         return(DM_NORMAL);
418     }
419     free(args[0]);
420     free(args[1]);              /* stop memory leaks, in your neighborhood. */
421
422     QueryLoop(elem, PrintNFSInfo, RealDeleteNFSService,
423               "Delete the Physical Filesystem on Directory");
424
425     FreeQueue(elem);
426     return(DM_NORMAL);
427 }    
This page took 0.127279 seconds and 5 git commands to generate.