esx_util.c 15.0 KB
Newer Older
1 2

/*
3
 * esx_util.c: utility functions for the VMware ESX driver
4
 *
5
 * Copyright (C) 2010-2012 Red Hat, Inc.
6
 * Copyright (C) 2009-2011 Matthias Bolte <matthias.bolte@googlemail.com>
7 8 9 10 11 12 13 14 15 16 17 18 19
 * Copyright (C) 2009 Maximilian Wilhelm <max@rfc2324.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
20
 * License along with this library.  If not, see
O
Osier Yang 已提交
21
 * <http://www.gnu.org/licenses/>.
22 23 24 25 26 27 28 29 30 31
 *
 */

#include <config.h>

#include <netdb.h>

#include "internal.h"
#include "datatypes.h"
#include "util.h"
32
#include "viralloc.h"
33
#include "virlog.h"
34
#include "uuid.h"
35
#include "vmx.h"
36
#include "esx_private.h"
37 38 39 40
#include "esx_util.h"

#define VIR_FROM_THIS VIR_FROM_ESX

41

42

43
int
M
Martin Kletzander 已提交
44
esxUtil_ParseUri(esxUtil_ParsedUri **parsedUri, virURIPtr uri)
45
{
M
Matthias Bolte 已提交
46
    int result = -1;
47 48 49
    int i;
    int noVerify;
    int autoAnswer;
M
Matthias Bolte 已提交
50
    char *tmp;
51

M
Matthias Bolte 已提交
52
    if (parsedUri == NULL || *parsedUri != NULL) {
53
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Invalid argument"));
54
        return -1;
55 56
    }

M
Matthias Bolte 已提交
57
    if (VIR_ALLOC(*parsedUri) < 0) {
58 59
        virReportOOMError();
        return -1;
60 61
    }

62 63
    for (i = 0; i < uri->paramsCount; i++) {
        virURIParamPtr queryParam = &uri->params[i];
64

65
        if (STRCASEEQ(queryParam->name, "transport")) {
M
Matthias Bolte 已提交
66
            VIR_FREE((*parsedUri)->transport);
67

M
Matthias Bolte 已提交
68
            (*parsedUri)->transport = strdup(queryParam->value);
69

M
Matthias Bolte 已提交
70
            if ((*parsedUri)->transport == NULL) {
71
                virReportOOMError();
M
Matthias Bolte 已提交
72
                goto cleanup;
73 74
            }

M
Matthias Bolte 已提交
75 76
            if (STRNEQ((*parsedUri)->transport, "http") &&
                STRNEQ((*parsedUri)->transport, "https")) {
77 78 79 80
                virReportError(VIR_ERR_INVALID_ARG,
                               _("Query parameter 'transport' has unexpected value "
                                 "'%s' (should be http|https)"),
                               (*parsedUri)->transport);
M
Matthias Bolte 已提交
81
                goto cleanup;
82
            }
83
        } else if (STRCASEEQ(queryParam->name, "vcenter")) {
M
Matthias Bolte 已提交
84
            VIR_FREE((*parsedUri)->vCenter);
85

M
Matthias Bolte 已提交
86
            (*parsedUri)->vCenter = strdup(queryParam->value);
87

M
Matthias Bolte 已提交
88
            if ((*parsedUri)->vCenter == NULL) {
89
                virReportOOMError();
M
Matthias Bolte 已提交
90
                goto cleanup;
91
            }
92
        } else if (STRCASEEQ(queryParam->name, "no_verify")) {
93 94
            if (virStrToLong_i(queryParam->value, NULL, 10, &noVerify) < 0 ||
                (noVerify != 0 && noVerify != 1)) {
95 96 97
                virReportError(VIR_ERR_INVALID_ARG,
                               _("Query parameter 'no_verify' has unexpected value "
                                 "'%s' (should be 0 or 1)"), queryParam->value);
M
Matthias Bolte 已提交
98
                goto cleanup;
99
            }
100

M
Matthias Bolte 已提交
101
            (*parsedUri)->noVerify = noVerify != 0;
102 103 104
        } else if (STRCASEEQ(queryParam->name, "auto_answer")) {
            if (virStrToLong_i(queryParam->value, NULL, 10, &autoAnswer) < 0 ||
                (autoAnswer != 0 && autoAnswer != 1)) {
105 106 107
                virReportError(VIR_ERR_INVALID_ARG,
                               _("Query parameter 'auto_answer' has unexpected "
                                 "value '%s' (should be 0 or 1)"), queryParam->value);
M
Matthias Bolte 已提交
108
                goto cleanup;
109
            }
110

M
Matthias Bolte 已提交
111
            (*parsedUri)->autoAnswer = autoAnswer != 0;
M
Matthias Bolte 已提交
112 113
        } else if (STRCASEEQ(queryParam->name, "proxy")) {
            /* Expected format: [<type>://]<hostname>[:<port>] */
M
Matthias Bolte 已提交
114 115 116 117
            (*parsedUri)->proxy = true;
            (*parsedUri)->proxy_type = CURLPROXY_HTTP;
            VIR_FREE((*parsedUri)->proxy_hostname);
            (*parsedUri)->proxy_port = 1080;
M
Matthias Bolte 已提交
118 119

            if ((tmp = STRSKIP(queryParam->value, "http://")) != NULL) {
M
Matthias Bolte 已提交
120
                (*parsedUri)->proxy_type = CURLPROXY_HTTP;
M
Matthias Bolte 已提交
121 122
            } else if ((tmp = STRSKIP(queryParam->value, "socks://")) != NULL ||
                       (tmp = STRSKIP(queryParam->value, "socks5://")) != NULL) {
M
Matthias Bolte 已提交
123
                (*parsedUri)->proxy_type = CURLPROXY_SOCKS5;
M
Matthias Bolte 已提交
124
            } else if ((tmp = STRSKIP(queryParam->value, "socks4://")) != NULL) {
M
Matthias Bolte 已提交
125
                (*parsedUri)->proxy_type = CURLPROXY_SOCKS4;
M
Matthias Bolte 已提交
126
            } else if ((tmp = STRSKIP(queryParam->value, "socks4a://")) != NULL) {
M
Matthias Bolte 已提交
127
                (*parsedUri)->proxy_type = CURLPROXY_SOCKS4A;
M
Matthias Bolte 已提交
128 129 130
            } else if ((tmp = strstr(queryParam->value, "://")) != NULL) {
                *tmp = '\0';

131 132 133 134
                virReportError(VIR_ERR_INVALID_ARG,
                               _("Query parameter 'proxy' contains unexpected "
                                 "type '%s' (should be (http|socks(|4|4a|5))"),
                               queryParam->value);
M
Matthias Bolte 已提交
135 136 137 138 139
                goto cleanup;
            } else {
                tmp = queryParam->value;
            }

M
Matthias Bolte 已提交
140
            (*parsedUri)->proxy_hostname = strdup(tmp);
M
Matthias Bolte 已提交
141

M
Matthias Bolte 已提交
142
            if ((*parsedUri)->proxy_hostname == NULL) {
M
Matthias Bolte 已提交
143 144 145 146
                virReportOOMError();
                goto cleanup;
            }

M
Matthias Bolte 已提交
147 148
            if ((tmp = strchr((*parsedUri)->proxy_hostname, ':')) != NULL) {
                if (tmp == (*parsedUri)->proxy_hostname) {
149 150 151
                    virReportError(VIR_ERR_INVALID_ARG, "%s",
                                   _("Query parameter 'proxy' doesn't contain a "
                                     "hostname"));
M
Matthias Bolte 已提交
152 153 154 155 156 157
                    goto cleanup;
                }

                *tmp++ = '\0';

                if (virStrToLong_i(tmp, NULL, 10,
M
Matthias Bolte 已提交
158 159 160
                                   &(*parsedUri)->proxy_port) < 0 ||
                    (*parsedUri)->proxy_port < 1 ||
                    (*parsedUri)->proxy_port > 65535) {
161
                    virReportError(VIR_ERR_INVALID_ARG,
162 163 164
                                   _("Query parameter 'proxy' has unexpected "
                                     "port value '%s' (should be [1..65535])"),
                                   tmp);
M
Matthias Bolte 已提交
165 166 167
                    goto cleanup;
                }
            }
168 169 170 171 172 173
        } else {
            VIR_WARN("Ignoring unexpected query parameter '%s'",
                     queryParam->name);
        }
    }

M
Matthias Bolte 已提交
174
    if (uri->path != NULL) {
175
        (*parsedUri)->path = strdup(uri->path);
176

177
        if ((*parsedUri)->path == NULL) {
M
Matthias Bolte 已提交
178 179 180 181 182 183 184 185 186
            virReportOOMError();
            goto cleanup;
        }
    }

    if ((*parsedUri)->transport == NULL) {
        (*parsedUri)->transport = strdup("https");

        if ((*parsedUri)->transport == NULL) {
187
            virReportOOMError();
M
Matthias Bolte 已提交
188
            goto cleanup;
189 190 191
        }
    }

M
Matthias Bolte 已提交
192
    result = 0;
193

M
Matthias Bolte 已提交
194 195
  cleanup:
    if (result < 0) {
M
Matthias Bolte 已提交
196
        esxUtil_FreeParsedUri(parsedUri);
197 198
    }

M
Matthias Bolte 已提交
199
    return result;
200 201 202 203
}



