This commit was manufactured by cvs2svn to create branch 'SAMBA_3_0'.(This used to...
[ira/wip.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(BOOL force)
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         /* Only rescan every few minutes but force if necessary */
162
163         if (((unsigned)(t - last_scan) < WINBINDD_RESCAN_FREQ) && !force)
164                 return;
165
166         last_scan = t;
167
168         DEBUG(1, ("scanning trusted domain list\n"));
169
170         if (!(mem_ctx = talloc_init("init_domain_list")))
171                 return;
172
173         for (domain = _domain_list; domain; domain = domain->next) {
174                 NTSTATUS result;
175                 char **names;
176                 char **alt_names;
177                 int num_domains = 0;
178                 DOM_SID *dom_sids;
179                 int i;
180
181                 result = domain->methods->trusted_domains(domain, mem_ctx, &num_domains,
182                                                           &names, &alt_names, &dom_sids);
183                 if (!NT_STATUS_IS_OK(result)) {
184                         continue;
185                 }
186
187                 /* Add each domain to the trusted domain list. Each domain inherits
188                    the access methods of its parent */
189                 for(i = 0; i < num_domains; i++) {
190                         DEBUG(10,("Found domain %s\n", names[i]));
191                         add_trusted_domain(names[i], 
192                                            alt_names?alt_names[i]:NULL, 
193                                            domain->methods, &dom_sids[i]);
194                 }
195         }
196
197         talloc_destroy(mem_ctx);
198 }
199
200 /* Look up global info for the winbind daemon */
201 BOOL init_domain_list(void)
202 {
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         if (!secrets_fetch_domain_sid(domain->name, &domain->sid)) {
212                 DEBUG(1, ("Could not fetch sid for our domain %s\n",
213                           domain->name));
214                 return False;
215         }
216
217         /* get any alternate name for the primary domain */
218         cache_methods.alternate_name(domain);
219
220         /* do an initial scan for trusted domains */
221         rescan_trusted_domains(True);
222
223         return True;
224 }
225
226 /* Given a domain name, return the struct winbindd domain info for it 
227    if it is actually working. */
228
229 struct winbindd_domain *find_domain_from_name(const char *domain_name)
230 {
231         struct winbindd_domain *domain;
232
233         /* Search through list */
234
235         for (domain = domain_list(); domain != NULL; domain = domain->next) {
236                 if (strequal(domain_name, domain->name) ||
237                     (domain->alt_name[0] && strequal(domain_name, domain->alt_name)))
238                         return domain;
239         }
240
241         /* Not found */
242
243         return NULL;
244 }
245
246 /* Given a domain sid, return the struct winbindd domain info for it */
247
248 struct winbindd_domain *find_domain_from_sid(DOM_SID *sid)
249 {
250         struct winbindd_domain *domain;
251
252         /* Search through list */
253
254         for (domain = domain_list(); domain != NULL; domain = domain->next) {
255                 if (sid_compare_domain(sid, &domain->sid) == 0)
256                         return domain;
257         }
258
259         /* Not found */
260
261         return NULL;
262 }
263
264 /* Lookup a sid in a domain from a name */
265
266 BOOL winbindd_lookup_sid_by_name(struct winbindd_domain *domain, 
267                                  const char *name, DOM_SID *sid, 
268                                  enum SID_NAME_USE *type)
269 {
270         NTSTATUS result;
271         
272         /* Don't bother with machine accounts */
273         
274         if (name[strlen(name) - 1] == '$')
275                 return False;
276
277         /* Lookup name */
278         result = domain->methods->name_to_sid(domain, name, sid, type);
279         
280         /* Return rid and type if lookup successful */
281         if (!NT_STATUS_IS_OK(result)) {
282                 *type = SID_NAME_UNKNOWN;
283         }
284
285         return NT_STATUS_IS_OK(result);
286 }
287
288 /**
289  * @brief Lookup a name in a domain from a sid.
290  *
291  * @param sid Security ID you want to look up.
292  *
293  * @param name On success, set to the name corresponding to @p sid.
294  * 
295  * @param dom_name On success, set to the 'domain name' corresponding to @p sid.
296  * 
297  * @param type On success, contains the type of name: alias, group or
298  * user.
299  *
300  * @retval True if the name exists, in which case @p name and @p type
301  * are set, otherwise False.
302  **/
303 BOOL winbindd_lookup_name_by_sid(DOM_SID *sid,
304                                  fstring dom_name,
305                                  fstring name,
306                                  enum SID_NAME_USE *type)
307 {
308         char *names;
309         NTSTATUS result;
310         TALLOC_CTX *mem_ctx;
311         BOOL rv = False;
312         struct winbindd_domain *domain;
313
314         domain = find_domain_from_sid(sid);
315
316         if (!domain) {
317                 DEBUG(1,("Can't find domain from sid\n"));
318                 return False;
319         }
320
321         /* Lookup name */
322
323         if (!(mem_ctx = talloc_init("winbindd_lookup_name_by_sid")))
324                 return False;
325         
326         result = domain->methods->sid_to_name(domain, mem_ctx, sid, &names, type);
327
328         /* Return name and type if successful */
329         
330         if ((rv = NT_STATUS_IS_OK(result))) {
331                 fstrcpy(dom_name, domain->name);
332                 fstrcpy(name, names);
333         } else {
334                 *type = SID_NAME_UNKNOWN;
335                 fstrcpy(name, name_deadbeef);
336         }
337         
338         talloc_destroy(mem_ctx);
339
340         return rv;
341 }
342
343
344 /* Free state information held for {set,get,end}{pw,gr}ent() functions */
345
346 void free_getent_state(struct getent_state *state)
347 {
348         struct getent_state *temp;
349
350         /* Iterate over state list */
351
352         temp = state;
353
354         while(temp != NULL) {
355                 struct getent_state *next;
356
357                 /* Free sam entries then list entry */
358
359                 SAFE_FREE(state->sam_entries);
360                 DLIST_REMOVE(state, state);
361                 next = temp->next;
362
363                 SAFE_FREE(temp);
364                 temp = next;
365         }
366 }
367
368 /* Parse winbindd related parameters */
369
370 BOOL winbindd_param_init(void)
371 {
372         /* Parse winbind uid and winbind_gid parameters */
373
374         if (!lp_winbind_uid(&server_state.uid_low, &server_state.uid_high)) {
375                 DEBUG(0, ("winbind uid range missing or invalid\n"));
376                 return False;
377         }
378         
379         if (!lp_winbind_gid(&server_state.gid_low, &server_state.gid_high)) {
380                 DEBUG(0, ("winbind gid range missing or invalid\n"));
381                 return False;
382         }
383         
384         return True;
385 }
386
387 /* Check if a domain is present in a comma-separated list of domains */
388
389 BOOL check_domain_env(char *domain_env, char *domain)
390 {
391         fstring name;
392         const char *tmp = domain_env;
393
394         while(next_token(&tmp, name, ",", sizeof(fstring))) {
395                 if (strequal(name, domain))
396                         return True;
397         }
398
399         return False;
400 }
401
402 /* Parse a string of the form DOMAIN/user into a domain and a user */
403
404 BOOL parse_domain_user(const char *domuser, fstring domain, fstring user)
405 {
406         char *p = strchr(domuser,*lp_winbind_separator());
407
408         if (!(p || lp_winbind_use_default_domain()))
409                 return False;
410         
411         if(!p && lp_winbind_use_default_domain()) {
412                 fstrcpy(user, domuser);
413                 fstrcpy(domain, lp_workgroup());
414         } else {
415                 fstrcpy(user, p+1);
416                 fstrcpy(domain, domuser);
417                 domain[PTR_DIFF(p, domuser)] = 0;
418         }
419         strupper(domain);
420         return True;
421 }
422
423 /*
424     Fill DOMAIN\\USERNAME entry accounting 'winbind use default domain' and
425     'winbind separator' options.
426     This means:
427         - omit DOMAIN when 'winbind use default domain = true' and DOMAIN is
428         lp_workgroup
429          
430 */
431 void fill_domain_username(fstring name, const char *domain, const char *user)
432 {
433         if(lp_winbind_use_default_domain() &&
434             !strcmp(lp_workgroup(), domain)) {
435                 strlcpy(name, user, sizeof(fstring));
436         } else {
437                 slprintf(name, sizeof(fstring) - 1, "%s%s%s",
438                          domain, lp_winbind_separator(),
439                          user);
440         }
441 }
442
443 /*
444  * Winbindd socket accessor functions
445  */
446
447 char *get_winbind_priv_pipe_dir(void) 
448 {
449         return lock_path(WINBINDD_PRIV_SOCKET_SUBDIR);
450 }
451
452 /* Open the winbindd socket */
453
454 static int _winbindd_socket = -1;
455 static int _winbindd_priv_socket = -1;
456
457 int open_winbindd_socket(void)
458 {
459         if (_winbindd_socket == -1) {
460                 _winbindd_socket = create_pipe_sock(
461                         WINBINDD_SOCKET_DIR, WINBINDD_SOCKET_NAME, 0755);
462                 DEBUG(10, ("open_winbindd_socket: opened socket fd %d\n",
463                            _winbindd_socket));
464         }
465
466         return _winbindd_socket;
467 }
468
469 int open_winbindd_priv_socket(void)
470 {
471         if (_winbindd_priv_socket == -1) {
472                 _winbindd_priv_socket = create_pipe_sock(
473                         get_winbind_priv_pipe_dir(), WINBINDD_SOCKET_NAME, 0750);
474                 DEBUG(10, ("open_winbindd_priv_socket: opened socket fd %d\n",
475                            _winbindd_priv_socket));
476         }
477
478         return _winbindd_priv_socket;
479 }
480
481 /* Close the winbindd socket */
482
483 void close_winbindd_socket(void)
484 {
485         if (_winbindd_socket != -1) {
486                 DEBUG(10, ("close_winbindd_socket: closing socket fd %d\n",
487                            _winbindd_socket));
488                 close(_winbindd_socket);
489                 _winbindd_socket = -1;
490         }
491         if (_winbindd_priv_socket != -1) {
492                 DEBUG(10, ("close_winbindd_socket: closing socket fd %d\n",
493                            _winbindd_priv_socket));
494                 close(_winbindd_priv_socket);
495                 _winbindd_priv_socket = -1;
496         }
497 }
498
499 /*
500  * Client list accessor functions
501  */
502
503 static struct winbindd_cli_state *_client_list;
504 static int _num_clients;
505
506 /* Return list of all connected clients */
507
508 struct winbindd_cli_state *winbindd_client_list(void)
509 {
510         return _client_list;
511 }
512
513 /* Add a connection to the list */
514
515 void winbindd_add_client(struct winbindd_cli_state *cli)
516 {
517         DLIST_ADD(_client_list, cli);
518         _num_clients++;
519 }
520
521 /* Remove a client from the list */
522
523 void winbindd_remove_client(struct winbindd_cli_state *cli)
524 {
525         DLIST_REMOVE(_client_list, cli);
526         _num_clients--;
527 }
528
529 /* Close all open clients */
530
531 void winbindd_kill_all_clients(void)
532 {
533         struct winbindd_cli_state *cl = winbindd_client_list();
534
535         DEBUG(10, ("winbindd_kill_all_clients: going postal\n"));
536
537         while (cl) {
538                 struct winbindd_cli_state *next;
539                 
540                 next = cl->next;
541                 winbindd_remove_client(cl);
542                 cl = next;
543         }
544 }
545
546 /* Return number of open clients */
547
548 int winbindd_num_clients(void)
549 {
550         return _num_clients;
551 }