2 Unix SMB/CIFS implementation.
3 Winbind Utility functions
5 Copyright (C) Gerald (Jerry) Carter 2007
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "../libcli/security/security.h"
24 #if defined(WITH_WINBIND)
26 #include "nsswitch/libwbclient/wbclient.h"
28 struct passwd
* winbind_getpwnam(const char * name
)
31 struct passwd
* tmp_pwd
= NULL
;
32 struct passwd
* pwd
= NULL
;
34 result
= wbcGetpwnam(name
, &tmp_pwd
);
35 if (result
!= WBC_ERR_SUCCESS
)
38 pwd
= tcopy_passwd(talloc_tos(), tmp_pwd
);
40 wbcFreeMemory(tmp_pwd
);
45 struct passwd
* winbind_getpwsid(const struct dom_sid
*sid
)
48 struct passwd
* tmp_pwd
= NULL
;
49 struct passwd
* pwd
= NULL
;
50 struct wbcDomainSid dom_sid
;
52 memcpy(&dom_sid
, sid
, sizeof(dom_sid
));
54 result
= wbcGetpwsid(&dom_sid
, &tmp_pwd
);
55 if (result
!= WBC_ERR_SUCCESS
)
58 pwd
= tcopy_passwd(talloc_tos(), tmp_pwd
);
60 wbcFreeMemory(tmp_pwd
);
65 /* Call winbindd to convert a name to a sid */
67 bool winbind_lookup_name(const char *dom_name
, const char *name
, struct dom_sid
*sid
,
68 enum lsa_SidType
*name_type
)
70 struct wbcDomainSid dom_sid
;
74 result
= wbcLookupName(dom_name
, name
, &dom_sid
, &type
);
75 if (result
!= WBC_ERR_SUCCESS
)
78 memcpy(sid
, &dom_sid
, sizeof(struct dom_sid
));
79 *name_type
= (enum lsa_SidType
)type
;
84 /* Call winbindd to convert sid to name */
86 bool winbind_lookup_sid(TALLOC_CTX
*mem_ctx
, const struct dom_sid
*sid
,
87 const char **domain
, const char **name
,
88 enum lsa_SidType
*name_type
)
90 struct wbcDomainSid dom_sid
;
93 char *domain_name
= NULL
;
94 char *account_name
= NULL
;
96 memcpy(&dom_sid
, sid
, sizeof(dom_sid
));
98 result
= wbcLookupSid(&dom_sid
, &domain_name
, &account_name
, &type
);
99 if (result
!= WBC_ERR_SUCCESS
)
102 /* Copy out result */
105 *domain
= talloc_strdup(mem_ctx
, domain_name
);
108 *name
= talloc_strdup(mem_ctx
, account_name
);
110 *name_type
= (enum lsa_SidType
)type
;
112 DEBUG(10, ("winbind_lookup_sid: SUCCESS: SID %s -> %s %s\n",
113 sid_string_dbg(sid
), domain_name
, account_name
));
115 wbcFreeMemory(domain_name
);
116 wbcFreeMemory(account_name
);
118 if ((domain
&& !*domain
) || (name
&& !*name
)) {
119 DEBUG(0,("winbind_lookup_sid: talloc() failed!\n"));
127 /* Ping winbindd to see it is alive */
129 bool winbind_ping(void)
131 wbcErr result
= wbcPing();
133 return (result
== WBC_ERR_SUCCESS
);
136 /* Call winbindd to convert SID to uid */
138 bool winbind_sid_to_uid(uid_t
*puid
, const struct dom_sid
*sid
)
140 struct wbcDomainSid dom_sid
;
143 memcpy(&dom_sid
, sid
, sizeof(dom_sid
));
145 result
= wbcSidToUid(&dom_sid
, puid
);
147 return (result
== WBC_ERR_SUCCESS
);
150 /* Call winbindd to convert uid to sid */
152 bool winbind_uid_to_sid(struct dom_sid
*sid
, uid_t uid
)
154 struct wbcDomainSid dom_sid
;
157 result
= wbcUidToSid(uid
, &dom_sid
);
158 if (result
== WBC_ERR_SUCCESS
) {
159 memcpy(sid
, &dom_sid
, sizeof(struct dom_sid
));
161 sid_copy(sid
, &global_sid_NULL
);
164 return (result
== WBC_ERR_SUCCESS
);
167 /* Call winbindd to convert SID to gid */
169 bool winbind_sid_to_gid(gid_t
*pgid
, const struct dom_sid
*sid
)
171 struct wbcDomainSid dom_sid
;
174 memcpy(&dom_sid
, sid
, sizeof(dom_sid
));
176 result
= wbcSidToGid(&dom_sid
, pgid
);
178 return (result
== WBC_ERR_SUCCESS
);
181 /* Call winbindd to convert gid to sid */
183 bool winbind_gid_to_sid(struct dom_sid
*sid
, gid_t gid
)
185 struct wbcDomainSid dom_sid
;
188 result
= wbcGidToSid(gid
, &dom_sid
);
189 if (result
== WBC_ERR_SUCCESS
) {
190 memcpy(sid
, &dom_sid
, sizeof(struct dom_sid
));
192 sid_copy(sid
, &global_sid_NULL
);
195 return (result
== WBC_ERR_SUCCESS
);
198 /* Check for a trusted domain */
200 wbcErr
wb_is_trusted_domain(const char *domain
)
203 struct wbcDomainInfo
*info
= NULL
;
205 result
= wbcDomainInfo(domain
, &info
);
207 if (WBC_ERROR_IS_OK(result
)) {
214 /* Lookup a set of rids in a given domain */
216 bool winbind_lookup_rids(TALLOC_CTX
*mem_ctx
,
217 const struct dom_sid
*domain_sid
,
218 int num_rids
, uint32
*rids
,
219 const char **domain_name
,
220 const char ***names
, enum lsa_SidType
**types
)
222 const char *dom_name
= NULL
;
223 const char **namelist
= NULL
;
224 enum wbcSidType
*name_types
= NULL
;
225 struct wbcDomainSid dom_sid
;
229 memcpy(&dom_sid
, domain_sid
, sizeof(struct wbcDomainSid
));
231 ret
= wbcLookupRids(&dom_sid
, num_rids
, rids
,
232 &dom_name
, &namelist
, &name_types
);
233 if (ret
!= WBC_ERR_SUCCESS
) {
237 *domain_name
= talloc_strdup(mem_ctx
, dom_name
);
238 *names
= TALLOC_ARRAY(mem_ctx
, const char*, num_rids
);
239 *types
= TALLOC_ARRAY(mem_ctx
, enum lsa_SidType
, num_rids
);
241 for(i
=0; i
<num_rids
; i
++) {
242 (*names
)[i
] = talloc_strdup(*names
, namelist
[i
]);
243 (*types
)[i
] = (enum lsa_SidType
)name_types
[i
];
246 wbcFreeMemory(CONST_DISCARD(char*, dom_name
));
247 wbcFreeMemory(namelist
);
248 wbcFreeMemory(name_types
);
253 /* Ask Winbind to allocate a new uid for us */
255 bool winbind_allocate_uid(uid_t
*uid
)
259 ret
= wbcAllocateUid(uid
);
261 return (ret
== WBC_ERR_SUCCESS
);
264 /* Ask Winbind to allocate a new gid for us */
266 bool winbind_allocate_gid(gid_t
*gid
)
270 ret
= wbcAllocateGid(gid
);
272 return (ret
== WBC_ERR_SUCCESS
);
275 bool winbind_get_groups(TALLOC_CTX
* mem_ctx
, const char *account
, uint32_t *num_groups
, gid_t
**_groups
)
279 gid_t
*group_list
= NULL
;
281 ret
= wbcGetGroups(account
, &ngroups
, &group_list
);
282 if (ret
!= WBC_ERR_SUCCESS
)
285 *_groups
= TALLOC_ARRAY(mem_ctx
, gid_t
, ngroups
);
286 if (*_groups
== NULL
) {
287 wbcFreeMemory(group_list
);
291 memcpy(*_groups
, group_list
, ngroups
* sizeof(gid_t
));
292 *num_groups
= ngroups
;
294 wbcFreeMemory(group_list
);
298 bool winbind_get_sid_aliases(TALLOC_CTX
*mem_ctx
,
299 const struct dom_sid
*dom_sid
,
300 const struct dom_sid
*members
,
302 uint32_t **pp_alias_rids
,
303 size_t *p_num_alias_rids
)
306 struct wbcDomainSid domain_sid
;
307 struct wbcDomainSid
*sid_list
= NULL
;
312 memcpy(&domain_sid
, dom_sid
, sizeof(*dom_sid
));
314 sid_list
= TALLOC_ARRAY(mem_ctx
, struct wbcDomainSid
, num_members
);
316 for (i
=0; i
< num_members
; i
++) {
317 memcpy(&sid_list
[i
], &members
[i
], sizeof(sid_list
[i
]));
320 ret
= wbcGetSidAliases(&domain_sid
,
325 if (ret
!= WBC_ERR_SUCCESS
) {
329 *pp_alias_rids
= TALLOC_ARRAY(mem_ctx
, uint32_t, num_rids
);
330 if (*pp_alias_rids
== NULL
) {
335 memcpy(*pp_alias_rids
, rids
, sizeof(uint32_t) * num_rids
);
337 *p_num_alias_rids
= num_rids
;
343 #else /* WITH_WINBIND */
345 struct passwd
* winbind_getpwnam(const char * name
)
350 struct passwd
* winbind_getpwsid(const struct dom_sid
*sid
)
355 bool winbind_lookup_name(const char *dom_name
, const char *name
, struct dom_sid
*sid
,
356 enum lsa_SidType
*name_type
)
361 /* Call winbindd to convert sid to name */
363 bool winbind_lookup_sid(TALLOC_CTX
*mem_ctx
, const struct dom_sid
*sid
,
364 const char **domain
, const char **name
,
365 enum lsa_SidType
*name_type
)
370 /* Ping winbindd to see it is alive */
372 bool winbind_ping(void)
377 /* Call winbindd to convert SID to uid */
379 bool winbind_sid_to_uid(uid_t
*puid
, const struct dom_sid
*sid
)
384 /* Call winbindd to convert uid to sid */
386 bool winbind_uid_to_sid(struct dom_sid
*sid
, uid_t uid
)
391 /* Call winbindd to convert SID to gid */
393 bool winbind_sid_to_gid(gid_t
*pgid
, const struct dom_sid
*sid
)
398 /* Call winbindd to convert gid to sid */
400 bool winbind_gid_to_sid(struct dom_sid
*sid
, gid_t gid
)
405 /* Check for a trusted domain */
407 wbcErr
wb_is_trusted_domain(const char *domain
)
409 return WBC_ERR_UNKNOWN_FAILURE
;
412 /* Lookup a set of rids in a given domain */
414 bool winbind_lookup_rids(TALLOC_CTX
*mem_ctx
,
415 const struct dom_sid
*domain_sid
,
416 int num_rids
, uint32
*rids
,
417 const char **domain_name
,
418 const char ***names
, enum lsa_SidType
**types
)
423 /* Ask Winbind to allocate a new uid for us */
425 bool winbind_allocate_uid(uid_t
*uid
)
430 /* Ask Winbind to allocate a new gid for us */
432 bool winbind_allocate_gid(gid_t
*gid
)
437 bool winbind_get_groups(TALLOC_CTX
*mem_ctx
, const char *account
, uint32_t *num_groups
, gid_t
**_groups
)
442 bool winbind_get_sid_aliases(TALLOC_CTX
*mem_ctx
,
443 const struct dom_sid
*dom_sid
,
444 const struct dom_sid
*members
,
446 uint32_t **pp_alias_rids
,
447 size_t *p_num_alias_rids
)
452 #endif /* WITH_WINBIND */