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>
13 #include <moira_site.h>
14 #include <sys/types.h>
17 EXEC SQL INCLUDE sqlca;
20 #define min(x,y) ((x) < (y) ? (x) : (y))
22 char *whoami = "nfs.gen";
23 char *malloc(), *strsave();
25 struct hash *users, *groups;
37 fprintf(stderr, "usage: %s [outfile]\n", argv[0]);
41 initialize_sms_error_table();
42 sprintf(nfs_dir, "%s/nfs", DCM_DIR);
45 EXEC SQL CONNECT moira;
46 EXEC SQL SET LOCKMODE SESSION WHERE LEVEL=TABLE, READLOCK=SHARED;
49 EXEC SQL DATABASE moira;
58 EXEC SQL CLOSE DATABASE;
62 fprintf(stderr, "No files updated.\n");
63 if (argc == 2 && stat(argv[1], &sb) == 0)
68 sprintf(cmd, "cd %s; cp %s/nfs/* .; tar cf %s .",
69 nfs_dir, SMS_DIR, argv[1]);
78 /* Generate the files. Returns zero if nothing changed, non-zero otherwise. */
82 EXEC SQL BEGIN DECLARE SECTION;
83 char machname[41], listname[33];
84 EXEC SQL END DECLARE SECTION;
85 struct save_queue *machs, *lists;
92 EXEC SQL SELECT modtime INTO :listname FROM imembers WHERE list_id = 0;
93 EXEC SQL SELECT modtime INTO :listname FROM filesys WHERE filsys_id = 0;
94 EXEC SQL SELECT modtime INTO :listname FROM users WHERE users_id = 0;
95 EXEC SQL SELECT modtime INTO :listname FROM machine where mach_id=0;
96 EXEC SQL SELECT modtime INTO :listname FROM list WHERE list_id = 0;
97 EXEC SQL SELECT serverhosts INTO :listname FROM machine where mach_id=0;
100 /* The following is declarative, not executed,
101 * and so is dependent on where it is in the file,
102 * not in the order of execution of statements.
104 EXEC SQL WHENEVER SQLERROR GOTO sqlerr;
105 EXEC SQL DECLARE s_cursor CURSOR FOR
106 SELECT m.name, s.value3
107 FROM machine m, serverhosts s
108 WHERE m.mach_id = s.mach_id AND s.service = 'NFS' AND s.enable != 0;
109 EXEC SQL OPEN s_cursor;
111 EXEC SQL FETCH s_cursor INTO :machname, :listname;
112 if (sqlca.sqlcode != 0) break;
113 sq_save_unique_string(machs, strsave(strtrim(machname)));
114 sq_save_unique_string(lists, strsave(strtrim(listname)));
116 EXEC SQL CLOSE s_cursor;
118 changed = do_lists(lists);
120 changed += do_machs(machs);
123 com_err(whoami, MR_INGRES_ERR, " code %d\n", sqlca.sqlcode);
124 critical_alert("DCM", "NFS build encountered DATABASE ERROR %d",
130 /* Make all of the credentials lists that will be needed. Returns 0 if
131 * no files were actually changed
133 * The caller is responsible for acquiring locks on imembers, users, list
137 struct save_queue *lists;
142 EXEC SQL BEGIN DECLARE SECTION;
143 char *listname, *lsname, lname[33], uname[9];
144 int uid, id, flag1, flag2, flag3, flag4;
145 EXEC SQL END DECLARE SECTION;
147 sprintf(file, "%s/list-", nfs_dir);
149 if (stat(file, &sb) == 0) {
150 if ((ModDiff (&flag1, "users", sb.st_mtime)) ||
151 (ModDiff (&flag2, "list", sb.st_mtime)) ||
152 (ModDiff (&flag3, "imembers", sb.st_mtime)) ||
153 (ModDiff (&flag4, "serverhosts", sb.st_mtime))) exit (MR_DATE);
154 if (flag1 < 0 && flag2 < 0 && flag3 < 0 && flag4 < 0) {
155 fprintf(stderr, "The lists do not need to be rebuilt.\n");
162 /* build the list of everyone, and store it in a file whose name
163 * corresponds to the empty list.
167 fprintf(stderr, "Building specific lists\n");
168 /* now do each of the lists used by an NFS server */
170 while (sq_get_data(lists, &listname)) {
171 if (strlen(listname) == 0)
173 sprintf(file, "%s/list-%s", nfs_dir, listname);
174 fd = fopen(file, "w");
176 fprintf(stderr, "cannot open %s for output\n", file);
180 EXEC SQL DECLARE m_cursor CURSOR FOR
182 FROM imembers m, list l
183 WHERE m.list_id=l.list_id AND l.name = :listname AND
186 EXEC SQL OPEN m_cursor;
188 EXEC SQL FETCH m_cursor INTO :id;
189 if (sqlca.sqlcode != 0) break;
190 if (u = hash_lookup(users, id))
191 fprintf(fd, "%s\n", u);
193 EXEC SQL CLOSE m_cursor;
194 if (fclose(fd) == EOF) {
195 fprintf(stderr, "error closing %s\n", file);
199 /* don't free here either
204 com_err(whoami, MR_INGRES_ERR, " code %d\n", sqlca.sqlcode);
205 critical_alert("DCM", "NFS build encountered DATABASE ERROR %d",
211 /* Build the list of everybody. */
225 char buf[BUFSIZ], *l;
228 struct bucket *b, **p;
229 EXEC SQL BEGIN DECLARE SECTION;
231 int gid, id, lid, maxid, uid;
232 EXEC SQL END DECLARE SECTION;
235 struct save_queue *sq;
237 fprintf(stderr, "Building the list of everybody\n");
238 sprintf(buf, "%s/list-", nfs_dir);
239 fd = fopen(buf, "w");
241 fprintf(stderr, "cannot open %s for output\n", buf);
245 /* make space for group list */
246 groups = create_hash(15000);
248 /* retrieve simple groups */
249 EXEC SQL DECLARE l_cursor CURSOR FOR
252 WHERE grouplist != 0 AND active != 0
254 EXEC SQL OPEN l_cursor;
256 EXEC SQL FETCH l_cursor INTO :gid, :lid;
257 if (sqlca.sqlcode != 0) break;
258 sprintf(buf, ":%d", gid);
259 hash_store(groups, lid, strsave(buf));
261 EXEC SQL CLOSE l_cursor;
263 /* now do grplists */
264 users = create_hash(10000);
265 EXEC SQL DECLARE u_cursor CURSOR FOR
266 SELECT users_id, login, uid
270 EXEC SQL OPEN u_cursor;
272 EXEC SQL FETCH u_cursor INTO :id, :name, :uid;
273 if (sqlca.sqlcode != 0) break;
274 u = (struct user *) malloc(sizeof(struct user));
275 strcpy(u->name, strtrim(name));
278 hash_store(users, id, u);
280 EXEC SQL CLOSE u_cursor;
282 EXEC SQL DECLARE m_cursor2 CURSOR FOR
283 SELECT list_id, member_id
285 WHERE member_type = 'USER'
287 EXEC SQL OPEN m_cursor2;
289 EXEC SQL FETCH m_cursor2 INTO :lid, :id;
290 if (sqlca.sqlcode != 0) break;
291 if ((u = (struct user *) hash_lookup(users, id)) &&
292 ((l = hash_lookup(groups, lid)) != NULL)) {
293 g = (struct grp *) malloc(sizeof(struct grp));
299 EXEC SQL CLOSE m_cursor2;
301 for (p = &(users->data[users->size - 1]); p >= users->data; p--) {
302 for (b = *p; b; b = b->next) {
303 u = (struct user *)b->data;
304 sprintf(buf, "%s:%d", u->name, u->uid);
305 for (g = u->lists; g; g = g->next)
307 b->data = strsave(buf);
308 fprintf(fd, "%s\n", buf);
316 com_err(whoami, MR_INGRES_ERR, " code %d\n", sqlca.sqlcode);
317 critical_alert("DCM", "NFS build encountered DATABASE ERROR %d",
323 /* Now do each of the servers, linking the credentials list file and
324 * compiling the quota and dirs files.
326 * Caller is responsible for acquiring locks on machine, filesys, users
330 struct save_queue *machs;
332 EXEC SQL BEGIN DECLARE SECTION;
333 char *machname, listname[33], dev[33], dir[81], fstype[9];
334 int uid, quota, id, gid, flag1, flag2, flag3, flag4;
335 EXEC SQL END DECLARE SECTION;
336 char file[64], f1[64], f2[64], *cp, *index();
337 int prevuid, quotasum, olddev, oldmach;
339 struct hash *machines;
341 fprintf(stderr, "Building machine files\n");
344 machines = create_hash(100);
345 while (sq_get_data(machs, &machname)) {
346 EXEC SQL SELECT s.value3, m.mach_id
348 FROM serverhosts s, machine m
349 WHERE s.mach_id = m.mach_id AND m.name = :machname AND
352 sprintf(f1, "%s/list-%s", nfs_dir, strtrim(listname));
353 sprintf(f2, "%s/%s.cred", nfs_dir, machname);
354 unlink(f2); /* ignore errors on this unlink */
356 fprintf(stderr, "Cannot link %s to %s\n", f1, f2);
359 hash_store(machines, id, machname);
363 olddev = oldmach = -1;
366 EXEC SQL DECLARE q_cursor CURSOR FOR
367 SELECT DISTINCT q.quota, q.entity_id, q.phys_id, n.device, n.mach_id
368 FROM quota q, nfsphys n
369 WHERE n.nfsphys_id = q.phys_id AND q.phys_id != 0 AND
370 n.status < 16 AND q.type = 'USER'
371 ORDER BY n.mach_id, q.phys_id, q.entity_id;
372 EXEC SQL OPEN q_cursor;
374 EXEC SQL FETCH q_cursor INTO :quota, :uid, :flag1, :dev, :flag2;
375 if (sqlca.sqlcode != 0) break;
376 if (flag1 != olddev || flag2 != oldmach) {
378 fprintf(fd, "%d %d\n", prevuid, quotasum);
379 if (flag2 == 0 || !hash_lookup(machines, flag2))
385 while (cp = index(dev, '/')) *cp = '@';
386 sprintf(file, "%s/%s.%s.quotas", nfs_dir,
387 hash_lookup(machines, flag2), strtrim(dev));
388 fd = fopen(file, "w");
390 fprintf(stderr, "cannot open %s for output\n", file);
396 if (uid != prevuid) {
397 if ((cp = hash_lookup(users, prevuid)) &&
398 (cp = index(cp, ':')))
399 prevuid = atoi(cp+1);
401 fprintf(fd, "%d %d\n", prevuid, quotasum);
408 EXEC SQL CLOSE q_cursor;
409 if ((cp = hash_lookup(users, prevuid)) &&
410 (cp = index(cp, ':')))
411 prevuid = atoi(cp+1);
413 fprintf(fd, "%d %d\n", prevuid, quotasum);
414 if (fd != stdin && fclose(fd) == EOF) {
415 fprintf(stderr, "error closing %s", file);
419 olddev = oldmach = -1;
421 EXEC SQL DECLARE q_cursor2 CURSOR FOR
422 SELECT DISTINCT q.quota, q.entity_id, q.phys_id, n.device, n.mach_id,
424 FROM quota q, nfsphys n
425 WHERE n.nfsphys_id = q.phys_id AND q.phys_id != 0 AND
426 n.status > 15 AND q.type = 'GROUP'
427 ORDER BY mach_id, phys_id, entity_id;
428 EXEC SQL OPEN q_cursor2;
430 EXEC SQL FETCH q_cursor2 INTO :quota, :gid, :flag1, :dev,
432 if (sqlca.sqlcode != 0) break;
433 if (flag1 != olddev || flag2 != oldmach) {
435 fprintf(fd, "%d %d\n", prevuid, quotasum);
436 if (flag2 == 0 || !hash_lookup(machines, flag2))
442 while (cp = index(dev, '/')) *cp = '@';
443 sprintf(file, "%s/%s.%s.quotas", nfs_dir,
444 hash_lookup(machines, flag2), strtrim(dev));
445 fd = fopen(file, "w");
447 fprintf(stderr, "cannot open %s for output\n", file);
453 if (gid != prevuid) {
454 if (cp = hash_lookup(groups, prevuid))
455 prevuid = atoi(cp + 1);
457 fprintf(fd, "%d %d\n", prevuid, quotasum);
464 EXEC SQL CLOSE q_cursor2;
465 if (cp = hash_lookup(groups, prevuid))
466 prevuid = atoi(cp + 1);
468 fprintf(fd, "%d %d\n", prevuid, quotasum);
469 if (fd != stdin && fclose(fd) == EOF) {
470 fprintf(stderr, "error closing %s", file);
474 olddev = oldmach = -1;
477 EXEC SQL DECLARE q_cursor3 CURSOR FOR
478 SELECT DISTINCT f.name, f.lockertype, u.uid, l.gid, f.phys_id,
480 FROM users u, list l, nfsphys n, filesys f
481 WHERE u.users_id = f.owner AND
482 l.list_id = f.owners AND
483 f.createflg != 0 AND f.phys_id != 0 AND
485 f.phys_id = n.nfsphys_id
486 ORDER BY mach_id, phys_id;
487 EXEC SQL OPEN q_cursor3;
489 EXEC SQL FETCH q_cursor3 INTO :dir, :fstype, :uid, :gid, :flag1,
491 if (sqlca.sqlcode != 0) break;
492 if (flag1 != olddev || flag2 != oldmach) {
497 while (cp = index(dev, '/')) *cp = '@';
498 sprintf(file, "%s/%s.%s.dirs", nfs_dir,
499 hash_lookup(machines, flag2), strtrim(dev));
500 fd = fopen(file, "w");
502 fprintf(stderr, "cannot open %s for output\n", file);
506 fprintf(fd, "%s %d %d %s\n", strtrim(dir), uid, gid, strtrim(fstype));
508 EXEC SQL CLOSE q_cursor3;
509 if (fclose(fd) == EOF) {
510 fprintf(stderr, "error closing %s", file);
515 com_err(whoami, MR_INGRES_ERR, " code %d\n", sqlca.sqlcode);
516 critical_alert("DCM", "NFS build encountered DATABASE ERROR %d",