Use asprintf for string construction
[openafs.git] / src / budb / server.c
index e107aaa..d78b282 100644 (file)
@@ -13,8 +13,6 @@
 
 #include <roken.h>
 
-#include <fcntl.h>
-
 #ifdef AFS_NT40_ENV
 #include <WINNT/afsevent.h>
 #endif
@@ -74,6 +72,24 @@ 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_uint32 *myHost,
                     afs_uint32 *serverList)
@@ -167,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;
     }
 
@@ -313,17 +324,13 @@ truncateDatabase(void)
     afs_int32 code = 0;
     int fd;
 
-    path = malloc(strlen(globalConfPtr->databaseDirectory) +
-                 strlen(globalConfPtr->databaseName) +
-                 strlen(globalConfPtr->databaseExtension) + 1);
+    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;
@@ -482,6 +489,9 @@ main(int argc, char **argv)
            ERROR(code);
     }
 
+    /* initialize audit user check */
+    osi_audit_set_user_check(BU_conf, BU_IsLocalRealmMatch);
+
     /* initialize ubik */
     ubik_SetClientSecurityProcs(afsconf_ClientAuth, afsconf_UpToDate, BU_conf);
     ubik_SetServerSecurityProcs(afsconf_BuildServerSecurityObjects,
@@ -492,16 +502,11 @@ main(int argc, char **argv)
 
     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) {
@@ -523,6 +528,9 @@ main(int argc, char **argv)
        }
     }
 
+    /* Disable jumbograms */
+    rx_SetNoJumbo();
+
     code = ubik_ServerInitByInfo (globalConfPtr->myHost,
                                  htons(AFSCONF_BUDBPORT),
                                  &cellinfo,
@@ -538,9 +546,6 @@ main(int argc, char **argv)
 
     afsconf_BuildServerSecurityObjects(BU_conf, &securityClasses, &numClasses);
 
-    /* Disable jumbograms */
-    rx_SetNoJumbo();
-
     tservice =
        rx_NewServiceHost(host, 0, BUDB_SERVICE, "BackupDatabase",
                          securityClasses, numClasses, BUDB_ExecuteRequest);