OPENAFS-SA-2018-001 backup: use authenticated connection to butc
[openafs.git] / src / bucoord / dump.c
index 2e45b7f..e0b3ced 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 <afs/cmd.h>
-#ifdef AFS_NT40_ENV
-#include <winsock2.h>
-#else
-#include <sys/param.h>
-#include <sys/socket.h>
-#include <netinet/in.h>
-#include <netdb.h>
-#include <sys/time.h>
-#endif
+#include <afs/cellconfig.h>
 #include <lwp.h>
 #include <rx/rx.h>
 #include <afs/bubasics.h>
@@ -39,15 +29,12 @@ RCSID
 
 #include "bc.h"
 #include "error_macros.h"
+#include "bucoord_internal.h"
+#include "bucoord_prototypes.h"
 
 struct bc_dumpTask bc_dumpTasks[BC_MAXSIMDUMPS];
 
-extern char *bc_CopyString();
-extern void bc_HandleMisc();
 extern char *whoami;
-extern char *tailCompPtr();
-extern statusP createStatusNode();
-extern afs_int32 bc_jobNumber();
 
 extern afs_int32 lastTaskCode;
 
@@ -56,24 +43,25 @@ extern afs_int32 lastTaskCode;
 /* bc_Dumper
  *     called (indirectly) to make a dump
  * entry:
- *     aindex - index into dumpTask array, contains all the information 
+ *     aindex - index into dumpTask array, contains all the information
  *              relevant to the dump
  */
