3 * This generates the zone files necessary to load a hesiod server.
4 * The following zones are generated: passwd, uid, pobox, group,
5 * grplist, gid, filsys, cluster, pcap, sloc, service.
7 * (c) Copyright 1988 by the Massachusetts Institute of Technology.
8 * For copying and distribution information, please see the file
12 #include <mit-copyright.h>
16 #include <sys/types.h>
22 #define HESIOD_DIR "/u1/sms/dcm/hesiod"
24 #define min(x,y) ((x) < (y) ? (x) : (y))
25 struct hash *machines = NULL;
26 struct hash *users = NULL;
27 char *whoami = "hesiod.gen";
38 char *malloc(), *strsave();
39 char *ingres_date_and_time(), *ingres_time(), *ingres_date();
51 fprintf(stderr, "usage: %s [outfile]\n", argv[0]);
58 ## set lockmode session where level = table
60 changed = do_passwd();
61 changed += do_filsys();
62 changed += do_cluster();
63 changed += do_printers();
65 changed += do_service();
66 changed += do_groups();
71 fprintf(stderr, "No files updated.\n");
72 if (argc == 2 && stat(argv[1], &sb) == 0)
77 fprintf(stderr, "Building tar file.\n");
78 sprintf(cmd, "cd %s; tar cf %s .", HESIOD_DIR, argv[1]);
88 * ingerr: (supposedly) called when Ingres indicates an error.
89 * I have not yet been able to get this to work to intercept a
90 * database open error.
92 #define INGRES_DEADLOCK 4700
94 static int ingerr(num)
101 case INGRES_DEADLOCK:
102 ingres_errno = SMS_DEADLOCK;
105 ingres_errno = SMS_INGRES_ERR;
107 com_err(whoami, SMS_INGRES_ERR, " code %d\n", *num);
108 critical_alert("DCM", "Hesiod build encountered INGRES ERROR %d", *num);
121 machines = create_hash(1000);
122 ## retrieve (name = machine.#name, id = machine.mach_id) {
123 hash_store(machines, id, strsave(strtrim(name)));
130 FILE *pout, *uout, *bout;
131 char poutf[64], uoutf[64], poutft[64], uoutft[64], boutf[64], boutft[64];
132 struct stat psb, usb, bsb;
136 ## char login[9], shell[33], fullname[33], oa[17], op[13], hp[17], *filetime;
137 ## char nn[17], ptype[9];
138 ## int uid, flag1, flag2, id, pid;
140 sprintf(poutf, "%s/passwd.db", HESIOD_DIR);
141 sprintf(uoutf, "%s/uid.db", HESIOD_DIR);
142 sprintf(boutf, "%s/pobox.db", HESIOD_DIR);
144 if (stat(poutf, &psb) == 0 && stat(uoutf, &usb) == 0 &&
145 stat(boutf, &bsb) == 0) {
146 ftime = min(min(psb.st_mtime, usb.st_mtime), bsb.st_mtime);
147 filetime = ingres_date_and_time(ftime);
148 ## retrieve (flag1 = int4(interval("min", tblstats.modtime - filetime)))
149 ## where tblstats.table = "users"
150 ## retrieve (flag2 = int4(interval("min", tblstats.modtime - filetime)))
151 ## where tblstats.table = "machine"
152 if (flag1 < 0 && flag2 < 0) {
153 fprintf(stderr, "Files passwd.db, uid.db, and pobox.db do not need to be rebuilt.\n");
158 sprintf(poutft, "%s~", poutf);
159 pout = fopen(poutft, "w");
161 perror("cannot open passwd.db~ for write");
164 sprintf(uoutft, "%s~", uoutf);
165 uout = fopen(uoutft, "w");
167 perror("cannot open uid.db~ for write");
170 sprintf(boutft, "%s~", boutf);
171 bout = fopen(boutft, "w");
173 perror("cannot open pobox.db for write");
177 fprintf(stderr, "Building passwd.db, uid.db, and pobox.db\n");
180 users = create_hash(10000);
181 ## range of u is users
182 ## retrieve (login = u.#login, uid = u.#uid, shell = u.#shell,
183 ## fullname = u.#fullname, nn = u.nickname, oa = u.office_addr,
184 ## op = u.office_phone, hp = u.home_phone, id = u.users_id,
185 ## pid = u.pop_id, ptype = u.potype)
186 ## where u.status = 1 {
194 u = (struct user *) malloc(sizeof(struct user));
195 strcpy(u->name, login);
197 hash_store(users, id, u);
198 fprintf(pout, "%s.passwd\tHS UNSPECA \"%s:*:%d:101:%s,%s,%s,%s,%s:/mit/%s:%s\"\n",
199 login, login, uid, fullname, nn, oa, op, hp, login, shell);
200 fprintf(uout, "%d.uid\tHS CNAME %s.passwd\n", uid, login);
201 if (ptype[0] == 'P' && (mach = hash_lookup(machines, pid))) {
202 fprintf(bout, "%s.pobox\tHS UNSPECA \"POP %s %s\"\n",
207 if (fclose(pout) || fclose(uout) || fclose(bout)) {
208 fprintf(stderr, "Unsuccessful file close of passwd.db, uid.db, or pobox.db\n");
218 /************************************************************************
219 * WARNING: this routine mallocs a large amount of memory which it never frees.
220 * This is considered OK only because main has been arranged to call this
221 * last, so the process will exit shortly after we return.
226 FILE *iout, *gout, *lout;
227 char ioutf[64], goutf[64], loutf[64], buf[512], *l;
229 register struct bucket *b, **p;
232 struct stat isb, gsb, lsb;
234 register struct save_queue *sq;
235 struct save_queue *sq_create();
237 ## char name[33], *filetime;
238 ## int gid, id, lid, flag1, flag2, flag3, maxid;
241 sprintf(ioutf, "%s/gid.db", HESIOD_DIR);
242 sprintf(goutf, "%s/group.db", HESIOD_DIR);
243 sprintf(loutf, "%s/grplist.db", HESIOD_DIR);
245 if (stat(ioutf, &isb) == 0 && stat(goutf, &gsb) == 0 && stat(loutf, &lsb) == 0) {
246 ftime = min(isb.st_mtime, min(gsb.st_mtime, lsb.st_mtime));
247 filetime = ingres_date_and_time(ftime);
248 ## retrieve (flag1 = int4(interval("min", tblstats.modtime - filetime)))
249 ## where tblstats.table = "users"
250 ## retrieve (flag2 = int4(interval("min", tblstats.modtime - filetime)))
251 ## where tblstats.table = "list"
252 ## retrieve (flag3 = int4(interval("min", tblstats.modtime - filetime)))
253 ## where tblstats.table = "members"
254 if (flag1 < 0 && flag2 < 0 && flag3 < 0) {
255 fprintf(stderr, "Files gid.db, group.db and grplist.db do not need to be rebuilt.\n");
260 sprintf(buf, "%s~", ioutf);
261 iout = fopen(buf, "w");
263 perror("cannot open gid.db for write");
266 sprintf(buf, "%s~", goutf);
267 gout = fopen(buf, "w");
269 perror("cannot open group.db for write");
272 sprintf(buf, "%s~", loutf);
273 lout = fopen(buf, "w");
275 perror("cannot open grplist.db for write");
279 fprintf(stderr, "Building gid.db, group.db, and grplist.db\n");
281 /* make space for group list */
282 groups = create_hash(15000);
284 /* retrieve simple groups */
285 ## range of l is list
287 /* get lock records */
288 ## retrieve (name = l.modtime) where l.list_id = 0
289 ## retrieve (name = users.modtime) where users.users_id = 0
291 ## retrieve (name = l.#name, gid = l.#gid, lid = l.list_id)
292 ## where l.group != 0 and l.active != 0 {
294 sprintf(buf, "%s:%d", name, gid);
295 hash_store(groups, lid, strsave(buf));
296 fprintf(iout, "%d.gid\tHS CNAME %s.group\n", gid, name);
297 fprintf(gout, "%s.group\tHS UNSPECA \"%s:*:%d:\"\n",
304 /* get special cases: lists that aren't groups themselves but are
305 * members of groups. */
307 ## range of m is members
308 ## retrieve (name = list.#name, gid = list.#gid, lid = l.list_id)
309 ## where l.active != 0 and list.active != 0 and m.member_type = "LIST" and
310 ## m.member_id = l.list_id and m.list_id = list.list_id and
313 if (l = hash_lookup(groups, lid)) {
314 sprintf(buf, "%s:%s:%d", l, name, gid);
317 sprintf(buf, "%s:%d", name, gid);
319 hash_store(groups, lid, strsave(buf));
320 sq_save_unique_data(sq, lid);
323 while (sq_get_data(sq, &id)) {
324 ## repeat retrieve (lid = l.list_id) where m.member_type = "LIST" and
325 ## m.list_id = @id and m.member_id = l.list_id and l.active != 0 {
326 if (l = hash_lookup(groups, lid)) {
327 sprintf(buf, "%s:%s", l, hash_lookup(groups, id));
331 l = hash_lookup(groups, id);
333 hash_store(groups, lid, l);
334 sq_save_unique_data(sq, lid);
339 /* now do grplists */
341 users = create_hash(10000);
342 ## range of u is users
343 ## retrieve (id = u.users_id, name = u.login) where u.status = 1 {
344 u = (struct user *) malloc(sizeof(struct user));
345 strcpy(u->name, strtrim(name));
347 hash_store(users, id, u);
351 ## repeat retrieve (lid = m.list_id, id = m.member_id)
352 ## where m.member_type = "USER" {
353 if ((u = (struct user *) hash_lookup(users, id)) &&
354 (hash_lookup(groups, lid) != NULL)) {
355 g = (struct grp *) malloc(sizeof(struct grp));
363 for (p = &(users->data[users->size - 1]); p >= users->data; p--) {
364 for (b = *p; b; b = b->next) {
366 sprintf(buf, "%s.grplist\tHS UNSPECA \"",
367 ((struct user *)b->data)->name);
368 for (g = ((struct user *)b->data)->lists; g; g = g->next)
369 if (l = hash_lookup(groups, g->id)) {
376 /* Do duplicate detection here */
378 remove_duplicate_groups(buf);
386 /* This is commented out because it takes on the order of 30 minutes to
387 * run. Instead, we never free the memory, but the program will exit
393 for (p = &groups[maxid-1]; p >= groups; p--)
400 if (fclose(iout) || fclose(gout) || fclose(lout)) {
401 fprintf(stderr, "Unsuccessful close of gid.db, group.db, or grplist.db\n");
411 #define MAXGROUPS 256
413 remove_duplicate_groups(p)
419 int memory[MAXGROUPS], i, *mg = &memory[MAXGROUPS-1];
421 bzero(memory, MAXGROUPS * sizeof(int));
422 while (p = index(p, ':')) {
424 for (mp = memory; *mp && mp < mg; mp++)
426 if (p = index(p, ':'))
429 strcpy(--pp, "\"\n");
437 if (!(p = index(p, ':')))
447 char outf[64], outft[64], *mach, machbuf[128], *group;
451 struct save_queue *sq, *sq_create();
452 ## char name[33], type[9], loc[33], access[2], mount[33], trans[257];
453 ## char *filetime, comments[65];
454 ## int flag1, flag2, flag3, flag4, id, fid;
456 sprintf(outf, "%s/filsys.db", HESIOD_DIR);
458 if (stat(outf, &sb) == 0) {
460 filetime = ingres_date_and_time(ftime);
461 ## retrieve (flag1 = int4(interval("min", tblstats.modtime - filetime)))
462 ## where tblstats.table = "filesys"
463 ## retrieve (flag2 = int4(interval("min", tblstats.modtime - filetime)))
464 ## where tblstats.table = "machine"
465 ## retrieve (flag3 = int4(interval("min", tblstats.modtime - filetime)))
466 ## where tblstats.table = "alias"
467 ## retrieve (flag4 = int4(interval("min", tblstats.modtime - filetime)))
468 ## where tblstats.table = "fsgroup"
469 if (flag1 < 0 && flag2 < 0 && flag3 < 0 && flag4 < 0) {
470 fprintf(stderr, "File filsys.db does not need to be rebuilt.\n");
475 sprintf(outft, "%s~", outf);
476 out = fopen(outft, "w");
478 perror("cannot open filsys.db for write");
482 fprintf(stderr, "Building filsys.db\n");
486 ## range of f is filesys
487 ## retrieve (name = f.label, type = f.#type, loc = f.#name, id = f.mach_id,
488 ## access = f.#access, mount = f.#mount, comments = f.#comments,
489 ## fid = f.filsys_id) {
492 if (!strcmp(type, "NFS") || !strcmp(type, "RVD")) {
496 if (mach = hash_lookup(machines, id)) {
497 for (p = machbuf; *mach && *mach != '.'; mach++) {
499 *p++ = tolower(*mach);
504 fprintf(out, "%s.filsys\tHS UNSPECA \"%s %s %s %s %s\"\n",
505 name, type, loc, machbuf, access, mount);
507 } else if (!strcmp(type, "AFS")) {
511 fprintf(out, "%s.filsys\tHS UNSPECA \"AFS %s %s %s\"\n",
512 name, loc, access, mount);
513 } else if (!strcmp(type, "ERR")) {
515 fprintf(out, "%s.filsys\tHS UNSPECA \"ERR %s\"\n",
517 } else if (!strcmp(type, "FSGROUP")) {
518 sprintf(trans, "%s:%d", name, fid);
519 sq_save_data(sq, strsave(trans));
523 ## range of g is fsgroup
524 while (sq_get_data(sq, &group)) {
525 fid = atoi(index(group, ':')+1);
526 *index(group, ':') = 0;
527 ## retrieve (type = f.#type, loc = f.#name, id = f.mach_id,
528 ## access = f.#access, mount = f.#mount, comments = f.#comments,
529 ## name = f.#label, trans = g.#key)
530 ## where f.filsys_id = g.filsys_id and g.group_id = fid
531 ## sort by #trans, #name {
533 if (!strcmp(type, "NFS") || !strcmp(type, "RVD")) {
537 if (mach = hash_lookup(machines, id)) {
538 for (p = machbuf; *mach && *mach != '.'; mach++) {
540 *p++ = tolower(*mach);
545 fprintf(out, "%s.filsys\tHS UNSPECA \"%s %s %s %s %s\"\n",
546 group, type, loc, machbuf, access, mount);
548 } else if (!strcmp(type, "AFS")) {
552 fprintf(out, "%s.filsys\tHS UNSPECA \"AFS %s %s %s\"\n",
553 group, loc, access, mount);
554 } else if (!strcmp(type, "ERR")) {
556 fprintf(out, "%s.filsys\tHS UNSPECA \"ERR %s\"\n",
564 ## range of a is alias
565 ## retrieve (name = a.#name, trans = a.#trans) where a.#type = "FILESYS" {
568 fprintf(out, "%s.filsys\tHS CNAME %s.filsys\n", name, trans);
572 fprintf(stderr, "Unsuccessful close of filsys.db\n");
581 * Modified from sys/types.h:
583 int setsize; /* = howmany(setbits, NSETBITS) */
585 typedef long set_mask;
586 #define NSETBITS (sizeof(set_mask) * NBBY) /* bits per mask */
588 #define howmany(x, y) (((x)+((y)-1))/(y))
591 #define SET_SET(n, p) ((p)[(n)/NSETBITS] |= (1 << ((n) % NSETBITS)))
592 #define SET_CLR(n, p) ((p)[(n)/NSETBITS] &= ~(1 << ((n) % NSETBITS)))
593 #define SET_ISSET(n, p) ((p)[(n)/NSETBITS] & (1 << ((n) % NSETBITS)))
594 #define SET_CREATE() ((set_mask *)malloc(setsize * sizeof(set_mask)))
595 #define SET_ZERO(p) bzero((char *)(p), setsize * sizeof(set_mask))
596 #define SET_CMP(p1, p2) (bcmp((p1), (p2), setsize * sizeof(set_mask)))
602 char outf[64], outft[64], *mach, machbuf[33], *p;
605 ## int flag1, flag2, flag3, flag4, maxmach, maxclu, mid, cid, id;
606 ## char name[33], label[17], data[33], *filetime;
607 set_mask **machs, *ms, *ps;
609 sprintf(outf, "%s/cluster.db", HESIOD_DIR);
611 if (stat(outf, &sb) == 0) {
613 filetime = ingres_date_and_time(ftime);
614 ## retrieve (flag1 = int4(interval("min", tblstats.modtime - filetime)))
615 ## where tblstats.table = "cluster"
616 ## retrieve (flag2 = int4(interval("min", tblstats.modtime - filetime)))
617 ## where tblstats.table = "machine"
618 ## retrieve (flag3 = int4(interval("min", tblstats.modtime - filetime)))
619 ## where tblstats.table = "mcmap"
620 ## retrieve (flag4 = int4(interval("min", tblstats.modtime - filetime)))
621 ## where tblstats.table = "svc"
622 if (flag1 < 0 && flag2 < 0 && flag3 < 0 && flag4 < 0) {
623 fprintf(stderr, "File cluster.db does not need to be rebuilt.\n");
628 sprintf(outft, "%s~", outf);
629 out = fopen(outft, "w");
631 perror("cannot open cluster.db for write");
635 fprintf(stderr, "Building cluster.db\n");
638 ## range of c is cluster
640 ## retrieve (maxclu = max(c.clu_id))
642 setsize = howmany(maxclu, NSETBITS);
643 ## range of m is machine
644 ## retrieve (maxmach = max(m.mach_id))
646 machs = (set_mask **)malloc((maxmach + 1) * sizeof(set_mask **));
647 bzero(machs, (maxmach + 1) * sizeof(int));
649 ## range of p is mcmap
650 ## retrieve (mid = p.mach_id, cid = p.clu_id) {
651 if (!(ms = machs[mid])) {
652 ms = machs[mid] = SET_CREATE();
659 for (mid = 1; mid < maxmach; mid++) {
663 for (cid = 1; cid < maxclu; cid++) {
664 if (SET_ISSET(cid, ms)) {
665 ## repeat retrieve (label = d.serv_label, data = d.serv_cluster)
666 ## where d.clu_id = @cid {
670 "smsinternal-%d.cluster\tHS UNSPECA \"%s %s\"\n",
676 if (mach = hash_lookup(machines, mid)) {
677 for (p = machbuf; *mach && *mach != '.'; mach++)
680 fprintf(out, "%s.cluster\tHS CNAME smsinternal-%d.cluster\n",
683 for (id = mid + 1; id < maxmach; id++) {
684 if ((ps = machs[id]) && !SET_CMP(ms, ps)) {
687 if (mach = hash_lookup(machines, id)) {
688 for (p = machbuf; *mach && *mach != '.'; mach++)
691 fprintf(out,"%s.cluster\tHS CNAME smsinternal-%d.cluster\n",
700 ## retrieve (name = c.#name, label = d.serv_label, data = d.serv_cluster)
701 ## where c.clu_id = d.clu_id {
705 fprintf(out, "%s.cluster\tHS UNSPECA \"%s %s\"\n",
712 fprintf(stderr, "Unsuccessful close of cluster.db\n");
723 char outf[64], outft[64];
726 ## char name[17], rp[17], sd[33], rm[33], *filetime;
729 sprintf(outf, "%s/printcap.db", HESIOD_DIR);
731 if (stat(outf, &sb) == 0) {
733 filetime = ingres_date_and_time(ftime);
734 ## retrieve (flag = int4(interval("min", tblstats.modtime - filetime)))
735 ## where tblstats.table = "printcap"
737 fprintf(stderr, "File printcap.db does not need to be rebuilt.\n");
742 sprintf(outft, "%s~", outf);
743 out = fopen(outft, "w");
745 perror("cannot open printcap.db for write");
749 fprintf(stderr, "Building printcap.db\n");
751 ## range of p is printcap
752 ## retrieve (name = p.#name, rp = p.#rp, sd = p.dir, rm = machine.#name)
753 ## where machine.mach_id = p.mach_id {
758 fprintf(out, "%s.pcap\tHS UNSPECA \"%s:rp=%s:rm=%s:sd=%s\"\n",
759 name, name, rp, rm, sd);
763 fprintf(stderr, "Unsuccessful close of pcap.db\n");
773 FILE *out, *old, *new;
774 char outf[64], outft[64], *mach;
778 ## char service[17], *filetime;
779 ## int port, flag1, flag2, id;
781 sprintf(outf, "%s/sloc.db", HESIOD_DIR);
783 if (stat(outf, &sb) == 0) {
785 filetime = ingres_date_and_time(ftime);
786 ## retrieve (flag1 = int4(interval("min", tblstats.modtime - filetime)))
787 ## where tblstats.table = "serverhosts"
788 ## retrieve (flag2 = int4(interval("min", tblstats.modtime - filetime)))
789 ## where tblstats.table = "machine"
790 if (flag1 < 0 && flag2 < 0) {
791 fprintf(stderr, "File sloc.db does not need to be rebuilt.\n");
796 sprintf(outft, "%s~", outf);
797 out = fopen(outft, "w");
799 perror("cannot open sloc.db for write");
803 fprintf(stderr, "Building sloc.db\n");
806 ## range of s is serverhosts
807 ## retrieve (service = s.#service, id = s.mach_id)
808 ## sort by #service {
810 if (mach = hash_lookup(machines, id))
811 fprintf(out, "%s.sloc\tHS UNSPECA %s\n", service, mach);
815 fprintf(stderr, "Unsuccessful close of sloc.db\n");
826 char outf[64], outft[64];
829 ## char mach[33], service[33], protocol[9], altserv[129], *filetime;
830 ## int port, flag1, flag2;
832 sprintf(outf, "%s/service.db", HESIOD_DIR);
834 if (stat(outf, &sb) == 0) {
836 filetime = ingres_date_and_time(ftime);
837 ## retrieve (flag1 = int4(interval("min", tblstats.modtime - filetime)))
838 ## where tblstats.table = "services"
840 fprintf(stderr, "File service.db does not need to be rebuilt.\n");
845 sprintf(outft, "%s~", outf);
846 out = fopen(outft, "w");
848 perror("cannot open service.db for write");
852 fprintf(stderr, "Building service.db\n");
854 ## range of s is services
855 ## retrieve (service = s.name, protocol = lowercase(s.#protocol),
859 fprintf(out, "%s.service\tHS UNSPECA \"%s %s %d\"\n",
860 service, service, protocol, port);
863 ## range of a is alias
864 ## retrieve (service = a.name, altserv = a.trans) where a.type = "SERVICE" {
867 fprintf(out, "%s.service\tHS CNAME %s.service\n", service, altserv);
871 fprintf(stderr, "Unsuccessful close of service.db\n");