1 /* command interface to Pluto
2 * Copyright (C) 1997 Angelos D. Keromytis.
3 * Copyright (C) 1998-2001 D. Hugh Redelmeier.
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
25 #include <sys/types.h>
26 #include <sys/socket.h>
28 #include <netinet/in.h>
29 #include <arpa/inet.h>
35 #include "constants.h"
45 " [--optionsfrom <filename>]"
54 " --name <connection_name>"
57 " [--tunnelipv4 | --tunnelipv6]"
59 " (--host <ip-address> | --id <identity>)"
62 " [--ca <distinguished name>]"
63 " [--sendcert <policy>]"
65 " [--groups <access control groups>]"
67 " [--ikeport <port-number>]"
68 " [--nexthop <ip-address>]"
69 " [--srcip <ip-address>]"
71 " [--client <subnet> | --clientwithin <address range>]"
72 " [--clientprotoport <protocol>/<port>]"
75 " [--updown <updown>]"
78 " (--host <ip-address> | --id <identity>)"
81 " [--ca <distinguished name>]"
82 " [--sendcert <policy>]"
84 " [--ikeport <port-number>]"
85 " [--nexthop <ip-address>]"
86 " [--srcip <ip-address>]"
88 " [--client <subnet> | --clientwithin <address range>]"
89 " [--clientprotoport <protocol>/<port>]"
92 " [--updown <updown>]"
102 " [--ikelifetime <seconds>]"
103 " [--ipseclifetime <seconds>]"
105 " [--reykeymargin <seconds>]"
106 " [--reykeyfuzz <percentage>]"
108 " [--keyingtries <count>]"
110 " [--esp <esp-algos>]"
114 " [--dpdaction (none|clear|hold|restart)]"
116 " [--dpddelay <seconds> --dpdtimeout <seconds>]"
118 " [--initiateontraffic|--pass|--drop|--reject]"
120 " [--failnone|--failpass|--faildrop|--failreject]"
123 " (--route | --unroute)"
124 " --name <connection_name>"
129 " (--initiate | --terminate)"
130 " --name <connection_name>"
133 "opportunistic initiation: whack"
134 " [--tunnelipv4 | --tunnelipv6]"
136 " --oppohere <ip-address>"
137 " --oppothere <ip-address>"
141 " (--name <connection_name> | --caname <ca name>)"
144 " --deletestate <state_object_number>"
145 " --crash <ip-address>"
150 " [--pubkeyrsa <key>]"
159 " [--ldaphost <hostname>]"
160 " [--ldapbase <base>]"
165 " [--strictcrlpolicy]"
168 "debug: whack [--name <connection_name>]"
176 " [--debug-emitting]"
179 " [--debug-lifecycle]"
185 " [--debug-controlmore]"
190 " (--listen | --unlisten)"
192 "list: whack [--utc]"
217 " [--rereadocspcerts]"
223 " [--name <connection_name>] --status|--statusall"
226 " --scencrypt|scdecrypt <value>"
228 " [--outbase <base>]"
235 , ipsec_version_code());
238 static const char *label
= NULL
; /* --label operand, saved for diagnostics */
240 static const char *name
= NULL
; /* --name operand, saved for diagnostics */
242 /* print a string as a diagnostic, then exit whack unhappily */
244 diag(const char *mess
)
248 fprintf(stderr
, "whack error: ");
250 fprintf(stderr
, "%s ", label
);
252 fprintf(stderr
, "\"%s\" ", name
);
253 fprintf(stderr
, "%s\n", mess
);
256 exit(RC_WHACK_PROBLEM
);
259 /* conditially calls diag; prints second arg, if non-NULL, as quoted string */
261 diagq(err_t ugh
, const char *this)
271 char buf
[120]; /* arbitrary limit */
273 snprintf(buf
, sizeof(buf
), "%s \"%s\"", ugh
, this);
279 /* complex combined operands return one of these enumerated values
280 * Note: these become flags in an lset_t. Since there are more than
281 * 32, we partition them into:
282 * - OPT_* options (most random options)
283 * - LST_* options (list various internal data)
284 * - DBGOPT_* option (DEBUG options)
285 * - END_* options (End description options)
286 * - CD_* options (Connection Description options)
287 * - CA_* options (CA description options)
290 # define OPT_FIRST OPT_CTLBASE
332 # define OPT_LAST OPT_ASYNC /* last "normal" option */
334 /* Smartcard options */
336 # define SC_FIRST SC_ENCRYPT /* first smartcard option */
343 # define SC_LAST SC_OUTBASE /* last "smartcard" option */
347 # define LST_FIRST LST_UTC /* first list option */
363 # define LST_LAST LST_ALL /* last list option */
365 /* Connection End Description options */
367 # define END_FIRST END_HOST /* first end description */
384 #define END_LAST END_UPDOWN /* last end description*/
386 /* Connection Description options -- segregated */
388 # define CD_FIRST CD_TO /* first connection description */
391 # define CD_POLICY_FIRST CD_PSK
392 CD_PSK
, /* same order as POLICY_* */
393 CD_RSASIG
, /* same order as POLICY_* */
394 CD_ENCRYPT
, /* same order as POLICY_* */
395 CD_AUTHENTICATE
, /* same order as POLICY_* */
396 CD_COMPRESS
, /* same order as POLICY_* */
397 CD_TUNNEL
, /* same order as POLICY_* */
398 CD_PFS
, /* same order as POLICY_* */
399 CD_DISABLEARRIVALCHECK
, /* same order as POLICY_* */
400 CD_SHUNT0
, /* same order as POLICY_* */
401 CD_SHUNT1
, /* same order as POLICY_* */
402 CD_FAIL0
, /* same order as POLICY_* */
403 CD_FAIL1
, /* same order as POLICY_* */
404 CD_DONT_REKEY
, /* same order as POLICY_* */
423 # define CD_LAST CD_ESP /* last connection description */
425 /* Certificate Authority (CA) description options */
427 # define CA_FIRST CA_NAME /* first ca description */
438 # define CA_LAST CA_STRICT /* last ca description */
440 #ifdef DEBUG /* must be last so others are less than 32 to fit in lset_t */
441 # define DBGOPT_FIRST DBGOPT_NONE
443 /* NOTE: these definitions must match DBG_* and IMPAIR_* in constants.h */
447 DBGOPT_RAW
, /* same order as DBG_* */
448 DBGOPT_CRYPT
, /* same order as DBG_* */
449 DBGOPT_PARSING
, /* same order as DBG_* */
450 DBGOPT_EMITTING
, /* same order as DBG_* */
451 DBGOPT_CONTROL
, /* same order as DBG_* */
452 DBGOPT_LIFECYCLE
, /* same order as DBG_* */
453 DBGOPT_KLIPS
, /* same order as DBG_* */
454 DBGOPT_DNS
, /* same order as DBG_* */
455 DBGOPT_NATT
, /* same order as DBG_* */
456 DBGOPT_OPPO
, /* same order as DBG_* */
457 DBGOPT_CONTROLMORE
, /* same order as DBG_* */
459 DBGOPT_PRIVATE
, /* same order as DBG_* */
461 DBGOPT_IMPAIR_DELAY_ADNS_KEY_ANSWER
, /* same order as IMPAIR_* */
462 DBGOPT_IMPAIR_DELAY_ADNS_TXT_ANSWER
, /* same order as IMPAIR_* */
463 DBGOPT_IMPAIR_BUST_MI2
, /* same order as IMPAIR_* */
464 DBGOPT_IMPAIR_BUST_MR2
/* same order as IMPAIR_* */
466 # define DBGOPT_LAST DBGOPT_IMPAIR_BUST_MR2
471 /* Carve up space for result from getop_long.
472 * Stupidly, the only result is an int.
473 * Numeric arg is bit immediately left of basic value.
476 #define OPTION_OFFSET 256 /* to get out of the way of letter options */
477 #define NUMERIC_ARG (1 << 9) /* expect a numeric argument */
478 #define AUX_SHIFT 10 /* amount to shift for aux information */
480 static const struct option long_opts
[] = {
481 # define OO OPTION_OFFSET
482 /* name, has_arg, flag, val */
484 { "help", no_argument
, NULL
, 'h' },
485 { "version", no_argument
, NULL
, 'v' },
486 { "optionsfrom", required_argument
, NULL
, '+' },
487 { "label", required_argument
, NULL
, 'l' },
489 { "ctlbase", required_argument
, NULL
, OPT_CTLBASE
+ OO
},
490 { "name", required_argument
, NULL
, OPT_NAME
+ OO
},
492 { "keyid", required_argument
, NULL
, OPT_KEYID
+ OO
},
493 { "addkey", no_argument
, NULL
, OPT_ADDKEY
+ OO
},
494 { "pubkeyrsa", required_argument
, NULL
, OPT_PUBKEYRSA
+ OO
},
496 { "myid", required_argument
, NULL
, OPT_MYID
+ OO
},
498 { "route", no_argument
, NULL
, OPT_ROUTE
+ OO
},
499 { "unroute", no_argument
, NULL
, OPT_UNROUTE
+ OO
},
501 { "initiate", no_argument
, NULL
, OPT_INITIATE
+ OO
},
502 { "terminate", no_argument
, NULL
, OPT_TERMINATE
+ OO
},
503 { "delete", no_argument
, NULL
, OPT_DELETE
+ OO
},
504 { "deletestate", required_argument
, NULL
, OPT_DELETESTATE
+ OO
+ NUMERIC_ARG
},
505 { "crash", required_argument
, NULL
, OPT_DELETECRASH
+ OO
},
506 { "listen", no_argument
, NULL
, OPT_LISTEN
+ OO
},
507 { "unlisten", no_argument
, NULL
, OPT_UNLISTEN
+ OO
},
509 { "purgeocsp", no_argument
, NULL
, OPT_PURGEOCSP
+ OO
},
511 { "rereadsecrets", no_argument
, NULL
, OPT_REREADSECRETS
+ OO
},
512 { "rereadcacerts", no_argument
, NULL
, OPT_REREADCACERTS
+ OO
},
513 { "rereadaacerts", no_argument
, NULL
, OPT_REREADAACERTS
+ OO
},
514 { "rereadocspcerts", no_argument
, NULL
, OPT_REREADOCSPCERTS
+ OO
},
515 { "rereadacerts", no_argument
, NULL
, OPT_REREADACERTS
+ OO
},
516 { "rereadcrls", no_argument
, NULL
, OPT_REREADCRLS
+ OO
},
517 { "rereadall", no_argument
, NULL
, OPT_REREADALL
+ OO
},
518 { "status", no_argument
, NULL
, OPT_STATUS
+ OO
},
519 { "statusall", no_argument
, NULL
, OPT_STATUSALL
+ OO
},
520 { "shutdown", no_argument
, NULL
, OPT_SHUTDOWN
+ OO
},
522 { "oppohere", required_argument
, NULL
, OPT_OPPO_HERE
+ OO
},
523 { "oppothere", required_argument
, NULL
, OPT_OPPO_THERE
+ OO
},
525 { "asynchronous", no_argument
, NULL
, OPT_ASYNC
+ OO
},
527 /* smartcard options */
529 { "scencrypt", required_argument
, NULL
, SC_ENCRYPT
+ OO
},
530 { "scdecrypt", required_argument
, NULL
, SC_DECRYPT
+ OO
},
531 { "inbase", required_argument
, NULL
, SC_INBASE
+ OO
},
532 { "outbase", required_argument
, NULL
, SC_OUTBASE
+ OO
},
536 { "utc", no_argument
, NULL
, LST_UTC
+ OO
},
537 { "listalgs", no_argument
, NULL
, LST_ALGS
+ OO
},
538 { "listpubkeys", no_argument
, NULL
, LST_PUBKEYS
+ OO
},
539 { "listcerts", no_argument
, NULL
, LST_CERTS
+ OO
},
540 { "listcacerts", no_argument
, NULL
, LST_CACERTS
+ OO
},
541 { "listacerts", no_argument
, NULL
, LST_ACERTS
+ OO
},
542 { "listaacerts", no_argument
, NULL
, LST_AACERTS
+ OO
},
543 { "listocspcerts", no_argument
, NULL
, LST_OCSPCERTS
+ OO
},
544 { "listgroups", no_argument
, NULL
, LST_GROUPS
+ OO
},
545 { "listcainfos", no_argument
, NULL
, LST_CAINFOS
+ OO
},
546 { "listcrls", no_argument
, NULL
, LST_CRLS
+ OO
},
547 { "listocsp", no_argument
, NULL
, LST_OCSP
+ OO
},
548 { "listcards", no_argument
, NULL
, LST_CARDS
+ OO
},
549 { "listall", no_argument
, NULL
, LST_ALL
+ OO
},
551 /* options for an end description */
553 { "host", required_argument
, NULL
, END_HOST
+ OO
},
554 { "id", required_argument
, NULL
, END_ID
+ OO
},
555 { "cert", required_argument
, NULL
, END_CERT
+ OO
},
556 { "ca", required_argument
, NULL
, END_CA
+ OO
},
557 { "sendcert", required_argument
, NULL
, END_SENDCERT
+ OO
},
558 { "groups", required_argument
, NULL
, END_GROUPS
+ OO
},
559 { "ikeport", required_argument
, NULL
, END_IKEPORT
+ OO
+ NUMERIC_ARG
},
560 { "nexthop", required_argument
, NULL
, END_NEXTHOP
+ OO
},
561 { "client", required_argument
, NULL
, END_CLIENT
+ OO
},
562 { "clientwithin", required_argument
, NULL
, END_CLIENTWITHIN
+ OO
},
563 { "clientprotoport", required_argument
, NULL
, END_CLIENTPROTOPORT
+ OO
},
564 { "dnskeyondemand", no_argument
, NULL
, END_DNSKEYONDEMAND
+ OO
},
565 { "srcip", required_argument
, NULL
, END_SRCIP
+ OO
},
566 { "hostaccess", no_argument
, NULL
, END_HOSTACCESS
+ OO
},
567 { "updown", required_argument
, NULL
, END_UPDOWN
+ OO
},
569 /* options for a connection description */
571 { "to", no_argument
, NULL
, CD_TO
+ OO
},
573 { "psk", no_argument
, NULL
, CD_PSK
+ OO
},
574 { "rsasig", no_argument
, NULL
, CD_RSASIG
+ OO
},
576 { "encrypt", no_argument
, NULL
, CD_ENCRYPT
+ OO
},
577 { "authenticate", no_argument
, NULL
, CD_AUTHENTICATE
+ OO
},
578 { "compress", no_argument
, NULL
, CD_COMPRESS
+ OO
},
579 { "tunnel", no_argument
, NULL
, CD_TUNNEL
+ OO
},
580 { "tunnelipv4", no_argument
, NULL
, CD_TUNNELIPV4
+ OO
},
581 { "tunnelipv6", no_argument
, NULL
, CD_TUNNELIPV6
+ OO
},
582 { "pfs", no_argument
, NULL
, CD_PFS
+ OO
},
583 { "disablearrivalcheck", no_argument
, NULL
, CD_DISABLEARRIVALCHECK
+ OO
},
584 { "initiateontraffic", no_argument
, NULL
585 , CD_SHUNT0
+ (POLICY_SHUNT_TRAP
>> POLICY_SHUNT_SHIFT
<< AUX_SHIFT
) + OO
},
586 { "pass", no_argument
, NULL
587 , CD_SHUNT0
+ (POLICY_SHUNT_PASS
>> POLICY_SHUNT_SHIFT
<< AUX_SHIFT
) + OO
},
588 { "drop", no_argument
, NULL
589 , CD_SHUNT0
+ (POLICY_SHUNT_DROP
>> POLICY_SHUNT_SHIFT
<< AUX_SHIFT
) + OO
},
590 { "reject", no_argument
, NULL
591 , CD_SHUNT0
+ (POLICY_SHUNT_REJECT
>> POLICY_SHUNT_SHIFT
<< AUX_SHIFT
) + OO
},
592 { "failnone", no_argument
, NULL
593 , CD_FAIL0
+ (POLICY_FAIL_NONE
>> POLICY_FAIL_SHIFT
<< AUX_SHIFT
) + OO
},
594 { "failpass", no_argument
, NULL
595 , CD_FAIL0
+ (POLICY_FAIL_PASS
>> POLICY_FAIL_SHIFT
<< AUX_SHIFT
) + OO
},
596 { "faildrop", no_argument
, NULL
597 , CD_FAIL0
+ (POLICY_FAIL_DROP
>> POLICY_FAIL_SHIFT
<< AUX_SHIFT
) + OO
},
598 { "failreject", no_argument
, NULL
599 , CD_FAIL0
+ (POLICY_FAIL_REJECT
>> POLICY_FAIL_SHIFT
<< AUX_SHIFT
) + OO
},
600 { "dontrekey", no_argument
, NULL
, CD_DONT_REKEY
+ OO
},
601 { "ipv4", no_argument
, NULL
, CD_CONNIPV4
+ OO
},
602 { "ipv6", no_argument
, NULL
, CD_CONNIPV6
+ OO
},
604 { "ikelifetime", required_argument
, NULL
, CD_IKELIFETIME
+ OO
+ NUMERIC_ARG
},
605 { "ipseclifetime", required_argument
, NULL
, CD_IPSECLIFETIME
+ OO
+ NUMERIC_ARG
},
606 { "rekeymargin", required_argument
, NULL
, CD_RKMARGIN
+ OO
+ NUMERIC_ARG
},
607 { "rekeywindow", required_argument
, NULL
, CD_RKMARGIN
+ OO
+ NUMERIC_ARG
}, /* OBSOLETE */
608 { "rekeyfuzz", required_argument
, NULL
, CD_RKFUZZ
+ OO
+ NUMERIC_ARG
},
609 { "keyingtries", required_argument
, NULL
, CD_KTRIES
+ OO
+ NUMERIC_ARG
},
610 { "dpdaction", required_argument
, NULL
, CD_DPDACTION
+ OO
},
611 { "dpddelay", required_argument
, NULL
, CD_DPDDELAY
+ OO
+ NUMERIC_ARG
},
612 { "dpdtimeout", required_argument
, NULL
, CD_DPDTIMEOUT
+ OO
+ NUMERIC_ARG
},
613 { "ike", required_argument
, NULL
, CD_IKE
+ OO
},
614 { "pfsgroup", required_argument
, NULL
, CD_PFSGROUP
+ OO
},
615 { "esp", required_argument
, NULL
, CD_ESP
+ OO
},
617 /* options for a ca description */
619 { "caname", required_argument
, NULL
, CA_NAME
+ OO
},
620 { "cacert", required_argument
, NULL
, CA_CERT
+ OO
},
621 { "ldaphost", required_argument
, NULL
, CA_LDAPHOST
+ OO
},
622 { "ldapbase", required_argument
, NULL
, CA_LDAPBASE
+ OO
},
623 { "crluri", required_argument
, NULL
, CA_CRLURI
+ OO
},
624 { "crluri2", required_argument
, NULL
, CA_CRLURI2
+ OO
},
625 { "ocspuri", required_argument
, NULL
, CA_OCSPURI
+ OO
},
626 { "strictcrlpolicy", no_argument
, NULL
, CA_STRICT
+ OO
},
629 { "debug-none", no_argument
, NULL
, DBGOPT_NONE
+ OO
},
630 { "debug-all]", no_argument
, NULL
, DBGOPT_ALL
+ OO
},
631 { "debug-raw", no_argument
, NULL
, DBGOPT_RAW
+ OO
},
632 { "debug-crypt", no_argument
, NULL
, DBGOPT_CRYPT
+ OO
},
633 { "debug-parsing", no_argument
, NULL
, DBGOPT_PARSING
+ OO
},
634 { "debug-emitting", no_argument
, NULL
, DBGOPT_EMITTING
+ OO
},
635 { "debug-control", no_argument
, NULL
, DBGOPT_CONTROL
+ OO
},
636 { "debug-lifecycle", no_argument
, NULL
, DBGOPT_LIFECYCLE
+ OO
},
637 { "debug-klips", no_argument
, NULL
, DBGOPT_KLIPS
+ OO
},
638 { "debug-dns", no_argument
, NULL
, DBGOPT_DNS
+ OO
},
639 { "debug-natt", no_argument
, NULL
, DBGOPT_NATT
+ OO
},
640 { "debug-oppo", no_argument
, NULL
, DBGOPT_OPPO
+ OO
},
641 { "debug-controlmore", no_argument
, NULL
, DBGOPT_CONTROLMORE
+ OO
},
642 { "debug-private", no_argument
, NULL
, DBGOPT_PRIVATE
+ OO
},
644 { "impair-delay-adns-key-answer", no_argument
, NULL
, DBGOPT_IMPAIR_DELAY_ADNS_KEY_ANSWER
+ OO
},
645 { "impair-delay-adns-txt-answer", no_argument
, NULL
, DBGOPT_IMPAIR_DELAY_ADNS_TXT_ANSWER
+ OO
},
646 { "impair-bust-mi2", no_argument
, NULL
, DBGOPT_IMPAIR_BUST_MI2
+ OO
},
647 { "impair-bust-mr2", no_argument
, NULL
, DBGOPT_IMPAIR_BUST_MR2
+ OO
},
653 struct sockaddr_un ctl_addr
= { AF_UNIX
, DEFAULT_CTLBASE CTL_SUFFIX
};
655 /* helper variables and function to encode strings from whack message */
664 const char *s
= *p
== NULL?
"" : *p
; /* note: NULL becomes ""! */
665 size_t len
= strlen(s
) + 1;
667 if (str_roof
- next_str
< (ptrdiff_t)len
)
669 return FALSE
; /* fishy: no end found */
675 *p
= NULL
; /* don't send pointers on the wire! */
681 check_life_time(time_t life
, time_t limit
, const char *which
682 , const whack_message_t
*msg
)
684 time_t mint
= msg
->sa_rekey_margin
* (100 + msg
->sa_rekey_fuzz
) / 100;
688 char buf
[200]; /* arbitrary limit */
690 snprintf(buf
, sizeof(buf
)
691 , "%s [%lu seconds] must be less than %lu seconds"
692 , which
, (unsigned long)life
, (unsigned long)limit
);
695 if ((msg
->policy
& POLICY_DONT_REKEY
) == LEMPTY
&& life
<= mint
)
697 char buf
[200]; /* arbitrary limit */
699 snprintf(buf
, sizeof(buf
)
700 , "%s [%lu] must be greater than"
701 " rekeymargin*(100+rekeyfuzz)/100 [%lu*(100+%lu)/100 = %lu]"
703 , (unsigned long)life
704 , (unsigned long)msg
->sa_rekey_margin
705 , (unsigned long)msg
->sa_rekey_fuzz
706 , (unsigned long)mint
);
712 clear_end(whack_end_t
*e
)
719 e
->host_port
= IKE_UDP_PORT
;
723 update_ports(whack_message_t
*m
)
727 if (m
->left
.port
!= 0) {
728 port
= htons(m
->left
.port
);
729 setportof(port
, &m
->left
.host_addr
);
730 setportof(port
, &m
->left
.client
.addr
);
732 if (m
->right
.port
!= 0) {
733 port
= htons(m
->right
.port
);
734 setportof(port
, &m
->right
.host_addr
);
735 setportof(port
, &m
->right
.client
.addr
);
740 check_end(whack_end_t
*this, whack_end_t
*that
741 , bool default_nexthop
, sa_family_t caf
, sa_family_t taf
)
743 if (caf
!= addrtypeof(&this->host_addr
))
744 diag("address family of host inconsistent");
748 if (isanyaddr(&that
->host_addr
))
749 diag("our nexthop must be specified when other host is a %any or %opportunistic");
750 this->host_nexthop
= that
->host_addr
;
753 if (caf
!= addrtypeof(&this->host_nexthop
))
754 diag("address family of nexthop inconsistent");
756 if (this->has_client
)
758 if (taf
!= subnettypeof(&this->client
))
759 diag("address family of client subnet inconsistent");
763 /* fill in anyaddr-anyaddr as (missing) client subnet */
766 diagq(anyaddr(caf
, &cn
), NULL
);
767 diagq(rangetosubnet(&cn
, &cn
, &this->client
), NULL
);
770 /* fill in anyaddr if source IP is not defined */
771 if (!this->has_srcip
)
772 diagq(anyaddr(caf
, &this->host_srcip
), optarg
);
775 if (this->protocol
!= that
->protocol
)
776 diag("the protocol for leftprotoport and rightprotoport must be the same");
782 const char *buf
, *secret
;
786 usleep(20000); /* give fflush time for flushing */
787 buf
= getpass("Enter: ");
788 secret
= (buf
== NULL
)?
"" : buf
;
790 /* send the secret to pluto */
791 len
= strlen(secret
) + 1;
792 if (write(sock
, secret
, len
) != len
)
796 fprintf(stderr
, "whack: write() failed (%d %s)\n", e
, strerror(e
));
797 exit(RC_WHACK_PROBLEM
);
801 /* This is a hack for initiating ISAKMP exchanges. */
804 main(int argc
, char **argv
)
807 char esp_buf
[256]; /* uses snprintf */
815 end_seen_before_to
= LEMPTY
;
818 *tunnel_af_used_by
= NULL
;
820 /* check division of numbering space */
822 assert(OPTION_OFFSET
+ DBGOPT_LAST
< NUMERIC_ARG
);
824 assert(OPTION_OFFSET
+ CA_LAST
< NUMERIC_ARG
);
826 assert(OPT_LAST
- OPT_FIRST
< (sizeof opts_seen
* BITS_PER_BYTE
));
827 assert(SC_LAST
- SC_FIRST
< (sizeof sc_seen
* BITS_PER_BYTE
));
828 assert(LST_LAST
- LST_FIRST
< (sizeof lst_seen
* BITS_PER_BYTE
));
829 assert(END_LAST
- END_FIRST
< (sizeof end_seen
* BITS_PER_BYTE
));
830 assert(CD_LAST
- CD_FIRST
< (sizeof cd_seen
* BITS_PER_BYTE
));
831 assert(CA_LAST
- CA_FIRST
< (sizeof ca_seen
* BITS_PER_BYTE
));
832 #ifdef DEBUG /* must be last so others are less than (sizeof cd_seen * BITS_PER_BYTE) to fit in lset_t */
833 assert(DBGOPT_LAST
- DBGOPT_FIRST
< (sizeof cd_seen
* BITS_PER_BYTE
));
835 /* check that POLICY bit assignment matches with CD_ */
836 assert(LELEM(CD_DONT_REKEY
- CD_POLICY_FIRST
) == POLICY_DONT_REKEY
);
840 clear_end(&msg
.right
); /* left set from this after --to */
844 msg
.keyval
.ptr
= NULL
;
849 /* if a connection is added via whack then we assume IKEv1 */
852 msg
.sa_ike_life_seconds
= OAKLEY_ISAKMP_SA_LIFETIME_DEFAULT
;
853 msg
.sa_ipsec_life_seconds
= PLUTO_SA_LIFE_DURATION_DEFAULT
;
854 msg
.sa_rekey_margin
= SA_REPLACEMENT_MARGIN_DEFAULT
;
855 msg
.sa_rekey_fuzz
= SA_REPLACEMENT_FUZZ_DEFAULT
;
856 msg
.sa_keying_tries
= SA_REPLACEMENT_RETRIES_DEFAULT
;
858 msg
.addr_family
= AF_INET
;
859 msg
.tunnel_addr_family
= AF_INET
;
871 unsigned long opt_whole
= 0; /* numeric argument for some flags */
873 /* Note: we don't like the way short options get parsed
874 * by getopt_long, so we simply pass an empty string as
875 * the list. It could be "hp:d:c:o:eatfs" "NARXPECK".
877 int c
= getopt_long(argc
, argv
, "", long_opts
, &long_index
) - OPTION_OFFSET
;
880 /* decode a numeric argument, if expected */
888 opt_whole
= strtoul(optarg
, &endptr
, 0);
890 if (*endptr
!= '\0' || endptr
== optarg
)
891 diagq("badly formed numeric argument", optarg
);
893 if (c
>= (1 << AUX_SHIFT
))
895 aux
= c
>> AUX_SHIFT
;
896 c
-= aux
<< AUX_SHIFT
;
900 /* per-class option processing */
901 if (0 <= c
&& c
<= OPT_LAST
)
903 /* OPT_* options get added to opts_seen.
904 * Reject repeated options (unless later code intervenes).
909 diagq("duplicated flag", long_opts
[long_index
].name
);
912 else if (SC_FIRST
<= c
&& c
<= SC_LAST
)
914 /* SC_* options get added to sc_seen.
915 * Reject repeated options (unless later code intervenes).
917 lset_t f
= LELEM(c
- SC_FIRST
);
920 diagq("duplicated flag", long_opts
[long_index
].name
);
923 else if (LST_FIRST
<= c
&& c
<= LST_LAST
)
925 /* LST_* options get added to lst_seen.
926 * Reject repeated options (unless later code intervenes).
928 lset_t f
= LELEM(c
- LST_FIRST
);
931 diagq("duplicated flag", long_opts
[long_index
].name
);
935 else if (DBGOPT_FIRST
<= c
&& c
<= DBGOPT_LAST
)
937 msg
.whack_options
= TRUE
;
940 else if (END_FIRST
<= c
&& c
<= END_LAST
)
942 /* END_* options are added to end_seen.
943 * Reject repeated options (unless later code intervenes).
945 lset_t f
= LELEM(c
- END_FIRST
);
948 diagq("duplicated flag", long_opts
[long_index
].name
);
950 opts_seen
|= LELEM(OPT_CD
);
952 else if (CD_FIRST
<= c
&& c
<= CD_LAST
)
954 /* CD_* options are added to cd_seen.
955 * Reject repeated options (unless later code intervenes).
957 lset_t f
= LELEM(c
- CD_FIRST
);
960 diagq("duplicated flag", long_opts
[long_index
].name
);
962 opts_seen
|= LELEM(OPT_CD
);
964 else if (CA_FIRST
<= c
&& c
<= CA_LAST
)
966 /* CA_* options are added to ca_seen.
967 * Reject repeated options (unless later code intervenes).
969 lset_t f
= LELEM(c
- CA_FIRST
);
972 diagq("duplicated flag", long_opts
[long_index
].name
);
976 /* Note: "break"ing from switch terminates loop.
977 * most cases should end with "continue".
981 case EOF
- OPTION_OFFSET
: /* end of flags */
984 case 0 - OPTION_OFFSET
: /* long option already handled */
987 case ':' - OPTION_OFFSET
: /* diagnostic already printed by getopt_long */
988 case '?' - OPTION_OFFSET
: /* diagnostic already printed by getopt_long */
989 diag(NULL
); /* print no additional diagnostic, but exit sadly */
990 break; /* not actually reached */
992 case 'h' - OPTION_OFFSET
: /* --help */
994 return 0; /* GNU coding standards say to stop here */
996 case 'v' - OPTION_OFFSET
: /* --version */
998 const char **sp
= ipsec_copyright_notice();
1000 printf("%s\n", ipsec_version_string());
1001 for (; *sp
!= NULL
; sp
++)
1004 return 0; /* GNU coding standards say to stop here */
1006 case 'l' - OPTION_OFFSET
: /* --label <string> */
1007 label
= optarg
; /* remember for diagnostics */
1010 case '+' - OPTION_OFFSET
: /* --optionsfrom <filename> */
1011 optionsfrom(optarg
, &argc
, &argv
, optind
, stderr
);
1012 /* does not return on error */
1015 /* the rest of the options combine in complex ways */
1017 case OPT_CTLBASE
: /* --port <ctlbase> */
1018 if (snprintf(ctl_addr
.sun_path
, sizeof(ctl_addr
.sun_path
)
1019 , "%s%s", optarg
, CTL_SUFFIX
) == -1)
1020 diag("<ctlbase>" CTL_SUFFIX
" must be fit in a sun_addr");
1023 case OPT_NAME
: /* --name <connection-name> */
1028 case OPT_KEYID
: /* --keyid <identity> */
1029 msg
.whack_key
= !msg
.whack_sc_op
;
1030 msg
.keyid
= optarg
; /* decoded by Pluto */
1033 case OPT_MYID
: /* --myid <identity> */
1034 msg
.whack_myid
= TRUE
;
1035 msg
.myid
= optarg
; /* decoded by Pluto */
1038 case OPT_ADDKEY
: /* --addkey */
1039 msg
.whack_addkey
= TRUE
;
1042 case OPT_PUBKEYRSA
: /* --pubkeyrsa <key> */
1044 static char keyspace
[RSA_MAX_ENCODING_BYTES
]; /* room for 8K bit key */
1045 char diag_space
[TTODATAV_BUF
];
1046 const char *ugh
= ttodatav(optarg
, 0, 0
1047 , keyspace
, sizeof(keyspace
)
1048 , &msg
.keyval
.len
, diag_space
, sizeof(diag_space
)
1049 , TTODATAV_SPACECOUNTS
);
1053 char ugh_space
[80]; /* perhaps enough space */
1055 snprintf(ugh_space
, sizeof(ugh_space
)
1056 , "RSA public-key data malformed (%s)", ugh
);
1057 diagq(ugh_space
, optarg
);
1059 msg
.pubkey_alg
= PUBKEY_ALG_RSA
;
1060 msg
.keyval
.ptr
= keyspace
;
1064 case OPT_ROUTE
: /* --route */
1065 msg
.whack_route
= TRUE
;
1068 case OPT_UNROUTE
: /* --unroute */
1069 msg
.whack_unroute
= TRUE
;
1072 case OPT_INITIATE
: /* --initiate */
1073 msg
.whack_initiate
= TRUE
;
1076 case OPT_TERMINATE
: /* --terminate */
1077 msg
.whack_terminate
= TRUE
;
1080 case OPT_DELETE
: /* --delete */
1081 msg
.whack_delete
= TRUE
;
1084 case OPT_DELETESTATE
: /* --deletestate <state_object_number> */
1085 msg
.whack_deletestate
= TRUE
;
1086 msg
.whack_deletestateno
= opt_whole
;
1089 case OPT_DELETECRASH
: /* --crash <ip-address> */
1090 msg
.whack_crash
= TRUE
;
1091 tunnel_af_used_by
= long_opts
[long_index
].name
;
1092 diagq(ttoaddr(optarg
, 0, msg
.tunnel_addr_family
, &msg
.whack_crash_peer
), optarg
);
1093 if (isanyaddr(&msg
.whack_crash_peer
))
1094 diagq("0.0.0.0 or 0::0 isn't a valid client address", optarg
);
1097 case OPT_LISTEN
: /* --listen */
1098 msg
.whack_listen
= TRUE
;
1101 case OPT_UNLISTEN
: /* --unlisten */
1102 msg
.whack_unlisten
= TRUE
;
1105 case OPT_PURGEOCSP
: /* --purgeocsp */
1106 msg
.whack_purgeocsp
= TRUE
;
1109 case OPT_REREADSECRETS
: /* --rereadsecrets */
1110 case OPT_REREADCACERTS
: /* --rereadcacerts */
1111 case OPT_REREADAACERTS
: /* --rereadaacerts */
1112 case OPT_REREADOCSPCERTS
: /* --rereadocspcerts */
1113 case OPT_REREADACERTS
: /* --rereadacerts */
1114 case OPT_REREADCRLS
: /* --rereadcrls */
1115 msg
.whack_reread
|= LELEM(c
-OPT_REREADSECRETS
);
1118 case OPT_REREADALL
: /* --rereadall */
1119 msg
.whack_reread
= REREAD_ALL
;
1122 case OPT_STATUSALL
: /* --statusall */
1123 msg
.whack_statusall
= TRUE
;
1125 case OPT_STATUS
: /* --status */
1126 msg
.whack_status
= TRUE
;
1129 case OPT_SHUTDOWN
: /* --shutdown */
1130 msg
.whack_shutdown
= TRUE
;
1133 case OPT_OPPO_HERE
: /* --oppohere <ip-address> */
1134 tunnel_af_used_by
= long_opts
[long_index
].name
;
1135 diagq(ttoaddr(optarg
, 0, msg
.tunnel_addr_family
, &msg
.oppo_my_client
), optarg
);
1136 if (isanyaddr(&msg
.oppo_my_client
))
1137 diagq("0.0.0.0 or 0::0 isn't a valid client address", optarg
);
1140 case OPT_OPPO_THERE
: /* --oppohere <ip-address> */
1141 tunnel_af_used_by
= long_opts
[long_index
].name
;
1142 diagq(ttoaddr(optarg
, 0, msg
.tunnel_addr_family
, &msg
.oppo_peer_client
), optarg
);
1143 if (isanyaddr(&msg
.oppo_peer_client
))
1144 diagq("0.0.0.0 or 0::0 isn't a valid client address", optarg
);
1148 msg
.whack_async
= TRUE
;
1151 /* Smartcard options */
1153 case SC_ENCRYPT
: /* --scencrypt <plaintext data> */
1154 case SC_DECRYPT
: /* --scdecrypt <encrypted data> */
1155 msg
.whack_sc_op
= 1 + c
- SC_ENCRYPT
;
1156 msg
.whack_key
= FALSE
;
1157 msg
.sc_data
= optarg
;
1160 case SC_INBASE
: /* --inform <format> */
1161 case SC_OUTBASE
: /* --outform <format> */
1165 if (streq(optarg
, "16") || strcaseeq(optarg
, "hex"))
1167 else if (streq(optarg
, "64") || strcaseeq(optarg
, "base64"))
1169 else if (streq(optarg
, "256") || strcaseeq(optarg
, "text")
1170 || strcaseeq(optarg
, "ascii"))
1173 diagq("not a valid base", optarg
);
1184 case LST_UTC
: /* --utc */
1185 msg
.whack_utc
= TRUE
;
1188 case LST_ALGS
: /* --listalgs */
1189 case LST_PUBKEYS
: /* --listpubkeys */
1190 case LST_CERTS
: /* --listcerts */
1191 case LST_CACERTS
: /* --listcacerts */
1192 case LST_ACERTS
: /* --listacerts */
1193 case LST_AACERTS
: /* --listaacerts */
1194 case LST_OCSPCERTS
: /* --listocspcerts */
1195 case LST_GROUPS
: /* --listgroups */
1196 case LST_CAINFOS
: /* --listcainfos */
1197 case LST_CRLS
: /* --listcrls */
1198 case LST_OCSP
: /* --listocsp */
1199 case LST_CARDS
: /* --listcards */
1200 msg
.whack_list
|= LELEM(c
- LST_ALGS
);
1203 case LST_ALL
: /* --listall */
1204 msg
.whack_list
= LIST_ALL
;
1207 /* Connection Description options */
1209 case END_HOST
: /* --host <ip-address> */
1211 lset_t new_policy
= LEMPTY
;
1213 af_used_by
= long_opts
[long_index
].name
;
1214 diagq(anyaddr(msg
.addr_family
, &msg
.right
.host_addr
), optarg
);
1215 if (streq(optarg
, "%any"))
1218 else if (streq(optarg
, "%opportunistic"))
1220 /* always use tunnel mode; mark as opportunistic */
1221 new_policy
|= POLICY_TUNNEL
| POLICY_OPPO
;
1223 else if (streq(optarg
, "%group"))
1225 /* always use tunnel mode; mark as group */
1226 new_policy
|= POLICY_TUNNEL
| POLICY_GROUP
;
1228 else if (streq(optarg
, "%opportunisticgroup"))
1230 /* always use tunnel mode; mark as opportunistic */
1231 new_policy
|= POLICY_TUNNEL
| POLICY_OPPO
| POLICY_GROUP
;
1235 diagq(ttoaddr(optarg
, 0, msg
.addr_family
1236 , &msg
.right
.host_addr
), optarg
);
1239 msg
.policy
|= new_policy
;
1241 if (new_policy
& (POLICY_OPPO
| POLICY_GROUP
))
1243 if (!LHAS(end_seen
, END_CLIENT
- END_FIRST
))
1245 /* set host to 0.0.0 and --client to 0.0.0.0/0
1246 * or IPV6 equivalent
1250 tunnel_af_used_by
= optarg
;
1251 diagq(anyaddr(msg
.tunnel_addr_family
, &any
), optarg
);
1252 diagq(initsubnet(&any
, 0, '0', &msg
.right
.client
), optarg
);
1254 msg
.right
.has_client
= TRUE
;
1256 if (new_policy
& POLICY_GROUP
)
1258 /* client subnet must not be specified by user:
1259 * it will come from the group's file.
1261 if (LHAS(end_seen
, END_CLIENT
- END_FIRST
))
1262 diag("--host %group clashes with --client");
1264 end_seen
|= LELEM(END_CLIENT
- END_FIRST
);
1266 if (new_policy
& POLICY_OPPO
)
1267 msg
.right
.key_from_DNS_on_demand
= TRUE
;
1270 case END_ID
: /* --id <identity> */
1271 msg
.right
.id
= optarg
; /* decoded by Pluto */
1274 case END_CERT
: /* --cert <path> */
1275 msg
.right
.cert
= optarg
; /* decoded by Pluto */
1278 case END_CA
: /* --ca <distinguished name> */
1279 msg
.right
.ca
= optarg
; /* decoded by Pluto */
1283 if (streq(optarg
, "yes") || streq(optarg
, "always"))
1285 msg
.right
.sendcert
= CERT_ALWAYS_SEND
;
1287 else if (streq(optarg
, "no") || streq(optarg
, "never"))
1289 msg
.right
.sendcert
= CERT_NEVER_SEND
;
1291 else if (streq(optarg
, "ifasked"))
1293 msg
.right
.sendcert
= CERT_SEND_IF_ASKED
;
1297 diagq("whack sendcert value is not legal", optarg
);
1301 case END_GROUPS
:/* --groups <access control groups> */
1302 msg
.right
.groups
= optarg
; /* decoded by Pluto */
1305 case END_IKEPORT
: /* --ikeport <port-number> */
1306 if (opt_whole
<=0 || opt_whole
>= 0x10000)
1307 diagq("<port-number> must be a number between 1 and 65535", optarg
);
1308 msg
.right
.host_port
= opt_whole
;
1311 case END_NEXTHOP
: /* --nexthop <ip-address> */
1312 af_used_by
= long_opts
[long_index
].name
;
1313 if (streq(optarg
, "%direct"))
1314 diagq(anyaddr(msg
.addr_family
1315 , &msg
.right
.host_nexthop
), optarg
);
1317 diagq(ttoaddr(optarg
, 0, msg
.addr_family
1318 , &msg
.right
.host_nexthop
), optarg
);
1321 case END_SRCIP
: /* --srcip <ip-address> */
1322 af_used_by
= long_opts
[long_index
].name
;
1323 if (streq(optarg
, "%modeconfig") || streq(optarg
, "%modecfg"))
1325 msg
.right
.modecfg
= TRUE
;
1329 diagq(ttoaddr(optarg
, 0, msg
.addr_family
1330 , &msg
.right
.host_srcip
), optarg
);
1331 msg
.right
.has_srcip
= TRUE
;
1333 msg
.policy
|= POLICY_TUNNEL
; /* srcip => tunnel */
1336 case END_CLIENT
: /* --client <subnet> */
1337 if (end_seen
& LELEM(END_CLIENTWITHIN
- END_FIRST
))
1338 diag("--client conflicts with --clientwithin");
1339 tunnel_af_used_by
= long_opts
[long_index
].name
;
1340 if ((strlen(optarg
) >= 6 && strncmp(optarg
,"vhost:",6) == 0)
1341 || (strlen(optarg
) >= 5 && strncmp(optarg
,"vnet:",5) == 0))
1343 msg
.right
.virt
= optarg
;
1347 diagq(ttosubnet(optarg
, 0, msg
.tunnel_addr_family
, &msg
.right
.client
), optarg
);
1348 msg
.right
.has_client
= TRUE
;
1350 msg
.policy
|= POLICY_TUNNEL
; /* client => tunnel */
1353 case END_CLIENTWITHIN
: /* --clienwithin <address range> */
1354 if (end_seen
& LELEM(END_CLIENT
- END_FIRST
))
1355 diag("--clientwithin conflicts with --client");
1356 tunnel_af_used_by
= long_opts
[long_index
].name
;
1357 diagq(ttosubnet(optarg
, 0, msg
.tunnel_addr_family
, &msg
.right
.client
), optarg
);
1358 msg
.right
.has_client
= TRUE
;
1359 msg
.policy
|= POLICY_TUNNEL
; /* client => tunnel */
1360 msg
.right
.has_client_wildcard
= TRUE
;
1363 case END_CLIENTPROTOPORT
: /* --clientprotoport <protocol>/<port> */
1364 diagq(ttoprotoport(optarg
, 0, &msg
.right
.protocol
, &msg
.right
.port
1365 , &msg
.right
.has_port_wildcard
), optarg
);
1368 case END_DNSKEYONDEMAND
: /* --dnskeyondemand */
1369 msg
.right
.key_from_DNS_on_demand
= TRUE
;
1372 case END_HOSTACCESS
: /* --hostaccess */
1373 msg
.right
.hostaccess
= TRUE
;
1376 case END_UPDOWN
: /* --updown <updown> */
1377 msg
.right
.updown
= optarg
;
1380 case CD_TO
: /* --to */
1381 /* process right end, move it to left, reset it */
1382 if (!LHAS(end_seen
, END_HOST
- END_FIRST
))
1383 diag("connection missing --host before --to");
1384 msg
.left
= msg
.right
;
1385 clear_end(&msg
.right
);
1386 end_seen_before_to
= end_seen
;
1390 case CD_PSK
: /* --psk */
1391 case CD_RSASIG
: /* --rsasig */
1392 case CD_ENCRYPT
: /* --encrypt */
1393 case CD_AUTHENTICATE
: /* --authenticate */
1394 case CD_COMPRESS
: /* --compress */
1395 case CD_TUNNEL
: /* --tunnel */
1396 case CD_PFS
: /* --pfs */
1397 case CD_DISABLEARRIVALCHECK
: /* --disablearrivalcheck */
1398 case CD_DONT_REKEY
: /* --donotrekey */
1399 msg
.policy
|= LELEM(c
- CD_POLICY_FIRST
);
1402 /* --initiateontraffic
1408 msg
.policy
= (msg
.policy
& ~POLICY_SHUNT_MASK
)
1409 | ((lset_t
)aux
<< POLICY_SHUNT_SHIFT
);
1418 msg
.policy
= (msg
.policy
& ~POLICY_FAIL_MASK
)
1419 | ((lset_t
)aux
<< POLICY_FAIL_SHIFT
);
1422 case CD_IKELIFETIME
: /* --ikelifetime <seconds> */
1423 msg
.sa_ike_life_seconds
= opt_whole
;
1426 case CD_IPSECLIFETIME
: /* --ipseclifetime <seconds> */
1427 msg
.sa_ipsec_life_seconds
= opt_whole
;
1430 case CD_RKMARGIN
: /* --rekeymargin <seconds> */
1431 msg
.sa_rekey_margin
= opt_whole
;
1434 case CD_RKFUZZ
: /* --rekeyfuzz <percentage> */
1435 msg
.sa_rekey_fuzz
= opt_whole
;
1438 case CD_KTRIES
: /* --keyingtries <count> */
1439 msg
.sa_keying_tries
= opt_whole
;
1443 if (streq(optarg
, "none"))
1444 msg
.dpd_action
= DPD_ACTION_NONE
;
1445 else if (streq(optarg
, "clear"))
1446 msg
.dpd_action
= DPD_ACTION_CLEAR
;
1447 else if (streq(optarg
, "hold"))
1448 msg
.dpd_action
= DPD_ACTION_HOLD
;
1449 else if (streq(optarg
, "restart"))
1450 msg
.dpd_action
= DPD_ACTION_RESTART
;
1452 msg
.dpd_action
= DPD_ACTION_UNKNOWN
;
1456 msg
.dpd_delay
= opt_whole
;
1460 msg
.dpd_timeout
= opt_whole
;
1463 case CD_IKE
: /* --ike <ike_alg1,ike_alg2,...> */
1467 case CD_PFSGROUP
: /* --pfsgroup modpXXXX */
1468 msg
.pfsgroup
= optarg
;
1471 case CD_ESP
: /* --esp <esp_alg1,esp_alg2,...> */
1476 if (LHAS(cd_seen
, CD_CONNIPV6
- CD_FIRST
))
1477 diag("--ipv4 conflicts with --ipv6");
1479 /* Since this is the default, the flag is redundant.
1480 * So we don't need to set msg.addr_family
1481 * and we don't need to check af_used_by
1482 * and we don't have to consider defaulting tunnel_addr_family.
1487 if (LHAS(cd_seen
, CD_CONNIPV4
- CD_FIRST
))
1488 diag("--ipv6 conflicts with --ipv4");
1490 if (af_used_by
!= NULL
)
1491 diagq("--ipv6 must precede", af_used_by
);
1493 af_used_by
= long_opts
[long_index
].name
;
1494 msg
.addr_family
= AF_INET6
;
1496 /* Consider defaulting tunnel_addr_family to AF_INET6.
1497 * Do so only if it hasn't yet been specified or used.
1499 if (LDISJOINT(cd_seen
, LELEM(CD_TUNNELIPV4
- CD_FIRST
) | LELEM(CD_TUNNELIPV6
- CD_FIRST
))
1500 && tunnel_af_used_by
== NULL
)
1501 msg
.tunnel_addr_family
= AF_INET6
;
1505 if (LHAS(cd_seen
, CD_TUNNELIPV6
- CD_FIRST
))
1506 diag("--tunnelipv4 conflicts with --tunnelipv6");
1508 if (tunnel_af_used_by
!= NULL
)
1509 diagq("--tunnelipv4 must precede", af_used_by
);
1511 msg
.tunnel_addr_family
= AF_INET
;
1515 if (LHAS(cd_seen
, CD_TUNNELIPV4
- CD_FIRST
))
1516 diag("--tunnelipv6 conflicts with --tunnelipv4");
1518 if (tunnel_af_used_by
!= NULL
)
1519 diagq("--tunnelipv6 must precede", af_used_by
);
1521 msg
.tunnel_addr_family
= AF_INET6
;
1524 case CA_NAME
: /* --caname <name> */
1526 msg
.whack_ca
= TRUE
;
1528 case CA_CERT
: /* --cacert <path> */
1529 msg
.cacert
= optarg
;
1531 case CA_LDAPHOST
: /* --ldaphost <hostname> */
1532 msg
.ldaphost
= optarg
;
1534 case CA_LDAPBASE
: /* --ldapbase <base> */
1535 msg
.ldapbase
= optarg
;
1537 case CA_CRLURI
: /* --crluri <uri> */
1538 msg
.crluri
= optarg
;
1540 case CA_CRLURI2
: /* --crluri2 <uri> */
1541 msg
.crluri2
= optarg
;
1543 case CA_OCSPURI
: /* --ocspuri <uri> */
1544 msg
.ocspuri
= optarg
;
1546 case CA_STRICT
: /* --strictcrlpolicy */
1547 msg
.whack_strict
= TRUE
;
1551 case DBGOPT_NONE
: /* --debug-none */
1552 msg
.debugging
= DBG_NONE
;
1555 case DBGOPT_ALL
: /* --debug-all */
1556 msg
.debugging
|= DBG_ALL
; /* note: does not include PRIVATE */
1559 case DBGOPT_RAW
: /* --debug-raw */
1560 case DBGOPT_CRYPT
: /* --debug-crypt */
1561 case DBGOPT_PARSING
: /* --debug-parsing */
1562 case DBGOPT_EMITTING
: /* --debug-emitting */
1563 case DBGOPT_CONTROL
: /* --debug-control */
1564 case DBGOPT_LIFECYCLE
: /* --debug-lifecycle */
1565 case DBGOPT_KLIPS
: /* --debug-klips */
1566 case DBGOPT_DNS
: /* --debug-dns */
1567 case DBGOPT_NATT
: /* --debug-natt */
1568 case DBGOPT_OPPO
: /* --debug-oppo */
1569 case DBGOPT_CONTROLMORE
: /* --debug-controlmore */
1570 case DBGOPT_PRIVATE
: /* --debug-private */
1571 case DBGOPT_IMPAIR_DELAY_ADNS_KEY_ANSWER
: /* --impair-delay-adns-key-answer */
1572 case DBGOPT_IMPAIR_DELAY_ADNS_TXT_ANSWER
: /* --impair-delay-adns-txt-answer */
1573 case DBGOPT_IMPAIR_BUST_MI2
: /* --impair_bust_mi2 */
1574 case DBGOPT_IMPAIR_BUST_MR2
: /* --impair_bust_mr2 */
1575 msg
.debugging
|= LELEM(c
-DBGOPT_RAW
);
1579 assert(FALSE
); /* unknown return value */
1586 /* If you see this message unexpectedly, perhaps the
1587 * case for the previous option ended with "break"
1588 * instead of "continue"
1590 diagq("unexpected argument", argv
[optind
]);
1593 /* For each possible form of the command, figure out if an argument
1594 * suggests whether that form was intended, and if so, whether all
1595 * required information was supplied.
1598 /* check opportunistic initiation simulation request */
1599 switch (opts_seen
& (LELEM(OPT_OPPO_HERE
) | LELEM(OPT_OPPO_THERE
)))
1601 case LELEM(OPT_OPPO_HERE
):
1602 case LELEM(OPT_OPPO_THERE
):
1603 diag("--oppohere and --oppothere must be used together");
1605 case LELEM(OPT_OPPO_HERE
) | LELEM(OPT_OPPO_THERE
):
1606 msg
.whack_oppo_initiate
= TRUE
;
1607 if (LIN(cd_seen
, LELEM(CD_TUNNELIPV4
- CD_FIRST
) | LELEM(CD_TUNNELIPV6
- CD_FIRST
)))
1608 opts_seen
&= ~LELEM(OPT_CD
);
1612 /* check connection description */
1613 if (LHAS(opts_seen
, OPT_CD
))
1615 if (!LHAS(cd_seen
, CD_TO
-CD_FIRST
))
1616 diag("connection description option, but no --to");
1618 if (!LHAS(end_seen
, END_HOST
-END_FIRST
))
1619 diag("connection missing --host after --to");
1621 if (isanyaddr(&msg
.left
.host_addr
)
1622 && isanyaddr(&msg
.right
.host_addr
))
1623 diag("hosts cannot both be 0.0.0.0 or 0::0");
1625 if (msg
.policy
& POLICY_OPPO
)
1627 if ((msg
.policy
& (POLICY_PSK
| POLICY_RSASIG
)) != POLICY_RSASIG
)
1628 diag("only RSASIG is supported for opportunism");
1629 if ((msg
.policy
& POLICY_PFS
) == 0)
1630 diag("PFS required for opportunism");
1631 if ((msg
.policy
& POLICY_ENCRYPT
) == 0)
1632 diag("encryption required for opportunism");
1635 check_end(&msg
.left
, &msg
.right
, !LHAS(end_seen_before_to
, END_NEXTHOP
-END_FIRST
)
1636 , msg
.addr_family
, msg
.tunnel_addr_family
);
1638 check_end(&msg
.right
, &msg
.left
, !LHAS(end_seen
, END_NEXTHOP
-END_FIRST
)
1639 , msg
.addr_family
, msg
.tunnel_addr_family
);
1641 if (subnettypeof(&msg
.left
.client
) != subnettypeof(&msg
.right
.client
))
1642 diag("endpoints clash: one is IPv4 and the other is IPv6");
1644 if (NEVER_NEGOTIATE(msg
.policy
))
1646 /* we think this is just a shunt (because he didn't specify
1647 * a host authentication method). If he didn't specify a
1648 * shunt type, he's probably gotten it wrong.
1650 if ((msg
.policy
& POLICY_SHUNT_MASK
) == POLICY_SHUNT_TRAP
)
1651 diag("non-shunt connection must have --psk or --rsasig or both");
1655 /* not just a shunt: a real ipsec connection */
1656 if ((msg
.policy
& POLICY_ID_AUTH_MASK
) == LEMPTY
)
1657 diag("must specify --rsasig or --psk for a connection");
1659 if (!HAS_IPSEC_POLICY(msg
.policy
)
1660 && (msg
.left
.has_client
|| msg
.right
.has_client
))
1661 diag("must not specify clients for ISAKMP-only connection");
1664 msg
.whack_connection
= TRUE
;
1667 /* decide whether --name is mandatory or forbidden */
1668 if (!LDISJOINT(opts_seen
1669 , LELEM(OPT_ROUTE
) | LELEM(OPT_UNROUTE
)
1670 | LELEM(OPT_INITIATE
) | LELEM(OPT_TERMINATE
)
1671 | LELEM(OPT_DELETE
) | LELEM(OPT_CD
)))
1673 if (!LHAS(opts_seen
, OPT_NAME
) && !msg
.whack_ca
)
1674 diag("missing --name <connection_name>");
1676 else if (!msg
.whack_options
&& !msg
.whack_status
)
1678 if (LHAS(opts_seen
, OPT_NAME
))
1679 diag("no reason for --name");
1682 if (!LDISJOINT(opts_seen
, LELEM(OPT_PUBKEYRSA
) | LELEM(OPT_ADDKEY
)))
1684 if (!LHAS(opts_seen
, OPT_KEYID
))
1685 diag("--addkey and --pubkeyrsa require --keyid");
1688 if (!(msg
.whack_connection
|| msg
.whack_key
|| msg
.whack_myid
1689 || msg
.whack_delete
|| msg
.whack_deletestate
1690 || msg
.whack_initiate
|| msg
.whack_oppo_initiate
|| msg
.whack_terminate
1691 || msg
.whack_route
|| msg
.whack_unroute
|| msg
.whack_listen
1692 || msg
.whack_unlisten
|| msg
.whack_list
|| msg
.whack_purgeocsp
|| msg
.whack_reread
1693 || msg
.whack_ca
|| msg
.whack_status
|| msg
.whack_options
|| msg
.whack_shutdown
1694 || msg
.whack_sc_op
))
1696 diag("no action specified; try --help for hints");
1701 /* tricky quick and dirty check for wild values */
1702 if (msg
.sa_rekey_margin
!= 0
1703 && msg
.sa_rekey_fuzz
* msg
.sa_rekey_margin
* 4 / msg
.sa_rekey_margin
/ 4
1704 != msg
.sa_rekey_fuzz
)
1705 diag("rekeymargin or rekeyfuzz values are so large that they cause oveflow");
1707 check_life_time (msg
.sa_ike_life_seconds
, OAKLEY_ISAKMP_SA_LIFETIME_MAXIMUM
1708 , "ikelifetime", &msg
);
1710 check_life_time(msg
.sa_ipsec_life_seconds
, SA_LIFE_DURATION_MAXIMUM
1711 , "ipseclifetime", &msg
);
1713 if (msg
.dpd_action
== DPD_ACTION_UNKNOWN
)
1714 diag("dpdaction must be \"none\", \"clear\", \"hold\" or \"restart\"");
1716 if (msg
.dpd_action
!= DPD_ACTION_NONE
)
1718 if (msg
.dpd_delay
<= 0)
1719 diag("dpddelay must be larger than zero");
1721 if (msg
.dpd_timeout
<= 0)
1722 diag("dpdtimeout must be larger than zero");
1724 if (msg
.dpd_timeout
<= msg
.dpd_delay
)
1725 diag("dpdtimeout must be larger than dpddelay");
1728 /* pack strings for inclusion in message */
1729 next_str
= msg
.string
;
1730 str_roof
= &msg
.string
[sizeof(msg
.string
)];
1732 /* build esp message as esp="<esp>;<pfsgroup>" */
1734 snprintf(esp_buf
, sizeof (esp_buf
), "%s;%s",
1735 msg
.esp ? msg
.esp
: "",
1736 msg
.pfsgroup ? msg
.pfsgroup
: "");
1739 if (!pack_str(&msg
.name
) /* string 1 */
1740 || !pack_str(&msg
.left
.id
) /* string 2 */
1741 || !pack_str(&msg
.left
.cert
) /* string 3 */
1742 || !pack_str(&msg
.left
.ca
) /* string 4 */
1743 || !pack_str(&msg
.left
.groups
) /* string 5 */
1744 || !pack_str(&msg
.left
.updown
) /* string 6 */
1745 || !pack_str(&msg
.left
.virt
) /* string 7 */
1746 || !pack_str(&msg
.right
.id
) /* string 8 */
1747 || !pack_str(&msg
.right
.cert
) /* string 9 */
1748 || !pack_str(&msg
.right
.ca
) /* string 10 */
1749 || !pack_str(&msg
.right
.groups
) /* string 11 */
1750 || !pack_str(&msg
.right
.updown
) /* string 12 */
1751 || !pack_str(&msg
.right
.virt
) /* string 13 */
1752 || !pack_str(&msg
.keyid
) /* string 14 */
1753 || !pack_str(&msg
.myid
) /* string 15 */
1754 || !pack_str(&msg
.cacert
) /* string 16 */
1755 || !pack_str(&msg
.ldaphost
) /* string 17 */
1756 || !pack_str(&msg
.ldapbase
) /* string 18 */
1757 || !pack_str(&msg
.crluri
) /* string 19 */
1758 || !pack_str(&msg
.crluri2
) /* string 20 */
1759 || !pack_str(&msg
.ocspuri
) /* string 21 */
1760 || !pack_str(&msg
.ike
) /* string 22 */
1761 || !pack_str(&msg
.esp
) /* string 23 */
1762 || !pack_str(&msg
.sc_data
) /* string 24 */
1763 || str_roof
- next_str
< (ptrdiff_t)msg
.keyval
.len
) /* chunk (sort of string 5) */
1764 diag("too many bytes of strings to fit in message to pluto");
1766 memcpy(next_str
, msg
.keyval
.ptr
, msg
.keyval
.len
);
1767 msg
.keyval
.ptr
= NULL
;
1768 next_str
+= msg
.keyval
.len
;
1770 msg
.magic
= ((opts_seen
& ~LELEM(OPT_SHUTDOWN
))
1771 | sc_seen
| lst_seen
| cd_seen
| ca_seen
) != LEMPTY
1772 || msg
.whack_options
1773 ? WHACK_MAGIC
: WHACK_BASIC_MAGIC
;
1775 /* send message to Pluto */
1776 if (access(ctl_addr
.sun_path
, R_OK
| W_OK
) < 0)
1783 fprintf(stderr
, "whack: no right to communicate with pluto (access(\"%s\"))\n"
1784 , ctl_addr
.sun_path
);
1787 fprintf(stderr
, "whack: Pluto is not running (no \"%s\")\n"
1788 , ctl_addr
.sun_path
);
1791 fprintf(stderr
, "whack: access(\"%s\") failed with %d %s\n"
1792 , ctl_addr
.sun_path
, errno
, strerror(e
));
1795 exit(RC_WHACK_PROBLEM
);
1799 int sock
= socket(AF_UNIX
, SOCK_STREAM
, 0);
1800 int exit_status
= 0;
1801 ssize_t len
= next_str
- (char *)&msg
;
1807 fprintf(stderr
, "whack: socket() failed (%d %s)\n", e
, strerror(e
));
1808 exit(RC_WHACK_PROBLEM
);
1811 if (connect(sock
, (struct sockaddr
*)&ctl_addr
1812 , offsetof(struct sockaddr_un
, sun_path
) + strlen(ctl_addr
.sun_path
)) < 0)
1816 fprintf(stderr
, "whack:%s connect() for \"%s\" failed (%d %s)\n"
1817 , e
== ECONNREFUSED?
" is Pluto running? " : ""
1818 , ctl_addr
.sun_path
, e
, strerror(e
));
1819 exit(RC_WHACK_PROBLEM
);
1822 if (write(sock
, &msg
, len
) != len
)
1826 fprintf(stderr
, "whack: write() failed (%d %s)\n", e
, strerror(e
));
1827 exit(RC_WHACK_PROBLEM
);
1830 /* for now, just copy reply back to stdout */
1833 char buf
[4097]; /* arbitrary limit on log line length */
1839 ssize_t rl
= read(sock
, be
, (buf
+ sizeof(buf
)-1) - be
);
1845 fprintf(stderr
, "whack: read() failed (%d %s)\n", e
, strerror(e
));
1846 exit(RC_WHACK_PROBLEM
);
1851 fprintf(stderr
, "whack: last line from pluto too long or unterminated\n");
1860 char *le
= strchr(ls
, '\n');
1864 /* move last, partial line to start of buffer */
1865 memmove(buf
, ls
, be
-ls
);
1870 le
++; /* include NL in line */
1871 write(1, ls
, le
- ls
);
1873 /* figure out prefix number
1874 * and how it should affect our exit status
1877 unsigned long s
= strtoul(ls
, NULL
, 10);
1889 case RC_ENTERSECRET
:
1892 /* case RC_LOG_SERIOUS: */