sync 3.0 into HEAD for the last time
[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         /* Split sid into domain sid and user rid */
134         if (!string_to_sid(&sid, state->request.data.sid)) {
135                 DEBUG(1, ("Could not get convert sid %s from string\n", state->request.data.sid));
136                 return WINBINDD_ERROR;
137         }
138         
139         if ( state->request.flags & WBFLAG_QUERY_ONLY ) 
140                 flags = ID_QUERY_ONLY;
141         
142         /* Find uid for this sid and return it */
143         if ( !NT_STATUS_IS_OK(idmap_sid_to_uid(&sid, &(state->response.data.uid), flags)) ) {
144                 DEBUG(1, ("Could not get uid for sid %s\n", state->request.data.sid));
145                 return WINBINDD_ERROR;
146         }
147
148         return WINBINDD_OK;
149 }
150
151 /* Convert a sid to a gid.  We assume we only have one rid attached to the
152    sid.*/
153
154 enum winbindd_result winbindd_sid_to_gid(struct winbindd_cli_state *state)
155 {
156         DOM_SID sid;
157         uint32 flags = 0x0;
158
159         /* Ensure null termination */
160         state->request.data.sid[sizeof(state->request.data.sid)-1]='\0';
161
162         DEBUG(3, ("[%5lu]: sid to gid %s\n", (unsigned long)state->pid, 
163                   state->request.data.sid));
164
165         if (!string_to_sid(&sid, state->request.data.sid)) {
166                 DEBUG(1, ("Could not cvt string to sid %s\n", state->request.data.sid));
167                 return WINBINDD_ERROR;
168         }
169
170         if ( state->request.flags & WBFLAG_QUERY_ONLY ) 
171                 flags = ID_QUERY_ONLY;
172                 
173         /* Find gid for this sid and return it */
174         if ( !NT_STATUS_IS_OK(idmap_sid_to_gid(&sid, &(state->response.data.gid), flags)) ) {
175                 DEBUG(1, ("Could not get gid for sid %s\n", state->request.data.sid));
176                 return WINBINDD_ERROR;
177         }
178
179         return WINBINDD_OK;
180 }
181
182 /* Convert a uid to a sid */
183
184 enum winbindd_result winbindd_uid_to_sid(struct winbindd_cli_state *state)
185 {
186         DOM_SID sid;
187
188 #if 0   /* JERRY */
189         /* we cannot do this check this anymore since a domain member of 
190            a Samba domain may share unix accounts via NIS or LDAP.  In this 
191            case the uid/gid will be out of winbindd's range but still might
192            be resolved to a SID via an ldap idmap backend */
193            
194         if ((state->request.data.uid < server_state.uid_low ) ||
195             (state->request.data.uid > server_state.uid_high)) {
196                 return WINBINDD_ERROR;
197         }
198 #endif
199
200         DEBUG(3, ("[%5lu]: uid to sid %lu\n", (unsigned long)state->pid, 
201                   (unsigned long)state->request.data.uid));
202
203         /* Lookup rid for this uid */
204         if (!NT_STATUS_IS_OK(idmap_uid_to_sid(&sid, state->request.data.uid))) {
205                 DEBUG(1, ("Could not convert uid %lu to rid\n",
206                           (unsigned long)state->request.data.uid));
207                 return WINBINDD_ERROR;
208         }
209
210         sid_to_string(state->response.data.sid.sid, &sid);
211         state->response.data.sid.type = SID_NAME_USER;
212
213         return WINBINDD_OK;
214 }
215
216 /* Convert a gid to a sid */
217
218 enum winbindd_result winbindd_gid_to_sid(struct winbindd_cli_state *state)
219 {
220         DOM_SID sid;
221
222 #if 0   /* JERRY */
223         /* we cannot do this check this anymore since a domain member of 
224            a Samba domain may share unix accounts via NIS or LDAP.  In this 
225            case the uid/gid will be out of winbindd's range but still might
226            be resolved to a SID via an ldap idmap backend */
227            
228         if ((state->request.data.gid < server_state.gid_low) ||
229             (state->request.data.gid > server_state.gid_high)) {
230                 return WINBINDD_ERROR;
231         }
232 #endif
233
234         DEBUG(3, ("[%5lu]: gid to sid %lu\n", (unsigned long)state->pid,
235                   (unsigned long)state->request.data.gid));
236
237         /* Lookup sid for this uid */
238         if (!NT_STATUS_IS_OK(idmap_gid_to_sid(&sid, state->request.data.gid))) {
239                 DEBUG(1, ("Could not convert gid %lu to sid\n",
240                           (unsigned long)state->request.data.gid));
241                 return WINBINDD_ERROR;
242         }
243
244         /* Construct sid and return it */
245         sid_to_string(state->response.data.sid.sid, &sid);
246         state->response.data.sid.type = SID_NAME_DOM_GRP;
247
248         return WINBINDD_OK;
249 }