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_from_user(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())) {
141 DEBUG(1, ("out of memory\n"));
142 return WINBINDD_ERROR;
145 sid_split_rid(&user_sid, &user_rid);
147 status = domain->methods->query_user(domain, mem_ctx, user_rid, &user_info);
148 if (!NT_STATUS_IS_OK(status)) {
149 DEBUG(1, ("pwnam_from_user(): error getting user info for "
150 "user '%s'\n", 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, state->request.data.username,
157 user_rid, user_info.group_rid, user_info.full_name,
158 &state->response.data.pw)) {
159 talloc_destroy(mem_ctx);
160 return WINBINDD_ERROR;
163 talloc_destroy(mem_ctx);
168 /* Return a password structure given a uid number */
170 enum winbindd_result winbindd_getpwnam_from_uid(struct winbindd_cli_state *state)
173 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, 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 if (strcmp("\\", lp_winbind_separator()))
214 string_sub(user_name, "\\", lp_winbind_separator(),
217 /* Get some user info */
219 if (!(mem_ctx = talloc_init())) {
220 DEBUG(1, ("out of memory\n"));
221 return WINBINDD_ERROR;
224 status = domain->methods->query_user(domain, mem_ctx, user_rid, &user_info);
225 if (!NT_STATUS_IS_OK(status)) {
226 DEBUG(1, ("pwnam_from_uid(): error getting user info for "
227 "user '%s'\n", user_name));
228 return WINBINDD_ERROR;
231 /* Resolve gid number */
233 if (!winbindd_idmap_get_gid_from_rid(domain->name, user_info.group_rid, &gid)) {
234 DEBUG(1, ("error getting group id for user %s\n", user_name));
235 return WINBINDD_ERROR;
238 /* Fill in password structure */
240 if (!winbindd_fill_pwent(domain->name, user_name, user_rid, user_info.group_rid,
241 user_info.full_name, &state->response.data.pw)) {
242 return WINBINDD_ERROR;
245 talloc_destroy(mem_ctx);
251 * set/get/endpwent functions
254 /* Rewind file pointer for ntdom passwd database */
256 enum winbindd_result winbindd_setpwent(struct winbindd_cli_state *state)
258 struct winbindd_domain *tmp;
260 DEBUG(3, ("[%5d]: setpwent\n", state->pid));
262 /* Check user has enabled this */
264 if (!lp_winbind_enum_users())
265 return WINBINDD_ERROR;
267 /* Free old static data if it exists */
269 if (state->getpwent_state != NULL) {
270 free_getent_state(state->getpwent_state);
271 state->getpwent_state = NULL;
274 /* Create sam pipes for each domain we know about */
276 if (domain_list == NULL)
279 for(tmp = domain_list; tmp != NULL; tmp = tmp->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, tmp->name))
291 /* Create a state record for this domain */
293 if ((domain_state = (struct getent_state *)malloc(sizeof(struct getent_state))) == NULL)
294 return WINBINDD_ERROR;
296 ZERO_STRUCTP(domain_state);
298 domain_state->domain = tmp;
300 /* Add to list of open domains */
302 DLIST_ADD(state->getpwent_state, domain_state);
308 /* Close file pointer to ntdom passwd database */
310 enum winbindd_result winbindd_endpwent(struct winbindd_cli_state *state)
312 DEBUG(3, ("[%5d]: endpwent\n", state->pid));
314 free_getent_state(state->getpwent_state);
315 state->getpwent_state = NULL;
320 /* Get partial list of domain users for a domain. We fill in the sam_entries,
321 and num_sam_entries fields with domain user information. The dispinfo_ndx
322 field is incremented to the index of the next user to fetch. Return True if
323 some users were returned, False otherwise. */
325 #define MAX_FETCH_SAM_ENTRIES 100
327 static BOOL get_sam_user_entries(struct getent_state *ent)
331 WINBIND_USERINFO *info;
332 struct getpwent_user *name_list = NULL;
335 struct winbindd_methods *methods;
338 if (ent->num_sam_entries)
341 if (!(mem_ctx = talloc_init()))
344 methods = ent->domain->methods;
346 /* Free any existing user info */
348 SAFE_FREE(ent->sam_entries);
349 ent->num_sam_entries = 0;
351 /* Call query_user_list to get a list of usernames and user rids */
354 status = methods->query_user_list(ent->domain, mem_ctx,
355 &num_entries, &info);
358 struct getpwent_user *tnl;
360 tnl = (struct getpwent_user *)Realloc(name_list,
361 sizeof(struct getpwent_user) *
362 (ent->num_sam_entries +
366 DEBUG(0,("get_sam_user_entries: Realloc failed.\n"));
367 SAFE_FREE(name_list);
373 for (i = 0; i < num_entries; i++) {
374 /* Store account name and gecos */
375 if (!info[i].acct_name) {
376 fstrcpy(name_list[ent->num_sam_entries + i].name, "");
378 fstrcpy(name_list[ent->num_sam_entries + i].name,
381 if (!info[i].full_name) {
382 fstrcpy(name_list[ent->num_sam_entries + i].gecos, "");
384 fstrcpy(name_list[ent->num_sam_entries + i].gecos,
388 /* User and group ids */
389 name_list[ent->num_sam_entries+i].user_rid = info[i].user_rid;
390 name_list[ent->num_sam_entries+i].group_rid = info[i].group_rid;
393 ent->num_sam_entries += num_entries;
395 /* Fill in remaining fields */
397 ent->sam_entries = name_list;
398 ent->sam_entry_index = 0;
399 result = ent->num_sam_entries > 0;
403 talloc_destroy(mem_ctx);
408 /* Fetch next passwd entry from ntdom database */
410 #define MAX_GETPWENT_USERS 500
412 enum winbindd_result winbindd_getpwent(struct winbindd_cli_state *state)
414 struct getent_state *ent;
415 struct winbindd_pw *user_list;
416 int num_users, user_list_ndx = 0, i;
419 DEBUG(3, ("[%5d]: getpwent\n", state->pid));
421 /* Check user has enabled this */
423 if (!lp_winbind_enum_users())
424 return WINBINDD_ERROR;
426 /* Allocate space for returning a chunk of users */
428 num_users = MIN(MAX_GETPWENT_USERS, state->request.data.num_entries);
430 if ((state->response.extra_data =
431 malloc(num_users * sizeof(struct winbindd_pw))) == NULL)
432 return WINBINDD_ERROR;
434 memset(state->response.extra_data, 0, num_users *
435 sizeof(struct winbindd_pw));
437 user_list = (struct winbindd_pw *)state->response.extra_data;
438 sep = lp_winbind_separator();
440 if (!(ent = state->getpwent_state))
441 return WINBINDD_ERROR;
443 /* Start sending back users */
445 for (i = 0; i < num_users; i++) {
446 struct getpwent_user *name_list = NULL;
447 fstring domain_user_name;
450 /* Do we need to fetch another chunk of users? */
452 if (ent->num_sam_entries == ent->sam_entry_index) {
454 while(ent && !get_sam_user_entries(ent)) {
455 struct getent_state *next_ent;
457 /* Free state information for this domain */
459 SAFE_FREE(ent->sam_entries);
461 next_ent = ent->next;
462 DLIST_REMOVE(state->getpwent_state, ent);
468 /* No more domains */
474 name_list = ent->sam_entries;
476 /* Skip machine accounts */
478 if (name_list[ent->sam_entry_index].
479 name[strlen(name_list[ent->sam_entry_index].name) - 1]
481 ent->sam_entry_index++;
485 /* Lookup user info */
487 slprintf(domain_user_name, sizeof(domain_user_name) - 1,
488 "%s%s%s", ent->domain->name, sep,
489 name_list[ent->sam_entry_index].name);
491 result = winbindd_fill_pwent(
494 name_list[ent->sam_entry_index].user_rid,
495 name_list[ent->sam_entry_index].group_rid,
496 name_list[ent->sam_entry_index].gecos,
497 &user_list[user_list_ndx]);
499 ent->sam_entry_index++;
501 /* Add user to return list */
506 state->response.data.num_entries++;
507 state->response.length +=
508 sizeof(struct winbindd_pw);
511 DEBUG(1, ("could not lookup domain user %s\n",
517 return (user_list_ndx > 0) ? WINBINDD_OK : WINBINDD_ERROR;
520 /* List domain users without mapping to unix ids */
522 enum winbindd_result winbindd_list_users(struct winbindd_cli_state *state)
524 struct winbindd_domain *domain;
525 WINBIND_USERINFO *info;
526 uint32 num_entries = 0, total_entries = 0;
527 char *ted, *extra_data = NULL;
528 int extra_data_len = 0;
530 enum winbindd_result rv = WINBINDD_ERROR;
532 DEBUG(3, ("[%5d]: list users\n", state->pid));
534 if (!(mem_ctx = talloc_init()))
535 return WINBINDD_ERROR;
537 /* Enumerate over trusted domains */
539 if (domain_list == NULL)
542 for (domain = domain_list; domain; domain = domain->next) {
544 struct winbindd_methods *methods;
547 /* Skip domains other than WINBINDD_DOMAIN environment
550 if ((strcmp(state->request.domain, "") != 0) &&
551 !check_domain_env(state->request.domain, domain->name))
554 methods = domain->methods;
556 /* Query display info */
557 status = methods->query_user_list(domain, mem_ctx,
558 &num_entries, &info);
560 if (num_entries == 0)
563 /* Allocate some memory for extra data */
564 total_entries += num_entries;
566 ted = Realloc(extra_data, sizeof(fstring) * total_entries);
569 DEBUG(0,("winbindd_list_users: failed to enlarge buffer!\n"));
570 SAFE_FREE(extra_data);
575 /* Pack user list into extra data fields */
577 for (i = 0; i < num_entries; i++) {
578 fstring acct_name, name;
580 if (!info[i].acct_name) {
581 fstrcpy(acct_name, "");
583 fstrcpy(acct_name, info[i].acct_name);
586 slprintf(name, sizeof(name) - 1, "%s%s%s",
587 domain->name, lp_winbind_separator(),
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);