source: trunk/third/moira/gen/winad.pc @ 24319

Revision 24319, 10.9 KB checked in by broder, 14 years ago (diff)
New Moira snapshot from SVN.
Line 
1/* $Id: winad.pc 3956 2010-01-05 20:56:56Z zacheiss $
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);
35int do_containers(void);
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();
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
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];
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
214int 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, publicflg, hidden, 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, publicflg, hidden, maillist,
240    grouplist, description, 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, :publicflg,
247        :hidden, :maillist, :grouplist, :description, :acltype, :aclid;
248     
249      if (sqlca.sqlcode)
250              break;
251
252      strtrim(listname);
253      strtrim(description);
254      strtrim(acltype);
255
256     
257      strcpy(aclname, "NONE");
258      if (strcmp(acltype, "LIST") == 0)
259      {
260        EXEC SQL SELECT name into :aclname
261        FROM list
262        WHERE list_id = :aclid;
263      }
264      else if (strcmp(acltype, "USER") == 0)
265      {
266        EXEC SQL SELECT login into :aclname
267        FROM users
268        WHERE users_id = :aclid;
269      }
270      else if (strcmp(acltype, "KERBEROS") == 0)
271      {
272        EXEC SQL SELECT string into :aclname
273        FROM strings
274        WHERE string_id = :aclid;
275      }
276     
277      strtrim(aclname);
278       
279      fprintf(fout, "%d %s %d %d %d %d %d %s %s %s\n",
280                    list_id, listname, active, publicflg, hidden, maillist,
281              grouplist, acltype, aclname, description);
282        }
283
284  if (sqlca.sqlcode < 0)
285    db_error(sqlca.sqlcode);
286  EXEC SQL CLOSE l_cursor;
287  EXEC SQL COMMIT;
288
289  if (fclose(fout))
290  {
291    fprintf(stderr, "Unsuccessful file close of wingroup.db\n");
292    exit(MR_CCONFIG);
293  }
294
295  fix_file(foutf);
296  return 1;
297}
298
299int do_groupmembership(void)
300{
301  FILE *fout;
302  char foutf[MAXPATHLEN];
303  char foutft[MAXPATHLEN];
304  EXEC SQL BEGIN DECLARE SECTION;
305  char member_type[IMEMBERS_MEMBER_TYPE_SIZE];
306  char member_name[STRINGS_STRING_SIZE];
307  int list_id;
308  EXEC SQL END DECLARE SECTION;
309
310  sprintf(foutf, "%s/wingmember.db", winad_dir);
311  sprintf(foutft, "%s~", foutf);
312
313  fout = fopen(foutft, "w");
314  if (!fout)
315  {
316    perror("cannot open wingmember.db for write");
317    exit(MR_OCONFIG);
318  }
319
320  EXEC SQL DECLARE list_cursor CURSOR FOR
321    SELECT list_id
322    FROM list
323    WHERE active != 0
324    ORDER BY list_id;
325  EXEC SQL OPEN list_cursor;
326  while (1)
327    {
328      EXEC SQL FETCH list_cursor INTO :list_id;
329     
330      if (sqlca.sqlcode)
331              break;
332     
333      /* get all the users */
334      EXEC SQL DECLARE csr001 CURSOR FOR
335        SELECT i.member_type, u.login
336        FROM users u, imembers i
337        WHERE i.list_id = :list_id AND i.member_type = 'USER'
338        AND i.member_id = u.users_id
339        ORDER BY u.login;
340
341      EXEC SQL OPEN csr001;
342      while(1)
343      {
344        EXEC SQL FETCH csr001 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      EXEC SQL CLOSE csr001;
354
355      /* get all the KERBEROS AND STRINGS */
356      EXEC SQL DECLARE csr002 CURSOR FOR
357        SELECT i.member_type, s.string
358        FROM strings s, imembers i
359        WHERE i.list_id = :list_id AND
360        (i.member_type = 'KERBEROS' OR i.member_type = 'STRING')
361        AND i.member_id = s.string_id
362        ORDER BY s.string;
363
364      EXEC SQL OPEN csr002;
365      while(1)
366      {
367        EXEC SQL FETCH csr002 into :member_type, :member_name;
368        if (sqlca.sqlcode)
369          break;
370        fprintf(fout, "%d %s %s\n",
371                      list_id, member_type, member_name);
372      }
373
374      if (sqlca.sqlcode < 0)
375        db_error(sqlca.sqlcode);
376
377      EXEC SQL CLOSE csr002;
378
379      /* get all the machines */
380      EXEC SQL DECLARE csr003 CURSOR FOR
381        SELECT i.member_type, m.name
382        FROM machine m, imembers i
383        WHERE i.list_id = :list_id AND i.member_type = 'MACHINE'
384        AND i.member_id = m.mach_id
385        ORDER BY m.name;
386
387      EXEC SQL OPEN csr003;
388      while (1)
389        {
390          EXEC SQL FETCH csr003 into :member_type, :member_name;
391          if (sqlca.sqlcode)
392            break;
393          fprintf(fout, "%d %s %s\n", list_id, member_type, member_name);
394        }
395
396      if (sqlca.sqlcode < 0)
397        db_error(sqlca.sqlcode);
398       EXEC SQL CLOSE csr003;
399    }
400
401  if (sqlca.sqlcode < 0)
402    db_error(sqlca.sqlcode);
403
404  EXEC SQL CLOSE list_cursor;
405  EXEC SQL COMMIT;
406
407  if (fclose(fout))
408  {
409    fprintf(stderr, "Unsuccessful file close of wingmember.db\n");
410    exit(MR_CCONFIG);
411  }
412
413  fix_file(foutf);
414  return 1;
415}
416
417int do_containers(void)
418{
419  FILE *fout;
420  char foutf[MAXPATHLEN];
421  char foutft[MAXPATHLEN];
422  EXEC SQL BEGIN DECLARE SECTION;
423  char container_name[CONTAINERS_NAME_SIZE];
424  char acl_type[CONTAINERS_ACL_TYPE_SIZE];
425  char acl_name[STRINGS_STRING_SIZE];
426  char description[CONTAINERS_DESCRIPTION_SIZE];
427  int cnt_id;
428  int acl_id;
429  EXEC SQL END DECLARE SECTION;
430
431  sprintf(foutf, "%s/wincontainer.db", winad_dir);
432  sprintf(foutft, "%s~", foutf);
433
434  fout = fopen(foutft, "w");
435  if (!fout)
436  {
437    perror("cannot open wincontainer.db for write");
438    exit(MR_OCONFIG);
439  }
440
441  EXEC SQL DECLARE container_cursor CURSOR FOR
442    SELECT name, cnt_id, acl_type, acl_id, description
443    FROM containers
444    ORDER BY cnt_id, name;
445  EXEC SQL OPEN container_cursor;
446  while (1)
447    {
448      EXEC SQL FETCH container_cursor INTO :container_name, :cnt_id,
449      :acl_type, :acl_id, :description ;
450     
451      if (sqlca.sqlcode)
452              break;
453     
454      strtrim(container_name);
455      strtrim(acl_type);
456      strtrim(description);
457
458      strcpy(acl_name, "NONE");
459      if (strcmp(acl_type, "LIST") == 0)
460      {
461        EXEC SQL SELECT name into :acl_name
462        FROM list
463        WHERE list_id = :acl_id;
464      }
465      else if (strcmp(acl_type, "USER") == 0)
466      {
467        EXEC SQL SELECT login into :acl_name
468        FROM users
469        WHERE users_id = :acl_id;
470      }
471      else if (strcmp(acl_type, "KERBEROS") == 0)
472      {
473        EXEC SQL SELECT string into :acl_name
474        FROM strings
475        WHERE string_id = :acl_id;
476      }
477     
478      strtrim(acl_name);
479       
480      fprintf(fout, "%d,%s,%s,%s,%s\n",
481                    cnt_id, container_name, acl_type, acl_name,
482        description);
483        }
484  if (sqlca.sqlcode < 0)
485    db_error(sqlca.sqlcode);
486
487  EXEC SQL CLOSE container_cursor;
488  EXEC SQL COMMIT;
489
490  if (fclose(fout))
491  {
492    fprintf(stderr, "Unsuccessful file close of wincontainer.db\n");
493    exit(MR_CCONFIG);
494  }
495
496  fix_file(foutf);
497  return 1;
498}
499
Note: See TracBrowser for help on using the repository browser.