Call rx_InitHost once during daemon startup
[openafs.git] / src / kauth / kaserver.c
index df5a485..d28c736 100644 (file)
@@ -12,6 +12,7 @@
 #include <afs/stds.h>
 
 #include <roken.h>
+#include <afs/opr.h>
 
 #ifdef AFS_NT40_ENV
 #include <WINNT/afsevent.h>
@@ -54,7 +55,6 @@ struct afsconf_dir *KA_conf;  /* for getting cell info */
 int MinHours = 0;
 int npwSums = KA_NPWSUMS;      /* needs to be variable sometime */
 
-#include <stdarg.h>
 #if !defined(AFS_NT40_ENV) && !defined(AFS_LINUX20_ENV) && !defined(AFS_DARWIN_ENV) && !defined(AFS_XBSD_ENV)
 #undef vfprintf
 #define vfprintf(stream,fmt,args) _doprnt(fmt,args,stream)
@@ -69,6 +69,25 @@ KA_rxstat_userok(struct rx_call *call)
     return afsconf_SuperUser(KA_conf, call, NULL);
 }
 
+/**
+ * Return true if this name is a member of the local realm.
+ */
+static int
+KA_IsLocalRealmMatch(void *rock, char *name, char *inst, char *cell)
+{
+    struct afsconf_dir *dir = (struct afsconf_dir *)rock;
+    afs_int32 islocal = 0;     /* default to no */
+    int code;
+
+    code = afsconf_IsLocalRealmMatch(dir, &islocal, name, inst, cell);
+    if (code) {
+       ViceLog(0,
+               ("Failed local realm check; code=%d, name=%s, inst=%s, cell=%s\n",
+                code, name, inst, cell));
+    }
+    return islocal;
+}
+
 afs_int32
 es_Report(char *fmt, ...)
 {
@@ -144,7 +163,7 @@ main(int argc, char *argv[])
     const char *cellservdb, *dbpath, *lclpath;
     int a;
     char arg[32];
-    char default_lclpath[AFSDIR_PATH_MAX];
+    char *default_lclpath;
     int servers;
     int initFlags;
     int level;                 /* security level for Ubik */
@@ -152,6 +171,7 @@ main(int argc, char *argv[])
     char clones[MAXHOSTSPERCELL];
     afs_uint32 host = ntohl(INADDR_ANY);
     char *auditFileName = NULL;
+    struct logOptions logopts;
 
     struct rx_service *tservice;
     struct rx_securityClass *sca[1];
@@ -176,13 +196,15 @@ main(int argc, char *argv[])
 #endif
     osi_audit_init();
 
+    memset(&logopts, 0, sizeof(logopts));
+
     if (argc == 0) {
       usage:
        printf("Usage: kaserver [-noAuth] [-database <dbpath>] "
               "[-auditlog <log path>] [-audit-interface <file|sysvmq>] "
               "[-rxbind] [-localfiles <lclpath>] [-minhours <n>] "
               "[-servers <serverlist>] [-crossrealm] "
-              /*" [-enable_peer_stats] [-enable_process_stats] " */
+              "[-enable_peer_stats] [-enable_process_stats] "
               "[-help]\n");
        exit(1);
     }
@@ -206,8 +228,12 @@ main(int argc, char *argv[])
 
     cellservdb = AFSDIR_SERVER_ETC_DIRPATH;
     dbpath = AFSDIR_SERVER_KADB_FILEPATH;
-    strcompose(default_lclpath, AFSDIR_PATH_MAX, AFSDIR_SERVER_LOCAL_DIRPATH,
-              "/", AFSDIR_KADB_FILE, NULL);
+
+    if (asprintf(&default_lclpath, "%s/%s", AFSDIR_SERVER_LOCAL_DIRPATH,
+                AFSDIR_KADB_FILE) < 0) {
+       fprintf(stderr, "%s: No memory for default local dir path\n", argv[0]);
+       exit(2);
+    }
     lclpath = default_lclpath;
 
     debugOutput = 0;
@@ -298,7 +324,12 @@ main(int argc, char *argv[])
      * text logging. So open the AuthLog file for logging and redirect
      * stdin and stdout to it
      */
-    OpenLog(AFSDIR_SERVER_KALOG_FILEPATH);
+    logopts.lopt_dest = logDest_file;
+    logopts.lopt_filename = AFSDIR_SERVER_KALOG_FILEPATH;
+    logopts.lopt_rotateOnOpen = 1;
+    logopts.lopt_rotateStyle = logRotate_old;
+
+    OpenLog(&logopts);
     SetupLogSignals();
 #endif
 
@@ -309,9 +340,13 @@ main(int argc, char *argv[])
            "Migrating to a Kerberos 5 KDC is advised.  "
            "http://www.openafs.org/no-more-des.html\n"));
 
