]> andersk Git - moira.git/blob - incremental/afs.c
140357220f6aa4b2a5dc2b27b200fc1b56bc6e47
[moira.git] / incremental / 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 <stdio.h>
11 #include <stdlib.h>
12 #include <sys/resource.h>
13 #include <sys/types.h>
14 #include <sys/utsname.h>
15 #include <sys/file.h>
16 #include <string.h>
17 #include <unistd.h>
18
19 #include <com_err.h>
20 #include <krb.h>
21 #include <moira.h>
22 #include <moira_site.h>
23
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>
29
30 #define STOP_FILE "/moira/afs/noafs"
31 #define file_exists(file) (access((file), F_OK) == 0)
32
33 char *whoami;
34
35 /* Main stub routines */
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);
41
42 /* Support stub routines */
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);
48 long pr_try();
49 void check_afs(void);
50 int moira_connect(void);
51 int moira_disconnect(void);
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();
62 extern long pr_SIdToName();
63
64 static char tbl_buf[1024];
65 static struct member {
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)
76 {
77   int beforec, afterc, i;
78   char *table, **before, **after;
79   struct rlimit rl;
80
81   getrlimit(RLIMIT_NOFILE, &rl);
82   for (i = rl.rlim_cur; i > 2; i--)
83     close(i);
84
85   whoami = ((whoami = strrchr(argv[0], '/')) ? whoami+1 : argv[0]);
86
87   table = argv[1];
88   beforec = atoi(argv[2]);
89   before = &argv[4];
90   afterc = atoi(argv[3]);
91   after = &argv[4 + beforec];
92
93   setvbuf(stdout, NULL, _IOLBF, BUFSIZ);
94
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]);
102     }
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]);
109     }
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);
127 }
128
129
130 void do_user(char **before, int beforec, char **after, int afterc)
131 {
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;
153
154   if (astate == bstate && auid == buid &&
155       !strcmp(before[U_NAME], after[U_NAME]))
156     /* No AFS related attributes have changed */
157     return;
158
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));
172         }
173       return;
174     }
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));
185         }
186       return;
187     }
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))
204             return;
205         }
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         }
212
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));
233             }
234           moira_disconnect();
235         }
236       return;
237     }
238 }
239
240
241 void do_list(char **before, int beforec, char **after, int afterc)
242 {
243   int agid, bgid;
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]);
254     }
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]);
259     }
260
261   if (agid == 0 && bgid == 0)                   /* Not active groups */
262     return;
263
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));
283             }
284         }
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));
297             }
298         }
299       return;
300     }
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));
310         }
311       return;
312     }
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))
327             return;
328         }
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));
345             }
346         }
347
348       /* We need to make sure the group is properly populated */
349       if (beforec < L_ACTIVE)
350         return;
351
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;
359         }
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;
371     }
372 }
373
374
375
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
384 void do_member(char **before, int beforec, char **after, int afterc)
385 {
386   if (afterc)
387     {
388       if (afterc < LM_EXTRA_END)
389         return;
390       else
391         {
392           if (!atoi(after[LM_EXTRA_ACTIVE]) || !atoi(after[LM_EXTRA_GROUP]))
393             return;
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         {
404           if (!atoi(before[LM_EXTRA_ACTIVE]) || !atoi(before[LM_EXTRA_GROUP]))
405             return;
406         }
407       edit_group(0, before[LM_LIST], before[LM_TYPE], before[LM_MEMBER]);
408     }
409 }
410
411
412 void do_filesys(char **before, int beforec, char **after, int afterc)
413 {
414   char cmd[1024];
415   int acreate, atype, bcreate, btype;
416
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     }
424
425   if (beforec < FS_CREATE)
426     {
427       if (acreate == 0 || atype == 0)
428         return;
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;
437     }
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;
447     }
448
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"))
456         return;
457     }
458
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    */
465
466 #if 0
467   if (strcmp(before[FS_OWNER], after[FS_OWNER]) ||
468       strcmp(before[FS_OWNERS], after[FS_OWNERS]))
469     {
470       critical_alert("incremental",
471                      "Cannot change ownership of filesystem %s: Operation not yet supported",
472                      after[FS_NAME]);
473     }
474 #endif
475
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);
483 }
484
485
486 void do_quota(char **before, int beforec, char **after, int afterc)
487 {
488   char cmd[1024];
489
490   if (afterc < Q_DIRECTORY || strcmp("ANY", after[Q_TYPE]) ||
491       strncmp("/afs/", after[Q_DIRECTORY], 5))
492     return;
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;
499 }
500
501
502 void run_cmd(char *cmd)
503 {
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++;
515     }
516   if (!success)
517     critical_alert("incremental", "failed command: %s", cmd);
518 }
519
520
521 int add_user_lists(int ac, char **av, void *user)
522 {
523   if (atoi(av[L_ACTIVE]) && atoi(av[L_GROUP]))  /* active group ? */
524     edit_group(1, av[L_NAME], "USER", user);
525   return 0;
526 }
527
528
529 int add_list_members(int ac, char **av, void *group)
530 {
531   edit_group(1, group, av[0], av[1]);
532   return 0;
533 }
534
535 int check_user(int ac, char **av, void *ustate)
536 {
537   *(int *)ustate = atoi(av[U_STATE]);
538   return 0;
539 }
540
541
542 void edit_group(int op, char *group, char *type, char *member)
543 {
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);
575         }
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;
583     }
584
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));
614             }
615
616           /* We don't use moira_disconnect()
617            * because we may already be in the routine.
618            */
619           mr_disconnect();
620           mr_connections--;
621
622           if (!code && ustate!=1 && ustate!=2)
623             return; /* inactive user */
624           code = PRNOENT;
625         }
626
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));
631     }
632 }
633
634
635 long pr_try(long (*fn)(), char *a1, char *a2, char *a3, char *a4, char *a5,
636             char *a6, char *a7, char *a8)
637 {
638   static int initd = 0;
639   long code;
640   int tries = 0;
641
642   check_afs();
643
644   if (initd)
645     code = pr_Initialize(0, AFSCONF_CLIENTNAME, 0);
646   else
647     {
648       code = 0;
649       initd = 1;
650     }
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));
657       return code;
658     }
659
660   sleep(1);                                     /* give ptserver room */
661
662   while ((code = (*fn)(a1, a2, a3, a4, a5, a6, a7, a8)))
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;
682         }
683     }
684   return code;
685 }
686
687
688 void check_afs(void)
689 {
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);
700         }
701       sleep(60);
702     }
703 }
704
705
706 int moira_connect(void)
707 {
708   long code;
709
710   if (!mr_connections++)
711     {
712       struct utsname uts;
713       uname(&uts);
714       code = mr_connect(uts.nodename);
715       if (!code)
716         code = mr_auth("afs.incr");
717       return code;
718     }
719   return 0;
720 }
721
722 int moira_disconnect(void)
723 {
724   struct member *m;
725
726   if (!--mr_connections)
727     {
728       mr_disconnect();
729       while ((m = member_head))
730         {
731           edit_group(m->op, m->list, m->type, m->member);
732           member_head = m->next;
733           free(m);
734         }
735     }
736   return 0;
737 }
This page took 0.681294 seconds and 3 git commands to generate.