From nobody Mon Feb 9 04:45:49 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 170.10.129.124 as permitted sender) client-ip=170.10.129.124; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-delivery-124.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1675675036; cv=none; d=zohomail.com; s=zohoarc; b=ichq+SorIQ74rLp6ORXbDeqX9fVdhhyU0hoXihVqDozTaMWKMs1JgRYtxrSqob8wMBMmY1qBxuXJDB+9Ddi0/YbDkP+r0B2ovrvO5FRlm014yLUHrtJzc1+hzHN5ct2gPUuL9i6oFCq3QDO5c8qPqQlpDOAJfu2xFpBKk6sxzcg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1675675036; h=Content-Type:Content-Transfer-Encoding:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=lpXNVLSdhhdVBMpNlNfKvdNo6rXpPJHyq6FVWdCmWSQ=; b=OXHU5UQJFvGdkpJbS5l7QtHxMzAZjQrvJTGnq4HHvUN1iS/bsA25xPp44q7c6JyM+/JjitiVhFod53kX71ap1uY5ulS0z/VV8W9Yru4vwzOu+KM6XtOuYlB8jZ9ZJTCeM0un8+sCtAH5tgX1FhQ64mG6qrS4dAQyVcLX3Sud710= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by mx.zohomail.com with SMTPS id 1675675036652678.9366994712191; Mon, 6 Feb 2023 01:17:16 -0800 (PST) Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-619-Jiha-5wZPHWj2nLC6iseYQ-1; Mon, 06 Feb 2023 04:17:12 -0500 Received: from smtp.corp.redhat.com (int-mx10.intmail.prod.int.rdu2.redhat.com [10.11.54.10]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id DA6EF3C0CD4E; Mon, 6 Feb 2023 09:17:06 +0000 (UTC) Received: from mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (unknown [10.30.29.100]) by smtp.corp.redhat.com (Postfix) with ESMTP id BB192492B25; Mon, 6 Feb 2023 09:17:06 +0000 (UTC) Received: from mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (localhost [IPv6:::1]) by mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (Postfix) with ESMTP id A791C19465A3; Mon, 6 Feb 2023 09:17:06 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx10.intmail.prod.int.rdu2.redhat.com [10.11.54.10]) by mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (Postfix) with ESMTP id 099D0194658C for ; Mon, 6 Feb 2023 09:17:05 +0000 (UTC) Received: by smtp.corp.redhat.com (Postfix) id E0EF0492B21; Mon, 6 Feb 2023 09:17:04 +0000 (UTC) Received: from maggie.redhat.com (unknown [10.43.2.39]) by smtp.corp.redhat.com (Postfix) with ESMTP id 88AEB492B22 for ; Mon, 6 Feb 2023 09:17:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1675675035; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=lpXNVLSdhhdVBMpNlNfKvdNo6rXpPJHyq6FVWdCmWSQ=; b=bt39L1X0BhujpHlUnWOPhl++MBFa+bA/q395CZqmVSk8t4olyYPq8ZR2GcQQo/3u2YiO/X 05xlFS82fmIbTijEpgCnO0Wo8Ma2CGLjxwqXdlzwNcIs7tykPLXAFfq8GT7CpavhZCJOUD PXJBaRSsYy0xIgi9XgteuON/SRmd6BI= X-MC-Unique: Jiha-5wZPHWj2nLC6iseYQ-1 X-Original-To: libvir-list@listman.corp.redhat.com From: Michal Privoznik To: libvir-list@redhat.com Subject: [PATCH 05/10] src: Unify URI params parsing Date: Mon, 6 Feb 2023 10:16:53 +0100 Message-Id: <0463f1493435973821228b2eb23d3a02f09bc857.1675674809.git.mprivozn@redhat.com> In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.10 X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libvir-list-bounces@redhat.com Sender: "libvir-list" X-Scanned-By: MIMEDefang 3.1 on 10.11.54.10 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1675675038538100003 Content-Type: text/plain; charset="utf-8"; x-default="true" Now that we have VIR_EXTRACT_URI_ARG_* macros, we can use them in the rest of the places where an URI is parsed. This also unifies behavior wrt to query arguments handling. For instance, our virAdmConnectOpen() accepts "?socket=3D..." but not "?SOCKET=3D" whereas plain virConnectOpen() does accept both. Signed-off-by: Michal Privoznik --- src/admin/libvirt-admin.c | 15 +++--- src/esx/esx_util.c | 96 +++++++++++++++++++-------------------- src/hyperv/hyperv_util.c | 30 ++++++------ src/qemu/qemu_migration.c | 21 ++++----- src/util/virauth.c | 12 ++--- src/util/viruri.h | 12 +++++ 6 files changed, 92 insertions(+), 94 deletions(-) diff --git a/src/admin/libvirt-admin.c b/src/admin/libvirt-admin.c index 1786a283e5..0f2410d18d 100644 --- a/src/admin/libvirt-admin.c +++ b/src/admin/libvirt-admin.c @@ -109,16 +109,13 @@ getSocketPath(virURI *uri) =20 =20 for (i =3D 0; i < uri->paramsCount; i++) { - virURIParam *param =3D &uri->params[i]; + virURIParam *var =3D &uri->params[i]; =20 - if (STREQ(param->name, "socket")) { - g_free(sock_path); - sock_path =3D g_strdup(param->value); - } else { - virReportError(VIR_ERR_CONFIG_UNSUPPORTED, - _("Unknown URI parameter '%s'"), param->name); - return NULL; - } + VIR_EXTRACT_URI_ARG_STR("socket", sock_path); + + virReportError(VIR_ERR_CONFIG_UNSUPPORTED, + _("Unknown URI parameter '%s'"), var->name); + return NULL; } =20 if (!sock_path) { diff --git a/src/esx/esx_util.c b/src/esx/esx_util.c index 89d5517262..8c0f39ecc9 100644 --- a/src/esx/esx_util.c +++ b/src/esx/esx_util.c @@ -40,8 +40,8 @@ esxUtil_ParseUri(esxUtil_ParsedUri **parsedUri, virURI *u= ri) { int result =3D -1; size_t i; - int noVerify; - int autoAnswer; + int noVerify =3D -1; + int autoAnswer =3D -1; char *tmp; =20 ESX_VI_CHECK_ARG_LIST(parsedUri); @@ -49,71 +49,39 @@ esxUtil_ParseUri(esxUtil_ParsedUri **parsedUri, virURI = *uri) *parsedUri =3D g_new0(esxUtil_ParsedUri, 1); =20 for (i =3D 0; i < uri->paramsCount; i++) { - virURIParam *queryParam =3D &uri->params[i]; + virURIParam *var =3D &uri->params[i]; =20 - if (STRCASEEQ(queryParam->name, "transport")) { - g_free((*parsedUri)->transport); + VIR_EXTRACT_URI_ARG_STR("transport", (*parsedUri)->transport); + VIR_EXTRACT_URI_ARG_STR("vcenter", (*parsedUri)->vCenter); + VIR_EXTRACT_URI_ARG_INT("no_verify", noVerify, cleanup); + VIR_EXTRACT_URI_ARG_INT("auto_answer", autoAnswer, cleanup); =20 - (*parsedUri)->transport =3D g_strdup(queryParam->value); - - if (STRNEQ((*parsedUri)->transport, "http") && - STRNEQ((*parsedUri)->transport, "https")) { - virReportError(VIR_ERR_INVALID_ARG, - _("Query parameter 'transport' has unexpect= ed value " - "'%s' (should be http|https)"), - (*parsedUri)->transport); - goto cleanup; - } - } else if (STRCASEEQ(queryParam->name, "vcenter")) { - g_free((*parsedUri)->vCenter); - - (*parsedUri)->vCenter =3D g_strdup(queryParam->value); - } else if (STRCASEEQ(queryParam->name, "no_verify")) { - if (virStrToLong_i(queryParam->value, NULL, 10, &noVerify) < 0= || - (noVerify !=3D 0 && noVerify !=3D 1)) { - virReportError(VIR_ERR_INVALID_ARG, - _("Query parameter 'no_verify' has unexpect= ed value " - "'%s' (should be 0 or 1)"), queryParam->v= alue); - goto cleanup; - } - - (*parsedUri)->noVerify =3D noVerify !=3D 0; - } else if (STRCASEEQ(queryParam->name, "auto_answer")) { - if (virStrToLong_i(queryParam->value, NULL, 10, &autoAnswer) <= 0 || - (autoAnswer !=3D 0 && autoAnswer !=3D 1)) { - virReportError(VIR_ERR_INVALID_ARG, - _("Query parameter 'auto_answer' has unexpe= cted " - "value '%s' (should be 0 or 1)"), queryPa= ram->value); - goto cleanup; - } - - (*parsedUri)->autoAnswer =3D autoAnswer !=3D 0; - } else if (STRCASEEQ(queryParam->name, "proxy")) { + if (STRCASEEQ(var->name, "proxy")) { /* Expected format: [://][:] */ (*parsedUri)->proxy =3D true; (*parsedUri)->proxy_type =3D CURLPROXY_HTTP; g_clear_pointer(&(*parsedUri)->proxy_hostname, g_free); (*parsedUri)->proxy_port =3D 1080; =20 - if ((tmp =3D STRSKIP(queryParam->value, "http://"))) { + if ((tmp =3D STRSKIP(var->value, "http://"))) { (*parsedUri)->proxy_type =3D CURLPROXY_HTTP; - } else if ((tmp =3D STRSKIP(queryParam->value, "socks://")) || - (tmp =3D STRSKIP(queryParam->value, "socks5://"))) { + } else if ((tmp =3D STRSKIP(var->value, "socks://")) || + (tmp =3D STRSKIP(var->value, "socks5://"))) { (*parsedUri)->proxy_type =3D CURLPROXY_SOCKS5; - } else if ((tmp =3D STRSKIP(queryParam->value, "socks4://"))) { + } else if ((tmp =3D STRSKIP(var->value, "socks4://"))) { (*parsedUri)->proxy_type =3D CURLPROXY_SOCKS4; - } else if ((tmp =3D STRSKIP(queryParam->value, "socks4a://")))= { + } else if ((tmp =3D STRSKIP(var->value, "socks4a://"))) { (*parsedUri)->proxy_type =3D CURLPROXY_SOCKS4A; - } else if ((tmp =3D strstr(queryParam->value, "://"))) { + } else if ((tmp =3D strstr(var->value, "://"))) { *tmp =3D '\0'; =20 virReportError(VIR_ERR_INVALID_ARG, _("Query parameter 'proxy' contains unexpec= ted " "type '%s' (should be (http|socks(|4|4a|5= ))"), - queryParam->value); + var->value); goto cleanup; } else { - tmp =3D queryParam->value; + tmp =3D var->value; } =20 (*parsedUri)->proxy_hostname =3D g_strdup(tmp); @@ -141,7 +109,7 @@ esxUtil_ParseUri(esxUtil_ParsedUri **parsedUri, virURI = *uri) } } else { VIR_WARN("Ignoring unexpected query parameter '%s'", - queryParam->name); + var->name); } } =20 @@ -150,6 +118,36 @@ esxUtil_ParseUri(esxUtil_ParsedUri **parsedUri, virURI= *uri) if (!(*parsedUri)->transport) (*parsedUri)->transport =3D g_strdup("https"); =20 + if (STRNEQ((*parsedUri)->transport, "http") && + STRNEQ((*parsedUri)->transport, "https")) { + virReportError(VIR_ERR_INVALID_ARG, + _("Query parameter 'transport' has unexpected value= '%s' (should be http|https)"), + (*parsedUri)->transport); + goto cleanup; + } + + if (noVerify >=3D 0) { + if (noVerify !=3D 0 && noVerify !=3D 1) { + virReportError(VIR_ERR_INVALID_ARG, + _("Query parameter 'no_verify' has unexpected v= alue '%d' (should be 0 or 1)"), + noVerify); + goto cleanup; + } + + (*parsedUri)->noVerify =3D noVerify !=3D 0; + } + + if (autoAnswer >=3D 0) { + if (autoAnswer !=3D 0 && autoAnswer !=3D 1) { + virReportError(VIR_ERR_INVALID_ARG, + _("Query parameter 'auto_answer' has unexpected= value '%d' (should be 0 or 1)"), + autoAnswer); + goto cleanup; + } + + (*parsedUri)->autoAnswer =3D autoAnswer !=3D 0; + } + result =3D 0; =20 cleanup: diff --git a/src/hyperv/hyperv_util.c b/src/hyperv/hyperv_util.c index fe71e47285..38daa27743 100644 --- a/src/hyperv/hyperv_util.c +++ b/src/hyperv/hyperv_util.c @@ -45,30 +45,26 @@ hypervParseUri(hypervParsedUri **parsedUri, virURI *uri) *parsedUri =3D g_new0(hypervParsedUri, 1); =20 for (i =3D 0; i < uri->paramsCount; i++) { - virURIParam *queryParam =3D &uri->params[i]; + virURIParam *var =3D &uri->params[i]; =20 - if (STRCASEEQ(queryParam->name, "transport")) { - VIR_FREE((*parsedUri)->transport); + VIR_EXTRACT_URI_ARG_STR("transport", (*parsedUri)->transport); =20 - (*parsedUri)->transport =3D g_strdup(queryParam->value); - - if (STRNEQ((*parsedUri)->transport, "http") && - STRNEQ((*parsedUri)->transport, "https")) { - virReportError(VIR_ERR_INVALID_ARG, - _("Query parameter 'transport' has unexpect= ed value " - "'%s' (should be http|https)"), - (*parsedUri)->transport); - goto cleanup; - } - } else { - VIR_WARN("Ignoring unexpected query parameter '%s'", - queryParam->name); - } + VIR_WARN("Ignoring unexpected query parameter '%s'", + var->name); } =20 if (!(*parsedUri)->transport) (*parsedUri)->transport =3D g_strdup("https"); =20 + if (STRNEQ((*parsedUri)->transport, "http") && + STRNEQ((*parsedUri)->transport, "https")) { + virReportError(VIR_ERR_INVALID_ARG, + _("Query parameter 'transport' has unexpected value= " + "'%s' (should be http|https)"), + (*parsedUri)->transport); + goto cleanup; + } + result =3D 0; =20 cleanup: diff --git a/src/qemu/qemu_migration.c b/src/qemu/qemu_migration.c index 2720f0b083..3c38be46f7 100644 --- a/src/qemu/qemu_migration.c +++ b/src/qemu/qemu_migration.c @@ -2080,7 +2080,7 @@ qemuMigrationSrcGraphicsRelocate(virDomainObj *vm, int type =3D -1; int port =3D -1; int tlsPort =3D -1; - const char *tlsSubject =3D NULL; + g_autofree char *tlsSubject =3D NULL; int rc =3D -1; =20 if (!cookie || (!cookie->graphics && !graphicsuri)) @@ -2101,7 +2101,7 @@ qemuMigrationSrcGraphicsRelocate(virDomainObj *vm, =20 port =3D cookie->graphics->port; tlsPort =3D cookie->graphics->tlsPort; - tlsSubject =3D cookie->graphics->tlsSubject; + tlsSubject =3D g_strdup(cookie->graphics->tlsSubject); } =20 if (uri) { @@ -2119,18 +2119,10 @@ qemuMigrationSrcGraphicsRelocate(virDomainObj *vm, port =3D uri->port; =20 for (i =3D 0; i < uri->paramsCount; i++) { - virURIParam *param =3D uri->params + i; + virURIParam *var =3D &uri->params[i]; =20 - if (STRCASEEQ(param->name, "tlsPort")) { - if (virStrToLong_i(param->value, NULL, 10, &tlsPort) < 0) { - virReportError(VIR_ERR_INVALID_ARG, - _("invalid tlsPort number: %s"), - param->value); - return -1; - } - } else if (STRCASEEQ(param->name, "tlsSubject")) { - tlsSubject =3D param->value; - } + VIR_EXTRACT_URI_ARG_INT("tlsPort", tlsPort, error); + VIR_EXTRACT_URI_ARG_STR("tlsSubject", tlsSubject); } } =20 @@ -2157,6 +2149,9 @@ qemuMigrationSrcGraphicsRelocate(virDomainObj *vm, } =20 return rc; + + error: + return -1; } =20 =20 diff --git a/src/util/virauth.c b/src/util/virauth.c index 7b4a1bd8a5..afebb609a7 100644 --- a/src/util/virauth.c +++ b/src/util/virauth.c @@ -57,13 +57,13 @@ virAuthGetConfigFilePathURI(virURI *uri, =20 if (uri) { for (i =3D 0; i < uri->paramsCount; i++) { - if (STREQ_NULLABLE(uri->params[i].name, "authfile") && - uri->params[i].value) { - VIR_DEBUG("Using path from URI '%s'", uri->params[i].value= ); - *path =3D g_strdup(uri->params[i].value); - return 0; - } + virURIParam *var =3D &uri->params[i]; + + VIR_EXTRACT_URI_ARG_STR("authfile", *path); } + + if (*path) + return 0; } =20 userdir =3D virGetUserConfigDirectory(); diff --git a/src/util/viruri.h b/src/util/viruri.h index 0e4176c037..7e4f95a2b1 100644 --- a/src/util/viruri.h +++ b/src/util/viruri.h @@ -72,6 +72,18 @@ bool virURICheckUnixSocket(virURI *uri); continue; \ } =20 +#define VIR_EXTRACT_URI_ARG_INT(ARG_NAME, ARG_VAR, LABEL) \ + if (STRCASEEQ(var->name, ARG_NAME)) { \ + if (virStrToLong_i(var->value, NULL, 10, &(ARG_VAR)) < 0) { \ + virReportError(VIR_ERR_INVALID_ARG, \ + _("Failed to parse value of URI component %s"),= \ + var->name); \ + goto LABEL; \ + } \ + var->ignore =3D 1; \ + continue; \ + } + #define VIR_EXTRACT_URI_ARG_BOOL(ARG_NAME, ARG_VAR, LABEL) \ if (STRCASEEQ(var->name, ARG_NAME)) { \ int tmp; \ --=20 2.39.1