Ejemplo n.º 1
0
        public void SetUp()
        {
            // test user
            _testUser                   = new Netapi32.USER_INFO_1();
            _testUser.usri1_name        = "WaffleTestUser";
            _testUser.usri1_password    = Guid.NewGuid().ToString();
            _testUser.usri1_priv        = 1;
            _testUser.usri1_home_dir    = null;
            _testUser.comment           = "Waffle test user.";
            _testUser.usri1_script_path = null;
            int rc = Netapi32.NetUserAdd(null, 1, ref _testUser, 0);

            Assert.AreEqual(0, rc, new Win32Exception(rc).Message);
            // computer
            _computerName = Environment.MachineName;
            // fqn
            _testUserFqn = string.Format("{0}\\{1}", _computerName, _testUser.usri1_name);
            // join status
            IntPtr pDomain = IntPtr.Zero;

            rc = Netapi32.NetGetJoinInformation(null, out pDomain, out _joinStatus);
            Assert.AreEqual(Netapi32.NERR_Success, rc, new Win32Exception(rc).Message);
            _memberOf = Marshal.PtrToStringAuto(pDomain);
            Netapi32.NetApiBufferFree(pDomain);
        }
Ejemplo n.º 2
0
        public static string IsDomainJoined()
        {
            // returns Compuer Domain if the system is inside an AD (an nothing if it is not)
            try
            {
                NetJoinStatus status  = NetJoinStatus.NetSetupUnknownStatus;
                IntPtr        pDomain = IntPtr.Zero;
                int           result  = Netapi32.NetGetJoinInformation(null, out pDomain, out status);
                if (pDomain != IntPtr.Zero)
                {
                    Netapi32.NetApiBufferFree(pDomain);
                }

                if (result == Win32.ErrorSuccess)
                {
                    // If in domain, return domain name, if not, return empty
                    return(status == NetJoinStatus.NetSetupDomainName ? Environment.UserDomainName : "");
                }
            }

            catch (Exception ex)
            {
                Beaprint.GrayPrint(string.Format("  [X] Exception: {0}\n Trying to check if domain is joined using WMI", ex.Message));
                return(IsDomainJoinedWmi());
            }
            return("");
        }
Ejemplo n.º 3
0
        public static T[] NetServerEnum <T>(ServerTypes serverTypes = ServerTypes.Workstation | ServerTypes.Server, string domain = null, int level = 0) where T : struct
        {
            if (level == 0)
            {
                level = int.Parse(System.Text.RegularExpressions.Regex.Replace(typeof(T).Name, @"[^\d]", ""));
            }

            IntPtr bufptr = IntPtr.Zero;

            try
            {
                int    entriesRead, totalEntries;
                IntPtr resumeHandle = IntPtr.Zero;

                int ret = Netapi32.NetServerEnum(null, level, out bufptr, MAX_PREFERRED_LENGTH, out entriesRead, out totalEntries, serverTypes, domain, resumeHandle);
                if (ret == 0)
                {
                    return(InteropUtil.ToArray <T>(bufptr, entriesRead));
                }
                throw new System.ComponentModel.Win32Exception(ret);
            }
            finally
            {
                Netapi32.NetApiBufferFree(bufptr);
            }
        }
Ejemplo n.º 4
0
        public static T NetServerGetInfo <T>(string serverName, int level = 0) where T : struct
        {
            if (level == 0)
            {
                level = int.Parse(System.Text.RegularExpressions.Regex.Replace(typeof(T).Name, @"[^\d]", ""));
            }

            IntPtr ptr = IntPtr.Zero;

            try
            {
                int ret = Netapi32.NetServerGetInfo(serverName, level, out ptr);
                if (ret != 0)
                {
                    throw new System.ComponentModel.Win32Exception(ret);
                }

                return((T)Marshal.PtrToStructure(ptr, typeof(T)));
            }
            finally
            {
                if (ptr != IntPtr.Zero)
                {
                    Netapi32.NetApiBufferFree(ptr);
                }
            }
        }
Ejemplo n.º 5
0
 public void SetUp()
 {
     // computer
     _computerName = Environment.MachineName;
     // join status 
     IntPtr pDomain = IntPtr.Zero;
     Assert.AreEqual(Netapi32.NERR_Success, Netapi32.NetGetJoinInformation(null, out pDomain, out _joinStatus));
     _memberOf = Marshal.PtrToStringAuto(pDomain);
     Netapi32.NetApiBufferFree(pDomain);
 }
Ejemplo n.º 6
0
        /// <summary>
        /// Constructor with a computer name.
        /// </summary>
        /// <param name="computerName">Computer name.</param>
        public WindowsComputerImpl(string computerName)
        {
            _computerName = computerName;

            IntPtr pDomain = IntPtr.Zero;

            try
            {
                int rc = Netapi32.NetGetJoinInformation(computerName, out pDomain, out _joinStatus);
                if (rc == Netapi32.NERR_Success && _joinStatus != Netapi32.NetJoinStatus.NetSetupUnjoined)
                {
                    _memberOf = Marshal.PtrToStringAuto(pDomain);
                }
            }
            finally
            {
                if (pDomain != IntPtr.Zero)
                {
                    Netapi32.NetApiBufferFree(pDomain);
                }
            }
        }
        public static IEnumerable <USER_INFO_3> GetLocalUsers(string computerName)
        {
            uint MAX_PREFERRED_LENGTH = unchecked ((uint)-1);

            // Returns local users
            //  FILTER_NORMAL_ACCOUNT == 2
            var users  = new List <USER_INFO_3>();
            var retVal = Netapi32.NetUserEnum(computerName, 3, 2, out var bufPtr, MAX_PREFERRED_LENGTH, out var entriesRead, out var totalEntries, out var resume);

            if (retVal != 0)
            {
                var errorMessage = new Win32Exception(Marshal.GetLastWin32Error()).Message;
                throw new Exception("Error code " + retVal + ": " + errorMessage);
            }

            if (entriesRead == 0)
            {
                return(users);
            }

            var names    = new string[entriesRead];
            var userInfo = new USER_INFO_3[entriesRead];
            var iter     = bufPtr;

            for (var i = 0; i < entriesRead; i++)
            {
                userInfo[i] = (USER_INFO_3)Marshal.PtrToStructure(iter, typeof(USER_INFO_3));
                users.Add(userInfo[i]);

                //x64 safe
                iter = new IntPtr(iter.ToInt64() + Marshal.SizeOf(typeof(USER_INFO_3)));
            }
            Netapi32.NetApiBufferFree(bufPtr);

            return(users);
        }