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);
97 /* Password - set to "x" as we can't generate anything useful here.
98 Authentication can be done using the pam_winbind module. */
100 safe_strcpy(pw
->pw_passwd
, "x", sizeof(pw
->pw_passwd
) - 1);
105 /* Return a password structure from a username. */
107 enum winbindd_result
winbindd_getpwnam(struct winbindd_cli_state
*state
)
109 WINBIND_USERINFO user_info
;
113 fstring name_domain
, name_user
;
114 enum SID_NAME_USE name_type
;
115 struct winbindd_domain
*domain
;
118 /* Ensure null termination */
119 state
->request
.data
.username
[sizeof(state
->request
.data
.username
)-1]='\0';
121 DEBUG(3, ("[%5lu]: getpwnam %s\n", (unsigned long)state
->pid
,
122 state
->request
.data
.username
));
124 /* Parse domain and username */
126 parse_domain_user(state
->request
.data
.username
,
127 name_domain
, name_user
);
129 /* if this is our local domain (or no domain), the do a local tdb search */
131 if ( !*name_domain
|| strequal(name_domain
, get_global_sam_name()) ) {
132 if ( !(pw
= wb_getpwnam(name_user
)) ) {
133 DEBUG(5,("winbindd_getpwnam: lookup for %s\\%s failed\n",
134 name_domain
, name_user
));
135 return WINBINDD_ERROR
;
137 memcpy( &state
->response
.data
.pw
, pw
, sizeof(WINBINDD_PW
) );
141 /* should we deal with users for our domain? */
143 if ((domain
= find_domain_from_name(name_domain
)) == NULL
) {
144 DEBUG(5, ("no such domain: %s\n", name_domain
));
145 return WINBINDD_ERROR
;
148 if ( domain
->primary
&& lp_winbind_trusted_domains_only()) {
149 DEBUG(7,("winbindd_getpwnam: My domain -- rejecting getpwnam() for %s\\%s.\n",
150 name_domain
, name_user
));
151 return WINBINDD_ERROR
;
154 /* Get rid and name type from name */
156 if (!winbindd_lookup_sid_by_name(domain
, name_user
, &user_sid
, &name_type
)) {
157 DEBUG(1, ("user '%s' does not exist\n", name_user
));
158 return WINBINDD_ERROR
;
161 if (name_type
!= SID_NAME_USER
&& name_type
!= SID_NAME_COMPUTER
) {
162 DEBUG(1, ("name '%s' is not a user name: %d\n", name_user
,
164 return WINBINDD_ERROR
;
167 /* Get some user info. */
169 if (!(mem_ctx
= talloc_init("winbindd_getpwnam([%s]\\[%s])",
170 name_domain
, name_user
))) {
171 DEBUG(1, ("out of memory\n"));
172 return WINBINDD_ERROR
;
175 status
= domain
->methods
->query_user(domain
, mem_ctx
, &user_sid
,
178 if (!NT_STATUS_IS_OK(status
)) {
179 DEBUG(1, ("error getting user info for user '[%s]\\[%s]'\n",
180 name_domain
, name_user
));
181 talloc_destroy(mem_ctx
);
182 return WINBINDD_ERROR
;
185 /* Now take all this information and fill in a passwd structure */
186 if (!winbindd_fill_pwent(name_domain
, name_user
,
187 user_info
.user_sid
, user_info
.group_sid
,
189 &state
->response
.data
.pw
)) {
190 talloc_destroy(mem_ctx
);
191 return WINBINDD_ERROR
;
194 talloc_destroy(mem_ctx
);
199 /* Return a password structure given a uid number */
201 enum winbindd_result
winbindd_getpwuid(struct winbindd_cli_state
*state
)
204 struct winbindd_domain
*domain
;
208 enum SID_NAME_USE name_type
;
209 WINBIND_USERINFO user_info
;
214 /* Bug out if the uid isn't in the winbind range */
216 if ((state
->request
.data
.uid
< server_state
.uid_low
) ||
217 (state
->request
.data
.uid
> server_state
.uid_high
))
218 return WINBINDD_ERROR
;
220 DEBUG(3, ("[%5lu]: getpwuid %lu\n", (unsigned long)state
->pid
,
221 (unsigned long)state
->request
.data
.uid
));
223 /* always try local tdb first */
225 if ( (pw
= wb_getpwuid(state
->request
.data
.uid
)) != NULL
) {
226 memcpy( &state
->response
.data
.pw
, pw
, sizeof(WINBINDD_PW
) );
230 /* Get rid from uid */
232 if (!NT_STATUS_IS_OK(idmap_uid_to_sid(&user_sid
, state
->request
.data
.uid
))) {
233 DEBUG(1, ("could not convert uid %lu to SID\n",
234 (unsigned long)state
->request
.data
.uid
));
235 return WINBINDD_ERROR
;
238 /* Get name and name type from rid */
240 if (!winbindd_lookup_name_by_sid(&user_sid
, dom_name
, user_name
, &name_type
)) {
243 sid_to_string(temp
, &user_sid
);
244 DEBUG(1, ("could not lookup sid %s\n", temp
));
245 return WINBINDD_ERROR
;
248 domain
= find_domain_from_sid(&user_sid
);
251 DEBUG(1,("Can't find domain from sid\n"));
252 return WINBINDD_ERROR
;
255 /* Get some user info */
257 if (!(mem_ctx
= talloc_init("winbind_getpwuid(%lu)",
258 (unsigned long)state
->request
.data
.uid
))) {
260 DEBUG(1, ("out of memory\n"));
261 return WINBINDD_ERROR
;
264 status
= domain
->methods
->query_user(domain
, mem_ctx
, &user_sid
,
267 if (!NT_STATUS_IS_OK(status
)) {
268 DEBUG(1, ("error getting user info for user '%s'\n",
270 talloc_destroy(mem_ctx
);
271 return WINBINDD_ERROR
;
274 /* Check group has a gid number */
276 if (!NT_STATUS_IS_OK(idmap_sid_to_gid(user_info
.group_sid
, &gid
, 0))) {
277 DEBUG(1, ("error getting group id for user %s\n", user_name
));
278 talloc_destroy(mem_ctx
);
279 return WINBINDD_ERROR
;
282 /* Fill in password structure */
284 if (!winbindd_fill_pwent(domain
->name
, user_name
, user_info
.user_sid
,
286 user_info
.full_name
, &state
->response
.data
.pw
)) {
287 talloc_destroy(mem_ctx
);
288 return WINBINDD_ERROR
;
291 talloc_destroy(mem_ctx
);
297 * set/get/endpwent functions
300 /* Rewind file pointer for ntdom passwd database */
302 enum winbindd_result
winbindd_setpwent(struct winbindd_cli_state
*state
)
304 struct winbindd_domain
*domain
;
306 DEBUG(3, ("[%5lu]: setpwent\n", (unsigned long)state
->pid
));
308 /* Check user has enabled this */
310 if (!lp_winbind_enum_users())
311 return WINBINDD_ERROR
;
313 /* Free old static data if it exists */
315 if (state
->getpwent_state
!= NULL
) {
316 free_getent_state(state
->getpwent_state
);
317 state
->getpwent_state
= NULL
;
321 /* add any local users we have */
323 if ( (domain_state
= (struct getent_state
*)malloc(sizeof(struct getent_state
))) == NULL
)
324 return WINBINDD_ERROR
;
326 ZERO_STRUCTP(domain_state
);
328 /* Add to list of open domains */
330 DLIST_ADD(state
->getpwent_state
, domain_state
);
333 /* Create sam pipes for each domain we know about */
335 for(domain
= domain_list(); domain
!= NULL
; domain
= domain
->next
) {
336 struct getent_state
*domain_state
;
339 /* don't add our domaina if we are a PDC or if we
340 are a member of a Samba domain */
342 if ( (IS_DC
|| lp_winbind_trusted_domains_only())
343 && strequal(domain
->name
, lp_workgroup()) )
348 /* Create a state record for this domain */
350 if ((domain_state
= (struct getent_state
*)
351 malloc(sizeof(struct getent_state
))) == NULL
)
352 return WINBINDD_ERROR
;
354 ZERO_STRUCTP(domain_state
);
356 fstrcpy(domain_state
->domain_name
, domain
->name
);
358 /* Add to list of open domains */
360 DLIST_ADD(state
->getpwent_state
, domain_state
);
366 /* Close file pointer to ntdom passwd database */
368 enum winbindd_result
winbindd_endpwent(struct winbindd_cli_state
*state
)
370 DEBUG(3, ("[%5lu]: endpwent\n", (unsigned long)state
->pid
));
372 free_getent_state(state
->getpwent_state
);
373 state
->getpwent_state
= NULL
;
378 /* Get partial list of domain users for a domain. We fill in the sam_entries,
379 and num_sam_entries fields with domain user information. The dispinfo_ndx
380 field is incremented to the index of the next user to fetch. Return True if
381 some users were returned, False otherwise. */
383 #define MAX_FETCH_SAM_ENTRIES 100
385 static BOOL
get_sam_user_entries(struct getent_state
*ent
)
389 WINBIND_USERINFO
*info
;
390 struct getpwent_user
*name_list
= NULL
;
393 struct winbindd_domain
*domain
;
394 struct winbindd_methods
*methods
;
397 if (ent
->num_sam_entries
)
400 if (!(mem_ctx
= talloc_init("get_sam_user_entries(%s)",
404 if (!(domain
= find_domain_from_name(ent
->domain_name
))) {
405 DEBUG(3, ("no such domain %s in get_sam_user_entries\n",
410 methods
= domain
->methods
;
412 /* Free any existing user info */
414 SAFE_FREE(ent
->sam_entries
);
415 ent
->num_sam_entries
= 0;
417 /* Call query_user_list to get a list of usernames and user rids */
421 status
= methods
->query_user_list(domain
, mem_ctx
, &num_entries
,
425 struct getpwent_user
*tnl
;
427 tnl
= (struct getpwent_user
*)Realloc(name_list
,
428 sizeof(struct getpwent_user
) *
429 (ent
->num_sam_entries
+
433 DEBUG(0,("get_sam_user_entries realloc failed.\n"));
434 SAFE_FREE(name_list
);
440 for (i
= 0; i
< num_entries
; i
++) {
441 /* Store account name and gecos */
442 if (!info
[i
].acct_name
) {
443 fstrcpy(name_list
[ent
->num_sam_entries
+ i
].name
, "");
445 fstrcpy(name_list
[ent
->num_sam_entries
+ i
].name
,
448 if (!info
[i
].full_name
) {
449 fstrcpy(name_list
[ent
->num_sam_entries
+ i
].gecos
, "");
451 fstrcpy(name_list
[ent
->num_sam_entries
+ i
].gecos
,
455 /* User and group ids */
456 sid_copy(&name_list
[ent
->num_sam_entries
+i
].user_sid
, info
[i
].user_sid
);
457 sid_copy(&name_list
[ent
->num_sam_entries
+i
].group_sid
, info
[i
].group_sid
);
460 ent
->num_sam_entries
+= num_entries
;
462 /* Fill in remaining fields */
464 ent
->sam_entries
= name_list
;
465 ent
->sam_entry_index
= 0;
466 result
= ent
->num_sam_entries
> 0;
470 talloc_destroy(mem_ctx
);
475 /* Fetch next passwd entry from ntdom database */
477 #define MAX_GETPWENT_USERS 500
479 enum winbindd_result
winbindd_getpwent(struct winbindd_cli_state
*state
)
481 struct getent_state
*ent
;
482 struct winbindd_pw
*user_list
;
483 int num_users
, user_list_ndx
= 0, i
;
485 DEBUG(3, ("[%5lu]: getpwent\n", (unsigned long)state
->pid
));
487 /* Check user has enabled this */
489 if (!lp_winbind_enum_users())
490 return WINBINDD_ERROR
;
492 /* Allocate space for returning a chunk of users */
494 num_users
= MIN(MAX_GETPWENT_USERS
, state
->request
.data
.num_entries
);
496 if ((state
->response
.extra_data
=
497 malloc(num_users
* sizeof(struct winbindd_pw
))) == NULL
)
498 return WINBINDD_ERROR
;
500 memset(state
->response
.extra_data
, 0, num_users
*
501 sizeof(struct winbindd_pw
));
503 user_list
= (struct winbindd_pw
*)state
->response
.extra_data
;
505 if (!(ent
= state
->getpwent_state
))
506 return WINBINDD_ERROR
;
508 /* Start sending back users */
510 for (i
= 0; i
< num_users
; i
++) {
511 struct getpwent_user
*name_list
= NULL
;
514 /* Do we need to fetch another chunk of users? */
516 if (ent
->num_sam_entries
== ent
->sam_entry_index
) {
518 while(ent
&& !get_sam_user_entries(ent
)) {
519 struct getent_state
*next_ent
;
521 /* Free state information for this domain */
523 SAFE_FREE(ent
->sam_entries
);
525 next_ent
= ent
->next
;
526 DLIST_REMOVE(state
->getpwent_state
, ent
);
532 /* No more domains */
538 name_list
= ent
->sam_entries
;
540 /* Lookup user info */
542 result
= winbindd_fill_pwent(
544 name_list
[ent
->sam_entry_index
].name
,
545 &name_list
[ent
->sam_entry_index
].user_sid
,
546 &name_list
[ent
->sam_entry_index
].group_sid
,
547 name_list
[ent
->sam_entry_index
].gecos
,
548 &user_list
[user_list_ndx
]);
550 ent
->sam_entry_index
++;
552 /* Add user to return list */
557 state
->response
.data
.num_entries
++;
558 state
->response
.length
+=
559 sizeof(struct winbindd_pw
);
562 DEBUG(1, ("could not lookup domain user %s\n",
563 name_list
[ent
->sam_entry_index
].name
));
568 return (user_list_ndx
> 0) ? WINBINDD_OK
: WINBINDD_ERROR
;
571 /* List domain users without mapping to unix ids */
573 enum winbindd_result
winbindd_list_users(struct winbindd_cli_state
*state
)
575 struct winbindd_domain
*domain
;
576 WINBIND_USERINFO
*info
;
577 const char *which_domain
;
578 uint32 num_entries
= 0, total_entries
= 0;
579 char *ted
, *extra_data
= NULL
;
580 int extra_data_len
= 0;
582 enum winbindd_result rv
= WINBINDD_ERROR
;
584 DEBUG(3, ("[%5lu]: list users\n", (unsigned long)state
->pid
));
586 if (!(mem_ctx
= talloc_init("winbindd_list_users")))
587 return WINBINDD_ERROR
;
589 /* Ensure null termination */
590 state
->request
.domain_name
[sizeof(state
->request
.domain_name
)-1]='\0';
591 which_domain
= state
->request
.domain_name
;
593 /* Enumerate over trusted domains */
595 for (domain
= domain_list(); domain
; domain
= domain
->next
) {
597 struct winbindd_methods
*methods
;
600 /* if we have a domain name restricting the request and this
601 one in the list doesn't match, then just bypass the remainder
604 if ( *which_domain
&& !strequal(which_domain
, domain
->name
) )
607 methods
= domain
->methods
;
609 /* Query display info */
610 status
= methods
->query_user_list(domain
, mem_ctx
,
611 &num_entries
, &info
);
613 if (num_entries
== 0)
616 /* Allocate some memory for extra data */
617 total_entries
+= num_entries
;
619 ted
= Realloc(extra_data
, sizeof(fstring
) * total_entries
);
622 DEBUG(0,("failed to enlarge buffer!\n"));
623 SAFE_FREE(extra_data
);
628 /* Pack user list into extra data fields */
630 for (i
= 0; i
< num_entries
; i
++) {
631 fstring acct_name
, name
;
633 if (!info
[i
].acct_name
) {
634 fstrcpy(acct_name
, "");
636 fstrcpy(acct_name
, info
[i
].acct_name
);
639 fill_domain_username(name
, domain
->name
, acct_name
);
641 /* Append to extra data */
642 memcpy(&extra_data
[extra_data_len
], name
,
644 extra_data_len
+= strlen(name
);
645 extra_data
[extra_data_len
++] = ',';
649 /* Assign extra_data fields in response structure */
652 extra_data
[extra_data_len
- 1] = '\0';
653 state
->response
.extra_data
= extra_data
;
654 state
->response
.length
+= extra_data_len
;
657 /* No domains responded but that's still OK so don't return an
664 talloc_destroy(mem_ctx
);