~ubuntu-branches/ubuntu/natty/libvirt/natty-security

« back to all changes in this revision

Viewing changes to src/esx/esx_util.c

  • Committer: Bazaar Package Importer
  • Author(s): Serge Hallyn
  • Date: 2011-02-23 09:05:46 UTC
  • mfrom: (1.2.8 upstream) (3.4.25 sid)
  • Revision ID: james.westby@ubuntu.com-20110223090546-4pwmrrt7h51hr3l3
Tags: 0.8.8-1ubuntu1
* Resynchronize and merge from Debian unstable. Remaining changes:
  - debian/patches:
    * 9000-delayed_iff_up_bridge.patch
    * 9001-dont_clobber_existing_bridges.patch
    * 9002-better_default_uri_virsh.patch
    * 9003-better-default-arch.patch
    * 9004-libvirtd-group-name.patch
    * 9005-increase-unix-socket-timeout.patch
    * 9006-default-config-test-case.patch
    * 9007-fix-daemon-conf-ftbfs.patch
    * 9011-move-ebtables-script.patch
    * 9014-skip-nodeinfotest.patch
    * 9020-lp545795.patch
    * 9021-fix-uint64_t.patch
  - debian/patches/series:
    * Disable qemu-disable-network.diff.patch
  - debian/control:
    * set ubuntu maintainer
    * Build-Depends:
      - swap libxen to libxen3, qemu to qemu-kvm, and open-iscsi to
        open-iscsi-utils in Build-Depends
      - remove virtualbox Build-Depends
      - add libxml2 and libapparmor-dev Build-Depends
    * convert Vcs-Git to Xs-Debian-Vcs-Git
    * libvirt-bin Depends: move netcat-openbsd, bridge-utils, dnsmasq-base
      (>= 2.46-1), and iptables from Recommends to Depends
    * libvirt-bin Recommends: move qemu to Suggests
    * libvirt-bin Suggests: add apparmor
    * libvirt0 Recommands: move lvm2 to Suggests
  - keep debian/libvirt-bin.apport
  - keep debian/libvirt-bin.cron.daily
  - debian/libvirt-bin.dirs:
    * add apparmor, cron.daily, and apport dirs
  - debian/libvirt-bin.examples:
    * add debian/libvirt-suspendonreboot
  - debian/libvirt-bin.install:
    * add /etc/apparmor.d files
    * add apport hook
  - debian/libvirt-bin.manpages:
    * add debian/libvirt-migrate-qemu-disks.1
  - debian/libvirt-bin.postinst:
    * replace libvirt groupname with libvirtd
    * add each admin user to libvirtd group
    * call apparmor_parser on usr.sbin.libvirtd and
      usr.lib.libvirt.virt-aa-helper
    * call 'libvirt-migrate-qemu-disks -a' after
      libvirt-bin has started if migrating from
      older than 0.8.3-1ubuntu1
  - debian/libvirt-bin.postrm:
    * replace libvirt groupname with libvirtd
    * remove usr.sbin.libvirtd and
      usr.lib.libvirt.virt-aa-helper
  - keep added files under debian/:
    * libvirt-bin.upstart
    * libvirt-migrate-qemu-disks
    * libvirt-migrate-qemu-disks.1
    * libvirt-suspendonreboot
    * apparmor profiles
  - debian/README.Debian:
    * add 'Apparmor Profile' section
    * add 'Disk migration' section
  - debian/rules:
    * don't build with vbox since virtualbox-ose is in universe
    * add --with-apparmor to DEB_CONFIGURE_EXTRA_FLAGS
    * set DEB_DH_INSTALLINIT_ARGS to '--upstart-only'
    * set DEB_MAKE_CHECK_TARGET to 'check'
    * remove unneeded binary-install/libvirt-bin:: and clean::
      sections (they only deal with sysvinit stuff)
    * add build/libvirt-bin:: section to install
      - apparmor files
      - apport hooks
      - libvirt-migrate-qemu-disks
