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>
19 #define NFS_DIR "/u1/sms/dcm/nfs"
21 #define min(x,y) ((x) < (y) ? (x) : (y))
23 char *whoami = "nfs.gen";
24 char *malloc(), *strsave();
25 char *ingres_date_and_time(), *ingres_time(), *ingres_date();
37 fprintf(stderr, "usage: %s [outfile]\n", argv[0]);
43 ## set lockmode session where level = table
50 fprintf(stderr, "No files updated.\n");
51 if (argc == 2 && stat(argv[1], &sb) == 0)
56 sprintf(cmd, "cd %s; cp /u1/sms/nfs/* .; tar cf %s .", NFS_DIR, argv[1]);
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.
70 #define INGRES_DEADLOCK 4700
72 static int ingerr(num)
80 ingres_errno = SMS_DEADLOCK;
83 ingres_errno = SMS_INGRES_ERR;
85 com_err(whoami, SMS_INGRES_ERR, " code %d\n", *num);
86 critical_alert("DCM", "NFS build encountered INGRES ERROR %d", *num);
91 /* Generate the files. Returns zero if nothing changed, non-zero otherwise. */
95 ## char machname[33], listname[33];
96 struct save_queue *machs, *lists;
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));
108 changed = do_lists(lists);
109 changed += do_machs(machs);
114 /* Make all of the credentials lists that will be needed. Returns 0 if
115 * no files were actually changed */
118 struct save_queue *lists;
120 struct save_queue *sq, *members;
122 struct hash *users, *do_everyone();
125 ## char *listname, *lsname, lname[33], uname[9], *filetime;
126 ## int uid, id, flag1, flag2, flag3, flag4;
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");
147 ## retrieve (lname = list.modtime) where list.list_id = 0
148 ## retrieve (lname = users.modtime) where users.users_id = 0
150 /* build the list of everyone, and store it in a file whose name
151 * corresponds to the empty list.
153 users = do_everyone();
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)
164 sprintf(file, "%s/list-%s", NFS_DIR, listname);
165 fd = fopen(file, "w");
167 fprintf(stderr, "cannot open %s for output\n", file);
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));
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);
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!
193 fprintf(stderr, "error closing %s\n", file);
197 /* don't free here either
205 /* Build the list of everybody. */
216 struct hash *do_everyone()
218 char buf[BUFSIZ], *l;
219 struct hash *groups, *users;
222 struct bucket *b, **p;
224 ## int gid, id, lid, maxid, uid;
227 struct save_queue *sq;
229 fprintf(stderr, "Building the list of everybody\n");
230 sprintf(buf, "%s/list-", NFS_DIR);
231 fd = fopen(buf, "w");
233 fprintf(stderr, "cannot open %s for output\n", buf);
237 /* make space for group list */
238 groups = create_hash(15000);
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));
248 /* get special cases: lists that aren't groups themselves but are
249 * members of groups. */
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
256 if (l = hash_lookup(groups, lid)) {
257 sprintf(buf, "%s:%d", l, gid);
260 sprintf(buf, ":%d", gid);
262 hash_store(groups, lid, strsave(buf));
263 sq_save_data(sq, lid);
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
270 if (l = hash_lookup(groups, lid)) {
271 sprintf(buf, "%s%s", l, hash_lookup(groups, id));
275 l = hash_lookup(groups, id);
277 hash_store(groups, lid, l);
278 sq_save_unique_data(sq, lid);
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));
292 hash_store(users, id, u);
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));
306 for (p = &(users->data[users->size - 1]); p >= users->data; p--) {
307 for (b = *p; b; b = b->next) {
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)) {
316 /* should free stuff here... */
318 remove_duplicate_groups(buf);
320 b->data = strsave(buf);
321 fprintf(fd, "%s\n", buf);
330 #define MAXGROUPS 256
332 remove_duplicate_groups(p)
338 int memory[MAXGROUPS], i, *mg = &memory[MAXGROUPS-1];
340 bzero(memory, MAXGROUPS * sizeof(int));
343 while (p = index(p, ':')) {
346 for (mp = memory; *mp && mp < mg; mp++)
348 if (p = index(p, ':'))
361 /* Now do each of the servers, linking the credentials list file and
362 * compiling the quota and dirs files.
366 struct save_queue *machs;
368 ## char *machname, listname[33], dev[33], *device, dir[64], fstype[9];
370 ## int uid, quota, id, gid, flag1, flag2, flag3, flag4;
371 char file[64], f1[64], f2[64], *cp, *index();
372 int prevuid, quotasum;
375 struct save_queue *sq;
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");
396 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
406 /* acquire locks on users, lists, machines & filesystems */
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
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
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 */
421 fprintf(stderr, "Cannot link %s to %s\n", f1, f2);
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));
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");
436 fprintf(stderr, "cannot open %s for output\n", file);
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) {
446 fprintf(fd, "%d %d\n", prevuid, quotasum);
454 fprintf(fd, "%d %d\n", prevuid, quotasum);
456 fprintf(stderr, "error closing %s", file);
459 sprintf(file, "%s/%s.%s.dirs", NFS_DIR, machname, device);
460 fd = fopen(file, "w");
462 fprintf(stderr, "cannot open %s for output\n", file);
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);
472 fprintf(stderr, "error closing %s", file);