diff --git a/client/config.c b/client/config.c index 05043682..eff71c63 100644 --- a/client/config.c +++ b/client/config.c @@ -13,6 +13,9 @@ #include "../llconf/entry.h" #include "../llconf/ini.h" +#define USERAGENTHEADERMAXLENGTH 50 +#define OSCONFFILE "/etc/os-release" + int TDNFConfGetRpmVerbosity( PTDNF pTdnf @@ -26,6 +29,49 @@ TDNFConfGetRpmVerbosity( return nLogLevel; } +static uint32_t TDNFParseOSInfo(PTDNF_CONF pConf) +{ + char temp[USERAGENTHEADERMAXLENGTH]; + char name[10]; + char version[10]; + int name_found = 0; + uint32_t dwError = 0; + FILE *file = NULL; + + file = fopen(OSCONFFILE, "r"); + + if (!file) { + dwError = errno; + BAIL_ON_TDNF_SYSTEM_ERROR(dwError); + } + + while (fgets(temp, USERAGENTHEADERMAXLENGTH, file)) { + if (strstr(temp, "ID=") != NULL && name_found == 0) { + sscanf(temp, "ID=%s\n", name); + name_found = 1; + } else if (strstr(temp, "VERSION_ID=") != NULL) { + sscanf(temp, "VERSION_ID=%s\n", version); + } + } + + dwError = TDNFAllocateString(name, &pConf->pszOSName); + BAIL_ON_TDNF_ERROR(dwError); + + dwError = TDNFAllocateString(version, &pConf->pszOSVersion); + BAIL_ON_TDNF_ERROR(dwError); + +cleanup: + if(file) + { + fclose(file); + } + return dwError; + +error: + goto cleanup; + +} + uint32_t TDNFReadConfig( PTDNF pTdnf, @@ -40,6 +86,7 @@ TDNFReadConfig( char *pszPkgLocksDir = NULL; char *pszProtectedDir = NULL; + const char *pszTdnfVersion = NULL; const char *pszProxyUser = NULL; const char *pszProxyPass = NULL; @@ -91,6 +138,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; @@ -192,6 +242,17 @@ TDNFReadConfig( } } + pszTdnfVersion = TDNFGetVersion(); + + if (pConf->pszOSName == NULL) + TDNFAllocateString("UNKNOWN", &pConf->pszOSName); + + if (pConf->pszOSVersion == NULL) + TDNFAllocateString("UNKNOWN", &pConf->pszOSVersion); + + dwError = TDNFAllocateStringPrintf(&pConf->pszUserAgentHeader, "User-Agent: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 +387,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/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;