MOSHEXPORT int /* Win32 error */ madvapi_credread_generic(void* targetname, void** out_cred){ CREDENTIALW* pcred; BOOL b; b = CredReadW((LPCWSTR)targetname, CRED_TYPE_GENERIC, 0, &pcred); if(!b){ return GetLastError(); } *out_cred = pcred; return 0; }
DWORD APIENTRY NPAddConnection(LPNETRESOURCEW lpNetResource, LPWSTR lpPassword, LPWSTR lpUserName) { DWORD NpResult; DWORD dwType = lpNetResource->dwType; LPWSTR lpRemoteName = lpNetResource->lpRemoteName; LPWSTR lpLocalName = lpNetResource->lpLocalName; WCHAR LocalNameBuf[3]; PWSTR ClassName, InstanceName, RemoteName, P; ULONG ClassNameLen, InstanceNameLen; DWORD CredentialsKind; PWSTR PipeBuf = 0; #if defined(FSP_NP_CREDENTIAL_MANAGER) PCREDENTIALW Credential = 0; #endif if (dwType & RESOURCETYPE_PRINT) return WN_BAD_VALUE; if (!FspNpParseRemoteName(lpRemoteName, &ClassName, &ClassNameLen, &InstanceName, &InstanceNameLen)) return WN_BAD_NETNAME; RemoteName = lpRemoteName + 1; LocalNameBuf[0] = L'\0'; if (0 != lpLocalName && L'\0' != lpLocalName[0]) { if (!FspNpCheckLocalName(lpLocalName)) return WN_BAD_LOCALNAME; LocalNameBuf[0] = lpLocalName[0] & ~0x20; /* convert to uppercase */ LocalNameBuf[1] = L':'; LocalNameBuf[2] = L'\0'; if (GetLogicalDrives() & (1 << (LocalNameBuf[0] - 'A'))) return WN_ALREADY_CONNECTED; } FspNpGetCredentialsKind(lpRemoteName, &CredentialsKind); #if defined(FSP_NP_CREDENTIAL_MANAGER) /* if we need credentials and none were passed check with the credential manager */ if (FSP_NP_CREDENTIALS_NONE != CredentialsKind && 0 == lpPassword && CredReadW(lpRemoteName, CRED_TYPE_GENERIC, 0, &Credential)) { if (sizeof(WCHAR) <= Credential->CredentialBlobSize && L'\0' == ((PWSTR)(Credential->CredentialBlob)) [(Credential->CredentialBlobSize / sizeof(WCHAR)) - 1]) { lpUserName = Credential->UserName; lpPassword = (PVOID)Credential->CredentialBlob; } } #endif /* if we need credentials and we don't have any return ACCESS DENIED */ if (FSP_NP_CREDENTIALS_NONE != CredentialsKind) { int Length; if (0 == lpPassword || (0 == (Length = lstrlenW(lpPassword))) || CREDUI_MAX_PASSWORD_LENGTH < Length) { NpResult = WN_ACCESS_DENIED; goto exit; } } if (FSP_NP_CREDENTIALS_USERPASS == CredentialsKind) { int Length; if (0 == lpUserName || (0 == (Length = lstrlenW(lpUserName))) || CREDUI_MAX_USERNAME_LENGTH < Length) { NpResult = WN_ACCESS_DENIED; goto exit; } } PipeBuf = MemAlloc(LAUNCHER_PIPE_BUFFER_SIZE); if (0 == PipeBuf) { NpResult = WN_OUT_OF_MEMORY; goto exit; } /* we do not explicitly check, but assumption is it all fits in LAUNCHER_PIPE_BUFFER_SIZE */ P = PipeBuf; *P++ = FSP_NP_CREDENTIALS_NONE != CredentialsKind ? LauncherSvcInstanceStartWithSecret : LauncherSvcInstanceStart; memcpy(P, ClassName, ClassNameLen * sizeof(WCHAR)); P += ClassNameLen; *P++ = L'\0'; memcpy(P, InstanceName, InstanceNameLen * sizeof(WCHAR)); P += InstanceNameLen; *P++ = L'\0'; lstrcpyW(P, RemoteName); P += lstrlenW(RemoteName) + 1; lstrcpyW(P, LocalNameBuf); P += lstrlenW(LocalNameBuf) + 1; if (FSP_NP_CREDENTIALS_USERPASS == CredentialsKind) { lstrcpyW(P, lpUserName); P += lstrlenW(lpUserName) + 1; } if (FSP_NP_CREDENTIALS_NONE != CredentialsKind) { lstrcpyW(P, lpPassword); P += lstrlenW(lpPassword) + 1; } NpResult = FspNpCallLauncherPipe( PipeBuf, (ULONG)(P - PipeBuf) * sizeof(WCHAR), LAUNCHER_PIPE_BUFFER_SIZE); switch (NpResult) { case WN_SUCCESS: case WN_ACCESS_DENIED: break; case ERROR_ALREADY_EXISTS: /* * The file system is already running! If we are being asked for a drive mapping, * see if it is the one we already have to decide on the error code to return. */ if (L'\0' != LocalNameBuf[0]) { WCHAR ExpectRemoteNameBuf[sizeof(((FSP_FSCTL_VOLUME_PARAMS *)0)->Prefix) / sizeof(WCHAR)]; WCHAR RemoteNameBuf[sizeof(((FSP_FSCTL_VOLUME_PARAMS *)0)->Prefix) / sizeof(WCHAR)]; DWORD RemoteNameSize; P = ExpectRemoteNameBuf; *P++ = L'\\'; *P++ = L'\\'; memcpy(P, ClassName, ClassNameLen * sizeof(WCHAR)); P += ClassNameLen; *P++ = L'\\'; memcpy(P, InstanceName, InstanceNameLen * sizeof(WCHAR)); P += InstanceNameLen; *P++ = L'\0'; RemoteNameSize = sizeof RemoteNameBuf / sizeof(WCHAR); NpResult = NPGetConnection(LocalNameBuf, RemoteNameBuf, &RemoteNameSize); if (WN_SUCCESS == NpResult) NpResult = 0 == lstrcmpW(ExpectRemoteNameBuf, RemoteNameBuf) ? WN_SUCCESS : WN_NO_NETWORK; else NpResult = WN_NO_NETWORK; } else /* we are not being asked for a drive mapping, so whatever we have is good! */ NpResult = WN_SUCCESS; break; default: NpResult = WN_NO_NETWORK; break; } exit: MemFree(PipeBuf); #if defined(FSP_NP_CREDENTIAL_MANAGER) if (0 != Credential) CredFree(Credential); #endif return NpResult; }
void SG_password__get( SG_context *pCtx, const char *szRepoSpec, const char *szUsername, SG_string **ppstrPassword) { SG_string* pstrTarget = NULL; SG_string* pstrPassword = NULL; LPWSTR pwszTarget = NULL; SG_byte* pbPassword = NULL; PCREDENTIAL pCred = NULL; BOOL result = FALSE; SG_NULLARGCHECK_RETURN(szRepoSpec); SG_NULLARGCHECK_RETURN(szUsername); SG_NULLARGCHECK_RETURN(ppstrPassword); _get_key(pCtx, szRepoSpec, szUsername, &pstrTarget); if (SG_CONTEXT__HAS_ERR(pCtx)) { SG_error err, err2; err2 = SG_context__get_err(pCtx, &err); if (SG_IS_ERROR(err2)) { SG_ERR_DISCARD; SG_ERR_THROW(err2); } if (err & __SG_ERR__GETLASTERROR__) SG_ERR_DISCARD; else SG_ERR_RETHROW; } if (pstrTarget) { SG_ERR_CHECK( SG_utf8__extern_to_os_buffer__wchar(pCtx, SG_string__sz(pstrTarget), &pwszTarget, NULL) ); result = CredReadW(pwszTarget, CRED_TYPE_GENERIC, 0, &pCred); if (!result) { DWORD err = GetLastError(); if (err != ERROR_NOT_FOUND && err != ERROR_NO_SUCH_LOGON_SESSION) SG_ERR_THROW2( SG_ERR_GETLASTERROR(GetLastError()), (pCtx, "%s", "unable to retrieve saved credentials") ); } else { SG_uint32 size = pCred->CredentialBlobSize+sizeof(wchar_t); SG_ERR_CHECK( SG_allocN(pCtx, pCred->CredentialBlobSize+sizeof(wchar_t), pbPassword) ); memcpy(pbPassword, pCred->CredentialBlob, size); SG_ERR_CHECK( SG_string__alloc(pCtx, &pstrPassword) ); SG_ERR_CHECK( SG_utf8__intern_from_os_buffer__wchar(pCtx, pstrPassword, (const LPWSTR)pbPassword) ); *ppstrPassword = pstrPassword; pstrPassword = NULL; } } /* fall through */ fail: SG_STRING_NULLFREE(pCtx, pstrTarget); SG_STRING_NULLFREE(pCtx, pstrPassword); SG_NULLFREE(pCtx, pwszTarget); SG_NULLFREE(pCtx, pbPassword); if (pCred) CredFree(pCred); }