204 205

void
M
Matthias Bolte 已提交
206
esxUtil_FreeParsedUri(esxUtil_ParsedUri **parsedUri)
207
{
M
Matthias Bolte 已提交
208
    if (parsedUri == NULL || *parsedUri == NULL) {
209 210 211
        return;
    }

M
Matthias Bolte 已提交
212 213 214
    VIR_FREE((*parsedUri)->transport);
    VIR_FREE((*parsedUri)->vCenter);
    VIR_FREE((*parsedUri)->proxy_hostname);
215
    VIR_FREE((*parsedUri)->path);
216

M
Matthias Bolte 已提交
217
    VIR_FREE(*parsedUri);
218 219 220 221
}



222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
int
esxUtil_ParseVirtualMachineIDString(const char *id_string, int *id)
{
    /* Try to parse an integer from the complete string. */
    if (virStrToLong_i(id_string, NULL, 10, id) == 0) {
        return 0;
    }

    /*
     * If that fails try to parse an integer from the string tail
     * assuming the naming scheme Virtual Center seems to use.
     */
    if (STRPREFIX(id_string, "vm-")) {
        if (virStrToLong_i(id_string + 3, NULL, 10, id) == 0) {
            return 0;
        }
    }

    return -1;
}



M
Matthias Bolte 已提交
245
int
246
esxUtil_ParseDatastorePath(const char *datastorePath, char **datastoreName,
247
                           char **directoryName, char **directoryAndFileName)
