public static bool EnsureNullSession(string sServer, CredentialEntry ce) { if (String.IsNullOrEmpty(sServer)) { return(false); } Logger.Log(String.Format("Session.EnsureNullSession({0}, {1}) called", sServer, ce), Logger.netAPILogLevel); if (ce == null) { Logger.Log("Session.EnsureNullSession() called with CredentialEntry ce=null, returning false", Logger.authLogLevel); return(false); } sServer = Canon(sServer); try { // if we already have a session, don't recreate it if (alSessions.Contains(sServer)) { return(true); } else { try { DeleteNullSession(sServer); } catch (AuthSessionException aex) { Logger.Log("EnsureNullSession: DeleteNullSession failed (as expected) beacuse " + aex.Message, Logger.netAPILogLevel); } try { if (CreateNullSession(sServer, ce.MachineName, ce)) { alSessions.Add(sServer); return(true); } } catch (AuthSessionException aex) { Logger.Log("EnsureNullSession: CreateNullSession failed unexpectedly. beacuse " + aex.Message, Logger.netAPILogLevel); } } return(false); } catch (DllNotFoundException ex) { Logger.ShowUserError(String.Format( "DllNotFound Exception caught in SharesAPI.EnumShares: Could not load library: {0}", ex.Message)); ce.Invalidate(sServer); return(false); } catch (Exception ex) { Logger.LogException("Session.EnsureNullSession", ex); ce.Invalidate(sServer); return(false); } }
public static bool EnsureNullSession(string sServer, CredentialEntry ce) { if (String.IsNullOrEmpty(sServer)) { return false; } Logger.Log(String.Format("Session.EnsureNullSession({0}, {1}) called", sServer, ce), Logger.netAPILogLevel); if(ce == null) { Logger.Log("Session.EnsureNullSession() called with CredentialEntry ce=null, returning false", Logger.authLogLevel); return false; } sServer = Canon(sServer); try { // if we already have a session, don't recreate it if (alSessions.Contains(sServer)) { return true; } else { try { DeleteNullSession(sServer); } catch(AuthSessionException aex) { Logger.Log("EnsureNullSession: DeleteNullSession failed (as expected) beacuse " + aex.Message, Logger.netAPILogLevel); } try { if (CreateNullSession(sServer, ce.MachineName, ce)) { alSessions.Add(sServer); return true; } } catch (AuthSessionException aex) { Logger.Log("EnsureNullSession: CreateNullSession failed unexpectedly. beacuse " + aex.Message, Logger.netAPILogLevel); } } return false; } catch (DllNotFoundException ex) { Logger.ShowUserError(String.Format( "DllNotFound Exception caught in SharesAPI.EnumShares: Could not load library: {0}", ex.Message)); ce.Invalidate(sServer); return false; } catch (Exception ex) { Logger.LogException("Session.EnsureNullSession", ex); ce.Invalidate(sServer); return false; } }
#pragma warning restore 414 #endif #endregion #region Static Interface public static bool CreateNullSession(string sServer, string sShortname, CredentialEntry ce) { Logger.Log( String.Format("Session.CreateNullSession({0}, {1}, {2}) called", sServer, sShortname, ce), Logger.netAPILogLevel); int nErr; // set up the user name and password; map "root" as needed string sUsername; string sPassword; if (Configurations.currentPlatform == LikewiseTargetPlatform.Windows) { if (ce != null && !ce.DefaultCreds && ce.Password != new String('*', 16)) { if (ce.UserName.IndexOf('\\') < 0 && !String.IsNullOrEmpty(ce.Domain)) { sUsername = String.Format("{0}\\{1}", ce.Domain, ce.UserName); } else { sUsername = ce.UserName; } sPassword = ce.Password; } else { // setup for default creds sUsername = null; sPassword = null; Logger.Log("CreateNullSession(): Using default creds", Logger.netAPILogLevel); } } else { if (ce.UserName.IndexOf(@"\") < 0 && !String.IsNullOrEmpty(ce.Domain)) { sUsername = String.Format("{0}\\{1}", ce.Domain, ce.UserName); } else { sUsername = ce.UserName; } sPassword = ce.Password; } // set up a NETRESOURCE structure NETRESOURCE nr = new NETRESOURCE(); nr.dwScope = 0; nr.dwType = 0; nr.dwDisplayType = 0; nr.dwUsage = 0; nr.LocalName = null; nr.RemoteName = @"\\" + sServer + @"\IPC$"; nr.Comment = null; nr.Provider = null; // try the operation. Throws exception if it fails. if (Configurations.currentPlatform == LikewiseTargetPlatform.Windows) { nErr = WindowsSession.WNetAddConnection2(nr, sPassword, sUsername, 0); } else { nErr = WNetAddConnection2(nr, sPassword, sUsername, 0); } //HACK: WNetAddConnection2 appears to return 'access denied' even when it has just granted access! //this is a workaround with the side-effect that the user will never be warned when their password //is invalid. if (nErr == (int)ErrorCodes.WIN32Enum.ERROR_ACCESS_DENIED) { Logger.Log(String.Format("CreateNullSession(): Ignoring error! nErr={0}: {1}", nErr, ErrorCodes.WIN32String(nErr)), Logger.LogLevel.Error); System.Windows.Forms.MessageBox.Show("WNetAddConnection2 success but ERROR_ACCESS_DENIED"); return(true); } else if (nErr != 0) { ce.Invalidate(sServer); Logger.Log(String.Format("CreateNullSession(): nErr={0}: {1}", nErr, ErrorCodes.WIN32String(nErr)), Logger.LogLevel.Error); return(false); } Logger.Log("CreateNullSession() successful", Logger.netAPILogLevel); return(true); }
#pragma warning restore 414 #endif #endregion #region Static Interface public static bool CreateNullSession(string sServer, string sShortname, CredentialEntry ce) { Logger.Log( String.Format("Session.CreateNullSession({0}, {1}, {2}) called", sServer, sShortname, ce), Logger.netAPILogLevel); int nErr; // set up the user name and password; map "root" as needed string sUsername; string sPassword; if (Configurations.currentPlatform == LikewiseTargetPlatform.Windows) { if (ce != null && !ce.DefaultCreds && ce.Password != new String('*', 16)) { if (ce.UserName.IndexOf('\\') < 0 && !String.IsNullOrEmpty(ce.Domain)) { sUsername = String.Format("{0}\\{1}", ce.Domain, ce.UserName); } else { sUsername = ce.UserName; } sPassword = ce.Password; } else { // setup for default creds sUsername = null; sPassword = null; Logger.Log("CreateNullSession(): Using default creds", Logger.netAPILogLevel); } } else { if (ce.UserName.IndexOf(@"\") < 0 && !String.IsNullOrEmpty(ce.Domain)) { sUsername = String.Format("{0}\\{1}", ce.Domain, ce.UserName); } else { sUsername = ce.UserName; } sPassword = ce.Password; } // set up a NETRESOURCE structure NETRESOURCE nr = new NETRESOURCE(); nr.dwScope = 0; nr.dwType = 0; nr.dwDisplayType = 0; nr.dwUsage = 0; nr.LocalName = null; nr.RemoteName = @"\\" + sServer + @"\IPC$"; nr.Comment = null; nr.Provider = null; // try the operation. Throws exception if it fails. if (Configurations.currentPlatform == LikewiseTargetPlatform.Windows) { nErr = WindowsSession.WNetAddConnection2(nr, sPassword, sUsername, 0); } else { nErr = WNetAddConnection2(nr, sPassword, sUsername, 0); } //HACK: WNetAddConnection2 appears to return 'access denied' even when it has just granted access! //this is a workaround with the side-effect that the user will never be warned when their password //is invalid. if (nErr == (int) ErrorCodes.WIN32Enum.ERROR_ACCESS_DENIED) { Logger.Log(String.Format("CreateNullSession(): Ignoring error! nErr={0}: {1}", nErr, ErrorCodes.WIN32String(nErr)), Logger.LogLevel.Error); System.Windows.Forms.MessageBox.Show("WNetAddConnection2 success but ERROR_ACCESS_DENIED"); return true; } else if (nErr != 0) { ce.Invalidate(sServer); Logger.Log(String.Format("CreateNullSession(): nErr={0}: {1}", nErr, ErrorCodes.WIN32String(nErr)), Logger.LogLevel.Error); return false; } Logger.Log("CreateNullSession() successful", Logger.netAPILogLevel); return true; }