]> andersk Git - moira.git/blob - gen/nfs.dc
Diane Delgado's changes for a fixed table-locking order
[moira.git] / gen / nfs.dc
1 /* $Header$
2  *
3  * This generates the files necessary to load an nfs server.
4  *
5  *  (c) Copyright 1988, 1990 by the Massachusetts Institute of Technology.
6  *  For copying and distribution information, please see the file
7  *  <mit-copyright.h>.
8  */
9
10 #include <mit-copyright.h>
11 #include <stdio.h>
12 #include <moira.h>
13 #include <moira_site.h>
14 #include <sys/types.h>
15 #include <sys/stat.h>
16 #include <sys/time.h>
17 EXEC SQL INCLUDE sqlca;
18
19
20 #define min(x,y)        ((x) < (y) ? (x) : (y))
21
22 char *whoami = "nfs.gen";
23 char *malloc(), *strsave();
24 char nfs_dir[64];
25 struct hash *users, *groups;
26
27
28 main(argc, argv)
29 int argc;
30 char **argv;
31 {
32     char cmd[64];
33     struct stat sb;
34     int changed = 0;
35
36     if (argc > 2) {
37         fprintf(stderr, "usage: %s [outfile]\n", argv[0]);
38         exit(MR_ARGS);
39     }
40
41     initialize_sms_error_table();
42     sprintf(nfs_dir, "%s/nfs", DCM_DIR);
43
44 #ifsql INGRES
45     EXEC SQL CONNECT moira;
46     EXEC SQL SET LOCKMODE SESSION WHERE LEVEL=TABLE, READLOCK=SHARED;
47 #endsql
48 #ifsql INFORMIX
49     EXEC SQL DATABASE moira;
50 #endsql
51
52     changed = do_nfs();
53
54 #ifsql INGRES
55     EXEC SQL DISCONNECT;
56 #endsql
57 #ifsql INFORMIX
58     EXEC SQL CLOSE DATABASE;
59 #endsql
60
61     if (!changed) {
62         fprintf(stderr, "No files updated.\n");
63         if (argc == 2 && stat(argv[1], &sb) == 0)
64           exit(MR_NO_CHANGE);
65     }
66
67     if (argc == 2) {
68         sprintf(cmd, "cd %s; cp %s/nfs/* .; tar cf %s .",
69                 nfs_dir, SMS_DIR, argv[1]);
70         if (system(cmd))
71           exit(MR_TAR_FAIL);
72     }
73
74     exit(MR_SUCCESS);
75 }
76
77
78 /* Generate the files.  Returns zero if nothing changed, non-zero otherwise. */
79
80 int do_nfs()
81 {
82     EXEC SQL BEGIN DECLARE SECTION;
83     char machname[41], listname[33];
84     EXEC SQL END DECLARE SECTION;
85     struct save_queue *machs, *lists;
86     int changed;
87
88     machs = sq_create();
89     lists = sq_create();
90
91     /* Get Locks */
92     EXEC SQL SELECT modtime INTO :listname FROM imembers WHERE list_id = 0;
93     EXEC SQL SELECT modtime INTO :listname FROM filesys WHERE filsys_id = 0;
94     EXEC SQL SELECT modtime INTO :listname FROM users WHERE users_id = 0;
95     EXEC SQL SELECT modtime INTO :listname FROM machine where mach_id=0;
96     EXEC SQL SELECT modtime INTO :listname FROM list WHERE list_id = 0;
97     EXEC SQL SELECT serverhosts INTO :listname FROM machine where mach_id=0;
98
99
100     /* The following is declarative, not executed,
101      * and so is dependent on where it is in the file,
102      * not in the order of execution of statements.
103      */
104     EXEC SQL WHENEVER SQLERROR GOTO sqlerr;
105     EXEC SQL DECLARE s_cursor CURSOR FOR
106       SELECT m.name, s.value3
107       FROM machine m, serverhosts s
108       WHERE m.mach_id = s.mach_id AND s.service = 'NFS' AND s.enable != 0;
109     EXEC SQL OPEN s_cursor;
110     while (1) {
111         EXEC SQL FETCH s_cursor INTO :machname, :listname;
112         if (sqlca.sqlcode != 0) break;
113         sq_save_unique_string(machs, strsave(strtrim(machname)));
114         sq_save_unique_string(lists, strsave(strtrim(listname)));
115       }
116     EXEC SQL CLOSE s_cursor;
117
118     changed = do_lists(lists);
119     EXEC SQL COMMIT;
120     changed += do_machs(machs);
121     return(changed);
122  sqlerr:
123     com_err(whoami, MR_INGRES_ERR, " code %d\n", sqlca.sqlcode);
124     critical_alert("DCM", "NFS build encountered DATABASE ERROR %d",
125                    sqlca.sqlcode);
126     exit(MR_INGRES_ERR);
127 }
128
129
130 /* Make all of the credentials lists that will be needed.  Returns 0 if
131  * no files were actually changed 
132  *
133  * The caller is responsible for acquiring locks on imembers, users, list
134  */
135
136 int do_lists(lists)
137 struct save_queue *lists;
138 {
139     char file[64], *u;
140     struct stat sb;
141     FILE *fd;
142     EXEC SQL BEGIN DECLARE SECTION;
143     char *listname, *lsname, lname[33], uname[9];
144     int uid, id, flag1, flag2, flag3, flag4;
145     EXEC SQL END DECLARE SECTION;
146
147     sprintf(file, "%s/list-", nfs_dir);
148     /*
149     if (stat(file, &sb) == 0) {
150         if ((ModDiff (&flag1, "users", sb.st_mtime)) ||
151             (ModDiff (&flag2, "list", sb.st_mtime)) ||
152             (ModDiff (&flag3, "imembers", sb.st_mtime)) ||
153             (ModDiff (&flag4, "serverhosts", sb.st_mtime))) exit (MR_DATE);
154         if (flag1 < 0 && flag2 < 0 && flag3 < 0 && flag4 < 0) {
155             fprintf(stderr, "The lists do not need to be rebuilt.\n");
156             return(0);
157         }
158     }
159     */
160
161
162     /* build the list of everyone, and store it in a file whose name
163      * corresponds to the empty list.
164      */
165     do_everyone();
166
167     fprintf(stderr, "Building specific lists\n");
168     /* now do each of the lists used by an NFS server */
169
170     while (sq_get_data(lists, &listname)) {
171         if (strlen(listname) == 0)
172           continue;
173         sprintf(file, "%s/list-%s", nfs_dir, listname);
174         fd = fopen(file, "w");
175         if (!fd) {
176             fprintf(stderr, "cannot open %s for output\n", file);
177             exit(MR_OCONFIG);
178         }
179
180         EXEC SQL DECLARE m_cursor CURSOR FOR
181           SELECT m.member_id
182           FROM imembers m, list l
183           WHERE m.list_id=l.list_id AND l.name = :listname AND
184             m.member_type='USER'
185           ORDER BY member_id;
186         EXEC SQL OPEN m_cursor;
187         while (1) {
188             EXEC SQL FETCH m_cursor INTO :id;
189             if (sqlca.sqlcode != 0) break;
190             if (u = hash_lookup(users, id))
191               fprintf(fd, "%s\n", u);
192         }
193         EXEC SQL CLOSE m_cursor;
194         if (fclose(fd) == EOF) {
195             fprintf(stderr, "error closing %s\n", file);
196             exit(MR_CCONFIG);
197         }
198     }
199 /* don't free here either
200     sq_destroy(lists);
201  */
202     return(1);
203  sqlerr:
204     com_err(whoami, MR_INGRES_ERR, " code %d\n", sqlca.sqlcode);
205     critical_alert("DCM", "NFS build encountered DATABASE ERROR %d",
206                    sqlca.sqlcode);
207     exit(MR_INGRES_ERR);
208 }
209
210
211 /*  Build the list of everybody. */
212 struct grp {
213     struct grp *next;
214     char *lid;
215 };
216 struct user {
217     char name[9];
218     int uid;
219     struct grp *lists;
220 };
221
222
223 do_everyone()
224 {
225     char buf[BUFSIZ], *l;
226     struct user *u;
227     struct grp *g;
228     struct bucket *b, **p;
229     EXEC SQL BEGIN DECLARE SECTION;
230     char name[33];
231     int gid, id, lid, maxid, uid;
232     EXEC SQL END DECLARE SECTION;
233     FILE *fd;
234     int i;
235     struct save_queue *sq;
236
237     fprintf(stderr, "Building the list of everybody\n");
238     sprintf(buf, "%s/list-", nfs_dir);
239     fd = fopen(buf, "w");
240     if (!fd) {
241         fprintf(stderr, "cannot open %s for output\n", buf);
242         exit(MR_OCONFIG);
243     }
244
245     /* make space for group list */
246     groups = create_hash(15000);
247
248     /* retrieve simple groups */
249     EXEC SQL DECLARE l_cursor CURSOR FOR
250      SELECT gid, list_id
251      FROM list
252      WHERE grouplist != 0 AND active != 0
253      ORDER BY list_id;
254     EXEC SQL OPEN l_cursor;
255     while (1) {
256         EXEC SQL FETCH l_cursor INTO :gid, :lid;
257         if (sqlca.sqlcode != 0) break;
258         sprintf(buf, ":%d", gid);
259         hash_store(groups, lid, strsave(buf));
260       }
261     EXEC SQL CLOSE l_cursor;
262
263     /* now do grplists */
264     users = create_hash(10000);
265     EXEC SQL DECLARE u_cursor CURSOR FOR
266       SELECT users_id, login, uid
267       FROM users
268       WHERE status = 1
269       ORDER BY users_id;
270     EXEC SQL OPEN u_cursor;
271     while (1) {
272         EXEC SQL FETCH u_cursor INTO :id, :name, :uid;
273         if (sqlca.sqlcode != 0) break;
274         u = (struct user *) malloc(sizeof(struct user));
275         strcpy(u->name, strtrim(name));
276         u->uid = uid;
277         u->lists = NULL;
278         hash_store(users, id, u);
279       }
280     EXEC SQL CLOSE u_cursor;
281
282     EXEC SQL DECLARE m_cursor2 CURSOR FOR
283       SELECT list_id, member_id
284       FROM imembers
285       WHERE member_type = 'USER'
286       ORDER BY list_id;
287     EXEC SQL OPEN m_cursor2;
288     while (1) {
289         EXEC SQL FETCH m_cursor2 INTO :lid, :id;
290         if (sqlca.sqlcode != 0) break;
291         if ((u = (struct user *) hash_lookup(users, id)) &&
292             ((l = hash_lookup(groups, lid)) != NULL)) {
293             g = (struct grp *) malloc(sizeof(struct grp));
294             g->next = u->lists;
295             u->lists = g;
296             g->lid = l;
297           }
298       }
299     EXEC SQL CLOSE m_cursor2;
300
301     for (p = &(users->data[users->size - 1]); p >= users->data; p--) {
302         for (b = *p; b; b = b->next) {
303             u = (struct user *)b->data;
304             sprintf(buf, "%s:%d", u->name, u->uid);
305             for (g = u->lists; g; g = g->next)
306               strcat(buf, g->lid);
307             b->data = strsave(buf);
308             fprintf(fd, "%s\n", buf);
309         }
310     }
311
312     fclose(fd);
313     free(groups);
314     return(1);
315  sqlerr:
316     com_err(whoami, MR_INGRES_ERR, " code %d\n", sqlca.sqlcode);
317     critical_alert("DCM", "NFS build encountered DATABASE ERROR %d",
318                    sqlca.sqlcode);
319     exit(MR_INGRES_ERR);
320 }
321
322
323 /* Now do each of the servers, linking the credentials list file and 
324  * compiling the quota and dirs files.
325  *
326  * Caller is responsible for acquiring locks on machine, filesys, users
327  */
328
329 int do_machs(machs)
330 struct save_queue *machs;
331 {
332     EXEC SQL BEGIN DECLARE SECTION;
333     char *machname, listname[33], dev[33], dir[81], fstype[9];
334     int uid, quota, id, gid, flag1, flag2, flag3, flag4;
335     EXEC SQL END DECLARE SECTION;
336     char file[64], f1[64], f2[64], *cp, *index();
337     int prevuid, quotasum, olddev, oldmach;
338     FILE *fd;
339     struct hash *machines;
340
341     fprintf(stderr, "Building machine files\n");
342
343
344     machines = create_hash(100);
345     while (sq_get_data(machs, &machname)) {
346         EXEC SQL SELECT s.value3, m.mach_id
347           INTO :listname, :id
348           FROM serverhosts s, machine m
349           WHERE s.mach_id = m.mach_id AND m.name = :machname AND 
350             s.service = 'NFS';
351         strtrim(machname);
352         sprintf(f1, "%s/list-%s", nfs_dir, strtrim(listname));
353         sprintf(f2, "%s/%s.cred", nfs_dir, machname);
354         unlink(f2); /* ignore errors on this unlink */
355         if (link(f1, f2)) {
356             fprintf(stderr, "Cannot link %s to %s\n", f1, f2);
357             exit(MR_OCONFIG);
358         }
359         hash_store(machines, id, machname);
360     }
361
362
363     olddev = oldmach = -1;
364     fd = stdin;
365
366     EXEC SQL DECLARE q_cursor CURSOR FOR
367       SELECT DISTINCT q.quota, q.entity_id, q.phys_id, n.device, n.mach_id 
368       FROM quota q, nfsphys n
369       WHERE n.nfsphys_id = q.phys_id AND q.phys_id != 0 AND
370         n.status < 16 AND q.type = 'USER'
371       ORDER BY n.mach_id, q.phys_id, q.entity_id;
372     EXEC SQL OPEN q_cursor;
373     while (1) {
374         EXEC SQL FETCH q_cursor INTO :quota, :uid, :flag1, :dev, :flag2;
375         if (sqlca.sqlcode != 0) break;
376         if (flag1 != olddev || flag2 != oldmach) {
377             if (quotasum)
378               fprintf(fd, "%d %d\n", prevuid, quotasum);
379             if (flag2 == 0 || !hash_lookup(machines, flag2))
380               continue;
381             if (fd != stdin)
382               fclose(fd);
383             olddev = flag1;
384             oldmach = flag2;
385             while (cp = index(dev, '/')) *cp = '@';
386             sprintf(file, "%s/%s.%s.quotas", nfs_dir,
387                     hash_lookup(machines, flag2), strtrim(dev));
388             fd = fopen(file, "w");
389             if (!fd) {
390                 fprintf(stderr, "cannot open %s for output\n", file);
391                 exit(MR_OCONFIG);
392             }
393             prevuid = -1;
394             quotasum = 0;
395         }
396         if (uid != prevuid) {
397             if ((cp = hash_lookup(users, prevuid)) &&
398                 (cp = index(cp, ':')))
399               prevuid = atoi(cp+1);
400             if (quotasum)
401               fprintf(fd, "%d %d\n", prevuid, quotasum);
402             prevuid = uid;
403             quotasum = quota;
404         } else {
405             quotasum += quota;
406         }
407     }
408     EXEC SQL CLOSE q_cursor;
409     if ((cp = hash_lookup(users, prevuid)) &&
410         (cp = index(cp, ':')))
411       prevuid = atoi(cp+1);
412     if (quotasum)
413       fprintf(fd, "%d %d\n", prevuid, quotasum);
414     if (fd != stdin && fclose(fd) == EOF) {
415         fprintf(stderr, "error closing %s", file);
416         exit(MR_CCONFIG);
417       }
418
419     olddev = oldmach = -1;
420     fd = stdin;
421     EXEC SQL DECLARE q_cursor2 CURSOR FOR
422       SELECT DISTINCT q.quota, q.entity_id, q.phys_id, n.device, n.mach_id,
423         n.status 
424       FROM quota q, nfsphys n
425       WHERE n.nfsphys_id = q.phys_id AND q.phys_id != 0 AND
426         n.status > 15 AND q.type = 'GROUP'
427       ORDER BY mach_id, phys_id, entity_id;
428     EXEC SQL OPEN q_cursor2;
429     while (1) {
430         EXEC SQL FETCH q_cursor2 INTO :quota, :gid, :flag1, :dev,
431                 :flag2, :flag3;
432         if (sqlca.sqlcode != 0) break;
433         if (flag1 != olddev || flag2 != oldmach) {
434             if (quotasum)
435               fprintf(fd, "%d %d\n", prevuid, quotasum);
436             if (flag2 == 0 || !hash_lookup(machines, flag2))
437               continue;
438             if (fd != stdin)
439               fclose(fd);
440             olddev = flag1;
441             oldmach = flag2;
442             while (cp = index(dev, '/')) *cp = '@';
443             sprintf(file, "%s/%s.%s.quotas", nfs_dir,
444                     hash_lookup(machines, flag2), strtrim(dev));
445             fd = fopen(file, "w");
446             if (!fd) {
447                 fprintf(stderr, "cannot open %s for output\n", file);
448                 exit(MR_OCONFIG);
449             }
450             prevuid = -1;
451             quotasum = 0;
452         }
453         if (gid != prevuid) {
454             if (cp = hash_lookup(groups, prevuid))
455               prevuid = atoi(cp + 1);
456             if (quotasum)
457               fprintf(fd, "%d %d\n", prevuid, quotasum);
458             prevuid = gid;
459             quotasum = quota;
460         } else {
461             quotasum += quota;
462         }
463     }
464     EXEC SQL CLOSE q_cursor2;
465     if (cp = hash_lookup(groups, prevuid))
466       prevuid = atoi(cp + 1);
467     if (quotasum)
468       fprintf(fd, "%d %d\n", prevuid, quotasum);
469     if (fd != stdin && fclose(fd) == EOF) {
470         fprintf(stderr, "error closing %s", file);
471         exit(MR_CCONFIG);
472       }
473
474     olddev = oldmach = -1;
475     fd = stdin;
476
477     EXEC SQL DECLARE q_cursor3 CURSOR FOR
478       SELECT DISTINCT f.name, f.lockertype, u.uid, l.gid, f.phys_id, 
479                 f.mach_id, n.device 
480       FROM users u, list l, nfsphys n, filesys f
481       WHERE u.users_id = f.owner AND 
482               l.list_id = f.owners AND
483               f.createflg != 0 AND f.phys_id != 0 AND
484               f.type = 'NFS' AND 
485               f.phys_id = n.nfsphys_id
486       ORDER BY mach_id, phys_id;
487     EXEC SQL OPEN q_cursor3;
488     while (1) {
489         EXEC SQL FETCH q_cursor3 INTO :dir, :fstype, :uid, :gid, :flag1,
490                         :flag2, :dev;
491         if (sqlca.sqlcode != 0) break;
492         if (flag1 != olddev || flag2 != oldmach) {
493             if (fd != stdin)
494               fclose(fd);
495             olddev = flag1;
496             oldmach = flag2;
497             while (cp = index(dev, '/')) *cp = '@';
498             sprintf(file, "%s/%s.%s.dirs", nfs_dir,
499                     hash_lookup(machines, flag2), strtrim(dev));
500             fd = fopen(file, "w");
501             if (!fd) {
502                 fprintf(stderr, "cannot open %s for output\n", file);
503                 exit(MR_OCONFIG);
504             }
505         }
506         fprintf(fd, "%s %d %d %s\n", strtrim(dir), uid, gid, strtrim(fstype));
507     }    
508     EXEC SQL CLOSE q_cursor3;
509     if (fclose(fd) == EOF) {
510         fprintf(stderr, "error closing %s", file);
511         exit(MR_CCONFIG);
512     }
513     return(1);
514  sqlerr:
515     com_err(whoami, MR_INGRES_ERR, " code %d\n", sqlca.sqlcode);
516     critical_alert("DCM", "NFS build encountered DATABASE ERROR %d",
517                    sqlca.sqlcode);
518     exit(MR_INGRES_ERR);
519 }
This page took 0.076313 seconds and 5 git commands to generate.