From d470ceb5371a7d7f37cae81b019945eddf6abcac Mon Sep 17 00:00:00 2001 From: Tom Yu Date: Fri, 12 Feb 1999 03:26:27 +0000 Subject: [PATCH] * Makefile.in: Add sane_hostname.{o,c} * libpty.h: Add prototype for make_sane_hostname. * sane_hostname.c: New file; add function to "sanitize" hostname for logging purposes. git-svn-id: svn://anonsvn.mit.edu/krb5/trunk@11165 dc483132-0cff-0310-8789-dd5450dbe970 --- src/util/pty/ChangeLog | 9 ++++ src/util/pty/Makefile.in | 4 +- src/util/pty/libpty.h | 2 + src/util/pty/sane_hostname.c | 99 ++++++++++++++++++++++++++++++++++++ 4 files changed, 112 insertions(+), 2 deletions(-) create mode 100644 src/util/pty/sane_hostname.c diff --git a/src/util/pty/ChangeLog b/src/util/pty/ChangeLog index 3082e45fb..ab35f9b9d 100644 --- a/src/util/pty/ChangeLog +++ b/src/util/pty/ChangeLog @@ -1,3 +1,12 @@ +Thu Feb 11 22:24:03 1999 Tom Yu + + * Makefile.in: Add sane_hostname.{o,c} + + * libpty.h: Add prototype for make_sane_hostname. + + * sane_hostname.c: New file; add function to "sanitize" hostname + for logging purposes. + 1999-01-27 Theodore Ts'o * configure.in: Remove test CHECK_WAIT_TYPE since nothing is using diff --git a/src/util/pty/Makefile.in b/src/util/pty/Makefile.in index 5250555c0..2faa285a3 100644 --- a/src/util/pty/Makefile.in +++ b/src/util/pty/Makefile.in @@ -13,7 +13,7 @@ LIBMINOR=0 STLIBOBJS= cleanup.o getpty.o init_slave.o open_ctty.o open_slave.o \ update_utmp.o update_wtmp.o vhangup.o void_assoc.o pty_err.o \ - logwtmp.o init.o + logwtmp.o init.o sane_hostname.o STOBJLISTS=OBJS.ST @@ -29,7 +29,7 @@ CFILES=$(srcdir)/cleanup.c $(srcdir)/getpty.c $(srcdir)/init_slave.c \ $(srcdir)/open_ctty.c $(srcdir)/open_slave.c \ $(srcdir)/update_utmp.c $(srcdir)/update_wtmp.c $(srcdir)/vhangup.c \ $(srcdir)/void_assoc.c $(srcdir)/logwtmp.c \ - $(srcdir)/init.c + $(srcdir)/init.c $(srcdir)/sane_hostname.c SRCS=pty_err.c $(CFILES) diff --git a/src/util/pty/libpty.h b/src/util/pty/libpty.h index 51d834a3b..552a3ef86 100644 --- a/src/util/pty/libpty.h +++ b/src/util/pty/libpty.h @@ -41,6 +41,7 @@ long pty_update_utmp (int process_type,int pid, char *user, char *line, char *h long pty_logwtmp (char *tty, char * user, char *host); long pty_cleanup(char *slave, int pid, int update_utmp); +long pty_make_sane_hostname(struct sockaddr_in *, int, int, int, char **); #else /*__STDC__*/ long pty_init(); long pty_getpty(); @@ -52,6 +53,7 @@ long pty_initialize_slave(); long pty_update_utmp(); long pty_logwtmp(); long pty_cleanup(); +long pty_make_sane_hostname(); #endif /* __STDC__*/ #define __LIBPTY_H__ #endif diff --git a/src/util/pty/sane_hostname.c b/src/util/pty/sane_hostname.c new file mode 100644 index 000000000..93871d8d4 --- /dev/null +++ b/src/util/pty/sane_hostname.c @@ -0,0 +1,99 @@ +/* + * pty_make_sane_hostname: Make a sane hostname from an IP address. + * This returns allocated memory! + * + * Copyright 1999 by the Massachusetts Institute of Technology. + * + * Permission to use, copy, modify, and distribute this software and + * its documentation for any purpose and without fee is hereby + * granted, provided that the above copyright notice appear in all + * copies and that both that copyright notice and this permission + * notice appear in supporting documentation, and that the name of + * M.I.T. not be used in advertising or publicity pertaining to + * distribution of the software without specific, written prior + * permission. M.I.T. makes no representations about the suitability + * of this software for any purpose. It is provided "as is" without + * express or implied warranty. + * + */ +#include +#include +#include "libpty.h" +#include "pty-int.h" + +static long +do_ntoa(struct sockaddr_in *addr, + size_t hostlen, + char **out) +{ + strncpy(*out, inet_ntoa(addr->sin_addr), hostlen); + (*out)[hostlen - 1] = '\0'; + return 0; +} + +long +pty_make_sane_hostname(struct sockaddr_in *addr, + int maxlen, + int strip_ldomain, + int always_ipaddr, + char **out) +{ + struct hostent *hp; +#ifndef NO_UT_HOST + struct utmp ut; +#else + struct utmpx utx; +#endif + char *scratch; + char *cp, *domain; + char lhost[MAXHOSTNAMELEN]; + size_t ut_host_len; + + *out = NULL; + if (maxlen && maxlen < 16) + return -1; /* XXX */ +#ifndef NO_UT_HOST + ut_host_len = sizeof (ut.ut_host); +#else + ut_host_len = sizeof (utx.ut_host); +#endif + if (maxlen == 0) + maxlen = ut_host_len; + *out = malloc(ut_host_len); + if (*out == NULL) + return ENOMEM; + + if (always_ipaddr) { + return do_ntoa(addr, ut_host_len, out); + } + hp = gethostbyaddr((char *)&addr->sin_addr, sizeof (struct in_addr), + addr->sin_family); + if (hp == NULL) { + return do_ntoa(addr, ut_host_len, out); + } + strncpy(*out, hp->h_name, ut_host_len); + (*out)[ut_host_len - 1] = '\0'; + for (cp = *out; *cp != '\0'; cp++) + *cp = tolower(*cp); + + if (strip_ldomain) { + (void) gethostname(lhost, sizeof (lhost)); + hp = gethostbyname(lhost); + if (hp != NULL) { + strncpy(lhost, hp->h_name, sizeof (lhost)); + domain = strchr(lhost, '.'); + if (domain != NULL) { + for (cp = domain; *cp != '\0'; cp++) + *cp = tolower(*cp); + cp = strstr(*out, domain); + if (cp != NULL) + *cp = '\0'; + } + } + } + + if (strlen(*out) >= maxlen) { + return do_ntoa(addr, ut_host_len, out); + } + return 0; +} -- 2.26.2