]> andersk Git - moira.git/blob - gen/warehouse-lists.pc
*** empty log message ***
[moira.git] / gen / warehouse-lists.pc
1 /* $Id$
2  *
3  * (c) Copyright 2008 by the Massachusetts Institute of Technology.
4  */
5
6 #include <mit-copyright.h>
7 #include <moira.h>
8 #include <moira_site.h>
9
10 #include <sys/stat.h>
11
12 #include <ctype.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16
17 #include "util.h"
18
19 EXEC SQL INCLUDE sqlca;
20
21 char *whoami = "warehouse-lists.gen";
22 char *db = "moira/moira";
23
24 struct hash *lists;
25
26 void output_list(int id, void *list, void *out);
27
28 int main(int argc, char **argv)
29 {
30   char filename[MAXPATHLEN], *targetfile, *l;
31   FILE *out = stdout;
32   int cnt = 0;
33   EXEC SQL BEGIN DECLARE SECTION;
34   int lid;
35   char lname[LIST_NAME_SIZE];
36   EXEC SQL END DECLARE SECTION;
37
38   EXEC SQL CONNECT :db;
39
40   if (argc == 2)
41     {
42       targetfile = argv[1];
43       sprintf(filename, "%s~", targetfile);
44       if (!(out = fopen(filename, "w")))
45         {
46           fprintf(stderr, "unable to open %s for output\n", filename);
47           exit(MR_OCONFIG);
48         }
49     }
50   else if (argc != 1)
51     {
52       fprintf(stderr, "usage: %s [outfile]\n", argv[0]);
53       exit(MR_ARGS);
54     }
55   
56   lists = create_hash(15000);
57
58   EXEC SQL DECLARE l_cursor CURSOR FOR
59     SELECT l.list_id, l.name FROM list l
60     WHERE l.active = 1 and l.hidden = 0;
61   EXEC SQL OPEN l_cursor;
62   while (1)
63     {
64        EXEC SQL FETCH l_cursor INTO :lid, :lname;
65        if (sqlca.sqlcode)
66          break;
67        l = strdup(strtrim(lname));
68        if (hash_store(lists, lid, l) < 0)
69          {
70            fprintf(stderr, "Out of memory!\n");
71            exit(MR_NO_MEM);
72          }
73        cnt++;
74     }
75   EXEC SQL CLOSE l_cursor;
76   fprintf(stderr, "Loaded %d lists\n", cnt);
77
78   hash_step(lists, output_list, out);
79   
80   if (fclose(out))
81     {
82       perror("close failed");
83       exit(MR_CCONFIG);
84     }
85   
86   if (argc == 2)
87     fix_file(targetfile);
88   exit(MR_SUCCESS);
89 }
90
91 void output_list(int id, void *list, void *out)
92 {
93   EXEC SQL BEGIN DECLARE SECTION;
94   char *l = list;
95   int lid = id;
96   char acl_type[LIST_ACL_TYPE_SIZE], modtime[LIST_MODTIME_SIZE];
97   char acl_name[STRINGS_STRING_SIZE], login[USERS_LOGIN_SIZE];
98   char principal[STRINGS_STRING_SIZE];
99   int acl_id;
100   EXEC SQL END DECLARE SECTION;
101   char *maybecomma = "";
102
103   EXEC SQL SELECT acl_type, acl_id, modtime INTO :acl_type, :acl_id, :modtime
104     FROM list WHERE list_id = :lid;
105
106   strtrim(acl_type);
107   strtrim(modtime);
108   
109   strcpy(acl_name, "NONE");
110   if (strcmp(acl_type, "LIST") == 0)
111     EXEC SQL SELECT name into :acl_name FROM list WHERE list_id = :acl_id;
112   else if (strcmp(acl_type, "USER") == 0)
113     EXEC SQL SELECT login into :acl_name FROM users WHERE users_id = :acl_id;
114   else if (strcmp(acl_type, "KERBEROS") == 0)
115     EXEC SQL SELECT string into :acl_name FROM strings WHERE string_id = :acl_id;
116   strtrim(acl_name);
117
118   fprintf(out, "%s|%s|%s|", list, acl_type, acl_name);
119
120   EXEC SQL DECLARE u_cursor CURSOR FOR
121     SELECT UNIQUE u.login FROM users u, imembers i, list l
122     WHERE l.list_id = :lid AND l.list_id = i.list_id AND
123     i.member_type = 'USER' AND i.member_id = u.users_id;
124   EXEC SQL OPEN u_cursor;
125   while (1)
126     {
127       EXEC SQL FETCH u_cursor INTO :login;
128       if (sqlca.sqlcode)
129         break;
130       fprintf(out, "%s%s", maybecomma, strtrim(login));
131       maybecomma = ",";
132     }
133   EXEC SQL CLOSE u_cursor;
134
135   EXEC SQL DECLARE k_cursor CURSOR FOR
136     SELECT UNIQUE s.string FROM strings s, imembers i, list l
137     WHERE l.list_id = :lid AND l.list_id = i.list_id AND
138     (i.member_type = 'KERBEROS' OR i.member_type = 'STRING')
139     and i.member_id = s.string_id;
140   EXEC SQL OPEN k_cursor;
141   while (1)
142     {
143       EXEC SQL FETCH k_cursor INTO :principal;
144       if (sqlca.sqlcode)
145         break;
146        fprintf(out, "%s%s", maybecomma, strtrim(principal));
147        maybecomma = ",";
148     }
149   EXEC SQL CLOSE k_cursor;
150
151   fprintf(out, "|%s\n", modtime);
152 }
153
This page took 0.075353 seconds and 5 git commands to generate.