]> andersk Git - moira.git/blame - gen/winad.pc
Add support for get_host_by_account_number query.
[moira.git] / gen / winad.pc
CommitLineData
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
22EXEC SQL INCLUDE sqlca;
23
24#ifndef WINAD_SUBDIR
25#define WINAD_SUBDIR "winad"
26#endif
27
28char winad_dir[MAXPATHLEN];
29char *whoami = "winad.gen";
30char *db = "moira/moira";
31
32int do_user(void);
33int do_groups(void);
34int do_groupmembership(void);
4484634e 35int do_containers(void);
c31c844d 36
37int main(int argc, char **argv)
38{
39 char cmd[64];
40 struct stat sb;
41 int changed = 0;
42
43 if (argc > 2)
44 {
45 fprintf(stderr, "usage: %s [outfile]\n", argv[0]);
46 exit(MR_ARGS);
47 }
48
49 initialize_sms_error_table();
50 sprintf(winad_dir, "%s/%s", DCM_DIR, WINAD_SUBDIR);
51
52 EXEC SQL CONNECT :db;
53
54 changed = do_user();
55 changed += do_groups();
56 changed += do_groupmembership();
4484634e 57 changed += do_containers();
c31c844d 58
59 if (!changed)
60 {
61 fprintf(stderr, "No files updated.\n");
62 if (argc == 2 && stat(argv[1], &sb) == 0)
63 exit(MR_NO_CHANGE);
64 }
65
66 if (argc == 2)
67 {
68 fprintf(stderr, "Building tar file.\n");
69 sprintf(cmd, "cd %s; tar cf %s .", winad_dir, argv[1]);
70 if (system(cmd))
71 exit(MR_TAR_FAIL);
72 }
73
74 exit(MR_SUCCESS);
75}
76
77
78int do_user(void)
79{
80 FILE *fout;
81 char foutf[MAXPATHLEN];
82 char foutft[MAXPATHLEN];
83 EXEC SQL BEGIN DECLARE SECTION;
84 char login[USERS_LOGIN_SIZE];
85 char mit_id[USERS_CLEARID_SIZE];
86 int users_id, unix_uid, status;
87 char type[FILESYS_TYPE_SIZE];
88 char name[FILESYS_NAME_SIZE];
65382c46 89 int fid;
c31c844d 90 EXEC SQL END DECLARE SECTION;
91
92 sprintf(foutf, "%s/winuser.db", winad_dir);
93 sprintf(foutft, "%s~", foutf);
94
95 fout = fopen(foutft, "w");
96 if (!fout)
97 {
98 perror("cannot open winuser.db for write");
99 exit(MR_OCONFIG);
100 }
101
102 EXEC SQL DECLARE u_cursor CURSOR FOR
103 SELECT users_id, login, unix_uid, status, clearid
104 FROM users
105 ORDER BY users_id;
106 EXEC SQL OPEN u_cursor;
107 while (1)
108 {
109 EXEC SQL FETCH u_cursor INTO :users_id, :login, :unix_uid, :status,
110:mit_id;
111 if (sqlca.sqlcode)
112 break;
113 strtrim(login);
114 strtrim(mit_id);
115
65382c46 116 EXEC SQL SELECT filsys_id into :fid
117 FROM filesys
118 WHERE lockertype = 'HOMEDIR'
119 AND label = :login
120 AND type = 'FSGROUP';
c31c844d 121
122 if (sqlca.sqlcode == 0)
123 {
65382c46 124 EXEC SQL DECLARE f_cursor CURSOR FOR
125 SELECT type, name
126 FROM filesys a, fsgroup b
127 WHERE a.filsys_id=b.filsys_id
128 AND b.group_id=:fid
129 ORDER by key;
130
131 EXEC SQL OPEN f_cursor;
132
133 EXEC SQL FETCH f_cursor INTO :type, :name;
134
135 if (sqlca.sqlcode == 0)
136 {
137 strtrim(type);
138 strtrim(name);
139 }
140 else
141 {
4484634e 142 strcpy(type, "NONE");
143 strcpy(name, "NONE");
65382c46 144 }
145
146 EXEC SQL CLOSE f_cursor;
c31c844d 147 }
65382c46 148
149
c31c844d 150 else
151 {
65382c46 152 EXEC SQL SELECT type, name into :type, :name
153 FROM filesys
154 WHERE lockertype = 'HOMEDIR'
155 AND label=:login;
156
157 if (sqlca.sqlcode == 0)
158 {
159 strtrim(type);
160 strtrim(name);
161 }
162 else
163 {
4484634e 164 strcpy(type, "NONE");
165 strcpy(name, "NONE");
65382c46 166 }
c31c844d 167 }
65382c46 168
c31c844d 169 fprintf(fout, "%d %s %d %d %s %s %s\n",
65382c46 170 users_id, login, unix_uid, status, mit_id,
171 type, name);
172 }
173
c31c844d 174 if (sqlca.sqlcode < 0)
175 db_error(sqlca.sqlcode);
176 EXEC SQL CLOSE u_cursor;
177 EXEC SQL COMMIT;
178
179 if (fclose(fout))
180 {
181 fprintf(stderr, "Unsuccessful file close of winuser.db\n");
182 exit(MR_CCONFIG);
183 }
184
185 fix_file(foutf);
186
187 return 1;
188}
189
190int do_groups(void)
191{
192 FILE *fout;
193 char foutf[MAXPATHLEN];
194 char foutft[MAXPATHLEN];
195 EXEC SQL BEGIN DECLARE SECTION;
196 char listname[LIST_NAME_SIZE];
197 char description[LIST_DESCRIPTION_SIZE];
198 char acltype[LIST_ACL_TYPE_SIZE];
199 int aclid;
200 char aclname[STRINGS_STRING_SIZE];
201 int list_id, active, maillist, grouplist;
202 EXEC SQL END DECLARE SECTION;
203
204 sprintf(foutf, "%s/wingroup.db", winad_dir);
205 sprintf(foutft, "%s~", foutf);
206
207 fout = fopen(foutft, "w");
208 if (!fout)
209 {
210 perror("cannot open wingroup.db for write");
211 exit(MR_OCONFIG);
212 }
213
214 EXEC SQL DECLARE l_cursor CURSOR FOR
215 SELECT list_id, name, active, maillist, grouplist, description,
216acl_type, acl_id
217 FROM list
218 ORDER BY list_id;
219 EXEC SQL OPEN l_cursor;
220 while (1)
221 {
222 EXEC SQL FETCH l_cursor INTO :list_id, :listname, :active, :maillist,
223:grouplist,
224 :description, :acltype, :aclid;
225
226 if (sqlca.sqlcode)
227 break;
228
229 strtrim(listname);
230 strtrim(description);
231 strtrim(acltype);
232
233
4484634e 234 strcpy(aclname, "NONE");
c31c844d 235 if (strcmp(acltype, "LIST") == 0)
236 {
237 EXEC SQL SELECT name into :aclname
238 FROM list
239 WHERE list_id = :aclid;
240 }
241 else if (strcmp(acltype, "USER") == 0)
242 {
243 EXEC SQL SELECT login into :aclname
244 FROM users
245 WHERE users_id = :aclid;
246 }
247 else if (strcmp(acltype, "KERBEROS") == 0)
248 {
249 EXEC SQL SELECT string into :aclname
250 FROM strings
251 WHERE string_id = :aclid;
252 }
253
254 strtrim(aclname);
255
256 fprintf(fout, "%d %s %d %d %d %s %s %s\n",
257 list_id, listname, active, maillist, grouplist, acltype, aclname,
258description);
259 }
260
261 if (sqlca.sqlcode < 0)
262 db_error(sqlca.sqlcode);
263 EXEC SQL CLOSE l_cursor;
264 EXEC SQL COMMIT;
265
266 if (fclose(fout))
267 {
268 fprintf(stderr, "Unsuccessful file close of wingroup.db\n");
269 exit(MR_CCONFIG);
270 }
271
272 fix_file(foutf);
273 return 1;
274}
275
276int do_groupmembership(void)
277{
278 FILE *fout;
279 char foutf[MAXPATHLEN];
280 char foutft[MAXPATHLEN];
281 EXEC SQL BEGIN DECLARE SECTION;
282 char member_type[IMEMBERS_MEMBER_TYPE_SIZE];
283 char member_name[STRINGS_STRING_SIZE];
284 int list_id;
285 EXEC SQL END DECLARE SECTION;
286
287 sprintf(foutf, "%s/wingmember.db", winad_dir);
288 sprintf(foutft, "%s~", foutf);
289
290 fout = fopen(foutft, "w");
291 if (!fout)
292 {
293 perror("cannot open wingmember.db for write");
294 exit(MR_OCONFIG);
295 }
296
297 EXEC SQL DECLARE list_cursor CURSOR FOR
298 SELECT list_id
299 FROM list
e2a7bf03 300 WHERE active != 0
c31c844d 301 ORDER BY list_id;
302 EXEC SQL OPEN list_cursor;
303 while (1)
304 {
305 EXEC SQL FETCH list_cursor INTO :list_id;
306
307 if (sqlca.sqlcode)
308 break;
309
310 /* get all the users */
311 EXEC SQL DECLARE csr001 CURSOR FOR
312 SELECT i.member_type, u.login
313 FROM users u, imembers i
314 WHERE i.list_id = :list_id AND i.member_type = 'USER'
315 AND i.member_id = u.users_id
316 ORDER BY u.login;
317
318 EXEC SQL OPEN csr001;
319 while(1)
320 {
321 EXEC SQL FETCH csr001 into :member_type, :member_name;
322 if (sqlca.sqlcode)
323 break;
324 fprintf(fout, "%d %s %s\n",
325 list_id, member_type, member_name);
326 }
327
328 if (sqlca.sqlcode < 0)
329 db_error(sqlca.sqlcode);
330 EXEC SQL CLOSE csr001;
331
332 /* get all the KERBEROS AND STRINGS */
333 EXEC SQL DECLARE csr002 CURSOR FOR
334 SELECT i.member_type, s.string
335 FROM strings s, imembers i
336 WHERE i.list_id = :list_id AND
337 (i.member_type = 'KERBEROS' OR i.member_type = 'STRING')
338 AND i.member_id = s.string_id
339 ORDER BY s.string;
340
341 EXEC SQL OPEN csr002;
342 while(1)
343 {
344 EXEC SQL FETCH csr002 into :member_type, :member_name;
345 if (sqlca.sqlcode)
346 break;
347 fprintf(fout, "%d %s %s\n",
348 list_id, member_type, member_name);
349 }
350
351 if (sqlca.sqlcode < 0)
352 db_error(sqlca.sqlcode);
353
354 EXEC SQL CLOSE csr002;
355 }
356
357 if (sqlca.sqlcode < 0)
358 db_error(sqlca.sqlcode);
359
360 EXEC SQL CLOSE list_cursor;
361 EXEC SQL COMMIT;
362
363 if (fclose(fout))
364 {
365 fprintf(stderr, "Unsuccessful file close of wingmember.db\n");
366 exit(MR_CCONFIG);
367 }
368
369 fix_file(foutf);
370 return 1;
371}
4484634e 372
373int do_containers(void)
374{
375 FILE *fout;
376 char foutf[MAXPATHLEN];
377 char foutft[MAXPATHLEN];
378 EXEC SQL BEGIN DECLARE SECTION;
379 char container_name[CONTAINERS_NAME_SIZE];
380 char acl_type[CONTAINERS_ACL_TYPE_SIZE];
381 char acl_name[STRINGS_STRING_SIZE];
382 char description[CONTAINERS_DESCRIPTION_SIZE];
383 int cnt_id;
384 int acl_id;
385 EXEC SQL END DECLARE SECTION;
386
387 sprintf(foutf, "%s/wincontainer.db", winad_dir);
388 sprintf(foutft, "%s~", foutf);
389
390 fout = fopen(foutft, "w");
391 if (!fout)
392 {
393 perror("cannot open wincontainer.db for write");
394 exit(MR_OCONFIG);
395 }
396
397 EXEC SQL DECLARE container_cursor CURSOR FOR
398 SELECT name, cnt_id, acl_type, acl_id, description
399 FROM containers
400 ORDER BY cnt_id, name;
401 EXEC SQL OPEN container_cursor;
402 while (1)
403 {
404 EXEC SQL FETCH container_cursor INTO :container_name, :cnt_id,
405 :acl_type, :acl_id, :description ;
406
407 if (sqlca.sqlcode)
408 break;
409
410 strtrim(container_name);
411 strtrim(acl_type);
412 strtrim(description);
413
414 strcpy(acl_name, "NONE");
415 if (strcmp(acl_type, "LIST") == 0)
416 {
417 EXEC SQL SELECT name into :acl_name
418 FROM list
419 WHERE list_id = :acl_id;
420 }
421 else if (strcmp(acl_type, "USER") == 0)
422 {
423 EXEC SQL SELECT login into :acl_name
424 FROM users
425 WHERE users_id = :acl_id;
426 }
427 else if (strcmp(acl_type, "KERBEROS") == 0)
428 {
429 EXEC SQL SELECT string into :acl_name
430 FROM strings
431 WHERE string_id = :acl_id;
432 }
433
434 strtrim(acl_name);
435
436 fprintf(fout, "%d,%s,%s,%s,%s\n",
437 cnt_id, container_name, acl_type, acl_name,
438 description);
439 }
440 if (sqlca.sqlcode < 0)
441 db_error(sqlca.sqlcode);
442
443 EXEC SQL CLOSE container_cursor;
444 EXEC SQL COMMIT;
445
446 if (fclose(fout))
447 {
448 fprintf(stderr, "Unsuccessful file close of wincontainer.db\n");
449 exit(MR_CCONFIG);
450 }
451
452 fix_file(foutf);
453 return 1;
454}
455
This page took 1.170294 seconds and 5 git commands to generate.