3 * This generates the files necessary to load an nfs server.
5 * (c) Copyright 1988 by the Massachusetts Institute of Technology.
6 * For copying and distribution information, please see the file
10 #include <mit-copyright.h>
14 #include <sys/types.h>
18 #define NFS_DIR "/u1/sms/dcm/nfs"
20 #define min(x,y) ((x) < (y) ? (x) : (y))
22 char *whoami = "nfs.gen";
23 char *malloc(), *strsave();
24 char *ingres_date_and_time(), *ingres_time(), *ingres_date();
36 fprintf(stderr, "usage: %s [outfile]\n", argv[0]);
42 ## set lockmode session where level = table
49 fprintf(stderr, "No files updated.\n");
50 if (argc == 2 && stat(argv[1], &sb) == 0)
55 sprintf(cmd, "cd %s; cp /u1/sms/bin/install_* .; tar cf %s .", NFS_DIR, argv[1]);
65 * ingerr: (supposedly) called when Ingres indicates an error.
66 * I have not yet been able to get this to work to intercept a
67 * database open error.
69 #define INGRES_DEADLOCK 4700
71 static int ingerr(num)
79 ingres_errno = SMS_DEADLOCK;
82 ingres_errno = SMS_INGRES_ERR;
84 com_err(whoami, SMS_INGRES_ERR, " code %d\n", *num);
85 critical_alert("DCM", "NFS build encountered INGRES ERROR %d", *num);
90 /* Generate the files. Returns zero if nothing changed, non-zero otherwise. */
94 ## char machname[33], listname[33];
95 struct save_queue *machs, *lists;
100 ## range of s is serverhosts
101 ## retrieve (machname = trim(machine.name), listname = trim(s.value3))
102 ## where machine.mach_id = s.mach_id and s.enable != 0 {
103 sq_save_unique_string(machs, strsave(machname));
104 sq_save_unique_string(lists, strsave(listname));
107 changed = do_lists(lists);
108 changed += do_machs(machs);
113 /* Make all of the credentials lists that will be needed. Returns 0 if
114 * no files were actually changed */
117 struct save_queue *lists;
119 struct save_queue *sq, *members;
121 struct hash *users, *do_everyone();
124 ## char *listname, *lsname, lname[33], uname[9], *filetime;
125 ## int uid, id, flag1, flag2, flag3, flag4;
127 sprintf(file, "%s/list-", NFS_DIR);
128 if (stat(file, &sb) == 0) {
129 filetime = ingres_date_and_time(sb.st_mtime);
130 ## retrieve (flag1 = int4(interval("min", tblstats.modtime - filetime)))
131 ## where tblstats.table = "users"
132 ## retrieve (flag2 = int4(interval("min", tblstats.modtime - filetime)))
133 ## where tblstats.table = "list"
134 ## retrieve (flag3 = int4(interval("min", tblstats.modtime - filetime)))
135 ## where tblstats.table = "members"
136 ## retrieve (flag4 = int4(interval("min", tblstats.modtime - filetime)))
137 ## where tblstats.table = "serverhosts"
138 if (flag1 < 0 && flag2 < 0 && flag3 < 0 && flag4 < 0) {
139 fprintf(stderr, "The lists do not need to be rebuilt.\n");
146 ## retrieve (lname = list.modtime) where list.list_id = 0
147 ## retrieve (lname = users.modtime) where users.users_id = 0
149 /* build the list of everyone, and store it in a file whose name
150 * corresponds to the empty list.
152 users = do_everyone();
154 fprintf(stderr, "Building specific lists\n");
155 /* now do each of the lists used by an NFS server */
156 ## range of l is list
157 ## range of l1 is list
158 ## range of m is members
159 ## range of u is users
160 while (sq_get_data(lists, &listname)) {
161 if (strlen(listname) == 0)
163 sprintf(file, "%s/list-%s", NFS_DIR, listname);
164 fd = fopen(file, "w");
166 fprintf(stderr, "cannot open %s for output\n", file);
170 sq_save_data(sq, listname);
171 members = sq_create();
172 while (sq_get_data(sq, &lsname)) {
173 ## repeat retrieve (lname = trim(l.name))
174 ## where m.list_id = l1.list_id and l1.name = @lsname and
175 ## m.member_type = "LIST" and m.member_id = l.list_id {
176 sq_save_unique_string(sq, strsave(lname));
178 ## repeat retrieve (id = m.member_id)
179 ## where m.list_id = l1.list_id and l1.name = @lsname and
180 ## m.member_type = "USER" {
181 if (u = hash_lookup(users, id)) {
182 sq_save_unique_data(members, u);
186 while (sq_get_data(members, &lsname))
187 fprintf(fd, "%s\n", lsname);
188 /* test to see if this speeds things up; don't free anything!
192 fprintf(stderr, "error closing %s\n", file);
196 /* don't free here either
204 /* Build the list of everybody. */
215 struct hash *do_everyone()
217 char buf[BUFSIZ], *l;
218 struct hash *groups, *users;
221 struct bucket *b, **p;
223 ## int gid, id, lid, maxid, uid;
226 struct save_queue *sq;
228 fprintf(stderr, "Building the list of everybody\n");
229 sprintf(buf, "%s/list-", NFS_DIR);
230 fd = fopen(buf, "w");
232 fprintf(stderr, "cannot open %s for output\n", buf);
236 /* make space for group list */
237 groups = create_hash(15000);
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));
247 /* get special cases: lists that aren't groups themselves but are
248 * members of groups. */
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
255 if (l = hash_lookup(groups, lid)) {
256 sprintf(buf, "%s:%d", l, gid);
259 sprintf(buf, ":%d", gid);
261 hash_store(groups, lid, strsave(buf));
262 sq_save_data(sq, lid);
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
269 if (l = hash_lookup(groups, lid)) {
270 sprintf(buf, "%s%s", l, hash_lookup(groups, id));
274 l = hash_lookup(groups, id);
276 hash_store(groups, lid, l);
277 sq_save_unique_data(sq, lid);
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));
291 hash_store(users, id, u);
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));
305 for (p = &(users->data[users->size - 1]); p >= users->data; p--) {
306 for (b = *p; b; b = b->next) {
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)) {
315 /* should free stuff here... */
317 remove_duplicate_groups(buf);
319 b->data = strsave(buf);
320 fprintf(fd, "%s\n", buf);
329 #define MAXGROUPS 256
331 remove_duplicate_groups(p)
337 int memory[MAXGROUPS], i, *mg = &memory[MAXGROUPS-1];
339 bzero(memory, MAXGROUPS * sizeof(int));
342 while (p = index(p, ':')) {
345 for (mp = memory; *mp && mp < mg; mp++)
347 if (p = index(p, ':'))
360 /* Now do each of the servers, linking the credentials list file and
361 * compiling the quota and dirs files.
365 struct save_queue *machs;
367 ## char *machname, listname[33], dev[33], *device, dir[64], fstype[9];
369 ## int uid, quota, id, gid, flag1, flag2, flag3, flag4;
370 char file[64], f1[64], f2[64], *cp, *index();
371 int prevuid, quotasum;
374 struct save_queue *sq;
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");
395 fprintf(stderr, "Building machine files\n");
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 while (sq_get_data(machs, &machname)) {
406 ## repeat retrieve (listname = trim(s.value3))
407 ## where s.mach_id = m.mach_id and m.name = @machname
409 sprintf(f1, "%s/list-%s", NFS_DIR, listname);
410 sprintf(f2, "%s/%s.cred", NFS_DIR, machname);
411 unlink(f2); /* ignore errors on this unlink */
413 fprintf(stderr, "Cannot link %s to %s\n", f1, f2);
417 ## repeat retrieve (dev = trim(n.#device))
418 ## where n.mach_id = m.mach_id and m.name = @machname {
419 sq_save_data(sq, strsave(dev));
421 while (sq_get_data(sq, &device)) {
422 ## repeat retrieve (id = n.nfsphys_id) where n.mach_id = m.mach_id and
423 ## m.#name = @machname and n.#device = @device
424 while (cp = index(device, '/')) *cp = '@';
425 sprintf(file, "%s/%s.%s.quotas", NFS_DIR, machname, device);
426 fd = fopen(file, "w");
428 fprintf(stderr, "cannot open %s for output\n", file);
433 ## repeat retrieve (uid = u.#uid, quota = q.#quota)
434 ## where q.users_id = u.users_id and q.filsys_id = f.filsys_id and
435 ## f.phys_id = @id and u.status != 0 sort by #uid {
436 if (uid != prevuid) {
438 fprintf(fd, "%d %d\n", prevuid, quotasum);
446 fprintf(fd, "%d %d\n", prevuid, quotasum);
448 fprintf(stderr, "error closing %s", file);
451 sprintf(file, "%s/%s.%s.dirs", NFS_DIR, machname, device);
452 fd = fopen(file, "w");
454 fprintf(stderr, "cannot open %s for output\n", file);
457 ## repeat retrieve (dir = trim(f.#name), fstype = trim(f.lockertype),
458 ## uid = u.#uid, gid = l.#gid)
459 ## where f.phys_id = @id and f.owner = u.users_id and
460 ## f.owners = l.list_id and f.createflg != 0 {
461 fprintf(fd, "%s %d %d %s\n", dir, uid, gid, fstype);
464 fprintf(stderr, "error closing %s", file);