private bool NetUseWithCredentials()
        {
            uint returncode;
            try
            {
                USE_INFO_2 useinfo = new USE_INFO_2();

                useinfo.ui2_remote = sUNCPath;
                useinfo.ui2_username = sUser;
                useinfo.ui2_domainname = sDomain;
                useinfo.ui2_password = sPassword;
                useinfo.ui2_asg_type = 0;
                useinfo.ui2_usecount = 1;
                uint paramErrorIndex;
                returncode = NetUseAdd(null, 2, ref useinfo, out paramErrorIndex);
                iLastError = (int)returncode;
                return returncode == 0;
            }
            catch
            {
                iLastError = Marshal.GetLastWin32Error();
                return false;
            }
        }
Exemple #2
0
 public bool Connect(string UncPath, string Domain, string User, string Password)
 {
     try
     {
         var useinfo = new USE_INFO_2
         {
             ui2_remote     = UncPath,
             ui2_domainname = Domain,
             ui2_username   = User,
             ui2_password   = Password,
             ui2_asg_type   = 0,
             ui2_usecount   = 1
         };
         _UncPath = UncPath;
         uint parmError;
         uint Code = NetUseAdd(null, 2, ref useinfo, out parmError);
         _LastError = (int)Code;
         _IsConnect = (Code == 0);
         return(_IsConnect);
     }
     catch
     {
         _LastError = Marshal.GetLastWin32Error();
         return(false);
     }
 }
Exemple #3
0
        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        private bool NetUseWithCredentials()
        {
            uint returncode;

            try
            {
                USE_INFO_2 useinfo = new USE_INFO_2();

                useinfo.ui2_remote     = uncPath;
                useinfo.ui2_username   = user;
                useinfo.ui2_domainname = domain;
                useinfo.ui2_password   = password;
                useinfo.ui2_asg_type   = 0;
                //useinfo.ui2_usecount = 1;
                uint paramErrorIndex;
                returncode = NetUseAdd(null, 2, ref useinfo, out paramErrorIndex);
                lastError  = (int)returncode;
                return(returncode == 0);
            }
            catch
            {
                lastError = Marshal.GetLastWin32Error();
                //Exception ex = Marshal.GetExceptionForHR(lastError);

                return(false);
            }
        }
Exemple #4
0
        private bool NetUseWithCredentials()
        {
            try
            {
                var useinfo = new USE_INFO_2
                {
                    ui2_remote     = _sUncPath,
                    ui2_username   = _sUser,
                    ui2_domainname = _sDomain,
                    ui2_password   = _sPassword,
                    ui2_asg_type   = 0,
                    ui2_usecount   = 1
                };

                uint paramErrorIndex;
                uint returncode = NetUseAdd(null, 2, ref useinfo, out paramErrorIndex);
                _iLastError = (int)returncode;
                return(returncode == 0);
            }
            catch
            {
                _iLastError = Marshal.GetLastWin32Error();
                return(false);
            }
        }
Exemple #5
0
        private bool NetUseWithCredentials()
        {
            uint returncode;

            try
            {
                USE_INFO_2 useinfo = new USE_INFO_2();

                useinfo.ui2_remote     = sUNCPath;
                useinfo.ui2_username   = sUser;
                useinfo.ui2_domainname = sDomain;
                useinfo.ui2_password   = sPassword;
                useinfo.ui2_asg_type   = 0;
                useinfo.ui2_usecount   = 1;
                uint paramErrorIndex;
                returncode = NetUseAdd(null, 2, ref useinfo, out paramErrorIndex);
                iLastError = (int)returncode;
                return(returncode == 0);
            }
            catch
            {
                iLastError = Marshal.GetLastWin32Error();
                return(false);
            }
        }
