afsconf: Rework security flags
[openafs.git] / src / volser / volmain.c
index f30ae9c..f90b020 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
 #include <afsconfig.h>
 #include <afs/param.h>
 
-RCSID
-    ("$Header$");
+#include <roken.h>
 
 #include <sys/types.h>
+#include <string.h>
+#ifdef HAVE_STDINT_H
+# include <stdint.h>
+#endif
 #ifdef AFS_NT40_ENV
 #include <time.h>
 #include <fcntl.h>
@@ -23,23 +26,13 @@ RCSID
 #include <sys/time.h>
 #include <sys/file.h>
 #include <netinet/in.h>
-#endif
-#ifdef HAVE_STRING_H
-#include <string.h>
-#else
-#ifdef HAVE_STRINGS_H
-#include <strings.h>
-#endif
+#include <unistd.h>
 #endif
 #include <rx/xdr.h>
 #include <afs/afsint.h>
 #include <stdio.h>
 #include <signal.h>
-#ifdef AFS_PTHREAD_ENV
-#include <assert.h>
-#else /* AFS_PTHREAD_ENV */
-#include <afs/assert.h>
-#endif /* AFS_PTHREAD_ENV */
+#include <afs/afs_assert.h>
 #include <afs/prs_fs.h>
 #include <afs/nfs.h>
 #include <lwp.h>
@@ -53,19 +46,23 @@ RCSID
 #include <afs/volume.h>
 #include <afs/partition.h>
 #include <rx/rx.h>
+#include <rx/rxstat.h>
 #include <rx/rx_globals.h>
 #include <afs/auth.h>
-#include <rx/rxkad.h>
 #include <afs/cellconfig.h>
 #include <afs/keys.h>
+#include <afs/dir.h>
 #include <ubik.h>
 #include <fcntl.h>
 #include <sys/stat.h>
 
-#include "volser.h"
 #include <errno.h>
 #include <afs/audit.h>
 #include <afs/afsutil.h>
+#include <lwp.h>
+#include "volser.h"
+#include "volint.h"
+#include "volser_internal.h"
 
 /*@printflike@*/ extern void Log(const char *format, ...);
 /*@printflike@*/ extern void Abort(const char *format, ...);
@@ -74,11 +71,6 @@ RCSID
 #define N_SECURITY_OBJECTS 3
 
 extern struct Lock localLock;
-extern struct volser_trans *TransList();
-#ifndef AFS_PTHREAD_ENV
-extern int (*vol_PollProc) ();
-extern int IOMGR_Poll();
-#endif
 char *GlobalNameHack = NULL;
 int hackIsIn = 0;
 afs_int32 GlobalVolCloneId, GlobalVolParentId;
@@ -86,16 +78,16 @@ int GlobalVolType;
 int VolumeChanged;             /* XXXX */
 static char busyFlags[MAXHELPERS];
 struct volser_trans *QI_GlobalWriteTrans = 0;
-extern void AFSVolExecuteRequest();
-extern void RXSTATS_ExecuteRequest();
 struct afsconf_dir *tdir;
 static afs_int32 runningCalls = 0;
 int DoLogging = 0;
-#define MAXLWP 16
+int debuglevel = 0;
+#define MAXLWP 128
 int lwps = 9;
 int udpBufSize = 0;            /* UDP buffer size for receive */
 
 int rxBind = 0;
+int rxkadDisableDotCheck = 0;
 
 #define ADDRSPERSITE 16         /* Same global is in rx/rx_user.c */
 afs_uint32 SHostAddrs[ADDRSPERSITE];
@@ -106,36 +98,36 @@ afs_uint32 SHostAddrs[ADDRSPERSITE];
                       }
 
 #if defined(AFS_PTHREAD_ENV)
-char *
+int
 threadNum(void)
 {
-    return pthread_getspecific(rx_thread_id_key);
+    return (intptr_t)pthread_getspecific(rx_thread_id_key);
 }
 #endif
 
