]>
Commit | Line | Data |
---|---|---|
1 | /* $Header$ | |
2 | * | |
3 | * Do AFS incremental updates | |
4 | * | |
5 | * Copyright (C) 1989,1992 by the Massachusetts Institute of Technology | |
6 | * for copying and distribution information, please see the file | |
7 | * <mit-copyright.h>. | |
8 | */ | |
9 | ||
10 | #include <sys/types.h> | |
11 | #include <sys/file.h> | |
12 | #include <strings.h> | |
13 | ||
14 | #include <moira.h> | |
15 | #include <moira_site.h> | |
16 | ||
17 | #include <afs/param.h> | |
18 | #include <afs/cellconfig.h> | |
19 | #include <afs/venus.h> | |
20 | #include <afs/ptclient.h> | |
21 | #include <afs/pterror.h> | |
22 | ||
23 | #define file_exists(file) (access((file), F_OK) == 0) | |
24 | ||
25 | char *whoami; | |
26 | char *cellname = "ATHENA.MIT.EDU"; | |
27 | ||
28 | main(argc, argv) | |
29 | char **argv; | |
30 | int argc; | |
31 | { | |
32 | int beforec, afterc, i; | |
33 | char *table, **before, **after; | |
34 | #ifdef DEBUG | |
35 | char buf[1024]; | |
36 | #endif | |
37 | ||
38 | for (i = getdtablesize() - 1; i > 2; i--) | |
39 | close(i); | |
40 | ||
41 | table = argv[1]; | |
42 | beforec = atoi(argv[2]); | |
43 | before = &argv[4]; | |
44 | afterc = atoi(argv[3]); | |
45 | after = &argv[4 + beforec]; | |
46 | whoami = argv[0]; | |
47 | ||
48 | #ifdef DEBUG | |
49 | sprintf(buf, "%s (", table); | |
50 | for (i = 0; i < beforec; i++) { | |
51 | if (i > 0) | |
52 | strcat(buf, ","); | |
53 | strcat(buf, before[i]); | |
54 | } | |
55 | strcat(buf, ")->("); | |
56 | for (i = 0; i < afterc; i++) { | |
57 | if (i > 0) | |
58 | strcat(buf, ","); | |
59 | strcat(buf, after[i]); | |
60 | } | |
61 | strcat(buf, ")\n"); | |
62 | write(1,buf,strlen(buf)); | |
63 | #endif | |
64 | ||
65 | initialize_sms_error_table(); | |
66 | initialize_krb_error_table(); | |
67 | ||
68 | if (!strcmp(table, "users")) { | |
69 | do_user(before, beforec, after, afterc); | |
70 | } else if (!strcmp(table, "list")) { | |
71 | do_list(before, beforec, after, afterc); | |
72 | } else if (!strcmp(table, "members")) { | |
73 | do_member(before, beforec, after, afterc); | |
74 | } else if (!strcmp(table, "filesys")) { | |
75 | do_filesys(before, beforec, after, afterc); | |
76 | } else if (!strcmp(table, "quota")) { | |
77 | do_quota(before, beforec, after, afterc); | |
78 | } | |
79 | exit(0); | |
80 | } | |
81 | ||
82 | ||
83 | do_cmd(cmd) | |
84 | char *cmd; | |
85 | { | |
86 | int success = 0, tries = 0; | |
87 | ||
88 | while (success == 0 && tries < 1) { | |
89 | if (tries++) | |
90 | sleep(5*60); | |
91 | com_err(whoami, 0, "Executing command: %s", cmd); | |
92 | if (system(cmd) == 0) | |
93 | success++; | |
94 | } | |
95 | if (!success) | |
96 | critical_alert("incremental", "failed command: %s", cmd); | |
97 | } | |
98 | ||
99 | ||
100 | do_user(before, beforec, after, afterc) | |
101 | char **before; | |
102 | int beforec; | |
103 | char **after; | |
104 | int afterc; | |
105 | { | |
106 | int astate, bstate, auid, buid, code; | |
107 | ||
108 | auid = buid = astate = bstate = 0; | |
109 | if (afterc > U_STATE) astate = atoi(after[U_STATE]); | |
110 | if (beforec > U_STATE) bstate = atoi(before[U_STATE]); | |
111 | if (afterc > U_UID) auid = atoi(after[U_UID]); | |
112 | if (beforec > U_UID) buid = atoi(before[U_UID]); | |
113 | ||
114 | /* We consider "half-registered" users to be active */ | |
115 | if (astate == 2) astate = 1; | |
116 | if (bstate == 2) bstate = 1; | |
117 | ||
118 | if (astate != 1 && bstate != 1) /* inactive user */ | |
119 | return; | |
120 | ||
121 | if (astate == bstate && auid == buid && | |
122 | !strcmp(before[U_NAME], after[U_NAME])) | |
123 | /* No AFS related attributes have changed */ | |
124 | return; | |
125 | ||
126 | code=pr_Initialize(1, AFSCONF_CLIENTNAME, 0); | |
127 | if (code) { | |
128 | critical_alert("incremental", "Couldn't initialize libprot: %s", | |
129 | error_message(code)); | |
130 | return; | |
131 | } | |
132 | ||
133 | if (astate == bstate) { | |
134 | /* Only a modify has to be done */ | |
135 | code = pr_ChangeEntry(before[U_NAME], after[U_NAME], | |
136 | (auid==buid) ? 0 : auid, ""); | |
137 | if (code) { | |
138 | critical_alert("incremental", | |
139 | "Couldn't change user %s (id %d) to %s (id %d): %s", | |
140 | before[U_NAME], buid, after[U_NAME], auid, | |
141 | error_message(code)); | |
142 | } | |
143 | return; | |
144 | } | |
145 | if (bstate == 1) { | |
146 | code = pr_DeleteByID(buid); | |
147 | if (code && code != PRNOENT) { | |
148 | critical_alert("incremental", | |
149 | "Couldn't delete user %s (id %d): %s", | |
150 | before[U_NAME], buid, error_message(code)); | |
151 | } | |
152 | return; | |
153 | } | |
154 | if (astate == 1) { | |
155 | code = pr_CreateUser(after[U_NAME], &auid); | |
156 | if (code) { | |
157 | critical_alert("incremental", | |
158 | "Couldn't create user %s (id %d): %s", | |
159 | after[U_NAME], auid, error_message(code)); | |
160 | } | |
161 | return; | |
162 | } | |
163 | } | |
164 | ||
165 | ||
166 | ||
167 | do_list(before, beforec, after, afterc) | |
168 | char **before; | |
169 | int beforec; | |
170 | char **after; | |
171 | int afterc; | |
172 | { | |
173 | int agid, bgid; | |
174 | long code, id; | |
175 | char hostname[64]; | |
176 | char g1[PR_MAXNAMELEN], g2[PR_MAXNAMELEN]; | |
177 | char *av[2]; | |
178 | ||
179 | agid = bgid = 0; | |
180 | if (beforec > L_GID && atoi(before[L_ACTIVE]) && atoi(before[L_GROUP])) | |
181 | bgid = atoi(before[L_GID]); | |
182 | if (afterc > L_GID && atoi(after[L_ACTIVE]) && atoi(after[L_GROUP])) | |
183 | agid = atoi(after[L_GID]); | |
184 | ||
185 | if (agid == 0 && bgid == 0) /* Not active groups */ | |
186 | return; | |
187 | if (agid == bgid && !strcmp(after[L_NAME], before[L_NAME])) | |
188 | return; /* No change */ | |
189 | ||
190 | code=pr_Initialize(1, AFSCONF_CLIENTNAME, 0); | |
191 | if (code) { | |
192 | critical_alert("incremental", "Couldn't initialize libprot: %s", | |
193 | error_message(code)); | |
194 | return; | |
195 | } | |
196 | ||
197 | if (agid && bgid) { | |
198 | /* Only a modify is required */ | |
199 | strcpy(g1, "system:"); | |
200 | strcpy(g2, "system:"); | |
201 | strcat(g1, before[L_NAME]); | |
202 | strcat(g2, after[L_NAME]); | |
203 | code = pr_ChangeEntry(g1, g2, (agid==bgid) ? 0 : -agid, ""); | |
204 | if (code) { | |
205 | critical_alert("incremental", | |
206 | "Couldn't change group %s (id %d) to %s (id %d): %s", | |
207 | before[L_NAME], -bgid, after[L_NAME], -agid, | |
208 | error_message(code)); | |
209 | } | |
210 | return; | |
211 | } | |
212 | if (bgid) { | |
213 | code = pr_DeleteByID(-bgid); | |
214 | if (code && code != PRNOENT) { | |
215 | critical_alert("incremental", | |
216 | "Couldn't delete group %s (id %d): %s", | |
217 | before[L_NAME], -bgid, error_message(code)); | |
218 | } | |
219 | return; | |
220 | } | |
221 | if (agid) { | |
222 | strcpy(g1, "system:"); | |
223 | strcat(g1, after[L_NAME]); | |
224 | strcpy(g2, "system:administrators"); | |
225 | id = -agid; | |
226 | code = pr_CreateGroup(g1, g2, &id); | |
227 | if (code) { | |
228 | critical_alert("incremental", | |
229 | "Couldn't create group %s (id %d): %s", | |
230 | after[L_NAME], id, error_message(code)); | |
231 | return; | |
232 | } | |
233 | ||
234 | /* We need to make sure the group is properly populated */ | |
235 | if (beforec < L_ACTIVE || atoi(before[L_ACTIVE]) == 0) return; | |
236 | ||
237 | gethostname(hostname, sizeof(hostname)); | |
238 | code = mr_connect(hostname); | |
239 | if (!code) code = mr_auth("afs.incr"); | |
240 | if (code) { | |
241 | critical_alert("incremental", | |
242 | "Error contacting Moira server to resolve %s: %s", | |
243 | after[L_NAME], error_message(code)); | |
244 | return; | |
245 | } | |
246 | av[0] = "LIST"; | |
247 | av[1] = after[L_NAME]; | |
248 | get_members(2, av, after[L_NAME]); | |
249 | ||
250 | mr_disconnect(); | |
251 | return; | |
252 | } | |
253 | } | |
254 | ||
255 | ||
256 | do_member(before, beforec, after, afterc) | |
257 | char **before; | |
258 | int beforec; | |
259 | char **after; | |
260 | int afterc; | |
261 | { | |
262 | int code; | |
263 | char *p; | |
264 | ||
265 | if ((beforec < 4 || !atoi(before[LM_END])) && | |
266 | (afterc < 4 || !atoi(after[LM_END]))) | |
267 | return; | |
268 | ||
269 | code=pr_Initialize(1, AFSCONF_CLIENTNAME, 0); | |
270 | if (code) { | |
271 | critical_alert("incremental", "Couldn't initialize libprot: %s", | |
272 | error_message(code)); | |
273 | return; | |
274 | } | |
275 | ||
276 | if (afterc) | |
277 | edit_group(1, after[LM_LIST], after[LM_TYPE], after[LM_MEMBER]); | |
278 | if (beforec) | |
279 | edit_group(0, after[LM_LIST], after[LM_TYPE], after[LM_MEMBER]); | |
280 | } | |
281 | ||
282 | ||
283 | get_members(ac, av, group) | |
284 | int ac; | |
285 | char *av[]; | |
286 | char *group; | |
287 | { | |
288 | int code=0; | |
289 | ||
290 | if (strcmp(av[0], "LIST")) { | |
291 | sleep(1); /* give the ptserver room */ | |
292 | edit_group(1, group, av[0], av[1]); | |
293 | } else { | |
294 | code = mr_query("get_members_of_list", 1, &av[1], get_members, group); | |
295 | if (code) | |
296 | critical_alert("incremental", | |
297 | "Couldn't retrieve full membership of %s: %s", | |
298 | group, error_message(code)); | |
299 | } | |
300 | return code; | |
301 | } | |
302 | ||
303 | ||
304 | edit_group(op, group, type, member) | |
305 | int op; | |
306 | char *group; | |
307 | char *type; | |
308 | char *member; | |
309 | { | |
310 | char *p = 0; | |
311 | char buf[PR_MAXNAMELEN]; | |
312 | int (*fn)(); | |
313 | int code; | |
314 | extern long pr_AddToGroup(), pr_RemoveUserFromGroup(); | |
315 | ||
316 | fn = op ? pr_AddToGroup : pr_RemoveUserFromGroup; | |
317 | ||
318 | /* The following KERBEROS code allows for the use of entities | |
319 | * user@foreign_cell. | |
320 | */ | |
321 | if (!strcmp(type, "KERBEROS")) { | |
322 | p = index(member, '@'); | |
323 | if (p && !strcasecmp(p+1, cellname)) | |
324 | *p = 0; | |
325 | } else if (strcmp(type, "USER")) | |
326 | return; /* invalid type */ | |
327 | ||
328 | strcpy(buf, "system:"); | |
329 | strcat(buf, group); | |
330 | code = (*fn)(member, buf); | |
331 | if (code) { | |
332 | if (op==0 && code == PRNOENT) return; | |
333 | if (op==1 && code == PRIDEXIST) return; | |
334 | if (strcmp(type, "KERBEROS") || code != PRNOENT) { | |
335 | critical_alert("incremental", | |
336 | "Couldn't %s %s %s %s: %s", | |
337 | op ? "add" : "remove", member, | |
338 | op ? "to" : "from", buf, | |
339 | error_message(code)); | |
340 | } | |
341 | } | |
342 | if (p) *p = '@'; | |
343 | } | |
344 | ||
345 | ||
346 | do_filesys(before, beforec, after, afterc) | |
347 | char **before; | |
348 | int beforec; | |
349 | char **after; | |
350 | int afterc; | |
351 | { | |
352 | char cmd[1024]; | |
353 | ||
354 | if (beforec < FS_CREATE) { | |
355 | if (afterc < FS_CREATE || atoi(after[FS_CREATE])==0 || | |
356 | strcmp(after[FS_TYPE], "AFS")) | |
357 | return; | |
358 | ||
359 | /* new locker creation */ | |
360 | sprintf(cmd, "%s/perl -I%s %s/afs_create.pl %s %s %s %s %s %s", | |
361 | BIN_DIR, BIN_DIR, BIN_DIR, | |
362 | after[FS_NAME], after[FS_L_TYPE], after[FS_MACHINE], | |
363 | after[FS_PACK], after[FS_OWNER], after[FS_OWNERS]); | |
364 | do_cmd(cmd); | |
365 | return; | |
366 | } | |
367 | ||
368 | /* What do we do? When do we use FS_CREATE? | |
369 | * | |
370 | * TYPE change: AFS->ERR, ERR->AFS: rename/unmount/remount | |
371 | * LOCKERTYPE change: rename/remount | |
372 | * PACK change: remount | |
373 | * LABEL change: rename/remount | |
374 | * Deletion: rename/unmount | |
375 | */ | |
376 | } | |
377 | ||
378 | ||
379 | do_quota(before, beforec, after, afterc) | |
380 | char **before; | |
381 | int beforec; | |
382 | char **after; | |
383 | int afterc; | |
384 | { | |
385 | char cmd[1024]; | |
386 | ||
387 | if (afterc < Q_DIRECTORY || strcmp("ANY", after[Q_TYPE]) || | |
388 | strncmp("/afs/", after[Q_DIRECTORY], 5)) | |
389 | return; | |
390 | ||
391 | sprintf(cmd, "%s/perl -I%s %s/afs_quota.pl %s %s", | |
392 | BIN_DIR, BIN_DIR, BIN_DIR, | |
393 | after[Q_DIRECTORY], after[Q_QUOTA]); | |
394 | do_cmd(cmd); | |
395 | return; | |
396 | } |