-bc_Dumper(aindex)
+int
+bc_Dumper(int aindex)
 {
     struct rx_connection *tconn;
-    register struct bc_volumeDump *tde;
+    struct bc_volumeDump *tde;
     afs_int32 count, port;
-    struct tc_dumpDesc *volDesc = 0;
+    struct tc_dumpDesc *volDesc = NULL;
     struct tc_dumpArray volArray;
     char *baseNamePtr;
     statusP statusPtr;
 
     struct tc_dumpInterface dumpInterface;
     struct tc_dumpInterface *tcdiPtr = &dumpInterface;
-    register struct bc_dumpTask *dumpTaskPtr;
+    struct bc_dumpTask *dumpTaskPtr;
 
-    register afs_int32 code = 0;
+    afs_int32 code = 0;
 
     dumpTaskPtr = &bc_dumpTasks[aindex];
 
@@ -87,14 +75,18 @@ bc_Dumper(aindex)
        return (code);
 
     /* count number of volumes to be dumped and
-     * build array of volumes to be sent to backup system 
+     * build array of volumes to be sent to backup system
      */
     for (count = 0, tde = dumpTaskPtr->volumes; tde;
         tde = tde->next, count++);
-    volDesc =
-       (struct tc_dumpDesc *)malloc(count * sizeof(struct tc_dumpDesc));
+
+    /* Nothing to dump, so just return success */
+    if (count == 0)
+       goto error_exit;
+
+    volDesc = malloc(count * sizeof(struct tc_dumpDesc));
     if (!volDesc) {
-       afs_com_err(whoami, BC_NOMEM, "");
+       afs_com_err(whoami, BC_NOMEM, NULL);
        ERROR(BC_NOMEM);
     }
 
@@ -178,8 +170,8 @@ bc_Dumper(aindex)
  *     free the list of volumes used for dumps
  */
 
-freeDumpTaskVolumeList(vdptr)
-     struct bc_volumeDump *vdptr;
+void
+freeDumpTaskVolumeList(struct bc_volumeDump *vdptr)
 {
     struct bc_volumeDump *nextVdPtr;
 
@@ -195,14 +187,15 @@ freeDumpTaskVolumeList(vdptr)
 }
 
 /* bc_DmpRstStart
- *     The other half of the dump/restore create process call. In bc_StartDmpRst, 
+ *     The other half of the dump/restore create process call. In bc_StartDmpRst,
  *     we allocated a dumpTask entry. Here we do the task and then free the entry.
  */
-bc_DmpRstStart(aindex)
-     afs_int32 aindex;
+void *
+bc_DmpRstStart(void *param)
 {
-    register struct bc_dumpTask *tdump;
-    register afs_int32 code;
+    afs_int32 aindex = (intptr_t)param;
+    struct bc_dumpTask *tdump;
+    afs_int32 code;
 
     tdump = &bc_dumpTasks[aindex];
 
@@ -223,13 +216,13 @@ bc_DmpRstStart(aindex)
        free(tdump->portOffset);
     tdump->flags &= ~BC_DI_INUSE;
 
-    return code;
+    return (void *)(intptr_t)code;
 }
 
 /* bc_StartDmpRst
  *     function to start dump running. Packages the relevant information
  *     (from params) into any free dumpTask structure (globally allocated),
- *     and then invokes bc_DmpRstStart to do the work, passing it a single 
+ *     and then invokes bc_DmpRstStart to do the work, passing it a single
  *      parameter, the index into the dumpTask array.
  *
  * entry:
@@ -238,28 +231,18 @@ bc_DmpRstStart(aindex)
  *             bc_Restorer for doing restores
  */
 
-bc_StartDmpRst(aconfig, adname, avname, avolsToDump, adestServer,
-              adestPartition, afromDate, anewExt, aoldFlag, aparent, alevel,
-              aproc, ports, portCount, dsptr, append, dontExecute)
-     struct bc_config *aconfig;
-     char *adname;
-     char *avname;
-     struct bc_volumeDump *avolsToDump;
-     struct sockaddr_in *adestServer;
-     afs_int32 adestPartition;
-     afs_int32 afromDate;
-     char *anewExt;
-     int aoldFlag;
-     afs_int32 aparent, alevel;
-     int (*aproc) ();
-     afs_int32 *ports;
-     afs_int32 portCount;
-     struct bc_dumpSchedule *dsptr;
-     int append, dontExecute;
+int
+bc_StartDmpRst(struct bc_config *aconfig, char *adname, char *avname,
+              struct bc_volumeDump *avolsToDump,
+              struct sockaddr_in *adestServer,
+              afs_int32 adestPartition, afs_int32 afromDate, char *anewExt,
+              int aoldFlag, afs_int32 aparent, afs_int32 alevel,
+              int (*aproc) (int), afs_int32 *ports, afs_int32 portCount,
+              struct bc_dumpSchedule *dsptr, int append, int dontExecute)
 {
-    register int i;
-    register afs_int32 code;
-    char *junk;
+    int i;
+    afs_int32 code;
+    PROCESS junk;
 
     for (i = 0; i < BC_MAXSIMDUMPS; i++)
        if (!(bc_dumpTasks[i].flags & BC_DI_INUSE))
@@ -302,7 +285,7 @@ bc_StartDmpRst(aconfig, adname, avname, avolsToDump, adestServer,
 
     code =
        LWP_CreateProcess(bc_DmpRstStart, 20480, LWP_NORMAL_PRIORITY,
-                         (void *)i, "helper", &junk);
+                         (void *)(intptr_t)i, "helper", &junk);
     if (code) {
        bc_HandleMisc(code);
        afs_com_err(whoami, code, "; Can't start thread");
@@ -337,9 +320,7 @@ bc_StartDmpRst(aconfig, adname, avname, avolsToDump, adestServer,
  */
 
 afs_int32
-bc_FindDumpSlot(dumpID, port)
-     afs_int32 dumpID;
-     afs_int32 port;
+bc_FindDumpSlot(afs_int32 dumpID, afs_int32 port)
 {
     int i;
 
@@ -359,11 +340,9 @@ bc_FindDumpSlot(dumpID, port)
  *     label a tape
  */
 
-bc_LabelTape(afsname, pname, size, config, port)
-     char *afsname, *pname;
-     struct bc_config *config;
-     afs_int32 port;
-     afs_int32 size;
+int
+bc_LabelTape(char *afsname, char *pname, afs_int32 size,
+            struct bc_config *config, afs_int32 port)
 {
     struct rx_connection *tconn;
     afs_int32 code = 0;
@@ -409,9 +388,8 @@ bc_LabelTape(afsname, pname, size, config, port)
  *     a tape
  */
 
-bc_ReadLabel(config, port)
-     struct bc_config *config;
-     afs_int32 port;
+int
+bc_ReadLabel(struct bc_config *config, afs_int32 port)
 {
     struct rx_connection *tconn;
     struct tc_tapeLabel label;
@@ -443,19 +421,17 @@ bc_ReadLabel(config, port)
        printf("Tape read was labelled : <NULL>  size : %u\n", label.size);
     } else if (!label.tapeId) {
        printf("Tape read was labelled : %s size : %lu Kbytes\n", tname,
-              label.size);
+              (long unsigned int) label.size);
     } else {
        printf("Tape read was labelled : %s (%lu) size : %lu Kbytes\n", tname,
-              label.tapeId, label.size);
+              (long unsigned int) label.tapeId, (long unsigned int) label.size);
     }
 
     return 0;
 }
 
-bc_ScanDumps(config, dbAddFlag, port)
-     struct bc_config *config;
-     afs_int32 dbAddFlag;
-     afs_int32 port;
+int
+bc_ScanDumps(struct bc_config *config, afs_int32 dbAddFlag, afs_int32 port)
 {
     struct rx_connection *tconn;
     statusP statusPtr;
@@ -491,22 +467,44 @@ bc_ScanDumps(config, dbAddFlag, port)
 
 /* get a connection to the tape controller */
 afs_int32
-bc_GetConn(aconfig, aport, tconn)
-     struct bc_config *aconfig;
-     afs_int32 aport;
-     struct rx_connection **tconn;
+bc_GetConn(struct bc_config *aconfig, afs_int32 aport,
+          struct rx_connection **tconn)
 {
-    afs_int32 code = 0;
     afs_uint32 host;
+    afs_int32 code;
     unsigned short port;
     static struct rx_securityClass *rxsc;
+    static afs_int32 scIndex;
     struct bc_hostEntry *te;
 
     *tconn = (struct rx_connection *)0;
 
     /* use non-secure connections to butc */
-    if (!rxsc)
-       rxsc = rxnull_NewClientSecurityObject();
+    if (!rxsc) {
+       struct afsconf_dir *dir;
+       afsconf_secflags flags = AFSCONF_SECOPTS_FALLBACK_NULL;
+       char *cname;
+
+       if (nobutcauth)
+           flags |= AFSCONF_SECOPTS_NOAUTH;
+       if (localauth) {
+           flags |= AFSCONF_SECOPTS_LOCALAUTH;
+           dir = afsconf_Open(AFSDIR_SERVER_ETC_DIRPATH);
+       } else {
+           dir = afsconf_Open(AFSDIR_CLIENT_ETC_DIRPATH);
+       }
+       if (tcell[0] == '\0')
+           cname = NULL;
+       else
+           cname = tcell;
+       /* No need for cell info since butc is not a registered service */
+       code = afsconf_PickClientSecObj(dir, flags, NULL, cname, &rxsc, &scIndex,
+                                       NULL);
+       if (dir)
+           afsconf_Close(dir);
+       if (code)
+           return -1;
+    }
     if (!rxsc || !aconfig)
        return (-1);
 
@@ -519,8 +517,8 @@ bc_GetConn(aconfig, aport, tconn)
 
            port = htons(BC_TAPEPORT + aport);
 
-           /* servers is 1; sec index is 0 */
-           *tconn = rx_NewConnection(host, port, 1, rxsc, 0);
+           /* servers is 1 */
+           *tconn = rx_NewConnection(host, port, 1, rxsc, scIndex);
            return ((*tconn ? 0 : -1));
        }
     }
@@ -529,13 +527,13 @@ bc_GetConn(aconfig, aport, tconn)
 
 /* CheckTCVersion
  *     make sure we are talking to a compatible butc process.
- * exit: 
+ * exit:
  *     0 - ok
  *     -1 - not compatible
  */
 
-CheckTCVersion(tconn)
-     struct rx_connection *tconn;
+int
+CheckTCVersion(struct rx_connection *tconn)
 {
     struct tc_tcInfo tci;
     afs_int32 code;
@@ -550,10 +548,9 @@ CheckTCVersion(tconn)
     return 0;
 }
 
-ConnectButc(config, port, tconn)
-     struct bc_config *config;
-     afs_int32 port;
-     struct rx_connection **tconn;
+int
+ConnectButc(struct bc_config *config, afs_int32 port,
+           struct rx_connection **tconn)
 {
     afs_int32 code;