Remove unused variable in vos.c
[openafs.git] / src / volser / vos.c
index 0428be0..648606e 100644 (file)
 
 #include <sys/types.h>
 #include <string.h>
+#ifdef HAVE_STDINT_H
+# include <stdint.h>
+#endif
 #ifdef AFS_NT40_ENV
 #include <fcntl.h>
 #include <io.h>
 #include <winsock2.h>
+#include <WINNT/afsreg.h>
 #else
 #include <sys/time.h>
 #include <sys/file.h>
@@ -52,6 +56,7 @@
 #include <afs/ihandle.h>
 #include <afs/vnode.h>
 #include <afs/volume.h>
+#include <afs/com_err.h>
 #include "dump.h"
 #include "lockdata.h"
 
@@ -103,7 +108,7 @@ static struct tqHead busyHead, notokHead;
 static void
 qInit(struct tqHead *ahead)
 {
-    memset((char *)ahead, 0, sizeof(struct tqHead));
+    memset(ahead, 0, sizeof(struct tqHead));
     return;
 }
 
@@ -196,22 +201,35 @@ IsNumeric(char *name)
 
 
 /*
- * Parse a server name/address and return the address in HOST BYTE order
+ * Parse a server dotted address and return the address in network byte order
  */
 afs_int32
-GetServer(char *aname)
+GetServerNoresolve(char *aname)
 {
-    register struct hostent *th;
-    afs_int32 addr;
     int b1, b2, b3, b4;
-    register afs_int32 code;
-    char hostname[MAXHOSTCHARS];
+    afs_int32 addr;
+    afs_int32 code;
 
     code = sscanf(aname, "%d.%d.%d.%d", &b1, &b2, &b3, &b4);
     if (code == 4) {
        addr = (b1 << 24) | (b2 << 16) | (b3 << 8) | b4;
-       addr = ntohl(addr);     /* convert to host order */
-    } else {
+       addr = htonl(addr);     /* convert to network byte order */
+       return addr;
+    } else
+       return 0;
+}
+/*
+ * Parse a server name/address and return the address in network byte order
+ */
+afs_int32
+GetServer(char *aname)
+{
+    register struct hostent *th;
+    afs_int32 addr; /* in network byte order */
+    register afs_int32 code;
+    char hostname[MAXHOSTCHARS];
+
+    if ((addr = GetServerNoresolve(aname)) == 0) {
        th = gethostbyname(aname);
        if (!th)
            return 0;
@@ -222,7 +240,7 @@ GetServer(char *aname)
        code = gethostname(hostname, MAXHOSTCHARS);
        if (code)
            return 0;
-       th = gethostbyname(hostname);   /* returns host byte order */
+       th = gethostbyname(hostname);
        if (!th)
            return 0;
        memcpy(&addr, th->h_addr, sizeof(addr));
@@ -290,14 +308,15 @@ SendFile(usd_handle_t ufd, register struct rx_call *call, long blksize)
        FD_SET((intptr_t)(ufd->handle), &in);
        /* don't timeout if read blocks */
 #if defined(AFS_PTHREAD_ENV)
-       select(((int)(ufd->handle)) + 1, &in, 0, 0, 0);
+       select(((intptr_t)(ufd->handle)) + 1, &in, 0, 0, 0);
 #else
        IOMGR_Select(((intptr_t)(ufd->handle)) + 1, &in, 0, 0, 0);
 #endif
 #endif
        error = USD_READ(ufd, buffer, blksize, &nbytes);
        if (error) {
-           fprintf(STDERR, "File system read failed\n");
+           fprintf(STDERR, "File system read failed: %s\n",
+                   afs_error_message(error));
            break;
        }
        if (nbytes == 0) {
@@ -341,7 +360,8 @@ WriteData(struct rx_call *call, void *rock)
            code = USD_IOCTL(ufd, USD_IOCTL_GETBLKSIZE, &blksize);
        }
        if (code) {
-           fprintf(STDERR, "Could not access file '%s'\n", filename);
+           fprintf(STDERR, "Could not access file '%s': %s\n", filename,
+                   afs_error_message(code));
            error = VOLSERBADOP;
            goto wfail;
        }
@@ -403,7 +423,7 @@ ReceiveFile(usd_handle_t ufd, struct rx_call *call, long blksize)
            FD_SET((intptr_t)(ufd->handle), &out);
            /* don't timeout if write blocks */
 #if defined(AFS_PTHREAD_ENV)
-           select(((int)(ufd->handle)) + 1, &out, 0, 0, 0);
+           select(((intptr_t)(ufd->handle)) + 1, &out, 0, 0, 0);
 #else
            IOMGR_Select(((intptr_t)(ufd->handle)) + 1, 0, &out, 0, 0);
 #endif
@@ -411,7 +431,8 @@ ReceiveFile(usd_handle_t ufd, struct rx_call *call, long blksize)
            error =
                USD_WRITE(ufd, &buffer[bytesread - bytesleft], bytesleft, &w);
            if (error) {
-               fprintf(STDERR, "File system write failed\n");
+               fprintf(STDERR, "File system write failed: %s\n",
+                       afs_error_message(error));
                ERROR_EXIT(-1);
            }
        }
@@ -450,7 +471,8 @@ DumpFunction(struct rx_call *call, void *rock)
            code = USD_IOCTL(ufd, USD_IOCTL_GETBLKSIZE, &blksize);
        }
        if (code) {
-           fprintf(STDERR, "Could not create file '%s'\n", filename);
+           fprintf(STDERR, "Could not create file '%s': %s\n", filename,
+                   afs_error_message(code));
            ERROR_EXIT(VOLSERBADOP);
        }
     }
