s3:dom_sid Global replace of DOM_SID with struct dom_sid
[nivanova/samba-autobuild/.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 struct passwd * winbind_getpwnam(const char * name)
28 {
29         wbcErr result;
30         struct passwd * tmp_pwd = NULL;
31         struct passwd * pwd = NULL;
32
33         result = wbcGetpwnam(name, &tmp_pwd);
34         if (result != WBC_ERR_SUCCESS)
35                 return pwd;
36
37         pwd = tcopy_passwd(talloc_tos(), tmp_pwd);
38
39         wbcFreeMemory(tmp_pwd);
40
41         return pwd;
42 }
43
44 struct passwd * winbind_getpwsid(const struct dom_sid *sid)
45 {
46         wbcErr result;
47         struct passwd * tmp_pwd = NULL;
48         struct passwd * pwd = NULL;
49         struct wbcDomainSid dom_sid;
50
51         memcpy(&dom_sid, sid, sizeof(dom_sid));
52
53         result = wbcGetpwsid(&dom_sid, &tmp_pwd);
54         if (result != WBC_ERR_SUCCESS)
55                 return pwd;
56
57         pwd = tcopy_passwd(talloc_tos(), tmp_pwd);
58
59         wbcFreeMemory(tmp_pwd);
60
61         return pwd;
62 }
63
64 /* Call winbindd to convert a name to a sid */
65
66 bool winbind_lookup_name(const char *dom_name, const char *name, struct dom_sid *sid,
67                          enum lsa_SidType *name_type)
68 {
69         struct wbcDomainSid dom_sid;
70         wbcErr result;
71         enum wbcSidType type;   
72
73         result = wbcLookupName(dom_name, name, &dom_sid, &type);
74         if (result != WBC_ERR_SUCCESS)
75                 return false;
76
77         memcpy(sid, &dom_sid, sizeof(struct dom_sid));
78         *name_type = (enum lsa_SidType)type;    
79
80         return true;    
81 }
82
83 /* Call winbindd to convert sid to name */
84
85 bool winbind_lookup_sid(TALLOC_CTX *mem_ctx, const struct dom_sid *sid,
86                         const char **domain, const char **name,
87                         enum lsa_SidType *name_type)
88 {
89         struct wbcDomainSid dom_sid;
90         wbcErr result;
91         enum wbcSidType type;
92         char *domain_name = NULL;
93         char *account_name = NULL;
94
95         memcpy(&dom_sid, sid, sizeof(dom_sid)); 
96
97         result = wbcLookupSid(&dom_sid, &domain_name, &account_name, &type);
98         if (result != WBC_ERR_SUCCESS)
99                 return false;
100
101         /* Copy out result */
102
103         if (domain) {           
104                 *domain = talloc_strdup(mem_ctx, domain_name);
105         }
106         if (name) {
107                 *name = talloc_strdup(mem_ctx, account_name);
108         }
109         *name_type = (enum lsa_SidType)type;
110
111         DEBUG(10, ("winbind_lookup_sid: SUCCESS: SID %s -> %s %s\n", 
112                    sid_string_dbg(sid), domain_name, account_name));
113
114         wbcFreeMemory(domain_name);
115         wbcFreeMemory(account_name);
116
117         if ((domain && !*domain) || (name && !*name)) {         
118                 DEBUG(0,("winbind_lookup_sid: talloc() failed!\n"));
119                 return false;
120         }       
121
122
123         return true;
124 }
125
126 /* Ping winbindd to see it is alive */
127
128 bool winbind_ping(void)
129 {
130         wbcErr result = wbcPing();
131
132         return (result == WBC_ERR_SUCCESS);
133 }
134
135 /* Call winbindd to convert SID to uid */
136
137 bool winbind_sid_to_uid(uid_t *puid, const struct dom_sid *sid)
138 {
139         struct wbcDomainSid dom_sid;
140         wbcErr result;
141
142         memcpy(&dom_sid, sid, sizeof(dom_sid)); 
143
144         result = wbcSidToUid(&dom_sid, puid);   
145
146         return (result == WBC_ERR_SUCCESS);     
147 }
148
149 /* Call winbindd to convert uid to sid */
150
151 bool winbind_uid_to_sid(struct dom_sid *sid, uid_t uid)
152 {
153         struct wbcDomainSid dom_sid;
154         wbcErr result;
155
156         result = wbcUidToSid(uid, &dom_sid);
157         if (result == WBC_ERR_SUCCESS) {
158                 memcpy(sid, &dom_sid, sizeof(struct dom_sid));
159         } else {
160                 sid_copy(sid, &global_sid_NULL);
161         }
162
163         return (result == WBC_ERR_SUCCESS);
164 }
165
166 /* Call winbindd to convert SID to gid */
167
168 bool winbind_sid_to_gid(gid_t *pgid, const struct dom_sid *sid)
169 {
170         struct wbcDomainSid dom_sid;
171         wbcErr result;
172
173         memcpy(&dom_sid, sid, sizeof(dom_sid)); 
174
175         result = wbcSidToGid(&dom_sid, pgid);   
176
177         return (result == WBC_ERR_SUCCESS);     
178 }
179
180 /* Call winbindd to convert gid to sid */
181
182 bool winbind_gid_to_sid(struct dom_sid *sid, gid_t gid)
183 {
184         struct wbcDomainSid dom_sid;
185         wbcErr result;
186
187         result = wbcGidToSid(gid, &dom_sid);
188         if (result == WBC_ERR_SUCCESS) {
189                 memcpy(sid, &dom_sid, sizeof(struct dom_sid));
190         } else {
191                 sid_copy(sid, &global_sid_NULL);
192         }
193
194         return (result == WBC_ERR_SUCCESS);
195 }
196
197 /* Check for a trusted domain */
198
199 wbcErr wb_is_trusted_domain(const char *domain)
200 {
201         wbcErr result;
202         struct wbcDomainInfo *info = NULL;
203
204         result = wbcDomainInfo(domain, &info);
205
206         if (WBC_ERROR_IS_OK(result)) {
207                 wbcFreeMemory(info);
208         }
209
210         return result;  
211 }
212
213 /* Lookup a set of rids in a given domain */
214
215 bool winbind_lookup_rids(TALLOC_CTX *mem_ctx,
216                          const struct dom_sid *domain_sid,
217                          int num_rids, uint32 *rids,
218                          const char **domain_name,
219                          const char ***names, enum lsa_SidType **types)
220 {
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;  
227
228         memcpy(&dom_sid, domain_sid, sizeof(struct wbcDomainSid));
229
230         ret = wbcLookupRids(&dom_sid, num_rids, rids,
231                             &dom_name, &namelist, &name_types);
232         if (ret != WBC_ERR_SUCCESS) {           
233                 return false;
234         }       
235
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);
239
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];
243         }
244
245         wbcFreeMemory(CONST_DISCARD(char*, dom_name));
246         wbcFreeMemory(namelist);
247         wbcFreeMemory(name_types);
248
249         return true;    
250 }
251
252 /* Ask Winbind to allocate a new uid for us */
253
254 bool winbind_allocate_uid(uid_t *uid)
255 {
256         wbcErr ret;
257
258         ret = wbcAllocateUid(uid);
259
260         return (ret == WBC_ERR_SUCCESS);
261 }
262
263 /* Ask Winbind to allocate a new gid for us */
264
265 bool winbind_allocate_gid(gid_t *gid)
266 {
267         wbcErr ret;
268
269         ret = wbcAllocateGid(gid);
270
271         return (ret == WBC_ERR_SUCCESS);
272 }
273
274 bool winbind_get_groups(TALLOC_CTX * mem_ctx, const char *account, uint32_t *num_groups, gid_t **_groups)
275 {
276         wbcErr ret;
277         uint32_t ngroups;
278         gid_t *group_list = NULL;
279
280         ret = wbcGetGroups(account, &ngroups, &group_list);
281         if (ret != WBC_ERR_SUCCESS)
282                 return false;
283
284         *_groups = TALLOC_ARRAY(mem_ctx, gid_t, ngroups);
285         if (*_groups == NULL) {
286             wbcFreeMemory(group_list);
287             return false;
288         }
289
290         memcpy(*_groups, group_list, ngroups* sizeof(gid_t));
291         *num_groups = ngroups;
292
293         wbcFreeMemory(group_list);
294         return true;
295 }
296
297 bool winbind_get_sid_aliases(TALLOC_CTX *mem_ctx,
298                              const struct dom_sid *dom_sid,
299                              const struct dom_sid *members,
300                              size_t num_members,
301                              uint32_t **pp_alias_rids,
302                              size_t *p_num_alias_rids)
303 {
304         wbcErr ret;
305         struct wbcDomainSid domain_sid;
306         struct wbcDomainSid *sid_list = NULL;
307         size_t i;
308         uint32_t * rids;
309         uint32_t num_rids;
310
311         memcpy(&domain_sid, dom_sid, sizeof(*dom_sid));
312
313         sid_list = TALLOC_ARRAY(mem_ctx, struct wbcDomainSid, num_members);
314
315         for (i=0; i < num_members; i++) {
316             memcpy(&sid_list[i], &members[i], sizeof(sid_list[i]));
317         }
318
319         ret = wbcGetSidAliases(&domain_sid,
320                                sid_list,
321                                num_members,
322                                &rids,
323                                &num_rids);
324         if (ret != WBC_ERR_SUCCESS) {
325                 return false;
326         }
327
328         *pp_alias_rids = TALLOC_ARRAY(mem_ctx, uint32_t, num_rids);
329         if (*pp_alias_rids == NULL) {
330                 wbcFreeMemory(rids);
331                 return false;
332         }
333
334         memcpy(*pp_alias_rids, rids, sizeof(uint32_t) * num_rids);
335
336         *p_num_alias_rids = num_rids;
337         wbcFreeMemory(rids);
338
339         return true;
340 }
341
342 #else      /* WITH_WINBIND */
343
344 struct passwd * winbind_getpwnam(const char * name)
345 {
346         return NULL;
347 }
348
349 struct passwd * winbind_getpwsid(const struct dom_sid *sid)
350 {
351         return NULL;
352 }
353
354 bool winbind_lookup_name(const char *dom_name, const char *name, struct dom_sid *sid,
355                          enum lsa_SidType *name_type)
356 {
357         return false;
358 }
359
360 /* Call winbindd to convert sid to name */
361
362 bool winbind_lookup_sid(TALLOC_CTX *mem_ctx, const struct dom_sid *sid,
363                         const char **domain, const char **name,
364                         enum lsa_SidType *name_type)
365 {
366         return false;
367 }
368
369 /* Ping winbindd to see it is alive */
370
371 bool winbind_ping(void)
372 {
373         return false;
374 }
375
376 /* Call winbindd to convert SID to uid */
377
378 bool winbind_sid_to_uid(uid_t *puid, const struct dom_sid *sid)
379 {
380         return false;
381 }
382
383 /* Call winbindd to convert uid to sid */
384
385 bool winbind_uid_to_sid(struct dom_sid *sid, uid_t uid)
386 {
387         return false;
388 }
389
390 /* Call winbindd to convert SID to gid */
391
392 bool winbind_sid_to_gid(gid_t *pgid, const struct dom_sid *sid)
393 {
394         return false;   
395 }
396
397 /* Call winbindd to convert gid to sid */
398
399 bool winbind_gid_to_sid(struct dom_sid *sid, gid_t gid)
400 {
401         return false;
402 }
403
404 /* Check for a trusted domain */
405
406 wbcErr wb_is_trusted_domain(const char *domain)
407 {
408         return WBC_ERR_UNKNOWN_FAILURE;
409 }
410
411 /* Lookup a set of rids in a given domain */
412
413 bool winbind_lookup_rids(TALLOC_CTX *mem_ctx,
414                          const struct dom_sid *domain_sid,
415                          int num_rids, uint32 *rids,
416                          const char **domain_name,
417                          const char ***names, enum lsa_SidType **types)
418 {
419         return false;
420 }
421
422 /* Ask Winbind to allocate a new uid for us */
423
424 bool winbind_allocate_uid(uid_t *uid)
425 {
426         return false;
427 }
428
429 /* Ask Winbind to allocate a new gid for us */
430
431 bool winbind_allocate_gid(gid_t *gid)
432 {
433         return false;
434 }
435
436 bool winbind_get_groups(TALLOC_CTX *mem_ctx, const char *account, uint32_t *num_groups, gid_t **_groups)
437 {
438         return false;
439 }
440
441 bool winbind_get_sid_aliases(TALLOC_CTX *mem_ctx,
442                              const struct dom_sid *dom_sid,
443                              const struct dom_sid *members,
444                              size_t num_members,
445                              uint32_t **pp_alias_rids,
446                              size_t *p_num_alias_rids)
447 {
448         return false;
449 }
450
451 #endif     /* WITH_WINBIND */