]>
Commit | Line | Data |
---|---|---|
c31c844d | 1 | /* $Id$ |
2 | * | |
3 | * This generates the user, list, list membership, filesys data | |
4 | * for windows active directory update | |
5 | * | |
6 | * (c) Copyright 1988-2001 by the Massachusetts Institute of Technology. | |
7 | * For copying and distribution information, please see the file | |
8 | * <mit-copyright.h>. | |
9 | */ | |
10 | ||
11 | #include <mit-copyright.h> | |
12 | #include <moira.h> | |
13 | #include <moira_site.h> | |
14 | ||
15 | #include <sys/stat.h> | |
16 | #include <stdio.h> | |
17 | #include <stdlib.h> | |
18 | #include <string.h> | |
19 | ||
20 | #include "util.h" | |
21 | ||
22 | EXEC SQL INCLUDE sqlca; | |
23 | ||
24 | #ifndef WINAD_SUBDIR | |
25 | #define WINAD_SUBDIR "winad" | |
26 | #endif | |
27 | ||
28 | char winad_dir[MAXPATHLEN]; | |
29 | char *whoami = "winad.gen"; | |
30 | char *db = "moira/moira"; | |
31 | ||
32 | int do_user(void); | |
33 | int do_groups(void); | |
34 | int do_groupmembership(void); | |
35 | ||
36 | int main(int argc, char **argv) | |
37 | { | |
38 | char cmd[64]; | |
39 | struct stat sb; | |
40 | int changed = 0; | |
41 | ||
42 | if (argc > 2) | |
43 | { | |
44 | fprintf(stderr, "usage: %s [outfile]\n", argv[0]); | |
45 | exit(MR_ARGS); | |
46 | } | |
47 | ||
48 | initialize_sms_error_table(); | |
49 | sprintf(winad_dir, "%s/%s", DCM_DIR, WINAD_SUBDIR); | |
50 | ||
51 | EXEC SQL CONNECT :db; | |
52 | ||
53 | changed = do_user(); | |
54 | changed += do_groups(); | |
55 | changed += do_groupmembership(); | |
56 | ||
57 | if (!changed) | |
58 | { | |
59 | fprintf(stderr, "No files updated.\n"); | |
60 | if (argc == 2 && stat(argv[1], &sb) == 0) | |
61 | exit(MR_NO_CHANGE); | |
62 | } | |
63 | ||
64 | if (argc == 2) | |
65 | { | |
66 | fprintf(stderr, "Building tar file.\n"); | |
67 | sprintf(cmd, "cd %s; tar cf %s .", winad_dir, argv[1]); | |
68 | if (system(cmd)) | |
69 | exit(MR_TAR_FAIL); | |
70 | } | |
71 | ||
72 | exit(MR_SUCCESS); | |
73 | } | |
74 | ||
75 | ||
76 | int do_user(void) | |
77 | { | |
78 | FILE *fout; | |
79 | char foutf[MAXPATHLEN]; | |
80 | char foutft[MAXPATHLEN]; | |
81 | EXEC SQL BEGIN DECLARE SECTION; | |
82 | char login[USERS_LOGIN_SIZE]; | |
83 | char mit_id[USERS_CLEARID_SIZE]; | |
84 | int users_id, unix_uid, status; | |
85 | char type[FILESYS_TYPE_SIZE]; | |
86 | char name[FILESYS_NAME_SIZE]; | |
65382c46 | 87 | int fid; |
c31c844d | 88 | EXEC SQL END DECLARE SECTION; |
89 | ||
90 | sprintf(foutf, "%s/winuser.db", winad_dir); | |
91 | sprintf(foutft, "%s~", foutf); | |
92 | ||
93 | fout = fopen(foutft, "w"); | |
94 | if (!fout) | |
95 | { | |
96 | perror("cannot open winuser.db for write"); | |
97 | exit(MR_OCONFIG); | |
98 | } | |
99 | ||
100 | EXEC SQL DECLARE u_cursor CURSOR FOR | |
101 | SELECT users_id, login, unix_uid, status, clearid | |
102 | FROM users | |
103 | ORDER BY users_id; | |
104 | EXEC SQL OPEN u_cursor; | |
105 | while (1) | |
106 | { | |
107 | EXEC SQL FETCH u_cursor INTO :users_id, :login, :unix_uid, :status, | |
108 | :mit_id; | |
109 | if (sqlca.sqlcode) | |
110 | break; | |
111 | strtrim(login); | |
112 | strtrim(mit_id); | |
113 | ||
65382c46 | 114 | EXEC SQL SELECT filsys_id into :fid |
115 | FROM filesys | |
116 | WHERE lockertype = 'HOMEDIR' | |
117 | AND label = :login | |
118 | AND type = 'FSGROUP'; | |
c31c844d | 119 | |
120 | if (sqlca.sqlcode == 0) | |
121 | { | |
65382c46 | 122 | EXEC SQL DECLARE f_cursor CURSOR FOR |
123 | SELECT type, name | |
124 | FROM filesys a, fsgroup b | |
125 | WHERE a.filsys_id=b.filsys_id | |
126 | AND b.group_id=:fid | |
127 | ORDER by key; | |
128 | ||
129 | EXEC SQL OPEN f_cursor; | |
130 | ||
131 | EXEC SQL FETCH f_cursor INTO :type, :name; | |
132 | ||
133 | if (sqlca.sqlcode == 0) | |
134 | { | |
135 | strtrim(type); | |
136 | strtrim(name); | |
137 | } | |
138 | else | |
139 | { | |
140 | strcpy(type, ""); | |
141 | strcpy(name, ""); | |
142 | } | |
143 | ||
144 | EXEC SQL CLOSE f_cursor; | |
c31c844d | 145 | } |
65382c46 | 146 | |
147 | ||
c31c844d | 148 | else |
149 | { | |
65382c46 | 150 | EXEC SQL SELECT type, name into :type, :name |
151 | FROM filesys | |
152 | WHERE lockertype = 'HOMEDIR' | |
153 | AND label=:login; | |
154 | ||
155 | if (sqlca.sqlcode == 0) | |
156 | { | |
157 | strtrim(type); | |
158 | strtrim(name); | |
159 | } | |
160 | else | |
161 | { | |
162 | strcpy(type, ""); | |
163 | strcpy(name, ""); | |
164 | } | |
c31c844d | 165 | } |
65382c46 | 166 | |
c31c844d | 167 | fprintf(fout, "%d %s %d %d %s %s %s\n", |
65382c46 | 168 | users_id, login, unix_uid, status, mit_id, |
169 | type, name); | |
170 | } | |
171 | ||
c31c844d | 172 | if (sqlca.sqlcode < 0) |
173 | db_error(sqlca.sqlcode); | |
174 | EXEC SQL CLOSE u_cursor; | |
175 | EXEC SQL COMMIT; | |
176 | ||
177 | if (fclose(fout)) | |
178 | { | |
179 | fprintf(stderr, "Unsuccessful file close of winuser.db\n"); | |
180 | exit(MR_CCONFIG); | |
181 | } | |
182 | ||
183 | fix_file(foutf); | |
184 | ||
185 | return 1; | |
186 | } | |
187 | ||
188 | int do_groups(void) | |
189 | { | |
190 | FILE *fout; | |
191 | char foutf[MAXPATHLEN]; | |
192 | char foutft[MAXPATHLEN]; | |
193 | EXEC SQL BEGIN DECLARE SECTION; | |
194 | char listname[LIST_NAME_SIZE]; | |
195 | char description[LIST_DESCRIPTION_SIZE]; | |
196 | char acltype[LIST_ACL_TYPE_SIZE]; | |
197 | int aclid; | |
198 | char aclname[STRINGS_STRING_SIZE]; | |
199 | int list_id, active, maillist, grouplist; | |
200 | EXEC SQL END DECLARE SECTION; | |
201 | ||
202 | sprintf(foutf, "%s/wingroup.db", winad_dir); | |
203 | sprintf(foutft, "%s~", foutf); | |
204 | ||
205 | fout = fopen(foutft, "w"); | |
206 | if (!fout) | |
207 | { | |
208 | perror("cannot open wingroup.db for write"); | |
209 | exit(MR_OCONFIG); | |
210 | } | |
211 | ||
212 | EXEC SQL DECLARE l_cursor CURSOR FOR | |
213 | SELECT list_id, name, active, maillist, grouplist, description, | |
214 | acl_type, acl_id | |
215 | FROM list | |
216 | ORDER BY list_id; | |
217 | EXEC SQL OPEN l_cursor; | |
218 | while (1) | |
219 | { | |
220 | EXEC SQL FETCH l_cursor INTO :list_id, :listname, :active, :maillist, | |
221 | :grouplist, | |
222 | :description, :acltype, :aclid; | |
223 | ||
224 | if (sqlca.sqlcode) | |
225 | break; | |
226 | ||
227 | strtrim(listname); | |
228 | strtrim(description); | |
229 | strtrim(acltype); | |
230 | ||
231 | ||
232 | strcpy(aclname, ""); | |
233 | if (strcmp(acltype, "LIST") == 0) | |
234 | { | |
235 | EXEC SQL SELECT name into :aclname | |
236 | FROM list | |
237 | WHERE list_id = :aclid; | |
238 | } | |
239 | else if (strcmp(acltype, "USER") == 0) | |
240 | { | |
241 | EXEC SQL SELECT login into :aclname | |
242 | FROM users | |
243 | WHERE users_id = :aclid; | |
244 | } | |
245 | else if (strcmp(acltype, "KERBEROS") == 0) | |
246 | { | |
247 | EXEC SQL SELECT string into :aclname | |
248 | FROM strings | |
249 | WHERE string_id = :aclid; | |
250 | } | |
251 | ||
252 | strtrim(aclname); | |
253 | ||
254 | fprintf(fout, "%d %s %d %d %d %s %s %s\n", | |
255 | list_id, listname, active, maillist, grouplist, acltype, aclname, | |
256 | description); | |
257 | } | |
258 | ||
259 | if (sqlca.sqlcode < 0) | |
260 | db_error(sqlca.sqlcode); | |
261 | EXEC SQL CLOSE l_cursor; | |
262 | EXEC SQL COMMIT; | |
263 | ||
264 | if (fclose(fout)) | |
265 | { | |
266 | fprintf(stderr, "Unsuccessful file close of wingroup.db\n"); | |
267 | exit(MR_CCONFIG); | |
268 | } | |
269 | ||
270 | fix_file(foutf); | |
271 | return 1; | |
272 | } | |
273 | ||
274 | int do_groupmembership(void) | |
275 | { | |
276 | FILE *fout; | |
277 | char foutf[MAXPATHLEN]; | |
278 | char foutft[MAXPATHLEN]; | |
279 | EXEC SQL BEGIN DECLARE SECTION; | |
280 | char member_type[IMEMBERS_MEMBER_TYPE_SIZE]; | |
281 | char member_name[STRINGS_STRING_SIZE]; | |
282 | int list_id; | |
283 | EXEC SQL END DECLARE SECTION; | |
284 | ||
285 | sprintf(foutf, "%s/wingmember.db", winad_dir); | |
286 | sprintf(foutft, "%s~", foutf); | |
287 | ||
288 | fout = fopen(foutft, "w"); | |
289 | if (!fout) | |
290 | { | |
291 | perror("cannot open wingmember.db for write"); | |
292 | exit(MR_OCONFIG); | |
293 | } | |
294 | ||
295 | EXEC SQL DECLARE list_cursor CURSOR FOR | |
296 | SELECT list_id | |
297 | FROM list | |
e2a7bf03 | 298 | WHERE active != 0 |
c31c844d | 299 | ORDER BY list_id; |
300 | EXEC SQL OPEN list_cursor; | |
301 | while (1) | |
302 | { | |
303 | EXEC SQL FETCH list_cursor INTO :list_id; | |
304 | ||
305 | if (sqlca.sqlcode) | |
306 | break; | |
307 | ||
308 | /* get all the users */ | |
309 | EXEC SQL DECLARE csr001 CURSOR FOR | |
310 | SELECT i.member_type, u.login | |
311 | FROM users u, imembers i | |
312 | WHERE i.list_id = :list_id AND i.member_type = 'USER' | |
313 | AND i.member_id = u.users_id | |
314 | ORDER BY u.login; | |
315 | ||
316 | EXEC SQL OPEN csr001; | |
317 | while(1) | |
318 | { | |
319 | EXEC SQL FETCH csr001 into :member_type, :member_name; | |
320 | if (sqlca.sqlcode) | |
321 | break; | |
322 | fprintf(fout, "%d %s %s\n", | |
323 | list_id, member_type, member_name); | |
324 | } | |
325 | ||
326 | if (sqlca.sqlcode < 0) | |
327 | db_error(sqlca.sqlcode); | |
328 | EXEC SQL CLOSE csr001; | |
329 | ||
330 | /* get all the KERBEROS AND STRINGS */ | |
331 | EXEC SQL DECLARE csr002 CURSOR FOR | |
332 | SELECT i.member_type, s.string | |
333 | FROM strings s, imembers i | |
334 | WHERE i.list_id = :list_id AND | |
335 | (i.member_type = 'KERBEROS' OR i.member_type = 'STRING') | |
336 | AND i.member_id = s.string_id | |
337 | ORDER BY s.string; | |
338 | ||
339 | EXEC SQL OPEN csr002; | |
340 | while(1) | |
341 | { | |
342 | EXEC SQL FETCH csr002 into :member_type, :member_name; | |
343 | if (sqlca.sqlcode) | |
344 | break; | |
345 | fprintf(fout, "%d %s %s\n", | |
346 | list_id, member_type, member_name); | |
347 | } | |
348 | ||
349 | if (sqlca.sqlcode < 0) | |
350 | db_error(sqlca.sqlcode); | |
351 | ||
352 | EXEC SQL CLOSE csr002; | |
353 | } | |
354 | ||
355 | if (sqlca.sqlcode < 0) | |
356 | db_error(sqlca.sqlcode); | |
357 | ||
358 | EXEC SQL CLOSE list_cursor; | |
359 | EXEC SQL COMMIT; | |
360 | ||
361 | if (fclose(fout)) | |
362 | { | |
363 | fprintf(stderr, "Unsuccessful file close of wingmember.db\n"); | |
364 | exit(MR_CCONFIG); | |
365 | } | |
366 | ||
367 | fix_file(foutf); | |
368 | return 1; | |
369 | } |