xref: /titanic_51/usr/src/lib/gss_mechs/mech_krb5/krb5/os/genaddrs.c (revision 159d09a20817016f09b3ea28d1bdada4a336bb91)
1 
2 /*
3  * lib/krb5/os/genaddrs.c
4  *
5  * Copyright 1995 by the Massachusetts Institute of Technology.
6  * All Rights Reserved.
7  *
8  * Export of this software from the United States of America may
9  *   require a specific license from the United States Government.
10  *   It is the responsibility of any person or organization contemplating
11  *   export to obtain such a license before exporting.
12  *
13  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
14  * distribute this software and its documentation for any purpose and
15  * without fee is hereby granted, provided that the above copyright
16  * notice appear in all copies and that both that copyright notice and
17  * this permission notice appear in supporting documentation, and that
18  * the name of M.I.T. not be used in advertising or publicity pertaining
19  * to distribution of the software without specific, written prior
20  * permission.  Furthermore if you modify this software you must label
21  * your software as modified software and not distribute it in such a
22  * fashion that it might be confused with the original M.I.T. software.
23  * M.I.T. makes no representations about the suitability of
24  * this software for any purpose.  It is provided "as is" without express
25  * or implied warranty.
26  *
27  *
28  * Take an IP addr & port and generate a full IP address.
29  */
30 
31 #include "k5-int.h"
32 #include "os-proto.h"
33 
34 #if !defined(_WINSOCKAPI_)
35 #include <netinet/in.h>
36 #endif
37 
38 /* Solaris Kerberos */
39 #include <inet/ip.h>
40 #include <inet/ip6.h>
41 
42 struct addrpair {
43     krb5_address addr, port;
44 };
45 
46 #define SET(TARG, THING, TYPE) \
47 	((TARG).contents = (krb5_octet *) &(THING),	\
48 	 (TARG).length = sizeof (THING),		\
49 	 (TARG).addrtype = (TYPE))
50 
51 static void *cvtaddr (struct sockaddr_storage *a, struct addrpair *ap)
52 {
53     switch (ss2sa(a)->sa_family) {
54     case AF_INET:
55 	SET (ap->port, ss2sin(a)->sin_port, ADDRTYPE_IPPORT);
56 	SET (ap->addr, ss2sin(a)->sin_addr, ADDRTYPE_INET);
57 	return a;
58 #ifdef KRB5_USE_INET6
59     case AF_INET6:
60 	SET (ap->port, ss2sin6(a)->sin6_port, ADDRTYPE_IPPORT);
61 	if (IN6_IS_ADDR_V4MAPPED (&ss2sin6(a)->sin6_addr)) {
62 	    ap->addr.addrtype = ADDRTYPE_INET;
63 	    /* Solaris Kerberos */
64 	    ap->addr.contents = (IPV6_ADDR_LEN - IPV4_ADDR_LEN) +
65 		(krb5_octet *) &ss2sin6(a)->sin6_addr;
66 	    ap->addr.length = IPV4_ADDR_LEN;
67 	} else
68 	    SET (ap->addr, ss2sin6(a)->sin6_addr, ADDRTYPE_INET6);
69 	return a;
70 #endif
71     default:
72 	return 0;
73     }
74 }
75 
76 krb5_error_code KRB5_CALLCONV
77 krb5_auth_con_genaddrs(krb5_context context, krb5_auth_context auth_context, int infd, int flags)
78 {
79     krb5_error_code 	  retval;
80     krb5_address	* laddr;
81     krb5_address	* lport;
82     krb5_address	* raddr;
83     krb5_address	* rport;
84     SOCKET		fd = (SOCKET) infd;
85     struct addrpair laddrs, raddrs;
86 
87 #ifdef HAVE_NETINET_IN_H
88     struct sockaddr_storage lsaddr, rsaddr;
89     GETSOCKNAME_ARG3_TYPE ssize;
90 
91     ssize = sizeof(struct sockaddr_storage);
92     if ((flags & KRB5_AUTH_CONTEXT_GENERATE_LOCAL_FULL_ADDR) ||
93 	(flags & KRB5_AUTH_CONTEXT_GENERATE_LOCAL_ADDR)) {
94     	if ((retval = getsockname(fd, (GETSOCKNAME_ARG2_TYPE *) &lsaddr,
95 				  &ssize)))
96 	    return retval;
97 
98 	if (cvtaddr (&lsaddr, &laddrs)) {
99 	    laddr = &laddrs.addr;
100 	    if (flags & KRB5_AUTH_CONTEXT_GENERATE_LOCAL_FULL_ADDR)
101 		lport = &laddrs.port;
102 	    else
103 		lport = 0;
104 	} else
105 	    return KRB5_PROG_ATYPE_NOSUPP;
106     } else {
107 	laddr = NULL;
108 	lport = NULL;
109     }
110 
111     ssize = sizeof(struct sockaddr_storage);
112     if ((flags & KRB5_AUTH_CONTEXT_GENERATE_REMOTE_FULL_ADDR) ||
113 	(flags & KRB5_AUTH_CONTEXT_GENERATE_REMOTE_ADDR)) {
114         if ((retval = getpeername(fd, (GETPEERNAME_ARG2_TYPE *) &rsaddr,
115 				  &ssize)))
116 	    return errno;
117 
118 	if (cvtaddr (&rsaddr, &raddrs)) {
119 	    raddr = &raddrs.addr;
120 	    if (flags & KRB5_AUTH_CONTEXT_GENERATE_REMOTE_FULL_ADDR)
121 		rport = &raddrs.port;
122 	    else
123 		rport = 0;
124 	} else
125 	    return KRB5_PROG_ATYPE_NOSUPP;
126     } else {
127 	raddr = NULL;
128 	rport = NULL;
129     }
130 
131     if (!(retval = krb5_auth_con_setaddrs(context, auth_context, laddr, raddr)))
132     	return (krb5_auth_con_setports(context, auth_context, lport, rport));
133     return retval;
134 #else
135     return KRB5_PROG_ATYPE_NOSUPP;
136 #endif
137 }
138