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.
28 #define DBGC_CLASS DBGC_WINBIND
31 * @file winbindd_util.c
33 * Winbind daemon for NT domain authentication nss module.
38 * Used to clobber name fields that have an undefined value.
40 * Correct code should never look at a field that has this value.
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
;
53 When was the last scan of trusted domains done?
58 static time_t last_trustdom_scan
;
60 struct winbindd_domain
*domain_list(void)
70 /* Free all entries in the trusted domain list */
72 void free_domain_list(void)
74 struct winbindd_domain
*domain
= _domain_list
;
77 struct winbindd_domain
*next
= domain
->next
;
79 DLIST_REMOVE(_domain_list
, domain
);
85 static BOOL
is_internal_domain(const DOM_SID
*sid
)
90 return (sid_check_is_domain(sid
) || sid_check_is_builtin(sid
));
94 /* Add a trusted domain to our list of domains */
95 static struct winbindd_domain
*add_trusted_domain(const char *domain_name
, const char *alt_name
,
96 struct winbindd_methods
*methods
,
99 struct winbindd_domain
*domain
;
100 const char *alternative_name
= NULL
;
101 static const DOM_SID null_sid
;
103 /* ignore alt_name if we are not in an AD domain */
105 if ( (lp_security() == SEC_ADS
) && alt_name
&& *alt_name
) {
106 alternative_name
= alt_name
;
109 /* We can't call domain_list() as this function is called from
110 init_domain_list() and we'll get stuck in a loop. */
111 for (domain
= _domain_list
; domain
; domain
= domain
->next
) {
112 if (strequal(domain_name
, domain
->name
) ||
113 strequal(domain_name
, domain
->alt_name
)) {
116 if (alternative_name
&& *alternative_name
) {
117 if (strequal(alternative_name
, domain
->name
) ||
118 strequal(alternative_name
, domain
->alt_name
)) {
123 if (sid_equal(sid
, &null_sid
) ) {
125 } else if (sid_equal(sid
, &domain
->sid
)) {
131 /* Create new domain entry */
133 if ((domain
= SMB_MALLOC_P(struct winbindd_domain
)) == NULL
)
138 ZERO_STRUCTP(domain
);
140 /* prioritise the short name */
141 if (strchr_m(domain_name
, '.') && alternative_name
&& *alternative_name
) {
142 fstrcpy(domain
->name
, alternative_name
);
143 fstrcpy(domain
->alt_name
, domain_name
);
145 fstrcpy(domain
->name
, domain_name
);
146 if (alternative_name
) {
147 fstrcpy(domain
->alt_name
, alternative_name
);
151 domain
->methods
= methods
;
152 domain
->backend
= NULL
;
153 domain
->internal
= is_internal_domain(sid
);
154 domain
->sequence_number
= DOM_SEQUENCE_NONE
;
155 domain
->last_seq_check
= 0;
156 domain
->initialized
= False
;
158 sid_copy(&domain
->sid
, sid
);
161 /* Link to domain list */
162 DLIST_ADD(_domain_list
, domain
);
164 DEBUG(2,("Added domain %s %s %s\n",
165 domain
->name
, domain
->alt_name
,
166 &domain
->sid
?sid_string_static(&domain
->sid
):""));
171 /********************************************************************
172 rescan our domains looking for new trusted domains
173 ********************************************************************/
175 struct trustdom_state
{
177 struct winbindd_response
*response
;
180 static void trustdom_recv(void *private, BOOL success
);
182 static void add_trusted_domains( struct winbindd_domain
*domain
)
185 struct winbindd_request
*request
;
186 struct winbindd_response
*response
;
188 struct trustdom_state
*state
;
190 mem_ctx
= talloc_init("add_trusted_domains");
191 if (mem_ctx
== NULL
) {
192 DEBUG(0, ("talloc_init failed\n"));
196 request
= TALLOC_ZERO_P(mem_ctx
, struct winbindd_request
);
197 response
= TALLOC_P(mem_ctx
, struct winbindd_response
);
198 state
= TALLOC_P(mem_ctx
, struct trustdom_state
);
200 if ((request
== NULL
) || (response
== NULL
) || (state
== NULL
)) {
201 DEBUG(0, ("talloc failed\n"));
202 talloc_destroy(mem_ctx
);
206 state
->mem_ctx
= mem_ctx
;
207 state
->response
= response
;
209 request
->length
= sizeof(*request
);
210 request
->cmd
= WINBINDD_LIST_TRUSTDOM
;
212 async_domain_request(mem_ctx
, domain
, request
, response
,
213 trustdom_recv
, state
);
216 static void trustdom_recv(void *private, BOOL success
)
218 extern struct winbindd_methods cache_methods
;
219 struct trustdom_state
*state
=
220 talloc_get_type_abort(private, struct trustdom_state
);
221 struct winbindd_response
*response
= state
->response
;
224 if ((!success
) || (response
->result
!= WINBINDD_OK
)) {
225 DEBUG(1, ("Could not receive trustdoms\n"));
226 talloc_destroy(state
->mem_ctx
);
230 p
= response
->extra_data
;
232 while ((p
!= NULL
) && (*p
!= '\0')) {
233 char *q
, *sidstr
, *alt_name
;
236 alt_name
= strchr(p
, '\\');
237 if (alt_name
== NULL
) {
238 DEBUG(0, ("Got invalid trustdom response\n"));
245 sidstr
= strchr(alt_name
, '\\');
246 if (sidstr
== NULL
) {
247 DEBUG(0, ("Got invalid trustdom response\n"));
254 q
= strchr(sidstr
, '\n');
258 if (!string_to_sid(&sid
, sidstr
)) {
259 DEBUG(0, ("Got invalid trustdom response\n"));
263 if (find_domain_from_name_noinit(p
) == NULL
) {
264 struct winbindd_domain
*domain
;
265 char *alternate_name
= NULL
;
267 /* use the real alt_name if we have one, else pass in NULL */
269 if ( !strequal( alt_name
, "(null)" ) )
270 alternate_name
= alt_name
;
272 domain
= add_trusted_domain(p
, alternate_name
,
275 setup_domain_child(domain
, &domain
->child
, NULL
);
282 SAFE_FREE(response
->extra_data
);
283 talloc_destroy(state
->mem_ctx
);
286 /********************************************************************
287 Periodically we need to refresh the trusted domain cache for smbd
288 ********************************************************************/
290 void rescan_trusted_domains( void )
292 time_t now
= time(NULL
);
294 /* see if the time has come... */
296 if ((now
>= last_trustdom_scan
) &&
297 ((now
-last_trustdom_scan
) < WINBINDD_RESCAN_FREQ
) )
300 /* this will only add new domains we didn't already know about */
302 add_trusted_domains( find_our_domain() );
304 last_trustdom_scan
= now
;
309 struct init_child_state
{
311 struct winbindd_domain
*domain
;
312 struct winbindd_request
*request
;
313 struct winbindd_response
*response
;
314 void (*continuation
)(void *private, BOOL success
);
318 static void init_child_recv(void *private, BOOL success
);
319 static void init_child_getdc_recv(void *private, BOOL success
);
321 enum winbindd_result
init_child_connection(struct winbindd_domain
*domain
,
322 void (*continuation
)(void *private,
327 struct winbindd_request
*request
;
328 struct winbindd_response
*response
;
329 struct init_child_state
*state
;
331 mem_ctx
= talloc_init("init_child_connection");
332 if (mem_ctx
== NULL
) {
333 DEBUG(0, ("talloc_init failed\n"));
334 return WINBINDD_ERROR
;
337 request
= TALLOC_ZERO_P(mem_ctx
, struct winbindd_request
);
338 response
= TALLOC_P(mem_ctx
, struct winbindd_response
);
339 state
= TALLOC_P(mem_ctx
, struct init_child_state
);
341 if ((request
== NULL
) || (response
== NULL
) || (state
== NULL
)) {
342 DEBUG(0, ("talloc failed\n"));
343 continuation(private, False
);
344 return WINBINDD_ERROR
;
347 request
->length
= sizeof(*request
);
349 state
->mem_ctx
= mem_ctx
;
350 state
->domain
= domain
;
351 state
->request
= request
;
352 state
->response
= response
;
353 state
->continuation
= continuation
;
354 state
->private = private;
356 if (domain
->primary
) {
357 /* The primary domain has to find the DC name itself */
358 request
->cmd
= WINBINDD_INIT_CONNECTION
;
359 fstrcpy(request
->domain_name
, domain
->name
);
360 request
->data
.init_conn
.is_primary
= True
;
361 fstrcpy(request
->data
.init_conn
.dcname
, "");
363 async_request(mem_ctx
, &domain
->child
, request
, response
,
364 init_child_recv
, state
);
365 return WINBINDD_PENDING
;
368 /* This is *not* the primary domain, let's ask our DC about a DC
371 request
->cmd
= WINBINDD_GETDCNAME
;
372 fstrcpy(request
->domain_name
, domain
->name
);
374 async_domain_request(mem_ctx
, find_our_domain(), request
, response
,
375 init_child_getdc_recv
, state
);
376 return WINBINDD_PENDING
;
379 static void init_child_getdc_recv(void *private, BOOL success
)
381 struct init_child_state
*state
=
382 talloc_get_type_abort(private, struct init_child_state
);
383 const char *dcname
= "";
385 DEBUG(10, ("Received getdcname response\n"));
387 if (success
&& (state
->response
->result
== WINBINDD_OK
)) {
388 dcname
= state
->response
->data
.dc_name
;
391 state
->request
->cmd
= WINBINDD_INIT_CONNECTION
;
392 fstrcpy(state
->request
->domain_name
, state
->domain
->name
);
393 state
->request
->data
.init_conn
.is_primary
= False
;
394 fstrcpy(state
->request
->data
.init_conn
.dcname
, dcname
);
396 async_request(state
->mem_ctx
, &state
->domain
->child
,
397 state
->request
, state
->response
,
398 init_child_recv
, state
);
401 static void init_child_recv(void *private, BOOL success
)
403 struct init_child_state
*state
=
404 talloc_get_type_abort(private, struct init_child_state
);
406 DEBUG(5, ("Received child initialization response for domain %s\n",
407 state
->domain
->name
));
409 if ((!success
) || (state
->response
->result
!= WINBINDD_OK
)) {
410 DEBUG(3, ("Could not init child\n"));
411 state
->continuation(state
->private, False
);
412 talloc_destroy(state
->mem_ctx
);
416 fstrcpy(state
->domain
->name
,
417 state
->response
->data
.domain_info
.name
);
418 fstrcpy(state
->domain
->alt_name
,
419 state
->response
->data
.domain_info
.alt_name
);
420 string_to_sid(&state
->domain
->sid
,
421 state
->response
->data
.domain_info
.sid
);
422 state
->domain
->native_mode
=
423 state
->response
->data
.domain_info
.native_mode
;
424 state
->domain
->active_directory
=
425 state
->response
->data
.domain_info
.active_directory
;
426 state
->domain
->sequence_number
=
427 state
->response
->data
.domain_info
.sequence_number
;
429 state
->domain
->initialized
= 1;
431 if (state
->continuation
!= NULL
)
432 state
->continuation(state
->private, True
);
433 talloc_destroy(state
->mem_ctx
);
436 enum winbindd_result
winbindd_dual_init_connection(struct winbindd_domain
*domain
,
437 struct winbindd_cli_state
*state
)
439 struct in_addr ipaddr
;
441 /* Ensure null termination */
442 state
->request
.domain_name
443 [sizeof(state
->request
.domain_name
)-1]='\0';
444 state
->request
.data
.init_conn
.dcname
445 [sizeof(state
->request
.data
.init_conn
.dcname
)-1]='\0';
447 fstrcpy(domain
->dcname
, state
->request
.data
.init_conn
.dcname
);
449 if (strlen(domain
->dcname
) > 0) {
450 if (!resolve_name(domain
->dcname
, &ipaddr
, 0x20)) {
451 DEBUG(2, ("Could not resolve DC name %s for domain %s\n",
452 domain
->dcname
, domain
->name
));
453 return WINBINDD_ERROR
;
456 domain
->dcaddr
.sin_family
= PF_INET
;
457 putip((char *)&(domain
->dcaddr
.sin_addr
), (char *)&ipaddr
);
458 domain
->dcaddr
.sin_port
= 0;
461 set_dc_type_and_flags(domain
);
463 if (!domain
->initialized
) {
464 DEBUG(1, ("Could not initialize domain %s\n",
465 state
->request
.domain_name
));
466 return WINBINDD_ERROR
;
469 fstrcpy(state
->response
.data
.domain_info
.name
, domain
->name
);
470 fstrcpy(state
->response
.data
.domain_info
.alt_name
, domain
->alt_name
);
471 fstrcpy(state
->response
.data
.domain_info
.sid
,
472 sid_string_static(&domain
->sid
));
474 state
->response
.data
.domain_info
.native_mode
475 = domain
->native_mode
;
476 state
->response
.data
.domain_info
.active_directory
477 = domain
->active_directory
;
478 state
->response
.data
.domain_info
.primary
480 state
->response
.data
.domain_info
.sequence_number
=
481 domain
->sequence_number
;
486 /* Look up global info for the winbind daemon */
487 void init_domain_list(void)
489 extern struct winbindd_methods cache_methods
;
490 extern struct winbindd_methods passdb_methods
;
491 struct winbindd_domain
*domain
;
493 /* Free existing list */
496 /* Add ourselves as the first entry. */
499 domain
= add_trusted_domain(get_global_sam_name(), NULL
,
501 get_global_sam_sid());
506 if (!secrets_fetch_domain_sid(lp_workgroup(), &our_sid
)) {
507 DEBUG(0, ("Could not fetch our SID - did we join?\n"));
510 domain
= add_trusted_domain( lp_workgroup(), lp_realm(),
511 &cache_methods
, &our_sid
);
514 domain
->primary
= True
;
515 setup_domain_child(domain
, &domain
->child
, NULL
);
517 /* Add our local SAM domains */
519 domain
= add_trusted_domain("BUILTIN", NULL
, &passdb_methods
,
520 &global_sid_Builtin
);
521 setup_domain_child(domain
, &domain
->child
, NULL
);
524 domain
= add_trusted_domain(get_global_sam_name(), NULL
,
526 get_global_sam_sid());
527 setup_domain_child(domain
, &domain
->child
, NULL
);
532 * Given a domain name, return the struct winbindd domain info for it
534 * @note Do *not* pass lp_workgroup() to this function. domain_list
535 * may modify it's value, and free that pointer. Instead, our local
536 * domain may be found by calling find_our_domain().
540 * @return The domain structure for the named domain, if it is working.
543 struct winbindd_domain
*find_domain_from_name_noinit(const char *domain_name
)
545 struct winbindd_domain
*domain
;
547 /* Search through list */
549 for (domain
= domain_list(); domain
!= NULL
; domain
= domain
->next
) {
550 if (strequal(domain_name
, domain
->name
) ||
551 (domain
->alt_name
[0] &&
552 strequal(domain_name
, domain
->alt_name
))) {
562 struct winbindd_domain
*find_domain_from_name(const char *domain_name
)
564 struct winbindd_domain
*domain
;
566 domain
= find_domain_from_name_noinit(domain_name
);
571 if (!domain
->initialized
)
572 set_dc_type_and_flags(domain
);
577 /* Given a domain sid, return the struct winbindd domain info for it */
579 struct winbindd_domain
*find_domain_from_sid_noinit(const DOM_SID
*sid
)
581 struct winbindd_domain
*domain
;
583 /* Search through list */
585 for (domain
= domain_list(); domain
!= NULL
; domain
= domain
->next
) {
586 if (sid_compare_domain(sid
, &domain
->sid
) == 0)
595 /* Given a domain sid, return the struct winbindd domain info for it */
597 struct winbindd_domain
*find_domain_from_sid(const DOM_SID
*sid
)
599 struct winbindd_domain
*domain
;
601 domain
= find_domain_from_sid_noinit(sid
);
606 if (!domain
->initialized
)
607 set_dc_type_and_flags(domain
);
612 struct winbindd_domain
*find_our_domain(void)
614 struct winbindd_domain
*domain
;
616 /* Search through list */
618 for (domain
= domain_list(); domain
!= NULL
; domain
= domain
->next
) {
623 smb_panic("Could not find our domain\n");
627 struct winbindd_domain
*find_builtin_domain(void)
630 struct winbindd_domain
*domain
;
632 string_to_sid(&sid
, "S-1-5-32");
633 domain
= find_domain_from_sid(&sid
);
636 smb_panic("Could not find BUILTIN domain\n");
641 /* Find the appropriate domain to lookup a name or SID */
643 struct winbindd_domain
*find_lookup_domain_from_sid(const DOM_SID
*sid
)
645 /* A DC can't ask the local smbd for remote SIDs, here winbindd is the
646 * one to contact the external DC's. On member servers the internal
647 * domains are different: These are part of the local SAM. */
649 if (IS_DC
|| is_internal_domain(sid
))
650 return find_domain_from_sid(sid
);
652 /* On a member server a query for SID or name can always go to our
655 return find_our_domain();
658 struct winbindd_domain
*find_lookup_domain_from_name(const char *domain_name
)
660 if (IS_DC
|| strequal(domain_name
, "BUILTIN") ||
661 strequal(domain_name
, get_global_sam_name()))
662 return find_domain_from_name_noinit(domain_name
);
664 return find_our_domain();
667 /* Lookup a sid in a domain from a name */
669 BOOL
winbindd_lookup_sid_by_name(TALLOC_CTX
*mem_ctx
,
670 struct winbindd_domain
*domain
,
671 const char *domain_name
,
672 const char *name
, DOM_SID
*sid
,
673 enum SID_NAME_USE
*type
)
678 result
= domain
->methods
->name_to_sid(domain
, mem_ctx
, domain_name
, name
, sid
, type
);
680 /* Return rid and type if lookup successful */
681 if (!NT_STATUS_IS_OK(result
)) {
682 *type
= SID_NAME_UNKNOWN
;
685 return NT_STATUS_IS_OK(result
);
689 * @brief Lookup a name in a domain from a sid.
691 * @param sid Security ID you want to look up.
692 * @param name On success, set to the name corresponding to @p sid.
693 * @param dom_name On success, set to the 'domain name' corresponding to @p sid.
694 * @param type On success, contains the type of name: alias, group or
696 * @retval True if the name exists, in which case @p name and @p type
697 * are set, otherwise False.
699 BOOL
winbindd_lookup_name_by_sid(TALLOC_CTX
*mem_ctx
,
703 enum SID_NAME_USE
*type
)
709 struct winbindd_domain
*domain
;
711 domain
= find_lookup_domain_from_sid(sid
);
714 DEBUG(1,("Can't find domain from sid\n"));
720 result
= domain
->methods
->sid_to_name(domain
, mem_ctx
, sid
, &dom_names
, &names
, type
);
722 /* Return name and type if successful */
724 if ((rv
= NT_STATUS_IS_OK(result
))) {
725 fstrcpy(dom_name
, dom_names
);
726 fstrcpy(name
, names
);
728 *type
= SID_NAME_UNKNOWN
;
729 fstrcpy(name
, name_deadbeef
);
735 /* Free state information held for {set,get,end}{pw,gr}ent() functions */
737 void free_getent_state(struct getent_state
*state
)
739 struct getent_state
*temp
;
741 /* Iterate over state list */
745 while(temp
!= NULL
) {
746 struct getent_state
*next
;
748 /* Free sam entries then list entry */
750 SAFE_FREE(state
->sam_entries
);
751 DLIST_REMOVE(state
, state
);
759 /* Parse winbindd related parameters */
761 BOOL
winbindd_param_init(void)
763 /* Parse winbind uid and winbind_gid parameters */
765 if (!lp_idmap_uid(&server_state
.uid_low
, &server_state
.uid_high
)) {
766 DEBUG(0, ("winbindd: idmap uid range missing or invalid\n"));
767 DEBUG(0, ("winbindd: cannot continue, exiting.\n"));
771 if (!lp_idmap_gid(&server_state
.gid_low
, &server_state
.gid_high
)) {
772 DEBUG(0, ("winbindd: idmap gid range missing or invalid\n"));
773 DEBUG(0, ("winbindd: cannot continue, exiting.\n"));
780 BOOL
is_in_uid_range(uid_t uid
)
782 return ((uid
>= server_state
.uid_low
) &&
783 (uid
<= server_state
.uid_high
));
786 BOOL
is_in_gid_range(gid_t gid
)
788 return ((gid
>= server_state
.gid_low
) &&
789 (gid
<= server_state
.gid_high
));
792 /* Is this a domain which we may assume no DOMAIN\ prefix? */
794 static BOOL
assume_domain(const char *domain
) {
795 if ((lp_winbind_use_default_domain()
796 || lp_winbind_trusted_domains_only()) &&
797 strequal(lp_workgroup(), domain
))
800 if (strequal(get_global_sam_name(), domain
))
806 /* Parse a string of the form DOMAIN/user into a domain and a user */
808 BOOL
parse_domain_user(const char *domuser
, fstring domain
, fstring user
)
810 char *p
= strchr(domuser
,*lp_winbind_separator());
813 fstrcpy(user
, domuser
);
815 if ( assume_domain(lp_workgroup())) {
816 fstrcpy(domain
, lp_workgroup());
818 fstrcpy( domain
, get_global_sam_name() );
823 fstrcpy(domain
, domuser
);
824 domain
[PTR_DIFF(p
, domuser
)] = 0;
832 BOOL
parse_domain_user_talloc(TALLOC_CTX
*mem_ctx
, const char *domuser
,
833 char **domain
, char **user
)
835 fstring fstr_domain
, fstr_user
;
836 parse_domain_user(domuser
, fstr_domain
, fstr_user
);
837 *domain
= talloc_strdup(mem_ctx
, fstr_domain
);
838 *user
= talloc_strdup(mem_ctx
, fstr_user
);
839 return ((*domain
!= NULL
) && (*user
!= NULL
));
843 Fill DOMAIN\\USERNAME entry accounting 'winbind use default domain' and
844 'winbind separator' options.
846 - omit DOMAIN when 'winbind use default domain = true' and DOMAIN is
849 If we are a PDC or BDC, and this is for our domain, do likewise.
851 Also, if omit DOMAIN if 'winbind trusted domains only = true', as the
852 username is then unqualified in unix
855 void fill_domain_username(fstring name
, const char *domain
, const char *user
)
859 fstrcpy(tmp_user
, user
);
861 if (assume_domain(domain
)) {
862 strlcpy(name
, user
, sizeof(fstring
));
864 slprintf(name
, sizeof(fstring
) - 1, "%s%c%s",
865 domain
, *lp_winbind_separator(),
871 * Winbindd socket accessor functions
874 char *get_winbind_priv_pipe_dir(void)
876 return lock_path(WINBINDD_PRIV_SOCKET_SUBDIR
);
879 /* Open the winbindd socket */
881 static int _winbindd_socket
= -1;
882 static int _winbindd_priv_socket
= -1;
884 int open_winbindd_socket(void)
886 if (_winbindd_socket
== -1) {
887 _winbindd_socket
= create_pipe_sock(
888 WINBINDD_SOCKET_DIR
, WINBINDD_SOCKET_NAME
, 0755);
889 DEBUG(10, ("open_winbindd_socket: opened socket fd %d\n",
893 return _winbindd_socket
;
896 int open_winbindd_priv_socket(void)
898 if (_winbindd_priv_socket
== -1) {
899 _winbindd_priv_socket
= create_pipe_sock(
900 get_winbind_priv_pipe_dir(), WINBINDD_SOCKET_NAME
, 0750);
901 DEBUG(10, ("open_winbindd_priv_socket: opened socket fd %d\n",
902 _winbindd_priv_socket
));
905 return _winbindd_priv_socket
;
908 /* Close the winbindd socket */
910 void close_winbindd_socket(void)
912 if (_winbindd_socket
!= -1) {
913 DEBUG(10, ("close_winbindd_socket: closing socket fd %d\n",
915 close(_winbindd_socket
);
916 _winbindd_socket
= -1;
918 if (_winbindd_priv_socket
!= -1) {
919 DEBUG(10, ("close_winbindd_socket: closing socket fd %d\n",
920 _winbindd_priv_socket
));
921 close(_winbindd_priv_socket
);
922 _winbindd_priv_socket
= -1;
927 * Client list accessor functions
930 static struct winbindd_cli_state
*_client_list
;
931 static int _num_clients
;
933 /* Return list of all connected clients */
935 struct winbindd_cli_state
*winbindd_client_list(void)
940 /* Add a connection to the list */
942 void winbindd_add_client(struct winbindd_cli_state
*cli
)
944 DLIST_ADD(_client_list
, cli
);
948 /* Remove a client from the list */
950 void winbindd_remove_client(struct winbindd_cli_state
*cli
)
952 DLIST_REMOVE(_client_list
, cli
);
956 /* Demote a client to be the last in the list */
958 void winbindd_demote_client(struct winbindd_cli_state
*cli
)
960 struct winbindd_cli_state
*tmp
;
961 DLIST_DEMOTE(_client_list
, cli
, tmp
);
964 /* Close all open clients */
966 void winbindd_kill_all_clients(void)
968 struct winbindd_cli_state
*cl
= winbindd_client_list();
970 DEBUG(10, ("winbindd_kill_all_clients: going postal\n"));
973 struct winbindd_cli_state
*next
;
976 winbindd_remove_client(cl
);
981 /* Return number of open clients */
983 int winbindd_num_clients(void)
988 /*****************************************************************************
989 For idmap conversion: convert one record to new format
990 Ancient versions (eg 2.2.3a) of winbindd_idmap.tdb mapped DOMAINNAME/rid
992 *****************************************************************************/
993 static int convert_fn(TDB_CONTEXT
*tdb
, TDB_DATA key
, TDB_DATA data
, void *state
)
995 struct winbindd_domain
*domain
;
1002 BOOL
*failed
= (BOOL
*)state
;
1004 DEBUG(10,("Converting %s\n", key
.dptr
));
1006 p
= strchr(key
.dptr
, '/');
1011 fstrcpy(dom_name
, key
.dptr
);
1014 domain
= find_domain_from_name(dom_name
);
1015 if (domain
== NULL
) {
1016 /* We must delete the old record. */
1017 DEBUG(0,("Unable to find domain %s\n", dom_name
));
1018 DEBUG(0,("deleting record %s\n", key
.dptr
));
1020 if (tdb_delete(tdb
, key
) != 0) {
1021 DEBUG(0, ("Unable to delete record %s\n", key
.dptr
));
1031 sid_copy(&sid
, &domain
->sid
);
1032 sid_append_rid(&sid
, rid
);
1034 sid_to_string(keystr
, &sid
);
1036 key2
.dsize
= strlen(keystr
) + 1;
1038 if (tdb_store(tdb
, key2
, data
, TDB_INSERT
) != 0) {
1039 DEBUG(0,("Unable to add record %s\n", key2
.dptr
));
1044 if (tdb_store(tdb
, data
, key2
, TDB_REPLACE
) != 0) {
1045 DEBUG(0,("Unable to update record %s\n", data
.dptr
));
1050 if (tdb_delete(tdb
, key
) != 0) {
1051 DEBUG(0,("Unable to delete record %s\n", key
.dptr
));
1059 /* These definitions are from sam/idmap_tdb.c. Replicated here just
1060 out of laziness.... :-( */
1062 /* High water mark keys */
1063 #define HWM_GROUP "GROUP HWM"
1064 #define HWM_USER "USER HWM"
1066 /* idmap version determines auto-conversion */
1067 #define IDMAP_VERSION 2
1070 /*****************************************************************************
1071 Convert the idmap database from an older version.
1072 *****************************************************************************/
1074 static BOOL
idmap_convert(const char *idmap_name
)
1077 BOOL bigendianheader
;
1078 BOOL failed
= False
;
1079 TDB_CONTEXT
*idmap_tdb
;
1081 if (!(idmap_tdb
= tdb_open_log(idmap_name
, 0,
1082 TDB_DEFAULT
, O_RDWR
,
1084 DEBUG(0, ("idmap_convert: Unable to open idmap database\n"));
1088 bigendianheader
= (idmap_tdb
->flags
& TDB_BIGENDIAN
) ? True
: False
;
1090 vers
= tdb_fetch_int32(idmap_tdb
, "IDMAP_VERSION");
1092 if (((vers
== -1) && bigendianheader
) || (IREV(vers
) == IDMAP_VERSION
)) {
1093 /* Arrggghh ! Bytereversed or old big-endian - make order independent ! */
1095 * high and low records were created on a
1096 * big endian machine and will need byte-reversing.
1101 wm
= tdb_fetch_int32(idmap_tdb
, HWM_USER
);
1106 wm
= server_state
.uid_low
;
1109 if (tdb_store_int32(idmap_tdb
, HWM_USER
, wm
) == -1) {
1110 DEBUG(0, ("idmap_convert: Unable to byteswap user hwm in idmap database\n"));
1111 tdb_close(idmap_tdb
);
1115 wm
= tdb_fetch_int32(idmap_tdb
, HWM_GROUP
);
1119 wm
= server_state
.gid_low
;
1122 if (tdb_store_int32(idmap_tdb
, HWM_GROUP
, wm
) == -1) {
1123 DEBUG(0, ("idmap_convert: Unable to byteswap group hwm in idmap database\n"));
1124 tdb_close(idmap_tdb
);
1129 /* the old format stored as DOMAIN/rid - now we store the SID direct */
1130 tdb_traverse(idmap_tdb
, convert_fn
, &failed
);
1133 DEBUG(0, ("Problem during conversion\n"));
1134 tdb_close(idmap_tdb
);
1138 if (tdb_store_int32(idmap_tdb
, "IDMAP_VERSION", IDMAP_VERSION
) == -1) {
1139 DEBUG(0, ("idmap_convert: Unable to dtore idmap version in databse\n"));
1140 tdb_close(idmap_tdb
);
1144 tdb_close(idmap_tdb
);
1148 /*****************************************************************************
1149 Convert the idmap database from an older version if necessary
1150 *****************************************************************************/
1152 BOOL
winbindd_upgrade_idmap(void)
1155 pstring backup_name
;
1156 SMB_STRUCT_STAT stbuf
;
1157 TDB_CONTEXT
*idmap_tdb
;
1159 pstrcpy(idmap_name
, lock_path("winbindd_idmap.tdb"));
1161 if (!file_exist(idmap_name
, &stbuf
)) {
1162 /* nothing to convert return */
1166 if (!(idmap_tdb
= tdb_open_log(idmap_name
, 0,
1167 TDB_DEFAULT
, O_RDWR
,
1169 DEBUG(0, ("idmap_convert: Unable to open idmap database\n"));
1173 if (tdb_fetch_int32(idmap_tdb
, "IDMAP_VERSION") == IDMAP_VERSION
) {
1174 /* nothing to convert return */
1175 tdb_close(idmap_tdb
);
1179 /* backup_tdb expects the tdb not to be open */
1180 tdb_close(idmap_tdb
);
1182 DEBUG(0, ("Upgrading winbindd_idmap.tdb from an old version\n"));
1184 pstrcpy(backup_name
, idmap_name
);
1185 pstrcat(backup_name
, ".bak");
1187 if (backup_tdb(idmap_name
, backup_name
) != 0) {
1188 DEBUG(0, ("Could not backup idmap database\n"));
1192 return idmap_convert(idmap_name
);