s4-auth: Use kerberos util functions in srv_keytab
[Samba.git] / source3 / lib / addrchange.c
blob9a628f7f6c2720edd22a093a00f4d24eaf3f08d2
1 /*
2 * Samba Unix/Linux SMB client library
3 * Copyright (C) Volker Lendecke 2011
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 #include "includes.h"
20 #include "lib/addrchange.h"
21 #include "../lib/util/tevent_ntstatus.h"
23 #if HAVE_LINUX_RTNETLINK_H
25 #include "asm/types.h"
26 #include "linux/netlink.h"
27 #include "linux/rtnetlink.h"
28 #include "lib/tsocket/tsocket.h"
30 struct addrchange_context {
31 struct tdgram_context *sock;
34 NTSTATUS addrchange_context_create(TALLOC_CTX *mem_ctx,
35 struct addrchange_context **pctx)
37 struct addrchange_context *ctx;
38 struct sockaddr_nl addr;
39 NTSTATUS status;
40 int sock = -1;
41 int res;
42 bool ok;
44 ctx = talloc(mem_ctx, struct addrchange_context);
45 if (ctx == NULL) {
46 return NT_STATUS_NO_MEMORY;
49 sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
50 if (sock == -1) {
51 status = map_nt_error_from_unix(errno);
52 goto fail;
55 ok = smb_set_close_on_exec(sock);
56 if (!ok) {
57 status = map_nt_error_from_unix(errno);
58 goto fail;
61 res = set_blocking(sock, false);
62 if (res == -1) {
63 status = map_nt_error_from_unix(errno);
64 goto fail;
68 * We're interested in address changes
70 ZERO_STRUCT(addr);
71 addr.nl_family = AF_NETLINK;
72 addr.nl_groups = RTMGRP_IPV6_IFADDR | RTMGRP_IPV4_IFADDR;
74 res = bind(sock, (struct sockaddr *)(void *)&addr, sizeof(addr));
75 if (res == -1) {
76 status = map_nt_error_from_unix(errno);
77 goto fail;
80 res = tdgram_bsd_existing_socket(ctx, sock, &ctx->sock);
81 if (res == -1) {
82 status = map_nt_error_from_unix(errno);
83 goto fail;
86 *pctx = ctx;
87 return NT_STATUS_OK;
88 fail:
89 if (sock != -1) {
90 close(sock);
92 TALLOC_FREE(ctx);
93 return status;
96 struct addrchange_state {
97 struct tevent_context *ev;
98 struct addrchange_context *ctx;
99 uint8_t *buf;
100 struct tsocket_address *fromaddr;
102 enum addrchange_type type;
103 struct sockaddr_storage addr;
106 static void addrchange_done(struct tevent_req *subreq);
108 struct tevent_req *addrchange_send(TALLOC_CTX *mem_ctx,
109 struct tevent_context *ev,
110 struct addrchange_context *ctx)
112 struct tevent_req *req, *subreq;
113 struct addrchange_state *state;
115 req = tevent_req_create(mem_ctx, &state, struct addrchange_state);
116 if (req == NULL) {
117 return NULL;
119 state->ev = ev;
120 state->ctx = ctx;
122 subreq = tdgram_recvfrom_send(state, state->ev, state->ctx->sock);
123 if (tevent_req_nomem(subreq, req)) {
124 return tevent_req_post(req, state->ev);
126 tevent_req_set_callback(subreq, addrchange_done, req);
127 return req;
130 static void addrchange_done(struct tevent_req *subreq)
132 struct tevent_req *req = tevent_req_callback_data(
133 subreq, struct tevent_req);
134 struct addrchange_state *state = tevent_req_data(
135 req, struct addrchange_state);
136 union {
137 struct sockaddr sa;
138 struct sockaddr_nl nl;
139 struct sockaddr_storage ss;
140 } fromaddr;
141 struct nlmsghdr *h;
142 struct ifaddrmsg *ifa;
143 struct rtattr *rta;
144 ssize_t received;
145 int len;
146 int err;
147 bool found;
149 received = tdgram_recvfrom_recv(subreq, &err, state,
150 &state->buf,
151 &state->fromaddr);
152 TALLOC_FREE(subreq);
153 if (received == -1) {
154 DEBUG(10, ("tdgram_recvfrom_recv returned %s\n", strerror(err)));
155 tevent_req_nterror(req, map_nt_error_from_unix(err));
156 return;
158 len = tsocket_address_bsd_sockaddr(state->fromaddr,
159 &fromaddr.sa,
160 sizeof(fromaddr));
162 if ((len != sizeof(fromaddr.nl) ||
163 fromaddr.sa.sa_family != AF_NETLINK))
165 DEBUG(10, ("Got message from wrong addr\n"));
166 goto retry;
169 if (fromaddr.nl.nl_pid != 0) {
170 DEBUG(10, ("Got msg from pid %d, not from the kernel\n",
171 (int)fromaddr.nl.nl_pid));
172 goto retry;
175 if (received < sizeof(struct nlmsghdr)) {
176 DEBUG(10, ("received %d, expected at least %d\n",
177 (int)received, (int)sizeof(struct nlmsghdr)));
178 goto retry;
181 h = (struct nlmsghdr *)state->buf;
182 if (h->nlmsg_len < sizeof(struct nlmsghdr)) {
183 DEBUG(10, ("nlmsg_len=%d, expected at least %d\n",
184 (int)h->nlmsg_len, (int)sizeof(struct nlmsghdr)));
185 goto retry;
187 if (h->nlmsg_len > received) {
188 DEBUG(10, ("nlmsg_len=%d, expected at most %d\n",
189 (int)h->nlmsg_len, (int)received));
190 goto retry;
192 switch (h->nlmsg_type) {
193 case RTM_NEWADDR:
194 state->type = ADDRCHANGE_ADD;
195 break;
196 case RTM_DELADDR:
197 state->type = ADDRCHANGE_DEL;
198 break;
199 default:
200 DEBUG(10, ("Got unexpected type %d - ignoring\n", h->nlmsg_type));
201 goto retry;
204 if (h->nlmsg_len < sizeof(struct nlmsghdr)+sizeof(struct ifaddrmsg)) {
205 DEBUG(10, ("nlmsg_len=%d, expected at least %d\n",
206 (int)h->nlmsg_len,
207 (int)(sizeof(struct nlmsghdr)
208 +sizeof(struct ifaddrmsg))));
209 tevent_req_nterror(req, NT_STATUS_UNEXPECTED_IO_ERROR);
210 return;
213 ifa = (struct ifaddrmsg *)NLMSG_DATA(h);
215 state->addr.ss_family = ifa->ifa_family;
217 rta = IFA_RTA(ifa);
218 len = h->nlmsg_len - sizeof(struct nlmsghdr) + sizeof(struct ifaddrmsg);
220 found = false;
222 for (rta = IFA_RTA(ifa); RTA_OK(rta, len); rta = RTA_NEXT(rta, len)) {
224 if ((rta->rta_type != IFA_LOCAL)
225 && (rta->rta_type != IFA_ADDRESS)) {
226 continue;
229 switch (ifa->ifa_family) {
230 case AF_INET: {
231 struct sockaddr_in *v4_addr;
232 v4_addr = (struct sockaddr_in *)(void *)&state->addr;
234 if (RTA_PAYLOAD(rta) != sizeof(uint32_t)) {
235 continue;
237 v4_addr->sin_addr.s_addr = *(uint32_t *)RTA_DATA(rta);
238 found = true;
239 break;
241 case AF_INET6: {
242 struct sockaddr_in6 *v6_addr;
243 v6_addr = (struct sockaddr_in6 *)(void *)&state->addr;
245 if (RTA_PAYLOAD(rta) !=
246 sizeof(v6_addr->sin6_addr.s6_addr)) {
247 continue;
249 memcpy(v6_addr->sin6_addr.s6_addr, RTA_DATA(rta),
250 sizeof(v6_addr->sin6_addr.s6_addr));
251 found = true;
252 break;
257 if (!found) {
258 tevent_req_nterror(req, NT_STATUS_INVALID_ADDRESS);
259 return;
262 tevent_req_done(req);
263 return;
265 retry:
266 TALLOC_FREE(state->buf);
267 TALLOC_FREE(state->fromaddr);
269 subreq = tdgram_recvfrom_send(state, state->ev, state->ctx->sock);
270 if (tevent_req_nomem(subreq, req)) {
271 return;
273 tevent_req_set_callback(subreq, addrchange_done, req);
276 NTSTATUS addrchange_recv(struct tevent_req *req, enum addrchange_type *type,
277 struct sockaddr_storage *addr)
279 struct addrchange_state *state = tevent_req_data(
280 req, struct addrchange_state);
281 NTSTATUS status;
283 if (tevent_req_is_nterror(req, &status)) {
284 tevent_req_received(req);
285 return status;
288 *type = state->type;
289 *addr = state->addr;
290 tevent_req_received(req);
291 return NT_STATUS_OK;
294 #else
296 NTSTATUS addrchange_context_create(TALLOC_CTX *mem_ctx,
297 struct addrchange_context **pctx)
299 return NT_STATUS_NOT_SUPPORTED;
302 struct tevent_req *addrchange_send(TALLOC_CTX *mem_ctx,
303 struct tevent_context *ev,
304 struct addrchange_context *ctx)
306 return NULL;
309 NTSTATUS addrchange_recv(struct tevent_req *req, enum addrchange_type *type,
310 struct sockaddr_storage *addr)
312 return NT_STATUS_NOT_IMPLEMENTED;
315 #endif