try to write a useful string as host in utmp, using the same algoritm

as telnetd


git-svn-id: svn://svn.h5l.se/heimdal/trunk/heimdal@9661 ec53bebd-3082-4978-b11e-865c3cabbd6b
This commit is contained in:
Assar Westerlund
2001-02-08 16:08:47 +00:00
parent c0ff298e15
commit f63c97442d
2 changed files with 45 additions and 3 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright (c) 1995, 1996, 1997, 1999 Kungliga Tekniska H<>gskolan
* Copyright (c) 1995 - 2001 Kungliga Tekniska H<>gskolan
* (Royal Institute of Technology, Stockholm, Sweden).
* All rights reserved.
*
@@ -35,6 +35,48 @@
RCSID("$Id$");
/* try to put something useful from hostname into dst, dst_sz:
* full name, first component or address */
void
shrink_hostname (const char *hostname,
char *dst, size_t dst_sz)
{
char local_hostname[MaxHostNameLen];
char *ld, *hd;
int ret;
struct addrinfo *ai;
if (strlen(hostname) < dst_sz) {
strlcpy (dst, hostname, dst_sz);
return;
}
gethostname (local_hostname, sizeof(local_hostname));
hd = strchr (hostname, '.');
ld = strchr (local_hostname, '.');
if (hd != NULL && ld != NULL && strcmp(hd, ld) == 0
&& hd - hostname < dst_sz) {
strlcpy (dst, hostname, dst_sz);
dst[hd - hostname] = '\0';
return;
}
ret = getaddrinfo (hostname, NULL, NULL, &ai);
if (ret) {
strncpy (dst, hostname, dst_sz);
return;
}
ret = getnameinfo (ai->ai_addr, ai->ai_addrlen,
dst, dst_sz,
NULL, 0,
NI_NUMERICHOST);
freeaddrinfo (ai);
if (ret) {
strncpy (dst, hostname, dst_sz);
return;
}
}
void
prepare_utmp (struct utmp *utmp, char *tty,
const char *username, const char *hostname)
@@ -60,7 +102,7 @@ prepare_utmp (struct utmp *utmp, char *tty,
# endif
# ifdef HAVE_STRUCT_UTMP_UT_HOST
strncpy(utmp->ut_host, hostname, sizeof(utmp->ut_host));
shrink_hostname (hostname, utmp->ut_host, sizeof(utmp->ut_host));
# endif
# ifdef HAVE_STRUCT_UTMP_UT_TYPE

View File

@@ -21,7 +21,7 @@ utmpx_update(struct utmpx *ut, char *line, const char *user, const char *host)
strncpy(ut->ut_id, make_id(clean_tty), sizeof(ut->ut_id));
#endif
strncpy(ut->ut_user, user, sizeof(ut->ut_user));
strncpy(ut->ut_host, host, sizeof(ut->ut_host));
shrink_hostname (host, ut->ut_host, sizeof(ut->ut_host));
#ifdef HAVE_STRUCT_UTMPX_UT_SYSLEN
ut->ut_syslen = strlen(host) + 1;
if (ut->ut_syslen > sizeof(ut->ut_host))