ldb: add tests for ldb_wildcard_compare
[Samba.git] / source3 / lib / winbind_util.c
blob7e3f8ab9eb81c2a98eca3f3b2a49f606aff42bcc
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"
23 #include "../lib/util/util_pw.h"
24 #include "nsswitch/libwbclient/wbclient.h"
26 #include "lib/winbind_util.h"
28 #if defined(WITH_WINBIND)
30 struct passwd * winbind_getpwnam(const char * name)
32 wbcErr result;
33 struct passwd * tmp_pwd = NULL;
34 struct passwd * pwd = NULL;
36 result = wbcGetpwnam(name, &tmp_pwd);
37 if (result != WBC_ERR_SUCCESS)
38 return pwd;
40 pwd = tcopy_passwd(talloc_tos(), tmp_pwd);
42 wbcFreeMemory(tmp_pwd);
44 return pwd;
47 struct passwd * winbind_getpwsid(const struct dom_sid *sid)
49 wbcErr result;
50 struct passwd * tmp_pwd = NULL;
51 struct passwd * pwd = NULL;
52 struct wbcDomainSid dom_sid;
54 memcpy(&dom_sid, sid, sizeof(dom_sid));
56 result = wbcGetpwsid(&dom_sid, &tmp_pwd);
57 if (result != WBC_ERR_SUCCESS)
58 return pwd;
60 pwd = tcopy_passwd(talloc_tos(), tmp_pwd);
62 wbcFreeMemory(tmp_pwd);
64 return pwd;
67 /* Call winbindd to convert a name to a sid */
69 bool winbind_lookup_name(const char *dom_name, const char *name, struct dom_sid *sid,
70 enum lsa_SidType *name_type)
72 struct wbcDomainSid dom_sid;
73 wbcErr result;
74 enum wbcSidType type;
76 result = wbcLookupName(dom_name, name, &dom_sid, &type);
77 if (result != WBC_ERR_SUCCESS)
78 return false;
80 memcpy(sid, &dom_sid, sizeof(struct dom_sid));
81 *name_type = (enum lsa_SidType)type;
83 return true;
86 /* Call winbindd to convert sid to name */
88 bool winbind_lookup_sid(TALLOC_CTX *mem_ctx, const struct dom_sid *sid,
89 const char **domain, const char **name,
90 enum lsa_SidType *name_type)
92 struct wbcDomainSid dom_sid;
93 wbcErr result;
94 enum wbcSidType type;
95 char *domain_name = NULL;
96 char *account_name = NULL;
97 struct dom_sid_buf buf;
99 memcpy(&dom_sid, sid, sizeof(dom_sid));
101 result = wbcLookupSid(&dom_sid, &domain_name, &account_name, &type);
102 if (result != WBC_ERR_SUCCESS)
103 return false;
105 /* Copy out result */
107 if (domain) {
108 *domain = talloc_strdup(mem_ctx, domain_name);
110 if (name) {
111 *name = talloc_strdup(mem_ctx, account_name);
113 *name_type = (enum lsa_SidType)type;
115 DEBUG(10, ("winbind_lookup_sid: SUCCESS: SID %s -> %s %s\n",
116 dom_sid_str_buf(sid, &buf), domain_name, account_name));
118 wbcFreeMemory(domain_name);
119 wbcFreeMemory(account_name);
121 if ((domain && !*domain) || (name && !*name)) {
122 DEBUG(0,("winbind_lookup_sid: talloc() failed!\n"));
123 return false;
127 return true;
130 /* Ping winbindd to see it is alive */
132 bool winbind_ping(void)
134 wbcErr result = wbcPing();
136 return (result == WBC_ERR_SUCCESS);
139 /* Call winbindd to convert SID to uid */
141 bool winbind_sid_to_uid(uid_t *puid, const struct dom_sid *sid)
143 struct wbcDomainSid dom_sid;
144 wbcErr result;
146 memcpy(&dom_sid, sid, sizeof(dom_sid));
148 result = wbcSidToUid(&dom_sid, puid);
150 return (result == WBC_ERR_SUCCESS);
153 /* Call winbindd to convert SID to gid */
155 bool winbind_sid_to_gid(gid_t *pgid, const struct dom_sid *sid)
157 struct wbcDomainSid dom_sid;
158 wbcErr result;
160 memcpy(&dom_sid, sid, sizeof(dom_sid));
162 result = wbcSidToGid(&dom_sid, pgid);
164 return (result == WBC_ERR_SUCCESS);
167 bool winbind_xid_to_sid(struct dom_sid *sid, const struct unixid *xid)
169 struct wbcUnixId wbc_xid;
170 struct wbcDomainSid dom_sid;
171 wbcErr result;
173 switch (xid->type) {
174 case ID_TYPE_UID:
175 wbc_xid = (struct wbcUnixId) {
176 .type = WBC_ID_TYPE_UID, .id.uid = xid->id
178 break;
179 case ID_TYPE_GID:
180 wbc_xid = (struct wbcUnixId) {
181 .type = WBC_ID_TYPE_GID, .id.gid = xid->id
183 break;
184 default:
185 return false;
188 result = wbcUnixIdsToSids(&wbc_xid, 1, &dom_sid);
189 if (result != WBC_ERR_SUCCESS) {
190 return false;
193 memcpy(sid, &dom_sid, sizeof(struct dom_sid));
194 return true;
197 /* Check for a trusted domain */
199 wbcErr wb_is_trusted_domain(const char *domain)
201 wbcErr result;
202 struct wbcDomainInfo *info = NULL;
204 result = wbcDomainInfo(domain, &info);
206 if (WBC_ERROR_IS_OK(result)) {
207 wbcFreeMemory(info);
210 return result;
213 /* Lookup a set of rids in a given domain */
215 bool winbind_lookup_rids(TALLOC_CTX *mem_ctx,
216 const struct dom_sid *domain_sid,
217 int num_rids, uint32_t *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;
225 wbcErr ret;
226 int i;
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) {
233 return false;
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(discard_const_p(char, dom_name));
246 wbcFreeMemory(namelist);
247 wbcFreeMemory(name_types);
249 return true;
252 /* Ask Winbind to allocate a new uid for us */
254 bool winbind_allocate_uid(uid_t *uid)
256 wbcErr ret;
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)
267 wbcErr ret;
269 ret = wbcAllocateGid(gid);
271 return (ret == WBC_ERR_SUCCESS);
274 bool winbind_lookup_usersids(TALLOC_CTX *mem_ctx,
275 const struct dom_sid *user_sid,
276 uint32_t *p_num_sids,
277 struct dom_sid **p_sids)
279 wbcErr ret;
280 struct wbcDomainSid dom_sid;
281 struct wbcDomainSid *sid_list = NULL;
282 uint32_t num_sids;
284 memcpy(&dom_sid, user_sid, sizeof(dom_sid));
286 ret = wbcLookupUserSids(&dom_sid,
287 false,
288 &num_sids,
289 &sid_list);
290 if (ret != WBC_ERR_SUCCESS) {
291 return false;
294 *p_sids = talloc_array(mem_ctx, struct dom_sid, num_sids);
295 if (*p_sids == NULL) {
296 wbcFreeMemory(sid_list);
297 return false;
300 memcpy(*p_sids, sid_list, sizeof(dom_sid) * num_sids);
302 *p_num_sids = num_sids;
303 wbcFreeMemory(sid_list);
305 return true;
308 #else /* WITH_WINBIND */
310 struct passwd * winbind_getpwnam(const char * name)
312 return NULL;
315 struct passwd * winbind_getpwsid(const struct dom_sid *sid)
317 return NULL;
320 bool winbind_lookup_name(const char *dom_name, const char *name, struct dom_sid *sid,
321 enum lsa_SidType *name_type)
323 return false;
326 /* Call winbindd to convert sid to name */
328 bool winbind_lookup_sid(TALLOC_CTX *mem_ctx, const struct dom_sid *sid,
329 const char **domain, const char **name,
330 enum lsa_SidType *name_type)
332 return false;
335 /* Ping winbindd to see it is alive */
337 bool winbind_ping(void)
339 return false;
342 /* Call winbindd to convert SID to uid */
344 bool winbind_sid_to_uid(uid_t *puid, const struct dom_sid *sid)
346 return false;
349 /* Call winbindd to convert SID to gid */
351 bool winbind_sid_to_gid(gid_t *pgid, const struct dom_sid *sid)
353 return false;
356 /* Call winbindd to convert uid or gid to SID */
358 bool winbind_xid_to_sid(struct dom_sid *sid, const struct unixid *xid)
360 return false;
363 /* Check for a trusted domain */
365 wbcErr wb_is_trusted_domain(const char *domain)
367 return WBC_ERR_UNKNOWN_FAILURE;
370 /* Lookup a set of rids in a given domain */
372 bool winbind_lookup_rids(TALLOC_CTX *mem_ctx,
373 const struct dom_sid *domain_sid,
374 int num_rids, uint32_t *rids,
375 const char **domain_name,
376 const char ***names, enum lsa_SidType **types)
378 return false;
381 /* Ask Winbind to allocate a new uid for us */
383 bool winbind_allocate_uid(uid_t *uid)
385 return false;
388 /* Ask Winbind to allocate a new gid for us */
390 bool winbind_allocate_gid(gid_t *gid)
392 return false;
395 bool winbind_lookup_usersids(TALLOC_CTX *mem_ctx,
396 const struct dom_sid *user_sid,
397 uint32_t *p_num_sids,
398 struct dom_sid **p_sids)
400 return false;
403 #endif /* WITH_WINBIND */