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