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]);
48 fprintf(stderr, "No files updated.\n");
49 if (argc == 2 && stat(argv[1], &sb) == 0)
54 sprintf(cmd, "cd %s; cp /u1/sms/bin/install_* .; tar cf %s .", NFS_DIR, argv[1]);
64 * ingerr: (supposedly) called when Ingres indicates an error.
65 * I have not yet been able to get this to work to intercept a
66 * database open error.
68 #define INGRES_DEADLOCK 4700
70 static int ingerr(num)
78 ingres_errno = SMS_DEADLOCK;
81 ingres_errno = SMS_INGRES_ERR;
83 com_err(whoami, SMS_INGRES_ERR, " code %d\n", *num);
84 critical_alert("DCM", "NFS build encountered INGRES ERROR %d", *num);
89 /* Generate the files. Returns zero if nothing changed, non-zero otherwise. */
93 ## char machname[33], listname[33];
94 struct save_queue *machs, *lists;
99 ## range of s is serverhosts
100 ## retrieve (machname = trim(machine.name), listname = trim(s.value3))
101 ## where machine.mach_id = s.mach_id and s.enable != 0 {
102 sq_save_unique_string(machs, strsave(machname));
103 sq_save_unique_string(lists, strsave(listname));
106 changed = do_lists(lists);
107 changed += do_machs(machs);
112 /* Make all of the credentials lists that will be needed. Returns 0 if
113 * no files were actually changed */
116 struct save_queue *lists;
118 struct save_queue *sq, *members;
120 struct hash *users, *do_everyone();
123 ## char *listname, *lsname, lname[33], uname[9], *filetime;
124 ## int uid, id, flag1, flag2, flag3, flag4;
126 sprintf(file, "%s/list-", NFS_DIR);
127 if (stat(file, &sb) == 0) {
128 filetime = ingres_date_and_time(sb.st_mtime);
129 ## retrieve (flag1 = int4(interval("min", tblstats.modtime - filetime)))
130 ## where tblstats.table = "users"
131 ## retrieve (flag2 = int4(interval("min", tblstats.modtime - filetime)))
132 ## where tblstats.table = "list"
133 ## retrieve (flag3 = int4(interval("min", tblstats.modtime - filetime)))
134 ## where tblstats.table = "members"
135 ## retrieve (flag4 = int4(interval("min", tblstats.modtime - filetime)))
136 ## where tblstats.table = "serverhosts"
137 if (flag1 < 0 && flag2 < 0 && flag3 < 0 && flag4 < 0) {
138 fprintf(stderr, "The lists do not need to be rebuilt.\n");
143 /* build the list of everyone, and store it in a file whose name
144 * corresponds to the empty list.
146 users = do_everyone();
148 fprintf(stderr, "Building specific lists\n");
149 /* now do each of the lists used by an NFS server */
150 ## range of l is list
151 ## range of l1 is list
152 ## range of m is members
153 ## range of u is users
154 while (sq_get_data(lists, &listname)) {
155 if (strlen(listname) == 0)
157 sprintf(file, "%s/list-%s", NFS_DIR, listname);
158 fd = fopen(file, "w");
160 fprintf(stderr, "cannot open %s for output\n", file);
164 sq_save_data(sq, listname);
165 members = sq_create();
166 while (sq_get_data(sq, &lsname)) {
167 ## repeat retrieve (lname = trim(l.name))
168 ## where m.list_id = l1.list_id and l1.name = @lsname and
169 ## m.member_type = "LIST" and m.member_id = l.list_id {
170 sq_save_unique_string(sq, strsave(lname));
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 sq_save_unique_data(members, u);
180 while (sq_get_data(members, &lsname))
181 fprintf(fd, "%s\n", lsname);
185 fprintf(stderr, "error closing %s\n", file);
194 /* Build the list of everybody. */
205 struct hash *do_everyone()
207 char buf[BUFSIZ], *l;
208 struct hash *groups, *users;
211 struct bucket *b, **p;
213 ## int gid, id, lid, maxid, uid;
216 struct save_queue *sq;
218 fprintf(stderr, "Building the list of everybody\n");
219 sprintf(buf, "%s/list-", NFS_DIR);
220 fd = fopen(buf, "w");
222 fprintf(stderr, "cannot open %s for output\n", buf);
226 /* make space for group list */
227 groups = create_hash(15000);
229 /* retrieve simple groups */
230 ## range of l is list
231 ## retrieve (gid = l.#gid, lid = l.list_id)
232 ## where l.group != 0 and l.active != 0 {
233 sprintf(buf, ":%d", gid);
234 hash_store(groups, lid, strsave(buf));
237 /* get special cases: lists that aren't groups themselves but are
238 * members of groups. */
240 ## range of m is members
241 ## retrieve (gid = list.#gid, lid = l.list_id)
242 ## where l.active != 0 and list.active != 0 and m.member_type = "LIST" and
243 ## m.member_id = l.list_id and m.list_id = list.list_id and
245 if (l = hash_lookup(groups, lid)) {
246 sprintf(buf, "%s:%d", l, gid);
249 sprintf(buf, ":%d", gid);
251 hash_store(groups, lid, strsave(buf));
252 sq_save_data(sq, lid);
255 while (sq_get_data(sq, &id)) {
256 ## repeat retrieve (lid = l.list_id) where l.active != 0 and
257 ## m.member_type = "LIST" and m.member_id = l.list_id and
259 if (l = hash_lookup(groups, lid)) {
260 sprintf(buf, "%s%s", l, hash_lookup(groups, id));
264 l = hash_lookup(groups, id);
266 hash_store(groups, lid, l);
267 sq_save_unique_data(sq, lid);
272 /* now do grplists */
273 users = create_hash(10000);
274 ## range of u is users
275 ## retrieve (id = u.users_id, name = u.login, uid = u.#uid)
276 ## where u.status = 1 {
277 u = (struct user *) malloc(sizeof(struct user));
278 strcpy(u->name, strtrim(name));
281 hash_store(users, id, u);
284 ## repeat retrieve (lid = m.list_id, id = m.member_id)
285 ## where m.member_type = "USER" {
286 if ((u = (struct user *) hash_lookup(users, id)) &&
287 (hash_lookup(groups, lid) != NULL)) {
288 g = (struct grp *) malloc(sizeof(struct grp));
295 for (p = &(users->data[users->size - 1]); p >= users->data; p--) {
296 for (b = *p; b; b = b->next) {
298 u = (struct user *)b->data;
299 sprintf(buf, "%s:%d", u->name, u->uid);
300 for (g = u->lists; g; g = g->next)
301 if (l = hash_lookup(groups, g->id)) {
305 /* should free stuff here... */
307 remove_duplicate_groups(buf);
309 b->data = strsave(buf);
310 fprintf(fd, "%s\n", buf);
319 #define MAXGROUPS 256
321 remove_duplicate_groups(p)
327 int memory[MAXGROUPS], i, *mg = &memory[MAXGROUPS-1];
329 bzero(memory, MAXGROUPS * sizeof(int));
332 while (p = index(p, ':')) {
335 for (mp = memory; *mp && mp < mg; mp++)
337 if (p = index(p, ':'))
350 /* Now do each of the servers, linking the credentials list file and
351 * compiling the quota and dirs files.
355 struct save_queue *machs;
357 ## char *machname, listname[33], dev[33], *device, dir[64], fstype[9];
359 ## int uid, quota, id, gid, flag1, flag2, flag3, flag4;
360 char file[64], f1[64], f2[64], *cp, *index();
361 int prevuid, quotasum;
364 struct save_queue *sq;
367 sprintf(file, "%s/list-", NFS_DIR);
368 if (stat(file, &sb) == 0) {
369 filetime = ingres_date_and_time(sb.st_mtime);
370 # retrieve (flag1 = int4(interval("min", tblstats.modtime - filetime)))
371 # where tblstats.table = "serverhosts"
372 # retrieve (flag2 = int4(interval("min", tblstats.modtime - filetime)))
373 # where tblstats.table = "filesys"
374 # retrieve (flag3 = int4(interval("min", tblstats.modtime - filetime)))
375 # where tblstats.table = "nfsquota"
376 # retrieve (flag4 = int4(interval("min", tblstats.modtime - filetime)))
377 # where tblstats.table = "nfsphys"
378 if (flag1 < 0 && flag2 < 0 && flag3 < 0 && flag4 < 0) {
379 fprintf(stderr, "The machine files do not need to be rebuilt.\n");
385 fprintf(stderr, "Building machine files\n");
387 ## range of s is serverhosts
388 ## range of m is machine
389 ## range of n is nfsphys
390 ## range of q is nfsquota
391 ## range of f is filesys
392 ## range of u is users
393 ## range of l is list
394 while (sq_get_data(machs, &machname)) {
395 ## repeat retrieve (listname = trim(s.value3))
396 ## where s.mach_id = m.mach_id and m.name = @machname
398 sprintf(f1, "%s/list-%s", NFS_DIR, listname);
399 sprintf(f2, "%s/%s.cred", NFS_DIR, machname);
400 unlink(f2); /* ignore errors on this unlink */
402 fprintf(stderr, "Cannot link %s to %s\n", f1, f2);
406 ## repeat retrieve (dev = trim(n.#device))
407 ## where n.mach_id = m.mach_id and m.name = @machname {
408 sq_save_data(sq, strsave(dev));
410 while (sq_get_data(sq, &device)) {
411 ## repeat retrieve (id = n.nfsphys_id) where n.mach_id = m.mach_id and
412 ## m.#name = @machname and n.#device = @device
413 while (cp = index(device, '/')) *cp = '@';
414 sprintf(file, "%s/%s.%s.quotas", NFS_DIR, machname, device);
415 fd = fopen(file, "w");
417 fprintf(stderr, "cannot open %s for output\n", file);
422 ## repeat retrieve (uid = u.#uid, quota = q.#quota)
423 ## where q.users_id = u.users_id and q.filsys_id = f.filsys_id and
424 ## f.phys_id = @id and u.status != 0 sort by #uid {
425 if (uid != prevuid) {
427 fprintf(fd, "%d %d\n", prevuid, quotasum);
435 fprintf(fd, "%d %d\n", prevuid, quotasum);
437 fprintf(stderr, "error closing %s", file);
440 sprintf(file, "%s/%s.%s.dirs", NFS_DIR, machname, device);
441 fd = fopen(file, "w");
443 fprintf(stderr, "cannot open %s for output\n", file);
446 ## repeat retrieve (dir = trim(f.#name), fstype = trim(f.lockertype),
447 ## uid = u.#uid, gid = l.#gid)
448 ## where f.phys_id = @id and f.owner = u.users_id and
449 ## f.owners = l.list_id and f.createflg != 0 {
450 fprintf(fd, "%s %d %d %s\n", dir, uid, gid, fstype);
453 fprintf(stderr, "error closing %s", file);