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