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.

447 lines
11 KiB

  1. using System;
  2. using System.DirectoryServices;
  3. using System.Resources;
  4. using MSScriptControl;
  5. using System.Collections;
  6. namespace mbsh
  7. {
  8. /// <summary>
  9. /// The actual shell processor.
  10. /// </summary>
  11. public class CProcessor
  12. {
  13. public CProcessor(string mbPath)
  14. {
  15. // Set language
  16. m_sLanguage = c_defaultLanguage;
  17. // Get a resource manager
  18. m_resources = new ResourceManager("Mbsh.Mbsh", System.Reflection.Assembly.GetExecutingAssembly());
  19. // Init the WScript object
  20. m_WScript = new CWScript();
  21. // Init the script control
  22. InitEngine(c_defaultLanguage);
  23. // Set path
  24. MBPath = mbPath;
  25. }
  26. public void DoWork()
  27. {
  28. m_bWorkDone = false;
  29. while (!m_bWorkDone)
  30. {
  31. try
  32. {
  33. string s_inputLine;
  34. string token;
  35. CTokenizer localTok;
  36. s_inputLine = ShowPrompt();
  37. localTok = new CTokenizer(s_inputLine, null);
  38. token = localTok.GetNextToken();
  39. token = token.ToUpper();
  40. if (String.Equals(token, m_commandPrefix + m_resources.GetString("engCommand")))
  41. {
  42. // change the scripting engine
  43. if (localTok.NumToks < 2)
  44. {
  45. throw new CMbshException(m_resources.GetString("engError"));
  46. }
  47. string language = localTok.GetNextToken();
  48. InitEngine(language);
  49. }
  50. else if (String.Equals(token, m_commandPrefix + m_resources.GetString("helpCommand")))
  51. {
  52. // help command
  53. if (localTok.NumToks < 2)
  54. {
  55. // general help
  56. Console.WriteLine(m_resources.GetString("generalHelp"));
  57. }
  58. else
  59. {
  60. // specific help
  61. Console.WriteLine(m_resources.GetString("specificHelp"));
  62. }
  63. }
  64. else if (String.Equals(token, m_commandPrefix + m_resources.GetString("dirCommand")))
  65. {
  66. EnumProps();
  67. }
  68. else if (String.Equals(token, m_commandPrefix + m_resources.GetString("setCommand")))
  69. {
  70. if (localTok.NumToks < 3)
  71. {
  72. throw new CMbshException(m_resources.GetString("setError"));
  73. }
  74. string propName = localTok.GetNextToken();
  75. string propVal = localTok.GetNextToken();
  76. SetProperty(propName, propVal);
  77. }
  78. else if (String.Equals(token, m_commandPrefix + m_resources.GetString("getCommand")))
  79. {
  80. if (localTok.NumToks < 2)
  81. {
  82. throw new CMbshException(m_resources.GetString("getError"));
  83. }
  84. GetProperty(localTok.GetNextToken());
  85. }
  86. else if (String.Equals(token, m_commandPrefix + m_resources.GetString("cdCommand")))
  87. {
  88. //change metabase node
  89. if (localTok.NumToks < 2)
  90. {
  91. throw new CMbshException(m_resources.GetString("cdError"));
  92. }
  93. ChangeDir(localTok);
  94. }
  95. else if ((String.Equals(token, m_commandPrefix + m_resources.GetString("exitCommand"))) ||
  96. (String.Equals(token, m_commandPrefix + m_resources.GetString("quitCommand"))))
  97. {
  98. // quitting
  99. m_bWorkDone = true;
  100. }
  101. else if (token.StartsWith(m_commandPrefix))
  102. {
  103. // unknown command
  104. throw new CMbshException(m_resources.GetString("unknownCommand") + token +
  105. "\n" + m_resources.GetString("useHelp") + m_commandPrefix +
  106. m_resources.GetString("helpCommand"));
  107. }
  108. else
  109. {
  110. m_scriptControl.ExecuteStatement(s_inputLine);
  111. }
  112. }
  113. catch(Exception e)
  114. {
  115. Console.WriteLine(e.Message);
  116. }
  117. }
  118. }
  119. private string ShowPrompt()
  120. {
  121. Console.Write(MBPath + c_promptSuffix);
  122. return Console.ReadLine();
  123. }
  124. private string m_mbPath = CMbshApp.c_DefaultMBPath;
  125. private const string c_promptSuffix = ">";
  126. private const string c_pathDelim = "/";
  127. private char[] c_cPathDelim = {'/'};
  128. private string m_commandPrefix = ".";
  129. private const string c_upPath = "..";
  130. private const string c_altPathDelim = "\\";
  131. private bool m_bWorkDone = false;
  132. private DirectoryEntry m_dir = null;
  133. private ResourceManager m_resources;
  134. /// <summary>
  135. /// Gets the ADSI object for the specified path
  136. /// </summary>
  137. private DirectoryEntry GetPathObject(string sPath)
  138. {
  139. string s_adsiPath;
  140. if (sPath.StartsWith(CMbshApp.c_ConnectString))
  141. {
  142. s_adsiPath = sPath;
  143. }
  144. else
  145. {
  146. s_adsiPath= CMbshApp.c_ConnectString + sPath;
  147. }
  148. try
  149. {
  150. if (DirectoryEntry.Exists(s_adsiPath))
  151. {
  152. return new System.DirectoryServices.DirectoryEntry(s_adsiPath);
  153. }
  154. else
  155. {
  156. return null;
  157. }
  158. }
  159. catch
  160. {
  161. return null;
  162. }
  163. }
  164. /// <summary>
  165. /// MSScriptControl.ScriptControl
  166. /// </summary>
  167. private ScriptControl m_scriptControl;
  168. /// <summary>
  169. /// Default script engine
  170. /// </summary>
  171. private const string c_defaultLanguage = "JScript";
  172. /// <summary>
  173. /// The active script engine
  174. /// </summary>
  175. private string m_sLanguage;
  176. /// <summary>
  177. /// Initializes m_scriptControl
  178. /// </summary>
  179. private void InitEngine(string language)
  180. {
  181. // Get the script control
  182. m_scriptControl = new MSScriptControl.ScriptControlClass();
  183. try
  184. {
  185. m_scriptControl.Language = language;
  186. m_sLanguage = language;
  187. }
  188. catch
  189. {
  190. Console.WriteLine(m_resources.GetString("noLang"));
  191. m_scriptControl.Language = m_sLanguage;
  192. }
  193. m_scriptControl.AllowUI = true;
  194. }
  195. /// <summary>
  196. /// Instantiation of WScript object
  197. /// </summary>
  198. private CWScript m_WScript;
  199. /// <summary>
  200. /// Sets a property in the metabase
  201. /// </summary>
  202. private void SetProperty(string propName, string propVal)
  203. {
  204. PropertyValueCollection propValColl;
  205. PropertyCollection collection = m_dir.Properties;
  206. try
  207. {
  208. propValColl = collection[propName];
  209. }
  210. catch
  211. {
  212. throw new CMbshException(m_resources.GetString("invalidProp"));
  213. }
  214. propValColl.Value = propVal;
  215. m_dir.CommitChanges();
  216. }
  217. private void ChangeDir(CTokenizer localTok)
  218. {
  219. string path = localTok.GetNextToken();
  220. path.Replace(c_altPathDelim, c_pathDelim);
  221. CTokenizer pathTokens = new CTokenizer(path, c_cPathDelim);
  222. string newPath;
  223. // absolute path
  224. if (path.StartsWith(c_pathDelim))
  225. {
  226. newPath = c_pathDelim;
  227. }
  228. // relative path
  229. else
  230. {
  231. newPath = MBPath;
  232. }
  233. for (int pathIndex = 0; pathIndex < pathTokens.NumToks; pathIndex++)
  234. {
  235. string pathToken = pathTokens.GetNextToken();
  236. // ".."
  237. if (String.Equals(pathToken, c_upPath))
  238. {
  239. int lastSlash = newPath.LastIndexOf(c_pathDelim);
  240. newPath = newPath.Remove(lastSlash, newPath.Length - lastSlash);
  241. if (0 == newPath.Length)
  242. {
  243. newPath = c_pathDelim;
  244. }
  245. }
  246. else
  247. {
  248. if (newPath.EndsWith(c_pathDelim))
  249. {
  250. newPath = newPath + pathToken;
  251. }
  252. else
  253. {
  254. newPath = newPath + c_pathDelim + pathToken;
  255. }
  256. }
  257. }
  258. MBPath = newPath;
  259. }
  260. /// <summary>
  261. /// Outputs the value of the selected property
  262. /// </summary>
  263. private void GetProperty(string propName)
  264. {
  265. PropertyValueCollection propValColl;
  266. PropertyCollection collection = m_dir.Properties;
  267. try
  268. {
  269. propValColl = collection[propName];
  270. }
  271. catch
  272. {
  273. throw new CMbshException(m_resources.GetString("invalidProp"));
  274. }
  275. Console.WriteLine(propValColl.Value.ToString());
  276. }
  277. /// <summary>
  278. /// Outputs all properties at the node
  279. /// </summary>
  280. private void EnumProps()
  281. {
  282. PropertyValueCollection propValColl;
  283. DirectoryEntry propEntry;
  284. PropertyCollection collection = m_dir.Properties;
  285. // Get the schema entry for the class of our object
  286. ActiveDs.IADsClass schemaEntry = (ActiveDs.IADsClass)m_dir.SchemaEntry.NativeObject;
  287. Array[] propLists = new Array[2];
  288. propLists[0] = (Array)schemaEntry.MandatoryProperties;
  289. propLists[1] = (Array)schemaEntry.OptionalProperties;
  290. foreach (Array propList in propLists)
  291. {
  292. foreach (string propName in propList)
  293. {
  294. if (!propName.Equals(string.Empty))
  295. {
  296. try
  297. {
  298. propValColl = collection[propName];
  299. // don't do anything if there's no value
  300. if (null != propValColl.Value)
  301. {
  302. string propVal = propValColl.Value.ToString();
  303. if (!propVal.Equals(string.Empty))
  304. {
  305. // check if isInherit
  306. IISOle.IISPropertyAttribute retVal = null;
  307. Object[] invokeArgs = {propName};
  308. try
  309. {
  310. retVal = (IISOle.IISPropertyAttribute) m_dir.Invoke("GetPropertyAttribObj", invokeArgs);
  311. }
  312. catch
  313. {
  314. // ignore the error - check for null below
  315. }
  316. if (null != retVal)
  317. {
  318. // check if IsInherit
  319. if (!retVal.Isinherit)
  320. {
  321. string propPath = m_dir.SchemaEntry.Parent.Path + "/" + propName;
  322. propEntry = GetPathObject(propPath);
  323. ActiveDs.IADsProperty iProp = (ActiveDs.IADsProperty)propEntry.NativeObject;
  324. string propSyntax = "(" + iProp.Syntax + ")";
  325. string vpropName = propName.PadRight(30, ' ');
  326. Console.Write(vpropName + " : ");
  327. string typename = propSyntax.ToUpper().PadRight(10, ' ');
  328. Console.Write(typename + " ");
  329. Console.WriteLine(propVal);
  330. }
  331. }
  332. }
  333. }
  334. }
  335. catch
  336. {
  337. Console.WriteLine(m_resources.GetString("noDisplay") + propName);
  338. }
  339. }
  340. }
  341. }
  342. }
  343. private string MBPath
  344. {
  345. get
  346. {
  347. return m_mbPath;
  348. }
  349. set
  350. {
  351. m_dir = GetPathObject(value);
  352. if (null != m_dir)
  353. {
  354. m_mbPath = String.Copy(value);
  355. // need the next line so that the control actually gets reset, even if we haven't used it
  356. m_scriptControl.State = MSScriptControl.ScriptControlStates.Connected;
  357. m_scriptControl.Reset();
  358. m_scriptControl.AddObject(m_resources.GetString("thisNodeName"), m_dir.NativeObject, true);
  359. m_scriptControl.AddObject(m_resources.GetString("WScriptName"), m_WScript, true);
  360. return;
  361. }
  362. m_dir = GetPathObject(m_mbPath);
  363. if (null != m_dir)
  364. {
  365. // throw "can't set path" exception
  366. throw new CMbshException(m_resources.GetString("failedADSI") + value +
  367. "\n" + m_resources.GetString("succeededADSI") + m_mbPath);
  368. }
  369. // throw "can't set any path" exception
  370. if (String.Equals(value, m_mbPath))
  371. {
  372. throw new CMbshException(m_resources.GetString("failedADSI") + value);
  373. }
  374. else
  375. {
  376. throw new CMbshException(m_resources.GetString("failedADSI") + value +
  377. "\n" + m_resources.GetString("failedADSI") + m_mbPath);
  378. }
  379. }
  380. }
  381. }
  382. }