* The following Ubuntu packaging changes occurred during the divergence
  between Debian and Ubuntu. These changes are not new, but included here
  for completeness: (0.8.5-0ubuntu1 - 0.8.5-0ubuntu5):
  - Have upstart job source /etc/default/libvirt-bin.  This is only a
    temporary fix until upstart provides proper default override support
    through /etc/init/libvirt-bin.override (or any other mechanism).
    (LP: 708172)
  - debian/apparmor/usr.sbin.libvirtd: use PUx instead of Ux for executables
    (LP: 573315)
  - Rebuild with python 2.7 as the python default.
  - debian/libvirt-bin.cron.daily: use shell globbing to enumerate xml files.
    Based on patch thanks to Henryk Plötz (LP: 655176)
* Dropped the following patches included/fixed upstream:
  - 9010-dont-disable-ipv6.patch
  - 9022-build-cleanup-declaration-of-xen-tests.patch
  - 9023-vah-require-uuid.patch
  - 9009-autodetect-nc-params.patch
    * rolled into Debian's
      Autodetect-if-the-remote-nc-command-supports-the-q-o.patch
* Updated the following patches:
  - 9011-move-ebtables-script.patch:
    * LOCALSTATEDIR is defined in configmake.h
  - 9000-9006: added DEP-3 tags
  - 9002-better_default_uri_virsh.patch: updated (context changed)
* New patches:
  - 9022-drop-booton-when-kernel-specified.patch (LP: #720426)
  - 9023-fix-lxc-console-hangup.patch (LP: #668369)
  - 9024-skip-broken-commandtest.patch
* debian/patches/series:
  - don't apply Disable-CHECKSUM-rule.patch: our iptables can do this
  - don't apply Debian-specific Debianize-libvirt-guests.patch

Show diffs side-by-side

added added

removed removed

Lines of Context:
24
24
 
25
25
#include <config.h>
26
26
 
27
 
#include <c-ctype.h>
28
27
#include <netdb.h>
29
28
 
30
29
#include "internal.h"
34
33
#include "memory.h"
35
34
#include "logging.h"
36
35
#include "uuid.h"
 
36
#include "vmx.h"
37
37
#include "esx_private.h"
38
38
#include "esx_util.h"
39
39
 
415
415
 
416
416
 
417
417
int
418
 
esxUtil_GetConfigString(virConfPtr conf, const char *name, char **string,
419
 
                        bool optional)
420
 
{
421
 
    virConfValuePtr value;
422
 
 
423
 
    *string = NULL;
424
 
    value = virConfGetValue(conf, name);
425
 
 
426
 
    if (value == NULL) {
427
 
        if (optional) {
428
 
            return 0;
429
 
        }
430
 
 
431
 
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
432
 
                  _("Missing essential config entry '%s'"), name);
433
 
        return -1;
434
 
    }
435
 
 
436
 
    if (value->type != VIR_CONF_STRING) {
437
 
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
438
 
                  _("Config entry '%s' must be a string"), name);
439
 
        return -1;
440
 
    }
441
 
 
442
 
    if (value->str == NULL) {
443
 
        if (optional) {
444
 
            return 0;
445
 
        }
446
 
 
447
 
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
448
 
                  _("Missing essential config entry '%s'"), name);
449
 
        return -1;
450
 
    }
451
 
 
452
 
    *string = strdup(value->str);
453
 
 
454
 
    if (*string == NULL) {
455
 
        virReportOOMError();
456
 
        return -1;
457
 
    }
458
 
 
459
 
    return 0;
460
 
}
461
 
 
462
 
 
463
 
 
464
 
int
465
 
esxUtil_GetConfigUUID(virConfPtr conf, const char *name, unsigned char *uuid,
466
 
                      bool optional)
467
 
{
468
 
    virConfValuePtr value;
469
 
 
470
 
    value = virConfGetValue(conf, name);
471
 
 
472
 
    if (value == NULL) {
473
 
        if (optional) {
474
 
            return 0;
475
 
        } else {
476
 
            ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
477
 
                      _("Missing essential config entry '%s'"), name);
478
 
            return -1;
479
 
        }
480
 
    }
481
 
 
482
 
    if (value->type != VIR_CONF_STRING) {
483
 
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
484
 
                  _("Config entry '%s' must be a string"), name);
485
 
        return -1;
486
 
    }
487
 
 
488
 
    if (value->str == NULL) {
489
 
        if (optional) {
490
 
            return 0;
491
 
        } else {
492
 
            ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
493
 
                      _("Missing essential config entry '%s'"), name);
494
 
            return -1;
495
 
        }
