fix for bug 680 (heads up). This gist is to map the
[Samba.git] / source / nsswitch / winbindd_sid.c
blob0faeec56369bd025e1ba8e980623c06d900335ee
1 /*
2 Unix SMB/CIFS implementation.
4 Winbind daemon - sid related functions
6 Copyright (C) Tim Potter 2000
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.
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.
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.
23 #include "winbindd.h"
25 #undef DBGC_CLASS
26 #define DBGC_CLASS DBGC_WINBIND
28 /* Convert a string */
30 enum winbindd_result winbindd_lookupsid(struct winbindd_cli_state *state)
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;
39 /* Ensure null termination */
40 state->request.data.sid[sizeof(state->request.data.sid)-1]='\0';
42 DEBUG(3, ("[%5lu]: lookupsid %s\n", (unsigned long)state->pid,
43 state->request.data.sid));
45 /* Lookup sid from PDC using lsa_lookup_sids() */
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;
52 /* Don't look up BUILTIN sids */
54 sid_copy(&tmp_sid, &sid);
55 sid_split_rid(&tmp_sid, &rid);
57 if (sid_equal(&tmp_sid, &global_sid_Builtin)) {
58 return WINBINDD_ERROR;
61 /* Lookup the sid */
63 if (!winbindd_lookup_name_by_sid(&sid, dom_name, name, &type)) {
64 return WINBINDD_ERROR;
67 fstrcpy(state->response.data.name.dom_name, dom_name);
68 fstrcpy(state->response.data.name.name, name);
70 state->response.data.name.type = type;
72 return WINBINDD_OK;
76 /**
77 * Look up the SID for a qualified name.
78 **/
79 enum winbindd_result winbindd_lookupname(struct winbindd_cli_state *state)
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;
87 /* Ensure null termination */
88 state->request.data.sid[sizeof(state->request.data.name.dom_name)-1]='\0';
90 /* Ensure null termination */
91 state->request.data.sid[sizeof(state->request.data.name.name)-1]='\0';
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));
98 name_domain = state->request.data.name.dom_name;
99 name_user = state->request.data.name.name;
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;
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;
112 sid_to_string(sid_str, &sid);
113 fstrcpy(state->response.data.sid.sid, sid_str);
114 state->response.data.sid.type = type;
116 return WINBINDD_OK;
119 /* Convert a sid to a uid. We assume we only have one rid attached to the
120 sid. */
122 enum winbindd_result winbindd_sid_to_uid(struct winbindd_cli_state *state)
124 DOM_SID sid;
125 uint32 flags = 0x0;
127 /* Ensure null termination */
128 state->request.data.sid[sizeof(state->request.data.sid)-1]='\0';
130 DEBUG(3, ("[%5lu]: sid to uid %s\n", (unsigned long)state->pid,
131 state->request.data.sid));
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;
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 */
142 if ( lp_winbind_trusted_domains_only() ) {
143 struct winbindd_domain *domain = NULL;
144 DOM_SID sid2;
145 uint32 rid;
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;
153 sid_copy( &sid2, &sid );
154 sid_split_rid( &sid2, &rid );
156 if ( sid_equal( &sid2, &domain->sid ) ) {
158 fstring domain_name;
159 fstring user;
160 enum SID_NAME_USE type;
161 struct passwd *pw = NULL;
162 unid_t id;
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().*/
170 /* But first check and see if we don't already have a mapping */
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;
176 /* now fall back to the hard way */
178 if ( !winbindd_lookup_name_by_sid(&sid, domain_name, user, &type) )
179 return WINBINDD_ERROR;
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;
187 state->response.data.uid = pw->pw_uid;
189 id.uid = pw->pw_uid;
190 idmap_set_mapping( &sid, id, ID_USERID );
192 return WINBINDD_OK;
197 if ( state->request.flags & WBFLAG_QUERY_ONLY )
198 flags = ID_QUERY_ONLY;
200 /* Find uid for this sid and return it */
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;
207 return WINBINDD_OK;
210 /* Convert a sid to a gid. We assume we only have one rid attached to the
211 sid.*/
213 enum winbindd_result winbindd_sid_to_gid(struct winbindd_cli_state *state)
215 DOM_SID sid;
216 uint32 flags = 0x0;
218 /* Ensure null termination */
219 state->request.data.sid[sizeof(state->request.data.sid)-1]='\0';
221 DEBUG(3, ("[%5lu]: sid to gid %s\n", (unsigned long)state->pid,
222 state->request.data.sid));
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;
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 */
233 if ( lp_winbind_trusted_domains_only() ) {
234 struct winbindd_domain *domain = NULL;
235 DOM_SID sid2;
236 uint32 rid;
237 unid_t id;
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;
245 sid_copy( &sid2, &sid );
246 sid_split_rid( &sid2, &rid );
248 if ( sid_equal( &sid2, &domain->sid ) ) {
250 fstring domain_name;
251 fstring group;
252 enum SID_NAME_USE type;
253 struct group *grp = NULL;
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().*/
260 /* But first check and see if we don't already have a mapping */
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;
266 /* now fall back to the hard way */
268 if ( !winbindd_lookup_name_by_sid(&sid, domain_name, group, &type) )
269 return WINBINDD_ERROR;
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;
277 state->response.data.gid = grp->gr_gid;
279 id.gid = grp->gr_gid;
280 idmap_set_mapping( &sid, id, ID_GROUPID );
282 return WINBINDD_OK;
287 if ( state->request.flags & WBFLAG_QUERY_ONLY )
288 flags = ID_QUERY_ONLY;
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;
296 return WINBINDD_OK;
299 /* Convert a uid to a sid */
301 enum winbindd_result winbindd_uid_to_sid(struct winbindd_cli_state *state)
303 DOM_SID sid;
305 DEBUG(3, ("[%5lu]: uid to sid %lu\n", (unsigned long)state->pid,
306 (unsigned long)state->request.data.uid));
308 if ( (state->request.data.uid < server_state.uid_low )
309 || (state->request.data.uid > server_state.uid_high) )
311 struct passwd *pw = NULL;
312 enum SID_NAME_USE type;
313 unid_t id;
314 struct winbindd_domain *domain;
316 /* SPECIAL CASE FOR MEMBERS OF SAMBA DOMAINS */
318 /* if we don't trust /etc/password then when can't know
319 anything about this uid */
321 if ( !lp_winbind_trusted_domains_only() )
322 return WINBINDD_ERROR;
325 /* look for an idmap entry first */
327 if ( NT_STATUS_IS_OK(idmap_uid_to_sid(&sid, state->request.data.uid)) )
328 goto done;
330 /* if users exist in /etc/passwd, we should try to
331 use that uid. Get the username and the lookup the SID */
333 if ( !(pw = getpwuid(state->request.data.uid)) )
334 return WINBINDD_ERROR;
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;
341 if ( !winbindd_lookup_sid_by_name(domain, pw->pw_name, &sid, &type) )
342 return WINBINDD_ERROR;
344 if ( type != SID_NAME_USER )
345 return WINBINDD_ERROR;
347 /* don't fail if we can't store it */
349 id.uid = pw->pw_uid;
350 idmap_set_mapping( &sid, id, ID_USERID );
352 goto done;
355 /* Lookup rid for this uid */
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;
363 done:
364 sid_to_string(state->response.data.sid.sid, &sid);
365 state->response.data.sid.type = SID_NAME_USER;
367 return WINBINDD_OK;
370 /* Convert a gid to a sid */
372 enum winbindd_result winbindd_gid_to_sid(struct winbindd_cli_state *state)
374 DOM_SID sid;
376 DEBUG(3, ("[%5lu]: gid to sid %lu\n", (unsigned long)state->pid,
377 (unsigned long)state->request.data.gid));
379 if ( (state->request.data.gid < server_state.gid_low)
380 || (state->request.data.gid > server_state.gid_high) )
382 struct group *grp = NULL;
383 enum SID_NAME_USE type;
384 unid_t id;
385 struct winbindd_domain *domain;
387 /* SPECIAL CASE FOR MEMBERS OF SAMBA DOMAINS */
389 /* if we don't trust /etc/group then when can't know
390 anything about this gid */
392 if ( !lp_winbind_trusted_domains_only() )
393 return WINBINDD_ERROR;
395 /* look for an idmap entry first */
397 if ( NT_STATUS_IS_OK(idmap_gid_to_sid(&sid, state->request.data.gid)) )
398 goto done;
400 /* if users exist in /etc/group, we should try to
401 use that gid. Get the username and the lookup the SID */
403 if ( !(grp = getgrgid(state->request.data.gid)) )
404 return WINBINDD_ERROR;
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;
411 if ( !winbindd_lookup_sid_by_name(domain, grp->gr_name, &sid, &type) )
412 return WINBINDD_ERROR;
414 if ( type!=SID_NAME_DOM_GRP && type!=SID_NAME_ALIAS )
415 return WINBINDD_ERROR;
417 /* don't fail if we can't store it */
419 id.gid = grp->gr_gid;
420 idmap_set_mapping( &sid, id, ID_GROUPID );
422 goto done;
425 /* Lookup sid for this uid */
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;
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;
438 return WINBINDD_OK;