Exemple #6
0
        public bool Mount(string drive, string networkPath, Credential credential)
        {
            try
            {
                if (!string.IsNullOrEmpty(drive))
                {
                    NetUseDel("", drive, 2);
                }
                else
                {
                    NetUseDel("", networkPath, 2);
                }

                USE_INFO_2 useInfo = new USE_INFO_2();
                useInfo.ui2_local    = drive;
                useInfo.ui2_remote   = networkPath;
                useInfo.ui2_password = credential.Password;
                useInfo.ui2_asg_type = 0;    //disk drive
                useInfo.ui2_usecount = 1;
                useInfo.ui2_username = credential.Username;
                //useInfo.ui2_domainname = "";

                uint paramErrorIndex;
                uint returnCode = NetUseAdd(null, 2, ref useInfo, out paramErrorIndex);
                if (returnCode != 0)
                {
                    return(false);
                }
            }
            catch (Exception) { return(false); }
            return(true);
        }
        internal static bool Mount(string drive, string networkPath, string username, string password)
        {
            if (!string.IsNullOrEmpty(drive))
            {
                NetUseDel("", drive, 2);
            }
            else
            {
                NetUseDel("", networkPath, 2);
            }

            USE_INFO_2 useInfo = new USE_INFO_2
            {
                ui2_local    = drive,
                ui2_remote   = networkPath,
                ui2_asg_type = 0,    //disk drive
                ui2_usecount = 1
            };

            if (!String.IsNullOrEmpty(username))
            {
                useInfo.ui2_username = username;
                useInfo.ui2_password = password;
            }

            uint paramErrorIndex;
            uint returnCode = NetUseAdd(null, 2, ref useInfo, out paramErrorIndex);

            if (returnCode != 0)
            {
                throw new Exception(GetSystemMessage((int)returnCode));
            }
            return(true);
        }
Exemple #8
0
        /// <summary>
        /// Authenticates a network share
        /// </summary>
        /// <param name="unc">UNC to network share</param>
        /// <param name="domainName">Domain name</param>
        /// <param name="userName">Username</param>
        /// <param name="password">Password</param>
        /// <param name="invalidParamName">If windows returns error 87 (Invalid Param), this contains the name of the parameter which was invalid</param>
        /// <returns>Returns the error code from Windows NetUseAdd</returns>
        public static int ConnectShare(string unc, string domainName, string userName, string password, out string invalidParamName)
        {
            invalidParamName = "";

            USE_INFO_2 useInfo = new USE_INFO_2();

            useInfo.ui2_local      = string.Empty;
            useInfo.ui2_remote     = unc;
            useInfo.ui2_password   = password;
            useInfo.ui2_asg_type   = 0;  //disk drive
            useInfo.ui2_usecount   = 1;
            useInfo.ui2_username   = userName;
            useInfo.ui2_domainname = domainName;
            uint paramErrorIndex;

            uint returnCode = NetUseAdd(null, 2, ref useInfo, out paramErrorIndex);

            if (returnCode == ERROR_INVALID_PARAMETER) // If we have an invalid parameter, then identify the type of invalid parameter
            {
                switch (paramErrorIndex)
                {
                case 1:
                    invalidParamName = "1: Local device name (null)";
                    break;

                case 2:
                    invalidParamName = "2: Remote share name (" + unc + ")";
                    break;

                case 3:
                    invalidParamName = "3: Password (" + new String('*', password.Length) + ")";
                    break;

                case 4:
                    invalidParamName = "4: ASG Remote resource type (disk)";
                    break;

                case 5:
                    invalidParamName = "5: Username (" + userName + ")";
                    break;

                case 6:
                    invalidParamName = "6: Domain name (" + domainName + ")";
                    break;

                default:
                    invalidParamName = paramErrorIndex.ToString() + ": Unknown parameter";
                    break;
                }
            }

            return((int)returnCode);
        }
Exemple #9
0
 /// <summary>
 /// Открытие доступа на папку по учетной записи
 /// </summary>
 /// <param name="path"></param>
 /// <param name="domain"></param>
 /// <param name="user"></param>
 /// <param name="password"></param>
 /// <returns></returns>
 public static uint NetUseAdd(string path, string domain, string user, string password)
 {
     USE_INFO_2 useinfo = new USE_INFO_2();
     useinfo.ui2_remote = path;
     useinfo.ui2_username = user;
     useinfo.ui2_domainname = domain;
     useinfo.ui2_password = password;
     useinfo.ui2_asg_type = 0;
     useinfo.ui2_usecount = 1;
     uint paramErrorIndex;
     uint code = NetUseAdd(null, 2, ref useinfo, out paramErrorIndex);
     return code;
 }
