]>
Commit | Line | Data |
---|---|---|
f852c398 | 1 | /* $Header$ |
2 | * | |
3 | * This generates zephyr acl files | |
4 | * | |
5 | * (c) Copyright 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 <moira_site.h> | |
14 | #include <sys/types.h> | |
15 | #include <sys/stat.h> | |
16 | #include <sys/time.h> | |
17 | EXEC SQL INCLUDE sqlca; | |
18 | ||
19 | extern int errno; | |
20 | char *whoami = "zephyr.gen"; | |
21 | ||
22 | char zephyr_dir[BUFSIZ]; | |
23 | ||
24 | ||
25 | main(argc, argv) | |
26 | int argc; | |
27 | char **argv; | |
28 | { | |
29 | struct stat sb; | |
30 | int changed; | |
31 | char cmd[256]; | |
32 | ||
33 | if (argc > 2) { | |
34 | fprintf(stderr, "usage: %s [outfile]\n", argv[0]); | |
35 | exit(MR_ARGS); | |
36 | } | |
37 | ||
38 | sprintf(zephyr_dir, "%s/zephyr", DCM_DIR); | |
39 | ||
40 | #ifsql INGRES | |
968f523c | 41 | EXEC SQL CONNECT moira; |
42 | EXEC SQL SET LOCKMODE SESSION WHERE LEVEL=TABLE, READLOCK=SHARED; | |
f852c398 | 43 | #endsql |
44 | #ifsql INFORMIX | |
968f523c | 45 | EXEC SQL DATABASE moira; |
f852c398 | 46 | #endsql |
47 | ||
48 | changed = do_classes(); | |
49 | ||
50 | #ifsql INGRES | |
968f523c | 51 | EXEC SQL DISCONNECT; |
f852c398 | 52 | #endsql |
53 | #ifsql INFORMIX | |
968f523c | 54 | EXEC SQL CLOSE DATABASE; |
f852c398 | 55 | #endsql |
56 | ||
57 | if (!changed) { | |
58 | fprintf(stderr, "No files updated.\n"); | |
59 | if (argc == 2 && stat(argv[1], &sb) == 0) | |
60 | exit(MR_NO_CHANGE); | |
61 | } | |
62 | ||
63 | if (argc == 2) { | |
64 | fprintf(stderr, "Building tar file.\n"); | |
65 | sprintf(cmd, "cd %s; tar cf %s .", zephyr_dir, argv[1]); | |
66 | if (system(cmd)) | |
67 | exit(MR_TAR_FAIL); | |
68 | } | |
69 | ||
70 | exit(MR_SUCCESS); | |
71 | } | |
72 | ||
73 | ||
74 | struct zclass { | |
75 | char class[17]; | |
76 | char xtype[9]; | |
77 | char stype[9]; | |
78 | char wtype[9]; | |
79 | char utype[9]; | |
80 | int xid; | |
81 | int sid; | |
82 | int wid; | |
83 | int uid; | |
84 | struct zclass *next; | |
85 | }; | |
86 | ||
87 | ||
88 | int do_classes() | |
89 | { | |
90 | FILE *out; | |
91 | char outclass[256], buf[256]; | |
92 | struct zclass *top, *next, *zc; | |
93 | struct stat sb; | |
94 | int flag1, flag2; | |
95 | EXEC SQL BEGIN DECLARE SECTION; | |
96 | char zclass[17], zxtype[9], zstype[9], zwtype[9], zutype[9]; | |
97 | int zxid, zsid, zwid, zuid; | |
98 | EXEC SQL END DECLARE SECTION; | |
99 | ||
100 | sprintf(outclass, "%s/class-registry.acl", zephyr_dir); | |
101 | if (stat(outclass, &sb) == 0) { | |
102 | if (ModDiff(&flag1, "zephyr", sb.st_mtime) || | |
103 | ModDiff(&flag2, "imembers", sb.st_mtime)) | |
104 | exit(MR_DATE); | |
105 | if (flag1 < 0 && flag2 < 0) { | |
106 | fprintf(stderr, "Zephyr files do not need to be rebuilt.\n"); | |
107 | return(0); | |
108 | } | |
109 | } | |
110 | ||
111 | sprintf(buf, "%s~", outclass); | |
112 | out = fopen(buf, "w"); | |
113 | if (out == NULL) { | |
114 | perror("opening class-registry.acl"); | |
115 | exit(MR_OCONFIG); | |
116 | } | |
117 | top = (struct zclass *)malloc(sizeof(struct zclass)); | |
118 | next = top; | |
119 | ||
968f523c | 120 | /* The following is declarative, not executed, |
121 | * and so is dependent on where it is in the file, | |
122 | * not in the order of execution of statements. | |
123 | */ | |
f852c398 | 124 | EXEC SQL WHENEVER SQLERROR GOTO sqlerr; |
741abed8 | 125 | |
126 | /* Acquire locks for tables */ | |
127 | EXEC SQL SELECT modtime INTO :zclass FROM imembers WHERE list_id=0; | |
128 | EXEC SQL SELECT modtime INTO :zclass FROM users WHERE list_id = 0; | |
129 | EXEC SQL SELECT modtime INTO :zclass FROM zephyr WHERE xmt_id = 0; | |
130 | ||
f852c398 | 131 | EXEC SQL DECLARE classes CURSOR FOR SELECT class, xmt_type, xmt_id, |
132 | sub_type, sub_id, iws_type, iws_id, iui_type, iui_id | |
741abed8 | 133 | FROM zephyr WHERE xmt_id != 0; |
f852c398 | 134 | EXEC SQL OPEN classes; |
135 | while (1) { | |
136 | EXEC SQL FETCH classes INTO | |
137 | :zclass, :zxtype, :zxid, :zstype, :zsid, | |
138 | :zwtype, :zwid, :zutype, :zuid; | |
139 | if (sqlca.sqlcode != 0) break; | |
140 | zc = (struct zclass *)malloc(sizeof(struct zclass)); | |
141 | next->next = zc; | |
142 | next = zc; | |
143 | strcpy(zc->class, zclass); | |
144 | strcpy(zc->xtype, zxtype); | |
145 | strcpy(zc->stype, zstype); | |
146 | strcpy(zc->wtype, zwtype); | |
147 | strcpy(zc->utype, zutype); | |
148 | zc->xid = zxid; | |
149 | zc->sid = zsid; | |
150 | zc->wid = zwid; | |
151 | zc->uid = zuid; | |
152 | } | |
153 | if (sqlca.sqlcode != 100) { | |
154 | fprintf(stderr, "DBMS error %d\n", sqlca.sqlcode); | |
155 | exit(MR_INGRES_ERR); | |
156 | } | |
157 | EXEC SQL CLOSE classes; | |
158 | for (zc = top->next; zc; zc = zc->next) { | |
159 | fprintf(out, "%s:\n", strtrim(zc->class)); | |
160 | fprintf(stderr, "Working on %s\n", zc->class); | |
161 | if (!strcasecmp(strtrim(zc->xtype), "LIST")) { | |
162 | getlist(zc->xid, zc->class, "xmt"); | |
163 | } else if (!strcasecmp(zc->xtype, "KERBEROS")) { | |
164 | getstring(zc->xid, zc->class, "xmt"); | |
165 | } else if (!strcasecmp(zc->xtype, "USER")) { | |
166 | getuser(zc->xid, zc->class, "xmt"); | |
167 | } else if (!strcasecmp(zc->xtype, "NONE")) { | |
168 | getnone(zc->class, "xmt"); | |
169 | } | |
170 | if (!strcasecmp(strtrim(zc->stype), "LIST")) { | |
171 | getlist(zc->sid, zc->class, "sub"); | |
172 | } else if (!strcasecmp(zc->stype, "KERBEROS")) { | |
173 | getstring(zc->sid, zc->class, "sub"); | |
174 | } else if (!strcasecmp(zc->stype, "USER")) { | |
175 | getuser(zc->sid, zc->class, "sub"); | |
176 | } else if (!strcasecmp(zc->stype, "NONE")) { | |
177 | getnone(zc->class, "sub"); | |
178 | } | |
179 | if (!strcasecmp(strtrim(zc->wtype), "LIST")) { | |
180 | getlist(zc->wid, zc->class, "iws"); | |
181 | } else if (!strcasecmp(zc->wtype, "KERBEROS")) { | |
182 | getstring(zc->wid, zc->class, "iws"); | |
183 | } else if (!strcasecmp(zc->wtype, "USER")) { | |
184 | getuser(zc->wid, zc->class, "iws"); | |
185 | } else if (!strcasecmp(zc->wtype, "NONE")) { | |
186 | getnone(zc->class, "iws"); | |
187 | } | |
188 | if (!strcasecmp(strtrim(zc->utype), "LIST")) { | |
189 | getlist(zc->uid, zc->class, "iui"); | |
190 | } else if (!strcasecmp(zc->utype, "KERBEROS")) { | |
191 | getstring(zc->uid, zc->class, "iui"); | |
192 | } else if (!strcasecmp(zc->utype, "USER")) { | |
193 | getuser(zc->uid, zc->class, "iui"); | |
194 | } else if (!strcasecmp(zc->utype, "NONE")) { | |
195 | getnone(zc->class, "iui"); | |
196 | } | |
197 | } | |
198 | fclose(out); | |
199 | fix_file(outclass); | |
200 | return(1); | |
201 | sqlerr: | |
202 | com_err(whoami, MR_INGRES_ERR, " code %d\n", sqlca.sqlcode); | |
203 | critical_alert("DCM", "Zephyr build encountered DATABASE ERROR %d", | |
204 | sqlca.sqlcode); | |
205 | exit(MR_INGRES_ERR); | |
206 | } | |
207 | ||
208 | ||
209 | getlist(list_id, zclass, atype) | |
210 | int list_id; | |
211 | char *zclass; | |
212 | char *atype; | |
213 | { | |
214 | char ofile[256], buf[256]; | |
215 | FILE *out; | |
216 | EXEC SQL BEGIN DECLARE SECTION; | |
217 | char str[257]; | |
218 | int zid; | |
219 | EXEC SQL END DECLARE SECTION; | |
220 | ||
221 | zid = list_id; | |
222 | sprintf(ofile, "%s/%s-%s.acl", zephyr_dir, atype, zclass); | |
223 | fprintf(buf, "%s~", ofile); | |
224 | out = fopen(buf, "w"); | |
225 | if (out == NULL) { | |
226 | perror("opening acl file"); | |
227 | exit(MR_OCONFIG); | |
228 | } | |
229 | ||
230 | EXEC SQL DECLARE umember CURSOR FOR SELECT users.login | |
231 | FROM users, imembers | |
968f523c | 232 | WHERE imembers.list_id = :zid and imembers.member_type='USER' and |
f852c398 | 233 | imembers.member_id=users.users_id; |
234 | EXEC SQL OPEN umember; | |
235 | while (1) { | |
236 | EXEC SQL FETCH umember INTO :str; | |
237 | if (sqlca.sqlcode != 0) break; | |
238 | fprintf(out, "%s\n", strtrim(str)); | |
239 | } | |
240 | if (sqlca.sqlcode != 100) { | |
241 | fprintf(stderr, "DBMS error %d\n", sqlca.sqlcode); | |
242 | exit(MR_INGRES_ERR); | |
243 | } | |
244 | EXEC SQL CLOSE umember; | |
245 | ||
246 | EXEC SQL DECLARE smember CURSOR FOR SELECT strings.string | |
247 | FROM strings, imembers | |
248 | WHERE imembers.list_id = :zid and imembers.member_id=strings.string_id | |
968f523c | 249 | and (imembers.member_type='STRING' or imembers.member_type='KERBEROS'); |
f852c398 | 250 | EXEC SQL OPEN smember; |
251 | while (1) { | |
252 | EXEC SQL FETCH smember INTO :str; | |
253 | if (sqlca.sqlcode != 0) break; | |
254 | fprintf(out, "%s\n", strtrim(str)); | |
255 | } | |
256 | if (sqlca.sqlcode != 100) { | |
257 | fprintf(stderr, "DBMS error %d\n", sqlca.sqlcode); | |
258 | exit(MR_INGRES_ERR); | |
259 | } | |
260 | EXEC SQL CLOSE smember; | |
261 | if (fclose(out)) { | |
262 | perror("closing acl file"); | |
263 | exit(MR_CCONFIG); | |
264 | } | |
265 | fix_file(ofile); | |
266 | return; | |
267 | sqlerr: | |
268 | com_err(whoami, MR_INGRES_ERR, " code %d\n", sqlca.sqlcode); | |
269 | critical_alert("DCM", "Zephyr build encountered DATABASE ERROR %d", | |
270 | sqlca.sqlcode); | |
271 | exit(MR_INGRES_ERR); | |
272 | } | |
273 | ||
274 | getstring(string_id, zclass, atype) | |
275 | int string_id; | |
276 | char *zclass; | |
277 | char *atype; | |
278 | { | |
279 | char ofile[256], buf[256]; | |
280 | FILE *out; | |
281 | EXEC SQL BEGIN DECLARE SECTION; | |
282 | char str[257]; | |
283 | int zid; | |
284 | EXEC SQL END DECLARE SECTION; | |
285 | ||
286 | zid = string_id; | |
287 | sprintf(ofile, "%s/%s-%s.acl", zephyr_dir, atype, zclass); | |
288 | fprintf(buf, "%s~", ofile); | |
289 | out = fopen(buf, "w"); | |
290 | if (out == NULL) { | |
291 | perror("opening acl file"); | |
292 | exit(MR_OCONFIG); | |
293 | } | |
294 | ||
295 | EXEC SQL SELECT string INTO :str FROM strings WHERE string_id = :zid; | |
296 | if (sqlca.sqlcode != 0) { | |
297 | if (sqlca.sqlcode == 100) { | |
298 | fprintf(stderr, "String %d not found for class %s type %s\n", | |
299 | zid, zclass, atype); | |
300 | } else { | |
301 | fprintf(stderr, "SQL error %d\n", sqlca.sqlcode); | |
302 | exit(MR_INGRES_ERR); | |
303 | } | |
304 | } | |
305 | if (!strcmp(strtrim(str), "WILDCARD")) { | |
306 | strcpy(str, "*.*@*"); | |
307 | } | |
308 | fprintf(out, "%s\n", str); | |
309 | if (fclose(out)) { | |
310 | perror("closing acl file"); | |
311 | exit(MR_CCONFIG); | |
312 | } | |
313 | fix_file(ofile); | |
314 | return; | |
315 | sqlerr: | |
316 | com_err(whoami, MR_INGRES_ERR, " code %d\n", sqlca.sqlcode); | |
317 | critical_alert("DCM", "Zephyr build encountered DATABASE ERROR %d", | |
318 | sqlca.sqlcode); | |
319 | exit(MR_INGRES_ERR); | |
320 | } | |
321 | ||
322 | ||
323 | getuser(user_id, zclass, atype) | |
324 | int user_id; | |
325 | char *zclass; | |
326 | char *atype; | |
327 | { | |
328 | char ofile[256], buf[256]; | |
329 | FILE *out; | |
330 | EXEC SQL BEGIN DECLARE SECTION; | |
331 | char login[9]; | |
332 | int zid; | |
333 | EXEC SQL END DECLARE SECTION; | |
334 | ||
335 | zid = user_id; | |
336 | sprintf(ofile, "%s/%s-%s.acl", zephyr_dir, atype, zclass); | |
337 | fprintf(buf, "%s~", ofile); | |
338 | out = fopen(buf, "w"); | |
339 | if (out == NULL) { | |
340 | perror("opening acl file"); | |
341 | exit(MR_OCONFIG); | |
342 | } | |
343 | ||
344 | EXEC SQL SELECT login INTO :login FROM users WHERE users_id = :zid; | |
345 | if (sqlca.sqlcode != 0) { | |
346 | if (sqlca.sqlcode == 100) { | |
347 | fprintf(stderr, "User %d not found\n", zid); | |
348 | } else { | |
349 | fprintf(stderr, "SQL error %d\n", sqlca.sqlcode); | |
350 | exit(MR_INGRES_ERR); | |
351 | } | |
352 | } | |
353 | fprintf(out, "%s\n", strtrim(login)); | |
354 | if (fclose(out)) { | |
355 | perror("closing acl file"); | |
356 | exit(MR_CCONFIG); | |
357 | } | |
358 | fix_file(ofile); | |
359 | return; | |
360 | sqlerr: | |
361 | com_err(whoami, MR_INGRES_ERR, " code %d\n", sqlca.sqlcode); | |
362 | critical_alert("DCM", "Zephyr build encountered DATABASE ERROR %d", | |
363 | sqlca.sqlcode); | |
364 | exit(MR_INGRES_ERR); | |
365 | } | |
366 | ||
367 | ||
368 | getnone(zclass, atype) | |
369 | char *zclass; | |
370 | char *atype; | |
371 | { | |
372 | char ofile[256]; | |
373 | ||
374 | sprintf(ofile, "%s/%s-%s.acl", zephyr_dir, atype, zclass); | |
375 | unlink(ofile); | |
376 | } |