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]);
59 changed = do_passwd();
60 changed += do_pobox();
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)));
131 char poutf[64], uoutf[64], poutft[64], uoutft[64];
132 struct stat psb, usb;
135 ## char login[9], shell[33], fullname[33], oa[17], op[13], hp[17], *filetime;
137 ## int uid, flag, id;
139 sprintf(poutf, "%s/passwd.db", HESIOD_DIR);
140 sprintf(uoutf, "%s/uid.db", HESIOD_DIR);
142 if (stat(poutf, &psb) == 0 && stat(uoutf, &usb) == 0) {
143 ftime = min(psb.st_mtime, usb.st_mtime);
144 filetime = ingres_date_and_time(ftime);
145 ## retrieve (flag = int4(interval("min", tblstats.modtime - filetime)))
146 ## where tblstats.table = "users"
148 fprintf(stderr, "Files passwd.db and uid.db do not need to be rebuilt.\n");
153 sprintf(poutft, "%s~", poutf);
154 pout = fopen(poutft, "w");
156 perror("cannot open passwd.db~ for write");
159 sprintf(uoutft, "%s~", uoutf);
160 uout = fopen(uoutft, "w");
162 perror("cannot open uid.db~ for write");
166 fprintf(stderr, "Building passwd.db and uid.db\n");
168 users = create_hash(10000);
169 ## range of u is users
170 ## retrieve (login = u.#login, uid = u.#uid, shell = u.#shell,
171 ## fullname = u.#fullname, nn = u.nickname, oa = u.office_addr,
172 ## op = u.office_phone, hp = u.home_phone, id = u.users_id)
173 ## where u.status = 1 {
181 u = (struct user *) malloc(sizeof(struct user));
182 strcpy(u->name, login);
184 hash_store(users, id, u);
185 fprintf(pout, "%s.passwd\tHS UNSPECA \"%s:*:%d:101:%s,%s,%s,%s,%s:/mit/%s:%s\"\n",
186 login, login, uid, fullname, nn, oa, op, hp, login, shell);
187 fprintf(uout, "%d.uid\tHS CNAME %s.passwd\n", uid, login);
190 if (fclose(pout) || fclose(uout)) {
191 fprintf(stderr, "Unsuccessful file close of passwd.db or uid.db\n");
203 char outf[64], outft[64], *mach;
206 ## char login[9], *filetime;
207 ## int flag1, flag2, id;
209 sprintf(outf, "%s/pobox.db", HESIOD_DIR);
211 if (stat(outf, &sb) == 0) {
213 filetime = ingres_date_and_time(ftime);
214 ## retrieve (flag1 = int4(interval("min", tblstats.modtime - filetime)))
215 ## where tblstats.table = "users"
216 ## retrieve (flag2 = int4(interval("min", tblstats.modtime - filetime)))
217 ## where tblstats.table = "machine"
218 if (flag1 < 0 && flag2 < 0) {
219 fprintf(stderr,"File pobox.db does not need to be rebuilt.\n");
224 sprintf(outft, "%s~", outf);
225 out = fopen(outft, "w");
227 perror("cannot open pobox.db for write");
231 fprintf(stderr, "Building pobox.db\n");
234 ## range of u is users
235 ## retrieve (login = u.#login, id = u.pop_id)
236 ## where u.status = 1 and u.potype = "POP" {
238 if (mach = hash_lookup(machines, id))
239 fprintf(out, "%s.pobox\tHS UNSPECA \"POP %s %s\"\n",
244 fprintf(stderr, "Unsuccessful close of pobox.db\n");
252 /************************************************************************
253 * WARNING: this routine mallocs a large amount of memory which it never frees.
254 * This is considered OK only because main has been arranged to call this
255 * last, so the process will exit shortly after we return.
260 FILE *iout, *gout, *lout;
261 char ioutf[64], goutf[64], loutf[64], buf[512], *l;
263 register struct bucket *b, **p;
266 struct stat isb, gsb, lsb;
268 register struct save_queue *sq;
269 struct save_queue *sq_create();
271 ## char name[33], *filetime;
272 ## int gid, id, lid, flag1, flag2, flag3, maxid;
275 sprintf(ioutf, "%s/gid.db", HESIOD_DIR);
276 sprintf(goutf, "%s/group.db", HESIOD_DIR);
277 sprintf(loutf, "%s/grplist.db", HESIOD_DIR);
279 if (stat(ioutf, &isb) == 0 && stat(goutf, &gsb) == 0 && stat(loutf, &lsb) == 0) {
280 ftime = min(isb.st_mtime, min(gsb.st_mtime, lsb.st_mtime));
281 filetime = ingres_date_and_time(ftime);
282 ## retrieve (flag1 = int4(interval("min", tblstats.modtime - filetime)))
283 ## where tblstats.table = "users"
284 ## retrieve (flag2 = int4(interval("min", tblstats.modtime - filetime)))
285 ## where tblstats.table = "list"
286 ## retrieve (flag3 = int4(interval("min", tblstats.modtime - filetime)))
287 ## where tblstats.table = "members"
288 if (flag1 < 0 && flag2 < 0 && flag3 < 0) {
289 fprintf(stderr, "Files gid.db, group.db and grplist.db do not need to be rebuilt.\n");
294 sprintf(buf, "%s~", ioutf);
295 iout = fopen(buf, "w");
297 perror("cannot open gid.db for write");
300 sprintf(buf, "%s~", goutf);
301 gout = fopen(buf, "w");
303 perror("cannot open group.db for write");
306 sprintf(buf, "%s~", loutf);
307 lout = fopen(buf, "w");
309 perror("cannot open grplist.db for write");
313 fprintf(stderr, "Building gid.db, group.db, and grplist.db\n");
315 /* make space for group list */
316 groups = create_hash(15000);
318 /* retrieve simple groups */
319 ## range of l is list
321 ## retrieve (name = l.#name, gid = l.#gid, lid = l.list_id)
322 ## where l.group != 0 and l.active != 0 {
324 sprintf(buf, "%s:%d", name, gid);
325 hash_store(groups, lid, strsave(buf));
326 fprintf(iout, "%d.gid\tHS CNAME %s.group\n", gid, name);
327 fprintf(gout, "%s.group\tHS UNSPECA \"%s:*:%d:\"\n",
334 /* get special cases: lists that aren't groups themselves but are
335 * members of groups. */
337 ## range of m is members
338 ## retrieve (name = list.#name, gid = list.#gid, lid = l.list_id)
339 ## where l.active != 0 and list.active != 0 and m.member_type = "LIST" and
340 ## m.member_id = l.list_id and m.list_id = list.list_id and
343 if (l = hash_lookup(groups, lid)) {
344 sprintf(buf, "%s:%s:%d", l, name, gid);
347 sprintf(buf, "%s:%d", name, gid);
349 hash_store(groups, lid, strsave(buf));
350 sq_save_unique_data(sq, lid);
353 while (sq_get_data(sq, &id)) {
354 ## repeat retrieve (lid = l.list_id) where m.member_type = "LIST" and
355 ## m.list_id = @id and m.member_id = l.list_id and l.active != 0 {
356 if (l = hash_lookup(groups, lid)) {
357 sprintf(buf, "%s:%s", l, hash_lookup(groups, id));
361 l = hash_lookup(groups, id);
363 hash_store(groups, lid, l);
364 sq_save_unique_data(sq, lid);
369 /* now do grplists */
371 users = create_hash(10000);
372 ## range of u is users
373 ## retrieve (id = u.users_id, name = u.login) where u.status = 1 {
374 u = (struct user *) malloc(sizeof(struct user));
375 strcpy(u->name, strtrim(name));
377 hash_store(users, id, u);
381 ## repeat retrieve (lid = m.list_id, id = m.member_id)
382 ## where m.member_type = "USER" {
383 if ((u = (struct user *) hash_lookup(users, id)) &&
384 (hash_lookup(groups, lid) != NULL)) {
385 g = (struct grp *) malloc(sizeof(struct grp));
393 for (p = &(users->data[users->size - 1]); p >= users->data; p--) {
394 for (b = *p; b; b = b->next) {
396 sprintf(buf, "%s.grplist\tHS UNSPECA \"",
397 ((struct user *)b->data)->name);
398 for (g = ((struct user *)b->data)->lists; g; g = g->next)
399 if (l = hash_lookup(groups, g->id)) {
406 /* Do duplicate detection here */
408 remove_duplicate_groups(buf);
415 /* This is commented out because it takes on the order of 30 minutes to
416 * run. Instead, we never free the memory, but the program will exit
422 for (p = &groups[maxid-1]; p >= groups; p--)
429 if (fclose(iout) || fclose(gout) || fclose(lout)) {
430 fprintf(stderr, "Unsuccessful close of gid.db, group.db, or grplist.db\n");
440 #define MAXGROUPS 256
442 remove_duplicate_groups(p)
448 int memory[MAXGROUPS], i, *mg = &memory[MAXGROUPS-1];
450 bzero(memory, MAXGROUPS * sizeof(int));
451 while (p = index(p, ':')) {
453 for (mp = memory; *mp && mp < mg; mp++)
455 if (p = index(p, ':'))
458 strcpy(--pp, "\"\n");
466 if (!(p = index(p, ':')))
476 char outf[64], outft[64], *mach, machbuf[128];
480 ## char name[33], type[9], loc[33], access[2], mount[33], trans[257];
481 ## char *filetime, comments[65];
482 ## int flag1, flag2, flag3, id;
484 sprintf(outf, "%s/filsys.db", HESIOD_DIR);
486 if (stat(outf, &sb) == 0) {
488 filetime = ingres_date_and_time(ftime);
489 ## retrieve (flag1 = int4(interval("min", tblstats.modtime - filetime)))
490 ## where tblstats.table = "filesys"
491 ## retrieve (flag2 = int4(interval("min", tblstats.modtime - filetime)))
492 ## where tblstats.table = "machine"
493 ## retrieve (flag3 = int4(interval("min", tblstats.modtime - filetime)))
494 ## where tblstats.table = "alias"
495 if (flag1 < 0 && flag2 < 0 && flag3 < 0) {
496 fprintf(stderr, "File filsys.db does not need to be rebuilt.\n");
501 sprintf(outft, "%s~", outf);
502 out = fopen(outft, "w");
504 perror("cannot open filsys.db for write");
508 fprintf(stderr, "Building filsys.db\n");
511 ## range of f is filesys
512 ## retrieve (name = f.label, type = f.#type, loc = f.#name, id = f.mach_id,
513 ## access = f.#access, mount = f.#mount, comments = f.#comments) {
516 if (strcmp(type, "ERR")) {
520 if (mach = hash_lookup(machines, id)) {
521 for (p = machbuf; *mach && *mach != '.'; mach++) {
523 *p++ = tolower(*mach);
528 fprintf(out, "%s.filsys\tHS UNSPECA \"%s %s %s %s %s\"\n",
529 name, type, loc, machbuf, access, mount);
533 fprintf(out, "%s.filsys\tHS UNSPECA \"ERR %s\"\n",
540 ## range of a is alias
541 ## retrieve (name = a.#name, trans = a.#trans) where a.#type = "FILESYS" {
544 fprintf(out, "%s.filsys\tHS CNAME %s.filsys\n", name, trans);
548 fprintf(stderr, "Unsuccessful close of filsys.db\n");
557 * Modified from sys/types.h:
559 int setsize; /* = howmany(setbits, NSETBITS) */
561 typedef long set_mask;
562 #define NSETBITS (sizeof(set_mask) * NBBY) /* bits per mask */
564 #define howmany(x, y) (((x)+((y)-1))/(y))
567 #define SET_SET(n, p) ((p)[(n)/NSETBITS] |= (1 << ((n) % NSETBITS)))
568 #define SET_CLR(n, p) ((p)[(n)/NSETBITS] &= ~(1 << ((n) % NSETBITS)))
569 #define SET_ISSET(n, p) ((p)[(n)/NSETBITS] & (1 << ((n) % NSETBITS)))
570 #define SET_CREATE() ((set_mask *)malloc(setsize * sizeof(set_mask)))
571 #define SET_ZERO(p) bzero((char *)(p), setsize * sizeof(set_mask))
572 #define SET_CMP(p1, p2) (bcmp((p1), (p2), setsize * sizeof(set_mask)))
578 char outf[64], outft[64], *mach, machbuf[33], *p;
581 ## int flag1, flag2, flag3, flag4, maxmach, maxclu, mid, cid, id;
582 ## char name[33], label[17], data[33], *filetime;
583 set_mask **machs, *ms, *ps;
585 sprintf(outf, "%s/cluster.db", HESIOD_DIR);
587 if (stat(outf, &sb) == 0) {
589 filetime = ingres_date_and_time(ftime);
590 ## retrieve (flag1 = int4(interval("min", tblstats.modtime - filetime)))
591 ## where tblstats.table = "cluster"
592 ## retrieve (flag2 = int4(interval("min", tblstats.modtime - filetime)))
593 ## where tblstats.table = "machine"
594 ## retrieve (flag3 = int4(interval("min", tblstats.modtime - filetime)))
595 ## where tblstats.table = "mcmap"
596 ## retrieve (flag4 = int4(interval("min", tblstats.modtime - filetime)))
597 ## where tblstats.table = "svc"
598 if (flag1 < 0 && flag2 < 0 && flag3 < 0 && flag4 < 0) {
599 fprintf(stderr, "File cluster.db does not need to be rebuilt.\n");
604 sprintf(outft, "%s~", outf);
605 out = fopen(outft, "w");
607 perror("cannot open cluster.db for write");
611 fprintf(stderr, "Building cluster.db\n");
614 ## range of c is cluster
616 ## retrieve (maxclu = max(c.clu_id))
618 setsize = howmany(maxclu, NSETBITS);
619 ## range of m is machine
620 ## retrieve (maxmach = max(m.mach_id))
622 machs = (set_mask **)malloc((maxmach + 1) * sizeof(set_mask **));
623 bzero(machs, (maxmach + 1) * sizeof(int));
625 ## range of p is mcmap
626 ## retrieve (mid = p.mach_id, cid = p.clu_id) {
627 if (!(ms = machs[mid])) {
628 ms = machs[mid] = SET_CREATE();
635 for (mid = 1; mid < maxmach; mid++) {
639 for (cid = 1; cid < maxclu; cid++) {
640 if (SET_ISSET(cid, ms)) {
641 ## repeat retrieve (label = d.serv_label, data = d.serv_cluster)
642 ## where d.clu_id = @cid {
646 "smsinternal-%d.cluster\tHS UNSPECA \"%s %s\"\n",
652 if (mach = hash_lookup(machines, mid)) {
653 for (p = machbuf; *mach && *mach != '.'; mach++)
656 fprintf(out, "%s.cluster\tHS CNAME smsinternal-%d.cluster\n",
659 for (id = mid + 1; id < maxmach; id++) {
660 if ((ps = machs[id]) && !SET_CMP(ms, ps)) {
663 if (mach = hash_lookup(machines, id)) {
664 for (p = machbuf; *mach && *mach != '.'; mach++)
667 fprintf(out,"%s.cluster\tHS CNAME smsinternal-%d.cluster\n",
676 ## retrieve (name = c.#name, label = d.serv_label, data = d.serv_cluster)
677 ## where c.clu_id = d.clu_id {
681 fprintf(out, "%s.cluster\tHS UNSPECA \"%s %s\"\n",
688 fprintf(stderr, "Unsuccessful close of cluster.db\n");
699 char outf[64], outft[64];
702 ## char name[17], rp[17], sd[33], rm[33], *filetime;
705 sprintf(outf, "%s/printcap.db", HESIOD_DIR);
707 if (stat(outf, &sb) == 0) {
709 filetime = ingres_date_and_time(ftime);
710 ## retrieve (flag = int4(interval("min", tblstats.modtime - filetime)))
711 ## where tblstats.table = "printcap"
713 fprintf(stderr, "File printcap.db does not need to be rebuilt.\n");
718 sprintf(outft, "%s~", outf);
719 out = fopen(outft, "w");
721 perror("cannot open printcap.db for write");
725 fprintf(stderr, "Building printcap.db\n");
727 ## range of p is printcap
728 ## retrieve (name = p.#name, rp = p.#rp, sd = p.dir, rm = machine.#name)
729 ## where machine.mach_id = p.mach_id {
734 fprintf(out, "%s.pcap\tHS UNSPECA \"%s:rp=%s:rm=%s:sd=%s\"\n",
735 name, name, rp, rm, sd);
739 fprintf(stderr, "Unsuccessful close of pcap.db\n");
749 FILE *out, *old, *new;
750 char outf[64], outft[64], *mach;
754 ## char service[17], *filetime;
755 ## int port, flag1, flag2, id;
757 sprintf(outf, "%s/sloc.db", HESIOD_DIR);
759 if (stat(outf, &sb) == 0) {
761 filetime = ingres_date_and_time(ftime);
762 ## retrieve (flag1 = int4(interval("min", tblstats.modtime - filetime)))
763 ## where tblstats.table = "serverhosts"
764 ## retrieve (flag2 = int4(interval("min", tblstats.modtime - filetime)))
765 ## where tblstats.table = "machine"
766 if (flag1 < 0 && flag2 < 0) {
767 fprintf(stderr, "File sloc.db does not need to be rebuilt.\n");
772 sprintf(outft, "%s~", outf);
773 out = fopen(outft, "w");
775 perror("cannot open sloc.db for write");
779 fprintf(stderr, "Building sloc.db\n");
782 ## range of s is serverhosts
783 ## retrieve (service = s.#service, id = s.mach_id)
784 ## sort by #service {
786 if (mach = hash_lookup(machines, id))
787 fprintf(out, "%s.sloc\tHS UNSPECA %s\n", service, mach);
791 fprintf(stderr, "Unsuccessful close of sloc.db\n");
802 char outf[64], outft[64];
805 ## char mach[33], service[33], protocol[9], altserv[129], *filetime;
806 ## int port, flag1, flag2;
808 sprintf(outf, "%s/service.db", HESIOD_DIR);
810 if (stat(outf, &sb) == 0) {
812 filetime = ingres_date_and_time(ftime);
813 ## retrieve (flag1 = int4(interval("min", tblstats.modtime - filetime)))
814 ## where tblstats.table = "services"
816 fprintf(stderr, "File service.db does not need to be rebuilt.\n");
821 sprintf(outft, "%s~", outf);
822 out = fopen(outft, "w");
824 perror("cannot open service.db for write");
828 fprintf(stderr, "Building service.db\n");
830 ## range of s is services
831 ## retrieve (service = s.name, protocol = lowercase(s.#protocol),
835 fprintf(out, "%s.service\tHS UNSPECA \"%s %s %d\"\n",
836 service, service, protocol, port);
839 ## range of a is alias
840 ## retrieve (service = a.name, altserv = a.trans) where a.type = "SERVICE" {
843 fprintf(out, "%s.service\tHS CNAME %s.service\n", service, altserv);
847 fprintf(stderr, "Unsuccessful close of service.db\n");