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