2 Unix SMB/CIFS implementation.
6 Copyright (C) Tim Potter 2000
7 Copyright (C) Andrew Tridgell 2000
9 This library is free software; you can redistribute it and/or
10 modify it under the terms of the GNU Library General Public
11 License as published by the Free Software Foundation; either
12 version 2 of the License, or (at your option) any later version.
14 This library is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 Library General Public License for more details.
19 You should have received a copy of the GNU Library General Public
20 License along with this library; if not, write to the
21 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 Boston, MA 02111-1307, USA.
26 #include "nsswitch/sys_nss.h"
28 NSS_STATUS
winbindd_request(int req_type
,
29 struct winbindd_request
*request
,
30 struct winbindd_response
*response
);
32 static BOOL
parse_domain_user(const char *domuser
, fstring domain
, fstring user
)
34 char *p
= strchr(domuser
,*lp_winbind_separator());
40 fstrcpy(domain
, domuser
);
41 domain
[PTR_DIFF(p
, domuser
)] = 0;
46 /* Call winbindd to convert a name to a sid */
48 BOOL
winbind_lookup_name(const char *dom_name
, const char *name
, DOM_SID
*sid
,
49 enum SID_NAME_USE
*name_type
)
51 struct winbindd_request request
;
52 struct winbindd_response response
;
55 if (!sid
|| !name_type
)
58 /* Send off request */
61 ZERO_STRUCT(response
);
63 if (dom_name
== NULL
) {
64 if (!parse_domain_user(name
, request
.data
.name
.dom_name
, request
.data
.name
.name
))
67 fstrcpy(request
.data
.name
.dom_name
, dom_name
);
68 fstrcpy(request
.data
.name
.name
, name
);
71 if ((result
= winbindd_request(WINBINDD_LOOKUPNAME
, &request
,
72 &response
)) == NSS_STATUS_SUCCESS
) {
73 string_to_sid(sid
, response
.data
.sid
.sid
);
74 *name_type
= (enum SID_NAME_USE
)response
.data
.sid
.type
;
77 return result
== NSS_STATUS_SUCCESS
;
80 /* Call winbindd to convert sid to name */
82 BOOL
winbind_lookup_sid(DOM_SID
*sid
,
83 fstring dom_name
, fstring name
,
84 enum SID_NAME_USE
*name_type
)
86 struct winbindd_request request
;
87 struct winbindd_response response
;
91 /* Initialise request */
94 ZERO_STRUCT(response
);
96 sid_to_string(sid_str
, sid
);
97 fstrcpy(request
.data
.sid
, sid_str
);
101 result
= winbindd_request(WINBINDD_LOOKUPSID
, &request
, &response
);
103 /* Copy out result */
105 if (result
== NSS_STATUS_SUCCESS
) {
106 fstrcpy(dom_name
, response
.data
.name
.dom_name
);
107 fstrcpy(name
, response
.data
.name
.name
);
108 *name_type
= (enum SID_NAME_USE
)response
.data
.name
.type
;
110 DEBUG(10, ("winbind_lookup_sid: SUCCESS: SID %s -> %s %s\n",
111 sid_str
, dom_name
, name
));
114 return (result
== NSS_STATUS_SUCCESS
);
117 /* Call winbindd to convert SID to uid */
119 BOOL
winbind_sid_to_uid(uid_t
*puid
, DOM_SID
*sid
)
121 struct winbindd_request request
;
122 struct winbindd_response response
;
129 /* Initialise request */
131 ZERO_STRUCT(request
);
132 ZERO_STRUCT(response
);
134 sid_to_string(sid_str
, sid
);
135 fstrcpy(request
.data
.sid
, sid_str
);
139 result
= winbindd_request(WINBINDD_SID_TO_UID
, &request
, &response
);
141 /* Copy out result */
143 if (result
== NSS_STATUS_SUCCESS
) {
144 *puid
= response
.data
.uid
;
147 return (result
== NSS_STATUS_SUCCESS
);
150 /* Call winbindd to convert uid to sid */
152 BOOL
winbind_uid_to_sid(DOM_SID
*sid
, uid_t uid
)
154 struct winbindd_request request
;
155 struct winbindd_response response
;
161 /* Initialise request */
163 ZERO_STRUCT(request
);
164 ZERO_STRUCT(response
);
166 request
.data
.uid
= uid
;
170 result
= winbindd_request(WINBINDD_UID_TO_SID
, &request
, &response
);
172 /* Copy out result */
174 if (result
== NSS_STATUS_SUCCESS
) {
175 string_to_sid(sid
, response
.data
.sid
.sid
);
177 sid_copy(sid
, &global_sid_NULL
);
180 return (result
== NSS_STATUS_SUCCESS
);
183 /* Call winbindd to convert SID to gid */
185 BOOL
winbind_sid_to_gid(gid_t
*pgid
, DOM_SID
*sid
)
187 struct winbindd_request request
;
188 struct winbindd_response response
;
195 /* Initialise request */
197 ZERO_STRUCT(request
);
198 ZERO_STRUCT(response
);
200 sid_to_string(sid_str
, sid
);
201 fstrcpy(request
.data
.sid
, sid_str
);
205 result
= winbindd_request(WINBINDD_SID_TO_GID
, &request
, &response
);
207 /* Copy out result */
209 if (result
== NSS_STATUS_SUCCESS
) {
210 *pgid
= response
.data
.gid
;
213 return (result
== NSS_STATUS_SUCCESS
);
216 /* Call winbindd to convert gid to sid */
218 BOOL
winbind_gid_to_sid(DOM_SID
*sid
, gid_t gid
)
220 struct winbindd_request request
;
221 struct winbindd_response response
;
227 /* Initialise request */
229 ZERO_STRUCT(request
);
230 ZERO_STRUCT(response
);
232 request
.data
.gid
= gid
;
236 result
= winbindd_request(WINBINDD_GID_TO_SID
, &request
, &response
);
238 /* Copy out result */
240 if (result
== NSS_STATUS_SUCCESS
) {
241 string_to_sid(sid
, response
.data
.sid
.sid
);
243 sid_copy(sid
, &global_sid_NULL
);
246 return (result
== NSS_STATUS_SUCCESS
);
249 /* Fetch the list of groups a user is a member of from winbindd. This is
250 used by winbind_getgroups. */
252 static int wb_getgroups(const char *user
, gid_t
**groups
)
254 struct winbindd_request request
;
255 struct winbindd_response response
;
260 fstrcpy(request
.data
.username
, user
);
262 ZERO_STRUCT(response
);
264 result
= winbindd_request(WINBINDD_GETGROUPS
, &request
, &response
);
266 if (result
== NSS_STATUS_SUCCESS
) {
268 /* Return group list. Don't forget to free the group list
271 *groups
= (gid_t
*)response
.extra_data
;
272 return response
.data
.num_entries
;
278 /* Call winbindd to initialise group membership. This is necessary for
279 some systems (i.e RH5.2) that do not have an initgroups function as part
280 of the nss extension. In RH5.2 this is implemented using getgrent()
281 which can be amazingly inefficient as well as having problems with
284 int winbind_initgroups(char *user
, gid_t gid
)
286 gid_t
*tgr
, *groups
= NULL
;
289 /* Call normal initgroups if we are a local user */
291 if (!strchr(user
, *lp_winbind_separator())) {
292 return initgroups(user
, gid
);
295 result
= wb_getgroups(user
, &groups
);
297 DEBUG(10,("winbind_getgroups: %s: result = %s\n", user
,
298 result
== -1 ? "FAIL" : "SUCCESS"));
301 int ngroups
= result
, i
;
302 BOOL is_member
= False
;
304 /* Check to see if the passed gid is already in the list */
306 for (i
= 0; i
< ngroups
; i
++) {
307 if (groups
[i
] == gid
) {
312 /* Add group to list if necessary */
315 tgr
= (gid_t
*)Realloc(groups
, sizeof(gid_t
) * ngroups
+ 1);
324 groups
[ngroups
] = gid
;
330 if (sys_setgroups(ngroups
, groups
) == -1) {
338 /* The call failed. Set errno to something so we don't get
339 a bogus value from the last failed system call. */
344 /* Free response data if necessary */
352 /* Return a list of groups the user is a member of. This function is
353 useful for large systems where inverting the group database would be too
354 time consuming. If size is zero, list is not modified and the total
355 number of groups for the user is returned. */
357 int winbind_getgroups(const char *user
, int size
, gid_t
*list
)
359 gid_t
*groups
= NULL
;
363 * Don't do the lookup if the name has no separator _and_ we are not in
364 * 'winbind use default domain' mode.
367 if (!(strchr(user
, *lp_winbind_separator()) || lp_winbind_use_default_domain()))
370 /* Fetch list of groups */
372 result
= wb_getgroups(user
, &groups
);
379 errno
= EINVAL
; /* This is what getgroups() does */
383 /* Copy list of groups across */
385 for (i
= 0; i
< result
; i
++) {
394 /* Utility function. Convert a uid_t to a name if possible. */
396 BOOL
winbind_uidtoname(fstring name
, uid_t uid
)
401 enum SID_NAME_USE name_type
;
403 if (!winbind_uid_to_sid(&sid
, uid
))
405 if (!winbind_lookup_sid(&sid
, dom_name
, user_name
, &name_type
))
408 if (name_type
!= SID_NAME_USER
)
411 slprintf(name
, sizeof(fstring
)-1, "%s%s%s", dom_name
,
412 lp_winbind_separator(), user_name
);
417 /* Utility function. Convert a gid_t to a name if possible. */
419 BOOL
winbind_gidtoname(fstring name
, gid_t gid
)
424 enum SID_NAME_USE name_type
;
426 if (!winbind_gid_to_sid(&sid
, gid
))
428 if (!winbind_lookup_sid(&sid
, dom_name
, group_name
, &name_type
))
431 if (name_type
!= SID_NAME_DOM_GRP
)
434 slprintf(name
, sizeof(fstring
)-1, "%s%s%s", dom_name
,
435 lp_winbind_separator(), group_name
);
440 /* Utility function. Convert a name to a uid_t if possible. */
442 BOOL
winbind_nametouid(uid_t
*puid
, const char *name
)
445 enum SID_NAME_USE name_type
;
447 if (!winbind_lookup_name(NULL
, name
, &sid
, &name_type
))
450 if (name_type
!= SID_NAME_USER
)
453 return winbind_sid_to_uid(puid
, &sid
);
456 /* Utility function. Convert a name to a gid_t if possible. */
458 BOOL
winbind_nametogid(gid_t
*pgid
, const char *gname
)
461 enum SID_NAME_USE name_type
;
463 if (!winbind_lookup_name(NULL
, gname
, &g_sid
, &name_type
))
466 if (name_type
!= SID_NAME_DOM_GRP
)
469 return winbind_sid_to_gid(pgid
, &g_sid
);