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 const buflen = MAXPATHLEN;
222 char buf[buflen], *l;
225 struct bucket *b, **p;
226 EXEC SQL BEGIN DECLARE SECTION;
227 char name[USERS_LOGIN_SIZE];
228 int gid, id, lid, uid;
229 EXEC SQL END DECLARE SECTION;
232 fprintf(stderr, "Building the list of everybody\n");
233 sprintf(buf, "%s/list-", nfs_dir);
234 fd = fopen(buf, "w");
237 fprintf(stderr, "cannot open %s for output\n", buf);
241 /* make space for group list */
242 groups = create_hash(15000);
244 /* retrieve simple groups */
245 EXEC SQL DECLARE l_cursor CURSOR FOR
248 WHERE grouplist != 0 AND active != 0
250 EXEC SQL OPEN l_cursor;
253 EXEC SQL FETCH l_cursor INTO :gid, :lid;
256 sprintf(buf, ":%d", gid);
257 hash_store(groups, lid, strdup(buf));
259 EXEC SQL CLOSE l_cursor;
261 /* now do grplists */
262 users = create_hash(10000);
263 EXEC SQL DECLARE u_cursor CURSOR FOR
264 SELECT users_id, login, unix_uid
268 EXEC SQL OPEN u_cursor;
271 EXEC SQL FETCH u_cursor INTO :id, :name, :uid;
274 u = 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;
290 EXEC SQL FETCH m_cursor2 INTO :lid, :id;
293 if ((u = hash_lookup(users, id)) && (l = hash_lookup(groups, lid)))
295 g = malloc(sizeof(struct grp));
301 EXEC SQL CLOSE m_cursor2;
303 for (p = &(users->data[users->size - 1]); p >= users->data; p--)
305 for (b = *p; b; b = b->next)
307 u = (struct user *)b->data;
308 sprintf(buf, "%s:%d:101", u->name, u->uid);
309 for (g = u->lists; g; g = g->next)
311 if ((strlen(buf) + strlen(g->lid)) <= buflen)
315 com_err(whoami, 0, "truncated server-side grp list for %s",
320 b->data = strdup(buf);
321 fprintf(fd, "%s\n", buf);
329 db_error(sqlca.sqlcode);
334 /* Now do each of the servers, linking the credentials list file and
335 * compiling the quota and dirs files.
338 int do_machs(struct save_queue *machs)
340 EXEC SQL BEGIN DECLARE SECTION;
341 char *machname, listname[SERVERHOSTS_VALUE3_SIZE];
342 char dev[NFSPHYS_DEVICE_SIZE], dir[FILESYS_NAME_SIZE];
343 char fstype[FILESYS_LOCKERTYPE_SIZE];
344 int uid, quota, id, gid, flag1, flag2, flag3;
345 EXEC SQL END DECLARE SECTION;
346 char file[MAXPATHLEN], f1[MAXPATHLEN], f2[MAXPATHLEN], *cp;
347 int prevuid, quotasum, olddev, oldmach;
349 struct hash *machines;
351 fprintf(stderr, "Building machine files\n");
353 machines = create_hash(100);
354 while (sq_get_data(machs, &machname))
356 EXEC SQL SELECT s.value3, m.mach_id
358 FROM serverhosts s, machine m
359 WHERE s.mach_id = m.mach_id AND m.name = :machname AND
362 sprintf(f1, "%s/list-%s", nfs_dir, strtrim(listname));
363 sprintf(f2, "%s/%s.cred", nfs_dir, machname);
364 unlink(f2); /* ignore errors on this unlink */
367 fprintf(stderr, "Cannot link %s to %s\n", f1, f2);
370 hash_store(machines, id, machname);
373 olddev = oldmach = -1;
376 EXEC SQL DECLARE q_cursor CURSOR FOR
377 SELECT DISTINCT q.quota, q.entity_id, q.phys_id, n.device, n.mach_id
378 FROM quota q, nfsphys n
379 WHERE n.nfsphys_id = q.phys_id AND q.phys_id != 0 AND
380 n.status < 16 AND q.type = 'USER'
381 ORDER BY n.mach_id, q.phys_id, q.entity_id;
382 EXEC SQL OPEN q_cursor;
385 EXEC SQL FETCH q_cursor INTO :quota, :uid, :flag1, :dev, :flag2;
388 if (flag1 != olddev || flag2 != oldmach)
391 fprintf(fd, "%d %d\n", prevuid, quotasum);
392 if (flag2 == 0 || !hash_lookup(machines, flag2))
398 while ((cp = strchr(dev, '/')))
400 sprintf(file, "%s/%s.%s.quotas", nfs_dir,
401 (char *)hash_lookup(machines, flag2), strtrim(dev));
402 fd = fopen(file, "w");
405 fprintf(stderr, "cannot open %s for output\n", file);
413 if ((cp = hash_lookup(users, prevuid)) &&
414 (cp = strchr(cp, ':')))
415 prevuid = atoi(cp + 1);
417 fprintf(fd, "%d %d\n", prevuid, quotasum);
424 EXEC SQL CLOSE q_cursor;
425 if ((cp = hash_lookup(users, prevuid)) &&
426 (cp = strchr(cp, ':')))
427 prevuid = atoi(cp + 1);
429 fprintf(fd, "%d %d\n", prevuid, quotasum);
430 if (fd != stdin && fclose(fd) == EOF)
432 fprintf(stderr, "error closing %s", file);
436 olddev = oldmach = -1;
438 EXEC SQL DECLARE q_cursor2 CURSOR FOR
439 SELECT DISTINCT q.quota, q.entity_id, q.phys_id, n.device, n.mach_id,
441 FROM quota q, nfsphys n
442 WHERE n.nfsphys_id = q.phys_id AND q.phys_id != 0 AND
443 n.status > 15 AND q.type = 'GROUP'
444 ORDER BY mach_id, phys_id, entity_id;
445 EXEC SQL OPEN q_cursor2;
448 EXEC SQL FETCH q_cursor2 INTO :quota, :gid, :flag1, :dev,
452 if (flag1 != olddev || flag2 != oldmach)
455 fprintf(fd, "%d %d\n", prevuid, quotasum);
456 if (flag2 == 0 || !hash_lookup(machines, flag2))
462 while ((cp = strchr(dev, '/')))
464 sprintf(file, "%s/%s.%s.quotas", nfs_dir,
465 (char *)hash_lookup(machines, flag2), strtrim(dev));
466 fd = fopen(file, "w");
469 fprintf(stderr, "cannot open %s for output\n", file);
477 if ((cp = hash_lookup(groups, prevuid)))
478 prevuid = atoi(cp + 1);
480 fprintf(fd, "%d %d\n", prevuid, quotasum);
487 EXEC SQL CLOSE q_cursor2;
488 if ((cp = hash_lookup(groups, prevuid)))
489 prevuid = atoi(cp + 1);
491 fprintf(fd, "%d %d\n", prevuid, quotasum);
492 if (fd != stdin && fclose(fd) == EOF)
494 fprintf(stderr, "error closing %s", file);
498 olddev = oldmach = -1;
501 EXEC SQL DECLARE q_cursor3 CURSOR FOR
502 SELECT DISTINCT f.name, f.lockertype, u.unix_uid, l.gid, f.phys_id,
504 FROM users u, list l, nfsphys n, filesys f
505 WHERE u.users_id = f.owner AND l.list_id = f.owners
506 AND f.createflg != 0 AND f.phys_id != 0 AND f.type = 'NFS'
507 AND f.phys_id = n.nfsphys_id
508 ORDER BY mach_id, phys_id;
509 EXEC SQL OPEN q_cursor3;
512 EXEC SQL FETCH q_cursor3 INTO :dir, :fstype, :uid, :gid, :flag1,
516 if ((flag1 != olddev || flag2 != oldmach) &&
517 hash_lookup(machines, flag2))
523 while ((cp = strchr(dev, '/')))
525 sprintf(file, "%s/%s.%s.dirs", nfs_dir,
526 (char *)hash_lookup(machines, flag2), strtrim(dev));
527 fd = fopen(file, "w");
530 fprintf(stderr, "cannot open %s for output\n", file);
534 fprintf(fd, "%s %d %d %s\n", strtrim(dir), uid, gid, strtrim(fstype));
536 EXEC SQL CLOSE q_cursor3;
537 if (fclose(fd) == EOF)
539 fprintf(stderr, "error closing %s", file);
544 db_error(sqlca.sqlcode);