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))
37 char *whoami = "nfs.gen";
38 char *malloc(), *strsave();
39 char *ingres_date_and_time(), *ingres_time(), *ingres_date();
51 fprintf(stderr, "usage: %s [outfile]\n", argv[0]);
63 fprintf(stderr, "No files updated.\n");
64 if (argc == 2 && stat(argv[1], &sb) == 0)
69 sprintf(cmd, "cd %s; cp /u1/sms/bin/install_* .; tar cf %s .", NFS_DIR, argv[1]);
79 * ingerr: (supposedly) called when Ingres indicates an error.
80 * I have not yet been able to get this to work to intercept a
81 * database open error.
83 #define INGRES_DEADLOCK 4700
85 static int ingerr(num)
93 ingres_errno = SMS_DEADLOCK;
96 ingres_errno = SMS_INGRES_ERR;
98 com_err(whoami, SMS_INGRES_ERR, " code %d\n", *num);
99 critical_alert("DCM", "NFS build encountered INGRES ERROR %d", *num);
104 /* Generate the files. Returns zero if nothing changed, non-zero otherwise. */
108 ## char machname[33], listname[33];
109 struct save_queue *machs, *lists;
114 ## range of s is serverhosts
115 ## retrieve (machname = trim(machine.name), listname = trim(s.value3))
116 ## where machine.mach_id = s.mach_id and s.enable != 0 {
117 sq_save_unique_string(machs, strsave(machname));
118 sq_save_unique_string(lists, strsave(listname));
121 changed = do_lists(lists);
122 changed += do_machs(machs);
127 /* Make all of the credentials lists that will be needed. Returns 0 if
128 * no files were actually changed */
131 struct save_queue *lists;
133 struct save_queue *sq, *members;
135 struct hash *users, *do_everyone();
138 ## char *listname, *lsname, lname[33], uname[9], *filetime;
139 ## int uid, id, flag1, flag2, flag3, flag4;
141 sprintf(file, "%s/list-", NFS_DIR);
142 if (stat(file, &sb) == 0) {
143 filetime = ingres_date_and_time(sb.st_mtime);
144 ## retrieve (flag1 = int4(interval("min", tblstats.modtime - filetime)))
145 ## where tblstats.table = "users"
146 ## retrieve (flag2 = int4(interval("min", tblstats.modtime - filetime)))
147 ## where tblstats.table = "list"
148 ## retrieve (flag3 = int4(interval("min", tblstats.modtime - filetime)))
149 ## where tblstats.table = "members"
150 ## retrieve (flag4 = int4(interval("min", tblstats.modtime - filetime)))
151 ## where tblstats.table = "serverhosts"
152 if (flag1 < 0 && flag2 < 0 && flag3 < 0 && flag4 < 0) {
153 fprintf(stderr, "The lists do not need to be rebuilt.\n");
158 /* build the list of everyone, and store it in a file whose name
159 * corresponds to the empty list.
161 users = do_everyone();
163 fprintf(stderr, "Building specific lists\n");
164 /* now do each of the lists used by an NFS server */
165 ## range of l is list
166 ## range of l1 is list
167 ## range of m is members
168 ## range of u is users
169 while (sq_get_data(lists, &listname)) {
170 if (strlen(listname) == 0)
172 sprintf(file, "%s/list-%s", NFS_DIR, listname);
173 fd = fopen(file, "w");
175 fprintf(stderr, "cannot open %s for output\n", file);
179 sq_save_data(sq, listname);
180 members = sq_create();
181 while (sq_get_data(sq, &lsname)) {
182 ## repeat retrieve (lname = trim(l.name))
183 ## where m.list_id = l1.list_id and l1.name = @lsname and
184 ## m.member_type = "LIST" and m.member_id = l.list_id {
185 sq_save_unique_string(sq, strsave(lname));
187 ## repeat retrieve (id = m.member_id)
188 ## where m.list_id = l1.list_id and l1.name = @lsname and
189 ## m.member_type = "USER" {
190 if (u = hash_lookup(users, id)) {
191 sq_save_unique_data(members, u);
195 while (sq_get_data(members, &lsname))
196 fprintf(fd, "%s\n", lsname);
200 fprintf(stderr, "error closing %s\n", file);
209 /* Build the list of everybody. */
220 struct hash *do_everyone()
222 char buf[BUFSIZ], *l;
223 struct hash *groups, *users;
226 struct bucket *b, **p;
228 ## int gid, id, lid, maxid, uid;
231 struct save_queue *sq;
233 fprintf(stderr, "Building the list of everybody\n");
234 sprintf(buf, "%s/list-", NFS_DIR);
235 fd = fopen(buf, "w");
237 fprintf(stderr, "cannot open %s for output\n", buf);
241 /* make space for group list */
242 groups = create_hash(15000);
244 /* retrieve simple groups */
245 ## range of l is list
246 ## retrieve (gid = l.#gid, lid = l.list_id)
247 ## where l.group != 0 and l.active != 0 {
248 sprintf(buf, ":%d", gid);
249 hash_store(groups, lid, strsave(buf));
252 /* get special cases: lists that aren't groups themselves but are
253 * members of groups. */
255 ## range of m is members
256 ## retrieve (gid = list.#gid, lid = l.list_id)
257 ## where l.active != 0 and list.active != 0 and m.member_type = "LIST" and
258 ## m.member_id = l.list_id and m.list_id = list.list_id and
260 if (l = hash_lookup(groups, lid)) {
261 sprintf(buf, "%s:%d", l, gid);
264 sprintf(buf, ":%d", gid);
266 hash_store(groups, lid, strsave(buf));
267 sq_save_data(sq, lid);
270 while (sq_get_data(sq, &id)) {
271 ## repeat retrieve (lid = l.list_id) where l.active != 0 and
272 ## m.member_type = "LIST" and m.member_id = l.list_id and
274 if (l = hash_lookup(groups, lid)) {
275 sprintf(buf, "%s%s", l, hash_lookup(groups, id));
279 l = hash_lookup(groups, id);
281 hash_store(groups, lid, l);
282 sq_save_unique_data(sq, lid);
287 /* now do grplists */
288 users = create_hash(10000);
289 ## range of u is users
290 ## retrieve (id = u.users_id, name = u.login, uid = u.#uid)
291 ## where u.status = 1 {
292 u = (struct user *) malloc(sizeof(struct user));
293 strcpy(u->name, strtrim(name));
296 hash_store(users, id, u);
299 ## repeat retrieve (lid = m.list_id, id = m.member_id)
300 ## where m.member_type = "USER" {
301 if ((u = (struct user *) hash_lookup(users, id)) &&
302 (hash_lookup(groups, lid) != NULL)) {
303 g = (struct grp *) malloc(sizeof(struct grp));
310 for (p = &(users->data[users->size - 1]); p >= users->data; p--) {
311 for (b = *p; b; b = b->next) {
313 u = (struct user *)b->data;
314 sprintf(buf, "%s:%d", u->name, u->uid);
315 for (g = u->lists; g; g = g->next)
316 if (l = hash_lookup(groups, g->id)) {
320 /* should free stuff here... */
322 remove_duplicate_groups(buf);
324 b->data = strsave(buf);
325 fprintf(fd, "%s\n", buf);
334 #define MAXGROUPS 256
336 remove_duplicate_groups(p)
342 int memory[MAXGROUPS], i, *mg = &memory[MAXGROUPS-1];
344 bzero(memory, MAXGROUPS * sizeof(int));
347 while (p = index(p, ':')) {
350 for (mp = memory; *mp && mp < mg; mp++)
352 if (p = index(p, ':'))
365 /* Now do each of the servers, linking the credentials list file and
366 * compiling the quota and dirs files.
370 struct save_queue *machs;
372 ## char *machname, listname[33], dev[33], *device, dir[64], fstype[9];
374 ## int uid, quota, id, gid, flag1, flag2, flag3, flag4;
375 char file[64], f1[64], f2[64], *cp, *index();
376 int prevuid, quotasum;
379 struct save_queue *sq;
382 sprintf(file, "%s/list-", NFS_DIR);
383 if (stat(file, &sb) == 0) {
384 filetime = ingres_date_and_time(sb.st_mtime);
385 # retrieve (flag1 = int4(interval("min", tblstats.modtime - filetime)))
386 # where tblstats.table = "serverhosts"
387 # retrieve (flag2 = int4(interval("min", tblstats.modtime - filetime)))
388 # where tblstats.table = "filesys"
389 # retrieve (flag3 = int4(interval("min", tblstats.modtime - filetime)))
390 # where tblstats.table = "nfsquota"
391 # retrieve (flag4 = int4(interval("min", tblstats.modtime - filetime)))
392 # where tblstats.table = "nfsphys"
393 if (flag1 < 0 && flag2 < 0 && flag3 < 0 && flag4 < 0) {
394 fprintf(stderr, "The machine files do not need to be rebuilt.\n");
400 fprintf(stderr, "Building machine files\n");
402 ## range of s is serverhosts
403 ## range of m is machine
404 ## range of n is nfsphys
405 ## range of q is nfsquota
406 ## range of f is filesys
407 ## range of u is users
408 ## range of l is list
409 while (sq_get_data(machs, &machname)) {
410 ## repeat retrieve (listname = trim(s.value3))
411 ## where s.mach_id = m.mach_id and m.name = @machname
413 sprintf(f1, "%s/list-%s", NFS_DIR, listname);
414 sprintf(f2, "%s/%s.cred", NFS_DIR, machname);
415 unlink(f2); /* ignore errors on this unlink */
417 fprintf(stderr, "Cannot link %s to %s\n", f1, f2);
421 ## repeat retrieve (dev = trim(n.#device))
422 ## where n.mach_id = m.mach_id and m.name = @machname {
423 sq_save_data(sq, strsave(dev));
425 while (sq_get_data(sq, &device)) {
426 ## repeat retrieve (id = n.nfsphys_id) where n.mach_id = m.mach_id and
427 ## m.#name = @machname and n.#device = @device
428 while (cp = index(device, '/')) *cp = '@';
429 sprintf(file, "%s/%s.%s.quotas", NFS_DIR, machname, device);
430 fd = fopen(file, "w");
432 fprintf(stderr, "cannot open %s for output\n", file);
437 ## repeat retrieve (uid = u.#uid, quota = q.#quota)
438 ## where q.users_id = u.users_id and q.filsys_id = f.filsys_id and
439 ## f.phys_id = @id and u.status != 0 sort by #uid {
440 if (uid != prevuid) {
442 fprintf(fd, "%d %d\n", prevuid, quotasum);
450 fprintf(fd, "%d %d\n", prevuid, quotasum);
452 fprintf(stderr, "error closing %s", file);
455 sprintf(file, "%s/%s.%s.dirs", NFS_DIR, machname, device);
456 fd = fopen(file, "w");
458 fprintf(stderr, "cannot open %s for output\n", file);
461 ## repeat retrieve (dir = trim(f.#name), fstype = trim(f.lockertype),
462 ## uid = u.#uid, gid = l.#gid)
463 ## where f.phys_id = @id and f.owner = u.users_id and
464 ## f.owners = l.list_id and f.createflg != 0 {
465 fprintf(fd, "%s %d %d %s\n", dir, uid, gid, fstype);
468 fprintf(stderr, "error closing %s", file);