3 * This generates the files necessary to load an nfs server.
5 * (c) Copyright 1988, 1990 by the Massachusetts Institute of Technology.
6 * For copying and distribution information, please see the file
10 #include <mit-copyright.h>
15 #include <moira_site.h>
16 #include <sys/types.h>
19 EXEC SQL INCLUDE sqlca;
22 #define min(x, y) ((x) < (y) ? (x) : (y))
24 char *whoami = "nfs.gen";
25 char *db = "moira/moira";
28 struct hash *users, *groups;
31 int main(int argc, char **argv)
39 fprintf(stderr, "usage: %s [outfile]\n", argv[0]);
43 initialize_sms_error_table();
44 sprintf(nfs_dir, "%s/nfs", DCM_DIR);
54 fprintf(stderr, "No files updated.\n");
55 if (argc == 2 && stat(argv[1], &sb) == 0)
61 sprintf(cmd, "cd %s; cp %s/nfs/* .; tar cf %s .",
62 nfs_dir, SMS_DIR, argv[1]);
71 /* Generate the files. Returns zero if nothing changed, non-zero otherwise. */
75 EXEC SQL BEGIN DECLARE SECTION;
76 char machname[41], listname[33];
77 EXEC SQL END DECLARE SECTION;
78 struct save_queue *machs, *lists;
84 /* The following is declarative, not executed,
85 * and so is dependent on where it is in the file,
86 * not in the order of execution of statements.
88 EXEC SQL WHENEVER SQLERROR GOTO sqlerr;
90 EXEC SQL DECLARE s_cursor CURSOR FOR
91 SELECT m.name, s.value3
92 FROM machine m, serverhosts s
93 WHERE m.mach_id = s.mach_id AND s.service = 'NFS' AND s.enable != 0;
94 EXEC SQL OPEN s_cursor;
97 EXEC SQL FETCH s_cursor INTO :machname, :listname;
100 sq_save_unique_string(machs, strsave(strtrim(machname)));
101 sq_save_unique_string(lists, strsave(strtrim(listname)));
103 EXEC SQL CLOSE s_cursor;
105 changed = do_lists(lists);
107 changed += do_machs(machs);
110 db_error(sqlca.sqlcode);
115 /* Make all of the credentials lists that will be needed. Returns 0 if
116 * no files were actually changed
119 int do_lists(struct save_queue *lists)
124 EXEC SQL BEGIN DECLARE SECTION;
125 char *listname, *lsname, lname[33], uname[9];
126 int uid, id, flag1, flag2, flag3, flag4;
127 EXEC SQL END DECLARE SECTION;
129 sprintf(file, "%s/list-", nfs_dir);
131 if (stat(file, &sb) == 0)
133 if ((ModDiff (&flag1, "users", sb.st_mtime)) ||
134 (ModDiff (&flag2, "list", sb.st_mtime)) ||
135 (ModDiff (&flag3, "imembers", sb.st_mtime)) ||
136 (ModDiff (&flag4, "serverhosts", sb.st_mtime)))
138 if (flag1 < 0 && flag2 < 0 && flag3 < 0 && flag4 < 0)
140 fprintf(stderr, "The lists do not need to be rebuilt.\n");
146 /* build the list of everyone, and store it in a file whose name
147 * corresponds to the empty list.
151 fprintf(stderr, "Building specific lists\n");
152 /* now do each of the lists used by an NFS server */
154 while (sq_get_data(lists, &listname))
156 if (strlen(listname) == 0)
158 sprintf(file, "%s/list-%s", nfs_dir, listname);
159 fd = fopen(file, "w");
162 fprintf(stderr, "cannot open %s for output\n", file);
166 EXEC SQL DECLARE m_cursor CURSOR FOR
168 FROM imembers m, list l
169 WHERE m.list_id = l.list_id AND l.name = :listname
170 AND m.member_type = 'USER'
172 EXEC SQL OPEN m_cursor;
175 EXEC SQL FETCH m_cursor INTO :id;
178 if ((u = hash_lookup(users, id)))
179 fprintf(fd, "%s\n", u);
181 EXEC SQL CLOSE m_cursor;
182 if (fclose(fd) == EOF)
184 fprintf(stderr, "error closing %s\n", file);
188 /* don't free here either
193 db_error(sqlca.sqlcode);
198 /* Build the list of everybody. */
212 char buf[BUFSIZ], *l;
215 struct bucket *b, **p;
216 EXEC SQL BEGIN DECLARE SECTION;
218 int gid, id, lid, maxid, uid;
219 EXEC SQL END DECLARE SECTION;
222 struct save_queue *sq;
224 fprintf(stderr, "Building the list of everybody\n");
225 sprintf(buf, "%s/list-", nfs_dir);
226 fd = fopen(buf, "w");
229 fprintf(stderr, "cannot open %s for output\n", buf);
233 /* make space for group list */
234 groups = create_hash(15000);
236 /* retrieve simple groups */
237 EXEC SQL DECLARE l_cursor CURSOR FOR
240 WHERE grouplist != 0 AND active != 0
242 EXEC SQL OPEN l_cursor;
245 EXEC SQL FETCH l_cursor INTO :gid, :lid;
248 sprintf(buf, ":%d", gid);
249 hash_store(groups, lid, strsave(buf));
251 EXEC SQL CLOSE l_cursor;
253 /* now do grplists */
254 users = create_hash(10000);
255 EXEC SQL DECLARE u_cursor CURSOR FOR
256 SELECT users_id, login, unix_uid
260 EXEC SQL OPEN u_cursor;
263 EXEC SQL FETCH u_cursor INTO :id, :name, :uid;
266 u = malloc(sizeof(struct user));
267 strcpy(u->name, strtrim(name));
270 hash_store(users, id, u);
272 EXEC SQL CLOSE u_cursor;
274 EXEC SQL DECLARE m_cursor2 CURSOR FOR
275 SELECT list_id, member_id
277 WHERE member_type = 'USER'
279 EXEC SQL OPEN m_cursor2;
282 EXEC SQL FETCH m_cursor2 INTO :lid, :id;
285 if ((u = (struct user *) hash_lookup(users, id)) &&
286 (l = hash_lookup(groups, lid)))
288 g = malloc(sizeof(struct grp));
294 EXEC SQL CLOSE m_cursor2;
296 for (p = &(users->data[users->size - 1]); p >= users->data; p--)
298 for (b = *p; b; b = b->next)
300 u = (struct user *)b->data;
301 sprintf(buf, "%s:%d", u->name, u->uid);
302 for (g = u->lists; g; g = g->next)
304 b->data = strsave(buf);
305 fprintf(fd, "%s\n", buf);
312 db_error(sqlca.sqlcode);
317 /* Now do each of the servers, linking the credentials list file and
318 * compiling the quota and dirs files.
321 int do_machs(struct save_queue *machs)
323 EXEC SQL BEGIN DECLARE SECTION;
324 char *machname, listname[33], dev[33], dir[81], fstype[9];
325 int uid, quota, id, gid, flag1, flag2, flag3, flag4;
326 EXEC SQL END DECLARE SECTION;
327 char file[64], f1[64], f2[64], *cp;
328 int prevuid, quotasum, olddev, oldmach;
330 struct hash *machines;
332 fprintf(stderr, "Building machine files\n");
334 machines = create_hash(100);
335 while (sq_get_data(machs, &machname))
337 EXEC SQL SELECT s.value3, m.mach_id
339 FROM serverhosts s, machine m
340 WHERE s.mach_id = m.mach_id AND m.name = :machname AND
343 sprintf(f1, "%s/list-%s", nfs_dir, strtrim(listname));
344 sprintf(f2, "%s/%s.cred", nfs_dir, machname);
345 unlink(f2); /* ignore errors on this unlink */
348 fprintf(stderr, "Cannot link %s to %s\n", f1, f2);
351 hash_store(machines, id, machname);
354 olddev = oldmach = -1;
357 EXEC SQL DECLARE q_cursor CURSOR FOR
358 SELECT DISTINCT q.quota, q.entity_id, q.phys_id, n.device, n.mach_id
359 FROM quota q, nfsphys n
360 WHERE n.nfsphys_id = q.phys_id AND q.phys_id != 0 AND
361 n.status < 16 AND q.type = 'USER'
362 ORDER BY n.mach_id, q.phys_id, q.entity_id;
363 EXEC SQL OPEN q_cursor;
366 EXEC SQL FETCH q_cursor INTO :quota, :uid, :flag1, :dev, :flag2;
369 if (flag1 != olddev || flag2 != oldmach)
372 fprintf(fd, "%d %d\n", prevuid, quotasum);
373 if (flag2 == 0 || !hash_lookup(machines, flag2))
379 while (cp = strchr(dev, '/'))
381 sprintf(file, "%s/%s.%s.quotas", nfs_dir,
382 hash_lookup(machines, flag2), strtrim(dev));
383 fd = fopen(file, "w");
386 fprintf(stderr, "cannot open %s for output\n", file);
394 if ((cp = hash_lookup(users, prevuid)) &&
395 (cp = strchr(cp, ':')))
396 prevuid = atoi(cp + 1);
398 fprintf(fd, "%d %d\n", prevuid, quotasum);
405 EXEC SQL CLOSE q_cursor;
406 if ((cp = hash_lookup(users, prevuid)) &&
407 (cp = strchr(cp, ':')))
408 prevuid = atoi(cp + 1);
410 fprintf(fd, "%d %d\n", prevuid, quotasum);
411 if (fd != stdin && fclose(fd) == EOF)
413 fprintf(stderr, "error closing %s", file);
417 olddev = oldmach = -1;
419 EXEC SQL DECLARE q_cursor2 CURSOR FOR
420 SELECT DISTINCT q.quota, q.entity_id, q.phys_id, n.device, n.mach_id,
422 FROM quota q, nfsphys n
423 WHERE n.nfsphys_id = q.phys_id AND q.phys_id != 0 AND
424 n.status > 15 AND q.type = 'GROUP'
425 ORDER BY mach_id, phys_id, entity_id;
426 EXEC SQL OPEN q_cursor2;
429 EXEC SQL FETCH q_cursor2 INTO :quota, :gid, :flag1, :dev,
433 if (flag1 != olddev || flag2 != oldmach)
436 fprintf(fd, "%d %d\n", prevuid, quotasum);
437 if (flag2 == 0 || !hash_lookup(machines, flag2))
443 while (cp = strchr(dev, '/'))
445 sprintf(file, "%s/%s.%s.quotas", nfs_dir,
446 hash_lookup(machines, flag2), strtrim(dev));
447 fd = fopen(file, "w");
450 fprintf(stderr, "cannot open %s for output\n", file);
458 if (cp = hash_lookup(groups, prevuid))
459 prevuid = atoi(cp + 1);
461 fprintf(fd, "%d %d\n", prevuid, quotasum);
468 EXEC SQL CLOSE q_cursor2;
469 if ((cp = hash_lookup(groups, prevuid)))
470 prevuid = atoi(cp + 1);
472 fprintf(fd, "%d %d\n", prevuid, quotasum);
473 if (fd != stdin && fclose(fd) == EOF)
475 fprintf(stderr, "error closing %s", file);
479 olddev = oldmach = -1;
482 EXEC SQL DECLARE q_cursor3 CURSOR FOR
483 SELECT DISTINCT f.name, f.lockertype, u.unix_uid, l.gid, f.phys_id,
485 FROM users u, list l, nfsphys n, filesys f
486 WHERE u.users_id = f.owner AND l.list_id = f.owners
487 AND f.createflg != 0 AND f.phys_id != 0 AND f.type = 'NFS'
488 AND f.phys_id = n.nfsphys_id
489 ORDER BY mach_id, phys_id;
490 EXEC SQL OPEN q_cursor3;
493 EXEC SQL FETCH q_cursor3 INTO :dir, :fstype, :uid, :gid, :flag1,
497 if ((flag1 != olddev || flag2 != oldmach) &&
498 hash_lookup(machines, flag2))
504 while (cp = strchr(dev, '/'))
506 sprintf(file, "%s/%s.%s.dirs", nfs_dir,
507 hash_lookup(machines, flag2), strtrim(dev));
508 fd = fopen(file, "w");
511 fprintf(stderr, "cannot open %s for output\n", file);
515 fprintf(fd, "%s %d %d %s\n", strtrim(dir), uid, gid, strtrim(fstype));
517 EXEC SQL CLOSE q_cursor3;
518 if (fclose(fd) == EOF)
520 fprintf(stderr, "error closing %s", file);
525 db_error(sqlca.sqlcode);