budb: Fail if afsconf_GetExtendedCellInfo does
[openafs.git] / src / budb / server.c
index d63591b..ee4536c 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * Copyright 2000, International Business Machines Corporation and others.
  * All Rights Reserved.
- * 
+ *
  * This software has been released under the terms of the IBM Public
  * License.  For details, see the LICENSE file in the top-level source
  * directory or online at http://www.openafs.org/dl/license10.html
@@ -9,24 +9,13 @@
 
 #include <afsconfig.h>
 #include <afs/param.h>
+#include <afs/stds.h>
 
+#include <roken.h>
 
-#include <fcntl.h>
-#include <sys/stat.h>
 #ifdef AFS_NT40_ENV
-#include <winsock2.h>
 #include <WINNT/afsevent.h>
-#else
-#include <netinet/in.h>
-#include <sys/file.h>
-#include <sys/time.h>
-#include <netdb.h>
 #endif
-#include <string.h>
-#include <afs/stds.h>
-#include <sys/types.h>
-#include <time.h>
-#include <stdio.h>
 #include <afs/cmd.h>
 #include <lwp.h>
 #include <ubik.h>
 #include <afs/bubasics.h>
 #include <afs/afsutil.h>
 #include <afs/com_err.h>
-#include <errno.h>
-#ifdef AFS_AIX32_ENV
-#include <signal.h>
-#endif
+#include <afs/audit.h>
+
 #include "budb_errs.h"
 #include "database.h"
 #include "error_macros.h"
 #include "budb_internal.h"
 #include "globals.h"
-#include "afs/audit.h"
 
 struct ubik_dbase *BU_dbase;
 struct afsconf_dir *BU_conf;   /* for getting cell info */
@@ -57,7 +43,7 @@ int truncateDatabase(void);
 int parseServerList(struct cmd_item *);
 
 char lcell[MAXKTCREALMLEN];
-afs_int32 myHost = 0;
+afs_uint32 myHost = 0;
 int helpOption;
 
 /* server's global configuration information. This is exported to other
@@ -79,14 +65,6 @@ int lwps   = 3;
 #define ADDRSPERSITE 16         /* Same global is in rx/rx_user.c */
 afs_uint32 SHostAddrs[ADDRSPERSITE];
 
-#if defined(AFS_PTHREAD_ENV)
-char *
-threadNum(void)
-{
-    return pthread_getspecific(rx_thread_id_key);
-}
-#endif
-
 /* check whether caller is authorized to manage RX statistics */
 int
 BU_rxstat_userok(struct rx_call *call)
@@ -94,9 +72,27 @@ BU_rxstat_userok(struct rx_call *call)
     return afsconf_SuperUser(BU_conf, call, NULL);
 }
 
+/**
+ * Return true if this name is a member of the local realm.
+ */
+int
+BU_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) {
+       LogError(code, "Failed local realm check; name=%s, inst=%s, cell=%s\n",
+                name, inst, cell);
+    }
+    return islocal;
+}
+
 int
