2 * PROJECT: Local Security Authority Server DLL
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: dll/win32/samsrv/user.c
5 * PURPOSE: User specific helper functions
6 * COPYRIGHT: Copyright 2013 Eric Kohl
9 /* INCLUDES ****************************************************************/
13 WINE_DEFAULT_DEBUG_CHANNEL(samsrv
);
16 /* FUNCTIONS ***************************************************************/
19 SampOpenUserObject(IN PSAM_DB_OBJECT DomainObject
,
21 IN ACCESS_MASK DesiredAccess
,
22 OUT PSAM_DB_OBJECT
*UserObject
)
26 TRACE("(%p %lu %lx %p)\n",
27 DomainObject
, UserId
, DesiredAccess
, UserObject
);
29 /* Convert the RID into a string (hex) */
30 swprintf(szRid
, L
"%08lX", UserId
);
32 /* Create the user object */
33 return SampOpenDbObject(DomainObject
,
44 SampAddGroupMembershipToUser(IN PSAM_DB_OBJECT UserObject
,
48 PGROUP_MEMBERSHIP GroupsBuffer
= NULL
;
49 ULONG GroupsCount
= 0;
54 TRACE("(%p %lu %lx)\n",
55 UserObject
, GroupId
, Attributes
);
57 Status
= SampGetObjectAttribute(UserObject
,
62 if (!NT_SUCCESS(Status
) && Status
!= STATUS_OBJECT_NAME_NOT_FOUND
)
65 GroupsBuffer
= midl_user_allocate(Length
+ sizeof(GROUP_MEMBERSHIP
));
66 if (GroupsBuffer
== NULL
)
68 Status
= STATUS_INSUFFICIENT_RESOURCES
;
72 if (Status
!= STATUS_OBJECT_NAME_NOT_FOUND
)
74 Status
= SampGetObjectAttribute(UserObject
,
79 if (!NT_SUCCESS(Status
))
82 GroupsCount
= Length
/ sizeof(GROUP_MEMBERSHIP
);
85 for (i
= 0; i
< GroupsCount
; i
++)
87 if (GroupsBuffer
[i
].RelativeId
== GroupId
)
89 Status
= STATUS_MEMBER_IN_GROUP
;
94 GroupsBuffer
[GroupsCount
].RelativeId
= GroupId
;
95 GroupsBuffer
[GroupsCount
].Attributes
= Attributes
;
96 Length
+= sizeof(GROUP_MEMBERSHIP
);
98 Status
= SampSetObjectAttribute(UserObject
,
105 if (GroupsBuffer
!= NULL
)
106 midl_user_free(GroupsBuffer
);
113 SampRemoveGroupMembershipFromUser(IN PSAM_DB_OBJECT UserObject
,
116 PGROUP_MEMBERSHIP GroupsBuffer
= NULL
;
117 ULONG GroupsCount
= 0;
123 UserObject
, GroupId
);
125 Status
= SampGetObjectAttribute(UserObject
,
131 if (Status
== STATUS_OBJECT_NAME_NOT_FOUND
)
132 return STATUS_MEMBER_NOT_IN_GROUP
;
134 if (!NT_SUCCESS(Status
))
137 GroupsBuffer
= midl_user_allocate(Length
);
138 if (GroupsBuffer
== NULL
)
140 Status
= STATUS_INSUFFICIENT_RESOURCES
;
144 Status
= SampGetObjectAttribute(UserObject
,
149 if (!NT_SUCCESS(Status
))
152 Status
= STATUS_MEMBER_NOT_IN_GROUP
;
154 GroupsCount
= Length
/ sizeof(GROUP_MEMBERSHIP
);
155 for (i
= 0; i
< GroupsCount
; i
++)
157 if (GroupsBuffer
[i
].RelativeId
== GroupId
)
159 Length
-= sizeof(GROUP_MEMBERSHIP
);
160 Status
= STATUS_SUCCESS
;
164 if (Status
== STATUS_SUCCESS
&& i
< GroupsCount
- 1)
166 CopyMemory(&GroupsBuffer
[i
],
167 &GroupsBuffer
[i
+ 1],
168 sizeof(GROUP_MEMBERSHIP
));
172 if (!NT_SUCCESS(Status
))
175 Status
= SampSetObjectAttribute(UserObject
,
182 if (GroupsBuffer
!= NULL
)
183 midl_user_free(GroupsBuffer
);
190 SampGetUserGroupAttributes(IN PSAM_DB_OBJECT DomainObject
,
193 OUT PULONG GroupAttributes
)
195 PSAM_DB_OBJECT UserObject
= NULL
;
196 PGROUP_MEMBERSHIP GroupsBuffer
= NULL
;
201 Status
= SampOpenUserObject(DomainObject
,
205 if (!NT_SUCCESS(Status
))
210 SampGetObjectAttribute(UserObject
,
217 return STATUS_UNSUCCESSFUL
; /* FIXME */
219 GroupsBuffer
= midl_user_allocate(Length
);
220 if (GroupsBuffer
== NULL
)
222 Status
= STATUS_INSUFFICIENT_RESOURCES
;
226 Status
= SampGetObjectAttribute(UserObject
,
231 if (!NT_SUCCESS(Status
))
234 for (i
= 0; i
< (Length
/ sizeof(GROUP_MEMBERSHIP
)); i
++)
236 if (GroupsBuffer
[i
].RelativeId
== GroupId
)
238 *GroupAttributes
= GroupsBuffer
[i
].Attributes
;
244 if (GroupsBuffer
!= NULL
)
245 midl_user_free(GroupsBuffer
);
247 if (UserObject
!= NULL
)
248 SampCloseDbObject(UserObject
);
255 SampSetUserGroupAttributes(IN PSAM_DB_OBJECT DomainObject
,
258 IN ULONG GroupAttributes
)
260 PSAM_DB_OBJECT UserObject
= NULL
;
261 PGROUP_MEMBERSHIP GroupsBuffer
= NULL
;
266 Status
= SampOpenUserObject(DomainObject
,
270 if (!NT_SUCCESS(Status
))
275 SampGetObjectAttribute(UserObject
,
282 return STATUS_UNSUCCESSFUL
; /* FIXME */
284 GroupsBuffer
= midl_user_allocate(Length
);
285 if (GroupsBuffer
== NULL
)
287 Status
= STATUS_INSUFFICIENT_RESOURCES
;
291 Status
= SampGetObjectAttribute(UserObject
,
296 if (!NT_SUCCESS(Status
))
299 for (i
= 0; i
< (Length
/ sizeof(GROUP_MEMBERSHIP
)); i
++)
301 if (GroupsBuffer
[i
].RelativeId
== GroupId
)
303 GroupsBuffer
[i
].Attributes
= GroupAttributes
;
308 Status
= SampSetObjectAttribute(UserObject
,
315 if (GroupsBuffer
!= NULL
)
316 midl_user_free(GroupsBuffer
);
318 if (UserObject
!= NULL
)
319 SampCloseDbObject(UserObject
);