1 | /* $Id: ndb.pc,v 1.5 2000-07-17 17:52:43 zacheiss Exp $ |
---|
2 | * |
---|
3 | * This generates the msql database for the network tables. |
---|
4 | * |
---|
5 | * Copyright 1998 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 <moira.h> |
---|
12 | |
---|
13 | #include <sys/stat.h> |
---|
14 | |
---|
15 | #include <stdio.h> |
---|
16 | #include <time.h> |
---|
17 | |
---|
18 | #include "util.h" |
---|
19 | |
---|
20 | EXEC SQL INCLUDE sqlca; |
---|
21 | |
---|
22 | RCSID("$Header: /afs/athena.mit.edu/astaff/project/moiradev/repository/moira/gen/ndb.pc,v 1.5 2000-07-17 17:52:43 zacheiss Exp $"); |
---|
23 | |
---|
24 | char *whoami = "ndb.gen"; |
---|
25 | char *db = "moira/moira"; |
---|
26 | |
---|
27 | void users(FILE *out); |
---|
28 | void hosts(FILE *out); |
---|
29 | |
---|
30 | int main(int argc, char **argv) |
---|
31 | { |
---|
32 | FILE *out = stdout; |
---|
33 | char *outf = NULL, outft[MAXPATHLEN]; |
---|
34 | struct stat sb; |
---|
35 | int flag; |
---|
36 | |
---|
37 | EXEC SQL CONNECT :db; |
---|
38 | |
---|
39 | if (argc == 2) |
---|
40 | { |
---|
41 | outf = argv[1]; |
---|
42 | sprintf(outft, "%s~", outf); |
---|
43 | if (!(out = fopen(outft, "w"))) |
---|
44 | { |
---|
45 | fprintf(stderr, "unable to open %s for output\n", outf); |
---|
46 | exit(MR_OCONFIG); |
---|
47 | } |
---|
48 | } |
---|
49 | else if (argc != 1) |
---|
50 | { |
---|
51 | fprintf(stderr, "usage: %s [outfile]\n", argv[0]); |
---|
52 | exit(MR_ARGS); |
---|
53 | } |
---|
54 | else |
---|
55 | outf = NULL; |
---|
56 | |
---|
57 | EXEC SQL COMMIT; |
---|
58 | |
---|
59 | fprintf(stderr, "users...\n"); |
---|
60 | users(out); |
---|
61 | fprintf(stderr, "hosts...\n"); |
---|
62 | hosts(out); |
---|
63 | |
---|
64 | if (fclose(out) < 0) |
---|
65 | { |
---|
66 | perror("close failed"); |
---|
67 | exit(MR_CCONFIG); |
---|
68 | } |
---|
69 | |
---|
70 | if (outf) |
---|
71 | fix_file(outf); |
---|
72 | exit(MR_SUCCESS); |
---|
73 | } |
---|
74 | |
---|
75 | void users(FILE *out) |
---|
76 | { |
---|
77 | char *c; |
---|
78 | EXEC SQL BEGIN DECLARE SECTION; |
---|
79 | char login[USERS_LOGIN_SIZE], id[USERS_CLEARID_SIZE]; |
---|
80 | char first[USERS_FIRST_SIZE], middle[USERS_MIDDLE_SIZE]; |
---|
81 | char last[USERS_LAST_SIZE], type[USERS_TYPE_SIZE]; |
---|
82 | int status, users_id; |
---|
83 | EXEC SQL END DECLARE SECTION; |
---|
84 | |
---|
85 | EXEC SQL WHENEVER SQLERROR GOTO sqlerr; |
---|
86 | |
---|
87 | EXEC SQL DECLARE users1 CURSOR FOR |
---|
88 | SELECT login, clearid, users_id, type, status, first, middle, last |
---|
89 | FROM users WHERE clearid != '0' |
---|
90 | AND login NOT LIKE '#%'; |
---|
91 | EXEC SQL OPEN users1; |
---|
92 | while (1) |
---|
93 | { |
---|
94 | EXEC SQL FETCH users1 INTO :login, :id, :users_id, :type, :status, |
---|
95 | :first, :middle, :last; |
---|
96 | |
---|
97 | if (sqlca.sqlcode) |
---|
98 | break; |
---|
99 | strtrim(login); |
---|
100 | strtrim(id); |
---|
101 | strtrim(type); |
---|
102 | strtrim(first); |
---|
103 | strtrim(middle); |
---|
104 | strtrim(last); |
---|
105 | if (!*id) |
---|
106 | continue; |
---|
107 | fprintf(out, "user,%d,%s,%s,%s,%d,%s,%s,%s\n", users_id, id, login, |
---|
108 | type, status, first, middle, last); |
---|
109 | } |
---|
110 | |
---|
111 | EXEC SQL CLOSE users1; |
---|
112 | |
---|
113 | EXEC SQL COMMIT; |
---|
114 | |
---|
115 | return; |
---|
116 | |
---|
117 | sqlerr: |
---|
118 | db_error(sqlca.sqlcode); |
---|
119 | exit(MR_DBMS_ERR); |
---|
120 | } |
---|
121 | |
---|
122 | void hosts(FILE *out) |
---|
123 | { |
---|
124 | struct hash *users; |
---|
125 | char *p; |
---|
126 | int i; |
---|
127 | EXEC SQL BEGIN DECLARE SECTION; |
---|
128 | char name[MACHINE_NAME_SIZE], owner_type[MACHINE_OWNER_TYPE_SIZE]; |
---|
129 | char addr[MACHINE_ADDRESS_SIZE]; |
---|
130 | int id, use, status, owner; |
---|
131 | EXEC SQL END DECLARE SECTION; |
---|
132 | |
---|
133 | EXEC SQL WHENEVER SQLERROR GOTO sqlerr; |
---|
134 | |
---|
135 | fprintf(stderr, "aliases...\n"); |
---|
136 | |
---|
137 | EXEC SQL DECLARE hosts1 CURSOR FOR SELECT |
---|
138 | mach_id, name FROM hostalias; |
---|
139 | EXEC SQL OPEN hosts1; |
---|
140 | |
---|
141 | while (1) |
---|
142 | { |
---|
143 | EXEC SQL FETCH hosts1 INTO :id, :name; |
---|
144 | if (sqlca.sqlcode) |
---|
145 | break; |
---|
146 | if (id == 0) |
---|
147 | continue; |
---|
148 | if (!*strtrim(name)) |
---|
149 | continue; |
---|
150 | if ((i = strlen(name)) < 9 || strcmp(&name[i - 8], ".MIT.EDU")) |
---|
151 | { |
---|
152 | fprintf(stderr, "Name %s not in MIT domain\n", name); |
---|
153 | continue; |
---|
154 | } |
---|
155 | fprintf(out, "host_alias,%d,%s\n", id, name); |
---|
156 | } |
---|
157 | |
---|
158 | EXEC SQL CLOSE hosts1; |
---|
159 | |
---|
160 | EXEC SQL COMMIT; |
---|
161 | |
---|
162 | fprintf(stderr, "hosts (for real)...\n"); |
---|
163 | |
---|
164 | EXEC SQL DECLARE hosts3 CURSOR FOR SELECT |
---|
165 | name, mach_id, address, use, status, owner_type, owner_id |
---|
166 | FROM machine; |
---|
167 | EXEC SQL OPEN hosts3; |
---|
168 | while (1) |
---|
169 | { |
---|
170 | EXEC SQL FETCH hosts3 INTO :name, :id, :addr, :use, :status, |
---|
171 | :owner_type, :owner; |
---|
172 | if (sqlca.sqlcode) |
---|
173 | break; |
---|
174 | if (id == 0) |
---|
175 | continue; |
---|
176 | if (!*strtrim(name)) |
---|
177 | continue; |
---|
178 | if ((i = strlen(name)) < 9 || strcmp(&name[i - 8], ".MIT.EDU")) |
---|
179 | continue; |
---|
180 | strtrim(addr); |
---|
181 | strtrim(owner_type); |
---|
182 | fprintf(out, "host,%d,%s,%s,%d,0,%d", id, name, addr, use, status); |
---|
183 | if (!strcmp(owner_type, "USER")) |
---|
184 | fprintf(out, ",USER,%d\n", owner); |
---|
185 | else |
---|
186 | fprintf(out, ",NONE,0\n"); |
---|
187 | } |
---|
188 | |
---|
189 | EXEC SQL CLOSE hosts3; |
---|
190 | |
---|
191 | EXEC SQL COMMIT; |
---|
192 | |
---|
193 | return; |
---|
194 | sqlerr: |
---|
195 | db_error(sqlca.sqlcode); |
---|
196 | exit(MR_DBMS_ERR); |
---|
197 | } |
---|
198 | |
---|
199 | |
---|