2 Unix SMB/CIFS implementation.
4 Winbind daemon - user related functions
6 Copyright (C) Tim Potter 2000
7 Copyright (C) Jeremy Allison 2001.
8 Copyright (C) Gerald (Jerry) Carter 2003.
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 #define DBGC_CLASS DBGC_WINBIND
31 extern userdom_struct current_user_info
;
33 /* Fill a pwent structure with information we have obtained */
35 static BOOL
winbindd_fill_pwent(char *dom_name
, char *user_name
,
36 DOM_SID
*user_sid
, DOM_SID
*group_sid
,
37 char *full_name
, struct winbindd_pw
*pw
)
39 fstring output_username
;
44 if (!pw
|| !dom_name
|| !user_name
)
47 /* Resolve the uid number */
49 if (!NT_STATUS_IS_OK(idmap_sid_to_uid(user_sid
, &pw
->pw_uid
, 0))) {
50 DEBUG(1, ("error getting user id for sid %s\n", sid_to_string(sid_string
, user_sid
)));
54 /* Resolve the gid number */
56 if (!NT_STATUS_IS_OK(idmap_sid_to_gid(group_sid
, &pw
->pw_gid
, 0))) {
57 DEBUG(1, ("error getting group id for sid %s\n", sid_to_string(sid_string
, group_sid
)));
63 fill_domain_username(output_username
, dom_name
, user_name
);
65 safe_strcpy(pw
->pw_name
, output_username
, sizeof(pw
->pw_name
) - 1);
67 /* Full name (gecos) */
69 safe_strcpy(pw
->pw_gecos
, full_name
, sizeof(pw
->pw_gecos
) - 1);
71 /* Home directory and shell - use template config parameters. The
72 defaults are /tmp for the home directory and /bin/false for
75 /* The substitution of %U and %D in the 'template homedir' is done
76 by alloc_sub_specified() below. */
78 fstrcpy(current_user_info
.domain
, dom_name
);
80 homedir
= alloc_sub_specified(lp_template_homedir(), user_name
, dom_name
, pw
->pw_uid
, pw
->pw_gid
);
85 safe_strcpy(pw
->pw_dir
, homedir
, sizeof(pw
->pw_dir
) - 1);
89 shell
= alloc_sub_specified(lp_template_shell(), user_name
, dom_name
, pw
->pw_uid
, pw
->pw_gid
);
94 safe_strcpy(pw
->pw_shell
, shell
,
95 sizeof(pw
->pw_shell
) - 1);
99 /* Password - set to "x" as we can't generate anything useful here.
100 Authentication can be done using the pam_winbind module. */
102 safe_strcpy(pw
->pw_passwd
, "x", sizeof(pw
->pw_passwd
) - 1);
107 /* Return a password structure from a username. */
109 enum winbindd_result
winbindd_getpwnam(struct winbindd_cli_state
*state
)
111 WINBIND_USERINFO user_info
;
115 fstring name_domain
, name_user
;
116 enum SID_NAME_USE name_type
;
117 struct winbindd_domain
*domain
;
120 /* Ensure null termination */
121 state
->request
.data
.username
[sizeof(state
->request
.data
.username
)-1]='\0';
123 DEBUG(3, ("[%5lu]: getpwnam %s\n", (unsigned long)state
->pid
,
124 state
->request
.data
.username
));
126 /* Parse domain and username */
128 parse_domain_user(state
->request
.data
.username
,
129 name_domain
, name_user
);
131 /* if this is our local domain (or no domain), the do a local tdb search */
133 if ( !*name_domain
|| strequal(name_domain
, get_global_sam_name()) ) {
134 if ( !(pw
= wb_getpwnam(name_user
)) ) {
135 DEBUG(5,("winbindd_getpwnam: lookup for %s\\%s failed\n",
136 name_domain
, name_user
));
137 return WINBINDD_ERROR
;
139 memcpy( &state
->response
.data
.pw
, pw
, sizeof(WINBINDD_PW
) );
143 /* should we deal with users for our domain? */
145 if ((domain
= find_domain_from_name(name_domain
)) == NULL
) {
146 DEBUG(5, ("no such domain: %s\n", name_domain
));
147 return WINBINDD_ERROR
;
150 if ( domain
->primary
&& lp_winbind_trusted_domains_only()) {
151 DEBUG(7,("winbindd_getpwnam: My domain -- rejecting getpwnam() for %s\\%s.\n",
152 name_domain
, name_user
));
153 return WINBINDD_ERROR
;
156 /* Get rid and name type from name */
158 if (!winbindd_lookup_sid_by_name(domain
, domain
->name
, name_user
, &user_sid
, &name_type
)) {
159 DEBUG(1, ("user '%s' does not exist\n", name_user
));
160 return WINBINDD_ERROR
;
163 if (name_type
!= SID_NAME_USER
&& name_type
!= SID_NAME_COMPUTER
) {
164 DEBUG(1, ("name '%s' is not a user name: %d\n", name_user
,
166 return WINBINDD_ERROR
;
169 /* Get some user info. */
171 if (!(mem_ctx
= talloc_init("winbindd_getpwnam([%s]\\[%s])",
172 name_domain
, name_user
))) {
173 DEBUG(1, ("out of memory\n"));
174 return WINBINDD_ERROR
;
177 status
= domain
->methods
->query_user(domain
, mem_ctx
, &user_sid
,
180 if (!NT_STATUS_IS_OK(status
)) {
181 DEBUG(1, ("error getting user info for user '[%s]\\[%s]'\n",
182 name_domain
, name_user
));
183 talloc_destroy(mem_ctx
);
184 return WINBINDD_ERROR
;
187 /* Now take all this information and fill in a passwd structure */
188 if (!winbindd_fill_pwent(name_domain
, user_info
.acct_name
,
189 user_info
.user_sid
, user_info
.group_sid
,
191 &state
->response
.data
.pw
)) {
192 talloc_destroy(mem_ctx
);
193 return WINBINDD_ERROR
;
196 talloc_destroy(mem_ctx
);
201 /* Return a password structure given a uid number */
203 enum winbindd_result
winbindd_getpwuid(struct winbindd_cli_state
*state
)
206 struct winbindd_domain
*domain
;
210 enum SID_NAME_USE name_type
;
211 WINBIND_USERINFO user_info
;
216 /* Bug out if the uid isn't in the winbind range */
218 if ((state
->request
.data
.uid
< server_state
.uid_low
) ||
219 (state
->request
.data
.uid
> server_state
.uid_high
))
220 return WINBINDD_ERROR
;
222 DEBUG(3, ("[%5lu]: getpwuid %lu\n", (unsigned long)state
->pid
,
223 (unsigned long)state
->request
.data
.uid
));
225 /* always try local tdb first */
227 if ( (pw
= wb_getpwuid(state
->request
.data
.uid
)) != NULL
) {
228 memcpy( &state
->response
.data
.pw
, pw
, sizeof(WINBINDD_PW
) );
232 /* Get rid from uid */
234 if (!NT_STATUS_IS_OK(idmap_uid_to_sid(&user_sid
, state
->request
.data
.uid
))) {
235 DEBUG(1, ("could not convert uid %lu to SID\n",
236 (unsigned long)state
->request
.data
.uid
));
237 return WINBINDD_ERROR
;
240 /* Get name and name type from rid */
242 if (!winbindd_lookup_name_by_sid(&user_sid
, dom_name
, user_name
, &name_type
)) {
245 sid_to_string(temp
, &user_sid
);
246 DEBUG(1, ("could not lookup sid %s\n", temp
));
247 return WINBINDD_ERROR
;
250 domain
= find_domain_from_sid(&user_sid
);
253 DEBUG(1,("Can't find domain from sid\n"));
254 return WINBINDD_ERROR
;
257 /* Get some user info */
259 if (!(mem_ctx
= talloc_init("winbind_getpwuid(%lu)",
260 (unsigned long)state
->request
.data
.uid
))) {
262 DEBUG(1, ("out of memory\n"));
263 return WINBINDD_ERROR
;
266 status
= domain
->methods
->query_user(domain
, mem_ctx
, &user_sid
,
269 if (!NT_STATUS_IS_OK(status
)) {
270 DEBUG(1, ("error getting user info for user '%s'\n",
272 talloc_destroy(mem_ctx
);
273 return WINBINDD_ERROR
;
276 /* Check group has a gid number */
278 if (!NT_STATUS_IS_OK(idmap_sid_to_gid(user_info
.group_sid
, &gid
, 0))) {
279 DEBUG(1, ("error getting group id for user %s\n", user_name
));
280 talloc_destroy(mem_ctx
);
281 return WINBINDD_ERROR
;
284 /* Fill in password structure */
286 if (!winbindd_fill_pwent(domain
->name
, user_info
.acct_name
, user_info
.user_sid
,
288 user_info
.full_name
, &state
->response
.data
.pw
)) {
289 talloc_destroy(mem_ctx
);
290 return WINBINDD_ERROR
;
293 talloc_destroy(mem_ctx
);
299 * set/get/endpwent functions
302 /* Rewind file pointer for ntdom passwd database */
304 enum winbindd_result
winbindd_setpwent(struct winbindd_cli_state
*state
)
306 struct winbindd_domain
*domain
;
308 DEBUG(3, ("[%5lu]: setpwent\n", (unsigned long)state
->pid
));
310 /* Check user has enabled this */
312 if (!lp_winbind_enum_users())
313 return WINBINDD_ERROR
;
315 /* Free old static data if it exists */
317 if (state
->getpwent_state
!= NULL
) {
318 free_getent_state(state
->getpwent_state
);
319 state
->getpwent_state
= NULL
;
323 /* add any local users we have */
325 if ( (domain_state
= (struct getent_state
*)malloc(sizeof(struct getent_state
))) == NULL
)
326 return WINBINDD_ERROR
;
328 ZERO_STRUCTP(domain_state
);
330 /* Add to list of open domains */
332 DLIST_ADD(state
->getpwent_state
, domain_state
);
335 /* Create sam pipes for each domain we know about */
337 for(domain
= domain_list(); domain
!= NULL
; domain
= domain
->next
) {
338 struct getent_state
*domain_state
;
341 /* don't add our domaina if we are a PDC or if we
342 are a member of a Samba domain */
344 if ( (IS_DC
|| lp_winbind_trusted_domains_only())
345 && strequal(domain
->name
, lp_workgroup()) )
350 /* Create a state record for this domain */
352 if ((domain_state
= (struct getent_state
*)
353 malloc(sizeof(struct getent_state
))) == NULL
)
354 return WINBINDD_ERROR
;
356 ZERO_STRUCTP(domain_state
);
358 fstrcpy(domain_state
->domain_name
, domain
->name
);
360 /* Add to list of open domains */
362 DLIST_ADD(state
->getpwent_state
, domain_state
);
365 state
->getpwent_initialized
= True
;
370 /* Close file pointer to ntdom passwd database */
372 enum winbindd_result
winbindd_endpwent(struct winbindd_cli_state
*state
)
374 DEBUG(3, ("[%5lu]: endpwent\n", (unsigned long)state
->pid
));
376 free_getent_state(state
->getpwent_state
);
377 state
->getpwent_initialized
= False
;
378 state
->getpwent_state
= NULL
;
383 /* Get partial list of domain users for a domain. We fill in the sam_entries,
384 and num_sam_entries fields with domain user information. The dispinfo_ndx
385 field is incremented to the index of the next user to fetch. Return True if
386 some users were returned, False otherwise. */
388 static BOOL
get_sam_user_entries(struct getent_state
*ent
)
392 WINBIND_USERINFO
*info
;
393 struct getpwent_user
*name_list
= NULL
;
396 struct winbindd_domain
*domain
;
397 struct winbindd_methods
*methods
;
400 if (ent
->num_sam_entries
)
403 if (!(mem_ctx
= talloc_init("get_sam_user_entries(%s)",
407 if (!(domain
= find_domain_from_name(ent
->domain_name
))) {
408 DEBUG(3, ("no such domain %s in get_sam_user_entries\n",
413 methods
= domain
->methods
;
415 /* Free any existing user info */
417 SAFE_FREE(ent
->sam_entries
);
418 ent
->num_sam_entries
= 0;
420 /* Call query_user_list to get a list of usernames and user rids */
424 status
= methods
->query_user_list(domain
, mem_ctx
, &num_entries
,
428 struct getpwent_user
*tnl
;
430 tnl
= (struct getpwent_user
*)Realloc(name_list
,
431 sizeof(struct getpwent_user
) *
432 (ent
->num_sam_entries
+
436 DEBUG(0,("get_sam_user_entries realloc failed.\n"));
437 SAFE_FREE(name_list
);
443 for (i
= 0; i
< num_entries
; i
++) {
444 /* Store account name and gecos */
445 if (!info
[i
].acct_name
) {
446 fstrcpy(name_list
[ent
->num_sam_entries
+ i
].name
, "");
448 fstrcpy(name_list
[ent
->num_sam_entries
+ i
].name
,
451 if (!info
[i
].full_name
) {
452 fstrcpy(name_list
[ent
->num_sam_entries
+ i
].gecos
, "");
454 fstrcpy(name_list
[ent
->num_sam_entries
+ i
].gecos
,
458 /* User and group ids */
459 sid_copy(&name_list
[ent
->num_sam_entries
+i
].user_sid
, info
[i
].user_sid
);
460 sid_copy(&name_list
[ent
->num_sam_entries
+i
].group_sid
, info
[i
].group_sid
);
463 ent
->num_sam_entries
+= num_entries
;
465 /* Fill in remaining fields */
467 ent
->sam_entries
= name_list
;
468 ent
->sam_entry_index
= 0;
469 result
= ent
->num_sam_entries
> 0;
473 talloc_destroy(mem_ctx
);
478 /* Fetch next passwd entry from ntdom database */
480 #define MAX_GETPWENT_USERS 500
482 enum winbindd_result
winbindd_getpwent(struct winbindd_cli_state
*state
)
484 struct getent_state
*ent
;
485 struct winbindd_pw
*user_list
;
486 int num_users
, user_list_ndx
= 0, i
;
488 DEBUG(3, ("[%5lu]: getpwent\n", (unsigned long)state
->pid
));
490 /* Check user has enabled this */
492 if (!lp_winbind_enum_users())
493 return WINBINDD_ERROR
;
495 /* Allocate space for returning a chunk of users */
497 num_users
= MIN(MAX_GETPWENT_USERS
, state
->request
.data
.num_entries
);
499 if ((state
->response
.extra_data
=
500 malloc(num_users
* sizeof(struct winbindd_pw
))) == NULL
)
501 return WINBINDD_ERROR
;
503 memset(state
->response
.extra_data
, 0, num_users
*
504 sizeof(struct winbindd_pw
));
506 user_list
= (struct winbindd_pw
*)state
->response
.extra_data
;
508 if (!state
->getpwent_initialized
)
509 winbindd_setpwent(state
);
511 if (!(ent
= state
->getpwent_state
))
512 return WINBINDD_ERROR
;
514 /* Start sending back users */
516 for (i
= 0; i
< num_users
; i
++) {
517 struct getpwent_user
*name_list
= NULL
;
520 /* Do we need to fetch another chunk of users? */
522 if (ent
->num_sam_entries
== ent
->sam_entry_index
) {
524 while(ent
&& !get_sam_user_entries(ent
)) {
525 struct getent_state
*next_ent
;
527 /* Free state information for this domain */
529 SAFE_FREE(ent
->sam_entries
);
531 next_ent
= ent
->next
;
532 DLIST_REMOVE(state
->getpwent_state
, ent
);
538 /* No more domains */
544 name_list
= ent
->sam_entries
;
546 /* Lookup user info */
548 result
= winbindd_fill_pwent(
550 name_list
[ent
->sam_entry_index
].name
,
551 &name_list
[ent
->sam_entry_index
].user_sid
,
552 &name_list
[ent
->sam_entry_index
].group_sid
,
553 name_list
[ent
->sam_entry_index
].gecos
,
554 &user_list
[user_list_ndx
]);
556 ent
->sam_entry_index
++;
558 /* Add user to return list */
563 state
->response
.data
.num_entries
++;
564 state
->response
.length
+=
565 sizeof(struct winbindd_pw
);
568 DEBUG(1, ("could not lookup domain user %s\n",
569 name_list
[ent
->sam_entry_index
].name
));
574 return (user_list_ndx
> 0) ? WINBINDD_OK
: WINBINDD_ERROR
;
577 /* List domain users without mapping to unix ids */
579 enum winbindd_result
winbindd_list_users(struct winbindd_cli_state
*state
)
581 struct winbindd_domain
*domain
;
582 WINBIND_USERINFO
*info
;
583 const char *which_domain
;
584 uint32 num_entries
= 0, total_entries
= 0;
585 char *ted
, *extra_data
= NULL
;
586 int extra_data_len
= 0;
588 enum winbindd_result rv
= WINBINDD_ERROR
;
590 DEBUG(3, ("[%5lu]: list users\n", (unsigned long)state
->pid
));
592 if (!(mem_ctx
= talloc_init("winbindd_list_users")))
593 return WINBINDD_ERROR
;
595 /* Ensure null termination */
596 state
->request
.domain_name
[sizeof(state
->request
.domain_name
)-1]='\0';
597 which_domain
= state
->request
.domain_name
;
599 /* Enumerate over trusted domains */
601 for (domain
= domain_list(); domain
; domain
= domain
->next
) {
603 struct winbindd_methods
*methods
;
606 /* if we have a domain name restricting the request and this
607 one in the list doesn't match, then just bypass the remainder
610 if ( *which_domain
&& !strequal(which_domain
, domain
->name
) )
613 methods
= domain
->methods
;
615 /* Query display info */
616 status
= methods
->query_user_list(domain
, mem_ctx
,
617 &num_entries
, &info
);
619 if (num_entries
== 0)
622 /* Allocate some memory for extra data */
623 total_entries
+= num_entries
;
625 ted
= Realloc(extra_data
, sizeof(fstring
) * total_entries
);
628 DEBUG(0,("failed to enlarge buffer!\n"));
629 SAFE_FREE(extra_data
);
634 /* Pack user list into extra data fields */
636 for (i
= 0; i
< num_entries
; i
++) {
637 fstring acct_name
, name
;
639 if (!info
[i
].acct_name
) {
640 fstrcpy(acct_name
, "");
642 fstrcpy(acct_name
, info
[i
].acct_name
);
645 fill_domain_username(name
, domain
->name
, acct_name
);
647 /* Append to extra data */
648 memcpy(&extra_data
[extra_data_len
], name
,
650 extra_data_len
+= strlen(name
);
651 extra_data
[extra_data_len
++] = ',';
655 /* Assign extra_data fields in response structure */
658 extra_data
[extra_data_len
- 1] = '\0';
659 state
->response
.extra_data
= extra_data
;
660 state
->response
.length
+= extra_data_len
;
663 /* No domains responded but that's still OK so don't return an
670 talloc_destroy(mem_ctx
);