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