added name_to_sid to the backend
[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         parse_domain_user(name, name_domain, name_user);
69         
70         /* The substitution of %U and %D in the 'template homedir' is done
71            by lp_string() calling standard_sub_basic(). */
72
73         fstrcpy(current_user_info.smb_name, name_user);
74         fstrcpy(current_user_info.domain, name_domain);
75
76         pstrcpy(homedir, lp_template_homedir());
77         
78         safe_strcpy(pw->pw_dir, homedir, sizeof(pw->pw_dir) - 1);
79         
80         safe_strcpy(pw->pw_shell, lp_template_shell(), 
81                     sizeof(pw->pw_shell) - 1);
82         
83         /* Password - set to "x" as we can't generate anything useful here.
84            Authentication can be done using the pam_ntdom module. */
85
86         safe_strcpy(pw->pw_passwd, "x", sizeof(pw->pw_passwd) - 1);
87         
88         return True;
89 }
90
91 /************************************************************************
92  Empty static struct for negative caching.
93 *************************************************************************/
94
95 static struct winbindd_pw negative_pw_cache_entry;
96
97 /* Return a password structure from a username.  Specify whether cached data 
98    can be returned. */
99
100 enum winbindd_result winbindd_getpwnam_from_user(struct winbindd_cli_state *state) 
101 {
102         uint32 user_rid, group_rid;
103         SAM_USERINFO_CTR *user_info;
104         DOM_SID user_sid;
105         fstring name_domain, name_user, name, gecos_name;
106         enum SID_NAME_USE name_type;
107         struct winbindd_domain *domain;
108         TALLOC_CTX *mem_ctx;
109         
110         DEBUG(3, ("[%5d]: getpwnam %s\n", state->pid,
111                   state->request.data.username));
112         
113         /* Parse domain and username */
114
115         parse_domain_user(state->request.data.username, name_domain, 
116                           name_user);
117
118         /* Reject names that don't have a domain - i.e name_domain contains 
119            the entire name. */
120  
121         if (strequal(name_domain, ""))
122                 return WINBINDD_ERROR;
123         
124         if ((domain = find_domain_from_name(name_domain)) == NULL) {
125                 DEBUG(5, ("No such domain: %s\n", name_domain));
126                 return WINBINDD_ERROR;
127         }
128
129         /* Check for cached user entry */
130
131         if (winbindd_fetch_user_cache_entry(domain, name_user, &state->response.data.pw)) {
132                 /* Check if this is a negative cache entry. */
133                 if (memcmp(&negative_pw_cache_entry, &state->response.data.pw,
134                                                 sizeof(state->response.data.pw)) == 0)
135                         return WINBINDD_ERROR;
136                 return WINBINDD_OK;
137         }
138
139         slprintf(name, sizeof(name) - 1, "%s\\%s", name_domain, name_user);
140         
141         /* Get rid and name type from name */
142
143         if (!winbindd_lookup_sid_by_name(domain, name, &user_sid, &name_type)) {
144                 DEBUG(1, ("user '%s' does not exist\n", name_user));
145                 winbindd_store_user_cache_entry(domain, name_user, &negative_pw_cache_entry);
146                 return WINBINDD_ERROR;
147         }
148
149         if (name_type != SID_NAME_USER) {
150                 DEBUG(1, ("name '%s' is not a user name: %d\n", name_user, 
151                           name_type));
152                 winbindd_store_user_cache_entry(domain, name_user, &negative_pw_cache_entry);
153                 return WINBINDD_ERROR;
154         }
155         
156         /* Get some user info.  Split the user rid from the sid obtained
157            from the winbind_lookup_by_name() call and use it in a
158            winbind_lookup_userinfo() */
159     
160         if (!(mem_ctx = talloc_init())) {
161                 DEBUG(1, ("out of memory\n"));
162                 return WINBINDD_ERROR;
163         }
164
165         sid_split_rid(&user_sid, &user_rid);
166         
167         if (!winbindd_lookup_userinfo(domain, mem_ctx, user_rid, &user_info)) {
168                 DEBUG(1, ("pwnam_from_user(): error getting user info for "
169                           "user '%s'\n", name_user));
170                 winbindd_store_user_cache_entry(domain, name_user, &negative_pw_cache_entry);
171                 return WINBINDD_ERROR;
172         }
173     
174         group_rid = user_info->info.id21->group_rid;
175
176         unistr2_to_ascii(gecos_name, &user_info->info.id21->uni_full_name,
177                          sizeof(gecos_name) - 1);
178
179         talloc_destroy(mem_ctx);
180         user_info = NULL;
181
182         /* Now take all this information and fill in a passwd structure */
183         
184         if (!winbindd_fill_pwent(name_domain, state->request.data.username, 
185                                  user_rid, group_rid, gecos_name,
186                                  &state->response.data.pw)) {
187                 winbindd_store_user_cache_entry(domain, name_user, &negative_pw_cache_entry);
188                 return WINBINDD_ERROR;
189         }
190         
191         winbindd_store_user_cache_entry(domain, name_user, &state->response.data.pw);
192         
193         return WINBINDD_OK;
194 }       
195
196 /* Return a password structure given a uid number */
197
198 enum winbindd_result winbindd_getpwnam_from_uid(struct winbindd_cli_state *state)
199 {
200         DOM_SID user_sid;
201         struct winbindd_domain *domain;
202         uint32 user_rid, group_rid;
203         fstring user_name, gecos_name;
204         enum SID_NAME_USE name_type;
205         SAM_USERINFO_CTR *user_info;
206         gid_t gid;
207         TALLOC_CTX *mem_ctx;
208         
209         /* Bug out if the uid isn't in the winbind range */
210
211         if ((state->request.data.uid < server_state.uid_low ) ||
212             (state->request.data.uid > server_state.uid_high))
213                 return WINBINDD_ERROR;
214
215         DEBUG(3, ("[%5d]: getpwuid %d\n", state->pid, 
216                   state->request.data.uid));
217         
218         /* Get rid from uid */
219
220         if (!winbindd_idmap_get_rid_from_uid(state->request.data.uid, 
221                                              &user_rid, &domain)) {
222                 DEBUG(1, ("Could not convert uid %d to rid\n", 
223                           state->request.data.uid));
224                 return WINBINDD_ERROR;
225         }
226         
227         /* Check for cached uid entry */
228
229         if (winbindd_fetch_uid_cache_entry(domain, 
230                                            state->request.data.uid,
231                                            &state->response.data.pw)) {
232                 /* Check if this is a negative cache entry. */
233                 if (memcmp(&negative_pw_cache_entry, &state->response.data.pw,
234                                                 sizeof(state->response.data.pw)) == 0)
235                         return WINBINDD_ERROR;
236                 return WINBINDD_OK;
237         }
238
239         /* Get name and name type from rid */
240
241         sid_copy(&user_sid, &domain->sid);
242         sid_append_rid(&user_sid, user_rid);
243         
244         if (!winbindd_lookup_name_by_sid(&user_sid, user_name, &name_type)) {
245                 fstring temp;
246                 
247                 sid_to_string(temp, &user_sid);
248                 DEBUG(1, ("Could not lookup sid %s\n", temp));
249
250                 winbindd_store_uid_cache_entry(domain, state->request.data.uid, &negative_pw_cache_entry);
251                 return WINBINDD_ERROR;
252         }
253         
254         if (strcmp("\\", lp_winbind_separator()))
255                 string_sub(user_name, "\\", lp_winbind_separator(), 
256                            sizeof(fstring));
257
258         /* Get some user info */
259         
260         if (!(mem_ctx = talloc_init())) {
261                 DEBUG(1, ("out of memory\n"));
262                 return WINBINDD_ERROR;
263         }
264
265         if (!winbindd_lookup_userinfo(domain, mem_ctx, user_rid, &user_info)) {
266                 DEBUG(1, ("pwnam_from_uid(): error getting user info for "
267                           "user '%s'\n", user_name));
268                 winbindd_store_uid_cache_entry(domain, state->request.data.uid, &negative_pw_cache_entry);
269                 return WINBINDD_ERROR;
270         }
271         
272         group_rid = user_info->info.id21->group_rid;
273         unistr2_to_ascii(gecos_name, &user_info->info.id21->uni_full_name,
274                          sizeof(gecos_name) - 1);
275
276         talloc_destroy(mem_ctx);
277         user_info = NULL;
278
279         /* Resolve gid number */
280
281         if (!winbindd_idmap_get_gid_from_rid(domain->name, group_rid, &gid)) {
282                 DEBUG(1, ("error getting group id for user %s\n", user_name));
283                 return WINBINDD_ERROR;
284         }
285
286         /* Fill in password structure */
287
288         if (!winbindd_fill_pwent(domain->name, user_name, user_rid, group_rid,
289                                  gecos_name, &state->response.data.pw)) {
290                 winbindd_store_uid_cache_entry(domain, state->request.data.uid, &negative_pw_cache_entry);
291                 return WINBINDD_ERROR;
292         }
293         
294         winbindd_store_uid_cache_entry(domain, state->request.data.uid, &state->response.data.pw);
295         
296         return WINBINDD_OK;
297 }
298
299 /*
300  * set/get/endpwent functions
301  */
302
303 /* Rewind file pointer for ntdom passwd database */
304
305 enum winbindd_result winbindd_setpwent(struct winbindd_cli_state *state)
306 {
307         struct winbindd_domain *tmp;
308         
309         DEBUG(3, ("[%5d]: setpwent\n", state->pid));
310         
311         /* Check user has enabled this */
312         
313         if (!lp_winbind_enum_users())
314                 return WINBINDD_ERROR;
315
316         /* Free old static data if it exists */
317         
318         if (state->getpwent_state != NULL) {
319                 free_getent_state(state->getpwent_state);
320                 state->getpwent_state = NULL;
321         }
322         
323         /* Create sam pipes for each domain we know about */
324         
325         if (domain_list == NULL)
326                 get_domain_info();
327
328         for(tmp = domain_list; tmp != NULL; tmp = tmp->next) {
329                 struct getent_state *domain_state;
330                 
331                 /*
332                  * Skip domains other than WINBINDD_DOMAIN environment
333                  * variable.
334                  */
335                 
336                 if ((strcmp(state->request.domain, "") != 0) &&
337                                 !check_domain_env(state->request.domain, tmp->name))
338                         continue;
339
340                 /* Create a state record for this domain */
341                 
342                 if ((domain_state = (struct getent_state *)malloc(sizeof(struct getent_state))) == NULL)
343                         return WINBINDD_ERROR;
344                 
345                 ZERO_STRUCTP(domain_state);
346
347                 domain_state->domain = tmp;
348
349                 /* Add to list of open domains */
350                 
351                 DLIST_ADD(state->getpwent_state, domain_state);
352         }
353         
354         return WINBINDD_OK;
355 }
356
357 /* Close file pointer to ntdom passwd database */
358
359 enum winbindd_result winbindd_endpwent(struct winbindd_cli_state *state)
360 {
361         DEBUG(3, ("[%5d]: endpwent\n", state->pid));
362
363         free_getent_state(state->getpwent_state);    
364         state->getpwent_state = NULL;
365         
366         return WINBINDD_OK;
367 }
368
369 /* Get partial list of domain users for a domain.  We fill in the sam_entries,
370    and num_sam_entries fields with domain user information.  The dispinfo_ndx
371    field is incremented to the index of the next user to fetch.  Return True if
372    some users were returned, False otherwise. */
373
374 #define MAX_FETCH_SAM_ENTRIES 100
375
376 static BOOL get_sam_user_entries(struct getent_state *ent)
377 {
378         NTSTATUS status;
379         uint32 num_entries;
380         WINBIND_DISPINFO *info;
381         struct getpwent_user *name_list = NULL;
382         BOOL result = False;
383         TALLOC_CTX *mem_ctx;
384         struct winbindd_methods *methods;
385
386         if (ent->got_all_sam_entries)
387                 return False;
388
389         if (!(mem_ctx = talloc_init()))
390                 return False;
391
392         methods = ent->domain->methods;
393
394 #if 0
395         /* Look in cache for entries, else get them direct */
396                     
397         if (winbindd_fetch_user_cache(ent->domain,
398                                       (struct getpwent_user **)
399                                       &ent->sam_entries, 
400                                       &ent->num_sam_entries)) {
401                 return True;
402         }
403 #endif
404
405         /* Free any existing user info */
406
407         SAFE_FREE(ent->sam_entries);
408         ent->num_sam_entries = 0;
409         
410         /* Call query_dispinfo to get a list of usernames and user rids */
411
412         do {
413                 int i;
414                                         
415                 num_entries = 0;
416
417                 status = methods->query_dispinfo(ent->domain, mem_ctx,
418                                                  &ent->dispinfo_ndx, 
419                                                  &num_entries, &info);
420                 
421                 if (num_entries) {
422                         struct getpwent_user *tnl;
423
424                         tnl = (struct getpwent_user *)Realloc(name_list, 
425                                             sizeof(struct getpwent_user) *
426                                             (ent->num_sam_entries + 
427                                              num_entries));
428
429                         if (!tnl) {
430                                 DEBUG(0,("get_sam_user_entries: Realloc failed.\n"));
431                                 SAFE_FREE(name_list);
432                                 goto done;
433                         } else
434                                 name_list = tnl;
435                 }
436
437                 for (i = 0; i < num_entries; i++) {
438                         /* Store account name and gecos */
439                         if (!info[i].acct_name) {
440                                 fstrcpy(name_list[ent->num_sam_entries + i].name, "");
441                         } else {
442                                 fstrcpy(name_list[ent->num_sam_entries + i].name, 
443                                         info[i].acct_name); 
444                         }
445                         if (!info[i].full_name) {
446                                 fstrcpy(name_list[ent->num_sam_entries + i].gecos, "");
447                         } else {
448                                 fstrcpy(name_list[ent->num_sam_entries + i].gecos, 
449                                         info[i].full_name); 
450                         }
451
452                         /* User and group ids */
453                         name_list[ent->num_sam_entries+i].user_rid = info[i].user_rid;
454                         name_list[ent->num_sam_entries+i].group_rid = info[i].group_rid;
455                 }
456                 
457                 ent->num_sam_entries += num_entries;
458
459                 if (NT_STATUS_V(status) != NT_STATUS_V(STATUS_MORE_ENTRIES))
460                         break;
461
462         } while (ent->num_sam_entries < MAX_FETCH_SAM_ENTRIES);
463         
464 #if 0
465         /* Fill cache with received entries */
466         
467         winbindd_store_user_cache(ent->domain, ent->sam_entries, 
468                                   ent->num_sam_entries);
469 #endif
470
471         /* Fill in remaining fields */
472         
473         ent->sam_entries = name_list;
474         ent->sam_entry_index = 0;
475         ent->got_all_sam_entries = (NT_STATUS_V(status) != NT_STATUS_V(STATUS_MORE_ENTRIES));
476
477         result = ent->num_sam_entries > 0;
478
479  done:
480
481         talloc_destroy(mem_ctx);
482
483         return result;
484 }
485
486 /* Fetch next passwd entry from ntdom database */
487
488 #define MAX_GETPWENT_USERS 500
489
490 enum winbindd_result winbindd_getpwent(struct winbindd_cli_state *state)
491 {
492         struct getent_state *ent;
493         struct winbindd_pw *user_list;
494         int num_users, user_list_ndx = 0, i;
495         char *sep;
496
497         DEBUG(3, ("[%5d]: getpwent\n", state->pid));
498
499         /* Check user has enabled this */
500
501         if (!lp_winbind_enum_users())
502                 return WINBINDD_ERROR;
503
504         /* Allocate space for returning a chunk of users */
505
506         num_users = MIN(MAX_GETPWENT_USERS, state->request.data.num_entries);
507         
508         if ((state->response.extra_data = 
509              malloc(num_users * sizeof(struct winbindd_pw))) == NULL)
510                 return WINBINDD_ERROR;
511
512         memset(state->response.extra_data, 0, num_users * 
513                sizeof(struct winbindd_pw));
514
515         user_list = (struct winbindd_pw *)state->response.extra_data;
516         sep = lp_winbind_separator();
517         
518         if (!(ent = state->getpwent_state))
519                 return WINBINDD_ERROR;
520
521         /* Start sending back users */
522
523         for (i = 0; i < num_users; i++) {
524                 struct getpwent_user *name_list = NULL;
525                 fstring domain_user_name;
526                 uint32 result;
527
528                 /* Do we need to fetch another chunk of users? */
529
530                 if (ent->num_sam_entries == ent->sam_entry_index) {
531
532                         while(ent && !get_sam_user_entries(ent)) {
533                                 struct getent_state *next_ent;
534
535                                 /* Free state information for this domain */
536
537                                 SAFE_FREE(ent->sam_entries);
538
539                                 next_ent = ent->next;
540                                 DLIST_REMOVE(state->getpwent_state, ent);
541
542                                 SAFE_FREE(ent);
543                                 ent = next_ent;
544                         }
545  
546                         /* No more domains */
547
548                         if (!ent) 
549                                 break;
550                 }
551
552                 name_list = ent->sam_entries;
553
554                 /* Skip machine accounts */
555
556                 if (name_list[ent->sam_entry_index].
557                     name[strlen(name_list[ent->sam_entry_index].name) - 1] 
558                     == '$') {
559                         ent->sam_entry_index++;
560                         continue;
561                 }
562
563                 /* Lookup user info */
564                 
565                 slprintf(domain_user_name, sizeof(domain_user_name) - 1,
566                          "%s%s%s", ent->domain->name, sep,
567                          name_list[ent->sam_entry_index].name);
568                 
569                 result = winbindd_fill_pwent(
570                         ent->domain->name, 
571                         domain_user_name,
572                         name_list[ent->sam_entry_index].user_rid,
573                         name_list[ent->sam_entry_index].group_rid,
574                         name_list[ent->sam_entry_index].gecos,
575                         &user_list[user_list_ndx]);
576                 
577                 ent->sam_entry_index++;
578                 
579                 /* Add user to return list */
580                 
581                 if (result) {
582                                 
583                         user_list_ndx++;
584                         state->response.data.num_entries++;
585                         state->response.length += 
586                                 sizeof(struct winbindd_pw);
587
588                 } else
589                         DEBUG(1, ("could not lookup domain user %s\n",
590                                   domain_user_name));
591         }
592
593         /* Out of domains */
594
595         return (user_list_ndx > 0) ? WINBINDD_OK : WINBINDD_ERROR;
596 }
597
598 /* List domain users without mapping to unix ids */
599
600 enum winbindd_result winbindd_list_users(struct winbindd_cli_state *state)
601 {
602         struct winbindd_domain *domain;
603         WINBIND_DISPINFO *info;
604         uint32 num_entries = 0, total_entries = 0;
605         char *ted, *extra_data = NULL;
606         int extra_data_len = 0;
607         TALLOC_CTX *mem_ctx;
608         enum winbindd_result rv = WINBINDD_ERROR;
609
610         DEBUG(3, ("[%5d]: list users\n", state->pid));
611
612         if (!(mem_ctx = talloc_init()))
613                 return WINBINDD_ERROR;
614
615         /* Enumerate over trusted domains */
616
617         if (domain_list == NULL)
618                 get_domain_info();
619
620         for (domain = domain_list; domain; domain = domain->next) {
621                 NTSTATUS status;
622                 uint32 start_ndx = 0;
623                 struct winbindd_methods *methods;
624
625                 /* Skip domains other than WINBINDD_DOMAIN environment
626                    variable */ 
627
628                 if ((strcmp(state->request.domain, "") != 0) &&
629                     !check_domain_env(state->request.domain, domain->name))
630                         continue;
631
632                 methods = domain->methods;
633
634                 /* Query display info */
635
636                 do {
637                         int i;
638
639                         status = methods->query_dispinfo(domain, mem_ctx, &start_ndx, 
640                                                          &num_entries, &info);
641
642                         if (num_entries == 0)
643                                 continue;
644
645                         /* Allocate some memory for extra data */
646                         total_entries += num_entries;
647                         
648                         ted = Realloc(extra_data, sizeof(fstring) * 
649                                              total_entries);
650                         
651                         if (!ted) {
652                                 DEBUG(0,("winbindd_list_users: failed to enlarge buffer!\n"));
653                                 SAFE_FREE(extra_data);
654                                 goto done;
655                         } else 
656                                 extra_data = ted;
657                         
658                         /* Pack user list into extra data fields */
659                         
660                         for (i = 0; i < num_entries; i++) {
661                                 fstring acct_name, name;
662
663                                 if (!info[i].acct_name) {
664                                         fstrcpy(acct_name, "");
665                                 } else {
666                                         fstrcpy(acct_name, info[i].acct_name);
667                                 }
668                                                  
669                                 slprintf(name, sizeof(name) - 1, "%s%s%s",
670                                          domain->name, lp_winbind_separator(),
671                                          acct_name);
672
673                                 /* Append to extra data */
674                                 memcpy(&extra_data[extra_data_len], name, 
675                                        strlen(name));
676                                 extra_data_len += strlen(name);
677                                 extra_data[extra_data_len++] = ',';
678                         }   
679                 } while (NT_STATUS_V(status) == NT_STATUS_V(STATUS_MORE_ENTRIES));
680         }
681
682         /* Assign extra_data fields in response structure */
683
684         if (extra_data) {
685                 extra_data[extra_data_len - 1] = '\0';
686                 state->response.extra_data = extra_data;
687                 state->response.length += extra_data_len;
688         }
689
690         /* No domains responded but that's still OK so don't return an
691            error. */
692
693         rv = WINBINDD_OK;
694
695  done:
696
697         talloc_destroy(mem_ctx);
698
699         return rv;
700 }