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.

198 lines
6.6 KiB

  1. /*****************************************************************************\
  2. FILE: INStoXML.cpp
  3. DESCRIPTION:
  4. This code will convert an INS (internet settings) or ISP (Internet
  5. Service Provider) file to an Account AutoDiscovery XML file.
  6. BryanSt 11/8/1999
  7. Copyright (C) Microsoft Corp 1999-1999. All rights reserved.
  8. \*****************************************************************************/
  9. #include "priv.h"
  10. #include <cowsite.h>
  11. #include <atlbase.h>
  12. #include "util.h"
  13. #include "AutoDiscover.h"
  14. #define XML_POST_REQUEST L"<?xml version=\"1.0\"?>\r\n<AUTODISCOVERY Version=\"0.1\">\r\n\t<REQUEST ACCOUNTTYPE=\"EMail\" EMAIL=\"%ls\">\r\n\t</REQUEST>\r\n</AUTODISCOVERY>\r\n"
  15. #define XML_TEMPLATE_DISPNAME "<USER DISPLAYNAME=\"%ls\" />"
  16. #define XML_TEMPLATE_ACCOUNT "<?xml version=\"1.0\"?>\r\n<AUTODISCOVERY Version=\"0.1\">\r\n %hs\r\n <ACCOUNT TYPE=\"EMail\" EXPIRE=\"7\">\r\n <PROTOCOL TYPE=\"POP3\" SERVERNAME=\"%ls\">\r\n <AUTH>\r\n <PASSWORD TYPE=\"%ls\" />\r\n </AUTH>\r\n </PROTOCOL>\r\n <PROTOCOL TYPE=\"SMTP\" SERVERNAME=\"%ls\">\r\n <AUTH>\r\n <PASSWORD TYPE=\"%ls\" />\r\n </AUTH>\r\n </PROTOCOL>\r\n </ACCOUNT>\r\n</AUTODISCOVERY>"
  17. #define STR_SECTION_INET_MAIL TEXT("Internet_Mail")
  18. #define STR_INIKEY_POP_SERVER TEXT("POP_Server")
  19. #define STR_INIKEY_POP_NAME TEXT("POP_Logon_Name")
  20. #define STR_INIKEY_SMTP_SERVER TEXT("SMTP_Server")
  21. #define STR_INIKEY_AUTH_TYPE TEXT("Logon_Using_SPA")
  22. #define STR_INIKEY_EMAIL_NAME TEXT("Email_Name") // DisplayName
  23. #define STR_TYPE_POP3 TEXT("POP3")
  24. #define STR_YES TEXT("yes")
  25. #define STR_SPA TEXT("SPA")
  26. typedef struct tagAUTODISCOVERYSETTINGS
  27. {
  28. LPWSTR pwszType;
  29. LPWSTR pwszPOPServer;
  30. LPWSTR pwszPOPServerLoginName;
  31. LPWSTR pwszSMTPServer;
  32. LPWSTR pwszDisplayName;
  33. LPWSTR pwszAuthType;
  34. } AUTODISCOVERYSETTINGS;
  35. //===========================
  36. // *** API Internals & Helpers ***
  37. //===========================
  38. HRESULT _GetAutoDiscSettings(LPCWSTR pwszINSFile, AUTODISCOVERYSETTINGS * pads)
  39. {
  40. WCHAR wzTemp[MAX_URL_STRING];
  41. HRESULT hr = GetPrivateProfileStringHrWrap(STR_SECTION_INET_MAIL, STR_INIKEY_POP_SERVER, L"", wzTemp, ARRAYSIZE(wzTemp), pwszINSFile);
  42. if (SUCCEEDED(hr) && wzTemp[0])
  43. {
  44. pads->pwszPOPServer = StrDupW(wzTemp);
  45. hr = ((pads->pwszPOPServer) ? S_OK : E_OUTOFMEMORY);
  46. if (SUCCEEDED(hr))
  47. {
  48. pads->pwszType = StrDupW(STR_TYPE_POP3);
  49. hr = ((pads->pwszType) ? S_OK : E_OUTOFMEMORY);
  50. }
  51. }
  52. hr = GetPrivateProfileStringHrWrap(STR_SECTION_INET_MAIL, STR_INIKEY_POP_NAME, L"", wzTemp, ARRAYSIZE(wzTemp), pwszINSFile);
  53. if (SUCCEEDED(hr) && wzTemp[0])
  54. {
  55. pads->pwszPOPServerLoginName = StrDupW(wzTemp);
  56. hr = ((pads->pwszPOPServerLoginName) ? S_OK : E_OUTOFMEMORY);
  57. }
  58. hr = GetPrivateProfileStringHrWrap(STR_SECTION_INET_MAIL, STR_INIKEY_SMTP_SERVER, L"", wzTemp, ARRAYSIZE(wzTemp), pwszINSFile);
  59. if (SUCCEEDED(hr) && wzTemp[0])
  60. {
  61. pads->pwszSMTPServer = StrDupW(wzTemp);
  62. hr = ((pads->pwszSMTPServer) ? S_OK : E_OUTOFMEMORY);
  63. }
  64. hr = GetPrivateProfileStringHrWrap(STR_SECTION_INET_MAIL, STR_INIKEY_AUTH_TYPE, L"", wzTemp, ARRAYSIZE(wzTemp), pwszINSFile);
  65. if (SUCCEEDED(hr) && wzTemp[0] && StrCmpIW(wzTemp, STR_YES))
  66. {
  67. pads->pwszAuthType = StrDupW(STR_SPA);
  68. hr = ((pads->pwszAuthType) ? S_OK : E_OUTOFMEMORY);
  69. }
  70. hr = GetPrivateProfileStringHrWrap(STR_SECTION_INET_MAIL, STR_INIKEY_EMAIL_NAME, L"", wzTemp, ARRAYSIZE(wzTemp), pwszINSFile);
  71. if (SUCCEEDED(hr) && wzTemp[0])
  72. {
  73. pads->pwszDisplayName = StrDupW(wzTemp);
  74. hr = ((pads->pwszDisplayName) ? S_OK : E_OUTOFMEMORY);
  75. }
  76. return hr;
  77. }
  78. HRESULT _CreateXMLFromSettings(AUTODISCOVERYSETTINGS * pads, LPSTR * ppszXML)
  79. {
  80. CHAR szUserTagXML[MAX_URL_STRING];
  81. CHAR szAccountXML[MAX_URL_STRING * 4];
  82. HRESULT hr = S_OK;
  83. szUserTagXML[0] = 0;
  84. szAccountXML[0] = 0;
  85. if (pads->pwszDisplayName)
  86. {
  87. wnsprintfA(szUserTagXML, ARRAYSIZE(szUserTagXML), XML_TEMPLATE_DISPNAME, pads->pwszDisplayName);
  88. }
  89. // TODO: Do we need to add the POP Login Name?
  90. wnsprintfA(szAccountXML, ARRAYSIZE(szAccountXML), XML_TEMPLATE_ACCOUNT, szUserTagXML, pads->pwszPOPServer, pads->pwszAuthType, pads->pwszSMTPServer, pads->pwszAuthType);
  91. *ppszXML = StrDupA(szAccountXML);
  92. hr = ((*ppszXML) ? S_OK : E_OUTOFMEMORY);
  93. return hr;
  94. }
  95. HRESULT _SaveToFile(LPCWSTR pwszFileName, LPCSTR pszXML)
  96. {
  97. HANDLE hFile;
  98. HRESULT hr = CreateFileHrWrap(pwszFileName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL, &hFile);
  99. if (SUCCEEDED(hr))
  100. {
  101. DWORD chFSWritten;
  102. hr = WriteFileWrap(hFile, pszXML, lstrlenA(pszXML), &chFSWritten, NULL);
  103. CloseHandle(hFile);
  104. }
  105. return hr;
  106. }
  107. HRESULT _FreeAutoDiscSettings(AUTODISCOVERYSETTINGS * pads)
  108. {
  109. if (pads->pwszType) LocalFree(pads->pwszType);
  110. if (pads->pwszPOPServer) LocalFree(pads->pwszPOPServer);
  111. if (pads->pwszPOPServerLoginName) LocalFree(pads->pwszPOPServerLoginName);
  112. if (pads->pwszSMTPServer) LocalFree(pads->pwszSMTPServer);
  113. if (pads->pwszDisplayName) LocalFree(pads->pwszDisplayName);
  114. if (pads->pwszAuthType) LocalFree(pads->pwszAuthType);
  115. return S_OK;
  116. }
  117. //===========================
  118. // *** APIs ***
  119. //===========================
  120. bool IsINSFile(LPCWSTR pwszINSFile)
  121. {
  122. WCHAR wzPOPServer[MAX_PATH];
  123. bool fIsINSFile = false;
  124. HRESULT hr = GetPrivateProfileStringHrWrap(STR_SECTION_INET_MAIL, STR_INIKEY_POP_SERVER, L"", wzPOPServer, ARRAYSIZE(wzPOPServer), pwszINSFile);
  125. if (SUCCEEDED(hr) && wzPOPServer[0])
  126. {
  127. // We consider this a valid Account AutoDiscovery compatible INS/ISP
  128. // file if it has:
  129. // [Internet_Mail]
  130. // POP_Server=xxxxx
  131. fIsINSFile = true;
  132. }
  133. return fIsINSFile;
  134. }
  135. HRESULT ConvertINSToXML(LPCWSTR pwszINSFile)
  136. {
  137. AUTODISCOVERYSETTINGS ads = {0};
  138. HRESULT hr = _GetAutoDiscSettings(pwszINSFile, &ads);
  139. if (SUCCEEDED(hr))
  140. {
  141. LPSTR pszXML;
  142. hr = _CreateXMLFromSettings(&ads, &pszXML);
  143. if (SUCCEEDED(hr))
  144. {
  145. hr = _SaveToFile(pwszINSFile, pszXML);
  146. LocalFree(pszXML);
  147. }
  148. _FreeAutoDiscSettings(&ads);
  149. }
  150. return hr;
  151. }