3 * This generates the files necessary to load an nfs server.
13 #define NFS_DIR "/u1/sms/dcm/nfs"
15 #define min(x,y) ((x) < (y) ? (x) : (y))
32 char *whoami = "nfs.gen";
33 char *malloc(), *strsave();
34 char *ingres_date_and_time(), *ingres_time(), *ingres_date();
46 fprintf(stderr, "usage: %s [outfile]\n", argv[0]);
58 fprintf(stderr, "No files updated.\n");
59 if (argc == 2 && stat(argv[1], &sb) == 0)
64 sprintf(cmd, "cd %s; cp /u1/sms/bin/install_* .; tar cf %s .", NFS_DIR, argv[1]);
74 * ingerr: (supposedly) called when Ingres indicates an error.
75 * I have not yet been able to get this to work to intercept a
76 * database open error.
78 #define INGRES_DEADLOCK 4700
80 static int ingerr(num)
88 ingres_errno = SMS_DEADLOCK;
91 ingres_errno = SMS_INGRES_ERR;
93 com_err(whoami, SMS_INGRES_ERR, " code %d\n", *num);
94 critical_alert("DCM", "NFS build encountered INGRES ERROR %d", *num);
99 /* Generate the files. Returns zero if nothing changed, non-zero otherwise. */
103 ## char machname[33], listname[33];
104 struct save_queue *machs, *lists;
109 ## range of s is serverhosts
110 ## retrieve (machname = trim(machine.name), listname = trim(s.value3))
111 ## where machine.mach_id = s.mach_id and s.enable != 0 {
112 sq_save_unique_string(machs, strsave(machname));
113 sq_save_unique_string(lists, strsave(listname));
116 changed = do_lists(lists);
117 changed += do_machs(machs);
122 /* Make all of the credentials lists that will be needed. Returns 0 if
123 * no files were actually changed */
126 struct save_queue *lists;
128 struct save_queue *sq;
130 struct hash *users, *do_everyone();
133 ## char *listname, *lsname, lname[33], uname[9], *filetime;
134 ## int uid, id, flag1, flag2, flag3, flag4;
136 sprintf(file, "%s/list-", NFS_DIR);
137 if (stat(file, &sb) == 0) {
138 filetime = ingres_date_and_time(sb.st_mtime);
139 ## retrieve (flag1 = int4(interval("min", tblstats.modtime - filetime)))
140 ## where tblstats.table = "users"
141 ## retrieve (flag2 = int4(interval("min", tblstats.modtime - filetime)))
142 ## where tblstats.table = "list"
143 ## retrieve (flag3 = int4(interval("min", tblstats.modtime - filetime)))
144 ## where tblstats.table = "members"
145 ## retrieve (flag4 = int4(interval("min", tblstats.modtime - filetime)))
146 ## where tblstats.table = "serverhosts"
147 if (flag1 < 0 && flag2 < 0 && flag3 < 0 && flag4 < 0) {
148 fprintf(stderr, "The lists do not need to be rebuilt.\n");
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 members
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);
174 sq_save_data(sq, listname);
175 while (sq_get_data(sq, &lsname)) {
176 ## repeat retrieve (lname = trim(l.name))
177 ## where m.list_id = l1.list_id and l1.name = @lsname and
178 ## m.member_type = "LIST" and m.member_id = l.list_id {
179 sq_save_unique_string(sq, lname);
181 ## repeat retrieve (id = m.member_id)
182 ## where m.list_id = l1.list_id and l1.name = @lsname and
183 ## m.member_type = "USER" {
184 if (u = hash_lookup(users, id))
185 fprintf(fd, "%s\n", u);
190 fprintf(stderr, "error closing %s\n", file);
199 /* Build the list of everybody. */
210 struct hash *do_everyone()
212 char buf[BUFSIZ], *l;
213 struct hash *groups, *users;
216 struct bucket *b, **p;
218 ## int gid, id, lid, maxid, uid;
221 struct save_queue *sq;
223 fprintf(stderr, "Building the list of everybody\n");
224 sprintf(buf, "%s/list-", NFS_DIR);
225 fd = fopen(buf, "w");
227 fprintf(stderr, "cannot open %s for output\n", buf);
231 /* make space for group list */
232 groups = create_hash(15000);
234 /* retrieve simple groups */
235 ## range of l is list
236 ## retrieve (gid = l.#gid, lid = l.list_id)
237 ## where l.group != 0 and l.active != 0 {
238 sprintf(buf, ":%d", gid);
239 hash_store(groups, lid, strsave(buf));
242 /* get special cases: lists that aren't groups themselves but are
243 * members of groups. */
245 ## range of m is members
246 ## retrieve (gid = list.#gid, lid = l.list_id)
247 ## where l.active != 0 and list.active != 0 and m.member_type = "LIST" and
248 ## m.member_id = l.list_id and m.list_id = list.list_id and
250 if (l = hash_lookup(groups, lid)) {
251 sprintf(buf, "%s:%d", l, gid);
254 sprintf(buf, ":%d", gid);
256 hash_store(groups, lid, strsave(buf));
257 sq_save_data(sq, lid);
260 while (sq_get_data(sq, &id)) {
261 ## repeat retrieve (lid = l.list_id) where l.active != 0 and
262 ## m.member_type = "LIST" and m.member_id = l.list_id and
264 if (l = hash_lookup(groups, lid)) {
265 sprintf(buf, "%s%s", l, hash_lookup(groups, id));
269 l = hash_lookup(groups, id);
271 hash_store(groups, lid, l);
272 sq_save_unique_data(sq, lid);
277 /* now do grplists */
278 users = create_hash(10000);
279 ## range of u is users
280 ## retrieve (id = u.users_id, name = u.login, uid = u.#uid)
281 ## where u.status = 1 {
282 u = (struct user *) malloc(sizeof(struct user));
283 strcpy(u->name, strtrim(name));
286 hash_store(users, id, u);
289 ## repeat retrieve (lid = m.list_id, id = m.member_id)
290 ## where m.member_type = "USER" {
291 if ((u = (struct user *) hash_lookup(users, id)) &&
292 (hash_lookup(groups, lid) != NULL)) {
293 g = (struct grp *) malloc(sizeof(struct grp));
300 for (p = &(users->data[users->size - 1]); p >= users->data; p--) {
301 for (b = *p; b; b = b->next) {
303 u = (struct user *)b->data;
304 sprintf(buf, "%s:%d", u->name, u->uid);
305 for (g = u->lists; g; g = g->next)
306 if (l = hash_lookup(groups, g->id)) {
310 /* should free stuff here... */
312 remove_duplicate_groups(buf);
314 b->data = strsave(buf);
315 fprintf(fd, "%s\n", buf);
324 #define MAXGROUPS 256
326 remove_duplicate_groups(p)
332 int memory[MAXGROUPS], i, *mg = &memory[MAXGROUPS-1];
334 bzero(memory, MAXGROUPS * sizeof(int));
337 while (p = index(p, ':')) {
340 for (mp = memory; *mp && mp < mg; mp++)
342 if (p = index(p, ':'))
355 /* Now do each of the servers, linking the credentials list file and
356 * compiling the quota and dirs files.
360 struct save_queue *machs;
362 ## char *machname, listname[33], dev[33], *device, dir[64], fstype[9];
364 ## int uid, quota, id, gid, flag1, flag2, flag3, flag4;
365 char file[64], f1[64], f2[64], *cp, *index();
366 int prevuid, quotasum;
369 struct save_queue *sq;
372 sprintf(file, "%s/list-", NFS_DIR);
373 if (stat(file, &sb) == 0) {
374 filetime = ingres_date_and_time(sb.st_mtime);
375 # retrieve (flag1 = int4(interval("min", tblstats.modtime - filetime)))
376 # where tblstats.table = "serverhosts"
377 # retrieve (flag2 = int4(interval("min", tblstats.modtime - filetime)))
378 # where tblstats.table = "filesys"
379 # retrieve (flag3 = int4(interval("min", tblstats.modtime - filetime)))
380 # where tblstats.table = "nfsquota"
381 # retrieve (flag4 = int4(interval("min", tblstats.modtime - filetime)))
382 # where tblstats.table = "nfsphys"
383 if (flag1 < 0 && flag2 < 0 && flag3 < 0 && flag4 < 0) {
384 fprintf(stderr, "The machine files do not need to be rebuilt.\n");
390 fprintf(stderr, "Building machine files\n");
392 ## range of s is serverhosts
393 ## range of m is machine
394 ## range of n is nfsphys
395 ## range of q is nfsquota
396 ## range of f is filesys
397 ## range of u is users
398 ## range of l is list
399 while (sq_get_data(machs, &machname)) {
400 ## repeat retrieve (listname = trim(s.value3))
401 ## where s.mach_id = m.mach_id and m.name = @machname
403 sprintf(f1, "%s/list-%s", NFS_DIR, listname);
404 sprintf(f2, "%s/%s.cred", NFS_DIR, machname);
405 unlink(f2); /* ignore errors on this unlink */
407 fprintf(stderr, "Cannot link %s to %s\n", f1, f2);
411 ## repeat retrieve (dev = trim(n.#device))
412 ## where n.mach_id = m.mach_id and m.name = @machname {
413 sq_save_data(sq, strsave(dev));
415 while (sq_get_data(sq, &device)) {
416 ## repeat retrieve (id = n.nfsphys_id) where n.mach_id = m.mach_id and
417 ## m.#name = @machname and n.#device = @device
418 while (cp = index(device, '/')) *cp = '@';
419 sprintf(file, "%s/%s.%s.quotas", NFS_DIR, machname, device);
420 fd = fopen(file, "w");
422 fprintf(stderr, "cannot open %s for output\n", file);
427 ## repeat retrieve (uid = u.#uid, quota = q.#quota)
428 ## where q.users_id = u.users_id and q.filsys_id = f.filsys_id and
429 ## f.phys_id = @id and u.status != 0 sort by #uid {
430 if (uid != prevuid) {
432 fprintf(fd, "%d %d\n", prevuid, quotasum);
440 fprintf(fd, "%d %d\n", prevuid, quotasum);
442 fprintf(stderr, "error closing %s", file);
445 sprintf(file, "%s/%s.%s.dirs", NFS_DIR, machname, device);
446 fd = fopen(file, "w");
448 fprintf(stderr, "cannot open %s for output\n", file);
451 ## repeat retrieve (dir = trim(f.#name), fstype = trim(f.lockertype),
452 ## uid = u.#uid, gid = l.#gid)
453 ## where f.phys_id = @id and f.owner = u.users_id and
454 ## f.owners = l.list_id and f.createflg != 0 {
455 fprintf(fd, "%s %d %d %s\n", dir, uid, gid, fstype);
458 fprintf(stderr, "error closing %s", file);