Пример #1
0
const bool	OTSettings::CheckSet_bool(const OTString & strSection, const OTString & strKey, const bool	   & bDefault,	 bool	  & out_bResult,   bool & out_bIsNew, const OTString & strComment)
{
	if (! strSection.Exists())			{ OTLog::vError("%s: Error: %s is Empty!\n", __FUNCTION__, "strSection"			); OT_ASSERT(false); }
	if (! strKey.Exists())				{ OTLog::vError("%s: Error: %s is Empty!\n", __FUNCTION__, "strKey"				); OT_ASSERT(false); }

	bool bKeyExist, bNewKeyCheck, bTempResult;
	if (! Check_bool(strSection,strKey,bTempResult,bKeyExist)) return false;

	if (bKeyExist)
	{
		// Already have a key, lets use it's value.
		out_bIsNew = false;
		out_bResult = bTempResult;
		return true;
	}
	else
	{
		if (! Set_bool(strSection,strKey,bDefault,bNewKeyCheck,strComment)) return false;
		if (bNewKeyCheck)
		{
			// Success
			out_bIsNew = true;
			out_bResult = bDefault;
			return true;
		}
	}

	// If we get here, error!
	OT_ASSERT(false);
	return false;
}
Пример #2
0
bool OTASCIIArmor::WriteArmoredFile(const OTString & foldername, const OTString & filename,
                                    const // for "-----BEGIN OT LEDGER-----", str_type would contain "LEDGER"
                                          std::string str_type, // There's no default, to force you to enter the right string.
                                    bool  bEscaped/*=false*/)
{    
    OT_ASSERT(foldername.Exists());
    OT_ASSERT(filename.Exists());
    // ----------------------------------
    const char * szFunc = "OTASCIIArmor::WriteArmoredFile";
    // ----------------------------------
    OTString strOutput;
    
    if (this->WriteArmoredString(strOutput, str_type, bEscaped) && strOutput.Exists())
    {
        // WRITE IT TO THE FILE
        // StorePlainString will attempt to create all the folders leading up to the path
        // for the output file.
        //
        bool bSaved = OTDB::StorePlainString(strOutput.Get(), foldername.Get(), filename.Get());
        
        if (!bSaved)
        {
            OTLog::vError("%s: Failed saving to file: %s%s%s\n\n Contents:\n\n%s\n\n", szFunc,
                          foldername.Get(), OTLog::PathSeparator(), filename.Get(), strOutput.Get());
            return false;
        }        
        // --------------------------------------------------------------------        
        return true;
    }
    // -----------------------
    return false;
}
Пример #3
0
const bool	OTSettings::Set_bool(const OTString & strSection, const OTString & strKey, const bool	  & bValue,   bool & out_bNewOrUpdate, const OTString & strComment)
{
	if (! strSection.Exists())			{ OTLog::vError("%s: Error: %s is Empty!\n", __FUNCTION__, "strSection"			); OT_ASSERT(false); }
	if (! strKey.Exists())				{ OTLog::vError("%s: Error: %s is Empty!\n", __FUNCTION__, "strKey"				); OT_ASSERT(false); }
	const OTString strValue(bValue ? "true" : "false");

	return Set_str(strSection,strKey,strValue,out_bNewOrUpdate,strComment);
}
Пример #4
0
const bool OTPaths::Set(
	OTSettings * pConfig,
	const				  OTString	  & strSection,
	const				  OTString	  & strKey,
	const				  OTString	  & strValue,
	const				  bool		  & bIsRelative,
	bool		& out_bIsNewOrUpdated,
	const				  OTString	  & strComment
	)
{
	if (!strSection.Exists())		{ OTLog::sError("%s: Null: %s passed in!\n", __FUNCTION__, "strSection"	); OT_ASSERT(false); }
	if (!strKey.Exists())			{ OTLog::sError("%s: Null: %s passed in!\n", __FUNCTION__, "strKey"		); OT_ASSERT(false); }

	out_bIsNewOrUpdated = false;

	if (NULL == pConfig)  { OT_ASSERT(false); return false; }

	const bool bPreLoaded(pConfig->IsLoaded());

	if (!bPreLoaded)  // we only need to load, if not already loaded.
	{
		pConfig->Reset();
		if(!pConfig->Load()) { OT_ASSERT(false); return false; }
	}

	bool bBoolIsNew(false), bStringIsNew(false);
	OTString  strRelativeKey("");

	strRelativeKey.Format("%s%s",strKey.Get(),OT_CONFIG_ISRELATIVE);

	if(pConfig->Set_bool(strSection,strRelativeKey,bIsRelative,bBoolIsNew,strComment))
	{
		if(pConfig->Set_str(strSection,strKey,strValue,bStringIsNew))
		{
			if(bBoolIsNew && bStringIsNew) //using existing key
			{
				out_bIsNewOrUpdated = true;
			}

			if (!bPreLoaded)
			{
				if(!pConfig->Save()) { OT_ASSERT(false); return false; }
				pConfig->Reset();
			}

			return true;
		}
	}

	// if we get here, there has been a error!
	OT_ASSERT(false);
	pConfig->Reset();
	return false;
}
Пример #5
0
const bool	OTSettings::LogChange_bool(const OTString & strSection,const OTString & strKey,const bool	  & bValue	)
{
	if (! strSection.Exists())			{ OTLog::vError("%s: Error: %s is Empty!\n", __FUNCTION__, "strSection"			); OT_ASSERT(false); }
	if (! strKey.Exists())				{ OTLog::vError("%s: Error: %s is Empty!\n", __FUNCTION__, "strKey"				); OT_ASSERT(false); }

	OTString strCategory, strOption;
	if (!OTLog::StringFill(strCategory,strSection.Get(),12)) return false;
	if (!OTLog::StringFill(strOption,strKey.Get(),30," to:")) return false;

	OTLog::vOutput(1, "Setting %s %s %s \n",strCategory.Get(),strOption.Get(),bValue ? "true" : "false");
	return true;
}
Пример #6
0
const bool	OTSettings::Set_long(const OTString & strSection, const OTString & strKey, const long	  & lValue,   bool & out_bNewOrUpdate, const OTString & strComment)
{
	if (! strSection.Exists())		{ OTLog::vError("%s: Error: %s is Empty!\n", __FUNCTION__, "strSection"			); OT_ASSERT(false); }
	if (strSection.Compare(""))		{ OTLog::vError("%s: Error: %s is Blank!\n", __FUNCTION__, "strSection"			); OT_ASSERT(false); }

	if (! strKey.Exists())			{ OTLog::vError("%s: Error: %s is Empty!\n", __FUNCTION__, "strKey"				); OT_ASSERT(false); }
	if (strKey.Compare(""))			{ OTLog::vError("%s: Error: %s is Blank!\n", __FUNCTION__, "strKey"				); OT_ASSERT(false); }

	OTString strValue; strValue.Format("%ld",lValue);

	const char * const szComment = (strComment.Exists() && !strComment.Compare("")) ? strComment.Get() : NULL;
	
	OTString strOldValue, strNewValue;
	bool bOldKeyExist, bNewKeyExist;

	// Check if Old Key exists.
	if(! Check_str(strSection,strKey,strOldValue,bOldKeyExist)) return false;

	if (bOldKeyExist)
	{
		if (strValue.Compare(strOldValue))
		{
			out_bNewOrUpdate = false;
			return true;
		}
	}

	// Log to Output Setting Change
	if (! LogChange_str(strSection,strKey,strValue)) return false;

	// Set New Value
	SI_Error rc = p_iniSimple -> SetLongValue(strSection.Get(),strKey.Get(),lValue,szComment,false,true);
	if (0 > rc) return false;

	// Check if the new value is the same as intended.
	if (! Check_str(strSection,strKey,strNewValue,bNewKeyExist)) return false;

	if (bNewKeyExist)
	{
		if (strValue.Compare(strNewValue))
		{
			// Success
			out_bNewOrUpdate = true;
			return true;
		}
	}

	// If we get here, error!
	OT_ASSERT(false);
	return false;
}
Пример #7
0
const bool	OTSettings::LogChange_str (const OTString & strSection,const OTString & strKey,const OTString & strValue)
{
	if (! strSection.Exists())			{ OTLog::vError("%s: Error: %s is Empty!\n", __FUNCTION__, "strSection"			); OT_ASSERT(false); }
	if (! strKey.Exists())				{ OTLog::vError("%s: Error: %s is Empty!\n", __FUNCTION__, "strKey"				); OT_ASSERT(false); }

	const char * const szValue	 = (strValue.Exists()	&& !strValue.Compare("")  ) ? strValue.Get()   : "NULL";

	OTString strCategory, strOption;
	if (!OTLog::StringFill(strCategory,strSection.Get(),12)) return false;
	if (!OTLog::StringFill(strOption,strKey.Get(),30," to:")) return false;

	OTLog::vOutput(1, "Setting %s %s %s \n",strCategory.Get(),strOption.Get(),szValue);
	return true;
}
Пример #8
0
// static
bool OTKeyring::Gnome_StoreSecret(const OTString& strUser,
                                  const OTPassword& thePassword,
                                  const std::string& str_display)
{
    OT_ASSERT(strUser.Exists());
    OT_ASSERT(thePassword.getMemorySize() > 0);

    OTData theData(thePassword.getMemory(), thePassword.getMemorySize());
    OTASCIIArmor ascData(theData);
    theData.zeroMemory(); // security reasons.

    OTString strOutput;
    const bool bSuccess =
        ascData.Exists() &&
        ascData.WriteArmoredString(strOutput, "DERIVED KEY"); // There's no
                                                              // default, to
                                                              // force you to
                                                              // enter the right
                                                              // string.
    ascData.zeroMemory();

    GnomeKeyringResult theResult = GNOME_KEYRING_RESULT_IO_ERROR;

    if (bSuccess && strOutput.Exists()) {
        theResult = gnome_keyring_store_password_sync(
            GNOME_KEYRING_NETWORK_PASSWORD,
            GNOME_KEYRING_DEFAULT, // GNOME_KEYRING_SESSION,
            str_display.c_str(), strOutput.Get(), "user", strUser.Get(),
            "protocol", "opentxs", // todo: hardcoding.
            nullptr);
        strOutput.zeroMemory();

        bool bResult = false;

        if (theResult == GNOME_KEYRING_RESULT_OK)
            bResult = true;
        else
            otErr << "OTKeyring::Gnome_StoreSecret: "
                  << "Failure in gnome_keyring_store_password_sync: "
                  << gnome_keyring_result_to_message(theResult) << '\n';

        return bResult;
    }

    otOut << "OTKeyring::Gnome_StoreSecret: No secret to store.\n";

    return false;
}
Пример #9
0
// static
bool OTKeyring::Mac_StoreSecret(const OTString& strUser,
                                const OTPassword& thePassword,
                                const std::string& str_display)
{
    OT_ASSERT(strUser.Exists());
    OT_ASSERT(thePassword.getMemorySize() > 0);

    const std::string service_name = "opentxs";
    const std::string account_name = strUser.Get();

    OTMacKeychain theKeychain;
    void* vData =
        const_cast<void*>(static_cast<const void*>(thePassword.getMemory()));

    OSStatus theError = theKeychain.AddSecret(
        nullptr, service_name.size(), service_name.data(), account_name.size(),
        account_name.data(), thePassword.getMemorySize(),
        vData, // thePassword.getMemory()
        nullptr);
    if (theError != noErr) {
        otErr
            << "OTKeyring::Mac_StoreSecret: Error in theKeychain.AddSecret.\n";
        return false;
    }

    return true;
}
Пример #10
0
// static
bool OTKeyring::Mac_RetrieveSecret(const OTString& strUser,
                                   OTPassword& thePassword,
                                   const std::string& str_display)
{
    OT_ASSERT(strUser.Exists());

    const std::string service_name = "opentxs";
    const std::string account_name = strUser.Get();

    uint32_t password_length = 0;
    void* password_data = nullptr;

    OTMacKeychain theKeychain;

    OSStatus theError = theKeychain.FindSecret(
        nullptr, service_name.size(), service_name.data(), account_name.size(),
        account_name.data(), &password_length, // output.
        &password_data, nullptr);
    if (theError == noErr) {
        thePassword.setMemory(password_data, password_length);
        theKeychain.ItemFreeContent(nullptr, password_data);
        return true;
    }
    else
        otErr << "OTKeyring::Mac_RetrieveSecret: Error in "
                 "theKeychain.FindSecret.\n";

    return false;
}
Пример #11
0
// static
bool OTKeyring::IOS_StoreSecret(const OTString& strUser,
                                const OTPassword& thePassword,
                                const std::string& str_display)
{
    OT_ASSERT(strUser.Exists());
    OT_ASSERT(thePassword.getMemorySize() > 0);

    CFStringRef service_name = CFSTR("opentxs");
    CFStringRef account_name = CFStringCreateWithCString(nullptr, strUser.Get(),
                                                         kCFStringEncodingUTF8);
    CFDataRef vData = CFDataCreateWithBytesNoCopy(
        nullptr, thePassword.getMemory_uint8(), thePassword.getMemorySize(),
        kCFAllocatorNull);

    const void* keys[] = {kSecClass, kSecAttrService, kSecAttrAccount,
                          kSecValueData};
    const void* values[] = {kSecClassGenericPassword, service_name,
                            account_name, vData};
    CFDictionaryRef item =
        CFDictionaryCreate(nullptr, keys, values, 4, nullptr, nullptr);

    OSStatus theError = SecItemAdd(item, nullptr);

    CFRelease(item);
    CFRelease(vData);
    CFRelease(account_name);

    if (theError != noErr) {
        otErr << "OTKeyring::IOS_StoreSecret: Error in SecItemAdd.\n";
        return false;
    }

    return true;
}
Пример #12
0
// static
bool OTKeyring::IOS_RetrieveSecret(const OTString& strUser,
                                   OTPassword& thePassword,
                                   const std::string& str_display)
{
    OT_ASSERT(strUser.Exists());

    CFStringRef service_name = CFSTR("opentxs");
    CFStringRef account_name = CFStringCreateWithCString(nullptr, strUser.Get(),
                                                         kCFStringEncodingUTF8);
    CFDataRef vData = nullptr;

    const void* keys[] = {kSecClass, kSecAttrService, kSecAttrAccount,
                          kSecReturnData};
    const void* values[] = {kSecClassGenericPassword, service_name,
                            account_name, kCFBooleanTrue};
    CFDictionaryRef query =
        CFDictionaryCreate(nullptr, keys, values, 4, nullptr, nullptr);

    OSStatus theError = SecItemCopyMatching(query, (CFTypeRef*)&vData);

    CFRelease(query);
    CFRelease(account_name);

    if (theError != noErr) {
        otErr
            << "OTKeyring::IOS_RetrieveSecret: Error in SecItemCopyMatching.\n";
        return false;
    }

    thePassword.setMemory(CFDataGetBytePtr(vData), CFDataGetLength(vData));
    CFRelease(vData);

    return true;
}
Пример #13
0
// static
bool OTKeyring::IOS_DeleteSecret(const OTString& strUser,
                                 const std::string& str_display)
{
    OT_ASSERT(strUser.Exists());

    CFStringRef service_name = CFSTR("opentxs");
    CFStringRef account_name = CFStringCreateWithCString(nullptr, strUser.Get(),
                                                         kCFStringEncodingUTF8);

    const void* keys[] = {kSecClass, kSecAttrService, kSecAttrAccount};
    const void* values[] = {kSecClassGenericPassword, service_name,
                            account_name};
    CFDictionaryRef query =
        CFDictionaryCreate(nullptr, keys, values, 3, nullptr, nullptr);

    OSStatus theError = SecItemDelete(query);

    CFRelease(query);
    CFRelease(account_name);

    if (theError != noErr) {
        otErr << "OTKeyring::IOS_RetrieveSecret: Error in SecItemDelete.\n";
        return false;
    }

    return true;
}
Пример #14
0
const bool	OTSettings::Check_str (const OTString & strSection, const OTString & strKey, OTString & out_strResult,	bool & out_bKeyExist) const
{
	if (! strSection.Exists())		{ OTLog::vError("%s: Error: %s is Empty!\n", __FUNCTION__, "strSection"			); OT_ASSERT(false); }
	if (strSection.Compare(""))		{ OTLog::vError("%s: Error: %s is Blank!\n", __FUNCTION__, "strSection"			); OT_ASSERT(false); }

	if (! strKey.Exists())			{ OTLog::vError("%s: Error: %s is Empty!\n", __FUNCTION__, "strKey"				); OT_ASSERT(false); }
	if (strKey.Compare(""))			{ OTLog::vError("%s: Error: %s is Blank!\n", __FUNCTION__, "strKey"				); OT_ASSERT(false); }

	const char * szVar = p_iniSimple -> GetValue(strSection.Get(), strKey.Get(),NULL);
	OTString strVar(szVar);

	if (strVar.Exists() && !strVar.Compare("")) {out_bKeyExist = true; out_strResult = strVar; }
	else { out_bKeyExist = false; out_strResult = ""; }

	return true;
}
Пример #15
0
// static
bool OTKeyring::KWallet_DeleteSecret(const OTString& strUser,
                                     const std::string& str_display)
{
    OT_ASSERT(strUser.Exists());

    KWallet::Wallet* pWallet = OTKeyring::OpenKWallet();

    if (nullptr != pWallet) {
        const QString qstrKey(strUser.Get());

        bool bResult = false;

        if (pWallet->removeEntry(qstrKey) == 0) // delete the entry
            bResult = true;
        else
            otErr << "OTKeyring::KWallet_DeleteSecret: Failed trying to erase "
                     "secret from KWallet.\n";

        return bResult;
    }

    otErr << "OTKeyring::KWallet_DeleteSecret: Unable to open kwallet.\n";

    return false;
}
Пример #16
0
const bool OTPaths::PathExists(const OTString & strPath)
{
	if (!strPath.Exists()) { OTLog::sError("%s: Null: %s passed in!\n", __FUNCTION__, "strPath" ); OT_ASSERT(false); }


	// remove trailing backslash for stat
	std::string l_strPath(strPath.Get());
	l_strPath = (OTString::replace_chars(l_strPath,"\\",'/'));  // all \ to /

	//std::string l_strPath_stat = l_strPath;
	std::string l_strPath_stat("");
	
	// remove last / if it exists (for l_strPath_stat)
	if ('/' == *l_strPath.rbegin())
		l_strPath_stat = l_strPath.substr(0, l_strPath.size()-1);
	else l_strPath_stat = l_strPath;

	struct stat st; 
    memset(&st, 0, sizeof(st));
    
	if (0 == stat(l_strPath_stat.c_str(), &st)) // good we have at-least on a node
	{
		if ('/' != *l_strPath.rbegin())
		{
			long temp_l=0;
			return FileExists(strPath,temp_l);
		}
		else
		{
			return FolderExists(strPath);
		}
	}
	return false;
}
Пример #17
0
const bool	OTSettings::Save(const OTString & strConfigurationFileExactPath)
{
	if (! strConfigurationFileExactPath.Exists()) { OTLog::vError("%s: Error: %s is Empty!\n", __FUNCTION__, "strConfigurationFileExactPath"); return false; }

	SI_Error rc = p_iniSimple -> SaveFile(strConfigurationFileExactPath.Get());
	if (0 > rc) return false;
	else return true;
}
Пример #18
0
void OTString::LowLevelSetStr(const OTString & strBuf)
{ 
	if (strBuf.Exists()) 
	{ 
		m_lLength = strBuf.m_lLength; 
		m_strBuffer = str_dup2(strBuf.m_strBuffer, m_lLength); 
	} 
}
Пример #19
0
// static
bool OTKeyring::KWallet_StoreSecret(const OTString& strUser,
                                    const OTPassword& thePassword,
                                    const std::string& str_display)
{
    OT_ASSERT(strUser.Exists());
    OT_ASSERT(thePassword.getMemorySize() > 0);

    KWallet::Wallet* pWallet = OTKeyring::OpenKWallet();

    if (nullptr != pWallet) {
        const QString qstrKey(strUser.Get());

        OTData theData(thePassword.getMemory(), thePassword.getMemorySize());
        OTASCIIArmor ascData(theData);
        theData.zeroMemory(); // security reasons.

        OTString strOutput;
        const bool bSuccess =
            ascData.Exists() &&
            ascData.WriteArmoredString(
                strOutput, "DERIVED KEY"); // There's no default, to force you
                                           // to enter the right string.
        ascData.zeroMemory();

        // Set the password
        //
        bool bReturnVal = false;

        if (bSuccess && strOutput.Exists() &&
            pWallet->writePassword(qstrKey,
                                   QString::fromUtf8(strOutput.Get())) == 0)
            bReturnVal = true;
        else
            otErr << "OTKeyring::KWallet_StoreSecret: Failed trying to store "
                     "secret into KWallet.\n";

        strOutput.zeroMemory();

        return bReturnVal;
    }

    otErr << "OTKeyring::KWallet_StoreSecret: Unable to open kwallet.\n";

    return false;
}
Пример #20
0
bool OTSocket_ZMQ_4::Listen(const OTString & strBindingPath)
{
    if (!strBindingPath.Exists())		{ OTLog::vError("%s: Error: %s dosn't exist!\n", __FUNCTION__, "strBindingPath");	OT_FAIL; }
    if (5 > strBindingPath.GetLength()) { OTLog::vError("%s: Error: %s is too short!\n", __FUNCTION__, "strBindingPath");	OT_FAIL; }

    m_strBindingPath = strBindingPath;

    return (this->Listen());
}
Пример #21
0
bool OTSocket_ZMQ_4::Connect(const OTString & strConnectPath)
{
    if (!strConnectPath.Exists())		{ OTLog::vError("%s: Error: %s dosn't exist!\n", __FUNCTION__, "strConnectPath");	OT_FAIL; }
    if (5 > strConnectPath.GetLength()) { OTLog::vError("%s: Error: %s is too short!\n", __FUNCTION__, "strConnectPath");	OT_FAIL; }

    m_strConnectPath = strConnectPath;  // set the connection path.

    return (this->Connect());
}
Пример #22
0
int OTPayment::ProcessXMLNode(irr::io::IrrXMLReader*& xml)
{
    const OTString strNodeName(xml->getNodeName());
    
	if (strNodeName.Compare("payment"))
	{	
		m_strVersion = xml->getAttributeValue("version"); 

        const OTString strPaymentType = xml->getAttributeValue("type");
        
		if (strPaymentType.Exists())
			m_Type = OTPayment::GetTypeFromString(strPaymentType);
        else
            m_Type = OTPayment::ERROR_STATE;
        
		OTLog::vOutput(4, "Loaded payment... Type: %s\n----------\n",
                       this->GetTypeString());
		
		return (OTPayment::ERROR_STATE == m_Type) ? (-1) : 1;
	}
	
	else if (strNodeName.Compare("contents")) 
	{
        OTString strContents;
		
		if (!OTContract::LoadEncodedTextField(xml, strContents) || 
            !strContents.Exists() ||
            // -------------------------------
            !this->SetPayment(strContents))
		{
			OTLog::vError("OTPayment::ProcessXMLNode: ERROR: \"contents\" field without a value, OR error setting that "
                          "value onto this object. Raw:\n\n%s\n\n", strContents.Get());
			
			return (-1); // error condition
		}
		// else success -- the value is now set on this object.
        // todo security: Make sure the type of the payment that's ACTUALLY there
        // matches the type expected (based on the m_Type that we already read, above.)
        
		return 1;
	}

	return 0;
}
Пример #23
0
const bool	OTSettings::CheckSet_str (const OTString & strSection, const OTString & strKey, const OTString & strDefault, OTString & out_strResult, bool & out_bIsNew, const OTString & strComment)
{
	if (! strSection.Exists())			{ OTLog::vError("%s: Error: %s is Empty!\n", __FUNCTION__, "strSection"			); OT_ASSERT(false); }
	if (! strKey.Exists())				{ OTLog::vError("%s: Error: %s is Empty!\n", __FUNCTION__, "strKey"				); OT_ASSERT(false); }

	const char * const szDefault = (strDefault.Exists() && !strDefault.Compare("")) ? strDefault.Get() : NULL;

	OTString strTempResult;
	bool bKeyExist, bNewKeyCheck;
	if (! Check_str(strSection,strKey,strTempResult,bKeyExist)) return false;

	if (bKeyExist)
	{
		// Already have a key, lets use it's value.
		out_bIsNew = false;
		out_strResult = strTempResult;
		return true;
	}
	else
	{
		if (! Set_str(strSection,strKey,strDefault,bNewKeyCheck,strComment)) return false;

		if (NULL == szDefault) // The Default is to have no key.
		{
			// Success
			out_bIsNew = false;
			out_strResult = "";
			return true;
		}
		
		if (bNewKeyCheck)
		{
			// Success
			out_bIsNew = true;
			out_strResult = strDefault;
			return true;
		}
	}

	// If we get here, error!
	OT_ASSERT(false);
	return false;
}
Пример #24
0
const bool OTPaths::AppendFile(OTString & out_strPath, const OTString & strBasePath, const OTString & strFileName)
{
	if (!strBasePath.Exists())	{ OTLog::sError("%s: Null: %s passed in!\n", __FUNCTION__, "strBasePath"); OT_ASSERT(false); }
	if (!strFileName.Exists())	{ OTLog::sError("%s: Null: %s passed in!\n", __FUNCTION__, "strFileName"); OT_ASSERT(false); }

	OTString l_strBasePath_fix(""), l_strFileName_fix("");

	if(!FixPath(strBasePath,l_strBasePath_fix,true)) return false;
	if(!FixPath(strFileName,l_strFileName_fix,false)) return false;

	std::string l_strBasePath(l_strBasePath_fix.Get()), l_strFileName(l_strFileName_fix.Get());

	l_strBasePath.append(l_strFileName);

	const OTString l_strPath(l_strBasePath);

	out_strPath = l_strPath;
	return true;
}
Пример #25
0
// static
bool OTKeyring::KWallet_RetrieveSecret(const OTString& strUser,
                                       OTPassword& thePassword,
                                       const std::string& str_display)
{
    OT_ASSERT(strUser.Exists());

    KWallet::Wallet* pWallet = OTKeyring::OpenKWallet();

    if (nullptr != pWallet) {
        const QString qstrKey(strUser.Get());
        QString qstrPwd;

        // Get the password
        //
        if (pWallet->readPassword(qstrKey, qstrPwd) == 0) {
            const std::string str_password =
                qstrPwd.toStdString(); // todo security: notice str_password
                                       // isn't zero'd here.

            OTString strData(str_password);
            OTASCIIArmor ascData;

            const bool bLoaded =
                strData.Exists() && ascData.LoadFromString(strData);
            strData.zeroMemory();

            if (!bLoaded)
                otErr << __FUNCTION__ << ": Failed trying to decode secret "
                                         "from KWallet contents.\n";
            else {
                OTData thePayload(ascData);
                ascData.zeroMemory();
                if (thePayload.IsEmpty())
                    otErr << __FUNCTION__ << ": Failed trying to decode secret "
                                             "OTData from OTASCIIArmor from "
                                             "KWallet contents.\n";
                else {
                    thePassword.setMemory(thePayload.GetPayloadPointer(),
                                          thePayload.GetSize());
                    thePayload.zeroMemory(); // for security.
                    return true;
                }
            }
        }
        else
            otErr << __FUNCITON__
                  << ": Failed trying to retrieve secret from KWallet.\n";
    }

    // Not an error: what if it just hasn't been set there yet?
    //
    otWarn << "OTKeyring::KWallet_RetrieveSecret: No secret found.\n";

    return false;
}
// This code reads up the file, discards the bookends, and saves only the gibberish itself.
bool OTASCIIArmor::LoadFromFile(const OTString & foldername, const OTString & filename)
{	
	/*
	std::ifstream fin(filename.Get(), std::ios::binary);
		
	if (!fin.is_open())
	{
		OTLog::vError("Error opening file in OTASCIIArmor::LoadFromFile: %s\n", filename.Get());
		return false;
	}

	return LoadFromifstream(fin);	
	 */
	
	OT_ASSERT(foldername.Exists());
	OT_ASSERT(filename.Exists());
	
	// --------------------------------------------------------------------
	
	if (false == OTDB::Exists(foldername.Get(), filename.Get()))
	{
		OTLog::vError("OTASCIIArmor::LoadFromFile: File does not exist: %s%s%s\n", 
					  foldername.Get(), OTLog::PathSeparator(), filename.Get());
		return false;
	}

	// --------------------------------------------------------------------
	//
	OTString strFileContents(OTDB::QueryPlainString(foldername.Get(), filename.Get())); // <=== LOADING FROM DATA STORE.
	
	if (strFileContents.GetLength() < 2)
	{
		OTLog::vError("OTASCIIArmor::LoadFromFile: Error reading file: %s%s%s\n", 
					  foldername.Get(), OTLog::PathSeparator(), filename.Get());
		return false;
	}

	// --------------------------------------------------------------------
	
	return LoadFromString(strFileContents);	
}
Пример #27
0
bool OTString::Contains(const OTString& strCompare) const
{
    if (NULL == m_strBuffer || !strCompare.Exists())
    {
        return false;
    }

    if (strstr(m_strBuffer, strCompare.Get()))
        return true;

    return false;
}
Пример #28
0
// append a string at the end of the current buffer.
void OTString::Concatenate(const OTString & strBuf)
{
    std::string str_output;

    if ((m_lLength > 0) && (NULL != m_strBuffer))
        str_output += m_strBuffer;

    if (strBuf.Exists() && (strBuf.GetLength() > 0))
        str_output += strBuf.Get();

    Set(str_output.c_str());
}
Пример #29
0
// static
bool OTKeyring::Mac_DeleteSecret(const OTString& strUser,
                                 const std::string& str_display)
{
    OT_ASSERT(strUser.Exists());

    const std::string service_name = "opentxs";
    const std::string account_name = strUser.Get();

    OTMacKeychain theKeychain;

    // Setup the attributes the for the keychain item
    SecKeychainAttribute attrs[] = {{kSecServiceItemAttr, service_name.length(),
                                     (char*)service_name.c_str()},
                                    {kSecAccountItemAttr, account_name.length(),
                                     (char*)account_name.c_str()}};
    SecKeychainAttributeList attributes = {sizeof(attrs) / sizeof(attrs[0]),
                                           attrs};

    SecKeychainItemRef theItem = nullptr;
    SecKeychainSearchRef theSearch = nullptr;
    OSStatus theStatus = 0;
    OSErr theResult;

    theResult = theKeychain.SearchCreateFromAttributes(
        nullptr, nullptr, // CFTypeRef SecItemClass, // unused here.
        kSecGenericPasswordItemClass, &attributes, &theSearch);

    bool bReturnVal = false;

    if (errSecSuccess == theResult) // Success searching, now let's iterate the
                                    // results and count them.
    {
        int32_t numberOfItemsFound = 0;
        while (theKeychain.SearchCopyNext(theSearch, &theItem) == noErr) {
            numberOfItemsFound++;
        }

        if (numberOfItemsFound > 0) {
            theStatus = theKeychain.ItemDelete(theItem);

            if (theStatus != 0)
                otErr << "OTKeyring::Mac_DeleteSecret: Error deleting item "
                         "from keychain.\n";
            else
                bReturnVal = true;
        }

        CFRelease(theItem);
        CFRelease(theSearch);
    }

    return bReturnVal;
}
Пример #30
0
const char * OTLog::PeekMemlogBack()
{
	if (__logDeque.size() <= 0)
		return NULL;
	
	OTString * pStr = __logDeque.back();
	
	if ((NULL != pStr) && (pStr->Exists()))
		return pStr->Get();
	
	return NULL;	
}