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 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503
|
/* whack communicating routines
* Copyright (C) 1997 Angelos D. Keromytis.
* Copyright (C) 1998-2001 D. Hugh Redelmeier.
*
* 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 2 of the License, or (at your
* option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
*
* 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.
*
* RCSID $Id: rcv_whack.c,v 1.92 2003/09/18 02:21:06 mcr Exp $
*/
#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <resolv.h>
#include <arpa/nameser.h> /* missing from <resolv.h> on old systems */
#include <sys/queue.h>
#include <freeswan.h>
#include "constants.h"
#include "defs.h"
#include "id.h"
#include "connections.h" /* needs id.h */
#include "foodgroups.h"
#include "whack.h" /* needs connections.h */
#include "packet.h"
#include "demux.h" /* needs packet.h */
#include "state.h"
#include "ipsec_doi.h" /* needs demux.h and state.h */
#include "kernel.h" /* needs connections.h */
#include "rcv_whack.h"
#include "log.h"
#include "keys.h"
#include "adns.h" /* needs <resolv.h> */
#include "dnskey.h" /* needs keys.h and adns.h */
#include "server.h"
/* helper variables and function to decode strings from whack message */
static char *next_str
, *str_roof;
static bool
unpack_str(char **p)
{
char *end = memchr(next_str, '\0', str_roof - next_str);
if (end == NULL)
{
return FALSE; /* fishy: no end found */
}
else
{
*p = next_str == end? NULL : next_str;
next_str = end + 1;
return TRUE;
}
}
/* bits loading keys from asynchronous DNS */
enum key_add_attempt {
ka_TXT,
#ifdef USE_KEYRR
ka_KEY,
#endif
ka_roof /* largest value + 1 */
};
struct key_add_common {
int refCount;
char *diag[ka_roof];
int whack_fd;
bool success;
};
struct key_add_continuation {
struct adns_continuation ac; /* common prefix */
struct key_add_common *common; /* common data */
enum key_add_attempt lookingfor;
};
static void
key_add_ugh(const struct id *keyid, err_t ugh)
{
char name[IDTOA_BUF]; /* longer IDs will be truncated in message */
(void)idtoa(keyid, name, sizeof(name));
loglog(RC_NOKEY
, "failure to fetch key for %s from DNS: %s", name, ugh);
}
/* last one out: turn out the lights */
static void
key_add_merge(struct key_add_common *oc, const struct id *keyid)
{
if (oc->refCount == 0)
{
enum key_add_attempt kaa;
/* if no success, print all diagnostics */
if (!oc->success)
for (kaa = ka_TXT; kaa != ka_roof; kaa++)
key_add_ugh(keyid, oc->diag[kaa]);
for (kaa = ka_TXT; kaa != ka_roof; kaa++)
pfreeany(oc->diag[kaa]);
close(oc->whack_fd);
pfree(oc);
}
}
static void
key_add_continue(struct adns_continuation *ac, err_t ugh)
{
struct key_add_continuation *kc = (void *) ac;
struct key_add_common *oc = kc->common;
passert(whack_log_fd == NULL_FD);
whack_log_fd = oc->whack_fd;
if (ugh != NULL)
{
oc->diag[kc->lookingfor] = clone_str(ugh, "key add error");
}
else
{
oc->success = TRUE;
transfer_to_public_keys(kc->ac.gateways_from_dns
#ifdef USE_KEYRR
, &kc->ac.keys_from_dns
#endif /* USE_KEYRR */
);
}
oc->refCount--;
key_add_merge(oc, &ac->id);
whack_log_fd = NULL_FD;
}
static void
key_add_request(const struct whack_message *msg)
{
struct id keyid;
err_t ugh = atoid(msg->keyid, &keyid, FALSE);
if (ugh != NULL)
{
loglog(RC_BADID, "bad --keyid \"%s\": %s", msg->keyid, ugh);
}
else
{
if (!msg->whack_addkey)
delete_public_keys(&keyid, msg->pubkey_alg);
if (msg->keyval.len == 0)
{
struct key_add_common *oc
= alloc_thing(struct key_add_common
, "key add common things");
enum key_add_attempt kaa;
/* initialize state shared by queries */
oc->refCount = 0;
oc->whack_fd = dup_any(whack_log_fd);
oc->success = FALSE;
for (kaa = ka_TXT; kaa != ka_roof; kaa++)
{
struct key_add_continuation *kc
= alloc_thing(struct key_add_continuation
, "key add continuation");
oc->diag[kaa] = NULL;
oc->refCount++;
kc->common = oc;
kc->lookingfor = kaa;
switch (kaa)
{
case ka_TXT:
ugh = start_adns_query(&keyid
, &keyid /* same */
, T_TXT
, key_add_continue
, &kc->ac);
break;
#ifdef USE_KEYRR
case ka_KEY:
ugh = start_adns_query(&keyid
, NULL
, T_KEY
, key_add_continue
, &kc->ac);
break;
#endif /* USE_KEYRR */
default:
bad_case(kaa); /* suppress gcc warning */
}
if (ugh != NULL)
{
oc->diag[kaa] = clone_str(ugh, "early key add failure");
oc->refCount--;
}
}
/* Done launching queries.
* Handle total failure case.
*/
key_add_merge(oc, &keyid);
}
else
{
ugh = add_public_key(&keyid, DAL_LOCAL, msg->pubkey_alg
, &msg->keyval, &pubkeys);
if (ugh != NULL)
loglog(RC_LOG_SERIOUS, "%s", ugh);
}
}
}
/* Handle a kernel request. Supposedly, there's a message in
* the kernelsock socket.
*/
void
whack_handle(int whackctlfd)
{
struct whack_message msg;
struct sockaddr_un whackaddr;
int whackaddrlen = sizeof(whackaddr);
int whackfd = accept(whackctlfd, (struct sockaddr *)&whackaddr, &whackaddrlen);
/* Note: actual value in n should fit in int. To print, cast to int. */
ssize_t n;
if (whackfd < 0)
{
log_errno((e, "accept() failed in whack_handle()"));
return;
}
n = read(whackfd, &msg, sizeof(msg));
if (n == -1)
{
log_errno((e, "read() failed in whack_handle()"));
close(whackfd);
return;
}
whack_log_fd = whackfd;
/* sanity check message */
{
err_t ugh = NULL;
next_str = msg.string;
str_roof = (char *)&msg + n;
if ((size_t)n < offsetof(struct whack_message, whack_shutdown) + sizeof(msg.whack_shutdown))
{
ugh = builddiag("ignoring runt message from whack: got %d bytes", n);
}
else if (msg.magic != WHACK_MAGIC)
{
if (msg.magic == WHACK_BASIC_MAGIC)
{
/* Only basic commands. Simpler inter-version compatability. */
if (msg.whack_status)
show_status();
if (msg.whack_shutdown)
{
plog("shutting down");
exit_pluto(0); /* delete lock and leave, with 0 status */
}
ugh = ""; /* bail early, but without complaint */
}
else
{
ugh = builddiag("ignoring message from whack with bad magic %d; should be %d; probably wrong version"
, msg.magic, WHACK_MAGIC);
}
}
else if (next_str > str_roof)
{
ugh = builddiag("ignoring truncated message from whack: got %d bytes; expected %u"
, (int) n, (unsigned) sizeof(msg));
}
else if (!unpack_str(&msg.name) /* string 1 */
|| !unpack_str(&msg.left.id) /* string 2 */
|| !unpack_str(&msg.left.updown) /* string 3 */
|| !unpack_str(&msg.right.id) /* string 4 */
|| !unpack_str(&msg.right.updown) /* string 5 */
|| !unpack_str(&msg.keyid) /* string 6 */
|| !unpack_str(&msg.myid) /* string 7 */
|| str_roof - next_str != (ptrdiff_t)msg.keyval.len) /* check chunk */
{
ugh = "message from whack contains bad string";
}
else
{
msg.keyval.ptr = next_str; /* grab chunk */
}
if (ugh != NULL)
{
if (*ugh != '\0')
loglog(RC_BADWHACKMESSAGE, "%s", ugh);
whack_log_fd = NULL_FD;
close(whackfd);
return;
}
}
if (msg.whack_options)
{
#ifdef DEBUG
if (msg.name == NULL)
{
/* we do a two-step so that if either old or new would
* cause the message to print, it will be printed.
*/
cur_debugging |= msg.debugging;
DBG(DBG_CONTROL
, DBG_log("base debugging = %s"
, bitnamesof(debug_bit_names, msg.debugging)));
cur_debugging = base_debugging = msg.debugging;
}
else if (!msg.whack_connection)
{
struct connection *c = con_by_name(msg.name, TRUE);
if (c != NULL)
{
c->extra_debugging = msg.debugging;
DBG(DBG_CONTROL
, DBG_log("\"%s\" extra_debugging = %s"
, c->name
, bitnamesof(debug_bit_names, c->extra_debugging)));
}
}
#endif
}
if (msg.whack_myid)
set_myid(MYID_SPECIFIED, msg.myid);
/* Deleting combined with adding a connection works as replace.
* To make this more useful, in only this combination,
* delete will silently ignore the lack of the connection.
*/
if (msg.whack_delete)
delete_connections_by_name(msg.name, !msg.whack_connection);
if (msg.whack_deletestate)
{
struct state *st = state_with_serialno(msg.whack_deletestateno);
if (st == NULL)
{
loglog(RC_UNKNOWN_NAME, "no state #%lu to delete"
, msg.whack_deletestateno);
}
else
{
delete_state(st);
}
}
if (msg.whack_crash)
delete_states_by_peer(&msg.whack_crash_peer);
if (msg.whack_connection)
add_connection(&msg);
/* process "listen" before any operation that could require it */
if (msg.whack_listen)
{
close_peerlog(); /* close any open per-peer logs */
plog("listening for IKE messages");
listening = TRUE;
daily_log_reset();
reset_adns_restart_count();
set_myFQDN();
find_ifaces();
load_preshared_secrets();
load_groups();
}
if (msg.whack_unlisten)
{
plog("no longer listening for IKE messages");
listening = FALSE;
}
if (msg.whack_key)
{
/* add a public key */
key_add_request(&msg);
}
if (msg.whack_route)
{
if (!listening)
whack_log(RC_DEAF, "need --listen before --route");
else
{
struct connection *c = con_by_name(msg.name, TRUE);
if (c != NULL)
{
set_cur_connection(c);
if (!oriented(*c))
whack_log(RC_ORIENT
, "we have no ipsecN interface for either end of this connection");
else if (c->policy & POLICY_GROUP)
route_group(c);
else if (!trap_connection(c))
whack_log(RC_ROUTE, "could not route");
reset_cur_connection();
}
}
}
if (msg.whack_unroute)
{
struct connection *c = con_by_name(msg.name, TRUE);
if (c != NULL)
{
struct spd_route *sr;
int fail = 0;
set_cur_connection(c);
for (sr = &c->spd; sr != NULL; sr = sr->next)
{
if (sr->routing >= RT_ROUTED_TUNNEL)
fail++;
}
if (fail > 0)
whack_log(RC_RTBUSY, "cannot unroute: route busy");
else if (c->policy & POLICY_GROUP)
unroute_group(c);
else
unroute_connection(c);
reset_cur_connection();
}
}
if (msg.whack_initiate)
{
if (!listening)
whack_log(RC_DEAF, "need --listen before --initiate");
else
initiate_connection(msg.name
, msg.whack_async? NULL_FD : dup_any(whackfd));
}
if (msg.whack_oppo_initiate)
{
if (!listening)
whack_log(RC_DEAF, "need --listen before opportunistic initiation");
else
initiate_opportunistic(&msg.oppo_my_client, &msg.oppo_peer_client
, FALSE
, msg.whack_async? NULL_FD : dup_any(whackfd));
}
if (msg.whack_terminate)
terminate_connection(msg.name);
if (msg.whack_status)
show_status();
if (msg.whack_shutdown)
{
plog("shutting down");
exit_pluto(0); /* delete lock and leave, with 0 status */
}
whack_log_fd = NULL_FD;
close(whackfd);
}
/*
* Local Variables:
* c-basic-offset:4
* c-style: pluto
* End:
*/
|