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 12d3d44
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 0 deletions.
67 changes: 67 additions & 0 deletions client/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) {

This comment has been minimized.

Copy link
@sshedi

sshedi Oct 16, 2023

Contributor

fix the indentation

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,
Expand All @@ -40,6 +87,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 +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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
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 12d3d44

Please sign in to comment.