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.
11 #include <sys/types.h>
16 #define HESIOD_DIR "/u1/sms/dcm/hesiod"
18 #define min(x,y) ((x) < (y) ? (x) : (y))
19 struct hash *machines = NULL;
20 struct hash *users = NULL;
31 char *malloc(), *strsave();
32 char *ingres_date_and_time(), *ingres_time(), *ingres_date();
43 fprintf(stderr, "usage: %s [outfile]\n", argv[0]);
49 changed = do_passwd();
50 changed += do_pobox();
51 changed += do_filsys();
52 changed += do_cluster();
53 changed += do_printers();
55 changed += do_service();
56 changed += do_groups();
61 fprintf(stderr, "No files updated.\n");
62 if (argc == 2 && stat(argv[1], &sb) == 0)
67 fprintf(stderr, "Building tar file.\n");
68 sprintf(cmd, "cd %s; tar cf %s .", HESIOD_DIR, argv[1]);
85 machines = create_hash(1000);
86 ## retrieve (name = machine.#name, id = machine.mach_id) {
87 hash_store(machines, id, strsave(strtrim(name)));
95 char poutf[64], uoutf[64], poutft[64], uoutft[64];
99 ## char login[9], shell[33], fullname[33], oa[17], op[13], hp[17], *filetime;
101 ## int uid, flag, id;
103 sprintf(poutf, "%s/passwd.db", HESIOD_DIR);
104 sprintf(uoutf, "%s/uid.db", HESIOD_DIR);
106 if (stat(poutf, &psb) == 0 && stat(uoutf, &usb) == 0) {
107 ftime = min(psb.st_mtime, usb.st_mtime);
108 filetime = ingres_date_and_time(ftime);
109 ## retrieve (flag = int4(interval("min", tblstats.modtime - filetime)))
110 ## where tblstats.table = "users"
112 fprintf(stderr, "Files passwd.db and uid.db do not need to be rebuilt.\n");
117 sprintf(poutft, "%s~", poutf);
118 pout = fopen(poutft, "w");
120 perror("cannot open passwd.db~ for write");
123 sprintf(uoutft, "%s~", uoutf);
124 uout = fopen(uoutft, "w");
126 perror("cannot open uid.db~ for write");
130 fprintf(stderr, "Building passwd.db and uid.db\n");
132 users = create_hash(10000);
133 ## range of u is users
134 ## retrieve (login = u.#login, uid = u.#uid, shell = u.#shell,
135 ## fullname = u.#fullname, nn = u.nickname, oa = u.office_addr,
136 ## op = u.office_phone, hp = u.home_phone, id = u.users_id)
137 ## where u.status = 1 {
145 u = (struct user *) malloc(sizeof(struct user));
146 strcpy(u->name, login);
148 hash_store(users, id, u);
149 fprintf(pout, "%s.passwd\tHS UNSPECA \"%s:*:%d:101:%s,%s,%s,%s,%s:/mit/%s:%s\"\n",
150 login, login, uid, fullname, nn, oa, op, hp, login, shell);
151 fprintf(uout, "%d.uid\tHS CNAME %s.passwd\n", uid, login);
154 if (fclose(pout) || fclose(uout)) {
155 fprintf(stderr, "Unsuccessful file close of passwd.db or uid.db\n");
167 char outf[64], outft[64], *mach;
170 ## char login[9], *filetime;
171 ## int flag1, flag2, id;
173 sprintf(outf, "%s/pobox.db", HESIOD_DIR);
175 if (stat(outf, &sb) == 0) {
177 filetime = ingres_date_and_time(ftime);
178 ## retrieve (flag1 = int4(interval("min", tblstats.modtime - filetime)))
179 ## where tblstats.table = "users"
180 ## retrieve (flag2 = int4(interval("min", tblstats.modtime - filetime)))
181 ## where tblstats.table = "machine"
182 if (flag1 < 0 && flag2 < 0) {
183 fprintf(stderr,"File pobox.db does not need to be rebuilt.\n");
188 sprintf(outft, "%s~", outf);
189 out = fopen(outft, "w");
191 perror("cannot open pobox.db for write");
195 fprintf(stderr, "Building pobox.db\n");
198 ## range of u is users
199 ## retrieve (login = u.#login, id = u.pop_id)
200 ## where u.status = 1 and u.potype = "POP" {
202 if (mach = hash_lookup(machines, id))
203 fprintf(out, "%s.pobox\tHS UNSPECA \"POP %s %s\"\n",
208 fprintf(stderr, "Unsuccessful close of pobox.db\n");
216 /************************************************************************
217 * WARNING: this routine mallocs a large amount of memory which it never frees.
218 * This is considered OK only because main has been arranged to call this
219 * last, so the process will exit shortly after we return.
224 FILE *iout, *gout, *lout;
225 char ioutf[64], goutf[64], loutf[64], buf[256], *l;
227 register struct bucket *b, **p;
230 struct stat isb, gsb, lsb;
232 register struct save_queue *sq;
233 struct save_queue *sq_create();
235 ## char name[33], *filetime;
236 ## int gid, id, lid, flag1, flag2, flag3, maxid;
239 sprintf(ioutf, "%s/gid.db", HESIOD_DIR);
240 sprintf(goutf, "%s/group.db", HESIOD_DIR);
241 sprintf(loutf, "%s/grplist.db", HESIOD_DIR);
243 if (stat(ioutf, &isb) == 0 && stat(goutf, &gsb) == 0 && stat(loutf, &lsb) == 0) {
244 ftime = min(isb.st_mtime, min(gsb.st_mtime, lsb.st_mtime));
245 filetime = ingres_date_and_time(ftime);
246 ## retrieve (flag1 = int4(interval("min", tblstats.modtime - filetime)))
247 ## where tblstats.table = "users"
248 ## retrieve (flag2 = int4(interval("min", tblstats.modtime - filetime)))
249 ## where tblstats.table = "list"
250 ## retrieve (flag3 = int4(interval("min", tblstats.modtime - filetime)))
251 ## where tblstats.table = "members"
252 if (flag1 < 0 && flag2 < 0 && flag3 < 0) {
253 fprintf(stderr, "Files gid.db, group.db and grplist.db do not need to be rebuilt.\n");
258 sprintf(buf, "%s~", ioutf);
259 iout = fopen(buf, "w");
261 perror("cannot open gid.db for write");
264 sprintf(buf, "%s~", goutf);
265 gout = fopen(buf, "w");
267 perror("cannot open group.db for write");
270 sprintf(buf, "%s~", loutf);
271 lout = fopen(buf, "w");
273 perror("cannot open grplist.db for write");
277 fprintf(stderr, "Building gid.db, group.db, and grplist.db\n");
279 /* make space for group list */
280 groups = create_hash(15000);
282 /* retrieve simple groups */
283 ## range of l is list
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 /* get special cases: lists that aren't groups themselves but are
299 * members of groups. */
301 ## range of m is members
302 ## retrieve (name = list.#name, gid = list.#gid, lid = l.list_id)
303 ## where l.group = 0 and l.active != 0 and m.member_type = "LIST" and
304 ## m.member_id = l.list_id and m.list_id = list.list_id and
307 if (l = hash_lookup(groups, lid)) {
308 sprintf(buf, "%s:%s:%d", l, name, gid);
311 sprintf(buf, "%s:%d", name, gid);
313 hash_store(groups, lid, strsave(buf));
314 sq_save_data(sq, lid);
317 while (sq_get_data(sq, &id)) {
318 ## repeat retrieve (name = l.#name, gid = l.#gid, lid = l.list_id)
319 ## where l.group = 0 and m.member_type = "LIST" and
320 ## m.member_id = l.list_id and m.list_id = @id {
322 if (l = hash_lookup(groups, lid)) {
323 sprintf(buf, "%s:%s:%d", l, name, gid);
326 sprintf(buf, "%s:%d", name, gid);
328 hash_store(groups, lid, strsave(buf));
329 sq_save_unique_data(sq, lid);
334 /* now do grplists */
336 users = create_hash(10000);
337 ## range of u is users
338 ## retrieve (id = u.users_id, name = u.login) where u.status = 1 {
339 u = (struct user *) malloc(sizeof(struct user));
340 strcpy(u->name, strtrim(name));
342 hash_store(users, id, u);
346 ## repeat retrieve (lid = m.list_id, id = m.member_id)
347 ## where m.member_type = "USER" {
348 if (u = (struct user *) hash_lookup(users, id)) {
349 g = (struct grp *) malloc(sizeof(struct grp));
357 for (p = &(users->data[users->size - 1]); p >= users->data; p--) {
358 for (b = *p; b; b = b->next) {
359 fprintf(lout, "%s.grplist\tHS UNSPECA \"",
360 ((struct user *)b->data)->name);
361 for (g = ((struct user *)b->data)->lists; g; g = g->next)
362 if (l = hash_lookup(groups, g->id))
363 fprintf(lout, "%s%s", l, g->next ? ":" : "");
369 /* This is commented out because it takes on the order of 30 minutes to
370 * run. Instead, we never free the memory, but the program will exit
376 for (p = &groups[maxid-1]; p >= groups; p--)
383 if (fclose(iout) || fclose(gout) || fclose(lout)) {
384 fprintf(stderr, "Unsuccessful close of gid.db, group.db, or grplist.db\n");
397 char outf[64], outft[64], *mach;
400 ## char name[33], type[9], loc[33], access[2], mount[33], trans[257];
401 ## char *filetime, comments[65];
402 ## int flag1, flag2, flag3, id;
404 sprintf(outf, "%s/filsys.db", HESIOD_DIR);
406 if (stat(outf, &sb) == 0) {
408 filetime = ingres_date_and_time(ftime);
409 ## retrieve (flag1 = int4(interval("min", tblstats.modtime - filetime)))
410 ## where tblstats.table = "filesys"
411 ## retrieve (flag2 = int4(interval("min", tblstats.modtime - filetime)))
412 ## where tblstats.table = "machine"
413 ## retrieve (flag3 = int4(interval("min", tblstats.modtime - filetime)))
414 ## where tblstats.table = "alias"
415 if (flag1 < 0 && flag2 < 0 && flag3 < 0) {
416 fprintf(stderr, "File filsys.db does not need to be rebuilt.\n");
421 sprintf(outft, "%s~", outf);
422 out = fopen(outft, "w");
424 perror("cannot open filsys.db for write");
428 fprintf(stderr, "Building filsys.db\n");
431 ## range of f is filesys
432 ## retrieve (name = f.label, type = f.#type, loc = f.#name, id = f.mach_id,
433 ## access = f.#access, mount = f.#mount, comments = f.#comments) {
436 if (strcmp(type, "ERR")) {
440 if (mach = hash_lookup(machines, id))
441 fprintf(out, "%s.filsys\tHS UNSPECA \"%s %s %s %s %s\"\n",
442 name, type, loc, mach, access, mount);
445 fprintf(out, "%s.filsys\tHS UNSPECA \"ERR %s\"\n",
452 ## range of a is alias
453 ## retrieve (name = a.#name, trans = a.#trans) where a.#type = "FILESYS" {
456 fprintf(out, "%s.filsys\tHS CNAME %s.filsys\n", name, trans);
460 fprintf(stderr, "Unsuccessful close of filsys.db\n");
469 * Modified from sys/types.h:
471 int setsize; /* = howmany(setbits, NSETBITS) */
473 typedef long set_mask;
474 #define NSETBITS (sizeof(set_mask) * NBBY) /* bits per mask */
476 #define howmany(x, y) (((x)+((y)-1))/(y))
479 #define SET_SET(n, p) ((p)[(n)/NSETBITS] |= (1 << ((n) % NSETBITS)))
480 #define SET_CLR(n, p) ((p)[(n)/NSETBITS] &= ~(1 << ((n) % NSETBITS)))
481 #define SET_ISSET(n, p) ((p)[(n)/NSETBITS] & (1 << ((n) % NSETBITS)))
482 #define SET_CREATE() ((set_mask *)malloc(setsize * sizeof(set_mask)))
483 #define SET_ZERO(p) bzero((char *)(p), setsize * sizeof(set_mask))
484 #define SET_CMP(p1, p2) (bcmp((p1), (p2), setsize * sizeof(set_mask)))
490 char outf[64], outft[64], *mach, machbuf[33], *p;
493 ## int flag1, flag2, flag3, flag4, maxmach, maxclu, mid, cid, id;
494 ## char name[33], label[17], data[33], *filetime;
495 set_mask **machs, *ms, *ps;
497 sprintf(outf, "%s/cluster.db", HESIOD_DIR);
499 if (stat(outf, &sb) == 0) {
501 filetime = ingres_date_and_time(ftime);
502 ## retrieve (flag1 = int4(interval("min", tblstats.modtime - filetime)))
503 ## where tblstats.table = "cluster"
504 ## retrieve (flag2 = int4(interval("min", tblstats.modtime - filetime)))
505 ## where tblstats.table = "machine"
506 ## retrieve (flag3 = int4(interval("min", tblstats.modtime - filetime)))
507 ## where tblstats.table = "mcmap"
508 ## retrieve (flag4 = int4(interval("min", tblstats.modtime - filetime)))
509 ## where tblstats.table = "svc"
510 if (flag1 < 0 && flag2 < 0 && flag3 < 0 && flag4 < 0) {
511 fprintf(stderr, "File cluster.db does not need to be rebuilt.\n");
516 sprintf(outft, "%s~", outf);
517 out = fopen(outft, "w");
519 perror("cannot open cluster.db for write");
523 fprintf(stderr, "Building cluster.db\n");
526 ## range of c is cluster
528 ## retrieve (maxclu = max(c.clu_id))
529 setsize = howmany(maxclu, NSETBITS);
530 ## range of m is machine
531 ## retrieve (maxmach = max(m.mach_id))
532 machs = (set_mask **)malloc((maxmach + 1) * sizeof(set_mask **));
533 bzero(machs, (maxmach + 1) * sizeof(int));
535 ## range of p is mcmap
536 ## retrieve (mid = p.mach_id, cid = p.clu_id) {
537 if (!(ms = machs[mid])) {
538 ms = machs[mid] = SET_CREATE();
545 for (mid = 1; mid < maxmach; mid++) {
549 for (cid = 1; cid < maxclu; cid++) {
550 if (SET_ISSET(cid, ms)) {
551 ## repeat retrieve (label = d.serv_label, data = d.serv_cluster)
552 ## where d.clu_id = @cid {
556 "smsinternal-%d.cluster\tHS UNSPECA \"%s %s\"\n",
562 if (mach = hash_lookup(machines, mid)) {
563 for (p = machbuf; *mach && *mach != '.'; mach++)
566 fprintf(out, "%s.cluster\tHS CNAME smsinternal-%d.cluster\n",
569 for (id = mid + 1; id < maxmach; id++) {
570 if ((ps = machs[id]) && !SET_CMP(ms, ps)) {
573 if (mach = hash_lookup(machines, id)) {
574 for (p = machbuf; *mach && *mach != '.'; mach++)
577 fprintf(out,"%s.cluster\tHS CNAME smsinternal-%d.cluster\n",
586 ## retrieve (name = c.#name, label = d.serv_label, data = d.serv_cluster)
587 ## where c.clu_id = d.clu_id {
591 fprintf(out, "%s.cluster\tHS UNSPECA \"%s %s\"\n",
598 fprintf(stderr, "Unsuccessful close of cluster.db\n");
609 char outf[64], outft[64];
612 ## char name[17], rp[17], sd[33], rm[33], *filetime;
615 sprintf(outf, "%s/printcap.db", HESIOD_DIR);
617 if (stat(outf, &sb) == 0) {
619 filetime = ingres_date_and_time(ftime);
620 ## retrieve (flag = int4(interval("min", tblstats.modtime - filetime)))
621 ## where tblstats.table = "printcap"
623 fprintf(stderr, "File printcap.db does not need to be rebuilt.\n");
628 sprintf(outft, "%s~", outf);
629 out = fopen(outft, "w");
631 perror("cannot open printcap.db for write");
635 fprintf(stderr, "Building printcap.db\n");
637 ## range of p is printcap
638 ## retrieve (name = p.#name, rp = p.#rp, sd = p.dir, rm = machine.#name)
639 ## where machine.mach_id = p.mach_id {
644 fprintf(out, "%s.pcap\tHS UNSPECA \"%s:rp=%s:rm=%s:sd=%s\"\n",
645 name, name, rp, rm, sd);
649 fprintf(stderr, "Unsuccessful close of pcap.db\n");
659 FILE *out, *old, *new;
660 char outf[64], outft[64], *mach;
664 ## char service[17], *filetime;
665 ## int port, flag1, flag2, id;
667 sprintf(outf, "%s/sloc.db", HESIOD_DIR);
669 if (stat(outf, &sb) == 0) {
671 filetime = ingres_date_and_time(ftime);
672 ## retrieve (flag1 = int4(interval("min", tblstats.modtime - filetime)))
673 ## where tblstats.table = "serverhosts"
674 ## retrieve (flag2 = int4(interval("min", tblstats.modtime - filetime)))
675 ## where tblstats.table = "machine"
676 if (flag1 < 0 && flag2 < 0) {
677 fprintf(stderr, "File sloc.db does not need to be rebuilt.\n");
682 sprintf(outft, "%s~", outf);
683 out = fopen(outft, "w");
685 perror("cannot open sloc.db for write");
689 fprintf(stderr, "Building sloc.db\n");
692 ## range of s is serverhosts
693 ## retrieve (service = s.#service, id = s.mach_id)
694 ## sort by #service {
696 if (mach = hash_lookup(machines, id))
697 fprintf(out, "%s.sloc\tHS UNSPECA %s\n", service, mach);
701 fprintf(stderr, "Unsuccessful close of sloc.db\n");
712 char outf[64], outft[64];
715 ## char mach[33], service[33], protocol[9], altserv[129], *filetime;
716 ## int port, flag1, flag2;
718 sprintf(outf, "%s/service.db", HESIOD_DIR);
720 if (stat(outf, &sb) == 0) {
722 filetime = ingres_date_and_time(ftime);
723 ## retrieve (flag1 = int4(interval("min", tblstats.modtime - filetime)))
724 ## where tblstats.table = "services"
726 fprintf(stderr, "File service.db does not need to be rebuilt.\n");
731 sprintf(outft, "%s~", outf);
732 out = fopen(outft, "w");
734 perror("cannot open service.db for write");
738 fprintf(stderr, "Building service.db\n");
740 ## range of s is services
741 ## retrieve (service = s.name, protocol = lowercase(s.#protocol),
745 fprintf(out, "%s.service\tHS UNSPECA \"%s %s %d\"\n",
746 service, service, protocol, port);
749 ## range of a is alias
750 ## retrieve (service = a.name, altserv = a.trans) where a.type = "SERVICE" {
753 fprintf(out, "%s.service\tHS CNAME %s.service\n", service, altserv);
757 fprintf(stderr, "Unsuccessful close of service.db\n");