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