Exemplo n.º 1
0
        private static void GetLocallyLoggedOnUsers(string computerName, SyncList.SyncList <DprCurrentUsers> result)
        {
            var usersList = new List <DprCurrentUsers>( );

            using (var regHku = RegistryKey.OpenRemoteBaseKey(RegistryHive.Users, string.Empty)) {
                foreach (var currentSid in regHku.GetSubKeyNames( ).Where(IsSid))
                {
                    var cu = new DprCurrentUsers(computerName)
                    {
                        Sid = currentSid
                    };
                    try {
                        if (Win32.WellKnownSids.ContainsKey(currentSid))
                        {
                            cu.Domain   = computerName;                                 // Local account
                            cu.UserName = Win32.WellKnownSids[currentSid];
                        }
                        else
                        {
                            GetUserAccountFromSid(ref cu);
                        }
                        cu.ProfileFolder = RegistryHelpers.GetString(computerName, RegistryHive.LocalMachine, string.Format(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\{0}", currentSid), @"ProfileImagePath");
                        cu.LastLogon     = GetUsersLogonTimestamp(cu);
                    } catch (Exception ex) {
                        GlobalLogging.WriteLine(Logging.LogSeverity.Error, @"Exception - {0} - {1}", ex.TargetSite, ex.Message);
                        cu = new DprCurrentUsers(computerName, ConnectionStatuses.Error)
                        {
                            Sid = currentSid
                        };
                    }
                    cu.LogonType = LogonTypes.Local;
                    usersList.Add(cu);
                }
            }
            result.AddRange(usersList);
        }
Exemplo n.º 2
0
        private static Win32.Error GetNetworkUsers(string computerName, ref SyncList.SyncList <DprCurrentUsers> result)
        {
            Win32.Error res;
            var         er        = 0;
            var         tr        = 0;
            var         resume    = 0;
            var         buffer    = IntPtr.Zero;
            var         usersList = new List <DprCurrentUsers>( );

            do
            {
                try {
                    res = (Win32.Error)Win32.NetSessionEnum(computerName, null, null, 502, out buffer, -1, ref er, ref tr, ref resume);
                    if (res == Win32.Error.ErrorMoreData || res == Win32.Error.Success)
                    {
                        var bufferPtrInt = buffer.ToInt32( );
                        for (var i = 0; i < er; i++)
                        {
                            var sessionInfo = (Win32.SessionInfo502)Marshal.PtrToStructure(new IntPtr(bufferPtrInt), typeof(Win32.SessionInfo502));
                            var cu          = new DprCurrentUsers(computerName)
                            {
                                UserName = sessionInfo.userName, LastLogon = DateTime.Now.AddSeconds(-sessionInfo.logonDuration), LogonType = LogonTypes.Share
                            };
                            usersList.Add(cu);
                            bufferPtrInt += Marshal.SizeOf(typeof(Win32.SessionInfo502));
                        }
                    }
                    else
                    {
                        switch (res)
                        {
                        case Win32.Error.ErrorAccessDenied:
                            GlobalLogging.WriteLine(Logging.LogSeverity.Error, @"GetNetworkUsers: Access Denied: {0}", computerName);
                            break;

                        case Win32.Error.ErrorNotEnoughMemory:
                            GlobalLogging.WriteLine(Logging.LogSeverity.Error, @"GetNetworkUsers: Not Enough Memory: {0}", computerName);
                            break;

                        case Win32.Error.ErrorBadNetpath:
                            GlobalLogging.WriteLine(Logging.LogSeverity.Error, @"GetNetworkUsers: Bad Network Path: {0}", computerName);
                            break;

                        case Win32.Error.ErrorNetworkBusy:
                            GlobalLogging.WriteLine(Logging.LogSeverity.Error, @"GetNetworkUsers: Network Busy: {0}", computerName);
                            break;

                        case Win32.Error.ErrorInvalidParameter:
                            GlobalLogging.WriteLine(Logging.LogSeverity.Error, @"GetNetworkUsers: Invalid Parameter: {0}", computerName);
                            break;

                        case Win32.Error.ErrorInsufficientBuffer:
                            GlobalLogging.WriteLine(Logging.LogSeverity.Error, @"GetNetworkUsers: Insufficient Buff: {0}", computerName);
                            break;

                        case Win32.Error.ErrorInvalidLevel:
                            GlobalLogging.WriteLine(Logging.LogSeverity.Error, @"GetNetworkUsers: Invalid Level: {0}", computerName);
                            break;

                        case Win32.Error.ErrorExtendedError:
                            GlobalLogging.WriteLine(Logging.LogSeverity.Error, @"GetNetworkUsers: Exended Error: {0}", computerName);
                            break;

                        case Win32.Error.ErrorNoNetwork:
                            GlobalLogging.WriteLine(Logging.LogSeverity.Error, @"GetNetworkUsers: No Network: {0}", computerName);
                            break;

                        case Win32.Error.ErrorInvalidHandleState:
                            GlobalLogging.WriteLine(Logging.LogSeverity.Error, @"GetNetworkUsers: Invalid Handle State: {0}", computerName);
                            break;

                        case Win32.Error.NerrBase:
                            GlobalLogging.WriteLine(Logging.LogSeverity.Error, @"GetNetworkUsers: NERR_BASE: {0}", computerName);
                            break;

                        case Win32.Error.NerrUnknownDevDir:
                            GlobalLogging.WriteLine(Logging.LogSeverity.Error, @"GetNetworkUsers: Unknown Device Directory: {0}", computerName);
                            break;

                        case Win32.Error.NerrDuplicateShare:
                            GlobalLogging.WriteLine(Logging.LogSeverity.Error, @"GetNetworkUsers: Duplicate Share: {0}", computerName);
                            break;

                        case Win32.Error.NerrBufTooSmall:
                            GlobalLogging.WriteLine(Logging.LogSeverity.Error, @"GetNetworkUsers: Buffer too small: {0}", computerName);
                            break;

                        case Win32.Error.ErrorNoBrowserServersFound:
                            GlobalLogging.WriteLine(Logging.LogSeverity.Error, @"GetNetworkUsers: No Browser Servers Found: {0}", computerName);
                            break;
                        }
                        return(res);
                    }
                } finally {
                    if (IntPtr.Zero != buffer)
                    {
                        Win32.NetApiBufferFree(buffer);
                    }
                }
            } while(res == Win32.Error.ErrorMoreData);
            result.AddRange(usersList);
            return(Win32.Error.Success);
        }