3cf068a6e0da11d812a89c4f3954ce066f960e90
[ira/wip.git] / source3 / lib / winbind_util.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Winbind Utility functions
4
5    Copyright (C) Gerald (Jerry) Carter   2007
6    
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.
11    
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.
16    
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/>.
19 */
20
21 #include "includes.h"
22
23 #if defined(WITH_WINBIND)
24
25 #include "nsswitch/libwbclient/wbclient.h"
26
27 /* Call winbindd to convert a name to a sid */
28
29 bool winbind_lookup_name(const char *dom_name, const char *name, DOM_SID *sid, 
30                          enum lsa_SidType *name_type)
31 {
32         struct wbcDomainSid dom_sid;
33         wbcErr result;
34         enum wbcSidType type;   
35
36         result = wbcLookupName(dom_name, name, &dom_sid, &type);
37         if (result != WBC_ERR_SUCCESS)
38                 return false;
39
40         memcpy(sid, &dom_sid, sizeof(DOM_SID)); 
41         *name_type = (enum lsa_SidType)type;    
42
43         return true;    
44 }
45
46 /* Call winbindd to convert sid to name */
47
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)
51 {
52         struct wbcDomainSid dom_sid;
53         wbcErr result;
54         enum wbcSidType type;
55         char *domain_name = NULL;
56         char *account_name = NULL;
57
58         memcpy(&dom_sid, sid, sizeof(dom_sid)); 
59
60         result = wbcLookupSid(&dom_sid, &domain_name, &account_name, &type);
61         if (result != WBC_ERR_SUCCESS)
62                 return false;
63
64         /* Copy out result */
65
66         if (domain) {           
67                 *domain = talloc_strdup(mem_ctx, domain_name);
68         }
69         if (name) {
70                 *name = talloc_strdup(mem_ctx, account_name);
71         }
72         *name_type = (enum lsa_SidType)type;
73
74         DEBUG(10, ("winbind_lookup_sid: SUCCESS: SID %s -> %s %s\n", 
75                    sid_string_dbg(sid), domain_name, account_name));
76
77         wbcFreeMemory(domain_name);
78         wbcFreeMemory(account_name);
79         
80         if ((domain && !*domain) || (name && !*name)) {         
81                 DEBUG(0,("winbind_lookup_sid: talloc() failed!\n"));
82                 return false;
83         }       
84
85
86         return true;
87 }
88
89 /* Ping winbindd to see it is alive */
90
91 bool winbind_ping(void)
92 {
93         wbcErr result = wbcPing();
94
95         return (result == WBC_ERR_SUCCESS);
96 }
97
98 /* Call winbindd to convert SID to uid */
99
100 bool winbind_sid_to_uid(uid_t *puid, const DOM_SID *sid)
101 {
102         struct wbcDomainSid dom_sid;
103         wbcErr result;
104
105         memcpy(&dom_sid, sid, sizeof(dom_sid)); 
106
107         result = wbcSidToUid(&dom_sid, puid);   
108
109         return (result == WBC_ERR_SUCCESS);     
110 }
111
112 /* Call winbindd to convert uid to sid */
113
114 bool winbind_uid_to_sid(DOM_SID *sid, uid_t uid)
115 {
116         struct wbcDomainSid dom_sid;
117         wbcErr result;
118
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);
124         }
125
126         return (result == WBC_ERR_SUCCESS);
127 }
128
129 /* Call winbindd to convert SID to gid */
130
131 bool winbind_sid_to_gid(gid_t *pgid, const DOM_SID *sid)
132 {
133         struct wbcDomainSid dom_sid;
134         wbcErr result;
135
136         memcpy(&dom_sid, sid, sizeof(dom_sid)); 
137
138         result = wbcSidToGid(&dom_sid, pgid);   
139
140         return (result == WBC_ERR_SUCCESS);     
141 }
142
143 /* Call winbindd to convert gid to sid */
144
145 bool winbind_gid_to_sid(DOM_SID *sid, gid_t gid)
146 {
147         struct wbcDomainSid dom_sid;
148         wbcErr result;
149
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);
155         }
156
157         return (result == WBC_ERR_SUCCESS);
158 }
159
160 /* Check for a trusted domain */
161
162 wbcErr wb_is_trusted_domain(const char *domain)
163 {
164         wbcErr result;
165         struct wbcDomainInfo *info = NULL;
166         
167         result = wbcDomainInfo(domain, &info);
168
169         if (WBC_ERROR_IS_OK(result)) {
170                 wbcFreeMemory(info);
171         }
172
173         return result;  
174 }
175
176 /* Lookup a set of rids in a given domain */
177
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)
183 {
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;  
190         
191         memcpy(&dom_sid, domain_sid, sizeof(struct wbcDomainSid));
192         
193         ret = wbcLookupRids(&dom_sid, num_rids, rids,
194                             &dom_name, &namelist, &name_types);
195         if (ret != WBC_ERR_SUCCESS) {           
196                 return false;
197         }       
198         
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);
202
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];
206         }
207
208         wbcFreeMemory(CONST_DISCARD(char*, dom_name));
209         wbcFreeMemory(namelist);
210         wbcFreeMemory(name_types);
211         
212         return true;    
213 }
214
215 /* Ask Winbind to allocate a new uid for us */
216
217 bool winbind_allocate_uid(uid_t *uid)
218 {
219         wbcErr ret;
220         
221         ret = wbcAllocateUid(uid);
222         
223         return (ret == WBC_ERR_SUCCESS);
224 }
225
226 /* Ask Winbind to allocate a new gid for us */
227
228 bool winbind_allocate_gid(gid_t *gid)
229 {
230         wbcErr ret;
231         
232         ret = wbcAllocateGid(gid);
233         
234         return (ret == WBC_ERR_SUCCESS);
235 }
236
237 #else      /* WITH_WINBIND */
238
239 bool winbind_lookup_name(const char *dom_name, const char *name, DOM_SID *sid, 
240                          enum lsa_SidType *name_type)
241 {
242         return false;
243 }
244
245 /* Call winbindd to convert sid to name */
246
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)
250 {
251         return false;
252 }
253
254 /* Ping winbindd to see it is alive */
255
256 bool winbind_ping(void)
257 {
258         return false;
259 }
260
261 /* Call winbindd to convert SID to uid */
262
263 bool winbind_sid_to_uid(uid_t *puid, const DOM_SID *sid)
264 {
265         return false;
266 }
267
268 /* Call winbindd to convert uid to sid */
269
270 bool winbind_uid_to_sid(DOM_SID *sid, uid_t uid)
271 {
272         return false;
273 }
274
275 /* Call winbindd to convert SID to gid */
276
277 bool winbind_sid_to_gid(gid_t *pgid, const DOM_SID *sid)
278 {
279         return false;   
280 }
281
282 /* Call winbindd to convert gid to sid */
283
284 bool winbind_gid_to_sid(DOM_SID *sid, gid_t gid)
285 {
286         return false;
287 }
288
289 /* Check for a trusted domain */
290
291 wbcErr wb_is_trusted_domain(const char *domain)
292 {
293         return WBC_ERR_UNKNOWN_FAILURE;
294 }
295
296 /* Lookup a set of rids in a given domain */
297
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)
303 {
304         return false;
305 }
306
307 /* Ask Winbind to allocate a new uid for us */
308
309 bool winbind_allocate_uid(uid_t *uid)
310 {
311         return false;
312 }
313
314 /* Ask Winbind to allocate a new gid for us */
315
316 bool winbind_allocate_gid(gid_t *gid)
317 {
318         return false;
319 }
320
321 #endif     /* WITH_WINBIND */