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