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