source: trunk/third/moira/incremental/afs/afs.c @ 24319

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