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