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();
35 fprintf(stderr, "usage: %s [outfile]\n", argv[0]);
39 initialize_sms_error_table();
40 sprintf(nfs_dir, "%s/nfs", DCM_DIR);
43 EXEC SQL CONNECT moira;
46 EXEC SQL DATABASE moira;
55 EXEC SQL CLOSE DATABASE;
59 fprintf(stderr, "No files updated.\n");
60 if (argc == 2 && stat(argv[1], &sb) == 0)
65 sprintf(cmd, "cd %s; cp %s/nfs/* .; tar cf %s .",
66 nfs_dir, SMS_DIR, argv[1]);
75 /* Generate the files. Returns zero if nothing changed, non-zero otherwise. */
79 EXEC SQL BEGIN DECLARE SECTION;
80 char machname[33], listname[33];
81 EXEC SQL END DECLARE SECTION;
82 struct save_queue *machs, *lists;
88 /* The following is declarative, not executed,
89 * and so is dependent on where it is in the file,
90 * not in the order of execution of statements.
92 EXEC SQL WHENEVER SQLERROR GOTO sqlerr;
93 EXEC SQL DECLARE s_cursor CURSOR FOR
94 SELECT m.name, s.value3
95 FROM machine m, serverhosts s
96 WHERE m.mach_id = s.mach_id AND s.service = "NFS" AND s.enable != 0;
97 EXEC SQL OPEN s_cursor;
99 EXEC SQL FETCH s_cursor INTO :machname, :listname;
100 if (sqlca.sqlcode != 0) break;
101 sq_save_unique_string(machs, strsave(strtrim(machname)));
102 sq_save_unique_string(lists, strsave(strtrim(listname)));
104 EXEC SQL CLOSE s_cursor;
106 changed = do_lists(lists);
107 changed += do_machs(machs);
110 com_err(whoami, MR_INGRES_ERR, " code %d\n", sqlca.sqlcode);
111 critical_alert("DCM", "NFS build encountered DATABASE ERROR %d",
117 /* Make all of the credentials lists that will be needed. Returns 0 if
118 * no files were actually changed */
121 struct save_queue *lists;
124 struct hash *users, *do_everyone();
127 EXEC SQL BEGIN DECLARE SECTION;
128 char *listname, *lsname, lname[33], uname[9];
129 int uid, id, flag1, flag2, flag3, flag4;
130 EXEC SQL END DECLARE SECTION;
132 sprintf(file, "%s/list-", nfs_dir);
133 if (stat(file, &sb) == 0) {
134 if ((ModDiff (&flag1, "users", sb.st_mtime)) ||
135 (ModDiff (&flag2, "list", sb.st_mtime)) ||
136 (ModDiff (&flag3, "imembers", sb.st_mtime)) ||
137 (ModDiff (&flag4, "serverhosts", sb.st_mtime))) exit (MR_DATE);
138 if (flag1 < 0 && flag2 < 0 && flag3 < 0 && flag4 < 0) {
139 fprintf(stderr, "The lists do not need to be rebuilt.\n");
145 EXEC SQL SELECT modtime INTO :lname FROM list WHERE list_id = 0;
146 EXEC SQL SELECT modtime INTO :lname FROM users WHERE users_id = 0;
148 /* build the list of everyone, and store it in a file whose name
149 * corresponds to the empty list.
151 users = do_everyone();
153 fprintf(stderr, "Building specific lists\n");
154 /* now do each of the lists used by an NFS server */
156 while (sq_get_data(lists, &listname)) {
157 if (strlen(listname) == 0)
159 sprintf(file, "%s/list-%s", nfs_dir, listname);
160 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 AND
170 m.member_type="USER";
171 EXEC SQL OPEN m_cursor;
173 EXEC SQL FETCH m_cursor INTO :id;
174 if (sqlca.sqlcode != 0) break;
175 if (u = hash_lookup(users, id))
176 fprintf(fd, "%s\n", u);
178 EXEC SQL CLOSE m_cursor;
179 if (fclose(fd) == EOF) {
180 fprintf(stderr, "error closing %s\n", file);
184 /* don't free here either
189 com_err(whoami, MR_INGRES_ERR, " code %d\n", sqlca.sqlcode);
190 critical_alert("DCM", "NFS build encountered DATABASE ERROR %d",
196 /* Build the list of everybody. */
208 struct hash *do_everyone()
210 char buf[BUFSIZ], *l;
211 struct hash *groups, *users;
214 struct bucket *b, **p;
215 EXEC SQL BEGIN DECLARE SECTION;
217 int gid, id, lid, maxid, uid;
218 EXEC SQL END DECLARE SECTION;
221 struct save_queue *sq;
223 fprintf(stderr, "Building the list of everybody\n");
224 sprintf(buf, "%s/list-", nfs_dir);
225 fd = fopen(buf, "w");
227 fprintf(stderr, "cannot open %s for output\n", buf);
231 /* make space for group list */
232 groups = create_hash(15000);
234 /* retrieve simple groups */
235 EXEC SQL DECLARE l_cursor CURSOR FOR
238 WHERE grouplist != 0 AND active != 0;
239 EXEC SQL OPEN l_cursor;
241 EXEC SQL FETCH l_cursor INTO :gid, :lid;
242 if (sqlca.sqlcode != 0) break;
243 sprintf(buf, ":%d", gid);
244 hash_store(groups, lid, strsave(buf));
246 EXEC SQL CLOSE l_cursor;
248 /* now do grplists */
249 users = create_hash(10000);
250 EXEC SQL DECLARE u_cursor CURSOR FOR
251 SELECT users_id, login, uid
254 EXEC SQL OPEN u_cursor;
256 EXEC SQL FETCH u_cursor INTO :id, :name, :uid;
257 if (sqlca.sqlcode != 0) break;
258 u = (struct user *) malloc(sizeof(struct user));
259 strcpy(u->name, strtrim(name));
262 hash_store(users, id, u);
264 EXEC SQL CLOSE u_cursor;
266 EXEC SQL DECLARE m_cursor2 CURSOR FOR
267 SELECT list_id, member_id
269 WHERE member_type = "USER";
270 EXEC SQL OPEN m_cursor2;
272 EXEC SQL FETCH m_cursor2 INTO :lid, :id;
273 if (sqlca.sqlcode != 0) break;
274 if ((u = (struct user *) hash_lookup(users, id)) &&
275 ((l = hash_lookup(groups, lid)) != NULL)) {
276 g = (struct grp *) malloc(sizeof(struct grp));
282 EXEC SQL CLOSE m_cursor2;
284 for (p = &(users->data[users->size - 1]); p >= users->data; p--) {
285 for (b = *p; b; b = b->next) {
286 u = (struct user *)b->data;
287 sprintf(buf, "%s:%d", u->name, u->uid);
288 for (g = u->lists; g; g = g->next)
290 b->data = strsave(buf);
291 fprintf(fd, "%s\n", buf);
299 com_err(whoami, MR_INGRES_ERR, " code %d\n", sqlca.sqlcode);
300 critical_alert("DCM", "NFS build encountered DATABASE ERROR %d",
306 /* Now do each of the servers, linking the credentials list file and
307 * compiling the quota and dirs files.
311 struct save_queue *machs;
313 EXEC SQL BEGIN DECLARE SECTION;
314 char *machname, listname[33], dev[33], dir[81], fstype[9];
315 int uid, quota, id, gid, flag1, flag2, flag3, flag4;
316 EXEC SQL END DECLARE SECTION;
317 char file[64], f1[64], f2[64], *cp, *index();
318 int prevuid, quotasum, olddev, oldmach;
320 struct hash *machines;
322 fprintf(stderr, "Building machine files\n");
324 /* acquire locks on machines & filesystems */
325 EXEC SQL SELECT modtime INTO :listname FROM users WHERE users_id = 0;
326 EXEC SQL SELECT modtime INTO :listname FROM machine WHERE users_id = 0;
327 EXEC SQL SELECT modtime INTO :listname FROM filesys WHERE filsys_id = 0;
329 machines = create_hash(100);
330 while (sq_get_data(machs, &machname)) {
331 EXEC SQL SELECT s.value3, m.mach_id
333 FROM serverhosts s, machine m
334 WHERE s.mach_id = m.mach_id AND m.name = :machname;
336 sprintf(f1, "%s/list-%s", nfs_dir, strtrim(listname));
337 sprintf(f2, "%s/%s.cred", nfs_dir, machname);
338 unlink(f2); /* ignore errors on this unlink */
340 fprintf(stderr, "Cannot link %s to %s\n", f1, f2);
343 hash_store(machines, id, machname);
347 olddev = oldmach = -1;
350 EXEC SQL DECLARE q_cursor CURSOR FOR
351 SELECT DISTINCT q.quota, u.uid, q.phys_id, n.device, n.mach_id
352 FROM quota q, users u, nfsphys n
353 WHERE u.users_id = q.entity_id AND n.nfsphys_id = q.phys_id AND
354 q.phys_id != 0 AND n.status < 16 AND q.type = "USER"
355 ORDER BY mach_id, phys_id, uid;
356 EXEC SQL OPEN q_cursor;
358 EXEC SQL FETCH q_cursor INTO :quota, :uid, :flag1, :dev, :flag2;
359 if (sqlca.sqlcode != 0) break;
360 if (flag1 != olddev || flag2 != oldmach) {
364 while (cp = index(dev, '/')) *cp = '@';
365 sprintf(file, "%s/%s.%s.quotas", nfs_dir,
366 hash_lookup(machines, flag2), strtrim(dev));
367 fd = fopen(file, "w");
369 fprintf(stderr, "cannot open %s for output\n", file);
375 if (uid != prevuid) {
377 fprintf(fd, "%d %d\n", prevuid, quotasum);
384 EXEC SQL CLOSE q_cursor;
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, l.gid, q.phys_id, n.device, n.mach_id, n.status
396 FROM quota q, list l, nfsphys n
397 WHERE l.list_id = q.entity_id AND n.nfsphys_id = q.phys_id AND
398 q.phys_id != 0 AND n.status > 15 AND q.type = "GROUP"
399 ORDER BY mach_id, phys_id, gid;
400 EXEC SQL OPEN q_cursor2;
402 EXEC SQL FETCH q_cursor2 INTO :quota, :gid, :flag1, :dev,
404 if (sqlca.sqlcode != 0) break;
405 if (flag1 != olddev || flag2 != oldmach) {
409 while (cp = index(dev, '/')) *cp = '@';
410 sprintf(file, "%s/%s.%s.quotas", nfs_dir,
411 hash_lookup(machines, flag2), strtrim(dev));
412 fd = fopen(file, "w");
414 fprintf(stderr, "cannot open %s for output\n", file);
420 if (gid != prevuid) {
422 fprintf(fd, "%d %d\n", prevuid, quotasum);
429 EXEC SQL CLOSE q_cursor2;
431 fprintf(fd, "%d %d\n", prevuid, quotasum);
432 if (fd != stdin && fclose(fd) == EOF) {
433 fprintf(stderr, "error closing %s", file);
437 olddev = oldmach = -1;
439 EXEC SQL DECLARE q_cursor3 CURSOR FOR
440 SELECT DISTINCT f.name, f.lockertype, u.uid, l.gid, f.phys_id,
442 FROM users u, list l, nfsphys n, filesys f
443 WHERE u.users_id = f.owner AND
444 l.list_id = f.owners AND
445 f.createflg != 0 AND f.phys_id != 0 AND
447 f.phys_id = n.nfsphys_id
448 ORDER BY mach_id, phys_id;
449 EXEC SQL OPEN q_cursor3;
451 EXEC SQL FETCH q_cursor3 INTO :dir, :fstype, :uid, :gid, :flag1,
453 if (sqlca.sqlcode != 0) break;
454 if (flag1 != olddev || flag2 != oldmach) {
458 while (cp = index(dev, '/')) *cp = '@';
459 sprintf(file, "%s/%s.%s.dirs", nfs_dir,
460 hash_lookup(machines, flag2), strtrim(dev));
461 fd = fopen(file, "w");
463 fprintf(stderr, "cannot open %s for output\n", file);
467 fprintf(fd, "%s %d %d %s\n", strtrim(dir), uid, gid, strtrim(fstype));
469 EXEC SQL CLOSE q_cursor3;
470 if (fclose(fd) == EOF) {
471 fprintf(stderr, "error closing %s", file);
476 com_err(whoami, MR_INGRES_ERR, " code %d\n", sqlca.sqlcode);
477 critical_alert("DCM", "NFS build encountered DATABASE ERROR %d",