]> andersk Git - moira.git/blame - gen/nfs.qc
fix extraction of recursive members of specific credentials lists
[moira.git] / gen / nfs.qc
CommitLineData
6efe4fbf 1/* $Header$
2 *
3 * This generates the files necessary to load an nfs server.
0a5ff702 4 *
5 * (c) Copyright 1988 by the Massachusetts Institute of Technology.
6 * For copying and distribution information, please see the file
7 * <mit-copyright.h>.
6efe4fbf 8 */
9
0a5ff702 10#include <mit-copyright.h>
6efe4fbf 11#include <stdio.h>
12#include <sms.h>
13#include <sms_app.h>
14#include <sys/types.h>
15#include <sys/stat.h>
16#include <sys/time.h>
17
18#define NFS_DIR "/u1/sms/dcm/nfs"
19
20#define min(x,y) ((x) < (y) ? (x) : (y))
21
3548eee6 22struct np {
23 char mach[33];
24 char dev[33];
25 char list[33];
26};
27
28struct fs {
29 int nfsphys;
30 char *dir;
31 int owner;
32 int owners;
33 struct qt *quota;
34};
35
36
969cfb2b 37char *whoami = "nfs.gen";
6efe4fbf 38char *malloc(), *strsave();
39char *ingres_date_and_time(), *ingres_time(), *ingres_date();
40
41main(argc, argv)
42int argc;
43char **argv;
44{
45 char cmd[64];
46 struct stat sb;
3548eee6 47 int changed = 0;
969cfb2b 48 int ingerr();
6efe4fbf 49
50 if (argc > 2) {
51 fprintf(stderr, "usage: %s [outfile]\n", argv[0]);
52 exit(SMS_ARGS);
53 }
54
969cfb2b 55 IIseterr(ingerr);
6efe4fbf 56## ingres sms
6efe4fbf 57
3548eee6 58 changed = do_nfs();
6efe4fbf 59
60## exit
61
3548eee6 62 if (!changed) {
63 fprintf(stderr, "No files updated.\n");
64 if (argc == 2 && stat(argv[1], &sb) == 0)
65 exit(SMS_NO_CHANGE);
66 }
67
6efe4fbf 68 if (argc == 2) {
969cfb2b 69 sprintf(cmd, "cd %s; cp /u1/sms/bin/install_* .; tar cf %s .", NFS_DIR, argv[1]);
6efe4fbf 70 if (system(cmd))
71 exit(SMS_TAR_FAIL);
72 }
73
74 exit(SMS_SUCCESS);
75}
76
77
969cfb2b 78/*
79 * ingerr: (supposedly) called when Ingres indicates an error.
80 * I have not yet been able to get this to work to intercept a
81 * database open error.
82 */
83#define INGRES_DEADLOCK 4700
84
85static int ingerr(num)
86 int *num;
87{
88 char buf[256];
89 int ingres_errno;
90
91 switch (*num) {
92 case INGRES_DEADLOCK:
93 ingres_errno = SMS_DEADLOCK;
94 break;
95 default:
96 ingres_errno = SMS_INGRES_ERR;
97 }
98 com_err(whoami, SMS_INGRES_ERR, " code %d\n", *num);
99 critical_alert("DCM", "NFS build encountered INGRES ERROR %d", *num);
100 exit(ingres_errno);
101}
102
103
3548eee6 104/* Generate the files. Returns zero if nothing changed, non-zero otherwise. */
105
106int do_nfs()
6efe4fbf 107##{
108## char machname[33], listname[33];
109 struct save_queue *machs, *lists;
3548eee6 110 int changed;
6efe4fbf 111
112 machs = sq_create();
113 lists = sq_create();
114## range of s is serverhosts
115## retrieve (machname = trim(machine.name), listname = trim(s.value3))
116## where machine.mach_id = s.mach_id and s.enable != 0 {
117 sq_save_unique_string(machs, strsave(machname));
118 sq_save_unique_string(lists, strsave(listname));
119## }
120
3548eee6 121 changed = do_lists(lists);
122 changed += do_machs(machs);
123 return(changed);
6efe4fbf 124##}
125
126
3548eee6 127/* Make all of the credentials lists that will be needed. Returns 0 if
128 * no files were actually changed */
129
130int do_lists(lists)
6efe4fbf 131struct save_queue *lists;
132##{
de6a5fe1 133 struct save_queue *sq, *members;
3548eee6 134 char file[64], *u;
135 struct hash *users, *do_everyone();
136 struct stat sb;
6efe4fbf 137 FILE *fd;
3548eee6 138## char *listname, *lsname, lname[33], uname[9], *filetime;
139## int uid, id, flag1, flag2, flag3, flag4;
6efe4fbf 140
3548eee6 141 sprintf(file, "%s/list-", NFS_DIR);
142 if (stat(file, &sb) == 0) {
143 filetime = ingres_date_and_time(sb.st_mtime);
144## retrieve (flag1 = int4(interval("min", tblstats.modtime - filetime)))
145## where tblstats.table = "users"
146## retrieve (flag2 = int4(interval("min", tblstats.modtime - filetime)))
147## where tblstats.table = "list"
148## retrieve (flag3 = int4(interval("min", tblstats.modtime - filetime)))
149## where tblstats.table = "members"
150## retrieve (flag4 = int4(interval("min", tblstats.modtime - filetime)))
151## where tblstats.table = "serverhosts"
152 if (flag1 < 0 && flag2 < 0 && flag3 < 0 && flag4 < 0) {
153 fprintf(stderr, "The lists do not need to be rebuilt.\n");
154 return(0);
155 }
156 }
157
158 /* build the list of everyone, and store it in a file whose name
159 * corresponds to the empty list.
160 */
6efe4fbf 161 users = do_everyone();
162
3548eee6 163 fprintf(stderr, "Building specific lists\n");
164 /* now do each of the lists used by an NFS server */
6efe4fbf 165## range of l is list
166## range of l1 is list
167## range of m is members
168## range of u is users
169 while (sq_get_data(lists, &listname)) {
170 if (strlen(listname) == 0)
171 continue;
172 sprintf(file, "%s/list-%s", NFS_DIR, listname);
173 fd = fopen(file, "w");
174 if (!fd) {
175 fprintf(stderr, "cannot open %s for output\n", file);
176 exit(SMS_OCONFIG);
177 }
178 sq = sq_create();
179 sq_save_data(sq, listname);
de6a5fe1 180 members = sq_create();
6efe4fbf 181 while (sq_get_data(sq, &lsname)) {
182## repeat retrieve (lname = trim(l.name))
183## where m.list_id = l1.list_id and l1.name = @lsname and
184## m.member_type = "LIST" and m.member_id = l.list_id {
de6a5fe1 185 sq_save_unique_string(sq, strsave(lname));
6efe4fbf 186## }
187## repeat retrieve (id = m.member_id)
188## where m.list_id = l1.list_id and l1.name = @lsname and
189## m.member_type = "USER" {
de6a5fe1 190 if (u = hash_lookup(users, id)) {
191 sq_save_unique_data(members, u);
192 }
6efe4fbf 193## }
194 }
de6a5fe1 195 while (sq_get_data(members, &lsname))
196 fprintf(fd, "%s\n", lsname);
197 sq_destroy(members);
6efe4fbf 198 sq_destroy(sq);
199 if (fclose(fd)) {
200 fprintf(stderr, "error closing %s\n", file);
201 exit(SMS_CCONFIG);
202 }
203 }
204 sq_destroy(lists);
3548eee6 205 return(1);
6efe4fbf 206##}
207
208
3548eee6 209/* Build the list of everybody. */
210struct grp {
211 struct grp *next;
212 int id;
213};
214struct user {
215 char name[9];
d3c7c2f9 216 int uid;
3548eee6 217 struct grp *lists;
218};
219
220struct hash *do_everyone()
6efe4fbf 221##{
3548eee6 222 char buf[BUFSIZ], *l;
223 struct hash *groups, *users;
224 struct user *u;
225 struct grp *g;
226 struct bucket *b, **p;
6efe4fbf 227## char name[33];
228## int gid, id, lid, maxid, uid;
229 FILE *fd;
969cfb2b 230 int i;
6efe4fbf 231 struct save_queue *sq;
232
3548eee6 233 fprintf(stderr, "Building the list of everybody\n");
6efe4fbf 234 sprintf(buf, "%s/list-", NFS_DIR);
235 fd = fopen(buf, "w");
236 if (!fd) {
237 fprintf(stderr, "cannot open %s for output\n", buf);
238 exit(SMS_OCONFIG);
239 }
240
241 /* make space for group list */
3548eee6 242 groups = create_hash(15000);
6efe4fbf 243
244 /* retrieve simple groups */
3548eee6 245## range of l is list
6efe4fbf 246## retrieve (gid = l.#gid, lid = l.list_id)
3548eee6 247## where l.group != 0 and l.active != 0 {
248 sprintf(buf, ":%d", gid);
249 hash_store(groups, lid, strsave(buf));
6efe4fbf 250## }
251
252 /* get special cases: lists that aren't groups themselves but are
253 * members of groups. */
254 sq = sq_create();
255## range of m is members
256## retrieve (gid = list.#gid, lid = l.list_id)
969cfb2b 257## where l.active != 0 and list.active != 0 and m.member_type = "LIST" and
6efe4fbf 258## m.member_id = l.list_id and m.list_id = list.list_id and
259## list.group != 0 {
3548eee6 260 if (l = hash_lookup(groups, lid)) {
261 sprintf(buf, "%s:%d", l, gid);
262 free(l);
263 } else {
264 sprintf(buf, ":%d", gid);
265 }
266 hash_store(groups, lid, strsave(buf));
267 sq_save_data(sq, lid);
6efe4fbf 268## }
3548eee6 269
6efe4fbf 270 while (sq_get_data(sq, &id)) {
969cfb2b 271## repeat retrieve (lid = l.list_id) where l.active != 0 and
272## m.member_type = "LIST" and m.member_id = l.list_id and
273## m.list_id = @id {
3548eee6 274 if (l = hash_lookup(groups, lid)) {
275 sprintf(buf, "%s%s", l, hash_lookup(groups, id));
276 free(l);
969cfb2b 277 l = strsave(buf);
3548eee6 278 } else {
969cfb2b 279 l = hash_lookup(groups, id);
3548eee6 280 }
969cfb2b 281 hash_store(groups, lid, l);
3548eee6 282 sq_save_unique_data(sq, lid);
283## }
6efe4fbf 284 }
285 sq_destroy(sq);
286
3548eee6 287 /* now do grplists */
288 users = create_hash(10000);
6efe4fbf 289## range of u is users
d3c7c2f9 290## retrieve (id = u.users_id, name = u.login, uid = u.#uid)
291## where u.status = 1 {
3548eee6 292 u = (struct user *) malloc(sizeof(struct user));
293 strcpy(u->name, strtrim(name));
d3c7c2f9 294 u->uid = uid;
3548eee6 295 u->lists = NULL;
296 hash_store(users, id, u);
297## }
6efe4fbf 298
3548eee6 299## repeat retrieve (lid = m.list_id, id = m.member_id)
300## where m.member_type = "USER" {
969cfb2b 301 if ((u = (struct user *) hash_lookup(users, id)) &&
302 (hash_lookup(groups, lid) != NULL)) {
3548eee6 303 g = (struct grp *) malloc(sizeof(struct grp));
304 g->next = u->lists;
305 u->lists = g;
306 g->id = lid;
307 }
6efe4fbf 308## }
3548eee6 309
310 for (p = &(users->data[users->size - 1]); p >= users->data; p--) {
311 for (b = *p; b; b = b->next) {
969cfb2b 312 i = 0;
d3c7c2f9 313 u = (struct user *)b->data;
314 sprintf(buf, "%s:%d", u->name, u->uid);
315 for (g = u->lists; g; g = g->next)
969cfb2b 316 if (l = hash_lookup(groups, g->id)) {
317 i++;
318 strcat(buf, l);
319 }
3548eee6 320 /* should free stuff here... */
969cfb2b 321 if (i > 1) {
322 remove_duplicate_groups(buf);
323 }
3548eee6 324 b->data = strsave(buf);
325 fprintf(fd, "%s\n", buf);
326 }
6efe4fbf 327 }
3548eee6 328
6efe4fbf 329 fclose(fd);
6efe4fbf 330 free(groups);
331 return(users);
332##}
333
969cfb2b 334#define MAXGROUPS 256
335
336remove_duplicate_groups(p)
337register char *p;
338{
339 register char *pp;
340 char *index();
341 register int *mp;
342 int memory[MAXGROUPS], i, *mg = &memory[MAXGROUPS-1];
343
344 bzero(memory, MAXGROUPS * sizeof(int));
345 p = index(p, ':');
346 p++;
347 while (p = index(p, ':')) {
348 i = atoi(++p);
349 pp = p;
350 for (mp = memory; *mp && mp < mg; mp++)
351 if (*mp == i) {
352 if (p = index(p, ':'))
353 strcpy(pp, ++p);
354 else {
355 *(--pp) = 0;
356 }
357 p = pp - 2;
358 break;
359 }
360 *mp = i;
361 }
362}
363
6efe4fbf 364
3548eee6 365/* Now do each of the servers, linking the credentials list file and
366 * compiling the quota and dirs files.
367 */
368
369int do_machs(machs)
6efe4fbf 370struct save_queue *machs;
371##{
3548eee6 372## char *machname, listname[33], dev[33], *device, dir[64], fstype[9];
373## char *filetime;
374## int uid, quota, id, gid, flag1, flag2, flag3, flag4;
6efe4fbf 375 char file[64], f1[64], f2[64], *cp, *index();
376 int prevuid, quotasum;
377 FILE *fd;
3548eee6 378 struct stat sb;
6efe4fbf 379 struct save_queue *sq;
380
3548eee6 381/*
382 sprintf(file, "%s/list-", NFS_DIR);
383 if (stat(file, &sb) == 0) {
384 filetime = ingres_date_and_time(sb.st_mtime);
385 # retrieve (flag1 = int4(interval("min", tblstats.modtime - filetime)))
386 # where tblstats.table = "serverhosts"
387 # retrieve (flag2 = int4(interval("min", tblstats.modtime - filetime)))
388 # where tblstats.table = "filesys"
389 # retrieve (flag3 = int4(interval("min", tblstats.modtime - filetime)))
390 # where tblstats.table = "nfsquota"
391 # retrieve (flag4 = int4(interval("min", tblstats.modtime - filetime)))
392 # where tblstats.table = "nfsphys"
393 if (flag1 < 0 && flag2 < 0 && flag3 < 0 && flag4 < 0) {
394 fprintf(stderr, "The machine files do not need to be rebuilt.\n");
395 return(0);
396 }
397 }
398*/
399
400 fprintf(stderr, "Building machine files\n");
401
6efe4fbf 402## range of s is serverhosts
403## range of m is machine
404## range of n is nfsphys
405## range of q is nfsquota
406## range of f is filesys
407## range of u is users
408## range of l is list
409 while (sq_get_data(machs, &machname)) {
3548eee6 410## repeat retrieve (listname = trim(s.value3))
6efe4fbf 411## where s.mach_id = m.mach_id and m.name = @machname
3548eee6 412 strtrim(machname);
6efe4fbf 413 sprintf(f1, "%s/list-%s", NFS_DIR, listname);
414 sprintf(f2, "%s/%s.cred", NFS_DIR, machname);
415 unlink(f2); /* ignore errors on this unlink */
416 if (link(f1, f2)) {
417 fprintf(stderr, "Cannot link %s to %s\n", f1, f2);
418 exit(SMS_OCONFIG);
419 }
420 sq = sq_create();
421## repeat retrieve (dev = trim(n.#device))
422## where n.mach_id = m.mach_id and m.name = @machname {
423 sq_save_data(sq, strsave(dev));
424## }
425 while (sq_get_data(sq, &device)) {
426## repeat retrieve (id = n.nfsphys_id) where n.mach_id = m.mach_id and
427## m.#name = @machname and n.#device = @device
428 while (cp = index(device, '/')) *cp = '@';
429 sprintf(file, "%s/%s.%s.quotas", NFS_DIR, machname, device);
430 fd = fopen(file, "w");
431 if (!fd) {
432 fprintf(stderr, "cannot open %s for output\n", file);
433 exit(SMS_OCONFIG);
434 }
435 prevuid = -1;
436 quotasum = 0;
437## repeat retrieve (uid = u.#uid, quota = q.#quota)
438## where q.users_id = u.users_id and q.filsys_id = f.filsys_id and
439## f.phys_id = @id and u.status != 0 sort by #uid {
440 if (uid != prevuid) {
441 if (quotasum)
442 fprintf(fd, "%d %d\n", prevuid, quotasum);
443 prevuid = uid;
444 quotasum = quota;
445 } else {
446 quotasum += quota;
447 }
448## }
449 if (quotasum)
450 fprintf(fd, "%d %d\n", prevuid, quotasum);
451 if (fclose(fd)) {
452 fprintf(stderr, "error closing %s", file);
453 exit(SMS_CCONFIG);
454 }
455 sprintf(file, "%s/%s.%s.dirs", NFS_DIR, machname, device);
456 fd = fopen(file, "w");
457 if (!fd) {
458 fprintf(stderr, "cannot open %s for output\n", file);
459 exit(SMS_OCONFIG);
460 }
3548eee6 461## repeat retrieve (dir = trim(f.#name), fstype = trim(f.lockertype),
462## uid = u.#uid, gid = l.#gid)
6efe4fbf 463## where f.phys_id = @id and f.owner = u.users_id and
464## f.owners = l.list_id and f.createflg != 0 {
3548eee6 465 fprintf(fd, "%s %d %d %s\n", dir, uid, gid, fstype);
6efe4fbf 466## }
467 if (fclose(fd)) {
468 fprintf(stderr, "error closing %s", file);
469 exit(SMS_CCONFIG);
470 }
471 }
472 sq_destroy(sq);
473 }
3548eee6 474 return(1);
6efe4fbf 475##}
This page took 0.123753 seconds and 5 git commands to generate.