]>
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; |
125 | EXEC SQL DECLARE classes CURSOR FOR SELECT class, xmt_type, xmt_id, | |
126 | sub_type, sub_id, iws_type, iws_id, iui_type, iui_id | |
127 | FROM zephyr; | |
128 | EXEC SQL OPEN classes; | |
129 | while (1) { | |
130 | EXEC SQL FETCH classes INTO | |
131 | :zclass, :zxtype, :zxid, :zstype, :zsid, | |
132 | :zwtype, :zwid, :zutype, :zuid; | |
133 | if (sqlca.sqlcode != 0) break; | |
134 | zc = (struct zclass *)malloc(sizeof(struct zclass)); | |
135 | next->next = zc; | |
136 | next = zc; | |
137 | strcpy(zc->class, zclass); | |
138 | strcpy(zc->xtype, zxtype); | |
139 | strcpy(zc->stype, zstype); | |
140 | strcpy(zc->wtype, zwtype); | |
141 | strcpy(zc->utype, zutype); | |
142 | zc->xid = zxid; | |
143 | zc->sid = zsid; | |
144 | zc->wid = zwid; | |
145 | zc->uid = zuid; | |
146 | } | |
147 | if (sqlca.sqlcode != 100) { | |
148 | fprintf(stderr, "DBMS error %d\n", sqlca.sqlcode); | |
149 | exit(MR_INGRES_ERR); | |
150 | } | |
151 | EXEC SQL CLOSE classes; | |
152 | for (zc = top->next; zc; zc = zc->next) { | |
153 | fprintf(out, "%s:\n", strtrim(zc->class)); | |
154 | fprintf(stderr, "Working on %s\n", zc->class); | |
155 | if (!strcasecmp(strtrim(zc->xtype), "LIST")) { | |
156 | getlist(zc->xid, zc->class, "xmt"); | |
157 | } else if (!strcasecmp(zc->xtype, "KERBEROS")) { | |
158 | getstring(zc->xid, zc->class, "xmt"); | |
159 | } else if (!strcasecmp(zc->xtype, "USER")) { | |
160 | getuser(zc->xid, zc->class, "xmt"); | |
161 | } else if (!strcasecmp(zc->xtype, "NONE")) { | |
162 | getnone(zc->class, "xmt"); | |
163 | } | |
164 | if (!strcasecmp(strtrim(zc->stype), "LIST")) { | |
165 | getlist(zc->sid, zc->class, "sub"); | |
166 | } else if (!strcasecmp(zc->stype, "KERBEROS")) { | |
167 | getstring(zc->sid, zc->class, "sub"); | |
168 | } else if (!strcasecmp(zc->stype, "USER")) { | |
169 | getuser(zc->sid, zc->class, "sub"); | |
170 | } else if (!strcasecmp(zc->stype, "NONE")) { | |
171 | getnone(zc->class, "sub"); | |
172 | } | |
173 | if (!strcasecmp(strtrim(zc->wtype), "LIST")) { | |
174 | getlist(zc->wid, zc->class, "iws"); | |
175 | } else if (!strcasecmp(zc->wtype, "KERBEROS")) { | |
176 | getstring(zc->wid, zc->class, "iws"); | |
177 | } else if (!strcasecmp(zc->wtype, "USER")) { | |
178 | getuser(zc->wid, zc->class, "iws"); | |
179 | } else if (!strcasecmp(zc->wtype, "NONE")) { | |
180 | getnone(zc->class, "iws"); | |
181 | } | |
182 | if (!strcasecmp(strtrim(zc->utype), "LIST")) { | |
183 | getlist(zc->uid, zc->class, "iui"); | |
184 | } else if (!strcasecmp(zc->utype, "KERBEROS")) { | |
185 | getstring(zc->uid, zc->class, "iui"); | |
186 | } else if (!strcasecmp(zc->utype, "USER")) { | |
187 | getuser(zc->uid, zc->class, "iui"); | |
188 | } else if (!strcasecmp(zc->utype, "NONE")) { | |
189 | getnone(zc->class, "iui"); | |
190 | } | |
191 | } | |
192 | fclose(out); | |
193 | fix_file(outclass); | |
194 | return(1); | |
195 | sqlerr: | |
196 | com_err(whoami, MR_INGRES_ERR, " code %d\n", sqlca.sqlcode); | |
197 | critical_alert("DCM", "Zephyr build encountered DATABASE ERROR %d", | |
198 | sqlca.sqlcode); | |
199 | exit(MR_INGRES_ERR); | |
200 | } | |
201 | ||
202 | ||
203 | getlist(list_id, zclass, atype) | |
204 | int list_id; | |
205 | char *zclass; | |
206 | char *atype; | |
207 | { | |
208 | char ofile[256], buf[256]; | |
209 | FILE *out; | |
210 | EXEC SQL BEGIN DECLARE SECTION; | |
211 | char str[257]; | |
212 | int zid; | |
213 | EXEC SQL END DECLARE SECTION; | |
214 | ||
215 | zid = list_id; | |
216 | sprintf(ofile, "%s/%s-%s.acl", zephyr_dir, atype, zclass); | |
217 | fprintf(buf, "%s~", ofile); | |
218 | out = fopen(buf, "w"); | |
219 | if (out == NULL) { | |
220 | perror("opening acl file"); | |
221 | exit(MR_OCONFIG); | |
222 | } | |
223 | ||
224 | EXEC SQL DECLARE umember CURSOR FOR SELECT users.login | |
225 | FROM users, imembers | |
968f523c | 226 | WHERE imembers.list_id = :zid and imembers.member_type='USER' and |
f852c398 | 227 | imembers.member_id=users.users_id; |
228 | EXEC SQL OPEN umember; | |
229 | while (1) { | |
230 | EXEC SQL FETCH umember INTO :str; | |
231 | if (sqlca.sqlcode != 0) break; | |
232 | fprintf(out, "%s\n", strtrim(str)); | |
233 | } | |
234 | if (sqlca.sqlcode != 100) { | |
235 | fprintf(stderr, "DBMS error %d\n", sqlca.sqlcode); | |
236 | exit(MR_INGRES_ERR); | |
237 | } | |
238 | EXEC SQL CLOSE umember; | |
239 | ||
240 | EXEC SQL DECLARE smember CURSOR FOR SELECT strings.string | |
241 | FROM strings, imembers | |
242 | WHERE imembers.list_id = :zid and imembers.member_id=strings.string_id | |
968f523c | 243 | and (imembers.member_type='STRING' or imembers.member_type='KERBEROS'); |
f852c398 | 244 | EXEC SQL OPEN smember; |
245 | while (1) { | |
246 | EXEC SQL FETCH smember INTO :str; | |
247 | if (sqlca.sqlcode != 0) break; | |
248 | fprintf(out, "%s\n", strtrim(str)); | |
249 | } | |
250 | if (sqlca.sqlcode != 100) { | |
251 | fprintf(stderr, "DBMS error %d\n", sqlca.sqlcode); | |
252 | exit(MR_INGRES_ERR); | |
253 | } | |
254 | EXEC SQL CLOSE smember; | |
255 | if (fclose(out)) { | |
256 | perror("closing acl file"); | |
257 | exit(MR_CCONFIG); | |
258 | } | |
259 | fix_file(ofile); | |
260 | return; | |
261 | sqlerr: | |
262 | com_err(whoami, MR_INGRES_ERR, " code %d\n", sqlca.sqlcode); | |
263 | critical_alert("DCM", "Zephyr build encountered DATABASE ERROR %d", | |
264 | sqlca.sqlcode); | |
265 | exit(MR_INGRES_ERR); | |
266 | } | |
267 | ||
268 | getstring(string_id, zclass, atype) | |
269 | int string_id; | |
270 | char *zclass; | |
271 | char *atype; | |
272 | { | |
273 | char ofile[256], buf[256]; | |
274 | FILE *out; | |
275 | EXEC SQL BEGIN DECLARE SECTION; | |
276 | char str[257]; | |
277 | int zid; | |
278 | EXEC SQL END DECLARE SECTION; | |
279 | ||
280 | zid = string_id; | |
281 | sprintf(ofile, "%s/%s-%s.acl", zephyr_dir, atype, zclass); | |
282 | fprintf(buf, "%s~", ofile); | |
283 | out = fopen(buf, "w"); | |
284 | if (out == NULL) { | |
285 | perror("opening acl file"); | |
286 | exit(MR_OCONFIG); | |
287 | } | |
288 | ||
289 | EXEC SQL SELECT string INTO :str FROM strings WHERE string_id = :zid; | |
290 | if (sqlca.sqlcode != 0) { | |
291 | if (sqlca.sqlcode == 100) { | |
292 | fprintf(stderr, "String %d not found for class %s type %s\n", | |
293 | zid, zclass, atype); | |
294 | } else { | |
295 | fprintf(stderr, "SQL error %d\n", sqlca.sqlcode); | |
296 | exit(MR_INGRES_ERR); | |
297 | } | |
298 | } | |
299 | if (!strcmp(strtrim(str), "WILDCARD")) { | |
300 | strcpy(str, "*.*@*"); | |
301 | } | |
302 | fprintf(out, "%s\n", str); | |
303 | if (fclose(out)) { | |
304 | perror("closing acl file"); | |
305 | exit(MR_CCONFIG); | |
306 | } | |
307 | fix_file(ofile); | |
308 | return; | |
309 | sqlerr: | |
310 | com_err(whoami, MR_INGRES_ERR, " code %d\n", sqlca.sqlcode); | |
311 | critical_alert("DCM", "Zephyr build encountered DATABASE ERROR %d", | |
312 | sqlca.sqlcode); | |
313 | exit(MR_INGRES_ERR); | |
314 | } | |
315 | ||
316 | ||
317 | getuser(user_id, zclass, atype) | |
318 | int user_id; | |
319 | char *zclass; | |
320 | char *atype; | |
321 | { | |
322 | char ofile[256], buf[256]; | |
323 | FILE *out; | |
324 | EXEC SQL BEGIN DECLARE SECTION; | |
325 | char login[9]; | |
326 | int zid; | |
327 | EXEC SQL END DECLARE SECTION; | |
328 | ||
329 | zid = user_id; | |
330 | sprintf(ofile, "%s/%s-%s.acl", zephyr_dir, atype, zclass); | |
331 | fprintf(buf, "%s~", ofile); | |
332 | out = fopen(buf, "w"); | |
333 | if (out == NULL) { | |
334 | perror("opening acl file"); | |
335 | exit(MR_OCONFIG); | |
336 | } | |
337 | ||
338 | EXEC SQL SELECT login INTO :login FROM users WHERE users_id = :zid; | |
339 | if (sqlca.sqlcode != 0) { | |
340 | if (sqlca.sqlcode == 100) { | |
341 | fprintf(stderr, "User %d not found\n", zid); | |
342 | } else { | |
343 | fprintf(stderr, "SQL error %d\n", sqlca.sqlcode); | |
344 | exit(MR_INGRES_ERR); | |
345 | } | |
346 | } | |
347 | fprintf(out, "%s\n", strtrim(login)); | |
348 | if (fclose(out)) { | |
349 | perror("closing acl file"); | |
350 | exit(MR_CCONFIG); | |
351 | } | |
352 | fix_file(ofile); | |
353 | return; | |
354 | sqlerr: | |
355 | com_err(whoami, MR_INGRES_ERR, " code %d\n", sqlca.sqlcode); | |
356 | critical_alert("DCM", "Zephyr build encountered DATABASE ERROR %d", | |
357 | sqlca.sqlcode); | |
358 | exit(MR_INGRES_ERR); | |
359 | } | |
360 | ||
361 | ||
362 | getnone(zclass, atype) | |
363 | char *zclass; | |
364 | char *atype; | |
365 | { | |
366 | char ofile[256]; | |
367 | ||
368 | sprintf(ofile, "%s/%s-%s.acl", zephyr_dir, atype, zclass); | |
369 | unlink(ofile); | |
370 | } |