vos: reinstate the -localauth option for vos setaddrs
[openafs.git] / src / volser / vos.c
index 8b08d37..f12312e 100644 (file)
@@ -234,6 +234,7 @@ GetServer(char *aname)
     afs_uint32 addr; /* in network byte order */
     afs_int32 code;
     char hostname[MAXHOSTCHARS];
+    afs_uint32 **addr_list;
     int i;
 
     addr = GetServerNoresolve(aname);
@@ -245,10 +246,11 @@ GetServer(char *aname)
     }
 
     th = gethostbyname(aname);
-    if (th != NULL) {
-       for (i=0; i < th->h_length; i++) {
-           if (!rx_IsLoopbackAddr(ntohl(*(afs_uint32 *)th->h_addr_list[i]))) {
-               memcpy(&addr, th->h_addr_list[i], sizeof(addr));
+    if (th != NULL && th->h_addrtype == AF_INET) {
+       addr_list = (afs_uint32 **)th->h_addr_list;
+       for(i = 0; addr_list[i] != NULL; i++) {
+           if (!rx_IsLoopbackAddr(ntohl(*addr_list[i]))) {
+               memcpy(&addr, addr_list[i], sizeof(addr));
                return addr;
            }
        }
@@ -263,10 +265,11 @@ GetServer(char *aname)
        code = gethostname(hostname, MAXHOSTCHARS);
        if (code == 0) {
            th = gethostbyname(hostname);
-           if (th != NULL) {
-               for (i=0; i < th->h_length; i++) {
-                   if (!rx_IsLoopbackAddr(ntohl(*(afs_uint32 *)th->h_addr_list[i]))) {
-                       memcpy(&addr, th->h_addr_list[i], sizeof(addr));
+           if (th != NULL && th->h_addrtype == AF_INET) {
+               addr_list = (afs_uint32 **)th->h_addr_list;
+               for (i=0; addr_list[i] != NULL; i++) {
+                   if (!rx_IsLoopbackAddr(ntohl(*addr_list[i]))) {
+                       memcpy(&addr, addr_list[i], sizeof(addr));
                        return addr;
                    }
                }
@@ -1411,10 +1414,10 @@ GetServerAndPart(struct nvldbentry *entry, int voltype, afs_uint32 *server,
 
     /* Doesn't check for non-existance of backup volume */
     if ((voltype == RWVOL) || (voltype == BACKVOL)) {
-       vtype = ITSRWVOL;
+       vtype = VLSF_RWVOL;
        istart = 0;             /* seach the entire entry */
     } else {
-       vtype = ITSROVOL;
+       vtype = VLSF_ROVOL;
        /* Seach from beginning of entry or pick up where we left off */
        istart = ((*previdx < 0) ? 0 : *previdx + 1);
     }
@@ -1674,7 +1677,7 @@ ExamineVolume(struct cmd_syndesc *as, void *arock)
        if (code) {
            error = code;
            if (code == ENODEV) {
-               if ((voltype == BACKVOL) && !(entry.flags & BACK_EXISTS)) {
+               if ((voltype == BACKVOL) && !(entry.flags & VLF_BACKEXISTS)) {
                    /* The VLDB says there is no backup volume and its not on disk */
                    fprintf(STDERR, "Volume %s does not exist\n",
                            as->parms[0].items->data);
@@ -1699,7 +1702,7 @@ ExamineVolume(struct cmd_syndesc *as, void *arock)
            } else
                VolumeStats_int(pntr, &entry, aserver, apart, voltype);
 
-           if ((voltype == BACKVOL) && !(entry.flags & BACK_EXISTS)) {
+           if ((voltype == BACKVOL) && !(entry.flags & VLF_BACKEXISTS)) {
                /* The VLDB says there is no backup volume yet we found one on disk */
                fprintf(STDERR, "Volume %s does not exist in VLDB\n",
                        as->parms[0].items->data);
@@ -2078,7 +2081,7 @@ DeleteAll(struct nvldbentry *entry)
     for (i = 0; i < entry->nServers; i++) {
        curserver = entry->serverNumber[i];
        curpart = entry->serverPartition[i];
-       if (entry->serverFlags[i] & ITSROVOL) {
+       if (entry->serverFlags[i] & VLSF_ROVOL) {
            volid = entry->volumeId[ROVOL];
        } else {
            volid = entry->volumeId[RWVOL];
@@ -2155,9 +2158,9 @@ DeleteVolume(struct cmd_syndesc *as, void *arock)
            return (code);
        }
 
-       if (((volid == entry.volumeId[RWVOL]) && (entry.flags & RW_EXISTS))
+       if (((volid == entry.volumeId[RWVOL]) && (entry.flags & VLF_RWEXISTS))
            || ((volid == entry.volumeId[BACKVOL])
-               && (entry.flags & BACK_EXISTS))) {
+               && (entry.flags & VLF_BACKEXISTS))) {
            idx = Lp_GetRwIndex(&entry);
            if ((idx == -1) || (server && (server != entry.serverNumber[idx]))
                || ((partition != -1)
@@ -2167,9 +2170,9 @@ DeleteVolume(struct cmd_syndesc *as, void *arock)
                return ENOENT;
            }
        } else if ((volid == entry.volumeId[ROVOL])
-                  && (entry.flags & RO_EXISTS)) {
+                  && (entry.flags & VLF_ROEXISTS)) {
            for (idx = -1, j = 0; j < entry.nServers; j++) {
-               if (entry.serverFlags[j] != ITSROVOL)
+               if (!(entry.serverFlags[j] & VLSF_ROVOL))
                    continue;
 
                if (((server == 0) || (server == entry.serverNumber[j]))
@@ -2842,7 +2845,7 @@ BackupVolume(struct cmd_syndesc *as, void *arock)
 
     /* is there a backup volume already? */
 
-    if (entry.flags & BACK_EXISTS) {
+    if (entry.flags & VLF_BACKEXISTS) {
        /* yep, where is it? */
 
        buvolid = entry.volumeId[BACKVOL];
@@ -2891,8 +2894,10 @@ ReleaseVolume(struct cmd_syndesc *as, void *arock)
 
     if (as->parms[1].items) /* -force */
        flags |= (REL_COMPLETE | REL_FULLDUMPS);
-    if (as->parms[2].items) /* -stayonline */
-       flags |= REL_STAYUP;
+    if (as->parms[2].items) { /* -stayonline */
+       fprintf(STDERR, "vos: -stayonline not supported\n");
+       return EINVAL;
+    }
     if (as->parms[3].items) /* -force-reclone */
         flags |= REL_COMPLETE;
 
@@ -4259,8 +4264,8 @@ GetVolumeInfo(afs_uint32 volid, afs_uint32 *server, afs_int32 *part, afs_int32 *
     if (volid == rentry->volumeId[ROVOL]) {
        *voltype = ROVOL;
        for (i = 0; i < rentry->nServers; i++) {
-           if ((index == -1) && (rentry->serverFlags[i] & ITSROVOL)
-               && !(rentry->serverFlags[i] & RO_DONTUSE))
+           if ((index == -1) && (rentry->serverFlags[i] & VLSF_ROVOL)
+               && !(rentry->serverFlags[i] & VLSF_DONTUSE))
                index = i;
        }
        if (index == -1) {
@@ -4503,9 +4508,9 @@ static int CompareVldbEntry(char *p1, char *p2)
     pos2 = -1;
 
     for(i = 0; i < arg1->nServers; i++)
-       if(arg1->serverFlags[i] & ITSRWVOL) pos1 = i;
+       if(arg1->serverFlags[i] & VLSF_RWVOL) pos1 = i;
     for(i = 0; i < arg2->nServers; i++)
-       if(arg2->serverFlags[i] & ITSRWVOL) pos2 = i;
+       if(arg2->serverFlags[i] & VLSF_RWVOL) pos2 = i;
     if(pos1 == -1 || pos2 == -1){
        pos1 = 0;
        pos2 = 0;
@@ -4963,7 +4968,7 @@ BackSys(struct cmd_syndesc *as, void *arock)
            continue;
        }
 
-       if (!(vllist->flags & RW_EXISTS)) {
+       if (!(vllist->flags & VLF_RWEXISTS)) {
            if (verbose) {
                fprintf(STDOUT,
                        "Omitting to backup %s since RW volume does not exist \n",
@@ -5244,6 +5249,7 @@ ChangeAddr(struct cmd_syndesc *as, void *arock)
 {
     afs_int32 ip1, ip2, vcode;
     int remove = 0;
+    int force = 0;
 
     if (noresolve)
        ip1 = GetServerNoresolve(as->parms[0].items->data);
@@ -5261,6 +5267,10 @@ ChangeAddr(struct cmd_syndesc *as, void *arock)
        return (EINVAL);
     }
 
+    if (as->parms[3].items) {
+       force = 1;
+    }
+
     if (as->parms[1].items) {
        if (noresolve)
            ip2 = GetServerNoresolve(as->parms[1].items->data);
@@ -5280,6 +5290,42 @@ ChangeAddr(struct cmd_syndesc *as, void *arock)
        ip1 = 0xffffffff;
     }
 
+    if (!remove && !force) {
+       afs_int32 m_nentries;
+       bulkaddrs m_addrs;
+       afs_int32 m_uniq = 0;
+       afsUUID m_uuid;
+       ListAddrByAttributes m_attrs;
+       char buffer[128];
+
+       memset(&m_attrs, 0, sizeof(m_attrs));
+       memset(&m_uuid, 0, sizeof(m_uuid));
+       memset(&m_addrs, 0, sizeof(m_addrs));
+       memset(buffer, 0, sizeof(buffer));
+
+       m_attrs.Mask = VLADDR_IPADDR;
+       m_attrs.ipaddr = ntohl(ip1);    /* -oldaddr */
+
+       vcode =
+           ubik_VL_GetAddrsU(cstruct, UBIK_CALL_NEW, &m_attrs, &m_uuid,
+                             &m_uniq, &m_nentries, &m_addrs);
+       xdr_free((xdrproc_t) xdr_bulkaddrs, &m_addrs);
+       switch (vcode) {
+       case 0:         /* mh entry detected */
+           afsUUID_to_string(&m_uuid, buffer, sizeof(buffer) - 1);
+           fprintf(STDERR, "vos: Refusing to change address in multi-homed server entry.\n");
+           fprintf(STDERR, "     -oldaddr address is registered to file server UUID %s\n", buffer);
+           fprintf(STDERR, "     Please restart the file server or use vos setaddrs.\n");
+           return EINVAL;
+       case VL_NOENT:
+           break;
+       default:
+           fprintf(STDERR, "vos: could not list the server addresses\n");
+           PrintError("", vcode);
+           return vcode;
+       }
+    }
+
     vcode = ubik_VL_ChangeAddr(cstruct, UBIK_CALL_NEW, ntohl(ip1), ntohl(ip2));
     if (vcode) {
        char hoststr1[16], hoststr2[16];
@@ -5316,7 +5362,7 @@ print_addrs(const bulkaddrs * addrs, afsUUID * m_uuid, int nentries,
            int print)
 {
     int i;
-    afs_uint32 *addrp;
+    afs_uint32 addr;
     char buf[1024];
 
     if (print) {
@@ -5325,14 +5371,13 @@ print_addrs(const bulkaddrs * addrs, afsUUID * m_uuid, int nentries,
     }
 
     /* print out the list of all the server */
-    addrp = (afs_uint32 *) addrs->bulkaddrs_val;
-    for (i = 0; i < nentries; i++, addrp++) {
-       *addrp = htonl(*addrp);
+    for (i = 0; i < addrs->bulkaddrs_len; i++) {
+       addr = htonl(addrs->bulkaddrs_val[i]);
        if (noresolve) {
            char hoststr[16];
-           printf("%s\n", afs_inet_ntoa_r(*addrp, hoststr));
+           printf("%s\n", afs_inet_ntoa_r(addr, hoststr));
        } else {
-           printf("%s\n", hostutil_GetNameByINet(*addrp));
+           printf("%s\n", hostutil_GetNameByINet(addr));
        }
     }
 
@@ -5515,6 +5560,68 @@ SetAddrs(struct cmd_syndesc *as, void *arock)
 }
 
 static int
+RemoveAddrs(struct cmd_syndesc *as, void *arock)
+{
+    afs_int32 code;
+    ListAddrByAttributes attrs;
+    afsUUID uuid;
+    afs_int32 uniq = 0;
+    afs_int32 nentries = 0;
+    bulkaddrs addrs;
+    afs_uint32 ip1;
+    afs_uint32 ip2;
+
+    memset(&attrs, 0, sizeof(ListAddrByAttributes));
+    memset(&addrs, 0, sizeof(bulkaddrs));
+    memset(&uuid, 0, sizeof(afsUUID));
+    attrs.Mask = VLADDR_UUID;
+
+    if (as->parms[0].items) {  /* -uuid */
+       if (afsUUID_from_string(as->parms[0].items->data, &attrs.uuid) < 0) {
+           fprintf(STDERR, "vos: invalid UUID '%s'\n",
+                   as->parms[0].items->data);
+           return EINVAL;
+       }
+    }
+
+    code =
+       ubik_VL_GetAddrsU(cstruct, UBIK_CALL_NEW, &attrs, &uuid, &uniq,
+                         &nentries, &addrs);
+    if (code == VL_NOENT) {
+       fprintf(STDERR, "vos: UUID not found\n");
+       goto out;
+    }
+    if (code != 0) {
+       fprintf(STDERR, "vos: could not list the server addresses\n");
+       PrintError("", code);
+       goto out;
+    }
+    if (addrs.bulkaddrs_len == 0) {
+       fprintf(STDERR, "vos: no addresses found for UUID\n");
+       goto out;
+    }
+
+    ip2 = addrs.bulkaddrs_val[0]; /* network byte order */
+    ip1 = 0xffffffff;             /* indicates removal mode */
+
+    if (verbose) {
+       printf("vos: Removing UUID with hosts:\n");
+       print_addrs(&addrs, &uuid, nentries, 1);
+    }
+
+    code = ubik_VL_ChangeAddr(cstruct, UBIK_CALL_NEW, ip1, ip2);
+    if (code) {
+       fprintf(STDERR, "Could not remove server entry from the VLDB.\n");
+       PrintError("", code);
+    }
+
+  out:
+    xdr_free((xdrproc_t) xdr_bulkaddrs, &addrs);
+    return code;
+}
+
+
+static int
 LockEntry(struct cmd_syndesc *as, void *arock)
 {
     afs_uint32 avolid;
@@ -5606,12 +5713,12 @@ ConvertRO(struct cmd_syndesc *as, void *arock)
 
     MapHostToNetwork(&entry);
     for (i = 0; i < entry.nServers; i++) {
-       if (entry.serverFlags[i] & ITSRWVOL) {
+       if (entry.serverFlags[i] & VLSF_RWVOL) {
            rwserver = entry.serverNumber[i];
            rwpartition = entry.serverPartition[i];
            if (roserver)
                break;
-       } else if ((entry.serverFlags[i] & ITSROVOL) && !roserver) {
+       } else if ((entry.serverFlags[i] & VLSF_ROVOL) && !roserver) {
            same = VLDB_IsSameAddrs(server, entry.serverNumber[i], &code);
            if (code) {
                fprintf(STDERR,
@@ -6010,7 +6117,8 @@ main(int argc, char **argv)
     cmd_AddParm(ts, "-force", CMD_FLAG, CMD_OPTIONAL,
                "force a complete release and full dumps");
     cmd_AddParmAlias(ts, 1, "-f"); /* original force option */
-    cmd_AddParm(ts, "-stayonline", CMD_FLAG, CMD_OPTIONAL,
+    /* keep this reserved */
+    cmd_AddParm(ts, "-stayonline", CMD_FLAG, CMD_OPTIONAL|CMD_HIDDEN,
                "release to cloned temp vol, then clone back to repsite RO");
     cmd_AddParm(ts, "-force-reclone", CMD_FLAG, CMD_OPTIONAL,
                "force a reclone and complete release with incremental dumps");
@@ -6228,6 +6336,8 @@ main(int argc, char **argv)
     cmd_AddParm(ts, "-newaddr", CMD_SINGLE, CMD_OPTIONAL, "new IP address");
     cmd_AddParm(ts, "-remove", CMD_FLAG, CMD_OPTIONAL,
                "remove the IP address from the VLDB");
+    cmd_AddParm(ts, "-force", CMD_FLAG, CMD_OPTIONAL,
+               "allow multi-homed server entry change (not recommended)");
     COMMONPARMS;
 
     ts = cmd_CreateSyntax("listaddrs", ListAddrs, NULL, 0,
@@ -6264,11 +6374,16 @@ main(int argc, char **argv)
     COMMONPARMS;
 
     ts = cmd_CreateSyntax("setaddrs", SetAddrs, NULL, 0,
-                         "set the list of IP address for a given UUID in the VLDB");
+                         "set the list of IP addresses 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;
 
+    ts = cmd_CreateSyntax("remaddrs", RemoveAddrs, NULL, 0,
+                         "remove the list of IP addresses for a given UUID in the VLDB");
+    cmd_AddParm(ts, "-uuid", CMD_SINGLE, 0, "uuid of server");
     COMMONPARMS;
+
     code = cmd_Dispatch(argc, argv);
     if (rxInitDone) {
        /* Shut down the ubik_client and rx connections */