]> andersk Git - moira.git/blob - gen/winad.pc
Only care about active lists.
[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
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];
87   EXEC SQL END DECLARE SECTION;
88
89   sprintf(foutf, "%s/winuser.db", winad_dir);
90   sprintf(foutft, "%s~", foutf);
91
92   fout = fopen(foutft, "w");
93   if (!fout)
94   {
95     perror("cannot open winuser.db for write");
96     exit(MR_OCONFIG);
97   }
98
99   EXEC SQL DECLARE u_cursor CURSOR FOR
100     SELECT users_id, login, unix_uid, status, clearid
101     FROM users
102     ORDER BY users_id;
103   EXEC SQL OPEN u_cursor;
104   while (1)
105     {
106       EXEC SQL FETCH u_cursor INTO :users_id, :login, :unix_uid, :status,
107 :mit_id;
108       if (sqlca.sqlcode)
109               break;
110       strtrim(login);
111       strtrim(mit_id);
112       
113       EXEC SQL SELECT type, name INTO :type, :name 
114       FROM filesys 
115       WHERE lockertype = 'HOMEDIR'
116       AND owner = :users_id;
117
118       if (sqlca.sqlcode == 0)
119       {
120         strtrim(type);
121         strtrim(name);
122       }
123       else
124       {
125         strcpy(type, "");
126         strcpy(name, "");
127       }
128       fprintf(fout, "%d %s %d %d %s %s %s\n",
129                     users_id, login, unix_uid, status, mit_id, 
130         type, name);
131         }
132
133   if (sqlca.sqlcode < 0)
134     db_error(sqlca.sqlcode);
135   EXEC SQL CLOSE u_cursor;
136   EXEC SQL COMMIT;
137
138   if (fclose(fout))
139   {
140     fprintf(stderr, "Unsuccessful file close of winuser.db\n");
141     exit(MR_CCONFIG);
142   }
143
144   fix_file(foutf);
145
146   return 1;
147 }
148
149 int do_groups(void)
150 {
151   FILE *fout;
152   char foutf[MAXPATHLEN];
153   char foutft[MAXPATHLEN];
154   EXEC SQL BEGIN DECLARE SECTION;
155   char listname[LIST_NAME_SIZE];
156   char description[LIST_DESCRIPTION_SIZE];
157   char acltype[LIST_ACL_TYPE_SIZE];
158   int aclid;
159   char aclname[STRINGS_STRING_SIZE];
160   int list_id, active, maillist, grouplist;
161   EXEC SQL END DECLARE SECTION;
162
163   sprintf(foutf, "%s/wingroup.db", winad_dir);
164   sprintf(foutft, "%s~", foutf);
165
166   fout = fopen(foutft, "w");
167   if (!fout)
168   {
169     perror("cannot open wingroup.db for write");
170     exit(MR_OCONFIG);
171   }
172
173   EXEC SQL DECLARE l_cursor CURSOR FOR
174     SELECT list_id, name, active, maillist, grouplist, description,
175 acl_type, acl_id
176     FROM list
177     ORDER BY list_id;
178   EXEC SQL OPEN l_cursor;
179   while (1)
180     {
181       EXEC SQL FETCH l_cursor INTO :list_id, :listname, :active, :maillist,
182 :grouplist,
183             :description, :acltype, :aclid;
184       
185       if (sqlca.sqlcode)
186               break;
187
188       strtrim(listname);
189       strtrim(description);
190       strtrim(acltype);
191
192       
193       strcpy(aclname, "");
194       if (strcmp(acltype, "LIST") == 0)
195       {
196         EXEC SQL SELECT name into :aclname
197         FROM list
198         WHERE list_id = :aclid;
199       }
200       else if (strcmp(acltype, "USER") == 0)
201       {
202         EXEC SQL SELECT login into :aclname
203         FROM users
204         WHERE users_id = :aclid;
205       }
206       else if (strcmp(acltype, "KERBEROS") == 0)
207       {
208         EXEC SQL SELECT string into :aclname
209         FROM strings
210         WHERE string_id = :aclid;
211       }
212       
213       strtrim(aclname);
214        
215       fprintf(fout, "%d %s %d %d %d %s %s %s\n",
216                     list_id, listname, active, maillist, grouplist, acltype, aclname,
217 description);
218         }
219
220   if (sqlca.sqlcode < 0)
221     db_error(sqlca.sqlcode);
222   EXEC SQL CLOSE l_cursor;
223   EXEC SQL COMMIT;
224
225   if (fclose(fout))
226   {
227     fprintf(stderr, "Unsuccessful file close of wingroup.db\n");
228     exit(MR_CCONFIG);
229   }
230
231   fix_file(foutf);
232   return 1;
233 }
234
235 int do_groupmembership(void)
236 {
237   FILE *fout;
238   char foutf[MAXPATHLEN];
239   char foutft[MAXPATHLEN];
240   EXEC SQL BEGIN DECLARE SECTION;
241   char member_type[IMEMBERS_MEMBER_TYPE_SIZE];
242   char member_name[STRINGS_STRING_SIZE];
243   int list_id;
244   EXEC SQL END DECLARE SECTION;
245
246   sprintf(foutf, "%s/wingmember.db", winad_dir);
247   sprintf(foutft, "%s~", foutf);
248
249   fout = fopen(foutft, "w");
250   if (!fout)
251   {
252     perror("cannot open wingmember.db for write");
253     exit(MR_OCONFIG);
254   }
255
256   EXEC SQL DECLARE list_cursor CURSOR FOR
257     SELECT list_id
258     FROM list
259     WHERE active != 0
260     ORDER BY list_id;
261   EXEC SQL OPEN list_cursor;
262   while (1)
263     {
264       EXEC SQL FETCH list_cursor INTO :list_id;
265       
266       if (sqlca.sqlcode)
267               break;
268       
269       /* get all the users */
270       EXEC SQL DECLARE csr001 CURSOR FOR
271         SELECT i.member_type, u.login
272         FROM users u, imembers i
273         WHERE i.list_id = :list_id AND i.member_type = 'USER'
274         AND i.member_id = u.users_id 
275         ORDER BY u.login;
276
277       EXEC SQL OPEN csr001;
278       while(1)
279       {
280         EXEC SQL FETCH csr001 into :member_type, :member_name;
281         if (sqlca.sqlcode)
282           break;
283         fprintf(fout, "%d %s %s\n",
284                       list_id, member_type, member_name);
285       }
286
287       if (sqlca.sqlcode < 0)
288         db_error(sqlca.sqlcode);
289       EXEC SQL CLOSE csr001;
290
291       /* get all the KERBEROS AND STRINGS */
292       EXEC SQL DECLARE csr002 CURSOR FOR
293         SELECT i.member_type, s.string
294         FROM strings s, imembers i
295         WHERE i.list_id = :list_id AND 
296         (i.member_type = 'KERBEROS' OR i.member_type = 'STRING')
297         AND i.member_id = s.string_id 
298         ORDER BY s.string;
299
300       EXEC SQL OPEN csr002;
301       while(1)
302       {
303         EXEC SQL FETCH csr002 into :member_type, :member_name;
304         if (sqlca.sqlcode)
305           break;
306         fprintf(fout, "%d %s %s\n",
307                       list_id, member_type, member_name);
308       }
309
310       if (sqlca.sqlcode < 0)
311         db_error(sqlca.sqlcode);
312
313       EXEC SQL CLOSE csr002;
314         }
315
316   if (sqlca.sqlcode < 0)
317     db_error(sqlca.sqlcode);
318
319   EXEC SQL CLOSE list_cursor;
320   EXEC SQL COMMIT;
321
322   if (fclose(fout))
323   {
324     fprintf(stderr, "Unsuccessful file close of wingmember.db\n");
325     exit(MR_CCONFIG);
326   }
327
328   fix_file(foutf);
329   return 1;
330 }
This page took 0.061964 seconds and 5 git commands to generate.