-static afs_int32
+static void
 MyBeforeProc(struct rx_call *acall)
 {
     VTRANS_LOCK;
     runningCalls++;
     VTRANS_UNLOCK;
-    return 0;
+    return;
 }
 
-static afs_int32
+static void
 MyAfterProc(struct rx_call *acall, afs_int32 code)
 {
     VTRANS_LOCK;
     runningCalls--;
     VTRANS_UNLOCK;
-    return 0;
+    return;
 }
 
 /* Called every GCWAKEUP seconds to try to unlock all our partitions,
- * if we're idle and there are no active transactions 
+ * if we're idle and there are no active transactions
  */
 static void
-TryUnlock()
+TryUnlock(void)
 {
     /* if there are no running calls, and there are no active transactions, then
      * it should be safe to release any partition locks we've accumulated */
@@ -148,8 +140,8 @@ TryUnlock()
 }
 
 /* background daemon for timing out transactions */
-static void
-BKGLoop()
+static void*
+BKGLoop(void *unused)
 {
     struct timeval tv;
     int loop = 0;
@@ -158,7 +150,11 @@ BKGLoop()
        tv.tv_sec = GCWAKEUP;
        tv.tv_usec = 0;
 #ifdef AFS_PTHREAD_ENV
+#ifdef AFS_NT40_ENV
+        Sleep(GCWAKEUP * 1000);
+#else
         select(0, 0, 0, 0, &tv);
+#endif
 #else
        (void)IOMGR_Select(0, 0, 0, 0, &tv);
 #endif
@@ -170,12 +166,15 @@ BKGLoop()
            ReOpenLog(AFSDIR_SERVER_VOLSERLOG_FILEPATH);
        }
     }
+
+    return NULL;
 }
 
 /* Background daemon for sleeping so the volserver does not become I/O bound */
 afs_int32 TTsleep, TTrun;
-static void
-BKGSleep()
+#ifndef AFS_PTHREAD_ENV
+static void *
+BKGSleep(void *unused)
 {
     struct volser_trans *tt;
 
@@ -188,13 +187,17 @@ BKGSleep()
 #endif
            VTRANS_LOCK;
            for (tt = TransList(); tt; tt = tt->next) {
+                VTRANS_OBJ_LOCK(tt);
                if ((strcmp(tt->lastProcName, "DeleteVolume") == 0)
                    || (strcmp(tt->lastProcName, "Clone") == 0)
                    || (strcmp(tt->lastProcName, "ReClone") == 0)
                    || (strcmp(tt->lastProcName, "Forward") == 0)
                    || (strcmp(tt->lastProcName, "Restore") == 0)
-                   || (strcmp(tt->lastProcName, "ForwardMulti") == 0))
+                   || (strcmp(tt->lastProcName, "ForwardMulti") == 0)) {
+                    VTRANS_OBJ_UNLOCK(tt);
                    break;
+                }
+                VTRANS_OBJ_UNLOCK(tt);
            }
            if (tt) {
                VTRANS_UNLOCK;
@@ -203,16 +206,32 @@ BKGSleep()
                VTRANS_UNLOCK;
        }
     }
+    return NULL;
 }
+#endif
 
-#ifndef AFS_NT40_ENV
+#ifdef AFS_NT40_ENV
+/* no volser_syscall */
+#elif defined(AFS_SUN511_ENV)
+int
+volser_syscall(afs_uint32 a3, afs_uint32 a4, void *a5)
+{
+    int err, code;
+    code = ioctl_sun_afs_syscall(28 /* AFSCALL_CALL */, a3, a4, a5, 0, 0, 0,
+                                 &err);
+    if (code) {
+       err = code;
+    }
+    return err;
+}
+#else
 int
 volser_syscall(afs_uint32 a3, afs_uint32 a4, void *a5)
 {
     afs_uint32 rcode;
-    void (*old) ();
-
 #ifndef AFS_LINUX20_ENV
+    void (*old) (int);
+
     old = signal(SIGSYS, SIG_IGN);
 #endif
     rcode =
@@ -235,25 +254,26 @@ vol_rxstat_userok(struct rx_call *call)
 }
 
 #include "AFS_component_version_number.c"
