Leaked source code of windows server 2003
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.

870 lines
21 KiB

  1. /*++
  2. Copyright (C) 1992-98 Microsft Corporation. All rights reserved.
  3. Module Name:
  4. cstmdlg.c
  5. Abstract:
  6. Contains the code to call the custom Entrypoints corresponding
  7. to RasCustomDialDlg and RasCustomEntryDlg.
  8. Author:
  9. Rao Salapaka (raos) 09-Jan-1998
  10. Revision History:
  11. Rao Salapaka (raos) 07-Mar-2000 Added DwCustomTerminalDlg for scripting
  12. changes.
  13. --*/
  14. #include <windows.h> // Win32 root
  15. #include <debug.h> // Trace/Assert library
  16. #include <pbk.h> // Heap macros
  17. #include <rasdlg.h> // RAS common dialog
  18. /*++
  19. Routine Description:
  20. Loads entrypoints for rasman and rasapi32 dlls.
  21. Arguments:
  22. void
  23. Return Value:
  24. ERROR_SUCCESS if successful
  25. --*/
  26. DWORD
  27. DwInitializeCustomDlg()
  28. {
  29. DWORD dwErr;
  30. dwErr = LoadRasapi32Dll();
  31. if(ERROR_SUCCESS != dwErr)
  32. {
  33. TRACE1("Failed to load rasapi32.dll. %d",
  34. dwErr);
  35. goto done;
  36. }
  37. dwErr = LoadRasmanDll();
  38. if(ERROR_SUCCESS != dwErr)
  39. {
  40. TRACE1("Failed to load rasman.dll. %d",
  41. dwErr);
  42. goto done;
  43. }
  44. done:
  45. return dwErr;
  46. }
  47. /*++
  48. Routine Description:
  49. Loads the custom dll if specified in the ras phonebook
  50. entry and Gets the entry point specified by the dwFnId.
  51. The possible entry points retrieved are RasCustomDialDlg
  52. and RasCustomEntryDlg.
  53. Arguments:
  54. lpszPhonebook - The phonebook path to be used to look for
  55. the entry. This is not allowed to be NULL.
  56. lpszEntry - The EntryName to be used. This is not allowed
  57. to be NULL.
  58. pfCustomDllSpecified - Buffer to hold a BOOL indicating if
  59. a custom dll was specified for the
  60. entry.
  61. pfnCustomEntryPoint - Buffer to hold the address of the
  62. Custom Entry Point.
  63. phInstDll - Buffer to hold the HINSTANCE of the loaded
  64. dll.
  65. dwFnId - Function Id indicating which Entry Point to load.
  66. The possible values are CUSTOM_RASDIALDLG and
  67. CUSTOM_RASENTRYDLG.
  68. Return Value:
  69. ERROR_SUCCESS if successful
  70. --*/
  71. DWORD
  72. DwGetCustomDllEntryPoint(
  73. LPCTSTR lpszPhonebook,
  74. LPCTSTR lpszEntry,
  75. BOOL *pfCustomDllSpecified,
  76. FARPROC *pfnCustomEntryPoint,
  77. HINSTANCE *phInstDll,
  78. DWORD dwFnId,
  79. LPTSTR pszCustomDialerName
  80. )
  81. {
  82. DWORD dwErr = ERROR_SUCCESS;
  83. DWORD dwSize = sizeof(RASENTRY);
  84. LPTSTR pszExpandedPath = NULL;
  85. BOOL fTrusted = FALSE;
  86. CHAR *apszFn[] = {
  87. "RasCustomDialDlg",
  88. "RasCustomEntryDlg",
  89. "RasCustomDial",
  90. "RasCustomDeleteEntryNotify"
  91. };
  92. TRACE("DwGetCustomDllEntryPoints..");
  93. //
  94. // Initialize. This will load rasman and
  95. // rasapi32 dlls
  96. //
  97. dwErr = DwInitializeCustomDlg();
  98. if(ERROR_SUCCESS != dwErr)
  99. {
  100. TRACE1("Failed to load Initialize. %d",
  101. dwErr);
  102. goto done;
  103. }
  104. //
  105. // Initialize Out parameters
  106. //
  107. *pfnCustomEntryPoint = NULL;
  108. if(pfCustomDllSpecified)
  109. {
  110. *pfCustomDllSpecified = FALSE;
  111. }
  112. if(NULL == pszCustomDialerName)
  113. {
  114. RASENTRY re;
  115. RASENTRY *pre = &re;
  116. ZeroMemory(&re, sizeof(RASENTRY));
  117. //
  118. // Get the entry properties if customdialer is
  119. // not specified
  120. //
  121. re.dwSize = sizeof(RASENTRY);
  122. dwErr = g_pRasGetEntryProperties(
  123. lpszPhonebook,
  124. lpszEntry,
  125. &re,
  126. &dwSize,
  127. NULL,
  128. NULL);
  129. if(ERROR_BUFFER_TOO_SMALL == dwErr)
  130. {
  131. pre = (RASENTRY *) LocalAlloc(LPTR, dwSize);
  132. if(NULL == pre)
  133. {
  134. dwErr = GetLastError();
  135. goto done;
  136. }
  137. pre->dwSize = sizeof(RASENTRY);
  138. dwErr = g_pRasGetEntryProperties(
  139. lpszPhonebook,
  140. lpszEntry,
  141. pre,
  142. &dwSize,
  143. NULL,
  144. NULL);
  145. }
  146. if( (ERROR_SUCCESS != dwErr)
  147. || (TEXT('\0') == pre->szCustomDialDll[0]))
  148. {
  149. if(pre != &re)
  150. {
  151. LocalFree(pre);
  152. }
  153. goto done;
  154. }
  155. pszCustomDialerName = pre->szCustomDialDll;
  156. }
  157. if(pfCustomDllSpecified)
  158. {
  159. *pfCustomDllSpecified = TRUE;
  160. }
  161. //
  162. // Check to see if this dll is a trusted dll
  163. //
  164. dwErr = RasIsTrustedCustomDll(
  165. NULL,
  166. pszCustomDialerName,
  167. &fTrusted);
  168. if(!fTrusted)
  169. {
  170. dwErr = ERROR_ACCESS_DENIED;
  171. goto done;
  172. }
  173. //
  174. // Expand the path in case it has environment variables.
  175. //
  176. dwErr = DwGetExpandedDllPath(pszCustomDialerName,
  177. &pszExpandedPath);
  178. if(ERROR_SUCCESS != dwErr)
  179. {
  180. goto done;
  181. }
  182. //
  183. // Load the dll
  184. //
  185. if (NULL == (*phInstDll = LoadLibrary(
  186. pszExpandedPath
  187. )))
  188. {
  189. dwErr = GetLastError();
  190. TRACE2("LoadLibrary %ws failed. %d",
  191. pszCustomDialerName,
  192. dwErr);
  193. goto done;
  194. }
  195. //
  196. // Get the custom entrypoint
  197. //
  198. if(NULL == (*pfnCustomEntryPoint = GetProcAddress(
  199. *phInstDll,
  200. apszFn[dwFnId])))
  201. {
  202. dwErr = GetLastError();
  203. TRACE2("GetProcAddress %s failed. %d",
  204. apszFn[dwFnId],
  205. dwErr);
  206. goto done;
  207. }
  208. done:
  209. if( ERROR_SUCCESS != dwErr
  210. && *phInstDll)
  211. {
  212. FreeLibrary(*phInstDll);
  213. *phInstDll = NULL;
  214. }
  215. if(NULL != pszExpandedPath)
  216. {
  217. LocalFree(pszExpandedPath);
  218. }
  219. TRACE1("DwGetCustomDllEntryPoints done. %d",
  220. dwErr);
  221. return dwErr;
  222. }
  223. DWORD
  224. DwGetEntryMode(LPCTSTR lpszPhonebook,
  225. LPCTSTR lpszEntry,
  226. PBFILE *pFileIn,
  227. DWORD *pdwFlags)
  228. {
  229. DWORD dwErr = ERROR_SUCCESS;
  230. PBFILE file;
  231. PBFILE *pFile;
  232. DTLNODE *pdtlnode = NULL;
  233. DWORD dwFlags = 0;
  234. if(NULL == pdwFlags)
  235. {
  236. dwErr = E_INVALIDARG;
  237. goto done;
  238. }
  239. if(NULL != pFileIn)
  240. {
  241. pFile = pFileIn;
  242. }
  243. else
  244. {
  245. pFile = &file;
  246. }
  247. if(NULL == lpszPhonebook)
  248. {
  249. dwErr = GetPbkAndEntryName(lpszPhonebook,
  250. lpszEntry,
  251. 0,
  252. pFile,
  253. &pdtlnode);
  254. if(ERROR_SUCCESS != dwErr)
  255. {
  256. goto done;
  257. }
  258. if(IsPublicPhonebook(pFile->pszPath))
  259. {
  260. dwFlags |= REN_AllUsers;
  261. }
  262. if(NULL == pFileIn)
  263. {
  264. ClosePhonebookFile(pFile);
  265. }
  266. }
  267. else if(IsPublicPhonebook(lpszPhonebook))
  268. {
  269. dwFlags |= REN_AllUsers;
  270. }
  271. *pdwFlags = dwFlags;
  272. done:
  273. return dwErr;
  274. }
  275. /*++
  276. Routine Description:
  277. Invokes the custom dial dlg if such a dialog is specified
  278. in the phonebook entry.
  279. Arguments:
  280. lpszPhonebook - Semantics of this is the same as in the
  281. win32 api RasDialDlg.
  282. lpszEntry - Semantics of this is the same as in the win32
  283. api RasDialDlg.
  284. lpPhoneNumber - Semantics of this is the same as in the
  285. win32 api RasDialDlg.
  286. lpInfo - Semantics of this is the same as in the win32
  287. api RasDialDlg.
  288. pfStatus - Buffer to hold the result of calling the custom
  289. Dial Dlg. The semantics of the value stored is
  290. the same as the return value of the win32 api
  291. RasDialdlg.
  292. Return Value:
  293. ERROR_SUCCESS if successful
  294. --*/
  295. DWORD
  296. DwCustomDialDlg(
  297. LPTSTR lpszPhonebook,
  298. LPTSTR lpszEntry,
  299. LPTSTR lpszPhoneNumber,
  300. LPRASDIALDLG lpInfo,
  301. DWORD dwFlags,
  302. BOOL *pfStatus,
  303. PVOID pvInfo,
  304. LPTSTR pszCustomDialerName)
  305. {
  306. DWORD dwErr = ERROR_SUCCESS;
  307. HINSTANCE hInstDll = NULL;
  308. BOOL fCustomDll;
  309. RasCustomDialDlgFn pfnRasCustomDialDlg = NULL;
  310. CHAR *pszPhonebookA = NULL;
  311. CHAR *pszEntryNameA = NULL;
  312. PBFILE file;
  313. DTLNODE *pdtlnode = NULL;
  314. DWORD dwEntryMode = 0;
  315. TRACE("DwCustomDialDlg..");
  316. if( NULL == lpszEntry
  317. || TEXT('\0') == lpszEntry[0])
  318. {
  319. dwErr = E_NOINTERFACE;
  320. goto done;
  321. }
  322. //
  323. // Load rasman and rasapi32 dlls
  324. //
  325. dwErr = DwInitializeCustomDlg();
  326. if(ERROR_SUCCESS != dwErr)
  327. {
  328. lpInfo->dwError = dwErr;
  329. *pfStatus = FALSE;
  330. dwErr = ERROR_SUCCESS;
  331. goto done;
  332. }
  333. //
  334. // Get the EntryPoint
  335. //
  336. dwErr = DwGetCustomDllEntryPoint(
  337. lpszPhonebook,
  338. lpszEntry,
  339. &fCustomDll,
  340. (FARPROC *) &pfnRasCustomDialDlg,
  341. &hInstDll,
  342. CUSTOM_RASDIALDLG,
  343. pszCustomDialerName);
  344. if( ERROR_SUCCESS != dwErr
  345. && fCustomDll)
  346. {
  347. //
  348. // Custom dll was specified for this
  349. // entry but something else failed.
  350. //
  351. lpInfo->dwError = dwErr;
  352. *pfStatus = FALSE;
  353. dwErr = ERROR_SUCCESS;
  354. goto done;
  355. }
  356. else if (!fCustomDll)
  357. {
  358. dwErr = E_NOINTERFACE;
  359. goto done;
  360. }
  361. ASSERT(NULL != pfnRasCustomDialDlg);
  362. dwErr = DwGetEntryMode(lpszPhonebook,
  363. lpszEntry,
  364. &file,
  365. &dwEntryMode);
  366. if(ERROR_SUCCESS != dwErr)
  367. {
  368. lpInfo->dwError = dwErr;
  369. *pfStatus = FALSE;
  370. dwErr = ERROR_SUCCESS;
  371. goto done;
  372. }
  373. dwFlags |= dwEntryMode;
  374. pszPhonebookA = StrDupAFromT((NULL == lpszPhonebook)
  375. ? file.pszPath
  376. : lpszPhonebook);
  377. if(NULL == lpszPhonebook)
  378. {
  379. ClosePhonebookFile(&file);
  380. }
  381. pszEntryNameA = StrDupAFromT(lpszEntry);
  382. if( NULL == pszPhonebookA
  383. || NULL == pszEntryNameA)
  384. {
  385. *pfStatus = FALSE;
  386. lpInfo->dwError = ERROR_NOT_ENOUGH_MEMORY;
  387. dwErr = ERROR_SUCCESS;
  388. goto done;
  389. }
  390. //
  391. // Call the entry point. After this point dwErr
  392. // returned should always be ERROR_SUCCESS, Since
  393. // this means CustomDialDlg was handled and any
  394. // error will be returned via the fStatus and
  395. // lpInfo->dwError pair.
  396. //
  397. *pfStatus = (pfnRasCustomDialDlg) (
  398. hInstDll,
  399. dwFlags,
  400. lpszPhonebook,
  401. lpszEntry,
  402. lpszPhoneNumber,
  403. lpInfo,
  404. pvInfo);
  405. if(*pfStatus)
  406. {
  407. //
  408. // Mark this connection as being connected
  409. // through custom dialing
  410. //
  411. dwErr = g_pRasReferenceCustomCount((HCONN) NULL,
  412. TRUE,
  413. pszPhonebookA,
  414. pszEntryNameA,
  415. NULL);
  416. if(ERROR_SUCCESS != dwErr)
  417. {
  418. TRACE1("RasReferenceCustomCount failed. %d",
  419. dwErr);
  420. *pfStatus = FALSE;
  421. lpInfo->dwError = dwErr;
  422. dwErr = ERROR_SUCCESS;
  423. goto done;
  424. }
  425. }
  426. done:
  427. if(NULL != hInstDll)
  428. {
  429. FreeLibrary(hInstDll);
  430. }
  431. if(NULL != pszPhonebookA)
  432. {
  433. Free(pszPhonebookA);
  434. }
  435. if(NULL != pszEntryNameA)
  436. {
  437. Free(pszEntryNameA);
  438. }
  439. TRACE1("DwCustomDialDlg done. %d",
  440. dwErr);
  441. return dwErr;
  442. }
  443. /*++
  444. Routine Description:
  445. Invokes the Custom EntryDlg if such a dialog is specified
  446. in the RasPhonebookEntry
  447. Arguments:
  448. lpszPhonebook - Semantics of this is the same as the win32
  449. api RasEntryDlg
  450. lpszEntry - Semantics of this is the same as the win32 api
  451. RasEntryDlg
  452. lpInfo - Semantics of this is the same as the win32 api
  453. RasEntryDlg.
  454. pfStatus - Buffer to hold the result of calling the custom
  455. Entry Point. The semantics of the value returned
  456. in this buffer is the same as the return value
  457. of the win32 api RasEntryDlg.
  458. Return Value:
  459. ERROR_SUCCESS if successful
  460. --*/
  461. DWORD
  462. DwCustomEntryDlg(
  463. LPTSTR lpszPhonebook,
  464. LPTSTR lpszEntry,
  465. LPRASENTRYDLG lpInfo,
  466. BOOL *pfStatus)
  467. {
  468. DWORD dwErr = ERROR_SUCCESS;
  469. HINSTANCE hInstDll = NULL;
  470. BOOL fCustomDll = FALSE;
  471. RasCustomEntryDlgFn pfnRasCustomEntryDlg = NULL;
  472. DWORD dwFlags = REN_User;
  473. TRACE("DwCustomEntryDlg..");
  474. if( NULL == lpszEntry
  475. || TEXT('\0') == lpszEntry[0])
  476. {
  477. dwErr = E_NOINTERFACE;
  478. goto done;
  479. }
  480. //
  481. // Get the EntryPoint
  482. //
  483. dwErr = DwGetCustomDllEntryPoint(
  484. lpszPhonebook,
  485. lpszEntry,
  486. &fCustomDll,
  487. (FARPROC *) &pfnRasCustomEntryDlg,
  488. &hInstDll,
  489. CUSTOM_RASENTRYDLG,
  490. NULL);
  491. if( ERROR_SUCCESS != dwErr
  492. && fCustomDll)
  493. {
  494. //
  495. // Custom dll was specified for this
  496. // entry but something else failed.
  497. //
  498. lpInfo->dwError = dwErr;
  499. *pfStatus = FALSE;
  500. dwErr = ERROR_SUCCESS;
  501. goto done;
  502. }
  503. else if (!fCustomDll)
  504. {
  505. dwErr = E_NOINTERFACE;
  506. goto done;
  507. }
  508. ASSERT(NULL != pfnRasCustomEntryDlg);
  509. dwErr = DwGetEntryMode(lpszPhonebook,
  510. lpszEntry,
  511. NULL,
  512. &dwFlags);
  513. if(ERROR_SUCCESS != dwErr)
  514. {
  515. //
  516. // Custom dll was specified for this
  517. // entry but something else failed.
  518. //
  519. lpInfo->dwError = dwErr;
  520. *pfStatus = FALSE;
  521. dwErr = ERROR_SUCCESS;
  522. goto done;
  523. }
  524. //
  525. // Call the entry point. After this point dwErr
  526. // returned should always be ERROR_SUCCESS, Since
  527. // this means CustomDialDlg was handled and any
  528. // error will be returned via the fStatus and
  529. // lpInfo->dwError pair.
  530. //
  531. *pfStatus = (pfnRasCustomEntryDlg) (
  532. hInstDll,
  533. lpszPhonebook,
  534. lpszEntry,
  535. lpInfo,
  536. dwFlags);
  537. done:
  538. if(NULL != hInstDll)
  539. {
  540. FreeLibrary(hInstDll);
  541. }
  542. TRACE1("DwCustomEntryDlg done. %d",
  543. dwErr);
  544. return dwErr;
  545. }
  546. DWORD
  547. DwCustomDeleteEntryNotify(
  548. LPCTSTR pszPhonebook,
  549. LPCTSTR pszEntry,
  550. LPTSTR pszCustomDialerName)
  551. {
  552. DWORD dwErr = NO_ERROR;
  553. RasCustomDeleteEntryNotifyFn pfnCustomDeleteEntryNotify;
  554. HINSTANCE hInstDll = NULL;
  555. BOOL fCustomDll;
  556. DWORD dwFlags = 0;
  557. if( NULL == pszEntry
  558. || TEXT('\0') == pszEntry[0])
  559. {
  560. dwErr = E_INVALIDARG;
  561. goto done;
  562. }
  563. //
  564. // Get the EntryPoint
  565. //
  566. dwErr = DwGetCustomDllEntryPoint(
  567. pszPhonebook,
  568. pszEntry,
  569. &fCustomDll,
  570. (FARPROC *) &pfnCustomDeleteEntryNotify,
  571. &hInstDll,
  572. CUSTOM_RASDELETEENTRYNOTIFY,
  573. pszCustomDialerName);
  574. if(NO_ERROR != dwErr)
  575. {
  576. goto done;
  577. }
  578. dwErr = DwGetEntryMode(pszPhonebook,
  579. pszEntry,
  580. NULL,
  581. &dwFlags);
  582. if(NO_ERROR != dwErr)
  583. {
  584. goto done;
  585. }
  586. dwErr = pfnCustomDeleteEntryNotify(
  587. pszPhonebook,
  588. pszEntry,
  589. dwFlags);
  590. done:
  591. if(NULL != hInstDll)
  592. {
  593. FreeLibrary(hInstDll);
  594. }
  595. return dwErr;
  596. }
  597. DWORD
  598. DwCustomTerminalDlg(TCHAR *pszPhonebook,
  599. HRASCONN hrasconn,
  600. PBENTRY *pEntry,
  601. HWND hwndDlg,
  602. RASDIALPARAMS *prdp,
  603. PVOID pvReserved)
  604. {
  605. DWORD retcode = SUCCESS;
  606. HPORT hport;
  607. CHAR szCustomScriptDll[MAX_PATH + 1];
  608. HINSTANCE hInst = NULL;
  609. RasCustomScriptExecuteFn fnCustomScript;
  610. RASCUSTOMSCRIPTEXTENSIONS rcse;
  611. hport = g_pRasGetHport(hrasconn);
  612. if(INVALID_HPORT == hport)
  613. {
  614. TRACE("DwCustomTermianlDlg: RasGetHport retured INVALID_HPORT");
  615. retcode = ERROR_INVALID_PORT_HANDLE;
  616. goto done;
  617. }
  618. //
  619. // Get the custom script dll from rasman
  620. //
  621. retcode = RasGetCustomScriptDll(szCustomScriptDll);
  622. if(ERROR_SUCCESS != retcode)
  623. {
  624. TRACE1(
  625. "DwCustomTerminalDlg: RasGetCustomScriptDll "
  626. "returned 0x%x",
  627. retcode);
  628. goto done;
  629. }
  630. //
  631. // Load the 3rd party dll
  632. //
  633. hInst = LoadLibraryA(szCustomScriptDll);
  634. if(NULL == hInst)
  635. {
  636. retcode = GetLastError();
  637. TRACE2(
  638. "DwCustomTerminalDlg: couldn't load %s. 0x%x",
  639. szCustomScriptDll,
  640. retcode);
  641. goto done;
  642. }
  643. //
  644. // Get the exported function pointer
  645. //
  646. fnCustomScript = (RasCustomScriptExecuteFn) GetProcAddress(
  647. hInst,
  648. "RasCustomScriptExecute");
  649. if(NULL == fnCustomScript)
  650. {
  651. retcode = GetLastError();
  652. TRACE1(
  653. "DwCustomTerminalDlg: GetprocAddress failed 0x%x",
  654. retcode);
  655. goto done;
  656. }
  657. ZeroMemory(&rcse, sizeof(RASCUSTOMSCRIPTEXTENSIONS));
  658. rcse.dwSize = sizeof(RASCUSTOMSCRIPTEXTENSIONS);
  659. rcse.pfnRasSetCommSettings = RasSetCommSettings;
  660. //
  661. // Call the function
  662. //
  663. retcode = (DWORD) fnCustomScript(
  664. hport,
  665. pszPhonebook,
  666. pEntry->pszEntryName,
  667. g_pRasGetBuffer,
  668. g_pRasFreeBuffer,
  669. g_pRasPortSend,
  670. g_pRasPortReceive,
  671. g_pRasPortReceiveEx,
  672. hwndDlg,
  673. prdp,
  674. &rcse);
  675. TRACE1(
  676. "DwCustomTerminalDlg: fnCustomScript returned 0x%x",
  677. retcode);
  678. done:
  679. if(NULL != hInst)
  680. {
  681. FreeLibrary(hInst);
  682. }
  683. return retcode;
  684. }