]> andersk Git - moira.git/blobdiff - afssync/sync.dc
Punt restriction against apostrophes in strings: the new server
[moira.git] / afssync / sync.dc
index 5831e1530f13569ff1026aaf83039a2fe3a65f03..03e4c2940bfe0860fe2b08f5c4979d13260c6f98 100644 (file)
@@ -9,6 +9,7 @@
 #include <mit-copyright.h>
 #include <stdio.h>
 #include <sys/file.h>
+#include <strings.h>
 
 #include <rx/xdr.h>
 #include "ptint.h"
 #include <moira_site.h>
 #include <ctype.h>
 
+/* The following enables the processing of .root instances */
+#define DO_KERBEROS
+
 EXEC SQL INCLUDE sqlca;
 
+EXEC SQL BEGIN DECLARE SECTION;
+char db[33] = "moira";
+EXEC SQL END DECLARE SECTION;
+
 #define min(x,y)       ((x) < (y) ? (x) : (y))
 char *whoami = "sync";
 
-char *malloc(), *strsave();
 int dbase_fd;
 
 int ucount = 0;
 int gcount = 0;
+int kcount = 0;
 int mcount = 0;
 
 struct hash *users;
 struct hash *groups;
+#ifdef DO_KERBEROS
+struct hash *strings;
+#endif
 
 struct member {
     struct entry *user;
@@ -45,21 +56,13 @@ struct entry {
     struct member *members;
 };
 
-int ingerr();
-
 main(argc, argv)
 int argc;
 char **argv;
 {
-    EXEC SQL BEGIN DECLARE SECTION;
-    char db[9];
-    EXEC SQL END DECLARE SECTION;
-
     int status;
     long t;
 
-    strcpy(db, "moira");
-
     if (argc > 2 && !strcmp(argv[1], "-db")) {
        strncpy(db, argv[2], sizeof(db)-1);
        argc -= 2;
@@ -79,9 +82,13 @@ char **argv;
     initialize_sms_error_table();
     initialize_pt_error_table();
     Initdb();                                  /* Initialize prdb */
+    setlinebuf(stdout);
     
     users = create_hash(10000);
     groups = create_hash(15000);
+#ifdef DO_KERBEROS
+    strings = create_hash(1000);
+#endif
 
     EXEC SQL WHENEVER SQLERROR CALL sqlerr;
 #ifsql INGRES
@@ -96,8 +103,8 @@ char **argv;
     do_groups();
 
     t = time(0);
-    fprintf(stderr, "Done (%d users, %d groups, %d members): %s",
-           ucount, gcount, mcount, ctime(&t));
+    fprintf(stderr, "Done (%d users, %d groups, %d kerberos, %d members): %s",
+           ucount, gcount, kcount, mcount, ctime(&t));
 
 #ifsql INGRES
     EXEC SQL DISCONNECT;
@@ -164,8 +171,8 @@ do_passwd()
 do_groups()
 {
     EXEC SQL BEGIN DECLARE SECTION;
-    char name[33];
-    int gid, id, lid, hide;
+    char name[33], string[129];
+    int gid, id, lid, hide, ustatus;
     EXEC SQL END DECLARE SECTION;
 
     long status, pos;
@@ -232,20 +239,93 @@ do_groups()
     EXEC SQL CLOSE l_cursor;
 
     t = time(0);
-    fprintf(stderr, "Doing user members: %s", ctime(&t));
+    fprintf(stderr, "Reading/preparing members: %s", ctime(&t));
 
     EXEC SQL DECLARE m_cursor CURSOR FOR
-       SELECT m.list_id, m.member_id
+       SELECT m.list_id, m.member_id, m.member_type
        FROM imembers m
-       WHERE m.member_type='USER'
        ORDER BY member_id;
     EXEC SQL OPEN m_cursor;
     while (1) {
-       EXEC SQL FETCH m_cursor INTO :lid, :id;
+       EXEC SQL FETCH m_cursor INTO :lid, :id, :name;
        if (sqlca.sqlcode != 0) break;
-       
-       if ((u = (struct entry *)hash_lookup(users, id)) &&
-           (g = (struct entry *)hash_lookup(groups, lid))) {
+
+       if (!(g = (struct entry *)hash_lookup(groups, lid)))
+           continue;
+
+       strtrim(name);
+       if (!strcmp(name, "USER")) {
+           if (u = (struct entry *)hash_lookup(users, id)) {
+               m = (struct member *)malloc(sizeof(struct member));
+               m->user = u;
+               m->group = g;
+               m->unext = u->members;
+               m->gnext = g->members;
+               u->members = g->members = m;
+               mcount++;
+           }
+       }
+#ifdef DO_KERBEROS
+       else if (!strcmp(name, "KERBEROS")) {
+           if (!(u = (struct entry *)hash_lookup(strings, id))) {
+               char *p;
+               
+               EXEC SQL SELECT string INTO :string
+                   FROM strings WHERE string_id = :id;
+
+               aid = 0;
+               strtrim(string);
+
+               if (p = (char *)index(string, '@')) {
+                   *p = 0;
+                   if (strcmp(p+1, "ATHENA.MIT.EDU")) aid = -1;
+               } else aid = -1;
+
+               if ((p = (char *)index(string, '.')) && (p-string) < 9 &&
+                   !strcmp(p+1, "root"))
+               {
+                   strncpy(name, string, p-string);
+                   name[p-string] = 0;
+               } else aid = -1;
+
+               if (aid == 0) {
+                   EXEC SQL DECLARE k_cursor2 CURSOR FOR
+                       SELECT uid, status
+                       FROM users
+                       WHERE login = :name
+                       ORDER BY uid;
+                   EXEC SQL OPEN k_cursor2;
+                   while (1) {
+                       if (sqlca.sqlcode) break;
+                       EXEC SQL FETCH k_cursor2 INTO :lid, :ustatus;
+                       if (ustatus==1 || ustatus==2) aid = lid+65536;
+                   }
+                   EXEC SQL CLOSE k_cursor2;
+               }
+
+               if (aid > 0) {
+                   if (FindByID(0,aid))
+                       status = PRIDEXIST;
+                   else
+                       status = CreateEntry(0, string, &aid, 1 /*idflag*/,
+                                            0 /*gflag*/, SYSADMINID /*oid*/,
+                                            SYSADMINID /*cid */);
+                   if (status) {
+                       fprintf(stderr, "Error adding %s (id %d): %s\n",
+                               string, aid, error_message(status));
+                       if (status != PRIDEXIST) aid = 0;
+                   }
+               } else
+                   aid = 0;
+           
+               u = (struct entry *)malloc(sizeof(struct entry));
+               u->id = aid;
+               u->members = 0;
+               hash_store(strings, id, u);
+               if (aid) kcount++;
+           }
+           if (u->id == 0) continue;
+
            m = (struct member *)malloc(sizeof(struct member));
            m->user = u;
            m->group = g;
@@ -254,9 +334,13 @@ do_groups()
            u->members = g->members = m;
            mcount++;
        }
+#endif
     }
     EXEC SQL CLOSE m_cursor;
 
+    t = time(0);
+    fprintf(stderr, "Doing members: %s", ctime(&t));
+
     /* Do the bulk of the membership quickly.
      * Add PRSIZE members into the user/group record.  After that, we
      * require the use of continuation records, but we assume this is
@@ -281,6 +365,27 @@ do_groups()
            }
        }
     }
+#ifdef DO_KERBEROS
+    for (p = &(strings->data[strings->size - 1]); p >= strings->data; p--) {
+       for (b = *p; b; b = b->next) {
+           if ((u = (struct entry *)b->data)->members == 0)
+               continue;
+           pos = FindByID(0, u->id);
+           pr_Read(0, 0, pos, &uentry, sizeof(uentry));
+           for (t=0, m=u->members; m && t<PRSIZE; m=m->unext, t++)
+               uentry.entries[t] = htonl(m->group->id);
+           uentry.count = htonl(t);
+           pr_Write(0, 0, pos, &uentry, sizeof(uentry));
+           if (m) {
+               pr_ReadEntry(0, 0, pos, &uentry);
+               while (m) {
+                   AddToEntry(0, &uentry, pos, m->group->id);
+                   m = m->unext;
+               }
+           }
+       }
+    }
+#endif
     for (p = &(groups->data[groups->size - 1]); p >= groups->data; p--) {
        for (b = *p; b; b = b->next) {
            if ((g = (struct entry *)b->data)->members == 0)
This page took 0.041291 seconds and 4 git commands to generate.