xref: /onnv-gate/usr/src/lib/gss_mechs/mech_krb5/krb5/os/genaddrs.c (revision 7934:6aeeafc994de)
10Sstevel@tonic-gate 
20Sstevel@tonic-gate /*
30Sstevel@tonic-gate  * lib/krb5/os/genaddrs.c
40Sstevel@tonic-gate  *
50Sstevel@tonic-gate  * Copyright 1995 by the Massachusetts Institute of Technology.
60Sstevel@tonic-gate  * All Rights Reserved.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * Export of this software from the United States of America may
90Sstevel@tonic-gate  *   require a specific license from the United States Government.
100Sstevel@tonic-gate  *   It is the responsibility of any person or organization contemplating
110Sstevel@tonic-gate  *   export to obtain such a license before exporting.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
140Sstevel@tonic-gate  * distribute this software and its documentation for any purpose and
150Sstevel@tonic-gate  * without fee is hereby granted, provided that the above copyright
160Sstevel@tonic-gate  * notice appear in all copies and that both that copyright notice and
170Sstevel@tonic-gate  * this permission notice appear in supporting documentation, and that
180Sstevel@tonic-gate  * the name of M.I.T. not be used in advertising or publicity pertaining
190Sstevel@tonic-gate  * to distribution of the software without specific, written prior
200Sstevel@tonic-gate  * permission.  Furthermore if you modify this software you must label
210Sstevel@tonic-gate  * your software as modified software and not distribute it in such a
220Sstevel@tonic-gate  * fashion that it might be confused with the original M.I.T. software.
230Sstevel@tonic-gate  * M.I.T. makes no representations about the suitability of
240Sstevel@tonic-gate  * this software for any purpose.  It is provided "as is" without express
250Sstevel@tonic-gate  * or implied warranty.
260Sstevel@tonic-gate  *
270Sstevel@tonic-gate  *
280Sstevel@tonic-gate  * Take an IP addr & port and generate a full IP address.
290Sstevel@tonic-gate  */
300Sstevel@tonic-gate 
31*7934SMark.Phalan@Sun.COM #include "k5-int.h"
320Sstevel@tonic-gate #include "os-proto.h"
330Sstevel@tonic-gate 
34*7934SMark.Phalan@Sun.COM #if !defined(_WINSOCKAPI_)
350Sstevel@tonic-gate #include <netinet/in.h>
360Sstevel@tonic-gate #endif
37*7934SMark.Phalan@Sun.COM 
38*7934SMark.Phalan@Sun.COM /* Solaris Kerberos */
390Sstevel@tonic-gate #include <inet/ip.h>
400Sstevel@tonic-gate #include <inet/ip6.h>
410Sstevel@tonic-gate 
420Sstevel@tonic-gate struct addrpair {
430Sstevel@tonic-gate     krb5_address addr, port;
440Sstevel@tonic-gate };
450Sstevel@tonic-gate 
460Sstevel@tonic-gate #define SET(TARG, THING, TYPE) \
470Sstevel@tonic-gate 	((TARG).contents = (krb5_octet *) &(THING),	\
480Sstevel@tonic-gate 	 (TARG).length = sizeof (THING),		\
490Sstevel@tonic-gate 	 (TARG).addrtype = (TYPE))
500Sstevel@tonic-gate 
cvtaddr(struct sockaddr_storage * a,struct addrpair * ap)510Sstevel@tonic-gate static void *cvtaddr (struct sockaddr_storage *a, struct addrpair *ap)
520Sstevel@tonic-gate {
530Sstevel@tonic-gate     switch (ss2sa(a)->sa_family) {
540Sstevel@tonic-gate     case AF_INET:
550Sstevel@tonic-gate 	SET (ap->port, ss2sin(a)->sin_port, ADDRTYPE_IPPORT);
560Sstevel@tonic-gate 	SET (ap->addr, ss2sin(a)->sin_addr, ADDRTYPE_INET);
570Sstevel@tonic-gate 	return a;
580Sstevel@tonic-gate #ifdef KRB5_USE_INET6
590Sstevel@tonic-gate     case AF_INET6:
600Sstevel@tonic-gate 	SET (ap->port, ss2sin6(a)->sin6_port, ADDRTYPE_IPPORT);
610Sstevel@tonic-gate 	if (IN6_IS_ADDR_V4MAPPED (&ss2sin6(a)->sin6_addr)) {
620Sstevel@tonic-gate 	    ap->addr.addrtype = ADDRTYPE_INET;
63*7934SMark.Phalan@Sun.COM 	    /* Solaris Kerberos */
640Sstevel@tonic-gate 	    ap->addr.contents = (IPV6_ADDR_LEN - IPV4_ADDR_LEN) +
650Sstevel@tonic-gate 		(krb5_octet *) &ss2sin6(a)->sin6_addr;
660Sstevel@tonic-gate 	    ap->addr.length = IPV4_ADDR_LEN;
670Sstevel@tonic-gate 	} else
680Sstevel@tonic-gate 	    SET (ap->addr, ss2sin6(a)->sin6_addr, ADDRTYPE_INET6);
690Sstevel@tonic-gate 	return a;
700Sstevel@tonic-gate #endif
710Sstevel@tonic-gate     default:
720Sstevel@tonic-gate 	return 0;
730Sstevel@tonic-gate     }
740Sstevel@tonic-gate }
750Sstevel@tonic-gate 
760Sstevel@tonic-gate krb5_error_code KRB5_CALLCONV
krb5_auth_con_genaddrs(krb5_context context,krb5_auth_context auth_context,int infd,int flags)770Sstevel@tonic-gate krb5_auth_con_genaddrs(krb5_context context, krb5_auth_context auth_context, int infd, int flags)
780Sstevel@tonic-gate {
790Sstevel@tonic-gate     krb5_error_code 	  retval;
800Sstevel@tonic-gate     krb5_address	* laddr;
810Sstevel@tonic-gate     krb5_address	* lport;
820Sstevel@tonic-gate     krb5_address	* raddr;
830Sstevel@tonic-gate     krb5_address	* rport;
840Sstevel@tonic-gate     SOCKET		fd = (SOCKET) infd;
850Sstevel@tonic-gate     struct addrpair laddrs, raddrs;
860Sstevel@tonic-gate 
870Sstevel@tonic-gate #ifdef HAVE_NETINET_IN_H
880Sstevel@tonic-gate     struct sockaddr_storage lsaddr, rsaddr;
890Sstevel@tonic-gate     GETSOCKNAME_ARG3_TYPE ssize;
900Sstevel@tonic-gate 
910Sstevel@tonic-gate     ssize = sizeof(struct sockaddr_storage);
920Sstevel@tonic-gate     if ((flags & KRB5_AUTH_CONTEXT_GENERATE_LOCAL_FULL_ADDR) ||
930Sstevel@tonic-gate 	(flags & KRB5_AUTH_CONTEXT_GENERATE_LOCAL_ADDR)) {
940Sstevel@tonic-gate     	if ((retval = getsockname(fd, (GETSOCKNAME_ARG2_TYPE *) &lsaddr,
950Sstevel@tonic-gate 				  &ssize)))
960Sstevel@tonic-gate 	    return retval;
970Sstevel@tonic-gate 
980Sstevel@tonic-gate 	if (cvtaddr (&lsaddr, &laddrs)) {
990Sstevel@tonic-gate 	    laddr = &laddrs.addr;
1000Sstevel@tonic-gate 	    if (flags & KRB5_AUTH_CONTEXT_GENERATE_LOCAL_FULL_ADDR)
1010Sstevel@tonic-gate 		lport = &laddrs.port;
1020Sstevel@tonic-gate 	    else
1030Sstevel@tonic-gate 		lport = 0;
1040Sstevel@tonic-gate 	} else
1050Sstevel@tonic-gate 	    return KRB5_PROG_ATYPE_NOSUPP;
1060Sstevel@tonic-gate     } else {
1070Sstevel@tonic-gate 	laddr = NULL;
1080Sstevel@tonic-gate 	lport = NULL;
1090Sstevel@tonic-gate     }
1100Sstevel@tonic-gate 
1110Sstevel@tonic-gate     ssize = sizeof(struct sockaddr_storage);
1120Sstevel@tonic-gate     if ((flags & KRB5_AUTH_CONTEXT_GENERATE_REMOTE_FULL_ADDR) ||
1130Sstevel@tonic-gate 	(flags & KRB5_AUTH_CONTEXT_GENERATE_REMOTE_ADDR)) {
1140Sstevel@tonic-gate         if ((retval = getpeername(fd, (GETPEERNAME_ARG2_TYPE *) &rsaddr,
1150Sstevel@tonic-gate 				  &ssize)))
1160Sstevel@tonic-gate 	    return errno;
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate 	if (cvtaddr (&rsaddr, &raddrs)) {
1190Sstevel@tonic-gate 	    raddr = &raddrs.addr;
1200Sstevel@tonic-gate 	    if (flags & KRB5_AUTH_CONTEXT_GENERATE_REMOTE_FULL_ADDR)
1210Sstevel@tonic-gate 		rport = &raddrs.port;
1220Sstevel@tonic-gate 	    else
1230Sstevel@tonic-gate 		rport = 0;
1240Sstevel@tonic-gate 	} else
1250Sstevel@tonic-gate 	    return KRB5_PROG_ATYPE_NOSUPP;
1260Sstevel@tonic-gate     } else {
1270Sstevel@tonic-gate 	raddr = NULL;
1280Sstevel@tonic-gate 	rport = NULL;
1290Sstevel@tonic-gate     }
1300Sstevel@tonic-gate 
1310Sstevel@tonic-gate     if (!(retval = krb5_auth_con_setaddrs(context, auth_context, laddr, raddr)))
1320Sstevel@tonic-gate     	return (krb5_auth_con_setports(context, auth_context, lport, rport));
1330Sstevel@tonic-gate     return retval;
1340Sstevel@tonic-gate #else
1350Sstevel@tonic-gate     return KRB5_PROG_ATYPE_NOSUPP;
1360Sstevel@tonic-gate #endif
1370Sstevel@tonic-gate }
138