]> andersk Git - moira.git/blob - gen/nfs.qc
added copyright message
[moira.git] / gen / nfs.qc
1 /* $Header$
2  *
3  * This generates the files necessary to load an nfs server.
4  *
5  *  (c) Copyright 1988 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 <sms.h>
13 #include <sms_app.h>
14 #include <sys/types.h>
15 #include <sys/stat.h>
16 #include <sys/time.h>
17
18 #define NFS_DIR "/u1/sms/dcm/nfs"
19
20 #define min(x,y)        ((x) < (y) ? (x) : (y))
21
22 struct np {
23     char mach[33];
24     char dev[33];
25     char list[33];
26 };
27
28 struct fs {
29     int nfsphys;
30     char *dir;
31     int owner;
32     int owners;
33     struct qt *quota;
34 };
35
36
37 char *whoami = "nfs.gen";
38 char *malloc(), *strsave();
39 char *ingres_date_and_time(), *ingres_time(), *ingres_date();
40
41 main(argc, argv)
42 int argc;
43 char **argv;
44 {
45     char cmd[64];
46     struct stat sb;
47     int changed = 0;
48     int ingerr();
49
50     if (argc > 2) {
51         fprintf(stderr, "usage: %s [outfile]\n", argv[0]);
52         exit(SMS_ARGS);
53     }
54
55     IIseterr(ingerr);
56 ##  ingres sms
57
58     changed = do_nfs();
59
60 ##  exit
61
62     if (!changed) {
63         fprintf(stderr, "No files updated.\n");
64         if (argc == 2 && stat(argv[1], &sb) == 0)
65           exit(SMS_NO_CHANGE);
66     }
67
68     if (argc == 2) {
69         sprintf(cmd, "cd %s; cp /u1/sms/bin/install_* .; tar cf %s .", NFS_DIR, argv[1]);
70         if (system(cmd))
71           exit(SMS_TAR_FAIL);
72     }
73
74     exit(SMS_SUCCESS);
75 }
76
77
78 /*
79  * ingerr: (supposedly) called when Ingres indicates an error.
80  * I have not yet been able to get this to work to intercept a
81  * database open error.
82  */
83 #define INGRES_DEADLOCK 4700
84
85 static int ingerr(num)
86     int *num;
87 {
88     char buf[256];
89     int ingres_errno;
90
91     switch (*num) {
92     case INGRES_DEADLOCK:
93         ingres_errno = SMS_DEADLOCK;
94         break;
95     default:
96         ingres_errno = SMS_INGRES_ERR;
97     }
98     com_err(whoami, SMS_INGRES_ERR, " code %d\n", *num);
99     critical_alert("DCM", "NFS build encountered INGRES ERROR %d", *num);
100     exit(ingres_errno);
101 }
102
103
104 /* Generate the files.  Returns zero if nothing changed, non-zero otherwise. */
105
106 int do_nfs()
107 ##{
108 ##  char machname[33], listname[33];
109     struct save_queue *machs, *lists;
110     int changed;
111
112     machs = sq_create();
113     lists = sq_create();
114 ##  range of s is serverhosts
115 ##  retrieve (machname = trim(machine.name), listname = trim(s.value3))
116 ##      where machine.mach_id = s.mach_id and s.enable != 0 {
117       sq_save_unique_string(machs, strsave(machname));
118       sq_save_unique_string(lists, strsave(listname));
119 ##  }
120
121     changed = do_lists(lists);
122     changed += do_machs(machs);
123     return(changed);
124 ##}
125
126
127 /* Make all of the credentials lists that will be needed.  Returns 0 if
128  * no files were actually changed */
129
130 int do_lists(lists)
131 struct save_queue *lists;
132 ##{
133     struct save_queue *sq;
134     char file[64], *u;
135     struct hash *users, *do_everyone();
136     struct stat sb;
137     FILE *fd;
138 ##  char *listname, *lsname, lname[33], uname[9], *filetime;
139 ##  int uid, id, flag1, flag2, flag3, flag4;
140
141     sprintf(file, "%s/list-", NFS_DIR);
142     if (stat(file, &sb) == 0) {
143         filetime = ingres_date_and_time(sb.st_mtime);
144 ##      retrieve (flag1 = int4(interval("min", tblstats.modtime - filetime)))
145 ##              where tblstats.table = "users"
146 ##      retrieve (flag2 = int4(interval("min", tblstats.modtime - filetime)))
147 ##              where tblstats.table = "list"
148 ##      retrieve (flag3 = int4(interval("min", tblstats.modtime - filetime)))
149 ##              where tblstats.table = "members"
150 ##      retrieve (flag4 = int4(interval("min", tblstats.modtime - filetime)))
151 ##              where tblstats.table = "serverhosts"
152         if (flag1 < 0 && flag2 < 0 && flag3 < 0 && flag4 < 0) {
153             fprintf(stderr, "The lists do not need to be rebuilt.\n");
154             return(0);
155         }
156     }
157
158     /* build the list of everyone, and store it in a file whose name
159      * corresponds to the empty list.
160      */
161     users = do_everyone();
162
163     fprintf(stderr, "Building specific lists\n");
164     /* now do each of the lists used by an NFS server */
165 ##  range of l is list
166 ##  range of l1 is list
167 ##  range of m is members
168 ##  range of u is users
169     while (sq_get_data(lists, &listname)) {
170         if (strlen(listname) == 0)
171           continue;
172         sprintf(file, "%s/list-%s", NFS_DIR, listname);
173         fd = fopen(file, "w");
174         if (!fd) {
175             fprintf(stderr, "cannot open %s for output\n", file);
176             exit(SMS_OCONFIG);
177         }
178         sq = sq_create();
179         sq_save_data(sq, listname);
180         while (sq_get_data(sq, &lsname)) {
181 ##          repeat retrieve (lname = trim(l.name))
182 ##              where m.list_id = l1.list_id and l1.name = @lsname and
183 ##                    m.member_type = "LIST" and m.member_id = l.list_id {
184               sq_save_unique_string(sq, lname);
185 ##          }
186 ##          repeat retrieve (id = m.member_id)
187 ##              where m.list_id = l1.list_id and l1.name = @lsname and
188 ##                    m.member_type = "USER" {
189               if (u = hash_lookup(users, id)) 
190                 fprintf(fd, "%s\n", u);
191 ##          }
192         }
193         sq_destroy(sq);
194         if (fclose(fd)) {
195             fprintf(stderr, "error closing %s\n", file);
196             exit(SMS_CCONFIG);
197         }
198     }
199     sq_destroy(lists);
200     return(1);
201 ##}
202
203
204 /*  Build the list of everybody. */
205 struct grp {
206     struct grp *next;
207     int id;
208 };
209 struct user {
210     char name[9];
211     int uid;
212     struct grp *lists;
213 };
214
215 struct hash *do_everyone()
216 ##{
217     char buf[BUFSIZ], *l;
218     struct hash *groups, *users;
219     struct user *u;
220     struct grp *g;
221     struct bucket *b, **p;
222 ##  char name[33];
223 ##  int gid, id, lid, maxid, uid;
224     FILE *fd;
225     int i;
226     struct save_queue *sq;
227
228     fprintf(stderr, "Building the list of everybody\n");
229     sprintf(buf, "%s/list-", NFS_DIR);
230     fd = fopen(buf, "w");
231     if (!fd) {
232         fprintf(stderr, "cannot open %s for output\n", buf);
233         exit(SMS_OCONFIG);
234     }
235
236     /* make space for group list */
237     groups = create_hash(15000);
238
239     /* retrieve simple groups */
240 ##  range of l is list
241 ##  retrieve (gid = l.#gid, lid = l.list_id)
242 ##      where l.group != 0 and l.active != 0 {
243       sprintf(buf, ":%d", gid);
244       hash_store(groups, lid, strsave(buf));
245 ##  }
246
247     /* get special cases: lists that aren't groups themselves but are
248      * members of groups. */
249     sq = sq_create();
250 ##  range of m is members
251 ##  retrieve (gid = list.#gid, lid = l.list_id)
252 ##      where l.active != 0 and list.active != 0 and m.member_type = "LIST" and
253 ##              m.member_id = l.list_id and m.list_id = list.list_id and
254 ##              list.group != 0 {
255       if (l = hash_lookup(groups, lid)) {
256           sprintf(buf, "%s:%d", l, gid);
257           free(l);
258       } else {
259           sprintf(buf, ":%d", gid);
260       }
261       hash_store(groups, lid, strsave(buf));
262       sq_save_data(sq, lid);
263 ##  }
264
265     while (sq_get_data(sq, &id)) {
266 ##      repeat retrieve (lid = l.list_id) where l.active != 0 and
267 ##              m.member_type = "LIST" and m.member_id = l.list_id and
268 ##              m.list_id = @id {
269           if (l = hash_lookup(groups, lid)) {
270               sprintf(buf, "%s%s", l, hash_lookup(groups, id));
271               free(l);
272               l = strsave(buf);
273           } else {
274               l = hash_lookup(groups, id);
275           }
276           hash_store(groups, lid, l);
277           sq_save_unique_data(sq, lid);
278 ##      }
279     }
280     sq_destroy(sq);
281
282     /* now do grplists */
283     users = create_hash(10000);
284 ##  range of u is users
285 ##  retrieve (id = u.users_id, name = u.login, uid = u.#uid)
286 ##       where u.status = 1 {
287       u = (struct user *) malloc(sizeof(struct user));
288       strcpy(u->name, strtrim(name));
289       u->uid = uid;
290       u->lists = NULL;
291       hash_store(users, id, u);
292 ##  }
293
294 ##  repeat retrieve (lid = m.list_id, id = m.member_id)
295 ##      where m.member_type = "USER" {
296       if ((u = (struct user *) hash_lookup(users, id)) &&
297           (hash_lookup(groups, lid) != NULL)) {
298           g = (struct grp *) malloc(sizeof(struct grp));
299           g->next = u->lists;
300           u->lists = g;
301           g->id = lid;
302       }
303 ##  }
304
305     for (p = &(users->data[users->size - 1]); p >= users->data; p--) {
306         for (b = *p; b; b = b->next) {
307             i = 0;
308             u = (struct user *)b->data;
309             sprintf(buf, "%s:%d", u->name, u->uid);
310             for (g = u->lists; g; g = g->next)
311               if (l = hash_lookup(groups, g->id)) {
312                   i++;
313                   strcat(buf, l);
314               }
315             /* should free stuff here... */
316             if (i > 1) {
317                 remove_duplicate_groups(buf);
318             }
319             b->data = strsave(buf);
320             fprintf(fd, "%s\n", buf);
321         }
322     }
323
324     fclose(fd);
325     free(groups);
326     return(users);
327 ##}
328
329 #define MAXGROUPS 256
330
331 remove_duplicate_groups(p)
332 register char *p;
333 {
334     register char *pp;
335     char *index();
336     register int *mp;
337     int memory[MAXGROUPS], i, *mg = &memory[MAXGROUPS-1];
338
339     bzero(memory, MAXGROUPS * sizeof(int));
340     p = index(p, ':');
341     p++;
342     while (p = index(p, ':')) {
343         i = atoi(++p);
344         pp = p;
345         for (mp = memory; *mp && mp < mg; mp++)
346           if (*mp == i) {
347               if (p = index(p, ':'))
348                 strcpy(pp, ++p);
349               else {
350                   *(--pp) = 0;
351               }
352               p = pp - 2;
353               break;
354           }
355         *mp = i;
356     }
357 }
358
359
360 /* Now do each of the servers, linking the credentials list file and 
361  * compiling the quota and dirs files.
362  */
363
364 int do_machs(machs)
365 struct save_queue *machs;
366 ##{
367 ##  char *machname, listname[33], dev[33], *device, dir[64], fstype[9];
368 ##  char *filetime;
369 ##  int uid, quota, id, gid, flag1, flag2, flag3, flag4;
370     char file[64], f1[64], f2[64], *cp, *index();
371     int prevuid, quotasum;
372     FILE *fd;
373     struct stat sb;
374     struct save_queue *sq;
375
376 /*
377     sprintf(file, "%s/list-", NFS_DIR);
378     if (stat(file, &sb) == 0) {
379         filetime = ingres_date_and_time(sb.st_mtime);
380  #      retrieve (flag1 = int4(interval("min", tblstats.modtime - filetime)))
381  #              where tblstats.table = "serverhosts"
382  #      retrieve (flag2 = int4(interval("min", tblstats.modtime - filetime)))
383  #              where tblstats.table = "filesys"
384  #      retrieve (flag3 = int4(interval("min", tblstats.modtime - filetime)))
385  #              where tblstats.table = "nfsquota"
386  #      retrieve (flag4 = int4(interval("min", tblstats.modtime - filetime)))
387  #              where tblstats.table = "nfsphys"
388         if (flag1 < 0 && flag2 < 0 && flag3 < 0 && flag4 < 0) {
389             fprintf(stderr, "The machine files do not need to be rebuilt.\n");
390             return(0);
391         }
392     }
393 */
394
395     fprintf(stderr, "Building machine files\n");
396
397 ##  range of s is serverhosts
398 ##  range of m is machine
399 ##  range of n is nfsphys
400 ##  range of q is nfsquota
401 ##  range of f is filesys
402 ##  range of u is users
403 ##  range of l is list
404     while (sq_get_data(machs, &machname)) {
405 ##      repeat retrieve (listname = trim(s.value3))
406 ##          where s.mach_id = m.mach_id and m.name = @machname
407         strtrim(machname);
408         sprintf(f1, "%s/list-%s", NFS_DIR, listname);
409         sprintf(f2, "%s/%s.cred", NFS_DIR, machname);
410         unlink(f2); /* ignore errors on this unlink */
411         if (link(f1, f2)) {
412             fprintf(stderr, "Cannot link %s to %s\n", f1, f2);
413             exit(SMS_OCONFIG);
414         }
415         sq = sq_create();
416 ##      repeat retrieve (dev = trim(n.#device))
417 ##          where n.mach_id = m.mach_id and m.name = @machname {
418           sq_save_data(sq, strsave(dev));
419 ##      }
420         while (sq_get_data(sq, &device)) {
421 ##          repeat retrieve (id = n.nfsphys_id) where n.mach_id = m.mach_id and
422 ##              m.#name = @machname and n.#device = @device
423             while (cp = index(device, '/')) *cp = '@';
424             sprintf(file, "%s/%s.%s.quotas", NFS_DIR, machname, device);
425             fd = fopen(file, "w");
426             if (!fd) {
427                 fprintf(stderr, "cannot open %s for output\n", file);
428                 exit(SMS_OCONFIG);
429             }
430             prevuid = -1;
431             quotasum = 0;
432 ##          repeat retrieve (uid = u.#uid, quota = q.#quota)
433 ##              where q.users_id = u.users_id and q.filsys_id = f.filsys_id and
434 ##                    f.phys_id = @id and u.status != 0 sort by #uid {
435                 if (uid != prevuid) {
436                     if (quotasum)
437                       fprintf(fd, "%d %d\n", prevuid, quotasum);
438                     prevuid = uid;
439                     quotasum = quota;
440                 } else {
441                     quotasum += quota;
442                 }
443 ##          }
444             if (quotasum)
445               fprintf(fd, "%d %d\n", prevuid, quotasum);
446             if (fclose(fd)) {
447                 fprintf(stderr, "error closing %s", file);
448                 exit(SMS_CCONFIG);
449             }
450             sprintf(file, "%s/%s.%s.dirs", NFS_DIR, machname, device);
451             fd = fopen(file, "w");
452             if (!fd) {
453                 fprintf(stderr, "cannot open %s for output\n", file);
454                 exit(SMS_OCONFIG);
455             }
456 ##          repeat retrieve (dir = trim(f.#name), fstype = trim(f.lockertype),
457 ##                           uid = u.#uid, gid = l.#gid)
458 ##              where f.phys_id = @id and f.owner = u.users_id and 
459 ##                    f.owners = l.list_id and f.createflg != 0 {
460               fprintf(fd, "%s %d %d %s\n", dir, uid, gid, fstype);
461 ##          }
462             if (fclose(fd)) {
463                 fprintf(stderr, "error closing %s", file);
464                 exit(SMS_CCONFIG);
465             }
466         }
467         sq_destroy(sq);
468     }
469     return(1);
470 ##}
This page took 0.102032 seconds and 5 git commands to generate.