From 938b9f453f510169bd46eb14a9e952e436c73a5f Mon Sep 17 00:00:00 2001 From: Jaroslav Rohel Date: Tue, 25 Jun 2024 08:31:07 +0200 Subject: [PATCH] Support colon in username, use LRO_USERNAME and LRO_PASSWORD Requires librepo version >= 1.18.0 --- CMakeLists.txt | 2 +- libdnf.spec | 2 +- libdnf/dnf-repo.cpp | 27 ++++++++++----------------- libdnf/repo/Repo.cpp | 22 +++++++--------------- 4 files changed, 19 insertions(+), 34 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 6444c3740..c6bd46a6d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -61,7 +61,7 @@ include_directories(${GLIB_INCLUDE_DIRS}) pkg_check_modules(JSONC REQUIRED json-c) include_directories(${JSONC_INCLUDE_DIRS}) pkg_check_modules(LIBMODULEMD REQUIRED modulemd-2.0>=2.11.2) -pkg_check_modules(REPO REQUIRED librepo>=1.15.0) +pkg_check_modules(REPO REQUIRED librepo>=1.18.0) include_directories(${REPO_INCLUDE_DIRS}) link_directories(${REPO_LIBRARY_DIRS}) pkg_check_modules(RPM REQUIRED rpm>=4.15.0) diff --git a/libdnf.spec b/libdnf.spec index fafc55f1a..896ab3908 100644 --- a/libdnf.spec +++ b/libdnf.spec @@ -1,6 +1,6 @@ %global libsolv_version 0.7.21 %global libmodulemd_version 2.13.0 -%global librepo_version 1.15.0 +%global librepo_version 1.18.0 %global dnf_conflict 4.11.0 %global swig_version 3.0.12 %global libdnf_major_version 0 diff --git a/libdnf/dnf-repo.cpp b/libdnf/dnf-repo.cpp index ca3d1920a..db460a11b 100644 --- a/libdnf/dnf-repo.cpp +++ b/libdnf/dnf-repo.cpp @@ -817,20 +817,16 @@ dnf_repo_set_metadata_expire(DnfRepo *repo, guint metadata_expire) /** * @brief Format user password string * -* Returns user and password in user:password form. If encode is True, -* special characters in user and password are URL encoded. +* Returns user and password in user:password form. +* Special characters in user and password are URL encoded. * * @param user Username * @param passwd Password -* @param encode If quote is True, special characters in user and password are URL encoded. * @return User and password in user:password form */ -static std::string formatUserPassString(const std::string & user, const std::string & passwd, bool encode) +static std::string formatUserPassString(const std::string & user, const std::string & passwd) { - if (encode) - return libdnf::urlEncode(user) + ":" + libdnf::urlEncode(passwd); - else - return user + ":" + passwd; + return libdnf::urlEncode(user) + ":" + libdnf::urlEncode(passwd); } /* Resets repository configuration options previously readed from repository @@ -1130,7 +1126,7 @@ dnf_repo_set_keyfile_data(DnfRepo *repo, gboolean reloadFromGKeyFile, GError **e "repo '%s': 'proxy_username' is set but not 'proxy_password'", repoId); return FALSE; } - tmp_str = formatUserPassString(tmp_str, conf->proxy_password().getValue(), true); + tmp_str = formatUserPassString(tmp_str, conf->proxy_password().getValue()); tmp_cstr = tmp_str.c_str(); } } @@ -1138,14 +1134,11 @@ dnf_repo_set_keyfile_data(DnfRepo *repo, gboolean reloadFromGKeyFile, GError **e return FALSE; // setup username and password - tmp_cstr = NULL; - tmp_str = conf->username().getValue(); - if (!tmp_str.empty()) { - // TODO Use URL encoded form, needs support in librepo - tmp_str = formatUserPassString(tmp_str, conf->password().getValue(), false); - tmp_cstr = tmp_str.c_str(); - } - if (!lr_handle_setopt(priv->repo_handle, error, LRO_USERPWD, tmp_cstr)) + auto & username = conf->username().getValue(); + if (!lr_handle_setopt(priv->repo_handle, error, LRO_USERNAME, username.empty() ? NULL : username.c_str())) + return FALSE; + auto & password = conf->password().getValue(); + if (!lr_handle_setopt(priv->repo_handle, error, LRO_PASSWORD, password.empty() ? NULL : password.c_str())) return FALSE; auto proxy_sslverify = conf->proxy_sslverify().getValue(); diff --git a/libdnf/repo/Repo.cpp b/libdnf/repo/Repo.cpp index ead986189..f020a5380 100644 --- a/libdnf/repo/Repo.cpp +++ b/libdnf/repo/Repo.cpp @@ -252,20 +252,16 @@ int Repo::Impl::mirrorFailureCB(void * data, const char * msg, const char * url, /** * @brief Format user password string * -* Returns user and password in user:password form. If quote is True, -* special characters in user and password are URL encoded. +* Returns user and password in user:password form. +* Special characters in user and password are URL encoded. * * @param user Username * @param passwd Password -* @param encode If quote is True, special characters in user and password are URL encoded. * @return User and password in user:password form */ -static std::string formatUserPassString(const std::string & user, const std::string & passwd, bool encode) +static std::string formatUserPassString(const std::string & user, const std::string & passwd) { - if (encode) - return urlEncode(user) + ":" + urlEncode(passwd); - else - return user + ":" + passwd; + return urlEncode(user) + ":" + urlEncode(passwd); } Repo::Impl::Impl(Repo & owner, const std::string & id, Type type, std::unique_ptr && conf) @@ -524,12 +520,8 @@ static void setHandle(LrHandle * h, ConfigT & config, const char * repoId = null } // setup username/password if needed - auto userpwd = config.username().getValue(); - if (!userpwd.empty()) { - // TODO Use URL encoded form, needs support in librepo - userpwd = formatUserPassString(userpwd, config.password().getValue(), false); - handleSetOpt(h, LRO_USERPWD, userpwd.c_str()); - } + handleSetOpt(h, LRO_USERNAME, config.username().getValue().empty() ? NULL : config.username().getValue().c_str()); + handleSetOpt(h, LRO_PASSWORD, config.password().getValue().empty() ? NULL : config.password().getValue().c_str()); if (!config.proxy().empty() && !config.proxy().getValue().empty()) handleSetOpt(h, LRO_PROXY, config.proxy().getValue().c_str()); @@ -548,7 +540,7 @@ static void setHandle(LrHandle * h, ConfigT & config, const char * repoId = null else throw RepoError(_("'proxy_username' is set but not 'proxy_password'")); } - userpwd = formatUserPassString(userpwd, config.proxy_password().getValue(), true); + userpwd = formatUserPassString(userpwd, config.proxy_password().getValue()); handleSetOpt(h, LRO_PROXYUSERPWD, userpwd.c_str()); } }