3 * This generates the files necessary to load an nfs server.
5 * Copyright (C) 1988-1998 by the Massachusetts Institute of Technology.
6 * For copying and distribution information, please see the file
10 #include <mit-copyright.h>
12 #include <moira_site.h>
15 #include <sys/types.h>
24 EXEC SQL INCLUDE sqlca;
28 #define min(x, y) ((x) < (y) ? (x) : (y))
30 char *whoami = "nfs.gen";
31 char *db = "moira/moira";
32 char nfs_dir[MAXPATHLEN];
33 struct hash *users, *groups;
36 int do_lists(struct save_queue *lists);
37 void do_everyone(void);
38 int do_machs(struct save_queue *machs);
40 int main(int argc, char **argv)
48 fprintf(stderr, "usage: %s [outfile]\n", argv[0]);
52 initialize_sms_error_table();
53 sprintf(nfs_dir, "%s/nfs", DCM_DIR);
63 fprintf(stderr, "No files updated.\n");
64 if (argc == 2 && stat(argv[1], &sb) == 0)
70 sprintf(cmd, "cd %s; cp %s/nfs/* .; tar cf %s .",
71 nfs_dir, MOIRA_DIR, argv[1]);
80 /* Generate the files. Returns zero if nothing changed, non-zero otherwise. */
84 EXEC SQL BEGIN DECLARE SECTION;
85 char machname[MACHINE_NAME_SIZE], listname[SERVERHOSTS_VALUE3_SIZE];
86 EXEC SQL END DECLARE SECTION;
87 struct save_queue *machs, *lists;
93 /* The following is declarative, not executed,
94 * and so is dependent on where it is in the file,
95 * not in the order of execution of statements.
97 EXEC SQL WHENEVER SQLERROR GOTO sqlerr;
99 EXEC SQL DECLARE s_cursor CURSOR FOR
100 SELECT m.name, s.value3
101 FROM machine m, serverhosts s
102 WHERE m.mach_id = s.mach_id AND s.service = 'NFS' AND s.enable != 0;
103 EXEC SQL OPEN s_cursor;
106 EXEC SQL FETCH s_cursor INTO :machname, :listname;
109 sq_save_unique_string(machs, strdup(strtrim(machname)));
110 sq_save_unique_string(lists, strdup(strtrim(listname)));
112 EXEC SQL CLOSE s_cursor;
114 changed = do_lists(lists);
116 changed += do_machs(machs);
119 db_error(sqlca.sqlcode);
124 /* Make all of the credentials lists that will be needed. Returns 0 if
125 * no files were actually changed
128 int do_lists(struct save_queue *lists)
130 char file[MAXPATHLEN], *u;
132 EXEC SQL BEGIN DECLARE SECTION;
135 EXEC SQL END DECLARE SECTION;
137 sprintf(file, "%s/list-", nfs_dir);
139 if (stat(file, &sb) == 0)
141 if ((ModDiff (&flag1, "users", sb.st_mtime)) ||
142 (ModDiff (&flag2, "list", sb.st_mtime)) ||
143 (ModDiff (&flag3, "imembers", sb.st_mtime)) ||
144 (ModDiff (&flag4, "serverhosts", sb.st_mtime)))
146 if (flag1 < 0 && flag2 < 0 && flag3 < 0 && flag4 < 0)
148 fprintf(stderr, "The lists do not need to be rebuilt.\n");
154 /* build the list of everyone, and store it in a file whose name
155 * corresponds to the empty list.
159 fprintf(stderr, "Building specific lists\n");
160 /* now do each of the lists used by an NFS server */
162 while (sq_get_data(lists, &listname))
164 if (strlen(listname) == 0)
166 sprintf(file, "%s/list-%s", nfs_dir, listname);
167 fd = fopen(file, "w");
170 fprintf(stderr, "cannot open %s for output\n", file);
174 EXEC SQL DECLARE m_cursor CURSOR FOR
176 FROM imembers m, list l
177 WHERE m.list_id = l.list_id AND l.name = :listname
178 AND m.member_type = 'USER'
180 EXEC SQL OPEN m_cursor;
183 EXEC SQL FETCH m_cursor INTO :id;
186 if ((u = hash_lookup(users, id)))
187 fprintf(fd, "%s\n", u);
189 EXEC SQL CLOSE m_cursor;
190 if (fclose(fd) == EOF)
192 fprintf(stderr, "error closing %s\n", file);
196 /* don't free here either
201 db_error(sqlca.sqlcode);
206 /* Build the list of everybody. */
213 char name[USERS_LOGIN_SIZE];
219 void do_everyone(void)
221 char buf[MAXPATHLEN], *l;
224 struct bucket *b, **p;
225 EXEC SQL BEGIN DECLARE SECTION;
226 char name[USERS_LOGIN_SIZE];
227 int gid, id, lid, uid;
228 EXEC SQL END DECLARE SECTION;
231 fprintf(stderr, "Building the list of everybody\n");
232 sprintf(buf, "%s/list-", nfs_dir);
233 fd = fopen(buf, "w");
236 fprintf(stderr, "cannot open %s for output\n", buf);
240 /* make space for group list */
241 groups = create_hash(15000);
243 /* retrieve simple groups */
244 EXEC SQL DECLARE l_cursor CURSOR FOR
247 WHERE grouplist != 0 AND active != 0
249 EXEC SQL OPEN l_cursor;
252 EXEC SQL FETCH l_cursor INTO :gid, :lid;
255 sprintf(buf, ":%d", gid);
256 hash_store(groups, lid, strdup(buf));
258 EXEC SQL CLOSE l_cursor;
260 /* now do grplists */
261 users = create_hash(10000);
262 EXEC SQL DECLARE u_cursor CURSOR FOR
263 SELECT users_id, login, unix_uid
267 EXEC SQL OPEN u_cursor;
270 EXEC SQL FETCH u_cursor INTO :id, :name, :uid;
273 u = malloc(sizeof(struct user));
274 strcpy(u->name, strtrim(name));
277 hash_store(users, id, u);
279 EXEC SQL CLOSE u_cursor;
281 EXEC SQL DECLARE m_cursor2 CURSOR FOR
282 SELECT list_id, member_id
284 WHERE member_type = 'USER'
286 EXEC SQL OPEN m_cursor2;
289 EXEC SQL FETCH m_cursor2 INTO :lid, :id;
292 if ((u = hash_lookup(users, id)) && (l = hash_lookup(groups, lid)))
294 g = malloc(sizeof(struct grp));
300 EXEC SQL CLOSE m_cursor2;
302 for (p = &(users->data[users->size - 1]); p >= users->data; p--)
304 for (b = *p; b; b = b->next)
306 u = (struct user *)b->data;
307 sprintf(buf, "%s:%d:101", u->name, u->uid);
308 for (g = u->lists; g; g = g->next)
310 b->data = strdup(buf);
311 fprintf(fd, "%s\n", buf);
319 db_error(sqlca.sqlcode);
324 /* Now do each of the servers, linking the credentials list file and
325 * compiling the quota and dirs files.
328 int do_machs(struct save_queue *machs)
330 EXEC SQL BEGIN DECLARE SECTION;
331 char *machname, listname[SERVERHOSTS_VALUE3_SIZE];
332 char dev[NFSPHYS_DEVICE_SIZE], dir[FILESYS_NAME_SIZE];
333 char fstype[FILESYS_LOCKERTYPE_SIZE];
334 int uid, quota, id, gid, flag1, flag2, flag3;
335 EXEC SQL END DECLARE SECTION;
336 char file[MAXPATHLEN], f1[MAXPATHLEN], f2[MAXPATHLEN], *cp;
337 int prevuid, quotasum, olddev, oldmach;
339 struct hash *machines;
341 fprintf(stderr, "Building machine files\n");
343 machines = create_hash(100);
344 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 */
357 fprintf(stderr, "Cannot link %s to %s\n", f1, f2);
360 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;
375 EXEC SQL FETCH q_cursor INTO :quota, :uid, :flag1, :dev, :flag2;
378 if (flag1 != olddev || flag2 != oldmach)
381 fprintf(fd, "%d %d\n", prevuid, quotasum);
382 if (flag2 == 0 || !hash_lookup(machines, flag2))
388 while ((cp = strchr(dev, '/')))
390 sprintf(file, "%s/%s.%s.quotas", nfs_dir,
391 (char *)hash_lookup(machines, flag2), strtrim(dev));
392 fd = fopen(file, "w");
395 fprintf(stderr, "cannot open %s for output\n", file);
403 if ((cp = hash_lookup(users, prevuid)) &&
404 (cp = strchr(cp, ':')))
405 prevuid = atoi(cp + 1);
407 fprintf(fd, "%d %d\n", prevuid, quotasum);
414 EXEC SQL CLOSE q_cursor;
415 if ((cp = hash_lookup(users, prevuid)) &&
416 (cp = strchr(cp, ':')))
417 prevuid = atoi(cp + 1);
419 fprintf(fd, "%d %d\n", prevuid, quotasum);
420 if (fd != stdin && fclose(fd) == EOF)
422 fprintf(stderr, "error closing %s", file);
426 olddev = oldmach = -1;
428 EXEC SQL DECLARE q_cursor2 CURSOR FOR
429 SELECT DISTINCT q.quota, q.entity_id, q.phys_id, n.device, n.mach_id,
431 FROM quota q, nfsphys n
432 WHERE n.nfsphys_id = q.phys_id AND q.phys_id != 0 AND
433 n.status > 15 AND q.type = 'GROUP'
434 ORDER BY mach_id, phys_id, entity_id;
435 EXEC SQL OPEN q_cursor2;
438 EXEC SQL FETCH q_cursor2 INTO :quota, :gid, :flag1, :dev,
442 if (flag1 != olddev || flag2 != oldmach)
445 fprintf(fd, "%d %d\n", prevuid, quotasum);
446 if (flag2 == 0 || !hash_lookup(machines, flag2))
452 while ((cp = strchr(dev, '/')))
454 sprintf(file, "%s/%s.%s.quotas", nfs_dir,
455 (char *)hash_lookup(machines, flag2), strtrim(dev));
456 fd = fopen(file, "w");
459 fprintf(stderr, "cannot open %s for output\n", file);
467 if ((cp = hash_lookup(groups, prevuid)))
468 prevuid = atoi(cp + 1);
470 fprintf(fd, "%d %d\n", prevuid, quotasum);
477 EXEC SQL CLOSE q_cursor2;
478 if ((cp = hash_lookup(groups, prevuid)))
479 prevuid = atoi(cp + 1);
481 fprintf(fd, "%d %d\n", prevuid, quotasum);
482 if (fd != stdin && fclose(fd) == EOF)
484 fprintf(stderr, "error closing %s", file);
488 olddev = oldmach = -1;
491 EXEC SQL DECLARE q_cursor3 CURSOR FOR
492 SELECT DISTINCT f.name, f.lockertype, u.unix_uid, l.gid, f.phys_id,
494 FROM users u, list l, nfsphys n, filesys f
495 WHERE u.users_id = f.owner AND l.list_id = f.owners
496 AND f.createflg != 0 AND f.phys_id != 0 AND f.type = 'NFS'
497 AND f.phys_id = n.nfsphys_id
498 ORDER BY mach_id, phys_id;
499 EXEC SQL OPEN q_cursor3;
502 EXEC SQL FETCH q_cursor3 INTO :dir, :fstype, :uid, :gid, :flag1,
506 if ((flag1 != olddev || flag2 != oldmach) &&
507 hash_lookup(machines, flag2))
513 while ((cp = strchr(dev, '/')))
515 sprintf(file, "%s/%s.%s.dirs", nfs_dir,
516 (char *)hash_lookup(machines, flag2), strtrim(dev));
517 fd = fopen(file, "w");
520 fprintf(stderr, "cannot open %s for output\n", file);
524 fprintf(fd, "%s %d %d %s\n", strtrim(dir), uid, gid, strtrim(fstype));
526 EXEC SQL CLOSE q_cursor3;
527 if (fclose(fd) == EOF)
529 fprintf(stderr, "error closing %s", file);
534 db_error(sqlca.sqlcode);