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.
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
;
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
;
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
;
78 * Look up the SID for a qualified name.
80 enum winbindd_result
winbindd_lookupname(struct winbindd_cli_state
*state
)
82 enum SID_NAME_USE type
;
84 char *name_domain
, *name_user
;
86 struct winbindd_domain
*domain
;
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());
99 name_domain
= state
->request
.data
.name
.name
;
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",
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
;
127 /* Convert a sid to a uid. We assume we only have one rid attached to the
130 enum winbindd_result
winbindd_sid_to_uid(struct winbindd_cli_state
*state
)
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
;
155 domain
= find_our_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
) ) {
168 enum SID_NAME_USE type
;
169 struct passwd
*pw
= NULL
;
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
)) )
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
;
198 idmap_set_mapping( &sid
, id
, ID_USERID
);
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
;
218 /* Convert a sid to a gid. We assume we only have one rid attached to the
221 enum winbindd_result
winbindd_sid_to_gid(struct winbindd_cli_state
*state
)
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
;
247 domain
= find_our_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
) ) {
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
)) )
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
);
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
;
307 /* Convert a uid to a sid */
309 enum winbindd_result
winbindd_uid_to_sid(struct winbindd_cli_state
*state
)
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
;
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
)) )
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 */
358 idmap_set_mapping( &sid
, id
, ID_USERID
);
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
;
372 sid_to_string(state
->response
.data
.sid
.sid
, &sid
);
373 state
->response
.data
.sid
.type
= SID_NAME_USER
;
378 /* Convert a gid to a sid */
380 enum winbindd_result
winbindd_gid_to_sid(struct winbindd_cli_state
*state
)
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
;
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
)) )
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
);
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
;
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
;