Exemple #10
0
        public bool NetUseWithCredentials()
        {
            var settingService = new ServiceSetting();

            if (settingService.GetSetting(SettingStrings.StorageType).Value == "Local")
            {
                return(true);
            }
            sUNCPath  = settingService.GetSetting(SettingStrings.StoragePath).Value.TrimEnd('\\'); //dont' know why, but mount fails if path ends with \
            sUser     = settingService.GetSetting(SettingStrings.StorageUsername).Value;
            sPassword =
                new EncryptionServices().DecryptText(settingService.GetSetting(SettingStrings.StoragePassword).Value);
            sDomain = settingService.GetSetting(SettingStrings.StorageDomain).Value;
            uint returncode;

            try
            {
                var useinfo = new USE_INFO_2();

                useinfo.ui2_remote     = sUNCPath;
                useinfo.ui2_username   = sUser;
                useinfo.ui2_domainname = sDomain;
                useinfo.ui2_password   = sPassword;
                useinfo.ui2_asg_type   = 0;
                useinfo.ui2_usecount   = 1;
                uint paramErrorIndex;
                returncode = NetUseAdd(null, 2, ref useinfo, out paramErrorIndex);
                LastError  = (int)returncode;
                if (returncode != 1219 && returncode != 0)
                {
                    Logger.Error("Could Not Connect To Storage Location: " + sUNCPath);
                    Logger.Error("Error Code: " + returncode);
                }
                return(returncode == 0);
            }
            catch (Exception ex)
            {
                LastError = Marshal.GetLastWin32Error();
                Logger.Error("Could Not Connect To Share");
                Logger.Error(ex.Message);
                return(false);
            }
        }
        private bool NetUseWithCredentials()
        {
            uint returncode;

            try
            {
                USE_INFO_2 useinfo = new USE_INFO_2();

                useinfo.ui2_remote     = sUNCPath;
                useinfo.ui2_username   = sUser;
                useinfo.ui2_domainname = sDomain;
                useinfo.ui2_password   = sPassword;
                useinfo.ui2_asg_type   = 0;
                useinfo.ui2_usecount   = 1;
                uint paramErrorIndex;
                returncode = NativeMethods.NetUseAdd(null, 2, ref useinfo, out paramErrorIndex);
                iLastError = (int)returncode;
            }
            catch
            {
                iLastError = Marshal.GetLastWin32Error();
            }

            //If 0 it means there's no error
            if (iLastError == 0)
            {
                return(true);
            }
            //Default error codes that are being ignored.
            //1394 = No Session Key
            //64   = The Specified Network name is no longer available
            //1326,1219 Multiple connections to a server or shared resource by the same user, using more than one user name, are not allowed. Disconnect all previous connections to the server or shared resource and try again. It means that there's already connection to this folder.
            else if (ArchiveConstants.GetIgnoredErrorCodes().Contains(iLastError.ToString()))
            {
                //Error is being ignored
                iLastError = 0;
                return(true);
            }
            else
            {
                return(false);
            }
        }
Exemple #12
0
 public bool Open()
 {
     try
     {
         USE_INFO_2 useInfo = new USE_INFO_2();
         useInfo.ui2_remote     = _uncPath;
         useInfo.ui2_domainname = _domain;
         useInfo.ui2_username   = _userName;
         useInfo.ui2_password   = _password;
         useInfo.ui2_asg_type   = 0;
         useInfo.ui2_usecount   = 1;
         uint paramErrorIndex;
         uint returnCode = NetUseAdd(null, 2, ref useInfo, out paramErrorIndex);
         return((LastError = (int)returnCode) == 0);
     }
     catch
     {
         LastError = Marshal.GetLastWin32Error();
         return(false);
     }
 }
