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