Source code of Windows XP (NT5)
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.

359 lines
9.8 KiB

  1. #include "stdafx.h"
  2. #include <time.h>
  3. #include <stdlib.h>
  4. #include <ole2.h>
  5. #include "iadmw.h"
  6. #include "iiscnfgp.h"
  7. #include "mdkey.h"
  8. #include "mdentry.h"
  9. #include "mdacl.h"
  10. #include "svc.h"
  11. #include "dllmain.h"
  12. #define PORT_NUM_MIN 2000
  13. #define PORT_NUM_MAX 9999
  14. extern OCMANAGER_ROUTINES gHelperRoutines;
  15. void CreateHTMLALink(int iPort);
  16. void RetrieveHTMLAInstanceNumFromMetabase(OUT TCHAR *p);
  17. int RetrieveHTMLAPortNumFromMetabase();
  18. INT Register_iis_htmla()
  19. {
  20. UINT i = 1; // instance number is in range of 1 - 4 billion
  21. UINT n, iPort;
  22. CString csRoot = _T("LM/W3SVC");
  23. CString strBuf;
  24. CString csPathRoot;
  25. ACTION_TYPE atHTMLA = GetSubcompAction(_T("iis_htmla"), TRUE);
  26. ProcessSection(g_pTheApp->m_hInfHandle, _T("register_iis_htmla_before"));
  27. #ifndef _CHICAGO_
  28. //
  29. // HTMLA instance use a random port number
  30. // this port number is in the range of 2000 - 9999
  31. //
  32. srand((unsigned)time(NULL));
  33. iPort = (UINT) ( ((float)rand() / (float)RAND_MAX ) * (PORT_NUM_MAX - PORT_NUM_MIN) + PORT_NUM_MIN);
  34. //
  35. // WARNING on upgrade. Get the next open instance number!!!!!!!
  36. //
  37. n = GetInstNumber(csRoot, i);
  38. strBuf.Format( _T("%d"), n );
  39. //
  40. // Check if there is an existing HTMLA deal in the metabase!
  41. //
  42. // if this is upgrading from win95, then make sure to write the acl...
  43. if (!g_pTheApp->m_bWin95Migration)
  44. {
  45. if (g_pTheApp->m_bUpgradeTypeHasMetabaseFlag)
  46. {
  47. TCHAR szInst[_MAX_PATH];
  48. RetrieveHTMLAInstanceNumFromMetabase(szInst);
  49. if (*szInst)
  50. {
  51. // let's use this Instance number instead of the one that we already have (next open one)
  52. strBuf = szInst;
  53. if (IsValidNumber((LPCTSTR)strBuf))
  54. {
  55. n=_ttoi((LPCTSTR)strBuf);
  56. }
  57. }
  58. // HTMLA link in IISv4.0 is in "Start Menu/Programs/Microsoft Internet Information Server"
  59. // In IISv5.0, we need to set it in "Start Menu/Programs/Administrative Tools"
  60. UINT iTempPort = 0;
  61. iTempPort = RetrieveHTMLAPortNumFromMetabase();
  62. if (iTempPort != 0){iPort = iTempPort;}
  63. }
  64. }
  65. AdvanceProgressBarTickGauge();
  66. //
  67. // Create "LM/W3SVC/N"
  68. //
  69. CString csPath = csRoot;
  70. csPath += _T("/");
  71. csPath += strBuf; // "LM/W3SVC/N"
  72. CMapStringToString HTMLA_VRootsMap;
  73. CString name, value;
  74. POSITION pos;
  75. name = _T("/");
  76. value.Format(_T("%s\\iisadmin,,%x"), g_pTheApp->m_csPathInetsrv, MD_ACCESS_SCRIPT | MD_ACCESS_READ);
  77. HTMLA_VRootsMap.SetAt(name, value);
  78. name = _T("/iisadmin");
  79. value.Format(_T("%s\\iisadmin,,%x"), g_pTheApp->m_csPathInetsrv, MD_ACCESS_SCRIPT | MD_ACCESS_READ);
  80. HTMLA_VRootsMap.SetAt(name, value);
  81. name = _T("/iisHelp");
  82. value.Format(_T("%s\\Help\\iishelp,,%x"), g_pTheApp->m_csWinDir, MD_ACCESS_SCRIPT | MD_ACCESS_READ);
  83. HTMLA_VRootsMap.SetAt(name, value);
  84. pos = HTMLA_VRootsMap.GetStartPosition();
  85. while (pos)
  86. {
  87. HTMLA_VRootsMap.GetNextAssoc(pos, name, value);
  88. CreateMDVRootTree(csPath, name, value, NULL,n);
  89. AdvanceProgressBarTickGauge();
  90. }
  91. SetAppFriendlyName(csPath);
  92. //
  93. // "LM/W3SVC/N"
  94. //
  95. WriteToMD_IIsWebServerInstance_WWW(csPath);
  96. WriteToMD_NotDeleteAble(csPath);
  97. WriteToMD_ServerSize(csPath);
  98. WriteToMD_ServerComment(csPath, IDS_ADMIN_SERVER_COMMENT);
  99. WriteToMD_ServerBindings_HTMLA(csPath, iPort);
  100. WriteToMD_CertMapper(csPath);
  101. //
  102. // "LM/W3SVC/N/Root"
  103. //
  104. if (g_pTheApp->m_eOS == OS_NT && g_pTheApp->m_eNTOSType != OT_NTW)
  105. {
  106. csPathRoot = csPath + _T("/Root");
  107. // Restrict the Access to the admin instance
  108. SetIISADMINRestriction(csPathRoot);
  109. WriteToMD_Authorization(csPathRoot, MD_AUTH_NT);
  110. // bug299809
  111. // removed per bug317177
  112. //CString csHttpCustomString;
  113. //MyLoadString(IDS_HTTPCUSTOM_UTF8, csHttpCustomString);
  114. //WriteToMD_HttpCustom(csPathRoot, csHttpCustomString, TRUE);
  115. }
  116. //
  117. // "LM/W3SVC/N/Root"
  118. //
  119. // Add this extra asp thingy only for htmla
  120. // commented out for Beta3, will re-enable after beta3
  121. // bug#316682
  122. // re-enabled per bug#340576
  123. // removed per bug#340576
  124. //csPathRoot = csPath + _T("/Root");
  125. //WriteToMD_AspCodepage(csPathRoot, 65001, FALSE);
  126. // Add this extra asp thingy only for htmla
  127. csPathRoot = csPath + _T("/Root");
  128. WriteToMD_EnableParentPaths_WWW(csPathRoot, TRUE);
  129. //
  130. // "LM/W3SVC"
  131. //
  132. WriteToMD_AdminInstance(csRoot, strBuf);
  133. CreateHTMLALink(iPort);
  134. AdvanceProgressBarTickGauge();
  135. #endif // _CHICAGO_
  136. ProcessSection(g_pTheApp->m_hInfHandle, _T("register_iis_htmla_after"));
  137. return (0);
  138. }
  139. void CreateHTMLALink(IN int iPort)
  140. /*++
  141. Routine Description:
  142. This function creates the HTMLA link in the "Start Menu/Programs/Administrative Tools".
  143. The link invokes: http://localhost:<iPort>
  144. Arguments:
  145. iPort is the random port number used by the HTMLA instance.
  146. It is in the range of 2000 - 9999. We create the link only when iPort is valid.
  147. Return Value:
  148. void
  149. --*/
  150. {
  151. if (iPort >= PORT_NUM_MIN && iPort <= PORT_NUM_MAX)
  152. {
  153. TCHAR szIPort[20];
  154. _stprintf(szIPort, _T("%d"), iPort);
  155. SetupSetStringId_Wrapper(g_pTheApp->m_hInfHandle, 34001, szIPort);
  156. ProcessSection(g_pTheApp->m_hInfHandle, _T("register_iis_htmla_1_link"));
  157. }
  158. return;
  159. }
  160. INT Unregister_iis_htmla()
  161. {
  162. TCHAR szInst[_MAX_PATH];
  163. ProcessSection(g_pTheApp->m_hInfHandle, _T("unregister_iis_htmla_before"));
  164. AdvanceProgressBarTickGauge();
  165. if (CheckifServiceExist(_T("IISADMIN")) == 0 )
  166. {
  167. RetrieveHTMLAInstanceNumFromMetabase(szInst);
  168. if (*szInst)
  169. {
  170. CString csPath = _T("LM/W3SVC/");
  171. csPath += szInst;
  172. DeleteInProc(csPath);
  173. CMDKey cmdKey;
  174. cmdKey.OpenNode(_T("LM/W3SVC"));
  175. if ( (METADATA_HANDLE)cmdKey )
  176. {
  177. cmdKey.DeleteNode(szInst); // delete the admin instance
  178. cmdKey.DeleteData(MD_ADMIN_INSTANCE, STRING_METADATA); // delete the admin instance pointer
  179. cmdKey.Close();
  180. }
  181. AdvanceProgressBarTickGauge();
  182. }
  183. }
  184. ProcessSection(g_pTheApp->m_hInfHandle, _T("unregister_iis_htmla_1"));
  185. AdvanceProgressBarTickGauge();
  186. ProcessSection(g_pTheApp->m_hInfHandle, _T("unregister_iis_htmla_after"));
  187. AdvanceProgressBarTickGauge();
  188. return (0);
  189. }
  190. void RetrieveHTMLAInstanceNumFromMetabase(OUT TCHAR *p)
  191. /*++
  192. Routine Description:
  193. This function retrieves the instance number for the HTMLA instance from the Metabase.
  194. The instance number is in the form of a string, e.g., "12".
  195. If succeeds, p will hold this string.
  196. oherwise, p will hold an empty string.
  197. Arguments:
  198. Pointer to a string buffer to receive the instance number.
  199. Return Value:
  200. void
  201. --*/
  202. {
  203. iisDebugOut_Start(_T("RetrieveHTMLAInstanceNumFromMetabase"));
  204. TCHAR szInst[_MAX_PATH] = _T("");;
  205. CMDKey cmdKey;
  206. cmdKey.OpenNode(_T("LM/W3SVC"));
  207. if ( (METADATA_HANDLE)cmdKey )
  208. {
  209. DWORD attr, uType, dType, cbLen;
  210. cmdKey.GetData(MD_ADMIN_INSTANCE, &attr, &uType, &dType, &cbLen, (PBYTE)szInst, _MAX_PATH);
  211. cmdKey.Close();
  212. }
  213. _tcscpy(p, szInst);
  214. iisDebugOut_End1(_T("RetrieveHTMLAInstanceNumFromMetabase"),p);
  215. return;
  216. }
  217. int RetrieveHTMLAPortNumFromMetabase()
  218. /*++
  219. Routine Description:
  220. This function retrieves the random Port number for HTMLA instance from the Metabase.
  221. If succeeds, return the random Port number found;
  222. oherwise, return 0.
  223. Arguments:
  224. None
  225. Return Value:
  226. int - the Port number
  227. --*/
  228. {
  229. iisDebugOut_Start(_T("RetrieveHTMLAPortNumFromMetabase"));
  230. BOOL bFound = FALSE;
  231. int iPort = 0;
  232. DWORD attr, uType, dType, cbLen;
  233. BUFFER bufData;
  234. PBYTE pData;
  235. int BufSize;
  236. LPTSTR p;
  237. TCHAR szInst[_MAX_PATH];
  238. RetrieveHTMLAInstanceNumFromMetabase(szInst);
  239. if (*szInst)
  240. {
  241. CString csPath = _T("LM/W3SVC/");
  242. csPath += szInst;
  243. CMDKey cmdKey;
  244. cmdKey.OpenNode(csPath); //Open "LM/W3SVC/N"
  245. //
  246. // MD_SERVER_BINDINGS is a MULTI_SZ string,
  247. // each one of them has the format of xxx:<Port>:xxx
  248. // our goal is to get the <Port>
  249. //
  250. if ( (METADATA_HANDLE)cmdKey )
  251. {
  252. pData = (PBYTE)(bufData.QueryPtr());
  253. BufSize = bufData.QuerySize();
  254. cbLen = 0;
  255. bFound = cmdKey.GetData(MD_SERVER_BINDINGS, &attr, &uType, &dType, &cbLen, pData, BufSize);
  256. if (!bFound && (cbLen > 0))
  257. {
  258. if ( bufData.Resize(cbLen) )
  259. {
  260. pData = (PBYTE)(bufData.QueryPtr());
  261. BufSize = cbLen;
  262. cbLen = 0;
  263. bFound = cmdKey.GetData(MD_SERVER_BINDINGS, &attr, &uType, &dType, &cbLen, pData, BufSize);
  264. }
  265. }
  266. cmdKey.Close();
  267. }
  268. }
  269. if (bFound && (dType == MULTISZ_METADATA))
  270. {
  271. iisDebugOut((LOG_TYPE_TRACE, _T("RetrieveHTMLAPortNumFromMetabase():Found a previous HTMLA port num.\n")));
  272. p = (LPTSTR)pData;
  273. //
  274. // each string in pData is in the form of xxx:<Port>:xxx
  275. // retrieve port number from the first string
  276. //
  277. iisDebugOut((LOG_TYPE_TRACE, _T("RetrieveHTMLAPortNumFromMetabase():entry=%s.\n"),p));
  278. TCHAR *token;
  279. token = _tcstok(p, _T(":"));
  280. if (token)
  281. {
  282. iisDebugOut((LOG_TYPE_TRACE, _T("RetrieveHTMLAPortNumFromMetabase():token=%s.\n"),token));
  283. if (IsValidNumber((LPCTSTR)token)) {iPort = _ttoi(token);}
  284. }
  285. }
  286. iisDebugOut((LOG_TYPE_TRACE, _T("RetrieveHTMLAPortNumFromMetabase():Port=%d\n"), iPort));
  287. return iPort;
  288. }