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