]> andersk Git - moira.git/blob - gen/winad.pc
break out of case statement, don't fall through.
[moira.git] / gen / winad.pc
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 int do_containers(void);
36
37 int 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();
57   changed += do_containers();
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
78 int 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];
89   int fid;
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       
116       EXEC SQL SELECT filsys_id into :fid
117         FROM filesys
118         WHERE lockertype = 'HOMEDIR' 
119         AND label = :login
120         AND type = 'FSGROUP';
121
122       if (sqlca.sqlcode == 0)
123       {
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         {
142           strcpy(type, "NONE");
143           strcpy(name, "NONE");
144         }
145
146         EXEC SQL CLOSE f_cursor;
147       }
148         
149
150       else
151       {
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         {
164           strcpy(type, "NONE");
165           strcpy(name, "NONE");
166         }
167       }
168   
169       fprintf(fout, "%d %s %d %d %s %s %s\n",
170                       users_id, login, unix_uid, status, mit_id, 
171           type, name);
172     }
173   
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
190 int 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,
216 acl_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       
234       strcpy(aclname, "NONE");
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,
258 description);
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
276 int 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
300     WHERE active != 0
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 }
372
373 int 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 0.067486 seconds and 5 git commands to generate.