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.

1286 lines
38 KiB

  1. /*-----------------------------------------------------------------------------
  2. import.cpp
  3. This file contain all the functions that handle importing connection
  4. information from .DUN files
  5. Copyright (C) 1996 Microsoft Corporation
  6. All rights reserved.
  7. Authors:
  8. ChrisK ChrisKauffman
  9. History:
  10. Sat 10-Mar-1996 23:50:40 -by- Mark MacLin [mmaclin]
  11. this code started its life as ixport.c in RNAUI.DLL
  12. my thanks to viroont
  13. 7/22/96 ChrisK Cleaned and formatted
  14. -----------------------------------------------------------------------------*/
  15. #include "pch.hpp"
  16. #include "resource.h"
  17. #include "inetcfg.h"
  18. #define ARRAYSIZE(a) (sizeof(a) / sizeof((a)[0]))
  19. #define IDS_DEFAULT_SCP 0
  20. #define IDS_INI_SCRIPT_DIR 1
  21. #define IDS_INI_SCRIPT_SHORTDIR 2
  22. #define MAXLONGLEN 80
  23. #define MAXNAME 80
  24. //#ifdef __cplusplus
  25. //extern "C" {
  26. //#endif // __cplusplus
  27. //extern HINSTANCE g_hInstance;
  28. //#ifdef __cplusplus
  29. //}
  30. //#endif // __cplusplus
  31. #define MAXIPADDRLEN 20
  32. #define SIZE_ReadBuf 0x00008000 // 32K buffer size
  33. #define AUTORUNSIGNUPWIZARDAPI "InetConfigClient"
  34. typedef HRESULT (CALLBACK *PFNAUTORUNSIGNUPWIZARD) (HWND hwndParent,
  35. LPCTSTR lpszPhoneBook,
  36. LPCTSTR lpszConnectoidName,
  37. LPRASENTRY lpRasEntry,
  38. LPCTSTR lpszUsername,
  39. LPCTSTR lpszPassword,
  40. LPCTSTR lpszProfileName,
  41. LPINETCLIENTINFO lpINetClientInfo,
  42. DWORD dwfOptions,
  43. LPBOOL lpfNeedsRestart);
  44. #pragma data_seg(".rdata")
  45. TCHAR cszEntrySection[] = TEXT("Entry");
  46. TCHAR cszEntryName[] = TEXT("Entry_Name");
  47. TCHAR cszAlias[] = TEXT("Import_Name");
  48. TCHAR cszML[] = TEXT("Multilink");
  49. TCHAR cszPhoneSection[] = TEXT("Phone");
  50. TCHAR cszDialAsIs[] = TEXT("Dial_As_Is");
  51. TCHAR cszPhone[] = TEXT("Phone_Number");
  52. TCHAR cszAreaCode[] = TEXT("Area_Code");
  53. TCHAR cszCountryCode[] = TEXT("Country_Code");
  54. TCHAR cszCountryID[] = TEXT("Country_ID");
  55. TCHAR cszDeviceSection[] = TEXT("Device");
  56. TCHAR cszDeviceType[] = TEXT("Type");
  57. TCHAR cszDeviceName[] = TEXT("Name");
  58. TCHAR cszDevCfgSize[] = TEXT("Settings_Size");
  59. TCHAR cszDevCfg[] = TEXT("Settings");
  60. TCHAR cszServerSection[] = TEXT("Server");
  61. TCHAR cszServerType[] = TEXT("Type");
  62. TCHAR cszSWCompress[] = TEXT("SW_Compress");
  63. TCHAR cszPWEncrypt[] = TEXT("PW_Encrypt");
  64. TCHAR cszNetLogon[] = TEXT("Network_Logon");
  65. TCHAR cszSWEncrypt[] = TEXT("SW_Encrypt");
  66. TCHAR cszNetBEUI[] = TEXT("Negotiate_NetBEUI");
  67. TCHAR cszIPX[] = TEXT("Negotiate_IPX/SPX");
  68. TCHAR cszIP[] = TEXT("Negotiate_TCP/IP");
  69. TCHAR cszIPSection[] = TEXT("TCP/IP");
  70. TCHAR cszIPSpec[] = TEXT("Specify_IP_Address");
  71. TCHAR cszIPAddress[] = TEXT("IP_address");
  72. TCHAR cszServerSpec[] = TEXT("Specify_Server_Address");
  73. TCHAR cszDNSAddress[] = TEXT("DNS_address");
  74. TCHAR cszDNSAltAddress[] = TEXT("DNS_Alt_address");
  75. TCHAR cszWINSAddress[] = TEXT("WINS_address");
  76. TCHAR cszWINSAltAddress[]= TEXT("WINS_Alt_address");
  77. TCHAR cszIPCompress[] = TEXT("IP_Header_Compress");
  78. TCHAR cszWanPri[] = TEXT("Gateway_On_Remote");
  79. TCHAR cszMLSection[] = TEXT("Multilink");
  80. TCHAR cszLinkIndex[] = TEXT("Line_%s");
  81. TCHAR cszScriptingSection[] = TEXT("Scripting");
  82. TCHAR cszScriptName[] = TEXT("Name");
  83. TCHAR cszScriptSection[] = TEXT("Script_File");
  84. TCHAR cszYes[] = TEXT("yes");
  85. TCHAR cszNo[] = TEXT("no");
  86. TCHAR cszUserSection[] = TEXT("User");
  87. TCHAR cszUserName[] = TEXT("Name");
  88. TCHAR cszPassword[] = TEXT("Password");
  89. TCHAR szNull[] = TEXT("");
  90. #define DUN_NOPHONENUMBER TEXT("000000000000")
  91. struct {
  92. TCHAR *szType;
  93. DWORD dwType;
  94. DWORD dwfOptions;
  95. } aServerTypes[] =
  96. {
  97. {TEXT("PPP"), RASFP_Ppp, 0},
  98. {TEXT("SLIP"), RASFP_Slip, 0},
  99. {TEXT("CSLIP"), RASFP_Slip, RASEO_IpHeaderCompression},
  100. {TEXT("RAS"), RASFP_Ras, 0}
  101. };
  102. #pragma data_seg()
  103. //#define RASAPI_LIBRARY "RASAPI32.DLL"
  104. //#define RNAPH_LIBRARY "RNAPH.DLL"
  105. //typedef DWORD (WINAPI* PFNRASSETENTRYPROPERTIES)(LPSTR lpszPhonebook, LPSTR lpszEntry, LPBYTE lpbEntryInfo, DWORD dwEntryInfoSize, LPBYTE lpbDeviceInfo, DWORD dwDeviceInfoSize);
  106. //PreWriteConnectoid(LPSTR pszEntryName,RASENTRY *lpRasEntry)
  107. //{
  108. // FARPROC fp = NULL;
  109. // HINSTANCE hRNADLL;
  110. // DWORD dwRasSize;
  111. // hRNADLL = LoadLibrary(RASAPI_LIBRARY);
  112. // fp = NULL;
  113. // if (hRNADLL)
  114. // {
  115. // fp = GetProcAddress(hRNADLL,"RasGetEntryProperties");
  116. // }
  117. // if (!fp)
  118. // {
  119. // if (hRNADLL) FreeLibrary(hRNADLL);
  120. // hRNADLL = LoadLibrary(RNAPH_LIBRARY);
  121. // if (hRNADLL) fp = GetProcAddress(hRNADLL,"RasSetEntryProperties");
  122. // }
  123. // if (fp)
  124. // {
  125. // dwRasSize = sizeof(RASENTRY);
  126. // ((PFNRASSETENTRYPROPERTIES)fp)(NULL,pszEntryName,(LPBYTE)lpRasEntry,dwRasSize,NULL,0);
  127. // }
  128. // if (hRNADLL) FreeLibrary(hRNADLL);
  129. //}
  130. //****************************************************************************
  131. // DWORD NEAR PASCAL StrToip (LPSTR szIPAddress, LPDWORD lpdwAddr)
  132. //
  133. // This function converts a IP address string to an IP address structure.
  134. //
  135. // History:
  136. // Mon 18-Dec-1995 10:07:02 -by- Viroon Touranachun [viroont]
  137. // Cloned from SMMSCRPT.
  138. //****************************************************************************
  139. LPCTSTR NEAR PASCAL StrToSubip (LPCTSTR szIPAddress, BYTE *pVal)
  140. {
  141. LPCTSTR pszIP = szIPAddress;
  142. *pVal = (BYTE)Sz2W(pszIP);
  143. // skip over digits
  144. while (FIsDigit(*pszIP))
  145. {
  146. ++pszIP;
  147. }
  148. // skip over one or more separators
  149. while (*pszIP && !FIsDigit(*pszIP))
  150. {
  151. ++pszIP;
  152. }
  153. return pszIP;
  154. }
  155. DWORD NEAR PASCAL StrToip (LPCTSTR szIPAddress, RASIPADDR *ipAddr)
  156. {
  157. LPCTSTR pszIP = szIPAddress;
  158. pszIP = StrToSubip(pszIP, &ipAddr->a);
  159. pszIP = StrToSubip(pszIP, &ipAddr->b);
  160. pszIP = StrToSubip(pszIP, &ipAddr->c);
  161. pszIP = StrToSubip(pszIP, &ipAddr->d);
  162. return ERROR_SUCCESS;
  163. }
  164. //****************************************************************************
  165. // DWORD NEAR PASCAL ImportPhoneInfo(PPHONENUM ppn, LPCTSTR szFileName)
  166. //
  167. // This function imports the phone number.
  168. //
  169. // History:
  170. // Mon 18-Dec-1995 10:07:02 -by- Viroon Touranachun [viroont]
  171. // Created.
  172. //****************************************************************************
  173. DWORD NEAR PASCAL ImportPhoneInfo(LPRASENTRY lpRasEntry, LPCTSTR szFileName)
  174. {
  175. TCHAR szYesNo[MAXNAME];
  176. GetPrivateProfileString(cszPhoneSection,
  177. cszPhone,
  178. DUN_NOPHONENUMBER,
  179. lpRasEntry->szLocalPhoneNumber,
  180. RAS_MaxPhoneNumber,
  181. szFileName);
  182. /****************
  183. we need to accept entries w/o phone numbers
  184. if (GetPrivateProfileString(cszPhoneSection,
  185. cszPhone,
  186. szNull,
  187. lpRasEntry->szLocalPhoneNumber,
  188. sizeof(lpRasEntry->szLocalPhoneNumber),
  189. szFileName) == 0)
  190. {
  191. return ERROR_CORRUPT_PHONEBOOK;
  192. };
  193. ****************/
  194. lpRasEntry->dwfOptions &= ~RASEO_UseCountryAndAreaCodes;
  195. GetPrivateProfileString(cszPhoneSection,
  196. cszDialAsIs,
  197. cszYes,
  198. szYesNo,
  199. MAXNAME,
  200. szFileName);
  201. // Do we have to get country code and area code?
  202. //
  203. if (!lstrcmpi(szYesNo, cszNo))
  204. {
  205. // If we cannot get the country ID or it is zero, default to dial as is
  206. //
  207. if ((lpRasEntry->dwCountryID = GetPrivateProfileInt(cszPhoneSection,
  208. cszCountryID,
  209. 0,
  210. szFileName)) != 0)
  211. {
  212. lpRasEntry->dwCountryCode = GetPrivateProfileInt(cszPhoneSection,
  213. cszCountryCode,
  214. 1,
  215. szFileName);
  216. if (GetPrivateProfileString(cszPhoneSection,
  217. cszAreaCode,
  218. szNull,
  219. lpRasEntry->szAreaCode,
  220. RAS_MaxAreaCode,
  221. szFileName) != 0)
  222. {
  223. lpRasEntry->dwfOptions |= RASEO_UseCountryAndAreaCodes;
  224. }
  225. }
  226. }
  227. return ERROR_SUCCESS;
  228. }
  229. //****************************************************************************
  230. // DWORD NEAR PASCAL ImportServerInfo(PSMMINFO psmmi, LPSTR szFileName)
  231. //
  232. // This function imports the server type name and settings.
  233. //
  234. // History:
  235. // Mon 18-Dec-1995 10:07:02 -by- Viroon Touranachun [viroont]
  236. // Created.
  237. //****************************************************************************
  238. DWORD NEAR PASCAL ImportServerInfo(LPRASENTRY lpRasEntry, LPCTSTR szFileName)
  239. {
  240. TCHAR szYesNo[MAXNAME];
  241. TCHAR szType[MAXNAME];
  242. DWORD i;
  243. // Get the server type name
  244. //
  245. GetPrivateProfileString(cszServerSection,
  246. cszServerType,
  247. szNull,
  248. szType,
  249. MAXNAME,
  250. szFileName);
  251. // need to convert the string into
  252. // one of the following values
  253. // RASFP_Ppp
  254. // RASFP_Slip Note CSLIP is SLIP with IP compression on
  255. // RASFP_Ras
  256. for (i = 0; i < sizeof(aServerTypes)/sizeof(aServerTypes[0]); ++i)
  257. {
  258. if (!lstrcmpi(aServerTypes[i].szType, szType))
  259. {
  260. lpRasEntry->dwFramingProtocol = aServerTypes[i].dwType;
  261. lpRasEntry->dwfOptions |= aServerTypes[i].dwfOptions;
  262. break;
  263. }
  264. }
  265. // Get the server type settings
  266. //
  267. if (GetPrivateProfileString(cszServerSection,
  268. cszSWCompress,
  269. szNull,
  270. szYesNo,
  271. MAXNAME,
  272. szFileName))
  273. {
  274. if (!lstrcmpi(szYesNo, cszNo))
  275. {
  276. lpRasEntry->dwfOptions &= ~RASEO_SwCompression;
  277. }
  278. else
  279. {
  280. lpRasEntry->dwfOptions |= RASEO_SwCompression;
  281. };
  282. };
  283. if (GetPrivateProfileString(cszServerSection,
  284. cszPWEncrypt,
  285. szNull,
  286. szYesNo,
  287. MAXNAME,
  288. szFileName))
  289. {
  290. if (!lstrcmpi(szYesNo, cszNo))
  291. {
  292. lpRasEntry->dwfOptions &= ~RASEO_RequireEncryptedPw;
  293. }
  294. else
  295. {
  296. lpRasEntry->dwfOptions |= RASEO_RequireEncryptedPw;
  297. };
  298. };
  299. if (GetPrivateProfileString(cszServerSection,
  300. cszNetLogon,
  301. szNull,
  302. szYesNo,
  303. MAXNAME,
  304. szFileName))
  305. {
  306. if (!lstrcmpi(szYesNo, cszNo))
  307. {
  308. lpRasEntry->dwfOptions &= ~RASEO_NetworkLogon;
  309. }
  310. else
  311. {
  312. lpRasEntry->dwfOptions |= RASEO_NetworkLogon;
  313. };
  314. };
  315. if (GetPrivateProfileString(cszServerSection,
  316. cszSWEncrypt,
  317. szNull,
  318. szYesNo,
  319. MAXNAME,
  320. szFileName))
  321. {
  322. if (!lstrcmpi(szYesNo, cszNo))
  323. {
  324. lpRasEntry->dwfOptions &= ~RASEO_RequireDataEncryption;
  325. }
  326. else
  327. {
  328. lpRasEntry->dwfOptions |= RASEO_RequireDataEncryption;
  329. };
  330. };
  331. // Get the protocol settings
  332. //
  333. if (GetPrivateProfileString(cszServerSection,
  334. cszNetBEUI,
  335. szNull,
  336. szYesNo,
  337. MAXNAME,
  338. szFileName))
  339. {
  340. if (!lstrcmpi(szYesNo, cszNo))
  341. {
  342. //#ifdef _CHRISK
  343. lpRasEntry->dwfNetProtocols &= ~RASNP_NetBEUI;
  344. //#else
  345. // lpRasEntry->dwfNetProtocols &= ~RASNP_Netbeui;
  346. //#endif
  347. }
  348. else
  349. {
  350. //#ifdef _CHRISK
  351. lpRasEntry->dwfNetProtocols |= RASNP_NetBEUI;
  352. //#else
  353. // lpRasEntry->dwfNetProtocols |= RASNP_Netbeui;
  354. //#endif
  355. };
  356. };
  357. if (GetPrivateProfileString(cszServerSection,
  358. cszIPX,
  359. szNull,
  360. szYesNo,
  361. MAXNAME,
  362. szFileName))
  363. {
  364. if (!lstrcmpi(szYesNo, cszNo))
  365. {
  366. lpRasEntry->dwfNetProtocols &= ~RASNP_Ipx;
  367. }
  368. else
  369. {
  370. lpRasEntry->dwfNetProtocols |= RASNP_Ipx;
  371. };
  372. };
  373. if (GetPrivateProfileString(cszServerSection,
  374. cszIP,
  375. szNull,
  376. szYesNo,
  377. MAXNAME,
  378. szFileName))
  379. {
  380. if (!lstrcmpi(szYesNo, cszNo))
  381. {
  382. lpRasEntry->dwfNetProtocols &= ~RASNP_Ip;
  383. }
  384. else
  385. {
  386. lpRasEntry->dwfNetProtocols |= RASNP_Ip;
  387. };
  388. };
  389. return ERROR_SUCCESS;
  390. }
  391. //****************************************************************************
  392. // DWORD NEAR PASCAL ImportIPInfo(LPSTR szEntryName, LPSTR szFileName)
  393. //
  394. // This function imports the TCP/IP information
  395. //
  396. // History:
  397. // Mon 18-Dec-1995 10:07:02 -by- Viroon Touranachun [viroont]
  398. // Created.
  399. //****************************************************************************
  400. DWORD NEAR PASCAL ImportIPInfo(LPRASENTRY lpRasEntry, LPCTSTR szFileName)
  401. {
  402. TCHAR szIPAddr[MAXIPADDRLEN];
  403. TCHAR szYesNo[MAXNAME];
  404. // Import IP address information
  405. //
  406. if (GetPrivateProfileString(cszIPSection,
  407. cszIPSpec,
  408. szNull,
  409. szYesNo,
  410. MAXNAME,
  411. szFileName))
  412. {
  413. if (!lstrcmpi(szYesNo, cszYes))
  414. {
  415. // The import file has IP address specified, get the IP address
  416. //
  417. lpRasEntry->dwfOptions |= RASEO_SpecificIpAddr;
  418. if (GetPrivateProfileString(cszIPSection,
  419. cszIPAddress,
  420. szNull,
  421. szIPAddr,
  422. MAXIPADDRLEN,
  423. szFileName))
  424. {
  425. StrToip (szIPAddr, &lpRasEntry->ipaddr);
  426. };
  427. }
  428. else
  429. {
  430. lpRasEntry->dwfOptions &= ~RASEO_SpecificIpAddr;
  431. };
  432. };
  433. // Import Server address information
  434. //
  435. if (GetPrivateProfileString(cszIPSection,
  436. cszServerSpec,
  437. szNull,
  438. szYesNo,
  439. MAXNAME,
  440. szFileName))
  441. {
  442. if (!lstrcmpi(szYesNo, cszYes))
  443. {
  444. // The import file has server address specified, get the server address
  445. //
  446. lpRasEntry->dwfOptions |= RASEO_SpecificNameServers;
  447. if (GetPrivateProfileString(cszIPSection,
  448. cszDNSAddress,
  449. szNull,
  450. szIPAddr,
  451. MAXIPADDRLEN,
  452. szFileName))
  453. {
  454. StrToip (szIPAddr, &lpRasEntry->ipaddrDns);
  455. };
  456. if (GetPrivateProfileString(cszIPSection,
  457. cszDNSAltAddress,
  458. szNull,
  459. szIPAddr,
  460. MAXIPADDRLEN,
  461. szFileName))
  462. {
  463. StrToip (szIPAddr, &lpRasEntry->ipaddrDnsAlt);
  464. };
  465. if (GetPrivateProfileString(cszIPSection,
  466. cszWINSAddress,
  467. szNull,
  468. szIPAddr,
  469. MAXIPADDRLEN,
  470. szFileName))
  471. {
  472. StrToip (szIPAddr, &lpRasEntry->ipaddrWins);
  473. };
  474. if (GetPrivateProfileString(cszIPSection,
  475. cszWINSAltAddress,
  476. szNull,
  477. szIPAddr,
  478. MAXIPADDRLEN,
  479. szFileName))
  480. {
  481. StrToip (szIPAddr, &lpRasEntry->ipaddrWinsAlt);
  482. };
  483. }
  484. else
  485. {
  486. lpRasEntry->dwfOptions &= ~RASEO_SpecificNameServers;
  487. };
  488. };
  489. // Header compression and the gateway settings
  490. //
  491. if (GetPrivateProfileString(cszIPSection,
  492. cszIPCompress,
  493. szNull,
  494. szYesNo,
  495. MAXNAME,
  496. szFileName))
  497. {
  498. if (!lstrcmpi(szYesNo, cszNo))
  499. {
  500. lpRasEntry->dwfOptions &= ~RASEO_IpHeaderCompression;
  501. }
  502. else
  503. {
  504. lpRasEntry->dwfOptions |= RASEO_IpHeaderCompression;
  505. };
  506. };
  507. if (GetPrivateProfileString(cszIPSection,
  508. cszWanPri,
  509. szNull,
  510. szYesNo,
  511. MAXNAME,
  512. szFileName))
  513. {
  514. if (!lstrcmpi(szYesNo, cszNo))
  515. {
  516. lpRasEntry->dwfOptions &= ~RASEO_RemoteDefaultGateway;
  517. }
  518. else
  519. {
  520. lpRasEntry->dwfOptions |= RASEO_RemoteDefaultGateway;
  521. };
  522. };
  523. return ERROR_SUCCESS;
  524. }
  525. //****************************************************************************
  526. // HANDLE NEAR PASCAL CreateUniqueFile(LPSTR szPath, LPSTR szFile)
  527. //
  528. // This function creates a unique file. If the file already exists, it will
  529. // try to create a file with similar name and return the name.
  530. //
  531. // History:
  532. // Mon 18-Dec-1995 10:07:02 -by- Viroon Touranachun [viroont]
  533. // Created.
  534. //****************************************************************************
  535. HANDLE NEAR PASCAL CreateUniqueFile(LPTSTR szPath, LPTSTR szScript)
  536. {
  537. HANDLE hFile;
  538. LPTSTR pszSuffix, lpsz;
  539. UINT uSuffix;
  540. pszSuffix = szPath + lstrlen(szPath);
  541. lpsz = CharPrev(szPath, pszSuffix);
  542. if (*lpsz != '\\')
  543. {
  544. *pszSuffix = '\\';
  545. pszSuffix++;
  546. };
  547. lstrcpy(pszSuffix, szScript);
  548. // Try the specified filename
  549. //
  550. hFile = CreateFile(szPath, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
  551. FILE_ATTRIBUTE_NORMAL, NULL);
  552. // If the file exists
  553. //
  554. if ((hFile == INVALID_HANDLE_VALUE) && (GetLastError() == ERROR_FILE_EXISTS))
  555. {
  556. TCHAR szNewName[MAX_PATH];
  557. // Need to copy it to another name in the same directory
  558. //
  559. if (LoadString(g_hInstance, IDS_DEFAULT_SCP, szNewName, MAX_PATH))
  560. {
  561. // Increment the file index until a non-duplicated file can be created
  562. //
  563. uSuffix = 0;
  564. do
  565. {
  566. wsprintf(pszSuffix, szNewName, uSuffix);
  567. uSuffix++;
  568. hFile = CreateFile(szPath, GENERIC_WRITE, 0, NULL, CREATE_NEW,
  569. FILE_ATTRIBUTE_NORMAL, NULL);
  570. }
  571. while ((hFile == INVALID_HANDLE_VALUE) &&
  572. (GetLastError() == ERROR_FILE_EXISTS) &&
  573. (uSuffix < 0x0000FFFF));
  574. };
  575. };
  576. // If we do not have the file, reserve the pathname
  577. //
  578. if (hFile == INVALID_HANDLE_VALUE)
  579. {
  580. *pszSuffix = '\0';
  581. };
  582. return hFile;
  583. }
  584. //****************************************************************************
  585. // HANDLE NEAR PASCAL CreateScriptFile(LPTSTR szScript, LPTSTR szImportFile)
  586. //
  587. // This function creates the script file
  588. //
  589. // History:
  590. // Mon 18-Dec-1995 10:07:02 -by- Viroon Touranachun [viroont]
  591. // Created.
  592. //****************************************************************************
  593. HANDLE NEAR PASCAL CreateScriptFile(LPTSTR szScript, LPCTSTR szImportFile)
  594. {
  595. LPTSTR pszPath, pszShortName;
  596. LPTSTR pszDir;
  597. DWORD cb;
  598. HANDLE hFile;
  599. // Assume failure
  600. //
  601. hFile = INVALID_HANDLE_VALUE;
  602. // Allocate a buffer for pathname
  603. //
  604. if ((pszPath = (LPTSTR)GlobalAlloc(LMEM_FIXED, 2*MAX_PATH)) == NULL)
  605. {
  606. TraceMsg(TF_GENERAL, "CONNECT:CreateScriptFile(): Local Alloc failed\n");
  607. return INVALID_HANDLE_VALUE;
  608. }
  609. pszShortName = pszPath+MAX_PATH;
  610. // Get the default directory
  611. //
  612. if (GetWindowsDirectory(pszPath, MAX_PATH) != 0)
  613. {
  614. // Get the Windows drive
  615. //
  616. pszDir = pszPath;
  617. while((*pszDir != '\\') && (*pszDir != '\0'))
  618. {
  619. pszDir = CharNext(pszDir);
  620. };
  621. // Did we find Windows drive?
  622. //
  623. if (*pszDir != '\0')
  624. {
  625. // Prepare the drive
  626. //
  627. cb = (DWORD)(pszDir - pszPath);
  628. MyMemCpy(szScript, pszPath, cb);
  629. pszDir = szScript + cb;
  630. // Get the script filename
  631. //
  632. if (GetPrivateProfileString(cszScriptingSection,
  633. cszScriptName,
  634. szNull,
  635. pszShortName,
  636. MAX_PATH,
  637. szImportFile) != 0)
  638. {
  639. // Try the favorite script directory
  640. //
  641. if (LoadString(g_hInstance, IDS_INI_SCRIPT_DIR, pszDir,
  642. MAX_PATH - cb) != 0)
  643. {
  644. // Try creating the file
  645. //
  646. hFile = CreateUniqueFile(szScript, pszShortName);
  647. };
  648. // If we do not have the file yet, try the second favorite
  649. //
  650. if (hFile == INVALID_HANDLE_VALUE)
  651. {
  652. if (LoadString(g_hInstance, IDS_INI_SCRIPT_SHORTDIR, pszDir,
  653. MAX_PATH - cb))
  654. {
  655. // Try creating the file
  656. //
  657. hFile = CreateUniqueFile(szScript, pszShortName);
  658. };
  659. };
  660. // If we do not have the file yet, try Windows directory
  661. //
  662. if (hFile == INVALID_HANDLE_VALUE)
  663. {
  664. // Get original Windows directory
  665. //
  666. lstrcpy(szScript, pszPath);
  667. // Try one more time
  668. //
  669. hFile = CreateUniqueFile(szScript, pszShortName);
  670. };
  671. };
  672. };
  673. };
  674. GlobalFree((HLOCAL)pszPath);
  675. return hFile;
  676. }
  677. //****************************************************************************
  678. // DWORD NEAR PASCAL ImportScriptFile(LPTSTR szEntryName, LPTSTR szImportFile)
  679. //
  680. // This function imports the script file
  681. //
  682. // History:
  683. // Mon 18-Dec-1995 10:07:02 -by- Viroon Touranachun [viroont]
  684. // Created.
  685. //****************************************************************************
  686. DWORD NEAR PASCAL ImportScriptFile(LPRASENTRY lpRasEntry, LPCTSTR szImportFile)
  687. {
  688. HANDLE hfScript;
  689. LPTSTR pszLine;
  690. LPTSTR pszFile;
  691. int i, iMaxLine;
  692. UINT cbSize, cbRet;
  693. TCHAR szTmp[4];
  694. DWORD dwRet;
  695. dwRet=ERROR_SUCCESS;
  696. // If a script section does not exist, do nothing
  697. //
  698. if (GetPrivateProfileString(cszScriptingSection,
  699. cszScriptName,
  700. szNull,
  701. szTmp,
  702. 4,
  703. szImportFile) == 0)
  704. {
  705. return ERROR_SUCCESS;
  706. };
  707. // Allocate a buffer for the script lines
  708. //
  709. if ((pszLine = (LPTSTR)GlobalAlloc(LMEM_FIXED, SIZE_ReadBuf+MAX_PATH))
  710. == NULL)
  711. {
  712. TraceMsg(TF_GENERAL, "CONNECT:ImportScriptFile(): Local Alloc failed\n");
  713. return ERROR_OUTOFMEMORY;
  714. }
  715. // Look for script
  716. //
  717. if (GetPrivateProfileString(cszScriptSection,
  718. NULL,
  719. szNull,
  720. pszLine,
  721. SIZE_ReadBuf,
  722. szImportFile) != 0)
  723. {
  724. // Get the maximum line number
  725. //
  726. pszFile = pszLine;
  727. iMaxLine = -1;
  728. while (*pszFile)
  729. {
  730. i = Sz2W(pszFile);
  731. iMaxLine = max(iMaxLine, i);
  732. pszFile += lstrlen(pszFile)+1;
  733. };
  734. // If we have at least one line, we will import the script file
  735. //
  736. if (iMaxLine >= 0)
  737. {
  738. pszFile = pszLine+SIZE_ReadBuf;
  739. // Create the script file
  740. //
  741. //DebugBreak();
  742. hfScript = CreateScriptFile(pszFile, szImportFile);
  743. TraceMsg(TF_GENERAL, "CONNECT:ImportScriptFile(): CreateScriptFile hfScript %d, %s, %s\n",hfScript,pszFile,szImportFile);
  744. if (hfScript != INVALID_HANDLE_VALUE)
  745. {
  746. TCHAR szLineNum[MAXLONGLEN+1];
  747. // From The first line to the last line
  748. //
  749. for (i = 0; i <= iMaxLine; i++)
  750. {
  751. // Read the script line
  752. //
  753. wsprintf(szLineNum, TEXT("%d"), i);
  754. if ((cbSize = GetPrivateProfileString(cszScriptSection,
  755. szLineNum,
  756. szNull,
  757. pszLine,
  758. SIZE_ReadBuf,
  759. szImportFile)) != 0)
  760. {
  761. // Write to the script file
  762. //
  763. lstrcat(pszLine, TEXT("\x0d\x0a"));
  764. WriteFile(hfScript, pszLine, cbSize+2, (LPDWORD)&cbRet, NULL);
  765. };
  766. };
  767. CloseHandle(hfScript);
  768. // Associate it with the phonebook entry
  769. //
  770. lstrcpyn(lpRasEntry->szScript, pszFile, RAS_MaxEntryName);
  771. }
  772. else
  773. {
  774. dwRet = GetLastError();
  775. };
  776. }
  777. else
  778. {
  779. dwRet = ERROR_PATH_NOT_FOUND;
  780. };
  781. }
  782. else
  783. {
  784. dwRet = ERROR_PATH_NOT_FOUND;
  785. };
  786. GlobalFree((HLOCAL)pszLine);
  787. return dwRet;
  788. }
  789. //****************************************************************************
  790. // DWORD WINAPI RnaValidateImportEntry (LPTSTR)
  791. //
  792. // This function is called to validate an importable file
  793. //
  794. // History:
  795. // Wed 03-Jan-1996 09:45:01 -by- Viroon Touranachun [viroont]
  796. // Created.
  797. //****************************************************************************
  798. DWORD WINAPI RnaValidateImportEntry (LPCTSTR szFileName)
  799. {
  800. TCHAR szTmp[4];
  801. // Get the alias entry name
  802. //
  803. return (GetPrivateProfileString(cszEntrySection,
  804. cszEntryName,
  805. szNull,
  806. szTmp,
  807. 4,
  808. szFileName) > 0 ?
  809. ERROR_SUCCESS : ERROR_CORRUPT_PHONEBOOK);
  810. }
  811. //****************************************************************************
  812. // HRESULT ImportConnection (LPCTSTR szFileName, LPTSTR pszEntryName, LPTSTR pszUserName, LPTSTR pszPassword)
  813. //
  814. // This function is called to import an entry from a specified file
  815. //
  816. // History:
  817. // Mon 18-Dec-1995 10:07:02 -by- Viroon Touranachun [viroont]
  818. // Created.
  819. // Sat 16-Mar-1996 10:01:00 -by- Chris Kauffman [chrisk]
  820. // Modified to return HRESULT and load DLL dynamically
  821. //****************************************************************************
  822. HRESULT ImportConnection (LPCTSTR szFileName, LPTSTR pszEntryName, LPTSTR pszUserName, LPTSTR pszPassword)
  823. {
  824. LPRASENTRY lpRasEntry;
  825. DWORD dwRet;
  826. HINSTANCE hinetcfg;
  827. FARPROC fp;
  828. BOOL fNeedsRestart;
  829. //#ifdef DEBUG
  830. // char szDebug[256];
  831. //#endif
  832. //char szEntryName[RAS_MaxEntryName+1];
  833. //char szUserName[UNLEN+1];
  834. //char szPassword[PWLEN+1];
  835. //BOOL fNeedsRestart;
  836. // Get the size of device configuration
  837. // This also validates an exported file
  838. //
  839. if ((dwRet = RnaValidateImportEntry(szFileName)) != ERROR_SUCCESS)
  840. {
  841. return dwRet;
  842. };
  843. // Allocate a buffer for entry and device config
  844. //
  845. if ((lpRasEntry = (LPRASENTRY)GlobalAlloc(GPTR, sizeof(RASENTRY))) == NULL)
  846. {
  847. return ERROR_OUTOFMEMORY;
  848. };
  849. // Get the entry name
  850. // Need to find a good name for it and remember it as an alias
  851. //
  852. GetPrivateProfileString(cszEntrySection,
  853. cszEntryName,
  854. szNull,
  855. pszEntryName,
  856. RAS_MaxEntryName+1,
  857. szFileName);
  858. GetPrivateProfileString(cszUserSection,
  859. cszUserName,
  860. szNull,
  861. pszUserName,
  862. UNLEN+1,
  863. szFileName);
  864. GetPrivateProfileString(cszUserSection,
  865. cszPassword,
  866. szNull,
  867. pszPassword,
  868. PWLEN+1,
  869. szFileName);
  870. if ((dwRet = ImportPhoneInfo(lpRasEntry, szFileName))
  871. == ERROR_SUCCESS)
  872. {
  873. // Get device name, type and config
  874. //
  875. GetPrivateProfileString(cszDeviceSection,
  876. cszDeviceType,
  877. szNull,
  878. lpRasEntry->szDeviceType,
  879. RAS_MaxDeviceType,
  880. szFileName);
  881. // Get Server Type settings
  882. //
  883. ImportServerInfo(lpRasEntry, szFileName);
  884. // Get IP address
  885. //
  886. ImportIPInfo(lpRasEntry, szFileName);
  887. // Import the script file
  888. //
  889. if ((dwRet = ImportScriptFile(lpRasEntry, szFileName)) != ERROR_SUCCESS)
  890. {
  891. TraceMsg(TF_GENERAL, "CONNECT:ImportScriptFile Failed with the error %d,%s,%s",dwRet,szFileName,lpRasEntry->szScript);
  892. }
  893. lpRasEntry->dwSize = sizeof(RASENTRY);
  894. // Load and Locate AutoRunSignUpWizard entry point
  895. //
  896. hinetcfg = LoadLibrary(TEXT("INETCFG.DLL"));
  897. AssertMsg(hinetcfg,"Cannot find INETCFG.DLL");
  898. if (!hinetcfg)
  899. {
  900. dwRet = GetLastError();
  901. goto ImportConnectionExit;
  902. }
  903. fp = GetProcAddress(hinetcfg,AUTORUNSIGNUPWIZARDAPI);
  904. AssertMsg(fp,"Cannot find AutoRunSignupWizard entry point");
  905. if (!fp)
  906. {
  907. dwRet = GetLastError();
  908. goto ImportConnectionExit;
  909. }
  910. // Insert Autodialer
  911. //
  912. lstrcpy(lpRasEntry->szAutodialDll,TEXT("ICWDIAL.DLL"));
  913. lstrcpy(lpRasEntry->szAutodialFunc,TEXT("AutoDialHandler"));
  914. TraceMsg(TF_GENERAL, "CONNECT:Autodialer installed at %s, %s.\n",lpRasEntry->szAutodialDll,lpRasEntry->szAutodialFunc);
  915. // Call InetClientConfig
  916. //
  917. // PreWriteConnectoid(pszEntryName,lpRasEntry);
  918. dwRet = ((PFNAUTORUNSIGNUPWIZARD)fp)(
  919. NULL,
  920. NULL,
  921. pszEntryName,
  922. lpRasEntry,
  923. pszUserName,
  924. pszPassword,
  925. NULL,
  926. NULL,
  927. INETCFG_SETASAUTODIAL |
  928. INETCFG_INSTALLRNA |
  929. INETCFG_INSTALLTCP |
  930. INETCFG_OVERWRITEENTRY,
  931. &fNeedsRestart);
  932. #if !defined(WIN16)
  933. RasSetEntryPropertiesScriptPatch(lpRasEntry->szScript,pszEntryName);
  934. #endif //!Win16
  935. //RestoreDeskTopInternetCommand();
  936. TraceMsg(TF_GENERAL, "CONNECT:EntryName %s, User %s, Password %s, Number %s\n",pszEntryName,pszUserName,pszPassword,lpRasEntry->szLocalPhoneNumber);
  937. AssertMsg(!fNeedsRestart,"We have to reboot AGAIN!!");
  938. }
  939. // Exit and cleanup
  940. //
  941. ImportConnectionExit:
  942. if (hinetcfg) FreeLibrary(hinetcfg);
  943. GlobalFree((HLOCAL)lpRasEntry);
  944. return dwRet;
  945. }
  946. // ############################################################################
  947. HRESULT CreateEntryFromDUNFile(PTSTR pszDunFile)
  948. {
  949. TCHAR szFileName[MAX_PATH];
  950. //char szEntryName[RAS_MaxEntryName+1];
  951. TCHAR szUserName[UNLEN+1];
  952. TCHAR szPassword[PWLEN+1];
  953. LPTSTR pszTemp;
  954. HRESULT hr;
  955. hr = ERROR_SUCCESS;
  956. // Get fully qualified path name
  957. //
  958. if (!SearchPath(GIGetAppDir(),pszDunFile,NULL,MAX_PATH,&szFileName[0],&pszTemp))
  959. {
  960. //MsgBox(IDS_CANTREADDUNFILE,MB_APPLMODAL | MB_ICONERROR);
  961. hr = ERROR_FILE_NOT_FOUND;
  962. goto CreateEntryFromDUNFileExit;
  963. }
  964. // save current DUN file name in global (for ourself)
  965. SetCurrentDUNFile(&szFileName[0]);
  966. hr = ImportConnection (&szFileName[0], g_szEntryName, szUserName, szPassword);
  967. //if (hr != ERROR_SUCCESS)
  968. //{
  969. // MsgBox(IDS_CANTREADDUNFILE,MB_APPLMODAL | MB_ICONERROR);
  970. // goto CreateEntryFromDUNFileExit;
  971. //}// else {
  972. //
  973. // // place the name of the connectoid in the registry
  974. // //
  975. if (ERROR_SUCCESS != (StoreInSignUpReg((LPBYTE)g_szEntryName, lstrlen(g_szEntryName), REG_SZ, RASENTRYVALUENAME)))
  976. {
  977. MsgBox(IDS_CANTSAVEKEY,MB_MYERROR);
  978. goto CreateEntryFromDUNFileExit;
  979. }
  980. // }
  981. CreateEntryFromDUNFileExit:
  982. return hr;
  983. }
  984. // ############################################################################
  985. BOOL FSz2Dw(PCTSTR pSz,DWORD *dw)
  986. {
  987. DWORD val = 0;
  988. while (*pSz && *pSz != '.')
  989. {
  990. if (*pSz >= '0' && *pSz <= '9')
  991. {
  992. val *= 10;
  993. val += *pSz++ - '0';
  994. }
  995. else
  996. {
  997. return FALSE; //bad number
  998. }
  999. }
  1000. *dw = val;
  1001. return (TRUE);
  1002. }
  1003. // ############################################################################
  1004. BOOL BreakUpPhoneNumber(RASENTRY *prasentry, LPTSTR pszPhone)
  1005. {
  1006. PTSTR pszStart,pszNext, pszLim;
  1007. // LPPHONENUM ppn;
  1008. if (!pszPhone) return FALSE; // skip if no number
  1009. pszLim = pszPhone + lstrlen(pszPhone); // find end of string
  1010. //ppn = (fMain) ? &(pic->PhoneNum) : &(pic->PhoneNum2);
  1011. ////Get the country ID...
  1012. //ppn->dwCountryID = PBKDWCountryId();
  1013. // Get Country Code from phone number...
  1014. pszStart = _tcschr(pszPhone,'+');
  1015. if(!pszStart) goto error; // bad format
  1016. // get country code
  1017. pszStart = GetNextNumericChunk(pszStart, pszLim, &pszNext);
  1018. if(!pszStart || !pszNext) goto error; // bad format
  1019. //ppn->dwCountryCode = Sz2Dw(pszStart);
  1020. FSz2Dw(pszStart,&prasentry->dwCountryCode);
  1021. pszStart = pszNext;
  1022. //Now get the area code
  1023. pszStart = GetNextNumericChunk(pszStart, pszLim, &pszNext);
  1024. if(!pszStart || !pszNext) goto error; // bad format
  1025. //lstrcpy(ppn->szAreaCode, pszStart);
  1026. lstrcpyn(prasentry->szAreaCode,pszStart,ARRAYSIZE(prasentry->szAreaCode));
  1027. pszStart = pszNext;
  1028. //now the local phone number (everything from here to : or end)
  1029. pszNext = _tcschr(pszStart, ':');
  1030. if(pszNext) *pszNext='\0';
  1031. //lstrcpy(ppn->szLocal,pszStart);
  1032. lstrcpyn(prasentry->szLocalPhoneNumber,pszStart,RAS_MaxPhoneNumber);
  1033. //no extension. what is extension?
  1034. //ppn->szExtension[0] = '\0';
  1035. //GlobalFree(pszPhone);
  1036. return TRUE;
  1037. error:
  1038. // This means number is not canonical. Set it as local number anyway!
  1039. // memset(ppn, 0, sizeof(*ppn));
  1040. // Bug#422: need to strip stuff after : or dial fails!!
  1041. pszNext = _tcschr(pszPhone, ':');
  1042. if(pszNext) *pszNext='\0';
  1043. //lstrcpy(ppn->szLocal,pszPhone);
  1044. lstrcpy(prasentry->szLocalPhoneNumber,pszPhone);
  1045. //GlobalFree(pszPhone);
  1046. return TRUE;
  1047. }
  1048. // ############################################################################
  1049. int Sz2W (LPCTSTR szBuf)
  1050. {
  1051. DWORD dw;
  1052. if (FSz2Dw(szBuf,&dw))
  1053. {
  1054. return (WORD)dw;
  1055. }
  1056. return 0;
  1057. }
  1058. // ############################################################################
  1059. int FIsDigit( int c )
  1060. {
  1061. TCHAR szIn[2];
  1062. WORD rwOut[2];
  1063. szIn[0] = (TCHAR)c;
  1064. szIn[1] = '\0';
  1065. GetStringTypeEx(LOCALE_USER_DEFAULT,CT_CTYPE1,szIn,-1,rwOut);
  1066. return rwOut[0] & C1_DIGIT;
  1067. }
  1068. // ############################################################################
  1069. void *MyMemCpy(void *dest,const void *src, size_t count)
  1070. {
  1071. LPBYTE pbDest = (LPBYTE)dest;
  1072. LPBYTE pbSrc = (LPBYTE)src;
  1073. LPBYTE pbEnd = (LPBYTE)((DWORD_PTR)src + count);
  1074. while (pbSrc < pbEnd)
  1075. {
  1076. *pbDest = *pbSrc;
  1077. pbSrc++;
  1078. pbDest++;
  1079. }
  1080. return dest;
  1081. }
  1082. // ############################################################################
  1083. HRESULT ReadSignUpReg(LPBYTE lpbData, DWORD *pdwSize, DWORD dwType, LPCTSTR pszKey)
  1084. {
  1085. HRESULT hr = ERROR_ACCESS_DENIED;
  1086. HKEY hKey = 0;
  1087. hr = RegOpenKey(HKEY_LOCAL_MACHINE,SIGNUPKEY,&hKey);
  1088. if (hr != ERROR_SUCCESS) goto ReadSignUpRegExit;
  1089. hr = RegQueryValueEx(hKey,pszKey,0,&dwType,lpbData,pdwSize);
  1090. ReadSignUpRegExit:
  1091. if (hKey) RegCloseKey (hKey);
  1092. return hr;
  1093. }
  1094. // ############################################################################
  1095. HRESULT StoreInSignUpReg(LPBYTE lpbData, DWORD dwSize, DWORD dwType, LPCTSTR pszKey)
  1096. {
  1097. HRESULT hr = ERROR_ACCESS_DENIED;
  1098. HKEY hKey = 0;
  1099. hr = RegOpenKey(HKEY_LOCAL_MACHINE,SIGNUPKEY,&hKey);
  1100. if (hr != ERROR_SUCCESS) goto ReadSignUpRegExit;
  1101. hr = RegSetValueEx(hKey,pszKey,0,dwType,lpbData,sizeof(TCHAR)*dwSize);
  1102. ReadSignUpRegExit:
  1103. if (hKey) RegCloseKey (hKey);
  1104. return hr;
  1105. }
  1106. // ############################################################################
  1107. PTSTR GetNextNumericChunk(PTSTR psz, PTSTR pszLim, PTSTR* ppszNext)
  1108. {
  1109. PTSTR pszEnd;
  1110. // init for error case
  1111. *ppszNext = NULL;
  1112. // skip non numerics if any to start of next numeric chunk
  1113. while(*psz<'0' || *psz>'9')
  1114. {
  1115. if(psz >= pszLim) return NULL;
  1116. psz++;
  1117. }
  1118. // skip all numerics to end of country code
  1119. for(pszEnd=psz; *pszEnd>='0' && *pszEnd<='9' && pszEnd<pszLim; pszEnd++)
  1120. ;
  1121. // zap whatever delimiter there was to terminate this chunk
  1122. *pszEnd++ = '\0';
  1123. // return ptr to next chunk (pszEnd now points to it)
  1124. if(pszEnd<pszLim)
  1125. *ppszNext = pszEnd;
  1126. return psz; // return ptr to start of chunk
  1127. }