Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

600 lines
16 KiB

  1. /*++
  2. Copyright (C) Microsoft Corporation, 1990 - 1998
  3. All rights reserved
  4. Module Name:
  5. password.c
  6. Abstract:
  7. Author:
  8. Environment:
  9. User Mode -Win32
  10. Revision History:
  11. --*/
  12. #include "precomp.h"
  13. #pragma hdrstop
  14. #include <lm.h>
  15. #include "client.h"
  16. #ifdef OLD_CODE
  17. #define DLG_NETWORK_PASSWORD 500
  18. #define IDD_ENTER_PASSWORD_TEXT 501
  19. #define IDD_NETWORK_PASSWORD_SLE 502
  20. #define IDD_NETWORK_PASSWORD_HELP 503
  21. #define IDH_500_501 8912768 // Enter Network Password: "Enter password for %s:" (Static)
  22. #define IDH_500_502 8912786 // Enter Network Password: "" (Edit)
  23. #define ID_HELP_NETWORK_PASSWORD IDH_500_501
  24. DWORD RemoveFromReconnectList(LPTSTR pszRemotePath) ;
  25. DWORD AddToReconnectList(LPTSTR pszRemotePath) ;
  26. BOOL APIENTRY
  27. NetworkPasswordDialog(
  28. HWND hWnd,
  29. UINT usMsg,
  30. WPARAM wParam,
  31. LONG lParam
  32. );
  33. DLG_NETWORK_PASSWORD DIALOG 65, 17, 247, 61
  34. STYLE DS_MODALFRAME | WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU
  35. CAPTION "Enter Network Password"
  36. FONT 8, "MS Shell Dlg"
  37. BEGIN
  38. LTEXT "Enter password for %s:", IDD_ENTER_PASSWORD_TEXT, 10,
  39. 18, 170, 8
  40. LTEXT "&Password:", -1, 10, 36, 37, 8
  41. EDITTEXT IDD_NETWORK_PASSWORD_SLE, 50, 34, 132, 12, ES_PASSWORD |
  42. ES_AUTOHSCROLL
  43. PUSHBUTTON "OK", IDOK, 201, 6, 40, 14
  44. PUSHBUTTON "Cancel", IDCANCEL, 201, 23, 40, 14
  45. PUSHBUTTON "&Help", IDD_NETWORK_PASSWORD_HELP, 201, 40, 40, 14
  46. END
  47. #endif
  48. HMODULE hmoduleMpr = NULL;
  49. FARPROC pfnWNetAddConnection2 = NULL;
  50. FARPROC pfnWNetCancelConnection2 = NULL;
  51. FARPROC pfnWNetOpenEnum = NULL;
  52. FARPROC pfnWNetEnumResource = NULL;
  53. FARPROC pfnWNetCloseEnum = NULL;
  54. BOOL NetworkPasswordInitDialog( HWND hWnd, LPTSTR pServerShareName );
  55. BOOL NetworkPasswordOK( HWND hWnd );
  56. BOOL NetworkPasswordCancel( HWND hWnd );
  57. BOOL NetworkPasswordHelp( HWND hWnd );
  58. #ifdef UNICODE
  59. #define WNETADDCONNECTION2_NAME "WNetAddConnection2W"
  60. #define WNETOPENENUM_NAME "WNetOpenEnumW"
  61. #define WNETENUMRESOURCE_NAME "WNetEnumResourceW"
  62. #define WNETCLOSEENUM_NAME "WNetCloseEnum"
  63. #else
  64. #define WNETADDCONNECTION2_NAME "WNetAddConnection2A"
  65. #define WNETOPENENUM_NAME "WNetOpenEnumA"
  66. #define WNETENUMRESOURCE_NAME "WNetEnumResourceA"
  67. #define WNETCLOSEENUM_NAME "WNetCloseEnum"
  68. #endif
  69. BOOL APIENTRY
  70. NetworkPasswordDialog(
  71. HWND hWnd,
  72. UINT usMsg,
  73. WPARAM wParam,
  74. LONG lParam
  75. )
  76. {
  77. switch (usMsg)
  78. {
  79. case WM_INITDIALOG:
  80. return NetworkPasswordInitDialog( hWnd, (LPTSTR)lParam );
  81. case WM_COMMAND:
  82. switch (LOWORD(wParam))
  83. {
  84. case IDOK:
  85. return NetworkPasswordOK(hWnd);
  86. case IDCANCEL:
  87. return NetworkPasswordCancel(hWnd);
  88. case IDD_NETWORK_PASSWORD_HELP:
  89. NetworkPasswordHelp( hWnd );
  90. break;
  91. }
  92. break;
  93. }
  94. if( usMsg == WM_Help )
  95. NetworkPasswordHelp( hWnd );
  96. return FALSE;
  97. }
  98. /*
  99. *
  100. */
  101. BOOL NetworkPasswordInitDialog(
  102. HWND hWnd,
  103. LPTSTR pServerShareName
  104. )
  105. {
  106. TCHAR PasswordText[MAX_PATH];
  107. TCHAR ResourceText[64];
  108. /* Get the resource text, which includes a replaceable parameter:
  109. */
  110. GetDlgItemText( hWnd, IDD_ENTER_PASSWORD_TEXT,
  111. ResourceText, COUNTOF(ResourceText) );
  112. wsprintf( PasswordText, ResourceText, pServerShareName );
  113. SetDlgItemText( hWnd, IDD_ENTER_PASSWORD_TEXT, PasswordText );
  114. SetWindowLongPtr( hWnd, GWLP_USERDATA, (INT_PTR)pServerShareName );
  115. return TRUE;
  116. }
  117. /*
  118. *
  119. */
  120. BOOL NetworkPasswordOK(
  121. HWND hWnd
  122. )
  123. {
  124. TCHAR Password[MAX_PATH];
  125. LPTSTR pServerShareName = NULL;
  126. NET_API_STATUS Status;
  127. HANDLE hPrinter = NULL;
  128. NETRESOURCE NetResource;
  129. ZERO_OUT( &NetResource );
  130. if( GetDlgItemText( hWnd, IDD_NETWORK_PASSWORD_SLE,
  131. Password, COUNTOF(Password) ) )
  132. {
  133. pServerShareName = (LPTSTR)GetWindowLongPtr( hWnd, GWLP_USERDATA );
  134. NetResource.lpRemoteName = pServerShareName;
  135. NetResource.lpLocalName = NULL;
  136. NetResource.lpProvider = NULL;
  137. NetResource.dwType = RESOURCETYPE_PRINT;
  138. if( !hmoduleMpr )
  139. {
  140. if( hmoduleMpr = LoadLibrary( TEXT("mpr.dll") ) )
  141. {
  142. if( !( pfnWNetAddConnection2 =
  143. GetProcAddress( hmoduleMpr,
  144. WNETADDCONNECTION2_NAME ) ) ||
  145. !( pfnWNetOpenEnum =
  146. GetProcAddress( hmoduleMpr,
  147. WNETOPENENUM_NAME) ) ||
  148. !( pfnWNetEnumResource =
  149. GetProcAddress( hmoduleMpr,
  150. WNETENUMRESOURCE_NAME) ) ||
  151. !( pfnWNetCloseEnum =
  152. GetProcAddress( hmoduleMpr,
  153. WNETCLOSEENUM_NAME) ) )
  154. {
  155. pfnWNetAddConnection2 = NULL ;
  156. pfnWNetOpenEnum = NULL ;
  157. pfnWNetEnumResource = NULL ;
  158. pfnWNetCloseEnum = NULL ;
  159. FreeLibrary( hmoduleMpr );
  160. hmoduleMpr = NULL;
  161. }
  162. }
  163. }
  164. if( pfnWNetAddConnection2 )
  165. {
  166. Status = (*pfnWNetAddConnection2)( &NetResource, Password, NULL,
  167. CONNECT_UPDATE_PROFILE );
  168. if (Status == NO_ERROR)
  169. (void) AddToReconnectList(NetResource.lpRemoteName) ;
  170. }
  171. else
  172. {
  173. Status = GetLastError( );
  174. }
  175. if( Status != NO_ERROR )
  176. {
  177. DBGMSG( DBG_WARNING, ( "WNetAddConnection2 %s failed: Error %d\n",
  178. pServerShareName, GetLastError( ) ) );
  179. }
  180. if( ( Status != NO_ERROR )
  181. ||( !OpenPrinter( pServerShareName, &hPrinter, NULL ) ) )
  182. {
  183. ReportFailure( hWnd, IDS_MESSAGE_TITLE, IDS_COULDNOTCONNECTTOPRINTER );
  184. return TRUE;
  185. }
  186. }
  187. EndDialog( hWnd, (INT)hPrinter );
  188. return TRUE;
  189. }
  190. /*
  191. *
  192. */
  193. BOOL NetworkPasswordCancel(
  194. HWND hWnd
  195. )
  196. {
  197. EndDialog( hWnd, 0 );
  198. return TRUE;
  199. }
  200. /*
  201. *
  202. */
  203. BOOL NetworkPasswordHelp(
  204. HWND hWnd
  205. )
  206. {
  207. ShowHelp( hWnd, HELP_CONTEXT, ID_HELP_NETWORK_PASSWORD );
  208. return FALSE;
  209. }
  210. /////////////////////////////////////////////////////////////////////////////
  211. #define SZ_PRINTER_RECONNECTIONS TEXT("Printers\\RestoredConnections")
  212. /*
  213. * forward declare local functions
  214. */
  215. DWORD OpenReconnectKey(PHKEY phKey) ;
  216. DWORD AddToReconnectListEx(LPTSTR pszRemotePath,
  217. LPTSTR pszProviderName,
  218. LPTSTR pszUserContext) ;
  219. DWORD CreateMultiSzValue(PBYTE *ppszzMultiSzValue,
  220. LPDWORD pcbMultiSzValue,
  221. LPTSTR psz1,
  222. LPTSTR psz2) ;
  223. DWORD GetProviderName(LPTSTR pszRemoteName,
  224. LPTSTR *ppszProvider) ;
  225. /*
  226. * Function: AddToReconnectList
  227. * Description: adds the net path to list of print connections to
  228. * restore (saved in registry). calls AddToReconnectListEx
  229. * to do the real work.
  230. * Parameters: pszRemotePath - the path to save.
  231. * Returns: 0 if success, Win32 error otherwise
  232. */
  233. DWORD AddToReconnectList(LPTSTR pszRemotePath)
  234. {
  235. LPTSTR pszProvider ;
  236. DWORD err ;
  237. //
  238. // get the provider name corresponding to this remote path.
  239. //
  240. if ((err = GetProviderName(pszRemotePath, &pszProvider)) == ERROR_SUCCESS)
  241. {
  242. err = AddToReconnectListEx(pszRemotePath,
  243. pszProvider,
  244. NULL) ; // printman doesnt do connect as
  245. LocalFree(pszProvider) ;
  246. }
  247. return err ;
  248. }
  249. /*
  250. * Function: AddToReconnectListEx
  251. * Description: adds the netpath, providername, usercontext to list
  252. * of print connections to restore (saved in registry).
  253. * Parameters: pszRemotePath - the path to save (cannot be NULL)
  254. * pszProviderName - network provider to use (cannot be NULL)
  255. * pszUserContext - what user context (can be NULL)
  256. * Returns: 0 if success, Win32 error otherwise
  257. */
  258. DWORD AddToReconnectListEx(LPTSTR pszRemotePath,
  259. LPTSTR pszProviderName,
  260. LPTSTR pszUserContext)
  261. {
  262. BYTE *pszzMultiSzValue = NULL ;
  263. UINT cbMultiSzValue = 0 ;
  264. HKEY hKey ;
  265. DWORD err ;
  266. //
  267. // check parameters
  268. //
  269. if (!pszRemotePath || !*pszRemotePath)
  270. return ERROR_INVALID_PARAMETER ;
  271. if (!pszProviderName || !*pszProviderName)
  272. return ERROR_INVALID_PARAMETER ;
  273. //
  274. // open registry and create the MULTI_SZ
  275. //
  276. if (err = OpenReconnectKey(&hKey))
  277. return (err) ;
  278. if (err = CreateMultiSzValue(&pszzMultiSzValue,
  279. &cbMultiSzValue,
  280. pszProviderName,
  281. pszUserContext))
  282. {
  283. RegCloseKey(hKey) ;
  284. return err ;
  285. }
  286. //
  287. // set it!
  288. //
  289. err = RegSetValueEx(hKey,
  290. pszRemotePath,
  291. 0,
  292. REG_MULTI_SZ,
  293. pszzMultiSzValue,
  294. cbMultiSzValue) ;
  295. LocalFree( (HLOCAL) pszzMultiSzValue ) ;
  296. RegCloseKey(hKey) ;
  297. return err ;
  298. }
  299. /*
  300. * Function: RemoveFromReconnectList
  301. * Description: removes netpath from the list of print connections to
  302. * restore (saved in registry).
  303. * Parameters: pszRemotePath - the path to remove
  304. * Returns: 0 if success, Win32 error otherwise
  305. */
  306. DWORD RemoveFromReconnectList(LPTSTR pszRemotePath)
  307. {
  308. HKEY hKey ;
  309. DWORD err ;
  310. if (err = OpenReconnectKey(&hKey))
  311. return (err) ;
  312. err = RegDeleteValue(hKey,
  313. pszRemotePath) ;
  314. RegCloseKey(hKey) ;
  315. return err ;
  316. }
  317. /*
  318. * Function: OpenReconectKey
  319. * Description: opens the portion of registry containing the
  320. * print reconnect info.
  321. * Parameters: phKey - address to return the opened key.
  322. * Returns: 0 if success, Win32 error otherwise
  323. */
  324. DWORD OpenReconnectKey(PHKEY phKey)
  325. {
  326. DWORD err ;
  327. if (!phKey)
  328. return ERROR_INVALID_PARAMETER ;
  329. err = RegCreateKey(HKEY_CURRENT_USER,
  330. SZ_PRINTER_RECONNECTIONS,
  331. phKey) ;
  332. if (err != ERROR_SUCCESS)
  333. *phKey = 0 ;
  334. return err ;
  335. }
  336. /*
  337. * Function: CreateMultiSzValue
  338. * Description: creates a MULTI_SZ value from two strings.
  339. * allocates memory with LocalAlloc for the multi_sz string.
  340. * caller should free this.
  341. * Parameters: ppszzMultiSzValue - used to return the multi_sz
  342. * pcbMultiSzValue - used to return number of bytes used
  343. * psz1 - first string (must be non empty string)
  344. * psz2 - second string
  345. * Returns: 0 if success, Win32 error otherwise
  346. */
  347. DWORD CreateMultiSzValue(PBYTE *ppszzMultiSzValue,
  348. LPDWORD pcbMultiSzValue,
  349. LPTSTR psz1,
  350. LPTSTR psz2)
  351. {
  352. DWORD cch1, cch2 ;
  353. LPTSTR pszTmp ;
  354. //
  355. // figure out the size needed
  356. //
  357. cch1 = psz1 ? _tcslen(psz1) : 0 ;
  358. if (cch1 == 0)
  359. return ERROR_INVALID_PARAMETER ;
  360. if (!psz2)
  361. psz2 = TEXT("") ;
  362. cch2 = _tcslen(psz2) ;
  363. //
  364. // allocate the string
  365. //
  366. *pcbMultiSzValue = (cch1 + 1 +
  367. cch2 + 1 +
  368. 1 ) * sizeof(TCHAR) ;
  369. if (!(pszTmp = (LPTSTR) LocalAlloc(LPTR, *pcbMultiSzValue)))
  370. return ERROR_NOT_ENOUGH_MEMORY ;
  371. //
  372. //
  373. //
  374. *ppszzMultiSzValue = (PBYTE)pszTmp ;
  375. _tcscpy(pszTmp, psz1) ;
  376. pszTmp += (cch1 + 1) ;
  377. _tcscpy(pszTmp, psz2) ;
  378. pszTmp += (cch2 + 1) ;
  379. *pszTmp = 0 ;
  380. return ERROR_SUCCESS ;
  381. }
  382. /*
  383. * Function: GetProviderName
  384. * Description: from a connected remote path, find what provider has it.
  385. * LocalAlloc is called to allocate the return data.
  386. * caller should free this.
  387. * Parameters: pszRemotePath - the remote path of interest.
  388. * ppszProvider - used to return pointer to allocated string
  389. * Returns: 0 is success, Win32 error otherwise
  390. */
  391. DWORD GetProviderName(LPTSTR pszRemoteName, LPTSTR *ppszProvider)
  392. {
  393. DWORD err ;
  394. DWORD cEntries ;
  395. DWORD dwBufferSize ;
  396. BYTE *lpBuffer ;
  397. HANDLE hEnum = 0 ;
  398. if (!pszRemoteName)
  399. return ERROR_INVALID_PARAMETER ;
  400. if (!pfnWNetOpenEnum || !pfnWNetEnumResource || !pfnWNetCloseEnum)
  401. return ERROR_PATH_NOT_FOUND ;
  402. //
  403. // init the return pointer to NULL and open up the enumeration
  404. //
  405. *ppszProvider = NULL ;
  406. err = (*pfnWNetOpenEnum)(RESOURCE_CONNECTED,
  407. RESOURCETYPE_PRINT,
  408. 0,
  409. NULL,
  410. &hEnum) ;
  411. if (err != WN_SUCCESS)
  412. return err ;
  413. //
  414. // setup the return buufer and call the enum.
  415. // we always try for as many as we can.
  416. //
  417. cEntries = 0xFFFFFFFF ;
  418. dwBufferSize = 4096 ;
  419. lpBuffer = LocalAlloc(LPTR,
  420. dwBufferSize) ;
  421. if (!lpBuffer)
  422. {
  423. (void) (*pfnWNetCloseEnum)(hEnum) ;
  424. return (GetLastError()) ;
  425. }
  426. err = (*pfnWNetEnumResource)(hEnum,
  427. &cEntries,
  428. lpBuffer,
  429. &dwBufferSize ) ;
  430. do
  431. {
  432. switch(err)
  433. {
  434. case NO_ERROR:
  435. {
  436. DWORD i ;
  437. LPNETRESOURCE lpNetResource = (LPNETRESOURCE) lpBuffer ;
  438. LPTSTR pszProvider ;
  439. for (i = 0; i < cEntries ; i++, lpNetResource++)
  440. {
  441. if (lstrcmpi(lpNetResource->lpRemoteName, pszRemoteName)==0)
  442. {
  443. //
  444. // found one. the first will do.
  445. //
  446. if (!(lpNetResource->lpProvider))
  447. {
  448. //
  449. // no provider string, pretend we didnt find it
  450. //
  451. (void) (*pfnWNetCloseEnum)(hEnum) ;
  452. LocalFree( (HLOCAL) lpBuffer ) ;
  453. return(ERROR_PATH_NOT_FOUND) ;
  454. }
  455. else
  456. {
  457. //
  458. // have provider string
  459. //
  460. pszProvider = (LPTSTR) LocalAlloc( LPTR,
  461. (_tcslen(lpNetResource->lpProvider)+1) *
  462. sizeof(TCHAR)) ;
  463. if (!pszProvider)
  464. {
  465. err = GetLastError() ;
  466. (void) (*pfnWNetCloseEnum)(hEnum) ;
  467. LocalFree( (HLOCAL) lpBuffer ) ;
  468. return(err) ;
  469. }
  470. }
  471. _tcscpy(pszProvider, lpNetResource->lpProvider) ;
  472. (void) (*pfnWNetCloseEnum)(hEnum) ;
  473. LocalFree( (HLOCAL) lpBuffer ) ;
  474. *ppszProvider = pszProvider ;
  475. return NO_ERROR ;
  476. }
  477. }
  478. cEntries = 0xFFFFFFFF ;
  479. err = (*pfnWNetEnumResource)(hEnum,
  480. &cEntries,
  481. lpBuffer,
  482. &dwBufferSize ) ;
  483. break ;
  484. }
  485. case WN_NO_MORE_ENTRIES:
  486. break ;
  487. default:
  488. break ;
  489. }
  490. } while (err == NO_ERROR) ;
  491. (void) (*pfnWNetCloseEnum)(hEnum) ;
  492. LocalFree( (HLOCAL) lpBuffer ) ;
  493. return ERROR_PATH_NOT_FOUND ; // all other error map to this
  494. }