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