Exemplo n.º 1
0
        public void CopyUserMembership(NTLocalUser sourceRemoteUser, NTLocalUser destLocalUser)
        {
            ActionObserver.NotifyAction("Copying", "User Membership", sourceRemoteUser.FullName, destLocalUser.FullName);
            TextBank    existingMembers = new TextBank(destLocalUser.GetMembership());
            GroupCopier groupCopier     = new GroupCopier(false, false, true, DefaultUserPassword, false, ActionObserver);

            foreach (NTLocalGroup remoteGroup in sourceRemoteUser.GetMembership())
            {
                if (!existingMembers.ContainsText(remoteGroup.Name))
                {
                    NTLocalGroup localGroup = null;

                    // find a user by the same name of local machine
                    if (!NTHost.CurrentMachine.TryGetLocalGroup(remoteGroup.Name, out localGroup))
                    {
                        if (_importLocalGroups)
                        {
                            groupCopier.CopyRemoteGroupToLocalMachine(remoteGroup);
                        }
                    }

                    if (localGroup != null)
                    {
                        if (!existingMembers.ContainsText(localGroup.Name))
                        {
                            destLocalUser.AddMembership(localGroup);
                        }
                    }
                }
            }
        }
Exemplo n.º 2
0
        public bool TryTranslate(
            string remoteHost,
            string remoteName,
            WinAPI.ADVAPI32.SidNameUse remoteNameUse,
            out string translatedAccountName
            )
        {
            ActionObserver.NotifyAction("Translating", remoteNameUse.ToString(), remoteName, string.Empty);

            bool retval = false;

            translatedAccountName = string.Empty;
            string key = string.Format("{0}\\{1}", remoteHost, remoteName);

            if (_translations.ContainsKey(key))
            {
                translatedAccountName = _translations[key];
                retval = true;
            }
            else
            {
                // attempt to resolve with local user/group of same name
                if (AccountExistsLocally(remoteName))
                {
                    _translations[key]    = remoteName;
                    translatedAccountName = _translations[key];
                    retval = true;
                    ActionObserver.NotifyInformation("Translated remote account '{0}\\{1}' to already existing local account '{2}'", remoteHost, remoteName, translatedAccountName);
                }
                else if (_importObject)
                {
                    #region Import remote object
                    NTHost        host = new NTHost(remoteHost);
                    NTLocalObject obj;
                    if (host.TryGetLocalObject(remoteName, out obj))
                    {
                        if (obj is NTLocalUser)
                        {
                            NTLocalUser remoteUser = (NTLocalUser)obj;
                            UserCopier  userCopier = new UserCopier(
                                true,
                                false,
                                _defaultPassword,
                                false,
                                ActionObserver
                                );
                            NTLocalUser localUser =
                                userCopier.CopyRemoteUserToLocalMachine(remoteUser);
                            translatedAccountName = localUser.Name;

                            ActionObserver.NotifyInformation("Copied and translated remote user '{0}\\{1}' to local group '{2}'", remoteHost, remoteName, translatedAccountName);
                        }
                        else if (obj is NTLocalGroup)
                        {
                            NTLocalGroup remoteGroup = (NTLocalGroup)obj;
                            GroupCopier  groupCopier = new GroupCopier(
                                true,
                                false,
                                true,
                                _defaultPassword,
                                false,
                                ActionObserver
                                );

                            NTLocalGroup localGroup =
                                groupCopier.CopyRemoteGroupToLocalMachine(remoteGroup);
                            translatedAccountName = remoteGroup.Name;

                            ActionObserver.NotifyInformation("Copied and translated remote group '{0}\\{1}' to local group '{2}'", remoteHost, remoteName, translatedAccountName);
                        }
                    }

                    #endregion

                    _translations[key] = translatedAccountName;
                    retval             = true;
                }
            }

            if (!retval)
            {
                ActionObserver.NotifyWarning("Failed to translate '{0}\\{1}' into a local object.", remoteHost, remoteName);
            }
            return(retval);
        }