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