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>
23 #define HTYPE "UNSPECA"
30 #define min(x,y) ((x) < (y) ? (x) : (y))
31 struct hash *machines = NULL;
32 struct hash *users = NULL;
33 char *whoami = "hesiod.gen";
44 char *malloc(), *strsave();
45 char *ingres_date_and_time(), *ingres_time(), *ingres_date();
57 fprintf(stderr, "usage: %s [outfile]\n", argv[0]);
62 initialize_sms_error_table ();
63 sprintf(hesiod_dir, "%s/hesiod", DCM_DIR);
66 ## set lockmode session where level = table
68 changed = do_passwd();
69 changed += do_filsys();
70 changed += do_cluster();
71 changed += do_printcap();
72 changed += do_palladium();
74 changed += do_service();
75 changed += do_groups();
80 fprintf(stderr, "No files updated.\n");
81 if (argc == 2 && stat(argv[1], &sb) == 0)
86 fprintf(stderr, "Building tar file.\n");
87 sprintf(cmd, "cd %s; tar cf %s .", hesiod_dir, argv[1]);
97 * ingerr: (supposedly) called when Ingres indicates an error.
98 * I have not yet been able to get this to work to intercept a
99 * database open error.
101 #define INGRES_DEADLOCK 4700
103 static int ingerr(num)
109 case INGRES_DEADLOCK:
110 ingres_errno = SMS_DEADLOCK;
113 ingres_errno = SMS_INGRES_ERR;
115 com_err(whoami, SMS_INGRES_ERR, " code %d\n", *num);
116 critical_alert("DCM", "Hesiod build encountered INGRES ERROR %d", *num);
129 machines = create_hash(1000);
130 ## retrieve (name = machine.#name, id = machine.mach_id) {
131 hash_store(machines, id, strsave(strtrim(name)));
138 FILE *pout, *uout, *bout;
139 char poutf[64], uoutf[64], poutft[64], uoutft[64], boutf[64], boutft[64];
140 struct stat psb, usb, bsb;
144 ## char login[9], shell[33], fullname[33], oa[17], op[13], hp[17], *filetime;
145 ## char nn[17], ptype[9];
146 ## int uid, flag1, flag2, id, pid;
148 sprintf(poutf, "%s/passwd.db", hesiod_dir);
149 sprintf(uoutf, "%s/uid.db", hesiod_dir);
150 sprintf(boutf, "%s/pobox.db", hesiod_dir);
152 if (stat(poutf, &psb) == 0 && stat(uoutf, &usb) == 0 &&
153 stat(boutf, &bsb) == 0) {
154 ftime = min(min(psb.st_mtime, usb.st_mtime), bsb.st_mtime);
155 filetime = ingres_date_and_time(ftime);
156 ## retrieve (flag1 = int4(interval("min", tblstats.modtime - filetime)))
157 ## where tblstats.table = "users"
158 ## retrieve (flag2 = int4(interval("min", tblstats.modtime - filetime)))
159 ## where tblstats.table = "machine"
160 if (flag1 < 0 && flag2 < 0) {
161 fprintf(stderr, "Files passwd.db, uid.db, and pobox.db do not need to be rebuilt.\n");
166 sprintf(poutft, "%s~", poutf);
167 pout = fopen(poutft, "w");
169 perror("cannot open passwd.db~ for write");
172 sprintf(uoutft, "%s~", uoutf);
173 uout = fopen(uoutft, "w");
175 perror("cannot open uid.db~ for write");
178 sprintf(boutft, "%s~", boutf);
179 bout = fopen(boutft, "w");
181 perror("cannot open pobox.db for write");
185 fprintf(stderr, "Building passwd.db, uid.db, and pobox.db\n");
188 users = create_hash(12001);
189 ## range of u is users
190 ## retrieve (login = u.#login, uid = u.#uid, shell = u.#shell,
191 ## fullname = u.#fullname, nn = u.nickname, oa = u.office_addr,
192 ## op = u.office_phone, hp = u.home_phone, id = u.users_id,
193 ## pid = u.pop_id, ptype = u.potype)
194 ## where u.status = 1 {
202 u = (struct user *) malloc(sizeof(struct user));
203 strcpy(u->name, login);
205 hash_store(users, id, u);
206 fprintf(pout, "%s.passwd\tHS %s \"%s:*:%d:101:%s,%s,%s,%s,%s:/mit/%s:%s\"\n",
207 login, HTYPE, login, uid, fullname, nn, oa, op, hp, login, shell);
208 fprintf(uout, "%d.uid\tHS CNAME %s.passwd\n", uid, login);
209 if (ptype[0] == 'P' && (mach = hash_lookup(machines, pid))) {
210 fprintf(bout, "%s.pobox\tHS %s \"POP %s %s\"\n",
211 login, HTYPE, mach, login);
215 if (fclose(pout) || fclose(uout) || fclose(bout)) {
216 fprintf(stderr, "Unsuccessful file close of passwd.db, uid.db, or pobox.db\n");
228 FILE *iout, *gout, *lout;
229 char ioutf[64], goutf[64], loutf[64], buf[2048], *l;
231 register struct bucket *b, **p;
234 struct stat isb, gsb, lsb;
236 ## char name[33], *filetime;
237 ## int gid, id, lid, flag1, flag2, flag3;
240 sprintf(ioutf, "%s/gid.db", hesiod_dir);
241 sprintf(goutf, "%s/group.db", hesiod_dir);
242 sprintf(loutf, "%s/grplist.db", hesiod_dir);
244 if (stat(ioutf, &isb) == 0 && stat(goutf, &gsb) == 0 && stat(loutf, &lsb) == 0) {
245 ftime = min(isb.st_mtime, min(gsb.st_mtime, lsb.st_mtime));
246 filetime = ingres_date_and_time(ftime);
247 ## retrieve (flag1 = int4(interval("min", tblstats.modtime - filetime)))
248 ## where tblstats.table = "users"
249 ## retrieve (flag2 = int4(interval("min", tblstats.modtime - filetime)))
250 ## where tblstats.table = "list"
251 ## retrieve (flag3 = int4(interval("min", tblstats.modtime - filetime)))
252 ## where tblstats.table = "imembers"
253 if (flag1 < 0 && flag2 < 0 && flag3 < 0) {
254 fprintf(stderr, "Files gid.db, group.db and grplist.db do not need to be rebuilt.\n");
259 sprintf(buf, "%s~", ioutf);
260 iout = fopen(buf, "w");
262 perror("cannot open gid.db for write");
265 sprintf(buf, "%s~", goutf);
266 gout = fopen(buf, "w");
268 perror("cannot open group.db for write");
271 sprintf(buf, "%s~", loutf);
272 lout = fopen(buf, "w");
274 perror("cannot open grplist.db for write");
278 fprintf(stderr, "Building gid.db, group.db, and grplist.db\n");
280 /* make space for group list */
281 groups = create_hash(15001);
283 /* retrieve simple groups */
284 ## range of l is list
285 ## range of m is imembers
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 %s \"%s:*:%d:\"\n",
298 name, HTYPE, name, gid);
304 /* now do grplists */
306 users = create_hash(12001);
307 ## range of u is users
308 ## retrieve (id = u.users_id, name = u.login) where u.status = 1 {
309 u = (struct user *) malloc(sizeof(struct user));
310 strcpy(u->name, strtrim(name));
312 hash_store(users, id, u);
316 ## repeat retrieve (lid = m.list_id, id = m.member_id)
317 ## where m.member_type = "USER" {
318 if ((hash_lookup(groups, lid) != NULL) &&
319 (u = (struct user *) hash_lookup(users, id))) {
320 g = (struct grp *) malloc(sizeof(struct grp));
328 for (p = &(users->data[users->size - 1]); p >= users->data; p--) {
329 for (b = *p; b; b = b->next) {
330 if ((g = ((struct user *)b->data)->lists) == NULL)
332 fprintf(lout, "%s.grplist\tHS %s \"",
333 ((struct user *)b->data)->name, HTYPE);
334 for (; g; g = g->next)
335 if (l = hash_lookup(groups, g->id)) {
344 if (fclose(iout) || fclose(gout) || fclose(lout)) {
345 fprintf(stderr, "Unsuccessful close of gid.db, group.db, or grplist.db\n");
358 char outf[64], outft[64], *mach, machbuf[128], *group;
362 struct save_queue *sq, *sq_create();
363 ## char name[33], type[9], loc[81], access[2], mount[33], trans[257];
364 ## char *filetime, comments[65];
365 ## int flag1, flag2, flag3, flag4, id, fid;
368 sprintf(outf, "%s/filsys.db", hesiod_dir);
370 if (stat(outf, &sb) == 0) {
372 filetime = ingres_date_and_time(ftime);
373 ## retrieve (flag1 = int4(interval("min", tblstats.modtime - filetime)))
374 ## where tblstats.table = "filesys"
375 ## retrieve (flag2 = int4(interval("min", tblstats.modtime - filetime)))
376 ## where tblstats.table = "machine"
377 ## retrieve (flag3 = int4(interval("min", tblstats.modtime - filetime)))
378 ## where tblstats.table = "alias"
379 ## retrieve (flag4 = int4(interval("min", tblstats.modtime - filetime)))
380 ## where tblstats.table = "fsgroup"
381 if (flag1 < 0 && flag2 < 0 && flag3 < 0 && flag4 < 0) {
382 fprintf(stderr, "File filsys.db does not need to be rebuilt.\n");
387 sprintf(outft, "%s~", outf);
388 out = fopen(outft, "w");
390 perror("cannot open filsys.db for write");
394 fprintf(stderr, "Building filsys.db\n");
398 ## range of f is filesys
399 ## retrieve (name = f.label, type = f.#type, loc = f.#name, id = f.mach_id,
400 ## access = f.#access, mount = f.#mount, comments = f.#comments,
401 ## fid = f.filsys_id) {
403 if (!strcmp(type, "NFS") || !strcmp(type, "RVD")) {
404 if (mach = hash_lookup(machines, id)) {
405 for (p = machbuf; *mach && *mach != '.'; mach++) {
407 *p++ = tolower(*mach);
412 fprintf(out, "%s.filsys\tHS %s \"%s %s %s %s %s\"\n",
413 strtrim(name), HTYPE, type, strtrim(loc), machbuf,
414 strtrim(access), strtrim(mount));
416 } else if (!strcmp(type, "AFS")) {
417 fprintf(out, "%s.filsys\tHS %s \"AFS %s %s %s\"\n",
418 strtrim(name), HTYPE, strtrim(loc), strtrim(access),
420 } else if (!strcmp(type, "ERR")) {
421 fprintf(out, "%s.filsys\tHS %s \"ERR %s\"\n",
422 name, HTYPE, strtrim(comments));
423 } else if (!strcmp(type, "FSGROUP")) {
424 sprintf(trans, "%s:%d", strtrim(name), fid);
425 sq_save_data(sq, strsave(trans));
429 ## range of g is fsgroup
430 while (sq_get_data(sq, &group)) {
431 fid = atoi(index(group, ':')+1);
432 *index(group, ':') = 0;
433 ## retrieve (type = f.#type, loc = f.#name, id = f.mach_id,
434 ## access = f.#access, mount = f.#mount, comments = f.#comments,
435 ## name = f.#label, trans = g.#key)
436 ## where f.filsys_id = g.filsys_id and g.group_id = fid
437 ## sort by #trans, #name {
439 if (!strcmp(type, "NFS") || !strcmp(type, "RVD")) {
440 if (mach = hash_lookup(machines, id)) {
441 for (p = machbuf; *mach && *mach != '.'; mach++) {
443 *p++ = tolower(*mach);
448 fprintf(out, "%s.filsys\tHS %s \"%s %s %s %s %s\"\n",
449 group, HTYPE, type, strtrim(loc), machbuf,
450 strtrim(access), strtrim(mount));
452 } else if (!strcmp(type, "AFS")) {
453 fprintf(out, "%s.filsys\tHS %s \"AFS %s %s %s\"\n",
454 group, HTYPE, strtrim(loc), strtrim(access),
456 } else if (!strcmp(type, "ERR")) {
457 fprintf(out, "%s.filsys\tHS %s \"ERR %s\"\n",
458 group, HTYPE, strtrim(comments));
465 ## range of a is alias
466 ## retrieve (name = a.#name, trans = a.#trans) where a.#type = "FILESYS" {
467 fprintf(out, "%s.filsys\tHS CNAME %s.filsys\n",
468 strtrim(name), strtrim(trans));
472 fprintf(stderr, "Unsuccessful close of filsys.db\n");
481 * Modified from sys/types.h:
483 int setsize; /* = howmany(setbits, NSETBITS) */
485 typedef long set_mask;
486 #define NSETBITS (sizeof(set_mask) * NBBY) /* bits per mask */
488 #define howmany(x, y) (((x)+((y)-1))/(y))
491 #define SET_SET(n, p) ((p)[(n)/NSETBITS] |= (1 << ((n) % NSETBITS)))
492 #define SET_CLR(n, p) ((p)[(n)/NSETBITS] &= ~(1 << ((n) % NSETBITS)))
493 #define SET_ISSET(n, p) ((p)[(n)/NSETBITS] & (1 << ((n) % NSETBITS)))
494 #define SET_CREATE() ((set_mask *)malloc(setsize * sizeof(set_mask)))
495 #define SET_ZERO(p) bzero((char *)(p), setsize * sizeof(set_mask))
496 #define SET_CMP(p1, p2) (bcmp((p1), (p2), setsize * sizeof(set_mask)))
504 for (i = 0; i < setsize * NSETBITS; i++)
505 if (SET_ISSET(i, set))
514 char outf[64], outft[64], *mach, machbuf[33], *p;
517 ## int flag1, flag2, flag3, flag4, maxmach, maxclu, mid, cid, id;
518 ## char name[33], label[17], data[33], *filetime;
519 set_mask **machs, *ms, *ps;
522 sprintf(outf, "%s/cluster.db", hesiod_dir);
524 if (stat(outf, &sb) == 0) {
526 filetime = ingres_date_and_time(ftime);
527 ## retrieve (flag1 = int4(interval("min", tblstats.modtime - filetime)))
528 ## where tblstats.table = "cluster"
529 ## retrieve (flag2 = int4(interval("min", tblstats.modtime - filetime)))
530 ## where tblstats.table = "machine"
531 ## retrieve (flag3 = int4(interval("min", tblstats.modtime - filetime)))
532 ## where tblstats.table = "mcmap"
533 ## retrieve (flag4 = int4(interval("min", tblstats.modtime - filetime)))
534 ## where tblstats.table = "svc"
535 if (flag1 < 0 && flag2 < 0 && flag3 < 0 && flag4 < 0) {
536 fprintf(stderr, "File cluster.db does not need to be rebuilt.\n");
541 sprintf(outft, "%s~", outf);
542 out = fopen(outft, "w");
544 perror("cannot open cluster.db for write");
548 fprintf(stderr, "Building cluster.db\n");
551 ## range of c is cluster
553 ## retrieve (maxclu = max(c.clu_id))
555 setsize = howmany(maxclu, NSETBITS);
556 ## range of m is machine
557 ## retrieve (maxmach = max(m.mach_id))
559 machs = (set_mask **)malloc((maxmach + 1) * sizeof(set_mask **));
560 bzero(machs, (maxmach + 1) * sizeof(int));
562 ## range of p is mcmap
563 ## retrieve (mid = p.mach_id, cid = p.clu_id) {
564 if (!(ms = machs[mid])) {
565 ms = machs[mid] = SET_CREATE();
572 for (mid = 1; mid < maxmach; mid++) {
576 if (nbitsset(ms) > 1) {
578 for (cid = 1; cid < maxclu; cid++) {
579 if (SET_ISSET(cid, ms)) {
580 ## repeat retrieve (label = d.serv_label, data = d.serv_cluster)
581 ## where d.clu_id = @cid {
585 "smsinternal-%d.cluster\tHS %s \"%s %s\"\n",
586 mid, HTYPE, label, data);
592 for (cid = 1; cid < maxclu; cid++)
593 if (SET_ISSET(cid, ms)) break;
594 ## repeat retrieve (name = c.#name) where c.clu_id = @cid
598 if (mach = hash_lookup(machines, mid)) {
599 for (p = machbuf; *mach && *mach != '.'; mach++)
603 fprintf(out, "%s.cluster\tHS CNAME %s.cluster\n",
606 fprintf(out, "%s.cluster\tHS CNAME smsinternal-%d.cluster\n",
609 for (id = mid + 1; id < maxmach; id++) {
610 if ((ps = machs[id]) && !SET_CMP(ms, ps)) {
613 if (mach = hash_lookup(machines, id)) {
614 for (p = machbuf; *mach && *mach != '.'; mach++)
618 fprintf(out, "%s.cluster\tHS CNAME %s.cluster\n",
622 "%s.cluster\tHS CNAME smsinternal-%d.cluster\n",
631 ## retrieve (name = c.#name, label = d.serv_label, data = d.serv_cluster)
632 ## where c.clu_id = d.clu_id {
636 fprintf(out, "%s.cluster\tHS %s \"%s %s\"\n",
637 name, HTYPE, label, data);
643 fprintf(stderr, "Unsuccessful close of cluster.db\n");
654 char outf[64], outft[64];
657 ## char name[17], rp[17], sd[33], rm[33], *filetime;
660 sprintf(outf, "%s/printcap.db", hesiod_dir);
662 if (stat(outf, &sb) == 0) {
664 filetime = ingres_date_and_time(ftime);
665 ## retrieve (flag = int4(interval("min", tblstats.modtime - filetime)))
666 ## where tblstats.table = "printcap"
668 fprintf(stderr, "File printcap.db does not need to be rebuilt.\n");
673 sprintf(outft, "%s~", outf);
674 out = fopen(outft, "w");
676 perror("cannot open printcap.db for write");
680 fprintf(stderr, "Building printcap.db\n");
682 ## range of p is printcap
683 ## retrieve (name = p.#name, rp = p.#rp, sd = p.dir, rm = machine.#name)
684 ## where machine.mach_id = p.mach_id {
689 fprintf(out, "%s.pcap\tHS %s \"%s:rp=%s:rm=%s:sd=%s\"\n",
690 name, HTYPE, name, rp, rm, sd);
694 fprintf(stderr, "Unsuccessful close of pcap.db\n");
705 char outf[64], outft[64];
708 ## char name[33], rm[33], trans[129], *filetime;
709 ## int flag, flag1, ident;
711 sprintf(outf, "%s/palladium.db", hesiod_dir);
713 if (stat(outf, &sb) == 0) {
715 filetime = ingres_date_and_time(ftime);
716 ## retrieve (flag = int4(interval("min", tblstats.modtime - filetime)))
717 ## where tblstats.table = "palladium"
718 ## retrieve (flag1 = int4(interval("min", tblstats.modtime - filetime)))
719 ## where tblstats.table = "alias"
720 if (flag < 0 && flag1 < 0) {
721 fprintf(stderr, "File palladium.db does not need to be rebuilt.\n");
726 sprintf(outft, "%s~", outf);
727 out = fopen(outft, "w");
729 perror("cannot open palladium.db for write");
733 fprintf(stderr, "Building palladium.db\n");
735 ## range of p is palladium
736 ## retrieve (name = p.#name, ident = p.#ident, rm = machine.#name)
737 ## where machine.mach_id = p.mach_id {
741 "%s.palladium\tHS %s \"%s %d %s interface directory\"\n",
742 name, HTYPE, rm, ident, name);
744 ## range of a is alias
745 ## retrieve (name = a.#name, trans = a.#trans) where a.#type = "PALLADIUM" {
748 fprintf(out, "%s.palladium\tHS %s \"%s\"\n", name, HTYPE, trans);
752 fprintf(stderr, "Unsuccessful close of palladium.db\n");
763 char outf[64], outft[64], *mach;
766 ## char service[17], *filetime;
767 ## int flag1, flag2, id;
769 sprintf(outf, "%s/sloc.db", hesiod_dir);
771 if (stat(outf, &sb) == 0) {
773 filetime = ingres_date_and_time(ftime);
774 ## retrieve (flag1 = int4(interval("min", tblstats.modtime - filetime)))
775 ## where tblstats.table = "serverhosts"
776 ## retrieve (flag2 = int4(interval("min", tblstats.modtime - filetime)))
777 ## where tblstats.table = "machine"
778 if (flag1 < 0 && flag2 < 0) {
779 fprintf(stderr, "File sloc.db does not need to be rebuilt.\n");
784 sprintf(outft, "%s~", outf);
785 out = fopen(outft, "w");
787 perror("cannot open sloc.db for write");
791 fprintf(stderr, "Building sloc.db\n");
794 ## range of s is serverhosts
795 ## retrieve (service = s.#service, id = s.mach_id)
796 ## sort by #service {
798 if (mach = hash_lookup(machines, id))
799 fprintf(out, "%s.sloc\tHS %s %s\n", service, HTYPE, mach);
803 fprintf(stderr, "Unsuccessful close of sloc.db\n");
814 char outf[64], outft[64];
817 ## char service[33], protocol[9], altserv[129], *filetime;
820 sprintf(outf, "%s/service.db", hesiod_dir);
822 if (stat(outf, &sb) == 0) {
824 filetime = ingres_date_and_time(ftime);
825 ## retrieve (flag1 = int4(interval("min", tblstats.modtime - filetime)))
826 ## where tblstats.table = "services"
828 fprintf(stderr, "File service.db does not need to be rebuilt.\n");
833 sprintf(outft, "%s~", outf);
834 out = fopen(outft, "w");
836 perror("cannot open service.db for write");
840 fprintf(stderr, "Building service.db\n");
842 ## range of s is services
843 ## retrieve (service = s.name, protocol = lowercase(s.#protocol),
847 fprintf(out, "%s.service\tHS %s \"%s %s %d\"\n",
848 service, HTYPE, service, protocol, port);
851 ## range of a is alias
852 ## retrieve (service = a.name, altserv = a.trans) where a.type = "SERVICE" {
855 fprintf(out, "%s.service\tHS CNAME %s.service\n", service, altserv);
859 fprintf(stderr, "Unsuccessful close of service.db\n");