sync 3.0 into HEAD for the last time
[samba.git] / source / nsswitch / winbindd_user.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    Winbind daemon - user related functions
5
6    Copyright (C) Tim Potter 2000
7    Copyright (C) Jeremy Allison 2001.
8    Copyright (C) Gerald (Jerry) Carter 2003.
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 #undef DBGC_CLASS
28 #define DBGC_CLASS DBGC_WINBIND
29
30 extern userdom_struct current_user_info;
31
32 /* Fill a pwent structure with information we have obtained */
33
34 static BOOL winbindd_fill_pwent(char *dom_name, char *user_name, 
35                                 DOM_SID *user_sid, DOM_SID *group_sid,
36                                 char *full_name, struct winbindd_pw *pw)
37 {
38         fstring output_username;
39         pstring homedir;
40         fstring sid_string;
41         
42         if (!pw || !dom_name || !user_name)
43                 return False;
44         
45         /* Resolve the uid number */
46
47         if (!NT_STATUS_IS_OK(idmap_sid_to_uid(user_sid, &(pw->pw_uid), 0))) {
48                 DEBUG(1, ("error getting user id for sid %s\n", sid_to_string(sid_string, user_sid)));
49                 return False;
50         }
51         
52         /* Resolve the gid number */   
53
54         if (!NT_STATUS_IS_OK(idmap_sid_to_gid(group_sid, &(pw->pw_gid), 0))) {
55                 DEBUG(1, ("error getting group id for sid %s\n", sid_to_string(sid_string, group_sid)));
56                 return False;
57         }
58
59         /* Username */
60
61         fill_domain_username(output_username, dom_name, user_name); 
62
63         safe_strcpy(pw->pw_name, output_username, sizeof(pw->pw_name) - 1);
64         
65         /* Full name (gecos) */
66         
67         safe_strcpy(pw->pw_gecos, full_name, sizeof(pw->pw_gecos) - 1);
68
69         /* Home directory and shell - use template config parameters.  The
70            defaults are /tmp for the home directory and /bin/false for
71            shell. */
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, user_name);
77         sub_set_smb_name(user_name);
78         fstrcpy(current_user_info.domain, dom_name);
79
80         pstrcpy(homedir, lp_template_homedir());
81         
82         safe_strcpy(pw->pw_dir, homedir, sizeof(pw->pw_dir) - 1);
83         
84         safe_strcpy(pw->pw_shell, lp_template_shell(), 
85                     sizeof(pw->pw_shell) - 1);
86         
87         /* Password - set to "x" as we can't generate anything useful here.
88            Authentication can be done using the pam_winbind module. */
89
90         safe_strcpy(pw->pw_passwd, "x", sizeof(pw->pw_passwd) - 1);
91         
92         return True;
93 }
94
95 /* Return a password structure from a username.  */
96
97 enum winbindd_result winbindd_getpwnam(struct winbindd_cli_state *state) 
98 {
99         WINBIND_USERINFO user_info;
100         WINBINDD_PW *pw;
101         DOM_SID user_sid;
102         NTSTATUS status;
103         fstring name_domain, name_user;
104         enum SID_NAME_USE name_type;
105         struct winbindd_domain *domain;
106         TALLOC_CTX *mem_ctx;
107         
108         /* Ensure null termination */
109         state->request.data.username[sizeof(state->request.data.username)-1]='\0';
110
111         DEBUG(3, ("[%5lu]: getpwnam %s\n", (unsigned long)state->pid,
112                   state->request.data.username));
113         
114         /* Parse domain and username */
115
116         parse_domain_user(state->request.data.username, 
117                 name_domain, name_user);
118         
119         /* if this is our local domain (or no domain), the do a local tdb search */
120         
121         if ( !*name_domain || strequal(name_domain, get_global_sam_name()) ) {
122                 if ( !(pw = wb_getpwnam(name_user)) ) {
123                         DEBUG(5,("winbindd_getpwnam: lookup for %s\\%s failed\n",
124                                 name_domain, name_user));
125                         return WINBINDD_ERROR;
126                 }
127                 memcpy( &state->response.data.pw, pw, sizeof(WINBINDD_PW) );
128                 return WINBINDD_OK;
129         }
130
131         /* should we deal with users for our domain? */
132         
133         if ( lp_winbind_trusted_domains_only() && strequal(name_domain, lp_workgroup())) {
134                 DEBUG(7,("winbindd_getpwnam: My domain -- rejecting getpwnam() for %s\\%s.\n", 
135                         name_domain, name_user));
136                 return WINBINDD_ERROR;
137         }       
138         
139         if ((domain = find_domain_from_name(name_domain)) == NULL) {
140                 DEBUG(5, ("no such domain: %s\n", name_domain));
141                 return WINBINDD_ERROR;
142         }
143         
144         /* Get rid and name type from name */
145
146         if (!winbindd_lookup_sid_by_name(domain, name_user, &user_sid, &name_type)) {
147                 DEBUG(1, ("user '%s' does not exist\n", name_user));
148                 return WINBINDD_ERROR;
149         }
150
151         if (name_type != SID_NAME_USER) {
152                 DEBUG(1, ("name '%s' is not a user name: %d\n", name_user, 
153                           name_type));
154                 return WINBINDD_ERROR;
155         }
156         
157         /* Get some user info.  Split the user rid from the sid obtained
158            from the winbind_lookup_by_name() call and use it in a
159            winbind_lookup_userinfo() */
160     
161         if (!(mem_ctx = talloc_init("winbindd_getpwnam([%s]\\[%s])", 
162                                           name_domain, name_user))) {
163                 DEBUG(1, ("out of memory\n"));
164                 return WINBINDD_ERROR;
165         }
166
167         status = domain->methods->query_user(domain, mem_ctx, &user_sid, 
168                                              &user_info);
169
170         if (!NT_STATUS_IS_OK(status)) {
171                 DEBUG(1, ("error getting user info for user '[%s]\\[%s]'\n", 
172                           name_domain, name_user));
173                 talloc_destroy(mem_ctx);
174                 return WINBINDD_ERROR;
175         }
176     
177         /* Now take all this information and fill in a passwd structure */      
178         if (!winbindd_fill_pwent(name_domain, name_user, 
179                                  user_info.user_sid, user_info.group_sid, 
180                                  user_info.full_name,
181                                  &state->response.data.pw)) {
182                 talloc_destroy(mem_ctx);
183                 return WINBINDD_ERROR;
184         }
185
186         talloc_destroy(mem_ctx);
187         
188         return WINBINDD_OK;
189 }       
190
191 /* Return a password structure given a uid number */
192
193 enum winbindd_result winbindd_getpwuid(struct winbindd_cli_state *state)
194 {
195         DOM_SID user_sid;
196         struct winbindd_domain *domain;
197         WINBINDD_PW *pw;
198         fstring dom_name;
199         fstring user_name;
200         enum SID_NAME_USE name_type;
201         WINBIND_USERINFO user_info;
202         TALLOC_CTX *mem_ctx;
203         NTSTATUS status;
204         gid_t gid;
205         
206         /* Bug out if the uid isn't in the winbind range */
207
208         if ((state->request.data.uid < server_state.uid_low ) ||
209             (state->request.data.uid > server_state.uid_high))
210                 return WINBINDD_ERROR;
211
212         DEBUG(3, ("[%5lu]: getpwuid %lu\n", (unsigned long)state->pid, 
213                   (unsigned long)state->request.data.uid));
214
215         /* always try local tdb first */
216         
217         if ( (pw = wb_getpwuid(state->request.data.uid)) != NULL ) {
218                 memcpy( &state->response.data.pw, pw, sizeof(WINBINDD_PW) );
219                 return WINBINDD_OK;
220         }
221         
222         /* Get rid from uid */
223
224         if (!NT_STATUS_IS_OK(idmap_uid_to_sid(&user_sid, state->request.data.uid))) {
225                 DEBUG(1, ("could not convert uid %lu to SID\n", 
226                           (unsigned long)state->request.data.uid));
227                 return WINBINDD_ERROR;
228         }
229         
230         /* Get name and name type from rid */
231
232         if (!winbindd_lookup_name_by_sid(&user_sid, dom_name, user_name, &name_type)) {
233                 fstring temp;
234                 
235                 sid_to_string(temp, &user_sid);
236                 DEBUG(1, ("could not lookup sid %s\n", temp));
237                 return WINBINDD_ERROR;
238         }
239         
240         domain = find_domain_from_sid(&user_sid);
241
242         if (!domain) {
243                 DEBUG(1,("Can't find domain from sid\n"));
244                 return WINBINDD_ERROR;
245         }
246
247         /* Get some user info */
248         
249         if (!(mem_ctx = talloc_init("winbind_getpwuid(%lu)",
250                                     (unsigned long)state->request.data.uid))) {
251
252                 DEBUG(1, ("out of memory\n"));
253                 return WINBINDD_ERROR;
254         }
255
256         status = domain->methods->query_user(domain, mem_ctx, &user_sid, 
257                                              &user_info);
258
259         if (!NT_STATUS_IS_OK(status)) {
260                 DEBUG(1, ("error getting user info for user '%s'\n", 
261                           user_name));
262                 talloc_destroy(mem_ctx);
263                 return WINBINDD_ERROR;
264         }
265         
266         /* Check group has a gid number */
267
268         if (!NT_STATUS_IS_OK(idmap_sid_to_gid(user_info.group_sid, &gid, 0))) {
269                 DEBUG(1, ("error getting group id for user %s\n", user_name));
270                 talloc_destroy(mem_ctx);
271                 return WINBINDD_ERROR;
272         }
273
274         /* Fill in password structure */
275
276         if (!winbindd_fill_pwent(domain->name, user_name, user_info.user_sid, 
277                                  user_info.group_sid,
278                                  user_info.full_name, &state->response.data.pw)) {
279                 talloc_destroy(mem_ctx);
280                 return WINBINDD_ERROR;
281         }
282         
283         talloc_destroy(mem_ctx);
284
285         return WINBINDD_OK;
286 }
287
288 /*
289  * set/get/endpwent functions
290  */
291
292 /* Rewind file pointer for ntdom passwd database */
293
294 enum winbindd_result winbindd_setpwent(struct winbindd_cli_state *state)
295 {
296         struct winbindd_domain *domain;
297         
298         DEBUG(3, ("[%5lu]: setpwent\n", (unsigned long)state->pid));
299         
300         /* Check user has enabled this */
301         
302         if (!lp_winbind_enum_users())
303                 return WINBINDD_ERROR;
304
305         /* Free old static data if it exists */
306         
307         if (state->getpwent_state != NULL) {
308                 free_getent_state(state->getpwent_state);
309                 state->getpwent_state = NULL;
310         }
311
312 #if 0   /* JERRY */
313         /* add any local users we have */
314                 
315         if ( (domain_state = (struct getent_state *)malloc(sizeof(struct getent_state))) == NULL )
316                 return WINBINDD_ERROR;
317                 
318         ZERO_STRUCTP(domain_state);
319
320         /* Add to list of open domains */
321                 
322         DLIST_ADD(state->getpwent_state, domain_state);
323 #endif
324         
325         /* Create sam pipes for each domain we know about */
326         
327         for(domain = domain_list(); domain != NULL; domain = domain->next) {
328                 struct getent_state *domain_state;
329                 
330                 
331                 /* don't add our domaina if we are a PDC or if we 
332                    are a member of a Samba domain */
333                 
334                 if ( (IS_DC || lp_winbind_trusted_domains_only())
335                         && strequal(domain->name, lp_workgroup()) )
336                 {
337                         continue;
338                 }
339                                                 
340                 /* Create a state record for this domain */
341                 
342                 if ((domain_state = (struct getent_state *)
343                      malloc(sizeof(struct getent_state))) == NULL)
344                         return WINBINDD_ERROR;
345                 
346                 ZERO_STRUCTP(domain_state);
347
348                 fstrcpy(domain_state->domain_name, domain->name);
349
350                 /* Add to list of open domains */
351                 
352                 DLIST_ADD(state->getpwent_state, domain_state);
353         }
354         
355         return WINBINDD_OK;
356 }
357
358 /* Close file pointer to ntdom passwd database */
359
360 enum winbindd_result winbindd_endpwent(struct winbindd_cli_state *state)
361 {
362         DEBUG(3, ("[%5lu]: endpwent\n", (unsigned long)state->pid));
363
364         free_getent_state(state->getpwent_state);    
365         state->getpwent_state = NULL;
366         
367         return WINBINDD_OK;
368 }
369
370 /* Get partial list of domain users for a domain.  We fill in the sam_entries,
371    and num_sam_entries fields with domain user information.  The dispinfo_ndx
372    field is incremented to the index of the next user to fetch.  Return True if
373    some users were returned, False otherwise. */
374
375 #define MAX_FETCH_SAM_ENTRIES 100
376
377 static BOOL get_sam_user_entries(struct getent_state *ent)
378 {
379         NTSTATUS status;
380         uint32 num_entries;
381         WINBIND_USERINFO *info;
382         struct getpwent_user *name_list = NULL;
383         BOOL result = False;
384         TALLOC_CTX *mem_ctx;
385         struct winbindd_domain *domain;
386         struct winbindd_methods *methods;
387         unsigned int i;
388
389         if (ent->num_sam_entries)
390                 return False;
391
392         if (!(mem_ctx = talloc_init("get_sam_user_entries(%s)",
393                                     ent->domain_name)))
394                 return False;
395
396         if (!(domain = find_domain_from_name(ent->domain_name))) {
397                 DEBUG(3, ("no such domain %s in get_sam_user_entries\n",
398                           ent->domain_name));
399                 return False;
400         }
401
402         methods = domain->methods;
403
404         /* Free any existing user info */
405
406         SAFE_FREE(ent->sam_entries);
407         ent->num_sam_entries = 0;
408         
409         /* Call query_user_list to get a list of usernames and user rids */
410
411         num_entries = 0;
412
413         status = methods->query_user_list(domain, mem_ctx, &num_entries, 
414                                           &info);
415                 
416         if (num_entries) {
417                 struct getpwent_user *tnl;
418                 
419                 tnl = (struct getpwent_user *)Realloc(name_list, 
420                                                       sizeof(struct getpwent_user) *
421                                                       (ent->num_sam_entries + 
422                                                        num_entries));
423                 
424                 if (!tnl) {
425                         DEBUG(0,("get_sam_user_entries realloc failed.\n"));
426                         SAFE_FREE(name_list);
427                         goto done;
428                 } else
429                         name_list = tnl;
430         }
431
432         for (i = 0; i < num_entries; i++) {
433                 /* Store account name and gecos */
434                 if (!info[i].acct_name) {
435                         fstrcpy(name_list[ent->num_sam_entries + i].name, "");
436                 } else {
437                         fstrcpy(name_list[ent->num_sam_entries + i].name, 
438                                 info[i].acct_name); 
439                 }
440                 if (!info[i].full_name) {
441                         fstrcpy(name_list[ent->num_sam_entries + i].gecos, "");
442                 } else {
443                         fstrcpy(name_list[ent->num_sam_entries + i].gecos, 
444                                 info[i].full_name); 
445                 }
446                 
447                 /* User and group ids */
448                 sid_copy(&name_list[ent->num_sam_entries+i].user_sid, info[i].user_sid);
449                 sid_copy(&name_list[ent->num_sam_entries+i].group_sid, info[i].group_sid);
450         }
451                 
452         ent->num_sam_entries += num_entries;
453         
454         /* Fill in remaining fields */
455         
456         ent->sam_entries = name_list;
457         ent->sam_entry_index = 0;
458         result = ent->num_sam_entries > 0;
459
460  done:
461
462         talloc_destroy(mem_ctx);
463
464         return result;
465 }
466
467 /* Fetch next passwd entry from ntdom database */
468
469 #define MAX_GETPWENT_USERS 500
470
471 enum winbindd_result winbindd_getpwent(struct winbindd_cli_state *state)
472 {
473         struct getent_state *ent;
474         struct winbindd_pw *user_list;
475         int num_users, user_list_ndx = 0, i;
476
477         DEBUG(3, ("[%5lu]: getpwent\n", (unsigned long)state->pid));
478
479         /* Check user has enabled this */
480
481         if (!lp_winbind_enum_users())
482                 return WINBINDD_ERROR;
483
484         /* Allocate space for returning a chunk of users */
485
486         num_users = MIN(MAX_GETPWENT_USERS, state->request.data.num_entries);
487         
488         if ((state->response.extra_data = 
489              malloc(num_users * sizeof(struct winbindd_pw))) == NULL)
490                 return WINBINDD_ERROR;
491
492         memset(state->response.extra_data, 0, num_users * 
493                sizeof(struct winbindd_pw));
494
495         user_list = (struct winbindd_pw *)state->response.extra_data;
496         
497         if (!(ent = state->getpwent_state))
498                 return WINBINDD_ERROR;
499
500         /* Start sending back users */
501
502         for (i = 0; i < num_users; i++) {
503                 struct getpwent_user *name_list = NULL;
504                 uint32 result;
505
506                 /* Do we need to fetch another chunk of users? */
507
508                 if (ent->num_sam_entries == ent->sam_entry_index) {
509
510                         while(ent && !get_sam_user_entries(ent)) {
511                                 struct getent_state *next_ent;
512
513                                 /* Free state information for this domain */
514
515                                 SAFE_FREE(ent->sam_entries);
516
517                                 next_ent = ent->next;
518                                 DLIST_REMOVE(state->getpwent_state, ent);
519
520                                 SAFE_FREE(ent);
521                                 ent = next_ent;
522                         }
523  
524                         /* No more domains */
525
526                         if (!ent) 
527                                 break;
528                 }
529
530                 name_list = ent->sam_entries;
531
532                 /* Skip machine accounts */
533
534                 if (name_list[ent->sam_entry_index].
535                     name[strlen(name_list[ent->sam_entry_index].name) - 1] 
536                     == '$') {
537                         ent->sam_entry_index++;
538                         continue;
539                 }
540
541                 /* Lookup user info */
542                 
543                 result = winbindd_fill_pwent(
544                         ent->domain_name, 
545                         name_list[ent->sam_entry_index].name,
546                         &name_list[ent->sam_entry_index].user_sid,
547                         &name_list[ent->sam_entry_index].group_sid,
548                         name_list[ent->sam_entry_index].gecos,
549                         &user_list[user_list_ndx]);
550                 
551                 ent->sam_entry_index++;
552                 
553                 /* Add user to return list */
554                 
555                 if (result) {
556                                 
557                         user_list_ndx++;
558                         state->response.data.num_entries++;
559                         state->response.length += 
560                                 sizeof(struct winbindd_pw);
561
562                 } else
563                         DEBUG(1, ("could not lookup domain user %s\n",
564                                   name_list[ent->sam_entry_index].name));
565         }
566
567         /* Out of domains */
568
569         return (user_list_ndx > 0) ? WINBINDD_OK : WINBINDD_ERROR;
570 }
571
572 /* List domain users without mapping to unix ids */
573
574 enum winbindd_result winbindd_list_users(struct winbindd_cli_state *state)
575 {
576         struct winbindd_domain *domain;
577         WINBIND_USERINFO *info;
578         const char *which_domain;
579         uint32 num_entries = 0, total_entries = 0;
580         char *ted, *extra_data = NULL;
581         int extra_data_len = 0;
582         TALLOC_CTX *mem_ctx;
583         enum winbindd_result rv = WINBINDD_ERROR;
584
585         DEBUG(3, ("[%5lu]: list users\n", (unsigned long)state->pid));
586
587         if (!(mem_ctx = talloc_init("winbindd_list_users")))
588                 return WINBINDD_ERROR;
589
590         /* Ensure null termination */
591         state->request.domain_name[sizeof(state->request.domain_name)-1]='\0';  
592         which_domain = state->request.domain_name;
593         
594         /* Enumerate over trusted domains */
595
596         for (domain = domain_list(); domain; domain = domain->next) {
597                 NTSTATUS status;
598                 struct winbindd_methods *methods;
599                 unsigned int i;
600                 
601                 /* if we have a domain name restricting the request and this
602                    one in the list doesn't match, then just bypass the remainder
603                    of the loop */
604                    
605                 if ( *which_domain && !strequal(which_domain, domain->name) )
606                         continue;
607                         
608                 methods = domain->methods;
609
610                 /* Query display info */
611                 status = methods->query_user_list(domain, mem_ctx, 
612                                                   &num_entries, &info);
613
614                 if (num_entries == 0)
615                         continue;
616
617                 /* Allocate some memory for extra data */
618                 total_entries += num_entries;
619                         
620                 ted = Realloc(extra_data, sizeof(fstring) * total_entries);
621                         
622                 if (!ted) {
623                         DEBUG(0,("failed to enlarge buffer!\n"));
624                         SAFE_FREE(extra_data);
625                         goto done;
626                 } else 
627                         extra_data = ted;
628                         
629                 /* Pack user list into extra data fields */
630                         
631                 for (i = 0; i < num_entries; i++) {
632                         fstring acct_name, name;
633                         
634                         if (!info[i].acct_name) {
635                                 fstrcpy(acct_name, "");
636                         } else {
637                                 fstrcpy(acct_name, info[i].acct_name);
638                         }
639                         
640                         fill_domain_username(name, domain->name, acct_name);
641                         
642                                 /* Append to extra data */
643                         memcpy(&extra_data[extra_data_len], name, 
644                                strlen(name));
645                         extra_data_len += strlen(name);
646                         extra_data[extra_data_len++] = ',';
647                 }   
648         }
649
650         /* Assign extra_data fields in response structure */
651
652         if (extra_data) {
653                 extra_data[extra_data_len - 1] = '\0';
654                 state->response.extra_data = extra_data;
655                 state->response.length += extra_data_len;
656         }
657
658         /* No domains responded but that's still OK so don't return an
659            error. */
660
661         rv = WINBINDD_OK;
662
663  done:
664
665         talloc_destroy(mem_ctx);
666
667         return rv;
668 }