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