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>
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]);
56 initialize_sms_error_table ();
57 sprintf(hesiod_dir, "%s/hesiod", DCM_DIR);
60 ## set lockmode session where level = table
62 changed = do_passwd();
63 changed += do_filsys();
64 changed += do_cluster();
65 changed += do_printcap();
66 changed += do_palladium();
68 changed += do_service();
69 changed += do_groups();
74 fprintf(stderr, "No files updated.\n");
75 if (argc == 2 && stat(argv[1], &sb) == 0)
80 fprintf(stderr, "Building tar file.\n");
81 sprintf(cmd, "cd %s; tar cf %s .", hesiod_dir, argv[1]);
91 * ingerr: (supposedly) called when Ingres indicates an error.
92 * I have not yet been able to get this to work to intercept a
93 * database open error.
95 #define INGRES_DEADLOCK 4700
97 static int ingerr(num)
103 case INGRES_DEADLOCK:
104 ingres_errno = SMS_DEADLOCK;
107 ingres_errno = SMS_INGRES_ERR;
109 com_err(whoami, SMS_INGRES_ERR, " code %d\n", *num);
110 critical_alert("DCM", "Hesiod build encountered INGRES ERROR %d", *num);
123 machines = create_hash(1000);
124 ## retrieve (name = machine.#name, id = machine.mach_id) {
125 hash_store(machines, id, strsave(strtrim(name)));
132 FILE *pout, *uout, *bout;
133 char poutf[64], uoutf[64], poutft[64], uoutft[64], boutf[64], boutft[64];
134 struct stat psb, usb, bsb;
138 ## char login[9], shell[33], fullname[33], oa[17], op[13], hp[17], *filetime;
139 ## char nn[17], ptype[9];
140 ## int uid, flag1, flag2, id, pid;
142 sprintf(poutf, "%s/passwd.db", hesiod_dir);
143 sprintf(uoutf, "%s/uid.db", hesiod_dir);
144 sprintf(boutf, "%s/pobox.db", hesiod_dir);
146 if (stat(poutf, &psb) == 0 && stat(uoutf, &usb) == 0 &&
147 stat(boutf, &bsb) == 0) {
148 ftime = min(min(psb.st_mtime, usb.st_mtime), bsb.st_mtime);
149 filetime = ingres_date_and_time(ftime);
150 ## retrieve (flag1 = int4(interval("min", tblstats.modtime - filetime)))
151 ## where tblstats.table = "users"
152 ## retrieve (flag2 = int4(interval("min", tblstats.modtime - filetime)))
153 ## where tblstats.table = "machine"
154 if (flag1 < 0 && flag2 < 0) {
155 fprintf(stderr, "Files passwd.db, uid.db, and pobox.db do not need to be rebuilt.\n");
160 sprintf(poutft, "%s~", poutf);
161 pout = fopen(poutft, "w");
163 perror("cannot open passwd.db~ for write");
166 sprintf(uoutft, "%s~", uoutf);
167 uout = fopen(uoutft, "w");
169 perror("cannot open uid.db~ for write");
172 sprintf(boutft, "%s~", boutf);
173 bout = fopen(boutft, "w");
175 perror("cannot open pobox.db for write");
179 fprintf(stderr, "Building passwd.db, uid.db, and pobox.db\n");
182 users = create_hash(12001);
183 ## range of u is users
184 ## retrieve (login = u.#login, uid = u.#uid, shell = u.#shell,
185 ## fullname = u.#fullname, nn = u.nickname, oa = u.office_addr,
186 ## op = u.office_phone, hp = u.home_phone, id = u.users_id,
187 ## pid = u.pop_id, ptype = u.potype)
188 ## where u.status = 1 {
196 u = (struct user *) malloc(sizeof(struct user));
197 strcpy(u->name, login);
199 hash_store(users, id, u);
200 fprintf(pout, "%s.passwd\tHS UNSPECA \"%s:*:%d:101:%s,%s,%s,%s,%s:/mit/%s:%s\"\n",
201 login, login, uid, fullname, nn, oa, op, hp, login, shell);
202 fprintf(uout, "%d.uid\tHS CNAME %s.passwd\n", uid, login);
203 if (ptype[0] == 'P' && (mach = hash_lookup(machines, pid))) {
204 fprintf(bout, "%s.pobox\tHS UNSPECA \"POP %s %s\"\n",
209 if (fclose(pout) || fclose(uout) || fclose(bout)) {
210 fprintf(stderr, "Unsuccessful file close of passwd.db, uid.db, or pobox.db\n");
222 FILE *iout, *gout, *lout;
223 char ioutf[64], goutf[64], loutf[64], buf[2048], *l;
225 register struct bucket *b, **p;
228 struct stat isb, gsb, lsb;
230 ## char name[33], *filetime;
231 ## int gid, id, lid, flag1, flag2, flag3;
234 sprintf(ioutf, "%s/gid.db", hesiod_dir);
235 sprintf(goutf, "%s/group.db", hesiod_dir);
236 sprintf(loutf, "%s/grplist.db", hesiod_dir);
238 if (stat(ioutf, &isb) == 0 && stat(goutf, &gsb) == 0 && stat(loutf, &lsb) == 0) {
239 ftime = min(isb.st_mtime, min(gsb.st_mtime, lsb.st_mtime));
240 filetime = ingres_date_and_time(ftime);
241 ## retrieve (flag1 = int4(interval("min", tblstats.modtime - filetime)))
242 ## where tblstats.table = "users"
243 ## retrieve (flag2 = int4(interval("min", tblstats.modtime - filetime)))
244 ## where tblstats.table = "list"
245 ## retrieve (flag3 = int4(interval("min", tblstats.modtime - filetime)))
246 ## where tblstats.table = "imembers"
247 if (flag1 < 0 && flag2 < 0 && flag3 < 0) {
248 fprintf(stderr, "Files gid.db, group.db and grplist.db do not need to be rebuilt.\n");
253 sprintf(buf, "%s~", ioutf);
254 iout = fopen(buf, "w");
256 perror("cannot open gid.db for write");
259 sprintf(buf, "%s~", goutf);
260 gout = fopen(buf, "w");
262 perror("cannot open group.db for write");
265 sprintf(buf, "%s~", loutf);
266 lout = fopen(buf, "w");
268 perror("cannot open grplist.db for write");
272 fprintf(stderr, "Building gid.db, group.db, and grplist.db\n");
274 /* make space for group list */
275 groups = create_hash(15001);
277 /* retrieve simple groups */
278 ## range of l is list
279 ## range of m is imembers
281 /* get lock records */
282 ## retrieve (name = l.modtime) where l.list_id = 0
283 ## retrieve (name = users.modtime) where users.users_id = 0
285 ## retrieve (name = l.#name, gid = l.#gid, lid = l.list_id)
286 ## where l.group != 0 and l.active != 0 {
288 sprintf(buf, "%s:%d", name, gid);
289 hash_store(groups, lid, strsave(buf));
290 fprintf(iout, "%d.gid\tHS CNAME %s.group\n", gid, name);
291 fprintf(gout, "%s.group\tHS UNSPECA \"%s:*:%d:\"\n",
298 /* now do grplists */
300 users = create_hash(12001);
301 ## range of u is users
302 ## retrieve (id = u.users_id, name = u.login) where u.status = 1 {
303 u = (struct user *) malloc(sizeof(struct user));
304 strcpy(u->name, strtrim(name));
306 hash_store(users, id, u);
310 ## repeat retrieve (lid = m.list_id, id = m.member_id)
311 ## where m.member_type = "USER" {
312 if ((hash_lookup(groups, lid) != NULL) &&
313 (u = (struct user *) hash_lookup(users, id))) {
314 g = (struct grp *) malloc(sizeof(struct grp));
322 for (p = &(users->data[users->size - 1]); p >= users->data; p--) {
323 for (b = *p; b; b = b->next) {
324 if ((g = ((struct user *)b->data)->lists) == NULL)
326 fprintf(lout, "%s.grplist\tHS UNSPECA \"",
327 ((struct user *)b->data)->name);
328 for (; g; g = g->next)
329 if (l = hash_lookup(groups, g->id)) {
338 if (fclose(iout) || fclose(gout) || fclose(lout)) {
339 fprintf(stderr, "Unsuccessful close of gid.db, group.db, or grplist.db\n");
352 char outf[64], outft[64], *mach, machbuf[128], *group;
356 struct save_queue *sq, *sq_create();
357 ## char name[33], type[9], loc[81], access[2], mount[33], trans[257];
358 ## char *filetime, comments[65];
359 ## int flag1, flag2, flag3, flag4, id, fid;
362 sprintf(outf, "%s/filsys.db", hesiod_dir);
364 if (stat(outf, &sb) == 0) {
366 filetime = ingres_date_and_time(ftime);
367 ## retrieve (flag1 = int4(interval("min", tblstats.modtime - filetime)))
368 ## where tblstats.table = "filesys"
369 ## retrieve (flag2 = int4(interval("min", tblstats.modtime - filetime)))
370 ## where tblstats.table = "machine"
371 ## retrieve (flag3 = int4(interval("min", tblstats.modtime - filetime)))
372 ## where tblstats.table = "alias"
373 ## retrieve (flag4 = int4(interval("min", tblstats.modtime - filetime)))
374 ## where tblstats.table = "fsgroup"
375 if (flag1 < 0 && flag2 < 0 && flag3 < 0 && flag4 < 0) {
376 fprintf(stderr, "File filsys.db does not need to be rebuilt.\n");
381 sprintf(outft, "%s~", outf);
382 out = fopen(outft, "w");
384 perror("cannot open filsys.db for write");
388 fprintf(stderr, "Building filsys.db\n");
392 ## range of f is filesys
393 ## retrieve (name = f.label, type = f.#type, loc = f.#name, id = f.mach_id,
394 ## access = f.#access, mount = f.#mount, comments = f.#comments,
395 ## fid = f.filsys_id) {
397 if (!strcmp(type, "NFS") || !strcmp(type, "RVD")) {
398 if (mach = hash_lookup(machines, id)) {
399 for (p = machbuf; *mach && *mach != '.'; mach++) {
401 *p++ = tolower(*mach);
406 fprintf(out, "%s.filsys\tHS UNSPECA \"%s %s %s %s %s\"\n",
407 strtrim(name), type, strtrim(loc), machbuf,
408 strtrim(access), strtrim(mount));
410 } else if (!strcmp(type, "AFS")) {
411 fprintf(out, "%s.filsys\tHS UNSPECA \"AFS %s %s %s\"\n",
412 strtrim(name), strtrim(loc), strtrim(access),
414 } else if (!strcmp(type, "ERR")) {
415 fprintf(out, "%s.filsys\tHS UNSPECA \"ERR %s\"\n",
416 name, strtrim(comments));
417 } else if (!strcmp(type, "FSGROUP")) {
418 sprintf(trans, "%s:%d", strtrim(name), fid);
419 sq_save_data(sq, strsave(trans));
423 ## range of g is fsgroup
424 while (sq_get_data(sq, &group)) {
425 fid = atoi(index(group, ':')+1);
426 *index(group, ':') = 0;
427 ## retrieve (type = f.#type, loc = f.#name, id = f.mach_id,
428 ## access = f.#access, mount = f.#mount, comments = f.#comments,
429 ## name = f.#label, trans = g.#key)
430 ## where f.filsys_id = g.filsys_id and g.group_id = fid
431 ## sort by #trans, #name {
433 if (!strcmp(type, "NFS") || !strcmp(type, "RVD")) {
434 if (mach = hash_lookup(machines, id)) {
435 for (p = machbuf; *mach && *mach != '.'; mach++) {
437 *p++ = tolower(*mach);
442 fprintf(out, "%s.filsys\tHS UNSPECA \"%s %s %s %s %s\"\n",
443 group, type, strtrim(loc), machbuf,
444 strtrim(access), strtrim(mount));
446 } else if (!strcmp(type, "AFS")) {
447 fprintf(out, "%s.filsys\tHS UNSPECA \"AFS %s %s %s\"\n",
448 group, strtrim(loc), strtrim(access), strtrim(mount));
449 } else if (!strcmp(type, "ERR")) {
450 fprintf(out, "%s.filsys\tHS UNSPECA \"ERR %s\"\n",
451 group, strtrim(comments));
458 ## range of a is alias
459 ## retrieve (name = a.#name, trans = a.#trans) where a.#type = "FILESYS" {
460 fprintf(out, "%s.filsys\tHS CNAME %s.filsys\n",
461 strtrim(name), strtrim(trans));
465 fprintf(stderr, "Unsuccessful close of filsys.db\n");
474 * Modified from sys/types.h:
476 int setsize; /* = howmany(setbits, NSETBITS) */
478 typedef long set_mask;
479 #define NSETBITS (sizeof(set_mask) * NBBY) /* bits per mask */
481 #define howmany(x, y) (((x)+((y)-1))/(y))
484 #define SET_SET(n, p) ((p)[(n)/NSETBITS] |= (1 << ((n) % NSETBITS)))
485 #define SET_CLR(n, p) ((p)[(n)/NSETBITS] &= ~(1 << ((n) % NSETBITS)))
486 #define SET_ISSET(n, p) ((p)[(n)/NSETBITS] & (1 << ((n) % NSETBITS)))
487 #define SET_CREATE() ((set_mask *)malloc(setsize * sizeof(set_mask)))
488 #define SET_ZERO(p) bzero((char *)(p), setsize * sizeof(set_mask))
489 #define SET_CMP(p1, p2) (bcmp((p1), (p2), setsize * sizeof(set_mask)))
497 for (i = 0; i < setsize * NSETBITS; i++)
498 if (SET_ISSET(i, set))
507 char outf[64], outft[64], *mach, machbuf[33], *p;
510 ## int flag1, flag2, flag3, flag4, maxmach, maxclu, mid, cid, id;
511 ## char name[33], label[17], data[33], *filetime;
512 set_mask **machs, *ms, *ps;
515 sprintf(outf, "%s/cluster.db", hesiod_dir);
517 if (stat(outf, &sb) == 0) {
519 filetime = ingres_date_and_time(ftime);
520 ## retrieve (flag1 = int4(interval("min", tblstats.modtime - filetime)))
521 ## where tblstats.table = "cluster"
522 ## retrieve (flag2 = int4(interval("min", tblstats.modtime - filetime)))
523 ## where tblstats.table = "machine"
524 ## retrieve (flag3 = int4(interval("min", tblstats.modtime - filetime)))
525 ## where tblstats.table = "mcmap"
526 ## retrieve (flag4 = int4(interval("min", tblstats.modtime - filetime)))
527 ## where tblstats.table = "svc"
528 if (flag1 < 0 && flag2 < 0 && flag3 < 0 && flag4 < 0) {
529 fprintf(stderr, "File cluster.db does not need to be rebuilt.\n");
534 sprintf(outft, "%s~", outf);
535 out = fopen(outft, "w");
537 perror("cannot open cluster.db for write");
541 fprintf(stderr, "Building cluster.db\n");
544 ## range of c is cluster
546 ## retrieve (maxclu = max(c.clu_id))
548 setsize = howmany(maxclu, NSETBITS);
549 ## range of m is machine
550 ## retrieve (maxmach = max(m.mach_id))
552 machs = (set_mask **)malloc((maxmach + 1) * sizeof(set_mask **));
553 bzero(machs, (maxmach + 1) * sizeof(int));
555 ## range of p is mcmap
556 ## retrieve (mid = p.mach_id, cid = p.clu_id) {
557 if (!(ms = machs[mid])) {
558 ms = machs[mid] = SET_CREATE();
565 for (mid = 1; mid < maxmach; mid++) {
569 if (nbitsset(ms) > 1) {
571 for (cid = 1; cid < maxclu; cid++) {
572 if (SET_ISSET(cid, ms)) {
573 ## repeat retrieve (label = d.serv_label, data = d.serv_cluster)
574 ## where d.clu_id = @cid {
578 "smsinternal-%d.cluster\tHS UNSPECA \"%s %s\"\n",
585 for (cid = 1; cid < maxclu; cid++)
586 if (SET_ISSET(cid, ms)) break;
587 ## repeat retrieve (name = c.#name) where c.clu_id = @cid
591 if (mach = hash_lookup(machines, mid)) {
592 for (p = machbuf; *mach && *mach != '.'; mach++)
596 fprintf(out, "%s.cluster\tHS CNAME %s.cluster\n",
599 fprintf(out, "%s.cluster\tHS CNAME smsinternal-%d.cluster\n",
602 for (id = mid + 1; id < maxmach; id++) {
603 if ((ps = machs[id]) && !SET_CMP(ms, ps)) {
606 if (mach = hash_lookup(machines, id)) {
607 for (p = machbuf; *mach && *mach != '.'; mach++)
611 fprintf(out, "%s.cluster\tHS CNAME %s.cluster\n",
615 "%s.cluster\tHS CNAME smsinternal-%d.cluster\n",
624 ## retrieve (name = c.#name, label = d.serv_label, data = d.serv_cluster)
625 ## where c.clu_id = d.clu_id {
629 fprintf(out, "%s.cluster\tHS UNSPECA \"%s %s\"\n",
636 fprintf(stderr, "Unsuccessful close of cluster.db\n");
647 char outf[64], outft[64];
650 ## char name[17], rp[17], sd[33], rm[33], *filetime;
653 sprintf(outf, "%s/printcap.db", hesiod_dir);
655 if (stat(outf, &sb) == 0) {
657 filetime = ingres_date_and_time(ftime);
658 ## retrieve (flag = int4(interval("min", tblstats.modtime - filetime)))
659 ## where tblstats.table = "printcap"
661 fprintf(stderr, "File printcap.db does not need to be rebuilt.\n");
666 sprintf(outft, "%s~", outf);
667 out = fopen(outft, "w");
669 perror("cannot open printcap.db for write");
673 fprintf(stderr, "Building printcap.db\n");
675 ## range of p is printcap
676 ## retrieve (name = p.#name, rp = p.#rp, sd = p.dir, rm = machine.#name)
677 ## where machine.mach_id = p.mach_id {
682 fprintf(out, "%s.pcap\tHS UNSPECA \"%s:rp=%s:rm=%s:sd=%s\"\n",
683 name, name, rp, rm, sd);
687 fprintf(stderr, "Unsuccessful close of pcap.db\n");
698 char outf[64], outft[64];
701 ## char name[33], rm[33], trans[129], *filetime;
702 ## int flag, flag1, ident;
704 sprintf(outf, "%s/palladium.db", hesiod_dir);
706 if (stat(outf, &sb) == 0) {
708 filetime = ingres_date_and_time(ftime);
709 ## retrieve (flag = int4(interval("min", tblstats.modtime - filetime)))
710 ## where tblstats.table = "palladium"
711 ## retrieve (flag1 = int4(interval("min", tblstats.modtime - filetime)))
712 ## where tblstats.table = "alias"
713 if (flag < 0 && flag1 < 0) {
714 fprintf(stderr, "File palladium.db does not need to be rebuilt.\n");
719 sprintf(outft, "%s~", outf);
720 out = fopen(outft, "w");
722 perror("cannot open palladium.db for write");
726 fprintf(stderr, "Building palladium.db\n");
728 ## range of p is palladium
729 ## retrieve (name = p.#name, ident = p.#ident, rm = machine.#name)
730 ## where machine.mach_id = p.mach_id {
734 "%s.palladium\tHS UNSPECA \"%s %d %s interface directory\"\n",
735 name, rm, ident, name);
737 ## range of a is alias
738 ## retrieve (name = a.#name, trans = a.#trans) where a.#type = "PALLADIUM" {
741 fprintf(out, "%s.palladium\tHS UNSPECA \"%s\"\n", name, trans);
745 fprintf(stderr, "Unsuccessful close of palladium.db\n");
756 char outf[64], outft[64], *mach;
759 ## char service[17], *filetime;
760 ## int flag1, flag2, id;
762 sprintf(outf, "%s/sloc.db", hesiod_dir);
764 if (stat(outf, &sb) == 0) {
766 filetime = ingres_date_and_time(ftime);
767 ## retrieve (flag1 = int4(interval("min", tblstats.modtime - filetime)))
768 ## where tblstats.table = "serverhosts"
769 ## retrieve (flag2 = int4(interval("min", tblstats.modtime - filetime)))
770 ## where tblstats.table = "machine"
771 if (flag1 < 0 && flag2 < 0) {
772 fprintf(stderr, "File sloc.db does not need to be rebuilt.\n");
777 sprintf(outft, "%s~", outf);
778 out = fopen(outft, "w");
780 perror("cannot open sloc.db for write");
784 fprintf(stderr, "Building sloc.db\n");
787 ## range of s is serverhosts
788 ## retrieve (service = s.#service, id = s.mach_id)
789 ## sort by #service {
791 if (mach = hash_lookup(machines, id))
792 fprintf(out, "%s.sloc\tHS UNSPECA %s\n", service, mach);
796 fprintf(stderr, "Unsuccessful close of sloc.db\n");
807 char outf[64], outft[64];
810 ## char service[33], protocol[9], altserv[129], *filetime;
813 sprintf(outf, "%s/service.db", hesiod_dir);
815 if (stat(outf, &sb) == 0) {
817 filetime = ingres_date_and_time(ftime);
818 ## retrieve (flag1 = int4(interval("min", tblstats.modtime - filetime)))
819 ## where tblstats.table = "services"
821 fprintf(stderr, "File service.db does not need to be rebuilt.\n");
826 sprintf(outft, "%s~", outf);
827 out = fopen(outft, "w");
829 perror("cannot open service.db for write");
833 fprintf(stderr, "Building service.db\n");
835 ## range of s is services
836 ## retrieve (service = s.name, protocol = lowercase(s.#protocol),
840 fprintf(out, "%s.service\tHS UNSPECA \"%s %s %d\"\n",
841 service, service, protocol, port);
844 ## range of a is alias
845 ## retrieve (service = a.name, altserv = a.trans) where a.type = "SERVICE" {
848 fprintf(out, "%s.service\tHS CNAME %s.service\n", service, altserv);
852 fprintf(stderr, "Unsuccessful close of service.db\n");