]> andersk Git - moira.git/blob - gen/nfs.qc
49a3ac879b33b1983a9108a473c44e3d6b78d6c4
[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     initialize_sms_error_table();
43 ##  ingres sms
44 ##  set lockmode session where level = table
45
46     changed = do_nfs();
47
48 ##  exit
49
50     if (!changed) {
51         fprintf(stderr, "No files updated.\n");
52         if (argc == 2 && stat(argv[1], &sb) == 0)
53           exit(SMS_NO_CHANGE);
54     }
55
56     if (argc == 2) {
57         sprintf(cmd, "cd %s; cp /u1/sms/nfs/* .; tar cf %s .", NFS_DIR, argv[1]);
58         if (system(cmd))
59           exit(SMS_TAR_FAIL);
60     }
61
62     exit(SMS_SUCCESS);
63 }
64
65
66 /*
67  * ingerr: (supposedly) called when Ingres indicates an error.
68  * I have not yet been able to get this to work to intercept a
69  * database open error.
70  */
71 #define INGRES_DEADLOCK 4700
72
73 static int ingerr(num)
74     int *num;
75 {
76     char buf[256];
77     int ingres_errno;
78
79     switch (*num) {
80     case INGRES_DEADLOCK:
81         ingres_errno = SMS_DEADLOCK;
82         break;
83     default:
84         ingres_errno = SMS_INGRES_ERR;
85     }
86     com_err(whoami, SMS_INGRES_ERR, " code %d\n", *num);
87     critical_alert("DCM", "NFS build encountered INGRES ERROR %d", *num);
88     exit(ingres_errno);
89 }
90
91
92 /* Generate the files.  Returns zero if nothing changed, non-zero otherwise. */
93
94 int do_nfs()
95 ##{
96 ##  char machname[33], listname[33];
97     struct save_queue *machs, *lists;
98     int changed;
99
100     machs = sq_create();
101     lists = sq_create();
102 ##  range of s is serverhosts
103 ##  retrieve (machname = trim(machine.name), listname = trim(s.value3))
104 ##      where machine.mach_id = s.mach_id and s.enable != 0 {
105       sq_save_unique_string(machs, strsave(machname));
106       sq_save_unique_string(lists, strsave(listname));
107 ##  }
108
109     changed = do_lists(lists);
110     changed += do_machs(machs);
111     return(changed);
112 ##}
113
114
115 /* Make all of the credentials lists that will be needed.  Returns 0 if
116  * no files were actually changed */
117
118 int do_lists(lists)
119 struct save_queue *lists;
120 ##{
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 = "imembers"
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 imembers
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 ##      repeat retrieve (id = m.member_id)
171 ##          where m.list_id = l1.list_id and l1.name = @lsname and
172 ##          m.member_type = "USER" {
173             if (u = hash_lookup(users, id))
174               fprintf(fd, "%s\n", u);
175 ##      }
176         if (fclose(fd)) {
177             fprintf(stderr, "error closing %s\n", file);
178             exit(SMS_CCONFIG);
179         }
180     }
181 /* don't free here either
182     sq_destroy(lists);
183  */
184 ##  end transaction
185     return(1);
186 ##}
187
188
189 /*  Build the list of everybody. */
190 struct grp {
191     struct grp *next;
192     int id;
193 };
194 struct user {
195     char name[9];
196     int uid;
197     struct grp *lists;
198 };
199
200 struct hash *do_everyone()
201 ##{
202     char buf[BUFSIZ], *l;
203     struct hash *groups, *users;
204     struct user *u;
205     struct grp *g;
206     struct bucket *b, **p;
207 ##  char name[33];
208 ##  int gid, id, lid, maxid, uid;
209     FILE *fd;
210     int i;
211     struct save_queue *sq;
212
213     fprintf(stderr, "Building the list of everybody\n");
214     sprintf(buf, "%s/list-", NFS_DIR);
215     fd = fopen(buf, "w");
216     if (!fd) {
217         fprintf(stderr, "cannot open %s for output\n", buf);
218         exit(SMS_OCONFIG);
219     }
220
221     /* make space for group list */
222     groups = create_hash(15000);
223
224     /* retrieve simple groups */
225 ##  range of l is list
226 ##  retrieve (gid = l.#gid, lid = l.list_id)
227 ##      where l.group != 0 and l.active != 0 {
228       sprintf(buf, ":%d", gid);
229       hash_store(groups, lid, strsave(buf));
230 ##  }
231
232     /* now do grplists */
233     users = create_hash(10000);
234 ##  range of u is users
235 ##  retrieve (id = u.users_id, name = u.login, uid = u.#uid)
236 ##       where u.status = 1 {
237       u = (struct user *) malloc(sizeof(struct user));
238       strcpy(u->name, strtrim(name));
239       u->uid = uid;
240       u->lists = NULL;
241       hash_store(users, id, u);
242 ##  }
243
244 ##  range of m is imembers
245 ##  retrieve (lid = m.list_id, id = m.member_id) where m.member_type = "USER" {
246       if ((u = (struct user *) hash_lookup(users, id)) &&
247           (hash_lookup(groups, lid) != NULL)) {
248           g = (struct grp *) malloc(sizeof(struct grp));
249           g->next = u->lists;
250           u->lists = g;
251           g->id = lid;
252       }
253 ##  }
254
255     for (p = &(users->data[users->size - 1]); p >= users->data; p--) {
256         for (b = *p; b; b = b->next) {
257             i = 0;
258             u = (struct user *)b->data;
259             sprintf(buf, "%s:%d", u->name, u->uid);
260             for (g = u->lists; g; g = g->next)
261               if (l = hash_lookup(groups, g->id)) {
262                   i++;
263                   strcat(buf, l);
264               }
265             /* should free stuff here...
266             if (i > 1) {
267                 remove_duplicate_groups(buf);
268             } */
269             b->data = strsave(buf);
270             fprintf(fd, "%s\n", buf);
271         }
272     }
273
274     fclose(fd);
275     free(groups);
276     return(users);
277 ##}
278
279 #define MAXGROUPS 256
280
281 remove_duplicate_groups(p)
282 register char *p;
283 {
284     register char *pp;
285     char *index();
286     register int *mp;
287     int memory[MAXGROUPS], i, *mg = &memory[MAXGROUPS-1];
288
289     bzero(memory, MAXGROUPS * sizeof(int));
290     p = index(p, ':');
291     p++;
292     while (p = index(p, ':')) {
293         i = atoi(++p);
294         pp = p;
295         for (mp = memory; *mp && mp < mg; mp++)
296           if (*mp == i) {
297               if (p = index(p, ':'))
298                 strcpy(pp, ++p);
299               else {
300                   *(--pp) = 0;
301               }
302               p = pp - 2;
303               break;
304           }
305         *mp = i;
306     }
307 }
308
309
310 /* Now do each of the servers, linking the credentials list file and 
311  * compiling the quota and dirs files.
312  */
313
314 int do_machs(machs)
315 struct save_queue *machs;
316 ##{
317 ##  char *machname, listname[33], dev[33], *device, dir[64], fstype[9];
318 ##  char *filetime;
319 ##  int uid, quota, id, gid, flag1, flag2, flag3, flag4;
320     char file[64], f1[64], f2[64], *cp, *index();
321     int prevuid, quotasum;
322     FILE *fd;
323     struct stat sb;
324     struct save_queue *sq;
325
326 /*
327     sprintf(file, "%s/list-", NFS_DIR);
328     if (stat(file, &sb) == 0) {
329         filetime = ingres_date_and_time(sb.st_mtime);
330  #      retrieve (flag1 = int4(interval("min", tblstats.modtime - filetime)))
331  #              where tblstats.table = "serverhosts"
332  #      retrieve (flag2 = int4(interval("min", tblstats.modtime - filetime)))
333  #              where tblstats.table = "filesys"
334  #      retrieve (flag3 = int4(interval("min", tblstats.modtime - filetime)))
335  #              where tblstats.table = "nfsquota"
336  #      retrieve (flag4 = int4(interval("min", tblstats.modtime - filetime)))
337  #              where tblstats.table = "nfsphys"
338         if (flag1 < 0 && flag2 < 0 && flag3 < 0 && flag4 < 0) {
339             fprintf(stderr, "The machine files do not need to be rebuilt.\n");
340             return(0);
341         }
342     }
343 */
344
345     fprintf(stderr, "Building machine files\n");
346
347 ##  range of s is serverhosts
348 ##  range of m is machine
349 ##  range of n is nfsphys
350 ##  range of q is nfsquota
351 ##  range of f is filesys
352 ##  range of u is users
353 ##  range of l is list
354
355 /* acquire locks on users, lists, machines & filesystems */
356 ##  begin transaction
357 ##  retrieve (listname = u.modtime) where u.users_id = 0
358 ##  retrieve (listname = l.modtime) where l.list_id = 0
359 ##  retrieve (listname = m.modtime) where m.mach_id = 0
360 ##  retrieve (listname = f.modtime) where f.filsys_id = 0
361
362     while (sq_get_data(machs, &machname)) {
363 ##      repeat retrieve (listname = trim(s.value3))
364 ##          where s.mach_id = m.mach_id and m.name = @machname
365         strtrim(machname);
366         sprintf(f1, "%s/list-%s", NFS_DIR, listname);
367         sprintf(f2, "%s/%s.cred", NFS_DIR, machname);
368         unlink(f2); /* ignore errors on this unlink */
369         if (link(f1, f2)) {
370             fprintf(stderr, "Cannot link %s to %s\n", f1, f2);
371             exit(SMS_OCONFIG);
372         }
373         sq = sq_create();
374 ##      repeat retrieve (dev = trim(n.#device))
375 ##          where n.mach_id = m.mach_id and m.name = @machname {
376           sq_save_data(sq, strsave(dev));
377 ##      }
378         while (sq_get_data(sq, &device)) {
379 ##          repeat retrieve (id = n.nfsphys_id) where n.mach_id = m.mach_id and
380 ##              m.#name = @machname and n.#device = @device
381             while (cp = index(device, '/')) *cp = '@';
382             sprintf(file, "%s/%s.%s.quotas", NFS_DIR, machname, device);
383             fd = fopen(file, "w");
384             if (!fd) {
385                 fprintf(stderr, "cannot open %s for output\n", file);
386                 exit(SMS_OCONFIG);
387             }
388             prevuid = -1;
389             quotasum = 0;
390 ##          repeat retrieve (uid = u.#uid, quota = q.#quota)
391 ##              where q.users_id = u.users_id and q.filsys_id = f.filsys_id and
392 ##                    f.phys_id = @id and u.status != 0 sort by #uid {
393                 if (uid != prevuid) {
394                     if (quotasum)
395                       fprintf(fd, "%d %d\n", prevuid, quotasum);
396                     prevuid = uid;
397                     quotasum = quota;
398                 } else {
399                     quotasum += quota;
400                 }
401 ##          }
402             if (quotasum)
403               fprintf(fd, "%d %d\n", prevuid, quotasum);
404             if (fclose(fd)) {
405                 fprintf(stderr, "error closing %s", file);
406                 exit(SMS_CCONFIG);
407             }
408             sprintf(file, "%s/%s.%s.dirs", NFS_DIR, machname, device);
409             fd = fopen(file, "w");
410             if (!fd) {
411                 fprintf(stderr, "cannot open %s for output\n", file);
412                 exit(SMS_OCONFIG);
413             }
414 ##          repeat retrieve (dir = trim(f.#name), fstype = trim(f.lockertype),
415 ##                           uid = u.#uid, gid = l.#gid)
416 ##              where f.phys_id = @id and f.owner = u.users_id and 
417 ##                    f.owners = l.list_id and f.createflg != 0 {
418               fprintf(fd, "%s %d %d %s\n", dir, uid, gid, fstype);
419 ##          }
420             if (fclose(fd)) {
421                 fprintf(stderr, "error closing %s", file);
422                 exit(SMS_CCONFIG);
423             }
424         }
425         sq_destroy(sq);
426     }
427 ##  end transaction
428     return(1);
429 ##}
This page took 0.063935 seconds and 3 git commands to generate.