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.
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
;
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
;
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
;
77 * Look up the SID for a qualified name.
79 enum winbindd_result
winbindd_lookupname(struct winbindd_cli_state
*state
)
81 enum SID_NAME_USE type
;
83 char *name_domain
, *name_user
;
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",
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
;
119 /* Convert a sid to a uid. We assume we only have one rid attached to the
122 enum winbindd_result
winbindd_sid_to_uid(struct winbindd_cli_state
*state
)
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
;
147 domain
= find_domain_from_name( lp_workgroup() );
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
) ) {
160 enum SID_NAME_USE type
;
161 struct passwd
*pw
= NULL
;
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
)) )
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
;
190 idmap_set_mapping( &sid
, id
, ID_USERID
);
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
;
210 /* Convert a sid to a gid. We assume we only have one rid attached to the
213 enum winbindd_result
winbindd_sid_to_gid(struct winbindd_cli_state
*state
)
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
;
239 domain
= find_domain_from_name( lp_workgroup() );
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
) ) {
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
)) )
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
);
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
;
299 /* Convert a uid to a sid */
301 enum winbindd_result
winbindd_uid_to_sid(struct winbindd_cli_state
*state
)
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
;
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
)) )
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 */
350 idmap_set_mapping( &sid
, id
, ID_USERID
);
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
;
364 sid_to_string(state
->response
.data
.sid
.sid
, &sid
);
365 state
->response
.data
.sid
.type
= SID_NAME_USER
;
370 /* Convert a gid to a sid */
372 enum winbindd_result
winbindd_gid_to_sid(struct winbindd_cli_state
*state
)
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
;
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
)) )
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
);
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
;
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
;