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