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 DOM_SID *user_sid, DOM_SID *group_sid,
33 char *full_name, struct winbindd_pw *pw)
35 extern userdom_struct current_user_info;
36 fstring output_username;
40 if (!pw || !dom_name || !user_name)
43 /* Resolve the uid number */
45 if (NT_STATUS_IS_ERR(sid_to_uid(user_sid, &(pw->pw_uid)))) {
46 DEBUG(1, ("error getting user id for sid %s\n", sid_to_string(sid_string, user_sid)));
50 /* Resolve the gid number */
52 if (NT_STATUS_IS_ERR(sid_to_gid(group_sid, &(pw->pw_gid)))) {
53 DEBUG(1, ("error getting group id for sid %s\n", sid_to_string(sid_string, group_sid)));
59 fill_domain_username(output_username, dom_name, user_name);
61 safe_strcpy(pw->pw_name, output_username, sizeof(pw->pw_name) - 1);
63 /* Full name (gecos) */
65 safe_strcpy(pw->pw_gecos, full_name, sizeof(pw->pw_gecos) - 1);
67 /* Home directory and shell - use template config parameters. The
68 defaults are /tmp for the home directory and /bin/false for
71 /* The substitution of %U and %D in the 'template homedir' is done
72 by lp_string() calling standard_sub_basic(). */
74 fstrcpy(current_user_info.smb_name, user_name);
75 sub_set_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)
97 WINBIND_USERINFO user_info;
100 fstring name_domain, name_user;
101 enum SID_NAME_USE name_type;
102 struct winbindd_domain *domain;
105 /* Ensure null termination */
106 state->request.data.username[sizeof(state->request.data.username)-1]='\0';
108 DEBUG(3, ("[%5d]: getpwnam %s\n", state->pid,
109 state->request.data.username));
111 /* Parse domain and username */
113 if (!parse_domain_user(state->request.data.username, name_domain,
115 return WINBINDD_ERROR;
117 if ((domain = find_domain_from_name(name_domain)) == NULL) {
118 DEBUG(5, ("no such domain: %s\n", name_domain));
119 return WINBINDD_ERROR;
122 /* Get rid and name type from name */
124 if (!winbindd_lookup_sid_by_name(domain, name_user, &user_sid, &name_type)) {
125 DEBUG(1, ("user '%s' does not exist\n", name_user));
126 return WINBINDD_ERROR;
129 if (name_type != SID_NAME_USER) {
130 DEBUG(1, ("name '%s' is not a user name: %d\n", name_user,
132 return WINBINDD_ERROR;
135 /* Get some user info. Split the user rid from the sid obtained
136 from the winbind_lookup_by_name() call and use it in a
137 winbind_lookup_userinfo() */
139 if (!(mem_ctx = talloc_init("winbindd_getpwnam([%s]\\[%s])",
140 name_domain, name_user))) {
141 DEBUG(1, ("out of memory\n"));
142 return WINBINDD_ERROR;
145 status = domain->methods->query_user(domain, mem_ctx, &user_sid,
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_info.user_sid, user_info.group_sid,
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;
177 enum SID_NAME_USE name_type;
178 WINBIND_USERINFO user_info;
183 /* Bug out if the uid isn't in the winbind range */
185 if ((state->request.data.uid < server_state.uid_low ) ||
186 (state->request.data.uid > server_state.uid_high))
187 return WINBINDD_ERROR;
189 DEBUG(3, ("[%5d]: getpwuid %d\n", state->pid,
190 state->request.data.uid));
192 /* Get rid from uid */
194 if (NT_STATUS_IS_ERR(uid_to_sid(&user_sid, state->request.data.uid))) {
195 DEBUG(1, ("could not convert uid %d to SID\n",
196 state->request.data.uid));
197 return WINBINDD_ERROR;
200 /* Get name and name type from rid */
202 if (!winbindd_lookup_name_by_sid(&user_sid, dom_name, user_name, &name_type)) {
205 sid_to_string(temp, &user_sid);
206 DEBUG(1, ("could not lookup sid %s\n", temp));
207 return WINBINDD_ERROR;
210 domain = find_domain_from_sid(&user_sid);
213 DEBUG(1,("Can't find domain from sid\n"));
214 return WINBINDD_ERROR;
217 /* Get some user info */
219 if (!(mem_ctx = talloc_init("winbind_getpwuid(%d)",
220 state->request.data.uid))) {
222 DEBUG(1, ("out of memory\n"));
223 return WINBINDD_ERROR;
226 status = domain->methods->query_user(domain, mem_ctx, &user_sid,
229 if (!NT_STATUS_IS_OK(status)) {
230 DEBUG(1, ("error getting user info for user '%s'\n",
232 talloc_destroy(mem_ctx);
233 return WINBINDD_ERROR;
236 /* Check group has a gid number */
238 if (NT_STATUS_IS_ERR(sid_to_gid(user_info.group_sid, &gid))) {
239 DEBUG(1, ("error getting group id for user %s\n", user_name));
240 talloc_destroy(mem_ctx);
241 return WINBINDD_ERROR;
244 /* Fill in password structure */
246 if (!winbindd_fill_pwent(domain->name, user_name, user_info.user_sid,
248 user_info.full_name, &state->response.data.pw)) {
249 talloc_destroy(mem_ctx);
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 for(domain = domain_list(); domain != NULL; domain = domain->next) {
285 struct getent_state *domain_state;
287 /* Create a state record for this domain */
289 if ((domain_state = (struct getent_state *)
290 malloc(sizeof(struct getent_state))) == NULL)
291 return WINBINDD_ERROR;
293 ZERO_STRUCTP(domain_state);
295 fstrcpy(domain_state->domain_name, domain->name);
297 /* Add to list of open domains */
299 DLIST_ADD(state->getpwent_state, domain_state);
305 /* Close file pointer to ntdom passwd database */
307 enum winbindd_result winbindd_endpwent(struct winbindd_cli_state *state)
309 DEBUG(3, ("[%5d]: endpwent\n", state->pid));
311 free_getent_state(state->getpwent_state);
312 state->getpwent_state = NULL;
317 /* Get partial list of domain users for a domain. We fill in the sam_entries,
318 and num_sam_entries fields with domain user information. The dispinfo_ndx
319 field is incremented to the index of the next user to fetch. Return True if
320 some users were returned, False otherwise. */
322 #define MAX_FETCH_SAM_ENTRIES 100
324 static BOOL get_sam_user_entries(struct getent_state *ent)
328 WINBIND_USERINFO *info;
329 struct getpwent_user *name_list = NULL;
332 struct winbindd_domain *domain;
333 struct winbindd_methods *methods;
336 if (ent->num_sam_entries)
339 if (!(mem_ctx = talloc_init("get_sam_user_entries(%s)",
343 if (!(domain = find_domain_from_name(ent->domain_name))) {
344 DEBUG(3, ("no such domain %s in get_sam_user_entries\n",
349 methods = domain->methods;
351 /* Free any existing user info */
353 SAFE_FREE(ent->sam_entries);
354 ent->num_sam_entries = 0;
356 /* Call query_user_list to get a list of usernames and user rids */
360 status = methods->query_user_list(domain, mem_ctx, &num_entries,
364 struct getpwent_user *tnl;
366 tnl = (struct getpwent_user *)Realloc(name_list,
367 sizeof(struct getpwent_user) *
368 (ent->num_sam_entries +
372 DEBUG(0,("get_sam_user_entries realloc failed.\n"));
373 SAFE_FREE(name_list);
379 for (i = 0; i < num_entries; i++) {
380 /* Store account name and gecos */
381 if (!info[i].acct_name) {
382 fstrcpy(name_list[ent->num_sam_entries + i].name, "");
384 fstrcpy(name_list[ent->num_sam_entries + i].name,
387 if (!info[i].full_name) {
388 fstrcpy(name_list[ent->num_sam_entries + i].gecos, "");
390 fstrcpy(name_list[ent->num_sam_entries + i].gecos,
394 /* User and group ids */
395 sid_copy(&name_list[ent->num_sam_entries+i].user_sid, info[i].user_sid);
396 sid_copy(&name_list[ent->num_sam_entries+i].group_sid, info[i].group_sid);
399 ent->num_sam_entries += num_entries;
401 /* Fill in remaining fields */
403 ent->sam_entries = name_list;
404 ent->sam_entry_index = 0;
405 result = ent->num_sam_entries > 0;
409 talloc_destroy(mem_ctx);
414 /* Fetch next passwd entry from ntdom database */
416 #define MAX_GETPWENT_USERS 500
418 enum winbindd_result winbindd_getpwent(struct winbindd_cli_state *state)
420 struct getent_state *ent;
421 struct winbindd_pw *user_list;
422 int num_users, user_list_ndx = 0, i;
424 DEBUG(3, ("[%5d]: getpwent\n", state->pid));
426 /* Check user has enabled this */
428 if (!lp_winbind_enum_users())
429 return WINBINDD_ERROR;
431 /* Allocate space for returning a chunk of users */
433 num_users = MIN(MAX_GETPWENT_USERS, state->request.data.num_entries);
435 if ((state->response.extra_data =
436 malloc(num_users * sizeof(struct winbindd_pw))) == NULL)
437 return WINBINDD_ERROR;
439 memset(state->response.extra_data, 0, num_users *
440 sizeof(struct winbindd_pw));
442 user_list = (struct winbindd_pw *)state->response.extra_data;
444 if (!(ent = state->getpwent_state))
445 return WINBINDD_ERROR;
447 /* Start sending back users */
449 for (i = 0; i < num_users; i++) {
450 struct getpwent_user *name_list = NULL;
453 /* Do we need to fetch another chunk of users? */
455 if (ent->num_sam_entries == ent->sam_entry_index) {
457 while(ent && !get_sam_user_entries(ent)) {
458 struct getent_state *next_ent;
460 /* Free state information for this domain */
462 SAFE_FREE(ent->sam_entries);
464 next_ent = ent->next;
465 DLIST_REMOVE(state->getpwent_state, ent);
471 /* No more domains */
477 name_list = ent->sam_entries;
479 /* Skip machine accounts */
481 if (name_list[ent->sam_entry_index].
482 name[strlen(name_list[ent->sam_entry_index].name) - 1]
484 ent->sam_entry_index++;
488 /* Lookup user info */
490 result = winbindd_fill_pwent(
492 name_list[ent->sam_entry_index].name,
493 &name_list[ent->sam_entry_index].user_sid,
494 &name_list[ent->sam_entry_index].group_sid,
495 name_list[ent->sam_entry_index].gecos,
496 &user_list[user_list_ndx]);
498 ent->sam_entry_index++;
500 /* Add user to return list */
505 state->response.data.num_entries++;
506 state->response.length +=
507 sizeof(struct winbindd_pw);
510 DEBUG(1, ("could not lookup domain user %s\n",
511 name_list[ent->sam_entry_index].name));
516 return (user_list_ndx > 0) ? WINBINDD_OK : WINBINDD_ERROR;
519 /* List domain users without mapping to unix ids */
521 enum winbindd_result winbindd_list_users(struct winbindd_cli_state *state)
523 struct winbindd_domain *domain;
524 WINBIND_USERINFO *info;
525 uint32 num_entries = 0, total_entries = 0;
526 char *ted, *extra_data = NULL;
527 int extra_data_len = 0;
529 enum winbindd_result rv = WINBINDD_ERROR;
531 DEBUG(3, ("[%5d]: list users\n", state->pid));
533 if (!(mem_ctx = talloc_init("winbindd_list_users")))
534 return WINBINDD_ERROR;
536 /* Enumerate over trusted domains */
538 for (domain = domain_list(); domain; domain = domain->next) {
540 struct winbindd_methods *methods;
543 methods = domain->methods;
545 /* Query display info */
546 status = methods->query_user_list(domain, mem_ctx,
547 &num_entries, &info);
549 if (num_entries == 0)
552 /* Allocate some memory for extra data */
553 total_entries += num_entries;
555 ted = Realloc(extra_data, sizeof(fstring) * total_entries);
558 DEBUG(0,("failed to enlarge buffer!\n"));
559 SAFE_FREE(extra_data);
564 /* Pack user list into extra data fields */
566 for (i = 0; i < num_entries; i++) {
567 fstring acct_name, name;
569 if (!info[i].acct_name) {
570 fstrcpy(acct_name, "");
572 fstrcpy(acct_name, info[i].acct_name);
575 fill_domain_username(name, domain->name, acct_name);
577 /* Append to extra data */
578 memcpy(&extra_data[extra_data_len], name,
580 extra_data_len += strlen(name);
581 extra_data[extra_data_len++] = ',';
585 /* Assign extra_data fields in response structure */
588 extra_data[extra_data_len - 1] = '\0';
589 state->response.extra_data = extra_data;
590 state->response.length += extra_data_len;
593 /* No domains responded but that's still OK so don't return an
600 talloc_destroy(mem_ctx);