2 Unix SMB/CIFS implementation.
3 kerberos locator plugin
4 Copyright (C) Guenther Deschner 2007-2008
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include "nsswitch/winbind_client.h"
21 #include "libwbclient/wbclient.h"
27 #if defined(HAVE_KRB5) && defined(HAVE_KRB5_LOCATE_PLUGIN_H)
29 #include <krb5/locate_plugin.h>
31 #ifndef KRB5_PLUGIN_NO_HANDLE
32 #define KRB5_PLUGIN_NO_HANDLE KRB5_KDC_UNREACH /* Heimdal */
35 static const char *get_service_from_locate_service_type(enum locate_service_type svc
)
38 case locate_service_kdc
:
39 case locate_service_master_kdc
:
41 case locate_service_kadmin
:
42 case locate_service_krb524
:
45 case locate_service_kpasswd
:
55 static const char *locate_service_type_name(enum locate_service_type svc
)
58 case locate_service_kdc
:
59 return "locate_service_kdc";
60 case locate_service_master_kdc
:
61 return "locate_service_master_kdc";
62 case locate_service_kadmin
:
63 return "locate_service_kadmin";
64 case locate_service_krb524
:
65 return "locate_service_krb524";
66 case locate_service_kpasswd
:
67 return "locate_service_kpasswd";
74 static const char *socktype_name(int socktype
)
87 static const char *family_name(int family
)
94 #if defined(HAVE_IPV6)
106 * Check input parameters, return KRB5_PLUGIN_NO_HANDLE for unsupported ones
109 * @param realm string
110 * @param socktype integer
111 * @param family integer
116 static int smb_krb5_locator_lookup_sanity_check(enum locate_service_type svc
,
121 if (!realm
|| strlen(realm
) == 0) {
126 case locate_service_kdc
:
127 case locate_service_master_kdc
:
128 case locate_service_kpasswd
:
130 case locate_service_kadmin
:
131 case locate_service_krb524
:
132 return KRB5_PLUGIN_NO_HANDLE
;
141 #if defined(HAVE_IPV6)
152 case 0: /* Heimdal uses that */
162 * Try to get addrinfo for a given host and call the krb5 callback
165 * @param service string
166 * @param in struct addrinfo hint
167 * @param cbfunc krb5 callback function
168 * @param cbdata void pointer cbdata
170 * @return krb5_error_code.
173 static krb5_error_code
smb_krb5_locator_call_cbfunc(const char *name
,
176 int (*cbfunc
)(void *, int, struct sockaddr
*),
179 struct addrinfo
*out
= NULL
;
185 ret
= getaddrinfo(name
, service
, in
, &out
);
190 if (ret
== EAI_AGAIN
) {
196 fprintf(stderr
, "[%5u]: smb_krb5_locator_lookup: "
197 "getaddrinfo failed: %s (%d)\n",
198 (unsigned int)getpid(), gai_strerror(ret
), ret
);
201 return KRB5_PLUGIN_NO_HANDLE
;
204 ret
= cbfunc(cbdata
, out
->ai_socktype
, out
->ai_addr
);
207 fprintf(stderr
, "[%5u]: smb_krb5_locator_lookup: "
208 "failed to call callback: %s (%d)\n",
209 (unsigned int)getpid(), error_message(ret
), ret
);
218 * PUBLIC INTERFACE: locate init
220 * @param context krb5_context
221 * @param privata_data pointer to private data pointer
223 * @return krb5_error_code.
226 static krb5_error_code
smb_krb5_locator_init(krb5_context context
,
233 * PUBLIC INTERFACE: close locate
235 * @param private_data pointer to private data
240 static void smb_krb5_locator_close(void *private_data
)
246 static bool ask_winbind(const char *realm
, char **dcname
)
249 const char *dc
= NULL
;
250 struct wbcDomainControllerInfoEx
*dc_info
= NULL
;
253 flags
= WBC_LOOKUP_DC_KDC_REQUIRED
|
254 WBC_LOOKUP_DC_IS_DNS_NAME
|
255 WBC_LOOKUP_DC_RETURN_DNS_NAME
|
256 WBC_LOOKUP_DC_IP_REQUIRED
;
258 wbc_status
= wbcLookupDomainControllerEx(realm
, NULL
, NULL
, flags
, &dc_info
);
260 if (!WBC_ERROR_IS_OK(wbc_status
)) {
262 fprintf(stderr
,"[%5u]: smb_krb5_locator_lookup: failed with: %s\n",
263 (unsigned int)getpid(), wbcErrorString(wbc_status
));
268 if (dc_info
->dc_address
) {
269 dc
= dc_info
->dc_address
;
270 if (dc
[0] == '\\') dc
++;
271 if (dc
[0] == '\\') dc
++;
274 if (!dc
&& dc_info
->dc_unc
) {
275 dc
= dc_info
->dc_unc
;
276 if (dc
[0] == '\\') dc
++;
277 if (dc
[0] == '\\') dc
++;
281 wbcFreeMemory(dc_info
);
285 *dcname
= strdup(dc
);
287 wbcFreeMemory(dc_info
);
291 wbcFreeMemory(dc_info
);
296 * PUBLIC INTERFACE: locate lookup
298 * @param private_data pointer to private data
299 * @param svc enum locate_service_type.
300 * @param realm string
301 * @param socktype integer
302 * @param family integer
303 * @param cbfunc callback function to send back entries
304 * @param cbdata void pointer to cbdata
306 * @return krb5_error_code.
309 static krb5_error_code
smb_krb5_locator_lookup(void *private_data
,
310 enum locate_service_type svc
,
314 int (*cbfunc
)(void *, int, struct sockaddr
*),
318 struct addrinfo aihints
;
319 char *kdc_name
= NULL
;
320 const char *service
= get_service_from_locate_service_type(svc
);
322 ZERO_STRUCT(aihints
);
325 fprintf(stderr
,"[%5u]: smb_krb5_locator_lookup: called for '%s' "
327 "socktype: '%s' (%d), family: '%s' (%d)\n",
328 (unsigned int)getpid(), realm
,
329 locate_service_type_name(svc
), svc
,
330 socktype_name(socktype
), socktype
,
331 family_name(family
), family
);
333 ret
= smb_krb5_locator_lookup_sanity_check(svc
, realm
, socktype
,
337 fprintf(stderr
, "[%5u]: smb_krb5_locator_lookup: "
338 "returning ret: %s (%d)\n",
339 (unsigned int)getpid(), error_message(ret
), ret
);
344 if (!winbind_env_set()) {
345 if (!ask_winbind(realm
, &kdc_name
)) {
347 fprintf(stderr
, "[%5u]: smb_krb5_locator_lookup: "
348 "failed to query winbindd\n",
349 (unsigned int)getpid());
354 const char *env
= NULL
;
356 if (asprintf(&var
, "%s_%s",
357 WINBINDD_LOCATOR_KDC_ADDRESS
, realm
) == -1) {
363 fprintf(stderr
, "[%5u]: smb_krb5_locator_lookup: "
364 "failed to get kdc from env %s\n",
365 (unsigned int)getpid(), var
);
372 kdc_name
= strdup(env
);
378 fprintf(stderr
, "[%5u]: smb_krb5_locator_lookup: "
379 "got '%s' for '%s' from winbindd\n", (unsigned int)getpid(),
383 aihints
.ai_family
= family
;
384 aihints
.ai_socktype
= socktype
;
386 ret
= smb_krb5_locator_call_cbfunc(kdc_name
,
395 return KRB5_PLUGIN_NO_HANDLE
;
398 #ifdef HEIMDAL_KRB5_LOCATE_PLUGIN_H
399 #define SMB_KRB5_LOCATOR_SYMBOL_NAME resolve /* Heimdal */
401 #define SMB_KRB5_LOCATOR_SYMBOL_NAME service_locator /* MIT */
404 const krb5plugin_service_locate_ftable SMB_KRB5_LOCATOR_SYMBOL_NAME
= {
406 smb_krb5_locator_init
,
407 smb_krb5_locator_close
,
408 smb_krb5_locator_lookup
,