Merge winbind from Samba 3.0 onto HEAD.
[samba.git] / source / nsswitch / winbindd_sid.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    Winbind daemon - sid related functions
5
6    Copyright (C) Tim Potter 2000
7    
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "includes.h"
24 #include "winbindd.h"
25
26 #undef DBGC_CLASS
27 #define DBGC_CLASS DBGC_WINBIND
28
29 /* Convert a string  */
30
31 enum winbindd_result winbindd_lookupsid(struct winbindd_cli_state *state)
32 {
33         extern DOM_SID global_sid_Builtin;
34         enum SID_NAME_USE type;
35         DOM_SID sid, tmp_sid;
36         uint32 rid;
37         fstring name;
38         fstring dom_name;
39
40         /* Ensure null termination */
41         state->request.data.sid[sizeof(state->request.data.sid)-1]='\0';
42
43         DEBUG(3, ("[%5lu]: lookupsid %s\n", (unsigned long)state->pid, 
44                   state->request.data.sid));
45
46         /* Lookup sid from PDC using lsa_lookup_sids() */
47
48         if (!string_to_sid(&sid, state->request.data.sid)) {
49                 DEBUG(5, ("%s not a SID\n", state->request.data.sid));
50                 return WINBINDD_ERROR;
51         }
52
53         /* Don't look up BUILTIN sids */
54
55         sid_copy(&tmp_sid, &sid);
56         sid_split_rid(&tmp_sid, &rid);
57
58         if (sid_equal(&tmp_sid, &global_sid_Builtin)) {
59                 return WINBINDD_ERROR;
60         }
61
62         /* Lookup the sid */
63
64         if (!winbindd_lookup_name_by_sid(&sid, dom_name, name, &type)) {
65                 return WINBINDD_ERROR;
66         }
67
68         fstrcpy(state->response.data.name.dom_name, dom_name);
69         fstrcpy(state->response.data.name.name, name);
70
71         state->response.data.name.type = type;
72
73         return WINBINDD_OK;
74 }
75
76
77 /**
78  * Look up the SID for a qualified name.  
79  **/
80 enum winbindd_result winbindd_lookupname(struct winbindd_cli_state *state)
81 {
82         enum SID_NAME_USE type;
83         fstring sid_str;
84         char *name_domain, *name_user;
85         DOM_SID sid;
86         struct winbindd_domain *domain;
87         char *p;
88
89         /* Ensure null termination */
90         state->request.data.sid[sizeof(state->request.data.name.dom_name)-1]='\0';
91
92         /* Ensure null termination */
93         state->request.data.sid[sizeof(state->request.data.name.name)-1]='\0';
94
95         /* cope with the name being a fully qualified name */
96         p = strstr(state->request.data.name.name, lp_winbind_separator());
97         if (p) {
98                 *p = 0;
99                 name_domain = state->request.data.name.name;
100                 name_user = p+1;
101         } else {
102                 name_domain = state->request.data.name.dom_name;
103                 name_user = state->request.data.name.name;
104         }
105
106         DEBUG(3, ("[%5lu]: lookupname %s%s%s\n", (unsigned long)state->pid,
107                   name_domain, lp_winbind_separator(), name_user));
108
109         if ((domain = find_domain_from_name(name_domain)) == NULL) {
110                 DEBUG(0, ("could not find domain entry for domain %s\n", 
111                           name_domain));
112                 return WINBINDD_ERROR;
113         }
114
115         /* Lookup name from PDC using lsa_lookup_names() */
116         if (!winbindd_lookup_sid_by_name(domain, name_user, &sid, &type)) {
117                 return WINBINDD_ERROR;
118         }
119
120         sid_to_string(sid_str, &sid);
121         fstrcpy(state->response.data.sid.sid, sid_str);
122         state->response.data.sid.type = type;
123
124         return WINBINDD_OK;
125 }
126
127 /* Convert a sid to a uid.  We assume we only have one rid attached to the
128    sid. */
129
130 enum winbindd_result winbindd_sid_to_uid(struct winbindd_cli_state *state)
131 {
132         DOM_SID sid;
133         uint32 flags = 0x0;
134
135         /* Ensure null termination */
136         state->request.data.sid[sizeof(state->request.data.sid)-1]='\0';
137
138         DEBUG(3, ("[%5lu]: sid to uid %s\n", (unsigned long)state->pid,
139                   state->request.data.sid));
140
141         if (!string_to_sid(&sid, state->request.data.sid)) {
142                 DEBUG(1, ("Could not get convert sid %s from string\n", state->request.data.sid));
143                 return WINBINDD_ERROR;
144         }
145         
146         /* This gets a little tricky.  If we assume that usernames are syncd between
147            /etc/passwd and the windows domain (such as a member of a Samba domain),
148            the we need to get the uid from the OS and not alocate one ourselves */
149            
150         if ( lp_winbind_trusted_domains_only() ) {
151                 struct winbindd_domain *domain = NULL;
152                 DOM_SID sid2;
153                 uint32 rid;
154                 
155                 domain = find_domain_from_name( lp_workgroup() );
156                 if ( !domain ) {
157                         DEBUG(0,("winbindd_sid_to_uid: can't find my own domain!\n"));
158                         return WINBINDD_ERROR;
159                 }
160
161                 sid_copy( &sid2, &sid );
162                 sid_split_rid( &sid2, &rid );
163                 
164                 if ( sid_equal( &sid2, &domain->sid ) ) {
165                 
166                         fstring domain_name;
167                         fstring user;
168                         enum SID_NAME_USE type;
169                         struct passwd *pw = NULL;
170                         unid_t id;
171                         
172                         /* ok...here's we know that we are dealing with our
173                            own domain (the one to which we are joined).  And
174                            we know that there must be a UNIX account for this user.
175                            So we lookup the sid and the call getpwnam().*/
176                            
177                         
178                         /* But first check and see if we don't already have a mapping */
179                            
180                         flags = ID_QUERY_ONLY;
181                         if ( NT_STATUS_IS_OK(idmap_sid_to_uid(&sid, &(state->response.data.uid), flags)) )
182                                 return WINBINDD_OK;
183                                 
184                         /* now fall back to the hard way */
185                         
186                         if ( !winbindd_lookup_name_by_sid(&sid, domain_name, user, &type) )
187                                 return WINBINDD_ERROR;
188                                 
189                         if ( !(pw = getpwnam(user)) ) {
190                                 DEBUG(0,("winbindd_sid_to_uid: 'winbind trusted domains only' is "
191                                         "set but this user [%s] doesn't exist!\n", user));
192                                 return WINBINDD_ERROR;
193                         }
194                         
195                         state->response.data.uid = pw->pw_uid;
196
197                         id.uid = pw->pw_uid;
198                         idmap_set_mapping( &sid, id, ID_USERID );
199
200                         return WINBINDD_OK;
201                 }
202
203         }
204         
205         if ( state->request.flags & WBFLAG_QUERY_ONLY ) 
206                 flags = ID_QUERY_ONLY;
207         
208         /* Find uid for this sid and return it */
209         
210         if ( !NT_STATUS_IS_OK(idmap_sid_to_uid(&sid, &(state->response.data.uid), flags)) ) {
211                 DEBUG(1, ("Could not get uid for sid %s\n", state->request.data.sid));
212                 return WINBINDD_ERROR;
213         }
214
215         return WINBINDD_OK;
216 }
217
218 /* Convert a sid to a gid.  We assume we only have one rid attached to the
219    sid.*/
220
221 enum winbindd_result winbindd_sid_to_gid(struct winbindd_cli_state *state)
222 {
223         DOM_SID sid;
224         uint32 flags = 0x0;
225
226         /* Ensure null termination */
227         state->request.data.sid[sizeof(state->request.data.sid)-1]='\0';
228
229         DEBUG(3, ("[%5lu]: sid to gid %s\n", (unsigned long)state->pid, 
230                   state->request.data.sid));
231
232         if (!string_to_sid(&sid, state->request.data.sid)) {
233                 DEBUG(1, ("Could not cvt string to sid %s\n", state->request.data.sid));
234                 return WINBINDD_ERROR;
235         }
236
237         /* This gets a little tricky.  If we assume that usernames are syncd between
238            /etc/passwd and the windows domain (such as a member of a Samba domain),
239            the we need to get the uid from the OS and not alocate one ourselves */
240            
241         if ( lp_winbind_trusted_domains_only() ) {
242                 struct winbindd_domain *domain = NULL;
243                 DOM_SID sid2;
244                 uint32 rid;
245                 unid_t id;
246                 
247                 domain = find_domain_from_name( lp_workgroup() );
248                 if ( !domain ) {
249                         DEBUG(0,("winbindd_sid_to_uid: can't find my own domain!\n"));
250                         return WINBINDD_ERROR;
251                 }
252                 
253                 sid_copy( &sid2, &sid );
254                 sid_split_rid( &sid2, &rid );
255
256                 if ( sid_equal( &sid2, &domain->sid ) ) {
257                 
258                         fstring domain_name;
259                         fstring group;
260                         enum SID_NAME_USE type;
261                         struct group *grp = NULL;
262                         
263                         /* ok...here's we know that we are dealing with our
264                            own domain (the one to which we are joined).  And
265                            we know that there must be a UNIX account for this group.
266                            So we lookup the sid and the call getpwnam().*/
267                         
268                         /* But first check and see if we don't already have a mapping */
269                            
270                         flags = ID_QUERY_ONLY;
271                         if ( NT_STATUS_IS_OK(idmap_sid_to_gid(&sid, &(state->response.data.gid), flags)) )
272                                 return WINBINDD_OK;
273                                 
274                         /* now fall back to the hard way */
275                         
276                         if ( !winbindd_lookup_name_by_sid(&sid, domain_name, group, &type) )
277                                 return WINBINDD_ERROR;
278                                 
279                         if ( !(grp = getgrnam(group)) ) {
280                                 DEBUG(0,("winbindd_sid_to_uid: 'winbind trusted domains only' is "
281                                         "set but this group [%s] doesn't exist!\n", group));
282                                 return WINBINDD_ERROR;
283                         }
284                         
285                         state->response.data.gid = grp->gr_gid;
286
287                         id.gid = grp->gr_gid;
288                         idmap_set_mapping( &sid, id, ID_GROUPID );
289
290                         return WINBINDD_OK;
291                 }
292
293         }
294         
295         if ( state->request.flags & WBFLAG_QUERY_ONLY ) 
296                 flags = ID_QUERY_ONLY;
297                 
298         /* Find gid for this sid and return it */
299         if ( !NT_STATUS_IS_OK(idmap_sid_to_gid(&sid, &(state->response.data.gid), flags)) ) {
300                 DEBUG(1, ("Could not get gid for sid %s\n", state->request.data.sid));
301                 return WINBINDD_ERROR;
302         }
303
304         return WINBINDD_OK;
305 }
306
307 /* Convert a uid to a sid */
308
309 enum winbindd_result winbindd_uid_to_sid(struct winbindd_cli_state *state)
310 {
311         DOM_SID sid;
312
313         DEBUG(3, ("[%5lu]: uid to sid %lu\n", (unsigned long)state->pid, 
314                   (unsigned long)state->request.data.uid));
315
316         if ( (state->request.data.uid < server_state.uid_low ) 
317                 || (state->request.data.uid > server_state.uid_high) )
318         {
319                 struct passwd *pw = NULL;
320                 enum SID_NAME_USE type;
321                 unid_t id;
322                 struct winbindd_domain *domain;
323
324                 /* SPECIAL CASE FOR MEMBERS OF SAMBA DOMAINS */
325                 
326                 /* if we don't trust /etc/password then when can't know 
327                    anything about this uid */
328                    
329                 if ( !lp_winbind_trusted_domains_only() )
330                         return WINBINDD_ERROR;
331
332
333                 /* look for an idmap entry first */
334                         
335                 if ( NT_STATUS_IS_OK(idmap_uid_to_sid(&sid, state->request.data.uid)) ) 
336                         goto done;
337                 
338                 /* if users exist in /etc/passwd, we should try to 
339                    use that uid. Get the username and the lookup the SID */
340
341                 if ( !(pw = getpwuid(state->request.data.uid)) )
342                         return WINBINDD_ERROR;
343
344                 if ( !(domain = find_domain_from_name(lp_workgroup())) ) {
345                         DEBUG(0,("winbindd_uid_to_sid: can't find my own domain!\n"));
346                         return WINBINDD_ERROR;
347                 }
348
349                 if ( !winbindd_lookup_sid_by_name(domain, pw->pw_name, &sid, &type) )
350                         return WINBINDD_ERROR;
351                 
352                 if ( type != SID_NAME_USER )
353                         return WINBINDD_ERROR;
354                 
355                 /* don't fail if we can't store it */
356
357                 id.uid = pw->pw_uid;
358                 idmap_set_mapping( &sid, id, ID_USERID );
359                 
360                 goto done;
361         }
362
363         /* Lookup rid for this uid */
364         
365         if (!NT_STATUS_IS_OK(idmap_uid_to_sid(&sid, state->request.data.uid))) {
366                 DEBUG(1, ("Could not convert uid %lu to rid\n",
367                           (unsigned long)state->request.data.uid));
368                 return WINBINDD_ERROR;
369         }
370
371 done:
372         sid_to_string(state->response.data.sid.sid, &sid);
373         state->response.data.sid.type = SID_NAME_USER;
374
375         return WINBINDD_OK;
376 }
377
378 /* Convert a gid to a sid */
379
380 enum winbindd_result winbindd_gid_to_sid(struct winbindd_cli_state *state)
381 {
382         DOM_SID sid;
383
384         DEBUG(3, ("[%5lu]: gid to sid %lu\n", (unsigned long)state->pid,
385                   (unsigned long)state->request.data.gid));
386                   
387         if ( (state->request.data.gid < server_state.gid_low) 
388                 || (state->request.data.gid > server_state.gid_high) )
389         {               
390                 struct group *grp = NULL;
391                 enum SID_NAME_USE type;
392                 unid_t id;
393                 struct winbindd_domain *domain;
394
395                 /* SPECIAL CASE FOR MEMBERS OF SAMBA DOMAINS */
396                 
397                 /* if we don't trust /etc/group then when can't know 
398                    anything about this gid */
399                    
400                 if ( !lp_winbind_trusted_domains_only() )
401                         return WINBINDD_ERROR;
402
403                 /* look for an idmap entry first */
404                 
405                 if ( NT_STATUS_IS_OK(idmap_gid_to_sid(&sid, state->request.data.gid)) )
406                         goto done;
407                         
408                 /* if users exist in /etc/group, we should try to 
409                    use that gid. Get the username and the lookup the SID */
410
411                 if ( !(grp = getgrgid(state->request.data.gid)) )
412                         return WINBINDD_ERROR;
413
414                 if ( !(domain = find_domain_from_name(lp_workgroup())) ) {
415                         DEBUG(0,("winbindd_uid_to_sid: can't find my own domain!\n"));
416                         return WINBINDD_ERROR;
417                 }
418
419                 if ( !winbindd_lookup_sid_by_name(domain, grp->gr_name, &sid, &type) )
420                         return WINBINDD_ERROR;
421                 
422                 if ( type!=SID_NAME_DOM_GRP && type!=SID_NAME_ALIAS )
423                         return WINBINDD_ERROR;
424                 
425                 /* don't fail if we can't store it */
426                 
427                 id.gid = grp->gr_gid;
428                 idmap_set_mapping( &sid, id, ID_GROUPID );
429                 
430                 goto done;
431         }
432
433         /* Lookup sid for this uid */
434         
435         if (!NT_STATUS_IS_OK(idmap_gid_to_sid(&sid, state->request.data.gid))) {
436                 DEBUG(1, ("Could not convert gid %lu to sid\n",
437                           (unsigned long)state->request.data.gid));
438                 return WINBINDD_ERROR;
439         }
440
441 done:
442         /* Construct sid and return it */
443         sid_to_string(state->response.data.sid.sid, &sid);
444         state->response.data.sid.type = SID_NAME_DOM_GRP;
445
446         return WINBINDD_OK;
447 }