Exemple #13
0
        /// <summary>
        /// Connects to a UNC path using the credentials supplied.
        /// </summary>
        /// <returns>
        /// Returns <c>true</c> if it succeeds.
        /// </returns>
        private bool Connect()
        {
            try
            {
                USE_INFO_2 useinfo = new USE_INFO_2();

                useinfo.ui2_remote     = this._uncPath;
                useinfo.ui2_username   = this._user;
                useinfo.ui2_domainname = this._domain;
                useinfo.ui2_password   = this._password;
                useinfo.ui2_asg_type   = 0;
                useinfo.ui2_usecount   = 1;
                uint paramErrorIndex;
                uint returncode = NetUseAdd(null, 2, ref useinfo, out paramErrorIndex);
                this._lastError = (int)returncode;
                return(returncode == 0);
            }
            catch (Exception ex)
            {
                this._lastError = Marshal.GetLastWin32Error();
                return(false);
            }
        }
Exemple #14
0
 internal static extern NET_API_STATUS NetUseAdd(LPWSTR uncServerName, DWORD level, ref USE_INFO_2 buf, out DWORD paramError);
 internal static extern NET_API_STATUS NetUseAdd(
     string UncServerName,
     UInt32 Level,
     ref USE_INFO_2 Buf,
     out UInt32 ParmError);
        private bool NetUseWithCredentials()
        {
            try
            {
                var useinfo = new USE_INFO_2
                                  {
                                      ui2_remote = _sUncPath,
                                      ui2_username = _sUser,
                                      ui2_domainname = _sDomain,
                                      ui2_password = _sPassword,
                                      ui2_asg_type = 0,
                                      ui2_usecount = 1
                                  };

                uint paramErrorIndex;
                uint returncode = NetUseAdd(null, 2, ref useinfo, out paramErrorIndex);
                _iLastError = (int)returncode;
                return returncode == 0;
            }
            catch
            {
                _iLastError = Marshal.GetLastWin32Error();
                return false;
            }
        }
 internal static extern UInt32 NetUseAdd(
     String UncServerName,
     UInt32 Level,
     ref USE_INFO_2 Buf,
     out UInt32 ParmError);
Exemple #18
0
 internal static extern uint NetUseAdd(
     string UncServerName,
     uint Level,
     ref USE_INFO_2 Buf,
     out uint ParmError);
Exemple #19
0
 internal static extern UInt32 NetUseAdd(
     String UncServerName,
     UInt32 Level,
     ref USE_INFO_2 Buf,
     out UInt32 ParmError);
 internal static extern uint NetUseAdd(
     string UncServerName,
     uint Level,
     ref USE_INFO_2 Buf,
     out uint ParmError);
