got rid of start_ndx from query_user_list()
[tprouty/samba.git] / source / nsswitch / winbindd_user.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 2.2
4
5    Winbind daemon - user related functions
6
7    Copyright (C) Tim Potter 2000
8    Copyright (C) Jeremy Allison 2001.
9    
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.
14    
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.
19    
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.
23 */
24
25 #include "winbindd.h"
26
27 /* Fill a pwent structure with information we have obtained */
28
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)
32 {
33         extern userdom_struct current_user_info;
34         fstring name_domain, name_user;
35         pstring homedir;
36         
37         if (!pw || !name)
38                 return False;
39         
40         /* Resolve the uid number */
41         
42         if (!winbindd_idmap_get_uid_from_rid(domain_name, user_rid, 
43                                              &pw->pw_uid)) {
44                 DEBUG(1, ("error getting user id for rid %d\n", user_rid));
45                 return False;
46         }
47         
48         /* Resolve the gid number */   
49         
50         if (!winbindd_idmap_get_gid_from_rid(domain_name, group_rid, 
51                                              &pw->pw_gid)) {
52                 DEBUG(1, ("error getting group id for rid %d\n", group_rid));
53                 return False;
54         }
55
56         /* Username */
57         
58         safe_strcpy(pw->pw_name, name, sizeof(pw->pw_name) - 1);
59         
60         /* Full name (gecos) */
61         
62         safe_strcpy(pw->pw_gecos, full_name, sizeof(pw->pw_gecos) - 1);
63
64         /* Home directory and shell - use template config parameters.  The
65            defaults are /tmp for the home directory and /bin/false for
66            shell. */
67         
68         if (!parse_domain_user(name, name_domain, name_user)) {
69                 DEBUG(1, ("error parsing domain user for %s\n", name_user ));
70                 return False;
71         }
72         
73         /* The substitution of %U and %D in the 'template homedir' is done
74            by lp_string() calling standard_sub_basic(). */
75
76         fstrcpy(current_user_info.smb_name, name_user);
77         fstrcpy(current_user_info.domain, name_domain);
78
79         pstrcpy(homedir, lp_template_homedir());
80         
81         safe_strcpy(pw->pw_dir, homedir, sizeof(pw->pw_dir) - 1);
82         
83         safe_strcpy(pw->pw_shell, lp_template_shell(), 
84                     sizeof(pw->pw_shell) - 1);
85         
86         /* Password - set to "x" as we can't generate anything useful here.
87            Authentication can be done using the pam_ntdom module. */
88
89         safe_strcpy(pw->pw_passwd, "x", sizeof(pw->pw_passwd) - 1);
90         
91         return True;
92 }
93
94 /* Return a password structure from a username.  */
95
96 enum winbindd_result winbindd_getpwnam_from_user(struct winbindd_cli_state *state) 
97 {
98         uint32 user_rid;
99         WINBIND_USERINFO user_info;
100         DOM_SID user_sid;
101         NTSTATUS status;
102         fstring name_domain, name_user, name;
103         enum SID_NAME_USE name_type;
104         struct winbindd_domain *domain;
105         TALLOC_CTX *mem_ctx;
106         
107         DEBUG(3, ("[%5d]: getpwnam %s\n", state->pid,
108                   state->request.data.username));
109         
110         /* Parse domain and username */
111
112         if (!parse_domain_user(state->request.data.username, name_domain, 
113                           name_user))
114                 return WINBINDD_ERROR;
115         
116         if ((domain = find_domain_from_name(name_domain)) == NULL) {
117                 DEBUG(5, ("No such domain: %s\n", name_domain));
118                 return WINBINDD_ERROR;
119         }
120
121         slprintf(name, sizeof(name) - 1, "%s\\%s", name_domain, name_user);
122         
123         /* Get rid and name type from name */
124
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;
128         }
129
130         if (name_type != SID_NAME_USER) {
131                 DEBUG(1, ("name '%s' is not a user name: %d\n", name_user, 
132                           name_type));
133                 return WINBINDD_ERROR;
134         }
135         
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() */
139     
140         if (!(mem_ctx = talloc_init())) {
141                 DEBUG(1, ("out of memory\n"));
142                 return WINBINDD_ERROR;
143         }
144
145         sid_split_rid(&user_sid, &user_rid);
146
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;
153         }
154     
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;
161         }
162
163         talloc_destroy(mem_ctx);
164         
165         return WINBINDD_OK;
166 }       
167
168 /* Return a password structure given a uid number */
169
170 enum winbindd_result winbindd_getpwnam_from_uid(struct winbindd_cli_state *state)
171 {
172         DOM_SID user_sid;
173         struct winbindd_domain *domain;
174         uint32 user_rid;
175         fstring user_name;
176         enum SID_NAME_USE name_type;
177         WINBIND_USERINFO user_info;
178         gid_t gid;
179         TALLOC_CTX *mem_ctx;
180         NTSTATUS status;
181         
182         /* Bug out if the uid isn't in the winbind range */
183
184         if ((state->request.data.uid < server_state.uid_low ) ||
185             (state->request.data.uid > server_state.uid_high))
186                 return WINBINDD_ERROR;
187
188         DEBUG(3, ("[%5d]: getpwuid %d\n", state->pid, 
189                   state->request.data.uid));
190         
191         /* Get rid from uid */
192
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;
198         }
199         
200         /* Get name and name type from rid */
201
202         sid_copy(&user_sid, &domain->sid);
203         sid_append_rid(&user_sid, user_rid);
204         
205         if (!winbindd_lookup_name_by_sid(&user_sid, user_name, &name_type)) {
206                 fstring temp;
207                 
208                 sid_to_string(temp, &user_sid);
209                 DEBUG(1, ("Could not lookup sid %s\n", temp));
210                 return WINBINDD_ERROR;
211         }
212         
213         if (strcmp("\\", lp_winbind_separator()))
214                 string_sub(user_name, "\\", lp_winbind_separator(), 
215                            sizeof(fstring));
216
217         /* Get some user info */
218         
219         if (!(mem_ctx = talloc_init())) {
220                 DEBUG(1, ("out of memory\n"));
221                 return WINBINDD_ERROR;
222         }
223
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;
229         }
230         
231         /* Resolve gid number */
232
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;
236         }
237
238         /* Fill in password structure */
239
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;
243         }
244         
245         talloc_destroy(mem_ctx);
246
247         return WINBINDD_OK;
248 }
249
250 /*
251  * set/get/endpwent functions
252  */
253
254 /* Rewind file pointer for ntdom passwd database */
255
256 enum winbindd_result winbindd_setpwent(struct winbindd_cli_state *state)
257 {
258         struct winbindd_domain *tmp;
259         
260         DEBUG(3, ("[%5d]: setpwent\n", state->pid));
261         
262         /* Check user has enabled this */
263         
264         if (!lp_winbind_enum_users())
265                 return WINBINDD_ERROR;
266
267         /* Free old static data if it exists */
268         
269         if (state->getpwent_state != NULL) {
270                 free_getent_state(state->getpwent_state);
271                 state->getpwent_state = NULL;
272         }
273         
274         /* Create sam pipes for each domain we know about */
275         
276         if (domain_list == NULL)
277                 get_domain_info();
278
279         for(tmp = domain_list; tmp != NULL; tmp = tmp->next) {
280                 struct getent_state *domain_state;
281                 
282                 /*
283                  * Skip domains other than WINBINDD_DOMAIN environment
284                  * variable.
285                  */
286                 
287                 if ((strcmp(state->request.domain, "") != 0) &&
288                                 !check_domain_env(state->request.domain, tmp->name))
289                         continue;
290
291                 /* Create a state record for this domain */
292                 
293                 if ((domain_state = (struct getent_state *)malloc(sizeof(struct getent_state))) == NULL)
294                         return WINBINDD_ERROR;
295                 
296                 ZERO_STRUCTP(domain_state);
297
298                 domain_state->domain = tmp;
299
300                 /* Add to list of open domains */
301                 
302                 DLIST_ADD(state->getpwent_state, domain_state);
303         }
304         
305         return WINBINDD_OK;
306 }
307
308 /* Close file pointer to ntdom passwd database */
309
310 enum winbindd_result winbindd_endpwent(struct winbindd_cli_state *state)
311 {
312         DEBUG(3, ("[%5d]: endpwent\n", state->pid));
313
314         free_getent_state(state->getpwent_state);    
315         state->getpwent_state = NULL;
316         
317         return WINBINDD_OK;
318 }
319
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. */
324
325 #define MAX_FETCH_SAM_ENTRIES 100
326
327 static BOOL get_sam_user_entries(struct getent_state *ent)
328 {
329         NTSTATUS status;
330         uint32 num_entries;
331         WINBIND_USERINFO *info;
332         struct getpwent_user *name_list = NULL;
333         BOOL result = False;
334         TALLOC_CTX *mem_ctx;
335         struct winbindd_methods *methods;
336         int i;
337
338         if (ent->num_sam_entries)
339                 return False;
340
341         if (!(mem_ctx = talloc_init()))
342                 return False;
343
344         methods = ent->domain->methods;
345
346         /* Free any existing user info */
347
348         SAFE_FREE(ent->sam_entries);
349         ent->num_sam_entries = 0;
350         
351         /* Call query_user_list to get a list of usernames and user rids */
352         num_entries = 0;
353
354         status = methods->query_user_list(ent->domain, mem_ctx,
355                                           &num_entries, &info);
356                 
357         if (num_entries) {
358                 struct getpwent_user *tnl;
359                 
360                 tnl = (struct getpwent_user *)Realloc(name_list, 
361                                                       sizeof(struct getpwent_user) *
362                                                       (ent->num_sam_entries + 
363                                                        num_entries));
364                 
365                 if (!tnl) {
366                         DEBUG(0,("get_sam_user_entries: Realloc failed.\n"));
367                         SAFE_FREE(name_list);
368                         goto done;
369                 } else
370                         name_list = tnl;
371         }
372
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, "");
377                 } else {
378                         fstrcpy(name_list[ent->num_sam_entries + i].name, 
379                                 info[i].acct_name); 
380                 }
381                 if (!info[i].full_name) {
382                         fstrcpy(name_list[ent->num_sam_entries + i].gecos, "");
383                 } else {
384                         fstrcpy(name_list[ent->num_sam_entries + i].gecos, 
385                                 info[i].full_name); 
386                 }
387                 
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;
391         }
392                 
393         ent->num_sam_entries += num_entries;
394         
395         /* Fill in remaining fields */
396         
397         ent->sam_entries = name_list;
398         ent->sam_entry_index = 0;
399         result = ent->num_sam_entries > 0;
400
401  done:
402
403         talloc_destroy(mem_ctx);
404
405         return result;
406 }
407
408 /* Fetch next passwd entry from ntdom database */
409
410 #define MAX_GETPWENT_USERS 500
411
412 enum winbindd_result winbindd_getpwent(struct winbindd_cli_state *state)
413 {
414         struct getent_state *ent;
415         struct winbindd_pw *user_list;
416         int num_users, user_list_ndx = 0, i;
417         char *sep;
418
419         DEBUG(3, ("[%5d]: getpwent\n", state->pid));
420
421         /* Check user has enabled this */
422
423         if (!lp_winbind_enum_users())
424                 return WINBINDD_ERROR;
425
426         /* Allocate space for returning a chunk of users */
427
428         num_users = MIN(MAX_GETPWENT_USERS, state->request.data.num_entries);
429         
430         if ((state->response.extra_data = 
431              malloc(num_users * sizeof(struct winbindd_pw))) == NULL)
432                 return WINBINDD_ERROR;
433
434         memset(state->response.extra_data, 0, num_users * 
435                sizeof(struct winbindd_pw));
436
437         user_list = (struct winbindd_pw *)state->response.extra_data;
438         sep = lp_winbind_separator();
439         
440         if (!(ent = state->getpwent_state))
441                 return WINBINDD_ERROR;
442
443         /* Start sending back users */
444
445         for (i = 0; i < num_users; i++) {
446                 struct getpwent_user *name_list = NULL;
447                 fstring domain_user_name;
448                 uint32 result;
449
450                 /* Do we need to fetch another chunk of users? */
451
452                 if (ent->num_sam_entries == ent->sam_entry_index) {
453
454                         while(ent && !get_sam_user_entries(ent)) {
455                                 struct getent_state *next_ent;
456
457                                 /* Free state information for this domain */
458
459                                 SAFE_FREE(ent->sam_entries);
460
461                                 next_ent = ent->next;
462                                 DLIST_REMOVE(state->getpwent_state, ent);
463
464                                 SAFE_FREE(ent);
465                                 ent = next_ent;
466                         }
467  
468                         /* No more domains */
469
470                         if (!ent) 
471                                 break;
472                 }
473
474                 name_list = ent->sam_entries;
475
476                 /* Skip machine accounts */
477
478                 if (name_list[ent->sam_entry_index].
479                     name[strlen(name_list[ent->sam_entry_index].name) - 1] 
480                     == '$') {
481                         ent->sam_entry_index++;
482                         continue;
483                 }
484
485                 /* Lookup user info */
486                 
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);
490                 
491                 result = winbindd_fill_pwent(
492                         ent->domain->name, 
493                         domain_user_name,
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]);
498                 
499                 ent->sam_entry_index++;
500                 
501                 /* Add user to return list */
502                 
503                 if (result) {
504                                 
505                         user_list_ndx++;
506                         state->response.data.num_entries++;
507                         state->response.length += 
508                                 sizeof(struct winbindd_pw);
509
510                 } else
511                         DEBUG(1, ("could not lookup domain user %s\n",
512                                   domain_user_name));
513         }
514
515         /* Out of domains */
516
517         return (user_list_ndx > 0) ? WINBINDD_OK : WINBINDD_ERROR;
518 }
519
520 /* List domain users without mapping to unix ids */
521
522 enum winbindd_result winbindd_list_users(struct winbindd_cli_state *state)
523 {
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;
529         TALLOC_CTX *mem_ctx;
530         enum winbindd_result rv = WINBINDD_ERROR;
531
532         DEBUG(3, ("[%5d]: list users\n", state->pid));
533
534         if (!(mem_ctx = talloc_init()))
535                 return WINBINDD_ERROR;
536
537         /* Enumerate over trusted domains */
538
539         if (domain_list == NULL)
540                 get_domain_info();
541
542         for (domain = domain_list; domain; domain = domain->next) {
543                 NTSTATUS status;
544                 struct winbindd_methods *methods;
545                 int i;
546
547                 /* Skip domains other than WINBINDD_DOMAIN environment
548                    variable */ 
549
550                 if ((strcmp(state->request.domain, "") != 0) &&
551                     !check_domain_env(state->request.domain, domain->name))
552                         continue;
553
554                 methods = domain->methods;
555
556                 /* Query display info */
557                 status = methods->query_user_list(domain, mem_ctx, 
558                                                   &num_entries, &info);
559
560                 if (num_entries == 0)
561                         continue;
562
563                 /* Allocate some memory for extra data */
564                 total_entries += num_entries;
565                         
566                 ted = Realloc(extra_data, sizeof(fstring) * total_entries);
567                         
568                 if (!ted) {
569                         DEBUG(0,("winbindd_list_users: failed to enlarge buffer!\n"));
570                         SAFE_FREE(extra_data);
571                         goto done;
572                 } else 
573                         extra_data = ted;
574                         
575                 /* Pack user list into extra data fields */
576                         
577                 for (i = 0; i < num_entries; i++) {
578                         fstring acct_name, name;
579                         
580                         if (!info[i].acct_name) {
581                                 fstrcpy(acct_name, "");
582                         } else {
583                                 fstrcpy(acct_name, info[i].acct_name);
584                         }
585                         
586                         slprintf(name, sizeof(name) - 1, "%s%s%s",
587                                  domain->name, lp_winbind_separator(),
588                                  acct_name);
589                         
590                                 /* Append to extra data */
591                         memcpy(&extra_data[extra_data_len], name, 
592                                strlen(name));
593                         extra_data_len += strlen(name);
594                         extra_data[extra_data_len++] = ',';
595                 }   
596         }
597
598         /* Assign extra_data fields in response structure */
599
600         if (extra_data) {
601                 extra_data[extra_data_len - 1] = '\0';
602                 state->response.extra_data = extra_data;
603                 state->response.length += extra_data_len;
604         }
605
606         /* No domains responded but that's still OK so don't return an
607            error. */
608
609         rv = WINBINDD_OK;
610
611  done:
612
613         talloc_destroy(mem_ctx);
614
615         return rv;
616 }