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