r59: revert session key problem
[Samba/gebeck_regimport.git] / source / nsswitch / winbindd_sid.c
blob9fbf47046d651e684d30f14f9083c207abddb24c
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 "includes.h"
24 #include "winbindd.h"
26 #undef DBGC_CLASS
27 #define DBGC_CLASS DBGC_WINBIND
29 /* Convert a string */
31 enum winbindd_result winbindd_lookupsid(struct winbindd_cli_state *state)
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;
40 /* Ensure null termination */
41 state->request.data.sid[sizeof(state->request.data.sid)-1]='\0';
43 DEBUG(3, ("[%5lu]: lookupsid %s\n", (unsigned long)state->pid,
44 state->request.data.sid));
46 /* Lookup sid from PDC using lsa_lookup_sids() */
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;
53 /* Don't look up BUILTIN sids */
55 sid_copy(&tmp_sid, &sid);
56 sid_split_rid(&tmp_sid, &rid);
58 if (sid_equal(&tmp_sid, &global_sid_Builtin)) {
59 return WINBINDD_ERROR;
62 /* Lookup the sid */
64 if (!winbindd_lookup_name_by_sid(&sid, dom_name, name, &type)) {
65 return WINBINDD_ERROR;
68 fstrcpy(state->response.data.name.dom_name, dom_name);
69 fstrcpy(state->response.data.name.name, name);
71 state->response.data.name.type = type;
73 return WINBINDD_OK;
77 /**
78 * Look up the SID for a qualified name.
79 **/
80 enum winbindd_result winbindd_lookupname(struct winbindd_cli_state *state)
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;
89 /* Ensure null termination */
90 state->request.data.sid[sizeof(state->request.data.name.dom_name)-1]='\0';
92 /* Ensure null termination */
93 state->request.data.sid[sizeof(state->request.data.name.name)-1]='\0';
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;
106 DEBUG(3, ("[%5lu]: lookupname %s%s%s\n", (unsigned long)state->pid,
107 name_domain, lp_winbind_separator(), name_user));
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;
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;
120 sid_to_string(sid_str, &sid);
121 fstrcpy(state->response.data.sid.sid, sid_str);
122 state->response.data.sid.type = type;
124 return WINBINDD_OK;
127 /* Convert a sid to a uid. We assume we only have one rid attached to the
128 sid. */
130 enum winbindd_result winbindd_sid_to_uid(struct winbindd_cli_state *state)
132 DOM_SID sid;
133 uint32 flags = 0x0;
135 /* Ensure null termination */
136 state->request.data.sid[sizeof(state->request.data.sid)-1]='\0';
138 DEBUG(3, ("[%5lu]: sid to uid %s\n", (unsigned long)state->pid,
139 state->request.data.sid));
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;
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 */
150 if ( lp_winbind_trusted_domains_only() ) {
151 struct winbindd_domain *domain = NULL;
152 DOM_SID sid2;
153 uint32 rid;
155 domain = find_our_domain();
156 if ( !domain ) {
157 DEBUG(0,("winbindd_sid_to_uid: can't find my own domain!\n"));
158 return WINBINDD_ERROR;
161 sid_copy( &sid2, &sid );
162 sid_split_rid( &sid2, &rid );
164 if ( sid_equal( &sid2, &domain->sid ) ) {
166 fstring domain_name;
167 fstring user;
168 enum SID_NAME_USE type;
169 struct passwd *pw = NULL;
170 unid_t id;
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().*/
178 /* But first check and see if we don't already have a mapping */
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;
184 /* now fall back to the hard way */
186 if ( !winbindd_lookup_name_by_sid(&sid, domain_name, user, &type) )
187 return WINBINDD_ERROR;
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;
195 state->response.data.uid = pw->pw_uid;
197 id.uid = pw->pw_uid;
198 idmap_set_mapping( &sid, id, ID_USERID );
200 return WINBINDD_OK;
205 if ( state->request.flags & WBFLAG_QUERY_ONLY )
206 flags = ID_QUERY_ONLY;
208 /* Find uid for this sid and return it */
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;
215 return WINBINDD_OK;
218 /* Convert a sid to a gid. We assume we only have one rid attached to the
219 sid.*/
221 enum winbindd_result winbindd_sid_to_gid(struct winbindd_cli_state *state)
223 DOM_SID sid;
224 uint32 flags = 0x0;
226 /* Ensure null termination */
227 state->request.data.sid[sizeof(state->request.data.sid)-1]='\0';
229 DEBUG(3, ("[%5lu]: sid to gid %s\n", (unsigned long)state->pid,
230 state->request.data.sid));
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;
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 */
241 if ( lp_winbind_trusted_domains_only() ) {
242 struct winbindd_domain *domain = NULL;
243 DOM_SID sid2;
244 uint32 rid;
245 unid_t id;
247 domain = find_our_domain();
248 if ( !domain ) {
249 DEBUG(0,("winbindd_sid_to_uid: can't find my own domain!\n"));
250 return WINBINDD_ERROR;
253 sid_copy( &sid2, &sid );
254 sid_split_rid( &sid2, &rid );
256 if ( sid_equal( &sid2, &domain->sid ) ) {
258 fstring domain_name;
259 fstring group;
260 enum SID_NAME_USE type;
261 struct group *grp = NULL;
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().*/
268 /* But first check and see if we don't already have a mapping */
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;
274 /* now fall back to the hard way */
276 if ( !winbindd_lookup_name_by_sid(&sid, domain_name, group, &type) )
277 return WINBINDD_ERROR;
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;
285 state->response.data.gid = grp->gr_gid;
287 id.gid = grp->gr_gid;
288 idmap_set_mapping( &sid, id, ID_GROUPID );
290 return WINBINDD_OK;
295 if ( state->request.flags & WBFLAG_QUERY_ONLY )
296 flags = ID_QUERY_ONLY;
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;
304 return WINBINDD_OK;
307 /* Convert a uid to a sid */
309 enum winbindd_result winbindd_uid_to_sid(struct winbindd_cli_state *state)
311 DOM_SID sid;
313 DEBUG(3, ("[%5lu]: uid to sid %lu\n", (unsigned long)state->pid,
314 (unsigned long)state->request.data.uid));
316 if ( (state->request.data.uid < server_state.uid_low )
317 || (state->request.data.uid > server_state.uid_high) )
319 struct passwd *pw = NULL;
320 enum SID_NAME_USE type;
321 unid_t id;
322 struct winbindd_domain *domain;
324 /* SPECIAL CASE FOR MEMBERS OF SAMBA DOMAINS */
326 /* if we don't trust /etc/password then when can't know
327 anything about this uid */
329 if ( !lp_winbind_trusted_domains_only() )
330 return WINBINDD_ERROR;
333 /* look for an idmap entry first */
335 if ( NT_STATUS_IS_OK(idmap_uid_to_sid(&sid, state->request.data.uid)) )
336 goto done;
338 /* if users exist in /etc/passwd, we should try to
339 use that uid. Get the username and the lookup the SID */
341 if ( !(pw = getpwuid(state->request.data.uid)) )
342 return WINBINDD_ERROR;
344 if ( !(domain = find_our_domain()) ) {
345 DEBUG(0,("winbindd_uid_to_sid: can't find my own domain!\n"));
346 return WINBINDD_ERROR;
349 if ( !winbindd_lookup_sid_by_name(domain, pw->pw_name, &sid, &type) )
350 return WINBINDD_ERROR;
352 if ( type != SID_NAME_USER )
353 return WINBINDD_ERROR;
355 /* don't fail if we can't store it */
357 id.uid = pw->pw_uid;
358 idmap_set_mapping( &sid, id, ID_USERID );
360 goto done;
363 /* Lookup rid for this uid */
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;
371 done:
372 sid_to_string(state->response.data.sid.sid, &sid);
373 state->response.data.sid.type = SID_NAME_USER;
375 return WINBINDD_OK;
378 /* Convert a gid to a sid */
380 enum winbindd_result winbindd_gid_to_sid(struct winbindd_cli_state *state)
382 DOM_SID sid;
384 DEBUG(3, ("[%5lu]: gid to sid %lu\n", (unsigned long)state->pid,
385 (unsigned long)state->request.data.gid));
387 if ( (state->request.data.gid < server_state.gid_low)
388 || (state->request.data.gid > server_state.gid_high) )
390 struct group *grp = NULL;
391 enum SID_NAME_USE type;
392 unid_t id;
393 struct winbindd_domain *domain;
395 /* SPECIAL CASE FOR MEMBERS OF SAMBA DOMAINS */
397 /* if we don't trust /etc/group then when can't know
398 anything about this gid */
400 if ( !lp_winbind_trusted_domains_only() )
401 return WINBINDD_ERROR;
403 /* look for an idmap entry first */
405 if ( NT_STATUS_IS_OK(idmap_gid_to_sid(&sid, state->request.data.gid)) )
406 goto done;
408 /* if users exist in /etc/group, we should try to
409 use that gid. Get the username and the lookup the SID */
411 if ( !(grp = getgrgid(state->request.data.gid)) )
412 return WINBINDD_ERROR;
414 if ( !(domain = find_our_domain()) ) {
415 DEBUG(0,("winbindd_uid_to_sid: can't find my own domain!\n"));
416 return WINBINDD_ERROR;
419 if ( !winbindd_lookup_sid_by_name(domain, grp->gr_name, &sid, &type) )
420 return WINBINDD_ERROR;
422 if ( type!=SID_NAME_DOM_GRP && type!=SID_NAME_ALIAS )
423 return WINBINDD_ERROR;
425 /* don't fail if we can't store it */
427 id.gid = grp->gr_gid;
428 idmap_set_mapping( &sid, id, ID_GROUPID );
430 goto done;
433 /* Lookup sid for this uid */
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;
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;
446 return WINBINDD_OK;