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