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;
120 NTSTATUS Status
= STATUS_SUCCESS
;
123 UserObject
, GroupId
);
125 SampGetObjectAttribute(UserObject
,
132 return STATUS_MEMBER_NOT_IN_GROUP
;
134 GroupsBuffer
= midl_user_allocate(Length
);
135 if (GroupsBuffer
== NULL
)
137 Status
= STATUS_INSUFFICIENT_RESOURCES
;
141 Status
= SampGetObjectAttribute(UserObject
,
146 if (!NT_SUCCESS(Status
))
149 Status
= STATUS_MEMBER_NOT_IN_GROUP
;
151 GroupsCount
= Length
/ sizeof(GROUP_MEMBERSHIP
);
152 for (i
= 0; i
< GroupsCount
; i
++)
154 if (GroupsBuffer
[i
].RelativeId
== GroupId
)
156 Length
-= sizeof(GROUP_MEMBERSHIP
);
157 Status
= STATUS_SUCCESS
;
159 if (GroupsCount
- i
- 1 > 0)
161 CopyMemory(&GroupsBuffer
[i
],
162 &GroupsBuffer
[i
+ 1],
163 (GroupsCount
- i
- 1) * sizeof(GROUP_MEMBERSHIP
));
170 if (!NT_SUCCESS(Status
))
173 Status
= SampSetObjectAttribute(UserObject
,
180 if (GroupsBuffer
!= NULL
)
181 midl_user_free(GroupsBuffer
);
188 SampGetUserGroupAttributes(IN PSAM_DB_OBJECT DomainObject
,
191 OUT PULONG GroupAttributes
)
193 PSAM_DB_OBJECT UserObject
= NULL
;
194 PGROUP_MEMBERSHIP GroupsBuffer
= NULL
;
199 Status
= SampOpenUserObject(DomainObject
,
203 if (!NT_SUCCESS(Status
))
208 SampGetObjectAttribute(UserObject
,
215 return STATUS_UNSUCCESSFUL
; /* FIXME */
217 GroupsBuffer
= midl_user_allocate(Length
);
218 if (GroupsBuffer
== NULL
)
220 Status
= STATUS_INSUFFICIENT_RESOURCES
;
224 Status
= SampGetObjectAttribute(UserObject
,
229 if (!NT_SUCCESS(Status
))
232 for (i
= 0; i
< (Length
/ sizeof(GROUP_MEMBERSHIP
)); i
++)
234 if (GroupsBuffer
[i
].RelativeId
== GroupId
)
236 *GroupAttributes
= GroupsBuffer
[i
].Attributes
;
242 if (GroupsBuffer
!= NULL
)
243 midl_user_free(GroupsBuffer
);
245 if (UserObject
!= NULL
)
246 SampCloseDbObject(UserObject
);
253 SampSetUserGroupAttributes(IN PSAM_DB_OBJECT DomainObject
,
256 IN ULONG GroupAttributes
)
258 PSAM_DB_OBJECT UserObject
= NULL
;
259 PGROUP_MEMBERSHIP GroupsBuffer
= NULL
;
264 Status
= SampOpenUserObject(DomainObject
,
268 if (!NT_SUCCESS(Status
))
273 SampGetObjectAttribute(UserObject
,
280 return STATUS_UNSUCCESSFUL
; /* FIXME */
282 GroupsBuffer
= midl_user_allocate(Length
);
283 if (GroupsBuffer
== NULL
)
285 Status
= STATUS_INSUFFICIENT_RESOURCES
;
289 Status
= SampGetObjectAttribute(UserObject
,
294 if (!NT_SUCCESS(Status
))
297 for (i
= 0; i
< (Length
/ sizeof(GROUP_MEMBERSHIP
)); i
++)
299 if (GroupsBuffer
[i
].RelativeId
== GroupId
)
301 GroupsBuffer
[i
].Attributes
= GroupAttributes
;
306 Status
= SampSetObjectAttribute(UserObject
,
313 if (GroupsBuffer
!= NULL
)
314 midl_user_free(GroupsBuffer
);
316 if (UserObject
!= NULL
)
317 SampCloseDbObject(UserObject
);