@@ -516,7 +538,6 @@ DisplayFormat(volintInfo *pntr, afs_int32 server, afs_int32 part,
            t = pntr->creationDate;
            fprintf(STDOUT, "    Creation    %s",
                    ctime(&t));
-#ifdef FULL_LISTVOL_SWITCH
            t = pntr->copyDate;
            fprintf(STDOUT, "    Copy        %s",
                    ctime(&t));
@@ -532,7 +553,7 @@ DisplayFormat(volintInfo *pntr, afs_int32 server, afs_int32 part,
            if (t)
                fprintf(STDOUT, "    Last Access %s",
                        ctime(&t));
-#endif
+
            t = pntr->updateDate;
            if (!t)
                fprintf(STDOUT, "    Last Update Never\n");
@@ -671,7 +692,7 @@ XDisplayFormat(volintXInfo *a_xInfoP, afs_int32 a_servID, afs_int32 a_partID,
            t = a_xInfoP->creationDate;
            fprintf(STDOUT, "    Creation    %s",
                    ctime(&t));
-#ifdef FULL_LISTVOL_SWITCH
+
            t = a_xInfoP->copyDate;
            fprintf(STDOUT, "    Copy        %s",
                    ctime(&t));
@@ -687,7 +708,7 @@ XDisplayFormat(volintXInfo *a_xInfoP, afs_int32 a_servID, afs_int32 a_partID,
            if (t)
                fprintf(STDOUT, "    Last Access %s",
                        ctime(&t));
-#endif
+
            t = a_xInfoP->updateDate;
            if (!t)
                fprintf(STDOUT, "    Last Update Never\n");
@@ -826,7 +847,6 @@ XDisplayFormat(volintXInfo *a_xInfoP, afs_int32 a_servID, afs_int32 a_partID,
     }                          /*Default listing */
 }                              /*XDisplayFormat */
 
-#ifdef FULL_LISTVOL_SWITCH
 /*------------------------------------------------------------------------
  * PRIVATE XDisplayFormat2
  *
@@ -1023,9 +1043,7 @@ XDisplayFormat2(volintXInfo *a_xInfoP, afs_int32 a_servID, afs_int32 a_partID,
        }                       /*Screwed volume */
     }                          /*Default listing */
 }                              /*XDisplayFormat */
-#endif /*FULL_LISTVOL_SWITCH*/
 
-#ifdef FULL_LISTVOL_SWITCH
 static void
 DisplayFormat2(long server, long partition, volintInfo *pntr)
 {
@@ -1143,7 +1161,6 @@ DisplayVolumes2(long server, long partition, volintInfo *pntr, long count)
     }
     return;
 }
