]> andersk Git - moira.git/blob - gen/www.pc
add hair to deal with fsgroup homedirs and lockers (the first member
[moira.git] / gen / www.pc
1 /* $Id$
2  *
3  * This generates the web server user & group data.
4  *
5  * tom@mit.edu
6  *
7  * Copyright 1998 by the Massachusetts Institute of Technology.
8  * For copying and distribution information, please see the file
9  * <mit-copyright.h>.
10  */
11
12 #include <mit-copyright.h>
13 #include <moira.h>
14 #include <moira_site.h>
15 #include <sys/stat.h>
16 #include <stdio.h>
17 #include <string.h>
18 #include <time.h>
19 #include <errno.h>
20 #include "util.h"
21
22 EXEC SQL INCLUDE sqlca;
23
24 RCSID("$Header$");
25
26
27 #ifndef WWW_SUBDIR
28 #define WWW_SUBDIR "www"
29 #endif
30
31 #define MAX_RECSIZE 10240
32
33 char *whoami = "www.gen";
34 char *db = "moira/moira";
35
36 void filsys(FILE *out);
37 void group(FILE *out);
38
39 char www_dir[MAXPATHLEN];
40
41 char *colors[] = {"red", "green", "blue", "yellow", "mauve", "spooge",
42                   "rose", "ivory", "mitbeige", "twinkie", "midnightblue",
43                   "grey", "lilac", "orange", "purple", "ronhoffmanngreen"};
44 int ncolors = 16;
45
46
47 int main(int argc, char **argv)
48 {
49   char outgrp[MAXPATHLEN];
50   char outfs[MAXPATHLEN];
51   char wkgrp[MAXPATHLEN];
52   char wkfs[MAXPATHLEN];
53   char cmd[BUFSIZ];
54   FILE *outgrpf;
55   FILE *outfsf;
56   struct stat sb;
57   char *c;
58   int flag;
59
60   initialize_sms_error_table();
61
62   if (argc == 1)
63     sprintf(www_dir, "%s/%s", DCM_DIR, WWW_SUBDIR);
64   else
65     {
66       sprintf(www_dir, "%s", argv[1]);
67       /* fake out dcm - we'll append .out later */
68       if (c = strrchr(www_dir, '.'))
69         *c = '\0';
70     }
71
72   if (argc > 2)
73     {
74       fprintf(stderr, "usage: %s [outdir]\n", argv[0]);
75       exit(MR_ARGS);
76     }
77
78   if (stat(www_dir, &sb) < 0)
79     {
80       if (errno == ENOENT)
81         {
82           if (mkdir(www_dir, 0700) < 0)
83             {
84               fprintf(stderr, "%s: unable to make directory %s (%s))\n",
85                       whoami, www_dir, strerror(errno));
86               exit(MR_CCONFIG);
87             }
88         }
89       else
90         {
91           fprintf(stderr, "%s: cannot stat %s (%s)\n", whoami, www_dir,
92                   strerror(errno));
93           exit(MR_CCONFIG);
94         }
95     }
96   else if(!S_ISDIR(sb.st_mode))
97     {
98       fprintf(stderr, "%s: %s not a directory\n" , whoami, www_dir);
99       exit(MR_CCONFIG);
100     }
101
102   EXEC SQL CONNECT :db;
103
104   sprintf(outgrp, "%s/group", www_dir);
105   sprintf(outfs, "%s/filsys", www_dir);
106   sprintf(wkgrp, "%s/group~", www_dir);
107   sprintf(wkfs, "%s/filsys~", www_dir);
108
109   flag = 0;
110   if (stat(outfs, &sb) == 0)
111     {
112       if (ModDiff (&flag, "filesys", sb.st_mtime))
113         exit(MR_DATE);
114     }
115   if (flag < 0)
116     fprintf(stderr, "%s: %s does not need to be rebuilt.\n", whoami, outfs);
117   else
118     {
119       fprintf(stderr, "%s: building fs database...\n", whoami);
120       if (!(outfsf = fopen(wkfs, "w")))
121         {
122           fprintf(stderr, "%s: cannot open %s for writing (%s)\n", whoami,
123                   wkfs, strerror(errno));
124           exit(MR_OCONFIG);
125         }
126       filsys(outfsf);
127       if (fclose(outfsf))
128         {
129           fprintf(stderr, "%s: close of %s failed (%s)", whoami,
130                   wkfs, strerror(errno));
131           exit(MR_CCONFIG);
132         }
133       fix_file(outfs);
134     }
135
136   flag = 0;
137   if (stat(outgrp, &sb) == 0)
138     {
139       if (ModDiff (&flag, "list", sb.st_mtime) ||
140           ModDiff (&flag, "users", sb.st_mtime))
141         exit(MR_DATE);
142     }
143   if (flag < 0)
144     fprintf(stderr, "%s: %s does not need to be rebuilt.\n", whoami, outgrp);
145   else
146     {
147       fprintf(stderr, "%s: building group database...\n", whoami);
148       if (!(outgrpf = fopen(wkgrp, "w")))
149         {
150           fprintf(stderr, "%s: cannot open %s for writing (%s)\n", whoami,
151                   wkgrp, strerror(errno));
152           exit(MR_OCONFIG);
153         }
154       group(outgrpf);
155       if (fclose(outgrpf))
156         {
157           fprintf(stderr, "%s: close of %s failed (%s)", whoami, wkgrp,
158                   strerror(errno));
159           exit(MR_CCONFIG);
160         }
161       fix_file(outgrp);
162     }
163
164   fprintf(stderr, "%s: building tar file...\n", whoami);
165   sprintf(cmd, "(cd %s; tar cf - . ) > %s.out", www_dir, www_dir);
166   if (system(cmd))
167     exit(MR_TAR_FAIL);
168
169   exit(MR_SUCCESS);
170 }
171
172 void filsys(FILE *out)
173 {
174   char *c;
175   EXEC SQL BEGIN DECLARE SECTION;
176   char label[FILESYS_LABEL_SIZE], path[FILESYS_NAME_SIZE];
177   char type[FILESYS_TYPE_SIZE], key[FSGROUP_KEY_SIZE];
178   int status, fid;
179   EXEC SQL END DECLARE SECTION;
180
181   EXEC SQL WHENEVER SQLERROR GOTO sqlerr;
182
183   EXEC SQL DECLARE filsys_cursor CURSOR FOR
184     SELECT label, name, type, filsys_id FROM filesys
185     WHERE type='AFS' or type='FSGROUP'
186     ORDER by label;
187   EXEC SQL OPEN filsys_cursor;
188   while (1)
189     {
190       EXEC SQL FETCH filsys_cursor INTO :label, :path, :type, :fid;
191       if (sqlca.sqlcode)
192         break;
193
194       if (!*strtrim(label))
195         continue;
196
197       if (!strcmp(strtrim(type), "FSGROUP"))
198         {
199           EXEC SQL DECLARE group_cursor CURSOR FOR
200             SELECT f.name, g.key FROM filesys f, fsgroup g
201             WHERE f.filsys_id = g.filsys_id AND f.type='AFS'
202             AND g.group_id = :fid
203             ORDER BY g.key;
204           EXEC SQL OPEN group_cursor;
205           EXEC SQL FETCH group_cursor INTO :path, :key;
206           EXEC SQL CLOSE group_cursor;
207         }
208
209       if (!*strtrim(path))
210         continue;
211       fprintf(out, "%s:%s\n", label, path);
212     }
213
214   EXEC SQL CLOSE filsys_cursor;
215   EXEC SQL COMMIT;
216
217   return;
218
219 sqlerr:
220   db_error(sqlca.sqlcode);
221   exit(MR_DBMS_ERR);
222 }
223
224 void group(FILE *out)
225 {
226   int first;
227   int recsize;
228
229   EXEC SQL BEGIN DECLARE SECTION;
230   char user[USERS_LOGIN_SIZE];
231   char list[LIST_NAME_SIZE];
232   char parent[LIST_NAME_SIZE];
233   EXEC SQL END DECLARE SECTION;
234
235   EXEC SQL WHENEVER SQLERROR GOTO sqlerr;
236
237   EXEC SQL DECLARE user_cursor CURSOR FOR
238     SELECT login FROM users WHERE status=1 ORDER by login;
239   EXEC SQL OPEN user_cursor;
240
241   while (1)
242     {
243       EXEC SQL FETCH user_cursor INTO :user;
244       if (sqlca.sqlcode)
245         break;
246       strtrim(user);
247       recsize = strlen(user) + 3;
248       first = 1;
249
250       EXEC SQL DECLARE member_cursor CURSOR FOR
251         SELECT DISTINCT l.name from list l, imembers i, users u
252         WHERE u.login=:user AND u.users_id=i.member_id AND
253         i.member_type='USER' AND i.list_id=l.list_id AND
254         l.grouplist != 0 and l.active != 0
255         ORDER by l.name;
256       EXEC SQL OPEN member_cursor;
257
258       while (1)
259         {
260           EXEC SQL FETCH member_cursor INTO :list;
261           if (sqlca.sqlcode)
262             break;
263           strtrim(list);
264           if (first)
265             fprintf(out, "%s:xjOhFdLKGK84w:", user);
266           fprintf(out, "%s%s", first ? "" : ",", list);
267           recsize += strlen(list) + 1;
268           first = 0;
269           if (recsize > MAX_RECSIZE)
270             {
271               fprintf(stderr, "truncated group list for %s\n", user);
272               break;
273             }
274         }
275
276       EXEC SQL CLOSE member_cursor;
277       EXEC SQL COMMIT;
278
279       if (!first)
280         fprintf(out, "\n");
281     }
282
283   EXEC SQL CLOSE user_cursor;
284   EXEC SQL COMMIT;
285
286   return;
287
288 sqlerr:
289   db_error(sqlca.sqlcode);
290   exit(MR_DBMS_ERR);
291 }
This page took 0.066665 seconds and 5 git commands to generate.