-convert_cell_to_ubik(struct afsconf_cell *cellinfo, afs_int32 *myHost, 
-                    afs_int32 *serverList)
+convert_cell_to_ubik(struct afsconf_cell *cellinfo, afs_uint32 *myHost,
+                    afs_uint32 *serverList)
 {
     int i;
     char hostname[64];
@@ -109,7 +105,7 @@ convert_cell_to_ubik(struct afsconf_cell *cellinfo, afs_int32 *myHost,
        printf("prserver: couldn't get address of this host.\n");
        BUDB_EXIT(1);
     }
-    memcpy(myHost, th->h_addr, sizeof(afs_int32));
+    memcpy(myHost, th->h_addr, sizeof(afs_uint32));
 
     for (i = 0; i < cellinfo->numServers; i++)
        /* omit my host from serverList */
@@ -126,7 +122,7 @@ convert_cell_to_ubik(struct afsconf_cell *cellinfo, afs_int32 *myHost,
  *      If it were, this routine would never have been called.
  */
 static int
-MyBeforeProc(register struct cmd_syndesc *as, void *arock)
+MyBeforeProc(struct cmd_syndesc *as, void *arock)
 {
     helpOption = 0;
     return 0;
@@ -175,6 +171,8 @@ initializeArgHandler(void)
     cmd_AddParm(cptr, "-rxbind", CMD_FLAG, CMD_OPTIONAL,
                "bind the Rx socket (primary interface only)");
 
+    cmd_AddParm(cptr, "-audit-interface", CMD_SINGLE, CMD_OPTIONAL,
+               "audit interface (file or sysvmq)");
 }
 
 int
@@ -185,22 +183,17 @@ argHandler(struct cmd_syndesc *as, void *arock)
 
     /* database directory */
     if (as->parms[0].items != 0) {
-       globalConfPtr->databaseDirectory =
-           (char *)malloc(strlen(as->parms[0].items->data) + 1);
+       globalConfPtr->databaseDirectory = strdup(as->parms[0].items->data);
        if (globalConfPtr->databaseDirectory == 0)
            BUDB_EXIT(-1);
-       strcpy(globalConfPtr->databaseDirectory, as->parms[0].items->data);
     }
 
     /* -cellservdb, cell configuration directory */
     if (as->parms[1].items != 0) {
-       globalConfPtr->cellConfigdir =
-           (char *)malloc(strlen(as->parms[1].items->data) + 1);
+       globalConfPtr->cellConfigdir = strdup(as->parms[1].items->data);
        if (globalConfPtr->cellConfigdir == 0)
            BUDB_EXIT(-1);
 
-       strcpy(globalConfPtr->cellConfigdir, as->parms[1].items->data);
-
        globalConfPtr->debugFlags |= DF_RECHECKNOAUTH;
     }
 
@@ -226,11 +219,7 @@ argHandler(struct cmd_syndesc *as, void *arock)
     else
        ubik_nBuffers = 0;
 
-    if (as->parms[7].items != 0) {
-       char *fileName = as->parms[7].items->data;
-
-        osi_audit_file(fileName);
-    }
+    /* param 7 (-auditlog) handled below */
 
     /* user provided the number of threads    */
     if (as->parms[8].items != 0) {
@@ -252,6 +241,25 @@ argHandler(struct cmd_syndesc *as, void *arock)
        rxBind = 1;
     }
 
+    /* -audit-interface */
+    if (as->parms[10].items != 0) {
+       char *interface = as->parms[10].items->data;
+
+       if (osi_audit_interface(interface)) {
+           printf("Invalid audit interface '%s'\n", interface);
+           BUDB_EXIT(-1);
+       }
+    }
+
+    /* -auditlog */
+    /* needs to be after -audit-interface, so we osi_audit_interface
+     * before we osi_audit_file */
+    if (as->parms[7].items != 0) {
+       char *fileName = as->parms[7].items->data;
+
+        osi_audit_file(fileName);
+    }
+
     return 0;
 }
 
@@ -277,7 +285,7 @@ parseServerList(struct cmd_item *itemPtr)
     LogDebug(3, "%d servers\n", nservers);
 
     /* now can allocate the space for the server arguments */
-    serverArgs = (char **)malloc((nservers + 2) * sizeof(char *));
+    serverArgs = malloc((nservers + 2) * sizeof(char *));
     if (serverArgs == 0)
        ERROR(-1);
 
@@ -299,7 +307,7 @@ parseServerList(struct cmd_item *itemPtr)
        ERROR(code);
 
     /* free space for the server args */
-    free((char *)serverArgs);
+    free(serverArgs);
 
   error_exit:
     return (code);
@@ -316,18 +324,13 @@ truncateDatabase(void)
     afs_int32 code = 0;
     int fd;
 
-    path =
-       (char *)malloc(strlen(globalConfPtr->databaseDirectory) +
-                      strlen(globalConfPtr->databaseName) +
-                      strlen(globalConfPtr->databaseExtension) + 1);
-    if (path == 0)
+    asprintf(&path, "%s%s%s",
+            globalConfPtr->databaseDirectory,
+            globalConfPtr->databaseName,
+            globalConfPtr->databaseExtension);
+    if (path == NULL)
        ERROR(-1);
 
-    /* construct the database name */
-    strcpy(path, globalConfPtr->databaseDirectory);
-    strcat(path, globalConfPtr->databaseName);
-    strcat(path, globalConfPtr->databaseExtension);
-
     fd = open(path, O_RDWR, 0755);
     if (!fd) {
        code = errno;
@@ -338,6 +341,8 @@ truncateDatabase(void)
            close(fd);
     }
 
+    free(path);
+
   error_exit:
     return (code);
 }
@@ -360,7 +365,8 @@ main(int argc, char **argv)
     char  clones[MAXHOSTSPERCELL];
 
     struct rx_service *tservice;
-    struct rx_securityClass *sca[3];
+    struct rx_securityClass **securityClasses;
+    afs_int32 numClasses;
 
     extern int rx_stackSize;
 
@@ -375,8 +381,8 @@ main(int argc, char **argv)
 
 #ifdef AFS_AIX32_ENV
     /*
-     * The following signal action for AIX is necessary so that in case of a 
-     * crash (i.e. core is generated) we can include the user's data section 
+     * The following signal action for AIX is necessary so that in case of a
+     * crash (i.e. core is generated) we can include the user's data section
      * in the core dump. Unfortunately, by default, only a partial core is
      * generated which, in many cases, isn't too useful.
      */
@@ -417,7 +423,7 @@ main(int argc, char **argv)
 /*
     globalConfPtr->log = fopen(DEFAULT_LOGNAME,"a");
     if ( globalConfPtr->log == NULL )
-    {   
+    {
        printf("Can't open log file %s - aborting\n", DEFAULT_LOGNAME);
        BUDB_EXIT(-1);
     }
@@ -426,7 +432,7 @@ main(int argc, char **argv)
     srandom(1);
 
 #ifdef AFS_PTHREAD_ENV
-    SetLogThreadNumProgram( threadNum );
+    SetLogThreadNumProgram( rx_GetThreadNum );
 #endif
 
     /* process the user supplied args */