496
 
    }
497
 
 
498
 
    if (virUUIDParse(value->str, uuid) < 0) {
499
 
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
500
 
                  _("Could not parse UUID from string '%s'"), value->str);
501
 
        return -1;
502
 
    }
503
 
 
504
 
    return 0;
505
 
}
506
 
 
507
 
 
508
 
 
509
 
int
510
 
esxUtil_GetConfigLong(virConfPtr conf, const char *name, long long *number,
511
 
                      long long default_, bool optional)
512
 
{
513
 
    virConfValuePtr value;
514
 
 
515
 
    *number = default_;
516
 
    value = virConfGetValue(conf, name);
517
 
 
518
 
    if (value == NULL) {
519
 
        if (optional) {
520
 
            return 0;
521
 
        } else {
522
 
            ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
523
 
                      _("Missing essential config entry '%s'"), name);
524
 
            return -1;
525
 
        }
526
 
    }
527
 
 
528
 
    if (value->type == VIR_CONF_STRING) {
529
 
        if (value->str == NULL) {
530
 
            if (optional) {
531
 
                return 0;
532
 
            } else {
533
 
                ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
534
 
                          _("Missing essential config entry '%s'"), name);
535
 
                return -1;
536
 
            }
537
 
        }
538
 
 
539
 
        if (STREQ(value->str, "unlimited")) {
540
 
            *number = -1;
541
 
        } else if (virStrToLong_ll(value->str, NULL, 10, number) < 0) {
542
 
            ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
543
 
                      _("Config entry '%s' must represent an integer value"),
544
 
                      name);
545
 
            return -1;
546
 
        }
547
 
    } else {
548
 
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
549
 
                  _("Config entry '%s' must be a string"), name);
550
 
        return -1;
551
 
    }
552
 
 
553
 
    return 0;
554
 
}
555
 
 
556
 
 
557
 
 
558
 
int
559
 
esxUtil_GetConfigBoolean(virConfPtr conf, const char *name, bool *boolean_,
560
 
                         bool default_, bool optional)
561
 
{
562
 
    virConfValuePtr value;
563
 
 
564
 
    *boolean_ = default_;
565
 
    value = virConfGetValue(conf, name);
566
 
 
567
 
    if (value == NULL) {
568
 
        if (optional) {
569
 
            return 0;
570
 
        } else {
571
 
            ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
572
 
                      _("Missing essential config entry '%s'"), name);
573
 
            return -1;
574
 
        }
575
 
    }
576
 
 
577
 
    if (value->type == VIR_CONF_STRING) {
578
 
        if (value->str == NULL) {
579
 
            if (optional) {
580
 
                return 0;
581
 
            } else {
582
 
                ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
583
 
                          _("Missing essential config entry '%s'"), name);
584
 
                return -1;
585
 
            }
586
 
        }
587
 
 
588
 
        if (STRCASEEQ(value->str, "true")) {
589
 
            *boolean_ = 1;
590
 
        } else if (STRCASEEQ(value->str, "false")) {
591
 
            *boolean_ = 0;
592
 
        } else {
593
 
            ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
594
 
                      _("Config entry '%s' must represent a boolean value "
595
 
                        "(true|false)"), name);
596
 
            return -1;
597
 
        }
598
 
    } else {
599
 
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
600
 
                  _("Config entry '%s' must be a string"), name);
601
 
        return -1;
602
 
    }
603
 
 
604
 
    return 0;
605
 
}
606
 
 
607
 
 
608
 
 
609
 
