/// <summary> /// Method to store the plugin's host info to ini file. /// </summary> /// <param name="_hn"></param> private void SaveTargetMachineInfoToIni(Hostinfo _hn, IPlugIn plugin) { Logger.Log(String.Format( "LMCCredentials.SaveTargetMachineInfoToIni: saving the target machine info to ini: _hn =\n {0}", _hn == null ? "<null>" : _hn.ToString()), Logger.manageLogLevel); string sPath = Path.Combine(Application.UserAppDataPath, @"Temp.ini"); CreateCredentialsIni(sCredentialsFilePath); StreamReader reader = new StreamReader(sCredentialsFilePath); FileStream fStream = new FileStream(sPath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite); StreamWriter writer = new StreamWriter(fStream); string sPlugInName = plugin.GetName(); Logger.Log(String.Format( "LMCCredentials.SaveTargetMachineInfoToIni: plugin name={0}", sPlugInName == null ? "<null>" : sPlugInName), Logger.manageLogLevel); if (!String.IsNullOrEmpty(sPlugInName)) { string currentLine = reader.ReadLine(); while (!(currentLine != null && currentLine.Trim().Equals(sPlugInName)) && !reader.EndOfStream) { writer.WriteLine(currentLine); currentLine = reader.ReadLine(); } writer.WriteLine(sPlugInName); if (!String.IsNullOrEmpty(_hn.hostName)) { writer.WriteLine("hostname=" + _hn.hostName.Trim()); } if (_hn.creds != null && !String.IsNullOrEmpty(_hn.creds.UserName)) { writer.WriteLine("username="******"domainFQDN=" + _hn.domainName.Trim()); } if (_hn.creds != null && !String.IsNullOrEmpty(_hn.creds.Domain)) { writer.WriteLine("domainShort=" + _hn.creds.Domain.Trim()); } writer.WriteLine(""); //make sure the remainder of the old file is present in the new one. currentLine = reader.ReadLine(); while (!(currentLine != null && currentLine.Trim().Length > 0 && currentLine.Trim()[0] == '[') && !reader.EndOfStream) { currentLine = reader.ReadLine(); } while (!reader.EndOfStream) { writer.WriteLine(currentLine); currentLine = reader.ReadLine(); } if (reader.EndOfStream) { writer.WriteLine(""); } } writer.Flush(); writer.Close(); reader.Close(); fStream.Close(); fStream.Dispose(); if (File.Exists(sCredentialsFilePath) && File.Exists(sPath)) { File.Delete(sCredentialsFilePath); File.Move(sPath, sCredentialsFilePath); } }
private bool GetMachineInfoDelegate(string[] fields, Object context) { uint fieldsRequested = (uint)((UInt32)context); Logger.Log(String.Format( "Manage.GetMachineInfoDelegate: fieldsRequested=0x{0:x}", fieldsRequested), Logger.manageLogLevel); string shortHostName = null; string fullyQualifiedHostName = null; string FQDN = null; string fullyQualifiedDCName = null; string shortDomainName = null; string userName = null; string password = null; string errorMessage = null; bool result = true; int fieldIndex = 0; _hn = new Hostinfo(); _hn.creds = new CredentialEntry(); if (((uint)Hostinfo.FieldBitmaskBits.SHORT_HOSTNAME & fieldsRequested) > 0) { shortHostName = fields[fieldIndex++]; if (!Hostinfo.ValidateHostName(shortHostName, out errorMessage)) { result = false; Logger.Log("GetMachineInfoDelegate: invalid SHORT_HOSTNAME: " + errorMessage, Logger.manageLogLevel); } else { _hn.hostName = shortHostName; Logger.Log("GetMachineInfoDelegate: assigned SHORT_HOSTNAME:\n" + _hn.ToString(), Logger.manageLogLevel); } } if (result && ((uint)Hostinfo.FieldBitmaskBits.FQ_HOSTNAME & fieldsRequested) > 0) { fullyQualifiedHostName = fields[fieldIndex++]; if (!Hostinfo.ValidateFullyQualifiedHostName(fullyQualifiedHostName, out errorMessage)) { result = false; Logger.Log("GetMachineInfoDelegate: invalid FQ_HOSTNAME: " + errorMessage, Logger.manageLogLevel); } else { _hn.hostName = fullyQualifiedHostName; Logger.Log("GetMachineInfoDelegate: assigned FQ_HOSTNAME:\n" + _hn.ToString(), Logger.manageLogLevel); } } if (result && ((uint)Hostinfo.FieldBitmaskBits.FQDN & fieldsRequested) > 0) { FQDN = fields[fieldIndex++]; if (!Hostinfo.ValidateFQDN(FQDN, out errorMessage)) { result = false; Logger.Log("GetMachineInfoDelegate: invalid FQDN: " + errorMessage, Logger.manageLogLevel); } else { _hn.domainName = FQDN; Logger.Log("GetMachineInfoDelegate: assigned FQDN:\n" + _hn.ToString(), Logger.manageLogLevel); } } if (result && ((uint)Hostinfo.FieldBitmaskBits.FQ_DCNAME & fieldsRequested) > 0) { fullyQualifiedDCName = fields[fieldIndex++]; if (!Hostinfo.ValidateFullyQualifiedDCName(fullyQualifiedDCName, out errorMessage)) { result = false; Logger.Log("GetMachineInfoDelegate: invalid FQ_DCNAME: " + errorMessage, Logger.manageLogLevel); } else if (String.IsNullOrEmpty(_hn.hostName)) { _hn.domainControllerName = fullyQualifiedDCName; Logger.Log("GetMachineInfoDelegate: assigned FQ_DCNAME:\n" + _hn.ToString(), Logger.manageLogLevel); } } if (result && ((uint)Hostinfo.FieldBitmaskBits.CREDS_NT4DOMAIN & fieldsRequested) > 0) { shortDomainName = fields[fieldIndex++]; if (!Hostinfo.ValidateShortDomainName(shortDomainName, out errorMessage)) { result = false; Logger.Log("GetMachineInfoDelegate: invalid CREDS_NT4DOMAIN: " + errorMessage, Logger.manageLogLevel); } else { _hn.creds.Domain = shortDomainName; Logger.Log("GetMachineInfoDelegate: assigned CREDS_NT4DOMAIN:\n" + _hn.ToString(), Logger.manageLogLevel); } } if (result && ((uint)Hostinfo.FieldBitmaskBits.CREDS_USERNAME & fieldsRequested) > 0) { userName = fields[fieldIndex++]; if (!Hostinfo.ValidateUserName(userName, out errorMessage)) { result = false; Logger.Log("GetMachineInfoDelegate: invalid CREDS_USERNAME: "******"GetMachineInfoDelegate: assigned CREDS_USERNAME:\n" + _hn.ToString(), Logger.manageLogLevel); } } if (result && ((uint)Hostinfo.FieldBitmaskBits.CREDS_PASSWORD & fieldsRequested) > 0) { password = fields[fieldIndex++]; if (!Hostinfo.ValidatePassword(password, password, out errorMessage)) { result = false; Logger.Log("GetMachineInfoDelegate: invalid CREDS_PASSWORD: "******"GetMachineInfoDelegate: assigned CREDS_PASSWORD:\n" + _hn.ToString(), Logger.manageLogLevel); } } if (!result) { sc.ShowError(errorMessage); return false; } _hn.creds.MachineName = _hn.hostName; if (String.IsNullOrEmpty(_hn.domainControllerName) && !String.IsNullOrEmpty(_hn.domainName)) { try { CNetlogon.LWNET_DC_INFO DCInfo; uint netlogonError = CNetlogon.GetDCName(_hn.domainName, 0, out DCInfo); if (netlogonError == 0 && !String.IsNullOrEmpty(DCInfo.DomainControllerName)) { _hn.domainControllerName = DCInfo.DomainControllerName; if (String.IsNullOrEmpty(_hn.creds.Domain)) { _hn.creds.Domain = DCInfo.NetBIOSDomainName; } } else { _hn.domainControllerName = _hn.domainName; } if (netlogonError == 0 && !String.IsNullOrEmpty(DCInfo.NetBIOSDomainName)) { if (shortDomainName != null) { if (!shortDomainName.Trim().ToUpper().Equals(DCInfo.NetBIOSDomainName.Trim().ToUpper())) { errorMessage = "Invalid NT4-Style Domain Name"; sc.ShowError(errorMessage); return false; } } else { _hn.creds.Domain = DCInfo.NetBIOSDomainName; } } } catch(Exception ex) { Logger.Log("Exception occured while getting DCInfo ," + ex.Message); return false; } } SaveTargetMachineInfoToIni(_hn); getTargetMachineRequestor.SetContext(_hn); if (_hn.IsConnectionSuccess == false) { return false; } return true; }