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_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 user %s\\%s\n", s->domain->name,
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 = private_data;
368 DEBUG(5, ("Could not lookup name for user %s\n",
369 state->request.data.username));
370 request_error(state);
374 if ((type != SID_NAME_USER) && (type != SID_NAME_COMPUTER)) {
375 DEBUG(5, ("%s is not a user\n", state->request.data.username));
376 request_error(state);
380 winbindd_getpwsid(state, sid);
383 /* Return a password structure given a uid number */
385 void winbindd_getpwuid(struct winbindd_cli_state *state)
390 /* Bug out if the uid isn't in the winbind range */
392 if ((state->request.data.uid < server_state.uid_low ) ||
393 (state->request.data.uid > server_state.uid_high)) {
394 request_error(state);
398 DEBUG(3, ("[%5lu]: getpwuid %lu\n", (unsigned long)state->pid,
399 (unsigned long)state->request.data.uid));
401 status = idmap_uid_to_sid(&user_sid, state->request.data.uid,
402 IDMAP_FLAG_QUERY_ONLY | IDMAP_FLAG_CACHE_ONLY);
404 if (!NT_STATUS_IS_OK(status)) {
405 DEBUG(5, ("Could not find SID for uid %lu\n",
406 (unsigned long)state->request.data.uid));
407 request_error(state);
411 winbindd_getpwsid(state, &user_sid);
415 * set/get/endpwent functions
418 /* Rewind file pointer for ntdom passwd database */
420 static BOOL winbindd_setpwent_internal(struct winbindd_cli_state *state)
422 struct winbindd_domain *domain;
424 DEBUG(3, ("[%5lu]: setpwent\n", (unsigned long)state->pid));
426 /* Check user has enabled this */
428 if (!lp_winbind_enum_users()) {
432 /* Free old static data if it exists */
434 if (state->getpwent_state != NULL) {
435 free_getent_state(state->getpwent_state);
436 state->getpwent_state = NULL;
440 /* add any local users we have */
442 if ( (domain_state = (struct getent_state *)malloc(sizeof(struct getent_state))) == NULL )
445 ZERO_STRUCTP(domain_state);
447 /* Add to list of open domains */
449 DLIST_ADD(state->getpwent_state, domain_state);
452 /* Create sam pipes for each domain we know about */
454 for(domain = domain_list(); domain != NULL; domain = domain->next) {
455 struct getent_state *domain_state;
458 /* don't add our domaina if we are a PDC or if we
459 are a member of a Samba domain */
461 if ( (IS_DC || lp_winbind_trusted_domains_only())
462 && strequal(domain->name, lp_workgroup()) )
467 /* Create a state record for this domain */
469 if ((domain_state = SMB_MALLOC_P(struct getent_state)) == NULL) {
470 DEBUG(0, ("malloc failed\n"));
474 ZERO_STRUCTP(domain_state);
476 fstrcpy(domain_state->domain_name, domain->name);
478 /* Add to list of open domains */
480 DLIST_ADD(state->getpwent_state, domain_state);
483 state->getpwent_initialized = True;
487 void winbindd_setpwent(struct winbindd_cli_state *state)
489 if (winbindd_setpwent_internal(state)) {
492 request_error(state);
496 /* Close file pointer to ntdom passwd database */
498 void winbindd_endpwent(struct winbindd_cli_state *state)
500 DEBUG(3, ("[%5lu]: endpwent\n", (unsigned long)state->pid));
502 free_getent_state(state->getpwent_state);
503 state->getpwent_initialized = False;
504 state->getpwent_state = NULL;
508 /* Get partial list of domain users for a domain. We fill in the sam_entries,
509 and num_sam_entries fields with domain user information. The dispinfo_ndx
510 field is incremented to the index of the next user to fetch. Return True if
511 some users were returned, False otherwise. */
513 static BOOL get_sam_user_entries(struct getent_state *ent, TALLOC_CTX *mem_ctx)
517 WINBIND_USERINFO *info;
518 struct getpwent_user *name_list = NULL;
519 struct winbindd_domain *domain;
520 struct winbindd_methods *methods;
523 if (ent->num_sam_entries)
526 if (!(domain = find_domain_from_name(ent->domain_name))) {
527 DEBUG(3, ("no such domain %s in get_sam_user_entries\n",
532 methods = domain->methods;
534 /* Free any existing user info */
536 SAFE_FREE(ent->sam_entries);
537 ent->num_sam_entries = 0;
539 /* Call query_user_list to get a list of usernames and user rids */
543 status = methods->query_user_list(domain, mem_ctx, &num_entries,
546 if (!NT_STATUS_IS_OK(status)) {
547 DEBUG(10,("get_sam_user_entries: query_user_list failed with %s\n",
548 nt_errstr(status) ));
553 name_list = SMB_REALLOC_ARRAY(name_list, struct getpwent_user, ent->num_sam_entries + num_entries);
556 DEBUG(0,("get_sam_user_entries realloc failed.\n"));
561 for (i = 0; i < num_entries; i++) {
562 /* Store account name and gecos */
563 if (!info[i].acct_name) {
564 fstrcpy(name_list[ent->num_sam_entries + i].name, "");
566 fstrcpy(name_list[ent->num_sam_entries + i].name,
569 if (!info[i].full_name) {
570 fstrcpy(name_list[ent->num_sam_entries + i].gecos, "");
572 fstrcpy(name_list[ent->num_sam_entries + i].gecos,
575 if (!info[i].homedir) {
576 fstrcpy(name_list[ent->num_sam_entries + i].homedir, "");
578 fstrcpy(name_list[ent->num_sam_entries + i].homedir,
581 if (!info[i].shell) {
582 fstrcpy(name_list[ent->num_sam_entries + i].shell, "");
584 fstrcpy(name_list[ent->num_sam_entries + i].shell,
589 /* User and group ids */
590 sid_copy(&name_list[ent->num_sam_entries+i].user_sid,
592 sid_copy(&name_list[ent->num_sam_entries+i].group_sid,
596 ent->num_sam_entries += num_entries;
598 /* Fill in remaining fields */
600 ent->sam_entries = name_list;
601 ent->sam_entry_index = 0;
602 return ent->num_sam_entries > 0;
605 /* Fetch next passwd entry from ntdom database */
607 #define MAX_GETPWENT_USERS 500
609 void winbindd_getpwent(struct winbindd_cli_state *state)
611 struct getent_state *ent;
612 struct winbindd_pw *user_list;
613 int num_users, user_list_ndx = 0, i;
615 DEBUG(3, ("[%5lu]: getpwent\n", (unsigned long)state->pid));
617 /* Check user has enabled this */
619 if (!lp_winbind_enum_users()) {
620 request_error(state);
624 /* Allocate space for returning a chunk of users */
626 num_users = MIN(MAX_GETPWENT_USERS, state->request.data.num_entries);
628 if ((state->response.extra_data.data = SMB_MALLOC_ARRAY(struct winbindd_pw, num_users)) == NULL) {
629 request_error(state);
633 memset(state->response.extra_data.data, 0, num_users *
634 sizeof(struct winbindd_pw));
636 user_list = (struct winbindd_pw *)state->response.extra_data.data;
638 if (!state->getpwent_initialized)
639 winbindd_setpwent_internal(state);
641 if (!(ent = state->getpwent_state)) {
642 request_error(state);
646 /* Start sending back users */
648 for (i = 0; i < num_users; i++) {
649 struct getpwent_user *name_list = NULL;
652 /* Do we need to fetch another chunk of users? */
654 if (ent->num_sam_entries == ent->sam_entry_index) {
657 !get_sam_user_entries(ent, state->mem_ctx)) {
658 struct getent_state *next_ent;
660 /* Free state information for this domain */
662 SAFE_FREE(ent->sam_entries);
664 next_ent = ent->next;
665 DLIST_REMOVE(state->getpwent_state, ent);
671 /* No more domains */
677 name_list = ent->sam_entries;
679 /* Lookup user info */
681 result = winbindd_fill_pwent(
683 name_list[ent->sam_entry_index].name,
684 &name_list[ent->sam_entry_index].user_sid,
685 &name_list[ent->sam_entry_index].group_sid,
686 name_list[ent->sam_entry_index].gecos,
687 name_list[ent->sam_entry_index].homedir,
688 name_list[ent->sam_entry_index].shell,
689 &user_list[user_list_ndx]);
691 ent->sam_entry_index++;
693 /* Add user to return list */
698 state->response.data.num_entries++;
699 state->response.length +=
700 sizeof(struct winbindd_pw);
703 DEBUG(1, ("could not lookup domain user %s\n",
704 name_list[ent->sam_entry_index].name));
709 if (user_list_ndx > 0)
712 request_error(state);
715 /* List domain users without mapping to unix ids */
717 void winbindd_list_users(struct winbindd_cli_state *state)
719 struct winbindd_domain *domain;
720 WINBIND_USERINFO *info;
721 const char *which_domain;
722 uint32 num_entries = 0, total_entries = 0;
723 char *extra_data = NULL;
724 int extra_data_len = 0;
725 enum winbindd_result rv = WINBINDD_ERROR;
727 DEBUG(3, ("[%5lu]: list users\n", (unsigned long)state->pid));
729 /* Ensure null termination */
730 state->request.domain_name[sizeof(state->request.domain_name)-1]='\0';
731 which_domain = state->request.domain_name;
733 /* Enumerate over trusted domains */
735 for (domain = domain_list(); domain; domain = domain->next) {
737 struct winbindd_methods *methods;
740 /* if we have a domain name restricting the request and this
741 one in the list doesn't match, then just bypass the remainder
744 if ( *which_domain && !strequal(which_domain, domain->name) )
747 methods = domain->methods;
749 /* Query display info */
750 status = methods->query_user_list(domain, state->mem_ctx,
751 &num_entries, &info);
753 if (!NT_STATUS_IS_OK(status)) {
757 if (num_entries == 0)
760 /* Allocate some memory for extra data */
761 total_entries += num_entries;
763 extra_data = SMB_REALLOC(extra_data, sizeof(fstring) * total_entries);
766 DEBUG(0,("failed to enlarge buffer!\n"));
770 /* Pack user list into extra data fields */
772 for (i = 0; i < num_entries; i++) {
773 fstring acct_name, name;
775 if (!info[i].acct_name) {
776 fstrcpy(acct_name, "");
778 fstrcpy(acct_name, info[i].acct_name);
781 fill_domain_username(name, domain->name, acct_name, True);
783 /* Append to extra data */
784 memcpy(&extra_data[extra_data_len], name,
786 extra_data_len += strlen(name);
787 extra_data[extra_data_len++] = ',';
791 /* Assign extra_data fields in response structure */
794 extra_data[extra_data_len - 1] = '\0';
795 state->response.extra_data.data = extra_data;
796 state->response.length += extra_data_len;
799 /* No domains responded but that's still OK so don't return an
806 if (rv == WINBINDD_OK)
809 request_error(state);