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 /* The substitution of %U and %D in the 'template
45 homedir' is done by talloc_sub_specified() below.
46 If we have an in string (which means the value has already
47 been set in the nss_info backend), then use that.
48 Otherwise use the template value passed in. */
50 if ( in && !strequal(in,"") && lp_security() == SEC_ADS ) {
51 templ = talloc_sub_specified(NULL, in,
55 templ = talloc_sub_specified(NULL, lp_template,
63 safe_strcpy(out, templ, sizeof(fstring) - 1);
69 /* Fill a pwent structure with information we have obtained */
71 static BOOL winbindd_fill_pwent(char *dom_name, char *user_name,
72 DOM_SID *user_sid, DOM_SID *group_sid,
73 char *full_name, char *homedir, char *shell,
74 struct winbindd_pw *pw)
76 fstring output_username;
79 if (!pw || !dom_name || !user_name)
82 /* Resolve the uid number */
84 if (!NT_STATUS_IS_OK(idmap_sid_to_uid(user_sid, &pw->pw_uid))) {
85 DEBUG(1, ("error getting user id for sid %s\n", sid_to_string(sid_string, user_sid)));
89 /* Resolve the gid number */
91 if (!NT_STATUS_IS_OK(idmap_sid_to_gid(group_sid, &pw->pw_gid))) {
92 DEBUG(1, ("error getting group id for sid %s\n", sid_to_string(sid_string, group_sid)));
96 strlower_m(user_name);
100 fill_domain_username(output_username, dom_name, user_name, True);
102 safe_strcpy(pw->pw_name, output_username, sizeof(pw->pw_name) - 1);
104 /* Full name (gecos) */
106 safe_strcpy(pw->pw_gecos, full_name, sizeof(pw->pw_gecos) - 1);
108 /* Home directory and shell */
110 if (!fillup_pw_field(lp_template_homedir(), user_name, dom_name,
111 pw->pw_uid, pw->pw_gid, homedir, pw->pw_dir))
114 if (!fillup_pw_field(lp_template_shell(), user_name, dom_name,
115 pw->pw_uid, pw->pw_gid, shell, pw->pw_shell))
118 /* Password - set to "*" as we can't generate anything useful here.
119 Authentication can be done using the pam_winbind module. */
121 safe_strcpy(pw->pw_passwd, "*", sizeof(pw->pw_passwd) - 1);
126 /* Wrapper for domain->methods->query_user, only on the parent->child pipe */
128 enum winbindd_result winbindd_dual_userinfo(struct winbindd_domain *domain,
129 struct winbindd_cli_state *state)
132 WINBIND_USERINFO user_info;
135 /* Ensure null termination */
136 state->request.data.sid[sizeof(state->request.data.sid)-1]='\0';
138 DEBUG(3, ("[%5lu]: lookupsid %s\n", (unsigned long)state->pid,
139 state->request.data.sid));
141 if (!string_to_sid(&sid, state->request.data.sid)) {
142 DEBUG(5, ("%s not a SID\n", state->request.data.sid));
143 return WINBINDD_ERROR;
146 status = domain->methods->query_user(domain, state->mem_ctx,
148 if (!NT_STATUS_IS_OK(status)) {
149 DEBUG(1, ("error getting user info for sid %s\n",
150 sid_string_static(&sid)));
151 return WINBINDD_ERROR;
154 fstrcpy(state->response.data.user_info.acct_name, user_info.acct_name);
155 fstrcpy(state->response.data.user_info.full_name, user_info.full_name);
156 fstrcpy(state->response.data.user_info.homedir, user_info.homedir);
157 fstrcpy(state->response.data.user_info.shell, user_info.shell);
158 state->response.data.user_info.primary_gid = user_info.primary_gid;
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,
189 static void getpwsid_sid2uid_recv(void *private_data, BOOL success, uid_t uid);
190 static void getpwsid_sid2gid_recv(void *private_data, BOOL success, gid_t gid);
192 static void winbindd_getpwsid(struct winbindd_cli_state *state,
195 struct getpwsid_state *s;
197 s = TALLOC_ZERO_P(state->mem_ctx, struct getpwsid_state);
199 DEBUG(0, ("talloc failed\n"));
204 s->domain = find_domain_from_sid_noinit(sid);
205 if (s->domain == NULL) {
206 DEBUG(3, ("Could not find domain for sid %s\n",
207 sid_string_static(sid)));
211 sid_copy(&s->user_sid, sid);
213 query_user_async(s->state->mem_ctx, s->domain, sid,
214 getpwsid_queryuser_recv, s);
218 request_error(state);
221 static void getpwsid_queryuser_recv(void *private_data, BOOL success,
222 const char *acct_name,
223 const char *full_name,
230 struct getpwsid_state *s =
231 talloc_get_type_abort(private_data, struct getpwsid_state);
234 DEBUG(5, ("Could not query domain %s SID %s\n", s->domain->name,
235 sid_string_static(&s->user_sid)));
236 request_error(s->state);
240 if ( acct_name && *acct_name ) {
241 fstrcpy( username, acct_name );
243 char *domain_name = NULL;
244 enum lsa_SidType type;
245 char *user_name = NULL;
246 struct winbindd_domain *domain = NULL;
248 domain = find_lookup_domain_from_sid(&s->user_sid);
249 winbindd_lookup_name_by_sid(s->state->mem_ctx, domain,
250 &s->user_sid, &domain_name,
253 /* If this still fails we ar4e done. Just error out */
255 DEBUG(5,("Could not obtain a name for SID %s\n",
256 sid_string_static(&s->user_sid)));
257 request_error(s->state);
261 fstrcpy( username, user_name );
264 strlower_m( username );
265 s->username = talloc_strdup(s->state->mem_ctx, username);
267 ws_name_replace( s->username, WB_REPLACE_CHAR );
269 s->fullname = talloc_strdup(s->state->mem_ctx, full_name);
270 s->homedir = talloc_strdup(s->state->mem_ctx, homedir);
271 s->shell = talloc_strdup(s->state->mem_ctx, shell);
273 sid_copy(&s->group_sid, &s->domain->sid);
274 sid_append_rid(&s->group_sid, group_rid);
276 winbindd_sid2uid_async(s->state->mem_ctx, &s->user_sid,
277 getpwsid_sid2uid_recv, s);
280 static void getpwsid_sid2uid_recv(void *private_data, BOOL success, uid_t uid)
282 struct getpwsid_state *s =
283 talloc_get_type_abort(private_data, struct getpwsid_state);
286 DEBUG(5, ("Could not query uid for user %s\\%s\n",
287 s->domain->name, s->username));
288 request_error(s->state);
293 winbindd_sid2gid_async(s->state->mem_ctx, &s->group_sid,
294 getpwsid_sid2gid_recv, s);
297 static void getpwsid_sid2gid_recv(void *private_data, BOOL success, gid_t gid)
299 struct getpwsid_state *s =
300 talloc_get_type_abort(private_data, struct getpwsid_state);
301 struct winbindd_pw *pw;
302 fstring output_username;
304 /* allow the nss backend to override the primary group ID.
305 If the gid has already been set, then keep it.
306 This makes me feel dirty. If the nss backend already
307 gave us a gid, we don't really care whether the sid2gid()
308 call worked or not. --jerry */
310 if ( s->gid == (gid_t)-1 ) {
313 DEBUG(5, ("Could not query gid for user %s\\%s\n",
314 s->domain->name, s->username));
318 /* take what the sid2gid() call gave us */
322 pw = &s->state->response.data.pw;
325 fill_domain_username(output_username, s->domain->name, s->username, True);
326 safe_strcpy(pw->pw_name, output_username, sizeof(pw->pw_name) - 1);
327 safe_strcpy(pw->pw_gecos, s->fullname, sizeof(pw->pw_gecos) - 1);
329 if (!fillup_pw_field(lp_template_homedir(), s->username, s->domain->name,
330 pw->pw_uid, pw->pw_gid, s->homedir, pw->pw_dir)) {
331 DEBUG(5, ("Could not compose homedir\n"));
335 if (!fillup_pw_field(lp_template_shell(), s->username, s->domain->name,
336 pw->pw_uid, pw->pw_gid, s->shell, pw->pw_shell)) {
337 DEBUG(5, ("Could not compose shell\n"));
341 /* Password - set to "*" as we can't generate anything useful here.
342 Authentication can be done using the pam_winbind module. */
344 safe_strcpy(pw->pw_passwd, "*", sizeof(pw->pw_passwd) - 1);
346 request_ok(s->state);
350 request_error(s->state);
353 /* Return a password structure from a username. */
355 static void getpwnam_name2sid_recv(void *private_data, BOOL success,
356 const DOM_SID *sid, enum lsa_SidType type);
358 void winbindd_getpwnam(struct winbindd_cli_state *state)
360 struct winbindd_domain *domain;
361 fstring domname, username;
363 /* Ensure null termination */
364 state->request.data.username[sizeof(state->request.data.username)-1]='\0';
366 DEBUG(3, ("[%5lu]: getpwnam %s\n", (unsigned long)state->pid,
367 state->request.data.username));
369 ws_name_return( state->request.data.username, WB_REPLACE_CHAR );
371 if (!parse_domain_user(state->request.data.username, domname,
373 DEBUG(5, ("Could not parse domain user: %s\n",
374 state->request.data.username));
375 request_error(state);
379 /* Get info for the domain */
381 domain = find_domain_from_name(domname);
383 if (domain == NULL) {
384 DEBUG(7, ("could not find domain entry for domain %s. "
385 "Using primary domain\n", domname));
386 if ( (domain = find_our_domain()) == NULL ) {
387 DEBUG(0,("Cannot find my primary domain structure!\n"));
388 request_error(state);
393 if ( strequal(domname, lp_workgroup()) && lp_winbind_trusted_domains_only() ) {
394 DEBUG(7,("winbindd_getpwnam: My domain -- rejecting getpwnam() for %s\\%s.\n",
396 request_error(state);
400 /* Get rid and name type from name. The following costs 1 packet */
402 winbindd_lookupname_async(state->mem_ctx, domname, username,
403 getpwnam_name2sid_recv, state);
406 static void getpwnam_name2sid_recv(void *private_data, BOOL success,
407 const DOM_SID *sid, enum lsa_SidType type)
409 struct winbindd_cli_state *state =
410 (struct winbindd_cli_state *)private_data;
411 fstring domname, username;
414 DEBUG(5, ("Could not lookup name for user %s\n",
415 state->request.data.username));
416 request_error(state);
420 if ((type != SID_NAME_USER) && (type != SID_NAME_COMPUTER)) {
421 DEBUG(5, ("%s is not a user\n", state->request.data.username));
422 request_error(state);
426 if ( parse_domain_user(state->request.data.username, domname, username) ) {
427 check_domain_trusted( domname, sid );
432 winbindd_getpwsid(state, sid);
435 static void getpwuid_recv(void *private_data, BOOL success, const char *sid)
437 struct winbindd_cli_state *state =
438 (struct winbindd_cli_state *)private_data;
442 DEBUG(10,("uid2sid_recv: uid [%lu] to sid mapping failed\n.",
443 (unsigned long)(state->request.data.uid)));
444 request_error(state);
448 DEBUG(10,("uid2sid_recv: uid %lu has sid %s\n",
449 (unsigned long)(state->request.data.uid), sid));
451 string_to_sid(&user_sid, sid);
452 winbindd_getpwsid(state, &user_sid);
455 /* Return a password structure given a uid number */
456 void winbindd_getpwuid(struct winbindd_cli_state *state)
458 DEBUG(3, ("[%5lu]: getpwuid %lu\n", (unsigned long)state->pid,
459 (unsigned long)state->request.data.uid));
461 /* always query idmap via the async interface */
462 /* if this turns to be too slow we will add here a direct query to the cache */
463 winbindd_uid2sid_async(state->mem_ctx, state->request.data.uid, getpwuid_recv, state);
467 * set/get/endpwent functions
470 /* Rewind file pointer for ntdom passwd database */
472 static BOOL winbindd_setpwent_internal(struct winbindd_cli_state *state)
474 struct winbindd_domain *domain;
476 DEBUG(3, ("[%5lu]: setpwent\n", (unsigned long)state->pid));
478 /* Check user has enabled this */
480 if (!lp_winbind_enum_users()) {
484 /* Free old static data if it exists */
486 if (state->getpwent_state != NULL) {
487 free_getent_state(state->getpwent_state);
488 state->getpwent_state = NULL;
492 /* add any local users we have */
494 if ( (domain_state = (struct getent_state *)malloc(sizeof(struct getent_state))) == NULL )
497 ZERO_STRUCTP(domain_state);
499 /* Add to list of open domains */
501 DLIST_ADD(state->getpwent_state, domain_state);
504 /* Create sam pipes for each domain we know about */
506 for(domain = domain_list(); domain != NULL; domain = domain->next) {
507 struct getent_state *domain_state;
510 /* don't add our domaina if we are a PDC or if we
511 are a member of a Samba domain */
513 if ( (IS_DC || lp_winbind_trusted_domains_only())
514 && strequal(domain->name, lp_workgroup()) )
519 /* Create a state record for this domain */
521 if ((domain_state = SMB_MALLOC_P(struct getent_state)) == NULL) {
522 DEBUG(0, ("malloc failed\n"));
526 ZERO_STRUCTP(domain_state);
528 fstrcpy(domain_state->domain_name, domain->name);
530 /* Add to list of open domains */
532 DLIST_ADD(state->getpwent_state, domain_state);
535 state->getpwent_initialized = True;
539 void winbindd_setpwent(struct winbindd_cli_state *state)
541 if (winbindd_setpwent_internal(state)) {
544 request_error(state);
548 /* Close file pointer to ntdom passwd database */
550 void winbindd_endpwent(struct winbindd_cli_state *state)
552 DEBUG(3, ("[%5lu]: endpwent\n", (unsigned long)state->pid));
554 free_getent_state(state->getpwent_state);
555 state->getpwent_initialized = False;
556 state->getpwent_state = NULL;
560 /* Get partial list of domain users for a domain. We fill in the sam_entries,
561 and num_sam_entries fields with domain user information. The dispinfo_ndx
562 field is incremented to the index of the next user to fetch. Return True if
563 some users were returned, False otherwise. */
565 static BOOL get_sam_user_entries(struct getent_state *ent, TALLOC_CTX *mem_ctx)
569 WINBIND_USERINFO *info;
570 struct getpwent_user *name_list = NULL;
571 struct winbindd_domain *domain;
572 struct winbindd_methods *methods;
575 if (ent->num_sam_entries)
578 if (!(domain = find_domain_from_name(ent->domain_name))) {
579 DEBUG(3, ("no such domain %s in get_sam_user_entries\n",
584 methods = domain->methods;
586 /* Free any existing user info */
588 SAFE_FREE(ent->sam_entries);
589 ent->num_sam_entries = 0;
591 /* Call query_user_list to get a list of usernames and user rids */
595 status = methods->query_user_list(domain, mem_ctx, &num_entries,
598 if (!NT_STATUS_IS_OK(status)) {
599 DEBUG(10,("get_sam_user_entries: query_user_list failed with %s\n",
600 nt_errstr(status) ));
605 name_list = SMB_REALLOC_ARRAY(name_list, struct getpwent_user, ent->num_sam_entries + num_entries);
608 DEBUG(0,("get_sam_user_entries realloc failed.\n"));
613 for (i = 0; i < num_entries; i++) {
614 /* Store account name and gecos */
615 if (!info[i].acct_name) {
616 fstrcpy(name_list[ent->num_sam_entries + i].name, "");
618 fstrcpy(name_list[ent->num_sam_entries + i].name,
621 if (!info[i].full_name) {
622 fstrcpy(name_list[ent->num_sam_entries + i].gecos, "");
624 fstrcpy(name_list[ent->num_sam_entries + i].gecos,
627 if (!info[i].homedir) {
628 fstrcpy(name_list[ent->num_sam_entries + i].homedir, "");
630 fstrcpy(name_list[ent->num_sam_entries + i].homedir,
633 if (!info[i].shell) {
634 fstrcpy(name_list[ent->num_sam_entries + i].shell, "");
636 fstrcpy(name_list[ent->num_sam_entries + i].shell,
641 /* User and group ids */
642 sid_copy(&name_list[ent->num_sam_entries+i].user_sid,
644 sid_copy(&name_list[ent->num_sam_entries+i].group_sid,
648 ent->num_sam_entries += num_entries;
650 /* Fill in remaining fields */
652 ent->sam_entries = name_list;
653 ent->sam_entry_index = 0;
654 return ent->num_sam_entries > 0;
657 /* Fetch next passwd entry from ntdom database */
659 #define MAX_GETPWENT_USERS 500
661 void winbindd_getpwent(struct winbindd_cli_state *state)
663 struct getent_state *ent;
664 struct winbindd_pw *user_list;
665 int num_users, user_list_ndx = 0, i;
667 DEBUG(3, ("[%5lu]: getpwent\n", (unsigned long)state->pid));
669 /* Check user has enabled this */
671 if (!lp_winbind_enum_users()) {
672 request_error(state);
676 /* Allocate space for returning a chunk of users */
678 num_users = MIN(MAX_GETPWENT_USERS, state->request.data.num_entries);
680 if ((state->response.extra_data.data = SMB_MALLOC_ARRAY(struct winbindd_pw, num_users)) == NULL) {
681 request_error(state);
685 memset(state->response.extra_data.data, 0, num_users *
686 sizeof(struct winbindd_pw));
688 user_list = (struct winbindd_pw *)state->response.extra_data.data;
690 if (!state->getpwent_initialized)
691 winbindd_setpwent_internal(state);
693 if (!(ent = state->getpwent_state)) {
694 request_error(state);
698 /* Start sending back users */
700 for (i = 0; i < num_users; i++) {
701 struct getpwent_user *name_list = NULL;
704 /* Do we need to fetch another chunk of users? */
706 if (ent->num_sam_entries == ent->sam_entry_index) {
709 !get_sam_user_entries(ent, state->mem_ctx)) {
710 struct getent_state *next_ent;
712 /* Free state information for this domain */
714 SAFE_FREE(ent->sam_entries);
716 next_ent = ent->next;
717 DLIST_REMOVE(state->getpwent_state, ent);
723 /* No more domains */
729 name_list = (struct getpwent_user *)ent->sam_entries;
731 /* Lookup user info */
733 result = winbindd_fill_pwent(
735 name_list[ent->sam_entry_index].name,
736 &name_list[ent->sam_entry_index].user_sid,
737 &name_list[ent->sam_entry_index].group_sid,
738 name_list[ent->sam_entry_index].gecos,
739 name_list[ent->sam_entry_index].homedir,
740 name_list[ent->sam_entry_index].shell,
741 &user_list[user_list_ndx]);
743 ent->sam_entry_index++;
745 /* Add user to return list */
750 state->response.data.num_entries++;
751 state->response.length +=
752 sizeof(struct winbindd_pw);
755 DEBUG(1, ("could not lookup domain user %s\n",
756 name_list[ent->sam_entry_index].name));
761 if (user_list_ndx > 0)
764 request_error(state);
767 /* List domain users without mapping to unix ids */
769 void winbindd_list_users(struct winbindd_cli_state *state)
771 struct winbindd_domain *domain;
772 WINBIND_USERINFO *info;
773 const char *which_domain;
774 uint32 num_entries = 0, total_entries = 0;
775 char *extra_data = NULL;
776 int extra_data_len = 0;
777 enum winbindd_result rv = WINBINDD_ERROR;
779 DEBUG(3, ("[%5lu]: list users\n", (unsigned long)state->pid));
781 /* Ensure null termination */
782 state->request.domain_name[sizeof(state->request.domain_name)-1]='\0';
783 which_domain = state->request.domain_name;
785 /* Enumerate over trusted domains */
787 for (domain = domain_list(); domain; domain = domain->next) {
789 struct winbindd_methods *methods;
792 /* if we have a domain name restricting the request and this
793 one in the list doesn't match, then just bypass the remainder
796 if ( *which_domain && !strequal(which_domain, domain->name) )
799 methods = domain->methods;
801 /* Query display info */
802 status = methods->query_user_list(domain, state->mem_ctx,
803 &num_entries, &info);
805 if (!NT_STATUS_IS_OK(status)) {
809 if (num_entries == 0)
812 /* Allocate some memory for extra data */
813 total_entries += num_entries;
815 extra_data = (char *)SMB_REALLOC(
816 extra_data, sizeof(fstring) * total_entries);
819 DEBUG(0,("failed to enlarge buffer!\n"));
823 /* Pack user list into extra data fields */
825 for (i = 0; i < num_entries; i++) {
826 fstring acct_name, name;
828 if (!info[i].acct_name) {
829 fstrcpy(acct_name, "");
831 fstrcpy(acct_name, info[i].acct_name);
834 fill_domain_username(name, domain->name, acct_name, True);
836 /* Append to extra data */
837 memcpy(&extra_data[extra_data_len], name,
839 extra_data_len += strlen(name);
840 extra_data[extra_data_len++] = ',';
844 /* Assign extra_data fields in response structure */
847 extra_data[extra_data_len - 1] = '\0';
848 state->response.extra_data.data = extra_data;
849 state->response.length += extra_data_len;
852 /* No domains responded but that's still OK so don't return an
859 if (rv == WINBINDD_OK)
862 request_error(state);