int
610
418
esxUtil_ReformatUuid(const char *input, char *output)
611
419
{
612
420
    unsigned char uuid[VIR_UUID_BUFLEN];
626
434
 
627
435
 
628
436
char *
629
 
esxUtil_EscapeHex(const char *string, char escape, const char *special)
630
 
{
631
 
    char *escaped = NULL;
632
 
    size_t length = 1; /* 1 byte for termination */
633
 
    const char *tmp1 = string;
634
 
    char *tmp2;
635
 
 
636
 
    /* Calculate length of escaped string */
637
 
    while (*tmp1 != '\0') {
638
 
        if (*tmp1 == escape || strspn(tmp1, special) > 0) {
639
 
            length += 2;
640
 
        }
641
 
 
642
 
        ++tmp1;
643
 
        ++length;
644
 
    }
645
 
 
646
 
    if (VIR_ALLOC_N(escaped, length) < 0) {
647
 
        virReportOOMError();
648
 
        return NULL;
649
 
    }
650
 
 
651
 
    tmp1 = string; /* reading from this one */
652
 
    tmp2 = escaped; /* writing to this one */
653
 
 
654
 
    /* Escape to 'cXX' where c is the escape char and X is a hex digit */
655
 
    while (*tmp1 != '\0') {
656
 
        if (*tmp1 == escape || strspn(tmp1, special) > 0) {
657
 
            *tmp2++ = escape;
658
 
 
659
 
            snprintf(tmp2, 3, "%02x", (unsigned int)*tmp1);
660
 
 
661
 
            tmp2 += 2;
662
 
        } else {
663
 
            *tmp2++ = *tmp1;
664
 
        }
665
 
 
666
 
        ++tmp1;
667
 
    }
668
 
 
669
 
    *tmp2 = '\0';
670
 
 
671
 
    return escaped;
672
 
}
673
 
 
674
 
 
675
 
 
676
 
int
677
 
esxUtil_UnescapeHex(char *string, char escape)
678
 
{
679
 
    char *tmp1 = string; /* reading from this one */
680
 
    char *tmp2 = string; /* writing to this one */
681
 
 
682
 
    /* Unescape from 'cXX' where c is the escape char and X is a hex digit */
683
 
    while (*tmp1 != '\0') {
684
 
        if (*tmp1 == escape) {
685
 
            if (!c_isxdigit(tmp1[1]) || !c_isxdigit(tmp1[2])) {
686
 
                return -1;
687
 
            }
688
 
 
689
 
            *tmp2++ = virHexToBin(tmp1[1]) * 16 + virHexToBin(tmp1[2]);
690
 
            tmp1 += 3;
691
 
        } else {
692
 
            *tmp2++ = *tmp1++;
693
 
        }
694
 
    }
695
 
 
696
 
    *tmp2 = '\0';
697
 
 
698
 
    return 0;
699
 
}
700
 
 
701
 
 
702
 
 
703
 
char *
704
437
esxUtil_EscapeBase64(const char *string)
705
438
{
706
439
    /* 'normal' characters don't get base64 encoded */
805
538
 
806
539
    esxUtil_ReplaceSpecialWindowsPathChars(replaced);
807
540
 
808
 
    escaped1 = esxUtil_EscapeHexPercent(replaced);
 
541
    escaped1 = virVMXEscapeHexPercent(replaced);
809
542
 
810
543
    if (escaped1 == NULL) {
811
544
        goto cleanup;
819
552
 
820
553
    return escaped2;
821
554
}
822
 
 
823
 
 
824
 
 
825
 
char *
826
 
esxUtil_ConvertToUTF8(const char *encoding, const char *string)
827
 
{
828
 
    char *result = NULL;
829
 
    xmlCharEncodingHandlerPtr handler;
830
 
    xmlBufferPtr input;
831
 
    xmlBufferPtr utf8;
832
 
 
833
 
    handler = xmlFindCharEncodingHandler(encoding);
834
 
 
835
 
    if (handler == NULL) {
836
 
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
837
 
                  _("libxml2 doesn't handle %s encoding"), encoding);
838
 
        return NULL;
839
 
    }
840
 
 
841
 
    input = xmlBufferCreateStatic((char *)string, strlen(string));
842
 
    utf8 = xmlBufferCreate();
843
 
 
844
 
    if (xmlCharEncInFunc(handler, utf8, input) < 0) {
845
 
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
846
 
                  _("Could not convert from %s to UTF-8 encoding"), encoding);
847
 
        goto cleanup;
848
 
    }
849
 
 
850
 
    result = (char *)utf8->content;
851
 
    utf8->content = NULL;
852
 
 
853
 
  cleanup:
854
 
    xmlCharEncCloseFunc(handler);
855
 
    xmlBufferFree(input);
856
 
    xmlBufferFree(utf8);
857
 
 
858
 
    return result;
859
 
}