a06091d6a29404669f6355361a4f1df9a213ca78
[vlendec/samba-autobuild/.git] / source3 / nsswitch / winbindd_util.c
1 /* 
2    Unix SMB/Netbios implementation.
3
4    Winbind daemon for ntdom nss module
5
6    Copyright (C) Tim Potter 2000-2001
7    Copyright (C) 2001 by Martin Pool <mbp@samba.org>
8    
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 2 of the License, or
12    (at your option) any later version.
13    
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18    
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23
24 #include "winbindd.h"
25 #include "sids.h"
26
27 /**
28  * @file winbindd_util.c
29  *
30  * Winbind daemon for NT domain authentication nss module.
31  **/
32
33
34 /**
35  * Used to clobber name fields that have an undefined value.
36  *
37  * Correct code should never look at a field that has this value.
38  **/
39
40 static const fstring name_deadbeef = "<deadbeef>";
41
42 /* The list of trusted domains.  Note that the list can be deleted and
43    recreated using the init_domain_list() function so pointers to
44    individual winbindd_domain structures cannot be made.  Keep a copy of
45    the domain name instead. */
46
47 static struct winbindd_domain *_domain_list;
48
49 struct winbindd_domain *domain_list(void)
50 {
51         /* Initialise list */
52
53         if (!_domain_list)
54                 init_domain_list();
55
56         return _domain_list;
57 }
58
59 /* Free all entries in the trusted domain list */
60
61 void free_domain_list(void)
62 {
63         struct winbindd_domain *domain = _domain_list;
64
65         while(domain) {
66                 struct winbindd_domain *next = domain->next;
67                 
68                 DLIST_REMOVE(_domain_list, domain);
69                 SAFE_FREE(domain);
70                 domain = next;
71         }
72 }
73
74 /* Add a trusted domain to our list of domains */
75
76 static struct winbindd_domain *add_trusted_domain(char *domain_name,
77                                                   struct winbindd_methods *methods)
78 {
79         struct winbindd_domain *domain;
80         
81         /* We can't call domain_list() as this function is called from
82            init_domain_list() and we'll get stuck in a loop. */
83
84         for (domain = _domain_list; domain; domain = domain->next) {
85                 if (strcmp(domain_name, domain->name) == 0) {
86                         DEBUG(3, ("domain %s already in domain list\n", 
87                                   domain_name));
88                         return domain;
89                 }
90         }
91         
92         /* Create new domain entry */
93
94         if ((domain = (struct winbindd_domain *)
95              malloc(sizeof(*domain))) == NULL)
96                 return NULL;
97
98         /* Fill in fields */
99         
100         ZERO_STRUCTP(domain);
101
102         fstrcpy(domain->name, domain_name);
103         domain->methods = methods;
104         domain->sequence_number = DOM_SEQUENCE_NONE;
105         domain->last_seq_check = 0;
106
107         /* Link to domain list */
108         
109         DLIST_ADD(_domain_list, domain);
110         
111         return domain;
112 }
113
114 /* Look up global info for the winbind daemon */
115
116 BOOL init_domain_list(void)
117 {
118         NTSTATUS result;
119         TALLOC_CTX *mem_ctx;
120         extern struct winbindd_methods cache_methods;
121         struct winbindd_domain *domain;
122         DOM_SID *dom_sids;
123         char **names;
124         int num_domains = 0;
125
126         if (!(mem_ctx = talloc_init_named("init_domain_list")))
127                 return False;
128
129         /* Free existing list */
130
131         free_domain_list();
132
133         /* Add ourselves as the first entry */
134
135         domain = add_trusted_domain(lp_workgroup(), &cache_methods);
136
137         /* Now we *must* get the domain sid for our primary domain. Go into
138            a holding pattern until that is available */
139
140         result = cache_methods.domain_sid(domain, &domain->sid);
141         while (!NT_STATUS_IS_OK(result)) {
142                 sleep(10);
143                 DEBUG(1,("Retrying startup domain sid fetch for %s\n",
144                          domain->name));
145                 result = cache_methods.domain_sid(domain, &domain->sid);
146         }
147        
148         DEBUG(1,("Added domain %s (%s)\n", 
149                  domain->name, 
150                  sid_string_static(&domain->sid)));
151
152         DEBUG(1, ("getting trusted domain list\n"));
153
154         result = cache_methods.trusted_domains(domain, mem_ctx, &num_domains,
155                                                &names, &dom_sids);
156
157         /* Add each domain to the trusted domain list */
158         if (NT_STATUS_IS_OK(result)) {
159                 int i;
160                 for(i = 0; i < num_domains; i++) {
161                         domain = add_trusted_domain(names[i], &cache_methods);
162                         if (domain) {
163                                 sid_copy(&domain->sid, &dom_sids[i]);
164                         }
165                         DEBUG(1,("Added domain %s (%s)\n", 
166                                  domain->name, 
167                                  sid_string_static(&domain->sid)));
168
169                         /* this primes the connection */
170                         cache_methods.domain_sid(domain, &domain->sid);
171                 }
172         }
173
174         talloc_destroy(mem_ctx);
175         return True;
176 }
177
178 /* Given a domain name, return the struct winbindd domain info for it 
179    if it is actually working. */
180
181 struct winbindd_domain *find_domain_from_name(char *domain_name)
182 {
183         struct winbindd_domain *domain;
184
185         /* Search through list */
186
187         for (domain = domain_list(); domain != NULL; domain = domain->next) {
188                 if (strequal(domain_name, domain->name) ||
189                     strequal(domain_name, domain->full_name))
190                         return domain;
191         }
192
193         /* Not found */
194
195         return NULL;
196 }
197
198 /* Given a domain sid, return the struct winbindd domain info for it */
199
200 struct winbindd_domain *find_domain_from_sid(DOM_SID *sid)
201 {
202         struct winbindd_domain *domain;
203
204         /* Search through list */
205
206         for (domain = domain_list(); domain != NULL; domain = domain->next) {
207                 if (sid_compare_domain(sid, &domain->sid) == 0)
208                         return domain;
209         }
210
211         /* Not found */
212
213         return NULL;
214 }
215
216 /* Lookup a sid in a domain from a name */
217
218 BOOL winbindd_lookup_sid_by_name(struct winbindd_domain *domain, 
219                                  const char *name, DOM_SID *sid, 
220                                  enum SID_NAME_USE *type)
221 {
222         NTSTATUS result;
223         
224         /* Don't bother with machine accounts */
225         
226         if (name[strlen(name) - 1] == '$')
227                 return False;
228
229         /* Lookup name */
230         result = domain->methods->name_to_sid(domain, name, sid, type);
231         
232         /* Return rid and type if lookup successful */
233         if (!NT_STATUS_IS_OK(result)) {
234                 *type = SID_NAME_UNKNOWN;
235         }
236
237         return NT_STATUS_IS_OK(result);
238 }
239
240 /**
241  * @brief Lookup a name in a domain from a sid.
242  *
243  * @param sid Security ID you want to look up.
244  *
245  * @param name On success, set to the name corresponding to @p sid.
246  * 
247  * @param dom_name On success, set to the 'domain name' corresponding to @p sid.
248  * 
249  * @param type On success, contains the type of name: alias, group or
250  * user.
251  *
252  * @retval True if the name exists, in which case @p name and @p type
253  * are set, otherwise False.
254  **/
255 BOOL winbindd_lookup_name_by_sid(DOM_SID *sid,
256                                  fstring dom_name,
257                                  fstring name,
258                                  enum SID_NAME_USE *type)
259 {
260         char *names;
261         NTSTATUS result;
262         TALLOC_CTX *mem_ctx;
263         BOOL rv = False;
264         struct winbindd_domain *domain;
265
266         domain = find_domain_from_sid(sid);
267
268         if (!domain) {
269                 DEBUG(1,("Can't find domain from sid\n"));
270                 return False;
271         }
272
273         /* Lookup name */
274
275         if (!(mem_ctx = talloc_init_named("winbindd_lookup_name_by_sid")))
276                 return False;
277         
278         result = domain->methods->sid_to_name(domain, mem_ctx, sid, &names, type);
279
280         /* Return name and type if successful */
281         
282         if ((rv = NT_STATUS_IS_OK(result))) {
283                 fstrcpy(dom_name, domain->name);
284                 fstrcpy(name, names);
285         } else {
286                 *type = SID_NAME_UNKNOWN;
287                 fstrcpy(name, name_deadbeef);
288         }
289         
290         talloc_destroy(mem_ctx);
291
292         return rv;
293 }
294
295
296 /* Free state information held for {set,get,end}{pw,gr}ent() functions */
297
298 void free_getent_state(struct getent_state *state)
299 {
300         struct getent_state *temp;
301
302         /* Iterate over state list */
303
304         temp = state;
305
306         while(temp != NULL) {
307                 struct getent_state *next;
308
309                 /* Free sam entries then list entry */
310
311                 SAFE_FREE(state->sam_entries);
312                 DLIST_REMOVE(state, state);
313                 next = temp->next;
314
315                 SAFE_FREE(temp);
316                 temp = next;
317         }
318 }
319
320 /* Initialise trusted domain info */
321
322 BOOL winbindd_param_init(void)
323 {
324         /* Parse winbind uid and winbind_gid parameters */
325
326         if (!lp_winbind_uid(&server_state.uid_low, &server_state.uid_high)) {
327                 DEBUG(0, ("winbind uid range missing or invalid\n"));
328                 return False;
329         }
330         
331         if (!lp_winbind_gid(&server_state.gid_low, &server_state.gid_high)) {
332                 DEBUG(0, ("winbind gid range missing or invalid\n"));
333                 return False;
334         }
335         
336         return True;
337 }
338
339 /* Check if a domain is present in a comma-separated list of domains */
340
341 BOOL check_domain_env(char *domain_env, char *domain)
342 {
343         fstring name;
344         char *tmp = domain_env;
345
346         while(next_token(&tmp, name, ",", sizeof(fstring))) {
347                 if (strequal(name, domain))
348                         return True;
349         }
350
351         return False;
352 }
353
354 /* Parse a string of the form DOMAIN/user into a domain and a user */
355 extern fstring global_myworkgroup;
356
357 BOOL parse_domain_user(const char *domuser, fstring domain, fstring user)
358 {
359         char *p = strchr(domuser,*lp_winbind_separator());
360
361         if (!(p || lp_winbind_use_default_domain()))
362                 return False;
363         
364         if(!p && lp_winbind_use_default_domain()) {
365                 fstrcpy(user, domuser);
366                 fstrcpy(domain, global_myworkgroup);
367         } else {
368                 fstrcpy(user, p+1);
369                 fstrcpy(domain, domuser);
370                 domain[PTR_DIFF(p, domuser)] = 0;
371         }
372         strupper(domain);
373         return True;
374 }
375
376 /*
377     Fill DOMAIN\\USERNAME entry accounting 'winbind use default domain' and
378     'winbind separator' options.
379     This means:
380         - omit DOMAIN when 'winbind use default domain = true' and DOMAIN is
381         global_myworkgroup
382          
383 */
384 void fill_domain_username(fstring name, const char *domain, const char *user)
385 {
386         if(lp_winbind_use_default_domain() &&
387             !strcmp(global_myworkgroup, domain)) {
388                 strlcpy(name, user, sizeof(fstring));
389         } else {
390                 slprintf(name, sizeof(fstring) - 1, "%s%s%s",
391                          domain, lp_winbind_separator(),
392                          user);
393         }
394 }