From 12d3d44f3e2b407cd2ee2083771f0ddce05e0da7 Mon Sep 17 00:00:00 2001 From: Shivani Agarwal Date: Mon, 9 Oct 2023 10:13:05 +0000 Subject: [PATCH] Set a user agent for http/https tdnf requests --- client/config.c | 67 +++++++++++++++++++++++++++++++++++++++++++++ client/remoterepo.c | 9 ++++++ include/tdnftypes.h | 3 ++ 3 files changed, 79 insertions(+) diff --git a/client/config.c b/client/config.c index 05043682..412c568f 100644 --- a/client/config.c +++ b/client/config.c @@ -13,6 +13,9 @@ #include "../llconf/entry.h" #include "../llconf/ini.h" +#define USERAGENT_HEADER_MAX_LENGTH 50 +#define OS_CONF_FILE "/etc/os-release" + int TDNFConfGetRpmVerbosity( PTDNF pTdnf @@ -26,6 +29,50 @@ TDNFConfGetRpmVerbosity( return nLogLevel; } +static uint32_t TDNFParseOSInfo(PTDNF_CONF pConf) +{ + char temp[USERAGENT_HEADER_MAX_LENGTH]; + char *name = NULL; + char *version = NULL; + uint32_t dwError = 0; + FILE *file = NULL; + + file = fopen(OS_CONF_FILE, "r"); + + if (!file) { + dwError = errno; + BAIL_ON_TDNF_SYSTEM_ERROR(dwError); + } + + while (fgets(temp, USERAGENT_HEADER_MAX_LENGTH, file)) { + if (strncmp("ID=", temp, sizeof("ID=")-1) == 0) { + sscanf(temp, "ID=%s\n", temp); + dwError = TDNFAllocateString(temp, &name); + BAIL_ON_TDNF_ERROR(dwError); + } else if (strncmp("VERSION_ID=", temp, sizeof("VERSION_ID=")-1) == 0) { + sscanf(temp, "VERSION_ID=%s\n", temp); + dwError = TDNFAllocateString(temp, &version); + BAIL_ON_TDNF_ERROR(dwError); + } + } + + pConf->pszOSName = name; + pConf->pszOSVersion = version; + +cleanup: + if(file) + { + fclose(file); + } + return dwError; + +error: + TDNF_SAFE_FREE_MEMORY(name); + TDNF_SAFE_FREE_MEMORY(version); + goto cleanup; + +} + uint32_t TDNFReadConfig( PTDNF pTdnf, @@ -40,6 +87,7 @@ TDNFReadConfig( char *pszPkgLocksDir = NULL; char *pszProtectedDir = NULL; + const char *pszTdnfVersion = NULL; const char *pszProxyUser = NULL; const char *pszProxyPass = NULL; @@ -91,6 +139,11 @@ TDNFReadConfig( } } + dwError = TDNFParseOSInfo(pConf); + if (dwError != ENOENT) { + BAIL_ON_TDNF_ERROR(dwError); + } + /* cn_conf == NULL => we will not reach here */ /* coverity[var_deref_op] */ cn_top = cn_conf->first_child; @@ -192,6 +245,17 @@ TDNFReadConfig( } } + pszTdnfVersion = TDNFGetVersion(); + + if (pConf->pszOSName == NULL) + TDNFAllocateString("UNKNOWN", &pConf->pszOSName); + + if (pConf->pszOSVersion == NULL) + TDNFAllocateString("UNKNOWN", &pConf->pszOSVersion); + + dwError = TDNFAllocateStringPrintf(&pConf->pszUserAgentHeader, "tdnf/%s %s/%s", pszTdnfVersion, pConf->pszOSName, pConf->pszOSVersion); + BAIL_ON_TDNF_ERROR(dwError); + /* if plugins are not enabled explicitely, we have to disable them because it's the default */ if (!nPluginSet) { @@ -326,6 +390,9 @@ TDNFFreeConfig( TDNF_SAFE_FREE_MEMORY(pConf->pszVarReleaseVer); TDNF_SAFE_FREE_MEMORY(pConf->pszVarBaseArch); TDNF_SAFE_FREE_MEMORY(pConf->pszBaseArch); + TDNF_SAFE_FREE_MEMORY(pConf->pszUserAgentHeader); + TDNF_SAFE_FREE_MEMORY(pConf->pszOSName); + TDNF_SAFE_FREE_MEMORY(pConf->pszOSVersion); TDNF_SAFE_FREE_STRINGARRAY(pConf->ppszExcludes); TDNF_SAFE_FREE_STRINGARRAY(pConf->ppszMinVersions); TDNF_SAFE_FREE_STRINGARRAY(pConf->ppszPkgLocks); diff --git a/client/remoterepo.c b/client/remoterepo.c index fad15b5b..034cb4ab 100644 --- a/client/remoterepo.c +++ b/client/remoterepo.c @@ -194,6 +194,15 @@ TDNFDownloadFile( BAIL_ON_TDNF_ERROR(dwError); } + if(!IsNullOrEmptyString(pTdnf->pConf->pszUserAgentHeader)) + { + dwError = curl_easy_setopt( + pCurl, + CURLOPT_USERAGENT, + pTdnf->pConf->pszUserAgentHeader); + BAIL_ON_TDNF_ERROR(dwError); + } + dwError = TDNFRepoApplyProxySettings(pTdnf->pConf, pCurl); BAIL_ON_TDNF_ERROR(dwError); diff --git a/include/tdnftypes.h b/include/tdnftypes.h index 74124771..ef33d1b0 100644 --- a/include/tdnftypes.h +++ b/include/tdnftypes.h @@ -265,6 +265,9 @@ typedef struct _TDNF_CONF char* pszBaseArch; char* pszVarReleaseVer; char* pszVarBaseArch; + char* pszUserAgentHeader; + char* pszOSName; + char* pszOSVersion; char** ppszExcludes; char** ppszMinVersions; char** ppszPkgLocks;