-int 
+int
 main(int argc, char **argv)
 {
-    register afs_int32 code;
-    struct rx_securityClass *(securityObjects[3]);
+    afs_int32 code;
+    struct rx_securityClass **securityClasses;
+    afs_int32 numClasses;
     struct rx_service *service;
     struct ktc_encryptionKey tkey;
     int rxpackets = 100;
-    char commandLine[150];
-    int i;
-    int rxJumbograms = 1;      /* default is to send and receive jumbograms. */
+    int rxJumbograms = 0;      /* default is to send and receive jumbograms. */
     int rxMaxMTU = -1;
     int bufSize = 0;           /* temp variable to read in udp socket buf size */
     afs_uint32 host = ntohl(INADDR_ANY);
+    char *auditFileName = NULL;
+    VolumePackageOptions opts;
 
 #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.
      */
@@ -278,12 +298,6 @@ main(int argc, char **argv)
        exit(2);
     }
 
-    for (commandLine[0] = '\0', i = 0; i < argc; i++) {
-       if (i > 0)
-           strcat(commandLine, " ");
-       strcat(commandLine, argv[i]);
-    }
-
     TTsleep = TTrun = 0;
 
     /* parse cmd line */
@@ -295,6 +309,15 @@ main(int argc, char **argv)
            goto usage;
        } else if (strcmp(argv[code], "-rxbind") == 0) {
            rxBind = 1;
+       } else if (strcmp(argv[code], "-allow-dotted-principals") == 0) {
+           rxkadDisableDotCheck = 1;
+       } else if (strcmp(argv[code], "-d") == 0) {
+           if ((code + 1) >= argc) {
+               fprintf(stderr, "missing argument for -d\n");
+               return -1;
+           }
+           debuglevel = atoi(argv[++code]);
+           LogLevel = debuglevel;
        } else if (strcmp(argv[code], "-p") == 0) {
            lwps = atoi(argv[++code]);
            if (lwps > MAXLWP) {
@@ -303,47 +326,29 @@ main(int argc, char **argv)
                lwps = MAXLWP;
            }
        } else if (strcmp(argv[code], "-auditlog") == 0) {
-           int tempfd, flags;
-           FILE *auditout;
-           char oldName[MAXPATHLEN];
-           char *fileName = argv[++code];
+           auditFileName = argv[++code];
 
-#ifndef AFS_NT40_ENV
-           struct stat statbuf;
-           
-           if ((lstat(fileName, &statbuf) == 0) 
-               && (S_ISFIFO(statbuf.st_mode))) {
-               flags = O_WRONLY | O_NONBLOCK;
-           } else 
-#endif
-           {
-               strcpy(oldName, fileName);
-               strcat(oldName, ".old");
-               renamefile(fileName, oldName);
-               flags = O_WRONLY | O_TRUNC | O_CREAT;
+       } else if (strcmp(argv[code], "-audit-interface") == 0) {
+           char *interface = argv[++code];
+
+           if (osi_audit_interface(interface)) {
+               printf("Invalid audit interface '%s'\n", interface);
+               return -1;
            }
-           tempfd = open(fileName, flags, 0666);
-           if (tempfd > -1) {
-               auditout = fdopen(tempfd, "a");
-               if (auditout) {
-                   osi_audit_file(auditout);
-                   osi_audit(VS_StartEvent, 0, AUD_END);
-               } else
-                   printf("Warning: auditlog %s not writable, ignored.\n", fileName);
-           } else
-               printf("Warning: auditlog %s not writable, ignored.\n", fileName);
        } else if (strcmp(argv[code], "-nojumbo") == 0) {
            rxJumbograms = 0;
+       } else if (strcmp(argv[code], "-jumbo") == 0) {
+           rxJumbograms = 1;
        } else if (!strcmp(argv[code], "-rxmaxmtu")) {
            if ((code + 1) >= argc) {
-               fprintf(stderr, "missing argument for -rxmaxmtu\n"); 
-               exit(1); 
+               fprintf(stderr, "missing argument for -rxmaxmtu\n");
+               exit(1);
            }
            rxMaxMTU = atoi(argv[++code]);
-           if ((rxMaxMTU < RX_MIN_PACKET_SIZE) || 
+           if ((rxMaxMTU < RX_MIN_PACKET_SIZE) ||
                (rxMaxMTU > RX_MAX_PACKET_DATA_SIZE)) {
-               printf("rxMaxMTU %d% invalid; must be between %d-%d\n",
-                      rxMaxMTU, RX_MIN_PACKET_SIZE, 
+               printf("rxMaxMTU %d invalid; must be between %d-%" AFS_SIZET_FMT "\n",
+                      rxMaxMTU, RX_MIN_PACKET_SIZE,
                       RX_MAX_PACKET_DATA_SIZE);
                exit(1);
            }
@@ -385,16 +390,16 @@ main(int argc, char **argv)
          usage:
 #ifndef AFS_NT40_ENV
            printf("Usage: volserver [-log] [-p <number of processes>] "
-                  "[-auditlog <log path>] "
-                  "[-nojumbo] [-rxmaxmtu <bytes>] [-rxbind] "
+                  "[-auditlog <log path>] [-d <debug level>] "
+                  "[-nojumbo] [-jumbo] [-rxmaxmtu <bytes>] [-rxbind] [-allow-dotted-principals] "
                   "[-udpsize <size of socket buffer in bytes>] "
                   "[-syslog[=FACILITY]] "
                   "[-enable_peer_stats] [-enable_process_stats] "
                   "[-help]\n");
 #else
            printf("Usage: volserver [-log] [-p <number of processes>] "
-                  "[-auditlog <log path>] "
-                  "[-nojumbo] [-rxmaxmtu <bytes>] [-rxbind] "
+                  "[-auditlog <log path>] [-d <debug level>] "
+                  "[-nojumbo] [-jumbo] [-rxmaxmtu <bytes>] [-rxbind] [-allow-dotted-principals] "
                   "[-udpsize <size of socket buffer in bytes>] "
                   "[-enable_peer_stats] [-enable_process_stats] "
                   "[-help]\n");
@@ -402,6 +407,11 @@ main(int argc, char **argv)
            VS_EXIT(1);
        }
     }
+
+    if (auditFileName) {
+       osi_audit_file(auditFileName);
+       osi_audit(VS_StartEvent, 0, AUD_END);
+    }
 #ifdef AFS_SGI_VNODE_GLUE
     if (afs_init_kernel_config(-1) < 0) {
        printf
@@ -422,7 +432,15 @@ main(int argc, char **argv)
        exit(1);
     }
 #endif
-    VInitVolumePackage(volumeUtility, 0, 0, CONNECT_FS, 0);
+    /* Open VolserLog and map stdout, stderr into it; VInitVolumePackage2 can
+       log, so we need to do this here */
+    OpenLog(AFSDIR_SERVER_VOLSERLOG_FILEPATH);
+
+    VOptDefaults(volumeServer, &opts);
+    if (VInitVolumePackage2(volumeServer, &opts)) {
+       Log("Shutting down: errors encountered initializing volume package\n");
+       exit(1);
+    }
     /* For nuke() */
     Lock_Init(&localLock);
     DInit(40);
@@ -437,20 +455,18 @@ main(int argc, char **argv)
        rx_SetUdpBufSize(udpBufSize);   /* set the UDP buffer size for receive */
     if (rxBind) {
        afs_int32 ccode;
-#ifndef AFS_NT40_ENV
-        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 
-#endif 
+        } else
        {
             ccode = rx_getAllAddr(SHostAddrs, ADDRSPERSITE);
         }
-        if (ccode == 1) 
+        if (ccode == 1)
             host = SHostAddrs[0];
     }
 
@@ -471,18 +487,16 @@ main(int argc, char **argv)
     rx_SetRxDeadTime(420);
     memset(busyFlags, 0, sizeof(busyFlags));
 
-    /* Open FileLog and map stdout, stderr into it */
-    OpenLog(AFSDIR_SERVER_VOLSERLOG_FILEPATH);
     SetupLogSignals();
 
     {
 #ifdef AFS_PTHREAD_ENV
        pthread_t tid;
        pthread_attr_t tattr;
-       assert(pthread_attr_init(&tattr) == 0);
-       assert(pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_DETACHED) == 0);
+       osi_Assert(pthread_attr_init(&tattr) == 0);
+       osi_Assert(pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_DETACHED) == 0);
 
