void SG_password__set(
	SG_context* pCtx,
	const char *szRepoSpec,
	SG_string *pstrUserName,
	SG_string *pstrPassword)
{
	SG_string* pstrTarget = NULL;
	LPWSTR pwszTarget = NULL;
	LPWSTR pwszPassword = NULL;
	SG_uint32 lenPassword = 0;
	LPWSTR pwszUserName = NULL;
	CREDENTIAL cred;

	SG_NULLARGCHECK_RETURN(szRepoSpec);
	SG_NULLARGCHECK_RETURN(pstrUserName);
	SG_NULLARGCHECK_RETURN(pstrPassword);
	if (!SG_string__length_in_bytes(pstrUserName))
		SG_ERR_THROW2_RETURN(SG_ERR_INVALIDARG, (pCtx, "%s", "pstrUserName is empty"));

	SG_ERR_CHECK(  _get_key(pCtx, szRepoSpec, SG_string__sz(pstrUserName), &pstrTarget)  );
	SG_ERR_CHECK(  SG_utf8__extern_to_os_buffer__wchar(pCtx, SG_string__sz(pstrTarget), &pwszTarget, NULL)  );
	SG_ERR_CHECK(  SG_utf8__extern_to_os_buffer__wchar(pCtx, SG_string__sz(pstrPassword), &pwszPassword, &lenPassword)  );
	SG_ERR_CHECK(  SG_utf8__extern_to_os_buffer__wchar(pCtx, SG_string__sz(pstrUserName), &pwszUserName, NULL)  );

	SG_zero(cred);
	cred.Type = CRED_TYPE_GENERIC;
	cred.TargetName = pwszTarget;
	cred.CredentialBlob = (LPBYTE)pwszPassword;
	cred.CredentialBlobSize = lenPassword*sizeof(wchar_t);
	cred.Persist = CRED_PERSIST_LOCAL_MACHINE; // unsupported on Windows Vista Home Basic, Windows Vista Home Premium, Windows Vista Starter, and Windows XP Home Edition
	cred.UserName = pwszUserName;

	if ( !CredWriteW(&cred, 0) )
		SG_ERR_THROW2( SG_ERR_GETLASTERROR(GetLastError()), (pCtx, "%s", "unable to save credentials") );

	/* fall through */
fail:
	SG_STRING_NULLFREE(pCtx, pstrTarget);
	SG_NULLFREE(pCtx, pwszTarget);
	SG_NULLFREE(pCtx, pwszPassword);
	SG_NULLFREE(pCtx, pwszUserName);
}
Пример #2
0
MOSHEXPORT
int /* Win32 error */
madvapi_credwrite_generic(void* targetname, void* comment,
                          void* blob, int size, int persist){
    CREDENTIALW cred;
    BOOL b;
    ZeroMemory(&cred,sizeof(cred));
    cred.Flags = 0;
    cred.Type = CRED_TYPE_GENERIC;
    cred.TargetName = (LPWSTR)targetname;
    cred.Comment = (LPWSTR)comment;
    cred.CredentialBlobSize = size;
    cred.CredentialBlob = (LPBYTE)blob;
    cred.Persist = persist;
    cred.AttributeCount = 0;
    b = CredWriteW(&cred, 0);
    if(!b){
        return GetLastError();
    }
    return 0;
}
Пример #3
0
DWORD APIENTRY NPAddConnection3(HWND hwndOwner,
    LPNETRESOURCEW lpNetResource, LPWSTR lpPassword, LPWSTR lpUserName, DWORD dwFlags)
{
    DWORD NpResult;
    PWSTR RemoteName = lpNetResource->lpRemoteName;
    DWORD CredentialsKind;
    WCHAR UserName[CREDUI_MAX_USERNAME_LENGTH + 1], Password[CREDUI_MAX_PASSWORD_LENGTH + 1];
#if defined(FSP_NP_CREDENTIAL_MANAGER)
    BOOL Save = TRUE;
#endif

    //dwFlags |= CONNECT_INTERACTIVE | CONNECT_PROMPT; /* TESTING ONLY! */

    /* CONNECT_PROMPT is only valid if CONNECT_INTERACTIVE is also set */
    if (CONNECT_PROMPT == (dwFlags & (CONNECT_INTERACTIVE | CONNECT_PROMPT)))
        return WN_BAD_VALUE;

    /* if not CONNECT_PROMPT go ahead and attempt to NPAddConnection once */
    if (0 == (dwFlags & CONNECT_PROMPT))
    {
        NpResult = NPAddConnection(lpNetResource, lpPassword, lpUserName);
        if (WN_ACCESS_DENIED != NpResult || 0 == (dwFlags & CONNECT_INTERACTIVE))
            return NpResult;
    }

    FspNpGetCredentialsKind(RemoteName, &CredentialsKind);
    if (FSP_NP_CREDENTIALS_NONE == CredentialsKind)
        return WN_CANCEL;

    /* if CONNECT_INTERACTIVE keep asking the user for valid credentials or cancel */
    NpResult = WN_SUCCESS;
    lstrcpyW(UserName, L"UNSPECIFIED");
    Password[0] = L'\0';
    if (FSP_NP_CREDENTIALS_PASSWORD == CredentialsKind)
        FspNpParseUserName(RemoteName, UserName, sizeof UserName / sizeof UserName[0]);
    do
    {
        NpResult = FspNpGetCredentials(
            hwndOwner, RemoteName, NpResult,
            CredentialsKind,
#if defined(FSP_NP_CREDENTIAL_MANAGER)
            &Save,
#else
            0,
#endif
            UserName, sizeof UserName / sizeof UserName[0],
            Password, sizeof Password / sizeof Password[0]);
        if (WN_SUCCESS != NpResult)
            break;

        NpResult = NPAddConnection(lpNetResource, Password, UserName);
    } while (WN_ACCESS_DENIED == NpResult);

#if defined(FSP_NP_CREDENTIAL_MANAGER)
    if (WN_SUCCESS == NpResult && Save)
    {
        CREDENTIALW Credential;

        memset(&Credential, 0, sizeof Credential);
        Credential.Type = CRED_TYPE_GENERIC;
        Credential.Persist = CRED_PERSIST_LOCAL_MACHINE;
        Credential.TargetName = RemoteName;
        Credential.UserName = UserName;
        Credential.CredentialBlobSize = (lstrlenW(Password) + 1) * sizeof(WCHAR);
        Credential.CredentialBlob = (PVOID)Password;

        CredWriteW(&Credential, 0);
    }
#endif

    SecureZeroMemory(Password, sizeof Password);

    return NpResult;
}