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 *dom_name, char *user_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 output_username;
37 if (!pw || !dom_name || !user_name)
40 /* Resolve the uid number */
42 if (!winbindd_idmap_get_uid_from_rid(dom_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(dom_name, group_rid,
52 DEBUG(1, ("error getting group id for rid %d\n", group_rid));
58 fill_domain_username(output_username, dom_name, user_name);
60 safe_strcpy(pw->pw_name, output_username, sizeof(pw->pw_name) - 1);
62 /* Full name (gecos) */
64 safe_strcpy(pw->pw_gecos, full_name, sizeof(pw->pw_gecos) - 1);
66 /* Home directory and shell - use template config parameters. The
67 defaults are /tmp for the home directory and /bin/false for
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, user_name);
74 fstrcpy(current_user_info.domain, dom_name);
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_winbind module. */
86 safe_strcpy(pw->pw_passwd, "x", sizeof(pw->pw_passwd) - 1);
91 /* Return a password structure from a username. */
93 enum winbindd_result winbindd_getpwnam(struct winbindd_cli_state *state)
96 WINBIND_USERINFO user_info;
99 fstring name_domain, name_user;
100 enum SID_NAME_USE name_type;
101 struct winbindd_domain *domain;
104 DEBUG(3, ("[%5d]: getpwnam %s\n", state->pid,
105 state->request.data.username));
107 /* Parse domain and username */
109 if (!parse_domain_user(state->request.data.username, name_domain,
111 return WINBINDD_ERROR;
113 if ((domain = find_domain_from_name(name_domain)) == NULL) {
114 DEBUG(5, ("no such domain: %s\n", name_domain));
115 return WINBINDD_ERROR;
118 /* Get rid and name type from name */
120 if (!winbindd_lookup_sid_by_name(domain, name_user, &user_sid, &name_type)) {
121 DEBUG(1, ("user '%s' does not exist\n", name_user));
122 return WINBINDD_ERROR;
125 if (name_type != SID_NAME_USER) {
126 DEBUG(1, ("name '%s' is not a user name: %d\n", name_user,
128 return WINBINDD_ERROR;
131 /* Get some user info. Split the user rid from the sid obtained
132 from the winbind_lookup_by_name() call and use it in a
133 winbind_lookup_userinfo() */
135 if (!(mem_ctx = talloc_init_named("winbindd_getpwnam([%s]\\[%s])",
136 name_domain, name_user))) {
137 DEBUG(1, ("out of memory\n"));
138 return WINBINDD_ERROR;
141 sid_split_rid(&user_sid, &user_rid);
143 status = domain->methods->query_user(domain, mem_ctx, user_rid,
146 if (!NT_STATUS_IS_OK(status)) {
147 DEBUG(1, ("error getting user info for user '[%s]\\[%s]'\n",
148 name_domain, name_user));
149 talloc_destroy(mem_ctx);
150 return WINBINDD_ERROR;
153 /* Now take all this information and fill in a passwd structure */
154 if (!winbindd_fill_pwent(name_domain, name_user,
155 user_rid, user_info.group_rid,
157 &state->response.data.pw)) {
158 talloc_destroy(mem_ctx);
159 return WINBINDD_ERROR;
162 talloc_destroy(mem_ctx);
167 /* Return a password structure given a uid number */
169 enum winbindd_result winbindd_getpwuid(struct winbindd_cli_state *state)
172 struct winbindd_domain *domain;
176 enum SID_NAME_USE name_type;
177 WINBIND_USERINFO user_info;
182 /* Bug out if the uid isn't in the winbind range */
184 if ((state->request.data.uid < server_state.uid_low ) ||
185 (state->request.data.uid > server_state.uid_high))
186 return WINBINDD_ERROR;
188 DEBUG(3, ("[%5d]: getpwuid %d\n", state->pid,
189 state->request.data.uid));
191 /* Get rid from uid */
193 if (!winbindd_idmap_get_rid_from_uid(state->request.data.uid,
194 &user_rid, &domain)) {
195 DEBUG(1, ("could not convert uid %d to rid\n",
196 state->request.data.uid));
197 return WINBINDD_ERROR;
200 /* Get name and name type from rid */
202 sid_copy(&user_sid, &domain->sid);
203 sid_append_rid(&user_sid, user_rid);
205 if (!winbindd_lookup_name_by_sid(&user_sid, dom_name, user_name, &name_type)) {
208 sid_to_string(temp, &user_sid);
209 DEBUG(1, ("could not lookup sid %s\n", temp));
210 return WINBINDD_ERROR;
213 /* Get some user info */
215 if (!(mem_ctx = talloc_init_named("winbind_getpwuid(%d)",
216 state->request.data.uid))) {
218 DEBUG(1, ("out of memory\n"));
219 return WINBINDD_ERROR;
222 status = domain->methods->query_user(domain, mem_ctx, user_rid,
225 if (!NT_STATUS_IS_OK(status)) {
226 DEBUG(1, ("error getting user info for user '%s'\n",
228 talloc_destroy(mem_ctx);
229 return WINBINDD_ERROR;
232 /* Resolve gid number */
234 if (!winbindd_idmap_get_gid_from_rid(domain->name, user_info.group_rid, &gid)) {
235 DEBUG(1, ("error getting group id for user %s\n", user_name));
236 talloc_destroy(mem_ctx);
237 return WINBINDD_ERROR;
240 /* Fill in password structure */
242 if (!winbindd_fill_pwent(domain->name, user_name, user_rid, user_info.group_rid,
243 user_info.full_name, &state->response.data.pw)) {
244 talloc_destroy(mem_ctx);
245 return WINBINDD_ERROR;
248 talloc_destroy(mem_ctx);
254 * set/get/endpwent functions
257 /* Rewind file pointer for ntdom passwd database */
259 enum winbindd_result winbindd_setpwent(struct winbindd_cli_state *state)
261 struct winbindd_domain *domain;
263 DEBUG(3, ("[%5d]: setpwent\n", state->pid));
265 /* Check user has enabled this */
267 if (!lp_winbind_enum_users())
268 return WINBINDD_ERROR;
270 /* Free old static data if it exists */
272 if (state->getpwent_state != NULL) {
273 free_getent_state(state->getpwent_state);
274 state->getpwent_state = NULL;
277 /* Create sam pipes for each domain we know about */
279 for(domain = domain_list(); domain != NULL; domain = domain->next) {
280 struct getent_state *domain_state;
283 * Skip domains other than WINBINDD_DOMAIN environment
287 if ((strcmp(state->request.domain, "") != 0) &&
288 !check_domain_env(state->request.domain,
292 /* Create a state record for this domain */
294 if ((domain_state = (struct getent_state *)
295 malloc(sizeof(struct getent_state))) == NULL)
296 return WINBINDD_ERROR;
298 ZERO_STRUCTP(domain_state);
300 fstrcpy(domain_state->domain_name, domain->name);
302 /* Add to list of open domains */
304 DLIST_ADD(state->getpwent_state, domain_state);
310 /* Close file pointer to ntdom passwd database */
312 enum winbindd_result winbindd_endpwent(struct winbindd_cli_state *state)
314 DEBUG(3, ("[%5d]: endpwent\n", state->pid));
316 free_getent_state(state->getpwent_state);
317 state->getpwent_state = NULL;
322 /* Get partial list of domain users for a domain. We fill in the sam_entries,
323 and num_sam_entries fields with domain user information. The dispinfo_ndx
324 field is incremented to the index of the next user to fetch. Return True if
325 some users were returned, False otherwise. */
327 #define MAX_FETCH_SAM_ENTRIES 100
329 static BOOL get_sam_user_entries(struct getent_state *ent)
333 WINBIND_USERINFO *info;
334 struct getpwent_user *name_list = NULL;
337 struct winbindd_domain *domain;
338 struct winbindd_methods *methods;
341 if (ent->num_sam_entries)
344 if (!(mem_ctx = talloc_init_named("get_sam_user_entries(%s)",
348 if (!(domain = find_domain_from_name(ent->domain_name))) {
349 DEBUG(3, ("no such domain %s in get_sam_user_entries\n",
354 methods = domain->methods;
356 /* Free any existing user info */
358 SAFE_FREE(ent->sam_entries);
359 ent->num_sam_entries = 0;
361 /* Call query_user_list to get a list of usernames and user rids */
365 status = methods->query_user_list(domain, mem_ctx, &num_entries,
369 struct getpwent_user *tnl;
371 tnl = (struct getpwent_user *)Realloc(name_list,
372 sizeof(struct getpwent_user) *
373 (ent->num_sam_entries +
377 DEBUG(0,("get_sam_user_entries realloc failed.\n"));
378 SAFE_FREE(name_list);
384 for (i = 0; i < num_entries; i++) {
385 /* Store account name and gecos */
386 if (!info[i].acct_name) {
387 fstrcpy(name_list[ent->num_sam_entries + i].name, "");
389 fstrcpy(name_list[ent->num_sam_entries + i].name,
392 if (!info[i].full_name) {
393 fstrcpy(name_list[ent->num_sam_entries + i].gecos, "");
395 fstrcpy(name_list[ent->num_sam_entries + i].gecos,
399 /* User and group ids */
400 name_list[ent->num_sam_entries+i].user_rid = info[i].user_rid;
401 name_list[ent->num_sam_entries+i].group_rid = info[i].group_rid;
404 ent->num_sam_entries += num_entries;
406 /* Fill in remaining fields */
408 ent->sam_entries = name_list;
409 ent->sam_entry_index = 0;
410 result = ent->num_sam_entries > 0;
414 talloc_destroy(mem_ctx);
419 /* Fetch next passwd entry from ntdom database */
421 #define MAX_GETPWENT_USERS 500
423 enum winbindd_result winbindd_getpwent(struct winbindd_cli_state *state)
425 struct getent_state *ent;
426 struct winbindd_pw *user_list;
427 int num_users, user_list_ndx = 0, i;
429 DEBUG(3, ("[%5d]: getpwent\n", state->pid));
431 /* Check user has enabled this */
433 if (!lp_winbind_enum_users())
434 return WINBINDD_ERROR;
436 /* Allocate space for returning a chunk of users */
438 num_users = MIN(MAX_GETPWENT_USERS, state->request.data.num_entries);
440 if ((state->response.extra_data =
441 malloc(num_users * sizeof(struct winbindd_pw))) == NULL)
442 return WINBINDD_ERROR;
444 memset(state->response.extra_data, 0, num_users *
445 sizeof(struct winbindd_pw));
447 user_list = (struct winbindd_pw *)state->response.extra_data;
449 if (!(ent = state->getpwent_state))
450 return WINBINDD_ERROR;
452 /* Start sending back users */
454 for (i = 0; i < num_users; i++) {
455 struct getpwent_user *name_list = NULL;
456 fstring domain_user_name;
459 /* Do we need to fetch another chunk of users? */
461 if (ent->num_sam_entries == ent->sam_entry_index) {
463 while(ent && !get_sam_user_entries(ent)) {
464 struct getent_state *next_ent;
466 /* Free state information for this domain */
468 SAFE_FREE(ent->sam_entries);
470 next_ent = ent->next;
471 DLIST_REMOVE(state->getpwent_state, ent);
477 /* No more domains */
483 name_list = ent->sam_entries;
485 /* Skip machine accounts */
487 if (name_list[ent->sam_entry_index].
488 name[strlen(name_list[ent->sam_entry_index].name) - 1]
490 ent->sam_entry_index++;
494 /* Lookup user info */
496 result = winbindd_fill_pwent(
498 name_list[ent->sam_entry_index].name,
499 name_list[ent->sam_entry_index].user_rid,
500 name_list[ent->sam_entry_index].group_rid,
501 name_list[ent->sam_entry_index].gecos,
502 &user_list[user_list_ndx]);
504 ent->sam_entry_index++;
506 /* Add user to return list */
511 state->response.data.num_entries++;
512 state->response.length +=
513 sizeof(struct winbindd_pw);
516 DEBUG(1, ("could not lookup domain user %s\n",
522 return (user_list_ndx > 0) ? WINBINDD_OK : WINBINDD_ERROR;
525 /* List domain users without mapping to unix ids */
527 enum winbindd_result winbindd_list_users(struct winbindd_cli_state *state)
529 struct winbindd_domain *domain;
530 WINBIND_USERINFO *info;
531 uint32 num_entries = 0, total_entries = 0;
532 char *ted, *extra_data = NULL;
533 int extra_data_len = 0;
535 enum winbindd_result rv = WINBINDD_ERROR;
537 DEBUG(3, ("[%5d]: list users\n", state->pid));
539 if (!(mem_ctx = talloc_init_named("winbindd_list_users")))
540 return WINBINDD_ERROR;
542 /* Enumerate over trusted domains */
544 for (domain = domain_list(); domain; domain = domain->next) {
546 struct winbindd_methods *methods;
549 /* Skip domains other than WINBINDD_DOMAIN environment
552 if ((strcmp(state->request.domain, "") != 0) &&
553 !check_domain_env(state->request.domain, domain->name))
556 methods = domain->methods;
558 /* Query display info */
559 status = methods->query_user_list(domain, mem_ctx,
560 &num_entries, &info);
562 if (num_entries == 0)
565 /* Allocate some memory for extra data */
566 total_entries += num_entries;
568 ted = Realloc(extra_data, sizeof(fstring) * total_entries);
571 DEBUG(0,("failed to enlarge buffer!\n"));
572 SAFE_FREE(extra_data);
577 /* Pack user list into extra data fields */
579 for (i = 0; i < num_entries; i++) {
580 fstring acct_name, name;
582 if (!info[i].acct_name) {
583 fstrcpy(acct_name, "");
585 fstrcpy(acct_name, info[i].acct_name);
588 fill_domain_username(name, domain->name, acct_name);
590 /* Append to extra data */
591 memcpy(&extra_data[extra_data_len], name,
593 extra_data_len += strlen(name);
594 extra_data[extra_data_len++] = ',';
598 /* Assign extra_data fields in response structure */
601 extra_data[extra_data_len - 1] = '\0';
602 state->response.extra_data = extra_data;
603 state->response.length += extra_data_len;
606 /* No domains responded but that's still OK so don't return an
613 talloc_destroy(mem_ctx);