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