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);
46 EXEC SQL DATABASE sms;
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 EXEC SQL WHENEVER SQLERROR GOTO sqlerr;
89 EXEC SQL DECLARE s_cursor CURSOR FOR
90 SELECT m.name, s.value3
91 FROM machine m, serverhosts s
92 WHERE m.mach_id = s.mach_id AND s.service = "NFS" AND s.enable != 0;
93 EXEC SQL OPEN s_cursor;
95 EXEC SQL FETCH s_cursor INTO :machname, :listname;
96 if (sqlca.sqlcode != 0) break;
97 sq_save_unique_string(machs, strsave(strtrim(machname)));
98 sq_save_unique_string(lists, strsave(strtrim(listname)));
100 EXEC SQL CLOSE s_cursor;
102 changed = do_lists(lists);
103 changed += do_machs(machs);
106 com_err(whoami, MR_INGRES_ERR, " code %d\n", sqlca.sqlcode);
107 critical_alert("DCM", "NFS build encountered DATABASE ERROR %d",
113 /* Make all of the credentials lists that will be needed. Returns 0 if
114 * no files were actually changed */
117 struct save_queue *lists;
120 struct hash *users, *do_everyone();
123 EXEC SQL BEGIN DECLARE SECTION;
124 char *listname, *lsname, lname[33], uname[9];
125 int uid, id, flag1, flag2, flag3, flag4;
126 EXEC SQL END DECLARE SECTION;
128 sprintf(file, "%s/list-", nfs_dir);
129 if (stat(file, &sb) == 0) {
130 if ((ModDiff (&flag1, "users", sb.st_mtime)) ||
131 (ModDiff (&flag2, "list", sb.st_mtime)) ||
132 (ModDiff (&flag3, "imembers", sb.st_mtime)) ||
133 (ModDiff (&flag4, "serverhosts", sb.st_mtime))) exit (MR_DATE);
134 if (flag1 < 0 && flag2 < 0 && flag3 < 0 && flag4 < 0) {
135 fprintf(stderr, "The lists do not need to be rebuilt.\n");
141 EXEC SQL SELECT modtime INTO :lname FROM list WHERE list_id = 0;
142 EXEC SQL SELECT modtime INTO :lname FROM users WHERE users_id = 0;
144 /* build the list of everyone, and store it in a file whose name
145 * corresponds to the empty list.
147 users = do_everyone();
149 fprintf(stderr, "Building specific lists\n");
150 /* now do each of the lists used by an NFS server */
152 while (sq_get_data(lists, &listname)) {
153 if (strlen(listname) == 0)
155 sprintf(file, "%s/list-%s", nfs_dir, listname);
156 fd = fopen(file, "w");
158 fprintf(stderr, "cannot open %s for output\n", file);
162 EXEC SQL DECLARE m_cursor CURSOR FOR
164 FROM imembers m, list l
165 WHERE m.list_id=l.list_id AND l.name = :listname AND
166 m.member_type="USER";
167 EXEC SQL OPEN m_cursor;
169 EXEC SQL FETCH m_cursor INTO :id;
170 if (sqlca.sqlcode != 0) break;
171 if (u = hash_lookup(users, id))
172 fprintf(fd, "%s\n", u);
174 EXEC SQL CLOSE m_cursor;
175 if (fclose(fd) == EOF) {
176 fprintf(stderr, "error closing %s\n", file);
180 /* don't free here either
185 com_err(whoami, MR_INGRES_ERR, " code %d\n", sqlca.sqlcode);
186 critical_alert("DCM", "NFS build encountered DATABASE ERROR %d",
192 /* Build the list of everybody. */
204 struct hash *do_everyone()
206 char buf[BUFSIZ], *l;
207 struct hash *groups, *users;
210 struct bucket *b, **p;
211 EXEC SQL BEGIN DECLARE SECTION;
213 int gid, id, lid, maxid, uid;
214 EXEC SQL END DECLARE SECTION;
217 struct save_queue *sq;
219 fprintf(stderr, "Building the list of everybody\n");
220 sprintf(buf, "%s/list-", nfs_dir);
221 fd = fopen(buf, "w");
223 fprintf(stderr, "cannot open %s for output\n", buf);
227 /* make space for group list */
228 groups = create_hash(15000);
230 /* retrieve simple groups */
231 EXEC SQL DECLARE l_cursor CURSOR FOR
234 WHERE grp <> 0 AND active != 0;
235 EXEC SQL OPEN l_cursor;
237 EXEC SQL FETCH l_cursor INTO :gid, :lid;
238 if (sqlca.sqlcode != 0) break;
239 sprintf(buf, ":%d", gid);
240 hash_store(groups, lid, strsave(buf));
242 EXEC SQL CLOSE l_cursor;
244 /* now do grplists */
245 users = create_hash(10000);
246 EXEC SQL DECLARE u_cursor CURSOR FOR
247 SELECT users_id, login, uid
250 EXEC SQL OPEN u_cursor;
252 EXEC SQL FETCH u_cursor INTO :id, :name, :uid;
253 if (sqlca.sqlcode != 0) break;
254 u = (struct user *) malloc(sizeof(struct user));
255 strcpy(u->name, strtrim(name));
258 hash_store(users, id, u);
260 EXEC SQL CLOSE u_cursor;
262 EXEC SQL DECLARE m_cursor2 CURSOR FOR
263 SELECT list_id, member_id
265 WHERE member_type = "USER";
266 EXEC SQL OPEN m_cursor2;
268 EXEC SQL FETCH m_cursor2 INTO :lid, :id;
269 if (sqlca.sqlcode != 0) break;
270 if ((u = (struct user *) hash_lookup(users, id)) &&
271 ((l = hash_lookup(groups, lid)) != NULL)) {
272 g = (struct grp *) malloc(sizeof(struct grp));
278 EXEC SQL CLOSE m_cursor2;
280 for (p = &(users->data[users->size - 1]); p >= users->data; p--) {
281 for (b = *p; b; b = b->next) {
282 u = (struct user *)b->data;
283 sprintf(buf, "%s:%d", u->name, u->uid);
284 for (g = u->lists; g; g = g->next)
286 b->data = strsave(buf);
287 fprintf(fd, "%s\n", buf);
295 com_err(whoami, MR_INGRES_ERR, " code %d\n", sqlca.sqlcode);
296 critical_alert("DCM", "NFS build encountered DATABASE ERROR %d",
302 /* Now do each of the servers, linking the credentials list file and
303 * compiling the quota and dirs files.
307 struct save_queue *machs;
309 EXEC SQL BEGIN DECLARE SECTION;
310 char *machname, listname[33], dev[33], dir[81], fstype[9];
311 int uid, quota, id, gid, flag1, flag2, flag3, flag4;
312 EXEC SQL END DECLARE SECTION;
313 char file[64], f1[64], f2[64], *cp, *index();
314 int prevuid, quotasum, olddev, oldmach;
316 struct hash *machines;
318 fprintf(stderr, "Building machine files\n");
320 /* acquire locks on machines & filesystems */
321 EXEC SQL SELECT modtime INTO :listname FROM users WHERE users_id = 0;
322 EXEC SQL SELECT modtime INTO :listname FROM machine WHERE users_id = 0;
323 EXEC SQL SELECT modtime INTO :listname FROM filesys WHERE filsys_id = 0;
325 machines = create_hash(100);
326 while (sq_get_data(machs, &machname)) {
327 EXEC SQL SELECT s.value3, m.mach_id
329 FROM serverhosts s, machine m
330 WHERE s.mach_id = m.mach_id AND m.name = :machname;
332 sprintf(f1, "%s/list-%s", nfs_dir, strtrim(listname));
333 sprintf(f2, "%s/%s.cred", nfs_dir, machname);
334 unlink(f2); /* ignore errors on this unlink */
336 fprintf(stderr, "Cannot link %s to %s\n", f1, f2);
339 hash_store(machines, id, machname);
343 olddev = oldmach = -1;
346 EXEC SQL DECLARE q_cursor CURSOR FOR
347 SELECT DISTINCT q.quota, u.uid, q.phys_id, n.device, n.mach_id
348 FROM quota q, users u, nfsphys n
349 WHERE u.users_id = q.entity_id AND n.nfsphys_id = q.phys_id AND
350 q.phys_id != 0 AND n.status < 16 AND q.type = "USER"
351 ORDER BY mach_id, phys_id, uid;
352 EXEC SQL OPEN q_cursor;
354 EXEC SQL FETCH q_cursor INTO :quota, :uid, :flag1, :dev, :flag2;
355 if (sqlca.sqlcode != 0) break;
356 if (flag1 != olddev || flag2 != oldmach) {
360 while (cp = index(dev, '/')) *cp = '@';
361 sprintf(file, "%s/%s.%s.quotas", nfs_dir,
362 hash_lookup(machines, flag2), strtrim(dev));
363 fd = fopen(file, "w");
365 fprintf(stderr, "cannot open %s for output\n", file);
371 if (uid != prevuid) {
373 fprintf(fd, "%d %d\n", prevuid, quotasum);
380 EXEC SQL CLOSE q_cursor;
382 fprintf(fd, "%d %d\n", prevuid, quotasum);
383 if (fd != stdin && fclose(fd) == EOF) {
384 fprintf(stderr, "error closing %s", file);
388 olddev = oldmach = -1;
390 EXEC SQL DECLARE q_cursor2 CURSOR FOR
391 SELECT DISTINCT q.quota, l.gid, q.phys_id, n.device, n.mach_id, n.status
392 FROM quota q, list l, nfsphys n
393 WHERE l.list_id = q.entity_id AND n.nfsphys_id = q.phys_id AND
394 q.phys_id != 0 AND n.status > 15 AND q.type = "GROUP"
395 ORDER BY mach_id, phys_id, gid;
396 EXEC SQL OPEN q_cursor2;
398 EXEC SQL FETCH q_cursor2 INTO :quota, :gid, :flag1, :dev,
400 if (sqlca.sqlcode != 0) break;
401 if (flag1 != olddev || flag2 != oldmach) {
405 while (cp = index(dev, '/')) *cp = '@';
406 sprintf(file, "%s/%s.%s.quotas", nfs_dir,
407 hash_lookup(machines, flag2), strtrim(dev));
408 fd = fopen(file, "w");
410 fprintf(stderr, "cannot open %s for output\n", file);
416 if (gid != prevuid) {
418 fprintf(fd, "%d %d\n", prevuid, quotasum);
425 EXEC SQL CLOSE q_cursor2;
427 fprintf(fd, "%d %d\n", prevuid, quotasum);
428 if (fd != stdin && fclose(fd) == EOF) {
429 fprintf(stderr, "error closing %s", file);
433 olddev = oldmach = -1;
435 EXEC SQL DECLARE q_cursor3 CURSOR FOR
436 SELECT DISTINCT f.name, f.lockertype, u.uid, l.gid, f.phys_id,
438 FROM users u, list l, nfsphys n, filesys f
439 WHERE u.users_id = f.owner AND
440 l.list_id = f.owners AND
441 f.createflg != 0 AND f.phys_id != 0 AND
443 f.phys_id = n.nfsphys_id
444 ORDER BY mach_id, phys_id;
445 EXEC SQL OPEN q_cursor3;
447 EXEC SQL FETCH q_cursor3 INTO :dir, :fstype, :uid, :gid, :flag1,
449 if (sqlca.sqlcode != 0) break;
450 if (flag1 != olddev || flag2 != oldmach) {
454 while (cp = index(dev, '/')) *cp = '@';
455 sprintf(file, "%s/%s.%s.dirs", nfs_dir,
456 hash_lookup(machines, flag2), strtrim(dev));
457 fd = fopen(file, "w");
459 fprintf(stderr, "cannot open %s for output\n", file);
463 fprintf(fd, "%s %d %d %s\n", strtrim(dir), uid, gid, strtrim(fstype));
465 EXEC SQL CLOSE q_cursor3;
466 if (fclose(fd) == EOF) {
467 fprintf(stderr, "error closing %s", file);
472 com_err(whoami, MR_INGRES_ERR, " code %d\n", sqlca.sqlcode);
473 critical_alert("DCM", "NFS build encountered DATABASE ERROR %d",