]> andersk Git - moira.git/blob - gen/winad.pc
Fixes to previous revision.
[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   char homedir[USERS_WINHOMEDIR_SIZE];
90   char profiledir[USERS_WINPROFILEDIR_SIZE];
91   int fid;
92   EXEC SQL END DECLARE SECTION;
93
94   sprintf(foutf, "%s/winuser.db", winad_dir);
95   sprintf(foutft, "%s~", foutf);
96
97   fout = fopen(foutft, "w");
98   if (!fout)
99   {
100     perror("cannot open winuser.db for write");
101     exit(MR_OCONFIG);
102   }
103
104   EXEC SQL DECLARE u_cursor CURSOR FOR
105     SELECT users_id, login, unix_uid, status, clearid, winhomedir,
106     winprofiledir
107     FROM users
108     ORDER BY users_id;
109   EXEC SQL OPEN u_cursor;
110   while (1)
111     {
112       EXEC SQL FETCH u_cursor INTO :users_id, :login, :unix_uid, :status,
113 :mit_id, :homedir, :profiledir;
114       if (sqlca.sqlcode)
115               break;
116       strtrim(login);
117       strtrim(mit_id);
118       strtrim(homedir);
119       strtrim(profiledir);
120
121       if (strcmp(mit_id, "") == 0)
122         strcpy(mit_id, "0");
123
124       if (strcasecmp(homedir, "[AFS]") == 0 || strcasecmp(profiledir,
125                                                           "[AFS]") == 0)
126         {
127           EXEC SQL SELECT filsys_id into :fid
128             FROM filesys
129             WHERE lockertype = 'HOMEDIR' 
130             AND label = :login
131             AND type = 'FSGROUP';
132           
133           if (sqlca.sqlcode == 0)
134             {
135               EXEC SQL DECLARE f_cursor CURSOR FOR
136                 SELECT type, name 
137                 FROM filesys a, fsgroup b
138                 WHERE a.filsys_id=b.filsys_id 
139                 AND b.group_id=:fid
140                 ORDER by key;
141               
142               EXEC SQL OPEN f_cursor;
143               
144               EXEC SQL FETCH f_cursor INTO :type, :name;
145               
146               if (sqlca.sqlcode == 0)
147                 {
148                   strtrim(type);
149                   strtrim(name);
150                 }
151               else
152                 {
153                   strcpy(type, "NONE");
154                   strcpy(name, "NONE");
155                 }
156               
157               EXEC SQL CLOSE f_cursor;
158             }
159
160           else
161             {
162               EXEC SQL SELECT type, name into :type, :name
163                 FROM filesys
164                 WHERE lockertype = 'HOMEDIR' 
165                 AND label=:login;
166               
167               if (sqlca.sqlcode == 0)
168                 {
169                   strtrim(type);
170                   strtrim(name);
171                 }
172               else
173                 {
174                   strcpy(type, "NONE");
175                   strcpy(name, "NONE");
176                 }
177             }
178           if (strcasecmp(type, "AFS") != 0)
179             strcpy(name, "[LOCAL]");
180
181         }
182
183       if (strcasecmp(homedir, "[AFS]") == 0)
184         strcpy(homedir, name);
185
186       if (strcasecmp(profiledir, "[AFS]") == 0)
187         {
188           strcpy(profiledir, name);
189           if (strcasecmp(name, "[LOCAL]"))
190             strcat(profiledir, "/.winprofile");
191         }
192   
193       fprintf(fout, "%d %s %d %d %s %s %s\n",
194                       users_id, login, unix_uid, status, mit_id, 
195               homedir, profiledir);
196     }
197   
198   if (sqlca.sqlcode < 0)
199     db_error(sqlca.sqlcode);
200   EXEC SQL CLOSE u_cursor;
201   EXEC SQL COMMIT;
202
203   if (fclose(fout))
204   {
205     fprintf(stderr, "Unsuccessful file close of winuser.db\n");
206     exit(MR_CCONFIG);
207   }
208
209   fix_file(foutf);
210
211   return 1;
212 }
213
214 int do_groups(void)
215 {
216   FILE *fout;
217   char foutf[MAXPATHLEN];
218   char foutft[MAXPATHLEN];
219   EXEC SQL BEGIN DECLARE SECTION;
220   char listname[LIST_NAME_SIZE];
221   char description[LIST_DESCRIPTION_SIZE];
222   char acltype[LIST_ACL_TYPE_SIZE];
223   int aclid;
224   char aclname[STRINGS_STRING_SIZE];
225   int list_id, active, maillist, grouplist;
226   EXEC SQL END DECLARE SECTION;
227
228   sprintf(foutf, "%s/wingroup.db", winad_dir);
229   sprintf(foutft, "%s~", foutf);
230
231   fout = fopen(foutft, "w");
232   if (!fout)
233   {
234     perror("cannot open wingroup.db for write");
235     exit(MR_OCONFIG);
236   }
237
238   EXEC SQL DECLARE l_cursor CURSOR FOR
239     SELECT list_id, name, active, maillist, grouplist, description,
240 acl_type, acl_id
241     FROM list
242     ORDER BY list_id;
243   EXEC SQL OPEN l_cursor;
244   while (1)
245     {
246       EXEC SQL FETCH l_cursor INTO :list_id, :listname, :active, :maillist,
247 :grouplist,
248             :description, :acltype, :aclid;
249       
250       if (sqlca.sqlcode)
251               break;
252
253       strtrim(listname);
254       strtrim(description);
255       strtrim(acltype);
256
257       
258       strcpy(aclname, "NONE");
259       if (strcmp(acltype, "LIST") == 0)
260       {
261         EXEC SQL SELECT name into :aclname
262         FROM list
263         WHERE list_id = :aclid;
264       }
265       else if (strcmp(acltype, "USER") == 0)
266       {
267         EXEC SQL SELECT login into :aclname
268         FROM users
269         WHERE users_id = :aclid;
270       }
271       else if (strcmp(acltype, "KERBEROS") == 0)
272       {
273         EXEC SQL SELECT string into :aclname
274         FROM strings
275         WHERE string_id = :aclid;
276       }
277       
278       strtrim(aclname);
279        
280       fprintf(fout, "%d %s %d %d %d %s %s %s\n",
281                     list_id, listname, active, maillist, grouplist, acltype, aclname,
282 description);
283         }
284
285   if (sqlca.sqlcode < 0)
286     db_error(sqlca.sqlcode);
287   EXEC SQL CLOSE l_cursor;
288   EXEC SQL COMMIT;
289
290   if (fclose(fout))
291   {
292     fprintf(stderr, "Unsuccessful file close of wingroup.db\n");
293     exit(MR_CCONFIG);
294   }
295
296   fix_file(foutf);
297   return 1;
298 }
299
300 int do_groupmembership(void)
301 {
302   FILE *fout;
303   char foutf[MAXPATHLEN];
304   char foutft[MAXPATHLEN];
305   EXEC SQL BEGIN DECLARE SECTION;
306   char member_type[IMEMBERS_MEMBER_TYPE_SIZE];
307   char member_name[STRINGS_STRING_SIZE];
308   int list_id;
309   EXEC SQL END DECLARE SECTION;
310
311   sprintf(foutf, "%s/wingmember.db", winad_dir);
312   sprintf(foutft, "%s~", foutf);
313
314   fout = fopen(foutft, "w");
315   if (!fout)
316   {
317     perror("cannot open wingmember.db for write");
318     exit(MR_OCONFIG);
319   }
320
321   EXEC SQL DECLARE list_cursor CURSOR FOR
322     SELECT list_id
323     FROM list
324     WHERE active != 0
325     ORDER BY list_id;
326   EXEC SQL OPEN list_cursor;
327   while (1)
328     {
329       EXEC SQL FETCH list_cursor INTO :list_id;
330       
331       if (sqlca.sqlcode)
332               break;
333       
334       /* get all the users */
335       EXEC SQL DECLARE csr001 CURSOR FOR
336         SELECT i.member_type, u.login
337         FROM users u, imembers i
338         WHERE i.list_id = :list_id AND i.member_type = 'USER'
339         AND i.member_id = u.users_id 
340         ORDER BY u.login;
341
342       EXEC SQL OPEN csr001;
343       while(1)
344       {
345         EXEC SQL FETCH csr001 into :member_type, :member_name;
346         if (sqlca.sqlcode)
347           break;
348         fprintf(fout, "%d %s %s\n",
349                       list_id, member_type, member_name);
350       }
351
352       if (sqlca.sqlcode < 0)
353         db_error(sqlca.sqlcode);
354       EXEC SQL CLOSE csr001;
355
356       /* get all the KERBEROS AND STRINGS */
357       EXEC SQL DECLARE csr002 CURSOR FOR
358         SELECT i.member_type, s.string
359         FROM strings s, imembers i
360         WHERE i.list_id = :list_id AND 
361         (i.member_type = 'KERBEROS' OR i.member_type = 'STRING')
362         AND i.member_id = s.string_id 
363         ORDER BY s.string;
364
365       EXEC SQL OPEN csr002;
366       while(1)
367       {
368         EXEC SQL FETCH csr002 into :member_type, :member_name;
369         if (sqlca.sqlcode)
370           break;
371         fprintf(fout, "%d %s %s\n",
372                       list_id, member_type, member_name);
373       }
374
375       if (sqlca.sqlcode < 0)
376         db_error(sqlca.sqlcode);
377
378       EXEC SQL CLOSE csr002;
379         }
380
381   if (sqlca.sqlcode < 0)
382     db_error(sqlca.sqlcode);
383
384   EXEC SQL CLOSE list_cursor;
385   EXEC SQL COMMIT;
386
387   if (fclose(fout))
388   {
389     fprintf(stderr, "Unsuccessful file close of wingmember.db\n");
390     exit(MR_CCONFIG);
391   }
392
393   fix_file(foutf);
394   return 1;
395 }
396
397 int do_containers(void)
398 {
399   FILE *fout;
400   char foutf[MAXPATHLEN];
401   char foutft[MAXPATHLEN];
402   EXEC SQL BEGIN DECLARE SECTION;
403   char container_name[CONTAINERS_NAME_SIZE];
404   char acl_type[CONTAINERS_ACL_TYPE_SIZE];
405   char acl_name[STRINGS_STRING_SIZE];
406   char description[CONTAINERS_DESCRIPTION_SIZE];
407   int cnt_id;
408   int acl_id;
409   EXEC SQL END DECLARE SECTION;
410
411   sprintf(foutf, "%s/wincontainer.db", winad_dir);
412   sprintf(foutft, "%s~", foutf);
413
414   fout = fopen(foutft, "w");
415   if (!fout)
416   {
417     perror("cannot open wincontainer.db for write");
418     exit(MR_OCONFIG);
419   }
420
421   EXEC SQL DECLARE container_cursor CURSOR FOR
422     SELECT name, cnt_id, acl_type, acl_id, description
423     FROM containers
424     ORDER BY cnt_id, name;
425   EXEC SQL OPEN container_cursor;
426   while (1)
427     {
428       EXEC SQL FETCH container_cursor INTO :container_name, :cnt_id, 
429       :acl_type, :acl_id, :description ;
430       
431       if (sqlca.sqlcode)
432               break;
433       
434       strtrim(container_name);
435       strtrim(acl_type);
436       strtrim(description);
437
438       strcpy(acl_name, "NONE");
439       if (strcmp(acl_type, "LIST") == 0)
440       {
441         EXEC SQL SELECT name into :acl_name
442         FROM list
443         WHERE list_id = :acl_id;
444       }
445       else if (strcmp(acl_type, "USER") == 0)
446       {
447         EXEC SQL SELECT login into :acl_name
448         FROM users
449         WHERE users_id = :acl_id;
450       }
451       else if (strcmp(acl_type, "KERBEROS") == 0)
452       {
453         EXEC SQL SELECT string into :acl_name
454         FROM strings
455         WHERE string_id = :acl_id;
456       }
457       
458       strtrim(acl_name);
459        
460       fprintf(fout, "%d,%s,%s,%s,%s\n",
461                     cnt_id, container_name, acl_type, acl_name,
462         description);
463         }
464   if (sqlca.sqlcode < 0)
465     db_error(sqlca.sqlcode);
466
467   EXEC SQL CLOSE container_cursor;
468   EXEC SQL COMMIT;
469
470   if (fclose(fout))
471   {
472     fprintf(stderr, "Unsuccessful file close of wincontainer.db\n");
473     exit(MR_CCONFIG);
474   }
475
476   fix_file(foutf);
477   return 1;
478 }
479
This page took 0.071126 seconds and 5 git commands to generate.