1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
|
/*
Authors:
Pavel Březina <pbrezina@redhat.com>
Copyright (C) 2012 Red Hat
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <errno.h>
#include <tevent.h>
#include <talloc.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <ifaddrs.h>
#include "util/util.h"
#include "providers/ldap/sdap.h"
#include "providers/ldap/sdap_id_op.h"
#include "providers/ldap/sdap_sudo.h"
static int sdap_sudo_get_ip_addresses(TALLOC_CTX *mem_ctx, char ***_ip_addr);
struct sdap_sudo_get_hostinfo_state {
char **hostnames;
char **ip_addr;
};
struct tevent_req * sdap_sudo_get_hostinfo_send(TALLOC_CTX *mem_ctx,
struct sdap_options *opts,
struct be_ctx *be_ctx)
{
struct tevent_req *req = NULL;
struct sdap_sudo_get_hostinfo_state *state = NULL;
char *conf_hostnames = NULL;
char *conf_ip_addr = NULL;
int ret;
/* create request */
req = tevent_req_create(mem_ctx, &state, struct sdap_sudo_get_hostinfo_state);
if (req == NULL) {
DEBUG(SSSDBG_FATAL_FAILURE, ("tevent_req_create() failed\n"));
return NULL;
}
state->hostnames = NULL;
state->ip_addr = NULL;
/* load info from configuration */
conf_hostnames = dp_opt_get_string(opts->basic, SDAP_SUDO_HOSTNAMES);
conf_ip_addr = dp_opt_get_string(opts->basic, SDAP_SUDO_IP);
if (conf_hostnames != NULL) {
ret = split_on_separator(state, conf_hostnames, ' ', true,
&state->hostnames, NULL);
if (ret != EOK) {
DEBUG(SSSDBG_MINOR_FAILURE,
("Unable to parse hostnames [%d]: %s\n", ret, strerror(ret)));
goto done;
} else {
DEBUG(SSSDBG_CONF_SETTINGS, ("Hostnames set to: %s\n", conf_hostnames));
}
}
if (conf_ip_addr != NULL) {
ret = split_on_separator(state, conf_ip_addr, ' ', true,
&state->ip_addr, NULL);
if (ret != EOK) {
DEBUG(SSSDBG_MINOR_FAILURE,
("Unable to parse IP addresses [%d]: %s\n", ret, strerror(ret)));
goto done;
} else {
DEBUG(SSSDBG_CONF_SETTINGS, ("IP addresses set to: %s\n", conf_ip_addr));
}
}
/* if IP addresses are not specified, configure it automatically */
if (state->ip_addr == NULL) {
ret = sdap_sudo_get_ip_addresses(state, &state->ip_addr);
if (ret != EOK) {
}
}
done:
if (ret != EAGAIN) {
if (ret == EOK) {
tevent_req_done(req);
} else {
tevent_req_error(req, ret);
}
tevent_req_post(req, be_ctx->ev);
}
return req;
}
int sdap_sudo_get_hostinfo_recv(TALLOC_CTX *mem_ctx,
struct tevent_req *req,
char ***hostnames, char ***ip_addr)
{
struct sdap_sudo_get_hostinfo_state *state = NULL;
state = tevent_req_data(req, struct sdap_sudo_get_hostinfo_state);
TEVENT_REQ_RETURN_ON_ERROR(req);
*hostnames = talloc_steal(mem_ctx, state->hostnames);
*ip_addr = talloc_steal(mem_ctx, state->ip_addr);
return EOK;
}
static int sdap_sudo_get_ip_addresses(TALLOC_CTX *mem_ctx, char ***_ip_addr_list)
{
TALLOC_CTX *tmp_ctx = NULL;
char **ip_addr_list = NULL;
struct ifaddrs *ifaces = NULL;
struct ifaddrs *iface = NULL;
struct sockaddr_in *ip4_addr = NULL;
struct sockaddr_in *ip4_network = NULL;
struct sockaddr_in6 *ip6_addr = NULL;
struct sockaddr_in6 *ip6_network = NULL;
char ip_addr[INET6_ADDRSTRLEN + 1];
char network_addr[INET6_ADDRSTRLEN + 1];
in_addr_t ip4_netmask = 0;
uint32_t ip6_netmask = 0;
unsigned int netmask = 0;
void *sinx_addr = NULL;
void *sinx_network = NULL;
int addr_count = 0;
int ret;
int i;
tmp_ctx = talloc_new(NULL);
if (tmp_ctx == NULL) {
DEBUG(SSSDBG_CRIT_FAILURE, ("talloc_new() failed\n"));
return ENOMEM;
}
errno = 0;
ret = getifaddrs(&ifaces);
if (ret == -1) {
ret = errno;
DEBUG(SSSDBG_CRIT_FAILURE, ("Could not read interfaces [%d][%s]\n",
ret, strerror(ret)));
goto done;
}
for (iface = ifaces; iface != NULL; iface = iface->ifa_next) {
/* Some interfaces don't have an ifa_addr */
if (!iface->ifa_addr) continue;
netmask = 0;
switch (iface->ifa_addr->sa_family) {
case AF_INET:
ip4_addr = (struct sockaddr_in*)(iface->ifa_addr);
ip4_network = (struct sockaddr_in*)(iface->ifa_netmask);
/* ignore loopback */
if (inet_netof(ip4_addr->sin_addr) == IN_LOOPBACKNET) {
continue;
}
/* ignore multicast */
if (IN_MULTICAST(ip4_addr->sin_addr.s_addr)) {
continue;
}
/* ignore broadcast */
if (ntohl(ip4_addr->sin_addr.s_addr) == INADDR_BROADCAST) {
continue;
}
/* get network mask length */
ip4_netmask = ntohl(ip4_network->sin_addr.s_addr);
while (ip4_netmask) {
netmask++;
ip4_netmask <<= 1;
}
/* get network address */
ip4_network->sin_addr.s_addr = ip4_addr->sin_addr.s_addr
& ip4_network->sin_addr.s_addr;
sinx_addr = &ip4_addr->sin_addr;
sinx_network = &ip4_network->sin_addr;
break;
case AF_INET6:
ip6_addr = (struct sockaddr_in6*)(iface->ifa_addr);
ip6_network = (struct sockaddr_in6*)(iface->ifa_netmask);
/* ignore loopback */
if (IN6_IS_ADDR_LOOPBACK(&ip6_addr->sin6_addr)) {
continue;
}
/* ignore multicast */
if (IN6_IS_ADDR_MULTICAST(&ip6_addr->sin6_addr)) {
continue;
}
/* get network mask length */
for (i = 0; i < 4; i++) {
ip6_netmask = ntohl(((uint32_t*)(&ip6_network->sin6_addr))[i]);
while (ip6_netmask) {
netmask++;
ip6_netmask <<= 1;
}
}
/* get network address */
for (i = 0; i < 4; i++) {
((uint32_t*)(&ip6_network->sin6_addr))[i] =
((uint32_t*)(&ip6_addr->sin6_addr))[i]
& ((uint32_t*)(&ip6_network->sin6_addr))[i];
}
sinx_addr = &ip6_addr->sin6_addr;
sinx_network = &ip6_network->sin6_addr;
break;
default:
/* skip other families */
continue;
}
/* ip address */
errno = 0;
if (inet_ntop(iface->ifa_addr->sa_family, sinx_addr,
ip_addr, INET6_ADDRSTRLEN) == NULL) {
ret = errno;
DEBUG(SSSDBG_MINOR_FAILURE, ("inet_ntop() failed [%d]: %s\n",
ret, strerror(ret)));
goto done;
}
/* network */
errno = 0;
if (inet_ntop(iface->ifa_addr->sa_family, sinx_network,
network_addr, INET6_ADDRSTRLEN) == NULL) {
ret = errno;
DEBUG(SSSDBG_MINOR_FAILURE, ("inet_ntop() failed [%d]: %s\n",
ret, strerror(ret)));
goto done;
}
addr_count += 2;
ip_addr_list = talloc_realloc(tmp_ctx, ip_addr_list, char*,
addr_count + 1);
if (ip_addr_list == NULL) {
ret = ENOMEM;
goto done;
}
ip_addr_list[addr_count - 2] = talloc_strdup(ip_addr_list, ip_addr);
if (ip_addr_list[addr_count - 2] == NULL) {
ret = ENOMEM;
goto done;
}
ip_addr_list[addr_count - 1] = talloc_asprintf(ip_addr_list, "%s/%d",
network_addr, netmask);
if (ip_addr_list[addr_count - 1] == NULL) {
ret = ENOMEM;
goto done;
}
DEBUG(SSSDBG_TRACE_INTERNAL,
("Found IP address: %s in network %s/%d\n",
ip_addr, network_addr, netmask));
}
if (ip_addr_list) {
ip_addr_list[addr_count] = NULL;
}
*_ip_addr_list = talloc_steal(mem_ctx, ip_addr_list);
done:
freeifaddrs(ifaces);
talloc_free(tmp_ctx);
return ret;
}
|