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.

779 lines
19 KiB

  1. extern "C"
  2. {
  3. #include "precomp.h"
  4. #include <netcfgn.h>
  5. #include <netcfgp.h>
  6. #include <netcfgx.h>
  7. HRESULT
  8. HrInstallRas(
  9. IN CONST PWCHAR pszFilePath);
  10. HRESULT
  11. HrUninstallRas();
  12. VOID
  13. HrShowNetComponentsAll(
  14. IN BUFFER_WRITE_FILE* pBuff);
  15. VOID
  16. HrValidateRas(
  17. IN BUFFER_WRITE_FILE* pBuff);
  18. PWCHAR
  19. RasDiagVerifyAnswerFile(
  20. IN CONST PWCHAR pwszFilePath);
  21. }
  22. #define ASYCMINI L"sw\\{eeab7790-c514-11d1-b42b-00805fc1270e}"
  23. #define IRDAMINI L"ms_irdaminiport"
  24. #define IRMDMINI L"ms_irmodemminiport"
  25. #define L2TPMINI L"ms_l2tpminiport"
  26. #define PPTPMINI L"ms_pptpminiport"
  27. #define PTIMINI L"ms_ptiminiport"
  28. #define P3OEMINI L"ms_pppoeminiport"
  29. #define NDSNATLK L"ms_ndiswanatalk"
  30. #define NDWNNETM L"ms_ndiswanbh"
  31. #define NDWNTCP L"ms_ndiswanip"
  32. #define NDWNIPX L"ms_ndiswanipx"
  33. //#define NDWNNBFI L"ms_ndiswannbfin"
  34. //#define NDWNNBDO L"ms_ndiswannbfout"
  35. #define MSPPPOE L"ms_pppoe"
  36. #define MSPPTP L"ms_pptp"
  37. #define MSL2TP L"ms_l2tp"
  38. #define RASCLINT L"ms_rascli"
  39. #define RASSRV L"ms_rassrv"
  40. #define STEELHED L"ms_steelhead"
  41. //#define APPLAYER L"ms_alg"
  42. #define NDISWAN L"ms_ndiswan"
  43. #define RASMAN L"ms_rasman"
  44. typedef enum _NetClass
  45. {
  46. NC_NetAdapter = 0,
  47. NC_NetProtocol,
  48. NC_NetService,
  49. NC_NetClient,
  50. NC_Unknown
  51. } NetClass;
  52. typedef struct _RAS_COMP_IDS
  53. {
  54. PWCHAR szComponentId;
  55. NetClass nc;
  56. } RAS_COMP_IDS;
  57. // Globals
  58. //
  59. static CONST RAS_COMP_IDS c_RasCompIds[] =
  60. {
  61. {ASYCMINI, NC_NetAdapter},
  62. {IRDAMINI, NC_NetAdapter},
  63. {IRMDMINI, NC_NetAdapter},
  64. {L2TPMINI, NC_NetAdapter},
  65. {PPTPMINI, NC_NetAdapter},
  66. {PTIMINI, NC_NetAdapter},
  67. {P3OEMINI, NC_NetAdapter},
  68. {NDSNATLK, NC_NetAdapter},
  69. {NDWNNETM, NC_NetAdapter},
  70. {NDWNTCP, NC_NetAdapter},
  71. {NDWNIPX, NC_NetAdapter},
  72. // {NDWNNBFI, NC_NetAdapter},
  73. // {NDWNNBDO, NC_NetAdapter},
  74. {MSPPPOE, NC_NetProtocol},
  75. {MSPPTP, NC_NetProtocol},
  76. {MSL2TP, NC_NetProtocol},
  77. {RASCLINT, NC_NetService},
  78. {RASSRV, NC_NetService},
  79. {STEELHED, NC_NetService},
  80. // {APPLAYER, NC_NetService},
  81. {NDISWAN, NC_NetProtocol},
  82. {RASMAN, NC_NetService}
  83. };
  84. static CONST UINT g_ulNumRasCompIds = sizeof(c_RasCompIds) /
  85. sizeof(RAS_COMP_IDS);
  86. static CONST GUID* c_aguidClass[] =
  87. {
  88. &GUID_DEVCLASS_NET,
  89. &GUID_DEVCLASS_NETTRANS,
  90. &GUID_DEVCLASS_NETSERVICE,
  91. &GUID_DEVCLASS_NETCLIENT
  92. };
  93. HRESULT
  94. HrInstallNetComponent(
  95. IN INetCfg* pnc,
  96. IN CONST PWCHAR szComponentId,
  97. IN CONST NetClass nc,
  98. IN CONST PWCHAR pszFilePath);
  99. HRESULT
  100. HrUninstallNetComponent(
  101. IN INetCfg* pnc,
  102. IN CONST PWCHAR szComponentId);
  103. HRESULT
  104. HrShowNetComponents(
  105. IN BUFFER_WRITE_FILE* pBuff,
  106. IN INetCfg* pnc,
  107. IN CONST GUID* pguidClass);
  108. HRESULT
  109. HrConfigureComponent(
  110. IN INetCfgComponent* pncc,
  111. IN CONST PWCHAR pszComponentId,
  112. IN CONST NetClass nc,
  113. IN CONST PWCHAR pszAnswerfilePath);
  114. HRESULT
  115. HrGetINetCfg(
  116. IN BOOL fGetWriteLock,
  117. IN INetCfg** ppnc);
  118. HRESULT
  119. HrReleaseINetCfg(
  120. IN BOOL fHasWriteLock,
  121. IN INetCfg* pnc);
  122. inline ULONG ReleaseObj(IUnknown* punk)
  123. {
  124. return (punk) ? punk->Release() : 0;
  125. }
  126. HRESULT
  127. HrInstallRas(
  128. IN CONST PWCHAR pszFilePath)
  129. {
  130. BOOL fOk = FALSE;
  131. HRESULT hr = S_OK;
  132. INetCfg* pnc;
  133. //
  134. // Get INetCfg interface
  135. //
  136. hr = HrGetINetCfg(TRUE, &pnc);
  137. if (SUCCEEDED(hr))
  138. {
  139. // NT_PRODUCT_TYPE ProductType = NtProductServer;
  140. // RtlGetNtProductType(&ProductType);
  141. for (UINT i = 0; i < g_ulNumRasCompIds; i++)
  142. {
  143. if ((c_RasCompIds[i].nc == NC_NetAdapter) &&
  144. (lstrcmp(c_RasCompIds[i].szComponentId, ASYCMINI) != 0)
  145. )
  146. {
  147. continue;
  148. }
  149. /* else if ((ProductType != NtProductWinNt) &&
  150. (//(lstrcmp(c_RasCompIds[i].szComponentId, RASCLINT) == 0)||
  151. (lstrcmp(c_RasCompIds[i].szComponentId, APPLAYER) == 0))
  152. )
  153. {
  154. continue;
  155. }*/
  156. //
  157. // Install szComponentId
  158. //
  159. hr = HrInstallNetComponent(
  160. pnc,
  161. c_RasCompIds[i].szComponentId,
  162. c_RasCompIds[i].nc,
  163. pszFilePath);
  164. if (S_OK == hr)
  165. {
  166. fOk = TRUE;
  167. }
  168. }
  169. //
  170. // Apply the changes
  171. //
  172. if (fOk)
  173. {
  174. hr = pnc->Apply();
  175. }
  176. //
  177. // Release INetCfg
  178. //
  179. (VOID) HrReleaseINetCfg(TRUE, pnc);
  180. }
  181. return hr;
  182. }
  183. HRESULT
  184. HrUninstallRas()
  185. {
  186. BOOL fOk = FALSE;
  187. HRESULT hr = S_OK;
  188. INetCfg* pnc;
  189. //
  190. // Get INetCfg interface
  191. //
  192. hr = HrGetINetCfg(TRUE, &pnc);
  193. if (SUCCEEDED(hr))
  194. {
  195. for (UINT i = 0; i < g_ulNumRasCompIds; i++)
  196. {
  197. if ((c_RasCompIds[i].nc == NC_NetAdapter) &&
  198. (lstrcmp(c_RasCompIds[i].szComponentId, ASYCMINI) != 0)
  199. )
  200. {
  201. continue;
  202. }
  203. //
  204. // Uninstall szComponentId
  205. //
  206. hr = HrUninstallNetComponent(pnc, c_RasCompIds[i].szComponentId);
  207. if (S_OK == hr)
  208. {
  209. fOk = TRUE;
  210. }
  211. }
  212. //
  213. // Apply the changes
  214. //
  215. if (fOk)
  216. {
  217. hr = pnc->Apply();
  218. }
  219. //
  220. // Release INetCfg
  221. //
  222. (VOID) HrReleaseINetCfg(TRUE, pnc);
  223. }
  224. return hr;
  225. }
  226. ///
  227. // Install the specified net component
  228. //
  229. HRESULT
  230. HrInstallNetComponent(
  231. IN INetCfg* pnc,
  232. IN CONST PWCHAR szComponentId,
  233. IN CONST NetClass nc,
  234. IN CONST PWCHAR pszFilePath)
  235. {
  236. HRESULT hr = S_OK;
  237. OBO_TOKEN OboToken;
  238. INetCfgClassSetup* pncClassSetup;
  239. INetCfgComponent* pncc;
  240. //
  241. // OBO_TOKEN specifies the entity on whose behalf this
  242. // component is being installed
  243. //
  244. // set it to OBO_USER so that szComponentId will be installed
  245. // On-Behalf-Of "user"
  246. //
  247. ZeroMemory(&OboToken, sizeof(OboToken));
  248. OboToken.Type = OBO_USER;
  249. hr = pnc->QueryNetCfgClass(
  250. c_aguidClass[nc],
  251. IID_INetCfgClassSetup,
  252. (void**)&pncClassSetup);
  253. if (SUCCEEDED(hr))
  254. {
  255. hr = pncClassSetup->Install(szComponentId,
  256. &OboToken,
  257. NSF_POSTSYSINSTALL,
  258. 0,
  259. NULL,
  260. NULL,
  261. &pncc);
  262. if (S_OK == hr)
  263. {
  264. if (pszFilePath)
  265. {
  266. hr = HrConfigureComponent(pncc, szComponentId, nc, pszFilePath);
  267. }
  268. //
  269. // we dont want to use pncc (INetCfgComponent), release it
  270. //
  271. ReleaseObj(pncc);
  272. }
  273. ReleaseObj(pncClassSetup);
  274. }
  275. return hr;
  276. }
  277. //
  278. // Uninstall the specified component
  279. //
  280. HRESULT
  281. HrUninstallNetComponent(
  282. IN INetCfg* pnc,
  283. IN CONST PWCHAR szComponentId)
  284. {
  285. HRESULT hr = S_OK;
  286. OBO_TOKEN OboToken;
  287. INetCfgComponent* pncc;
  288. GUID guidClass;
  289. INetCfgClass* pncClass;
  290. INetCfgClassSetup* pncClassSetup;
  291. //
  292. // OBO_TOKEN specifies the entity on whose behalf this
  293. // component is being uninstalld
  294. //
  295. // set it to OBO_USER so that szComponentId will be uninstalld
  296. // On-Behalf-Of "user"
  297. //
  298. ZeroMemory (&OboToken, sizeof(OboToken));
  299. OboToken.Type = OBO_USER;
  300. //
  301. // see if the component is really installed
  302. //
  303. hr = pnc->FindComponent(szComponentId, &pncc);
  304. if (S_OK == hr)
  305. {
  306. //
  307. // yes, it is installed. obtain INetCfgClassSetup and DeInstall
  308. //
  309. hr = pncc->GetClassGuid(&guidClass);
  310. if (S_OK == hr)
  311. {
  312. hr = pnc->QueryNetCfgClass(
  313. &guidClass,
  314. IID_INetCfgClass,
  315. (void**)&pncClass);
  316. if (SUCCEEDED(hr))
  317. {
  318. hr = pncClass->QueryInterface(
  319. IID_INetCfgClassSetup,
  320. (void**)&pncClassSetup);
  321. if (SUCCEEDED(hr))
  322. {
  323. hr = pncClassSetup->DeInstall(pncc, &OboToken, NULL);
  324. ReleaseObj(pncClassSetup);
  325. }
  326. ReleaseObj(pncClass);
  327. }
  328. }
  329. ReleaseObj(pncc);
  330. }
  331. return hr;
  332. }
  333. //
  334. // Display the list of installed components of the specified class.
  335. //
  336. HRESULT
  337. HrShowNetComponents(
  338. IN BUFFER_WRITE_FILE* pBuff,
  339. IN INetCfg* pnc,
  340. IN CONST GUID* pguidClass)
  341. {
  342. ULONG celtFetched;
  343. PWSTR szInfId, szDisplayName;
  344. DWORD dwcc;
  345. HRESULT hr = S_OK;
  346. INetCfgClass* pncclass;
  347. INetCfgComponent* pncc;
  348. IEnumNetCfgComponent* pencc;
  349. hr = pnc->QueryNetCfgClass(
  350. pguidClass,
  351. IID_INetCfgClass,
  352. (void**)&pncclass);
  353. if (SUCCEEDED(hr))
  354. {
  355. //
  356. // get IEnumNetCfgComponent so that we can enumerate
  357. //
  358. hr = pncclass->EnumComponents(&pencc);
  359. ReleaseObj(pncclass);
  360. while (SUCCEEDED(hr) &&
  361. (S_OK == (hr = pencc->Next(1, &pncc, &celtFetched))))
  362. {
  363. hr = pncc->GetId(&szInfId);
  364. if (S_OK == hr)
  365. {
  366. hr = pncc->GetDisplayName(&szDisplayName);
  367. if (SUCCEEDED(hr))
  368. {
  369. BufferWriteMessage(
  370. pBuff,
  371. g_hModule,
  372. MSG_RASDIAG_SHOW_NETCOMP,
  373. szInfId,
  374. szDisplayName);
  375. CoTaskMemFree(szDisplayName);
  376. }
  377. CoTaskMemFree(szInfId);
  378. }
  379. //
  380. // we dont want to stop enumeration just because 1 component
  381. // failed either GetId or GetDisplayName, therefore reset hr to S_OK
  382. //
  383. hr = S_OK;
  384. ReleaseObj(pncc);
  385. }
  386. ReleaseObj(pencc);
  387. }
  388. return hr;
  389. }
  390. //
  391. // Display installed net components
  392. //
  393. VOID
  394. HrShowNetComponentsAll(
  395. IN BUFFER_WRITE_FILE* pBuff)
  396. {
  397. HRESULT hr = S_OK;
  398. INetCfg* pnc;
  399. static CONST ULONG c_cClassNames[] =
  400. {
  401. MSG_RASDIAG_SHOW_NETCOMP_NET_HDR,
  402. MSG_RASDIAG_SHOW_NETCOMP_NETTRANS_HDR,
  403. MSG_RASDIAG_SHOW_NETCOMP_NETSERVICE_HDR,
  404. MSG_RASDIAG_SHOW_NETCOMP_NETCLIENT_HDR,
  405. };
  406. static CONST UINT ulNumClasses = sizeof(c_cClassNames) / sizeof(ULONG);
  407. //
  408. // Get INetCfg interface
  409. //
  410. hr = HrGetINetCfg(FALSE, &pnc);
  411. if (SUCCEEDED(hr))
  412. {
  413. for (UINT i = 0 ; i < ulNumClasses; i++)
  414. {
  415. BufferWriteMessage(pBuff, g_hModule, c_cClassNames[i]);
  416. (VOID) HrShowNetComponents(pBuff, pnc, c_aguidClass[i]);
  417. }
  418. //
  419. // Release INetCfg
  420. //
  421. (VOID) HrReleaseINetCfg(FALSE, pnc);
  422. }
  423. if (FAILED(hr))
  424. {
  425. BufferWriteMessage(pBuff, g_hModule, EMSG_RASDIAG_SHOW_RASCHK_NETCOMP);
  426. }
  427. return;
  428. }
  429. //
  430. // Display installed ras components
  431. //
  432. VOID
  433. HrValidateRas(
  434. IN BUFFER_WRITE_FILE* pBuff)
  435. {
  436. HRESULT hr = S_OK;
  437. INetCfg* pnc;
  438. //
  439. // Get INetCfg interface
  440. //
  441. hr = HrGetINetCfg(TRUE, &pnc);
  442. if (SUCCEEDED(hr))
  443. {
  444. INetCfgComponent* pncc;
  445. // NT_PRODUCT_TYPE ProductType = NtProductServer;
  446. // RtlGetNtProductType(&ProductType);
  447. for (UINT i = 0; i < g_ulNumRasCompIds; i++)
  448. {
  449. //
  450. // see if the component is really installed
  451. //
  452. BufferWriteMessage(
  453. pBuff,
  454. g_hModule,
  455. MSG_RASDIAG_SHOW_RASCHK_NETCOMP,
  456. c_RasCompIds[i].szComponentId);
  457. hr = pnc->FindComponent(c_RasCompIds[i].szComponentId, &pncc);
  458. if (S_OK == hr)
  459. {
  460. BufferWriteMessage(
  461. pBuff,
  462. g_hModule,
  463. MSG_RASDIAG_SHOW_RASCHK_OK);
  464. ReleaseObj(pncc);
  465. }
  466. else
  467. {
  468. BufferWriteMessage(
  469. pBuff,
  470. g_hModule,
  471. MSG_RASDIAG_SHOW_RASCHK_NOTOK);
  472. hr = S_OK;
  473. }
  474. }
  475. //
  476. // Release INetCfg
  477. //
  478. (VOID) HrReleaseINetCfg(TRUE, pnc);
  479. }
  480. if (FAILED(hr))
  481. {
  482. BufferWriteMessage(pBuff, g_hModule, EMSG_RASDIAG_SHOW_RASCHK_RASCOMP);
  483. }
  484. return;
  485. }
  486. HRESULT
  487. HrConfigureComponent(
  488. IN INetCfgComponent* pncc,
  489. IN CONST PWCHAR pszComponentId,
  490. IN CONST NetClass nc,
  491. IN CONST PWCHAR pszAnswerfilePath)
  492. {
  493. WCHAR szParamsSectionName[1025];
  494. HRESULT hr = S_OK;
  495. static CONST PWCHAR c_pszSetupSection[] =
  496. {
  497. TOKEN_NETADAPTERS,
  498. TOKEN_NETPROTOCOL,
  499. TOKEN_NETSERVICES,
  500. TOKEN_NETCLIENTS
  501. };
  502. GetPrivateProfileString(
  503. c_pszSetupSection[nc],
  504. pszComponentId,
  505. g_pwszEmpty,
  506. szParamsSectionName,
  507. sizeof(szParamsSectionName) / sizeof(WCHAR) - 1,
  508. pszAnswerfilePath );
  509. //
  510. // Need to query for the private component interface which
  511. // gives us access to the INetCfgComponentSetup interface.
  512. //
  513. INetCfgComponentPrivate* pnccPrivate = NULL;
  514. hr = pncc->QueryInterface(
  515. IID_INetCfgComponentPrivate,
  516. reinterpret_cast<void**>(&pnccPrivate));
  517. if(SUCCEEDED(hr))
  518. {
  519. INetCfgComponentSetup* pNetCfgComponentSetup = NULL;
  520. //
  521. // Query the notify object for its setup interface.
  522. //
  523. hr = pnccPrivate->QueryNotifyObject(
  524. IID_INetCfgComponentSetup,
  525. reinterpret_cast<void**>(&pNetCfgComponentSetup));
  526. if(SUCCEEDED(hr))
  527. {
  528. hr = pNetCfgComponentSetup->ReadAnswerFile(
  529. pszAnswerfilePath,
  530. szParamsSectionName);
  531. if(SUCCEEDED(hr))
  532. {
  533. hr = pnccPrivate->SetDirty();
  534. }
  535. }
  536. }
  537. return hr;
  538. }
  539. //
  540. // Initialize COM, create and init INetCfg; Obtain write lock if indicated.
  541. //
  542. HRESULT
  543. HrGetINetCfg(
  544. IN BOOL fGetWriteLock,
  545. IN INetCfg** ppnc)
  546. {
  547. HRESULT hr=S_OK;
  548. //
  549. // Initialize the output parameters.
  550. //
  551. *ppnc = NULL;
  552. //
  553. // initialize COM
  554. //
  555. hr = CoInitializeEx(
  556. NULL,
  557. COINIT_DISABLE_OLE1DDE | COINIT_APARTMENTTHREADED);
  558. if (SUCCEEDED(hr))
  559. {
  560. //
  561. // Create the object implementing INetCfg.
  562. //
  563. INetCfg* pnc;
  564. hr = CoCreateInstance(
  565. CLSID_CNetCfg,
  566. NULL,
  567. CLSCTX_INPROC_SERVER,
  568. IID_INetCfg,
  569. (void**)&pnc);
  570. if (SUCCEEDED(hr))
  571. {
  572. INetCfgLock* pncLock = NULL;
  573. if (fGetWriteLock)
  574. {
  575. //
  576. // Get the locking interface
  577. //
  578. hr = pnc->QueryInterface(IID_INetCfgLock, (LPVOID *)&pncLock);
  579. if (SUCCEEDED(hr))
  580. {
  581. //
  582. // Attempt to lock the INetCfg for read/write
  583. //
  584. static CONST ULONG c_cmsTimeout = 15000;
  585. static CONST WCHAR c_szRasmontr[] =
  586. L"Remote Access Monitor (rasmontr.dll)";
  587. PWSTR szLockedBy;
  588. hr = pncLock->AcquireWriteLock(
  589. c_cmsTimeout,
  590. c_szRasmontr,
  591. &szLockedBy);
  592. if (S_FALSE == hr)
  593. {
  594. hr = NETCFG_E_NO_WRITE_LOCK;
  595. }
  596. }
  597. }
  598. if (SUCCEEDED(hr))
  599. {
  600. //
  601. // Initialize the INetCfg object.
  602. //
  603. hr = pnc->Initialize(NULL);
  604. if (SUCCEEDED(hr))
  605. {
  606. *ppnc = pnc;
  607. pnc->AddRef();
  608. }
  609. else
  610. {
  611. //
  612. // initialize failed, if obtained lock, release it
  613. //
  614. if (pncLock)
  615. {
  616. pncLock->ReleaseWriteLock();
  617. }
  618. }
  619. }
  620. ReleaseObj(pncLock);
  621. ReleaseObj(pnc);
  622. }
  623. if (FAILED(hr))
  624. {
  625. CoUninitialize();
  626. }
  627. }
  628. return hr;
  629. }
  630. //
  631. // Uninitialize INetCfg, release write lock (if present) and uninitialize COM.
  632. //
  633. HRESULT
  634. HrReleaseINetCfg(
  635. IN BOOL fHasWriteLock,
  636. IN INetCfg* pnc)
  637. {
  638. HRESULT hr = S_OK;
  639. //
  640. // uninitialize INetCfg
  641. //
  642. hr = pnc->Uninitialize();
  643. //
  644. // if write lock is present, unlock it
  645. //
  646. if (SUCCEEDED(hr) && fHasWriteLock)
  647. {
  648. INetCfgLock* pncLock;
  649. //
  650. // Get the locking interface
  651. //
  652. hr = pnc->QueryInterface(IID_INetCfgLock, (LPVOID *)&pncLock);
  653. if (SUCCEEDED(hr))
  654. {
  655. hr = pncLock->ReleaseWriteLock();
  656. ReleaseObj(pncLock);
  657. }
  658. }
  659. ReleaseObj(pnc);
  660. CoUninitialize();
  661. return hr;
  662. }
  663. PWCHAR
  664. RasDiagVerifyAnswerFile(
  665. IN CONST PWCHAR pwszFilePath)
  666. {
  667. WCHAR szFullPathToAnswerFile[MAX_PATH + 1];
  668. PWCHAR pszFilePath = NULL, pszDontCare;
  669. HANDLE hFile = NULL;
  670. //
  671. // Get the full path to the answerfile, ignore any error return
  672. //
  673. GetFullPathName(
  674. pwszFilePath,
  675. MAX_PATH,
  676. szFullPathToAnswerFile,
  677. &pszDontCare);
  678. //
  679. // Make sure the answerfile actually exists
  680. //
  681. hFile = CreateFile(
  682. szFullPathToAnswerFile,
  683. GENERIC_READ,
  684. FILE_SHARE_READ,
  685. NULL,
  686. OPEN_EXISTING,
  687. FILE_ATTRIBUTE_NORMAL,
  688. NULL);
  689. if(hFile != INVALID_HANDLE_VALUE)
  690. {
  691. pszFilePath = RutlStrDup(szFullPathToAnswerFile);
  692. CloseHandle(hFile);
  693. }
  694. return pszFilePath;
  695. }