brought the winbindd code into head
[tprouty/samba.git] / source / nsswitch / winbindd_user.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 2.0
4
5    Winbind daemon - user related function
6
7    Copyright (C) Tim Potter 2000
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 /* Fill a pwent structure with information we have obtained */
27
28 static void winbindd_fill_pwent(struct winbindd_pw *pw, char *name,
29                                 uid_t unix_uid, gid_t unix_gid, 
30                                 char *full_name)
31 {
32     pstring homedir;
33     fstring name_domain, name_user;
34
35     if (!pw || !name) {
36         return;
37     }
38
39     /* Fill in uid/gid */
40
41     pw->pw_uid = unix_uid;
42     pw->pw_gid = unix_gid;
43
44     /* Username */
45
46     safe_strcpy(pw->pw_name, name, sizeof(pw->pw_name) - 1);
47
48     /* Full name (gecos) */
49
50     safe_strcpy(pw->pw_gecos, full_name, sizeof(pw->pw_gecos) - 1);
51
52     /* Home directory and shell - use template config parameters.  The
53        defaults are /tmp for the home directory and /bin/false for shell. */
54
55     parse_domain_user(name, name_domain, name_user);
56
57     pstrcpy(homedir, lp_template_homedir());
58
59     pstring_sub(homedir, "%U", name_user);
60     pstring_sub(homedir, "%D", name_domain);
61
62     safe_strcpy(pw->pw_dir, homedir, sizeof(pw->pw_dir) - 1);
63
64     safe_strcpy(pw->pw_shell, lp_template_shell(), sizeof(pw->pw_shell) - 1);
65
66     /* Password - set to "x" as we can't generate anything useful here.
67        Authentication can be done using the pam_ntdom module. */
68
69     safe_strcpy(pw->pw_passwd, "x", sizeof(pw->pw_passwd) - 1);
70 }
71
72 /* Return a password structure from a username.  Specify whether cached data 
73    can be returned. */
74
75 enum winbindd_result winbindd_getpwnam_from_user(struct winbindd_cli_state *state) 
76 {
77     uint32 name_type, user_rid, group_rid;
78     SAM_USERINFO_CTR user_info;
79     DOM_SID user_sid;
80     fstring name_domain, name_user, name, gecos_name;
81     struct winbindd_domain *domain;
82     uid_t uid;
83     gid_t gid;
84
85     /* Parse domain and username */
86     parse_domain_user(state->request.data.username, name_domain, name_user);
87
88     /* Reject names that don't have a domain - i.e name_domain contains the
89        entire name. */
90  
91     if (strequal(name_domain, "")) {
92         return WINBINDD_ERROR;
93     }
94
95     /* Get info for the domain */
96
97     if ((domain = find_domain_from_name(name_domain)) == NULL) {
98         DEBUG(0, ("could not find domain entry for domain %s\n", name_domain));
99         return WINBINDD_ERROR;
100     }
101
102     /* Check for cached user entry */
103
104     if (winbindd_fetch_user_cache_entry(name_domain, name_user,
105                                         &state->response.data.pw)) {
106             return WINBINDD_OK;
107     }
108
109     slprintf(name,sizeof(name),"%s\\%s", name_domain, name_user);
110
111     /* Get rid and name type from name */
112     /* the following costs 1 packet */
113     if (!winbindd_lookup_sid_by_name(domain, name, &user_sid, &name_type)) {
114         DEBUG(1, ("user '%s' does not exist\n", name_user));
115         return WINBINDD_ERROR;
116     }
117
118     if (name_type != SID_NAME_USER) {
119         DEBUG(1, ("name '%s' is not a user name: %d\n", name_user, name_type));
120         return WINBINDD_ERROR;
121     }
122
123     /* Get some user info.  Split the user rid from the sid obtained from
124        the winbind_lookup_by_name() call and use it in a
125        winbind_lookup_userinfo() */
126     
127     sid_split_rid(&user_sid, &user_rid);
128
129     /* the following costs 3 packets */
130     if (!winbindd_lookup_userinfo(domain, user_rid, &user_info)) {
131         DEBUG(1, ("pwnam_from_user(): error getting user info for user '%s'\n",
132                   name_user));
133         return WINBINDD_ERROR;
134     }
135     
136     group_rid = user_info.info.id21->group_rid;
137     unistr2_to_ascii(gecos_name, &user_info.info.id21->uni_full_name,
138                      sizeof(gecos_name) - 1);
139
140     free_samr_userinfo_ctr(&user_info);
141
142     /* Resolve the uid number */
143
144     if (!winbindd_idmap_get_uid_from_rid(domain->name, user_rid, &uid)) {
145         DEBUG(1, ("error getting user id for user %s\n", name_user));
146         return WINBINDD_ERROR;
147     }
148
149     /* Resolve the gid number */   
150
151     if (!winbindd_idmap_get_gid_from_rid(domain->name, group_rid, &gid)) {
152         DEBUG(1, ("error getting group id for user %s\n", name_user));
153         return WINBINDD_ERROR;
154     }
155
156     /* Now take all this information and fill in a passwd structure */
157             
158     winbindd_fill_pwent(&state->response.data.pw, 
159                         state->request.data.username, uid, gid, 
160                         gecos_name);
161             
162     winbindd_fill_user_cache_entry(name_domain, name_user, 
163                                    &state->response.data.pw);
164
165     return WINBINDD_OK;
166 }       
167
168 /* Return a password structure given a uid number */
169
170 enum winbindd_result winbindd_getpwnam_from_uid(struct winbindd_cli_state 
171                                                 *state)
172 {
173     DOM_SID user_sid;
174     struct winbindd_domain *domain;
175     uint32 user_rid, group_rid;
176     fstring user_name, gecos_name;
177     enum SID_NAME_USE name_type;
178     SAM_USERINFO_CTR user_info;
179     gid_t gid;
180
181     /* Get rid from uid */
182     if (!winbindd_idmap_get_rid_from_uid(state->request.data.uid, &user_rid,
183                                          &domain)) {
184         DEBUG(1, ("Could not convert uid %d to rid\n", 
185                   state->request.data.uid));
186         return WINBINDD_ERROR;
187     }
188     
189     /* Check for cached uid entry */
190     if (winbindd_fetch_uid_cache_entry(domain->name, state->request.data.uid,
191                                        &state->response.data.pw)) {
192             return WINBINDD_OK;
193     }
194
195
196     /* Get name and name type from rid */
197
198     sid_copy(&user_sid, &domain->sid);
199     sid_append_rid(&user_sid, user_rid);
200
201     if (!winbindd_lookup_name_by_sid(domain, &user_sid, user_name, 
202                                      &name_type)) {
203         fstring temp;
204
205         sid_to_string(temp, &user_sid);
206         DEBUG(1, ("Could not lookup sid %s\n", temp));
207         return WINBINDD_ERROR;
208     }
209
210     string_sub(user_name, "\\", "/", sizeof(fstring));
211
212     /* Get some user info */
213     
214     if (!winbindd_lookup_userinfo(domain, user_rid, &user_info)) {
215         DEBUG(1, ("pwnam_from_uid(): error getting user info for user '%s'\n",
216                   user_name));
217         return WINBINDD_ERROR;
218     }
219
220     group_rid = user_info.info.id21->group_rid;
221     unistr2_to_ascii(gecos_name, &user_info.info.id21->uni_full_name,
222                      sizeof(gecos_name) - 1);
223
224     free_samr_userinfo_ctr(&user_info);
225
226     /* Resolve gid number */
227
228     if (!winbindd_idmap_get_gid_from_rid(domain->name, group_rid, &gid)) {
229         DEBUG(1, ("error getting group id for user %s\n", user_name));
230         return WINBINDD_ERROR;
231     }
232
233     /* Fill in password structure */
234
235     winbindd_fill_pwent(&state->response.data.pw, user_name, 
236                         state->request.data.uid, gid, gecos_name);
237
238     winbindd_fill_uid_cache_entry(domain->name, state->request.data.uid,
239                                   &state->response.data.pw);
240
241     return WINBINDD_OK;
242 }
243
244 /*
245  * set/get/endpwent functions
246  */
247
248 /* Rewind file pointer for ntdom passwd database */
249
250 enum winbindd_result winbindd_setpwent(struct winbindd_cli_state *state)
251 {
252     struct winbindd_domain *tmp;
253
254     if (state == NULL) return WINBINDD_ERROR;
255     
256     /* Free old static data if it exists */
257
258     if (state->getpwent_state != NULL) {
259         free_getent_state(state->getpwent_state);
260         state->getpwent_state = NULL;
261     }
262
263     /* Create sam pipes for each domain we know about */
264
265     for(tmp = domain_list; tmp != NULL; tmp = tmp->next) {
266         struct getent_state *domain_state;
267
268         /* Skip domains other than WINBINDD_DOMAIN environment variable */
269
270         if ((strcmp(state->request.domain, "") != 0) &&
271             (strcmp(state->request.domain, tmp->name) != 0)) {
272                 continue;
273         }
274
275         /* Create a state record for this domain */
276
277         if ((domain_state = (struct getent_state *)
278              malloc(sizeof(struct getent_state))) == NULL) {
279
280             return WINBINDD_ERROR;
281         }
282
283         ZERO_STRUCTP(domain_state);
284         domain_state->domain = tmp;
285
286         /* Add to list of open domains */
287
288         DLIST_ADD(state->getpwent_state, domain_state)
289     }
290
291     return WINBINDD_OK;
292 }
293
294 /* Close file pointer to ntdom passwd database */
295
296 enum winbindd_result winbindd_endpwent(struct winbindd_cli_state *state)
297 {
298     if (state == NULL) return WINBINDD_ERROR;
299
300     free_getent_state(state->getpwent_state);    
301     state->getpwent_state = NULL;
302
303     return WINBINDD_OK;
304 }
305
306 /* Fetch next passwd entry from ntdom database */
307
308 enum winbindd_result winbindd_getpwent(struct winbindd_cli_state *state)
309 {
310     if (state == NULL) return WINBINDD_ERROR;
311
312     /* Process the current head of the getent_state list */
313
314     while(state->getpwent_state != NULL) {
315         struct getent_state *ent = state->getpwent_state;
316
317         /* Get list of user entries for this pipe */
318
319         if (!ent->got_sam_entries) {
320             uint32 status, start_ndx = 0;
321
322             /* Look in cache for entries, else get them direct */
323
324             if (!winbindd_fetch_user_cache(ent->domain->name, 
325                                            &ent->sam_entries,
326                                            &ent->num_sam_entries)) {
327
328                 /* Fetch the user entries */
329
330                 if (!domain_handles_open(ent->domain)) goto cleanup;
331
332                 do {
333                     status =
334                         samr_enum_dom_users(
335                             &ent->domain->sam_dom_handle, &start_ndx, 0, 0, 
336                             0x10000, &ent->sam_entries, &ent->num_sam_entries);
337                 } while (status == STATUS_MORE_ENTRIES);
338
339                 /* Fill cache with received entries */
340             
341                 winbindd_fill_user_cache(ent->domain->name, ent->sam_entries, 
342                                          ent->num_sam_entries);
343             }
344             
345             ent->got_sam_entries = True;
346         }
347         
348         /* Send back a user */
349
350         while (ent->sam_entry_index < ent->num_sam_entries) {
351             enum winbindd_result result;
352             fstring domain_user_name;
353             char *user_name = (ent->sam_entries)
354                 [ent->sam_entry_index].acct_name; 
355                 
356             /* Don't bother with machine accounts */
357
358             if (user_name[strlen(user_name) - 1] == '$') {
359                 ent->sam_entry_index++;
360                 continue;
361             }
362
363             /* Prepend domain to name */
364
365             slprintf(domain_user_name, sizeof(domain_user_name),
366                      "%s/%s", ent->domain->name, user_name);
367                 
368             /* Get passwd entry from user name */
369                 
370             fstrcpy(state->request.data.username, domain_user_name);
371             result = winbindd_getpwnam_from_user(state);
372
373             ent->sam_entry_index++;
374                 
375             /* Return if user lookup worked */
376                 
377             if (result == WINBINDD_OK) {
378                 return result;
379             }
380                 
381             /* Try next user */
382             
383             DEBUG(1, ("could not getpwnam_from_user for username %s\n",
384                       domain_user_name));
385         }
386
387         /* We've exhausted all users for this pipe - close it down and
388            start on the next one. */
389
390     cleanup:
391
392         /* Free mallocated memory for sam entries.  The data stored here
393            may have been allocated from the cache. */
394
395         if (ent->sam_entries != NULL) free(ent->sam_entries);
396         ent->sam_entries = NULL;
397
398         /* Free state information for this domain */
399
400         {
401             struct getent_state *old_ent;
402
403             old_ent = state->getpwent_state;
404             DLIST_REMOVE(state->getpwent_state, state->getpwent_state);
405             free(old_ent);
406         }
407     }
408
409     /* Out of pipes so we're done */
410
411     return WINBINDD_ERROR;
412 }