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 sub_set_smb_name(user_name);
77 fstrcpy(current_user_info.domain, dom_name);
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_winbind 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;
103 enum SID_NAME_USE name_type;
104 struct winbindd_domain *domain;
107 /* Ensure null termination */
108 state->request.data.username[sizeof(state->request.data.username)-1]='\0';
110 DEBUG(3, ("[%5d]: getpwnam %s\n", state->pid,
111 state->request.data.username));
113 /* Parse domain and username */
115 if (!parse_domain_user(state->request.data.username, name_domain,
117 return WINBINDD_ERROR;
119 if ((domain = find_domain_from_name(name_domain)) == NULL) {
120 DEBUG(5, ("no such domain: %s\n", name_domain));
121 return WINBINDD_ERROR;
124 /* Get rid and name type from name */
126 if (!winbindd_lookup_sid_by_name(domain, name_user, &user_sid, &name_type)) {
127 DEBUG(1, ("user '%s' does not exist\n", name_user));
128 return WINBINDD_ERROR;
131 if (name_type != SID_NAME_USER) {
132 DEBUG(1, ("name '%s' is not a user name: %d\n", name_user,
134 return WINBINDD_ERROR;
137 /* Get some user info. Split the user rid from the sid obtained
138 from the winbind_lookup_by_name() call and use it in a
139 winbind_lookup_userinfo() */
141 if (!(mem_ctx = talloc_init("winbindd_getpwnam([%s]\\[%s])",
142 name_domain, name_user))) {
143 DEBUG(1, ("out of memory\n"));
144 return WINBINDD_ERROR;
147 sid_split_rid(&user_sid, &user_rid);
149 status = domain->methods->query_user(domain, mem_ctx, user_rid,
152 if (!NT_STATUS_IS_OK(status)) {
153 DEBUG(1, ("error getting user info for user '[%s]\\[%s]'\n",
154 name_domain, name_user));
155 talloc_destroy(mem_ctx);
156 return WINBINDD_ERROR;
159 /* Now take all this information and fill in a passwd structure */
160 if (!winbindd_fill_pwent(name_domain, name_user,
161 user_rid, user_info.group_rid,
163 &state->response.data.pw)) {
164 talloc_destroy(mem_ctx);
165 return WINBINDD_ERROR;
168 talloc_destroy(mem_ctx);
173 /* Return a password structure given a uid number */
175 enum winbindd_result winbindd_getpwuid(struct winbindd_cli_state *state)
178 struct winbindd_domain *domain;
182 enum SID_NAME_USE name_type;
183 WINBIND_USERINFO user_info;
188 /* Bug out if the uid isn't in the winbind range */
190 if ((state->request.data.uid < server_state.uid_low ) ||
191 (state->request.data.uid > server_state.uid_high))
192 return WINBINDD_ERROR;
194 DEBUG(3, ("[%5d]: getpwuid %d\n", state->pid,
195 state->request.data.uid));
197 /* Get rid from uid */
199 if (!winbindd_idmap_get_rid_from_uid(state->request.data.uid,
200 &user_rid, &domain)) {
201 DEBUG(1, ("could not convert uid %d to rid\n",
202 state->request.data.uid));
203 return WINBINDD_ERROR;
206 /* Get name and name type from rid */
208 sid_copy(&user_sid, &domain->sid);
209 sid_append_rid(&user_sid, user_rid);
211 if (!winbindd_lookup_name_by_sid(&user_sid, dom_name, user_name, &name_type)) {
214 sid_to_string(temp, &user_sid);
215 DEBUG(1, ("could not lookup sid %s\n", temp));
216 return WINBINDD_ERROR;
219 /* Get some user info */
221 if (!(mem_ctx = talloc_init("winbind_getpwuid(%d)",
222 state->request.data.uid))) {
224 DEBUG(1, ("out of memory\n"));
225 return WINBINDD_ERROR;
228 status = domain->methods->query_user(domain, mem_ctx, user_rid,
231 if (!NT_STATUS_IS_OK(status)) {
232 DEBUG(1, ("error getting user info for user '%s'\n",
234 talloc_destroy(mem_ctx);
235 return WINBINDD_ERROR;
238 /* Resolve gid number */
240 if (!winbindd_idmap_get_gid_from_rid(domain->name, user_info.group_rid, &gid)) {
241 DEBUG(1, ("error getting group id for user %s\n", user_name));
242 talloc_destroy(mem_ctx);
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 talloc_destroy(mem_ctx);
251 return WINBINDD_ERROR;
254 talloc_destroy(mem_ctx);
260 * set/get/endpwent functions
263 /* Rewind file pointer for ntdom passwd database */
265 enum winbindd_result winbindd_setpwent(struct winbindd_cli_state *state)
267 struct winbindd_domain *domain;
269 DEBUG(3, ("[%5d]: setpwent\n", state->pid));
271 /* Check user has enabled this */
273 if (!lp_winbind_enum_users())
274 return WINBINDD_ERROR;
276 /* Free old static data if it exists */
278 if (state->getpwent_state != NULL) {
279 free_getent_state(state->getpwent_state);
280 state->getpwent_state = NULL;
283 /* Create sam pipes for each domain we know about */
285 for(domain = domain_list(); domain != NULL; domain = domain->next) {
286 struct getent_state *domain_state;
288 /* Create a state record for this domain */
290 if ((domain_state = (struct getent_state *)
291 malloc(sizeof(struct getent_state))) == NULL)
292 return WINBINDD_ERROR;
294 ZERO_STRUCTP(domain_state);
296 fstrcpy(domain_state->domain_name, domain->name);
298 /* Add to list of open domains */
300 DLIST_ADD(state->getpwent_state, domain_state);
306 /* Close file pointer to ntdom passwd database */
308 enum winbindd_result winbindd_endpwent(struct winbindd_cli_state *state)
310 DEBUG(3, ("[%5d]: endpwent\n", state->pid));
312 free_getent_state(state->getpwent_state);
313 state->getpwent_state = NULL;
318 /* Get partial list of domain users for a domain. We fill in the sam_entries,
319 and num_sam_entries fields with domain user information. The dispinfo_ndx
320 field is incremented to the index of the next user to fetch. Return True if
321 some users were returned, False otherwise. */
323 #define MAX_FETCH_SAM_ENTRIES 100
325 static BOOL get_sam_user_entries(struct getent_state *ent)
329 WINBIND_USERINFO *info;
330 struct getpwent_user *name_list = NULL;
333 struct winbindd_domain *domain;
334 struct winbindd_methods *methods;
337 if (ent->num_sam_entries)
340 if (!(mem_ctx = talloc_init("get_sam_user_entries(%s)",
344 if (!(domain = find_domain_from_name(ent->domain_name))) {
345 DEBUG(3, ("no such domain %s in get_sam_user_entries\n",
350 methods = domain->methods;
352 /* Free any existing user info */
354 SAFE_FREE(ent->sam_entries);
355 ent->num_sam_entries = 0;
357 /* Call query_user_list to get a list of usernames and user rids */
361 status = methods->query_user_list(domain, mem_ctx, &num_entries,
365 struct getpwent_user *tnl;
367 tnl = (struct getpwent_user *)Realloc(name_list,
368 sizeof(struct getpwent_user) *
369 (ent->num_sam_entries +
373 DEBUG(0,("get_sam_user_entries realloc failed.\n"));
374 SAFE_FREE(name_list);
380 for (i = 0; i < num_entries; i++) {
381 /* Store account name and gecos */
382 if (!info[i].acct_name) {
383 fstrcpy(name_list[ent->num_sam_entries + i].name, "");
385 fstrcpy(name_list[ent->num_sam_entries + i].name,
388 if (!info[i].full_name) {
389 fstrcpy(name_list[ent->num_sam_entries + i].gecos, "");
391 fstrcpy(name_list[ent->num_sam_entries + i].gecos,
395 /* User and group ids */
396 name_list[ent->num_sam_entries+i].user_rid = info[i].user_rid;
397 name_list[ent->num_sam_entries+i].group_rid = info[i].group_rid;
400 ent->num_sam_entries += num_entries;
402 /* Fill in remaining fields */
404 ent->sam_entries = name_list;
405 ent->sam_entry_index = 0;
406 result = ent->num_sam_entries > 0;
410 talloc_destroy(mem_ctx);
415 /* Fetch next passwd entry from ntdom database */
417 #define MAX_GETPWENT_USERS 500
419 enum winbindd_result winbindd_getpwent(struct winbindd_cli_state *state)
421 struct getent_state *ent;
422 struct winbindd_pw *user_list;
423 int num_users, user_list_ndx = 0, i;
425 DEBUG(3, ("[%5d]: getpwent\n", state->pid));
427 /* Check user has enabled this */
429 if (!lp_winbind_enum_users())
430 return WINBINDD_ERROR;
432 /* Allocate space for returning a chunk of users */
434 num_users = MIN(MAX_GETPWENT_USERS, state->request.data.num_entries);
436 if ((state->response.extra_data =
437 malloc(num_users * sizeof(struct winbindd_pw))) == NULL)
438 return WINBINDD_ERROR;
440 memset(state->response.extra_data, 0, num_users *
441 sizeof(struct winbindd_pw));
443 user_list = (struct winbindd_pw *)state->response.extra_data;
445 if (!(ent = state->getpwent_state))
446 return WINBINDD_ERROR;
448 /* Start sending back users */
450 for (i = 0; i < num_users; i++) {
451 struct getpwent_user *name_list = NULL;
452 fstring domain_user_name;
455 /* Do we need to fetch another chunk of users? */
457 if (ent->num_sam_entries == ent->sam_entry_index) {
459 while(ent && !get_sam_user_entries(ent)) {
460 struct getent_state *next_ent;
462 /* Free state information for this domain */
464 SAFE_FREE(ent->sam_entries);
466 next_ent = ent->next;
467 DLIST_REMOVE(state->getpwent_state, ent);
473 /* No more domains */
479 name_list = ent->sam_entries;
481 /* Skip machine accounts */
483 if (name_list[ent->sam_entry_index].
484 name[strlen(name_list[ent->sam_entry_index].name) - 1]
486 ent->sam_entry_index++;
490 /* Lookup user info */
492 result = winbindd_fill_pwent(
494 name_list[ent->sam_entry_index].name,
495 name_list[ent->sam_entry_index].user_rid,
496 name_list[ent->sam_entry_index].group_rid,
497 name_list[ent->sam_entry_index].gecos,
498 &user_list[user_list_ndx]);
500 ent->sam_entry_index++;
502 /* Add user to return list */
507 state->response.data.num_entries++;
508 state->response.length +=
509 sizeof(struct winbindd_pw);
512 DEBUG(1, ("could not lookup domain user %s\n",
518 return (user_list_ndx > 0) ? WINBINDD_OK : WINBINDD_ERROR;
521 /* List domain users without mapping to unix ids */
523 enum winbindd_result winbindd_list_users(struct winbindd_cli_state *state)
525 struct winbindd_domain *domain;
526 WINBIND_USERINFO *info;
527 uint32 num_entries = 0, total_entries = 0;
528 char *ted, *extra_data = NULL;
529 int extra_data_len = 0;
531 enum winbindd_result rv = WINBINDD_ERROR;
533 DEBUG(3, ("[%5d]: list users\n", state->pid));
535 if (!(mem_ctx = talloc_init("winbindd_list_users")))
536 return WINBINDD_ERROR;
538 /* Enumerate over trusted domains */
540 for (domain = domain_list(); domain; domain = domain->next) {
542 struct winbindd_methods *methods;
545 methods = domain->methods;
547 /* Query display info */
548 status = methods->query_user_list(domain, mem_ctx,
549 &num_entries, &info);
551 if (num_entries == 0)
554 /* Allocate some memory for extra data */
555 total_entries += num_entries;
557 ted = Realloc(extra_data, sizeof(fstring) * total_entries);
560 DEBUG(0,("failed to enlarge buffer!\n"));
561 SAFE_FREE(extra_data);
566 /* Pack user list into extra data fields */
568 for (i = 0; i < num_entries; i++) {
569 fstring acct_name, name;
571 if (!info[i].acct_name) {
572 fstrcpy(acct_name, "");
574 fstrcpy(acct_name, info[i].acct_name);
577 fill_domain_username(name, domain->name, acct_name);
579 /* Append to extra data */
580 memcpy(&extra_data[extra_data_len], name,
582 extra_data_len += strlen(name);
583 extra_data[extra_data_len++] = ',';
587 /* Assign extra_data fields in response structure */
590 extra_data[extra_data_len - 1] = '\0';
591 state->response.extra_data = extra_data;
592 state->response.length += extra_data_len;
595 /* No domains responded but that's still OK so don't return an
602 talloc_destroy(mem_ctx);