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