-    code =
-       afsconf_GetExtendedCellInfo(KA_conf, cell, AFSCONF_KAUTHSERVICE,
-                                   &cellinfo, clones);
+    code = afsconf_GetExtendedCellInfo(KA_conf, cell, AFSCONF_KAUTHSERVICE,
+                                      &cellinfo, clones);
+    if (code) {
+       afs_com_err(whoami, code, "Couldn't read cell configuration");
+       exit(1);
+    }
+
     if (servers) {
        if ((code = ubik_ParseServerList(argc, argv, &myHost, serverList))) {
            afs_com_err(whoami, code, "Couldn't parse server list");
@@ -321,6 +356,11 @@ main(int argc, char *argv[])
        for (i = 1; i < MAXSERVERS; i++) {
            if (!serverList[i])
                break;
+           if (i >= MAXHOSTSPERCELL) {
+               fprintf(stderr,
+                       "Too many ubik servers specified on command line\n");
+               exit(1);
+           }
            cellinfo.hostAddr[i].sin_addr.s_addr = serverList[i];
        }
        cellinfo.numServers = i;
@@ -331,6 +371,9 @@ main(int argc, char *argv[])
        ViceLog(0, ("Using server list from %s cell database.\n", cell));
     }
 
+    /* initialize audit user check */
+    osi_audit_set_user_check(KA_conf, KA_IsLocalRealmMatch);
+
     /* initialize ubik */
     if (level == rxkad_clear)
        ubik_SetClientSecurityProcs(afsconf_ClientAuth, afsconf_UpToDate,
@@ -357,20 +400,28 @@ main(int argc, char *argv[])
         if (AFSDIR_SERVER_NETRESTRICT_FILEPATH ||
             AFSDIR_SERVER_NETINFO_FILEPATH) {
             char reason[1024];
-            ccode = parseNetFiles(SHostAddrs, NULL, NULL,
-                                           ADDRSPERSITE, reason,
-                                           AFSDIR_SERVER_NETINFO_FILEPATH,
-                                           AFSDIR_SERVER_NETRESTRICT_FILEPATH);
+            ccode = afsconf_ParseNetFiles(SHostAddrs, NULL, NULL,
+                                          ADDRSPERSITE, reason,
+                                          AFSDIR_SERVER_NETINFO_FILEPATH,
+                                          AFSDIR_SERVER_NETRESTRICT_FILEPATH);
         } else
        {
             ccode = rx_getAllAddr(SHostAddrs, ADDRSPERSITE);
         }
         if (ccode == 1) {
             host = SHostAddrs[0];
-           rx_InitHost(host, htons(AFSCONF_KAUTHPORT));
        }
     }
 
+    code = rx_InitHost(host, htons(AFSCONF_KAUTHPORT));
+    if (code) {
+       afs_com_err(whoami, code, "rx init failed");
+       exit(2);
+    }
+
+    /* Disable jumbograms */
+    rx_SetNoJumbo();
+
     if (servers)
        code =
            ubik_ServerInit(myHost, htons(AFSCONF_KAUTHPORT), serverList,
@@ -385,10 +436,7 @@ main(int argc, char *argv[])
        exit(2);
     }
 
-    sca[RX_SCINDEX_NULL] = rxnull_NewServerSecurityObject();
-
-    /* Disable jumbograms */
-    rx_SetNoJumbo();
+    sca[RX_SECIDX_NULL] = rxnull_NewServerSecurityObject();
 
     tservice =
        rx_NewServiceHost(host, 0, KA_AUTHENTICATION_SERVICE,
@@ -411,9 +459,9 @@ main(int argc, char *argv[])
     rx_SetMinProcs(tservice, 1);
     rx_SetMaxProcs(tservice, 1);
 
-    scm[RX_SCINDEX_NULL] = sca[RX_SCINDEX_NULL];
-    scm[RX_SCINDEX_VAB] = 0;
-    scm[RX_SCINDEX_KAD] =
+    scm[RX_SECIDX_NULL] = sca[RX_SECIDX_NULL];
+    scm[RX_SECIDX_VAB] = 0;
+    scm[RX_SECIDX_KAD] =
        rxkad_NewServerSecurityObject(rxkad_crypt, 0, kvno_admin_key, 0);
     tservice =
        rx_NewServiceHost(host, 0, KA_MAINTENANCE_SERVICE, "Maintenance", scm, 3,