1 | /* $Id: www.pc,v 1.8 2002-04-05 22:53:50 zacheiss Exp $ |
---|
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: /afs/.athena.mit.edu/astaff/project/moiradev/repository/moira/gen/www.pc,v 1.8 2002-04-05 22:53:50 zacheiss Exp $"); |
---|
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 | |
---|
59 | initialize_sms_error_table(); |
---|
60 | |
---|
61 | if (argc == 1) |
---|
62 | sprintf(www_dir, "%s/%s", DCM_DIR, WWW_SUBDIR); |
---|
63 | else |
---|
64 | { |
---|
65 | sprintf(www_dir, "%s", argv[1]); |
---|
66 | /* fake out dcm - we'll append .out later */ |
---|
67 | if (c = strrchr(www_dir, '.')) |
---|
68 | *c = '\0'; |
---|
69 | } |
---|
70 | |
---|
71 | if (argc > 2) |
---|
72 | { |
---|
73 | fprintf(stderr, "usage: %s [outdir]\n", argv[0]); |
---|
74 | exit(MR_ARGS); |
---|
75 | } |
---|
76 | |
---|
77 | if (stat(www_dir, &sb) < 0) |
---|
78 | { |
---|
79 | if (errno == ENOENT) |
---|
80 | { |
---|
81 | if (mkdir(www_dir, 0700) < 0) |
---|
82 | { |
---|
83 | fprintf(stderr, "%s: unable to make directory %s (%s))\n", |
---|
84 | whoami, www_dir, strerror(errno)); |
---|
85 | exit(MR_CCONFIG); |
---|
86 | } |
---|
87 | } |
---|
88 | else |
---|
89 | { |
---|
90 | fprintf(stderr, "%s: cannot stat %s (%s)\n", whoami, www_dir, |
---|
91 | strerror(errno)); |
---|
92 | exit(MR_CCONFIG); |
---|
93 | } |
---|
94 | } |
---|
95 | else if(!S_ISDIR(sb.st_mode)) |
---|
96 | { |
---|
97 | fprintf(stderr, "%s: %s not a directory\n" , whoami, www_dir); |
---|
98 | exit(MR_CCONFIG); |
---|
99 | } |
---|
100 | |
---|
101 | EXEC SQL CONNECT :db; |
---|
102 | |
---|
103 | sprintf(outgrp, "%s/group", www_dir); |
---|
104 | sprintf(outfs, "%s/filsys", www_dir); |
---|
105 | sprintf(wkgrp, "%s/group~", www_dir); |
---|
106 | sprintf(wkfs, "%s/filsys~", www_dir); |
---|
107 | |
---|
108 | fprintf(stderr, "%s: building fs database...\n", whoami); |
---|
109 | if (!(outfsf = fopen(wkfs, "w"))) |
---|
110 | { |
---|
111 | fprintf(stderr, "%s: cannot open %s for writing (%s)\n", whoami, |
---|
112 | wkfs, strerror(errno)); |
---|
113 | exit(MR_OCONFIG); |
---|
114 | } |
---|
115 | filsys(outfsf); |
---|
116 | if (fclose(outfsf)) |
---|
117 | { |
---|
118 | fprintf(stderr, "%s: close of %s failed (%s)", whoami, |
---|
119 | wkfs, strerror(errno)); |
---|
120 | exit(MR_CCONFIG); |
---|
121 | } |
---|
122 | fix_file(outfs); |
---|
123 | |
---|
124 | fprintf(stderr, "%s: building group database...\n", whoami); |
---|
125 | if (!(outgrpf = fopen(wkgrp, "w"))) |
---|
126 | { |
---|
127 | fprintf(stderr, "%s: cannot open %s for writing (%s)\n", whoami, |
---|
128 | wkgrp, strerror(errno)); |
---|
129 | exit(MR_OCONFIG); |
---|
130 | } |
---|
131 | group(outgrpf); |
---|
132 | if (fclose(outgrpf)) |
---|
133 | { |
---|
134 | fprintf(stderr, "%s: close of %s failed (%s)", whoami, wkgrp, |
---|
135 | strerror(errno)); |
---|
136 | exit(MR_CCONFIG); |
---|
137 | } |
---|
138 | fix_file(outgrp); |
---|
139 | |
---|
140 | fprintf(stderr, "%s: building tar file...\n", whoami); |
---|
141 | sprintf(cmd, "(cd %s; tar cf - . ) > %s.out", www_dir, www_dir); |
---|
142 | if (system(cmd)) |
---|
143 | exit(MR_TAR_FAIL); |
---|
144 | |
---|
145 | exit(MR_SUCCESS); |
---|
146 | } |
---|
147 | |
---|
148 | void filsys(FILE *out) |
---|
149 | { |
---|
150 | char *c; |
---|
151 | EXEC SQL BEGIN DECLARE SECTION; |
---|
152 | char label[FILESYS_LABEL_SIZE], path[FILESYS_NAME_SIZE]; |
---|
153 | char type[FILESYS_TYPE_SIZE], key[FSGROUP_KEY_SIZE]; |
---|
154 | char alias[ALIAS_NAME_SIZE]; |
---|
155 | int status, fid; |
---|
156 | EXEC SQL END DECLARE SECTION; |
---|
157 | |
---|
158 | EXEC SQL WHENEVER SQLERROR GOTO sqlerr; |
---|
159 | |
---|
160 | EXEC SQL DECLARE filsys_cursor CURSOR FOR |
---|
161 | SELECT label, name, type, filsys_id FROM filesys |
---|
162 | WHERE type='AFS' or type='FSGROUP' |
---|
163 | ORDER by label; |
---|
164 | EXEC SQL OPEN filsys_cursor; |
---|
165 | while (1) |
---|
166 | { |
---|
167 | EXEC SQL FETCH filsys_cursor INTO :label, :path, :type, :fid; |
---|
168 | if (sqlca.sqlcode) |
---|
169 | break; |
---|
170 | |
---|
171 | if (!*strtrim(label)) |
---|
172 | continue; |
---|
173 | |
---|
174 | if (!strcmp(strtrim(type), "FSGROUP")) |
---|
175 | { |
---|
176 | *path = '\0'; |
---|
177 | EXEC SQL DECLARE group_cursor CURSOR FOR |
---|
178 | SELECT f.name, g.key FROM filesys f, fsgroup g |
---|
179 | WHERE f.filsys_id = g.filsys_id AND f.type='AFS' |
---|
180 | AND g.group_id = :fid |
---|
181 | ORDER BY g.key; |
---|
182 | EXEC SQL OPEN group_cursor; |
---|
183 | EXEC SQL FETCH group_cursor INTO :path, :key; |
---|
184 | EXEC SQL CLOSE group_cursor; |
---|
185 | } |
---|
186 | |
---|
187 | EXEC SQL DECLARE alias_cursor CURSOR FOR |
---|
188 | SELECT name INTO :alias FROM alias |
---|
189 | WHERE type='FILESYS' AND trans=:label; |
---|
190 | |
---|
191 | if (!*strtrim(path)) |
---|
192 | continue; |
---|
193 | for (c = label; *c; c++) |
---|
194 | *c = tolower(*c); |
---|
195 | fprintf(out, "%s:%s\n", label, path); |
---|
196 | |
---|
197 | EXEC SQL OPEN alias_cursor; |
---|
198 | while (1) |
---|
199 | { |
---|
200 | EXEC SQL FETCH alias_cursor INTO :alias; |
---|
201 | if (sqlca.sqlcode) |
---|
202 | break; |
---|
203 | |
---|
204 | if (!*strtrim(alias)) |
---|
205 | continue; |
---|
206 | for (c = alias; *c; c++) |
---|
207 | *c = tolower(*c); |
---|
208 | fprintf(out, "%s:%s\n", alias, path); |
---|
209 | } |
---|
210 | EXEC SQL CLOSE alias_cursor; |
---|
211 | } |
---|
212 | |
---|
213 | EXEC SQL CLOSE filsys_cursor; |
---|
214 | EXEC SQL COMMIT; |
---|
215 | |
---|
216 | return; |
---|
217 | |
---|
218 | sqlerr: |
---|
219 | db_error(sqlca.sqlcode); |
---|
220 | exit(MR_DBMS_ERR); |
---|
221 | } |
---|
222 | |
---|
223 | void group(FILE *out) |
---|
224 | { |
---|
225 | int first; |
---|
226 | int recsize; |
---|
227 | |
---|
228 | EXEC SQL BEGIN DECLARE SECTION; |
---|
229 | char user[USERS_LOGIN_SIZE]; |
---|
230 | char list[LIST_NAME_SIZE]; |
---|
231 | char parent[LIST_NAME_SIZE]; |
---|
232 | EXEC SQL END DECLARE SECTION; |
---|
233 | |
---|
234 | EXEC SQL WHENEVER SQLERROR GOTO sqlerr; |
---|
235 | |
---|
236 | EXEC SQL DECLARE user_cursor CURSOR FOR |
---|
237 | SELECT login FROM users WHERE status=1 OR status=2 OR status=6 OR status=9 |
---|
238 | 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 | if (!isalpha(*user)) |
---|
248 | continue; |
---|
249 | |
---|
250 | recsize = strlen(user) + 3; |
---|
251 | first = 1; |
---|
252 | fprintf(out, "%s:xjOhFdLKGK84w:", user); |
---|
253 | |
---|
254 | EXEC SQL DECLARE member_cursor CURSOR FOR |
---|
255 | SELECT DISTINCT l.name from list l, imembers i, users u |
---|
256 | WHERE u.login=:user AND u.users_id=i.member_id AND |
---|
257 | i.member_type='USER' AND i.list_id=l.list_id AND |
---|
258 | l.grouplist != 0 and l.active != 0 |
---|
259 | ORDER by l.name; |
---|
260 | EXEC SQL OPEN member_cursor; |
---|
261 | |
---|
262 | while (1) |
---|
263 | { |
---|
264 | EXEC SQL FETCH member_cursor INTO :list; |
---|
265 | if (sqlca.sqlcode) |
---|
266 | break; |
---|
267 | strtrim(list); |
---|
268 | fprintf(out, "%s%s", first ? "" : ",", list); |
---|
269 | recsize += strlen(list) + 1; |
---|
270 | first = 0; |
---|
271 | if (recsize > MAX_RECSIZE) |
---|
272 | { |
---|
273 | fprintf(stderr, "truncated group list for %s\n", user); |
---|
274 | break; |
---|
275 | } |
---|
276 | } |
---|
277 | |
---|
278 | EXEC SQL CLOSE member_cursor; |
---|
279 | EXEC SQL COMMIT; |
---|
280 | |
---|
281 | fprintf(out, "\n"); |
---|
282 | } |
---|
283 | |
---|
284 | EXEC SQL CLOSE user_cursor; |
---|
285 | EXEC SQL COMMIT; |
---|
286 | |
---|
287 | return; |
---|
288 | |
---|
289 | sqlerr: |
---|
290 | db_error(sqlca.sqlcode); |
---|
291 | exit(MR_DBMS_ERR); |
---|
292 | } |
---|