Exemplo n.º 1
0
        /// <summary>
        /// Update host information
        /// </summary>
        /// <param name="editHost"></param>
        /// <param name="sessionID"></param>
        /// <returns></returns>
        public bool UpdateHost(EnterpriseHostEdit editHost, string sessionID)
        {
            using (var db = new MyrtilleEnterpriseDBContext())
            {
                if (!db.Session.Any(m => m.SessionID == sessionID && m.IsAdmin && m.Expire > DateTime.Now))
                {
                    return(false);
                }

                var host = db.Host.FirstOrDefault(m => m.ID == editHost.HostID);

                host.HostName             = editHost.HostName;
                host.HostAddress          = editHost.HostAddress;
                host.VMGuid               = editHost.VMGuid;
                host.VMEnhancedMode       = editHost.VMEnhancedMode;
                host.Protocol             = editHost.Protocol;
                host.StartRemoteProgram   = editHost.StartRemoteProgram;
                host.PromptForCredentials = editHost.PromptForCredentials;

                var currentGroups = db.HostAccessGroups
                                    .Where(m => m.HostID == editHost.HostID)
                                    .ToList();

                IEnumerable <string> groups = editHost.DirectoryGroups.Split(',').ToList();

                var hostsToDelete = currentGroups.Where(m => !groups.Any(p => p.Equals(m.AccessGroup, StringComparison.InvariantCultureIgnoreCase)));

                db.HostAccessGroups.RemoveRange(hostsToDelete);

                var hostAccess = groups
                                 .Where(m => !currentGroups.Any(p => p.AccessGroup.Equals(m, StringComparison.InvariantCultureIgnoreCase)))
                                 .Select(x => new HostAccessGroups
                {
                    HostID      = host.ID,
                    AccessGroup = x.Trim()
                });

                db.HostAccessGroups.AddRange(hostAccess.Where(m => m.AccessGroup != ""));

                db.SaveChanges();

                return(true);
            }
        }
Exemplo n.º 2
0
        public bool DeleteHost(long hostID, string sessionID)
        {
            var success = false;

            using (var db = new MyrtilleEnterpriseDBContext())
            {
                if (db.Session.Any(m => m.SessionID == sessionID && m.IsAdmin && m.Expire > DateTime.Now))
                {
                    var host = db.Host.FirstOrDefault(m => m.ID == hostID);
                    if (host != null)
                    {
                        db.Host.Remove(host);
                        db.SaveChanges();
                        success = true;
                    }
                }
            }

            return(success);
        }
Exemplo n.º 3
0
        public bool DeleteHost(long hostID, string sessionID)
        {
            using (var db = new MyrtilleEnterpriseDBContext())
            {
                if (!db.Session.Any(m => m.SessionID == sessionID && m.IsAdmin && m.Expire > DateTime.Now))
                {
                    return(false);
                }

                var host = db.Host.FirstOrDefault(m => m.ID == hostID);

                if (host == null)
                {
                    return(false);
                }

                db.Host.Remove(host);
                db.SaveChanges();
                return(true);
            }
        }
Exemplo n.º 4
0
        public bool AddSessionHostCredentials(EnterpriseHostSessionCredentials credentials)
        {
            using (var db = new MyrtilleEnterpriseDBContext())
            {
                var session = db.Session.FirstOrDefault(m => m.SessionID == credentials.SessionID);

                if (session == null)
                {
                    return(false);
                }

                if (!db.Host.Any(m => m.ID == credentials.HostID))
                {
                    return(false);
                }

                var sessionHost = db.SessionHostCredentials.FirstOrDefault(m => m.SessionID == session.ID &&
                                                                           m.HostID == credentials.HostID);

                if (sessionHost != null)
                {
                    db.SessionHostCredentials.Remove(sessionHost);
                }

                sessionHost = new SessionHostCredential
                {
                    SessionID = session.ID,
                    HostID    = credentials.HostID,
                    Domain    = credentials.Domain,
                    Username  = credentials.Username,
                    Password  = CryptoHelper.AES_Encrypt(CryptoHelper.RDP_Encrypt(credentials.Password), credentials.SessionKey)
                };


                db.SessionHostCredentials.Add(sessionHost);
                db.SaveChanges();

                return(true);
            }
        }
