source: trunk/third/moira/gen/warehouse-lists.pc @ 25455

Revision 25455, 4.2 KB checked in by jdreed, 12 years ago (diff)
In moira: * Re-snapshot moira at r4073 to pick up new changes to clients; the eunice issue described in the previous entry is no longer relevant
Line 
1/* $Id: warehouse-lists.pc 4052 2011-09-19 14:56:35Z zacheiss $
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
19EXEC SQL INCLUDE sqlca;
20
21char *whoami = "warehouse-lists.gen";
22char *db = "moira/moira";
23
24struct hash *lists;
25
26void output_list(int id, void *list, void *out);
27
28int 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
91void output_list(int id, void *list, void *out)
92{
93  EXEC SQL BEGIN DECLARE SECTION;
94  char *l = list;
95  int lid = id, count;
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], description[LIST_DESCRIPTION_SIZE];
99  int acl_id, active, maillist, grouplist, nfsgroup, publicflg, hidden;
100  EXEC SQL END DECLARE SECTION;
101  char *maybecomma = "";
102
103  EXEC SQL SELECT acl_type, acl_id, modtime, active, maillist,
104    grouplist, nfsgroup, publicflg, hidden, description INTO :acl_type,
105    :acl_id, :modtime, :active, :maillist, :grouplist, :nfsgroup,
106    :publicflg, :hidden, :description       
107    FROM list WHERE list_id = :lid;
108 
109  strtrim(acl_type);
110  strtrim(modtime);
111  strtrim(description);
112
113  for (count = 0; count < strlen(description); count++)
114    {
115      if (description[count] == '|')
116        description[count] = '-';
117    }
118
119  strcpy(acl_name, "NONE");
120  if (strcmp(acl_type, "LIST") == 0)
121    EXEC SQL SELECT name into :acl_name FROM list WHERE list_id = :acl_id;
122  else if (strcmp(acl_type, "USER") == 0)
123    EXEC SQL SELECT login into :acl_name FROM users WHERE users_id = :acl_id;
124  else if (strcmp(acl_type, "KERBEROS") == 0)
125    EXEC SQL SELECT string into :acl_name FROM strings WHERE string_id = :acl_id;
126  strtrim(acl_name);
127
128  fprintf(out, "%s|%s|%s|%d|%d|%d|%d|%d|%d|%s|", list, acl_type,
129          acl_name, active, maillist, grouplist, nfsgroup, publicflg, hidden, description);
130
131  EXEC SQL DECLARE u_cursor CURSOR FOR
132    SELECT UNIQUE u.login FROM users u, imembers i, list l
133    WHERE l.list_id = :lid AND l.list_id = i.list_id AND
134    i.member_type = 'USER' AND i.member_id = u.users_id;
135  EXEC SQL OPEN u_cursor;
136  while (1)
137    {
138      EXEC SQL FETCH u_cursor INTO :login;
139      if (sqlca.sqlcode)
140        break;
141      fprintf(out, "%s%s", maybecomma, strtrim(login));
142      maybecomma = ",";
143    }
144  EXEC SQL CLOSE u_cursor;
145
146  EXEC SQL DECLARE k_cursor CURSOR FOR
147    SELECT UNIQUE s.string FROM strings s, imembers i, list l
148    WHERE l.list_id = :lid AND l.list_id = i.list_id AND
149    (i.member_type = 'KERBEROS' OR i.member_type = 'STRING')
150    and i.member_id = s.string_id;
151  EXEC SQL OPEN k_cursor;
152  while (1)
153    {
154      EXEC SQL FETCH k_cursor INTO :principal;
155      if (sqlca.sqlcode)
156        break;
157       fprintf(out, "%s%s", maybecomma, strtrim(principal));
158       maybecomma = ",";
159    }
160  EXEC SQL CLOSE k_cursor;
161
162  fprintf(out, "|%s\n", modtime);
163}
164
Note: See TracBrowser for help on using the repository browser.