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 wbcFreeMemory(domain_name);
78 wbcFreeMemory(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);
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;
119 result = wbcUidToSid(uid, &dom_sid);
120 if (result == WBC_ERR_SUCCESS) {
121 memcpy(sid, &dom_sid, sizeof(DOM_SID));
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;
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;
150 result = wbcGidToSid(gid, &dom_sid);
151 if (result == WBC_ERR_SUCCESS) {
152 memcpy(sid, &dom_sid, sizeof(DOM_SID));
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)
165 struct wbcDomainInfo *info = NULL;
167 result = wbcDomainInfo(domain, &info);
169 if (WBC_ERROR_IS_OK(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;
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) {
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);
215 /* Ask Winbind to allocate a new uid for us */
217 bool winbind_allocate_uid(uid_t *uid)
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)
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)
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)
254 /* Ping winbindd to see it is alive */
256 bool winbind_ping(void)
261 /* Call winbindd to convert SID to uid */
263 bool winbind_sid_to_uid(uid_t *puid, const DOM_SID *sid)
268 /* Call winbindd to convert uid to sid */
270 bool winbind_uid_to_sid(DOM_SID *sid, uid_t uid)
275 /* Call winbindd to convert SID to gid */
277 bool winbind_sid_to_gid(gid_t *pgid, const DOM_SID *sid)
282 /* Call winbindd to convert gid to sid */
284 bool winbind_gid_to_sid(DOM_SID *sid, gid_t gid)
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)
307 /* Ask Winbind to allocate a new uid for us */
309 bool winbind_allocate_uid(uid_t *uid)
314 /* Ask Winbind to allocate a new gid for us */
316 bool winbind_allocate_gid(gid_t *gid)
321 #endif /* WITH_WINBIND */