-       assert(pthread_create(&tid, &tattr, (void *)BKGLoop, NULL) == 0);
+       osi_Assert(pthread_create(&tid, &tattr, BKGLoop, NULL) == 0);
 #else
        PROCESS pid;
        LWP_CreateProcess(BKGLoop, 16*1024, 3, 0, "vol bkg daemon", &pid);
@@ -499,15 +513,12 @@ main(int argc, char **argv)
        VS_EXIT(1);
     }
     afsconf_GetKey(tdir, 999, &tkey);
-    securityObjects[0] = rxnull_NewServerSecurityObject();
-    securityObjects[1] = (struct rx_securityClass *)0; /* don't bother with rxvab */
-    securityObjects[2] =
-       rxkad_NewServerSecurityObject(0, tdir, afsconf_GetKey, NULL);
-    if (securityObjects[0] == (struct rx_securityClass *)0)
+    afsconf_BuildServerSecurityObjects(tdir, &securityClasses, &numClasses);
+    if (securityClasses[0] == NULL)
        Abort("rxnull_NewServerSecurityObject");
     service =
-       rx_NewServiceHost(host, 0, VOLSERVICE_ID, "VOLSER", securityObjects, 3,
-                     AFSVolExecuteRequest);
+       rx_NewServiceHost(host, 0, VOLSERVICE_ID, "VOLSER", securityClasses,
+                         numClasses, AFSVolExecuteRequest);
     if (service == (struct rx_service *)0)
        Abort("rx_NewService");
     rx_SetBeforeProc(service, MyBeforeProc);
