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>
14 #include <moira_site.h>
15 #include <sys/types.h>
18 EXEC SQL INCLUDE sqlca;
21 #define min(x,y) ((x) < (y) ? (x) : (y))
23 char *whoami = "nfs.gen";
24 char *db = "moira/moira";
25 char *malloc(), *strsave();
27 struct hash *users, *groups;
39 fprintf(stderr, "usage: %s [outfile]\n", argv[0]);
43 initialize_sms_error_table();
44 sprintf(nfs_dir, "%s/nfs", DCM_DIR);
53 fprintf(stderr, "No files updated.\n");
54 if (argc == 2 && stat(argv[1], &sb) == 0)
59 sprintf(cmd, "cd %s; cp %s/nfs/* .; tar cf %s .",
60 nfs_dir, SMS_DIR, argv[1]);
69 /* Generate the files. Returns zero if nothing changed, non-zero otherwise. */
73 EXEC SQL BEGIN DECLARE SECTION;
74 char machname[41], listname[33];
75 EXEC SQL END DECLARE SECTION;
76 struct save_queue *machs, *lists;
82 /* The following is declarative, not executed,
83 * and so is dependent on where it is in the file,
84 * not in the order of execution of statements.
86 EXEC SQL WHENEVER SQLERROR GOTO sqlerr;
88 EXEC SQL DECLARE s_cursor CURSOR FOR
89 SELECT m.name, s.value3
90 FROM machine m, serverhosts s
91 WHERE m.mach_id = s.mach_id AND s.service = 'NFS' AND s.enable != 0;
92 EXEC SQL OPEN s_cursor;
94 EXEC SQL FETCH s_cursor INTO :machname, :listname;
95 if (sqlca.sqlcode != 0) break;
96 sq_save_unique_string(machs, strsave(strtrim(machname)));
97 sq_save_unique_string(lists, strsave(strtrim(listname)));
99 EXEC SQL CLOSE s_cursor;
101 changed = do_lists(lists);
103 changed += do_machs(machs);
106 db_error(sqlca.sqlcode);
111 /* Make all of the credentials lists that will be needed. Returns 0 if
112 * no files were actually changed
116 struct save_queue *lists;
121 EXEC SQL BEGIN DECLARE SECTION;
122 char *listname, *lsname, lname[33], uname[9];
123 int uid, id, flag1, flag2, flag3, flag4;
124 EXEC SQL END DECLARE SECTION;
126 sprintf(file, "%s/list-", nfs_dir);
128 if (stat(file, &sb) == 0) {
129 if ((ModDiff (&flag1, "users", sb.st_mtime)) ||
130 (ModDiff (&flag2, "list", sb.st_mtime)) ||
131 (ModDiff (&flag3, "imembers", sb.st_mtime)) ||
132 (ModDiff (&flag4, "serverhosts", sb.st_mtime))) exit (MR_DATE);
133 if (flag1 < 0 && flag2 < 0 && flag3 < 0 && flag4 < 0) {
134 fprintf(stderr, "The lists do not need to be rebuilt.\n");
141 /* build the list of everyone, and store it in a file whose name
142 * corresponds to the empty list.
146 fprintf(stderr, "Building specific lists\n");
147 /* now do each of the lists used by an NFS server */
149 while (sq_get_data(lists, &listname)) {
150 if (strlen(listname) == 0)
152 sprintf(file, "%s/list-%s", nfs_dir, listname);
153 fd = fopen(file, "w");
155 fprintf(stderr, "cannot open %s for output\n", file);
159 EXEC SQL DECLARE m_cursor CURSOR FOR
161 FROM imembers m, list l
162 WHERE m.list_id=l.list_id AND l.name = :listname AND
165 EXEC SQL OPEN m_cursor;
167 EXEC SQL FETCH m_cursor INTO :id;
168 if (sqlca.sqlcode != 0) break;
169 if (u = hash_lookup(users, id))
170 fprintf(fd, "%s\n", u);
172 EXEC SQL CLOSE m_cursor;
173 if (fclose(fd) == EOF) {
174 fprintf(stderr, "error closing %s\n", file);
178 /* don't free here either
183 db_error(sqlca.sqlcode);
188 /* Build the list of everybody. */
202 char buf[BUFSIZ], *l;
205 struct bucket *b, **p;
206 EXEC SQL BEGIN DECLARE SECTION;
208 int gid, id, lid, maxid, uid;
209 EXEC SQL END DECLARE SECTION;
212 struct save_queue *sq;
214 fprintf(stderr, "Building the list of everybody\n");
215 sprintf(buf, "%s/list-", nfs_dir);
216 fd = fopen(buf, "w");
218 fprintf(stderr, "cannot open %s for output\n", buf);
222 /* make space for group list */
223 groups = create_hash(15000);
225 /* retrieve simple groups */
226 EXEC SQL DECLARE l_cursor CURSOR FOR
229 WHERE grouplist != 0 AND active != 0
231 EXEC SQL OPEN l_cursor;
233 EXEC SQL FETCH l_cursor INTO :gid, :lid;
234 if (sqlca.sqlcode != 0) break;
235 sprintf(buf, ":%d", gid);
236 hash_store(groups, lid, strsave(buf));
238 EXEC SQL CLOSE l_cursor;
240 /* now do grplists */
241 users = create_hash(10000);
242 EXEC SQL DECLARE u_cursor CURSOR FOR
243 SELECT users_id, login, unix_uid
247 EXEC SQL OPEN u_cursor;
249 EXEC SQL FETCH u_cursor INTO :id, :name, :uid;
250 if (sqlca.sqlcode != 0) break;
251 u = (struct user *) malloc(sizeof(struct user));
252 strcpy(u->name, strtrim(name));
255 hash_store(users, id, u);
257 EXEC SQL CLOSE u_cursor;
259 EXEC SQL DECLARE m_cursor2 CURSOR FOR
260 SELECT list_id, member_id
262 WHERE member_type = 'USER'
264 EXEC SQL OPEN m_cursor2;
266 EXEC SQL FETCH m_cursor2 INTO :lid, :id;
267 if (sqlca.sqlcode != 0) break;
268 if ((u = (struct user *) hash_lookup(users, id)) &&
269 ((l = hash_lookup(groups, lid)) != NULL)) {
270 g = (struct grp *) malloc(sizeof(struct grp));
276 EXEC SQL CLOSE m_cursor2;
278 for (p = &(users->data[users->size - 1]); p >= users->data; p--) {
279 for (b = *p; b; b = b->next) {
280 u = (struct user *)b->data;
281 sprintf(buf, "%s:%d", u->name, u->uid);
282 for (g = u->lists; g; g = g->next)
284 b->data = strsave(buf);
285 fprintf(fd, "%s\n", buf);
293 db_error(sqlca.sqlcode);
298 /* Now do each of the servers, linking the credentials list file and
299 * compiling the quota and dirs files.
303 struct save_queue *machs;
305 EXEC SQL BEGIN DECLARE SECTION;
306 char *machname, listname[33], dev[33], dir[81], fstype[9];
307 int uid, quota, id, gid, flag1, flag2, flag3, flag4;
308 EXEC SQL END DECLARE SECTION;
309 char file[64], f1[64], f2[64], *cp;
310 int prevuid, quotasum, olddev, oldmach;
312 struct hash *machines;
314 fprintf(stderr, "Building machine files\n");
317 machines = create_hash(100);
318 while (sq_get_data(machs, &machname)) {
319 EXEC SQL SELECT s.value3, m.mach_id
321 FROM serverhosts s, machine m
322 WHERE s.mach_id = m.mach_id AND m.name = :machname AND
325 sprintf(f1, "%s/list-%s", nfs_dir, strtrim(listname));
326 sprintf(f2, "%s/%s.cred", nfs_dir, machname);
327 unlink(f2); /* ignore errors on this unlink */
329 fprintf(stderr, "Cannot link %s to %s\n", f1, f2);
332 hash_store(machines, id, machname);
336 olddev = oldmach = -1;
339 EXEC SQL DECLARE q_cursor CURSOR FOR
340 SELECT DISTINCT q.quota, q.entity_id, q.phys_id, n.device, n.mach_id
341 FROM quota q, nfsphys n
342 WHERE n.nfsphys_id = q.phys_id AND q.phys_id != 0 AND
343 n.status < 16 AND q.type = 'USER'
344 ORDER BY n.mach_id, q.phys_id, q.entity_id;
345 EXEC SQL OPEN q_cursor;
347 EXEC SQL FETCH q_cursor INTO :quota, :uid, :flag1, :dev, :flag2;
348 if (sqlca.sqlcode != 0) break;
349 if (flag1 != olddev || flag2 != oldmach) {
351 fprintf(fd, "%d %d\n", prevuid, quotasum);
352 if (flag2 == 0 || !hash_lookup(machines, flag2))
358 while (cp = strchr(dev, '/')) *cp = '@';
359 sprintf(file, "%s/%s.%s.quotas", nfs_dir,
360 hash_lookup(machines, flag2), strtrim(dev));
361 fd = fopen(file, "w");
363 fprintf(stderr, "cannot open %s for output\n", file);
369 if (uid != prevuid) {
370 if ((cp = hash_lookup(users, prevuid)) &&
371 (cp = strchr(cp, ':')))
372 prevuid = atoi(cp+1);
374 fprintf(fd, "%d %d\n", prevuid, quotasum);
381 EXEC SQL CLOSE q_cursor;
382 if ((cp = hash_lookup(users, prevuid)) &&
383 (cp = strchr(cp, ':')))
384 prevuid = atoi(cp+1);
386 fprintf(fd, "%d %d\n", prevuid, quotasum);
387 if (fd != stdin && fclose(fd) == EOF) {
388 fprintf(stderr, "error closing %s", file);
392 olddev = oldmach = -1;
394 EXEC SQL DECLARE q_cursor2 CURSOR FOR
395 SELECT DISTINCT q.quota, q.entity_id, q.phys_id, n.device, n.mach_id,
397 FROM quota q, nfsphys n
398 WHERE n.nfsphys_id = q.phys_id AND q.phys_id != 0 AND
399 n.status > 15 AND q.type = 'GROUP'
400 ORDER BY mach_id, phys_id, entity_id;
401 EXEC SQL OPEN q_cursor2;
403 EXEC SQL FETCH q_cursor2 INTO :quota, :gid, :flag1, :dev,
405 if (sqlca.sqlcode != 0) break;
406 if (flag1 != olddev || flag2 != oldmach) {
408 fprintf(fd, "%d %d\n", prevuid, quotasum);
409 if (flag2 == 0 || !hash_lookup(machines, flag2))
415 while (cp = strchr(dev, '/')) *cp = '@';
416 sprintf(file, "%s/%s.%s.quotas", nfs_dir,
417 hash_lookup(machines, flag2), strtrim(dev));
418 fd = fopen(file, "w");
420 fprintf(stderr, "cannot open %s for output\n", file);
426 if (gid != prevuid) {
427 if (cp = hash_lookup(groups, prevuid))
428 prevuid = atoi(cp + 1);
430 fprintf(fd, "%d %d\n", prevuid, quotasum);
437 EXEC SQL CLOSE q_cursor2;
438 if (cp = hash_lookup(groups, prevuid))
439 prevuid = atoi(cp + 1);
441 fprintf(fd, "%d %d\n", prevuid, quotasum);
442 if (fd != stdin && fclose(fd) == EOF) {
443 fprintf(stderr, "error closing %s", file);
447 olddev = oldmach = -1;
450 EXEC SQL DECLARE q_cursor3 CURSOR FOR
451 SELECT DISTINCT f.name, f.lockertype, u.unix_uid, l.gid, f.phys_id,
453 FROM users u, list l, nfsphys n, filesys f
454 WHERE u.users_id = f.owner AND
455 l.list_id = f.owners AND
456 f.createflg != 0 AND f.phys_id != 0 AND
458 f.phys_id = n.nfsphys_id
459 ORDER BY mach_id, phys_id;
460 EXEC SQL OPEN q_cursor3;
462 EXEC SQL FETCH q_cursor3 INTO :dir, :fstype, :uid, :gid, :flag1,
464 if (sqlca.sqlcode != 0) break;
465 if ((flag1 != olddev || flag2 != oldmach) &&
466 hash_lookup(machines, flag2)) {
471 while (cp = strchr(dev, '/')) *cp = '@';
472 sprintf(file, "%s/%s.%s.dirs", nfs_dir,
473 hash_lookup(machines, flag2), strtrim(dev));
474 fd = fopen(file, "w");
476 fprintf(stderr, "cannot open %s for output\n", file);
480 fprintf(fd, "%s %d %d %s\n", strtrim(dir), uid, gid, strtrim(fstype));
482 EXEC SQL CLOSE q_cursor3;
483 if (fclose(fd) == EOF) {
484 fprintf(stderr, "error closing %s", file);
489 db_error(sqlca.sqlcode);