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