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.
28 #define DBGC_CLASS DBGC_WINBIND
30 extern userdom_struct current_user_info
;
32 /* Fill a pwent structure with information we have obtained */
34 static BOOL
winbindd_fill_pwent(char *dom_name
, char *user_name
,
35 DOM_SID
*user_sid
, DOM_SID
*group_sid
,
36 char *full_name
, struct winbindd_pw
*pw
)
38 fstring output_username
;
42 if (!pw
|| !dom_name
|| !user_name
)
45 /* Resolve the uid number */
47 if (!NT_STATUS_IS_OK(idmap_sid_to_uid(user_sid
, &(pw
->pw_uid
), 0))) {
48 DEBUG(1, ("error getting user id for sid %s\n", sid_to_string(sid_string
, user_sid
)));
52 /* Resolve the gid number */
54 if (!NT_STATUS_IS_OK(idmap_sid_to_gid(group_sid
, &(pw
->pw_gid
), 0))) {
55 DEBUG(1, ("error getting group id for sid %s\n", sid_to_string(sid_string
, group_sid
)));
61 fill_domain_username(output_username
, dom_name
, user_name
);
63 safe_strcpy(pw
->pw_name
, output_username
, sizeof(pw
->pw_name
) - 1);
65 /* Full name (gecos) */
67 safe_strcpy(pw
->pw_gecos
, full_name
, sizeof(pw
->pw_gecos
) - 1);
69 /* Home directory and shell - use template config parameters. The
70 defaults are /tmp for the home directory and /bin/false for
73 /* The substitution of %U and %D in the 'template homedir' is done
74 by lp_string() calling standard_sub_basic(). */
76 fstrcpy(current_user_info
.smb_name
, user_name
);
77 sub_set_smb_name(user_name
);
78 fstrcpy(current_user_info
.domain
, dom_name
);
80 pstrcpy(homedir
, lp_template_homedir());
82 safe_strcpy(pw
->pw_dir
, homedir
, sizeof(pw
->pw_dir
) - 1);
84 safe_strcpy(pw
->pw_shell
, lp_template_shell(),
85 sizeof(pw
->pw_shell
) - 1);
87 /* Password - set to "x" as we can't generate anything useful here.
88 Authentication can be done using the pam_winbind module. */
90 safe_strcpy(pw
->pw_passwd
, "x", sizeof(pw
->pw_passwd
) - 1);
95 /* Return a password structure from a username. */
97 enum winbindd_result
winbindd_getpwnam(struct winbindd_cli_state
*state
)
99 WINBIND_USERINFO user_info
;
103 fstring name_domain
, name_user
;
104 enum SID_NAME_USE name_type
;
105 struct winbindd_domain
*domain
;
108 /* Ensure null termination */
109 state
->request
.data
.username
[sizeof(state
->request
.data
.username
)-1]='\0';
111 DEBUG(3, ("[%5lu]: getpwnam %s\n", (unsigned long)state
->pid
,
112 state
->request
.data
.username
));
114 /* Parse domain and username */
116 parse_domain_user(state
->request
.data
.username
,
117 name_domain
, name_user
);
119 /* if this is our local domain (or no domain), the do a local tdb search */
121 if ( !*name_domain
|| strequal(name_domain
, get_global_sam_name()) ) {
122 if ( !(pw
= wb_getpwnam(name_user
)) ) {
123 DEBUG(5,("winbindd_getpwnam: lookup for %s\\%s failed\n",
124 name_domain
, name_user
));
125 return WINBINDD_ERROR
;
127 memcpy( &state
->response
.data
.pw
, pw
, sizeof(WINBINDD_PW
) );
131 /* should we deal with users for our domain? */
133 if ( lp_winbind_trusted_domains_only() && strequal(name_domain
, lp_workgroup())) {
134 DEBUG(7,("winbindd_getpwnam: My domain -- rejecting getpwnam() for %s\\%s.\n",
135 name_domain
, name_user
));
136 return WINBINDD_ERROR
;
139 if ((domain
= find_domain_from_name(name_domain
)) == NULL
) {
140 DEBUG(5, ("no such domain: %s\n", name_domain
));
141 return WINBINDD_ERROR
;
144 /* Get rid and name type from name */
146 if (!winbindd_lookup_sid_by_name(domain
, name_user
, &user_sid
, &name_type
)) {
147 DEBUG(1, ("user '%s' does not exist\n", name_user
));
148 return WINBINDD_ERROR
;
151 if (name_type
!= SID_NAME_USER
) {
152 DEBUG(1, ("name '%s' is not a user name: %d\n", name_user
,
154 return WINBINDD_ERROR
;
157 /* Get some user info. Split the user rid from the sid obtained
158 from the winbind_lookup_by_name() call and use it in a
159 winbind_lookup_userinfo() */
161 if (!(mem_ctx
= talloc_init("winbindd_getpwnam([%s]\\[%s])",
162 name_domain
, name_user
))) {
163 DEBUG(1, ("out of memory\n"));
164 return WINBINDD_ERROR
;
167 status
= domain
->methods
->query_user(domain
, mem_ctx
, &user_sid
,
170 if (!NT_STATUS_IS_OK(status
)) {
171 DEBUG(1, ("error getting user info for user '[%s]\\[%s]'\n",
172 name_domain
, name_user
));
173 talloc_destroy(mem_ctx
);
174 return WINBINDD_ERROR
;
177 /* Now take all this information and fill in a passwd structure */
178 if (!winbindd_fill_pwent(name_domain
, name_user
,
179 user_info
.user_sid
, user_info
.group_sid
,
181 &state
->response
.data
.pw
)) {
182 talloc_destroy(mem_ctx
);
183 return WINBINDD_ERROR
;
186 talloc_destroy(mem_ctx
);
191 /* Return a password structure given a uid number */
193 enum winbindd_result
winbindd_getpwuid(struct winbindd_cli_state
*state
)
196 struct winbindd_domain
*domain
;
200 enum SID_NAME_USE name_type
;
201 WINBIND_USERINFO user_info
;
206 /* Bug out if the uid isn't in the winbind range */
208 if ((state
->request
.data
.uid
< server_state
.uid_low
) ||
209 (state
->request
.data
.uid
> server_state
.uid_high
))
210 return WINBINDD_ERROR
;
212 DEBUG(3, ("[%5lu]: getpwuid %lu\n", (unsigned long)state
->pid
,
213 (unsigned long)state
->request
.data
.uid
));
215 /* always try local tdb first */
217 if ( (pw
= wb_getpwuid(state
->request
.data
.uid
)) != NULL
) {
218 memcpy( &state
->response
.data
.pw
, pw
, sizeof(WINBINDD_PW
) );
222 /* Get rid from uid */
224 if (!NT_STATUS_IS_OK(idmap_uid_to_sid(&user_sid
, state
->request
.data
.uid
))) {
225 DEBUG(1, ("could not convert uid %lu to SID\n",
226 (unsigned long)state
->request
.data
.uid
));
227 return WINBINDD_ERROR
;
230 /* Get name and name type from rid */
232 if (!winbindd_lookup_name_by_sid(&user_sid
, dom_name
, user_name
, &name_type
)) {
235 sid_to_string(temp
, &user_sid
);
236 DEBUG(1, ("could not lookup sid %s\n", temp
));
237 return WINBINDD_ERROR
;
240 domain
= find_domain_from_sid(&user_sid
);
243 DEBUG(1,("Can't find domain from sid\n"));
244 return WINBINDD_ERROR
;
247 /* Get some user info */
249 if (!(mem_ctx
= talloc_init("winbind_getpwuid(%lu)",
250 (unsigned long)state
->request
.data
.uid
))) {
252 DEBUG(1, ("out of memory\n"));
253 return WINBINDD_ERROR
;
256 status
= domain
->methods
->query_user(domain
, mem_ctx
, &user_sid
,
259 if (!NT_STATUS_IS_OK(status
)) {
260 DEBUG(1, ("error getting user info for user '%s'\n",
262 talloc_destroy(mem_ctx
);
263 return WINBINDD_ERROR
;
266 /* Check group has a gid number */
268 if (!NT_STATUS_IS_OK(idmap_sid_to_gid(user_info
.group_sid
, &gid
, 0))) {
269 DEBUG(1, ("error getting group id for user %s\n", user_name
));
270 talloc_destroy(mem_ctx
);
271 return WINBINDD_ERROR
;
274 /* Fill in password structure */
276 if (!winbindd_fill_pwent(domain
->name
, user_name
, user_info
.user_sid
,
278 user_info
.full_name
, &state
->response
.data
.pw
)) {
279 talloc_destroy(mem_ctx
);
280 return WINBINDD_ERROR
;
283 talloc_destroy(mem_ctx
);
289 * set/get/endpwent functions
292 /* Rewind file pointer for ntdom passwd database */
294 enum winbindd_result
winbindd_setpwent(struct winbindd_cli_state
*state
)
296 struct winbindd_domain
*domain
;
298 DEBUG(3, ("[%5lu]: setpwent\n", (unsigned long)state
->pid
));
300 /* Check user has enabled this */
302 if (!lp_winbind_enum_users())
303 return WINBINDD_ERROR
;
305 /* Free old static data if it exists */
307 if (state
->getpwent_state
!= NULL
) {
308 free_getent_state(state
->getpwent_state
);
309 state
->getpwent_state
= NULL
;
313 /* add any local users we have */
315 if ( (domain_state
= (struct getent_state
*)malloc(sizeof(struct getent_state
))) == NULL
)
316 return WINBINDD_ERROR
;
318 ZERO_STRUCTP(domain_state
);
320 /* Add to list of open domains */
322 DLIST_ADD(state
->getpwent_state
, domain_state
);
325 /* Create sam pipes for each domain we know about */
327 for(domain
= domain_list(); domain
!= NULL
; domain
= domain
->next
) {
328 struct getent_state
*domain_state
;
331 /* don't add our domaina if we are a PDC or if we
332 are a member of a Samba domain */
334 if ( (IS_DC
|| lp_winbind_trusted_domains_only())
335 && strequal(domain
->name
, lp_workgroup()) )
340 /* Create a state record for this domain */
342 if ((domain_state
= (struct getent_state
*)
343 malloc(sizeof(struct getent_state
))) == NULL
)
344 return WINBINDD_ERROR
;
346 ZERO_STRUCTP(domain_state
);
348 fstrcpy(domain_state
->domain_name
, domain
->name
);
350 /* Add to list of open domains */
352 DLIST_ADD(state
->getpwent_state
, domain_state
);
358 /* Close file pointer to ntdom passwd database */
360 enum winbindd_result
winbindd_endpwent(struct winbindd_cli_state
*state
)
362 DEBUG(3, ("[%5lu]: endpwent\n", (unsigned long)state
->pid
));
364 free_getent_state(state
->getpwent_state
);
365 state
->getpwent_state
= NULL
;
370 /* Get partial list of domain users for a domain. We fill in the sam_entries,
371 and num_sam_entries fields with domain user information. The dispinfo_ndx
372 field is incremented to the index of the next user to fetch. Return True if
373 some users were returned, False otherwise. */
375 #define MAX_FETCH_SAM_ENTRIES 100
377 static BOOL
get_sam_user_entries(struct getent_state
*ent
)
381 WINBIND_USERINFO
*info
;
382 struct getpwent_user
*name_list
= NULL
;
385 struct winbindd_domain
*domain
;
386 struct winbindd_methods
*methods
;
389 if (ent
->num_sam_entries
)
392 if (!(mem_ctx
= talloc_init("get_sam_user_entries(%s)",
396 if (!(domain
= find_domain_from_name(ent
->domain_name
))) {
397 DEBUG(3, ("no such domain %s in get_sam_user_entries\n",
402 methods
= domain
->methods
;
404 /* Free any existing user info */
406 SAFE_FREE(ent
->sam_entries
);
407 ent
->num_sam_entries
= 0;
409 /* Call query_user_list to get a list of usernames and user rids */
413 status
= methods
->query_user_list(domain
, mem_ctx
, &num_entries
,
417 struct getpwent_user
*tnl
;
419 tnl
= (struct getpwent_user
*)Realloc(name_list
,
420 sizeof(struct getpwent_user
) *
421 (ent
->num_sam_entries
+
425 DEBUG(0,("get_sam_user_entries realloc failed.\n"));
426 SAFE_FREE(name_list
);
432 for (i
= 0; i
< num_entries
; i
++) {
433 /* Store account name and gecos */
434 if (!info
[i
].acct_name
) {
435 fstrcpy(name_list
[ent
->num_sam_entries
+ i
].name
, "");
437 fstrcpy(name_list
[ent
->num_sam_entries
+ i
].name
,
440 if (!info
[i
].full_name
) {
441 fstrcpy(name_list
[ent
->num_sam_entries
+ i
].gecos
, "");
443 fstrcpy(name_list
[ent
->num_sam_entries
+ i
].gecos
,
447 /* User and group ids */
448 sid_copy(&name_list
[ent
->num_sam_entries
+i
].user_sid
, info
[i
].user_sid
);
449 sid_copy(&name_list
[ent
->num_sam_entries
+i
].group_sid
, info
[i
].group_sid
);
452 ent
->num_sam_entries
+= num_entries
;
454 /* Fill in remaining fields */
456 ent
->sam_entries
= name_list
;
457 ent
->sam_entry_index
= 0;
458 result
= ent
->num_sam_entries
> 0;
462 talloc_destroy(mem_ctx
);
467 /* Fetch next passwd entry from ntdom database */
469 #define MAX_GETPWENT_USERS 500
471 enum winbindd_result
winbindd_getpwent(struct winbindd_cli_state
*state
)
473 struct getent_state
*ent
;
474 struct winbindd_pw
*user_list
;
475 int num_users
, user_list_ndx
= 0, i
;
477 DEBUG(3, ("[%5lu]: getpwent\n", (unsigned long)state
->pid
));
479 /* Check user has enabled this */
481 if (!lp_winbind_enum_users())
482 return WINBINDD_ERROR
;
484 /* Allocate space for returning a chunk of users */
486 num_users
= MIN(MAX_GETPWENT_USERS
, state
->request
.data
.num_entries
);
488 if ((state
->response
.extra_data
=
489 malloc(num_users
* sizeof(struct winbindd_pw
))) == NULL
)
490 return WINBINDD_ERROR
;
492 memset(state
->response
.extra_data
, 0, num_users
*
493 sizeof(struct winbindd_pw
));
495 user_list
= (struct winbindd_pw
*)state
->response
.extra_data
;
497 if (!(ent
= state
->getpwent_state
))
498 return WINBINDD_ERROR
;
500 /* Start sending back users */
502 for (i
= 0; i
< num_users
; i
++) {
503 struct getpwent_user
*name_list
= NULL
;
506 /* Do we need to fetch another chunk of users? */
508 if (ent
->num_sam_entries
== ent
->sam_entry_index
) {
510 while(ent
&& !get_sam_user_entries(ent
)) {
511 struct getent_state
*next_ent
;
513 /* Free state information for this domain */
515 SAFE_FREE(ent
->sam_entries
);
517 next_ent
= ent
->next
;
518 DLIST_REMOVE(state
->getpwent_state
, ent
);
524 /* No more domains */
530 name_list
= ent
->sam_entries
;
532 /* Skip machine accounts */
534 if (name_list
[ent
->sam_entry_index
].
535 name
[strlen(name_list
[ent
->sam_entry_index
].name
) - 1]
537 ent
->sam_entry_index
++;
541 /* Lookup user info */
543 result
= winbindd_fill_pwent(
545 name_list
[ent
->sam_entry_index
].name
,
546 &name_list
[ent
->sam_entry_index
].user_sid
,
547 &name_list
[ent
->sam_entry_index
].group_sid
,
548 name_list
[ent
->sam_entry_index
].gecos
,
549 &user_list
[user_list_ndx
]);
551 ent
->sam_entry_index
++;
553 /* Add user to return list */
558 state
->response
.data
.num_entries
++;
559 state
->response
.length
+=
560 sizeof(struct winbindd_pw
);
563 DEBUG(1, ("could not lookup domain user %s\n",
564 name_list
[ent
->sam_entry_index
].name
));
569 return (user_list_ndx
> 0) ? WINBINDD_OK
: WINBINDD_ERROR
;
572 /* List domain users without mapping to unix ids */
574 enum winbindd_result
winbindd_list_users(struct winbindd_cli_state
*state
)
576 struct winbindd_domain
*domain
;
577 WINBIND_USERINFO
*info
;
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 /* Enumerate over trusted domains */
591 for (domain
= domain_list(); domain
; domain
= domain
->next
) {
593 struct winbindd_methods
*methods
;
596 methods
= domain
->methods
;
598 /* Query display info */
599 status
= methods
->query_user_list(domain
, mem_ctx
,
600 &num_entries
, &info
);
602 if (num_entries
== 0)
605 /* Allocate some memory for extra data */
606 total_entries
+= num_entries
;
608 ted
= Realloc(extra_data
, sizeof(fstring
) * total_entries
);
611 DEBUG(0,("failed to enlarge buffer!\n"));
612 SAFE_FREE(extra_data
);
617 /* Pack user list into extra data fields */
619 for (i
= 0; i
< num_entries
; i
++) {
620 fstring acct_name
, name
;
622 if (!info
[i
].acct_name
) {
623 fstrcpy(acct_name
, "");
625 fstrcpy(acct_name
, info
[i
].acct_name
);
628 fill_domain_username(name
, domain
->name
, acct_name
);
630 /* Append to extra data */
631 memcpy(&extra_data
[extra_data_len
], name
,
633 extra_data_len
+= strlen(name
);
634 extra_data
[extra_data_len
++] = ',';
638 /* Assign extra_data fields in response structure */
641 extra_data
[extra_data_len
- 1] = '\0';
642 state
->response
.extra_data
= extra_data
;
643 state
->response
.length
+= extra_data_len
;
646 /* No domains responded but that's still OK so don't return an
653 talloc_destroy(mem_ctx
);