Beispiel #1
0
#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);
        }