]>
Commit | Line | Data |
---|---|---|
fb8809f4 | 1 | /* $Header$ |
2 | * | |
3 | * Do AFS incremental updates | |
4 | * | |
60c22e5f | 5 | * Copyright (C) 1989,1992 by the Massachusetts Institute of Technology |
fb8809f4 | 6 | * for copying and distribution information, please see the file |
7 | * <mit-copyright.h>. | |
8 | */ | |
9 | ||
16838ad1 | 10 | #include <stdio.h> |
60c22e5f | 11 | #include <sys/types.h> |
12 | #include <sys/file.h> | |
13 | #include <strings.h> | |
e2666aac | 14 | #include <unistd.h> |
60c22e5f | 15 | |
d1be2e3a | 16 | #include <krb.h> |
2ce085d2 | 17 | #include <moira.h> |
18 | #include <moira_site.h> | |
f633445d | 19 | |
60c22e5f | 20 | #include <afs/param.h> |
21 | #include <afs/cellconfig.h> | |
22 | #include <afs/venus.h> | |
23 | #include <afs/ptclient.h> | |
24 | #include <afs/pterror.h> | |
fb8809f4 | 25 | |
d1be2e3a | 26 | #define STOP_FILE "/moira/afs/noafs" |
60c22e5f | 27 | #define file_exists(file) (access((file), F_OK) == 0) |
fb8809f4 | 28 | |
16838ad1 | 29 | #if defined(vax) && !defined(__STDC__) |
30 | #define volatile | |
31 | #endif | |
32 | ||
fb8809f4 | 33 | char *whoami; |
34 | ||
eb63ae5f | 35 | /* Main stub routines */ |
36 | int do_user(); | |
37 | int do_list(); | |
38 | int do_member(); | |
39 | int do_filesys(); | |
40 | int do_quota(); | |
41 | ||
42 | /* Support stub routines */ | |
43 | int run_cmd(); | |
44 | int add_user_lists(); | |
063819ef | 45 | int add_list_members(); |
c69d9f2c | 46 | int check_user(); |
eb63ae5f | 47 | int edit_group(); |
e2666aac | 48 | long pr_try(); |
eb63ae5f | 49 | int check_afs(); |
50 | ||
51 | /* libprot.a routines */ | |
52 | extern long pr_Initialize(); | |
53 | extern long pr_CreateUser(); | |
54 | extern long pr_CreateGroup(); | |
55 | extern long pr_DeleteByID(); | |
56 | extern long pr_ChangeEntry(); | |
57 | extern long pr_SetFieldsEntry(); | |
58 | extern long pr_AddToGroup(); | |
59 | extern long pr_RemoveUserFromGroup(); | |
60 | ||
61 | static char tbl_buf[1024]; | |
2cb1f02c | 62 | static struct member { |
63 | int op; | |
64 | char list[33]; | |
65 | char type[9]; | |
66 | char member[129]; | |
67 | struct member *next; | |
68 | } *member_head = 0; | |
69 | ||
70 | static int mr_connections=0; | |
eb63ae5f | 71 | |
fb8809f4 | 72 | main(argc, argv) |
73 | char **argv; | |
74 | int argc; | |
75 | { | |
a7fdf2de | 76 | int beforec, afterc, i; |
fb8809f4 | 77 | char *table, **before, **after; |
78 | ||
a7fdf2de | 79 | for (i = getdtablesize() - 1; i > 2; i--) |
80 | close(i); | |
81 | ||
e2666aac | 82 | whoami = ((whoami = strrchr(argv[0], '/')) ? whoami+1 : argv[0]); |
d4ea75d6 | 83 | |
fb8809f4 | 84 | table = argv[1]; |
85 | beforec = atoi(argv[2]); | |
86 | before = &argv[4]; | |
87 | afterc = atoi(argv[3]); | |
88 | after = &argv[4 + beforec]; | |
f10e3ca2 | 89 | |
e2666aac | 90 | setvbuf(stdout, NULL, _IOLBF, BUFSIZ); |
eb63ae5f | 91 | |
92 | strcpy(tbl_buf, table); | |
93 | strcat(tbl_buf, " ("); | |
810d12a7 | 94 | for (i = 0; i < beforec; i++) { |
95 | if (i > 0) | |
eb63ae5f | 96 | strcat(tbl_buf, ","); |
97 | strcat(tbl_buf, before[i]); | |
810d12a7 | 98 | } |
eb63ae5f | 99 | strcat(tbl_buf, ")->("); |
810d12a7 | 100 | for (i = 0; i < afterc; i++) { |
101 | if (i > 0) | |
eb63ae5f | 102 | strcat(tbl_buf, ","); |
103 | strcat(tbl_buf, after[i]); | |
810d12a7 | 104 | } |
eb63ae5f | 105 | strcat(tbl_buf, ")"); |
d1be2e3a | 106 | #ifdef DEBUG |
92d359ba | 107 | com_err(whoami, 0, "%s", tbl_buf); |
810d12a7 | 108 | #endif |
109 | ||
fb8809f4 | 110 | initialize_sms_error_table(); |
111 | initialize_krb_error_table(); | |
112 | ||
113 | if (!strcmp(table, "users")) { | |
114 | do_user(before, beforec, after, afterc); | |
115 | } else if (!strcmp(table, "list")) { | |
116 | do_list(before, beforec, after, afterc); | |
e2666aac | 117 | } else if (!strcmp(table, "imembers")) { |
fb8809f4 | 118 | do_member(before, beforec, after, afterc); |
119 | } else if (!strcmp(table, "filesys")) { | |
120 | do_filesys(before, beforec, after, afterc); | |
1f377b55 | 121 | } else if (!strcmp(table, "quota")) { |
fb8809f4 | 122 | do_quota(before, beforec, after, afterc); |
123 | } | |
fb8809f4 | 124 | exit(0); |
125 | } | |
126 | ||
127 | ||
fb8809f4 | 128 | do_user(before, beforec, after, afterc) |
129 | char **before; | |
130 | int beforec; | |
131 | char **after; | |
132 | int afterc; | |
133 | { | |
eb63ae5f | 134 | int astate, bstate, auid, buid, code; |
9b273918 | 135 | char *av[2]; |
60c22e5f | 136 | |
137 | auid = buid = astate = bstate = 0; | |
138 | if (afterc > U_STATE) astate = atoi(after[U_STATE]); | |
139 | if (beforec > U_STATE) bstate = atoi(before[U_STATE]); | |
140 | if (afterc > U_UID) auid = atoi(after[U_UID]); | |
141 | if (beforec > U_UID) buid = atoi(before[U_UID]); | |
142 | ||
143 | /* We consider "half-registered" users to be active */ | |
810d12a7 | 144 | if (astate == 2) astate = 1; |
145 | if (bstate == 2) bstate = 1; | |
fb8809f4 | 146 | |
60c22e5f | 147 | if (astate != 1 && bstate != 1) /* inactive user */ |
fb8809f4 | 148 | return; |
60c22e5f | 149 | |
150 | if (astate == bstate && auid == buid && | |
151 | !strcmp(before[U_NAME], after[U_NAME])) | |
152 | /* No AFS related attributes have changed */ | |
fb8809f4 | 153 | return; |
fb8809f4 | 154 | |
60c22e5f | 155 | if (astate == bstate) { |
156 | /* Only a modify has to be done */ | |
92d359ba | 157 | com_err(whoami, 0, "Changing user %s (uid %d) to %s (uid %d)", |
158 | before[U_NAME], buid, after[U_NAME], auid); | |
159 | ||
eb63ae5f | 160 | code = pr_try(pr_ChangeEntry, before[U_NAME], after[U_NAME], auid, ""); |
60c22e5f | 161 | if (code) { |
162 | critical_alert("incremental", | |
163 | "Couldn't change user %s (id %d) to %s (id %d): %s", | |
164 | before[U_NAME], buid, after[U_NAME], auid, | |
165 | error_message(code)); | |
166 | } | |
167 | return; | |
168 | } | |
169 | if (bstate == 1) { | |
92d359ba | 170 | com_err(whoami, 0, "Deleting user %s (uid %d)", |
171 | before[U_NAME], buid); | |
172 | ||
eb63ae5f | 173 | code = pr_try(pr_DeleteByID, buid); |
60c22e5f | 174 | if (code && code != PRNOENT) { |
175 | critical_alert("incremental", | |
176 | "Couldn't delete user %s (id %d): %s", | |
177 | before[U_NAME], buid, error_message(code)); | |
178 | } | |
179 | return; | |
180 | } | |
181 | if (astate == 1) { | |
92d359ba | 182 | com_err(whoami, 0, "%s user %s (uid %d)", |
183 | ((bstate != 0) ? "Reactivating" : "Creating"), | |
184 | after[U_NAME], auid); | |
185 | ||
eb63ae5f | 186 | code = pr_try(pr_CreateUser, after[U_NAME], &auid); |
60c22e5f | 187 | if (code) { |
188 | critical_alert("incremental", | |
189 | "Couldn't create user %s (id %d): %s", | |
190 | after[U_NAME], auid, error_message(code)); | |
96786144 | 191 | return; |
60c22e5f | 192 | } |
2dbcb756 | 193 | |
0f82335a | 194 | if (bstate != 0) { |
9b273918 | 195 | /* Reactivating a user; get his group list */ |
14d69e25 | 196 | code = moira_connect(); |
9b273918 | 197 | if (code) { |
198 | critical_alert("incremental", | |
199 | "Error contacting Moira server to retrieve grouplist of user %s: %s", | |
200 | after[U_NAME], error_message(code)); | |
201 | return; | |
202 | } | |
2dbcb756 | 203 | av[0] = "ruser"; |
9b273918 | 204 | av[1] = after[U_NAME]; |
205 | code = mr_query("get_lists_of_member", 2, av, | |
206 | add_user_lists, after[U_NAME]); | |
b80d6d78 | 207 | if (code && code != MR_NO_MATCH) |
9b273918 | 208 | critical_alert("incremental", |
209 | "Couldn't retrieve membership of user %s: %s", | |
210 | after[U_NAME], error_message(code)); | |
14d69e25 | 211 | moira_disconnect(); |
9b273918 | 212 | } |
60c22e5f | 213 | return; |
fb8809f4 | 214 | } |
215 | } | |
216 | ||
217 | ||
fb8809f4 | 218 | do_list(before, beforec, after, afterc) |
219 | char **before; | |
220 | int beforec; | |
221 | char **after; | |
222 | int afterc; | |
223 | { | |
8ec23c21 | 224 | register int agid, bgid; |
eb63ae5f | 225 | int ahide, bhide; |
60c22e5f | 226 | long code, id; |
227 | char g1[PR_MAXNAMELEN], g2[PR_MAXNAMELEN]; | |
ed9a5300 | 228 | char *av[2]; |
fb8809f4 | 229 | |
60c22e5f | 230 | agid = bgid = 0; |
8ec23c21 | 231 | if (beforec > L_GID && atoi(before[L_ACTIVE]) && atoi(before[L_GROUP])) { |
60c22e5f | 232 | bgid = atoi(before[L_GID]); |
8ec23c21 | 233 | bhide = atoi(before[L_HIDDEN]); |
234 | } | |
235 | if (afterc > L_GID && atoi(after[L_ACTIVE]) && atoi(after[L_GROUP])) { | |
60c22e5f | 236 | agid = atoi(after[L_GID]); |
8ec23c21 | 237 | ahide = atoi(after[L_HIDDEN]); |
238 | } | |
fb8809f4 | 239 | |
60c22e5f | 240 | if (agid == 0 && bgid == 0) /* Not active groups */ |
241 | return; | |
60c22e5f | 242 | |
60c22e5f | 243 | if (agid && bgid) { |
8ec23c21 | 244 | if (strcmp(after[L_NAME], before[L_NAME])) { |
245 | /* Only a modify is required */ | |
246 | strcpy(g1, "system:"); | |
247 | strcpy(g2, "system:"); | |
248 | strcat(g1, before[L_NAME]); | |
249 | strcat(g2, after[L_NAME]); | |
92d359ba | 250 | |
251 | com_err(whoami, 0, "Changing group %s (gid %d) to %s (gid %d)", | |
252 | before[L_NAME], bgid, after[L_NAME], agid); | |
253 | ||
eb63ae5f | 254 | code = pr_try(pr_ChangeEntry, g1, g2, -agid, ""); |
8ec23c21 | 255 | if (code) { |
256 | critical_alert("incremental", | |
257 | "Couldn't change group %s (id %d) to %s (id %d): %s", | |
258 | before[L_NAME], -bgid, after[L_NAME], -agid, | |
259 | error_message(code)); | |
260 | } | |
261 | } | |
262 | if (ahide != bhide) { | |
92d359ba | 263 | com_err(whoami, 0, "Making group %s (gid %d) %s", |
264 | after[L_NAME], agid, | |
265 | (ahide ? "hidden" : "visible")); | |
eb63ae5f | 266 | code = pr_try(pr_SetFieldsEntry, -agid, PR_SF_ALLBITS, |
267 | (ahide ? PRP_STATUS_ANY : PRP_GROUP_DEFAULT) >>PRIVATE_SHIFT, | |
268 | 0 /*ngroups*/, 0 /*nusers*/); | |
8ec23c21 | 269 | if (code) { |
270 | critical_alert("incremental", | |
271 | "Couldn't set flags of group %s: %s", | |
272 | after[L_NAME], error_message(code)); | |
273 | } | |
60c22e5f | 274 | } |
fb8809f4 | 275 | return; |
276 | } | |
60c22e5f | 277 | if (bgid) { |
92d359ba | 278 | com_err(whoami, 0, "Deleting group %s (gid %d)", |
279 | before[L_NAME], bgid); | |
eb63ae5f | 280 | code = pr_try(pr_DeleteByID, -bgid); |
60c22e5f | 281 | if (code && code != PRNOENT) { |
282 | critical_alert("incremental", | |
283 | "Couldn't delete group %s (id %d): %s", | |
284 | before[L_NAME], -bgid, error_message(code)); | |
285 | } | |
fb8809f4 | 286 | return; |
287 | } | |
60c22e5f | 288 | if (agid) { |
289 | strcpy(g1, "system:"); | |
290 | strcat(g1, after[L_NAME]); | |
291 | strcpy(g2, "system:administrators"); | |
292 | id = -agid; | |
92d359ba | 293 | com_err(whoami, 0, "Creating %s group %s (gid %d)", |
294 | (ahide ? "hidden" : "visible"), after[L_NAME], agid); | |
eb63ae5f | 295 | code = pr_try(pr_CreateGroup, g1, g2, &id); |
60c22e5f | 296 | if (code) { |
297 | critical_alert("incremental", | |
298 | "Couldn't create group %s (id %d): %s", | |
ed9a5300 | 299 | after[L_NAME], id, error_message(code)); |
60c22e5f | 300 | return; |
301 | } | |
8ec23c21 | 302 | if (ahide) { |
eb63ae5f | 303 | code = pr_try(pr_SetFieldsEntry, -agid, PR_SF_ALLBITS, |
304 | (ahide ? PRP_STATUS_ANY : PRP_GROUP_DEFAULT) >>PRIVATE_SHIFT, | |
305 | 0 /*ngroups*/, 0 /*nusers*/); | |
8ec23c21 | 306 | if (code) { |
307 | critical_alert("incremental", | |
308 | "Couldn't set flags of group %s: %s", | |
309 | after[L_NAME], error_message(code)); | |
310 | } | |
311 | } | |
60c22e5f | 312 | |
313 | /* We need to make sure the group is properly populated */ | |
c69d9f2c | 314 | if (beforec < L_ACTIVE) return; |
60c22e5f | 315 | |
14d69e25 | 316 | code = moira_connect(); |
ed9a5300 | 317 | if (code) { |
318 | critical_alert("incremental", | |
319 | "Error contacting Moira server to resolve %s: %s", | |
320 | after[L_NAME], error_message(code)); | |
321 | return; | |
322 | } | |
cdf6674b | 323 | av[0] = after[L_NAME]; |
324 | code = mr_query("get_end_members_of_list", 1, av, | |
063819ef | 325 | add_list_members, after[L_NAME]); |
326 | if (code) | |
327 | critical_alert("incremental", | |
328 | "Couldn't retrieve full membership of list %s: %s", | |
329 | after[L_NAME], error_message(code)); | |
14d69e25 | 330 | moira_disconnect(); |
ed9a5300 | 331 | return; |
60c22e5f | 332 | } |
fb8809f4 | 333 | } |
334 | ||
335 | ||
f2cf09f5 | 336 | |
337 | #define LM_EXTRA_ACTIVE (LM_END) | |
338 | #define LM_EXTRA_PUBLIC (LM_END+1) | |
339 | #define LM_EXTRA_HIDDEN (LM_END+2) | |
340 | #define LM_EXTRA_MAILLIST (LM_END+3) | |
341 | #define LM_EXTRA_GROUP (LM_END+4) | |
342 | #define LM_EXTRA_GID (LM_END+5) | |
343 | #define LM_EXTRA_END (LM_END+6) | |
344 | ||
fb8809f4 | 345 | do_member(before, beforec, after, afterc) |
346 | char **before; | |
347 | int beforec; | |
348 | char **after; | |
349 | int afterc; | |
350 | { | |
60c22e5f | 351 | int code; |
352 | char *p; | |
60c22e5f | 353 | |
f2cf09f5 | 354 | if (afterc) { |
51b6cfe7 | 355 | if (afterc < LM_EXTRA_END) { |
f2cf09f5 | 356 | return; |
f2cf09f5 | 357 | } else |
a859d2ed | 358 | if (!atoi(after[LM_EXTRA_ACTIVE]) || !atoi(after[LM_EXTRA_GROUP])) |
f2cf09f5 | 359 | return; |
360 | ||
361 | edit_group(1, after[LM_LIST], after[LM_TYPE], after[LM_MEMBER]); | |
362 | } else if (beforec) { | |
51b6cfe7 | 363 | if (beforec < LM_EXTRA_END) { |
f2cf09f5 | 364 | return; |
f2cf09f5 | 365 | } else |
a859d2ed | 366 | if (!atoi(before[LM_EXTRA_ACTIVE]) || !atoi(before[LM_EXTRA_GROUP])) |
f2cf09f5 | 367 | return; |
e9f07a3b | 368 | edit_group(0, before[LM_LIST], before[LM_TYPE], before[LM_MEMBER]); |
f2cf09f5 | 369 | } |
ed9a5300 | 370 | } |
371 | ||
372 | ||
eb63ae5f | 373 | do_filesys(before, beforec, after, afterc) |
374 | char **before; | |
375 | int beforec; | |
376 | char **after; | |
377 | int afterc; | |
378 | { | |
379 | char cmd[1024]; | |
89e513d9 | 380 | int acreate, atype, btype; |
381 | ||
382 | if (afterc < FS_CREATE) { | |
383 | atype = acreate = 0; | |
384 | } else { | |
385 | atype = !strcmp(after[FS_TYPE], "AFS"); | |
386 | acreate = atoi(after[FS_CREATE]); | |
387 | } | |
388 | ||
eb63ae5f | 389 | if (beforec < FS_CREATE) { |
89e513d9 | 390 | if (acreate == 0 || atype == 0) return; |
eb63ae5f | 391 | |
392 | /* new locker creation */ | |
393 | sprintf(cmd, "%s/perl -I%s %s/afs_create.pl %s %s %s %s %s %s", | |
394 | BIN_DIR, BIN_DIR, BIN_DIR, | |
395 | after[FS_NAME], after[FS_L_TYPE], after[FS_MACHINE], | |
396 | after[FS_PACK], after[FS_OWNER], after[FS_OWNERS]); | |
397 | run_cmd(cmd); | |
398 | return; | |
399 | } | |
89e513d9 | 400 | |
89e513d9 | 401 | btype = !strcmp(before[FS_TYPE], "AFS"); |
eb63ae5f | 402 | if (afterc < FS_CREATE) { |
89e513d9 | 403 | if (btype) |
eb63ae5f | 404 | critical_alert("incremental", |
3b1e9c5f | 405 | "Cannot delete AFS filesystem %s: Operation not supported", |
eb63ae5f | 406 | before[FS_NAME]); |
407 | return; | |
eb63ae5f | 408 | } |
3b1e9c5f | 409 | |
410 | if (!acreate) | |
411 | return; | |
412 | ||
413 | /* Are we dealing with AFS lockers (could be type ERR lockers) */ | |
414 | if (!atype && !btype) | |
415 | if (strcmp(before[FS_TYPE], "ERR") || strcmp(after[FS_TYPE], "ERR")) | |
416 | return; | |
417 | ||
418 | /* By now, we know we are simply changing AFS filesystem attributes. | |
419 | * Operations supported: | |
420 | * Name change: rename/remount | |
421 | * Path change: remount | |
422 | * Type change: ERR<-->AFS | |
423 | */ | |
0f82335a | 424 | |
425 | #if 0 | |
3b1e9c5f | 426 | if (strcmp(before[FS_OWNER], after[FS_OWNER]) || |
3ae9c0f3 | 427 | strcmp(before[FS_OWNERS], after[FS_OWNERS])) |
3b1e9c5f | 428 | { |
429 | critical_alert("incremental", | |
430 | "Cannot change ownership of filesystem %s: Operation not yet supported", | |
431 | after[FS_NAME]); | |
432 | } | |
0f82335a | 433 | #endif |
3b1e9c5f | 434 | |
435 | sprintf(cmd, "%s/perl -I%s %s/afs_rename.pl %s %s %s %s %s %s %s %s %s %s", | |
436 | BIN_DIR, BIN_DIR, BIN_DIR, | |
3ae9c0f3 | 437 | before[FS_NAME], before[FS_MACHINE], before[FS_TYPE], |
3b1e9c5f | 438 | before[FS_L_TYPE], before[FS_PACK], |
3ae9c0f3 | 439 | after[FS_NAME], after[FS_MACHINE], after[FS_TYPE], |
3b1e9c5f | 440 | after[FS_L_TYPE], after[FS_PACK]); |
441 | run_cmd(cmd); | |
eb63ae5f | 442 | } |
443 | ||
444 | ||
445 | do_quota(before, beforec, after, afterc) | |
446 | char **before; | |
447 | int beforec; | |
448 | char **after; | |
449 | int afterc; | |
450 | { | |
451 | char cmd[1024]; | |
452 | ||
453 | if (afterc < Q_DIRECTORY || strcmp("ANY", after[Q_TYPE]) || | |
454 | strncmp("/afs/", after[Q_DIRECTORY], 5)) | |
455 | return; | |
456 | ||
457 | sprintf(cmd, "%s/perl -I%s %s/afs_quota.pl %s %s", | |
458 | BIN_DIR, BIN_DIR, BIN_DIR, | |
459 | after[Q_DIRECTORY], after[Q_QUOTA]); | |
460 | run_cmd(cmd); | |
461 | return; | |
462 | } | |
463 | ||
464 | ||
465 | run_cmd(cmd) | |
466 | char *cmd; | |
467 | { | |
468 | int success=0, tries=0; | |
469 | ||
470 | check_afs(); | |
471 | ||
472 | while (success == 0 && tries < 2) { | |
473 | if (tries++) | |
474 | sleep(90); | |
475 | com_err(whoami, 0, "Executing command: %s", cmd); | |
476 | if (system(cmd) == 0) | |
477 | success++; | |
478 | } | |
479 | if (!success) | |
480 | critical_alert("incremental", "failed command: %s", cmd); | |
481 | } | |
482 | ||
483 | ||
484 | add_user_lists(ac, av, user) | |
485 | int ac; | |
486 | char *av[]; | |
487 | char *user; | |
488 | { | |
2cb1f02c | 489 | if (atoi(av[L_ACTIVE]) && atoi(av[L_GROUP])) /* active group ? */ |
490 | edit_group(1, av[L_NAME], "USER", user); | |
d4ea75d6 | 491 | return 0; |
eb63ae5f | 492 | } |
493 | ||
494 | ||
063819ef | 495 | add_list_members(ac, av, group) |
ed9a5300 | 496 | int ac; |
497 | char *av[]; | |
498 | char *group; | |
499 | { | |
063819ef | 500 | edit_group(1, group, av[0], av[1]); |
d4ea75d6 | 501 | return 0; |
ed9a5300 | 502 | } |
503 | ||
504 | ||
c69d9f2c | 505 | check_user(ac, av, ustate) |
506 | int ac; | |
507 | char *av[]; | |
508 | int *ustate; | |
509 | { | |
2cb1f02c | 510 | *ustate = atoi(av[U_STATE]); |
d4ea75d6 | 511 | return 0; |
c69d9f2c | 512 | } |
513 | ||
514 | ||
ed9a5300 | 515 | edit_group(op, group, type, member) |
516 | int op; | |
517 | char *group; | |
518 | char *type; | |
519 | char *member; | |
520 | { | |
521 | char *p = 0; | |
522 | char buf[PR_MAXNAMELEN]; | |
c69d9f2c | 523 | int code, ustate; |
d1be2e3a | 524 | static char local_realm[REALM_SZ+1] = ""; |
2cb1f02c | 525 | struct member *m; |
ed9a5300 | 526 | |
60c22e5f | 527 | /* The following KERBEROS code allows for the use of entities |
528 | * user@foreign_cell. | |
529 | */ | |
d1be2e3a | 530 | if (!local_realm[0]) |
531 | krb_get_lrealm(local_realm, 1); | |
ed9a5300 | 532 | if (!strcmp(type, "KERBEROS")) { |
533 | p = index(member, '@'); | |
d1be2e3a | 534 | if (p && !strcasecmp(p+1, local_realm)) |
60c22e5f | 535 | *p = 0; |
ed9a5300 | 536 | } else if (strcmp(type, "USER")) |
537 | return; /* invalid type */ | |
60c22e5f | 538 | |
2cb1f02c | 539 | /* Cannot risk doing another query during a callback */ |
540 | /* We could do this simply for type USER, but eventually this may also | |
541 | * dynamically add KERBEROS types to the prdb, and we will need to do | |
542 | * a query to look up the uid of the null-instance user */ | |
543 | if (mr_connections) { | |
544 | m = (struct member *)malloc(sizeof(struct member)); | |
545 | if (!m) { | |
546 | critical_alert("incremental", "Out of memory"); | |
547 | exit(1); | |
548 | } | |
549 | m->op = op; | |
550 | strcpy(m->list, group); | |
551 | strcpy(m->type, type); | |
552 | strcpy(m->member, member); | |
553 | m->next = member_head; | |
554 | member_head = m; | |
555 | return; | |
556 | } | |
557 | ||
ed9a5300 | 558 | strcpy(buf, "system:"); |
559 | strcat(buf, group); | |
92d359ba | 560 | com_err(whoami, 0, "%s %s %s group %s", |
561 | (op ? "Adding" : "Removing"), member, | |
562 | (op ? "to" : "from"), group); | |
d4ea75d6 | 563 | code = 0; |
eb63ae5f | 564 | code=pr_try(op ? pr_AddToGroup : pr_RemoveUserFromGroup, member, buf); |
ed9a5300 | 565 | if (code) { |
c69d9f2c | 566 | if (op==1 && code == PRIDEXIST) return; /* Already added */ |
567 | ||
568 | if (code == PRNOENT) { /* Something is missing */ | |
569 | if (op==0) return; /* Already deleted */ | |
570 | if (!strcmp(type, "KERBEROS")) /* Special instances; ok */ | |
571 | return; | |
572 | ||
573 | /* Check whether the member being added is an active user */ | |
14d69e25 | 574 | code = moira_connect(); |
c69d9f2c | 575 | if (!code) code = mr_query("get_user_by_login", 1, &member, |
576 | check_user, &ustate); | |
577 | if (code) { | |
578 | critical_alert("incremental", | |
579 | "Error contacting Moira server to lookup user %s: %s", | |
580 | member, error_message(code)); | |
581 | } | |
6250582e | 582 | |
583 | /* We don't use moira_disconnect() | |
584 | * because we may already be in the routine. | |
585 | */ | |
586 | mr_disconnect(); | |
587 | mr_connections--; | |
588 | ||
c69d9f2c | 589 | if (!code && ustate!=1 && ustate!=2) return; /* inactive user */ |
32ed4a32 | 590 | code = PRNOENT; |
60c22e5f | 591 | } |
c69d9f2c | 592 | |
593 | critical_alert("incremental", | |
594 | "Couldn't %s %s %s %s: %s", | |
595 | op ? "add" : "remove", member, | |
596 | op ? "to" : "from", buf, | |
597 | error_message(code)); | |
57bc9f26 | 598 | } |
fb8809f4 | 599 | } |
600 | ||
601 | ||
eb63ae5f | 602 | long pr_try(fn, a1, a2, a3, a4, a5, a6, a7, a8) |
603 | long (*fn)(); | |
604 | char *a1, *a2, *a3, *a4, *a5, *a6, *a7, *a8; | |
fb8809f4 | 605 | { |
eb63ae5f | 606 | static int initd=0; |
e2666aac | 607 | register long code; |
eb63ae5f | 608 | register int tries = 0; |
609 | #ifdef DEBUG | |
610 | char fname[64]; | |
611 | #endif | |
612 | ||
613 | check_afs(); | |
d4ea75d6 | 614 | |
7fca28a9 | 615 | if (initd) { |
616 | code=pr_Initialize(0, AFSCONF_CLIENTNAME, 0); | |
617 | } else { | |
618 | code = 0; | |
eb63ae5f | 619 | initd = 1; |
9b9540a1 | 620 | } |
7fca28a9 | 621 | if (!code) code=pr_Initialize(1, AFSCONF_CLIENTNAME, 0); |
622 | if (code) { | |
623 | critical_alert("incremental", "Couldn't initialize libprot: %s", | |
624 | error_message(code)); | |
625 | return; | |
626 | } | |
627 | ||
16838ad1 | 628 | sleep(1); /* give ptserver room */ |
9b9540a1 | 629 | |
eb63ae5f | 630 | while (code = (*fn)(a1, a2, a3, a4, a5, a6, a7, a8)) { |
631 | #ifdef DEBUG | |
632 | long t; | |
633 | t = time(0); | |
634 | if (fn == pr_AddToGroup) strcpy(fname, "pr_AddToGroup"); | |
635 | else if (fn == pr_RemoveUserFromGroup) | |
636 | strcpy(fname, "pr_RemoveUserFromGroup"); | |
637 | else if (fn == pr_CreateUser) strcpy(fname, "pr_CreateUser"); | |
638 | else if (fn == pr_CreateGroup) strcpy(fname, "pr_CreateGroup"); | |
639 | else if (fn == pr_DeleteByID) strcpy(fname, "pr_DeleteByID"); | |
640 | else if (fn == pr_ChangeEntry) strcpy(fname, "pr_ChangeEntry"); | |
641 | else if (fn == pr_SetFieldsEntry) strcpy(fname, "pr_SetFieldsEntry"); | |
642 | else if (fn == pr_AddToGroup) strcpy(fname, "pr_AddToGroup"); | |
16838ad1 | 643 | else |
eb63ae5f | 644 | sprintf(fname, "pr_??? (0x%08x)", (long)fn); |
eb63ae5f | 645 | |
16838ad1 | 646 | com_err(whoami, code, "- %s failed (try %d @%u)", fname, tries+1, t); |
eb63ae5f | 647 | #endif |
16838ad1 | 648 | if (++tries > 2) break; /* 3 tries */ |
649 | ||
650 | if (code == UNOQUORUM) sleep(90); | |
651 | else sleep(15); | |
652 | ||
653 | /* Re-initialize the prdb connection */ | |
654 | code=pr_Initialize(0, AFSCONF_CLIENTNAME, 0); | |
655 | if (!code) code=pr_Initialize(1, AFSCONF_CLIENTNAME, 0); | |
656 | if (code) { | |
657 | critical_alert("incremental", "Couldn't re-initialize libprot: %s", | |
658 | error_message(code)); | |
659 | initd = 0; /* we lost */ | |
660 | break; | |
661 | } | |
9b9540a1 | 662 | } |
eb63ae5f | 663 | return code; |
fb8809f4 | 664 | } |
665 | ||
666 | ||
eb63ae5f | 667 | check_afs() |
fb8809f4 | 668 | { |
eb63ae5f | 669 | int i; |
670 | ||
671 | for (i=0; file_exists(STOP_FILE); i++) { | |
672 | if (i > 30) { | |
673 | critical_alert("incremental", | |
674 | "AFS incremental failed (%s exists): %s", | |
675 | STOP_FILE, tbl_buf); | |
676 | exit(1); | |
677 | } | |
678 | sleep(60); | |
679 | } | |
fb8809f4 | 680 | } |
14d69e25 | 681 | |
682 | ||
14d69e25 | 683 | moira_connect() |
684 | { | |
685 | static char hostname[64]; | |
686 | long code; | |
687 | ||
688 | if (!mr_connections++) { | |
689 | gethostname(hostname, sizeof(hostname)); | |
690 | code = mr_connect(hostname); | |
691 | if (!code) code = mr_auth("afs.incr"); | |
692 | return code; | |
693 | } | |
694 | return 0; | |
695 | } | |
696 | ||
697 | moira_disconnect() | |
698 | { | |
2cb1f02c | 699 | struct member *m; |
700 | ||
14d69e25 | 701 | if (!--mr_connections) { |
32ed4a32 | 702 | mr_disconnect(); |
703 | while(m = member_head) { | |
2cb1f02c | 704 | edit_group(m->op, m->list, m->type, m->member); |
705 | member_head = m->next; | |
706 | free(m); | |
707 | } | |
14d69e25 | 708 | } |
709 | return 0; | |
710 | } |