Exemplo n.º 5
0
        public EnterpriseSession Authenticate(string username, string password, string adminGroup, string domain, string netbiosDomain)
        {
            try
            {
                using (var context = new PrincipalContext(ContextType.Domain, domain, username, password))
                {
                    UserPrincipal user = UserPrincipal.FindByIdentity(context, IdentityType.SamAccountName, username);

                    DirectoryEntry entry = (DirectoryEntry)user.GetUnderlyingObject();

                    if (user.IsAccountLockedOut())
                    {
                        return(new EnterpriseSession
                        {
                            AuthenticationErrorCode = EnterpriseAuthenticationErrorCode.USER_ACCOUNT_LOCKED
                        });
                    }

                    if (user.Enabled != null && !(bool)user.Enabled)
                    {
                        return(new EnterpriseSession
                        {
                            AuthenticationErrorCode = EnterpriseAuthenticationErrorCode.ACCOUNT_DISABLED
                        });
                    }

                    if (user.AccountExpirationDate != null && (DateTime)user.AccountExpirationDate <= DateTime.Now)
                    {
                        return(new EnterpriseSession
                        {
                            AuthenticationErrorCode = EnterpriseAuthenticationErrorCode.ACCOUNT_EXPIRED
                        });
                    }

                    if (!user.PasswordNeverExpires) //&& !user.UserCannotChangePassword)
                    {
                        var expDate = (DateTime)entry.InvokeGet("PasswordExpirationDate");
                        // if the expiration date is not set, its default value is 1970/01/01
                        if (expDate <= DateTime.Now && expDate > new DateTime(1970, 1, 1))
                        {
                            return(new EnterpriseSession
                            {
                                AuthenticationErrorCode = EnterpriseAuthenticationErrorCode.PASSWORD_EXPIRED
                            });
                        }
                    }


                    var directoryGroups = new List <string>();

                    try
                    {
                        directoryGroups.AddRange(user.GetGroups().Select(m => m.Name).ToList <string>());
                    }
                    catch (Exception e)
                    {
                        //There is an issue accessing user primary ad group remotely,
                        //Exception: Information about the domain could not be retrieved (1355).
                        //in that case use another method which will exclude the primary domain
                        // might need to find another way to do this!
                        directoryGroups.AddRange(GetDirectoryGroups(entry));
                    }

                    //Add user to directory group to allow restriction to host to specific username
                    directoryGroups.Add(username);

                    bool isAdmin = directoryGroups.Any(m => m.Equals(adminGroup, StringComparison.InvariantCultureIgnoreCase));

                    string sessionID  = Guid.NewGuid().ToString();
                    string sessionKey = Guid.NewGuid().ToString("n");
                    using (var db = new MyrtilleEnterpriseDBContext())
                    {
                        var session = db.Session.FirstOrDefault(m => m.Username == username);
                        if (session != null)
                        {
                            db.Session.Remove(session);
                            db.SaveChanges();
                        }

                        session = new Session
                        {
                            Domain    = netbiosDomain,
                            Username  = username,
                            Password  = CryptoHelper.AES_Encrypt(CryptoHelper.RDP_Encrypt(password), sessionKey),
                            SessionID = sessionID,
                            IsAdmin   = isAdmin
                        };

                        db.Session.Add(session);
                        db.SaveChanges();

                        var groups = directoryGroups.Select(x => new SessionGroup
                        {
                            SessionID      = session.ID,
                            DirectoryGroup = x
                        });

                        db.SessionGroup.AddRange(groups);
                        db.SaveChanges();
                        return(new EnterpriseSession
                        {
                            Domain = netbiosDomain,
                            UserName = username,
                            SessionID = sessionID,
                            SessionKey = sessionKey,
                            IsAdmin = isAdmin,
                            SingleUseConnection = false
                        });
                    }
                }
            }
            catch (DirectoryServicesCOMException e)
            {
                var formattedError = (DirectoryExceptionHelper)e;

                return(new EnterpriseSession
                {
                    AuthenticationErrorCode = formattedError.ErrorCode
                });
            }
            catch (PrincipalOperationException e)
            {
                return(null);
            }
            catch (Exception e)
            {
                return(new EnterpriseSession
                {
                    AuthenticationErrorCode = EnterpriseAuthenticationErrorCode.UNKNOWN_ERROR
                });
            }
        }
Exemplo n.º 6
0
        public EnterpriseSession Authenticate(string username, string password, string adminGroup, string domain, string netbiosDomain)
        {
            EnterpriseSession enterpriseSession = null;

            try
            {
                var config             = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                var localAdminUser     = ((AppSettingsSection)config.GetSection("localAdmin")).Settings["LocalAdminUser"].Value;
                var localAdminPassword = ((AppSettingsSection)config.GetSection("localAdmin")).Settings["localAdminPassword"].Value;
                if (!username.Equals(localAdminUser))
                {
                    enterpriseSession = new EnterpriseSession
                    {
                        AuthenticationErrorCode = EnterpriseAuthenticationErrorCode.USER_NOT_FOUND
                    };
                }
                else
                {
                    if (!localAdminPassword.Equals("admin"))
                    {
                        localAdminPassword = CryptoHelper.AES_Decrypt(localAdminPassword, localAdminUser);
                    }
                    if (!password.Equals(localAdminPassword))
                    {
                        enterpriseSession = new EnterpriseSession
                        {
                            AuthenticationErrorCode = EnterpriseAuthenticationErrorCode.INVALID_LOGIN_CREDENTIALS
                        };
                    }
                    else
                    {
                        if (password.Equals("admin"))
                        {
                            enterpriseSession = new EnterpriseSession
                            {
                                AuthenticationErrorCode = EnterpriseAuthenticationErrorCode.PASSWORD_EXPIRED
                            };
                        }
                        else
                        {
                            using (var db = new MyrtilleEnterpriseDBContext())
                            {
                                var session = db.Session.FirstOrDefault(m => m.Username == username);
                                if (session != null)
                                {
                                    db.Session.Remove(session);
                                    db.SaveChanges();
                                }

                                string sessionID  = Guid.NewGuid().ToString();
                                string sessionKey = Guid.NewGuid().ToString("n");

                                session = new Session
                                {
                                    Domain    = netbiosDomain,
                                    Username  = username,
                                    Password  = CryptoHelper.AES_Encrypt(CryptoHelper.RDP_Encrypt(password), sessionKey),
                                    SessionID = sessionID,
                                    IsAdmin   = true
                                };

                                db.Session.Add(session);
                                db.SaveChanges();

                                enterpriseSession = new EnterpriseSession
                                {
                                    Domain              = netbiosDomain,
                                    UserName            = username,
                                    SessionID           = sessionID,
                                    SessionKey          = sessionKey,
                                    IsAdmin             = true,
                                    SingleUseConnection = false
                                };
                            }
                        }
                    }
                }
            }
            catch (Exception)
            {
                enterpriseSession = new EnterpriseSession
                {
                    AuthenticationErrorCode = EnterpriseAuthenticationErrorCode.UNKNOWN_ERROR
                };
            }

            return(enterpriseSession);
        }