M
Matthias Bolte 已提交
248
{
M
Matthias Bolte 已提交
249
    int result = -1;
250
    char *copyOfDatastorePath = NULL;
251 252 253
    char *tmp = NULL;
    char *saveptr = NULL;
    char *preliminaryDatastoreName = NULL;
254
    char *preliminaryDirectoryAndFileName = NULL;
M
Matthias Bolte 已提交
255

256 257 258
    if ((datastoreName != NULL && *datastoreName != NULL) ||
        (directoryName != NULL && *directoryName != NULL) ||
        (directoryAndFileName != NULL && *directoryAndFileName != NULL)) {
259
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Invalid argument"));
260
        return -1;
M
Matthias Bolte 已提交
261 262
    }

263
    if (esxVI_String_DeepCopyValue(&copyOfDatastorePath, datastorePath) < 0) {
M
Matthias Bolte 已提交
264
        goto cleanup;
265 266
    }

267 268 269
    /* Expected format: '[<datastore>] <path>' where <path> is optional */
    if ((tmp = STRSKIP(copyOfDatastorePath, "[")) == NULL || *tmp == ']' ||
        (preliminaryDatastoreName = strtok_r(tmp, "]", &saveptr)) == NULL) {
270 271 272
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Datastore path '%s' doesn't have expected format "
                         "'[<datastore>] <path>'"), datastorePath);
