]>
Commit | Line | Data |
---|---|---|
7ac48069 | 1 | /* $Id$ |
2289e07e | 2 | * |
3 | * This generates the hstath.txt hosttable. | |
4 | * | |
7ac48069 | 5 | * (c) Copyright 1993-1998 by the Massachusetts Institute of Technology. |
6 | * For copying and distribution information, please see the file | |
7 | * <mit-copyright.h>. | |
2289e07e | 8 | */ |
9 | ||
10 | #include <mit-copyright.h> | |
2289e07e | 11 | #include <moira.h> |
7ac48069 | 12 | |
2289e07e | 13 | #include <sys/stat.h> |
7ac48069 | 14 | |
15 | #include <netinet/in.h> | |
16 | #include <arpa/inet.h> | |
17 | ||
18 | #include <stdio.h> | |
19 | #include <stdlib.h> | |
20 | #include <string.h> | |
21 | #include <time.h> | |
22 | ||
23 | #include "util.h" | |
24 | ||
2289e07e | 25 | EXEC SQL INCLUDE sqlca; |
26 | ||
7ac48069 | 27 | RCSID("$Header$"); |
28 | ||
2289e07e | 29 | char *whoami = "hosts.gen"; |
9c04b191 | 30 | char *db = "moira/moira"; |
2289e07e | 31 | |
5eaef520 | 32 | int main(int argc, char **argv) |
2289e07e | 33 | { |
5eaef520 | 34 | FILE *out = stdout; |
dfaf9b68 | 35 | char *outf = NULL, outft[MAXPATHLEN], *p; |
5eaef520 | 36 | struct timeval now; |
a589d269 | 37 | int i; |
5eaef520 | 38 | struct hash *aliases; |
39 | EXEC SQL BEGIN DECLARE SECTION; | |
40 | int id; | |
dfaf9b68 | 41 | char name[MACHINE_NAME_SIZE], vendor[MACHINE_VENDOR_SIZE]; |
42 | char model[MACHINE_MODEL_SIZE], os[MACHINE_OS_SIZE]; | |
43 | char addr[MACHINE_ADDRESS_SIZE]; | |
5eaef520 | 44 | EXEC SQL END DECLARE SECTION; |
2289e07e | 45 | |
5eaef520 | 46 | EXEC SQL CONNECT :db; |
2289e07e | 47 | |
5eaef520 | 48 | if (argc == 2) |
49 | { | |
5eaef520 | 50 | outf = argv[1]; |
51 | sprintf(outft, "%s~", outf); | |
52 | if (!(out = fopen(outft, "w"))) | |
53 | { | |
54 | fprintf(stderr, "unable to open %s for output\n", outf); | |
55 | exit(MR_OCONFIG); | |
2289e07e | 56 | } |
2289e07e | 57 | } |
5eaef520 | 58 | else if (argc != 1) |
59 | { | |
60 | fprintf(stderr, "usage: %s [outfile]\n", argv[0]); | |
61 | exit(MR_ARGS); | |
62 | } | |
63 | else | |
64 | outf = NULL; | |
65 | ||
66 | EXEC SQL WHENEVER SQLERROR GOTO sqlerr; | |
2289e07e | 67 | |
5eaef520 | 68 | gettimeofday(&now, NULL); |
2289e07e | 69 | |
5eaef520 | 70 | fprintf(out, "; MIT Network Host Table\n;\n"); |
71 | fprintf(out, "; \t%cAuthor: $\n", '$'); | |
72 | fprintf(out, "; \t%cDate: $\n", '$'); | |
73 | fprintf(out, "; \t%cRevision: $\n;\n", '$'); | |
74 | fprintf(out, "; Host table generated by Moira at %s;\n", | |
75 | ctime(&now.tv_sec)); | |
2289e07e | 76 | |
5eaef520 | 77 | EXEC SQL DECLARE y CURSOR FOR SELECT mach_id, name FROM hostalias; |
78 | EXEC SQL OPEN y; | |
79 | aliases = create_hash(1001); | |
80 | while (1) | |
81 | { | |
82 | EXEC SQL FETCH y INTO :id, :name; | |
83 | if (sqlca.sqlcode) | |
84 | break; | |
85 | if (id == 0) | |
86 | continue; | |
87 | if (!*strtrim(name)) | |
88 | continue; | |
ed7faf49 | 89 | if ((i = strlen(name)) < 9 || !strchr(name, '.') || |
90 | strcmp(strchr(name, '.'), ".MIT.EDU")) | |
5eaef520 | 91 | { |
ed7faf49 | 92 | fprintf(stderr, "Ignoring alias %s\n", name); |
5eaef520 | 93 | continue; |
2289e07e | 94 | } |
5eaef520 | 95 | else |
96 | name[i - 8] = 0; | |
7ac48069 | 97 | if ((p = hash_lookup(aliases, id))) |
5eaef520 | 98 | { |
3283335f | 99 | p = realloc(p, strlen(p) + strlen(name) + 2); |
100 | sprintf(strchr(p, '\0'), ",%s", name); | |
101 | hash_update(aliases, id, p); | |
5eaef520 | 102 | } |
103 | else | |
7ac48069 | 104 | hash_store(aliases, id, strdup(name)); |
2289e07e | 105 | } |
106 | ||
5eaef520 | 107 | EXEC SQL DECLARE x CURSOR FOR SELECT |
108 | name, mach_id, vendor, model, os, address | |
c203b667 | 109 | FROM machine WHERE status = 1 AND mach_id > 0 |
110 | ORDER BY address; | |
5eaef520 | 111 | EXEC SQL OPEN x; |
112 | while (1) | |
113 | { | |
114 | EXEC SQL FETCH x INTO :name, :id, :vendor, :model, :os, :addr; | |
115 | if (sqlca.sqlcode) | |
116 | break; | |
5eaef520 | 117 | if (!*strtrim(name)) |
118 | continue; | |
ed7faf49 | 119 | if ((i = strlen(name)) < 9 || !strchr(name, '.') || |
120 | strcmp(strchr(name, '.'), ".MIT.EDU")) | |
121 | { | |
122 | fprintf(stderr, "Ignoring machine %s\n", name); | |
123 | continue; | |
124 | } | |
5eaef520 | 125 | else |
126 | name[i - 8] = 0; | |
127 | strtrim(vendor); | |
128 | strtrim(model); | |
129 | strtrim(os); | |
130 | strtrim(addr); | |
131 | if (*addr == 0 || inet_addr(addr) == -1) | |
132 | continue; | |
3283335f | 133 | fprintf(out, "HOST : %s : %s", addr, name); |
5eaef520 | 134 | if ((p = hash_lookup(aliases, id))) |
3283335f | 135 | fprintf(out, ",%s", p); |
5eaef520 | 136 | if ((*vendor || *model) && *os) |
137 | { | |
138 | if (*vendor && *model) | |
3283335f | 139 | fprintf(out, " : %s/%s : %s :\n", vendor, model, os); |
5eaef520 | 140 | else |
3283335f | 141 | fprintf(out, " : %s%s : %s :\n", vendor, model, os); |
2289e07e | 142 | } |
5eaef520 | 143 | else |
3283335f | 144 | fputs(" : \n", out); |
2289e07e | 145 | } |
146 | ||
5eaef520 | 147 | EXEC SQL CLOSE x; |
9c04b191 | 148 | |
5eaef520 | 149 | EXEC SQL COMMIT; |
2289e07e | 150 | |
5eaef520 | 151 | fprintf(out, "; End of automatically generated host table\n"); |
152 | if (fclose(out)) | |
153 | { | |
154 | perror("close failed"); | |
155 | exit(MR_CCONFIG); | |
2289e07e | 156 | } |
5eaef520 | 157 | if (outf) |
158 | fix_file(outf); | |
159 | exit(MR_SUCCESS); | |
2289e07e | 160 | |
5eaef520 | 161 | sqlerr: |
162 | db_error(sqlca.sqlcode); | |
163 | exit(MR_DBMS_ERR); | |
2289e07e | 164 | } |