re-write to use getaddrinfo

git-svn-id: svn://svn.h5l.se/heimdal/trunk/heimdal@7492 ec53bebd-3082-4978-b11e-865c3cabbd6b
This commit is contained in:
Assar Westerlund
1999-12-04 18:02:18 +00:00
parent dd4d1e9499
commit 661312f68b
3 changed files with 136 additions and 128 deletions

View File

@@ -62,52 +62,41 @@ static int kaspecials_flag;
static int static int
open_socket(krb5_context context, const char *hostname) open_socket(krb5_context context, const char *hostname)
{ {
struct hostent *hp = NULL; struct addrinfo *ai, *a;
struct addrinfo hints;
int error; int error;
int af; char portstr[NI_MAXSERV];
char **h;
int port;
#ifdef HAVE_IPV6 memset (&hints, 0, sizeof(hints));
if (hp == NULL) hints.ai_socktype = SOCK_STREAM;
hp = getipnodebyname (hostname, AF_INET6, 0, &error); hints.ai_protocol = IPPROTO_TCP;
#endif
if (hp == NULL)
hp = getipnodebyname (hostname, AF_INET, 0, &error);
if(hp == NULL){ snprintf (portstr, sizeof(portstr),
warnx("%s: %s", hostname, hstrerror(error)); "%u",
ntohs(krb5_getportbyname (context, "hprop", "tcp", HPROP_PORT)));
error = getaddrinfo (hostname, portstr, &hints, &ai);
if (error) {
warnx ("%s: %s", hostname, gai_strerror(error));
return -1; return -1;
} }
port = krb5_getportbyname (context, "hprop", "tcp", HPROP_PORT); for (a = ai; a != NULL; a = a->ai_next) {
af = hp->h_addrtype;
for (h = hp->h_addr_list; *h != NULL; ++h) {
int s; int s;
struct sockaddr_storage sa_ss;
struct sockaddr *sa = (struct sockaddr *)&sa_ss;
s = socket(af, SOCK_STREAM, 0); s = socket (a->ai_family, a->ai_socktype, a->ai_protocol);
if(s < 0){ if (s < 0)
warn("socket"); continue;
freehostent (hp); if (connect (s, a->ai_addr, a->ai_addrlen) < 0) {
return -1;
}
sa->sa_family = af;
socket_set_address_and_port (sa, *h, port);
if (connect (s, sa, socket_sockaddr_size(sa)) < 0) {
warn ("connect(%s)", hostname); warn ("connect(%s)", hostname);
close (s); close (s);
continue; continue;
} }
freehostent (hp); freeaddrinfo (ai);
return s; return s;
} }
freehostent (hp); warnx ("failed to contact %s", hostname);
freeaddrinfo (ai);
return -1; return -1;
} }

View File

