Forward port the change to talloc_init() to make all talloc contexts
[vlendec/samba-autobuild/.git] / source3 / nsswitch / winbindd_util.c
1 /* 
2    Unix SMB/CIFS 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
26 #undef DBGC_CLASS
27 #define DBGC_CLASS DBGC_WINBIND
28
29 /**
30  * @file winbindd_util.c
31  *
32  * Winbind daemon for NT domain authentication nss module.
33  **/
34
35
36 /**
37  * Used to clobber name fields that have an undefined value.
38  *
39  * Correct code should never look at a field that has this value.
40  **/
41
42 static const fstring name_deadbeef = "<deadbeef>";
43
44 /* The list of trusted domains.  Note that the list can be deleted and
45    recreated using the init_domain_list() function so pointers to
46    individual winbindd_domain structures cannot be made.  Keep a copy of
47    the domain name instead. */
48
49 static struct winbindd_domain *_domain_list;
50
51 struct winbindd_domain *domain_list(void)
52 {
53         /* Initialise list */
54
55         if (!_domain_list)
56                 init_domain_list();
57
58         return _domain_list;
59 }
60
61 /* Free all entries in the trusted domain list */
62
63 void free_domain_list(void)
64 {
65         struct winbindd_domain *domain = _domain_list;
66
67         while(domain) {
68                 struct winbindd_domain *next = domain->next;
69                 
70                 DLIST_REMOVE(_domain_list, domain);
71                 SAFE_FREE(domain);
72                 domain = next;
73         }
74 }
75
76
77 /* Add a trusted domain to our list of domains */
78 static struct winbindd_domain *add_trusted_domain(const char *domain_name, const char *alt_name,
79                                                   struct winbindd_methods *methods,
80                                                   DOM_SID *sid)
81 {
82         struct winbindd_domain *domain;
83         
84         /* We can't call domain_list() as this function is called from
85            init_domain_list() and we'll get stuck in a loop. */
86         for (domain = _domain_list; domain; domain = domain->next) {
87                 if (strcasecmp(domain_name, domain->name) == 0 ||
88                     strcasecmp(domain_name, domain->alt_name) == 0) {
89                         return domain;
90                 }
91                 if (alt_name && *alt_name) {
92                         if (strcasecmp(alt_name, domain->name) == 0 ||
93                             strcasecmp(alt_name, domain->alt_name) == 0) {
94                                 return domain;
95                         }
96                 }
97         }
98         
99         /* Create new domain entry */
100
101         if ((domain = (struct winbindd_domain *)
102              malloc(sizeof(*domain))) == NULL)
103                 return NULL;
104
105         /* Fill in fields */
106         
107         ZERO_STRUCTP(domain);
108
109         /* prioritise the short name */
110         if (strchr_m(domain_name, '.') && alt_name && *alt_name) {
111                 fstrcpy(domain->name, alt_name);
112                 fstrcpy(domain->alt_name, domain_name);
113         } else {
114         fstrcpy(domain->name, domain_name);
115                 if (alt_name) {
116                         fstrcpy(domain->alt_name, alt_name);
117                 }
118         }
119
120         domain->methods = methods;
121         domain->sequence_number = DOM_SEQUENCE_NONE;
122         domain->last_seq_check = 0;
123         if (sid) {
124                 sid_copy(&domain->sid, sid);
125         }
126         
127         /* see if this is a native mode win2k domain, but only for our own domain */
128            
129         if ( strequal( lp_workgroup(), domain_name) )   {
130                 domain->native_mode = cm_check_for_native_mode_win2k( domain_name );
131                 DEBUG(3,("add_trusted_domain: %s is a %s mode domain\n", domain_name,
132                                         domain->native_mode ? "native" : "mixed" ));
133         }       
134
135         /* Link to domain list */
136         DLIST_ADD(_domain_list, domain);
137         
138         DEBUG(1,("Added domain %s %s %s\n", 
139                  domain->name, domain->alt_name,
140                  sid?sid_string_static(&domain->sid):""));
141         
142         return domain;
143 }
144
145
146 /*
147   rescan our domains looking for new trusted domains
148  */
149 void rescan_trusted_domains(void)
150 {
151         struct winbindd_domain *domain;
152         TALLOC_CTX *mem_ctx;
153         static time_t last_scan;
154         time_t t = time(NULL);
155
156         /* trusted domains might be disabled */
157         if (!lp_allow_trusted_domains()) {
158                 return;
159         }
160         
161         /* ony rescan every few minutes */
162         if ((unsigned)(t - last_scan) < WINBINDD_RESCAN_FREQ) {
163                 return;
164         }
165         last_scan = t;
166
167         DEBUG(1, ("scanning trusted domain list\n"));
168
169         if (!(mem_ctx = talloc_init("init_domain_list")))
170                 return;
171
172         for (domain = _domain_list; domain; domain = domain->next) {
173                 NTSTATUS result;
174                 char **names;
175                 char **alt_names;
176                 int num_domains = 0;
177                 DOM_SID *dom_sids;
178                 int i;
179
180                 result = domain->methods->trusted_domains(domain, mem_ctx, &num_domains,
181                                                           &names, &alt_names, &dom_sids);
182                 if (!NT_STATUS_IS_OK(result)) {
183                         continue;
184                 }
185
186                 /* Add each domain to the trusted domain list. Each domain inherits
187                    the access methods of its parent */
188                 for(i = 0; i < num_domains; i++) {
189                         DEBUG(10,("Found domain %s\n", names[i]));
190                         add_trusted_domain(names[i], 
191                                            alt_names?alt_names[i]:NULL, 
192                                            domain->methods, &dom_sids[i]);
193                 }
194         }
195
196         talloc_destroy(mem_ctx);
197 }
198
199 /* Look up global info for the winbind daemon */
200 BOOL init_domain_list(void)
201 {
202         NTSTATUS result;
203         extern struct winbindd_methods cache_methods;
204         struct winbindd_domain *domain;
205
206         /* Free existing list */
207         free_domain_list();
208
209         /* Add ourselves as the first entry */
210         domain = add_trusted_domain(lp_workgroup(), NULL, &cache_methods, NULL);
211
212         /* 
213          * Now we *must* get the domain sid for our primary domain. Go into
214          * a holding pattern until that is available
215          */
216
217         result = cache_methods.domain_sid(domain, &domain->sid);
218         while (!NT_STATUS_IS_OK(result)) {
219
220                 sleep(10);
221                 DEBUG(1,("Retrying startup domain sid fetch for %s\n",
222                          domain->name));
223                 result = cache_methods.domain_sid(domain, &domain->sid);
224
225                 /* If we don't call lp_talloc_free() here we end up 
226                    accumulating memory in the "global" lp_talloc in
227                    param/loadparm.c */
228
229                 lp_talloc_free();
230         }
231        
232         /* get any alternate name for the primary domain */
233         cache_methods.alternate_name(domain);
234
235         /* do an initial scan for trusted domains */
236         rescan_trusted_domains();
237
238         return True;
239 }
240
241 /* Given a domain name, return the struct winbindd domain info for it 
242    if it is actually working. */
243
244 struct winbindd_domain *find_domain_from_name(const char *domain_name)
245 {
246         struct winbindd_domain *domain;
247
248         /* Search through list */
249
250         for (domain = domain_list(); domain != NULL; domain = domain->next) {
251                 if (strequal(domain_name, domain->name) ||
252                     (domain->alt_name[0] && strequal(domain_name, domain->alt_name)))
253                         return domain;
254         }
255
256         /* Not found */
257
258         return NULL;
259 }
260
261 /* Given a domain sid, return the struct winbindd domain info for it */
262
263 struct winbindd_domain *find_domain_from_sid(DOM_SID *sid)
264 {
265         struct winbindd_domain *domain;
266
267         /* Search through list */
268
269         for (domain = domain_list(); domain != NULL; domain = domain->next) {
270                 if (sid_compare_domain(sid, &domain->sid) == 0)
271                         return domain;
272         }
273
274         /* Not found */
275
276         return NULL;
277 }
278
279 /* Lookup a sid in a domain from a name */
280
281 BOOL winbindd_lookup_sid_by_name(struct winbindd_domain *domain, 
282                                  const char *name, DOM_SID *sid, 
283                                  enum SID_NAME_USE *type)
284 {
285         NTSTATUS result;
286         
287         /* Don't bother with machine accounts */
288         
289         if (name[strlen(name) - 1] == '$')
290                 return False;
291
292         /* Lookup name */
293         result = domain->methods->name_to_sid(domain, name, sid, type);
294         
295         /* Return rid and type if lookup successful */
296         if (!NT_STATUS_IS_OK(result)) {
297                 *type = SID_NAME_UNKNOWN;
298         }
299
300         return NT_STATUS_IS_OK(result);
301 }
302
303 /**
304  * @brief Lookup a name in a domain from a sid.
305  *
306  * @param sid Security ID you want to look up.
307  *
308  * @param name On success, set to the name corresponding to @p sid.
309  * 
310  * @param dom_name On success, set to the 'domain name' corresponding to @p sid.
311  * 
312  * @param type On success, contains the type of name: alias, group or
313  * user.
314  *
315  * @retval True if the name exists, in which case @p name and @p type
316  * are set, otherwise False.
317  **/
318 BOOL winbindd_lookup_name_by_sid(DOM_SID *sid,
319                                  fstring dom_name,
320                                  fstring name,
321                                  enum SID_NAME_USE *type)
322 {
323         char *names;
324         NTSTATUS result;
325         TALLOC_CTX *mem_ctx;
326         BOOL rv = False;
327         struct winbindd_domain *domain;
328
329         domain = find_domain_from_sid(sid);
330
331         if (!domain) {
332                 DEBUG(1,("Can't find domain from sid\n"));
333                 return False;
334         }
335
336         /* Lookup name */
337
338         if (!(mem_ctx = talloc_init("winbindd_lookup_name_by_sid")))
339                 return False;
340         
341         result = domain->methods->sid_to_name(domain, mem_ctx, sid, &names, type);
342
343         /* Return name and type if successful */
344         
345         if ((rv = NT_STATUS_IS_OK(result))) {
346                 fstrcpy(dom_name, domain->name);
347                 fstrcpy(name, names);
348         } else {
349                 *type = SID_NAME_UNKNOWN;
350                 fstrcpy(name, name_deadbeef);
351         }
352         
353         talloc_destroy(mem_ctx);
354
355         return rv;
356 }
357
358
359 /* Free state information held for {set,get,end}{pw,gr}ent() functions */
360
361 void free_getent_state(struct getent_state *state)
362 {
363         struct getent_state *temp;
364
365         /* Iterate over state list */
366
367         temp = state;
368
369         while(temp != NULL) {
370                 struct getent_state *next;
371
372                 /* Free sam entries then list entry */
373
374                 SAFE_FREE(state->sam_entries);
375                 DLIST_REMOVE(state, state);
376                 next = temp->next;
377
378                 SAFE_FREE(temp);
379                 temp = next;
380         }
381 }
382
383 /* Parse winbindd related parameters */
384
385 BOOL winbindd_param_init(void)
386 {
387         /* Parse winbind uid and winbind_gid parameters */
388
389         if (!lp_winbind_uid(&server_state.uid_low, &server_state.uid_high)) {
390                 DEBUG(0, ("winbind uid range missing or invalid\n"));
391                 return False;
392         }
393         
394         if (!lp_winbind_gid(&server_state.gid_low, &server_state.gid_high)) {
395                 DEBUG(0, ("winbind gid range missing or invalid\n"));
396                 return False;
397         }
398         
399         return True;
400 }
401
402 /* Check if a domain is present in a comma-separated list of domains */
403
404 BOOL check_domain_env(char *domain_env, char *domain)
405 {
406         fstring name;
407         const char *tmp = domain_env;
408
409         while(next_token(&tmp, name, ",", sizeof(fstring))) {
410                 if (strequal(name, domain))
411                         return True;
412         }
413
414         return False;
415 }
416
417 /* Parse a string of the form DOMAIN/user into a domain and a user */
418
419 BOOL parse_domain_user(const char *domuser, fstring domain, fstring user)
420 {
421         char *p = strchr(domuser,*lp_winbind_separator());
422
423         if (!(p || lp_winbind_use_default_domain()))
424                 return False;
425         
426         if(!p && lp_winbind_use_default_domain()) {
427                 fstrcpy(user, domuser);
428                 fstrcpy(domain, lp_workgroup());
429         } else {
430                 fstrcpy(user, p+1);
431                 fstrcpy(domain, domuser);
432                 domain[PTR_DIFF(p, domuser)] = 0;
433         }
434         strupper(domain);
435         return True;
436 }
437
438 /*
439     Fill DOMAIN\\USERNAME entry accounting 'winbind use default domain' and
440     'winbind separator' options.
441     This means:
442         - omit DOMAIN when 'winbind use default domain = true' and DOMAIN is
443         lp_workgroup
444          
445 */
446 void fill_domain_username(fstring name, const char *domain, const char *user)
447 {
448         if(lp_winbind_use_default_domain() &&
449             !strcmp(lp_workgroup(), domain)) {
450                 strlcpy(name, user, sizeof(fstring));
451         } else {
452                 slprintf(name, sizeof(fstring) - 1, "%s%s%s",
453                          domain, lp_winbind_separator(),
454                          user);
455         }
456 }
457
458 /*
459  * Winbindd socket accessor functions
460  */
461
462 /* Open the winbindd socket */
463
464 static int _winbindd_socket = -1;
465
466 int open_winbindd_socket(void)
467 {
468         if (_winbindd_socket == -1) {
469                 _winbindd_socket = create_pipe_sock(
470                         WINBINDD_SOCKET_DIR, WINBINDD_SOCKET_NAME, 0755);
471                 DEBUG(10, ("open_winbindd_socket: opened socket fd %d\n",
472                            _winbindd_socket));
473         }
474
475         return _winbindd_socket;
476 }
477
478 /* Close the winbindd socket */
479
480 void close_winbindd_socket(void)
481 {
482         if (_winbindd_socket != -1) {
483                 DEBUG(10, ("close_winbindd_socket: closing socket fd %d\n",
484                            _winbindd_socket));
485                 close(_winbindd_socket);
486                 _winbindd_socket = -1;
487         }
488 }
489
490 /*
491  * Client list accessor functions
492  */
493
494 static struct winbindd_cli_state *_client_list;
495 static int _num_clients;
496
497 /* Return list of all connected clients */
498
499 struct winbindd_cli_state *winbindd_client_list(void)
500 {
501         return _client_list;
502 }
503
504 /* Add a connection to the list */
505
506 void winbindd_add_client(struct winbindd_cli_state *cli)
507 {
508         DLIST_ADD(_client_list, cli);
509         _num_clients++;
510 }
511
512 /* Remove a client from the list */
513
514 void winbindd_remove_client(struct winbindd_cli_state *cli)
515 {
516         DLIST_REMOVE(_client_list, cli);
517         _num_clients--;
518 }
519
520 /* Close all open clients */
521
522 void winbindd_kill_all_clients(void)
523 {
524         struct winbindd_cli_state *cl = winbindd_client_list();
525
526         DEBUG(10, ("winbindd_kill_all_clients: going postal\n"));
527
528         while (cl) {
529                 struct winbindd_cli_state *next;
530                 
531                 next = cl->next;
532                 winbindd_remove_client(cl);
533                 cl = next;
534         }
535 }
536
537 /* Return number of open clients */
538
539 int winbindd_num_clients(void)
540 {
541         return _num_clients;
542 }