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 min(x,y) ((x) < (y) ? (x) : (y))
21 char *whoami = "nfs.gen";
22 char *malloc(), *strsave();
23 char *ingres_date_and_time(), *ingres_time(), *ingres_date();
36 fprintf(stderr, "usage: %s [outfile]\n", argv[0]);
41 initialize_sms_error_table();
42 sprintf(nfs_dir, "%s/nfs", DCM_DIR);
45 ## set lockmode session where level = table
52 fprintf(stderr, "No files updated.\n");
53 if (argc == 2 && stat(argv[1], &sb) == 0)
58 sprintf(cmd, "cd %s; cp %s/nfs/* .; tar cf %s .",
59 nfs_dir, SMS_DIR, argv[1]);
69 * ingerr: (supposedly) called when Ingres indicates an error.
70 * I have not yet been able to get this to work to intercept a
71 * database open error.
73 #define INGRES_DEADLOCK 4700
75 static int ingerr(num)
83 ingres_errno = SMS_DEADLOCK;
86 ingres_errno = SMS_INGRES_ERR;
88 com_err(whoami, SMS_INGRES_ERR, " code %d\n", *num);
89 critical_alert("DCM", "NFS build encountered INGRES ERROR %d", *num);
94 /* Generate the files. Returns zero if nothing changed, non-zero otherwise. */
98 ## char machname[33], listname[33];
99 struct save_queue *machs, *lists;
104 ## range of s is serverhosts
105 ## retrieve (machname = trim(machine.name), listname = trim(s.value3))
106 ## where machine.mach_id = s.mach_id and s.enable != 0 {
107 sq_save_unique_string(machs, strsave(machname));
108 sq_save_unique_string(lists, strsave(listname));
111 changed = do_lists(lists);
112 changed += do_machs(machs);
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 ## char *listname, *lsname, lname[33], uname[9], *filetime;
128 ## int uid, id, flag1, flag2, flag3, flag4;
130 sprintf(file, "%s/list-", nfs_dir);
131 if (stat(file, &sb) == 0) {
132 filetime = ingres_date_and_time(sb.st_mtime);
133 ## retrieve (flag1 = int4(interval("min", tblstats.modtime - filetime)))
134 ## where tblstats.table = "users"
135 ## retrieve (flag2 = int4(interval("min", tblstats.modtime - filetime)))
136 ## where tblstats.table = "list"
137 ## retrieve (flag3 = int4(interval("min", tblstats.modtime - filetime)))
138 ## where tblstats.table = "imembers"
139 ## retrieve (flag4 = int4(interval("min", tblstats.modtime - filetime)))
140 ## where tblstats.table = "serverhosts"
141 if (flag1 < 0 && flag2 < 0 && flag3 < 0 && flag4 < 0) {
142 fprintf(stderr, "The lists do not need to be rebuilt.\n");
149 ## retrieve (lname = list.modtime) where list.list_id = 0
150 ## retrieve (lname = users.modtime) where users.users_id = 0
152 /* build the list of everyone, and store it in a file whose name
153 * corresponds to the empty list.
155 users = do_everyone();
157 fprintf(stderr, "Building specific lists\n");
158 /* now do each of the lists used by an NFS server */
159 ## range of l is list
160 ## range of l1 is list
161 ## range of m is imembers
162 ## range of u is users
163 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");
169 fprintf(stderr, "cannot open %s for output\n", file);
172 ## repeat retrieve (id = m.member_id)
173 ## where m.list_id = l1.list_id and l1.name = @lsname and
174 ## m.member_type = "USER" {
175 if (u = hash_lookup(users, id))
176 fprintf(fd, "%s\n", u);
179 fprintf(stderr, "error closing %s\n", file);
183 /* don't free here either
191 /* Build the list of everybody. */
202 struct hash *do_everyone()
204 char buf[BUFSIZ], *l;
205 struct hash *groups, *users;
208 struct bucket *b, **p;
210 ## int gid, id, lid, maxid, uid;
213 struct save_queue *sq;
215 fprintf(stderr, "Building the list of everybody\n");
216 sprintf(buf, "%s/list-", nfs_dir);
217 fd = fopen(buf, "w");
219 fprintf(stderr, "cannot open %s for output\n", buf);
223 /* make space for group list */
224 groups = create_hash(15000);
226 /* retrieve simple groups */
227 ## range of l is list
228 ## retrieve (gid = l.#gid, lid = l.list_id)
229 ## where l.group != 0 and l.active != 0 {
230 sprintf(buf, ":%d", gid);
231 hash_store(groups, lid, strsave(buf));
234 /* now do grplists */
235 users = create_hash(10000);
236 ## range of u is users
237 ## retrieve (id = u.users_id, name = u.login, uid = u.#uid)
238 ## where u.status = 1 {
239 u = (struct user *) malloc(sizeof(struct user));
240 strcpy(u->name, strtrim(name));
243 hash_store(users, id, u);
246 ## range of m is imembers
247 ## retrieve (lid = m.list_id, id = m.member_id) where m.member_type = "USER" {
248 if ((u = (struct user *) hash_lookup(users, id)) &&
249 (hash_lookup(groups, lid) != NULL)) {
250 g = (struct grp *) malloc(sizeof(struct grp));
257 for (p = &(users->data[users->size - 1]); p >= users->data; p--) {
258 for (b = *p; b; b = b->next) {
260 u = (struct user *)b->data;
261 sprintf(buf, "%s:%d", u->name, u->uid);
262 for (g = u->lists; g; g = g->next)
263 if (l = hash_lookup(groups, g->id)) {
267 /* should free stuff here...
269 remove_duplicate_groups(buf);
271 b->data = strsave(buf);
272 fprintf(fd, "%s\n", buf);
281 #define MAXGROUPS 256
283 remove_duplicate_groups(p)
289 int memory[MAXGROUPS], i, *mg = &memory[MAXGROUPS-1];
291 bzero(memory, MAXGROUPS * sizeof(int));
294 while (p = index(p, ':')) {
297 for (mp = memory; *mp && mp < mg; mp++)
299 if (p = index(p, ':'))
312 /* Now do each of the servers, linking the credentials list file and
313 * compiling the quota and dirs files.
317 struct save_queue *machs;
319 ## char *machname, listname[33], dev[33], *device, dir[64], fstype[9];
321 ## int uid, quota, id, gid, flag1, flag2, flag3, flag4;
322 char file[64], f1[64], f2[64], *cp, *index();
323 int prevuid, quotasum;
326 struct save_queue *sq;
329 sprintf(file, "%s/list-", nfs_dir);
330 if (stat(file, &sb) == 0) {
331 filetime = ingres_date_and_time(sb.st_mtime);
332 # retrieve (flag1 = int4(interval("min", tblstats.modtime - filetime)))
333 # where tblstats.table = "serverhosts"
334 # retrieve (flag2 = int4(interval("min", tblstats.modtime - filetime)))
335 # where tblstats.table = "filesys"
336 # retrieve (flag3 = int4(interval("min", tblstats.modtime - filetime)))
337 # where tblstats.table = "nfsquota"
338 # retrieve (flag4 = int4(interval("min", tblstats.modtime - filetime)))
339 # where tblstats.table = "nfsphys"
340 if (flag1 < 0 && flag2 < 0 && flag3 < 0 && flag4 < 0) {
341 fprintf(stderr, "The machine files do not need to be rebuilt.\n");
347 fprintf(stderr, "Building machine files\n");
349 ## range of s is serverhosts
350 ## range of m is machine
351 ## range of n is nfsphys
352 ## range of q is nfsquota
353 ## range of f is filesys
354 ## range of u is users
355 ## range of l is list
357 /* acquire locks on users, lists, machines & filesystems */
359 ## retrieve (listname = u.modtime) where u.users_id = 0
360 ## retrieve (listname = l.modtime) where l.list_id = 0
361 ## retrieve (listname = m.modtime) where m.mach_id = 0
362 ## retrieve (listname = f.modtime) where f.filsys_id = 0
364 while (sq_get_data(machs, &machname)) {
365 ## repeat retrieve (listname = trim(s.value3))
366 ## where s.mach_id = m.mach_id and m.name = @machname
368 sprintf(f1, "%s/list-%s", nfs_dir, listname);
369 sprintf(f2, "%s/%s.cred", nfs_dir, machname);
370 unlink(f2); /* ignore errors on this unlink */
372 fprintf(stderr, "Cannot link %s to %s\n", f1, f2);
376 ## repeat retrieve (dev = trim(n.#device))
377 ## where n.mach_id = m.mach_id and m.name = @machname {
378 sq_save_data(sq, strsave(dev));
380 while (sq_get_data(sq, &device)) {
381 ## repeat retrieve (id = n.nfsphys_id) where n.mach_id = m.mach_id and
382 ## m.#name = @machname and n.#device = @device
383 while (cp = index(device, '/')) *cp = '@';
384 sprintf(file, "%s/%s.%s.quotas", nfs_dir, machname, device);
385 fd = fopen(file, "w");
387 fprintf(stderr, "cannot open %s for output\n", file);
392 ## repeat retrieve (uid = u.#uid, quota = q.#quota)
393 ## where q.users_id = u.users_id and q.filsys_id = f.filsys_id and
394 ## f.phys_id = @id and u.status != 0 sort by #uid {
395 if (uid != prevuid) {
397 fprintf(fd, "%d %d\n", prevuid, quotasum);
405 fprintf(fd, "%d %d\n", prevuid, quotasum);
407 fprintf(stderr, "error closing %s", file);
410 sprintf(file, "%s/%s.%s.dirs", nfs_dir, machname, device);
411 fd = fopen(file, "w");
413 fprintf(stderr, "cannot open %s for output\n", file);
416 ## repeat retrieve (dir = trim(f.#name), fstype = trim(f.lockertype),
417 ## uid = u.#uid, gid = l.#gid)
418 ## where f.phys_id = @id and f.owner = u.users_id and
419 ## f.owners = l.list_id and f.createflg != 0 {
420 fprintf(fd, "%s %d %d %s\n", dir, uid, gid, fstype);
423 fprintf(stderr, "error closing %s", file);