3 * This generates the files necessary to load an nfs server.
5 * (c) Copyright 1988 by the Massachusetts Institute of Technology.
6 * For copying and distribution information, please see the file
10 #include <mit-copyright.h>
14 #include <sys/types.h>
19 #define NFS_DIR "/u1/sms/dcm/nfs"
21 #define min(x,y) ((x) < (y) ? (x) : (y))
23 char *whoami = "nfs.gen";
24 char *malloc(), *strsave();
25 char *ingres_date_and_time(), *ingres_time(), *ingres_date();
37 fprintf(stderr, "usage: %s [outfile]\n", argv[0]);
42 initialize_sms_error_table();
44 ## set lockmode session where level = table
51 fprintf(stderr, "No files updated.\n");
52 if (argc == 2 && stat(argv[1], &sb) == 0)
57 sprintf(cmd, "cd %s; cp /u1/sms/nfs/* .; tar cf %s .", NFS_DIR, argv[1]);
67 * ingerr: (supposedly) called when Ingres indicates an error.
68 * I have not yet been able to get this to work to intercept a
69 * database open error.
71 #define INGRES_DEADLOCK 4700
73 static int ingerr(num)
81 ingres_errno = SMS_DEADLOCK;
84 ingres_errno = SMS_INGRES_ERR;
86 com_err(whoami, SMS_INGRES_ERR, " code %d\n", *num);
87 critical_alert("DCM", "NFS build encountered INGRES ERROR %d", *num);
92 /* Generate the files. Returns zero if nothing changed, non-zero otherwise. */
96 ## char machname[33], listname[33];
97 struct save_queue *machs, *lists;
102 ## range of s is serverhosts
103 ## retrieve (machname = trim(machine.name), listname = trim(s.value3))
104 ## where machine.mach_id = s.mach_id and s.enable != 0 {
105 sq_save_unique_string(machs, strsave(machname));
106 sq_save_unique_string(lists, strsave(listname));
109 changed = do_lists(lists);
110 changed += do_machs(machs);
115 /* Make all of the credentials lists that will be needed. Returns 0 if
116 * no files were actually changed */
119 struct save_queue *lists;
122 struct hash *users, *do_everyone();
125 ## char *listname, *lsname, lname[33], uname[9], *filetime;
126 ## int uid, id, flag1, flag2, flag3, flag4;
128 sprintf(file, "%s/list-", NFS_DIR);
129 if (stat(file, &sb) == 0) {
130 filetime = ingres_date_and_time(sb.st_mtime);
131 ## retrieve (flag1 = int4(interval("min", tblstats.modtime - filetime)))
132 ## where tblstats.table = "users"
133 ## retrieve (flag2 = int4(interval("min", tblstats.modtime - filetime)))
134 ## where tblstats.table = "list"
135 ## retrieve (flag3 = int4(interval("min", tblstats.modtime - filetime)))
136 ## where tblstats.table = "imembers"
137 ## retrieve (flag4 = int4(interval("min", tblstats.modtime - filetime)))
138 ## where tblstats.table = "serverhosts"
139 if (flag1 < 0 && flag2 < 0 && flag3 < 0 && flag4 < 0) {
140 fprintf(stderr, "The lists do not need to be rebuilt.\n");
147 ## retrieve (lname = list.modtime) where list.list_id = 0
148 ## retrieve (lname = users.modtime) where users.users_id = 0
150 /* build the list of everyone, and store it in a file whose name
151 * corresponds to the empty list.
153 users = do_everyone();
155 fprintf(stderr, "Building specific lists\n");
156 /* now do each of the lists used by an NFS server */
157 ## range of l is list
158 ## range of l1 is list
159 ## range of m is imembers
160 ## range of u is users
161 while (sq_get_data(lists, &listname)) {
162 if (strlen(listname) == 0)
164 sprintf(file, "%s/list-%s", NFS_DIR, listname);
165 fd = fopen(file, "w");
167 fprintf(stderr, "cannot open %s for output\n", file);
170 ## repeat retrieve (id = m.member_id)
171 ## where m.list_id = l1.list_id and l1.name = @lsname and
172 ## m.member_type = "USER" {
173 if (u = hash_lookup(users, id))
174 fprintf(fd, "%s\n", u);
177 fprintf(stderr, "error closing %s\n", file);
181 /* don't free here either
189 /* Build the list of everybody. */
200 struct hash *do_everyone()
202 char buf[BUFSIZ], *l;
203 struct hash *groups, *users;
206 struct bucket *b, **p;
208 ## int gid, id, lid, maxid, uid;
211 struct save_queue *sq;
213 fprintf(stderr, "Building the list of everybody\n");
214 sprintf(buf, "%s/list-", NFS_DIR);
215 fd = fopen(buf, "w");
217 fprintf(stderr, "cannot open %s for output\n", buf);
221 /* make space for group list */
222 groups = create_hash(15000);
224 /* retrieve simple groups */
225 ## range of l is list
226 ## retrieve (gid = l.#gid, lid = l.list_id)
227 ## where l.group != 0 and l.active != 0 {
228 sprintf(buf, ":%d", gid);
229 hash_store(groups, lid, strsave(buf));
232 /* now do grplists */
233 users = create_hash(10000);
234 ## range of u is users
235 ## retrieve (id = u.users_id, name = u.login, uid = u.#uid)
236 ## where u.status = 1 {
237 u = (struct user *) malloc(sizeof(struct user));
238 strcpy(u->name, strtrim(name));
241 hash_store(users, id, u);
244 ## range of m is imembers
245 ## retrieve (lid = m.list_id, id = m.member_id) where m.member_type = "USER" {
246 if ((u = (struct user *) hash_lookup(users, id)) &&
247 (hash_lookup(groups, lid) != NULL)) {
248 g = (struct grp *) malloc(sizeof(struct grp));
255 for (p = &(users->data[users->size - 1]); p >= users->data; p--) {
256 for (b = *p; b; b = b->next) {
258 u = (struct user *)b->data;
259 sprintf(buf, "%s:%d", u->name, u->uid);
260 for (g = u->lists; g; g = g->next)
261 if (l = hash_lookup(groups, g->id)) {
265 /* should free stuff here...
267 remove_duplicate_groups(buf);
269 b->data = strsave(buf);
270 fprintf(fd, "%s\n", buf);
279 #define MAXGROUPS 256
281 remove_duplicate_groups(p)
287 int memory[MAXGROUPS], i, *mg = &memory[MAXGROUPS-1];
289 bzero(memory, MAXGROUPS * sizeof(int));
292 while (p = index(p, ':')) {
295 for (mp = memory; *mp && mp < mg; mp++)
297 if (p = index(p, ':'))
310 /* Now do each of the servers, linking the credentials list file and
311 * compiling the quota and dirs files.
315 struct save_queue *machs;
317 ## char *machname, listname[33], dev[33], *device, dir[64], fstype[9];
319 ## int uid, quota, id, gid, flag1, flag2, flag3, flag4;
320 char file[64], f1[64], f2[64], *cp, *index();
321 int prevuid, quotasum;
324 struct save_queue *sq;
327 sprintf(file, "%s/list-", NFS_DIR);
328 if (stat(file, &sb) == 0) {
329 filetime = ingres_date_and_time(sb.st_mtime);
330 # retrieve (flag1 = int4(interval("min", tblstats.modtime - filetime)))
331 # where tblstats.table = "serverhosts"
332 # retrieve (flag2 = int4(interval("min", tblstats.modtime - filetime)))
333 # where tblstats.table = "filesys"
334 # retrieve (flag3 = int4(interval("min", tblstats.modtime - filetime)))
335 # where tblstats.table = "nfsquota"
336 # retrieve (flag4 = int4(interval("min", tblstats.modtime - filetime)))
337 # where tblstats.table = "nfsphys"
338 if (flag1 < 0 && flag2 < 0 && flag3 < 0 && flag4 < 0) {
339 fprintf(stderr, "The machine files do not need to be rebuilt.\n");
345 fprintf(stderr, "Building machine files\n");
347 ## range of s is serverhosts
348 ## range of m is machine
349 ## range of n is nfsphys
350 ## range of q is nfsquota
351 ## range of f is filesys
352 ## range of u is users
353 ## range of l is list
355 /* acquire locks on users, lists, machines & filesystems */
357 ## retrieve (listname = u.modtime) where u.users_id = 0
358 ## retrieve (listname = l.modtime) where l.list_id = 0
359 ## retrieve (listname = m.modtime) where m.mach_id = 0
360 ## retrieve (listname = f.modtime) where f.filsys_id = 0
362 while (sq_get_data(machs, &machname)) {
363 ## repeat retrieve (listname = trim(s.value3))
364 ## where s.mach_id = m.mach_id and m.name = @machname
366 sprintf(f1, "%s/list-%s", NFS_DIR, listname);
367 sprintf(f2, "%s/%s.cred", NFS_DIR, machname);
368 unlink(f2); /* ignore errors on this unlink */
370 fprintf(stderr, "Cannot link %s to %s\n", f1, f2);
374 ## repeat retrieve (dev = trim(n.#device))
375 ## where n.mach_id = m.mach_id and m.name = @machname {
376 sq_save_data(sq, strsave(dev));
378 while (sq_get_data(sq, &device)) {
379 ## repeat retrieve (id = n.nfsphys_id) where n.mach_id = m.mach_id and
380 ## m.#name = @machname and n.#device = @device
381 while (cp = index(device, '/')) *cp = '@';
382 sprintf(file, "%s/%s.%s.quotas", NFS_DIR, machname, device);
383 fd = fopen(file, "w");
385 fprintf(stderr, "cannot open %s for output\n", file);
390 ## repeat retrieve (uid = u.#uid, quota = q.#quota)
391 ## where q.users_id = u.users_id and q.filsys_id = f.filsys_id and
392 ## f.phys_id = @id and u.status != 0 sort by #uid {
393 if (uid != prevuid) {
395 fprintf(fd, "%d %d\n", prevuid, quotasum);
403 fprintf(fd, "%d %d\n", prevuid, quotasum);
405 fprintf(stderr, "error closing %s", file);
408 sprintf(file, "%s/%s.%s.dirs", NFS_DIR, machname, device);
409 fd = fopen(file, "w");
411 fprintf(stderr, "cannot open %s for output\n", file);
414 ## repeat retrieve (dir = trim(f.#name), fstype = trim(f.lockertype),
415 ## uid = u.#uid, gid = l.#gid)
416 ## where f.phys_id = @id and f.owner = u.users_id and
417 ## f.owners = l.list_id and f.createflg != 0 {
418 fprintf(fd, "%s %d %d %s\n", dir, uid, gid, fstype);
421 fprintf(stderr, "error closing %s", file);