r14403: * modifies create_local_nt_token() to create a BUILTIN\Administrators
[Samba.git] / source / nsswitch / winbindd_util.c
blob64b4dd27a3967a918105aece9ef5b193dcdb31ce
1 /*
2 Unix SMB/CIFS implementation.
4 Winbind daemon for ntdom nss module
6 Copyright (C) Tim Potter 2000-2001
7 Copyright (C) 2001 by Martin Pool <mbp@samba.org>
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.
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.
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.
24 #include "includes.h"
25 #include "winbindd.h"
27 #undef DBGC_CLASS
28 #define DBGC_CLASS DBGC_WINBIND
30 /**
31 * @file winbindd_util.c
33 * Winbind daemon for NT domain authentication nss module.
34 **/
37 /**
38 * Used to clobber name fields that have an undefined value.
40 * Correct code should never look at a field that has this value.
41 **/
43 static const fstring name_deadbeef = "<deadbeef>";
45 /* The list of trusted domains. Note that the list can be deleted and
46 recreated using the init_domain_list() function so pointers to
47 individual winbindd_domain structures cannot be made. Keep a copy of
48 the domain name instead. */
50 static struct winbindd_domain *_domain_list;
52 /**
53 When was the last scan of trusted domains done?
55 0 == not ever
58 static time_t last_trustdom_scan;
60 struct winbindd_domain *domain_list(void)
62 /* Initialise list */
64 if (!_domain_list && !init_domain_list()) {
65 return NULL;
68 return _domain_list;
71 /* Free all entries in the trusted domain list */
73 void free_domain_list(void)
75 struct winbindd_domain *domain = _domain_list;
77 while(domain) {
78 struct winbindd_domain *next = domain->next;
80 DLIST_REMOVE(_domain_list, domain);
81 SAFE_FREE(domain);
82 domain = next;
86 static BOOL is_internal_domain(const DOM_SID *sid)
88 if (sid == NULL)
89 return False;
91 return (sid_check_is_domain(sid) || sid_check_is_builtin(sid));
94 static BOOL is_in_internal_domain(const DOM_SID *sid)
96 if (sid == NULL)
97 return False;
99 return (sid_check_is_in_our_domain(sid) || sid_check_is_in_builtin(sid));
103 /* Add a trusted domain to our list of domains */
104 static struct winbindd_domain *add_trusted_domain(const char *domain_name, const char *alt_name,
105 struct winbindd_methods *methods,
106 const DOM_SID *sid)
108 struct winbindd_domain *domain;
109 const char *alternative_name = NULL;
111 /* ignore alt_name if we are not in an AD domain */
113 if ( (lp_security() == SEC_ADS) && alt_name && *alt_name) {
114 alternative_name = alt_name;
117 /* We can't call domain_list() as this function is called from
118 init_domain_list() and we'll get stuck in a loop. */
119 for (domain = _domain_list; domain; domain = domain->next) {
120 if (strequal(domain_name, domain->name) ||
121 strequal(domain_name, domain->alt_name)) {
122 return domain;
124 if (alternative_name && *alternative_name) {
125 if (strequal(alternative_name, domain->name) ||
126 strequal(alternative_name, domain->alt_name)) {
127 return domain;
130 if (sid) {
131 if (is_null_sid(sid)) {
133 } else if (sid_equal(sid, &domain->sid)) {
134 return domain;
139 /* Create new domain entry */
141 if ((domain = SMB_MALLOC_P(struct winbindd_domain)) == NULL)
142 return NULL;
144 /* Fill in fields */
146 ZERO_STRUCTP(domain);
148 /* prioritise the short name */
149 if (strchr_m(domain_name, '.') && alternative_name && *alternative_name) {
150 fstrcpy(domain->name, alternative_name);
151 fstrcpy(domain->alt_name, domain_name);
152 } else {
153 fstrcpy(domain->name, domain_name);
154 if (alternative_name) {
155 fstrcpy(domain->alt_name, alternative_name);
159 domain->methods = methods;
160 domain->backend = NULL;
161 domain->internal = is_internal_domain(sid);
162 domain->sequence_number = DOM_SEQUENCE_NONE;
163 domain->last_seq_check = 0;
164 domain->initialized = False;
165 domain->online = False;
166 if (sid) {
167 sid_copy(&domain->sid, sid);
170 /* Link to domain list */
171 DLIST_ADD(_domain_list, domain);
173 DEBUG(2,("Added domain %s %s %s\n",
174 domain->name, domain->alt_name,
175 &domain->sid?sid_string_static(&domain->sid):""));
177 return domain;
180 /********************************************************************
181 rescan our domains looking for new trusted domains
182 ********************************************************************/
184 struct trustdom_state {
185 TALLOC_CTX *mem_ctx;
186 struct winbindd_response *response;
189 static void trustdom_recv(void *private_data, BOOL success);
191 static void add_trusted_domains( struct winbindd_domain *domain )
193 TALLOC_CTX *mem_ctx;
194 struct winbindd_request *request;
195 struct winbindd_response *response;
197 struct trustdom_state *state;
199 mem_ctx = talloc_init("add_trusted_domains");
200 if (mem_ctx == NULL) {
201 DEBUG(0, ("talloc_init failed\n"));
202 return;
205 request = TALLOC_ZERO_P(mem_ctx, struct winbindd_request);
206 response = TALLOC_P(mem_ctx, struct winbindd_response);
207 state = TALLOC_P(mem_ctx, struct trustdom_state);
209 if ((request == NULL) || (response == NULL) || (state == NULL)) {
210 DEBUG(0, ("talloc failed\n"));
211 talloc_destroy(mem_ctx);
212 return;
215 state->mem_ctx = mem_ctx;
216 state->response = response;
218 request->length = sizeof(*request);
219 request->cmd = WINBINDD_LIST_TRUSTDOM;
221 async_domain_request(mem_ctx, domain, request, response,
222 trustdom_recv, state);
225 static void trustdom_recv(void *private_data, BOOL success)
227 extern struct winbindd_methods cache_methods;
228 struct trustdom_state *state =
229 talloc_get_type_abort(private_data, struct trustdom_state);
230 struct winbindd_response *response = state->response;
231 char *p;
233 if ((!success) || (response->result != WINBINDD_OK)) {
234 DEBUG(1, ("Could not receive trustdoms\n"));
235 talloc_destroy(state->mem_ctx);
236 return;
239 p = response->extra_data;
241 while ((p != NULL) && (*p != '\0')) {
242 char *q, *sidstr, *alt_name;
243 DOM_SID sid;
245 alt_name = strchr(p, '\\');
246 if (alt_name == NULL) {
247 DEBUG(0, ("Got invalid trustdom response\n"));
248 break;
251 *alt_name = '\0';
252 alt_name += 1;
254 sidstr = strchr(alt_name, '\\');
255 if (sidstr == NULL) {
256 DEBUG(0, ("Got invalid trustdom response\n"));
257 break;
260 *sidstr = '\0';
261 sidstr += 1;
263 q = strchr(sidstr, '\n');
264 if (q != NULL)
265 *q = '\0';
267 if (!string_to_sid(&sid, sidstr)) {
268 DEBUG(0, ("Got invalid trustdom response\n"));
269 break;
272 if (find_domain_from_name_noinit(p) == NULL) {
273 struct winbindd_domain *domain;
274 char *alternate_name = NULL;
276 /* use the real alt_name if we have one, else pass in NULL */
278 if ( !strequal( alt_name, "(null)" ) )
279 alternate_name = alt_name;
281 domain = add_trusted_domain(p, alternate_name,
282 &cache_methods,
283 &sid);
284 setup_domain_child(domain, &domain->child, NULL);
286 p=q;
287 if (p != NULL)
288 p += 1;
291 SAFE_FREE(response->extra_data);
292 talloc_destroy(state->mem_ctx);
295 /********************************************************************
296 Periodically we need to refresh the trusted domain cache for smbd
297 ********************************************************************/
299 void rescan_trusted_domains( void )
301 time_t now = time(NULL);
303 /* see if the time has come... */
305 if ((now >= last_trustdom_scan) &&
306 ((now-last_trustdom_scan) < WINBINDD_RESCAN_FREQ) )
307 return;
309 /* this will only add new domains we didn't already know about */
311 add_trusted_domains( find_our_domain() );
313 last_trustdom_scan = now;
315 return;
318 struct init_child_state {
319 TALLOC_CTX *mem_ctx;
320 struct winbindd_domain *domain;
321 struct winbindd_request *request;
322 struct winbindd_response *response;
323 void (*continuation)(void *private_data, BOOL success);
324 void *private_data;
327 static void init_child_recv(void *private_data, BOOL success);
328 static void init_child_getdc_recv(void *private_data, BOOL success);
330 enum winbindd_result init_child_connection(struct winbindd_domain *domain,
331 void (*continuation)(void *private_data,
332 BOOL success),
333 void *private_data)
335 TALLOC_CTX *mem_ctx;
336 struct winbindd_request *request;
337 struct winbindd_response *response;
338 struct init_child_state *state;
339 struct winbindd_domain *request_domain;
341 mem_ctx = talloc_init("init_child_connection");
342 if (mem_ctx == NULL) {
343 DEBUG(0, ("talloc_init failed\n"));
344 return WINBINDD_ERROR;
347 request = TALLOC_ZERO_P(mem_ctx, struct winbindd_request);
348 response = TALLOC_P(mem_ctx, struct winbindd_response);
349 state = TALLOC_P(mem_ctx, struct init_child_state);
351 if ((request == NULL) || (response == NULL) || (state == NULL)) {
352 DEBUG(0, ("talloc failed\n"));
353 continuation(private_data, False);
354 return WINBINDD_ERROR;
357 request->length = sizeof(*request);
359 state->mem_ctx = mem_ctx;
360 state->domain = domain;
361 state->request = request;
362 state->response = response;
363 state->continuation = continuation;
364 state->private_data = private_data;
366 if (domain->primary) {
367 /* The primary domain has to find the DC name itself */
368 request->cmd = WINBINDD_INIT_CONNECTION;
369 fstrcpy(request->domain_name, domain->name);
370 request->data.init_conn.is_primary = True;
371 fstrcpy(request->data.init_conn.dcname, "");
372 async_request(mem_ctx, &domain->child, request, response,
373 init_child_recv, state);
374 return WINBINDD_PENDING;
377 /* This is *not* the primary domain, let's ask our DC about a DC
378 * name */
380 request->cmd = WINBINDD_GETDCNAME;
381 fstrcpy(request->domain_name, domain->name);
383 /* save online flag */
384 request_domain = find_our_domain();
385 request_domain->online = domain->online;
387 async_domain_request(mem_ctx, request_domain, request, response,
388 init_child_getdc_recv, state);
389 return WINBINDD_PENDING;
392 static void init_child_getdc_recv(void *private_data, BOOL success)
394 struct init_child_state *state =
395 talloc_get_type_abort(private_data, struct init_child_state);
396 const char *dcname = "";
398 DEBUG(10, ("Received getdcname response\n"));
400 if (success && (state->response->result == WINBINDD_OK)) {
401 dcname = state->response->data.dc_name;
404 state->request->cmd = WINBINDD_INIT_CONNECTION;
405 fstrcpy(state->request->domain_name, state->domain->name);
406 state->request->data.init_conn.is_primary = False;
407 fstrcpy(state->request->data.init_conn.dcname, dcname);
409 async_request(state->mem_ctx, &state->domain->child,
410 state->request, state->response,
411 init_child_recv, state);
414 static void init_child_recv(void *private_data, BOOL success)
416 struct init_child_state *state =
417 talloc_get_type_abort(private_data, struct init_child_state);
419 DEBUG(5, ("Received child initialization response for domain %s\n",
420 state->domain->name));
422 if ((!success) || (state->response->result != WINBINDD_OK)) {
423 DEBUG(3, ("Could not init child\n"));
424 state->continuation(state->private_data, False);
425 talloc_destroy(state->mem_ctx);
426 return;
429 fstrcpy(state->domain->name,
430 state->response->data.domain_info.name);
431 fstrcpy(state->domain->alt_name,
432 state->response->data.domain_info.alt_name);
433 string_to_sid(&state->domain->sid,
434 state->response->data.domain_info.sid);
435 state->domain->native_mode =
436 state->response->data.domain_info.native_mode;
437 state->domain->active_directory =
438 state->response->data.domain_info.active_directory;
439 state->domain->sequence_number =
440 state->response->data.domain_info.sequence_number;
442 state->domain->initialized = 1;
444 if (state->continuation != NULL)
445 state->continuation(state->private_data, True);
446 talloc_destroy(state->mem_ctx);
449 enum winbindd_result winbindd_dual_init_connection(struct winbindd_domain *domain,
450 struct winbindd_cli_state *state)
452 struct in_addr ipaddr;
454 /* Ensure null termination */
455 state->request.domain_name
456 [sizeof(state->request.domain_name)-1]='\0';
457 state->request.data.init_conn.dcname
458 [sizeof(state->request.data.init_conn.dcname)-1]='\0';
460 if (strlen(state->request.data.init_conn.dcname) > 0) {
461 fstrcpy(domain->dcname, state->request.data.init_conn.dcname);
464 if (strlen(domain->dcname) > 0) {
465 if (!resolve_name(domain->dcname, &ipaddr, 0x20)) {
466 DEBUG(2, ("Could not resolve DC name %s for domain %s\n",
467 domain->dcname, domain->name));
468 return WINBINDD_ERROR;
471 domain->dcaddr.sin_family = PF_INET;
472 putip((char *)&(domain->dcaddr.sin_addr), (char *)&ipaddr);
473 domain->dcaddr.sin_port = 0;
476 set_dc_type_and_flags(domain);
478 if (!domain->initialized) {
479 DEBUG(1, ("Could not initialize domain %s\n",
480 state->request.domain_name));
481 return WINBINDD_ERROR;
484 fstrcpy(state->response.data.domain_info.name, domain->name);
485 fstrcpy(state->response.data.domain_info.alt_name, domain->alt_name);
486 fstrcpy(state->response.data.domain_info.sid,
487 sid_string_static(&domain->sid));
489 state->response.data.domain_info.native_mode
490 = domain->native_mode;
491 state->response.data.domain_info.active_directory
492 = domain->active_directory;
493 state->response.data.domain_info.primary
494 = domain->primary;
495 state->response.data.domain_info.sequence_number =
496 domain->sequence_number;
498 return WINBINDD_OK;
501 /* Look up global info for the winbind daemon */
502 BOOL init_domain_list(void)
504 extern struct winbindd_methods cache_methods;
505 extern struct winbindd_methods passdb_methods;
506 struct winbindd_domain *domain;
508 /* Free existing list */
509 free_domain_list();
511 /* Add ourselves as the first entry. */
513 if (IS_DC) {
514 domain = add_trusted_domain(get_global_sam_name(), NULL,
515 &passdb_methods,
516 get_global_sam_sid());
517 } else {
519 DOM_SID our_sid;
521 if (!secrets_fetch_domain_sid(lp_workgroup(), &our_sid)) {
522 DEBUG(0,("Could not fetch our SID - did we join?\n"));
523 return False;
526 domain = add_trusted_domain( lp_workgroup(), lp_realm(),
527 &cache_methods, &our_sid);
530 domain->primary = True;
531 setup_domain_child(domain, &domain->child, NULL);
533 /* Add our local SAM domains */
535 domain = add_trusted_domain("BUILTIN", NULL, &passdb_methods,
536 &global_sid_Builtin);
537 setup_domain_child(domain, &domain->child, NULL);
539 if (!IS_DC) {
540 domain = add_trusted_domain(get_global_sam_name(), NULL,
541 &passdb_methods,
542 get_global_sam_sid());
543 setup_domain_child(domain, &domain->child, NULL);
546 return True;
549 /**
550 * Given a domain name, return the struct winbindd domain info for it
552 * @note Do *not* pass lp_workgroup() to this function. domain_list
553 * may modify it's value, and free that pointer. Instead, our local
554 * domain may be found by calling find_our_domain().
555 * directly.
558 * @return The domain structure for the named domain, if it is working.
561 struct winbindd_domain *find_domain_from_name_noinit(const char *domain_name)
563 struct winbindd_domain *domain;
565 /* Search through list */
567 for (domain = domain_list(); domain != NULL; domain = domain->next) {
568 if (strequal(domain_name, domain->name) ||
569 (domain->alt_name[0] &&
570 strequal(domain_name, domain->alt_name))) {
571 return domain;
575 /* Not found */
577 return NULL;
580 struct winbindd_domain *find_domain_from_name(const char *domain_name)
582 struct winbindd_domain *domain;
584 domain = find_domain_from_name_noinit(domain_name);
586 if (domain == NULL)
587 return NULL;
589 if (!domain->initialized)
590 set_dc_type_and_flags(domain);
592 return domain;
595 /* Given a domain sid, return the struct winbindd domain info for it */
597 struct winbindd_domain *find_domain_from_sid_noinit(const DOM_SID *sid)
599 struct winbindd_domain *domain;
601 /* Search through list */
603 for (domain = domain_list(); domain != NULL; domain = domain->next) {
604 if (sid_compare_domain(sid, &domain->sid) == 0)
605 return domain;
608 /* Not found */
610 return NULL;
613 /* Given a domain sid, return the struct winbindd domain info for it */
615 struct winbindd_domain *find_domain_from_sid(const DOM_SID *sid)
617 struct winbindd_domain *domain;
619 domain = find_domain_from_sid_noinit(sid);
621 if (domain == NULL)
622 return NULL;
624 if (!domain->initialized)
625 set_dc_type_and_flags(domain);
627 return domain;
630 struct winbindd_domain *find_our_domain(void)
632 struct winbindd_domain *domain;
634 /* Search through list */
636 for (domain = domain_list(); domain != NULL; domain = domain->next) {
637 if (domain->primary)
638 return domain;
641 smb_panic("Could not find our domain\n");
642 return NULL;
645 struct winbindd_domain *find_builtin_domain(void)
647 DOM_SID sid;
648 struct winbindd_domain *domain;
650 string_to_sid(&sid, "S-1-5-32");
651 domain = find_domain_from_sid(&sid);
653 if (domain == NULL)
654 smb_panic("Could not find BUILTIN domain\n");
656 return domain;
659 /* Find the appropriate domain to lookup a name or SID */
661 struct winbindd_domain *find_lookup_domain_from_sid(const DOM_SID *sid)
663 /* A DC can't ask the local smbd for remote SIDs, here winbindd is the
664 * one to contact the external DC's. On member servers the internal
665 * domains are different: These are part of the local SAM. */
667 DEBUG(10, ("find_lookup_domain_from_sid(%s)\n",
668 sid_string_static(sid)));
670 if (IS_DC || is_internal_domain(sid) || is_in_internal_domain(sid)) {
671 DEBUG(10, ("calling find_domain_from_sid\n"));
672 return find_domain_from_sid(sid);
675 /* On a member server a query for SID or name can always go to our
676 * primary DC. */
678 DEBUG(10, ("calling find_our_domain\n"));
679 return find_our_domain();
682 struct winbindd_domain *find_lookup_domain_from_name(const char *domain_name)
684 if (IS_DC || strequal(domain_name, "BUILTIN") ||
685 strequal(domain_name, get_global_sam_name()))
686 return find_domain_from_name_noinit(domain_name);
688 return find_our_domain();
691 /* Lookup a sid in a domain from a name */
693 BOOL winbindd_lookup_sid_by_name(TALLOC_CTX *mem_ctx,
694 struct winbindd_domain *domain,
695 const char *domain_name,
696 const char *name, DOM_SID *sid,
697 enum SID_NAME_USE *type)
699 NTSTATUS result;
701 /* Lookup name */
702 result = domain->methods->name_to_sid(domain, mem_ctx, domain_name, name, sid, type);
704 /* Return rid and type if lookup successful */
705 if (!NT_STATUS_IS_OK(result)) {
706 *type = SID_NAME_UNKNOWN;
709 return NT_STATUS_IS_OK(result);
713 * @brief Lookup a name in a domain from a sid.
715 * @param sid Security ID you want to look up.
716 * @param name On success, set to the name corresponding to @p sid.
717 * @param dom_name On success, set to the 'domain name' corresponding to @p sid.
718 * @param type On success, contains the type of name: alias, group or
719 * user.
720 * @retval True if the name exists, in which case @p name and @p type
721 * are set, otherwise False.
723 BOOL winbindd_lookup_name_by_sid(TALLOC_CTX *mem_ctx,
724 DOM_SID *sid,
725 fstring dom_name,
726 fstring name,
727 enum SID_NAME_USE *type)
729 char *names;
730 char *dom_names;
731 NTSTATUS result;
732 BOOL rv = False;
733 struct winbindd_domain *domain;
735 domain = find_lookup_domain_from_sid(sid);
737 if (!domain) {
738 DEBUG(1,("Can't find domain from sid\n"));
739 return False;
742 /* Lookup name */
744 result = domain->methods->sid_to_name(domain, mem_ctx, sid, &dom_names, &names, type);
746 /* Return name and type if successful */
748 if ((rv = NT_STATUS_IS_OK(result))) {
749 fstrcpy(dom_name, dom_names);
750 fstrcpy(name, names);
751 } else {
752 *type = SID_NAME_UNKNOWN;
753 fstrcpy(name, name_deadbeef);
756 return rv;
759 /* Free state information held for {set,get,end}{pw,gr}ent() functions */
761 void free_getent_state(struct getent_state *state)
763 struct getent_state *temp;
765 /* Iterate over state list */
767 temp = state;
769 while(temp != NULL) {
770 struct getent_state *next;
772 /* Free sam entries then list entry */
774 SAFE_FREE(state->sam_entries);
775 DLIST_REMOVE(state, state);
776 next = temp->next;
778 SAFE_FREE(temp);
779 temp = next;
783 /* Parse winbindd related parameters */
785 BOOL winbindd_param_init(void)
787 /* Parse winbind uid and winbind_gid parameters */
789 if (!lp_idmap_uid(&server_state.uid_low, &server_state.uid_high)) {
790 DEBUG(0, ("winbindd: idmap uid range missing or invalid\n"));
791 DEBUG(0, ("winbindd: cannot continue, exiting.\n"));
792 return False;
795 if (!lp_idmap_gid(&server_state.gid_low, &server_state.gid_high)) {
796 DEBUG(0, ("winbindd: idmap gid range missing or invalid\n"));
797 DEBUG(0, ("winbindd: cannot continue, exiting.\n"));
798 return False;
801 return True;
804 BOOL is_in_uid_range(uid_t uid)
806 return ((uid >= server_state.uid_low) &&
807 (uid <= server_state.uid_high));
810 BOOL is_in_gid_range(gid_t gid)
812 return ((gid >= server_state.gid_low) &&
813 (gid <= server_state.gid_high));
816 /* Is this a domain which we may assume no DOMAIN\ prefix? */
818 static BOOL assume_domain(const char *domain) {
819 if ((lp_winbind_use_default_domain()
820 || lp_winbind_trusted_domains_only()) &&
821 strequal(lp_workgroup(), domain))
822 return True;
824 if (strequal(get_global_sam_name(), domain))
825 return True;
827 return False;
830 /* Parse a string of the form DOMAIN\user into a domain and a user */
832 BOOL parse_domain_user(const char *domuser, fstring domain, fstring user)
834 char *p = strchr(domuser,*lp_winbind_separator());
836 if ( !p ) {
837 fstrcpy(user, domuser);
839 if ( assume_domain(lp_workgroup())) {
840 fstrcpy(domain, lp_workgroup());
841 } else {
842 return False;
844 } else {
845 fstrcpy(user, p+1);
846 fstrcpy(domain, domuser);
847 domain[PTR_DIFF(p, domuser)] = 0;
850 strupper_m(domain);
852 return True;
855 BOOL parse_domain_user_talloc(TALLOC_CTX *mem_ctx, const char *domuser,
856 char **domain, char **user)
858 fstring fstr_domain, fstr_user;
859 if (!parse_domain_user(domuser, fstr_domain, fstr_user)) {
860 return False;
862 *domain = talloc_strdup(mem_ctx, fstr_domain);
863 *user = talloc_strdup(mem_ctx, fstr_user);
864 return ((*domain != NULL) && (*user != NULL));
868 Fill DOMAIN\\USERNAME entry accounting 'winbind use default domain' and
869 'winbind separator' options.
870 This means:
871 - omit DOMAIN when 'winbind use default domain = true' and DOMAIN is
872 lp_workgroup()
874 If we are a PDC or BDC, and this is for our domain, do likewise.
876 Also, if omit DOMAIN if 'winbind trusted domains only = true', as the
877 username is then unqualified in unix
880 void fill_domain_username(fstring name, const char *domain, const char *user, BOOL can_assume)
882 fstring tmp_user;
884 fstrcpy(tmp_user, user);
885 strlower_m(tmp_user);
887 if (can_assume && assume_domain(domain)) {
888 strlcpy(name, user, sizeof(fstring));
889 } else {
890 slprintf(name, sizeof(fstring) - 1, "%s%c%s",
891 domain, *lp_winbind_separator(),
892 tmp_user);
897 * Winbindd socket accessor functions
900 char *get_winbind_priv_pipe_dir(void)
902 return lock_path(WINBINDD_PRIV_SOCKET_SUBDIR);
905 /* Open the winbindd socket */
907 static int _winbindd_socket = -1;
908 static int _winbindd_priv_socket = -1;
910 int open_winbindd_socket(void)
912 if (_winbindd_socket == -1) {
913 _winbindd_socket = create_pipe_sock(
914 WINBINDD_SOCKET_DIR, WINBINDD_SOCKET_NAME, 0755);
915 DEBUG(10, ("open_winbindd_socket: opened socket fd %d\n",
916 _winbindd_socket));
919 return _winbindd_socket;
922 int open_winbindd_priv_socket(void)
924 if (_winbindd_priv_socket == -1) {
925 _winbindd_priv_socket = create_pipe_sock(
926 get_winbind_priv_pipe_dir(), WINBINDD_SOCKET_NAME, 0750);
927 DEBUG(10, ("open_winbindd_priv_socket: opened socket fd %d\n",
928 _winbindd_priv_socket));
931 return _winbindd_priv_socket;
934 /* Close the winbindd socket */
936 void close_winbindd_socket(void)
938 if (_winbindd_socket != -1) {
939 DEBUG(10, ("close_winbindd_socket: closing socket fd %d\n",
940 _winbindd_socket));
941 close(_winbindd_socket);
942 _winbindd_socket = -1;
944 if (_winbindd_priv_socket != -1) {
945 DEBUG(10, ("close_winbindd_socket: closing socket fd %d\n",
946 _winbindd_priv_socket));
947 close(_winbindd_priv_socket);
948 _winbindd_priv_socket = -1;
953 * Client list accessor functions
956 static struct winbindd_cli_state *_client_list;
957 static int _num_clients;
959 /* Return list of all connected clients */
961 struct winbindd_cli_state *winbindd_client_list(void)
963 return _client_list;
966 /* Add a connection to the list */
968 void winbindd_add_client(struct winbindd_cli_state *cli)
970 DLIST_ADD(_client_list, cli);
971 _num_clients++;
974 /* Remove a client from the list */
976 void winbindd_remove_client(struct winbindd_cli_state *cli)
978 DLIST_REMOVE(_client_list, cli);
979 _num_clients--;
982 /* Demote a client to be the last in the list */
984 void winbindd_demote_client(struct winbindd_cli_state *cli)
986 struct winbindd_cli_state *tmp;
987 DLIST_DEMOTE(_client_list, cli, tmp);
990 /* Close all open clients */
992 void winbindd_kill_all_clients(void)
994 struct winbindd_cli_state *cl = winbindd_client_list();
996 DEBUG(10, ("winbindd_kill_all_clients: going postal\n"));
998 while (cl) {
999 struct winbindd_cli_state *next;
1001 next = cl->next;
1002 winbindd_remove_client(cl);
1003 cl = next;
1007 /* Return number of open clients */
1009 int winbindd_num_clients(void)
1011 return _num_clients;
1014 /*****************************************************************************
1015 For idmap conversion: convert one record to new format
1016 Ancient versions (eg 2.2.3a) of winbindd_idmap.tdb mapped DOMAINNAME/rid
1017 instead of the SID.
1018 *****************************************************************************/
1019 static int convert_fn(TDB_CONTEXT *tdb, TDB_DATA key, TDB_DATA data, void *state)
1021 struct winbindd_domain *domain;
1022 char *p;
1023 DOM_SID sid;
1024 uint32 rid;
1025 fstring keystr;
1026 fstring dom_name;
1027 TDB_DATA key2;
1028 BOOL *failed = (BOOL *)state;
1030 DEBUG(10,("Converting %s\n", key.dptr));
1032 p = strchr(key.dptr, '/');
1033 if (!p)
1034 return 0;
1036 *p = 0;
1037 fstrcpy(dom_name, key.dptr);
1038 *p++ = '/';
1040 domain = find_domain_from_name(dom_name);
1041 if (domain == NULL) {
1042 /* We must delete the old record. */
1043 DEBUG(0,("Unable to find domain %s\n", dom_name ));
1044 DEBUG(0,("deleting record %s\n", key.dptr ));
1046 if (tdb_delete(tdb, key) != 0) {
1047 DEBUG(0, ("Unable to delete record %s\n", key.dptr));
1048 *failed = True;
1049 return -1;
1052 return 0;
1055 rid = atoi(p);
1057 sid_copy(&sid, &domain->sid);
1058 sid_append_rid(&sid, rid);
1060 sid_to_string(keystr, &sid);
1061 key2.dptr = keystr;
1062 key2.dsize = strlen(keystr) + 1;
1064 if (tdb_store(tdb, key2, data, TDB_INSERT) != 0) {
1065 DEBUG(0,("Unable to add record %s\n", key2.dptr ));
1066 *failed = True;
1067 return -1;
1070 if (tdb_store(tdb, data, key2, TDB_REPLACE) != 0) {
1071 DEBUG(0,("Unable to update record %s\n", data.dptr ));
1072 *failed = True;
1073 return -1;
1076 if (tdb_delete(tdb, key) != 0) {
1077 DEBUG(0,("Unable to delete record %s\n", key.dptr ));
1078 *failed = True;
1079 return -1;
1082 return 0;
1085 /* These definitions are from sam/idmap_tdb.c. Replicated here just
1086 out of laziness.... :-( */
1088 /* High water mark keys */
1089 #define HWM_GROUP "GROUP HWM"
1090 #define HWM_USER "USER HWM"
1092 /*****************************************************************************
1093 Convert the idmap database from an older version.
1094 *****************************************************************************/
1096 static BOOL idmap_convert(const char *idmap_name)
1098 int32 vers;
1099 BOOL bigendianheader;
1100 BOOL failed = False;
1101 TDB_CONTEXT *idmap_tdb;
1103 if (!(idmap_tdb = tdb_open_log(idmap_name, 0,
1104 TDB_DEFAULT, O_RDWR,
1105 0600))) {
1106 DEBUG(0, ("idmap_convert: Unable to open idmap database\n"));
1107 return False;
1110 bigendianheader = (idmap_tdb->flags & TDB_BIGENDIAN) ? True : False;
1112 vers = tdb_fetch_int32(idmap_tdb, "IDMAP_VERSION");
1114 if (((vers == -1) && bigendianheader) || (IREV(vers) == IDMAP_VERSION)) {
1115 /* Arrggghh ! Bytereversed or old big-endian - make order independent ! */
1117 * high and low records were created on a
1118 * big endian machine and will need byte-reversing.
1121 int32 wm;
1123 wm = tdb_fetch_int32(idmap_tdb, HWM_USER);
1125 if (wm != -1) {
1126 wm = IREV(wm);
1127 } else {
1128 wm = server_state.uid_low;
1131 if (tdb_store_int32(idmap_tdb, HWM_USER, wm) == -1) {
1132 DEBUG(0, ("idmap_convert: Unable to byteswap user hwm in idmap database\n"));
1133 tdb_close(idmap_tdb);
1134 return False;
1137 wm = tdb_fetch_int32(idmap_tdb, HWM_GROUP);
1138 if (wm != -1) {
1139 wm = IREV(wm);
1140 } else {
1141 wm = server_state.gid_low;
1144 if (tdb_store_int32(idmap_tdb, HWM_GROUP, wm) == -1) {
1145 DEBUG(0, ("idmap_convert: Unable to byteswap group hwm in idmap database\n"));
1146 tdb_close(idmap_tdb);
1147 return False;
1151 /* the old format stored as DOMAIN/rid - now we store the SID direct */
1152 tdb_traverse(idmap_tdb, convert_fn, &failed);
1154 if (failed) {
1155 DEBUG(0, ("Problem during conversion\n"));
1156 tdb_close(idmap_tdb);
1157 return False;
1160 if (tdb_store_int32(idmap_tdb, "IDMAP_VERSION", IDMAP_VERSION) == -1) {
1161 DEBUG(0, ("idmap_convert: Unable to dtore idmap version in databse\n"));
1162 tdb_close(idmap_tdb);
1163 return False;
1166 tdb_close(idmap_tdb);
1167 return True;
1170 /*****************************************************************************
1171 Convert the idmap database from an older version if necessary
1172 *****************************************************************************/
1174 BOOL winbindd_upgrade_idmap(void)
1176 pstring idmap_name;
1177 pstring backup_name;
1178 SMB_STRUCT_STAT stbuf;
1179 TDB_CONTEXT *idmap_tdb;
1181 pstrcpy(idmap_name, lock_path("winbindd_idmap.tdb"));
1183 if (!file_exist(idmap_name, &stbuf)) {
1184 /* nothing to convert return */
1185 return True;
1188 if (!(idmap_tdb = tdb_open_log(idmap_name, 0,
1189 TDB_DEFAULT, O_RDWR,
1190 0600))) {
1191 DEBUG(0, ("idmap_convert: Unable to open idmap database\n"));
1192 return False;
1195 if (tdb_fetch_int32(idmap_tdb, "IDMAP_VERSION") == IDMAP_VERSION) {
1196 /* nothing to convert return */
1197 tdb_close(idmap_tdb);
1198 return True;
1201 /* backup_tdb expects the tdb not to be open */
1202 tdb_close(idmap_tdb);
1204 DEBUG(0, ("Upgrading winbindd_idmap.tdb from an old version\n"));
1206 pstrcpy(backup_name, idmap_name);
1207 pstrcat(backup_name, ".bak");
1209 if (backup_tdb(idmap_name, backup_name) != 0) {
1210 DEBUG(0, ("Could not backup idmap database\n"));
1211 return False;
1214 return idmap_convert(idmap_name);