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.

395 lines
12 KiB

  1. #include "stdafx.h"
  2. #include <ole2.h>
  3. #undef UNICODE
  4. #include "iadm.h"
  5. #define UNICODE
  6. #include "iiscnfg.h"
  7. #include "mdkey.h"
  8. #include "mdentry.h"
  9. #include "utils.h"
  10. #include "regctrl.h"
  11. #include "userenv.h"
  12. #include "userenvp.h"
  13. GUID g_SMTPGuid = { 0x475e3e80, 0x3193, 0x11cf, 0xa7, 0xd8,
  14. 0x00, 0x80, 0x5f, 0x48, 0xa1, 0x35 };
  15. static TCHAR szShortSvcName[] = _T("SMTP");
  16. INT Register_iis_smtp_nt5(BOOL fUpgrade, BOOL fReinstall)
  17. {
  18. INT err = NERR_Success;
  19. CString csBinPath;
  20. BOOL fSvcExist = FALSE;
  21. BOOL fIISADMINExists = DetectExistingIISADMIN();
  22. if (fReinstall)
  23. return err;
  24. do {
  25. // set up registry values
  26. CRegKey regMachine = HKEY_LOCAL_MACHINE;
  27. // System\CurrentControlSet\Services\SMTPSVC\Parameters
  28. InsertSetupString( (LPCSTR) REG_SMTPPARAMETERS );
  29. // Software\Microsoft\Keyring\Parameters
  30. CString csSmtpkeyDll;
  31. CRegKey regKeyring( REG_KEYRING, regMachine );
  32. if ((HKEY) regKeyring )
  33. {
  34. csSmtpkeyDll = theApp.m_csPathInetsrv;
  35. csSmtpkeyDll += _T("\\smtpkey.dll");
  36. regKeyring.SetValue( szShortSvcName, csSmtpkeyDll );
  37. }
  38. // If we are upgrading, we will first delete the service and re-register
  39. if (fUpgrade)
  40. {
  41. InetDeleteService(SZ_SMTPSERVICENAME);
  42. InetRegisterService( theApp.m_csMachineName,
  43. SZ_SMTPSERVICENAME,
  44. &g_SMTPGuid, 0, 25, FALSE );
  45. }
  46. // Create or Config SMTP service
  47. CString csDisplayName;
  48. CString csDescription;
  49. MyLoadString( IDS_SMTPDISPLAYNAME, csDisplayName );
  50. MyLoadString(IDS_SMTPDESCRIPTION, csDescription);
  51. csBinPath = theApp.m_csPathInetsrv + _T("\\inetinfo.exe") ;
  52. err = InetCreateService(SZ_SMTPSERVICENAME,
  53. (LPCTSTR)csDisplayName,
  54. (LPCTSTR)csBinPath,
  55. theApp.m_fSuppressSmtp ? SERVICE_DISABLED : SERVICE_AUTO_START,
  56. SZ_SVC_DEPEND,
  57. (LPCTSTR)csDescription);
  58. if ( err != NERR_Success )
  59. {
  60. if (err == ERROR_SERVICE_EXISTS)
  61. {
  62. fSvcExist = TRUE;
  63. err = InetConfigService(SZ_SMTPSERVICENAME,
  64. (LPCTSTR)csDisplayName,
  65. (LPCTSTR)csBinPath,
  66. SZ_SVC_DEPEND,
  67. (LPCTSTR)csDescription);
  68. if (err != NERR_Success)
  69. {
  70. SetErrMsg(_T("SMTP InetConfigService failed"), err);
  71. }
  72. }
  73. }
  74. if (fIISADMINExists)
  75. {
  76. // Migrate registry keys to the metabase. Or create from default values
  77. // if fresh install
  78. MigrateIMSToMD(theApp.m_hInfHandle[MC_IMS],
  79. SZ_SMTPSERVICENAME,
  80. _T("SMTP_REG"),
  81. MDID_SMTP_ROUTING_SOURCES,
  82. fUpgrade);
  83. SetAdminACL_wrap(_T("LM/SMTPSVC/1"), (MD_ACR_READ | MD_ACR_ENUM_KEYS), TRUE);
  84. SetAdminACL_wrap(_T("LM/SMTPSVC"), (MD_ACR_READ | MD_ACR_ENUM_KEYS), TRUE);
  85. }
  86. // Create key \System\CurrentControlSet\Services\SmtpSvc\Performance:
  87. // Add the following values:
  88. // Library = smtpctrs.DLL
  89. // Open = OpenSMTPPerformanceData
  90. // Close = CloseSMTPPerformanceData
  91. // Collect = CollectSMTPPerformanceData
  92. InstallPerformance(REG_SMTPPERFORMANCE,
  93. _T("smtpctrs.DLL"),
  94. _T("OpenSmtpPerformanceData"),
  95. _T("CloseSmtpPerformanceData"),
  96. _T("CollectSmtpPerformanceData"));
  97. InstallPerformance(REG_NTFSPERFORMANCE,
  98. _T("snprfdll.DLL"),
  99. _T("NTFSDrvOpen"),
  100. _T("NTFSDrvClose"),
  101. _T("NTFSDrvCollect"));
  102. //
  103. // We used to register the SMTPB agent here. Now we unregister it in
  104. // case we're upgrading since it's no longer supported
  105. //
  106. RemoveAgent( SZ_SMTPSERVICENAME );
  107. // Create key \System\CurrentControlSet\Services\EventLog\System\SmtpSvc:
  108. // Add the following values:
  109. // EventMessageFile = ..\smtpmsg.dll
  110. // TypesSupported = 7
  111. csBinPath = theApp.m_csPathInetsrv + _T("\\smtpsvc.dll");
  112. AddEventLog( SZ_SMTPSERVICENAME, csBinPath, 0x07 );
  113. if (!fSvcExist)
  114. {
  115. InetRegisterService( theApp.m_csMachineName,
  116. SZ_SMTPSERVICENAME,
  117. &g_SMTPGuid, 0, 25, TRUE );
  118. }
  119. // Unload the counters and then reload them
  120. err = unlodctr( SZ_SMTPSERVICENAME );
  121. err = unlodctr( SZ_NTFSDRVSERVICENAME );
  122. err = lodctr(_T("smtpctrs.ini"));
  123. err = lodctr(_T("ntfsdrct.ini"));
  124. // register OLE objects
  125. SetEnvironmentVariable(_T("__SYSDIR"), theApp.m_csSysDir);
  126. SetEnvironmentVariable(_T("__INETSRV"), theApp.m_csPathInetsrv);
  127. err = (INT)RegisterOLEControlsFromInfSection(theApp.m_hInfHandle[MC_IMS],
  128. _T("SMTP_REGISTER"),
  129. TRUE);
  130. SetEnvironmentVariable(_T("__SYSDIR"), NULL);
  131. SetEnvironmentVariable(_T("__INETSRV"), NULL);
  132. // Server Events: We are clean installing MCIS, so we make sure we set up
  133. // everything, including the source type and event types.
  134. RegisterSEOForSmtp(TRUE);
  135. } while ( 0 );
  136. return err;
  137. }
  138. INT Unregister_iis_smtp()
  139. {
  140. CRegKey regMachine = HKEY_LOCAL_MACHINE;
  141. INT err = NERR_Success;
  142. // Unregister all of the NNTP sources in the SEO binding database
  143. UnregisterSEOSourcesForSMTP();
  144. // Unregister the OLE objets
  145. SetEnvironmentVariable(_T("__SYSDIR"), theApp.m_csSysDir);
  146. SetEnvironmentVariable(_T("__INETSRV"), theApp.m_csPathInetsrv);
  147. err = (INT)RegisterOLEControlsFromInfSection(theApp.m_hInfHandle[MC_IMS],
  148. _T("SMTP_K2_UNREGISTER"),
  149. FALSE);
  150. err = RegisterOLEControlsFromInfSection(theApp.m_hInfHandle[MC_IMS],
  151. _T("SMTP_UNREGISTER"),
  152. FALSE);
  153. SetEnvironmentVariable(_T("__SYSDIR"), NULL);
  154. SetEnvironmentVariable(_T("__INETSRV"), NULL);
  155. // Bug 51537: Remove MIB from K2 SMTP
  156. RemoveAgent( SZ_SMTPSERVICENAME );
  157. RemoveEventLog( SZ_SMTPSERVICENAME );
  158. err = unlodctr( SZ_SMTPSERVICENAME );
  159. err = unlodctr( SZ_NTFSDRVSERVICENAME );
  160. InetDeleteService(SZ_SMTPSERVICENAME);
  161. InetRegisterService( theApp.m_csMachineName,
  162. SZ_SMTPSERVICENAME,
  163. &g_SMTPGuid, 0, 25, FALSE );
  164. // Blow away the Services\SMTPSVC registry key
  165. CRegKey RegSvcs(HKEY_LOCAL_MACHINE, REG_SERVICES);
  166. if ((HKEY)RegSvcs)
  167. {
  168. RegSvcs.DeleteTree(SZ_SMTPSERVICENAME);
  169. RegSvcs.DeleteTree(SZ_NTFSDRVSERVICENAME);
  170. }
  171. // Blow away SMTP key manager
  172. CRegKey regKeyring( HKEY_LOCAL_MACHINE, REG_KEYRING );
  173. if ((HKEY) regKeyring )
  174. {
  175. regKeyring.DeleteValue(szShortSvcName);
  176. }
  177. // remove LM/SMTPSVC in the metabase
  178. if (DetectExistingIISADMIN())
  179. {
  180. CMDKey cmdKey;
  181. cmdKey.OpenNode(_T("LM"));
  182. if ( (METADATA_HANDLE)cmdKey ) {
  183. cmdKey.DeleteNode(SZ_SMTPSERVICENAME);
  184. cmdKey.Close();
  185. }
  186. }
  187. // remove K2 items from the program groups
  188. RemoveInternetShortcut(MC_IMS,
  189. IDS_PROGITEM_MAIL_SMTP_WEBADMIN,
  190. FALSE);
  191. RemoveInternetShortcut(MC_IMS,
  192. IDS_PROGITEM_MAIL_README,
  193. FALSE);
  194. RemoveInternetShortcut(MC_IMS,
  195. IDS_PROGITEM_MAIL_README_K2,
  196. FALSE);
  197. RemoveInternetShortcut(MC_IMS,
  198. IDS_PROGITEM_MCIS_MAIL_README,
  199. TRUE);
  200. RemoveInternetShortcut(MC_IMS,
  201. IDS_PROGITEM_MAIL_SMTP_WEBADMIN,
  202. TRUE);
  203. //
  204. // remove the one and only webadmin link from "administrative tools"
  205. //
  206. RemoveNt5InternetShortcut(MC_IMS,
  207. IDS_PROGITEM_MAIL_SMTP_WEBADMIN);
  208. return(err);
  209. }
  210. INT Upgrade_iis_smtp_nt5_fromk2(BOOL fFromK2)
  211. {
  212. // This function handles upgrade from NT4 K2, or MCIS 2.0
  213. INT err = NERR_Success;
  214. CString csBinPath;
  215. DebugOutput(_T("Upgrading from %s to B3 ..."), (fFromK2)? _T("NT4 K2") : _T("MCIS 2.0"));
  216. BOOL fSvcExist = FALSE;
  217. BOOL fIISADMINExists = DetectExistingIISADMIN();
  218. // set up registry values
  219. CRegKey regMachine = HKEY_LOCAL_MACHINE;
  220. // System\CurrentControlSet\Services\SMTPSVC\Parameters
  221. InsertSetupString( (LPCSTR) REG_SMTPPARAMETERS );
  222. if (fIISADMINExists)
  223. {
  224. // Migrate registry keys to the metabase. Or create from default values
  225. // if fresh install
  226. MigrateIMSToMD(theApp.m_hInfHandle[MC_IMS],
  227. SZ_SMTPSERVICENAME,
  228. _T("SMTP_REG_UPGRADEK2"),
  229. MDID_SMTP_ROUTING_SOURCES,
  230. TRUE);
  231. // bugbug: x5 bug 72284, nt bug 202496 Uncomment this when NT
  232. // is ready to accept these changes
  233. SetAdminACL_wrap(_T("LM/SMTPSVC/1"), (MD_ACR_READ | MD_ACR_ENUM_KEYS), TRUE);
  234. SetAdminACL_wrap(_T("LM/SMTPSVC"), (MD_ACR_READ | MD_ACR_ENUM_KEYS), TRUE);
  235. }
  236. // Unload the counters and then reload them
  237. err = unlodctr( SZ_SMTPSERVICENAME );
  238. err = unlodctr( SZ_NTFSDRVSERVICENAME );
  239. err = lodctr(_T("smtpctrs.ini"));
  240. err = lodctr(_T("ntfsdrct.ini"));
  241. SetEnvironmentVariable(_T("__SYSDIR"), theApp.m_csSysDir);
  242. SetEnvironmentVariable(_T("__INETSRV"), theApp.m_csPathInetsrv);
  243. err = (INT)RegisterOLEControlsFromInfSection(theApp.m_hInfHandle[MC_IMS],
  244. _T("SMTP_K2_UNREGISTER"),
  245. FALSE);
  246. err = (INT)RegisterOLEControlsFromInfSection(theApp.m_hInfHandle[MC_IMS],
  247. _T("SMTP_REGISTER"),
  248. TRUE);
  249. SetEnvironmentVariable(_T("__SYSDIR"), NULL);
  250. SetEnvironmentVariable(_T("__INETSRV"), NULL);
  251. // Server Events: We are clean installing MCIS, so we make sure we set up
  252. // everything, including the source type and event types.
  253. RegisterSEOForSmtp(TRUE);
  254. if (fFromK2)
  255. {
  256. // upgrade from K2, remove those K2 links
  257. RemoveInternetShortcut(MC_IMS,
  258. IDS_PROGITEM_MAIL_SMTP_WEBADMIN,
  259. FALSE);
  260. RemoveInternetShortcut(MC_IMS,
  261. IDS_PROGITEM_MAIL_README,
  262. FALSE);
  263. RemoveInternetShortcut(MC_IMS,
  264. IDS_PROGITEM_MAIL_README_K2,
  265. FALSE);
  266. }
  267. else
  268. {
  269. // upgrade from MCIS 2.0, remove those MCIS links
  270. RemoveInternetShortcut(MC_IMS,
  271. IDS_PROGITEM_MCIS_MAIL_README,
  272. TRUE);
  273. RemoveInternetShortcut(MC_IMS,
  274. IDS_PROGITEM_MAIL_SMTP_WEBADMIN,
  275. TRUE);
  276. RemoveISMLink();
  277. }
  278. return err;
  279. }
  280. INT Upgrade_iis_smtp_nt5_fromb2(BOOL fFromB2)
  281. {
  282. INT err = NERR_Success;
  283. DebugOutput(_T("Upgrading from NT5 %s to B3 ..."), (fFromB2)? _T("B2") : _T("B3"));
  284. // If it's just upgrades between B3 bits, don't need to do any metabase operations.
  285. if (!fFromB2)
  286. return err;
  287. BOOL fIISADMINExists = DetectExistingIISADMIN();
  288. // set the K2 Upgrade key to true.
  289. if (fIISADMINExists)
  290. {
  291. MigrateIMSToMD( theApp.m_hInfHandle[MC_IMS],
  292. NULL,
  293. _T("SMTP_REG_K2_TO_EE"),
  294. 0,
  295. FALSE,
  296. TRUE );
  297. MigrateIMSToMD( theApp.m_hInfHandle[MC_IMS],
  298. SZ_SMTPSERVICENAME,
  299. _T("SMTP_REG_UPGRADEB2"),
  300. MDID_SMTP_ROUTING_SOURCES,
  301. FALSE );
  302. SetAdminACL_wrap(_T("LM/SMTPSVC/1"), (MD_ACR_READ | MD_ACR_ENUM_KEYS), TRUE);
  303. SetAdminACL_wrap(_T("LM/SMTPSVC"), (MD_ACR_READ | MD_ACR_ENUM_KEYS), TRUE);
  304. }
  305. SetEnvironmentVariable(_T("__SYSDIR"), theApp.m_csSysDir);
  306. SetEnvironmentVariable(_T("__INETSRV"), theApp.m_csPathInetsrv);
  307. err = (INT)RegisterOLEControlsFromInfSection(theApp.m_hInfHandle[MC_IMS],
  308. _T("SMTP_K2_UNREGISTER"),
  309. FALSE);
  310. err = (INT)RegisterOLEControlsFromInfSection(theApp.m_hInfHandle[MC_IMS],
  311. _T("SMTP_REGISTER"),
  312. TRUE);
  313. SetEnvironmentVariable(_T("__SYSDIR"), NULL);
  314. SetEnvironmentVariable(_T("__INETSRV"), NULL);
  315. // Server Events: We are upgrading from K2, so we will register the
  316. // default site (instance) and the MBXSINK binding.
  317. RegisterSEOForSmtp(FALSE);
  318. // System\CurrentControlSet\Services\SMTPSVC\Parameters
  319. InsertSetupString( (LPCSTR) REG_SMTPPARAMETERS );
  320. return err;
  321. }