Add endpoint mapper rpc definitions
[Samba/gbeck.git] / source3 / nsswitch / winbindd_user.c
blobeab88c842eac75aa3a436c956a91ff12303b7b54
1 /*
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.
25 #include "winbindd.h"
27 #undef DBGC_CLASS
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;
39 pstring homedir;
40 fstring sid_string;
42 if (!pw || !dom_name || !user_name)
43 return False;
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)));
49 return False;
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)));
56 return False;
59 /* Username */
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
71 shell. */
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);
92 return True;
95 /* Return a password structure from a username. */
97 enum winbindd_result winbindd_getpwnam(struct winbindd_cli_state *state)
99 WINBIND_USERINFO user_info;
100 WINBINDD_PW *pw;
101 DOM_SID user_sid;
102 NTSTATUS status;
103 fstring name_domain, name_user;
104 enum SID_NAME_USE name_type;
105 struct winbindd_domain *domain;
106 TALLOC_CTX *mem_ctx;
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) );
128 return WINBINDD_OK;
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,
153 name_type));
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,
168 &user_info);
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,
180 user_info.full_name,
181 &state->response.data.pw)) {
182 talloc_destroy(mem_ctx);
183 return WINBINDD_ERROR;
186 talloc_destroy(mem_ctx);
188 return WINBINDD_OK;
191 /* Return a password structure given a uid number */
193 enum winbindd_result winbindd_getpwuid(struct winbindd_cli_state *state)
195 DOM_SID user_sid;
196 struct winbindd_domain *domain;
197 WINBINDD_PW *pw;
198 fstring dom_name;
199 fstring user_name;
200 enum SID_NAME_USE name_type;
201 WINBIND_USERINFO user_info;
202 TALLOC_CTX *mem_ctx;
203 NTSTATUS status;
204 gid_t gid;
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) );
219 return WINBINDD_OK;
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)) {
233 fstring temp;
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);
242 if (!domain) {
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,
257 &user_info);
259 if (!NT_STATUS_IS_OK(status)) {
260 DEBUG(1, ("error getting user info for user '%s'\n",
261 user_name));
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,
277 user_info.group_sid,
278 user_info.full_name, &state->response.data.pw)) {
279 talloc_destroy(mem_ctx);
280 return WINBINDD_ERROR;
283 talloc_destroy(mem_ctx);
285 return WINBINDD_OK;
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;
312 #if 0 /* JERRY */
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);
323 #endif
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()) )
337 continue;
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);
355 return WINBINDD_OK;
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;
367 return WINBINDD_OK;
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)
379 NTSTATUS status;
380 uint32 num_entries;
381 WINBIND_USERINFO *info;
382 struct getpwent_user *name_list = NULL;
383 BOOL result = False;
384 TALLOC_CTX *mem_ctx;
385 struct winbindd_domain *domain;
386 struct winbindd_methods *methods;
387 unsigned int i;
389 if (ent->num_sam_entries)
390 return False;
392 if (!(mem_ctx = talloc_init("get_sam_user_entries(%s)",
393 ent->domain_name)))
394 return False;
396 if (!(domain = find_domain_from_name(ent->domain_name))) {
397 DEBUG(3, ("no such domain %s in get_sam_user_entries\n",
398 ent->domain_name));
399 return False;
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 */
411 num_entries = 0;
413 status = methods->query_user_list(domain, mem_ctx, &num_entries,
414 &info);
416 if (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 +
422 num_entries));
424 if (!tnl) {
425 DEBUG(0,("get_sam_user_entries realloc failed.\n"));
426 SAFE_FREE(name_list);
427 goto done;
428 } else
429 name_list = tnl;
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, "");
436 } else {
437 fstrcpy(name_list[ent->num_sam_entries + i].name,
438 info[i].acct_name);
440 if (!info[i].full_name) {
441 fstrcpy(name_list[ent->num_sam_entries + i].gecos, "");
442 } else {
443 fstrcpy(name_list[ent->num_sam_entries + i].gecos,
444 info[i].full_name);
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;
460 done:
462 talloc_destroy(mem_ctx);
464 return result;
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;
504 uint32 result;
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);
520 SAFE_FREE(ent);
521 ent = next_ent;
524 /* No more domains */
526 if (!ent)
527 break;
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]
536 == '$') {
537 ent->sam_entry_index++;
538 continue;
541 /* Lookup user info */
543 result = winbindd_fill_pwent(
544 ent->domain_name,
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 */
555 if (result) {
557 user_list_ndx++;
558 state->response.data.num_entries++;
559 state->response.length +=
560 sizeof(struct winbindd_pw);
562 } else
563 DEBUG(1, ("could not lookup domain user %s\n",
564 name_list[ent->sam_entry_index].name));
567 /* Out of domains */
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 const char *which_domain;
579 uint32 num_entries = 0, total_entries = 0;
580 char *ted, *extra_data = NULL;
581 int extra_data_len = 0;
582 TALLOC_CTX *mem_ctx;
583 enum winbindd_result rv = WINBINDD_ERROR;
585 DEBUG(3, ("[%5lu]: list users\n", (unsigned long)state->pid));
587 if (!(mem_ctx = talloc_init("winbindd_list_users")))
588 return WINBINDD_ERROR;
590 /* Ensure null termination */
591 state->request.domain_name[sizeof(state->request.domain_name)-1]='\0';
592 which_domain = state->request.domain_name;
594 /* Enumerate over trusted domains */
596 for (domain = domain_list(); domain; domain = domain->next) {
597 NTSTATUS status;
598 struct winbindd_methods *methods;
599 unsigned int i;
601 /* if we have a domain name restricting the request and this
602 one in the list doesn't match, then just bypass the remainder
603 of the loop */
605 if ( *which_domain && !strequal(which_domain, domain->name) )
606 continue;
608 methods = domain->methods;
610 /* Query display info */
611 status = methods->query_user_list(domain, mem_ctx,
612 &num_entries, &info);
614 if (num_entries == 0)
615 continue;
617 /* Allocate some memory for extra data */
618 total_entries += num_entries;
620 ted = Realloc(extra_data, sizeof(fstring) * total_entries);
622 if (!ted) {
623 DEBUG(0,("failed to enlarge buffer!\n"));
624 SAFE_FREE(extra_data);
625 goto done;
626 } else
627 extra_data = ted;
629 /* Pack user list into extra data fields */
631 for (i = 0; i < num_entries; i++) {
632 fstring acct_name, name;
634 if (!info[i].acct_name) {
635 fstrcpy(acct_name, "");
636 } else {
637 fstrcpy(acct_name, info[i].acct_name);
640 fill_domain_username(name, domain->name, acct_name);
642 /* Append to extra data */
643 memcpy(&extra_data[extra_data_len], name,
644 strlen(name));
645 extra_data_len += strlen(name);
646 extra_data[extra_data_len++] = ',';
650 /* Assign extra_data fields in response structure */
652 if (extra_data) {
653 extra_data[extra_data_len - 1] = '\0';
654 state->response.extra_data = extra_data;
655 state->response.length += extra_data_len;
658 /* No domains responded but that's still OK so don't return an
659 error. */
661 rv = WINBINDD_OK;
663 done:
665 talloc_destroy(mem_ctx);
667 return rv;