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