2 * Copyright (c) 1997 - 2002 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 #include "krb5_locl.h"
48 static krb5_error_code
49 gethostname_fallback (krb5_context context
, krb5_addresses
*res
)
52 char hostname
[MAXHOSTNAMELEN
];
53 struct hostent
*hostent
;
55 if (gethostname (hostname
, sizeof(hostname
))) {
57 krb5_set_error_string (context
, "gethostname: %s", strerror(ret
));
60 hostent
= roken_gethostbyname (hostname
);
61 if (hostent
== NULL
) {
63 krb5_set_error_string (context
, "gethostbyname %s: %s",
64 hostname
, strerror(ret
));
68 res
->val
= malloc (sizeof(*res
->val
));
69 if (res
->val
== NULL
) {
70 krb5_set_error_string(context
, "malloc: out of memory");
73 res
->val
[0].addr_type
= hostent
->h_addrtype
;
74 res
->val
[0].address
.data
= NULL
;
75 res
->val
[0].address
.length
= 0;
76 ret
= krb5_data_copy (&res
->val
[0].address
,
87 LOOP
= 1, /* do include loopback interfaces */
88 LOOP_IF_NONE
= 2, /* include loopback if no other if's */
89 EXTRA_ADDRESSES
= 4, /* include extra addresses */
90 SCAN_INTERFACES
= 8 /* scan interfaces for addresses */
94 * Try to figure out the addresses of all configured interfaces with a
95 * lot of magic ioctls.
98 static krb5_error_code
99 find_all_addresses (krb5_context context
, krb5_addresses
*res
, int flags
)
101 struct sockaddr sa_zero
;
102 struct ifaddrs
*ifa0
, *ifa
;
103 krb5_error_code ret
= ENXIO
;
105 krb5_addresses ignore_addresses
;
109 if (getifaddrs(&ifa0
) == -1) {
111 krb5_set_error_string(context
, "getifaddrs: %s", strerror(ret
));
115 memset(&sa_zero
, 0, sizeof(sa_zero
));
117 /* First, count all the ifaddrs. */
118 for (ifa
= ifa0
, num
= 0; ifa
!= NULL
; ifa
= ifa
->ifa_next
, num
++)
123 krb5_set_error_string(context
, "no addresses found");
127 if (flags
& EXTRA_ADDRESSES
) {
128 /* we'll remove the addresses we don't care about */
129 ret
= krb5_get_ignore_addresses(context
, &ignore_addresses
);
134 /* Allocate storage for them. */
135 res
->val
= calloc(num
, sizeof(*res
->val
));
136 if (res
->val
== NULL
) {
137 krb5_free_addresses(context
, &ignore_addresses
);
139 krb5_set_error_string (context
, "malloc: out of memory");
143 /* Now traverse the list. */
144 for (ifa
= ifa0
, idx
= 0; ifa
!= NULL
; ifa
= ifa
->ifa_next
) {
145 if ((ifa
->ifa_flags
& IFF_UP
) == 0)
147 if (ifa
->ifa_addr
== NULL
)
149 if (memcmp(ifa
->ifa_addr
, &sa_zero
, sizeof(sa_zero
)) == 0)
151 if (krb5_sockaddr_uninteresting(ifa
->ifa_addr
))
153 if ((ifa
->ifa_flags
& IFF_LOOPBACK
) != 0) {
154 /* We'll deal with the LOOP_IF_NONE case later. */
155 if ((flags
& LOOP
) == 0)
159 ret
= krb5_sockaddr2address(context
, ifa
->ifa_addr
, &res
->val
[idx
]);
162 * The most likely error here is going to be "Program
163 * lacks support for address type". This is no big
164 * deal -- just continue, and we'll listen on the
165 * addresses who's type we *do* support.
169 /* possibly skip this address? */
170 if((flags
& EXTRA_ADDRESSES
) &&
171 krb5_address_search(context
, &res
->val
[idx
], &ignore_addresses
)) {
172 krb5_free_address(context
, &res
->val
[idx
]);
173 flags
&= ~LOOP_IF_NONE
; /* we actually found an address,
174 so don't add any loop-back
183 * If no addresses were found, and LOOP_IF_NONE is set, then find
184 * the loopback addresses and add them to our list.
186 if ((flags
& LOOP_IF_NONE
) != 0 && idx
== 0) {
187 for (ifa
= ifa0
; ifa
!= NULL
; ifa
= ifa
->ifa_next
) {
188 if ((ifa
->ifa_flags
& IFF_UP
) == 0)
190 if (ifa
->ifa_addr
== NULL
)
192 if (memcmp(ifa
->ifa_addr
, &sa_zero
, sizeof(sa_zero
)) == 0)
194 if (krb5_sockaddr_uninteresting(ifa
->ifa_addr
))
197 if ((ifa
->ifa_flags
& IFF_LOOPBACK
) != 0) {
198 ret
= krb5_sockaddr2address(context
,
199 ifa
->ifa_addr
, &res
->val
[idx
]);
206 if((flags
& EXTRA_ADDRESSES
) &&
207 krb5_address_search(context
, &res
->val
[idx
],
208 &ignore_addresses
)) {
209 krb5_free_address(context
, &res
->val
[idx
]);
217 if (flags
& EXTRA_ADDRESSES
)
218 krb5_free_addresses(context
, &ignore_addresses
);
223 res
->len
= idx
; /* Now a count. */
227 static krb5_error_code
228 get_addrs_int (krb5_context context
, krb5_addresses
*res
, int flags
)
230 krb5_error_code ret
= -1;
232 if (flags
& SCAN_INTERFACES
) {
233 ret
= find_all_addresses (context
, res
, flags
);
234 if(ret
|| res
->len
== 0)
235 ret
= gethostname_fallback (context
, res
);
242 if(ret
== 0 && (flags
& EXTRA_ADDRESSES
)) {
244 /* append user specified addresses */
245 ret
= krb5_get_extra_addresses(context
, &a
);
247 krb5_free_addresses(context
, res
);
250 ret
= krb5_append_addresses(context
, res
, &a
);
252 krb5_free_addresses(context
, res
);
255 krb5_free_addresses(context
, &a
);
265 * Try to get all addresses, but return the one corresponding to
266 * `hostname' if we fail.
268 * Only include loopback address if there are no other.
271 krb5_error_code KRB5_LIB_FUNCTION
272 krb5_get_all_client_addrs (krb5_context context
, krb5_addresses
*res
)
274 int flags
= LOOP_IF_NONE
| EXTRA_ADDRESSES
;
276 if (context
->scan_interfaces
)
277 flags
|= SCAN_INTERFACES
;
279 return get_addrs_int (context
, res
, flags
);
283 * Try to get all local addresses that a server should listen to.
284 * If that fails, we return the address corresponding to `hostname'.
287 krb5_error_code KRB5_LIB_FUNCTION
288 krb5_get_all_server_addrs (krb5_context context
, krb5_addresses
*res
)
290 return get_addrs_int (context
, res
, LOOP
| SCAN_INTERFACES
);