4983b9ced0d1df839770a0f4fcef343b181542bf
[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         SAFE_FREE(domain_name);
78         SAFE_FREE(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         if (result != WBC_ERR_SUCCESS)
109                 return False;
110
111         return (result == WBC_ERR_SUCCESS);     
112 }
113
114 /* Call winbindd to convert uid to sid */
115
116 bool winbind_uid_to_sid(DOM_SID *sid, uid_t uid)
117 {
118         struct wbcDomainSid dom_sid;
119         wbcErr result;
120
121         result = wbcUidToSid(uid, &dom_sid);
122         if (result == WBC_ERR_SUCCESS) {
123                 memcpy(sid, &dom_sid, sizeof(DOM_SID));
124         } else {
125                 sid_copy(sid, &global_sid_NULL);
126         }
127
128         return (result == WBC_ERR_SUCCESS);
129 }
130
131 /* Call winbindd to convert SID to gid */
132
133 bool winbind_sid_to_gid(gid_t *pgid, const DOM_SID *sid)
134 {
135         struct wbcDomainSid dom_sid;
136         wbcErr result;
137
138         memcpy(&dom_sid, sid, sizeof(dom_sid)); 
139
140         result = wbcSidToGid(&dom_sid, pgid);   
141         if (result != WBC_ERR_SUCCESS)
142                 return False;
143
144         return (result == WBC_ERR_SUCCESS);     
145 }
146
147 /* Call winbindd to convert gid to sid */
148
149 bool winbind_gid_to_sid(DOM_SID *sid, gid_t gid)
150 {
151         struct wbcDomainSid dom_sid;
152         wbcErr result;
153
154         result = wbcGidToSid(gid, &dom_sid);
155         if (result == WBC_ERR_SUCCESS) {
156                 memcpy(sid, &dom_sid, sizeof(DOM_SID));
157         } else {
158                 sid_copy(sid, &global_sid_NULL);
159         }
160
161         return (result == WBC_ERR_SUCCESS);
162 }
163
164 /* Check for a trusted domain */
165
166 wbcErr wb_is_trusted_domain(const char *domain)
167 {
168         wbcErr result;
169         struct wbcDomainInfo info;      
170         
171         result = wbcDomainInfo(domain, &info);
172
173         if (result == WBC_ERR_SUCCESS) {
174                 SAFE_FREE(info.short_name);
175                 SAFE_FREE(info.dns_name);
176         }
177
178         return result;  
179 }
180
181 /* Lookup a set of rids in a given domain */
182
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)
188 {
189         const char *dom_name = NULL;
190         const char **namelist = NULL;
191         enum wbcSidType *name_types = NULL;
192         struct wbcDomainSid dom_sid;
193         wbcErr ret;
194         int i;  
195         
196         memcpy(&dom_sid, domain_sid, sizeof(struct wbcDomainSid));
197         
198         ret = wbcLookupRids(&dom_sid, num_rids, rids,
199                             &dom_name, &namelist, &name_types);
200         if (ret != WBC_ERR_SUCCESS)
201                 return False;
202         
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);
206
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];
210
211                 free(CONST_DISCARD(char*, namelist[i]));                
212         }
213         free(namelist);
214         free(name_types);
215         
216         return True;    
217 }
218
219 /* Ask Winbind to allocate a new uid for us */
220
221 bool winbind_allocate_uid(uid_t *uid)
222 {
223         wbcErr ret;
224         
225         ret = wbcAllocateUid(uid);
226         
227         return (ret == WBC_ERR_SUCCESS);
228 }
229
230 /* Ask Winbind to allocate a new gid for us */
231
232 bool winbind_allocate_gid(gid_t *gid)
233 {
234         wbcErr ret;
235         
236         ret = wbcAllocateGid(gid);
237         
238         return (ret == WBC_ERR_SUCCESS);
239 }
240
241 #else      /* WITH_WINBIND */
242
243 bool winbind_lookup_name(const char *dom_name, const char *name, DOM_SID *sid, 
244                          enum lsa_SidType *name_type)
245 {
246         return False;
247 }
248
249 /* Call winbindd to convert sid to name */
250
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)
254 {
255         return False;
256 }
257
258 /* Ping winbindd to see it is alive */
259
260 bool winbind_ping(void)
261 {
262         return False;
263 }
264
265 /* Call winbindd to convert SID to uid */
266
267 bool winbind_sid_to_uid(uid_t *puid, const DOM_SID *sid)
268 {
269         return False;
270 }
271
272 /* Call winbindd to convert uid to sid */
273
274 bool winbind_uid_to_sid(DOM_SID *sid, uid_t uid)
275 {
276         return False;
277 }
278
279 /* Call winbindd to convert SID to gid */
280
281 bool winbind_sid_to_gid(gid_t *pgid, const DOM_SID *sid)
282 {
283         return False;   
284 }
285
286 /* Call winbindd to convert gid to sid */
287
288 bool winbind_gid_to_sid(DOM_SID *sid, gid_t gid)
289 {
290         return False;
291 }
292
293 /* Check for a trusted domain */
294
295 wbcErr wb_is_trusted_domain(const char *domain)
296 {
297         return WBC_ERR_UNKNOWN_FAILURE;
298 }
299
300 /* Lookup a set of rids in a given domain */
301
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)
307 {
308         return False;
309 }
310
311 /* Ask Winbind to allocate a new uid for us */
312
313 bool winbind_allocate_uid(uid_t *uid)
314 {
315         return False;
316 }
317
318 /* Ask Winbind to allocate a new gid for us */
319
320 bool winbind_allocate_gid(gid_t *gid)
321 {
322         return False;
323 }
324
325 #endif     /* WITH_WINBIND */