-#endif /* FULL_LISTVOL_SWITCH */
 
 static void
 DisplayVolumes(afs_int32 server, afs_int32 part, volintInfo *pntr,
@@ -1269,7 +1286,7 @@ XDisplayVolumes(afs_int32 a_servID, afs_int32 a_partID, volintXInfo *a_xInfoP,
     }
 
 }                              /*XDisplayVolumes */
-#ifdef FULL_LISTVOL_SWITCH
+
 /*------------------------------------------------------------------------
  * PRIVATE XDisplayVolumes2
  *
@@ -1355,7 +1372,6 @@ XDisplayVolumes2(afs_int32 a_servID, afs_int32 a_partID, volintXInfo *a_xInfoP,
     }
 
 }                              /*XDisplayVolumes2 */
-#endif /* FULL_LISTVOL_SWITCH */
 
 
 /* set <server> and <part> to the correct values depending on 
@@ -1624,13 +1640,10 @@ ExamineVolume(register struct cmd_syndesc *as, void *arock)
            foundserv = 1;
            if (wantExtendedInfo)
                XVolumeStats(xInfoP, &entry, aserver, apart, voltype);
-           else
-#ifdef FULL_LISTVOL_SWITCH
-           if (as->parms[2].items) {
+           else if (as->parms[2].items) {
                DisplayFormat2(aserver, apart, pntr);
                EnumerateEntry(&entry);
            } else
-#endif /* FULL_LISTVOL_SWITCH */
                VolumeStats_int(pntr, &entry, aserver, apart, voltype);
 
            if ((voltype == BACKVOL) && !(entry.flags & BACK_EXISTS)) {
@@ -3705,24 +3718,20 @@ ListVolumes(register struct cmd_syndesc *as, void *arock)
                        as->parms[0].items->data, pname,
                        (unsigned long)count);
            if (wantExtendedInfo) {
-#ifdef FULL_LISTVOL_SWITCH
                if (as->parms[6].items)
                    XDisplayVolumes2(aserver, dummyPartList.partId[i], origxInfoP,
                                count, int32list, fast, quiet);
                else
-#endif /* FULL_LISTVOL_SWITCH */
-               XDisplayVolumes(aserver, dummyPartList.partId[i], origxInfoP,
+                   XDisplayVolumes(aserver, dummyPartList.partId[i], origxInfoP,
                                count, int32list, fast, quiet);
                if (xInfoP)
                    free(xInfoP);
                xInfoP = (volintXInfo *) 0;
            } else {
-#ifdef FULL_LISTVOL_SWITCH
                if (as->parms[6].items)
                    DisplayVolumes2(aserver, dummyPartList.partId[i], oldpntr,
                                    count);
                else
-#endif /* FULL_LISTVOL_SWITCH */
                    DisplayVolumes(aserver, dummyPartList.partId[i], oldpntr,
                                   count, int32list, fast, quiet);
                if (pntr)
@@ -5140,7 +5149,10 @@ ChangeAddr(register struct cmd_syndesc *as, void *arock)
     afs_int32 ip1, ip2, vcode;
     int remove = 0;
 
-    ip1 = GetServer(as->parms[0].items->data);
+    if (noresolve)
+       ip1 = GetServerNoresolve(as->parms[0].items->data);
+    else
+       ip1 = GetServer(as->parms[0].items->data);
     if (!ip1) {
        fprintf(STDERR, "vos: invalid host address\n");
        return (EINVAL);
@@ -5154,7 +5166,10 @@ ChangeAddr(register struct cmd_syndesc *as, void *arock)
     }
 
     if (as->parms[1].items) {
-       ip2 = GetServer(as->parms[1].items->data);
+       if (noresolve)
+           ip2 = GetServerNoresolve(as->parms[1].items->data);
+       else
+           ip2 = GetServer(as->parms[1].items->data);
        if (!ip2) {
            fprintf(STDERR, "vos: invalid host address\n");
            return (EINVAL);
@@ -5169,18 +5184,22 @@ ChangeAddr(register struct cmd_syndesc *as, void *arock)
        ip1 = 0xffffffff;
     }
 
-    vcode = ubik_Call_New(VL_ChangeAddr, cstruct, 0, ntohl(ip1), ntohl(ip2));
+    vcode = ubik_VL_ChangeAddr(cstruct, UBIK_CALL_NEW, ntohl(ip1), ntohl(ip2));
     if (vcode) {
+       char hoststr1[16], hoststr2[16];
        if (remove) {
+           afs_inet_ntoa_r(ip2, hoststr2);
            fprintf(STDERR, "Could not remove server %s from the VLDB\n",
-                   as->parms[0].items->data);
+                   hoststr2);
            if (vcode == VL_NOENT) {
                fprintf(STDERR,
                        "vlserver does not support the remove flag or ");
            }
        } else {
+           afs_inet_ntoa_r(ip1, hoststr1);
+           afs_inet_ntoa_r(ip2, hoststr2);
            fprintf(STDERR, "Could not change server %s to server %s\n",
-                   as->parms[0].items->data, as->parms[1].items->data);
+                   hoststr1, hoststr2);
        }
        PrintError("", vcode);
        return (vcode);
@@ -5200,9 +5219,8 @@ static void
 print_addrs(const bulkaddrs * addrs, afsUUID * m_uuid, int nentries,
            int print)
 {
-    afs_int32 vcode;
+    afs_int32 vcode, m_uniq=0;
     afs_int32 i, j;
-    struct VLCallBack vlcb;
     afs_int32 *addrp;
     bulkaddrs m_addrs;
     ListAddrByAttributes m_attrs;
@@ -5236,7 +5254,8 @@ print_addrs(const bulkaddrs * addrs, afsUUID * m_uuid, int nentries,
                m_addrs.bulkaddrs_len = 0;
                vcode =
                    ubik_VL_GetAddrsU(cstruct, 0, &m_attrs, m_uuid,
-                                     (afs_int32 *)&vlcb, &m_nentries, &m_addrs);
+                                     &m_uniq, &m_nentries,
+                                     &m_addrs);
                if (vcode) {
                    fprintf(STDERR,
                            "vos: could not list the multi-homed server addresses\n");
@@ -5285,7 +5304,7 @@ print_addrs(const bulkaddrs * addrs, afsUUID * m_uuid, int nentries,
 static int
 ListAddrs(register struct cmd_syndesc *as, void *arock)
 {
-    afs_int32 vcode;
+    afs_int32 vcode, m_uniq=0;
     afs_int32 i, printuuid = 0;
     struct VLCallBack vlcb;
     afs_int32 nentries;
@@ -5331,8 +5350,8 @@ ListAddrs(register struct cmd_syndesc *as, void *arock)
     m_addrs.bulkaddrs_len = 0;
 
     vcode =
-       ubik_Call_New(VL_GetAddrs, cstruct, 0, 0, 0, &vlcb, &nentries,
-                     &m_addrs);
+       ubik_VL_GetAddrs(cstruct, UBIK_CALL_NEW, 0, 0, &vlcb, &nentries,
+                        &m_addrs);
     if (vcode) {
        fprintf(STDERR, "vos: could not list the server addresses\n");
        PrintError("", vcode);
@@ -5347,8 +5366,8 @@ ListAddrs(register struct cmd_syndesc *as, void *arock)
        m_attrs.index = i;
 
        vcode =
-           ubik_Call_New(VL_GetAddrsU, cstruct, 0, &m_attrs, &m_uuid,
-                         &vlcb, &m_nentries, &m_addrs);
+           ubik_VL_GetAddrsU(cstruct, UBIK_CALL_NEW, &m_attrs, &m_uuid,
+                             &m_uniq, &m_nentries, &m_addrs);
 
        if (vcode == VL_NOENT) {
            if (m_attrs.Mask == VLADDR_UUID) {
@@ -5386,6 +5405,71 @@ ListAddrs(register struct cmd_syndesc *as, void *arock)
     return 0;
 }
 
+
+static int
+SetAddrs(register struct cmd_syndesc *as, void *arock)
+{
+    afs_int32 vcode;
+    bulkaddrs m_addrs;
+    afsUUID askuuid;
+    afs_uint32 FS_HostAddrs_HBO[ADDRSPERSITE];
+
+    memset(&m_addrs, 0, sizeof(bulkaddrs));
+    memset(&askuuid, 0, sizeof(afsUUID));
+    if (as->parms[0].items) {
+       /* -uuid */
+        if (afsUUID_from_string(as->parms[0].items->data, &askuuid) < 0) {
+           fprintf(STDERR, "vos: invalid UUID '%s'\n",
+                   as->parms[0].items->data);
+           exit(-1);
+       }
+    }
+    if (as->parms[1].items) {
+       /* -host */
+       struct cmd_item *ti;
+       afs_int32 saddr;
+       int i = 0;
+
+       for (ti = as->parms[1].items; ti && i < ADDRSPERSITE; ti = ti->next) {
+
+           if (noresolve)
+               saddr = GetServerNoresolve(ti->data);
+           else
+               saddr = GetServer(ti->data);
+
+           if (!saddr) {
+               fprintf(STDERR, "vos: Can't get host info for '%s'\n",
+                       ti->data);
+               exit(-1);
+           }
+           /* Convert it to host byte order */
+           FS_HostAddrs_HBO[i] = ntohl(saddr);
+           i++;
+       }
+       m_addrs.bulkaddrs_len = i;
+       m_addrs.bulkaddrs_val = FS_HostAddrs_HBO;
+    }
+
+    vcode = ubik_VL_RegisterAddrs(cstruct, 0, &askuuid, 0, &m_addrs);
+
+    if (vcode) {
+       if (vcode == VL_MULTIPADDR) {
+           fprintf(STDERR, "vos: VL_RegisterAddrs rpc failed; The IP address exists on a different server; repair it\n");
+           PrintError("", vcode);
+           return vcode;
+       } else if (vcode == RXGEN_OPCODE) {
+           fprintf(STDERR, "vlserver doesn't support VL_RegisterAddrs rpc; ignored\n");
+           PrintError("", vcode);
+           return vcode;
+       }
+    }
+    if (verbose) {
+       fprintf(STDOUT, "vos: Changed UUID with addresses:\n");
+       print_addrs(&m_addrs, &askuuid, m_addrs.bulkaddrs_len, 1);
+    }
+    return 0;
+}
+
 static int
 LockEntry(register struct cmd_syndesc *as, void *arock)
 {
@@ -5663,6 +5747,39 @@ Sizes(register struct cmd_syndesc *as, void *arock)
     return 0;
 }
 
+static int
+EndTrans(register struct cmd_syndesc *as, void *arock)
+{
+    afs_int32 server, code, tid, rcode;
+    struct rx_connection *aconn;
+
+    server = GetServer(as->parms[0].items->data);
+    if (!server) {
+       fprintf(STDERR, "vos: host '%s' not found in host table\n",
+               as->parms[0].items->data);
+       return EINVAL;
+    }
+
+    code = util_GetInt32(as->parms[1].items->data, &tid);
+    if (code) {
+       fprintf(STDERR, "vos: bad integer specified for transaction ID.\n");
+       return code;
+    }
+
+    aconn = UV_Bind(server, AFSCONF_VOLUMEPORT);
+    code = AFSVolEndTrans(aconn, tid, &rcode);
+    if (!code) {
+       code = rcode;
+    }
+
+    if (code) {
+       PrintDiagnostics("endtrans", code);
+       return 1;
+    }
+
+    return 0;
+}
+
 int
 PrintDiagnostics(char *astring, afs_int32 acode)
 {
@@ -5678,6 +5795,29 @@ PrintDiagnostics(char *astring, afs_int32 acode)
 }
 
 
+#ifdef AFS_NT40_ENV
+static DWORD
+win32_enableCrypt(void)
+{
+    HKEY parmKey;
+    DWORD dummyLen;
+    DWORD cryptall = 0;
+    DWORD code;
+
+    /* Look up configuration parameters in Registry */
+    code = RegOpenKeyEx(HKEY_LOCAL_MACHINE, AFSREG_CLT_SVC_PARAM_SUBKEY,
+                        0, (IsWow64()?KEY_WOW64_64KEY:0)|KEY_QUERY_VALUE, &parmKey);
+    if (code != ERROR_SUCCESS) {
+        dummyLen = sizeof(cryptall);
+        RegQueryValueEx(parmKey, "SecurityLevel", NULL, NULL,
+                        (BYTE *) &cryptall, &dummyLen);
+    }
+    RegCloseKey (parmKey);
+
+    return cryptall;
+}
+#endif /* AFS_NT40_ENV */
+
 static int
 MyBeforeProc(struct cmd_syndesc *as, void *arock)
 {
@@ -5695,7 +5835,11 @@ MyBeforeProc(struct cmd_syndesc *as, void *arock)
        tcell = as->parms[12].items->data;
     if (as->parms[14].items)   /* -serverauth specified */
        sauth = 1;
-    if (as->parms[16].items)   /* -crypt specified */
+    if (as->parms[16].items     /* -encrypt specified */
+#ifdef AFS_NT40_ENV
+        || win32_enableCrypt()
+#endif /* AFS_NT40_ENV */
+         )
        vsu_SetCrypt(1);
     if ((code =
         vsu_ClientInit((as->parms[13].items != 0), confdir, tcell, sauth,
@@ -5931,10 +6075,8 @@ main(int argc, char **argv)
                "generate minimal information");
     cmd_AddParm(ts, "-extended", CMD_FLAG, CMD_OPTIONAL,
                "list extended volume fields");
-#ifdef FULL_LISTVOL_SWITCH
     cmd_AddParm(ts, "-format", CMD_FLAG, CMD_OPTIONAL,
                "machine readable format");
-#endif /* FULL_LISTVOL_SWITCH */
     COMMONPARMS;
 
     ts = cmd_CreateSyntax("syncvldb", SyncVldb, NULL,
@@ -5957,10 +6099,8 @@ main(int argc, char **argv)
     cmd_AddParm(ts, "-id", CMD_SINGLE, 0, "volume name or ID");
     cmd_AddParm(ts, "-extended", CMD_FLAG, CMD_OPTIONAL,
                "list extended volume fields");
-#ifdef FULL_LISTVOL_SWITCH
     cmd_AddParm(ts, "-format", CMD_FLAG, CMD_OPTIONAL,
                "machine readable format");
-#endif /* FULL_LISTVOL_SWITCH */
     COMMONPARMS;
     cmd_CreateAlias(ts, "volinfo");
 
@@ -6095,6 +6235,19 @@ main(int argc, char **argv)
     cmd_AddParm(ts, "-time", CMD_SINGLE, CMD_OPTIONAL, "dump from time");
     COMMONPARMS;
 
+    ts = cmd_CreateSyntax("endtrans", EndTrans, NULL,
+                         "end a volserver transaction");
+    cmd_AddParm(ts, "-server", CMD_SINGLE, 0, "machine name");
+    cmd_AddParm(ts, "-transaction", CMD_SINGLE, 0,
+               "transaction ID");
+    COMMONPARMS;
+
+    ts = cmd_CreateSyntax("setaddrs", SetAddrs, NULL,
+                         "set the list of IP address for a given UUID in the VLDB");
+    cmd_AddParm(ts, "-uuid", CMD_SINGLE, 0, "uuid of server");
+    cmd_AddParm(ts, "-host", CMD_LIST, 0, "address of host");
+
+    COMMONPARMS;
     code = cmd_Dispatch(argc, argv);
     if (rxInitDone) {
        /* Shut down the ubik_client and rx connections */