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/>.
23 #if defined(WITH_WINBIND)
25 #include "nsswitch/libwbclient/wbclient.h"
27 struct passwd
* winbind_getpwnam(const char * name
)
30 struct passwd
* tmp_pwd
= NULL
;
31 struct passwd
* pwd
= NULL
;
33 result
= wbcGetpwnam(name
, &tmp_pwd
);
34 if (result
!= WBC_ERR_SUCCESS
)
37 pwd
= tcopy_passwd(talloc_tos(), tmp_pwd
);
39 wbcFreeMemory(tmp_pwd
);
44 struct passwd
* winbind_getpwsid(const DOM_SID
*sid
)
47 struct passwd
* tmp_pwd
= NULL
;
48 struct passwd
* pwd
= NULL
;
49 struct wbcDomainSid dom_sid
;
51 memcpy(&dom_sid
, sid
, sizeof(dom_sid
));
53 result
= wbcGetpwsid(&dom_sid
, &tmp_pwd
);
54 if (result
!= WBC_ERR_SUCCESS
)
57 pwd
= tcopy_passwd(talloc_tos(), tmp_pwd
);
59 wbcFreeMemory(tmp_pwd
);
64 /* Call winbindd to convert a name to a sid */
66 bool winbind_lookup_name(const char *dom_name
, const char *name
, DOM_SID
*sid
,
67 enum lsa_SidType
*name_type
)
69 struct wbcDomainSid dom_sid
;
73 result
= wbcLookupName(dom_name
, name
, &dom_sid
, &type
);
74 if (result
!= WBC_ERR_SUCCESS
)
77 memcpy(sid
, &dom_sid
, sizeof(DOM_SID
));
78 *name_type
= (enum lsa_SidType
)type
;
83 /* Call winbindd to convert sid to name */
85 bool winbind_lookup_sid(TALLOC_CTX
*mem_ctx
, const DOM_SID
*sid
,
86 const char **domain
, const char **name
,
87 enum lsa_SidType
*name_type
)
89 struct wbcDomainSid dom_sid
;
92 char *domain_name
= NULL
;
93 char *account_name
= NULL
;
95 memcpy(&dom_sid
, sid
, sizeof(dom_sid
));
97 result
= wbcLookupSid(&dom_sid
, &domain_name
, &account_name
, &type
);
98 if (result
!= WBC_ERR_SUCCESS
)
101 /* Copy out result */
104 *domain
= talloc_strdup(mem_ctx
, domain_name
);
107 *name
= talloc_strdup(mem_ctx
, account_name
);
109 *name_type
= (enum lsa_SidType
)type
;
111 DEBUG(10, ("winbind_lookup_sid: SUCCESS: SID %s -> %s %s\n",
112 sid_string_dbg(sid
), domain_name
, account_name
));
114 wbcFreeMemory(domain_name
);
115 wbcFreeMemory(account_name
);
117 if ((domain
&& !*domain
) || (name
&& !*name
)) {
118 DEBUG(0,("winbind_lookup_sid: talloc() failed!\n"));
126 /* Ping winbindd to see it is alive */
128 bool winbind_ping(void)
130 wbcErr result
= wbcPing();
132 return (result
== WBC_ERR_SUCCESS
);
135 /* Call winbindd to convert SID to uid */
137 bool winbind_sid_to_uid(uid_t
*puid
, const DOM_SID
*sid
)
139 struct wbcDomainSid dom_sid
;
142 memcpy(&dom_sid
, sid
, sizeof(dom_sid
));
144 result
= wbcSidToUid(&dom_sid
, puid
);
146 return (result
== WBC_ERR_SUCCESS
);
149 /* Call winbindd to convert uid to sid */
151 bool winbind_uid_to_sid(DOM_SID
*sid
, uid_t uid
)
153 struct wbcDomainSid dom_sid
;
156 result
= wbcUidToSid(uid
, &dom_sid
);
157 if (result
== WBC_ERR_SUCCESS
) {
158 memcpy(sid
, &dom_sid
, sizeof(DOM_SID
));
160 sid_copy(sid
, &global_sid_NULL
);
163 return (result
== WBC_ERR_SUCCESS
);
166 /* Call winbindd to convert SID to gid */
168 bool winbind_sid_to_gid(gid_t
*pgid
, const DOM_SID
*sid
)
170 struct wbcDomainSid dom_sid
;
173 memcpy(&dom_sid
, sid
, sizeof(dom_sid
));
175 result
= wbcSidToGid(&dom_sid
, pgid
);
177 return (result
== WBC_ERR_SUCCESS
);
180 /* Call winbindd to convert gid to sid */
182 bool winbind_gid_to_sid(DOM_SID
*sid
, gid_t gid
)
184 struct wbcDomainSid dom_sid
;
187 result
= wbcGidToSid(gid
, &dom_sid
);
188 if (result
== WBC_ERR_SUCCESS
) {
189 memcpy(sid
, &dom_sid
, sizeof(DOM_SID
));
191 sid_copy(sid
, &global_sid_NULL
);
194 return (result
== WBC_ERR_SUCCESS
);
197 /* Check for a trusted domain */
199 wbcErr
wb_is_trusted_domain(const char *domain
)
202 struct wbcDomainInfo
*info
= NULL
;
204 result
= wbcDomainInfo(domain
, &info
);
206 if (WBC_ERROR_IS_OK(result
)) {
213 /* Lookup a set of rids in a given domain */
215 bool winbind_lookup_rids(TALLOC_CTX
*mem_ctx
,
216 const DOM_SID
*domain_sid
,
217 int num_rids
, uint32
*rids
,
218 const char **domain_name
,
219 const char ***names
, enum lsa_SidType
**types
)
221 const char *dom_name
= NULL
;
222 const char **namelist
= NULL
;
223 enum wbcSidType
*name_types
= NULL
;
224 struct wbcDomainSid dom_sid
;
228 memcpy(&dom_sid
, domain_sid
, sizeof(struct wbcDomainSid
));
230 ret
= wbcLookupRids(&dom_sid
, num_rids
, rids
,
231 &dom_name
, &namelist
, &name_types
);
232 if (ret
!= WBC_ERR_SUCCESS
) {
236 *domain_name
= talloc_strdup(mem_ctx
, dom_name
);
237 *names
= TALLOC_ARRAY(mem_ctx
, const char*, num_rids
);
238 *types
= TALLOC_ARRAY(mem_ctx
, enum lsa_SidType
, num_rids
);
240 for(i
=0; i
<num_rids
; i
++) {
241 (*names
)[i
] = talloc_strdup(*names
, namelist
[i
]);
242 (*types
)[i
] = (enum lsa_SidType
)name_types
[i
];
245 wbcFreeMemory(CONST_DISCARD(char*, dom_name
));
246 wbcFreeMemory(namelist
);
247 wbcFreeMemory(name_types
);
252 /* Ask Winbind to allocate a new uid for us */
254 bool winbind_allocate_uid(uid_t
*uid
)
258 ret
= wbcAllocateUid(uid
);
260 return (ret
== WBC_ERR_SUCCESS
);
263 /* Ask Winbind to allocate a new gid for us */
265 bool winbind_allocate_gid(gid_t
*gid
)
269 ret
= wbcAllocateGid(gid
);
271 return (ret
== WBC_ERR_SUCCESS
);
274 bool winbind_get_groups(TALLOC_CTX
* mem_ctx
, const char *account
, uint32_t *num_groups
, gid_t
**_groups
)
278 gid_t
*group_list
= NULL
;
280 ret
= wbcGetGroups(account
, &ngroups
, &group_list
);
281 if (ret
!= WBC_ERR_SUCCESS
)
284 *_groups
= TALLOC_ARRAY(mem_ctx
, gid_t
, ngroups
);
285 if (*_groups
== NULL
) {
286 wbcFreeMemory(group_list
);
290 memcpy(*_groups
, group_list
, ngroups
* sizeof(gid_t
));
291 *num_groups
= ngroups
;
293 wbcFreeMemory(group_list
);
297 bool winbind_get_sid_aliases(TALLOC_CTX
*mem_ctx
,
298 const DOM_SID
*dom_sid
,
299 const DOM_SID
*members
,
301 uint32_t **pp_alias_rids
,
302 size_t *p_num_alias_rids
)
305 struct wbcDomainSid domain_sid
;
306 struct wbcDomainSid
*sid_list
= NULL
;
311 memcpy(&domain_sid
, dom_sid
, sizeof(*dom_sid
));
313 sid_list
= TALLOC_ARRAY(mem_ctx
, struct wbcDomainSid
, num_members
);
315 for (i
=0; i
< num_members
; i
++) {
316 memcpy(&sid_list
[i
], &members
[i
], sizeof(sid_list
[i
]));
319 ret
= wbcGetSidAliases(&domain_sid
,
324 if (ret
!= WBC_ERR_SUCCESS
) {
328 *pp_alias_rids
= TALLOC_ARRAY(mem_ctx
, uint32_t, num_rids
);
329 if (*pp_alias_rids
== NULL
) {
334 memcpy(*pp_alias_rids
, rids
, sizeof(uint32_t) * num_rids
);
336 *p_num_alias_rids
= num_rids
;
342 #else /* WITH_WINBIND */
344 struct passwd
* winbind_getpwnam(const char * name
)
349 struct passwd
* winbind_getpwsid(const DOM_SID
*sid
)
354 bool winbind_lookup_name(const char *dom_name
, const char *name
, DOM_SID
*sid
,
355 enum lsa_SidType
*name_type
)
360 /* Call winbindd to convert sid to name */
362 bool winbind_lookup_sid(TALLOC_CTX
*mem_ctx
, const DOM_SID
*sid
,
363 const char **domain
, const char **name
,
364 enum lsa_SidType
*name_type
)
369 /* Ping winbindd to see it is alive */
371 bool winbind_ping(void)
376 /* Call winbindd to convert SID to uid */
378 bool winbind_sid_to_uid(uid_t
*puid
, const DOM_SID
*sid
)
383 /* Call winbindd to convert uid to sid */
385 bool winbind_uid_to_sid(DOM_SID
*sid
, uid_t uid
)
390 /* Call winbindd to convert SID to gid */
392 bool winbind_sid_to_gid(gid_t
*pgid
, const DOM_SID
*sid
)
397 /* Call winbindd to convert gid to sid */
399 bool winbind_gid_to_sid(DOM_SID
*sid
, gid_t gid
)
404 /* Check for a trusted domain */
406 wbcErr
wb_is_trusted_domain(const char *domain
)
408 return WBC_ERR_UNKNOWN_FAILURE
;
411 /* Lookup a set of rids in a given domain */
413 bool winbind_lookup_rids(TALLOC_CTX
*mem_ctx
,
414 const DOM_SID
*domain_sid
,
415 int num_rids
, uint32
*rids
,
416 const char **domain_name
,
417 const char ***names
, enum lsa_SidType
**types
)
422 /* Ask Winbind to allocate a new uid for us */
424 bool winbind_allocate_uid(uid_t
*uid
)
429 /* Ask Winbind to allocate a new gid for us */
431 bool winbind_allocate_gid(gid_t
*gid
)
436 bool winbind_get_groups(TALLOC_CTX
*mem_ctx
, const char *account
, uint32_t *num_groups
, gid_t
**_groups
)
441 bool winbind_get_sid_aliases(TALLOC_CTX
*mem_ctx
,
442 const DOM_SID
*dom_sid
,
443 const DOM_SID
*members
,
445 uint32_t **pp_alias_rids
,
446 size_t *p_num_alias_rids
)
451 #endif /* WITH_WINBIND */