Exemple #21
0
        static public void GetHost(string cname, string uname, string pword, bool getusers, double userstime, bool valid, bool invalid, bool getos, string output)
        {
            //Attempt connection
            string target   = "\\\\" + cname + "\\C$";
            string username = uname;
            string domain   = "";

            if (uname.IndexOf('\\') >= 0)
            {
                domain   = uname.Split('\\')[0];
                username = uname.Split('\\')[1];
            }
            USE_INFO_2 info1 = new USE_INFO_2();

            info1.ui2_local      = null;
            info1.ui2_asg_type   = 0xFFFFFFFF;
            info1.ui2_remote     = target;
            info1.ui2_username   = username;
            info1.ui2_password   = pword;
            info1.ui2_domainname = domain;
            uint paramErrorIndex;
            uint conn;
            uint disconn;
            int  attempt = 0;

            do
            {
                lock (cname)
                {
                    conn    = NetUseAdd(null, 2, ref info1, out paramErrorIndex);
                    disconn = NetUseDel(null, target, USE_FORCE);
                }
                if (attempt > 0)
                {
                    Thread.Sleep(2000 * attempt);
                }
                attempt += 1;
            }while (conn == (int)Status.DUPLICATE_CONNECTION && attempt < 5);
            string result = "Unknown";
            string info   = "";

            //Determine connection status
            switch (conn)
            {
            case (int)Status.SUCCESS:
                result = "LOCAL ADMIN!";

                //Do other fun enumeration
                if (getusers)
                {
                    info += "(users=" + String.Join(", ", (string[])GetUsers(target, userstime).ToArray(typeof(string))) + ")";
                }

                if (getos)
                {
                    info += "(os=" + GetOS(target) + ")";
                }

                if (disconn != 0)
                {
                    info += "(warning=Could not disconnect drive. Finish manually.)";
                }

                break;

            case (int)Status.ACCESS_DENIED:
                result = "Valid";
                break;

            case (int)Status.ACCOUNT_LOCKED_OUT:
                result = "Account locked out.";
                break;

            case (int)Status.ACCOUNT_EXPIRED:
                result = "Account expired.";
                break;

            case (int)Status.INVALID_PASSWORD:
                result = "Invalid Password";
                break;

            case (int)Status.INVALID_CREDS:
                result = "Invalid Creds";
                break;

            case (int)Status.BAD_NETWORK_NAME:
                result = "Bad Network Name";
                break;

            case (int)Status.NO_LOGON_SERVERS:
                result = "No logon servers available.";
                break;

            case (int)Status.DUPLICATE_CONNECTION:
                result = "Duplicate Connection";
                break;

            case (int)Status.RPC_UNAVAILABLE:
                result = "RPC Server Unavailable (may not be Windows)";
                break;

            default:
                result = "Other: " + conn;
                break;
            }

            bool wasvalid = (result.Equals("Valid") || result.Equals("LOCAL ADMIN!"));

            if ((!valid && !invalid) || (valid && wasvalid) || (invalid && !wasvalid))
            {
                string final;
                if (getusers || getos)
                {
                    final = String.Format("{0,-20}{1,-25}{2,-20}{3,-20}{4,-20}", cname, uname, pword, result, info);
                }
                else
                {
                    final = String.Format("{0,-20}{1,-25}{2,-20}{3,-20}", cname, uname, pword, result);
                }
                lock (writeLockObj)
                {
                    if (result.Equals("LOCAL ADMIN!"))
                    {
                        Console.ForegroundColor = ConsoleColor.Green;
                    }
                    else if (result.Equals("Valid"))
                    {
                        Console.ForegroundColor = ConsoleColor.White;
                    }
                    else if (info.Contains("warning"))
                    {
                        Console.ForegroundColor = ConsoleColor.Yellow;
                    }
                    else
                    {
                        Console.ForegroundColor = ConsoleColor.Red;
                    }

                    Console.WriteLine(final);
                    if (output != null)
                    {
                        File.AppendAllText(output, final + Environment.NewLine);
                    }
                }
            }

            Interlocked.Decrement(ref workingCounter);
            Interlocked.Increment(ref processedCounter);
            return;
        }
Exemple #22
0
 public static extern uint NetUseAdd(
     string UncServerName,
     UInt32 Level,
     ref USE_INFO_2 Buf,
     out UInt32 ParmError
     );
 internal static extern NET_API_STATUS NetUseAdd(
     LPWSTR UncServerName,
     DWORD Level,
     ref USE_INFO_2 Buf,
     out DWORD ParmError);
 internal static extern NET_API_STATUS NetUseAdd(LPWSTR UncServerName,
                                                 DWORD Level,
                                                 ref USE_INFO_2 Buf,
                                                 out DWORD ParmError);