@@ -273,26 +273,6 @@ get_cred_cache(krb5_context context,
return ret; return ret;
} }
static kadm5_ret_t
open_socket(struct hostent *hp, short port, int *sock)
{
struct sockaddr_in sin;
int s;
s = socket(AF_INET, SOCK_STREAM, 0);
if(s < 0)
return KADM5_FAILURE;
memset(&sin, 0, sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_port = port;
memcpy(&sin.sin_addr, hp->h_addr, hp->h_length);
if(connect(s, (struct sockaddr*)&sin, sizeof(sin)) < 0){
close(s);
return KADM5_RPC_ERROR;
}
*sock = s;
return 0;
}
static kadm5_ret_t static kadm5_ret_t
kadm5_c_init_with_context(krb5_context context, kadm5_c_init_with_context(krb5_context context,
const char *client_name, const char *client_name,
@@ -311,28 +291,55 @@ kadm5_c_init_with_context(krb5_context context,
krb5_principal server; krb5_principal server;
krb5_ccache cc; krb5_ccache cc;
int s; int s;
struct hostent *hp; struct addrinfo *ai, *a;
struct addrinfo hints;
int error;
char portstr[NI_MAXSERV];
memset (&hints, 0, sizeof(hints));
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
snprintf (portstr, sizeof(portstr), "%u", ctx->kadmind_port);
ret = _kadm5_c_init_context(&ctx, realm_params, context); ret = _kadm5_c_init_context(&ctx, realm_params, context);
if(ret) if(ret)
return ret; return ret;
hp = gethostbyname(ctx->admin_server);
if(hp == NULL) error = getaddrinfo (ctx->admin_server, portstr,
&hints, &ai);
if (error)
return KADM5_BAD_SERVER_NAME; return KADM5_BAD_SERVER_NAME;
ret = open_socket(hp, ctx->kadmind_port, &s); for (a = ai; a != NULL; a = a->ai_next) {
if(ret) int s;
return ret;
s = socket (a->ai_family, a->ai_socktype, a->ai_protocol);
if (s < 0)
continue;
if (connect (s, a->ai_addr, a->ai_addrlen) < 0) {
warn ("connect(%s)", ctx->admin_server);
close (s);
continue;
}
break;
}
if (a == NULL) {
freeaddrinfo (ai);
warnx ("failed to contact %s", ctx->admin_server);
return KADM5_FAILURE;
}
ret = get_cred_cache(context, client_name, service_name, ret = get_cred_cache(context, client_name, service_name,
password, prompter, keytab, ccache, &cc); password, prompter, keytab, ccache, &cc);
if(ret) { if(ret) {
freeaddrinfo (ai);
close(s); close(s);
return ret; return ret;
} }
ret = krb5_parse_name(context, KADM5_ADMIN_SERVICE, &server); ret = krb5_parse_name(context, KADM5_ADMIN_SERVICE, &server);
if(ret) { if(ret) {
freeaddrinfo (ai);
if(ccache == NULL) if(ccache == NULL)
krb5_cc_close(context, cc); krb5_cc_close(context, cc);
close(s); close(s);
@@ -360,14 +367,24 @@ kadm5_c_init_with_context(krb5_context context,
krb5_data_free(&enc_data); krb5_data_free(&enc_data);
} else if(ret == KRB5_SENDAUTH_BADAPPLVERS) { } else if(ret == KRB5_SENDAUTH_BADAPPLVERS) {
close(s); close(s);
ret = open_socket(hp, ctx->kadmind_port, &s);
if(ret) s = socket (a->ai_family, a->ai_socktype, a->ai_protocol);
return ret; if (s < 0) {
freeaddrinfo (ai);
return errno;
}
if (connect (s, a->ai_addr, a->ai_addrlen) < 0) {
freeaddrinfo (ai);
return errno;
}
freeaddrinfo (ai);
ret = krb5_sendauth(context, &ctx->ac, &s, ret = krb5_sendauth(context, &ctx->ac, &s,
KADMIN_OLD_APPL_VERSION, NULL, KADMIN_OLD_APPL_VERSION, NULL,
server, AP_OPTS_MUTUAL_REQUIRED, server, AP_OPTS_MUTUAL_REQUIRED,
NULL, NULL, cc, NULL, NULL, NULL); NULL, NULL, cc, NULL, NULL, NULL);
} }
freeaddrinfo (ai);
if(ret) { if(ret) {
close(s); close(s);
return ret; return ret;

View File

@@ -38,14 +38,16 @@ RCSID("$Id$");
static krb5_error_code static krb5_error_code
get_kdc_address (krb5_context context, get_kdc_address (krb5_context context,
krb5_realm realm, krb5_realm realm,
struct sockaddr *sa, struct addrinfo **ai)
int *sa_size)
{ {
struct addrinfo hints;
krb5_error_code ret; krb5_error_code ret;
struct hostent *hostent;
char **hostlist; char **hostlist;
int port = 0;
char portstr[NI_MAXSERV];
int error;
char *host;
char *dot; char *dot;
char *p;
ret = krb5_get_krb_admin_hst (context, ret = krb5_get_krb_admin_hst (context,
&realm, &realm,
@@ -53,33 +55,26 @@ get_kdc_address (krb5_context context,
if (ret) if (ret)
return ret; return ret;
p = *hostlist; host = *hostlist;
dot = strchr (p, ':'); dot = strchr (host, ':');
if (dot) if (dot != NULL) {
*dot = '\0'; char *end;
#ifdef HAVE_GETHOSTBYNAME2 *dot++ = '\0';
#ifdef HAVE_IPV6 port = strtol (dot, &end, 0);
hostent = gethostbyname2 (p, AF_INET6); }
if (hostent == NULL) if (port == 0)
#endif port = krb5_getportbyname (context, "kpasswd", "udp", KPASSWD_PORT);
hostent = gethostbyname2 (p, AF_INET); snprintf (portstr, sizeof(portstr), "%u", ntohs(port));
#else
hostent = roken_gethostbyname (p); memset (&hints, 0, sizeof(hints));
#endif hints.ai_socktype = SOCK_DGRAM;
hints.ai_protocol = IPPROTO_UDP;
error = getaddrinfo (host, portstr, &hints, ai);
krb5_free_krbhst (context, hostlist); krb5_free_krbhst (context, hostlist);
if (hostent == NULL) return error;
return h_errno; /* XXX */
return krb5_h_addr2sockaddr (hostent->h_addrtype,
hostent->h_addr_list[0],
sa,
sa_size,
krb5_getportbyname (context,
"kpasswd",
"udp",
KPASSWD_PORT));
} }
static krb5_error_code static krb5_error_code
@@ -284,6 +279,8 @@ krb5_change_password (krb5_context context,
krb5_auth_context auth_context = NULL; krb5_auth_context auth_context = NULL;
int sock; int sock;
int i; int i;
struct addrinfo *ai, *a;
struct sockaddr_storage __ss; struct sockaddr_storage __ss;
struct sockaddr *sa = (struct sockaddr *)&__ss; struct sockaddr *sa = (struct sockaddr *)&__ss;
int sa_size = sizeof(__ss); int sa_size = sizeof(__ss);
@@ -292,55 +289,60 @@ krb5_change_password (krb5_context context,
if (ret) if (ret)
return ret; return ret;
ret = get_kdc_address (context, creds->client->realm, sa, &sa_size); ret = get_kdc_address (context, creds->client->realm, &ai);
if (ret) if (ret)
goto out; goto out;
sock = socket (sa->sa_family, SOCK_DGRAM, 0);
if (sock < 0) {
ret = errno;
goto out;
}
krb5_auth_con_setflags (context, auth_context, krb5_auth_con_setflags (context, auth_context,
KRB5_AUTH_CONTEXT_DO_SEQUENCE); KRB5_AUTH_CONTEXT_DO_SEQUENCE);
for (i = 0; i < 5; ++i) { for (a = ai; a != NULL; a = a->ai_next) {
fd_set fdset; sock = socket (a->ai_family, a->ai_socktype, a->ai_protocol);
struct timeval tv; if (sock < 0)
continue;
ret = send_request (context, for (i = 0; i < 5; ++i) {
&auth_context, fd_set fdset;
creds, struct timeval tv;
sock,
sa,
sa_size,
newpw);
if (ret)
goto out;
FD_ZERO(&fdset); ret = send_request (context,
FD_SET(sock, &fdset); &auth_context,
tv.tv_usec = 0; creds,
tv.tv_sec = 1 << i; sock,
sa,
sa_size,
newpw);
if (ret)
goto out;
ret = select (sock + 1, &fdset, NULL, NULL, &tv); FD_ZERO(&fdset);
if (ret < 0 && errno != EINTR) FD_SET(sock, &fdset);
goto out; tv.tv_usec = 0;
if (ret == 1) tv.tv_sec = 1 << i;
ret = select (sock + 1, &fdset, NULL, NULL, &tv);
if (ret < 0 && errno != EINTR)
goto out;
if (ret == 1)
break;
}
if (i == 5) {
ret = KRB5_KDC_UNREACH;
close (sock);
continue;
}
ret = process_reply (context,
auth_context,
sock,
result_code,
result_code_string,
result_string);
close (sock);
if (ret == 0)
break; break;
} }
if (i == 5) { freeaddrinfo (ai);
ret = KRB5_KDC_UNREACH;
goto out;
}
ret = process_reply (context,
auth_context,
sock,
result_code,
result_code_string,
result_string);
out: out:
krb5_auth_con_free (context, auth_context); krb5_auth_con_free (context, auth_context);