From ea6c190a661170d905ca63e5deae9f0a399f039e Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Sat, 10 Mar 2007 01:28:10 +0100 Subject: login: add IPv6 support This support includes: * non-PAM version supports IPv6 ranges in /etc/usertty * utmp records with IPv6 addresses Based on patch by: Milan Zazrivec Signed-off-by: Karel Zak --- login-utils/login.c | 37 +++++++++++++++++++++++++++---------- 1 file changed, 27 insertions(+), 10 deletions(-) (limited to 'login-utils/login.c') diff --git a/login-utils/login.c b/login-utils/login.c index d5ec1fc09..e3b4f6fd0 100644 --- a/login-utils/login.c +++ b/login-utils/login.c @@ -172,7 +172,8 @@ struct passwd *pwd; #ifdef HAVE_SECURITY_PAM_MISC_H static struct passwd pwdcopy; #endif -char hostaddress[4]; /* used in checktty.c */ +char hostaddress[16]; /* used in checktty.c */ +sa_family_t hostfamily; /* used in checktty.c */ char *hostname; /* idem */ static char *username, *tty_name, *tty_number; static char thishost[100]; @@ -283,7 +284,7 @@ logbtmp(const char *line, const char *username, const char *hostname) { if (hostname) { xstrncpy(ut.ut_host, hostname, sizeof(ut.ut_host)); if (hostaddress[0]) - memcpy(&ut.ut_addr, hostaddress, sizeof(ut.ut_addr)); + memcpy(&ut.ut_addr_v6, hostaddress, sizeof(ut.ut_addr_v6)); } #if HAVE_UPDWTMP /* bad luck for ancient systems */ updwtmp(_PATH_BTMP, &ut); @@ -391,13 +392,29 @@ main(int argc, char **argv) hostname = strdup(optarg); /* strdup: Ambrose C. Li */ { - struct hostent *he = gethostbyname(hostname); - - /* he points to static storage; copy the part we use */ - hostaddress[0] = 0; - if (he && he->h_addr_list && he->h_addr_list[0]) - memcpy(hostaddress, he->h_addr_list[0], - sizeof(hostaddress)); + struct addrinfo hints, *info = NULL; + + memset(&hints, 0, sizeof(hints)); + hints.ai_flags = AI_ADDRCONFIG; + + hostaddress[0] = 0; + + if (getaddrinfo(hostname, NULL, &hints, &info)==0 && info) { + if (info->ai_family == AF_INET) { + struct sockaddr_in *sa = + (struct sockaddr_in *) info->ai_addr; + memcpy(hostaddress, &(sa->sin_addr), + sizeof(sa->sin_addr)); + } + else if (info->ai_family == AF_INET6) { + struct sockaddr_in6 *sa = + (struct sockaddr_in6 *) info->ai_addr; + memcpy(hostaddress, &(sa->sin6_addr), + sizeof(sa->sin6_addr)); + } + hostfamily = info->ai_family; + freeaddrinfo(info); + } } break; @@ -892,7 +909,7 @@ Michael Riepe if (hostname) { xstrncpy(ut.ut_host, hostname, sizeof(ut.ut_host)); if (hostaddress[0]) - memcpy(&ut.ut_addr, hostaddress, sizeof(ut.ut_addr)); + memcpy(&ut.ut_addr_v6, hostaddress, sizeof(ut.ut_addr_v6)); } pututline(&ut); -- cgit v1.2.3-55-g7522