@@ -474,8 +480,13 @@ main(int argc, char **argv)
 
        LogDebug(1, "Using server list from %s cell database.\n", lcell);
 
-       code = afsconf_GetExtendedCellInfo (BU_conf, lcell, 0, &cellinfo, 
-                                           &clones); 
+       code = afsconf_GetExtendedCellInfo (BU_conf, lcell, 0, &cellinfo,
+                                           clones);
+       if (code) {
+           LogError(0, "Can't read cell information\n");
+           ERROR(code);
+       }
+
        code =
            convert_cell_to_ubik(&cellinfo, &globalConfPtr->myHost,
                                 globalConfPtr->serverList);
@@ -483,43 +494,36 @@ main(int argc, char **argv)
            ERROR(code);
     }
 
-    /* initialize ubik */
-    ubik_CRXSecurityProc = afsconf_ClientAuth;
-    ubik_CRXSecurityRock = BU_conf;
-
-    ubik_SRXSecurityProc = afsconf_ServerAuth;
-    ubik_SRXSecurityRock = BU_conf;
+    /* initialize audit user check */
+    osi_audit_set_user_check(BU_conf, BU_IsLocalRealmMatch);
 
-    ubik_CheckRXSecurityProc = afsconf_CheckAuth;
-    ubik_CheckRXSecurityRock = BU_conf;
+    /* initialize ubik */
+    ubik_SetClientSecurityProcs(afsconf_ClientAuth, afsconf_UpToDate, BU_conf);
+    ubik_SetServerSecurityProcs(afsconf_BuildServerSecurityObjects,
+                               afsconf_CheckAuth, BU_conf);
 
     if (ubik_nBuffers == 0)
        ubik_nBuffers = 400;
 
     LogError(0, "Will allocate %d ubik buffers\n", ubik_nBuffers);
 
-    dbNamePtr =
-       (char *)malloc(strlen(globalConfPtr->databaseDirectory) +
-                      strlen(globalConfPtr->databaseName) + 1);
+    asprintf(&dbNamePtr, "%s%s", globalConfPtr->databaseDirectory,
+            globalConfPtr->databaseName);
     if (dbNamePtr == 0)
        ERROR(-1);
 
-    /* construct the database name */
-    strcpy(dbNamePtr, globalConfPtr->databaseDirectory);
-    strcat(dbNamePtr, globalConfPtr->databaseName);    /* name prefix */
-
     rx_SetRxDeadTime(60);      /* 60 seconds inactive before timeout */
 
     if (rxBind) {
        afs_int32 ccode;
-        if (AFSDIR_SERVER_NETRESTRICT_FILEPATH || 
+        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);
-        } else 
+            ccode = afsconf_ParseNetFiles(SHostAddrs, NULL, NULL,
+                                          ADDRSPERSITE, reason,
+                                          AFSDIR_SERVER_NETINFO_FILEPATH,
+                                          AFSDIR_SERVER_NETRESTRICT_FILEPATH);
+        } else
        {
             ccode = rx_getAllAddr(SHostAddrs, ADDRSPERSITE);
         }
@@ -529,10 +533,13 @@ main(int argc, char **argv)
        }
     }
 
+    /* Disable jumbograms */
+    rx_SetNoJumbo();
+
     code = ubik_ServerInitByInfo (globalConfPtr->myHost,
-                                 htons(AFSCONF_BUDBPORT), 
+                                 htons(AFSCONF_BUDBPORT),
                                  &cellinfo,
-                                 clones,              
+                                 clones,
                                  dbNamePtr,           /* name prefix */
                                  &BU_dbase);
 
@@ -542,18 +549,12 @@ main(int argc, char **argv)
        ERROR(code);
     }
 
-    sca[RX_SCINDEX_NULL] = rxnull_NewServerSecurityObject();
-    sca[RX_SCINDEX_VAB] = 0;
-    sca[RX_SCINDEX_KAD] =
-       rxkad_NewServerSecurityObject(rxkad_clear, BU_conf, afsconf_GetKey,
-                                     NULL);
-
-    /* Disable jumbograms */
-    rx_SetNoJumbo();
+    afsconf_BuildServerSecurityObjects(BU_conf, &securityClasses, &numClasses);
 
     tservice =
-       rx_NewServiceHost(host, 0, BUDB_SERVICE, "BackupDatabase", sca, 3,
-                     BUDB_ExecuteRequest);
+       rx_NewServiceHost(host, 0, BUDB_SERVICE, "BackupDatabase",
+                         securityClasses, numClasses, BUDB_ExecuteRequest);
+
     if (tservice == (struct rx_service *)0) {
        LogError(0, "Could not create backup database rx service\n");
        printf("Could not create backup database rx service\n");
@@ -610,7 +611,7 @@ LogDebug(int level, char *fmt, ... )
     va_list ap;
 
     va_start(ap, fmt);
-       
+
     if (debugging >= level) {
        /* log normally closed so can remove it */
        globalConfPtr->log = fopen(AFSDIR_SERVER_BUDBLOG_FILEPATH, "a");