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.

519 lines
17 KiB

  1. ######################################################
  2. # Company : ActiveState Tool Corp.
  3. # Author : James A. Snyder ( [email protected] )
  4. # Date : 7/11/98
  5. # Copyright � 1998 ActiveState Tool Corp., all rights reserved.
  6. #
  7. ######################################################
  8. # MetabaseConfig.pm
  9. package MetabaseConfig;
  10. use Win32::OLE;
  11. use strict;
  12. eval('use Win32::Service;');
  13. if(Win32::IsWinNT && $@) {
  14. print $@;
  15. }
  16. ######################################################
  17. # ScriptMap flags
  18. sub MD_SCRIPTMAPFLAG_SCRIPT_ENGINE{1};
  19. sub MD_SCRIPTMAPFLAG_CHECK_PATH_INFO{4};
  20. ######################################################
  21. # Access Permission Flags
  22. sub MD_ACCESS_READ { 0x00000001 }; # // Allow for Read
  23. sub MD_ACCESS_WRITE { 0x00000002 }; # // Allow for Write
  24. sub MD_ACCESS_EXECUTE { 0x00000004 }; # // Allow for Execute
  25. sub MD_ACCESS_SCRIPT { 0x00000200 }; # // Allow for Script execution
  26. sub MD_ACCESS_NO_REMOTE_WRITE { 0x00000400 }; # // Local host access only
  27. sub MD_ACCESS_NO_REMOTE_READ { 0x00001000 }; # // Local host access only
  28. sub MD_ACCESS_NO_REMOTE_EXECUTE { 0x00002000 }; # // Local host access only
  29. sub MD_ACCESS_NO_REMOTE_SCRIPT { 0x00004000 }; # // Local host access only
  30. ######################################################
  31. $MetabaseConfig::LogObject = undef;
  32. # Set the reference to the Log object
  33. sub SetLogObject {
  34. $MetabaseConfig::LogObject = shift;
  35. if(!$MetabaseConfig::LogObject->isa("Log")) {
  36. $MetabaseConfig::LogObject = undef;
  37. }
  38. }
  39. $MetabaseConfig::StatusStarted = 4;
  40. $MetabaseConfig::StatusStopped = 1;
  41. sub StopIISAdmin {
  42. my $output = `net stop IISAdmin /y`;
  43. if($?) {
  44. return "oops there was a problem stopping the IISAdmin service\n";
  45. }
  46. $output = `net start`;
  47. my @output = split($/, $output);
  48. my $grep_results = grep(/IIS Admin Service/, @output);
  49. if($grep_results) {
  50. return "oops we thought we stopped the IISAdmin service when we didn't\n";
  51. }
  52. # MetabaseConfig::StopService('W3SVC') || return "Error stopping the W3SVC service";
  53. # MetabaseConfig::StopService('MSFTPSVC') || return "Error stopping the MSFTPSVC service";
  54. # MetabaseConfig::StopService('IISADMIN') || return "Error stopping the IISADMIN service";
  55. # my $result = `net stop IISADMIN /y`;
  56. }
  57. sub StartIISAdmin {
  58. MetabaseConfig::StartService('IISADMIN') || return "Error starting the IISADMIN service";
  59. MetabaseConfig::StartService('W3SVC') || return "Error starting the W3SVC service";
  60. MetabaseConfig::StartService('MSFTPSVC') || return "Error starting the MSFTPSVC service";
  61. # my $result = `net start IISADMIN /y`;
  62. # $result = `net start W3SVC /y`;
  63. # $result = `net start MSFTPSVC /y`;
  64. }
  65. ######################################################
  66. # StopIISAdmin();
  67. sub StopService {
  68. my $service = shift;
  69. my $status = {};
  70. my $rv = Win32::Service::GetStatus('', $service, $status);
  71. if(!$rv) {
  72. print Win32::FormatMessage(Win32::GetLastError()), "\n";
  73. $MetabaseConfig::LogObject->ERROR("Could not GetStatus of $service service in first attempt MetabaseConfig::StopIISAdmin: $!") if $MetabaseConfig::LogObject;
  74. return 1;
  75. }
  76. if($status->{'CurrentState'} != $MetabaseConfig::StatusStopped) {
  77. $rv = Win32::Service::StopService('', $service);
  78. if(!$rv) {
  79. print Win32::FormatMessage(Win32::GetLastError()), "\n";
  80. $MetabaseConfig::LogObject->ERROR("Could not stop $service service in MetabaseConfig::StopIISAdmin: $!") if $MetabaseConfig::LogObject;
  81. return $rv;
  82. }
  83. while($status->{'CurrentState'} != $MetabaseConfig::StatusStopped) {
  84. sleep(10);
  85. $rv = Win32::Service::GetStatus('', $service, $status);
  86. if(!$rv) {
  87. print Win32::FormatMessage(Win32::GetLastError()), "\n";
  88. $MetabaseConfig::LogObject->ERROR("Could not GetStatus of $service service in MetabaseConfig::StopIISAdmin: $!") if $MetabaseConfig::LogObject;
  89. return $rv;
  90. }
  91. }
  92. }
  93. $MetabaseConfig::LogObject->TRACE("$service service is stopped in MetabaseConfig::StopIISAdmin") if $MetabaseConfig::LogObject;
  94. return 1;
  95. }
  96. ######################################################
  97. # StartIISAdmin();
  98. sub StartService {
  99. my $service = shift;
  100. my $status = {};
  101. my $rv = Win32::Service::GetStatus('', $service, $status);
  102. if(!$rv) {
  103. $MetabaseConfig::LogObject->ERROR("Could not GetStatus of $service service in first attempt MetabaseConfig::StartIISAdmin: $!") if $MetabaseConfig::LogObject;
  104. return 1;
  105. }
  106. if($status->{'CurrentState'} != $MetabaseConfig::StatusStarted) {
  107. $rv = Win32::Service::StartService('', $service);
  108. if(!$rv) {
  109. $MetabaseConfig::LogObject->ERROR("Could not start $service service in MetabaseConfig::StartIISAdmin: $!") if $MetabaseConfig::LogObject;
  110. return $rv;
  111. }
  112. while($status->{'CurrentState'} != $MetabaseConfig::StatusStarted) {
  113. sleep(5);
  114. $rv = Win32::Service::GetStatus('', $service, $status);
  115. if(!$rv) {
  116. $MetabaseConfig::LogObject->ERROR("Could not GetStatus of $service service in MetabaseConfig::StartIISAdmin: $!") if $MetabaseConfig::LogObject;
  117. return $rv;
  118. }
  119. }
  120. }
  121. $MetabaseConfig::LogObject->TRACE("$service service is started in MetabaseConfig::StartIISAdmin") if $MetabaseConfig::LogObject;
  122. return 1;
  123. }
  124. @MetabaseConfig::ServerStash = ();
  125. ######################################################
  126. # StashRunningServers()
  127. sub StashRunningServers {
  128. my $index = 1;
  129. my $path = 'IIS://localhost/W3SVC/';
  130. my $testPath = $path . $index;
  131. my $server;
  132. $MetabaseConfig::LogObject->TRACE("Stashing running web servers in MetabaseConfig::StashRunningServers") if $MetabaseConfig::LogObject;
  133. while ( ($server = Win32::OLE->GetObject($testPath)) )
  134. {
  135. $MetabaseConfig::ServerStash[$index] = ($server->Status() == 2);
  136. $index++;
  137. $testPath = $path . $index;
  138. }
  139. }
  140. ######################################################
  141. # StartStashedServers()
  142. sub StartStashedServers {
  143. my $index = 1;
  144. my $path = 'IIS://localhost/W3SVC/';
  145. my $testPath = $path . $index;
  146. my $server;
  147. my $wasStarted;
  148. $MetabaseConfig::LogObject->TRACE("Starting stashed web servers MetabaseConfig::StartStashedServers") if $MetabaseConfig::LogObject;
  149. foreach $wasStarted (@MetabaseConfig::ServerStash) {
  150. if($wasStarted == 1) {
  151. $server = Win32::OLE->GetObject($testPath);
  152. if(!$server) {
  153. $MetabaseConfig::LogObject->ERROR("Could not GetObject($testPath) in MetabaseConfig::StartStashedServers: " . Win32::OLE->LastError()) if $MetabaseConfig::LogObject;
  154. } else {
  155. $server->Start();
  156. }
  157. }
  158. $index++;
  159. $testPath = $path . $index;
  160. }
  161. }
  162. ######################################################
  163. # StartWWW( $dwWebServerID );
  164. sub StartWWW
  165. {
  166. my $serverID = $_[0];
  167. my $path = 'IIS://localhost/W3SVC/' . $serverID;
  168. my $server = Win32::OLE->GetObject($path);
  169. $MetabaseConfig::LogObject->TRACE("Starting WWWServer: $path") if $MetabaseConfig::LogObject;
  170. if(!$server) {
  171. $MetabaseConfig::LogObject->ERROR("Could not GetObject($path) in MetabaseConfig::StartWWW: " . Win32::OLE->LastError()) if $MetabaseConfig::LogObject;
  172. return undef;
  173. }
  174. $server->Start();
  175. }
  176. ######################################################
  177. # StopWWW( $dwWebServerID );
  178. sub StopWWW
  179. {
  180. my $serverID = $_[0];
  181. my $path = 'IIS://localhost/W3SVC/' . $serverID;
  182. my $server = Win32::OLE->GetObject($path);
  183. $MetabaseConfig::LogObject->TRACE("Stopping WWWServer: $path") if $MetabaseConfig::LogObject;
  184. if(!$server) {
  185. $MetabaseConfig::LogObject->ERROR("Could not GetObject($path) in MetabaseConfig::StopWWW: " . Win32::OLE->LastError()) if $MetabaseConfig::LogObject;
  186. return undef;
  187. }
  188. $server->Stop();
  189. }
  190. ######################################################
  191. # $arrayRef = EnumWebServers();
  192. sub EnumWebServers
  193. {
  194. my $index = 1;
  195. my $path = 'IIS://localhost/W3SVC/';
  196. my $testPath = $path . $index;
  197. my $server;
  198. my @webServers = ();
  199. while ( ($server=Win32::OLE->GetObject($testPath)) )
  200. {
  201. $webServers[$index] = $server->{ServerComment};
  202. $index++;
  203. $testPath = $path . $index;
  204. }
  205. return \@webServers;
  206. }
  207. ######################################################
  208. # GetFileExtMapping($dwServerID, $szVirDir, $szFileExt)
  209. sub GetFileExtMapping
  210. {
  211. if( @_ < 3 )
  212. {
  213. # die "Not enough Parameters for GetFileExtMapping()\n";
  214. }
  215. my $server = '';
  216. my $szVirDirPath = '';
  217. my $dwServerID = shift;
  218. my $szVirDir = shift;
  219. my $szFileExt = shift;
  220. my $scriptMap = '';
  221. # Create string that contains the Path to our Virutal directory or the WebServer's Root
  222. $szVirDirPath = 'IIS://localhost/W3SVC/' . $dwServerID . '/ROOT';
  223. $MetabaseConfig::LogObject->TRACE("Getting file extension mapping: $szFileExt") if $MetabaseConfig::LogObject;
  224. if( length($szVirDir) )
  225. {
  226. $szVirDirPath = $szVirDirPath . "/" . $szVirDir;
  227. }
  228. # Get the IIsVirtualDir Automation Object
  229. $server = Win32::OLE->GetObject($szVirDirPath);
  230. if(!$server) {
  231. $MetabaseConfig::LogObject->ERROR("Could not GetObject($szVirDirPath) in MetabaseConfig::GetFileExtMapping: " . Win32::OLE->LastError) if $MetabaseConfig::LogObject;
  232. return;
  233. }
  234. foreach $scriptMap (@{$server->{ScriptMaps}}) {
  235. if($scriptMap =~ /^$szFileExt,/i) {
  236. return $scriptMap;
  237. }
  238. }
  239. }
  240. ######################################################
  241. # RemoveFileExtMapping($dwServerID, $szVirDir, $szFileExt)
  242. sub RemoveFileExtMapping
  243. {
  244. if( @_ < 3 )
  245. {
  246. # die "Not enough Parameters for AddFileExtMapping()\n";
  247. }
  248. my $szVirDirPath = '';
  249. my @newScriptMap = ();
  250. my $dwServerID = shift;
  251. my $szVirDir = shift;
  252. my $szFileExt = shift;
  253. my $virDir;
  254. my $ScriptMap = '';
  255. if(GetFileExtMapping($dwServerID, $szVirDir, $szFileExt) eq '') {
  256. return 1;
  257. }
  258. # Create string that contains the Path to our Virutal directory or the WebServer's Root
  259. $szVirDirPath = 'IIS://localhost/W3SVC/' . $dwServerID . '/ROOT';
  260. if( length($szVirDir) )
  261. {
  262. $szVirDirPath = $szVirDirPath . "/" . $szVirDir;
  263. }
  264. # Get the IIsVirtualDir Automation Object
  265. $virDir = Win32::OLE->GetObject($szVirDirPath);
  266. if(!$virDir) {
  267. $MetabaseConfig::LogObject->ERROR("Could not GetObject($szVirDirPath) in MetabaseConfig::RemoveFileExtMapping: " . Win32::OLE->LastError()) if $MetabaseConfig::LogObject;
  268. return;
  269. }
  270. $MetabaseConfig::LogObject->TRACE("Removing file extension mapping: $szFileExt") if $MetabaseConfig::LogObject;
  271. foreach $ScriptMap (@{$virDir->{ScriptMaps}}) {
  272. if($ScriptMap !~ /^$szFileExt,/i) {
  273. push(@newScriptMap, $ScriptMap);
  274. }
  275. }
  276. # set the ScriptsMaps property to our new script map array
  277. $virDir->{ScriptMaps} = \@newScriptMap;
  278. # Save the new script mappings
  279. $virDir->SetInfo();
  280. }
  281. ######################################################
  282. # AddFileExtMapping($dwServerID, $szVirDir, $szFileExt, $lpszExec, $dwFlags, $szMethodExclusions)
  283. sub AddFileExtMapping
  284. {
  285. if( @_ < 6 )
  286. {
  287. # die "Not enough Parameters for AddFileExtMapping()\n";
  288. }
  289. my $server = '';
  290. my $szVirDirPath = '';
  291. my $scriptMapping = '';
  292. my @newScriptMap = ();
  293. my $dwServerID = shift;
  294. my $szVirDir = shift;
  295. my $szFileExt = shift;
  296. my $szExecPath = shift;
  297. my $dwFlags = shift;
  298. my $szMethodExc = shift;
  299. if(GetFileExtMapping($dwServerID, $szVirDir, $szFileExt) ne '') {
  300. return 1;
  301. }
  302. # Create string that contains the Path to our Virutal directory or the WebServer's Root
  303. $szVirDirPath = 'IIS://localhost/W3SVC/' . $dwServerID . '/ROOT';
  304. if( length($szVirDir) )
  305. {
  306. $szVirDirPath = $szVirDirPath . "/" . $szVirDir;
  307. }
  308. # Get the IIsVirtualDir Automation Object
  309. $server = Win32::OLE->GetObject($szVirDirPath);
  310. if(!$server) {
  311. $MetabaseConfig::LogObject->ERROR("Could not GetObject($szVirDirPath) in MetabaseConfig::AddFileExtMapping: " . Win32::OLE->LastError()) if $MetabaseConfig::LogObject;
  312. return;
  313. }
  314. # create our new script mapping entry
  315. $scriptMapping = "$szFileExt,$szExecPath,$dwFlags";
  316. # make sure the length of szMethodExc is greater than 2 before adding szMethodExc to the script mapping
  317. if( length($szMethodExc) > 2 )
  318. {
  319. $scriptMapping = $scriptMapping . ",$szMethodExc";
  320. }
  321. $MetabaseConfig::LogObject->TRACE("Adding file extension mapping: $scriptMapping") if $MetabaseConfig::LogObject;
  322. @newScriptMap = @{$server->{ScriptMaps}};
  323. push(@newScriptMap, $scriptMapping);
  324. $server->{ScriptMaps} = \@newScriptMap;
  325. # Save the new script mappings
  326. $server->SetInfo();
  327. }
  328. ######################################################
  329. # CreateVirDir( $dwServerID, $szPath, $szName, $dwAccessPerm, $bEnableDirBrowse, $bAppRoot);
  330. sub CreateVirDir
  331. {
  332. if( @_ < 6 )
  333. {
  334. # die "Not enough Parameters for CreateVirDir()\n";
  335. }
  336. # Local Variables
  337. my $serverPath;
  338. my $server;
  339. my $virDir;
  340. my $dwServerID = shift;
  341. my $szPath = shift;
  342. my $szName = shift;
  343. my $dwAccessPerm = shift;
  344. my $bEnableDirBrowse = shift;
  345. my $bAppRoot = shift;
  346. if($szPath eq "" || $szName eq "")
  347. {
  348. die "Incorrect Parameter to CreateVirDir() ...\n";
  349. }
  350. # Create string that contains the Path to our Webserver's Root
  351. $serverPath = 'IIS://localhost/W3SVC/' . $dwServerID . '/Root';
  352. $MetabaseConfig::LogObject->TRACE("Creating virtual directory: $szName") if $MetabaseConfig::LogObject;
  353. # Get the IIsWebServer Automation Object
  354. $server = Win32::OLE->GetObject($serverPath);
  355. if(!$server) {
  356. $MetabaseConfig::LogObject->ERROR("Could not GetObject($serverPath) in MetabaseConfig::CreateVirDir: " . Win32::OLE->LastError()) if $MetabaseConfig::LogObject;
  357. return;
  358. }
  359. # Create Our Virutual Directory or get it if it already exists
  360. $virDir = $server->Create('IIsWebVirtualDir', $szName);
  361. if( not UNIVERSAL::isa($virDir, 'Win32::OLE') )
  362. {
  363. $MetabaseConfig::LogObject->ERROR("Did not create IIsWebVirtualDir object in MetabaseConfig::CreateVirDir: " . Win32::OLE->LastError()) if $MetabaseConfig::LogObject;
  364. $virDir = $server->GetObject('IIsWebVirtualDir', $szName);
  365. if(!$virDir) {
  366. $MetabaseConfig::LogObject->ERROR("Could not GetObject($szName) in MetabaseConfig::CreateVirDir: " . Win32::OLE->LastError()) if $MetabaseConfig::LogObject;
  367. return;
  368. }
  369. }
  370. $virDir->{Path} = $szPath;
  371. $virDir->{AppFriendlyName} = $szName;
  372. $virDir->{EnableDirBrowsing} = $bEnableDirBrowse;
  373. $virDir->{AccessRead} = $dwAccessPerm & MD_ACCESS_READ;
  374. $virDir->{AccessWrite} = $dwAccessPerm & MD_ACCESS_WRITE;
  375. $virDir->{AccessExecute} = $dwAccessPerm & MD_ACCESS_EXECUTE;
  376. $virDir->{AccessScript} = $dwAccessPerm & MD_ACCESS_SCRIPT;
  377. $virDir->{AccessNoRemoteRead} = $dwAccessPerm & MD_ACCESS_NO_REMOTE_READ;
  378. $virDir->{AccessNoRemoteScript} = $dwAccessPerm & MD_ACCESS_NO_REMOTE_SCRIPT;
  379. $virDir->{AccessNoRemoteWrite} = $dwAccessPerm & MD_ACCESS_NO_REMOTE_WRITE;
  380. $virDir->{AccessNoRemoteExecute} = $dwAccessPerm & MD_ACCESS_NO_REMOTE_EXECUTE;
  381. $virDir->AppCreate($bAppRoot);
  382. $virDir->SetInfo();
  383. }
  384. ######################################################
  385. # DeleteVirDir( $dwServerID, $szVirDir );
  386. sub DeleteVirDir
  387. {
  388. my $dwServerID = $_[0];
  389. my $szVirDir = $_[1];
  390. my $szPath = '';
  391. my $server = '';
  392. if($dwServerID eq "" || $szVirDir eq "")
  393. {
  394. # die "Incorrect Parameter to DeleteVirDir() ...\n";
  395. }
  396. # Create string that contains the Path to our Webserver's Root
  397. $szPath = 'IIS://localhost/W3SVC/' . $dwServerID . '/Root';
  398. $MetabaseConfig::LogObject->TRACE("Deleting virtual directory: $szPath") if $MetabaseConfig::LogObject;
  399. # Get the IIsWebServer Automation Object
  400. $server = Win32::OLE->GetObject($szPath);
  401. if(!$server) {
  402. $MetabaseConfig::LogObject->ERROR("Could not GetObject($szPath) in MetabaseConfig::DeleteVirDir: " . Win32::OLE->LastError()) if $MetabaseConfig::LogObject;
  403. return;
  404. }
  405. $server->Delete( "IIsWebVirtualDir", $szVirDir );
  406. $server->SetInfo();
  407. }
  408. 1;