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 if (!parse_domain_user(name, name_domain, name_user)) {
69 DEBUG(1, ("error parsing domain user for %s\n", name_user ));
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, name_user);
77 fstrcpy(current_user_info.domain, name_domain);
79 pstrcpy(homedir, lp_template_homedir());
81 safe_strcpy(pw->pw_dir, homedir, sizeof(pw->pw_dir) - 1);
83 safe_strcpy(pw->pw_shell, lp_template_shell(),
84 sizeof(pw->pw_shell) - 1);
86 /* Password - set to "x" as we can't generate anything useful here.
87 Authentication can be done using the pam_ntdom module. */
89 safe_strcpy(pw->pw_passwd, "x", sizeof(pw->pw_passwd) - 1);
94 /* Return a password structure from a username. */
96 enum winbindd_result winbindd_getpwnam(struct winbindd_cli_state *state)
99 WINBIND_USERINFO user_info;
102 fstring name_domain, name_user, name;
103 enum SID_NAME_USE name_type;
104 struct winbindd_domain *domain;
107 DEBUG(3, ("[%5d]: getpwnam %s\n", state->pid,
108 state->request.data.username));
110 /* Parse domain and username */
112 if (!parse_domain_user(state->request.data.username, name_domain,
114 return WINBINDD_ERROR;
116 if ((domain = find_domain_from_name(name_domain)) == NULL) {
117 DEBUG(5, ("no such domain: %s\n", name_domain));
118 return WINBINDD_ERROR;
121 slprintf(name, sizeof(name) - 1, "%s\\%s", name_domain, name_user);
123 /* Get rid and name type from name */
125 if (!winbindd_lookup_sid_by_name(domain, name, &user_sid, &name_type)) {
126 DEBUG(1, ("user '%s' does not exist\n", name_user));
127 return WINBINDD_ERROR;
130 if (name_type != SID_NAME_USER) {
131 DEBUG(1, ("name '%s' is not a user name: %d\n", name_user,
133 return WINBINDD_ERROR;
136 /* Get some user info. Split the user rid from the sid obtained
137 from the winbind_lookup_by_name() call and use it in a
138 winbind_lookup_userinfo() */
140 if (!(mem_ctx = talloc_init_named("winbindd_getpwnam(%s)",
142 DEBUG(1, ("out of memory\n"));
143 return WINBINDD_ERROR;
146 sid_split_rid(&user_sid, &user_rid);
148 status = domain->methods->query_user(domain, mem_ctx, user_rid,
151 if (!NT_STATUS_IS_OK(status)) {
152 DEBUG(1, ("error getting user info for user '%s'\n",
154 talloc_destroy(mem_ctx);
155 return WINBINDD_ERROR;
158 /* Now take all this information and fill in a passwd structure */
159 if (!winbindd_fill_pwent(name_domain, state->request.data.username,
160 user_rid, user_info.group_rid,
162 &state->response.data.pw)) {
163 talloc_destroy(mem_ctx);
164 return WINBINDD_ERROR;
167 talloc_destroy(mem_ctx);
172 /* Return a password structure given a uid number */
174 enum winbindd_result winbindd_getpwuid(struct winbindd_cli_state *state)
177 struct winbindd_domain *domain;
180 enum SID_NAME_USE name_type;
181 WINBIND_USERINFO user_info;
186 /* Bug out if the uid isn't in the winbind range */
188 if ((state->request.data.uid < server_state.uid_low ) ||
189 (state->request.data.uid > server_state.uid_high))
190 return WINBINDD_ERROR;
192 DEBUG(3, ("[%5d]: getpwuid %d\n", state->pid,
193 state->request.data.uid));
195 /* Get rid from uid */
197 if (!winbindd_idmap_get_rid_from_uid(state->request.data.uid,
198 &user_rid, &domain)) {
199 DEBUG(1, ("could not convert uid %d to rid\n",
200 state->request.data.uid));
201 return WINBINDD_ERROR;
204 /* Get name and name type from rid */
206 sid_copy(&user_sid, &domain->sid);
207 sid_append_rid(&user_sid, user_rid);
209 if (!winbindd_lookup_name_by_sid(&user_sid, user_name, &name_type)) {
212 sid_to_string(temp, &user_sid);
213 DEBUG(1, ("could not lookup sid %s\n", temp));
214 return WINBINDD_ERROR;
217 if (strcmp("\\", lp_winbind_separator()))
218 string_sub(user_name, "\\", lp_winbind_separator(),
221 /* Get some user info */
223 if (!(mem_ctx = talloc_init_named("winbind_getpwuid(%d)",
224 state->request.data.uid))) {
226 DEBUG(1, ("out of memory\n"));
227 return WINBINDD_ERROR;
230 status = domain->methods->query_user(domain, mem_ctx, user_rid,
233 if (!NT_STATUS_IS_OK(status)) {
234 DEBUG(1, ("error getting user info for user '%s'\n",
236 return WINBINDD_ERROR;
239 /* Resolve gid number */
241 if (!winbindd_idmap_get_gid_from_rid(domain->name, user_info.group_rid, &gid)) {
242 DEBUG(1, ("error getting group id for user %s\n", user_name));
243 return WINBINDD_ERROR;
246 /* Fill in password structure */
248 if (!winbindd_fill_pwent(domain->name, user_name, user_rid, user_info.group_rid,
249 user_info.full_name, &state->response.data.pw)) {
250 return WINBINDD_ERROR;
253 talloc_destroy(mem_ctx);
259 * set/get/endpwent functions
262 /* Rewind file pointer for ntdom passwd database */
264 enum winbindd_result winbindd_setpwent(struct winbindd_cli_state *state)
266 struct winbindd_domain *domain;
268 DEBUG(3, ("[%5d]: setpwent\n", state->pid));
270 /* Check user has enabled this */
272 if (!lp_winbind_enum_users())
273 return WINBINDD_ERROR;
275 /* Free old static data if it exists */
277 if (state->getpwent_state != NULL) {
278 free_getent_state(state->getpwent_state);
279 state->getpwent_state = NULL;
282 /* Create sam pipes for each domain we know about */
284 if (domain_list == NULL)
287 for(domain = domain_list; domain != NULL; domain = domain->next) {
288 struct getent_state *domain_state;
291 * Skip domains other than WINBINDD_DOMAIN environment
295 if ((strcmp(state->request.domain, "") != 0) &&
296 !check_domain_env(state->request.domain,
300 /* Create a state record for this domain */
302 if ((domain_state = (struct getent_state *)
303 malloc(sizeof(struct getent_state))) == NULL)
304 return WINBINDD_ERROR;
306 ZERO_STRUCTP(domain_state);
308 domain_state->domain = domain;
310 /* Add to list of open domains */
312 DLIST_ADD(state->getpwent_state, domain_state);
318 /* Close file pointer to ntdom passwd database */
320 enum winbindd_result winbindd_endpwent(struct winbindd_cli_state *state)
322 DEBUG(3, ("[%5d]: endpwent\n", state->pid));
324 free_getent_state(state->getpwent_state);
325 state->getpwent_state = NULL;
330 /* Get partial list of domain users for a domain. We fill in the sam_entries,
331 and num_sam_entries fields with domain user information. The dispinfo_ndx
332 field is incremented to the index of the next user to fetch. Return True if
333 some users were returned, False otherwise. */
335 #define MAX_FETCH_SAM_ENTRIES 100
337 static BOOL get_sam_user_entries(struct getent_state *ent)
341 WINBIND_USERINFO *info;
342 struct getpwent_user *name_list = NULL;
345 struct winbindd_methods *methods;
348 if (ent->num_sam_entries)
351 if (!(mem_ctx = talloc_init_named("get_sam_user_entries(%s)",
355 methods = ent->domain->methods;
357 /* Free any existing user info */
359 SAFE_FREE(ent->sam_entries);
360 ent->num_sam_entries = 0;
362 /* Call query_user_list to get a list of usernames and user rids */
365 status = methods->query_user_list(ent->domain, mem_ctx,
366 &num_entries, &info);
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;
430 DEBUG(3, ("[%5d]: getpwent\n", state->pid));
432 /* Check user has enabled this */
434 if (!lp_winbind_enum_users())
435 return WINBINDD_ERROR;
437 /* Allocate space for returning a chunk of users */
439 num_users = MIN(MAX_GETPWENT_USERS, state->request.data.num_entries);
441 if ((state->response.extra_data =
442 malloc(num_users * sizeof(struct winbindd_pw))) == NULL)
443 return WINBINDD_ERROR;
445 memset(state->response.extra_data, 0, num_users *
446 sizeof(struct winbindd_pw));
448 user_list = (struct winbindd_pw *)state->response.extra_data;
449 sep = lp_winbind_separator();
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 slprintf(domain_user_name, sizeof(domain_user_name) - 1,
499 "%s%s%s", ent->domain->name, sep,
500 name_list[ent->sam_entry_index].name);
502 result = winbindd_fill_pwent(
505 name_list[ent->sam_entry_index].user_rid,
506 name_list[ent->sam_entry_index].group_rid,
507 name_list[ent->sam_entry_index].gecos,
508 &user_list[user_list_ndx]);
510 ent->sam_entry_index++;
512 /* Add user to return list */
517 state->response.data.num_entries++;
518 state->response.length +=
519 sizeof(struct winbindd_pw);
522 DEBUG(1, ("could not lookup domain user %s\n",
528 return (user_list_ndx > 0) ? WINBINDD_OK : WINBINDD_ERROR;
531 /* List domain users without mapping to unix ids */
533 enum winbindd_result winbindd_list_users(struct winbindd_cli_state *state)
535 struct winbindd_domain *domain;
536 WINBIND_USERINFO *info;
537 uint32 num_entries = 0, total_entries = 0;
538 char *ted, *extra_data = NULL;
539 int extra_data_len = 0;
541 enum winbindd_result rv = WINBINDD_ERROR;
543 DEBUG(3, ("[%5d]: list users\n", state->pid));
545 if (!(mem_ctx = talloc_init_named("winbindd_list_users")))
546 return WINBINDD_ERROR;
548 /* Enumerate over trusted domains */
550 if (domain_list == NULL)
553 for (domain = domain_list; domain; domain = domain->next) {
555 struct winbindd_methods *methods;
558 /* Skip domains other than WINBINDD_DOMAIN environment
561 if ((strcmp(state->request.domain, "") != 0) &&
562 !check_domain_env(state->request.domain, domain->name))
565 methods = domain->methods;
567 /* Query display info */
568 status = methods->query_user_list(domain, mem_ctx,
569 &num_entries, &info);
571 if (num_entries == 0)
574 /* Allocate some memory for extra data */
575 total_entries += num_entries;
577 ted = Realloc(extra_data, sizeof(fstring) * total_entries);
580 DEBUG(0,("failed to enlarge buffer!\n"));
581 SAFE_FREE(extra_data);
586 /* Pack user list into extra data fields */
588 for (i = 0; i < num_entries; i++) {
589 fstring acct_name, name;
591 if (!info[i].acct_name) {
592 fstrcpy(acct_name, "");
594 fstrcpy(acct_name, info[i].acct_name);
597 slprintf(name, sizeof(name) - 1, "%s%s%s",
598 domain->name, lp_winbind_separator(),
601 /* Append to extra data */
602 memcpy(&extra_data[extra_data_len], name,
604 extra_data_len += strlen(name);
605 extra_data[extra_data_len++] = ',';
609 /* Assign extra_data fields in response structure */
612 extra_data[extra_data_len - 1] = '\0';
613 state->response.extra_data = extra_data;
614 state->response.length += extra_data_len;
617 /* No domains responded but that's still OK so don't return an
624 talloc_destroy(mem_ctx);