@@ -516,21 +527,29 @@ main(int argc, char **argv)
     if (lwps < 4)
        lwps = 4;
     rx_SetMaxProcs(service, lwps);
-#ifdef AFS_SGI_ENV
-    rx_SetStackSize(service, 49152);
+#if defined(AFS_XBSD_ENV)
+    rx_SetStackSize(service, (128 * 1024));
+#elif defined(AFS_SGI_ENV)
+    rx_SetStackSize(service, (48 * 1024));
 #else
-    rx_SetStackSize(service, 32768);
+    rx_SetStackSize(service, (32 * 1024));
 #endif
 
+    if (rxkadDisableDotCheck) {
+        rx_SetSecurityConfiguration(service, RXS_CONFIG_FLAGS,
+                                    (void *)RXS_CONFIG_FLAGS_DISABLE_DOTCHECK);
+    }
+
     service =
-       rx_NewService(0, RX_STATS_SERVICE_ID, "rpcstats", securityObjects, 3,
-                     RXSTATS_ExecuteRequest);
+       rx_NewService(0, RX_STATS_SERVICE_ID, "rpcstats", securityClasses,
+                     numClasses, RXSTATS_ExecuteRequest);
     if (service == (struct rx_service *)0)
        Abort("rx_NewService");
     rx_SetMinProcs(service, 2);
     rx_SetMaxProcs(service, 4);
 
-    Log("Starting AFS Volserver %s (%s)\n", VolserVersion, commandLine);
+    LogCommandLine(argc, argv, "Volserver", VolserVersion, "Starting AFS",
+                  Log);
     if (TTsleep) {
        Log("Will sleep %d second%s every %d second%s\n", TTsleep,
            (TTsleep > 1) ? "s" : "", TTrun + TTsleep,
@@ -544,4 +563,5 @@ main(int argc, char **argv)
 
     osi_audit(VS_FinishEvent, (-1), AUD_END);
     Abort("StartServer returned?");
+    return 0; /* not reached */
 }