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 static BOOL
fillup_pw_field(const char *lp_template
,
44 if (in
&& !strequal(in
,"") && lp_security() == SEC_ADS
&& (get_nss_info(domname
))) {
45 safe_strcpy(out
, in
, sizeof(fstring
) - 1);
49 /* Home directory and shell - use template config parameters. The
50 defaults are /tmp for the home directory and /bin/false for
53 /* The substitution of %U and %D in the 'template homedir' is done
54 by talloc_sub_specified() below. */
56 templ
= talloc_sub_specified(NULL
, lp_template
, username
, domname
,
62 safe_strcpy(out
, templ
, sizeof(fstring
) - 1);
68 /* Fill a pwent structure with information we have obtained */
70 static BOOL
winbindd_fill_pwent(char *dom_name
, char *user_name
,
71 DOM_SID
*user_sid
, DOM_SID
*group_sid
,
72 char *full_name
, char *homedir
, char *shell
,
73 struct winbindd_pw
*pw
)
75 fstring output_username
;
78 if (!pw
|| !dom_name
|| !user_name
)
81 /* Resolve the uid number */
83 if (!NT_STATUS_IS_OK(idmap_sid_to_uid(user_sid
, &pw
->pw_uid
, 0))) {
84 DEBUG(1, ("error getting user id for sid %s\n", sid_to_string(sid_string
, user_sid
)));
88 /* Resolve the gid number */
90 if (!NT_STATUS_IS_OK(idmap_sid_to_gid(group_sid
, &pw
->pw_gid
, 0))) {
91 DEBUG(1, ("error getting group id for sid %s\n", sid_to_string(sid_string
, group_sid
)));
95 strlower_m(user_name
);
99 fill_domain_username(output_username
, dom_name
, user_name
, True
);
101 safe_strcpy(pw
->pw_name
, output_username
, sizeof(pw
->pw_name
) - 1);
103 /* Full name (gecos) */
105 safe_strcpy(pw
->pw_gecos
, full_name
, sizeof(pw
->pw_gecos
) - 1);
107 /* Home directory and shell - use template config parameters. The
108 defaults are /tmp for the home directory and /bin/false for
111 if (!fillup_pw_field(lp_template_homedir(), user_name
, dom_name
,
112 pw
->pw_uid
, pw
->pw_gid
, homedir
, pw
->pw_dir
))
115 if (!fillup_pw_field(lp_template_shell(), user_name
, dom_name
,
116 pw
->pw_uid
, pw
->pw_gid
, shell
, pw
->pw_shell
))
119 /* Password - set to "*" as we can't generate anything useful here.
120 Authentication can be done using the pam_winbind module. */
122 safe_strcpy(pw
->pw_passwd
, "*", sizeof(pw
->pw_passwd
) - 1);
127 /* Wrapper for domain->methods->query_user, only on the parent->child pipe */
129 enum winbindd_result
winbindd_dual_userinfo(struct winbindd_domain
*domain
,
130 struct winbindd_cli_state
*state
)
133 WINBIND_USERINFO user_info
;
136 /* Ensure null termination */
137 state
->request
.data
.sid
[sizeof(state
->request
.data
.sid
)-1]='\0';
139 DEBUG(3, ("[%5lu]: lookupsid %s\n", (unsigned long)state
->pid
,
140 state
->request
.data
.sid
));
142 if (!string_to_sid(&sid
, state
->request
.data
.sid
)) {
143 DEBUG(5, ("%s not a SID\n", state
->request
.data
.sid
));
144 return WINBINDD_ERROR
;
147 status
= domain
->methods
->query_user(domain
, state
->mem_ctx
,
149 if (!NT_STATUS_IS_OK(status
)) {
150 DEBUG(1, ("error getting user info for sid %s\n",
151 sid_string_static(&sid
)));
152 return WINBINDD_ERROR
;
155 fstrcpy(state
->response
.data
.user_info
.acct_name
, user_info
.acct_name
);
156 fstrcpy(state
->response
.data
.user_info
.full_name
, user_info
.full_name
);
157 fstrcpy(state
->response
.data
.user_info
.homedir
, user_info
.homedir
);
158 fstrcpy(state
->response
.data
.user_info
.shell
, user_info
.shell
);
159 if (!sid_peek_check_rid(&domain
->sid
, &user_info
.group_sid
,
160 &state
->response
.data
.user_info
.group_rid
)) {
161 DEBUG(1, ("Could not extract group rid out of %s\n",
162 sid_string_static(&sid
)));
163 return WINBINDD_ERROR
;
169 struct getpwsid_state
{
170 struct winbindd_cli_state
*state
;
171 struct winbindd_domain
*domain
;
182 static void getpwsid_queryuser_recv(void *private_data
, BOOL success
,
183 const char *acct_name
,
184 const char *full_name
,
188 static void getpwsid_sid2uid_recv(void *private_data
, BOOL success
, uid_t uid
);
189 static void getpwsid_sid2gid_recv(void *private_data
, BOOL success
, gid_t gid
);
191 static void winbindd_getpwsid(struct winbindd_cli_state
*state
,
194 struct getpwsid_state
*s
;
196 s
= TALLOC_ZERO_P(state
->mem_ctx
, struct getpwsid_state
);
198 DEBUG(0, ("talloc failed\n"));
203 s
->domain
= find_domain_from_sid_noinit(sid
);
204 if (s
->domain
== NULL
) {
205 DEBUG(3, ("Could not find domain for sid %s\n",
206 sid_string_static(sid
)));
210 sid_copy(&s
->user_sid
, sid
);
212 query_user_async(s
->state
->mem_ctx
, s
->domain
, sid
,
213 getpwsid_queryuser_recv
, s
);
217 request_error(state
);
220 static void getpwsid_queryuser_recv(void *private_data
, BOOL success
,
221 const char *acct_name
,
222 const char *full_name
,
228 struct getpwsid_state
*s
=
229 talloc_get_type_abort(private_data
, struct getpwsid_state
);
232 DEBUG(5, ("Could not query domain %s SID %s\n", s
->domain
->name
,
233 sid_string_static(&s
->user_sid
)));
234 request_error(s
->state
);
238 fstrcpy( username
, acct_name
);
239 strlower_m( username
);
240 s
->username
= talloc_strdup(s
->state
->mem_ctx
, username
);
241 s
->fullname
= talloc_strdup(s
->state
->mem_ctx
, full_name
);
242 s
->homedir
= talloc_strdup(s
->state
->mem_ctx
, homedir
);
243 s
->shell
= talloc_strdup(s
->state
->mem_ctx
, shell
);
244 sid_copy(&s
->group_sid
, &s
->domain
->sid
);
245 sid_append_rid(&s
->group_sid
, group_rid
);
247 winbindd_sid2uid_async(s
->state
->mem_ctx
, &s
->user_sid
,
248 getpwsid_sid2uid_recv
, s
);
251 static void getpwsid_sid2uid_recv(void *private_data
, BOOL success
, uid_t uid
)
253 struct getpwsid_state
*s
=
254 talloc_get_type_abort(private_data
, struct getpwsid_state
);
257 DEBUG(5, ("Could not query user's %s\\%s uid\n",
258 s
->domain
->name
, s
->username
));
259 request_error(s
->state
);
264 winbindd_sid2gid_async(s
->state
->mem_ctx
, &s
->group_sid
,
265 getpwsid_sid2gid_recv
, s
);
268 static void getpwsid_sid2gid_recv(void *private_data
, BOOL success
, gid_t gid
)
270 struct getpwsid_state
*s
=
271 talloc_get_type_abort(private_data
, struct getpwsid_state
);
272 struct winbindd_pw
*pw
;
273 fstring output_username
;
276 DEBUG(5, ("Could not query user's %s\\%s\n gid",
277 s
->domain
->name
, s
->username
));
283 pw
= &s
->state
->response
.data
.pw
;
286 fill_domain_username(output_username
, s
->domain
->name
, s
->username
, True
);
287 safe_strcpy(pw
->pw_name
, output_username
, sizeof(pw
->pw_name
) - 1);
288 safe_strcpy(pw
->pw_gecos
, s
->fullname
, sizeof(pw
->pw_gecos
) - 1);
290 if (!fillup_pw_field(lp_template_homedir(), s
->username
, s
->domain
->name
,
291 pw
->pw_uid
, pw
->pw_gid
, s
->homedir
, pw
->pw_dir
)) {
292 DEBUG(5, ("Could not compose homedir\n"));
296 if (!fillup_pw_field(lp_template_shell(), s
->username
, s
->domain
->name
,
297 pw
->pw_uid
, pw
->pw_gid
, s
->shell
, pw
->pw_shell
)) {
298 DEBUG(5, ("Could not compose shell\n"));
302 /* Password - set to "*" as we can't generate anything useful here.
303 Authentication can be done using the pam_winbind module. */
305 safe_strcpy(pw
->pw_passwd
, "*", sizeof(pw
->pw_passwd
) - 1);
307 request_ok(s
->state
);
311 request_error(s
->state
);
314 /* Return a password structure from a username. */
316 static void getpwnam_name2sid_recv(void *private_data
, BOOL success
,
317 const DOM_SID
*sid
, enum SID_NAME_USE type
);
319 void winbindd_getpwnam(struct winbindd_cli_state
*state
)
321 struct winbindd_domain
*domain
;
322 fstring domname
, username
;
324 /* Ensure null termination */
325 state
->request
.data
.username
[sizeof(state
->request
.data
.username
)-1]='\0';
327 DEBUG(3, ("[%5lu]: getpwnam %s\n", (unsigned long)state
->pid
,
328 state
->request
.data
.username
));
330 if (!parse_domain_user(state
->request
.data
.username
, domname
,
332 DEBUG(5, ("Could not parse domain user: %s\n",
333 state
->request
.data
.username
));
334 request_error(state
);
338 /* Get info for the domain */
340 domain
= find_domain_from_name(domname
);
342 if (domain
== NULL
) {
343 DEBUG(7, ("could not find domain entry for domain %s\n",
345 request_error(state
);
349 if ( strequal(domname
, lp_workgroup()) && lp_winbind_trusted_domains_only() ) {
350 DEBUG(7,("winbindd_getpwnam: My domain -- rejecting getpwnam() for %s\\%s.\n",
352 request_error(state
);
356 /* Get rid and name type from name. The following costs 1 packet */
358 winbindd_lookupname_async(state
->mem_ctx
, domname
, username
,
359 getpwnam_name2sid_recv
, state
);
362 static void getpwnam_name2sid_recv(void *private_data
, BOOL success
,
363 const DOM_SID
*sid
, enum SID_NAME_USE type
)
365 struct winbindd_cli_state
*state
=
366 (struct winbindd_cli_state
*)private_data
;
369 DEBUG(5, ("Could not lookup name for user %s\n",
370 state
->request
.data
.username
));
371 request_error(state
);
375 if ((type
!= SID_NAME_USER
) && (type
!= SID_NAME_COMPUTER
)) {
376 DEBUG(5, ("%s is not a user\n", state
->request
.data
.username
));
377 request_error(state
);
381 winbindd_getpwsid(state
, sid
);
384 static void getpwuid_recv(void *private_data
, BOOL success
, const char *sid
)
386 struct winbindd_cli_state
*state
=
387 (struct winbindd_cli_state
*)private_data
;
391 DEBUG(10,("uid2sid_recv: uid [%lu] to sid mapping failed\n.",
392 (unsigned long)(state
->request
.data
.uid
)));
393 request_error(state
);
397 DEBUG(10,("uid2sid_recv: uid %lu has sid %s\n",
398 (unsigned long)(state
->request
.data
.uid
), sid
));
400 string_to_sid(&user_sid
, sid
);
401 winbindd_getpwsid(state
, &user_sid
);
404 /* Return a password structure given a uid number */
405 void winbindd_getpwuid(struct winbindd_cli_state
*state
)
410 /* Bug out if the uid isn't in the winbind range */
411 if ((state
->request
.data
.uid
< server_state
.uid_low
) ||
412 (state
->request
.data
.uid
> server_state
.uid_high
)) {
413 request_error(state
);
417 DEBUG(3, ("[%5lu]: getpwuid %lu\n", (unsigned long)state
->pid
,
418 (unsigned long)state
->request
.data
.uid
));
420 status
= idmap_uid_to_sid(&user_sid
, state
->request
.data
.uid
,
421 IDMAP_FLAG_QUERY_ONLY
| IDMAP_FLAG_CACHE_ONLY
);
423 if (NT_STATUS_IS_OK(status
)) {
424 winbindd_getpwsid(state
, &user_sid
);
428 DEBUG(10,("Could not find SID for uid %lu in the cache. Querying idmap backend\n",
429 (unsigned long)state
->request
.data
.uid
));
431 winbindd_uid2sid_async(state
->mem_ctx
, state
->request
.data
.uid
, getpwuid_recv
, state
);
435 * set/get/endpwent functions
438 /* Rewind file pointer for ntdom passwd database */
440 static BOOL
winbindd_setpwent_internal(struct winbindd_cli_state
*state
)
442 struct winbindd_domain
*domain
;
444 DEBUG(3, ("[%5lu]: setpwent\n", (unsigned long)state
->pid
));
446 /* Check user has enabled this */
448 if (!lp_winbind_enum_users()) {
452 /* Free old static data if it exists */
454 if (state
->getpwent_state
!= NULL
) {
455 free_getent_state(state
->getpwent_state
);
456 state
->getpwent_state
= NULL
;
460 /* add any local users we have */
462 if ( (domain_state
= (struct getent_state
*)malloc(sizeof(struct getent_state
))) == NULL
)
465 ZERO_STRUCTP(domain_state
);
467 /* Add to list of open domains */
469 DLIST_ADD(state
->getpwent_state
, domain_state
);
472 /* Create sam pipes for each domain we know about */
474 for(domain
= domain_list(); domain
!= NULL
; domain
= domain
->next
) {
475 struct getent_state
*domain_state
;
478 /* don't add our domaina if we are a PDC or if we
479 are a member of a Samba domain */
481 if ( (IS_DC
|| lp_winbind_trusted_domains_only())
482 && strequal(domain
->name
, lp_workgroup()) )
487 /* Create a state record for this domain */
489 if ((domain_state
= SMB_MALLOC_P(struct getent_state
)) == NULL
) {
490 DEBUG(0, ("malloc failed\n"));
494 ZERO_STRUCTP(domain_state
);
496 fstrcpy(domain_state
->domain_name
, domain
->name
);
498 /* Add to list of open domains */
500 DLIST_ADD(state
->getpwent_state
, domain_state
);
503 state
->getpwent_initialized
= True
;
507 void winbindd_setpwent(struct winbindd_cli_state
*state
)
509 if (winbindd_setpwent_internal(state
)) {
512 request_error(state
);
516 /* Close file pointer to ntdom passwd database */
518 void winbindd_endpwent(struct winbindd_cli_state
*state
)
520 DEBUG(3, ("[%5lu]: endpwent\n", (unsigned long)state
->pid
));
522 free_getent_state(state
->getpwent_state
);
523 state
->getpwent_initialized
= False
;
524 state
->getpwent_state
= NULL
;
528 /* Get partial list of domain users for a domain. We fill in the sam_entries,
529 and num_sam_entries fields with domain user information. The dispinfo_ndx
530 field is incremented to the index of the next user to fetch. Return True if
531 some users were returned, False otherwise. */
533 static BOOL
get_sam_user_entries(struct getent_state
*ent
, TALLOC_CTX
*mem_ctx
)
537 WINBIND_USERINFO
*info
;
538 struct getpwent_user
*name_list
= NULL
;
539 struct winbindd_domain
*domain
;
540 struct winbindd_methods
*methods
;
543 if (ent
->num_sam_entries
)
546 if (!(domain
= find_domain_from_name(ent
->domain_name
))) {
547 DEBUG(3, ("no such domain %s in get_sam_user_entries\n",
552 methods
= domain
->methods
;
554 /* Free any existing user info */
556 SAFE_FREE(ent
->sam_entries
);
557 ent
->num_sam_entries
= 0;
559 /* Call query_user_list to get a list of usernames and user rids */
563 status
= methods
->query_user_list(domain
, mem_ctx
, &num_entries
,
566 if (!NT_STATUS_IS_OK(status
)) {
567 DEBUG(10,("get_sam_user_entries: query_user_list failed with %s\n",
568 nt_errstr(status
) ));
573 name_list
= SMB_REALLOC_ARRAY(name_list
, struct getpwent_user
, ent
->num_sam_entries
+ num_entries
);
576 DEBUG(0,("get_sam_user_entries realloc failed.\n"));
581 for (i
= 0; i
< num_entries
; i
++) {
582 /* Store account name and gecos */
583 if (!info
[i
].acct_name
) {
584 fstrcpy(name_list
[ent
->num_sam_entries
+ i
].name
, "");
586 fstrcpy(name_list
[ent
->num_sam_entries
+ i
].name
,
589 if (!info
[i
].full_name
) {
590 fstrcpy(name_list
[ent
->num_sam_entries
+ i
].gecos
, "");
592 fstrcpy(name_list
[ent
->num_sam_entries
+ i
].gecos
,
595 if (!info
[i
].homedir
) {
596 fstrcpy(name_list
[ent
->num_sam_entries
+ i
].homedir
, "");
598 fstrcpy(name_list
[ent
->num_sam_entries
+ i
].homedir
,
601 if (!info
[i
].shell
) {
602 fstrcpy(name_list
[ent
->num_sam_entries
+ i
].shell
, "");
604 fstrcpy(name_list
[ent
->num_sam_entries
+ i
].shell
,
609 /* User and group ids */
610 sid_copy(&name_list
[ent
->num_sam_entries
+i
].user_sid
,
612 sid_copy(&name_list
[ent
->num_sam_entries
+i
].group_sid
,
616 ent
->num_sam_entries
+= num_entries
;
618 /* Fill in remaining fields */
620 ent
->sam_entries
= name_list
;
621 ent
->sam_entry_index
= 0;
622 return ent
->num_sam_entries
> 0;
625 /* Fetch next passwd entry from ntdom database */
627 #define MAX_GETPWENT_USERS 500
629 void winbindd_getpwent(struct winbindd_cli_state
*state
)
631 struct getent_state
*ent
;
632 struct winbindd_pw
*user_list
;
633 int num_users
, user_list_ndx
= 0, i
;
635 DEBUG(3, ("[%5lu]: getpwent\n", (unsigned long)state
->pid
));
637 /* Check user has enabled this */
639 if (!lp_winbind_enum_users()) {
640 request_error(state
);
644 /* Allocate space for returning a chunk of users */
646 num_users
= MIN(MAX_GETPWENT_USERS
, state
->request
.data
.num_entries
);
648 if ((state
->response
.extra_data
.data
= SMB_MALLOC_ARRAY(struct winbindd_pw
, num_users
)) == NULL
) {
649 request_error(state
);
653 memset(state
->response
.extra_data
.data
, 0, num_users
*
654 sizeof(struct winbindd_pw
));
656 user_list
= (struct winbindd_pw
*)state
->response
.extra_data
.data
;
658 if (!state
->getpwent_initialized
)
659 winbindd_setpwent_internal(state
);
661 if (!(ent
= state
->getpwent_state
)) {
662 request_error(state
);
666 /* Start sending back users */
668 for (i
= 0; i
< num_users
; i
++) {
669 struct getpwent_user
*name_list
= NULL
;
672 /* Do we need to fetch another chunk of users? */
674 if (ent
->num_sam_entries
== ent
->sam_entry_index
) {
677 !get_sam_user_entries(ent
, state
->mem_ctx
)) {
678 struct getent_state
*next_ent
;
680 /* Free state information for this domain */
682 SAFE_FREE(ent
->sam_entries
);
684 next_ent
= ent
->next
;
685 DLIST_REMOVE(state
->getpwent_state
, ent
);
691 /* No more domains */
697 name_list
= (struct getpwent_user
*)ent
->sam_entries
;
699 /* Lookup user info */
701 result
= winbindd_fill_pwent(
703 name_list
[ent
->sam_entry_index
].name
,
704 &name_list
[ent
->sam_entry_index
].user_sid
,
705 &name_list
[ent
->sam_entry_index
].group_sid
,
706 name_list
[ent
->sam_entry_index
].gecos
,
707 name_list
[ent
->sam_entry_index
].homedir
,
708 name_list
[ent
->sam_entry_index
].shell
,
709 &user_list
[user_list_ndx
]);
711 ent
->sam_entry_index
++;
713 /* Add user to return list */
718 state
->response
.data
.num_entries
++;
719 state
->response
.length
+=
720 sizeof(struct winbindd_pw
);
723 DEBUG(1, ("could not lookup domain user %s\n",
724 name_list
[ent
->sam_entry_index
].name
));
729 if (user_list_ndx
> 0)
732 request_error(state
);
735 /* List domain users without mapping to unix ids */
737 void winbindd_list_users(struct winbindd_cli_state
*state
)
739 struct winbindd_domain
*domain
;
740 WINBIND_USERINFO
*info
;
741 const char *which_domain
;
742 uint32 num_entries
= 0, total_entries
= 0;
743 char *extra_data
= NULL
;
744 int extra_data_len
= 0;
745 enum winbindd_result rv
= WINBINDD_ERROR
;
747 DEBUG(3, ("[%5lu]: list users\n", (unsigned long)state
->pid
));
749 /* Ensure null termination */
750 state
->request
.domain_name
[sizeof(state
->request
.domain_name
)-1]='\0';
751 which_domain
= state
->request
.domain_name
;
753 /* Enumerate over trusted domains */
755 for (domain
= domain_list(); domain
; domain
= domain
->next
) {
757 struct winbindd_methods
*methods
;
760 /* if we have a domain name restricting the request and this
761 one in the list doesn't match, then just bypass the remainder
764 if ( *which_domain
&& !strequal(which_domain
, domain
->name
) )
767 methods
= domain
->methods
;
769 /* Query display info */
770 status
= methods
->query_user_list(domain
, state
->mem_ctx
,
771 &num_entries
, &info
);
773 if (!NT_STATUS_IS_OK(status
)) {
777 if (num_entries
== 0)
780 /* Allocate some memory for extra data */
781 total_entries
+= num_entries
;
783 extra_data
= (char *)SMB_REALLOC(
784 extra_data
, sizeof(fstring
) * total_entries
);
787 DEBUG(0,("failed to enlarge buffer!\n"));
791 /* Pack user list into extra data fields */
793 for (i
= 0; i
< num_entries
; i
++) {
794 fstring acct_name
, name
;
796 if (!info
[i
].acct_name
) {
797 fstrcpy(acct_name
, "");
799 fstrcpy(acct_name
, info
[i
].acct_name
);
802 fill_domain_username(name
, domain
->name
, acct_name
, True
);
804 /* Append to extra data */
805 memcpy(&extra_data
[extra_data_len
], name
,
807 extra_data_len
+= strlen(name
);
808 extra_data
[extra_data_len
++] = ',';
812 /* Assign extra_data fields in response structure */
815 extra_data
[extra_data_len
- 1] = '\0';
816 state
->response
.extra_data
.data
= extra_data
;
817 state
->response
.length
+= extra_data_len
;
820 /* No domains responded but that's still OK so don't return an
827 if (rv
== WINBINDD_OK
)
830 request_error(state
);