Exemple #25
0
        static void Main(string[] args)
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            if (args.Length >= 2 && args[0] == "/o")
            {
                XmlSerializer xs = new XmlSerializer(typeof(OFA));
                using (FileStream fs = File.OpenRead(args[1])) {
                    if (fs.Length > 0)
                    {
                        var ofa = (OFA)xs.Deserialize(fs);
                        var fp  = ofa.filePath;

                        Match M = Regex.Match(fp, "^\\\\\\\\[^\\\\]+\\\\", RegexOptions.IgnoreCase);
                        if (M.Success)
                        {
                            String wantTo = M.Value;
                            if (GetFileAttributes(fp) == uint.MaxValue)
                            {
                                int err = Marshal.GetLastWin32Error();
                                if (err == 5)
                                {
                                    IntPtr ptr;
                                    int    nRead, nTotal;
                                    int    r = NetUseEnum(null, 2, out ptr, 65536, out nRead, out nTotal, IntPtr.Zero);
                                    if (r == 0)
                                    {
                                        USE_INFO_2[]      ents    = new USE_INFO_2[nRead];
                                        String            conns   = null;
                                        List <USE_INFO_2> targets = new List <USE_INFO_2>();
                                        for (int x = 0; x < ents.Length; x++)
                                        {
                                            var ent = ents[x] = (USE_INFO_2)Marshal.PtrToStructure(new IntPtr(ptr.ToInt64() + Marshal.SizeOf(typeof(USE_INFO_2)) * x), typeof(USE_INFO_2));
                                            if (ent.remote.IndexOf(wantTo, StringComparison.InvariantCultureIgnoreCase) == 0)
                                            {
                                                conns += (ent.local + " " + ent.remote + " (" + ent.domainname + "\\" + ent.username + ")").Trim() + "\n";
                                                targets.Add(ent);
                                            }
                                        }
                                        if (conns != null)
                                        {
                                            DialogResult selection = MessageBox.Show(String.Format("{0} へアクセスできません。次の接続をすべて強制的に切断して、アクセスしますか。\n\n" + conns, fp), "OpenFolderApp", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Exclamation);
                                            if (selection == DialogResult.Yes)
                                            {
                                                foreach (USE_INFO_2 ent in ents)
                                                {
                                                    if (ent.local != null)
                                                    {
                                                        NetUseDel(null, ent.local, USE_LOTS_OF_FORCE);
                                                    }
                                                    if (ent.remote != null)
                                                    {
                                                        NetUseDel(null, ent.remote, USE_LOTS_OF_FORCE);
                                                    }
                                                }
                                            }
                                            else if (selection == DialogResult.Cancel)
                                            {
                                                return;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        try {
                            Process.Start(ofa.filePath);
                        }
                        catch (Exception err) {
                            MessageBox.Show(err.Message, "OpenFolderApp", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                        }
                    }
                }
            }
            else if (args.Length >= 2 && args[0] == "/e")
            {
                Application.Run(new EditForm(args[1]));
            }
        }
Exemple #26
0
        /// <summary>
        /// Authenticates a network share
        /// </summary>
        /// <param name="unc">UNC to network share</param>
        /// <param name="domainName">Domain name</param>
        /// <param name="userName">Username</param>
        /// <param name="password">Password</param>
        /// <param name="invalidParamName">If windows returns error 87 (Invalid Param), this contains the name of the parameter which was invalid</param>
        /// <returns>Returns the error code from Windows NetUseAdd</returns>
        public static int ConnectShare(string unc, string domainName, string userName, string password, out string invalidParamName)
        {
            invalidParamName = "";

            USE_INFO_2 useInfo = new USE_INFO_2();
            useInfo.ui2_local = string.Empty;
            useInfo.ui2_remote = unc;
            useInfo.ui2_password = password;
            useInfo.ui2_asg_type = 0;    //disk drive
            useInfo.ui2_usecount = 1;
            useInfo.ui2_username = userName;
            useInfo.ui2_domainname = domainName;
            uint paramErrorIndex;

            uint returnCode = NetUseAdd(null, 2, ref useInfo, out paramErrorIndex);

            if (returnCode == ERROR_INVALID_PARAMETER) // If we have an invalid parameter, then identify the type of invalid parameter
            {
                switch (paramErrorIndex)
                {
                    case 1:
                        invalidParamName = "1: Local device name (null)";
                        break;

                    case 2:
                        invalidParamName = "2: Remote share name (" + unc + ")";
                        break;

                    case 3:
                        invalidParamName = "3: Password (" + new String('*', password.Length) + ")";
                        break;

                    case 4:
                        invalidParamName = "4: ASG Remote resource type (disk)";
                        break;

                    case 5:
                        invalidParamName = "5: Username (" + userName + ")";
                        break;

                    case 6:
                        invalidParamName = "6: Domain name (" + domainName + ")";
                        break;

                    default:
                        invalidParamName = paramErrorIndex.ToString() + ": Unknown parameter";
                        break;
                }
            }

            return (int)returnCode;
        }