Skip to content

Commit

Permalink
Set a user agent for http/https tdnf requests
Browse files Browse the repository at this point in the history
  • Loading branch information
shivania2 committed Oct 16, 2023
1 parent 847f535 commit d9dc867
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 0 deletions.
70 changes: 70 additions & 0 deletions client/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
#include "../llconf/entry.h"
#include "../llconf/ini.h"

#define OS_CONF_FILE "/etc/os-release"

int
TDNFConfGetRpmVerbosity(
PTDNF pTdnf
Expand All @@ -26,6 +28,56 @@ TDNFConfGetRpmVerbosity(
return nLogLevel;
}

static uint32_t TDNFParseOSInfo(PTDNF_CONF pConf)
{
char *buf;
char *name = NULL;
char *version = NULL;
uint32_t dwError = 0;
FILE *file = NULL;
size_t buf_len = 0;
ssize_t bytes_read;

file = fopen(OS_CONF_FILE, "r");

if (!file) {
pr_info("Warning: %s file is not present in the system\n", OS_CONF_FILE);
return 0;
}

while ((bytes_read = getline(&buf, &buf_len, file)) != -1)
{
if (strncmp("ID=", buf, sizeof("ID=")-1) == 0)
{
sscanf(buf, "ID=%s\n", buf);
dwError = TDNFAllocateString(buf, &name);
BAIL_ON_TDNF_ERROR(dwError);
}
else if (strncmp("VERSION_ID=", buf, sizeof("VERSION_ID=")-1) == 0)
{
sscanf(buf, "VERSION_ID=%s\n", buf);
dwError = TDNFAllocateString(buf, &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,
Expand All @@ -40,6 +92,7 @@ TDNFReadConfig(
char *pszPkgLocksDir = NULL;
char *pszProtectedDir = NULL;

const char *pszTdnfVersion = NULL;
const char *pszProxyUser = NULL;
const char *pszProxyPass = NULL;

Expand Down Expand Up @@ -91,6 +144,9 @@ TDNFReadConfig(
}
}

dwError = TDNFParseOSInfo(pConf);
BAIL_ON_TDNF_ERROR(dwError);

/* cn_conf == NULL => we will not reach here */
/* coverity[var_deref_op] */
cn_top = cn_conf->first_child;
Expand Down Expand Up @@ -192,6 +248,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) {
Expand Down Expand Up @@ -326,6 +393,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);
Expand Down
9 changes: 9 additions & 0 deletions client/remoterepo.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
3 changes: 3 additions & 0 deletions include/tdnftypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit d9dc867

Please sign in to comment.