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 enum SID_NAME_USE type
;
38 /* Ensure null termination */
39 state
->request
.data
.sid
[sizeof(state
->request
.data
.sid
)-1]='\0';
41 DEBUG(3, ("[%5lu]: lookupsid %s\n", (unsigned long)state
->pid
,
42 state
->request
.data
.sid
));
44 /* Lookup sid from PDC using lsa_lookup_sids() */
46 if (!string_to_sid(&sid
, state
->request
.data
.sid
)) {
47 DEBUG(5, ("%s not a SID\n", state
->request
.data
.sid
));
48 return WINBINDD_ERROR
;
53 if (!winbindd_lookup_name_by_sid(&sid
, dom_name
, name
, &type
)) {
54 return WINBINDD_ERROR
;
57 fstrcpy(state
->response
.data
.name
.dom_name
, dom_name
);
58 fstrcpy(state
->response
.data
.name
.name
, name
);
60 state
->response
.data
.name
.type
= type
;
67 * Look up the SID for a qualified name.
69 enum winbindd_result
winbindd_lookupname(struct winbindd_cli_state
*state
)
71 enum SID_NAME_USE type
;
73 char *name_domain
, *name_user
;
75 struct winbindd_domain
*domain
;
78 /* Ensure null termination */
79 state
->request
.data
.sid
[sizeof(state
->request
.data
.name
.dom_name
)-1]='\0';
81 /* Ensure null termination */
82 state
->request
.data
.sid
[sizeof(state
->request
.data
.name
.name
)-1]='\0';
84 /* cope with the name being a fully qualified name */
85 p
= strstr(state
->request
.data
.name
.name
, lp_winbind_separator());
88 name_domain
= state
->request
.data
.name
.name
;
91 name_domain
= state
->request
.data
.name
.dom_name
;
92 name_user
= state
->request
.data
.name
.name
;
95 DEBUG(3, ("[%5lu]: lookupname %s%s%s\n", (unsigned long)state
->pid
,
96 name_domain
, lp_winbind_separator(), name_user
));
98 if ((domain
= find_domain_from_name(name_domain
)) == NULL
) {
99 DEBUG(0, ("could not find domain entry for domain %s\n",
101 return WINBINDD_ERROR
;
104 /* Lookup name from PDC using lsa_lookup_names() */
105 if (!winbindd_lookup_sid_by_name(domain
, name_user
, &sid
, &type
)) {
106 return WINBINDD_ERROR
;
109 sid_to_string(sid_str
, &sid
);
110 fstrcpy(state
->response
.data
.sid
.sid
, sid_str
);
111 state
->response
.data
.sid
.type
= type
;
116 /* Convert a sid to a uid. We assume we only have one rid attached to the
119 enum winbindd_result
winbindd_sid_to_uid(struct winbindd_cli_state
*state
)
124 /* Ensure null termination */
125 state
->request
.data
.sid
[sizeof(state
->request
.data
.sid
)-1]='\0';
127 DEBUG(3, ("[%5lu]: sid to uid %s\n", (unsigned long)state
->pid
,
128 state
->request
.data
.sid
));
130 if (!string_to_sid(&sid
, state
->request
.data
.sid
)) {
131 DEBUG(1, ("Could not get convert sid %s from string\n", state
->request
.data
.sid
));
132 return WINBINDD_ERROR
;
135 /* This gets a little tricky. If we assume that usernames are syncd between
136 /etc/passwd and the windows domain (such as a member of a Samba domain),
137 the we need to get the uid from the OS and not alocate one ourselves */
139 if ( lp_winbind_trusted_domains_only() ) {
140 struct winbindd_domain
*domain
= NULL
;
144 domain
= find_our_domain();
146 DEBUG(0,("winbindd_sid_to_uid: can't find my own domain!\n"));
147 return WINBINDD_ERROR
;
150 sid_copy( &sid2
, &sid
);
151 sid_split_rid( &sid2
, &rid
);
153 if ( sid_equal( &sid2
, &domain
->sid
) ) {
157 enum SID_NAME_USE type
;
158 struct passwd
*pw
= NULL
;
161 /* ok...here's we know that we are dealing with our
162 own domain (the one to which we are joined). And
163 we know that there must be a UNIX account for this user.
164 So we lookup the sid and the call getpwnam().*/
167 /* But first check and see if we don't already have a mapping */
169 flags
= ID_QUERY_ONLY
;
170 if ( NT_STATUS_IS_OK(idmap_sid_to_uid(&sid
, &(state
->response
.data
.uid
), flags
)) )
173 /* now fall back to the hard way */
175 if ( !winbindd_lookup_name_by_sid(&sid
, domain_name
, user
, &type
) )
176 return WINBINDD_ERROR
;
178 if ( !(pw
= getpwnam(user
)) ) {
179 DEBUG(0,("winbindd_sid_to_uid: 'winbind trusted domains only' is "
180 "set but this user [%s] doesn't exist!\n", user
));
181 return WINBINDD_ERROR
;
184 state
->response
.data
.uid
= pw
->pw_uid
;
187 idmap_set_mapping( &sid
, id
, ID_USERID
);
194 if ( state
->request
.flags
& WBFLAG_QUERY_ONLY
)
195 flags
= ID_QUERY_ONLY
;
197 /* Find uid for this sid and return it */
199 if ( !NT_STATUS_IS_OK(idmap_sid_to_uid(&sid
, &(state
->response
.data
.uid
), flags
)) ) {
200 DEBUG(1, ("Could not get uid for sid %s\n", state
->request
.data
.sid
));
201 return WINBINDD_ERROR
;
207 /* Convert a sid to a gid. We assume we only have one rid attached to the
210 enum winbindd_result
winbindd_sid_to_gid(struct winbindd_cli_state
*state
)
215 /* Ensure null termination */
216 state
->request
.data
.sid
[sizeof(state
->request
.data
.sid
)-1]='\0';
218 DEBUG(3, ("[%5lu]: sid to gid %s\n", (unsigned long)state
->pid
,
219 state
->request
.data
.sid
));
221 if (!string_to_sid(&sid
, state
->request
.data
.sid
)) {
222 DEBUG(1, ("Could not cvt string to sid %s\n", state
->request
.data
.sid
));
223 return WINBINDD_ERROR
;
226 /* This gets a little tricky. If we assume that usernames are syncd between
227 /etc/passwd and the windows domain (such as a member of a Samba domain),
228 the we need to get the uid from the OS and not alocate one ourselves */
230 if ( lp_winbind_trusted_domains_only() ) {
231 struct winbindd_domain
*domain
= NULL
;
236 domain
= find_our_domain();
238 DEBUG(0,("winbindd_sid_to_uid: can't find my own domain!\n"));
239 return WINBINDD_ERROR
;
242 sid_copy( &sid2
, &sid
);
243 sid_split_rid( &sid2
, &rid
);
245 if ( sid_equal( &sid2
, &domain
->sid
) ) {
249 enum SID_NAME_USE type
;
250 struct group
*grp
= NULL
;
252 /* ok...here's we know that we are dealing with our
253 own domain (the one to which we are joined). And
254 we know that there must be a UNIX account for this group.
255 So we lookup the sid and the call getpwnam().*/
257 /* But first check and see if we don't already have a mapping */
259 flags
= ID_QUERY_ONLY
;
260 if ( NT_STATUS_IS_OK(idmap_sid_to_gid(&sid
, &(state
->response
.data
.gid
), flags
)) )
263 /* now fall back to the hard way */
265 if ( !winbindd_lookup_name_by_sid(&sid
, domain_name
, group
, &type
) )
266 return WINBINDD_ERROR
;
268 if ( !(grp
= getgrnam(group
)) ) {
269 DEBUG(0,("winbindd_sid_to_uid: 'winbind trusted domains only' is "
270 "set but this group [%s] doesn't exist!\n", group
));
271 return WINBINDD_ERROR
;
274 state
->response
.data
.gid
= grp
->gr_gid
;
276 id
.gid
= grp
->gr_gid
;
277 idmap_set_mapping( &sid
, id
, ID_GROUPID
);
284 if ( state
->request
.flags
& WBFLAG_QUERY_ONLY
)
285 flags
= ID_QUERY_ONLY
;
287 /* Find gid for this sid and return it */
288 if ( !NT_STATUS_IS_OK(idmap_sid_to_gid(&sid
, &(state
->response
.data
.gid
), flags
)) ) {
289 DEBUG(1, ("Could not get gid for sid %s\n", state
->request
.data
.sid
));
290 return WINBINDD_ERROR
;
296 /* Convert a uid to a sid */
298 enum winbindd_result
winbindd_uid_to_sid(struct winbindd_cli_state
*state
)
302 DEBUG(3, ("[%5lu]: uid to sid %lu\n", (unsigned long)state
->pid
,
303 (unsigned long)state
->request
.data
.uid
));
305 if ( (state
->request
.data
.uid
< server_state
.uid_low
)
306 || (state
->request
.data
.uid
> server_state
.uid_high
) )
308 struct passwd
*pw
= NULL
;
309 enum SID_NAME_USE type
;
311 struct winbindd_domain
*domain
;
313 /* SPECIAL CASE FOR MEMBERS OF SAMBA DOMAINS */
315 /* if we don't trust /etc/password then when can't know
316 anything about this uid */
318 if ( !lp_winbind_trusted_domains_only() )
319 return WINBINDD_ERROR
;
322 /* look for an idmap entry first */
324 if ( NT_STATUS_IS_OK(idmap_uid_to_sid(&sid
, state
->request
.data
.uid
)) )
327 /* if users exist in /etc/passwd, we should try to
328 use that uid. Get the username and the lookup the SID */
330 if ( !(pw
= getpwuid(state
->request
.data
.uid
)) )
331 return WINBINDD_ERROR
;
333 if ( !(domain
= find_our_domain()) ) {
334 DEBUG(0,("winbindd_uid_to_sid: can't find my own domain!\n"));
335 return WINBINDD_ERROR
;
338 if ( !winbindd_lookup_sid_by_name(domain
, pw
->pw_name
, &sid
, &type
) )
339 return WINBINDD_ERROR
;
341 if ( type
!= SID_NAME_USER
)
342 return WINBINDD_ERROR
;
344 /* don't fail if we can't store it */
347 idmap_set_mapping( &sid
, id
, ID_USERID
);
352 /* Lookup rid for this uid */
354 if (!NT_STATUS_IS_OK(idmap_uid_to_sid(&sid
, state
->request
.data
.uid
))) {
355 DEBUG(1, ("Could not convert uid %lu to rid\n",
356 (unsigned long)state
->request
.data
.uid
));
357 return WINBINDD_ERROR
;
361 sid_to_string(state
->response
.data
.sid
.sid
, &sid
);
362 state
->response
.data
.sid
.type
= SID_NAME_USER
;
367 /* Convert a gid to a sid */
369 enum winbindd_result
winbindd_gid_to_sid(struct winbindd_cli_state
*state
)
373 DEBUG(3, ("[%5lu]: gid to sid %lu\n", (unsigned long)state
->pid
,
374 (unsigned long)state
->request
.data
.gid
));
376 if ( (state
->request
.data
.gid
< server_state
.gid_low
)
377 || (state
->request
.data
.gid
> server_state
.gid_high
) )
379 struct group
*grp
= NULL
;
380 enum SID_NAME_USE type
;
382 struct winbindd_domain
*domain
;
384 /* SPECIAL CASE FOR MEMBERS OF SAMBA DOMAINS */
386 /* if we don't trust /etc/group then when can't know
387 anything about this gid */
389 if ( !lp_winbind_trusted_domains_only() )
390 return WINBINDD_ERROR
;
392 /* look for an idmap entry first */
394 if ( NT_STATUS_IS_OK(idmap_gid_to_sid(&sid
, state
->request
.data
.gid
)) )
397 /* if users exist in /etc/group, we should try to
398 use that gid. Get the username and the lookup the SID */
400 if ( !(grp
= getgrgid(state
->request
.data
.gid
)) )
401 return WINBINDD_ERROR
;
403 if ( !(domain
= find_our_domain()) ) {
404 DEBUG(0,("winbindd_uid_to_sid: can't find my own domain!\n"));
405 return WINBINDD_ERROR
;
408 if ( !winbindd_lookup_sid_by_name(domain
, grp
->gr_name
, &sid
, &type
) )
409 return WINBINDD_ERROR
;
411 if ( type
!=SID_NAME_DOM_GRP
&& type
!=SID_NAME_ALIAS
)
412 return WINBINDD_ERROR
;
414 /* don't fail if we can't store it */
416 id
.gid
= grp
->gr_gid
;
417 idmap_set_mapping( &sid
, id
, ID_GROUPID
);
422 /* Lookup sid for this uid */
424 if (!NT_STATUS_IS_OK(idmap_gid_to_sid(&sid
, state
->request
.data
.gid
))) {
425 DEBUG(1, ("Could not convert gid %lu to sid\n",
426 (unsigned long)state
->request
.data
.gid
));
427 return WINBINDD_ERROR
;
431 /* Construct sid and return it */
432 sid_to_string(state
->response
.data
.sid
.sid
, &sid
);
433 state
->response
.data
.sid
.type
= SID_NAME_DOM_GRP
;
438 enum winbindd_result
winbindd_allocate_rid(struct winbindd_cli_state
*state
)
440 if ( !state
->privileged
) {
441 DEBUG(2, ("winbindd_allocate_rid: non-privileged access "
443 return WINBINDD_ERROR
;
446 /* We tell idmap to always allocate a user RID. There might be a good
447 * reason to keep RID allocation for users to even and groups to
448 * odd. This needs discussion I think. For now only allocate user
451 if (!NT_STATUS_IS_OK(idmap_allocate_rid(&state
->response
.data
.rid
,
453 return WINBINDD_ERROR
;