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