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