A big tidyup while thinking about getting trusted domains being re-read
[ira/wip.git] / source3 / 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(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_named("winbindd_getpwnam(%s)", 
141                                           name_user))) {
142                 DEBUG(1, ("out of memory\n"));
143                 return WINBINDD_ERROR;
144         }
145
146         sid_split_rid(&user_sid, &user_rid);
147
148         status = domain->methods->query_user(domain, mem_ctx, user_rid, 
149                                              &user_info);
150
151         if (!NT_STATUS_IS_OK(status)) {
152                 DEBUG(1, ("error getting user info for user '%s'\n", 
153                           name_user));
154                 talloc_destroy(mem_ctx);
155                 return WINBINDD_ERROR;
156         }
157     
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, 
161                                  user_info.full_name,
162                                  &state->response.data.pw)) {
163                 talloc_destroy(mem_ctx);
164                 return WINBINDD_ERROR;
165         }
166
167         talloc_destroy(mem_ctx);
168         
169         return WINBINDD_OK;
170 }       
171
172 /* Return a password structure given a uid number */
173
174 enum winbindd_result winbindd_getpwuid(struct winbindd_cli_state *state)
175 {
176         DOM_SID user_sid;
177         struct winbindd_domain *domain;
178         uint32 user_rid;
179         fstring user_name;
180         enum SID_NAME_USE name_type;
181         WINBIND_USERINFO user_info;
182         gid_t gid;
183         TALLOC_CTX *mem_ctx;
184         NTSTATUS status;
185         
186         /* Bug out if the uid isn't in the winbind range */
187
188         if ((state->request.data.uid < server_state.uid_low ) ||
189             (state->request.data.uid > server_state.uid_high))
190                 return WINBINDD_ERROR;
191
192         DEBUG(3, ("[%5d]: getpwuid %d\n", state->pid, 
193                   state->request.data.uid));
194         
195         /* Get rid from uid */
196
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;
202         }
203         
204         /* Get name and name type from rid */
205
206         sid_copy(&user_sid, &domain->sid);
207         sid_append_rid(&user_sid, user_rid);
208         
209         if (!winbindd_lookup_name_by_sid(&user_sid, user_name, &name_type)) {
210                 fstring temp;
211                 
212                 sid_to_string(temp, &user_sid);
213                 DEBUG(1, ("could not lookup sid %s\n", temp));
214                 return WINBINDD_ERROR;
215         }
216         
217         if (strcmp("\\", lp_winbind_separator()))
218                 string_sub(user_name, "\\", lp_winbind_separator(), 
219                            sizeof(fstring));
220
221         /* Get some user info */
222         
223         if (!(mem_ctx = talloc_init_named("winbind_getpwuid(%d)",
224                                           state->request.data.uid))) {
225
226                 DEBUG(1, ("out of memory\n"));
227                 return WINBINDD_ERROR;
228         }
229
230         status = domain->methods->query_user(domain, mem_ctx, user_rid, 
231                                              &user_info);
232
233         if (!NT_STATUS_IS_OK(status)) {
234                 DEBUG(1, ("error getting user info for user '%s'\n", 
235                           user_name));
236                 return WINBINDD_ERROR;
237         }
238         
239         /* Resolve gid number */
240
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;
244         }
245
246         /* Fill in password structure */
247
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;
251         }
252         
253         talloc_destroy(mem_ctx);
254
255         return WINBINDD_OK;
256 }
257
258 /*
259  * set/get/endpwent functions
260  */
261
262 /* Rewind file pointer for ntdom passwd database */
263
264 enum winbindd_result winbindd_setpwent(struct winbindd_cli_state *state)
265 {
266         struct winbindd_domain *domain;
267         
268         DEBUG(3, ("[%5d]: setpwent\n", state->pid));
269         
270         /* Check user has enabled this */
271         
272         if (!lp_winbind_enum_users())
273                 return WINBINDD_ERROR;
274
275         /* Free old static data if it exists */
276         
277         if (state->getpwent_state != NULL) {
278                 free_getent_state(state->getpwent_state);
279                 state->getpwent_state = NULL;
280         }
281         
282         /* Create sam pipes for each domain we know about */
283         
284         if (domain_list == NULL)
285                 get_domain_info();
286
287         for(domain = domain_list; domain != NULL; domain = domain->next) {
288                 struct getent_state *domain_state;
289                 
290                 /*
291                  * Skip domains other than WINBINDD_DOMAIN environment
292                  * variable.
293                  */
294                 
295                 if ((strcmp(state->request.domain, "") != 0) &&
296                                 !check_domain_env(state->request.domain, 
297                                                   domain->name))
298                         continue;
299
300                 /* Create a state record for this domain */
301                 
302                 if ((domain_state = (struct getent_state *)
303                      malloc(sizeof(struct getent_state))) == NULL)
304                         return WINBINDD_ERROR;
305                 
306                 ZERO_STRUCTP(domain_state);
307
308                 domain_state->domain = domain;
309
310                 /* Add to list of open domains */
311                 
312                 DLIST_ADD(state->getpwent_state, domain_state);
313         }
314         
315         return WINBINDD_OK;
316 }
317
318 /* Close file pointer to ntdom passwd database */
319
320 enum winbindd_result winbindd_endpwent(struct winbindd_cli_state *state)
321 {
322         DEBUG(3, ("[%5d]: endpwent\n", state->pid));
323
324         free_getent_state(state->getpwent_state);    
325         state->getpwent_state = NULL;
326         
327         return WINBINDD_OK;
328 }
329
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. */
334
335 #define MAX_FETCH_SAM_ENTRIES 100
336
337 static BOOL get_sam_user_entries(struct getent_state *ent)
338 {
339         NTSTATUS status;
340         uint32 num_entries;
341         WINBIND_USERINFO *info;
342         struct getpwent_user *name_list = NULL;
343         BOOL result = False;
344         TALLOC_CTX *mem_ctx;
345         struct winbindd_methods *methods;
346         int i;
347
348         if (ent->num_sam_entries)
349                 return False;
350
351         if (!(mem_ctx = talloc_init_named("get_sam_user_entries(%s)",
352                                           ent->domain->name)))
353                 return False;
354
355         methods = ent->domain->methods;
356
357         /* Free any existing user info */
358
359         SAFE_FREE(ent->sam_entries);
360         ent->num_sam_entries = 0;
361         
362         /* Call query_user_list to get a list of usernames and user rids */
363         num_entries = 0;
364
365         status = methods->query_user_list(ent->domain, mem_ctx,
366                                           &num_entries, &info);
367                 
368         if (num_entries) {
369                 struct getpwent_user *tnl;
370                 
371                 tnl = (struct getpwent_user *)Realloc(name_list, 
372                                                       sizeof(struct getpwent_user) *
373                                                       (ent->num_sam_entries + 
374                                                        num_entries));
375                 
376                 if (!tnl) {
377                         DEBUG(0,("get_sam_user_entries realloc failed.\n"));
378                         SAFE_FREE(name_list);
379                         goto done;
380                 } else
381                         name_list = tnl;
382         }
383
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, "");
388                 } else {
389                         fstrcpy(name_list[ent->num_sam_entries + i].name, 
390                                 info[i].acct_name); 
391                 }
392                 if (!info[i].full_name) {
393                         fstrcpy(name_list[ent->num_sam_entries + i].gecos, "");
394                 } else {
395                         fstrcpy(name_list[ent->num_sam_entries + i].gecos, 
396                                 info[i].full_name); 
397                 }
398                 
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;
402         }
403                 
404         ent->num_sam_entries += num_entries;
405         
406         /* Fill in remaining fields */
407         
408         ent->sam_entries = name_list;
409         ent->sam_entry_index = 0;
410         result = ent->num_sam_entries > 0;
411
412  done:
413
414         talloc_destroy(mem_ctx);
415
416         return result;
417 }
418
419 /* Fetch next passwd entry from ntdom database */
420
421 #define MAX_GETPWENT_USERS 500
422
423 enum winbindd_result winbindd_getpwent(struct winbindd_cli_state *state)
424 {
425         struct getent_state *ent;
426         struct winbindd_pw *user_list;
427         int num_users, user_list_ndx = 0, i;
428         char *sep;
429
430         DEBUG(3, ("[%5d]: getpwent\n", state->pid));
431
432         /* Check user has enabled this */
433
434         if (!lp_winbind_enum_users())
435                 return WINBINDD_ERROR;
436
437         /* Allocate space for returning a chunk of users */
438
439         num_users = MIN(MAX_GETPWENT_USERS, state->request.data.num_entries);
440         
441         if ((state->response.extra_data = 
442              malloc(num_users * sizeof(struct winbindd_pw))) == NULL)
443                 return WINBINDD_ERROR;
444
445         memset(state->response.extra_data, 0, num_users * 
446                sizeof(struct winbindd_pw));
447
448         user_list = (struct winbindd_pw *)state->response.extra_data;
449         sep = lp_winbind_separator();
450         
451         if (!(ent = state->getpwent_state))
452                 return WINBINDD_ERROR;
453
454         /* Start sending back users */
455
456         for (i = 0; i < num_users; i++) {
457                 struct getpwent_user *name_list = NULL;
458                 fstring domain_user_name;
459                 uint32 result;
460
461                 /* Do we need to fetch another chunk of users? */
462
463                 if (ent->num_sam_entries == ent->sam_entry_index) {
464
465                         while(ent && !get_sam_user_entries(ent)) {
466                                 struct getent_state *next_ent;
467
468                                 /* Free state information for this domain */
469
470                                 SAFE_FREE(ent->sam_entries);
471
472                                 next_ent = ent->next;
473                                 DLIST_REMOVE(state->getpwent_state, ent);
474
475                                 SAFE_FREE(ent);
476                                 ent = next_ent;
477                         }
478  
479                         /* No more domains */
480
481                         if (!ent) 
482                                 break;
483                 }
484
485                 name_list = ent->sam_entries;
486
487                 /* Skip machine accounts */
488
489                 if (name_list[ent->sam_entry_index].
490                     name[strlen(name_list[ent->sam_entry_index].name) - 1] 
491                     == '$') {
492                         ent->sam_entry_index++;
493                         continue;
494                 }
495
496                 /* Lookup user info */
497                 
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);
501                 
502                 result = winbindd_fill_pwent(
503                         ent->domain->name, 
504                         domain_user_name,
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]);
509                 
510                 ent->sam_entry_index++;
511                 
512                 /* Add user to return list */
513                 
514                 if (result) {
515                                 
516                         user_list_ndx++;
517                         state->response.data.num_entries++;
518                         state->response.length += 
519                                 sizeof(struct winbindd_pw);
520
521                 } else
522                         DEBUG(1, ("could not lookup domain user %s\n",
523                                   domain_user_name));
524         }
525
526         /* Out of domains */
527
528         return (user_list_ndx > 0) ? WINBINDD_OK : WINBINDD_ERROR;
529 }
530
531 /* List domain users without mapping to unix ids */
532
533 enum winbindd_result winbindd_list_users(struct winbindd_cli_state *state)
534 {
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;
540         TALLOC_CTX *mem_ctx;
541         enum winbindd_result rv = WINBINDD_ERROR;
542
543         DEBUG(3, ("[%5d]: list users\n", state->pid));
544
545         if (!(mem_ctx = talloc_init_named("winbindd_list_users")))
546                 return WINBINDD_ERROR;
547
548         /* Enumerate over trusted domains */
549
550         if (domain_list == NULL)
551                 get_domain_info();
552
553         for (domain = domain_list; domain; domain = domain->next) {
554                 NTSTATUS status;
555                 struct winbindd_methods *methods;
556                 int i;
557
558                 /* Skip domains other than WINBINDD_DOMAIN environment
559                    variable */ 
560
561                 if ((strcmp(state->request.domain, "") != 0) &&
562                     !check_domain_env(state->request.domain, domain->name))
563                         continue;
564
565                 methods = domain->methods;
566
567                 /* Query display info */
568                 status = methods->query_user_list(domain, mem_ctx, 
569                                                   &num_entries, &info);
570
571                 if (num_entries == 0)
572                         continue;
573
574                 /* Allocate some memory for extra data */
575                 total_entries += num_entries;
576                         
577                 ted = Realloc(extra_data, sizeof(fstring) * total_entries);
578                         
579                 if (!ted) {
580                         DEBUG(0,("failed to enlarge buffer!\n"));
581                         SAFE_FREE(extra_data);
582                         goto done;
583                 } else 
584                         extra_data = ted;
585                         
586                 /* Pack user list into extra data fields */
587                         
588                 for (i = 0; i < num_entries; i++) {
589                         fstring acct_name, name;
590                         
591                         if (!info[i].acct_name) {
592                                 fstrcpy(acct_name, "");
593                         } else {
594                                 fstrcpy(acct_name, info[i].acct_name);
595                         }
596                         
597                         slprintf(name, sizeof(name) - 1, "%s%s%s",
598                                  domain->name, lp_winbind_separator(),
599                                  acct_name);
600                         
601                                 /* Append to extra data */
602                         memcpy(&extra_data[extra_data_len], name, 
603                                strlen(name));
604                         extra_data_len += strlen(name);
605                         extra_data[extra_data_len++] = ',';
606                 }   
607         }
608
609         /* Assign extra_data fields in response structure */
610
611         if (extra_data) {
612                 extra_data[extra_data_len - 1] = '\0';
613                 state->response.extra_data = extra_data;
614                 state->response.length += extra_data_len;
615         }
616
617         /* No domains responded but that's still OK so don't return an
618            error. */
619
620         rv = WINBINDD_OK;
621
622  done:
623
624         talloc_destroy(mem_ctx);
625
626         return rv;
627 }