2 Unix SMB/CIFS implementation.
4 Winbind daemon - user related functions
6 Copyright (C) Tim Potter 2000
7 Copyright (C) Jeremy Allison 2001.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 #define DBGC_CLASS DBGC_WINBIND
29 /* Fill a pwent structure with information we have obtained */
31 static BOOL winbindd_fill_pwent(char *dom_name, char *user_name,
32 uint32 user_rid, uint32 group_rid,
33 char *full_name, struct winbindd_pw *pw)
35 extern userdom_struct current_user_info;
36 fstring output_username;
39 if (!pw || !dom_name || !user_name)
42 /* Resolve the uid number */
44 if (!winbindd_idmap_get_uid_from_rid(dom_name, user_rid,
46 DEBUG(1, ("error getting user id for rid %d\n", user_rid));
50 /* Resolve the gid number */
52 if (!winbindd_idmap_get_gid_from_rid(dom_name, group_rid,
54 DEBUG(1, ("error getting group id for rid %d\n", group_rid));
60 fill_domain_username(output_username, dom_name, user_name);
62 safe_strcpy(pw->pw_name, output_username, sizeof(pw->pw_name) - 1);
64 /* Full name (gecos) */
66 safe_strcpy(pw->pw_gecos, full_name, sizeof(pw->pw_gecos) - 1);
68 /* Home directory and shell - use template config parameters. The
69 defaults are /tmp for the home directory and /bin/false for
72 /* The substitution of %U and %D in the 'template homedir' is done
73 by lp_string() calling standard_sub_basic(). */
75 fstrcpy(current_user_info.smb_name, user_name);
76 fstrcpy(current_user_info.domain, dom_name);
78 pstrcpy(homedir, lp_template_homedir());
80 safe_strcpy(pw->pw_dir, homedir, sizeof(pw->pw_dir) - 1);
82 safe_strcpy(pw->pw_shell, lp_template_shell(),
83 sizeof(pw->pw_shell) - 1);
85 /* Password - set to "x" as we can't generate anything useful here.
86 Authentication can be done using the pam_winbind module. */
88 safe_strcpy(pw->pw_passwd, "x", sizeof(pw->pw_passwd) - 1);
93 /* Return a password structure from a username. */
95 enum winbindd_result winbindd_getpwnam(struct winbindd_cli_state *state)
98 WINBIND_USERINFO user_info;
101 fstring name_domain, name_user;
102 enum SID_NAME_USE name_type;
103 struct winbindd_domain *domain;
106 DEBUG(3, ("[%5d]: getpwnam %s\n", state->pid,
107 state->request.data.username));
109 /* Parse domain and username */
111 if (!parse_domain_user(state->request.data.username, name_domain,
113 return WINBINDD_ERROR;
115 if ((domain = find_domain_from_name(name_domain)) == NULL) {
116 DEBUG(5, ("no such domain: %s\n", name_domain));
117 return WINBINDD_ERROR;
120 /* Get rid and name type from name */
122 if (!winbindd_lookup_sid_by_name(domain, name_user, &user_sid, &name_type)) {
123 DEBUG(1, ("user '%s' does not exist\n", name_user));
124 return WINBINDD_ERROR;
127 if (name_type != SID_NAME_USER) {
128 DEBUG(1, ("name '%s' is not a user name: %d\n", name_user,
130 return WINBINDD_ERROR;
133 /* Get some user info. Split the user rid from the sid obtained
134 from the winbind_lookup_by_name() call and use it in a
135 winbind_lookup_userinfo() */
137 if (!(mem_ctx = talloc_init_named("winbindd_getpwnam([%s]\\[%s])",
138 name_domain, name_user))) {
139 DEBUG(1, ("out of memory\n"));
140 return WINBINDD_ERROR;
143 sid_split_rid(&user_sid, &user_rid);
145 status = domain->methods->query_user(domain, mem_ctx, user_rid,
148 if (!NT_STATUS_IS_OK(status)) {
149 DEBUG(1, ("error getting user info for user '[%s]\\[%s]'\n",
150 name_domain, name_user));
151 talloc_destroy(mem_ctx);
152 return WINBINDD_ERROR;
155 /* Now take all this information and fill in a passwd structure */
156 if (!winbindd_fill_pwent(name_domain, name_user,
157 user_rid, user_info.group_rid,
159 &state->response.data.pw)) {
160 talloc_destroy(mem_ctx);
161 return WINBINDD_ERROR;
164 talloc_destroy(mem_ctx);
169 /* Return a password structure given a uid number */
171 enum winbindd_result winbindd_getpwuid(struct winbindd_cli_state *state)
174 struct winbindd_domain *domain;
178 enum SID_NAME_USE name_type;
179 WINBIND_USERINFO user_info;
184 /* Bug out if the uid isn't in the winbind range */
186 if ((state->request.data.uid < server_state.uid_low ) ||
187 (state->request.data.uid > server_state.uid_high))
188 return WINBINDD_ERROR;
190 DEBUG(3, ("[%5d]: getpwuid %d\n", state->pid,
191 state->request.data.uid));
193 /* Get rid from uid */
195 if (!winbindd_idmap_get_rid_from_uid(state->request.data.uid,
196 &user_rid, &domain)) {
197 DEBUG(1, ("could not convert uid %d to rid\n",
198 state->request.data.uid));
199 return WINBINDD_ERROR;
202 /* Get name and name type from rid */
204 sid_copy(&user_sid, &domain->sid);
205 sid_append_rid(&user_sid, user_rid);
207 if (!winbindd_lookup_name_by_sid(&user_sid, dom_name, user_name, &name_type)) {
210 sid_to_string(temp, &user_sid);
211 DEBUG(1, ("could not lookup sid %s\n", temp));
212 return WINBINDD_ERROR;
215 /* Get some user info */
217 if (!(mem_ctx = talloc_init_named("winbind_getpwuid(%d)",
218 state->request.data.uid))) {
220 DEBUG(1, ("out of memory\n"));
221 return WINBINDD_ERROR;
224 status = domain->methods->query_user(domain, mem_ctx, user_rid,
227 if (!NT_STATUS_IS_OK(status)) {
228 DEBUG(1, ("error getting user info for user '%s'\n",
230 talloc_destroy(mem_ctx);
231 return WINBINDD_ERROR;
234 /* Resolve gid number */
236 if (!winbindd_idmap_get_gid_from_rid(domain->name, user_info.group_rid, &gid)) {
237 DEBUG(1, ("error getting group id for user %s\n", user_name));
238 talloc_destroy(mem_ctx);
239 return WINBINDD_ERROR;
242 /* Fill in password structure */
244 if (!winbindd_fill_pwent(domain->name, user_name, user_rid, user_info.group_rid,
245 user_info.full_name, &state->response.data.pw)) {
246 talloc_destroy(mem_ctx);
247 return WINBINDD_ERROR;
250 talloc_destroy(mem_ctx);
256 * set/get/endpwent functions
259 /* Rewind file pointer for ntdom passwd database */
261 enum winbindd_result winbindd_setpwent(struct winbindd_cli_state *state)
263 struct winbindd_domain *domain;
265 DEBUG(3, ("[%5d]: setpwent\n", state->pid));
267 /* Check user has enabled this */
269 if (!lp_winbind_enum_users())
270 return WINBINDD_ERROR;
272 /* Free old static data if it exists */
274 if (state->getpwent_state != NULL) {
275 free_getent_state(state->getpwent_state);
276 state->getpwent_state = NULL;
279 /* Create sam pipes for each domain we know about */
281 for(domain = domain_list(); domain != NULL; domain = domain->next) {
282 struct getent_state *domain_state;
285 * Skip domains other than WINBINDD_DOMAIN environment
289 if ((strcmp(state->request.domain, "") != 0) &&
290 !check_domain_env(state->request.domain,
294 /* Create a state record for this domain */
296 if ((domain_state = (struct getent_state *)
297 malloc(sizeof(struct getent_state))) == NULL)
298 return WINBINDD_ERROR;
300 ZERO_STRUCTP(domain_state);
302 fstrcpy(domain_state->domain_name, domain->name);
304 /* Add to list of open domains */
306 DLIST_ADD(state->getpwent_state, domain_state);
312 /* Close file pointer to ntdom passwd database */
314 enum winbindd_result winbindd_endpwent(struct winbindd_cli_state *state)
316 DEBUG(3, ("[%5d]: endpwent\n", state->pid));
318 free_getent_state(state->getpwent_state);
319 state->getpwent_state = NULL;
324 /* Get partial list of domain users for a domain. We fill in the sam_entries,
325 and num_sam_entries fields with domain user information. The dispinfo_ndx
326 field is incremented to the index of the next user to fetch. Return True if
327 some users were returned, False otherwise. */
329 #define MAX_FETCH_SAM_ENTRIES 100
331 static BOOL get_sam_user_entries(struct getent_state *ent)
335 WINBIND_USERINFO *info;
336 struct getpwent_user *name_list = NULL;
339 struct winbindd_domain *domain;
340 struct winbindd_methods *methods;
343 if (ent->num_sam_entries)
346 if (!(mem_ctx = talloc_init_named("get_sam_user_entries(%s)",
350 if (!(domain = find_domain_from_name(ent->domain_name))) {
351 DEBUG(3, ("no such domain %s in get_sam_user_entries\n",
356 methods = domain->methods;
358 /* Free any existing user info */
360 SAFE_FREE(ent->sam_entries);
361 ent->num_sam_entries = 0;
363 /* Call query_user_list to get a list of usernames and user rids */
367 status = methods->query_user_list(domain, mem_ctx, &num_entries,
371 struct getpwent_user *tnl;
373 tnl = (struct getpwent_user *)Realloc(name_list,
374 sizeof(struct getpwent_user) *
375 (ent->num_sam_entries +
379 DEBUG(0,("get_sam_user_entries realloc failed.\n"));
380 SAFE_FREE(name_list);
386 for (i = 0; i < num_entries; i++) {
387 /* Store account name and gecos */
388 if (!info[i].acct_name) {
389 fstrcpy(name_list[ent->num_sam_entries + i].name, "");
391 fstrcpy(name_list[ent->num_sam_entries + i].name,
394 if (!info[i].full_name) {
395 fstrcpy(name_list[ent->num_sam_entries + i].gecos, "");
397 fstrcpy(name_list[ent->num_sam_entries + i].gecos,
401 /* User and group ids */
402 name_list[ent->num_sam_entries+i].user_rid = info[i].user_rid;
403 name_list[ent->num_sam_entries+i].group_rid = info[i].group_rid;
406 ent->num_sam_entries += num_entries;
408 /* Fill in remaining fields */
410 ent->sam_entries = name_list;
411 ent->sam_entry_index = 0;
412 result = ent->num_sam_entries > 0;
416 talloc_destroy(mem_ctx);
421 /* Fetch next passwd entry from ntdom database */
423 #define MAX_GETPWENT_USERS 500
425 enum winbindd_result winbindd_getpwent(struct winbindd_cli_state *state)
427 struct getent_state *ent;
428 struct winbindd_pw *user_list;
429 int num_users, user_list_ndx = 0, i;
431 DEBUG(3, ("[%5d]: getpwent\n", state->pid));
433 /* Check user has enabled this */
435 if (!lp_winbind_enum_users())
436 return WINBINDD_ERROR;
438 /* Allocate space for returning a chunk of users */
440 num_users = MIN(MAX_GETPWENT_USERS, state->request.data.num_entries);
442 if ((state->response.extra_data =
443 malloc(num_users * sizeof(struct winbindd_pw))) == NULL)
444 return WINBINDD_ERROR;
446 memset(state->response.extra_data, 0, num_users *
447 sizeof(struct winbindd_pw));
449 user_list = (struct winbindd_pw *)state->response.extra_data;
451 if (!(ent = state->getpwent_state))
452 return WINBINDD_ERROR;
454 /* Start sending back users */
456 for (i = 0; i < num_users; i++) {
457 struct getpwent_user *name_list = NULL;
458 fstring domain_user_name;
461 /* Do we need to fetch another chunk of users? */
463 if (ent->num_sam_entries == ent->sam_entry_index) {
465 while(ent && !get_sam_user_entries(ent)) {
466 struct getent_state *next_ent;
468 /* Free state information for this domain */
470 SAFE_FREE(ent->sam_entries);
472 next_ent = ent->next;
473 DLIST_REMOVE(state->getpwent_state, ent);
479 /* No more domains */
485 name_list = ent->sam_entries;
487 /* Skip machine accounts */
489 if (name_list[ent->sam_entry_index].
490 name[strlen(name_list[ent->sam_entry_index].name) - 1]
492 ent->sam_entry_index++;
496 /* Lookup user info */
498 result = winbindd_fill_pwent(
500 name_list[ent->sam_entry_index].name,
501 name_list[ent->sam_entry_index].user_rid,
502 name_list[ent->sam_entry_index].group_rid,
503 name_list[ent->sam_entry_index].gecos,
504 &user_list[user_list_ndx]);
506 ent->sam_entry_index++;
508 /* Add user to return list */
513 state->response.data.num_entries++;
514 state->response.length +=
515 sizeof(struct winbindd_pw);
518 DEBUG(1, ("could not lookup domain user %s\n",
524 return (user_list_ndx > 0) ? WINBINDD_OK : WINBINDD_ERROR;
527 /* List domain users without mapping to unix ids */
529 enum winbindd_result winbindd_list_users(struct winbindd_cli_state *state)
531 struct winbindd_domain *domain;
532 WINBIND_USERINFO *info;
533 uint32 num_entries = 0, total_entries = 0;
534 char *ted, *extra_data = NULL;
535 int extra_data_len = 0;
537 enum winbindd_result rv = WINBINDD_ERROR;
539 DEBUG(3, ("[%5d]: list users\n", state->pid));
541 if (!(mem_ctx = talloc_init_named("winbindd_list_users")))
542 return WINBINDD_ERROR;
544 /* Enumerate over trusted domains */
546 for (domain = domain_list(); domain; domain = domain->next) {
548 struct winbindd_methods *methods;
551 /* Skip domains other than WINBINDD_DOMAIN environment
554 if ((strcmp(state->request.domain, "") != 0) &&
555 !check_domain_env(state->request.domain, domain->name))
558 methods = domain->methods;
560 /* Query display info */
561 status = methods->query_user_list(domain, mem_ctx,
562 &num_entries, &info);
564 if (num_entries == 0)
567 /* Allocate some memory for extra data */
568 total_entries += num_entries;
570 ted = Realloc(extra_data, sizeof(fstring) * total_entries);
573 DEBUG(0,("failed to enlarge buffer!\n"));
574 SAFE_FREE(extra_data);
579 /* Pack user list into extra data fields */
581 for (i = 0; i < num_entries; i++) {
582 fstring acct_name, name;
584 if (!info[i].acct_name) {
585 fstrcpy(acct_name, "");
587 fstrcpy(acct_name, info[i].acct_name);
590 fill_domain_username(name, domain->name, acct_name);
592 /* Append to extra data */
593 memcpy(&extra_data[extra_data_len], name,
595 extra_data_len += strlen(name);
596 extra_data[extra_data_len++] = ',';
600 /* Assign extra_data fields in response structure */
603 extra_data[extra_data_len - 1] = '\0';
604 state->response.extra_data = extra_data;
605 state->response.length += extra_data_len;
608 /* No domains responded but that's still OK so don't return an
615 talloc_destroy(mem_ctx);