2 Unix SMB/Netbios implementation.
5 Winbind daemon - user related functions
7 Copyright (C) Tim Potter 2000
8 Copyright (C) Jeremy Allison 2001.
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.
27 /* Fill a pwent structure with information we have obtained */
29 static BOOL winbindd_fill_pwent(char *domain_name, char *name,
30 uint32 user_rid, uint32 group_rid,
31 char *full_name, struct winbindd_pw *pw)
33 extern userdom_struct current_user_info;
34 fstring name_domain, name_user;
40 /* Resolve the uid number */
42 if (!winbindd_idmap_get_uid_from_rid(domain_name, user_rid,
44 DEBUG(1, ("error getting user id for rid %d\n", user_rid));
48 /* Resolve the gid number */
50 if (!winbindd_idmap_get_gid_from_rid(domain_name, group_rid,
52 DEBUG(1, ("error getting group id for rid %d\n", group_rid));
58 safe_strcpy(pw->pw_name, name, sizeof(pw->pw_name) - 1);
60 /* Full name (gecos) */
62 safe_strcpy(pw->pw_gecos, full_name, sizeof(pw->pw_gecos) - 1);
64 /* Home directory and shell - use template config parameters. The
65 defaults are /tmp for the home directory and /bin/false for
68 parse_domain_user(name, name_domain, name_user);
70 /* The substitution of %U and %D in the 'template homedir' is done
71 by lp_string() calling standard_sub_basic(). */
73 fstrcpy(current_user_info.smb_name, name_user);
74 fstrcpy(current_user_info.domain, name_domain);
76 pstrcpy(homedir, lp_template_homedir());
78 safe_strcpy(pw->pw_dir, homedir, sizeof(pw->pw_dir) - 1);
80 safe_strcpy(pw->pw_shell, lp_template_shell(),
81 sizeof(pw->pw_shell) - 1);
83 /* Password - set to "x" as we can't generate anything useful here.
84 Authentication can be done using the pam_ntdom module. */
86 safe_strcpy(pw->pw_passwd, "x", sizeof(pw->pw_passwd) - 1);
91 /************************************************************************
92 Empty static struct for negative caching.
93 *************************************************************************/
95 static struct winbindd_pw negative_pw_cache_entry;
97 /* Return a password structure from a username. Specify whether cached data
100 enum winbindd_result winbindd_getpwnam_from_user(struct winbindd_cli_state *state)
102 uint32 user_rid, group_rid;
103 SAM_USERINFO_CTR *user_info;
105 fstring name_domain, name_user, name, gecos_name;
106 enum SID_NAME_USE name_type;
107 struct winbindd_domain *domain;
110 DEBUG(3, ("[%5d]: getpwnam %s\n", state->pid,
111 state->request.data.username));
113 /* Parse domain and username */
115 parse_domain_user(state->request.data.username, name_domain,
118 /* Reject names that don't have a domain - i.e name_domain contains
121 if (strequal(name_domain, ""))
122 return WINBINDD_ERROR;
124 if ((domain = find_domain_from_name(name_domain)) == NULL) {
125 DEBUG(5, ("No such domain: %s\n", name_domain));
126 return WINBINDD_ERROR;
129 /* Check for cached user entry */
131 if (winbindd_fetch_user_cache_entry(domain, name_user, &state->response.data.pw)) {
132 /* Check if this is a negative cache entry. */
133 if (memcmp(&negative_pw_cache_entry, &state->response.data.pw,
134 sizeof(state->response.data.pw)) == 0)
135 return WINBINDD_ERROR;
139 slprintf(name, sizeof(name) - 1, "%s\\%s", name_domain, name_user);
141 /* Get rid and name type from name */
143 if (!winbindd_lookup_sid_by_name(domain, name, &user_sid, &name_type)) {
144 DEBUG(1, ("user '%s' does not exist\n", name_user));
145 winbindd_store_user_cache_entry(domain, name_user, &negative_pw_cache_entry);
146 return WINBINDD_ERROR;
149 if (name_type != SID_NAME_USER) {
150 DEBUG(1, ("name '%s' is not a user name: %d\n", name_user,
152 winbindd_store_user_cache_entry(domain, name_user, &negative_pw_cache_entry);
153 return WINBINDD_ERROR;
156 /* Get some user info. Split the user rid from the sid obtained
157 from the winbind_lookup_by_name() call and use it in a
158 winbind_lookup_userinfo() */
160 if (!(mem_ctx = talloc_init())) {
161 DEBUG(1, ("out of memory\n"));
162 return WINBINDD_ERROR;
165 sid_split_rid(&user_sid, &user_rid);
167 if (!winbindd_lookup_userinfo(domain, mem_ctx, user_rid, &user_info)) {
168 DEBUG(1, ("pwnam_from_user(): error getting user info for "
169 "user '%s'\n", name_user));
170 winbindd_store_user_cache_entry(domain, name_user, &negative_pw_cache_entry);
171 return WINBINDD_ERROR;
174 group_rid = user_info->info.id21->group_rid;
176 unistr2_to_ascii(gecos_name, &user_info->info.id21->uni_full_name,
177 sizeof(gecos_name) - 1);
179 talloc_destroy(mem_ctx);
182 /* Now take all this information and fill in a passwd structure */
184 if (!winbindd_fill_pwent(name_domain, state->request.data.username,
185 user_rid, group_rid, gecos_name,
186 &state->response.data.pw)) {
187 winbindd_store_user_cache_entry(domain, name_user, &negative_pw_cache_entry);
188 return WINBINDD_ERROR;
191 winbindd_store_user_cache_entry(domain, name_user, &state->response.data.pw);
196 /* Return a password structure given a uid number */
198 enum winbindd_result winbindd_getpwnam_from_uid(struct winbindd_cli_state *state)
201 struct winbindd_domain *domain;
202 uint32 user_rid, group_rid;
203 fstring user_name, gecos_name;
204 enum SID_NAME_USE name_type;
205 SAM_USERINFO_CTR *user_info;
209 /* Bug out if the uid isn't in the winbind range */
211 if ((state->request.data.uid < server_state.uid_low ) ||
212 (state->request.data.uid > server_state.uid_high))
213 return WINBINDD_ERROR;
215 DEBUG(3, ("[%5d]: getpwuid %d\n", state->pid,
216 state->request.data.uid));
218 /* Get rid from uid */
220 if (!winbindd_idmap_get_rid_from_uid(state->request.data.uid,
221 &user_rid, &domain)) {
222 DEBUG(1, ("Could not convert uid %d to rid\n",
223 state->request.data.uid));
224 return WINBINDD_ERROR;
227 /* Check for cached uid entry */
229 if (winbindd_fetch_uid_cache_entry(domain,
230 state->request.data.uid,
231 &state->response.data.pw)) {
232 /* Check if this is a negative cache entry. */
233 if (memcmp(&negative_pw_cache_entry, &state->response.data.pw,
234 sizeof(state->response.data.pw)) == 0)
235 return WINBINDD_ERROR;
239 /* Get name and name type from rid */
241 sid_copy(&user_sid, &domain->sid);
242 sid_append_rid(&user_sid, user_rid);
244 if (!winbindd_lookup_name_by_sid(&user_sid, user_name, &name_type)) {
247 sid_to_string(temp, &user_sid);
248 DEBUG(1, ("Could not lookup sid %s\n", temp));
250 winbindd_store_uid_cache_entry(domain, state->request.data.uid, &negative_pw_cache_entry);
251 return WINBINDD_ERROR;
254 if (strcmp("\\", lp_winbind_separator()))
255 string_sub(user_name, "\\", lp_winbind_separator(),
258 /* Get some user info */
260 if (!(mem_ctx = talloc_init())) {
261 DEBUG(1, ("out of memory\n"));
262 return WINBINDD_ERROR;
265 if (!winbindd_lookup_userinfo(domain, mem_ctx, user_rid, &user_info)) {
266 DEBUG(1, ("pwnam_from_uid(): error getting user info for "
267 "user '%s'\n", user_name));
268 winbindd_store_uid_cache_entry(domain, state->request.data.uid, &negative_pw_cache_entry);
269 return WINBINDD_ERROR;
272 group_rid = user_info->info.id21->group_rid;
273 unistr2_to_ascii(gecos_name, &user_info->info.id21->uni_full_name,
274 sizeof(gecos_name) - 1);
276 talloc_destroy(mem_ctx);
279 /* Resolve gid number */
281 if (!winbindd_idmap_get_gid_from_rid(domain->name, group_rid, &gid)) {
282 DEBUG(1, ("error getting group id for user %s\n", user_name));
283 return WINBINDD_ERROR;
286 /* Fill in password structure */
288 if (!winbindd_fill_pwent(domain->name, user_name, user_rid, group_rid,
289 gecos_name, &state->response.data.pw)) {
290 winbindd_store_uid_cache_entry(domain, state->request.data.uid, &negative_pw_cache_entry);
291 return WINBINDD_ERROR;
294 winbindd_store_uid_cache_entry(domain, state->request.data.uid, &state->response.data.pw);
300 * set/get/endpwent functions
303 /* Rewind file pointer for ntdom passwd database */
305 enum winbindd_result winbindd_setpwent(struct winbindd_cli_state *state)
307 struct winbindd_domain *tmp;
309 DEBUG(3, ("[%5d]: setpwent\n", state->pid));
311 /* Check user has enabled this */
313 if (!lp_winbind_enum_users())
314 return WINBINDD_ERROR;
316 /* Free old static data if it exists */
318 if (state->getpwent_state != NULL) {
319 free_getent_state(state->getpwent_state);
320 state->getpwent_state = NULL;
323 /* Create sam pipes for each domain we know about */
325 if (domain_list == NULL)
328 for(tmp = domain_list; tmp != NULL; tmp = tmp->next) {
329 struct getent_state *domain_state;
332 * Skip domains other than WINBINDD_DOMAIN environment
336 if ((strcmp(state->request.domain, "") != 0) &&
337 !check_domain_env(state->request.domain, tmp->name))
340 /* Create a state record for this domain */
342 if ((domain_state = (struct getent_state *)malloc(sizeof(struct getent_state))) == NULL)
343 return WINBINDD_ERROR;
345 ZERO_STRUCTP(domain_state);
347 domain_state->domain = tmp;
349 /* Add to list of open domains */
351 DLIST_ADD(state->getpwent_state, domain_state);
357 /* Close file pointer to ntdom passwd database */
359 enum winbindd_result winbindd_endpwent(struct winbindd_cli_state *state)
361 DEBUG(3, ("[%5d]: endpwent\n", state->pid));
363 free_getent_state(state->getpwent_state);
364 state->getpwent_state = NULL;
369 /* Get partial list of domain users for a domain. We fill in the sam_entries,
370 and num_sam_entries fields with domain user information. The dispinfo_ndx
371 field is incremented to the index of the next user to fetch. Return True if
372 some users were returned, False otherwise. */
374 #define MAX_FETCH_SAM_ENTRIES 100
376 static BOOL get_sam_user_entries(struct getent_state *ent)
380 WINBIND_DISPINFO *info;
381 struct getpwent_user *name_list = NULL;
384 struct winbindd_methods *methods;
386 if (ent->got_all_sam_entries)
389 if (!(mem_ctx = talloc_init()))
392 methods = ent->domain->methods;
395 /* Look in cache for entries, else get them direct */
397 if (winbindd_fetch_user_cache(ent->domain,
398 (struct getpwent_user **)
400 &ent->num_sam_entries)) {
405 /* Free any existing user info */
407 SAFE_FREE(ent->sam_entries);
408 ent->num_sam_entries = 0;
410 /* Call query_dispinfo to get a list of usernames and user rids */
417 status = methods->query_dispinfo(ent->domain, mem_ctx,
419 &num_entries, &info);
422 struct getpwent_user *tnl;
424 tnl = (struct getpwent_user *)Realloc(name_list,
425 sizeof(struct getpwent_user) *
426 (ent->num_sam_entries +
430 DEBUG(0,("get_sam_user_entries: Realloc failed.\n"));
431 SAFE_FREE(name_list);
437 for (i = 0; i < num_entries; i++) {
438 /* Store account name and gecos */
439 if (!info[i].acct_name) {
440 fstrcpy(name_list[ent->num_sam_entries + i].name, "");
442 fstrcpy(name_list[ent->num_sam_entries + i].name,
445 if (!info[i].full_name) {
446 fstrcpy(name_list[ent->num_sam_entries + i].gecos, "");
448 fstrcpy(name_list[ent->num_sam_entries + i].gecos,
452 /* User and group ids */
453 name_list[ent->num_sam_entries+i].user_rid = info[i].user_rid;
454 name_list[ent->num_sam_entries+i].group_rid = info[i].group_rid;
457 ent->num_sam_entries += num_entries;
459 if (NT_STATUS_V(status) != NT_STATUS_V(STATUS_MORE_ENTRIES))
462 } while (ent->num_sam_entries < MAX_FETCH_SAM_ENTRIES);
465 /* Fill cache with received entries */
467 winbindd_store_user_cache(ent->domain, ent->sam_entries,
468 ent->num_sam_entries);
471 /* Fill in remaining fields */
473 ent->sam_entries = name_list;
474 ent->sam_entry_index = 0;
475 ent->got_all_sam_entries = (NT_STATUS_V(status) != NT_STATUS_V(STATUS_MORE_ENTRIES));
477 result = ent->num_sam_entries > 0;
481 talloc_destroy(mem_ctx);
486 /* Fetch next passwd entry from ntdom database */
488 #define MAX_GETPWENT_USERS 500
490 enum winbindd_result winbindd_getpwent(struct winbindd_cli_state *state)
492 struct getent_state *ent;
493 struct winbindd_pw *user_list;
494 int num_users, user_list_ndx = 0, i;
497 DEBUG(3, ("[%5d]: getpwent\n", state->pid));
499 /* Check user has enabled this */
501 if (!lp_winbind_enum_users())
502 return WINBINDD_ERROR;
504 /* Allocate space for returning a chunk of users */
506 num_users = MIN(MAX_GETPWENT_USERS, state->request.data.num_entries);
508 if ((state->response.extra_data =
509 malloc(num_users * sizeof(struct winbindd_pw))) == NULL)
510 return WINBINDD_ERROR;
512 memset(state->response.extra_data, 0, num_users *
513 sizeof(struct winbindd_pw));
515 user_list = (struct winbindd_pw *)state->response.extra_data;
516 sep = lp_winbind_separator();
518 if (!(ent = state->getpwent_state))
519 return WINBINDD_ERROR;
521 /* Start sending back users */
523 for (i = 0; i < num_users; i++) {
524 struct getpwent_user *name_list = NULL;
525 fstring domain_user_name;
528 /* Do we need to fetch another chunk of users? */
530 if (ent->num_sam_entries == ent->sam_entry_index) {
532 while(ent && !get_sam_user_entries(ent)) {
533 struct getent_state *next_ent;
535 /* Free state information for this domain */
537 SAFE_FREE(ent->sam_entries);
539 next_ent = ent->next;
540 DLIST_REMOVE(state->getpwent_state, ent);
546 /* No more domains */
552 name_list = ent->sam_entries;
554 /* Skip machine accounts */
556 if (name_list[ent->sam_entry_index].
557 name[strlen(name_list[ent->sam_entry_index].name) - 1]
559 ent->sam_entry_index++;
563 /* Lookup user info */
565 slprintf(domain_user_name, sizeof(domain_user_name) - 1,
566 "%s%s%s", ent->domain->name, sep,
567 name_list[ent->sam_entry_index].name);
569 result = winbindd_fill_pwent(
572 name_list[ent->sam_entry_index].user_rid,
573 name_list[ent->sam_entry_index].group_rid,
574 name_list[ent->sam_entry_index].gecos,
575 &user_list[user_list_ndx]);
577 ent->sam_entry_index++;
579 /* Add user to return list */
584 state->response.data.num_entries++;
585 state->response.length +=
586 sizeof(struct winbindd_pw);
589 DEBUG(1, ("could not lookup domain user %s\n",
595 return (user_list_ndx > 0) ? WINBINDD_OK : WINBINDD_ERROR;
598 /* List domain users without mapping to unix ids */
600 enum winbindd_result winbindd_list_users(struct winbindd_cli_state *state)
602 struct winbindd_domain *domain;
603 WINBIND_DISPINFO *info;
604 uint32 num_entries = 0, total_entries = 0;
605 char *ted, *extra_data = NULL;
606 int extra_data_len = 0;
608 enum winbindd_result rv = WINBINDD_ERROR;
610 DEBUG(3, ("[%5d]: list users\n", state->pid));
612 if (!(mem_ctx = talloc_init()))
613 return WINBINDD_ERROR;
615 /* Enumerate over trusted domains */
617 if (domain_list == NULL)
620 for (domain = domain_list; domain; domain = domain->next) {
622 uint32 start_ndx = 0;
623 struct winbindd_methods *methods;
625 /* Skip domains other than WINBINDD_DOMAIN environment
628 if ((strcmp(state->request.domain, "") != 0) &&
629 !check_domain_env(state->request.domain, domain->name))
632 methods = domain->methods;
634 /* Query display info */
639 status = methods->query_dispinfo(domain, mem_ctx, &start_ndx,
640 &num_entries, &info);
642 if (num_entries == 0)
645 /* Allocate some memory for extra data */
646 total_entries += num_entries;
648 ted = Realloc(extra_data, sizeof(fstring) *
652 DEBUG(0,("winbindd_list_users: failed to enlarge buffer!\n"));
653 SAFE_FREE(extra_data);
658 /* Pack user list into extra data fields */
660 for (i = 0; i < num_entries; i++) {
661 fstring acct_name, name;
663 if (!info[i].acct_name) {
664 fstrcpy(acct_name, "");
666 fstrcpy(acct_name, info[i].acct_name);
669 slprintf(name, sizeof(name) - 1, "%s%s%s",
670 domain->name, lp_winbind_separator(),
673 /* Append to extra data */
674 memcpy(&extra_data[extra_data_len], name,
676 extra_data_len += strlen(name);
677 extra_data[extra_data_len++] = ',';
679 } while (NT_STATUS_V(status) == NT_STATUS_V(STATUS_MORE_ENTRIES));
682 /* Assign extra_data fields in response structure */
685 extra_data[extra_data_len - 1] = '\0';
686 state->response.extra_data = extra_data;
687 state->response.length += extra_data_len;
690 /* No domains responded but that's still OK so don't return an
697 talloc_destroy(mem_ctx);