]> andersk Git - moira.git/blob - gen/hesiod.dc
Case-insensitive stuff.
[moira.git] / gen / hesiod.dc
1 /* $Header$
2  *
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.
6  *
7  *  (c) Copyright 1988, 1990 by the Massachusetts Institute of Technology.
8  *  For copying and distribution information, please see the file
9  *  <mit-copyright.h>.
10  */
11
12 #include <mit-copyright.h>
13 #include <stdio.h>
14 #include <moira.h>
15 #include <moira_site.h>
16 #include <sys/types.h>
17 #include <sys/stat.h>
18 #include <sys/time.h>
19 #include <ctype.h>
20 EXEC SQL INCLUDE sqlca;
21
22
23 #ifdef ATHENA
24 #define HTYPE "UNSPECA"
25 #else
26 #define HTYPE "TXT"
27 #endif
28
29 char hesiod_dir[64];
30
31 #define min(x,y)        ((x) < (y) ? (x) : (y))
32 struct hash *machines = NULL;
33 struct hash *users = NULL;
34 char *whoami = "hesiod.gen";
35
36 struct grp {
37     struct grp *next;
38     char *lid;
39 };
40 struct user {
41     char name[9];
42     struct grp *lists;
43 };
44
45 void lowercase();
46 char *malloc(), *strsave();
47
48
49 main(argc, argv)
50 int argc;
51 char **argv;
52 {
53     char cmd[64];
54     struct stat sb;
55     int changed = 0;
56
57     if (argc > 2) {
58         fprintf(stderr, "usage: %s [outfile]\n", argv[0]);
59         exit(MR_ARGS);
60     }
61
62     initialize_sms_error_table ();
63     sprintf(hesiod_dir, "%s/hesiod", DCM_DIR);
64
65 #ifsql INGRES
66     EXEC SQL CONNECT sms;
67 #endsql
68 #ifsql INFORMIX
69     EXEC SQL DATABASE sms;
70 #endsql
71
72     changed = do_passwd();
73     changed += do_filsys();
74     changed += do_cluster();
75     changed += do_printcap();
76     changed += do_palladium();
77     changed += do_sloc();
78     changed += do_service();
79     changed += do_groups();
80
81 #ifsql INGRES
82     EXEC SQL DISCONNECT;
83 #endsql
84 #ifsql INFORMIX
85     EXEC SQL CLOSE DATABASE;
86 #endsql
87
88     if (!changed) {
89         fprintf(stderr, "No files updated.\n");
90         if (argc == 2 && stat(argv[1], &sb) == 0)
91           exit(MR_NO_CHANGE);
92     }
93
94     if (argc == 2) {
95         fprintf(stderr, "Building tar file.\n");
96         sprintf(cmd, "cd %s; tar cf %s .", hesiod_dir, argv[1]);
97         if (system(cmd))
98           exit(MR_TAR_FAIL);
99     }
100
101     exit(MR_SUCCESS);
102 }
103
104
105 get_mach()
106 {
107     EXEC SQL BEGIN DECLARE SECTION;
108     int id;
109     char name[33];
110     EXEC SQL END DECLARE SECTION;
111
112     if (machines)
113       return;
114
115     machines = create_hash(1000);
116     EXEC SQL DECLARE m_cursor CURSOR FOR
117       SELECT NAME, MACH_ID
118         FROM MACHINE;
119     EXEC SQL OPEN m_cursor;
120     while (1) {
121         EXEC SQL FETCH m_cursor INTO :name, :id;
122         if (sqlca.sqlcode != 0) break;
123         hash_store(machines, id, strsave(strtrim(name)));
124     }
125     if (sqlca.sqlcode < 0) {
126         com_err(whoami, MR_INGRES_ERR, " code %d\n", sqlca.sqlcode);
127         critical_alert("DCM", "Hesiod build encountered DATABASE ERROR %d",
128                        sqlca.sqlcode);
129         exit(MR_INGRES_ERR);
130     }
131     EXEC SQL CLOSE m_cursor;
132 #ifsql INGRES
133 #endsql
134 #ifsql INFORMIX
135     EXEC SQL COMMIT WORK;
136 #endsql
137 }
138
139
140 do_passwd()
141 {
142     FILE *pout, *uout, *bout;
143     char poutf[64], uoutf[64], poutft[64], uoutft[64], boutf[64], boutft[64];
144     struct stat psb, usb, bsb;
145     time_t ftime;
146     struct user *u;
147     char *mach;
148     EXEC SQL BEGIN DECLARE SECTION;
149     char login[9], shell[33], fullname[33], oa[17], op[13], hp[17];
150     char nn[17], ptype[9];
151     int uid, flag1, flag2, id, pid, status;
152     EXEC SQL END DECLARE SECTION;
153
154     sprintf(poutf, "%s/passwd.db", hesiod_dir);
155     sprintf(uoutf, "%s/uid.db", hesiod_dir);
156     sprintf(boutf, "%s/pobox.db", hesiod_dir);
157
158     if (stat(poutf, &psb) == 0 && stat(uoutf, &usb) == 0 &&
159         stat(boutf, &bsb) == 0) {
160         ftime = min(min(psb.st_mtime, usb.st_mtime), bsb.st_mtime);
161         if (ModDiff (&flag1, "users", ftime) ||
162             ModDiff (&flag2, "machine", ftime))
163             exit (MR_DATE);
164         if (flag1 < 0 && flag2 < 0) {
165             fprintf(stderr, "Files passwd.db, uid.db, and pobox.db do not need to be rebuilt.\n");
166             return(0);
167       }
168     }
169
170     sprintf(poutft, "%s~", poutf);
171     pout = fopen(poutft, "w");
172     if (!pout) {
173         perror("cannot open passwd.db~ for write");
174         exit(MR_OCONFIG);
175     }
176     sprintf(uoutft, "%s~", uoutf);
177     uout = fopen(uoutft, "w");
178     if (!uout) {
179         perror("cannot open uid.db~ for write");
180         exit(MR_OCONFIG);
181     }
182     sprintf(boutft, "%s~", boutf);
183     bout = fopen(boutft, "w");
184     if (!bout) {
185         perror("cannot open pobox.db for write");
186         exit(MR_OCONFIG);
187     }
188
189     fprintf(stderr, "Building passwd.db, uid.db, and pobox.db\n");
190     get_mach();
191
192     users = create_hash(12001);
193     EXEC SQL DECLARE u_cursor CURSOR FOR
194       SELECT login, uid, shell, fullname, nickname, office_addr,
195              office_phone, home_phone, users_id, pop_id, potype, status
196       FROM users
197       WHERE status = 1 or status = 6;
198     EXEC SQL OPEN u_cursor;
199     while (1) {
200         EXEC SQL FETCH u_cursor INTO :login, :uid, :shell, :fullname, :nn,
201                        :oa, :op, :hp, :id, :pid, :ptype, :status;
202             if (sqlca.sqlcode != 0) break;
203             strtrim(login);
204             dequote(fullname);
205             dequote(nn);
206             dequote(oa);
207             dequote(op);
208             dequote(hp);
209             dequote(shell);
210             u = (struct user *) malloc(sizeof(struct user));
211             strcpy(u->name, login);
212             u->lists = NULL;
213             hash_store(users, id, u);
214             if (status == 1) {
215                 fprintf(pout, "%s.passwd\tHS %s \"%s:*:%d:101:%s,%s,%s,%s,%s:/mit/%s:%s\"\n",
216                         login, HTYPE, login, uid, fullname, nn, oa, op, hp,
217                         login, shell);
218                 fprintf(uout, "%d.uid\tHS CNAME %s.passwd\n", uid, login);
219             }
220             if (ptype[0] == 'P' && (mach = hash_lookup(machines, pid))) {
221                 fprintf(bout, "%s.pobox\tHS %s \"POP %s %s\"\n",
222                         login, HTYPE, mach, login);
223             }
224     }
225     if (sqlca.sqlcode < 0) {
226         com_err(whoami, MR_INGRES_ERR, " code %d\n", sqlca.sqlcode);
227         critical_alert("DCM", "Hesiod build encountered DATABASE ERROR %d",
228                        sqlca.sqlcode);
229         exit(MR_INGRES_ERR);
230     }
231     EXEC SQL CLOSE u_cursor;
232 #ifsql INGRES
233 #endsql
234 #ifsql INFORMIX
235     EXEC SQL COMMIT WORK;
236 #endsql
237
238     if (fclose(pout) || fclose(uout) || fclose(bout)) {
239         fprintf(stderr, "Unsuccessful file close of passwd.db, uid.db, or pobox.db\n");
240         exit(MR_CCONFIG);
241     }
242     fix_file(poutf);
243     fix_file(uoutf);
244     fix_file(boutf);
245     return(1);
246 }
247
248
249 do_groups()
250 {
251     FILE *iout, *gout, *lout;
252     char ioutf[64], goutf[64], loutf[64], buf[2048], *l;
253     struct hash *groups;
254     register struct bucket *b, **p;
255     struct grp *g;
256     struct user *u;
257     struct stat isb, gsb, lsb;
258     time_t ftime;
259     EXEC SQL BEGIN DECLARE SECTION;
260     char name[33];
261     int gid, id, lid, flag1, flag2, flag3;
262     EXEC SQL END DECLARE SECTION;
263
264     /* open files */
265     sprintf(ioutf, "%s/gid.db", hesiod_dir);
266     sprintf(goutf, "%s/group.db", hesiod_dir);
267     sprintf(loutf, "%s/grplist.db", hesiod_dir);
268
269     if (stat(ioutf, &isb) == 0 && stat(goutf, &gsb) == 0 && stat(loutf, &lsb) == 0) {
270         ftime = min(isb.st_mtime, min(gsb.st_mtime, lsb.st_mtime));
271         if (ModDiff (&flag1, "users", ftime) ||
272             ModDiff (&flag2, "list", ftime) ||
273             ModDiff (&flag3, "imembers", ftime))
274             exit (MR_DATE);
275         if (flag1 < 0 && flag2 < 0 && flag3 < 0) {
276             fprintf(stderr, "Files gid.db, group.db and grplist.db do not need to be rebuilt.\n");
277             return(0);
278       }
279     }
280
281     sprintf(buf, "%s~", ioutf);
282     iout = fopen(buf, "w");
283     if (!iout) {
284         perror("cannot open gid.db for write");
285         exit(MR_OCONFIG);
286     }
287     sprintf(buf, "%s~", goutf);
288     gout = fopen(buf, "w");
289     if (!gout) {
290         perror("cannot open group.db for write");
291         exit(MR_OCONFIG);
292     }
293     sprintf(buf, "%s~", loutf);
294     lout = fopen(buf, "w");
295     if (!lout) {
296         perror("cannot open grplist.db for write");
297         exit(MR_OCONFIG);
298     }
299
300     fprintf(stderr, "Building gid.db, group.db, and grplist.db\n");
301
302     /* make space for group list */
303     groups = create_hash(15001);
304
305     EXEC SQL WHENEVER SQLERROR GOTO sqlerr;
306
307     /* get lock records */
308     EXEC SQL SELECT modtime INTO :name FROM list WHERE list_id = 0;
309     EXEC SQL SELECT modtime INTO :name FROM users WHERE users_id = 0;
310
311     EXEC SQL DECLARE l_cursor CURSOR FOR
312       SELECT name, gid, list_id
313         FROM list
314           WHERE grp != 0 AND active != 0;
315     EXEC SQL OPEN l_cursor;
316     while (1) {
317         EXEC SQL FETCH l_cursor INTO :name, :gid, :lid;
318         if (sqlca.sqlcode != 0) break;
319         strtrim(name);
320         sprintf(buf, "%s:%d", name, gid);
321         hash_store(groups, lid, strsave(buf));
322         fprintf(iout, "%d.gid\tHS CNAME %s.group\n", gid, name);
323         fprintf(gout, "%s.group\tHS %s \"%s:*:%d:\"\n",
324                 name, HTYPE, name, gid);
325     }
326     EXEC SQL CLOSE l_cursor;
327
328     fflush(iout);
329     fflush(gout);
330
331     /* now do grplists */
332     if (users == NULL) {
333         users = create_hash(12001);
334         EXEC SQL DECLARE u_cursor2 CURSOR FOR
335           SELECT users_id, login
336             FROM users
337               WHERE status = 1;
338         EXEC SQL OPEN u_cursor2;
339         while (1) {
340             EXEC SQL FETCH u_cursor2 INTO :id, :name;
341             if (sqlca.sqlcode != 0) break;
342             u = (struct user *) malloc(sizeof(struct user));
343             strcpy(u->name, strtrim(name));
344             u->lists = NULL;
345             hash_store(users, id, u);
346         }
347         EXEC SQL CLOSE u_cursor2;
348     }
349
350     EXEC SQL DECLARE i_cursor CURSOR FOR
351       SELECT list_id, member_id
352         FROM imembers
353           WHERE member_type = "USER";
354     EXEC SQL OPEN i_cursor;
355     while (1) {
356         EXEC SQL FETCH i_cursor INTO :lid, :id;
357         if (sqlca.sqlcode != 0) break;
358         if (((l = hash_lookup(groups, lid)) != NULL) &&
359             (u = (struct user *) hash_lookup(users, id))) {
360             g = (struct grp *) malloc(sizeof(struct grp));
361             g->next = u->lists;
362             u->lists = g;
363             g->lid = l;
364         }
365     }
366     EXEC SQL CLOSE i_cursor;
367 #ifsql INGRES
368 #endsql
369 #ifsql INFORMIX
370     EXEC SQL COMMIT WORK;
371 #endsql
372
373     for (p = &(users->data[users->size - 1]); p >= users->data; p--) {
374         for (b = *p; b; b = b->next) {
375             if ((g = ((struct user *)b->data)->lists) == NULL)
376               continue;
377             fprintf(lout, "%s.grplist\tHS %s \"",
378                     ((struct user *)b->data)->name, HTYPE);
379             for (; g; g = g->next) {
380                 fputs(g->lid, lout);
381                 if (g->next)
382                   putc(':', lout);
383             }
384             fputs("\"\n", lout);
385         }
386     }
387
388     if (fclose(iout) || fclose(gout) || fclose(lout)) {
389         fprintf(stderr, "Unsuccessful close of gid.db, group.db, or grplist.db\n");
390         exit(MR_CCONFIG);
391     }
392     fix_file(ioutf);
393     fix_file(goutf);
394     fix_file(loutf);
395     return(1);
396  sqlerr:
397     com_err(whoami, MR_INGRES_ERR, " code %d\n", sqlca.sqlcode);
398     critical_alert("DCM", "Hesiod build encountered DATABASE ERROR %d",
399                    sqlca.sqlcode);
400     exit(MR_INGRES_ERR);
401 }
402
403
404 do_filsys()
405 {
406     FILE *out;
407     char outf[64], outft[64], *mach, *group;
408     register char *p;
409     struct stat sb;
410     time_t ftime;
411     struct save_queue *sq, *sq_create();
412     EXEC SQL BEGIN DECLARE SECTION;
413     char name[33], type[9], loc[81], access[2], mount[33], trans[257];
414     char comments[65];
415     int flag1, flag2, flag3, flag4, id, fid;
416     EXEC SQL END DECLARE SECTION;
417     char *index();
418
419     sprintf(outf, "%s/filsys.db", hesiod_dir);
420
421     if (stat(outf, &sb) == 0) {
422         ftime = sb.st_mtime;
423
424         if (ModDiff(&flag1, "filesys", ftime)) exit (MR_DATE);
425         if (ModDiff(&flag2, "machine", ftime)) exit (MR_DATE);
426         if (ModDiff(&flag3, "alias", ftime)) exit (MR_DATE);
427         if (ModDiff(&flag4, "fsgroup", ftime)) exit (MR_DATE);
428
429         if (flag1 < 0 && flag2 < 0 && flag3 < 0 && flag4 < 0) {
430             fprintf(stderr, "File filsys.db does not need to be rebuilt.\n");
431             return(0);
432       }
433     }
434
435     sprintf(outft, "%s~", outf);
436     out = fopen(outft, "w");
437     if (!out) {
438         perror("cannot open filsys.db for write");
439         exit(MR_OCONFIG);
440     }
441
442     fprintf(stderr, "Building filsys.db\n");
443     get_mach();
444     sq = sq_create();
445
446     EXEC SQL DECLARE f_cursor CURSOR FOR
447       SELECT label, type, name, mach_id, access, mount, comments, filsys_id
448         FROM filesys;
449     EXEC SQL OPEN f_cursor;
450     while (1) {
451         EXEC SQL FETCH f_cursor INTO :name, :type, :loc, :id, :access, 
452                                    :mount, :comments, :fid;
453         if (sqlca.sqlcode != 0) break;
454         strtrim(type);
455         if (!strcmp(type, "NFS") || !strcmp(type, "RVD")) {
456             if (mach = hash_lookup(machines, id)) {
457                 fprintf(out, "%s.filsys\tHS %s \"%s %s %s %s %s\"\n",
458                         strtrim(name), HTYPE, type, strtrim(loc), mach,
459                         strtrim(access), strtrim(mount));
460             }
461         } else if (!strcmp(type, "AFS")) {
462             fprintf(out, "%s.filsys\tHS %s \"AFS %s %s %s\"\n",
463                     strtrim(name), HTYPE, strtrim(loc), strtrim(access),
464                     strtrim(mount));
465         } else if (!strcmp(type, "ERR")) {
466             fprintf(out, "%s.filsys\tHS %s \"ERR %s\"\n",
467                     strtrim(name), HTYPE, strtrim(comments));
468         } else if (!strcmp(type, "FSGROUP")) {
469             sprintf(trans, "%s:%d", strtrim(name), fid);
470             sq_save_data(sq, strsave(trans));
471         }
472     }
473     EXEC SQL CLOSE f_cursor;
474
475     while (sq_get_data(sq, &group)) {
476         fid = atoi(index(group, ':')+1);
477         *index(group, ':') = 0;
478
479         EXEC SQL DECLARE f_cursor2 CURSOR FOR
480           SELECT DISTINCT f.type, f.name, f.mach_id, f.access, f.mount,
481                 f.comments, f.label, g.key
482           FROM filesys f, fsgroup g
483             WHERE f.filsys_id = g.filsys_id AND g.group_id = :fid
484             ORDER BY key, label;
485         EXEC SQL OPEN f_cursor2;
486         while (1) {
487             EXEC SQL FETCH f_cursor2 INTO :type, :loc, :id, :access,:mount,
488                                     :comments, :name, :trans;
489             if (sqlca.sqlcode != 0) break;
490             strtrim(type);
491             if (!strcmp(type, "NFS") || !strcmp(type, "RVD")) {
492                 if (mach = hash_lookup(machines, id)) {
493                     fprintf(out, "%s.filsys\tHS %s \"%s %s %s %s %s\"\n",
494                             group, HTYPE, type, strtrim(loc), mach,
495                             strtrim(access), strtrim(mount));
496                 }
497             } else if (!strcmp(type, "AFS")) {
498                 fprintf(out, "%s.filsys\tHS %s \"AFS %s %s %s\"\n",
499                         group, HTYPE, strtrim(loc), strtrim(access),
500                         strtrim(mount));
501             } else if (!strcmp(type, "ERR")) {
502                 fprintf(out, "%s.filsys\tHS %s \"ERR %s\"\n",
503                         group, HTYPE, strtrim(comments));
504             }
505         }
506         EXEC SQL CLOSE f_cursor2;
507         free(group);
508     }
509     sq_destroy(sq);
510
511     EXEC SQL DECLARE a_cursor CURSOR FOR
512       SELECT name, trans
513         FROM alias
514           WHERE type = "FILESYS";
515     EXEC SQL OPEN a_cursor;
516     while (1) {
517         EXEC SQL FETCH a_cursor INTO :name, :trans;
518         if (sqlca.sqlcode != 0) break;
519         fprintf(out, "%s.filsys\tHS CNAME %s.filsys\n",
520                 strtrim(name), strtrim(trans));
521     }
522     EXEC SQL CLOSE a_cursor;
523 #ifsql INGRES
524 #endsql
525 #ifsql INFORMIX
526     EXEC SQL COMMIT WORK;
527 #endsql
528
529     if (fclose(out)) {
530         fprintf(stderr, "Unsuccessful close of filsys.db\n");
531         exit(MR_CCONFIG);
532     }
533     fix_file(outf);
534     return(1);
535  sqlerr:
536     com_err(whoami, MR_INGRES_ERR, " code %d\n", sqlca.sqlcode);
537     critical_alert("DCM", "Hesiod build encountered DATABASE ERROR %d",
538                    sqlca.sqlcode);
539     exit(MR_INGRES_ERR);
540 }
541
542
543 /*
544  * Modified from sys/types.h:
545  */
546 int setsize;    /* = howmany(setbits, NSETBITS) */
547
548 typedef long    set_mask;
549 #define NSETBITS        (sizeof(set_mask) * NBBY)       /* bits per mask */
550 #ifndef howmany
551 #define howmany(x, y)   (((x)+((y)-1))/(y))
552 #endif
553
554 #define SET_SET(n, p)   ((p)[(n)/NSETBITS] |=  (1 << ((n) % NSETBITS)))
555 #define SET_CLR(n, p)   ((p)[(n)/NSETBITS] &= ~(1 << ((n) % NSETBITS)))
556 #define SET_ISSET(n, p) ((p)[(n)/NSETBITS] &   (1 << ((n) % NSETBITS)))
557 #define SET_CREATE()    ((set_mask *)malloc(setsize * sizeof(set_mask)))
558 #define SET_ZERO(p)     bzero((char *)(p), setsize * sizeof(set_mask))
559 #define SET_CMP(p1, p2) (bcmp((p1), (p2), setsize * sizeof(set_mask)))
560
561 int nbitsset(set)
562 set_mask *set;
563 {
564     int i, ret;
565     ret = 0;
566     for (i = 0; i < setsize * NSETBITS; i++)
567       if (SET_ISSET(i, set))
568         ret++;
569     return(ret);
570 }
571
572
573 do_cluster()
574 {
575     FILE *out;
576     char outf[64], outft[64], *mach, machbuf[33], *p;
577     struct stat sb;
578     time_t ftime;
579     EXEC SQL BEGIN DECLARE SECTION;
580     int flag1, flag2, flag3, flag4, maxmach, maxclu, mid, cid, id;
581     char name[33], label2[17], data[33];
582     EXEC SQL END DECLARE SECTION;
583     set_mask **machs, *ms, *ps;
584     int oneclu;
585
586     sprintf(outf, "%s/cluster.db", hesiod_dir);
587
588     if (stat(outf, &sb) == 0) {
589         ftime = sb.st_mtime;
590         if (ModDiff (&flag1, "cluster", ftime)
591             || ModDiff (&flag2, "machine", ftime)
592             || ModDiff (&flag3, "mcmap", ftime)
593             || ModDiff (&flag4, "svc", ftime)) exit (MR_DATE);
594         if (flag1 < 0 && flag2 < 0 && flag3 < 0 && flag4 < 0) {
595             fprintf(stderr, "File cluster.db does not need to be rebuilt.\n");
596             return(0);
597       }
598     }
599
600     sprintf(outft, "%s~", outf);
601     out = fopen(outft, "w");
602     if (!out) {
603         perror("cannot open cluster.db for write");
604         exit(MR_OCONFIG);
605     }
606
607     fprintf(stderr, "Building cluster.db\n");
608     get_mach();
609
610     EXEC SQL SELECT MAX(clu_id) INTO :maxclu FROM cluster;
611     maxclu++;
612     setsize = howmany(maxclu, NSETBITS);
613
614     EXEC SQL SELECT MAX(mach_id) INTO :maxmach FROM machine;
615     maxmach++;
616     machs = (set_mask **)malloc((maxmach + 1) * sizeof(set_mask **));
617     bzero(machs, (maxmach + 1) * sizeof(int));
618
619     EXEC SQL DECLARE p_cursor CURSOR FOR
620       SELECT mach_id, clu_id
621         FROM mcmap;
622     EXEC SQL OPEN p_cursor;
623     while (1) {
624         EXEC SQL FETCH p_cursor INTO :mid, :cid;
625         if (sqlca.sqlcode != 0) break;
626         if (!(ms = machs[mid])) {
627             ms = machs[mid] = SET_CREATE();
628             SET_ZERO(ms);
629         }
630         SET_SET(cid, ms);
631     }
632     EXEC SQL CLOSE p_cursor;
633
634     for (mid = 1; mid < maxmach; mid++) {
635         if (!machs[mid])
636           continue;
637         ms = machs[mid];
638         if (nbitsset(ms) > 1) {
639             oneclu = 0;
640             for (cid = 1; cid < maxclu; cid++) {
641                 if (SET_ISSET(cid, ms)) {
642                     EXEC SQL DECLARE d_cursor CURSOR FOR
643                       SELECT serv_label, serv_cluster
644                       FROM svc
645                       WHERE clu_id = :cid;
646                     EXEC SQL OPEN d_cursor;
647                     while (1) {
648                         EXEC SQL FETCH d_cursor INTO :label2, :data;
649                         if (sqlca.sqlcode != 0) break;
650                         strtrim(label2);
651                         strtrim(data);
652                         fprintf(out,
653                                 "mrinternal-%d.cluster\tHS %s \"%s %s\"\n",
654                                 mid, HTYPE, label2, data);
655                     }
656                     EXEC SQL CLOSE d_cursor;
657                 }
658             }
659         } else {
660             oneclu = 1;
661             for (cid = 1; cid < maxclu; cid++)
662               if (SET_ISSET(cid, ms)) break;
663
664             EXEC SQL SELECT name INTO :name FROM cluster WHERE clu_id = :cid;
665             strtrim(name);
666         }
667
668         if (mach = hash_lookup(machines, mid)) {
669             for (p = machbuf; *mach && *mach != '.'; mach++)
670               *p++ = *mach;
671             *p = 0;
672             if (oneclu)
673               fprintf(out, "%s.cluster\tHS CNAME %s.cluster\n",
674                       machbuf, name);
675             else
676               fprintf(out, "%s.cluster\tHS CNAME mrinternal-%d.cluster\n",
677                       machbuf, mid);
678         }
679         for (id = mid + 1; id < maxmach; id++) {
680             if ((ps = machs[id]) && !SET_CMP(ms, ps)) {
681                 free(ps);
682                 machs[id] = NULL;
683                 if (mach = hash_lookup(machines, id)) {
684                     for (p = machbuf; *mach && *mach != '.'; mach++)
685                       *p++ = *mach;
686                     *p = 0;
687                     if (oneclu)
688                       fprintf(out, "%s.cluster\tHS CNAME %s.cluster\n",
689                               machbuf, name);
690                     else
691                       fprintf(out,
692                               "%s.cluster\tHS CNAME mrinternal-%d.cluster\n",
693                               machbuf, mid);
694                 }
695             }
696         }
697         free(ms);
698         machs[mid] = NULL;
699     }
700
701     EXEC SQL DECLARE d_cursor2 CURSOR FOR
702       SELECT c.name, d.serv_label, d.serv_cluster
703       FROM svc d, cluster c
704       WHERE c.clu_id = d.clu_id; 
705     EXEC SQL OPEN d_cursor2; 
706         while (1) { 
707             EXEC SQL FETCH d_cursor2 INTO :name, :label2, :data; 
708             if (sqlca.sqlcode != 0) break;
709             strtrim(name);
710             strtrim(label2);
711             strtrim(data);
712             fprintf(out, "%s.cluster\tHS %s \"%s %s\"\n",
713                     name, HTYPE, label2, data);
714     }
715     free(machs);
716 #ifsql INGRES
717 #endsql
718 #ifsql INFORMIX
719     EXEC SQL COMMIT WORK;
720 #endsql
721
722     if (fclose(out)) {
723         fprintf(stderr, "Unsuccessful close of cluster.db\n");
724         exit(MR_CCONFIG);
725     }
726     fix_file(outf);
727     return(1);
728  sqlerr:
729     com_err(whoami, MR_INGRES_ERR, " code %d\n", sqlca.sqlcode);
730     critical_alert("DCM", "Hesiod build encountered DATABASE ERROR %d",
731                    sqlca.sqlcode);
732     exit(MR_INGRES_ERR);
733 }
734
735
736 do_printcap()
737 {
738     FILE *out;
739     char outf[64], outft[64];
740     struct stat sb;
741     time_t ftime;
742     EXEC SQL BEGIN DECLARE SECTION;
743     char name[17], rp[17], sd[33];
744     int flag, ka, pc, rm, rq;
745     EXEC SQL END DECLARE SECTION;
746
747     sprintf(outf, "%s/printcap.db", hesiod_dir);
748
749     if (stat(outf, &sb) == 0) {
750         ftime = sb.st_mtime;
751         if (ModDiff (&flag, "printcap", ftime)) exit (MR_DATE);
752         if (flag < 0) {
753             fprintf(stderr, "File printcap.db does not need to be rebuilt.\n");
754             return(0);
755       }
756     }
757
758     sprintf(outft, "%s~", outf);
759     out = fopen(outft, "w");
760     if (!out) {
761         perror("cannot open printcap.db for write");
762         exit(MR_OCONFIG);
763     }
764
765     fprintf(stderr, "Building printcap.db\n");
766     get_mach();
767
768     EXEC SQL DECLARE p_cursor2 CURSOR FOR
769       SELECT name, rp, dir, mach_id, auth, price, quotaserver
770       FROM printcap;
771     EXEC SQL OPEN p_cursor2; 
772     while (1) { 
773         EXEC SQL FETCH p_cursor2 INTO :name, :rp, :sd, :rm, :ka, :pc, :rq; 
774         if (sqlca.sqlcode != 0) break;
775         strtrim(name);
776         strtrim(rp);
777         strtrim(sd);
778         fprintf(out, "%s.pcap\tHS %s \"%s:rp=%s:rm=%s:sd=%s:ka#%d:pc#%d",
779                 name, HTYPE, name, rp, hash_lookup(machines, rm), sd, ka, pc);
780         if (rq)
781           fprintf(out, ":rq=%s\"\n", hash_lookup(machines, rq));
782         else
783           fputs("\"\n", out);
784     }
785     EXEC SQL CLOSE p_cursor2;
786 #ifsql INGRES
787 #endsql
788 #ifsql INFORMIX
789     EXEC SQL COMMIT WORK;
790 #endsql
791
792     if (fclose(out)) {
793         fprintf(stderr, "Unsuccessful close of pcap.db\n");
794         exit(MR_CCONFIG);
795     }
796     fix_file(outf);
797     return(1);
798  sqlerr:
799     com_err(whoami, MR_INGRES_ERR, " code %d\n", sqlca.sqlcode);
800     critical_alert("DCM", "Hesiod build encountered DATABASE ERROR %d",
801                    sqlca.sqlcode);
802     exit(MR_INGRES_ERR);
803   }
804
805
806 do_palladium()
807 {
808     FILE *out;
809     char outf[64], outft[64];
810     struct stat sb;
811     time_t ftime;
812     EXEC SQL BEGIN DECLARE SECTION;
813     char name[33], trans[129];
814     int flag, flag1, ident, rm;
815     EXEC SQL END DECLARE SECTION;
816
817     sprintf(outf, "%s/palladium.db", hesiod_dir);
818
819     if (stat(outf, &sb) == 0) {
820         ftime = sb.st_mtime;
821         if ((ModDiff (&flag, "palladium", ftime)) ||
822             (ModDiff (&flag1, "alias", ftime))) exit (MR_DATE);
823         if (flag < 0 && flag1 < 0) {
824             fprintf(stderr, "File palladium.db does not need to be rebuilt.\n");
825             return(0);
826       }
827     }
828
829     sprintf(outft, "%s~", outf);
830     out = fopen(outft, "w");
831     if (!out) {
832         perror("cannot open palladium.db for write");
833         exit(MR_OCONFIG);
834     }
835
836     fprintf(stderr, "Building palladium.db\n");
837     get_mach();
838
839     EXEC SQL DECLARE p_cursor3 CURSOR FOR
840       SELECT name, ident, mach_id
841       FROM palladium;
842     EXEC SQL OPEN p_cursor3; 
843     while (1) { 
844         EXEC SQL FETCH p_cursor3 INTO :name, :ident, :rm;
845         if (sqlca.sqlcode != 0) break;
846         strtrim(name);
847         fprintf(out,
848                 "%s.palladium\tHS %s \"%s %d %s interface directory\"\n",
849                 name, HTYPE, hash_lookup(machines, rm), ident, name);
850     }
851     EXEC SQL CLOSE p_cursor3;
852
853     EXEC SQL DECLARE a_cursor2 CURSOR FOR
854       SELECT name, trans
855       FROM alias
856       WHERE type = "PALLADIUM";
857     EXEC SQL OPEN a_cursor2; 
858     while (1) { 
859         EXEC SQL FETCH a_cursor2 INTO :name, :trans;
860         if (sqlca.sqlcode != 0) break;
861         strtrim(name);
862         strtrim(trans);
863         fprintf(out, "%s.palladium\tHS %s \"%s\"\n", name, HTYPE, trans);
864     }
865     EXEC SQL CLOSE a_cursor2;
866 #ifsql INGRES
867 #endsql
868 #ifsql INFORMIX
869     EXEC SQL COMMIT WORK;
870 #endsql
871
872     if (fclose(out)) {
873         fprintf(stderr, "Unsuccessful close of palladium.db\n");
874         exit(MR_CCONFIG);
875     }
876     fix_file(outf);
877     return(1);
878  sqlerr:
879     com_err(whoami, MR_INGRES_ERR, " code %d\n", sqlca.sqlcode);
880     critical_alert("DCM", "Hesiod build encountered DATABASE ERROR %d",
881                    sqlca.sqlcode);
882     exit(MR_INGRES_ERR);
883 }
884
885
886 do_sloc()
887 {
888     FILE *out;
889     char outf[64], outft[64], *mach;
890     struct stat sb;
891     time_t ftime;
892     EXEC SQL BEGIN DECLARE SECTION;
893     char service[17];
894     int flag1, flag2, id;
895     EXEC SQL END DECLARE SECTION;
896
897     sprintf(outf, "%s/sloc.db", hesiod_dir);
898
899     if (stat(outf, &sb) == 0) {
900         ftime = sb.st_mtime;
901         if ((ModDiff (&flag1, "serverhosts", ftime)) ||
902             (ModDiff (&flag2, "machine", ftime))) exit (MR_DATE);
903         if (flag1 < 0 && flag2 < 0) {
904             fprintf(stderr, "File sloc.db does not need to be rebuilt.\n");
905             return(0);
906       }
907     }
908
909     sprintf(outft, "%s~", outf);
910     out = fopen(outft, "w");
911     if (!out) {
912         perror("cannot open sloc.db for write");
913         exit(MR_OCONFIG);
914     }
915
916     fprintf(stderr, "Building sloc.db\n");
917     get_mach();
918
919     EXEC SQL DECLARE s_cursor CURSOR FOR
920       SELECT DISTINCT service, mach_id
921       FROM serverhosts
922       ORDER BY service;
923     EXEC SQL OPEN s_cursor; 
924     while (1) { 
925         EXEC SQL FETCH s_cursor INTO :service, :id;
926         if (sqlca.sqlcode != 0) break;
927         strtrim(service);
928         if (mach = hash_lookup(machines, id))
929           fprintf(out, "%s.sloc\tHS %s %s\n", service, HTYPE, mach);
930     }
931     EXEC SQL CLOSE s_cursor;
932 #ifsql INGRES
933 #endsql
934 #ifsql INFORMIX
935     EXEC SQL COMMIT WORK;
936 #endsql
937
938     if (fclose(out)) {
939         fprintf(stderr, "Unsuccessful close of sloc.db\n");
940         exit(MR_CCONFIG);
941     }
942
943     fix_file(outf);
944     return(1);
945  sqlerr:
946     com_err(whoami, MR_INGRES_ERR, " code %d\n", sqlca.sqlcode);
947     critical_alert("DCM", "Hesiod build encountered DATABASE ERROR %d",
948                    sqlca.sqlcode);
949     exit(MR_INGRES_ERR);
950 }
951
952 do_service()
953 {
954     FILE *out;
955     char outf[64], outft[64];
956     struct stat sb;
957     time_t ftime;
958     EXEC SQL BEGIN DECLARE SECTION;
959     char service[33], protocol[9], altserv[129];
960     int port, flag1;
961     EXEC SQL END DECLARE SECTION;
962
963     sprintf(outf, "%s/service.db", hesiod_dir);
964
965     if (stat(outf, &sb) == 0) {
966         ftime = sb.st_mtime;
967         if (ModDiff (&flag1, "services", ftime)) exit (MR_DATE);
968         if (flag1 < 0) {
969             fprintf(stderr, "File service.db does not need to be rebuilt.\n");
970             return(0);
971       }
972     }
973
974     sprintf(outft, "%s~", outf);
975     out = fopen(outft, "w");
976     if (!out) {
977         perror("cannot open service.db for write");
978         exit(MR_OCONFIG);
979     }
980
981     fprintf(stderr, "Building service.db\n");
982
983     EXEC SQL DECLARE s_cursor2 CURSOR FOR
984       SELECT name, protocol, port
985       FROM services;
986     EXEC SQL OPEN s_cursor2;
987     while (1) { 
988         EXEC SQL FETCH s_cursor2 INTO :service, :protocol, :port;
989         if (sqlca.sqlcode != 0) break;
990         lowercase(protocol);      /* Convert protocol to lowercase */
991         strtrim(service);
992         strtrim(protocol);
993         fprintf(out, "%s.service\tHS %s \"%s %s %d\"\n",
994                 service, HTYPE, service, protocol, port);
995     }
996     EXEC SQL CLOSE s_cursor2;
997
998     EXEC SQL DECLARE a_cursor3 CURSOR FOR
999       SELECT name, trans
1000       FROM alias
1001       WHERE type = "SERVICE";
1002     EXEC SQL OPEN a_cursor3; 
1003     while (1) { 
1004         EXEC SQL FETCH a_cursor3 INTO :service, :altserv;
1005         if (sqlca.sqlcode != 0) break;
1006         strtrim(service);
1007         strtrim(altserv);
1008         fprintf(out, "%s.service\tHS CNAME %s.service\n", service, altserv);
1009     }
1010     EXEC SQL CLOSE a_cursor3;
1011 #ifsql INGRES
1012 #endsql
1013 #ifsql INFORMIX
1014     EXEC SQL COMMIT WORK;
1015 #endsql
1016
1017     if (fclose(out)) {
1018         fprintf(stderr, "Unsuccessful close of service.db\n");
1019         exit(MR_CCONFIG);
1020     }
1021     fix_file(outf);
1022     return(1);
1023  sqlerr:
1024     com_err(whoami, MR_INGRES_ERR, " code %d\n", sqlca.sqlcode);
1025     critical_alert("DCM", "Hesiod build encountered DATABASE ERROR %d",
1026                    sqlca.sqlcode);
1027     exit(MR_INGRES_ERR);
1028 }
This page took 0.163017 seconds and 5 git commands to generate.