M
Matthias Bolte 已提交
273
        goto cleanup;
M
Matthias Bolte 已提交
274 275
    }

276 277
    if (datastoreName != NULL &&
        esxVI_String_DeepCopyValue(datastoreName,
278
                                   preliminaryDatastoreName) < 0) {
M
Matthias Bolte 已提交
279
        goto cleanup;
280 281
    }

282
    preliminaryDirectoryAndFileName = strtok_r(NULL, "", &saveptr);
283

284 285 286 287 288 289
    if (preliminaryDirectoryAndFileName == NULL) {
        preliminaryDirectoryAndFileName = (char *)"";
    } else {
        preliminaryDirectoryAndFileName +=
          strspn(preliminaryDirectoryAndFileName, " ");
    }
M
Matthias Bolte 已提交
290

291 292 293 294 295
    if (directoryAndFileName != NULL &&
        esxVI_String_DeepCopyValue(directoryAndFileName,
                                   preliminaryDirectoryAndFileName) < 0) {
        goto cleanup;
    }
M
Matthias Bolte 已提交
296

297
    if (directoryName != NULL) {
298 299
        /* Split <path> into <directory>/<file> and remove /<file> */
        tmp = strrchr(preliminaryDirectoryAndFileName, '/');
300

301 302
        if (tmp != NULL) {
            *tmp = '\0';
M
Matthias Bolte 已提交
303 304
        }

305
        if (esxVI_String_DeepCopyValue(directoryName,
306
                                       preliminaryDirectoryAndFileName) < 0) {
M
Matthias Bolte 已提交
307
            goto cleanup;
308
        }
M
Matthias Bolte 已提交
309 310
    }

M
Matthias Bolte 已提交
311 312
    result = 0;

M
Matthias Bolte 已提交
313
  cleanup:
M
Matthias Bolte 已提交
314
    if (result < 0) {
315 316 317 318 319 320 321 322 323 324 325
        if (datastoreName != NULL) {
            VIR_FREE(*datastoreName);
        }

        if (directoryName != NULL) {
            VIR_FREE(*directoryName);
        }

        if (directoryAndFileName != NULL) {
            VIR_FREE(*directoryAndFileName);
        }
M
Matthias Bolte 已提交
326 327
    }

328
    VIR_FREE(copyOfDatastorePath);
M
Matthias Bolte 已提交
329 330 331 332 333 334

    return result;
}



335
int
336
esxUtil_ResolveHostname(const char *hostname,
M
Matthias Bolte 已提交
337
                        char *ipAddress, size_t ipAddress_length)
338 339 340 341 342
{
    struct addrinfo hints;
    struct addrinfo *result = NULL;
    int errcode;

343
    memset(&hints, 0, sizeof(hints));
344 345 346 347 348 349 350 351 352

    hints.ai_flags = AI_ADDRCONFIG;
    hints.ai_family = AF_INET;
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_protocol = 0;

    errcode = getaddrinfo(hostname, NULL, &hints, &result);

    if (errcode != 0) {
353 354 355
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("IP address lookup for host '%s' failed: %s"), hostname,
                       gai_strerror(errcode));
356 357 358 359
        return -1;
    }

    if (result == NULL) {
360 361 362
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("No IP address for host '%s' found: %s"), hostname,
                       gai_strerror(errcode));
363 364 365
        return -1;
    }

M
Matthias Bolte 已提交
366 367
    errcode = getnameinfo(result->ai_addr, result->ai_addrlen, ipAddress,
                          ipAddress_length, NULL, 0, NI_NUMERICHOST);
368 369

    if (errcode != 0) {
370 371 372
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Formatting IP address for host '%s' failed: %s"), hostname,
                       gai_strerror(errcode));
