objectclass -> objectClass
[Samba/gebeck_regimport.git] / source3 / lib / winbind_util.c
blob14356b09cf7b9d22b88621d871062d18856a12d0
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"
23 #if defined(WITH_WINBIND)
25 #include "nsswitch/libwbclient/wbclient.h"
27 /* Call winbindd to convert a name to a sid */
29 bool winbind_lookup_name(const char *dom_name, const char *name, DOM_SID *sid,
30 enum lsa_SidType *name_type)
32 struct wbcDomainSid dom_sid;
33 wbcErr result;
34 enum wbcSidType type;
36 result = wbcLookupName(dom_name, name, &dom_sid, &type);
37 if (result != WBC_ERR_SUCCESS)
38 return false;
40 memcpy(sid, &dom_sid, sizeof(DOM_SID));
41 *name_type = (enum lsa_SidType)type;
43 return true;
46 /* Call winbindd to convert sid to name */
48 bool winbind_lookup_sid(TALLOC_CTX *mem_ctx, const DOM_SID *sid,
49 const char **domain, const char **name,
50 enum lsa_SidType *name_type)
52 struct wbcDomainSid dom_sid;
53 wbcErr result;
54 enum wbcSidType type;
55 char *domain_name = NULL;
56 char *account_name = NULL;
58 memcpy(&dom_sid, sid, sizeof(dom_sid));
60 result = wbcLookupSid(&dom_sid, &domain_name, &account_name, &type);
61 if (result != WBC_ERR_SUCCESS)
62 return false;
64 /* Copy out result */
66 if (domain) {
67 *domain = talloc_strdup(mem_ctx, domain_name);
69 if (name) {
70 *name = talloc_strdup(mem_ctx, account_name);
72 *name_type = (enum lsa_SidType)type;
74 DEBUG(10, ("winbind_lookup_sid: SUCCESS: SID %s -> %s %s\n",
75 sid_string_dbg(sid), domain_name, account_name));
77 wbcFreeMemory(domain_name);
78 wbcFreeMemory(account_name);
80 if ((domain && !*domain) || (name && !*name)) {
81 DEBUG(0,("winbind_lookup_sid: talloc() failed!\n"));
82 return false;
86 return true;
89 /* Ping winbindd to see it is alive */
91 bool winbind_ping(void)
93 wbcErr result = wbcPing();
95 return (result == WBC_ERR_SUCCESS);
98 /* Call winbindd to convert SID to uid */
100 bool winbind_sid_to_uid(uid_t *puid, const DOM_SID *sid)
102 struct wbcDomainSid dom_sid;
103 wbcErr result;
105 memcpy(&dom_sid, sid, sizeof(dom_sid));
107 result = wbcSidToUid(&dom_sid, puid);
109 return (result == WBC_ERR_SUCCESS);
112 /* Call winbindd to convert uid to sid */
114 bool winbind_uid_to_sid(DOM_SID *sid, uid_t uid)
116 struct wbcDomainSid dom_sid;
117 wbcErr result;
119 result = wbcUidToSid(uid, &dom_sid);
120 if (result == WBC_ERR_SUCCESS) {
121 memcpy(sid, &dom_sid, sizeof(DOM_SID));
122 } else {
123 sid_copy(sid, &global_sid_NULL);
126 return (result == WBC_ERR_SUCCESS);
129 /* Call winbindd to convert SID to gid */
131 bool winbind_sid_to_gid(gid_t *pgid, const DOM_SID *sid)
133 struct wbcDomainSid dom_sid;
134 wbcErr result;
136 memcpy(&dom_sid, sid, sizeof(dom_sid));
138 result = wbcSidToGid(&dom_sid, pgid);
140 return (result == WBC_ERR_SUCCESS);
143 /* Call winbindd to convert gid to sid */
145 bool winbind_gid_to_sid(DOM_SID *sid, gid_t gid)
147 struct wbcDomainSid dom_sid;
148 wbcErr result;
150 result = wbcGidToSid(gid, &dom_sid);
151 if (result == WBC_ERR_SUCCESS) {
152 memcpy(sid, &dom_sid, sizeof(DOM_SID));
153 } else {
154 sid_copy(sid, &global_sid_NULL);
157 return (result == WBC_ERR_SUCCESS);
160 /* Check for a trusted domain */
162 wbcErr wb_is_trusted_domain(const char *domain)
164 wbcErr result;
165 struct wbcDomainInfo *info = NULL;
167 result = wbcDomainInfo(domain, &info);
169 if (WBC_ERROR_IS_OK(result)) {
170 wbcFreeMemory(info);
173 return result;
176 /* Lookup a set of rids in a given domain */
178 bool winbind_lookup_rids(TALLOC_CTX *mem_ctx,
179 const DOM_SID *domain_sid,
180 int num_rids, uint32 *rids,
181 const char **domain_name,
182 const char ***names, enum lsa_SidType **types)
184 const char *dom_name = NULL;
185 const char **namelist = NULL;
186 enum wbcSidType *name_types = NULL;
187 struct wbcDomainSid dom_sid;
188 wbcErr ret;
189 int i;
191 memcpy(&dom_sid, domain_sid, sizeof(struct wbcDomainSid));
193 ret = wbcLookupRids(&dom_sid, num_rids, rids,
194 &dom_name, &namelist, &name_types);
195 if (ret != WBC_ERR_SUCCESS) {
196 return false;
199 *domain_name = talloc_strdup(mem_ctx, dom_name);
200 *names = TALLOC_ARRAY(mem_ctx, const char*, num_rids);
201 *types = TALLOC_ARRAY(mem_ctx, enum lsa_SidType, num_rids);
203 for(i=0; i<num_rids; i++) {
204 (*names)[i] = talloc_strdup(*names, namelist[i]);
205 (*types)[i] = (enum lsa_SidType)name_types[i];
208 wbcFreeMemory(CONST_DISCARD(char*, dom_name));
209 wbcFreeMemory(namelist);
210 wbcFreeMemory(name_types);
212 return true;
215 /* Ask Winbind to allocate a new uid for us */
217 bool winbind_allocate_uid(uid_t *uid)
219 wbcErr ret;
221 ret = wbcAllocateUid(uid);
223 return (ret == WBC_ERR_SUCCESS);
226 /* Ask Winbind to allocate a new gid for us */
228 bool winbind_allocate_gid(gid_t *gid)
230 wbcErr ret;
232 ret = wbcAllocateGid(gid);
234 return (ret == WBC_ERR_SUCCESS);
237 #else /* WITH_WINBIND */
239 bool winbind_lookup_name(const char *dom_name, const char *name, DOM_SID *sid,
240 enum lsa_SidType *name_type)
242 return false;
245 /* Call winbindd to convert sid to name */
247 bool winbind_lookup_sid(TALLOC_CTX *mem_ctx, const DOM_SID *sid,
248 const char **domain, const char **name,
249 enum lsa_SidType *name_type)
251 return false;
254 /* Ping winbindd to see it is alive */
256 bool winbind_ping(void)
258 return false;
261 /* Call winbindd to convert SID to uid */
263 bool winbind_sid_to_uid(uid_t *puid, const DOM_SID *sid)
265 return false;
268 /* Call winbindd to convert uid to sid */
270 bool winbind_uid_to_sid(DOM_SID *sid, uid_t uid)
272 return false;
275 /* Call winbindd to convert SID to gid */
277 bool winbind_sid_to_gid(gid_t *pgid, const DOM_SID *sid)
279 return false;
282 /* Call winbindd to convert gid to sid */
284 bool winbind_gid_to_sid(DOM_SID *sid, gid_t gid)
286 return false;
289 /* Check for a trusted domain */
291 wbcErr wb_is_trusted_domain(const char *domain)
293 return WBC_ERR_UNKNOWN_FAILURE;
296 /* Lookup a set of rids in a given domain */
298 bool winbind_lookup_rids(TALLOC_CTX *mem_ctx,
299 const DOM_SID *domain_sid,
300 int num_rids, uint32 *rids,
301 const char **domain_name,
302 const char ***names, enum lsa_SidType **types)
304 return false;
307 /* Ask Winbind to allocate a new uid for us */
309 bool winbind_allocate_uid(uid_t *uid)
311 return false;
314 /* Ask Winbind to allocate a new gid for us */
316 bool winbind_allocate_gid(gid_t *gid)
318 return false;
321 #endif /* WITH_WINBIND */