/* Authors: Sumit Bose Copyright (C) 2009 Red Hat This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ #include #include #include #include #include #include #include #include #include #include #include "providers/krb5/krb5_auth.h" struct sssd_ctx { char *sssd_realm; struct addrinfo *sssd_kdc_addrinfo; }; krb5_error_code sssd_krb5_locator_init(krb5_context context, void **private_data) { struct sssd_ctx *ctx; const char *dummy; int ret; #ifdef KRB5_PLUGIN_DEBUG fprintf(stderr,"sssd_krb5_locator_init called\n"); #endif ctx = calloc(1,sizeof(struct sssd_ctx)); if (ctx == NULL) return ENOMEM; dummy = getenv(SSSD_KRB5_REALM); if (dummy == NULL) goto failed; ctx->sssd_realm = strdup(dummy); if (ctx->sssd_realm == NULL) goto failed; dummy = getenv(SSSD_KRB5_KDC); if (dummy == NULL) goto failed; ret = getaddrinfo(dummy, "kerberos", NULL, &ctx->sssd_kdc_addrinfo); if (ret != 0) { #ifdef KRB5_PLUGIN_DEBUG fprintf(stderr,"getaddrinfo failed [%d][%s].\n", ret, gai_strerror(ret)); if (ret == EAI_SYSTEM) { fprintf(stderr,"getaddrinfo failed [%d][%s].\n", errno, strerror(errno)); } #endif goto failed; } *private_data = ctx; return 0; failed: freeaddrinfo(ctx->sssd_kdc_addrinfo); free(ctx->sssd_realm); free(ctx); private_data = NULL; return EINVAL; } void sssd_krb5_locator_close(void *private_data) { struct sssd_ctx *ctx; #ifdef KRB5_PLUGIN_DEBUG fprintf(stderr,"sssd_krb5_locator_close called\n"); #endif if (private_data == NULL) return; ctx = (struct sssd_ctx *) private_data; freeaddrinfo(ctx->sssd_kdc_addrinfo); free(ctx->sssd_realm); free(ctx); private_data = NULL; return; } krb5_error_code sssd_krb5_locator_lookup(void *private_data, enum locate_service_type svc, const char *realm, int socktype, int family, int (*cbfunc)(void *, int, struct sockaddr *), void *cbdata) { int ret; struct addrinfo *ai; struct sssd_ctx *ctx; char hostip[NI_MAXHOST]; if (private_data == NULL) return KRB5_PLUGIN_NO_HANDLE; ctx = (struct sssd_ctx *) private_data; #ifdef KRB5_PLUGIN_DEBUG fprintf(stderr,"sssd_realm[%s] requested realm[%s] family[%d] " "socktype[%d] locate_service[%d]\n", ctx->sssd_realm, realm, family, socktype, svc); #endif switch (svc) { case locate_service_kdc: case locate_service_master_kdc: case locate_service_kadmin: break; case locate_service_krb524: case locate_service_kpasswd: return KRB5_PLUGIN_NO_HANDLE; default: return EINVAL; } switch (family) { case AF_UNSPEC: case AF_INET: case AF_INET6: break; default: return KRB5_PLUGIN_NO_HANDLE; } switch (socktype) { case SOCK_STREAM: case SOCK_DGRAM: break; default: return EINVAL; } if (strcmp(realm, ctx->sssd_realm) != 0) return KRB5_PLUGIN_NO_HANDLE; for (ai = ctx->sssd_kdc_addrinfo; ai != NULL; ai = ai->ai_next) { #ifdef KRB5_PLUGIN_DEBUG ret = getnameinfo(ai->ai_addr, ai->ai_addrlen, hostip, NI_MAXHOST, NULL, 0, NI_NUMERICHOST); if (ret != 0) { fprintf(stderr,"getnameinfo failed [%d][%s].\n", ret, gai_strerror(ret)); if (ret == EAI_SYSTEM) { fprintf(stderr,"getnameinfo failed [%d][%s].\n", errno, strerror(errno)); } } fprintf(stderr,"addr[%s] family[%d] socktype[%d] - ", hostip, ai->ai_family, ai->ai_socktype); #endif if ((family == AF_UNSPEC || ai->ai_family == family) && ai->ai_socktype == socktype) { ret = cbfunc(cbdata, socktype, ai->ai_addr); #ifdef KRB5_PLUGIN_DEBUG if (ret != 0) { fprintf(stderr,"\ncbfunc failed\n"); } else { fprintf(stderr,"used\n"); } } else { fprintf(stderr," NOT used\n"); #endif } } return 0; } const krb5plugin_service_locate_ftable service_locator = { 0, /* version */ sssd_krb5_locator_init, sssd_krb5_locator_close, sssd_krb5_locator_lookup, };