373 374 375 376 377 378 379 380 381 382 383
        freeaddrinfo(result);
        return -1;
    }

    freeaddrinfo(result);

    return 0;
}



384 385 386 387 388 389
int
esxUtil_ReformatUuid(const char *input, char *output)
{
    unsigned char uuid[VIR_UUID_BUFLEN];

    if (virUUIDParse(input, uuid) < 0) {
390 391 392
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Could not parse UUID from string '%s'"),
                       input);
393 394 395 396 397 398 399
        return -1;
    }

    virUUIDFormat(uuid, output);

    return 0;
}
400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507



char *
esxUtil_EscapeBase64(const char *string)
{
    /* 'normal' characters don't get base64 encoded */
    static const char *normal =
      "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'(),. _-";

    /* VMware uses ',' instead of the path separator '/' in the base64 alphabet */
    static const char *base64 =
      "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+,";

    virBuffer buffer = VIR_BUFFER_INITIALIZER;
    const char *tmp1 = string;
    size_t length;
    unsigned char c1, c2, c3;

    /* Escape sequences of non-'normal' characters as base64 without padding */
    while (*tmp1 != '\0') {
        length = strspn(tmp1, normal);

        if (length > 0) {
            virBufferAdd(&buffer, tmp1, length);

            tmp1 += length;
        } else {
            length = strcspn(tmp1, normal);

            virBufferAddChar(&buffer, '+');

            while (length > 0) {
                c1 = *tmp1++;
                c2 = length > 1 ? *tmp1++ : 0;
                c3 = length > 2 ? *tmp1++ : 0;

                virBufferAddChar(&buffer, base64[(c1 >> 2) & 0x3f]);
                virBufferAddChar(&buffer, base64[((c1 << 4) + (c2 >> 4)) & 0x3f]);

                if (length > 1) {
                    virBufferAddChar(&buffer, base64[((c2 << 2) + (c3 >> 6)) & 0x3f]);
                }

                if (length > 2) {
                    virBufferAddChar(&buffer, base64[c3 & 0x3f]);
                }

                length -= length > 3 ? 3 : length;
            }

            if (*tmp1 != '\0') {
                virBufferAddChar(&buffer, '-');
            }
        }
    }

    if (virBufferError(&buffer)) {
        virReportOOMError();
        virBufferFreeAndReset(&buffer);

        return NULL;
    }

    return virBufferContentAndReset(&buffer);
}



void
esxUtil_ReplaceSpecialWindowsPathChars(char *string)
{
    /* '/' and '\\' are missing on purpose */
    static const char *specials = "\"*<>:|?";

    char *tmp = string;
    size_t length;

    while (*tmp != '\0') {
        length = strspn(tmp, specials);

        while (length > 0) {
            *tmp++ = '_';
            --length;
        }

        if (*tmp != '\0') {
            ++tmp;
        }
    }
}



char *
esxUtil_EscapeDatastoreItem(const char *string)
{
    char *replaced = strdup(string);
    char *escaped1;
    char *escaped2 = NULL;

    if (replaced == NULL) {
        virReportOOMError();
        return NULL;
    }

    esxUtil_ReplaceSpecialWindowsPathChars(replaced);

508
    escaped1 = virVMXEscapeHexPercent(replaced);
509 510 511 512 513 514 515 516 517 518 519 520 521

    if (escaped1 == NULL) {
        goto cleanup;
    }

    escaped2 = esxUtil_EscapeBase64(escaped1);

  cleanup:
    VIR_FREE(replaced);
    VIR_FREE(escaped1);

    return escaped2;
}
M
Matthias Bolte 已提交
522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540



char *
esxUtil_EscapeForXml(const char *string)
{
    virBuffer buffer = VIR_BUFFER_INITIALIZER;

    virBufferEscapeString(&buffer, "%s", string);

    if (virBufferError(&buffer)) {
        virReportOOMError();
        virBufferFreeAndReset(&buffer);

        return NULL;
    }

    return virBufferContentAndReset(&buffer);
}