📄 rasreg.c
字号:
// We only support the system/registry phonebook.
RetVal = ERROR_CANNOT_OPEN_PHONEBOOK;
}
if (NULL == lpcb) {
// Invalid argument
RetVal = ERROR_INVALID_SIZE;
}
if (ERROR_SUCCESS != RetVal) {
DEBUGMSG( ZONE_RAS | ZONE_FUNCTION, (TEXT("-AfdRasEnumEntries() returning %d\r\n"), RetVal));
return RetVal;
}
// Initialize the return count information.
if (NULL != lpcEntries) {
*lpcEntries = 0;
}
hRes = RegOpenKeyEx( HKEY_CURRENT_USER, RASBOOK_KEY, 0, KEY_READ, &hKey );
if (ERROR_SUCCESS != hRes) {
DEBUGMSG( ZONE_WARN|ZONE_RAS,
(TEXT("AfdRasEnumEntries() unable to open Registry Key '%s' Error %d\r\n"),
RASBOOK_KEY, hRes));
AfdRasMakeDefault(reserved, lpszPhoneBookPath);
// Now try to open the key.
hRes = RegOpenKeyEx(HKEY_CURRENT_USER,RASBOOK_KEY,0,
KEY_READ,&hKey);
}
cEntries = 0;
if (ERROR_SUCCESS == hRes ) {
// Now loop over the sub-keys getting the names of each.
for (Index = 0; hRes == ERROR_SUCCESS; Index++) {
ValueLen = sizeof(ValueString) / sizeof(ValueString[0]);
hRes = RegEnumKeyEx (hKey, Index, ValueString, &ValueLen, NULL, NULL, NULL, &LastWrite);
if (hRes == ERROR_SUCCESS) {
DEBUGMSG(ZONE_RAS | ZONE_FUNCTION, (TEXT("AfdRasEnumEntries: Found entry %s\r\n"), ValueString));
cbRequired += sizeof(RASENTRYNAME);
if ((cbRequired <= *lpcb) && (NULL != lpRasEntryName)) {
// Stuff the data in.
lpRasEntryName[cEntries].dwSize = sizeof(RASENTRYNAME);
_tcscpy(lpRasEntryName[cEntries].szEntryName, ValueString);
// Tell them how many we wrote.
if (NULL != lpcEntries) {
(*lpcEntries)++;
}
}
// Keep track of how many we found?
cEntries++;
}
}
RegCloseKey (hKey);
}
// If no other error then
if ((ERROR_SUCCESS == RetVal) && (cbRequired > *lpcb)) {
RetVal = ERROR_BUFFER_TOO_SMALL;
}
// Number of bytes required/returned.
*lpcb = cbRequired;
DEBUGMSG(ZONE_RAS | ZONE_FUNCTION, (TEXT("-AfdRasEnumEntries() returning %d\r\n"), RetVal));
return RetVal;
}
/*****************************************************************************
*
* @func DWORD | RasGetEntryDialParams | The <f RasGetEntryDialParams>
* function retrieves the connection information saved by the
* last successful call to the <f RasDial> or
* <f RasGetEntryDialParams> function for a specified phonebook entry.
*
* @rdesc If the function succeeds, the return value is zero.
* If the function fails, the return value can be one of the
* following error codes.
* @ecode ERROR_CANNOT_OPEN_PHONEBOOK | Invalid szPhonebook
* @ecode ERROR_CANNOT_FIND_PHONEBOOK_ENTRY | Invalid szEntry
* @ecode ERROR_INVALID_PARAMETER | lpRasDialParams or lpfPassword is NULL,
* | or lpRasDialParams->dwSize is not correct
*
* @parm LPTSTR | lpszPhoneBook | pointer to the full path
* and filename of the phonebook file
* @parm LPRASDIALPARAMS | lprasdialparams | pointer to a structure that
* receives the connection properties
* @parm LPBOOL | lpfPassword | Indicates whether the user's
* password was retrieved.
*
*
*/
DWORD APIENTRY
AfdRasGetEntryDialParams(
LPTSTR lpszPhonebook,
LPRASDIALPARAMS lpRasDialParams,
LPBOOL lpfPassword)
{
DWORD dwResult;
DEBUGMSG( ZONE_FUNCTION,
( TEXT( "+RasGetEntryDialParams( %s, %s)\n"),
lpszPhonebook ? lpszPhonebook : TEXT("NULL"),
lpRasDialParams && lpRasDialParams->szEntryName ? lpRasDialParams->szEntryName : TEXT("NULL")) );
if ((NULL == lpRasDialParams)
|| (NULL == lpfPassword)
|| (sizeof(RASDIALPARAMS) != lpRasDialParams->dwSize)
|| !ValidateStringLength(lpRasDialParams->szEntryName, 1, RAS_MaxEntryName))
{
dwResult = ERROR_INVALID_PARAMETER;
}
else
{
// Get username/domain/password for szEntryName from CredMan
dwResult = RasCredentialsRead(lpRasDialParams, FALSE, lpfPassword);
// For security reasons do not expose the password. The user should not need to know it.
SecureZeroMemory(lpRasDialParams->szPassword, sizeof(lpRasDialParams->szPassword));
if (*lpfPassword)
memcpy(lpRasDialParams->szPassword, szDummyPassword, sizeof(szDummyPassword));
}
DEBUGMSG (ZONE_FUNCTION, (TEXT("-RasGetEntryDialParams Result=%u (%s,%s)\r\n"),
dwResult,
lpRasDialParams && lpRasDialParams->szUserName ? lpRasDialParams->szUserName : TEXT("(NULL)"),
lpRasDialParams && lpRasDialParams->szDomain ? lpRasDialParams->szDomain : TEXT("(NULL)")));
return dwResult;
}
//
// Replace the placeholder password with the real one from the registry
//
BOOL
FixRasPassword(
IN OUT LPRASDIALPARAMS lpRasDialParams)
{
DWORD dwResult = ERROR_SUCCESS;
if (_tcscmp(lpRasDialParams->szPassword, szDummyPassword) == 0)
{
// need to replace the dummy password with the real one.
// Look in the system phone book
dwResult = RasCredentialsRead(lpRasDialParams, TRUE, NULL);
}
return dwResult == ERROR_SUCCESS;
}
/*****************************************************************************
*
* @func DWORD | RasSetEntryDialParams | The <f RasSetEntryDialParams>
* function changes the connection information saved by the
* last successful call to the <f RasDial> or
* <f RasGetEntryDialParams> function for a specified phonebook entry.
*
* @rdesc If the function succeeds, the return value is zero.
* If the function fails, the return value can be one of the
* following error codes.
* @ecode ERROR_BUFFER_INVALID |
* The address or buffer specified by lpRasDialParams is invalid.
* @ecode ERROR_CANNOT_OPEN_PHONEBOOK |
* The phonebook is corrupted or missing components.
* @ecode ERROR_CANNOT_FIND_PHONEBOOK_ENTRY |
* The phonebook entry does not exist.
*
* @parm LPTSTR | lpszPhoneBook | pointer to the full path
* and filename of the phonebook file
* @parm LPRASDIALPARAMS | lprasdialparams | pointer to a structure that
* receives the connection properties
* @parm BOOL | fRemovePassword | Indicates whether the user's
* password should be saved.
*
*
*/
DWORD APIENTRY
AfdRasSetEntryDialParams(
IN LPTSTR lpszPhonebook,
IN LPRASDIALPARAMS lpRasDialParams,
IN BOOL fRemovePassword)
{
DWORD dwResult;
DEBUGMSG (ZONE_FUNCTION, (TEXT("RAS: +RasSetEntryDialParams %s %s\n"),
lpRasDialParams->szUserName,
lpRasDialParams->szDomain));
if ((NULL == lpRasDialParams)
|| (sizeof(RASDIALPARAMS) != lpRasDialParams->dwSize)
|| !ValidateStringLength(lpRasDialParams->szEntryName, 1, RAS_MaxEntryName)
|| !ValidateStringLength(lpRasDialParams->szPhoneNumber, 0, RAS_MaxPhoneNumber)
|| !ValidateStringLength(lpRasDialParams->szUserName, 0, UNLEN)
|| !ValidateStringLength(lpRasDialParams->szPassword, 0, PWLEN)
|| !ValidateStringLength(lpRasDialParams->szDomain, 0, DNLEN))
{
dwResult = ERROR_BUFFER_INVALID;
}
else
{
dwResult = RasCredentialsWrite(lpRasDialParams, fRemovePassword);
}
DEBUGMSG (ZONE_RAS | ZONE_FUNCTION, (TEXT("-AfdRasSetEntryDialParams : Result=%d\r"), dwResult));
return dwResult;
}
//
// Conversion support for CE 3.0 and earlier RASENTRY structures to CE 4.0.
// That is, for apps that have not been recompiled for 4.0, we translate the
// old format RASENTRY that they use to the new 4.0 format.
//
#define RAS_MaxDeviceName_V3 32
typedef struct tagRASENTRYW_V3
{
DWORD dwSize;
DWORD dwfOptions;
DWORD dwCountryID;
DWORD dwCountryCode;
WCHAR szAreaCode[ RAS_MaxAreaCode + 1 ];
WCHAR szLocalPhoneNumber[ RAS_MaxPhoneNumber + 1 ];
DWORD dwAlternateOffset;
RASIPADDR ipaddr;
RASIPADDR ipaddrDns;
RASIPADDR ipaddrDnsAlt;
RASIPADDR ipaddrWins;
RASIPADDR ipaddrWinsAlt;
DWORD dwFrameSize;
DWORD dwfNetProtocols;
DWORD dwFramingProtocol;
WCHAR szScript[ MAX_PATH ];
WCHAR szAutodialDll[ MAX_PATH ];
WCHAR szAutodialFunc[ MAX_PATH ];
WCHAR szDeviceType[ RAS_MaxDeviceType + 1 ];
WCHAR szDeviceName[ RAS_MaxDeviceName_V3 + 1 ];
WCHAR szX25PadType[ RAS_MaxPadType + 1 ];
WCHAR szX25Address[ RAS_MaxX25Address + 1 ];
WCHAR szX25Facilities[ RAS_MaxFacilities + 1 ];
WCHAR szX25UserData[ RAS_MaxUserData + 1 ];
DWORD dwChannels;
DWORD dwReserved1;
DWORD dwReserved2;
} RASENTRY_V3, *LPRASENTRY_V3;
/*****************************************************************************
*
*
* @func DWORD | RasSetEntryProperties | Comment on function.
*
* @rdesc
* If the function succeeds, the return value is zero.
* <nl>If the function fails, the return value can be one of the
* following error codes: <nl>
* @ecode ERROR_INVALID_PARAMETER |
* The function is called with an invalid parameter.
* @ecode ERROR_BUFFER_INVALID |
* The address or buffer specified by lpbEntryInfo is invalid.
* @ecode ERROR_BUFFER_TOO_SMALL |
* The buffer size indicated in lpdwEntryInfoSize is too small.
* @ecode ERROR_CANNOT_OPEN_PHONEBOOK |
* The phonebook is corrupted or missing components.
*
*
* @parm LPTSTR | lpszPhonebook |
* Points to a null terminated string that specifies the full path and
* filename of the phonebook file. This parameter can be NULL, in which
* case the default phonebook file is used. This parameter should always
* be NULL for Windows 95 and WinCE since the phonebook entries are
* stored in the registry.
* @parm LPTSTR | szEntry |
* Points to a null terminated string containing an existing entry name.
* If a "" entry name is specified, structures containing default
* values are returned.
* @parm LPBYTE | lpbEntry |
* Points to a RASENTRY structure that receives the connection data
* associated with the phonebook entry specified by the lpszEntry member
* followed by any additional bytes needed for the alternate phone number
* list if any. On entry, the dwSize member of this structure must
* specify the size of the structure.
* @parm DWORD | dwEntrySize |
* The size, in bytes, of the buffer specified by lpEntryInfo.
* @parm LPBYTE | lpb |
* Points to buffer of device-specific configuration information.
* This parameter may be NULL if the data is not required.
* @parm DWORD | dwSize |
* Points to a variable that contains the size, in bytes, of the buffer
* specified by lpb.
*
* @ex An example of how to use this function follows |
* No Example
*
*/
DWORD APIENTRY
AfdRasSetEntryProperties(
LPTSTR lpszPhonebook,
LPTSTR szEntry,
LPBYTE lpbEntry,
DWORD dwEntrySize,
LPBYTE lpb,
DWORD dwSize)
{
HKEY hKey;
LONG hRes;
DWORD RetVal;
RASPENTRY RaspEntry; // Private version of structure
DEBUGMSG( ZONE_RAS | ZONE_FUNCTION,
(TEXT("+AfdRasSetEntryProperties(0x%08X(%s), 0x%08X(%s), ")
TEXT("0x%X, %d, 0x%X, %d)\r\n"),
lpszPhonebook,
(lpszPhonebook != NULL) ? lpszPhonebook : TEXT("NULL"),
szEntry,
(szEntry != NULL) ? szEntry : TEXT("NULL"),
lpbEntry, dwEntrySize, lpb, dwSize) );
if (NULL == lpbEntry)
RetVal = ERROR_BUFFER_INVALID;
else if (dwEntrySize < sizeof(RASENTRY_V3)
|| (dwEntrySize > sizeof(RASENTRY_V3) && dwEntrySize < sizeof(RASENTRY)))
RetVal = ERROR_BUFFER_TOO_SMALL;
else
{
RetVal = CreateRasEntryKey(lpszPhonebook, szEntry, &hKey);
if (ERROR_SUCCESS == RetVal)
{
// Convert the value.
RaspConvPublicPriv ((LPRASENTRY)lpbEntry, &RaspEntry);
// Use the private structure size.
dwEntrySize = sizeof(RASPENTRY);
// Create The value.
hRes = RegSetValueEx (hKey, RASBOOK_ENTRY_VALUE, 0, REG_BINARY,
(LPBYTE)&RaspEntry, dwEntrySize);
if (ERROR_SUCCESS != hRes)
{
DEBUGMSG (ZONE_RAS | ZONE_ERROR,
(TEXT(" AfdRasSetEntryProperties: ERROR %d from RegSetValueEx\n"), hRes));
RetVal = ERROR_CANNOT_OPEN_PHONEBOOK;
}
else if (lpb)
{
BOOL fRet;
DATA_BLOB dataIn, dataOut = {0};
if (!_tcscmp (RaspEntry.szDeviceType, RASDT_Vpn))
{
// encrypt the device config struct for VPN (mainly so that L2TP secrets are encrypted in the registry)
// Note that the secrets are still accessible via RasGetEntryProperties, which has to return the
// decrypted version.
// Would be more secure if this work was done by L2TP itself. This would make it possible for the
// secrets to be hidden from the API. However that requires some rearchitecting.
dataIn.cbData = dwSize;
dataIn.pbData = lpb;
fRet = CryptProtectData(&dataIn, RaspEntry.szDeviceName, NULL, NULL, NULL, CRYPTPROTECT_SYSTEM, &dataOut);
if (fRet)
{
lpb = dataOut.pbData;
dwSize = dataOut.cbData;
}
else
{
hRes = GetLastError();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -