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>
13 #include <moira_site.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 = MR_DEADLOCK;
86 ingres_errno = MR_INGRES_ERR;
88 com_err(whoami, MR_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 = machine.name, listname = s.value3)
106 ## where machine.mach_id = s.mach_id and s.service = "NFS"
107 ## and s.enable != 0 {
108 sq_save_unique_string(machs, strsave(strtrim(machname)));
109 sq_save_unique_string(lists, strsave(strtrim(listname)));
112 changed = do_lists(lists);
113 changed += do_machs(machs);
118 /* Make all of the credentials lists that will be needed. Returns 0 if
119 * no files were actually changed */
122 struct save_queue *lists;
125 struct hash *users, *do_everyone();
128 ## char *listname, *lsname, lname[33], uname[9], *filetime;
129 ## int uid, id, flag1, flag2, flag3, flag4;
131 sprintf(file, "%s/list-", nfs_dir);
132 if (stat(file, &sb) == 0) {
133 filetime = ingres_date_and_time(sb.st_mtime);
134 ## retrieve (flag1 = int4(interval("min", tblstats.modtime - filetime)))
135 ## where tblstats.table = "users"
136 ## retrieve (flag2 = int4(interval("min", tblstats.modtime - filetime)))
137 ## where tblstats.table = "list"
138 ## retrieve (flag3 = int4(interval("min", tblstats.modtime - filetime)))
139 ## where tblstats.table = "imembers"
140 ## retrieve (flag4 = int4(interval("min", tblstats.modtime - filetime)))
141 ## where tblstats.table = "serverhosts"
142 if (flag1 < 0 && flag2 < 0 && flag3 < 0 && flag4 < 0) {
143 fprintf(stderr, "The lists do not need to be rebuilt.\n");
150 ## retrieve (lname = list.modtime) where list.list_id = 0
151 ## retrieve (lname = users.modtime) where users.users_id = 0
153 /* build the list of everyone, and store it in a file whose name
154 * corresponds to the empty list.
156 users = do_everyone();
158 fprintf(stderr, "Building specific lists\n");
159 /* now do each of the lists used by an NFS server */
160 ## range of l is list
161 ## range of l1 is list
162 ## range of m is imembers
163 ## range of u is users
164 while (sq_get_data(lists, &listname)) {
165 if (strlen(listname) == 0)
167 sprintf(file, "%s/list-%s", nfs_dir, listname);
168 fd = fopen(file, "w");
170 fprintf(stderr, "cannot open %s for output\n", file);
173 ## repeat retrieve (id = m.member_id)
174 ## where m.list_id = l1.list_id and l1.name = @listname and
175 ## m.member_type = "USER" {
176 if (u = hash_lookup(users, id))
177 fprintf(fd, "%s\n", u);
180 fprintf(stderr, "error closing %s\n", file);
184 /* don't free here either
192 /* Build the list of everybody. */
203 struct hash *do_everyone()
205 char buf[BUFSIZ], *l;
206 struct hash *groups, *users;
209 struct bucket *b, **p;
211 ## int gid, id, lid, maxid, uid;
214 struct save_queue *sq;
216 fprintf(stderr, "Building the list of everybody\n");
217 sprintf(buf, "%s/list-", nfs_dir);
218 fd = fopen(buf, "w");
220 fprintf(stderr, "cannot open %s for output\n", buf);
224 /* make space for group list */
225 groups = create_hash(15000);
227 /* retrieve simple groups */
228 ## range of l is list
229 ## retrieve (gid = l.#gid, lid = l.list_id)
230 ## where l.group != 0 and l.active != 0 {
231 sprintf(buf, ":%d", gid);
232 hash_store(groups, lid, strsave(buf));
235 /* now do grplists */
236 users = create_hash(10000);
237 ## range of u is users
238 ## retrieve (id = u.users_id, name = u.login, uid = u.#uid)
239 ## where u.status = 1 {
240 u = (struct user *) malloc(sizeof(struct user));
241 strcpy(u->name, strtrim(name));
244 hash_store(users, id, u);
247 ## range of m is imembers
248 ## retrieve (lid = m.list_id, id = m.member_id) where m.member_type = "USER" {
249 if ((u = (struct user *) hash_lookup(users, id)) &&
250 ((l = hash_lookup(groups, lid)) != NULL)) {
251 g = (struct grp *) malloc(sizeof(struct grp));
258 for (p = &(users->data[users->size - 1]); p >= users->data; p--) {
259 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)
264 b->data = strsave(buf);
265 fprintf(fd, "%s\n", buf);
275 /* Now do each of the servers, linking the credentials list file and
276 * compiling the quota and dirs files.
280 struct save_queue *machs;
282 ## char *machname, listname[33], dev[33], dir[81], fstype[9];
283 ## int uid, quota, id, gid, flag1, flag2, flag3, flag4;
284 char file[64], f1[64], f2[64], *cp, *index();
285 int prevuid, quotasum, olddev, oldmach;
287 struct hash *machines;
289 fprintf(stderr, "Building machine files\n");
291 ## range of s is serverhosts
292 ## range of m is machine
293 ## range of n is nfsphys
294 ## range of q is #quota
295 ## range of f is filesys
296 ## range of u is users
297 ## range of l is list
299 /* acquire locks on machines & filesystems */
301 ## retrieve (listname = u.modtime) where u.users_id = 0
302 ## retrieve (listname = m.modtime) where m.mach_id = 0
303 ## retrieve (listname = f.modtime) where f.filsys_id = 0
305 machines = create_hash(100);
306 while (sq_get_data(machs, &machname)) {
307 ## repeat retrieve (listname = s.value3, id = m.mach_id)
308 ## where s.mach_id = m.mach_id and m.name = @machname
310 sprintf(f1, "%s/list-%s", nfs_dir, strtrim(listname));
311 sprintf(f2, "%s/%s.cred", nfs_dir, machname);
312 unlink(f2); /* ignore errors on this unlink */
314 fprintf(stderr, "Cannot link %s to %s\n", f1, f2);
317 hash_store(machines, id, machname);
320 olddev = oldmach = -1;
322 ## retrieve (quota = q.#quota, uid = u.#uid, flag1 = q.phys_id,
323 ## dev = n.device, flag2 = n.mach_id)
324 ## where u.users_id = q.entity_id and n.nfsphys_id = q.phys_id and
325 ## q.phys_id != 0 and n.status < 16 and q.type = "USER"
326 ## sort by #flag2, #flag1, #uid {
327 if (flag1 != olddev || flag2 != oldmach) {
331 while (cp = index(dev, '/')) *cp = '@';
332 sprintf(file, "%s/%s.%s.quotas", nfs_dir,
333 hash_lookup(machines, flag2), strtrim(dev));
334 fd = fopen(file, "w");
336 fprintf(stderr, "cannot open %s for output\n", file);
342 if (uid != prevuid) {
344 fprintf(fd, "%d %d\n", prevuid, quotasum);
352 fprintf(fd, "%d %d\n", prevuid, quotasum);
353 if (fd != stdin && fclose(fd)) {
354 fprintf(stderr, "error closing %s", file);
358 olddev = oldmach = -1;
360 ## retrieve (quota = q.#quota, gid = l.#gid, flag1 = q.phys_id,
361 ## dev = n.device, flag2 = n.mach_id, flag3 = n.status)
362 ## where l.list_id = q.entity_id and n.nfsphys_id = q.phys_id and
363 ## q.phys_id != 0 and n.status > 15 and q.type = "GROUP"
364 ## sort by #flag2, #flag1, #gid {
365 if (flag1 != olddev || flag2 != oldmach) {
369 while (cp = index(dev, '/')) *cp = '@';
370 sprintf(file, "%s/%s.%s.quotas", nfs_dir,
371 hash_lookup(machines, flag2), strtrim(dev));
372 fd = fopen(file, "w");
374 fprintf(stderr, "cannot open %s for output\n", file);
380 if (gid != prevuid) {
382 fprintf(fd, "%d %d\n", prevuid, quotasum);
390 fprintf(fd, "%d %d\n", prevuid, quotasum);
391 if (fd != stdin && fclose(fd)) {
392 fprintf(stderr, "error closing %s", file);
396 olddev = oldmach = -1;
397 ## retrieve (dir = f.#name, fstype = f.lockertype, uid = u.#uid,
398 ## gid = l.#gid, flag1 = f.phys_id, flag2 = f.mach_id,
400 ## where u.users_id = f.owner and l.list_id = f.owners and
401 ## f.createflg != 0 and f.phys_id != 0 and f.type = "NFS" and
402 ## f.phys_id = n.nfsphys_id
403 ## sort by #flag2, #flag1 {
404 if (flag1 != olddev || flag2 != oldmach) {
408 while (cp = index(dev, '/')) *cp = '@';
409 sprintf(file, "%s/%s.%s.dirs", nfs_dir,
410 hash_lookup(machines, flag2), strtrim(dev));
411 fd = fopen(file, "w");
413 fprintf(stderr, "cannot open %s for output\n", file);
417 fprintf(fd, "%s %d %d %s\n", strtrim(dir), uid, gid, strtrim(fstype));
420 fprintf(stderr, "error closing %s", file);