2000-05-09 15:43:00 +04:00
/*
2002-01-30 09:08:46 +03:00
Unix SMB / CIFS implementation .
2000-05-09 15:43:00 +04:00
2001-05-07 08:32:40 +04:00
Winbind daemon - user related functions
2000-05-09 15:43:00 +04:00
Copyright ( C ) Tim Potter 2000
2001-11-28 01:39:57 +03:00
Copyright ( C ) Jeremy Allison 2001.
2003-06-30 21:24:59 +04:00
Copyright ( C ) Gerald ( Jerry ) Carter 2003.
2000-05-09 15:43:00 +04:00
This program is free software ; you can redistribute it and / or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation ; either version 2 of the License , or
( at your option ) any later version .
This program is distributed in the hope that it will be useful ,
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU General Public License for more details .
You should have received a copy of the GNU General Public License
along with this program ; if not , write to the Free Software
Foundation , Inc . , 675 Mass Ave , Cambridge , MA 0213 9 , USA .
*/
# include "winbindd.h"
2002-07-15 14:35:28 +04:00
# undef DBGC_CLASS
# define DBGC_CLASS DBGC_WINBIND
2003-06-16 22:37:54 +04:00
extern userdom_struct current_user_info ;
2000-05-09 15:43:00 +04:00
/* Fill a pwent structure with information we have obtained */
2002-01-20 04:24:59 +03:00
static BOOL winbindd_fill_pwent ( char * dom_name , char * user_name ,
2003-04-23 15:54:56 +04:00
DOM_SID * user_sid , DOM_SID * group_sid ,
2001-11-27 23:57:14 +03:00
char * full_name , struct winbindd_pw * pw )
2000-05-09 15:43:00 +04:00
{
2002-01-20 04:24:59 +03:00
fstring output_username ;
2001-05-07 08:32:40 +04:00
pstring homedir ;
2003-04-23 15:54:56 +04:00
fstring sid_string ;
2001-05-07 08:32:40 +04:00
2002-01-20 04:24:59 +03:00
if ( ! pw | | ! dom_name | | ! user_name )
2001-05-07 08:32:40 +04:00
return False ;
/* Resolve the uid number */
2003-05-12 22:12:31 +04:00
2003-07-07 09:11:10 +04:00
if ( ! NT_STATUS_IS_OK ( idmap_sid_to_uid ( user_sid , & ( pw - > pw_uid ) , 0 ) ) ) {
2003-04-23 15:54:56 +04:00
DEBUG ( 1 , ( " error getting user id for sid %s \n " , sid_to_string ( sid_string , user_sid ) ) ) ;
2001-05-07 08:32:40 +04:00
return False ;
}
/* Resolve the gid number */
2003-05-12 22:12:31 +04:00
2003-07-07 09:11:10 +04:00
if ( ! NT_STATUS_IS_OK ( idmap_sid_to_gid ( group_sid , & ( pw - > pw_gid ) , 0 ) ) ) {
2003-04-23 15:54:56 +04:00
DEBUG ( 1 , ( " error getting group id for sid %s \n " , sid_to_string ( sid_string , group_sid ) ) ) ;
2001-05-07 08:32:40 +04:00
return False ;
}
/* Username */
2002-01-20 04:24:59 +03:00
fill_domain_username ( output_username , dom_name , user_name ) ;
safe_strcpy ( pw - > pw_name , output_username , sizeof ( pw - > pw_name ) - 1 ) ;
2001-05-07 08:32:40 +04:00
/* Full name (gecos) */
safe_strcpy ( pw - > pw_gecos , full_name , sizeof ( pw - > pw_gecos ) - 1 ) ;
2001-10-29 07:50:17 +03:00
2001-05-07 08:32:40 +04:00
/* Home directory and shell - use template config parameters. The
defaults are / tmp for the home directory and / bin / false for
shell . */
2001-05-08 09:58:03 +04:00
/* The substitution of %U and %D in the 'template homedir' is done
by lp_string ( ) calling standard_sub_basic ( ) . */
2001-05-07 08:32:40 +04:00
2002-01-20 04:24:59 +03:00
fstrcpy ( current_user_info . smb_name , user_name ) ;
2003-02-12 04:08:40 +03:00
sub_set_smb_name ( user_name ) ;
2002-01-20 04:24:59 +03:00
fstrcpy ( current_user_info . domain , dom_name ) ;
2001-05-07 08:32:40 +04:00
2001-05-08 09:58:03 +04:00
pstrcpy ( homedir , lp_template_homedir ( ) ) ;
2001-05-07 08:32:40 +04:00
safe_strcpy ( pw - > pw_dir , homedir , sizeof ( pw - > pw_dir ) - 1 ) ;
safe_strcpy ( pw - > pw_shell , lp_template_shell ( ) ,
sizeof ( pw - > pw_shell ) - 1 ) ;
/* Password - set to "x" as we can't generate anything useful here.
2002-01-20 04:24:59 +03:00
Authentication can be done using the pam_winbind module . */
2001-05-07 08:32:40 +04:00
safe_strcpy ( pw - > pw_passwd , " x " , sizeof ( pw - > pw_passwd ) - 1 ) ;
return True ;
2000-05-09 15:43:00 +04:00
}
2001-12-10 04:05:50 +03:00
/* Return a password structure from a username. */
2000-05-09 15:43:00 +04:00
2002-01-10 09:20:03 +03:00
enum winbindd_result winbindd_getpwnam ( struct winbindd_cli_state * state )
2000-05-09 15:43:00 +04:00
{
2001-12-04 09:17:39 +03:00
WINBIND_USERINFO user_info ;
2003-07-09 20:44:47 +04:00
WINBINDD_PW * pw ;
2001-05-07 08:32:40 +04:00
DOM_SID user_sid ;
2001-12-04 09:17:39 +03:00
NTSTATUS status ;
2002-01-20 04:24:59 +03:00
fstring name_domain , name_user ;
2001-09-05 11:55:54 +04:00
enum SID_NAME_USE name_type ;
2001-11-21 12:59:15 +03:00
struct winbindd_domain * domain ;
TALLOC_CTX * mem_ctx ;
2001-05-07 08:32:40 +04:00
2002-08-17 21:00:51 +04:00
/* Ensure null termination */
state - > request . data . username [ sizeof ( state - > request . data . username ) - 1 ] = ' \0 ' ;
2003-07-22 08:31:20 +04:00
DEBUG ( 3 , ( " [%5lu]: getpwnam %s \n " , ( unsigned long ) state - > pid ,
2001-05-07 08:32:40 +04:00
state - > request . data . username ) ) ;
/* Parse domain and username */
2003-07-09 20:44:47 +04:00
parse_domain_user ( state - > request . data . username ,
name_domain , name_user ) ;
/* if this is our local domain (or no domain), the do a local tdb search */
2001-05-07 08:32:40 +04:00
2003-07-09 20:44:47 +04:00
if ( ! * name_domain | | strequal ( name_domain , get_global_sam_name ( ) ) ) {
if ( ! ( pw = wb_getpwnam ( name_user ) ) ) {
DEBUG ( 5 , ( " winbindd_getpwnam: lookup for %s \\ %s failed \n " ,
name_domain , name_user ) ) ;
return WINBINDD_ERROR ;
}
memcpy ( & state - > response . data . pw , pw , sizeof ( WINBINDD_PW ) ) ;
return WINBINDD_OK ;
}
Here's the code to make winbindd work on a Samba DC
to handle domain trusts. Jeremy and I talked about this
and it's going in as working code. It keeps winbind clean
and solves the trust problem with minimal changes.
To summarize, there are 2 basic cases where the deadlock would
occur. (1) lookuping up secondary groups for a user, and
(2) get[gr|pw]nam() calls that fall through the NSS layer because
they don't exist anywhere.
o To handle case #1, we bypass winbindd in sys_getgrouplist() unless
the username includes the 'winbind separator'.
o Case #2 is handled by adding checks in winbindd to return failure
if we are a DC and the domain matches our own.
This code has been tested using basic share connections, domain
logons, and with pam_winbind (both with and without 'winbind
use default domain'). The 'trustdomain' auth module should work
as well if an admin wants to manually create UNIX users for
acounts in the trusted domains.
Other misc fixes:
* we need to fix check_ntlm_password() to be able to determine
if an auth module is authoritative over a user (NT_STATUS_WRONG_PASSWORD,
etc...). I worked around my specific situation, but this needs to be
fixed. the winbindd auth module was causing delays.
* fix named server mutex deadlock between trust domain auth module
and winbindd looking up a uid
* make sure SAM_ACCOUNT gets stored in the server_info struct for the
_net_sam_logon() reply.
Configuration details:
The recommended method for supporting trusts is to use winbind.
The gets us around some of the server mutex issues as well.
* set 'files winbind' for passwd: and group: in /etc/nsswitch.conf
* create domain trusts like normal
* join winbind on the pdc to the Samba domain using 'net rpc join'
* add normal parameters to smb.conf for winbind
* set 'auth method = guest sam winbind'
* start smbd, nmbd, & winbindd
Problems that remain:
* join a Windows 2k/XP box to a Samba domain.
* create a 2-way trust between the Samba domain
and an NT domain
* logon to the windows client as a user from theh trusted
domain
* try to browse server in the trusted domain (or other
workstations). an NT client seems to work ok, but 2k
and XP either prompt for passwords or fail with errors.
apparanently this never got tested since no one has ever been
able to logon as a trusted user to a Samba domain from a Windows
client.
(This used to be commit f804b590f9dbf1f0147c06a0a2f12e221ae6fc3b)
2003-06-29 07:39:50 +04:00
2003-07-09 20:44:47 +04:00
/* should we deal with users for our domain? */
if ( lp_winbind_trusted_domains_only ( ) & & strequal ( name_domain , lp_workgroup ( ) ) ) {
DEBUG ( 7 , ( " winbindd_getpenam: My domain -- rejecting getpwnam() for %s \\ %s. \n " ,
Here's the code to make winbindd work on a Samba DC
to handle domain trusts. Jeremy and I talked about this
and it's going in as working code. It keeps winbind clean
and solves the trust problem with minimal changes.
To summarize, there are 2 basic cases where the deadlock would
occur. (1) lookuping up secondary groups for a user, and
(2) get[gr|pw]nam() calls that fall through the NSS layer because
they don't exist anywhere.
o To handle case #1, we bypass winbindd in sys_getgrouplist() unless
the username includes the 'winbind separator'.
o Case #2 is handled by adding checks in winbindd to return failure
if we are a DC and the domain matches our own.
This code has been tested using basic share connections, domain
logons, and with pam_winbind (both with and without 'winbind
use default domain'). The 'trustdomain' auth module should work
as well if an admin wants to manually create UNIX users for
acounts in the trusted domains.
Other misc fixes:
* we need to fix check_ntlm_password() to be able to determine
if an auth module is authoritative over a user (NT_STATUS_WRONG_PASSWORD,
etc...). I worked around my specific situation, but this needs to be
fixed. the winbindd auth module was causing delays.
* fix named server mutex deadlock between trust domain auth module
and winbindd looking up a uid
* make sure SAM_ACCOUNT gets stored in the server_info struct for the
_net_sam_logon() reply.
Configuration details:
The recommended method for supporting trusts is to use winbind.
The gets us around some of the server mutex issues as well.
* set 'files winbind' for passwd: and group: in /etc/nsswitch.conf
* create domain trusts like normal
* join winbind on the pdc to the Samba domain using 'net rpc join'
* add normal parameters to smb.conf for winbind
* set 'auth method = guest sam winbind'
* start smbd, nmbd, & winbindd
Problems that remain:
* join a Windows 2k/XP box to a Samba domain.
* create a 2-way trust between the Samba domain
and an NT domain
* logon to the windows client as a user from theh trusted
domain
* try to browse server in the trusted domain (or other
workstations). an NT client seems to work ok, but 2k
and XP either prompt for passwords or fail with errors.
apparanently this never got tested since no one has ever been
able to logon as a trusted user to a Samba domain from a Windows
client.
(This used to be commit f804b590f9dbf1f0147c06a0a2f12e221ae6fc3b)
2003-06-29 07:39:50 +04:00
name_domain , name_user ) ) ;
return WINBINDD_ERROR ;
}
2001-11-21 12:59:15 +03:00
if ( ( domain = find_domain_from_name ( name_domain ) ) = = NULL ) {
2002-01-10 09:20:03 +03:00
DEBUG ( 5 , ( " no such domain: %s \n " , name_domain ) ) ;
2001-11-21 12:59:15 +03:00
return WINBINDD_ERROR ;
}
2001-05-07 08:32:40 +04:00
2001-10-10 02:55:00 +04:00
/* Get rid and name type from name */
2001-05-07 08:32:40 +04:00
2002-01-26 12:55:38 +03:00
if ( ! winbindd_lookup_sid_by_name ( domain , name_user , & user_sid , & name_type ) ) {
2001-05-07 08:32:40 +04:00
DEBUG ( 1 , ( " user '%s' does not exist \n " , name_user ) ) ;
return WINBINDD_ERROR ;
}
if ( name_type ! = SID_NAME_USER ) {
DEBUG ( 1 , ( " name '%s' is not a user name: %d \n " , name_user ,
name_type ) ) ;
return WINBINDD_ERROR ;
}
/* Get some user info. Split the user rid from the sid obtained
from the winbind_lookup_by_name ( ) call and use it in a
winbind_lookup_userinfo ( ) */
2000-05-09 15:43:00 +04:00
2002-12-20 23:21:31 +03:00
if ( ! ( mem_ctx = talloc_init ( " winbindd_getpwnam([%s] \\ [%s]) " ,
2002-01-20 04:24:59 +03:00
name_domain , name_user ) ) ) {
2001-11-27 09:28:06 +03:00
DEBUG ( 1 , ( " out of memory \n " ) ) ;
return WINBINDD_ERROR ;
}
2001-10-29 07:50:17 +03:00
2003-04-23 15:54:56 +04:00
status = domain - > methods - > query_user ( domain , mem_ctx , & user_sid ,
2002-01-10 09:20:03 +03:00
& user_info ) ;
2001-12-04 09:17:39 +03:00
if ( ! NT_STATUS_IS_OK ( status ) ) {
2002-01-20 04:24:59 +03:00
DEBUG ( 1 , ( " error getting user info for user '[%s] \\ [%s]' \n " ,
name_domain , name_user ) ) ;
2001-12-04 09:17:39 +03:00
talloc_destroy ( mem_ctx ) ;
2001-05-07 08:32:40 +04:00
return WINBINDD_ERROR ;
}
2000-05-09 15:43:00 +04:00
2001-12-04 09:17:39 +03:00
/* Now take all this information and fill in a passwd structure */
2002-01-20 04:24:59 +03:00
if ( ! winbindd_fill_pwent ( name_domain , name_user ,
2003-04-23 15:54:56 +04:00
user_info . user_sid , user_info . group_sid ,
2002-01-10 09:20:03 +03:00
user_info . full_name ,
2001-05-07 08:32:40 +04:00
& state - > response . data . pw ) ) {
2001-12-04 09:17:39 +03:00
talloc_destroy ( mem_ctx ) ;
2001-05-07 08:32:40 +04:00
return WINBINDD_ERROR ;
}
2001-12-04 09:17:39 +03:00
talloc_destroy ( mem_ctx ) ;
2001-05-07 08:32:40 +04:00
return WINBINDD_OK ;
2000-05-09 15:43:00 +04:00
}
/* Return a password structure given a uid number */
2002-01-10 09:20:03 +03:00
enum winbindd_result winbindd_getpwuid ( struct winbindd_cli_state * state )
2000-05-09 15:43:00 +04:00
{
2001-05-07 08:32:40 +04:00
DOM_SID user_sid ;
struct winbindd_domain * domain ;
2003-07-09 20:44:47 +04:00
WINBINDD_PW * pw ;
2002-01-20 04:24:59 +03:00
fstring dom_name ;
2001-12-04 09:17:39 +03:00
fstring user_name ;
2001-05-07 08:32:40 +04:00
enum SID_NAME_USE name_type ;
2001-12-04 09:17:39 +03:00
WINBIND_USERINFO user_info ;
2001-11-27 23:57:14 +03:00
TALLOC_CTX * mem_ctx ;
2001-12-04 09:17:39 +03:00
NTSTATUS status ;
2003-05-12 22:12:31 +04:00
gid_t gid ;
2001-05-07 08:32:40 +04:00
/* Bug out if the uid isn't in the winbind range */
if ( ( state - > request . data . uid < server_state . uid_low ) | |
2001-10-19 05:46:43 +04:00
( state - > request . data . uid > server_state . uid_high ) )
2001-05-07 08:32:40 +04:00
return WINBINDD_ERROR ;
2003-07-22 10:52:39 +04:00
DEBUG ( 3 , ( " [%5lu]: getpwuid %lu \n " , ( unsigned long ) state - > pid ,
( unsigned long ) state - > request . data . uid ) ) ;
2003-07-09 20:44:47 +04:00
/* always try local tdb first */
if ( ( pw = wb_getpwuid ( state - > request . data . uid ) ) ! = NULL ) {
memcpy ( & state - > response . data . pw , pw , sizeof ( WINBINDD_PW ) ) ;
return WINBINDD_OK ;
}
2001-05-07 08:32:40 +04:00
/* Get rid from uid */
2003-07-07 09:11:10 +04:00
if ( ! NT_STATUS_IS_OK ( idmap_uid_to_sid ( & user_sid , state - > request . data . uid ) ) ) {
2003-07-22 08:31:20 +04:00
DEBUG ( 1 , ( " could not convert uid %lu to SID \n " ,
( unsigned long ) state - > request . data . uid ) ) ;
2001-05-07 08:32:40 +04:00
return WINBINDD_ERROR ;
}
/* Get name and name type from rid */
2002-01-20 04:24:59 +03:00
if ( ! winbindd_lookup_name_by_sid ( & user_sid , dom_name , user_name , & name_type ) ) {
2001-05-07 08:32:40 +04:00
fstring temp ;
sid_to_string ( temp , & user_sid ) ;
2002-01-10 09:20:03 +03:00
DEBUG ( 1 , ( " could not lookup sid %s \n " , temp ) ) ;
2001-05-07 08:32:40 +04:00
return WINBINDD_ERROR ;
}
2003-04-23 15:54:56 +04:00
domain = find_domain_from_sid ( & user_sid ) ;
if ( ! domain ) {
DEBUG ( 1 , ( " Can't find domain from sid \n " ) ) ;
return WINBINDD_ERROR ;
}
2001-05-07 08:32:40 +04:00
/* Get some user info */
2003-07-22 08:31:20 +04:00
if ( ! ( mem_ctx = talloc_init ( " winbind_getpwuid(%lu) " ,
( unsigned long ) state - > request . data . uid ) ) ) {
2002-01-10 09:20:03 +03:00
2001-11-27 09:28:06 +03:00
DEBUG ( 1 , ( " out of memory \n " ) ) ;
return WINBINDD_ERROR ;
}
2001-10-29 07:50:17 +03:00
2003-04-23 15:54:56 +04:00
status = domain - > methods - > query_user ( domain , mem_ctx , & user_sid ,
2002-01-10 09:20:03 +03:00
& user_info ) ;
2001-12-04 09:17:39 +03:00
if ( ! NT_STATUS_IS_OK ( status ) ) {
2002-01-10 09:20:03 +03:00
DEBUG ( 1 , ( " error getting user info for user '%s' \n " ,
user_name ) ) ;
2002-01-20 04:24:59 +03:00
talloc_destroy ( mem_ctx ) ;
2001-05-07 08:32:40 +04:00
return WINBINDD_ERROR ;
}
2003-05-12 22:12:31 +04:00
/* Check group has a gid number */
2001-05-07 08:32:40 +04:00
2003-07-07 09:11:10 +04:00
if ( ! NT_STATUS_IS_OK ( idmap_sid_to_gid ( user_info . group_sid , & gid , 0 ) ) ) {
2001-05-07 08:32:40 +04:00
DEBUG ( 1 , ( " error getting group id for user %s \n " , user_name ) ) ;
2002-01-20 04:24:59 +03:00
talloc_destroy ( mem_ctx ) ;
2001-05-07 08:32:40 +04:00
return WINBINDD_ERROR ;
}
/* Fill in password structure */
2003-04-23 15:54:56 +04:00
if ( ! winbindd_fill_pwent ( domain - > name , user_name , user_info . user_sid ,
user_info . group_sid ,
2001-12-04 09:17:39 +03:00
user_info . full_name , & state - > response . data . pw ) ) {
2002-01-20 04:24:59 +03:00
talloc_destroy ( mem_ctx ) ;
2001-05-07 08:32:40 +04:00
return WINBINDD_ERROR ;
2001-11-27 23:57:14 +03:00
}
2001-05-07 08:32:40 +04:00
2001-12-04 09:17:39 +03:00
talloc_destroy ( mem_ctx ) ;
2001-05-07 08:32:40 +04:00
return WINBINDD_OK ;
2000-05-09 15:43:00 +04:00
}
/*
* set / get / endpwent functions
*/
/* Rewind file pointer for ntdom passwd database */
enum winbindd_result winbindd_setpwent ( struct winbindd_cli_state * state )
{
2002-01-10 09:20:03 +03:00
struct winbindd_domain * domain ;
2001-10-08 04:34:14 +04:00
2003-07-22 08:31:20 +04:00
DEBUG ( 3 , ( " [%5lu]: setpwent \n " , ( unsigned long ) state - > pid ) ) ;
2001-10-08 04:34:14 +04:00
2001-11-27 23:57:14 +03:00
/* Check user has enabled this */
2001-10-08 04:34:14 +04:00
2001-11-27 23:57:14 +03:00
if ( ! lp_winbind_enum_users ( ) )
return WINBINDD_ERROR ;
2001-10-08 04:34:14 +04:00
2001-11-27 23:57:14 +03:00
/* Free old static data if it exists */
2001-10-08 04:34:14 +04:00
2001-11-27 23:57:14 +03:00
if ( state - > getpwent_state ! = NULL ) {
free_getent_state ( state - > getpwent_state ) ;
state - > getpwent_state = NULL ;
}
2003-07-11 09:33:40 +04:00
#if 0 /* JERRY */
/* add any local users we have */
if ( ( domain_state = ( struct getent_state * ) malloc ( sizeof ( struct getent_state ) ) ) = = NULL )
return WINBINDD_ERROR ;
ZERO_STRUCTP ( domain_state ) ;
/* Add to list of open domains */
DLIST_ADD ( state - > getpwent_state , domain_state ) ;
# endif
2001-10-08 04:34:14 +04:00
2001-11-27 23:57:14 +03:00
/* Create sam pipes for each domain we know about */
2001-10-08 04:34:14 +04:00
2002-01-11 08:33:45 +03:00
for ( domain = domain_list ( ) ; domain ! = NULL ; domain = domain - > next ) {
2001-11-27 23:57:14 +03:00
struct getent_state * domain_state ;
2001-10-08 04:34:14 +04:00
2003-06-30 21:24:59 +04:00
2003-07-07 09:11:10 +04:00
/* don't add our domaina if we are a PDC or if we
are a member of a Samba domain */
2003-06-30 21:24:59 +04:00
2003-07-07 09:11:10 +04:00
if ( ( IS_DC | | lp_winbind_trusted_domains_only ( ) )
& & strequal ( domain - > name , lp_workgroup ( ) ) )
{
continue ;
}
2003-06-30 21:24:59 +04:00
2001-11-27 23:57:14 +03:00
/* Create a state record for this domain */
2001-10-08 04:34:14 +04:00
2002-01-10 09:20:03 +03:00
if ( ( domain_state = ( struct getent_state * )
malloc ( sizeof ( struct getent_state ) ) ) = = NULL )
2001-11-27 23:57:14 +03:00
return WINBINDD_ERROR ;
2001-10-08 04:34:14 +04:00
2001-11-27 23:57:14 +03:00
ZERO_STRUCTP ( domain_state ) ;
2001-10-19 05:46:43 +04:00
2002-01-11 08:33:45 +03:00
fstrcpy ( domain_state - > domain_name , domain - > name ) ;
2001-10-08 04:34:14 +04:00
2001-11-27 23:57:14 +03:00
/* Add to list of open domains */
2001-10-08 04:34:14 +04:00
2001-11-27 23:57:14 +03:00
DLIST_ADD ( state - > getpwent_state , domain_state ) ;
}
2001-10-08 04:34:14 +04:00
2001-11-27 23:57:14 +03:00
return WINBINDD_OK ;
2000-05-09 15:43:00 +04:00
}
/* Close file pointer to ntdom passwd database */
enum winbindd_result winbindd_endpwent ( struct winbindd_cli_state * state )
{
2003-07-22 08:31:20 +04:00
DEBUG ( 3 , ( " [%5lu]: endpwent \n " , ( unsigned long ) state - > pid ) ) ;
2000-05-09 15:43:00 +04:00
2001-11-27 23:57:14 +03:00
free_getent_state ( state - > getpwent_state ) ;
state - > getpwent_state = NULL ;
2001-10-08 04:34:14 +04:00
2001-11-27 23:57:14 +03:00
return WINBINDD_OK ;
2000-05-09 15:43:00 +04:00
}
2001-05-07 08:32:40 +04:00
/* Get partial list of domain users for a domain. We fill in the sam_entries,
and num_sam_entries fields with domain user information . The dispinfo_ndx
field is incremented to the index of the next user to fetch . Return True if
some users were returned , False otherwise . */
# define MAX_FETCH_SAM_ENTRIES 100
static BOOL get_sam_user_entries ( struct getent_state * ent )
{
2001-09-04 11:13:01 +04:00
NTSTATUS status ;
uint32 num_entries ;
2001-12-03 14:32:55 +03:00
WINBIND_USERINFO * info ;
2001-05-07 08:32:40 +04:00
struct getpwent_user * name_list = NULL ;
2001-11-27 23:57:14 +03:00
BOOL result = False ;
TALLOC_CTX * mem_ctx ;
2002-01-11 08:33:45 +03:00
struct winbindd_domain * domain ;
2001-12-01 15:31:43 +03:00
struct winbindd_methods * methods ;
2003-04-23 15:54:56 +04:00
unsigned int i ;
2001-05-07 08:32:40 +04:00
2001-12-11 03:03:58 +03:00
if ( ent - > num_sam_entries )
2001-05-07 08:32:40 +04:00
return False ;
2002-12-20 23:21:31 +03:00
if ( ! ( mem_ctx = talloc_init ( " get_sam_user_entries(%s) " ,
2003-04-23 15:54:56 +04:00
ent - > domain_name ) ) )
2001-11-27 23:57:14 +03:00
return False ;
2001-10-19 12:22:52 +04:00
2002-01-11 08:33:45 +03:00
if ( ! ( domain = find_domain_from_name ( ent - > domain_name ) ) ) {
DEBUG ( 3 , ( " no such domain %s in get_sam_user_entries \n " ,
ent - > domain_name ) ) ;
return False ;
}
methods = domain - > methods ;
2001-05-07 08:32:40 +04:00
/* Free any existing user info */
2001-09-17 08:52:45 +04:00
SAFE_FREE ( ent - > sam_entries ) ;
ent - > num_sam_entries = 0 ;
2001-12-03 14:32:55 +03:00
/* Call query_user_list to get a list of usernames and user rids */
2002-01-11 08:33:45 +03:00
2001-12-11 03:03:58 +03:00
num_entries = 0 ;
2001-05-07 08:32:40 +04:00
2002-01-11 08:33:45 +03:00
status = methods - > query_user_list ( domain , mem_ctx , & num_entries ,
& info ) ;
2001-05-07 08:32:40 +04:00
2001-12-11 03:03:58 +03:00
if ( num_entries ) {
struct getpwent_user * tnl ;
tnl = ( struct getpwent_user * ) Realloc ( name_list ,
sizeof ( struct getpwent_user ) *
( ent - > num_sam_entries +
num_entries ) ) ;
if ( ! tnl ) {
2002-01-10 09:20:03 +03:00
DEBUG ( 0 , ( " get_sam_user_entries realloc failed. \n " ) ) ;
2001-12-11 03:03:58 +03:00
SAFE_FREE ( name_list ) ;
goto done ;
} else
name_list = tnl ;
}
2001-05-07 08:32:40 +04:00
2001-12-11 03:03:58 +03:00
for ( i = 0 ; i < num_entries ; i + + ) {
/* Store account name and gecos */
if ( ! info [ i ] . acct_name ) {
fstrcpy ( name_list [ ent - > num_sam_entries + i ] . name , " " ) ;
} else {
fstrcpy ( name_list [ ent - > num_sam_entries + i ] . name ,
info [ i ] . acct_name ) ;
}
if ( ! info [ i ] . full_name ) {
fstrcpy ( name_list [ ent - > num_sam_entries + i ] . gecos , " " ) ;
} else {
fstrcpy ( name_list [ ent - > num_sam_entries + i ] . gecos ,
info [ i ] . full_name ) ;
2001-05-07 08:32:40 +04:00
}
2001-12-11 03:03:58 +03:00
/* User and group ids */
2003-04-23 15:54:56 +04:00
sid_copy ( & name_list [ ent - > num_sam_entries + i ] . user_sid , info [ i ] . user_sid ) ;
sid_copy ( & name_list [ ent - > num_sam_entries + i ] . group_sid , info [ i ] . group_sid ) ;
2001-12-11 03:03:58 +03:00
}
ent - > num_sam_entries + = num_entries ;
2001-05-07 08:32:40 +04:00
/* Fill in remaining fields */
ent - > sam_entries = name_list ;
ent - > sam_entry_index = 0 ;
2001-10-19 12:22:52 +04:00
result = ent - > num_sam_entries > 0 ;
done :
2001-05-07 08:32:40 +04:00
2001-11-27 23:57:14 +03:00
talloc_destroy ( mem_ctx ) ;
return result ;
2001-05-07 08:32:40 +04:00
}
2000-05-09 15:43:00 +04:00
/* Fetch next passwd entry from ntdom database */
2001-05-07 08:32:40 +04:00
# define MAX_GETPWENT_USERS 500
2000-05-09 15:43:00 +04:00
enum winbindd_result winbindd_getpwent ( struct winbindd_cli_state * state )
{
2001-05-07 08:32:40 +04:00
struct getent_state * ent ;
struct winbindd_pw * user_list ;
int num_users , user_list_ndx = 0 , i ;
2000-05-09 15:43:00 +04:00
2003-07-22 08:31:20 +04:00
DEBUG ( 3 , ( " [%5lu]: getpwent \n " , ( unsigned long ) state - > pid ) ) ;
2000-05-09 15:43:00 +04:00
2001-05-07 08:32:40 +04:00
/* Check user has enabled this */
2000-05-09 15:43:00 +04:00
2001-10-08 04:34:14 +04:00
if ( ! lp_winbind_enum_users ( ) )
2001-05-07 08:32:40 +04:00
return WINBINDD_ERROR ;
2000-05-09 15:43:00 +04:00
2001-05-07 08:32:40 +04:00
/* Allocate space for returning a chunk of users */
2000-05-09 15:43:00 +04:00
2001-05-07 08:32:40 +04:00
num_users = MIN ( MAX_GETPWENT_USERS , state - > request . data . num_entries ) ;
if ( ( state - > response . extra_data =
2001-10-08 04:34:14 +04:00
malloc ( num_users * sizeof ( struct winbindd_pw ) ) ) = = NULL )
2001-05-07 08:32:40 +04:00
return WINBINDD_ERROR ;
2000-05-09 15:43:00 +04:00
2001-05-07 08:32:40 +04:00
memset ( state - > response . extra_data , 0 , num_users *
sizeof ( struct winbindd_pw ) ) ;
2000-05-09 15:43:00 +04:00
2001-05-07 08:32:40 +04:00
user_list = ( struct winbindd_pw * ) state - > response . extra_data ;
2001-10-08 04:34:14 +04:00
if ( ! ( ent = state - > getpwent_state ) )
2001-05-07 08:32:40 +04:00
return WINBINDD_ERROR ;
2000-05-09 15:43:00 +04:00
2001-05-07 08:32:40 +04:00
/* Start sending back users */
2000-05-09 15:43:00 +04:00
2001-05-07 08:32:40 +04:00
for ( i = 0 ; i < num_users ; i + + ) {
struct getpwent_user * name_list = NULL ;
uint32 result ;
2000-05-09 15:43:00 +04:00
2001-05-07 08:32:40 +04:00
/* Do we need to fetch another chunk of users? */
2000-05-09 15:43:00 +04:00
2001-05-07 08:32:40 +04:00
if ( ent - > num_sam_entries = = ent - > sam_entry_index ) {
2000-05-09 15:43:00 +04:00
2001-05-07 08:32:40 +04:00
while ( ent & & ! get_sam_user_entries ( ent ) ) {
struct getent_state * next_ent ;
2000-05-09 15:43:00 +04:00
2001-05-07 08:32:40 +04:00
/* Free state information for this domain */
2000-05-09 15:43:00 +04:00
2001-09-17 08:52:45 +04:00
SAFE_FREE ( ent - > sam_entries ) ;
2000-05-09 15:43:00 +04:00
2001-05-07 08:32:40 +04:00
next_ent = ent - > next ;
DLIST_REMOVE ( state - > getpwent_state , ent ) ;
2001-09-17 08:52:45 +04:00
SAFE_FREE ( ent ) ;
2001-05-07 08:32:40 +04:00
ent = next_ent ;
}
/* No more domains */
2001-10-08 04:34:14 +04:00
if ( ! ent )
2001-11-27 09:28:06 +03:00
break ;
2001-05-07 08:32:40 +04:00
}
name_list = ent - > sam_entries ;
/* Skip machine accounts */
if ( name_list [ ent - > sam_entry_index ] .
name [ strlen ( name_list [ ent - > sam_entry_index ] . name ) - 1 ]
= = ' $ ' ) {
ent - > sam_entry_index + + ;
continue ;
}
/* Lookup user info */
result = winbindd_fill_pwent (
2002-01-11 08:33:45 +03:00
ent - > domain_name ,
2002-01-20 04:24:59 +03:00
name_list [ ent - > sam_entry_index ] . name ,
2003-04-23 15:54:56 +04:00
& name_list [ ent - > sam_entry_index ] . user_sid ,
& name_list [ ent - > sam_entry_index ] . group_sid ,
2001-05-07 08:32:40 +04:00
name_list [ ent - > sam_entry_index ] . gecos ,
& user_list [ user_list_ndx ] ) ;
ent - > sam_entry_index + + ;
/* Add user to return list */
if ( result ) {
user_list_ndx + + ;
state - > response . data . num_entries + + ;
state - > response . length + =
sizeof ( struct winbindd_pw ) ;
2001-10-08 04:34:14 +04:00
} else
2001-05-07 08:32:40 +04:00
DEBUG ( 1 , ( " could not lookup domain user %s \n " ,
2003-03-19 11:23:29 +03:00
name_list [ ent - > sam_entry_index ] . name ) ) ;
2001-05-07 08:32:40 +04:00
}
/* Out of domains */
return ( user_list_ndx > 0 ) ? WINBINDD_OK : WINBINDD_ERROR ;
}
2000-05-09 15:43:00 +04:00
2001-05-07 08:32:40 +04:00
/* List domain users without mapping to unix ids */
enum winbindd_result winbindd_list_users ( struct winbindd_cli_state * state )
{
2001-08-18 23:43:28 +04:00
struct winbindd_domain * domain ;
2001-12-03 14:32:55 +03:00
WINBIND_USERINFO * info ;
2001-08-18 23:43:28 +04:00
uint32 num_entries = 0 , total_entries = 0 ;
2001-08-12 21:30:01 +04:00
char * ted , * extra_data = NULL ;
2001-05-07 08:32:40 +04:00
int extra_data_len = 0 ;
2001-11-27 09:28:06 +03:00
TALLOC_CTX * mem_ctx ;
enum winbindd_result rv = WINBINDD_ERROR ;
2001-05-07 08:32:40 +04:00
2003-07-22 08:31:20 +04:00
DEBUG ( 3 , ( " [%5lu]: list users \n " , ( unsigned long ) state - > pid ) ) ;
2001-05-07 08:32:40 +04:00
2002-12-20 23:21:31 +03:00
if ( ! ( mem_ctx = talloc_init ( " winbindd_list_users " ) ) )
2001-11-27 09:28:06 +03:00
return WINBINDD_ERROR ;
2001-10-19 05:46:43 +04:00
2001-08-18 23:43:28 +04:00
/* Enumerate over trusted domains */
2001-05-07 08:32:40 +04:00
2002-01-11 08:33:45 +03:00
for ( domain = domain_list ( ) ; domain ; domain = domain - > next ) {
2001-09-04 11:13:01 +04:00
NTSTATUS status ;
2001-12-01 15:31:43 +03:00
struct winbindd_methods * methods ;
2003-04-23 15:54:56 +04:00
unsigned int i ;
2001-05-07 08:32:40 +04:00
2001-12-01 15:31:43 +03:00
methods = domain - > methods ;
2001-08-18 23:43:28 +04:00
/* Query display info */
2001-12-11 03:03:58 +03:00
status = methods - > query_user_list ( domain , mem_ctx ,
& num_entries , & info ) ;
2001-05-07 08:32:40 +04:00
2001-12-11 03:03:58 +03:00
if ( num_entries = = 0 )
continue ;
2001-05-07 08:32:40 +04:00
2001-12-11 03:03:58 +03:00
/* Allocate some memory for extra data */
total_entries + = num_entries ;
2001-05-07 08:32:40 +04:00
2001-12-11 03:03:58 +03:00
ted = Realloc ( extra_data , sizeof ( fstring ) * total_entries ) ;
2001-05-07 08:32:40 +04:00
2001-12-11 03:03:58 +03:00
if ( ! ted ) {
2002-01-10 09:20:03 +03:00
DEBUG ( 0 , ( " failed to enlarge buffer! \n " ) ) ;
2001-12-11 03:03:58 +03:00
SAFE_FREE ( extra_data ) ;
goto done ;
} else
extra_data = ted ;
2001-05-07 08:32:40 +04:00
2001-12-11 03:03:58 +03:00
/* Pack user list into extra data fields */
for ( i = 0 ; i < num_entries ; i + + ) {
fstring acct_name , name ;
if ( ! info [ i ] . acct_name ) {
fstrcpy ( acct_name , " " ) ;
} else {
fstrcpy ( acct_name , info [ i ] . acct_name ) ;
}
2002-01-18 05:37:55 +03:00
fill_domain_username ( name , domain - > name , acct_name ) ;
2001-05-07 08:32:40 +04:00
/* Append to extra data */
2001-12-11 03:03:58 +03:00
memcpy ( & extra_data [ extra_data_len ] , name ,
strlen ( name ) ) ;
extra_data_len + = strlen ( name ) ;
extra_data [ extra_data_len + + ] = ' , ' ;
}
2000-05-09 15:43:00 +04:00
}
2001-05-07 08:32:40 +04:00
/* Assign extra_data fields in response structure */
if ( extra_data ) {
extra_data [ extra_data_len - 1 ] = ' \0 ' ;
state - > response . extra_data = extra_data ;
state - > response . length + = extra_data_len ;
}
/* No domains responded but that's still OK so don't return an
error . */
2000-05-09 15:43:00 +04:00
2001-10-19 05:46:43 +04:00
rv = WINBINDD_OK ;
done :
2001-11-27 09:28:06 +03:00
talloc_destroy ( mem_ctx ) ;
return rv ;
2000-05-09 15:43:00 +04:00
}