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