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