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