Ejemplo n.º 1
0
        /// <summary>
        /// Remove members from a group.
        /// </summary>
        /// <param name="group">
        /// A <see cref="LocalGroup"/> object representing the group from which
        /// the members will be removed.
        /// </param>
        private void ProcessGroup(LocalGroup group)
        {
            string groupId = group.Name ?? group.SID.ToString();

            foreach (var member in this.Member)
            {
                LocalPrincipal principal = MakePrincipal(groupId, member);
                if (principal != null)
                {
                    var ex = sam.RemoveLocalGroupMember(group, principal);
                    if (ex != null)
                    {
                        WriteError(ex.MakeErrorRecord());
                    }
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// ProcessRecord method.
        /// </summary>
        protected override void ProcessRecord()
        {
            try
            {
                LocalGroup group = null;

                if (InputObject != null)
                {
                    if (CheckShouldProcess(InputObject.ToString()))
                    {
                        group = InputObject;
                    }
                }
                else if (Name != null)
                {
                    group = sam.GetLocalGroup(Name);

                    if (!CheckShouldProcess(Name))
                    {
                        group = null;
                    }
                }
                else if (SID != null)
                {
                    group = sam.GetLocalGroup(SID);

                    if (!CheckShouldProcess(SID.ToString()))
                    {
                        group = null;
                    }
                }

                if (group != null)
                {
                    var delta = group.Clone();

                    delta.Description = Description;
                    sam.UpdateLocalGroup(group, delta);
                }
            }
            catch (Exception ex)
            {
                WriteError(ex.MakeErrorRecord());
            }
        }
 private IEnumerable <LocalPrincipal> ProcessGroup(LocalGroup group)
 {
     return(ProcessesMembership(sam.GetLocalGroupMembers(group)));
 }
 /// <summary>
 /// Remove members from a group.
 /// </summary>
 /// <param name="group">
 /// A <see cref="LocalGroup"/> object representing the group from which
 /// the members will be removed.
 /// </param>
 private void ProcessGroup(LocalGroup group)
 {
     string groupId = group.Name ?? group.SID.ToString();
     foreach (var member in this.Member)
     {
         LocalPrincipal principal = MakePrincipal(groupId, member);
         if (null != principal)
         {
             var ex = sam.RemoveLocalGroupMember(group, principal);
             if (null != ex)
             {
                 WriteError(ex.MakeErrorRecord());
             }
         }
     }
 }
 private IEnumerable<LocalPrincipal> ProcessGroup(LocalGroup group)
 {
     return ProcessesMembership(sam.GetLocalGroupMembers(group));
 }
Ejemplo n.º 6
0
 /// <summary>
 /// Construct a new LocalGroup object that is a copy of another
 /// </summary>
 /// <param name="other"></param>
 private LocalGroup(LocalGroup other)
   : this(other.Name)
 {
     Description = other.Description;
 }
Ejemplo n.º 7
0
        /// <summary>
        /// Remove members from a local group.
        /// </summary>
        /// <param name="group">
        /// A <see cref="LocalGroup"/> object identifying the group from
        /// which to remove members
        /// </param>
        /// <param name="member">
        /// An object of type <see cref="LocalPrincipal"/> identifying
        /// the member to be removed.
        /// </param>
        /// <returns>
        /// An Exception object indicating any errors encountered.
        /// </returns>
        /// <exception cref="GroupNotFoundException">
        /// Thrown if the group could not be found.
        /// </exception>
        internal Exception RemoveLocalGroupMember(LocalGroup group, LocalPrincipal member)
        {
            context = new Context(ContextOperation.RemoveMember, ContextObjectType.Group, group.Name, group);

            if (group.SID == null)
                context.target = group = GetLocalGroup(group.Name);

            return RemoveGroupMember(group.SID, member);
        }
Ejemplo n.º 8
0
 /// <summary>
 /// Construct a new LocalGroup object that is a copy of another
 /// </summary>
 /// <param name="other"></param>
 private LocalGroup(LocalGroup other)
     : this(other.Name)
 {
     Description = other.Description;
 }
Ejemplo n.º 9
0
        /// <summary>
        /// Rename a local group.
        /// </summary>
        /// <param name="group">
        /// A <see cref="LocalGroup"/> object containing
        /// information about the local group to be renamed.
        /// </param>
        /// <param name="newName">
        /// A string containing the new name for the local group.
        /// </param>
        /// <exception cref="GroupNotFoundException">
        /// Thrown when the specified group cannot be found.
        /// </exception>
        internal void RenameLocalGroup(LocalGroup group, string newName)
        {
            context = new Context(ContextOperation.Rename, ContextObjectType.Group, group.Name, group);

            if (group.SID == null)
                context.target = group = GetLocalGroup(group.Name);

            RenameGroup(group.SID, newName);
        }
Ejemplo n.º 10
0
        /// <summary>
        /// Retrieve members of a Local group.
        /// </summary>
        /// <param name="group">
        /// A <see cref="LocalGroup"/> object identifying the group whose members
        /// are requested.
        /// </param>
        /// <returns>
        /// An IEnumerable of <see cref="LocalPrincipal"/> objects containing the group's
        /// members.
        /// </returns>
        internal IEnumerable<LocalPrincipal> GetLocalGroupMembers(LocalGroup group)
        {
            context = new Context(ContextOperation.GetMember, ContextObjectType.Group, group.Name, group);

            if (group.SID == null)
                context.target = group = GetLocalGroup(group.Name);

            return GetGroupMembers(group.SID);
        }
Ejemplo n.º 11
0
        /// <summary>
        /// Update a local group with new property values
        /// </summary>
        /// <param name="group">
        /// A <see cref="LocalGroup"/> object representing the group to be updated.
        /// </param>
        /// <param name="changed">
        /// A LocalGroup object containing the desired changes.
        /// </param>
        /// <remarks>
        /// Currently, a group's description is the only changeable property.
        /// </remarks>
        internal void UpdateLocalGroup(LocalGroup group, LocalGroup changed)
        {
            context = new Context(ContextOperation.Set, ContextObjectType.Group, group.Name, group);

            UpdateGroup(group, changed);
        }
Ejemplo n.º 12
0
        /// <summary>
        /// Create a local group.
        /// </summary>
        /// <param name="group">A <see cref="LocalGroup"/> object containing
        /// information about the local group to be created.
        /// </param>
        /// <returns>
        /// A new LocalGroup object containing information about the newly
        /// created local group.
        /// </returns>
        /// <exception cref="GroupExistsException">
        /// Thrown when an attempt is made to create a local group that already
        /// exists.
        /// </exception>
        internal LocalGroup CreateLocalGroup(LocalGroup group)
        {
            context = new Context(ContextOperation.New, ContextObjectType.Group, group.Name, group.Name);

            return CreateGroup(group, localDomainHandle);
        }
Ejemplo n.º 13
0
        /// <summary>
        /// Create a populated LocalGroup object from a SamRidEnumeration object,
        /// using an already-opened SAM alias handle.
        /// </summary>
        /// <param name="sre">
        /// A <see cref="SamRidEnumeration"/> object containing minimal information
        /// about a local group.
        /// </param>
        /// <param name="aliasHandle">
        /// Handle to an open SAM alias.
        /// </param>
        /// <returns>
        /// A LocalGroup object, populated with group information.
        /// </returns>
        private LocalGroup MakeLocalGroupObject(SamRidEnumeration sre, IntPtr aliasHandle)
        {
            IntPtr buffer = IntPtr.Zero;
            UInt32 status = 0;

            try
            {
                ALIAS_GENERAL_INFORMATION generalInfo;

                status = SamApi.SamQueryInformationAlias(aliasHandle,
                                                         ALIAS_INFORMATION_CLASS.AliasGeneralInformation,
                                                         out buffer);
                ThrowOnFailure(status);
                generalInfo = ClrFacade.PtrToStructure<ALIAS_GENERAL_INFORMATION>(buffer);

                LocalGroup group = new LocalGroup()
                                    {
                                        PrincipalSource = GetPrincipalSource(sre),
                                        SID = RidToSid(sre.domainHandle, sre.RelativeId),

                                        Name = generalInfo.Name.ToString(),
                                        Description = generalInfo.AdminComment.ToString()
                                    };

                return group;
            }
            finally
            {
                if (buffer != IntPtr.Zero)
                    status = SamApi.SamFreeMemory(buffer);
            }
        }
Ejemplo n.º 14
0
        /// <summary>
        /// Update a local group with new property values. This method provides
        /// the actual implementation.
        /// </summary>
        /// <param name="group">
        /// A <see cref="LocalGroup"/> object representing the group to be updated.
        /// </param>
        /// <param name="changed">
        /// A LocalGroup object containing the desired changes.
        /// </param>
        /// <remarks>
        /// Currently, a group's description is the only changeable property.
        /// </remarks>
        private void UpdateGroup(LocalGroup group, LocalGroup changed)
        {
            // Only description may be changed
            if (group.Description == changed.Description)
                return;

            IntPtr aliasHandle = IntPtr.Zero;
            IntPtr buffer = IntPtr.Zero;

            if (group.SID == null)
                group = GetLocalGroup(group.Name);

            var sre = GetGroupSre(group.SID);
            UInt32 status;

            try
            {
                status = SamApi.SamOpenAlias(sre.domainHandle,
                                                 Win32.MAXIMUM_ALLOWED,
                                                 sre.RelativeId,
                                                 out aliasHandle);
                ThrowOnFailure(status);

                ALIAS_ADM_COMMENT_INFORMATION info = new ALIAS_ADM_COMMENT_INFORMATION();

                info.AdminComment = new UNICODE_STRING(changed.Description);
                buffer = Marshal.AllocHGlobal(Marshal.SizeOf(info));
                Marshal.StructureToPtr(info, buffer, false);

                status = SamApi.SamSetInformationAlias(aliasHandle,
                                                       ALIAS_INFORMATION_CLASS.AliasAdminCommentInformation,
                                                       buffer);

                ThrowOnFailure(status);
            }
            finally
            {
                if (buffer != IntPtr.Zero)
                {
                    ClrFacade.DestroyStructure<ALIAS_ADM_COMMENT_INFORMATION>(buffer);
                    Marshal.FreeHGlobal(buffer);
                }
                if (aliasHandle != IntPtr.Zero)
                    status = SamApi.SamCloseHandle(aliasHandle);
            }
        }
Ejemplo n.º 15
0
        /// <summary>
        /// Create a new group in the specified domain.
        /// </summary>
        /// <param name="groupInfo">
        /// A <see cref="LocalGroup"/> object containing information about the new group.
        /// </param>
        /// <param name="domainHandle">Handle to the domain in which to create the new group.</param>
        /// <returns>
        /// A LocalGroup object that represents the newly-created group.
        /// </returns>
        private LocalGroup CreateGroup(LocalGroup groupInfo, IntPtr domainHandle)
        {
            IntPtr aliasHandle = IntPtr.Zero;
            IntPtr buffer = IntPtr.Zero;
            UNICODE_STRING str = new UNICODE_STRING();
            UInt32 status;

            try
            {
                UInt32 relativeId;

                str = new UNICODE_STRING(groupInfo.Name);

                buffer = Marshal.AllocHGlobal(Marshal.SizeOf(str));
                Marshal.StructureToPtr(str, buffer, false);

                status = SamApi.SamCreateAliasInDomain(domainHandle,
                                                       buffer,
                                                       Win32.MAXIMUM_ALLOWED,
                                                       out aliasHandle,
                                                       out relativeId);
                ClrFacade.DestroyStructure<UNICODE_STRING>(buffer);
                Marshal.FreeHGlobal(buffer);
                buffer = IntPtr.Zero;
                ThrowOnFailure(status);

                if (!string.IsNullOrEmpty(groupInfo.Description))
                {
                    ALIAS_ADM_COMMENT_INFORMATION info = new ALIAS_ADM_COMMENT_INFORMATION();

                    info.AdminComment = new UNICODE_STRING(groupInfo.Description);
                    buffer = Marshal.AllocHGlobal(Marshal.SizeOf(info));
                    Marshal.StructureToPtr(info, buffer, false);

                    status = SamApi.SamSetInformationAlias(aliasHandle,
                                                           ALIAS_INFORMATION_CLASS.AliasAdminCommentInformation,
                                                           buffer);

                    ClrFacade.DestroyStructure<ALIAS_ADM_COMMENT_INFORMATION>(buffer);
                    Marshal.FreeHGlobal(buffer);
                    buffer = IntPtr.Zero;
                    ThrowOnFailure(status);
                }

                return MakeLocalGroupObject(new SamRidEnumeration
                                                {
                                                    domainHandle = domainHandle,
                                                    Name = groupInfo.Name,
                                                    RelativeId = relativeId
                                                },
                                            aliasHandle);
            }
            finally
            {
                if (buffer != IntPtr.Zero)
                    Marshal.FreeHGlobal(buffer);
                if (aliasHandle != IntPtr.Zero)
                    status = SamApi.SamCloseHandle(aliasHandle);
            }
        }