]>
Commit | Line | Data |
---|---|---|
f2dd7e91 | 1 | /* $Header$ |
2 | * | |
3 | * This generates the msql database for the network tables. | |
4 | * | |
5 | * (c) Copyright 1988, 1990 by the Massachusetts Institute of Technology. | |
6 | * For copying and distribution information, please see the file | |
7 | * <mit-copyright.h>. | |
8 | */ | |
9 | ||
10 | #include <mit-copyright.h> | |
11 | #include <stdio.h> | |
12 | #include <moira.h> | |
13 | #include <sys/types.h> | |
14 | #include <sys/stat.h> | |
15 | #include <sys/time.h> | |
16 | EXEC SQL INCLUDE sqlca; | |
17 | ||
18 | extern int errno; | |
19 | char *whoami = "ndb.gen"; | |
20 | char *db = "moira/moira"; | |
21 | ||
22 | void users(FILE *out); | |
23 | void hosts(FILE *out); | |
24 | ||
25 | int main(int argc, char **argv) | |
26 | { | |
27 | FILE *out = stdout; | |
28 | char *outf = NULL, outft[64]; | |
29 | struct stat sb; | |
30 | int flag; | |
31 | ||
32 | EXEC SQL CONNECT :db; | |
33 | ||
34 | if (argc == 2) | |
35 | { | |
36 | outf = argv[1]; | |
37 | sprintf(outft, "%s~", outf); | |
38 | if (!(out = fopen(outft, "w"))) | |
39 | { | |
40 | fprintf(stderr, "unable to open %s for output\n", outf); | |
41 | exit(MR_OCONFIG); | |
42 | } | |
43 | } | |
44 | else if (argc != 1) | |
45 | { | |
46 | fprintf(stderr, "usage: %s [outfile]\n", argv[0]); | |
47 | exit(MR_ARGS); | |
48 | } | |
49 | else | |
50 | outf = NULL; | |
51 | ||
52 | EXEC SQL COMMIT; | |
53 | ||
54 | fprintf(stderr, "users...\n"); | |
55 | users(out); | |
56 | fprintf(stderr, "hosts...\n"); | |
57 | hosts(out); | |
58 | ||
59 | if(fclose(out) < 0) | |
60 | { | |
61 | perror("close failed"); | |
62 | exit(MR_CCONFIG); | |
63 | } | |
64 | ||
65 | if (outf) | |
66 | fix_file(outf); | |
67 | exit(MR_SUCCESS); | |
68 | } | |
69 | ||
70 | void users(FILE *out) | |
71 | { | |
72 | char *c; | |
73 | EXEC SQL BEGIN DECLARE SECTION; | |
74 | char login[9], id[17]; | |
75 | EXEC SQL END DECLARE SECTION; | |
76 | ||
77 | EXEC SQL WHENEVER SQLERROR GOTO sqlerr; | |
78 | ||
79 | EXEC SQL DECLARE users1 CURSOR FOR SELECT login, clearid | |
80 | FROM users WHERE clearid != '0' AND clearid != '999999999' | |
81 | AND login NOT LIKE '#%'; | |
82 | EXEC SQL OPEN users1; | |
83 | while (1) | |
84 | { | |
85 | EXEC SQL FETCH users1 INTO :login, id; | |
86 | ||
87 | if (sqlca.sqlcode) | |
88 | break; | |
89 | strtrim(login); | |
90 | strtrim(id); | |
91 | c = login; | |
92 | while (*c) | |
93 | { | |
94 | if (!isdigit(*c++)) | |
95 | break; | |
96 | } | |
97 | fprintf(out, "user,%s,%s\n", id, login); | |
98 | } | |
99 | ||
100 | EXEC SQL CLOSE users1; | |
101 | ||
102 | EXEC SQL COMMIT; | |
103 | ||
104 | return; | |
105 | ||
106 | sqlerr: | |
107 | db_error(sqlca.sqlcode); | |
108 | exit(MR_DBMS_ERR); | |
109 | } | |
110 | ||
111 | void hosts(FILE *out) | |
112 | { | |
113 | struct hash *users; | |
114 | char *p; | |
115 | int i; | |
116 | EXEC SQL BEGIN DECLARE SECTION; | |
117 | char name[128], mitid[17], owner_type[9], addr[17], inuse[64]; | |
118 | int id, use, status, owner; | |
119 | EXEC SQL END DECLARE SECTION; | |
120 | ||
121 | EXEC SQL WHENEVER SQLERROR GOTO sqlerr; | |
122 | ||
123 | fprintf(stderr, "aliases...\n"); | |
124 | ||
125 | EXEC SQL DECLARE hosts1 CURSOR FOR SELECT | |
126 | mach_id, name FROM hostalias; | |
127 | EXEC SQL OPEN hosts1; | |
128 | ||
129 | while (1) | |
130 | { | |
131 | EXEC SQL FETCH hosts1 INTO :id, :name; | |
132 | if (sqlca.sqlcode) | |
133 | break; | |
134 | if (id == 0) | |
135 | continue; | |
136 | if (!*strtrim(name)) | |
137 | continue; | |
138 | if ((i = strlen(name)) < 9 || strcmp(&name[i-8], ".MIT.EDU")) | |
139 | { | |
140 | fprintf(stderr, "Name %s not in MIT domain\n", name); | |
141 | continue; | |
142 | } | |
143 | fprintf(out, "host_alias,%d,%s\n", id, name); | |
144 | } | |
145 | ||
146 | EXEC SQL CLOSE hosts1; | |
147 | ||
148 | EXEC SQL COMMIT; | |
149 | ||
150 | fprintf(stderr, "users (again)...\n"); | |
151 | ||
152 | EXEC SQL DECLARE hosts2 CURSOR FOR SELECT | |
153 | users_id, clearid FROM users | |
154 | WHERE clearid != '0' and clearid !='999999999'; | |
155 | EXEC SQL OPEN hosts2; | |
156 | users = create_hash(20001); | |
157 | ||
158 | while (1) | |
159 | { | |
160 | EXEC SQL FETCH hosts2 INTO :id, :mitid; | |
161 | if (sqlca.sqlcode) | |
162 | break; | |
163 | if (id == 0) | |
164 | continue; | |
165 | if (!*strtrim(mitid)) | |
166 | continue; | |
167 | ||
168 | hash_store(users, id, strsave(mitid)); | |
169 | } | |
170 | ||
171 | EXEC SQL CLOSE hosts2; | |
172 | ||
173 | EXEC SQL COMMIT; | |
174 | ||
175 | fprintf(stderr, "hosts (for real)...\n"); | |
176 | ||
177 | EXEC SQL DECLARE hosts3 CURSOR FOR SELECT | |
178 | name, mach_id, address, use, inuse, status, owner_type, owner_id | |
179 | FROM machine; | |
180 | EXEC SQL OPEN hosts3; | |
181 | while (1) | |
182 | { | |
183 | EXEC SQL FETCH hosts3 INTO :name, :id, :addr, :use, :inuse, :status, | |
184 | :owner_type, :owner; | |
185 | if (sqlca.sqlcode) | |
186 | break; | |
187 | if (id == 0) | |
188 | continue; | |
189 | if (!*strtrim(name)) | |
190 | continue; | |
191 | if ((i = strlen(name)) < 9 || strcmp(&name[i-8], ".MIT.EDU")) | |
192 | continue; | |
193 | strtrim(addr); | |
194 | strtrim(owner_type); | |
195 | strtrim(inuse); | |
196 | fprintf(out, "host,%d,%s,%s,%d,0,%d", id,name,addr,use,status); | |
197 | if (!strcmp(owner_type, "USER")) | |
198 | { | |
199 | if (p = hash_lookup(users, owner)) | |
200 | fprintf(out, ",USER,%s\n", p); | |
201 | } | |
202 | else | |
203 | fprintf(out, ",NONE,0\n"); | |
204 | } | |
205 | ||
206 | EXEC SQL CLOSE hosts3; | |
207 | ||
208 | EXEC SQL COMMIT; | |
209 | ||
210 | return; | |
211 | sqlerr: | |
212 | db_error(sqlca.sqlcode); | |
213 | exit(MR_DBMS_ERR); | |
214 | } | |
215 | ||
216 |