diff options
author | brian <brian@FreeBSD.org> | 1999-04-06 23:02:35 +0000 |
---|---|---|
committer | brian <brian@FreeBSD.org> | 1999-04-06 23:02:35 +0000 |
commit | cf0ba219f719b19bf1fc5862d698b2713d849705 (patch) | |
tree | e156c2523449d0ca77b8482a4fc260c2d1c280bc /lib/libutil | |
parent | b3871d56a7a5710307edbf003c3c6b5db8f5cce6 (diff) | |
download | FreeBSD-src-cf0ba219f719b19bf1fc5862d698b2713d849705.zip FreeBSD-src-cf0ba219f719b19bf1fc5862d698b2713d849705.tar.gz |
Add realhostname() - a function to correctly lookup
a name by address and ensure that the name resolves
back to the original address.
Diffstat (limited to 'lib/libutil')
-rw-r--r-- | lib/libutil/Makefile | 4 | ||||
-rw-r--r-- | lib/libutil/libutil.h | 10 | ||||
-rw-r--r-- | lib/libutil/realhostname.3 | 106 | ||||
-rw-r--r-- | lib/libutil/realhostname.c | 73 |
4 files changed, 190 insertions, 3 deletions
diff --git a/lib/libutil/Makefile b/lib/libutil/Makefile index 7754e6e..9e9d04b 100644 --- a/lib/libutil/Makefile +++ b/lib/libutil/Makefile @@ -7,10 +7,10 @@ CFLAGS+=-Wall -DLIBC_SCCS -I${.CURDIR} -I${.CURDIR}/../../sys #CFLAGS+=LOGIN_CAP_AUTH SRCS= login.c login_tty.c logout.c logwtmp.c pty.c setproctitle.c \ login_cap.c login_class.c login_auth.c login_times.c login_ok.c \ - _secure_path.c uucplock.c property.c auth.c + _secure_path.c uucplock.c property.c auth.c realhostname.c MAN3+= login.3 login_auth.3 login_tty.3 logout.3 logwtmp.3 pty.3 \ setproctitle.3 login_cap.3 login_class.3 login_times.3 login_ok.3 \ - _secure_path.3 uucplock.3 property.3 auth.3 + _secure_path.3 uucplock.3 property.3 auth.3 realhostname.3 MAN5+= login.conf.5 auth.conf.5 MLINKS+= property.3 properties_read.3 property.3 properties_free.3 MLINKS+= property.3 property_find.3 diff --git a/lib/libutil/libutil.h b/lib/libutil/libutil.h index dda01c1..32957d9 100644 --- a/lib/libutil/libutil.h +++ b/lib/libutil/libutil.h @@ -18,7 +18,7 @@ * 5. Modifications may be freely made to this file providing the above * conditions are met. * - * $Id: libutil.h,v 1.20 1998/10/09 07:32:38 jkh Exp $ + * $Id: libutil.h,v 1.21 1998/12/16 17:52:15 bde Exp $ */ #ifndef _LIBUTIL_H_ @@ -37,6 +37,7 @@ typedef struct _property { struct termios; struct winsize; struct utmp; +struct in_addr; __BEGIN_DECLS void setproctitle __P((const char *_fmt, ...)) __printf0like(1, 2); @@ -58,6 +59,7 @@ properties properties_read __P((int fd)); void properties_free __P((properties list)); char *property_find __P((properties list, const char *name)); char *auth_getval __P((const char *name)); +int realhostname __P((char *host, size_t hsize, const struct in_addr *ip)); __END_DECLS #define UU_LOCK_INUSE (1) @@ -70,4 +72,10 @@ __END_DECLS #define UU_LOCK_TRY_ERR (-6) #define UU_LOCK_OWNER_ERR (-7) +/* return values from realhostname() */ +#define HOSTNAME_FOUND (0) +#define HOSTNAME_INCORRECTNAME (1) +#define HOSTNAME_INVALIDADDR (2) +#define HOSTNAME_INVALIDNAME (3) + #endif /* !_LIBUTIL_H_ */ diff --git a/lib/libutil/realhostname.3 b/lib/libutil/realhostname.3 new file mode 100644 index 0000000..4298e7f --- /dev/null +++ b/lib/libutil/realhostname.3 @@ -0,0 +1,106 @@ +.\" Copyright (c) 1999 Brian Somers <brian@Awfulhak.org> +.\" All rights reserved. +.\" +.\" Redistribution and use in source and binary forms, with or without +.\" modification, are permitted provided that the following conditions +.\" are met: +.\" 1. Redistributions of source code must retain the above copyright +.\" notice, this list of conditions and the following disclaimer. +.\" 2. Redistributions in binary form must reproduce the above copyright +.\" notice, this list of conditions and the following disclaimer in the +.\" documentation and/or other materials provided with the distribution. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND +.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE +.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY +.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +.\" SUCH DAMAGE. +.\" +.\" $Id:$ +.\" +.Dd April 6, 1999 +.Os +.Dt REALHOSTNAME 3 +.Sh NAME +.Nm realhostname +.Nd "convert an IP number to the real host name" +.Sh SYNOPSIS +.Fd #include <sys/types.h> +.Fd #include <netinet/in.h> +.Fd #include <libutil.h> +.Ft int +.Fn realhostname "char *host" "size_t hsize" "const struct in_addr *ip" +.Pp +Link with +.Va -lutil +on the +.Xr cc 1 +command line. +.Sh DESCRIPTION +The function +.Fn realhostname +converts +.Ar ip +to the corresponding host name. This is done by resolving +.Ar ip +to a host name and then ensuring that the host name resolves +back to +.Ar ip . +.Pp +.Ar host +must point to a buffer of at least +.Ar hsize +bytes, and will always be written to by this function. +.Pp +If the name resolution doesn't work both ways or if the host name is longer +than +.Ar hsize +bytes, +.Xr inet_ntoa 3 +is used to convert +.Ar ip +to an ascii form. +.Pp +If the string written to +.Ar host +is +.Ar hsize +bytes long, +.Ar host +will not be NUL terminated. +.Sh RETURN VALUES +.Fn realhostname +will return one of the following constants which are defined in +.Pa libutil.h : +.Pp +.Bl -tag -width XXX -offset XXX +.It Li HOSTNAME_FOUND +A valid host name was found. +.It Li HOSTNAME_INCORRECTNAME +A host name was found, but it did not resolve back to the passed +.Ar ip . +.Ar host +now contains the numeric value of +.Ar ip . +.It Li HOSTNAME_INVALIDADDR +.Ar ip +could not be resolved. +.Ar host +now contains the numeric value of +.Ar ip . +.It Li HOSTNAME_INVALIDNAME +A host name was found, but it could not be resolved back to any ip number. +.Ar host +now contains the numeric value of +.Ar ip . +.El +.Sh SEE ALSO +.Xr gethostbyaddr 3 , +.Xr gethostbyname 3 , +.Xr inet_ntoa 3 diff --git a/lib/libutil/realhostname.c b/lib/libutil/realhostname.c new file mode 100644 index 0000000..ff57011 --- /dev/null +++ b/lib/libutil/realhostname.c @@ -0,0 +1,73 @@ +/*- + * Copyright (c) 1999 Brian Somers <brian@Awfulhak.org> + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * $Id:$ + */ + +#include <sys/param.h> + +#include <sys/socket.h> +#include <netdb.h> +#include <netinet/in.h> +#include <arpa/inet.h> + +#include <stdio.h> +#include <string.h> + +#include "libutil.h" + +int +realhostname(char *host, size_t hsize, const struct in_addr *ip) +{ + int result; + struct hostent *hp; + + result = HOSTNAME_INVALIDADDR; + hp = gethostbyaddr((char *)ip, sizeof *ip, AF_INET); + + if (hp != NULL && strlen(hp->h_name) <= hsize) { + char lookup[MAXHOSTNAMELEN + 1]; + + strncpy(lookup, hp->h_name, sizeof lookup - 1); + lookup[sizeof lookup - 1] = '\0'; + hp = gethostbyname(lookup); + if (hp == NULL) + result = HOSTNAME_INVALIDNAME; + else for (; ; hp->h_addr_list++) { + if (hp->h_addr_list[0] == NULL) { + result = HOSTNAME_INCORRECTNAME; + break; + } + if (!memcmp(*hp->h_addr_list, ip, sizeof *ip)) { + strncpy(host, lookup, hsize); + return HOSTNAME_FOUND; + } + } + } + + strncpy(host, inet_ntoa(*ip), hsize); + + return result; +} |