Forward port the change to talloc_init() to make all talloc contexts
[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    
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 2 of the License, or
12    (at your option) any later version.
13    
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18    
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23
24 #include "winbindd.h"
25
26 #undef DBGC_CLASS
27 #define DBGC_CLASS DBGC_WINBIND
28
29 /* Fill a pwent structure with information we have obtained */
30
31 static BOOL winbindd_fill_pwent(char *dom_name, char *user_name, 
32                                 uint32 user_rid, uint32 group_rid, 
33                                 char *full_name, struct winbindd_pw *pw)
34 {
35         extern userdom_struct current_user_info;
36         fstring output_username;
37         pstring homedir;
38         
39         if (!pw || !dom_name || !user_name)
40                 return False;
41         
42         /* Resolve the uid number */
43         
44         if (!winbindd_idmap_get_uid_from_rid(dom_name, user_rid, 
45                                              &pw->pw_uid)) {
46                 DEBUG(1, ("error getting user id for rid %d\n", user_rid));
47                 return False;
48         }
49         
50         /* Resolve the gid number */   
51         
52         if (!winbindd_idmap_get_gid_from_rid(dom_name, group_rid, 
53                                              &pw->pw_gid)) {
54                 DEBUG(1, ("error getting group id for rid %d\n", group_rid));
55                 return False;
56         }
57
58         /* Username */
59
60         fill_domain_username(output_username, dom_name, user_name); 
61
62         safe_strcpy(pw->pw_name, output_username, sizeof(pw->pw_name) - 1);
63         
64         /* Full name (gecos) */
65         
66         safe_strcpy(pw->pw_gecos, full_name, sizeof(pw->pw_gecos) - 1);
67
68         /* Home directory and shell - use template config parameters.  The
69            defaults are /tmp for the home directory and /bin/false for
70            shell. */
71         
72         /* The substitution of %U and %D in the 'template homedir' is done
73            by lp_string() calling standard_sub_basic(). */
74
75         fstrcpy(current_user_info.smb_name, user_name);
76         fstrcpy(current_user_info.domain, dom_name);
77
78         pstrcpy(homedir, lp_template_homedir());
79         
80         safe_strcpy(pw->pw_dir, homedir, sizeof(pw->pw_dir) - 1);
81         
82         safe_strcpy(pw->pw_shell, lp_template_shell(), 
83                     sizeof(pw->pw_shell) - 1);
84         
85         /* Password - set to "x" as we can't generate anything useful here.
86            Authentication can be done using the pam_winbind module. */
87
88         safe_strcpy(pw->pw_passwd, "x", sizeof(pw->pw_passwd) - 1);
89         
90         return True;
91 }
92
93 /* Return a password structure from a username.  */
94
95 enum winbindd_result winbindd_getpwnam(struct winbindd_cli_state *state) 
96 {
97         uint32 user_rid;
98         WINBIND_USERINFO user_info;
99         DOM_SID user_sid;
100         NTSTATUS status;
101         fstring name_domain, name_user;
102         enum SID_NAME_USE name_type;
103         struct winbindd_domain *domain;
104         TALLOC_CTX *mem_ctx;
105         
106         /* Ensure null termination */
107         state->request.data.username[sizeof(state->request.data.username)-1]='\0';
108
109         DEBUG(3, ("[%5d]: getpwnam %s\n", state->pid,
110                   state->request.data.username));
111         
112         /* Parse domain and username */
113
114         if (!parse_domain_user(state->request.data.username, name_domain, 
115                                name_user))
116                 return WINBINDD_ERROR;
117         
118         if ((domain = find_domain_from_name(name_domain)) == NULL) {
119                 DEBUG(5, ("no such domain: %s\n", name_domain));
120                 return WINBINDD_ERROR;
121         }
122         
123         /* Get rid and name type from name */
124
125         if (!winbindd_lookup_sid_by_name(domain, name_user, &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("winbindd_getpwnam([%s]\\[%s])", 
141                                           name_domain, 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]\\[%s]'\n", 
153                           name_domain, 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, name_user, 
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 dom_name;
180         fstring user_name;
181         enum SID_NAME_USE name_type;
182         WINBIND_USERINFO user_info;
183         gid_t gid;
184         TALLOC_CTX *mem_ctx;
185         NTSTATUS status;
186         
187         /* Bug out if the uid isn't in the winbind range */
188
189         if ((state->request.data.uid < server_state.uid_low ) ||
190             (state->request.data.uid > server_state.uid_high))
191                 return WINBINDD_ERROR;
192
193         DEBUG(3, ("[%5d]: getpwuid %d\n", state->pid, 
194                   state->request.data.uid));
195         
196         /* Get rid from uid */
197
198         if (!winbindd_idmap_get_rid_from_uid(state->request.data.uid, 
199                                              &user_rid, &domain)) {
200                 DEBUG(1, ("could not convert uid %d to rid\n", 
201                           state->request.data.uid));
202                 return WINBINDD_ERROR;
203         }
204         
205         /* Get name and name type from rid */
206
207         sid_copy(&user_sid, &domain->sid);
208         sid_append_rid(&user_sid, user_rid);
209         
210         if (!winbindd_lookup_name_by_sid(&user_sid, dom_name, user_name, &name_type)) {
211                 fstring temp;
212                 
213                 sid_to_string(temp, &user_sid);
214                 DEBUG(1, ("could not lookup sid %s\n", temp));
215                 return WINBINDD_ERROR;
216         }
217         
218         /* Get some user info */
219         
220         if (!(mem_ctx = talloc_init("winbind_getpwuid(%d)",
221                                           state->request.data.uid))) {
222
223                 DEBUG(1, ("out of memory\n"));
224                 return WINBINDD_ERROR;
225         }
226
227         status = domain->methods->query_user(domain, mem_ctx, user_rid, 
228                                              &user_info);
229
230         if (!NT_STATUS_IS_OK(status)) {
231                 DEBUG(1, ("error getting user info for user '%s'\n", 
232                           user_name));
233                 talloc_destroy(mem_ctx);
234                 return WINBINDD_ERROR;
235         }
236         
237         /* Resolve gid number */
238
239         if (!winbindd_idmap_get_gid_from_rid(domain->name, user_info.group_rid, &gid)) {
240                 DEBUG(1, ("error getting group id for user %s\n", user_name));
241                 talloc_destroy(mem_ctx);
242                 return WINBINDD_ERROR;
243         }
244
245         /* Fill in password structure */
246
247         if (!winbindd_fill_pwent(domain->name, user_name, user_rid, user_info.group_rid,
248                                  user_info.full_name, &state->response.data.pw)) {
249                 talloc_destroy(mem_ctx);
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         for(domain = domain_list(); domain != NULL; domain = domain->next) {
285                 struct getent_state *domain_state;
286                 
287                 /* Create a state record for this domain */
288                 
289                 if ((domain_state = (struct getent_state *)
290                      malloc(sizeof(struct getent_state))) == NULL)
291                         return WINBINDD_ERROR;
292                 
293                 ZERO_STRUCTP(domain_state);
294
295                 fstrcpy(domain_state->domain_name, domain->name);
296
297                 /* Add to list of open domains */
298                 
299                 DLIST_ADD(state->getpwent_state, domain_state);
300         }
301         
302         return WINBINDD_OK;
303 }
304
305 /* Close file pointer to ntdom passwd database */
306
307 enum winbindd_result winbindd_endpwent(struct winbindd_cli_state *state)
308 {
309         DEBUG(3, ("[%5d]: endpwent\n", state->pid));
310
311         free_getent_state(state->getpwent_state);    
312         state->getpwent_state = NULL;
313         
314         return WINBINDD_OK;
315 }
316
317 /* Get partial list of domain users for a domain.  We fill in the sam_entries,
318    and num_sam_entries fields with domain user information.  The dispinfo_ndx
319    field is incremented to the index of the next user to fetch.  Return True if
320    some users were returned, False otherwise. */
321
322 #define MAX_FETCH_SAM_ENTRIES 100
323
324 static BOOL get_sam_user_entries(struct getent_state *ent)
325 {
326         NTSTATUS status;
327         uint32 num_entries;
328         WINBIND_USERINFO *info;
329         struct getpwent_user *name_list = NULL;
330         BOOL result = False;
331         TALLOC_CTX *mem_ctx;
332         struct winbindd_domain *domain;
333         struct winbindd_methods *methods;
334         int i;
335
336         if (ent->num_sam_entries)
337                 return False;
338
339         if (!(mem_ctx = talloc_init("get_sam_user_entries(%s)",
340                                           ent->domain_name)))
341                 return False;
342
343         if (!(domain = find_domain_from_name(ent->domain_name))) {
344                 DEBUG(3, ("no such domain %s in get_sam_user_entries\n",
345                           ent->domain_name));
346                 return False;
347         }
348
349         methods = domain->methods;
350
351         /* Free any existing user info */
352
353         SAFE_FREE(ent->sam_entries);
354         ent->num_sam_entries = 0;
355         
356         /* Call query_user_list to get a list of usernames and user rids */
357
358         num_entries = 0;
359
360         status = methods->query_user_list(domain, mem_ctx, &num_entries, 
361                                           &info);
362                 
363         if (num_entries) {
364                 struct getpwent_user *tnl;
365                 
366                 tnl = (struct getpwent_user *)Realloc(name_list, 
367                                                       sizeof(struct getpwent_user) *
368                                                       (ent->num_sam_entries + 
369                                                        num_entries));
370                 
371                 if (!tnl) {
372                         DEBUG(0,("get_sam_user_entries realloc failed.\n"));
373                         SAFE_FREE(name_list);
374                         goto done;
375                 } else
376                         name_list = tnl;
377         }
378
379         for (i = 0; i < num_entries; i++) {
380                 /* Store account name and gecos */
381                 if (!info[i].acct_name) {
382                         fstrcpy(name_list[ent->num_sam_entries + i].name, "");
383                 } else {
384                         fstrcpy(name_list[ent->num_sam_entries + i].name, 
385                                 info[i].acct_name); 
386                 }
387                 if (!info[i].full_name) {
388                         fstrcpy(name_list[ent->num_sam_entries + i].gecos, "");
389                 } else {
390                         fstrcpy(name_list[ent->num_sam_entries + i].gecos, 
391                                 info[i].full_name); 
392                 }
393                 
394                 /* User and group ids */
395                 name_list[ent->num_sam_entries+i].user_rid = info[i].user_rid;
396                 name_list[ent->num_sam_entries+i].group_rid = info[i].group_rid;
397         }
398                 
399         ent->num_sam_entries += num_entries;
400         
401         /* Fill in remaining fields */
402         
403         ent->sam_entries = name_list;
404         ent->sam_entry_index = 0;
405         result = ent->num_sam_entries > 0;
406
407  done:
408
409         talloc_destroy(mem_ctx);
410
411         return result;
412 }
413
414 /* Fetch next passwd entry from ntdom database */
415
416 #define MAX_GETPWENT_USERS 500
417
418 enum winbindd_result winbindd_getpwent(struct winbindd_cli_state *state)
419 {
420         struct getent_state *ent;
421         struct winbindd_pw *user_list;
422         int num_users, user_list_ndx = 0, i;
423
424         DEBUG(3, ("[%5d]: getpwent\n", state->pid));
425
426         /* Check user has enabled this */
427
428         if (!lp_winbind_enum_users())
429                 return WINBINDD_ERROR;
430
431         /* Allocate space for returning a chunk of users */
432
433         num_users = MIN(MAX_GETPWENT_USERS, state->request.data.num_entries);
434         
435         if ((state->response.extra_data = 
436              malloc(num_users * sizeof(struct winbindd_pw))) == NULL)
437                 return WINBINDD_ERROR;
438
439         memset(state->response.extra_data, 0, num_users * 
440                sizeof(struct winbindd_pw));
441
442         user_list = (struct winbindd_pw *)state->response.extra_data;
443         
444         if (!(ent = state->getpwent_state))
445                 return WINBINDD_ERROR;
446
447         /* Start sending back users */
448
449         for (i = 0; i < num_users; i++) {
450                 struct getpwent_user *name_list = NULL;
451                 fstring domain_user_name;
452                 uint32 result;
453
454                 /* Do we need to fetch another chunk of users? */
455
456                 if (ent->num_sam_entries == ent->sam_entry_index) {
457
458                         while(ent && !get_sam_user_entries(ent)) {
459                                 struct getent_state *next_ent;
460
461                                 /* Free state information for this domain */
462
463                                 SAFE_FREE(ent->sam_entries);
464
465                                 next_ent = ent->next;
466                                 DLIST_REMOVE(state->getpwent_state, ent);
467
468                                 SAFE_FREE(ent);
469                                 ent = next_ent;
470                         }
471  
472                         /* No more domains */
473
474                         if (!ent) 
475                                 break;
476                 }
477
478                 name_list = ent->sam_entries;
479
480                 /* Skip machine accounts */
481
482                 if (name_list[ent->sam_entry_index].
483                     name[strlen(name_list[ent->sam_entry_index].name) - 1] 
484                     == '$') {
485                         ent->sam_entry_index++;
486                         continue;
487                 }
488
489                 /* Lookup user info */
490                 
491                 result = winbindd_fill_pwent(
492                         ent->domain_name, 
493                         name_list[ent->sam_entry_index].name,
494                         name_list[ent->sam_entry_index].user_rid,
495                         name_list[ent->sam_entry_index].group_rid,
496                         name_list[ent->sam_entry_index].gecos,
497                         &user_list[user_list_ndx]);
498                 
499                 ent->sam_entry_index++;
500                 
501                 /* Add user to return list */
502                 
503                 if (result) {
504                                 
505                         user_list_ndx++;
506                         state->response.data.num_entries++;
507                         state->response.length += 
508                                 sizeof(struct winbindd_pw);
509
510                 } else
511                         DEBUG(1, ("could not lookup domain user %s\n",
512                                   domain_user_name));
513         }
514
515         /* Out of domains */
516
517         return (user_list_ndx > 0) ? WINBINDD_OK : WINBINDD_ERROR;
518 }
519
520 /* List domain users without mapping to unix ids */
521
522 enum winbindd_result winbindd_list_users(struct winbindd_cli_state *state)
523 {
524         struct winbindd_domain *domain;
525         WINBIND_USERINFO *info;
526         uint32 num_entries = 0, total_entries = 0;
527         char *ted, *extra_data = NULL;
528         int extra_data_len = 0;
529         TALLOC_CTX *mem_ctx;
530         enum winbindd_result rv = WINBINDD_ERROR;
531
532         DEBUG(3, ("[%5d]: list users\n", state->pid));
533
534         if (!(mem_ctx = talloc_init("winbindd_list_users")))
535                 return WINBINDD_ERROR;
536
537         /* Enumerate over trusted domains */
538
539         for (domain = domain_list(); domain; domain = domain->next) {
540                 NTSTATUS status;
541                 struct winbindd_methods *methods;
542                 int i;
543
544                 methods = domain->methods;
545
546                 /* Query display info */
547                 status = methods->query_user_list(domain, mem_ctx, 
548                                                   &num_entries, &info);
549
550                 if (num_entries == 0)
551                         continue;
552
553                 /* Allocate some memory for extra data */
554                 total_entries += num_entries;
555                         
556                 ted = Realloc(extra_data, sizeof(fstring) * total_entries);
557                         
558                 if (!ted) {
559                         DEBUG(0,("failed to enlarge buffer!\n"));
560                         SAFE_FREE(extra_data);
561                         goto done;
562                 } else 
563                         extra_data = ted;
564                         
565                 /* Pack user list into extra data fields */
566                         
567                 for (i = 0; i < num_entries; i++) {
568                         fstring acct_name, name;
569                         
570                         if (!info[i].acct_name) {
571                                 fstrcpy(acct_name, "");
572                         } else {
573                                 fstrcpy(acct_name, info[i].acct_name);
574                         }
575                         
576                         fill_domain_username(name, domain->name, acct_name);
577                         
578                                 /* Append to extra data */
579                         memcpy(&extra_data[extra_data_len], name, 
580                                strlen(name));
581                         extra_data_len += strlen(name);
582                         extra_data[extra_data_len++] = ',';
583                 }   
584         }
585
586         /* Assign extra_data fields in response structure */
587
588         if (extra_data) {
589                 extra_data[extra_data_len - 1] = '\0';
590                 state->response.extra_data = extra_data;
591                 state->response.length += extra_data_len;
592         }
593
594         /* No domains responded but that's still OK so don't return an
595            error. */
596
597         rv = WINBINDD_OK;
598
599  done:
600
601         talloc_destroy(mem_ctx);
602
603         return rv;
604 }