From nobody Mon Apr 29 10:23:36 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=libvir-list-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1513609014282527.2908816268656; Mon, 18 Dec 2017 06:56:54 -0800 (PST) Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 60771C04AC40; Mon, 18 Dec 2017 14:56:52 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 2F75784D17; Mon, 18 Dec 2017 14:56:52 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id E7D8B1800BDF; Mon, 18 Dec 2017 14:56:51 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id vBIEugLA009000 for ; Mon, 18 Dec 2017 09:56:42 -0500 Received: by smtp.corp.redhat.com (Postfix) id 39C7D7011C; Mon, 18 Dec 2017 14:56:42 +0000 (UTC) Received: from mx1.redhat.com (ext-mx07.extmail.prod.ext.phx2.redhat.com [10.5.110.31]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 313B37010A for ; Mon, 18 Dec 2017 14:56:40 +0000 (UTC) Received: from smtp.nue.novell.com (smtp.nue.novell.com [195.135.221.5]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id CE218C04AC40 for ; Mon, 18 Dec 2017 14:56:38 +0000 (UTC) Received: from emea4-mta.ukb.novell.com ([10.120.13.87]) by smtp.nue.novell.com with ESMTP (TLS encrypted); Mon, 18 Dec 2017 15:56:37 +0100 Received: from laptop.tf.local (nwb-a10-snat.microfocus.com [10.120.13.202]) by emea4-mta.ukb.novell.com with ESMTP (TLS encrypted); Mon, 18 Dec 2017 14:56:07 +0000 From: =?UTF-8?q?C=C3=A9dric=20Bosdonnat?= To: libvir-list@redhat.com Date: Mon, 18 Dec 2017 15:56:03 +0100 Message-Id: <20171218145604.20670-2-cbosdonnat@suse.com> In-Reply-To: <20171218145604.20670-1-cbosdonnat@suse.com> References: <20171218145604.20670-1-cbosdonnat@suse.com> X-Greylist: Sender passed SPF test, Sender IP whitelisted by DNSRBL, ACL 207 matched, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.31]); Mon, 18 Dec 2017 14:56:39 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.31]); Mon, 18 Dec 2017 14:56:39 +0000 (UTC) for IP:'195.135.221.5' DOMAIN:'smtp.nue.novell.com' HELO:'smtp.nue.novell.com' FROM:'cbosdonnat@suse.com' RCPT:'' X-RedHat-Spam-Score: -2.301 (RCVD_IN_DNSWL_MED, SPF_PASS) 195.135.221.5 smtp.nue.novell.com 195.135.221.5 smtp.nue.novell.com X-Scanned-By: MIMEDefang 2.78 on 10.5.110.31 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-loop: libvir-list@redhat.com Cc: =?UTF-8?q?C=C3=A9dric=20Bosdonnat?= Subject: [libvirt] [PATCH 1/2] Add virStringFilterChars() string utility X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.31]); Mon, 18 Dec 2017 14:56:52 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" Add a function to filter a string based on a list of valid characters. --- src/libvirt_private.syms | 1 + src/util/virstring.c | 24 ++++++++++++++++++++++++ src/util/virstring.h | 1 + tests/virstringtest.c | 46 ++++++++++++++++++++++++++++++++++++++++++++= ++ 4 files changed, 72 insertions(+) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index d5c3b9abb..31adaf324 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -2753,6 +2753,7 @@ virStrcpy; virStrdup; virStringBufferIsPrintable; virStringEncodeBase64; +virStringFilterChars; virStringHasChars; virStringHasControlChars; virStringIsEmpty; diff --git a/src/util/virstring.c b/src/util/virstring.c index b2ebce27f..b808aff2c 100644 --- a/src/util/virstring.c +++ b/src/util/virstring.c @@ -1293,6 +1293,30 @@ virStringStripControlChars(char *str) str[j] =3D '\0'; } =20 +/** + * virStringFilterChars: + * @str: the string to strip + * @valid: the valid characters for the string + * + * Modify the string in-place to remove the characters that aren't + * in the list of valid ones. + */ +void +virStringFilterChars(char *str, const char *valid) +{ + size_t len, i, j; + + if (!str) + return; + + len =3D strlen(str); + for (i =3D 0, j =3D 0; i < len; i++) { + if (strchr(valid, str[i])) + str[j++] =3D str[i]; + } + str[j] =3D '\0'; +} + /** * virStringToUpper: * @str: string to capitalize diff --git a/src/util/virstring.h b/src/util/virstring.h index b19abaf9f..8af054bce 100644 --- a/src/util/virstring.h +++ b/src/util/virstring.h @@ -293,6 +293,7 @@ bool virStringHasChars(const char *str, const char *chars); bool virStringHasControlChars(const char *str); void virStringStripControlChars(char *str); +void virStringFilterChars(char *str, const char *valid); =20 bool virStringIsPrintable(const char *str); bool virStringBufferIsPrintable(const uint8_t *buf, size_t buflen); diff --git a/tests/virstringtest.c b/tests/virstringtest.c index 320f7a398..e8518ede1 100644 --- a/tests/virstringtest.c +++ b/tests/virstringtest.c @@ -767,6 +767,36 @@ static int testStripControlChars(const void *args) return ret; } =20 +struct testFilterData { + const char *string; + const char *valid; + const char *result; +}; + +static int testFilterChars(const void *args) +{ + const struct testFilterData *data =3D args; + int ret =3D -1; + char *res =3D NULL; + + if (VIR_STRDUP(res, data->string) < 0) + goto cleanup; + + virStringFilterChars(res, data->valid); + + if (STRNEQ_NULLABLE(res, data->result)) { + fprintf(stderr, "Returned '%s', expected '%s'\n", + NULLSTR(res), NULLSTR(data->result)); + goto cleanup; + } + + ret =3D 0; + + cleanup: + VIR_FREE(res); + return ret; +} + static int mymain(void) { @@ -1085,6 +1115,22 @@ mymain(void) TEST_STRIP_CONTROL_CHARS("\x01H\x02" "E\x03L\x04L\x05O", "HELLO"); TEST_STRIP_CONTROL_CHARS("\x01\x02\x03\x04HELL\x05O", "HELLO"); TEST_STRIP_CONTROL_CHARS("\nhello \x01\x07hello\t", "\nhello hello\t"); + +#define TEST_FILTER_CHARS(str, filter, res) \ + do { \ + struct testFilterData filterData =3D { \ + .string =3D str, \ + .valid =3D filter, \ + .result =3D res, \ + }; \ + if (virTestRun("Filter chars from " #str, \ + testFilterChars, &filterData) < 0) \ + ret =3D -1; \ + } while (0) + + TEST_FILTER_CHARS(NULL, NULL, NULL); + TEST_FILTER_CHARS("hello 123 hello", "helo", "hellohello"); + return ret =3D=3D 0 ? EXIT_SUCCESS : EXIT_FAILURE; } =20 --=20 2.15.1 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list From nobody Mon Apr 29 10:23:36 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=libvir-list-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1513609016528148.42071409496805; Mon, 18 Dec 2017 06:56:56 -0800 (PST) Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 104DC8008D; Mon, 18 Dec 2017 14:56:55 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id DD5B27011C; Mon, 18 Dec 2017 14:56:54 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id A351E1801214; Mon, 18 Dec 2017 14:56:54 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id vBIEuitF009017 for ; Mon, 18 Dec 2017 09:56:44 -0500 Received: by smtp.corp.redhat.com (Postfix) id 403A76EE59; Mon, 18 Dec 2017 14:56:44 +0000 (UTC) Received: from mx1.redhat.com (ext-mx02.extmail.prod.ext.phx2.redhat.com [10.5.110.26]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 37CA970126 for ; Mon, 18 Dec 2017 14:56:40 +0000 (UTC) Received: from smtp.nue.novell.com (smtp.nue.novell.com [195.135.221.5]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id CE81980C0A for ; Mon, 18 Dec 2017 14:56:38 +0000 (UTC) Received: from emea4-mta.ukb.novell.com ([10.120.13.87]) by smtp.nue.novell.com with ESMTP (TLS encrypted); Mon, 18 Dec 2017 15:56:37 +0100 Received: from laptop.tf.local (nwb-a10-snat.microfocus.com [10.120.13.202]) by emea4-mta.ukb.novell.com with ESMTP (TLS encrypted); Mon, 18 Dec 2017 14:56:08 +0000 From: =?UTF-8?q?C=C3=A9dric=20Bosdonnat?= To: libvir-list@redhat.com Date: Mon, 18 Dec 2017 15:56:04 +0100 Message-Id: <20171218145604.20670-3-cbosdonnat@suse.com> In-Reply-To: <20171218145604.20670-1-cbosdonnat@suse.com> References: <20171218145604.20670-1-cbosdonnat@suse.com> X-Greylist: Sender passed SPF test, Sender IP whitelisted by DNSRBL, ACL 207 matched, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.26]); Mon, 18 Dec 2017 14:56:39 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.26]); Mon, 18 Dec 2017 14:56:39 +0000 (UTC) for IP:'195.135.221.5' DOMAIN:'smtp.nue.novell.com' HELO:'smtp.nue.novell.com' FROM:'cbosdonnat@suse.com' RCPT:'' X-RedHat-Spam-Score: -2.301 (RCVD_IN_DNSWL_MED, SPF_PASS) 195.135.221.5 smtp.nue.novell.com 195.135.221.5 smtp.nue.novell.com X-Scanned-By: MIMEDefang 2.78 on 10.5.110.26 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-loop: libvir-list@redhat.com Cc: =?UTF-8?q?C=C3=A9dric=20Bosdonnat?= Subject: [libvirt] [PATCH 2/2] lxc: set a hostname based on the container name X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.28]); Mon, 18 Dec 2017 14:56:55 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" Set a transient hostname on containers. The hostname is computed from the container name, only keeping the valid characters [a-zA-Z0-9-] in it. This filtering is based on RFC 1123 and allows a digit to start the hostname. --- src/lxc/lxc_container.c | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/src/lxc/lxc_container.c b/src/lxc/lxc_container.c index b7216d6ee..96fceaf1b 100644 --- a/src/lxc/lxc_container.c +++ b/src/lxc/lxc_container.c @@ -2159,6 +2159,37 @@ static int lxcContainerSetUserGroup(virCommandPtr cm= d, return 0; } =20 +static const char hostname_validchars[] =3D + "abcdefghijklmnopqrstuvwxyz" + "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "0123456789-"; + +static int lxcContainerSetHostname(virDomainDefPtr def) +{ + int ret =3D -1; + char *name =3D NULL; + char *hostname =3D NULL; + + /* Filter the VM name to get a valid hostname */ + if (VIR_STRDUP(name, def->name) < 0) + goto cleanup; + + /* RFC 1123 allows 0-9 digits as a first character in hostname */ + virStringFilterChars(name, hostname_validchars); + hostname =3D name; + if (strlen(name) > 0 && name[0] =3D=3D '-') + hostname =3D name + 1; + + if (sethostname(hostname, strlen(hostname)) < 0) { + virReportSystemError(errno, "%s", _("Failed to set hostname")); + goto cleanup; + } + ret =3D 0; + + cleanup: + VIR_FREE(name); + return ret; +} =20 /** * lxcContainerChild: @@ -2269,6 +2300,10 @@ static int lxcContainerChild(void *data) goto cleanup; } =20 + if (lxcContainerSetHostname(vmDef) < 0) + goto cleanup; + + /* drop a set of root capabilities */ if (lxcContainerDropCapabilities(vmDef, !!hasReboot) < 0) goto cleanup; --=20 2.15.1 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list