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 /* 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;
36 result = wbcLookupName(dom_name, name, &dom_sid, &type);
37 if (result != WBC_ERR_SUCCESS)
40 memcpy(sid, &dom_sid, sizeof(DOM_SID));
41 *name_type = (enum lsa_SidType)type;
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;
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)
67 *domain = talloc_strdup(mem_ctx, domain_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 SAFE_FREE(domain_name);
78 SAFE_FREE(account_name);
80 if ((domain && !*domain) || (name && !*name)) {
81 DEBUG(0,("winbind_lookup_sid: talloc() failed!\n"));
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;
105 memcpy(&dom_sid, sid, sizeof(dom_sid));
107 result = wbcSidToUid(&dom_sid, puid);
108 if (result != WBC_ERR_SUCCESS)
111 return (result == WBC_ERR_SUCCESS);
114 /* Call winbindd to convert uid to sid */
116 bool winbind_uid_to_sid(DOM_SID *sid, uid_t uid)
118 struct wbcDomainSid dom_sid;
121 result = wbcUidToSid(uid, &dom_sid);
122 if (result == WBC_ERR_SUCCESS) {
123 memcpy(sid, &dom_sid, sizeof(DOM_SID));
125 sid_copy(sid, &global_sid_NULL);
128 return (result == WBC_ERR_SUCCESS);
131 /* Call winbindd to convert SID to gid */
133 bool winbind_sid_to_gid(gid_t *pgid, const DOM_SID *sid)
135 struct wbcDomainSid dom_sid;
138 memcpy(&dom_sid, sid, sizeof(dom_sid));
140 result = wbcSidToGid(&dom_sid, pgid);
141 if (result != WBC_ERR_SUCCESS)
144 return (result == WBC_ERR_SUCCESS);
147 /* Call winbindd to convert gid to sid */
149 bool winbind_gid_to_sid(DOM_SID *sid, gid_t gid)
151 struct wbcDomainSid dom_sid;
154 result = wbcGidToSid(gid, &dom_sid);
155 if (result == WBC_ERR_SUCCESS) {
156 memcpy(sid, &dom_sid, sizeof(DOM_SID));
158 sid_copy(sid, &global_sid_NULL);
161 return (result == WBC_ERR_SUCCESS);
164 /* Check for a trusted domain */
166 wbcErr wb_is_trusted_domain(const char *domain)
169 struct wbcDomainInfo info;
171 result = wbcDomainInfo(domain, &info);
173 if (result == WBC_ERR_SUCCESS) {
174 SAFE_FREE(info.short_name);
175 SAFE_FREE(info.dns_name);
181 /* Lookup a set of rids in a given domain */
183 bool winbind_lookup_rids(TALLOC_CTX *mem_ctx,
184 const DOM_SID *domain_sid,
185 int num_rids, uint32 *rids,
186 const char **domain_name,
187 const char ***names, enum lsa_SidType **types)
189 const char *dom_name = NULL;
190 const char **namelist = NULL;
191 enum wbcSidType *name_types = NULL;
192 struct wbcDomainSid dom_sid;
196 memcpy(&dom_sid, domain_sid, sizeof(struct wbcDomainSid));
198 ret = wbcLookupRids(&dom_sid, num_rids, rids,
199 &dom_name, &namelist, &name_types);
200 if (ret != WBC_ERR_SUCCESS)
203 *domain_name = talloc_strdup(mem_ctx, dom_name);
204 *names = TALLOC_ARRAY(mem_ctx, const char*, num_rids);
205 *types = TALLOC_ARRAY(mem_ctx, enum lsa_SidType, num_rids);
207 for(i=0; i<num_rids; i++) {
208 (*names)[i] = talloc_strdup(names, namelist[i]);
209 (*types)[i] = (enum lsa_SidType)name_types[i];
211 free(CONST_DISCARD(char*, namelist[i]));
219 /* Ask Winbind to allocate a new uid for us */
221 bool winbind_allocate_uid(uid_t *uid)
225 ret = wbcAllocateUid(uid);
227 return (ret == WBC_ERR_SUCCESS);
230 /* Ask Winbind to allocate a new gid for us */
232 bool winbind_allocate_gid(gid_t *gid)
236 ret = wbcAllocateGid(gid);
238 return (ret == WBC_ERR_SUCCESS);
241 #else /* WITH_WINBIND */
243 bool winbind_lookup_name(const char *dom_name, const char *name, DOM_SID *sid,
244 enum lsa_SidType *name_type)
249 /* Call winbindd to convert sid to name */
251 bool winbind_lookup_sid(TALLOC_CTX *mem_ctx, const DOM_SID *sid,
252 const char **domain, const char **name,
253 enum lsa_SidType *name_type)
258 /* Ping winbindd to see it is alive */
260 bool winbind_ping(void)
265 /* Call winbindd to convert SID to uid */
267 bool winbind_sid_to_uid(uid_t *puid, const DOM_SID *sid)
272 /* Call winbindd to convert uid to sid */
274 bool winbind_uid_to_sid(DOM_SID *sid, uid_t uid)
279 /* Call winbindd to convert SID to gid */
281 bool winbind_sid_to_gid(gid_t *pgid, const DOM_SID *sid)
286 /* Call winbindd to convert gid to sid */
288 bool winbind_gid_to_sid(DOM_SID *sid, gid_t gid)
293 /* Check for a trusted domain */
295 wbcErr wb_is_trusted_domain(const char *domain)
297 return WBC_ERR_UNKNOWN_FAILURE;
300 /* Lookup a set of rids in a given domain */
302 bool winbind_lookup_rids(TALLOC_CTX *mem_ctx,
303 const DOM_SID *domain_sid,
304 int num_rids, uint32 *rids,
305 const char **domain_name,
306 const char ***names, enum lsa_SidType **types)
311 /* Ask Winbind to allocate a new uid for us */
313 bool winbind_allocate_uid(uid_t *uid)
318 /* Ask Winbind to allocate a new gid for us */
320 bool winbind_allocate_gid(gid_t *gid)
325 #endif /* WITH_WINBIND */