2 Unix SMB/CIFS implementation.
3 Winbind Utility functions
5 Copyright (C) Gerald (Jerry) Carter 2007
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "../libcli/security/security.h"
23 #include "../lib/util/util_pw.h"
24 #include "nsswitch/libwbclient/wbclient.h"
26 #include "lib/winbind_util.h"
28 #if defined(WITH_WINBIND)
30 struct passwd
* winbind_getpwnam(const char * name
)
33 struct passwd
* tmp_pwd
= NULL
;
34 struct passwd
* pwd
= NULL
;
36 result
= wbcGetpwnam(name
, &tmp_pwd
);
37 if (result
!= WBC_ERR_SUCCESS
)
40 pwd
= tcopy_passwd(talloc_tos(), tmp_pwd
);
42 wbcFreeMemory(tmp_pwd
);
47 struct passwd
* winbind_getpwsid(const struct dom_sid
*sid
)
50 struct passwd
* tmp_pwd
= NULL
;
51 struct passwd
* pwd
= NULL
;
52 struct wbcDomainSid dom_sid
;
54 memcpy(&dom_sid
, sid
, sizeof(dom_sid
));
56 result
= wbcGetpwsid(&dom_sid
, &tmp_pwd
);
57 if (result
!= WBC_ERR_SUCCESS
)
60 pwd
= tcopy_passwd(talloc_tos(), tmp_pwd
);
62 wbcFreeMemory(tmp_pwd
);
67 /* Call winbindd to convert a name to a sid */
69 bool winbind_lookup_name(const char *dom_name
, const char *name
, struct dom_sid
*sid
,
70 enum lsa_SidType
*name_type
)
72 struct wbcDomainSid dom_sid
;
76 result
= wbcLookupName(dom_name
, name
, &dom_sid
, &type
);
77 if (result
!= WBC_ERR_SUCCESS
)
80 memcpy(sid
, &dom_sid
, sizeof(struct dom_sid
));
81 *name_type
= (enum lsa_SidType
)type
;
86 /* Same as winbind_lookup_name(), but returning NTSTATUS instead of bool */
89 NTSTATUS
winbind_lookup_name_ex(const char *dom_name
,
92 enum lsa_SidType
*name_type
)
94 struct wbcDomainSid dom_sid
;
99 result
= wbcLookupName(dom_name
, name
, &dom_sid
, &type
);
101 status
= map_nt_error_from_wbcErr(result
);
102 if (!NT_STATUS_IS_OK(status
)) {
103 if ((lp_security() < SEC_DOMAIN
) &&
104 NT_STATUS_EQUAL(status
, NT_STATUS_SERVER_DISABLED
))
107 * If we're not a domain member and winbind is not
108 * running, treat this as not mapped.
110 status
= NT_STATUS_NONE_MAPPED
;
112 if (!NT_STATUS_EQUAL(status
, NT_STATUS_NONE_MAPPED
)) {
115 *name_type
= SID_NAME_UNKNOWN
;
120 memcpy(sid
, &dom_sid
, sizeof(struct dom_sid
));
121 *name_type
= (enum lsa_SidType
)type
;
126 /* Call winbindd to convert sid to name */
128 bool winbind_lookup_sid(TALLOC_CTX
*mem_ctx
, const struct dom_sid
*sid
,
129 const char **domain
, const char **name
,
130 enum lsa_SidType
*name_type
)
132 struct wbcDomainSid dom_sid
;
134 enum wbcSidType type
;
135 char *domain_name
= NULL
;
136 char *account_name
= NULL
;
137 struct dom_sid_buf buf
;
139 memcpy(&dom_sid
, sid
, sizeof(dom_sid
));
141 result
= wbcLookupSid(&dom_sid
, &domain_name
, &account_name
, &type
);
142 if (result
!= WBC_ERR_SUCCESS
)
145 /* Copy out result */
148 *domain
= talloc_strdup(mem_ctx
, domain_name
);
151 *name
= talloc_strdup(mem_ctx
, account_name
);
153 *name_type
= (enum lsa_SidType
)type
;
155 DEBUG(10, ("winbind_lookup_sid: SUCCESS: SID %s -> %s %s\n",
156 dom_sid_str_buf(sid
, &buf
), domain_name
, account_name
));
158 wbcFreeMemory(domain_name
);
159 wbcFreeMemory(account_name
);
161 if ((domain
&& !*domain
) || (name
&& !*name
)) {
162 DEBUG(0,("winbind_lookup_sid: talloc() failed!\n"));
170 /* Ping winbindd to see it is alive */
172 bool winbind_ping(void)
174 wbcErr result
= wbcPing();
176 return (result
== WBC_ERR_SUCCESS
);
179 /* Call winbindd to convert SID to uid */
181 bool winbind_sid_to_uid(uid_t
*puid
, const struct dom_sid
*sid
)
183 struct wbcDomainSid dom_sid
;
186 memcpy(&dom_sid
, sid
, sizeof(dom_sid
));
188 result
= wbcSidToUid(&dom_sid
, puid
);
190 return (result
== WBC_ERR_SUCCESS
);
193 /* Call winbindd to convert SID to gid */
195 bool winbind_sid_to_gid(gid_t
*pgid
, const struct dom_sid
*sid
)
197 struct wbcDomainSid dom_sid
;
200 memcpy(&dom_sid
, sid
, sizeof(dom_sid
));
202 result
= wbcSidToGid(&dom_sid
, pgid
);
204 return (result
== WBC_ERR_SUCCESS
);
207 bool winbind_xid_to_sid(struct dom_sid
*sid
, const struct unixid
*xid
)
209 struct wbcUnixId wbc_xid
;
210 struct wbcDomainSid dom_sid
;
215 wbc_xid
= (struct wbcUnixId
) {
216 .type
= WBC_ID_TYPE_UID
, .id
.uid
= xid
->id
220 wbc_xid
= (struct wbcUnixId
) {
221 .type
= WBC_ID_TYPE_GID
, .id
.gid
= xid
->id
228 result
= wbcUnixIdsToSids(&wbc_xid
, 1, &dom_sid
);
229 if (result
!= WBC_ERR_SUCCESS
) {
233 memcpy(sid
, &dom_sid
, sizeof(struct dom_sid
));
237 /* Check for a trusted domain */
239 wbcErr
wb_is_trusted_domain(const char *domain
)
242 struct wbcDomainInfo
*info
= NULL
;
244 result
= wbcDomainInfo(domain
, &info
);
246 if (WBC_ERROR_IS_OK(result
)) {
253 /* Lookup a set of rids in a given domain */
255 bool winbind_lookup_rids(TALLOC_CTX
*mem_ctx
,
256 const struct dom_sid
*domain_sid
,
257 int num_rids
, uint32_t *rids
,
258 const char **domain_name
,
259 const char ***names
, enum lsa_SidType
**types
)
261 const char *dom_name
= NULL
;
262 const char **namelist
= NULL
;
263 enum wbcSidType
*name_types
= NULL
;
264 struct wbcDomainSid dom_sid
;
268 memcpy(&dom_sid
, domain_sid
, sizeof(struct wbcDomainSid
));
270 ret
= wbcLookupRids(&dom_sid
, num_rids
, rids
,
271 &dom_name
, &namelist
, &name_types
);
272 if (ret
!= WBC_ERR_SUCCESS
) {
276 *domain_name
= talloc_strdup(mem_ctx
, dom_name
);
277 *names
= talloc_array(mem_ctx
, const char*, num_rids
);
278 *types
= talloc_array(mem_ctx
, enum lsa_SidType
, num_rids
);
280 for(i
=0; i
<num_rids
; i
++) {
281 (*names
)[i
] = talloc_strdup(*names
, namelist
[i
]);
282 (*types
)[i
] = (enum lsa_SidType
)name_types
[i
];
285 wbcFreeMemory(discard_const_p(char, dom_name
));
286 wbcFreeMemory(namelist
);
287 wbcFreeMemory(name_types
);
292 /* Ask Winbind to allocate a new uid for us */
294 bool winbind_allocate_uid(uid_t
*uid
)
298 ret
= wbcAllocateUid(uid
);
300 return (ret
== WBC_ERR_SUCCESS
);
303 /* Ask Winbind to allocate a new gid for us */
305 bool winbind_allocate_gid(gid_t
*gid
)
309 ret
= wbcAllocateGid(gid
);
311 return (ret
== WBC_ERR_SUCCESS
);
314 bool winbind_lookup_usersids(TALLOC_CTX
*mem_ctx
,
315 const struct dom_sid
*user_sid
,
316 uint32_t *p_num_sids
,
317 struct dom_sid
**p_sids
)
320 struct wbcDomainSid dom_sid
;
321 struct wbcDomainSid
*sid_list
= NULL
;
324 memcpy(&dom_sid
, user_sid
, sizeof(dom_sid
));
326 ret
= wbcLookupUserSids(&dom_sid
,
330 if (ret
!= WBC_ERR_SUCCESS
) {
334 *p_sids
= talloc_array(mem_ctx
, struct dom_sid
, num_sids
);
335 if (*p_sids
== NULL
) {
336 wbcFreeMemory(sid_list
);
340 memcpy(*p_sids
, sid_list
, sizeof(dom_sid
) * num_sids
);
342 *p_num_sids
= num_sids
;
343 wbcFreeMemory(sid_list
);
348 #else /* WITH_WINBIND */
350 struct passwd
* winbind_getpwnam(const char * name
)
355 struct passwd
* winbind_getpwsid(const struct dom_sid
*sid
)
360 bool winbind_lookup_name(const char *dom_name
, const char *name
, struct dom_sid
*sid
,
361 enum lsa_SidType
*name_type
)
367 NTSTATUS
winbind_lookup_name_ex(const char *dom_name
,
370 enum lsa_SidType
*name_type
)
372 *name_type
= SID_NAME_UNKNOWN
;
377 /* Call winbindd to convert sid to name */
379 bool winbind_lookup_sid(TALLOC_CTX
*mem_ctx
, const struct dom_sid
*sid
,
380 const char **domain
, const char **name
,
381 enum lsa_SidType
*name_type
)
386 /* Ping winbindd to see it is alive */
388 bool winbind_ping(void)
393 /* Call winbindd to convert SID to uid */
395 bool winbind_sid_to_uid(uid_t
*puid
, const struct dom_sid
*sid
)
400 /* Call winbindd to convert SID to gid */
402 bool winbind_sid_to_gid(gid_t
*pgid
, const struct dom_sid
*sid
)
407 /* Call winbindd to convert uid or gid to SID */
409 bool winbind_xid_to_sid(struct dom_sid
*sid
, const struct unixid
*xid
)
414 /* Check for a trusted domain */
416 wbcErr
wb_is_trusted_domain(const char *domain
)
418 return WBC_ERR_UNKNOWN_FAILURE
;
421 /* Lookup a set of rids in a given domain */
423 bool winbind_lookup_rids(TALLOC_CTX
*mem_ctx
,
424 const struct dom_sid
*domain_sid
,
425 int num_rids
, uint32_t *rids
,
426 const char **domain_name
,
427 const char ***names
, enum lsa_SidType
**types
)
432 /* Ask Winbind to allocate a new uid for us */
434 bool winbind_allocate_uid(uid_t
*uid
)
439 /* Ask Winbind to allocate a new gid for us */
441 bool winbind_allocate_gid(gid_t
*gid
)
446 bool winbind_lookup_usersids(TALLOC_CTX
*mem_ctx
,
447 const struct dom_sid
*user_sid
,
448 uint32_t *p_num_sids
,
449 struct dom_sid
**p_sids
)
454 #endif /* WITH_WINBIND */