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