nsstest: no need for two copies of the same tool.
[Samba/vl.git] / source3 / lib / winbind_util.c
blob57cb3a9f6a547463545abd577b658312bba1fa2a
1 /*
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/>.
21 #include "includes.h"
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)
30 wbcErr result;
31 struct passwd * tmp_pwd = NULL;
32 struct passwd * pwd = NULL;
34 result = wbcGetpwnam(name, &tmp_pwd);
35 if (result != WBC_ERR_SUCCESS)
36 return pwd;
38 pwd = tcopy_passwd(talloc_tos(), tmp_pwd);
40 wbcFreeMemory(tmp_pwd);
42 return pwd;
45 struct passwd * winbind_getpwsid(const struct dom_sid *sid)
47 wbcErr result;
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)
56 return pwd;
58 pwd = tcopy_passwd(talloc_tos(), tmp_pwd);
60 wbcFreeMemory(tmp_pwd);
62 return 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;
71 wbcErr result;
72 enum wbcSidType type;
74 result = wbcLookupName(dom_name, name, &dom_sid, &type);
75 if (result != WBC_ERR_SUCCESS)
76 return false;
78 memcpy(sid, &dom_sid, sizeof(struct dom_sid));
79 *name_type = (enum lsa_SidType)type;
81 return true;
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;
91 wbcErr result;
92 enum wbcSidType type;
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)
100 return false;
102 /* Copy out result */
104 if (domain) {
105 *domain = talloc_strdup(mem_ctx, domain_name);
107 if (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"));
120 return false;
124 return true;
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;
141 wbcErr result;
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;
155 wbcErr result;
157 result = wbcUidToSid(uid, &dom_sid);
158 if (result == WBC_ERR_SUCCESS) {
159 memcpy(sid, &dom_sid, sizeof(struct dom_sid));
160 } else {
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;
172 wbcErr result;
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;
186 wbcErr result;
188 result = wbcGidToSid(gid, &dom_sid);
189 if (result == WBC_ERR_SUCCESS) {
190 memcpy(sid, &dom_sid, sizeof(struct dom_sid));
191 } else {
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)
202 wbcErr result;
203 struct wbcDomainInfo *info = NULL;
205 result = wbcDomainInfo(domain, &info);
207 if (WBC_ERROR_IS_OK(result)) {
208 wbcFreeMemory(info);
211 return 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;
226 wbcErr ret;
227 int i;
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) {
234 return false;
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);
250 return true;
253 /* Ask Winbind to allocate a new uid for us */
255 bool winbind_allocate_uid(uid_t *uid)
257 wbcErr ret;
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)
268 wbcErr ret;
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)
277 wbcErr ret;
278 uint32_t ngroups;
279 gid_t *group_list = NULL;
281 ret = wbcGetGroups(account, &ngroups, &group_list);
282 if (ret != WBC_ERR_SUCCESS)
283 return false;
285 *_groups = TALLOC_ARRAY(mem_ctx, gid_t, ngroups);
286 if (*_groups == NULL) {
287 wbcFreeMemory(group_list);
288 return false;
291 memcpy(*_groups, group_list, ngroups* sizeof(gid_t));
292 *num_groups = ngroups;
294 wbcFreeMemory(group_list);
295 return true;
298 bool winbind_get_sid_aliases(TALLOC_CTX *mem_ctx,
299 const struct dom_sid *dom_sid,
300 const struct dom_sid *members,
301 size_t num_members,
302 uint32_t **pp_alias_rids,
303 size_t *p_num_alias_rids)
305 wbcErr ret;
306 struct wbcDomainSid domain_sid;
307 struct wbcDomainSid *sid_list = NULL;
308 size_t i;
309 uint32_t * rids;
310 uint32_t num_rids;
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,
321 sid_list,
322 num_members,
323 &rids,
324 &num_rids);
325 if (ret != WBC_ERR_SUCCESS) {
326 return false;
329 *pp_alias_rids = TALLOC_ARRAY(mem_ctx, uint32_t, num_rids);
330 if (*pp_alias_rids == NULL) {
331 wbcFreeMemory(rids);
332 return false;
335 memcpy(*pp_alias_rids, rids, sizeof(uint32_t) * num_rids);
337 *p_num_alias_rids = num_rids;
338 wbcFreeMemory(rids);
340 return true;
343 #else /* WITH_WINBIND */
345 struct passwd * winbind_getpwnam(const char * name)
347 return NULL;
350 struct passwd * winbind_getpwsid(const struct dom_sid *sid)
352 return NULL;
355 bool winbind_lookup_name(const char *dom_name, const char *name, struct dom_sid *sid,
356 enum lsa_SidType *name_type)
358 return false;
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)
367 return false;
370 /* Ping winbindd to see it is alive */
372 bool winbind_ping(void)
374 return false;
377 /* Call winbindd to convert SID to uid */
379 bool winbind_sid_to_uid(uid_t *puid, const struct dom_sid *sid)
381 return false;
384 /* Call winbindd to convert uid to sid */
386 bool winbind_uid_to_sid(struct dom_sid *sid, uid_t uid)
388 return false;
391 /* Call winbindd to convert SID to gid */
393 bool winbind_sid_to_gid(gid_t *pgid, const struct dom_sid *sid)
395 return false;
398 /* Call winbindd to convert gid to sid */
400 bool winbind_gid_to_sid(struct dom_sid *sid, gid_t gid)
402 return false;
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)
420 return false;
423 /* Ask Winbind to allocate a new uid for us */
425 bool winbind_allocate_uid(uid_t *uid)
427 return false;
430 /* Ask Winbind to allocate a new gid for us */
432 bool winbind_allocate_gid(gid_t *gid)
434 return false;
437 bool winbind_get_groups(TALLOC_CTX *mem_ctx, const char *account, uint32_t *num_groups, gid_t **_groups)
439 return false;
442 bool winbind_get_sid_aliases(TALLOC_CTX *mem_ctx,
443 const struct dom_sid *dom_sid,
444 const struct dom_sid *members,
445 size_t num_members,
446 uint32_t **pp_alias_rids,
447 size_t *p_num_alias_rids)
449 return false;
452 #endif /* WITH_WINBIND */