dd7f7721cd92fae766079c403249405fa4b15929
[strongswan.git] / src / whack / whack.c
1 /* command interface to Pluto
2 * Copyright (C) 1997 Angelos D. Keromytis.
3 * Copyright (C) 1998-2001 D. Hugh Redelmeier.
4 *
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>.
9 *
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
13 * for more details.
14 */
15
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <stddef.h>
19 #include <string.h>
20 #include <ctype.h>
21 #include <unistd.h>
22 #include <errno.h>
23 #include <sys/types.h>
24 #include <sys/socket.h>
25 #include <sys/un.h>
26 #include <netinet/in.h>
27 #include <arpa/inet.h>
28 #include <getopt.h>
29 #include <assert.h>
30
31 #include <freeswan.h>
32
33 #include <utils/optionsfrom.h>
34
35 #include "constants.h"
36 #include "defs.h"
37 #include "whack.h"
38
39 static void help(void)
40 {
41 fprintf(stderr
42 , "Usage:\n\n"
43 "all forms:"
44 " [--optionsfrom <filename>]"
45 " [--ctlbase <path>]"
46 " [--label <string>]"
47 "\n\n"
48 "help: whack"
49 " [--help]"
50 " [--version]"
51 "\n\n"
52 "connection: whack"
53 " --name <connection_name>"
54 " \\\n "
55 " [--ipv4 | --ipv6]"
56 " [--tunnelipv4 | --tunnelipv6]"
57 " \\\n "
58 " (--host <ip-address> | --id <identity>)"
59 " \\\n "
60 " [--cert <path>]"
61 " [--ca <distinguished name>]"
62 " [--sendcert <policy>]"
63 " \\\n "
64 " [--groups <access control groups>]"
65 " \\\n "
66 " [--ikeport <port-number>]"
67 " [--nexthop <ip-address>]"
68 " [--srcip <ip-address>]"
69 " \\\n "
70 " [--client <subnet> | --clientwithin <address range>]"
71 " [--clientprotoport <protocol>/<port>]"
72 " \\\n "
73 " [--dnskeyondemand]"
74 " [--updown <updown>]"
75 " \\\n "
76 " --to"
77 " (--host <ip-address> | --id <identity>)"
78 " \\\n "
79 " [--cert <path>]"
80 " [--ca <distinguished name>]"
81 " [--sendcert <policy>]"
82 " \\\n "
83 " [--ikeport <port-number>]"
84 " [--nexthop <ip-address>]"
85 " [--srcip <ip-address>]"
86 " \\\n "
87 " [--client <subnet> | --clientwithin <address range>]"
88 " [--clientprotoport <protocol>/<port>]"
89 " \\\n "
90 " [--dnskeyondemand]"
91 " [--updown <updown>]"
92 " [--psk]"
93 " [--rsasig]"
94 " \\\n "
95 " [--encrypt]"
96 " [--authenticate]"
97 " [--compress]"
98 " [--tunnel]"
99 " [--pfs]"
100 " \\\n "
101 " [--ikelifetime <seconds>]"
102 " [--ipseclifetime <seconds>]"
103 " \\\n "
104 " [--reykeymargin <seconds>]"
105 " [--reykeyfuzz <percentage>]"
106 " \\\n "
107 " [--keyingtries <count>]"
108 " \\\n "
109 " [--esp <esp-algos>]"
110 " \\\n "
111 " [--dontrekey]"
112
113 " [--dpdaction (none|clear|hold|restart)]"
114 " \\\n "
115 " [--dpddelay <seconds> --dpdtimeout <seconds>]"
116 " \\\n "
117 " [--initiateontraffic|--pass|--drop|--reject]"
118 " \\\n "
119 " [--failnone|--failpass|--faildrop|--failreject]"
120 "\n\n"
121 "routing: whack"
122 " (--route | --unroute)"
123 " --name <connection_name>"
124 "\n\n"
125 "initiation:"
126 "\n "
127 " whack"
128 " (--initiate | --terminate)"
129 " --name <connection_name>"
130 " [--asynchronous]"
131 "\n\n"
132 "opportunistic initiation: whack"
133 " [--tunnelipv4 | --tunnelipv6]"
134 " \\\n "
135 " --oppohere <ip-address>"
136 " --oppothere <ip-address>"
137 "\n\n"
138 "delete: whack"
139 " --delete"
140 " (--name <connection_name> | --caname <ca name>)"
141 "\n\n"
142 "deletestate: whack"
143 " --deletestate <state_object_number>"
144 " --crash <ip-address>"
145 "\n\n"
146 "pubkey: whack"
147 " --keyid <id>"
148 " [--addkey]"
149 " [--pubkeyrsa <key>]"
150 "\n\n"
151 "myid: whack"
152 " --myid <id>"
153 "\n\n"
154 "ca: whack"
155 " --caname <name>"
156 " --cacert <path>"
157 " \\\n "
158 " [--ldaphost <hostname>]"
159 " [--ldapbase <base>]"
160 " \\\n "
161 " [--crluri <uri>]"
162 " [--crluri2 <uri>]"
163 " [--ocspuri <uri>]"
164 " [--strictcrlpolicy]"
165 "\n\n"
166 #ifdef DEBUG
167 "debug: whack [--name <connection_name>]"
168 " \\\n "
169 " [--debug-none]"
170 " [--debug-all]"
171 " \\\n "
172 " [--debug-raw]"
173 " [--debug-crypt]"
174 " [--debug-parsing]"
175 " [--debug-emitting]"
176 " \\\n "
177 " [--debug-control]"
178 " [--debug-lifecycle]"
179 " [--debug-klips]"
180 " [--debug-dns]"
181 " \\\n "
182 " [--debug-natt]"
183 " [--debug-oppo]"
184 " [--debug-controlmore]"
185 " [--debug-private]"
186 "\n\n"
187 #endif
188 "listen: whack"
189 " (--listen | --unlisten)"
190 "\n\n"
191 "list: whack [--utc]"
192 " [--listalgs]"
193 " [--listpubkeys]"
194 " [--listcerts]"
195 " [--listcacerts]"
196 " \\\n "
197 " [--listacerts]"
198 " [--listaacerts]"
199 " [--listocspcerts]"
200 " [--listgroups]"
201 " \\\n "
202 " [--listcainfos]"
203 " [--listcrls]"
204 " [--listocsp]"
205 " [--listcards]"
206 " [--listall]"
207 "\n\n"
208 "purge: whack"
209 " [--purgeocsp]"
210 "\n\n"
211 "reread: whack"
212 " [--rereadsecrets]"
213 " [--rereadcacerts]"
214 " [--rereadaacerts]"
215 " \\\n "
216 " [--rereadocspcerts]"
217 " [--rereadacerts]"
218 " [--rereadcrls]"
219 " [--rereadall]"
220 "\n\n"
221 "status: whack"
222 " [--name <connection_name>] --status|--statusall"
223 "\n\n"
224 "scdecrypt: whack"
225 " --scencrypt|scdecrypt <value>"
226 " [--inbase <base>]"
227 " [--outbase <base>]"
228 " [--keyid <id>]"
229 "\n\n"
230 "shutdown: whack"
231 " --shutdown"
232 "\n\n"
233 "strongSwan %s\n"
234 , ipsec_version_code());
235 }
236
237 static const char *label = NULL; /* --label operand, saved for diagnostics */
238
239 static const char *name = NULL; /* --name operand, saved for diagnostics */
240
241 /* options read by optionsfrom */
242 options_t *options;
243
244 /**
245 * exit whack after cleaning up
246 */
247 static void whack_exit(int status)
248 {
249 options->destroy(options);
250 exit(status);
251 }
252
253 /**
254 * print a string as a diagnostic, then exit whack unhappily
255 */
256 static void diag(const char *mess)
257 {
258 if (mess != NULL)
259 {
260 fprintf(stderr, "whack error: ");
261 if (label != NULL)
262 {
263 fprintf(stderr, "%s ", label);
264 }
265 if (name != NULL)
266 {
267 fprintf(stderr, "\"%s\" ", name);
268 }
269 fprintf(stderr, "%s\n", mess);
270 }
271 whack_exit(RC_WHACK_PROBLEM);
272 }
273
274 /* conditially calls diag; prints second arg, if non-NULL, as quoted string */
275 static void diagq(err_t ugh, const char *this)
276 {
277 if (ugh != NULL)
278 {
279 if (this == NULL)
280 {
281 diag(ugh);
282 }
283 else
284 {
285 char buf[120]; /* arbitrary limit */
286
287 snprintf(buf, sizeof(buf), "%s \"%s\"", ugh, this);
288 diag(buf);
289 }
290 }
291 }
292
293 /* complex combined operands return one of these enumerated values
294 * Note: these become flags in an lset_t. Since there are more than
295 * 32, we partition them into:
296 * - OPT_* options (most random options)
297 * - LST_* options (list various internal data)
298 * - DBGOPT_* option (DEBUG options)
299 * - END_* options (End description options)
300 * - CD_* options (Connection Description options)
301 * - CA_* options (CA description options)
302 */
303 enum {
304 # define OPT_FIRST OPT_CTLBASE
305 OPT_CTLBASE,
306 OPT_NAME,
307
308 OPT_CD,
309
310 OPT_KEYID,
311 OPT_ADDKEY,
312 OPT_PUBKEYRSA,
313
314 OPT_MYID,
315
316 OPT_ROUTE,
317 OPT_UNROUTE,
318
319 OPT_INITIATE,
320 OPT_TERMINATE,
321 OPT_DELETE,
322 OPT_DELETESTATE,
323 OPT_LISTEN,
324 OPT_UNLISTEN,
325
326 OPT_PURGEOCSP,
327
328 OPT_REREADSECRETS,
329 OPT_REREADCACERTS,
330 OPT_REREADAACERTS,
331 OPT_REREADOCSPCERTS,
332 OPT_REREADACERTS,
333 OPT_REREADCRLS,
334 OPT_REREADALL,
335
336 OPT_STATUS,
337 OPT_STATUSALL,
338 OPT_SHUTDOWN,
339
340 OPT_OPPO_HERE,
341 OPT_OPPO_THERE,
342
343 OPT_ASYNC,
344 OPT_DELETECRASH,
345
346 # define OPT_LAST OPT_ASYNC /* last "normal" option */
347
348 /* Smartcard options */
349
350 # define SC_FIRST SC_ENCRYPT /* first smartcard option */
351
352 SC_ENCRYPT,
353 SC_DECRYPT,
354 SC_INBASE,
355 SC_OUTBASE,
356
357 # define SC_LAST SC_OUTBASE /* last "smartcard" option */
358
359 /* List options */
360
361 # define LST_FIRST LST_UTC /* first list option */
362 LST_UTC,
363 LST_ALGS,
364 LST_PUBKEYS,
365 LST_CERTS,
366 LST_CACERTS,
367 LST_ACERTS,
368 LST_AACERTS,
369 LST_OCSPCERTS,
370 LST_GROUPS,
371 LST_CAINFOS,
372 LST_CRLS,
373 LST_OCSP,
374 LST_CARDS,
375 LST_ALL,
376
377 # define LST_LAST LST_ALL /* last list option */
378
379 /* Connection End Description options */
380
381 # define END_FIRST END_HOST /* first end description */
382 END_HOST,
383 END_ID,
384 END_CERT,
385 END_CA,
386 END_SENDCERT,
387 END_GROUPS,
388 END_IKEPORT,
389 END_NEXTHOP,
390 END_CLIENT,
391 END_CLIENTWITHIN,
392 END_CLIENTPROTOPORT,
393 END_DNSKEYONDEMAND,
394 END_SRCIP,
395 END_HOSTACCESS,
396 END_UPDOWN,
397
398 #define END_LAST END_UPDOWN /* last end description*/
399
400 /* Connection Description options -- segregated */
401
402 # define CD_FIRST CD_TO /* first connection description */
403 CD_TO,
404
405 # define CD_POLICY_FIRST CD_PSK
406 CD_PSK, /* same order as POLICY_* */
407 CD_RSASIG, /* same order as POLICY_* */
408 CD_ENCRYPT, /* same order as POLICY_* */
409 CD_AUTHENTICATE, /* same order as POLICY_* */
410 CD_COMPRESS, /* same order as POLICY_* */
411 CD_TUNNEL, /* same order as POLICY_* */
412 CD_PFS, /* same order as POLICY_* */
413 CD_DISABLEARRIVALCHECK, /* same order as POLICY_* */
414 CD_SHUNT0, /* same order as POLICY_* */
415 CD_SHUNT1, /* same order as POLICY_* */
416 CD_FAIL0, /* same order as POLICY_* */
417 CD_FAIL1, /* same order as POLICY_* */
418 CD_DONT_REKEY, /* same order as POLICY_* */
419
420 CD_TUNNELIPV4,
421 CD_TUNNELIPV6,
422 CD_CONNIPV4,
423 CD_CONNIPV6,
424
425 CD_IKELIFETIME,
426 CD_IPSECLIFETIME,
427 CD_RKMARGIN,
428 CD_RKFUZZ,
429 CD_KTRIES,
430 CD_DPDACTION,
431 CD_DPDDELAY,
432 CD_DPDTIMEOUT,
433 CD_IKE,
434 CD_PFSGROUP,
435 CD_ESP,
436
437 # define CD_LAST CD_ESP /* last connection description */
438
439 /* Certificate Authority (CA) description options */
440
441 # define CA_FIRST CA_NAME /* first ca description */
442
443 CA_NAME,
444 CA_CERT,
445 CA_LDAPHOST,
446 CA_LDAPBASE,
447 CA_CRLURI,
448 CA_CRLURI2,
449 CA_OCSPURI,
450 CA_STRICT
451
452 # define CA_LAST CA_STRICT /* last ca description */
453
454 #ifdef DEBUG /* must be last so others are less than 32 to fit in lset_t */
455 # define DBGOPT_FIRST DBGOPT_NONE
456 ,
457 /* NOTE: these definitions must match DBG_* and IMPAIR_* in constants.h */
458 DBGOPT_NONE,
459 DBGOPT_ALL,
460
461 DBGOPT_RAW, /* same order as DBG_* */
462 DBGOPT_CRYPT, /* same order as DBG_* */
463 DBGOPT_PARSING, /* same order as DBG_* */
464 DBGOPT_EMITTING, /* same order as DBG_* */
465 DBGOPT_CONTROL, /* same order as DBG_* */
466 DBGOPT_LIFECYCLE, /* same order as DBG_* */
467 DBGOPT_KLIPS, /* same order as DBG_* */
468 DBGOPT_DNS, /* same order as DBG_* */
469 DBGOPT_NATT, /* same order as DBG_* */
470 DBGOPT_OPPO, /* same order as DBG_* */
471 DBGOPT_CONTROLMORE, /* same order as DBG_* */
472
473 DBGOPT_PRIVATE, /* same order as DBG_* */
474
475 DBGOPT_IMPAIR_DELAY_ADNS_KEY_ANSWER, /* same order as IMPAIR_* */
476 DBGOPT_IMPAIR_DELAY_ADNS_TXT_ANSWER, /* same order as IMPAIR_* */
477 DBGOPT_IMPAIR_BUST_MI2, /* same order as IMPAIR_* */
478 DBGOPT_IMPAIR_BUST_MR2 /* same order as IMPAIR_* */
479
480 # define DBGOPT_LAST DBGOPT_IMPAIR_BUST_MR2
481 #endif
482
483 };
484
485 /* Carve up space for result from getop_long.
486 * Stupidly, the only result is an int.
487 * Numeric arg is bit immediately left of basic value.
488 *
489 */
490 #define OPTION_OFFSET 256 /* to get out of the way of letter options */
491 #define NUMERIC_ARG (1 << 9) /* expect a numeric argument */
492 #define AUX_SHIFT 10 /* amount to shift for aux information */
493
494 static const struct option long_opts[] = {
495 # define OO OPTION_OFFSET
496 /* name, has_arg, flag, val */
497
498 { "help", no_argument, NULL, 'h' },
499 { "version", no_argument, NULL, 'v' },
500 { "optionsfrom", required_argument, NULL, '+' },
501 { "label", required_argument, NULL, 'l' },
502
503 { "ctlbase", required_argument, NULL, OPT_CTLBASE + OO },
504 { "name", required_argument, NULL, OPT_NAME + OO },
505
506 { "keyid", required_argument, NULL, OPT_KEYID + OO },
507 { "addkey", no_argument, NULL, OPT_ADDKEY + OO },
508 { "pubkeyrsa", required_argument, NULL, OPT_PUBKEYRSA + OO },
509
510 { "myid", required_argument, NULL, OPT_MYID + OO },
511
512 { "route", no_argument, NULL, OPT_ROUTE + OO },
513 { "unroute", no_argument, NULL, OPT_UNROUTE + OO },
514
515 { "initiate", no_argument, NULL, OPT_INITIATE + OO },
516 { "terminate", no_argument, NULL, OPT_TERMINATE + OO },
517 { "delete", no_argument, NULL, OPT_DELETE + OO },
518 { "deletestate", required_argument, NULL, OPT_DELETESTATE + OO + NUMERIC_ARG },
519 { "crash", required_argument, NULL, OPT_DELETECRASH + OO },
520 { "listen", no_argument, NULL, OPT_LISTEN + OO },
521 { "unlisten", no_argument, NULL, OPT_UNLISTEN + OO },
522
523 { "purgeocsp", no_argument, NULL, OPT_PURGEOCSP + OO },
524
525 { "rereadsecrets", no_argument, NULL, OPT_REREADSECRETS + OO },
526 { "rereadcacerts", no_argument, NULL, OPT_REREADCACERTS + OO },
527 { "rereadaacerts", no_argument, NULL, OPT_REREADAACERTS + OO },
528 { "rereadocspcerts", no_argument, NULL, OPT_REREADOCSPCERTS + OO },
529 { "rereadacerts", no_argument, NULL, OPT_REREADACERTS + OO },
530 { "rereadcrls", no_argument, NULL, OPT_REREADCRLS + OO },
531 { "rereadall", no_argument, NULL, OPT_REREADALL + OO },
532 { "status", no_argument, NULL, OPT_STATUS + OO },
533 { "statusall", no_argument, NULL, OPT_STATUSALL + OO },
534 { "shutdown", no_argument, NULL, OPT_SHUTDOWN + OO },
535
536 { "oppohere", required_argument, NULL, OPT_OPPO_HERE + OO },
537 { "oppothere", required_argument, NULL, OPT_OPPO_THERE + OO },
538
539 { "asynchronous", no_argument, NULL, OPT_ASYNC + OO },
540
541 /* smartcard options */
542
543 { "scencrypt", required_argument, NULL, SC_ENCRYPT + OO },
544 { "scdecrypt", required_argument, NULL, SC_DECRYPT + OO },
545 { "inbase", required_argument, NULL, SC_INBASE + OO },
546 { "outbase", required_argument, NULL, SC_OUTBASE + OO },
547
548 /* list options */
549
550 { "utc", no_argument, NULL, LST_UTC + OO },
551 { "listalgs", no_argument, NULL, LST_ALGS + OO },
552 { "listpubkeys", no_argument, NULL, LST_PUBKEYS + OO },
553 { "listcerts", no_argument, NULL, LST_CERTS + OO },
554 { "listcacerts", no_argument, NULL, LST_CACERTS + OO },
555 { "listacerts", no_argument, NULL, LST_ACERTS + OO },
556 { "listaacerts", no_argument, NULL, LST_AACERTS + OO },
557 { "listocspcerts", no_argument, NULL, LST_OCSPCERTS + OO },
558 { "listgroups", no_argument, NULL, LST_GROUPS + OO },
559 { "listcainfos", no_argument, NULL, LST_CAINFOS + OO },
560 { "listcrls", no_argument, NULL, LST_CRLS + OO },
561 { "listocsp", no_argument, NULL, LST_OCSP + OO },
562 { "listcards", no_argument, NULL, LST_CARDS + OO },
563 { "listall", no_argument, NULL, LST_ALL + OO },
564
565 /* options for an end description */
566
567 { "host", required_argument, NULL, END_HOST + OO },
568 { "id", required_argument, NULL, END_ID + OO },
569 { "cert", required_argument, NULL, END_CERT + OO },
570 { "ca", required_argument, NULL, END_CA + OO },
571 { "sendcert", required_argument, NULL, END_SENDCERT + OO },
572 { "groups", required_argument, NULL, END_GROUPS + OO },
573 { "ikeport", required_argument, NULL, END_IKEPORT + OO + NUMERIC_ARG },
574 { "nexthop", required_argument, NULL, END_NEXTHOP + OO },
575 { "client", required_argument, NULL, END_CLIENT + OO },
576 { "clientwithin", required_argument, NULL, END_CLIENTWITHIN + OO },
577 { "clientprotoport", required_argument, NULL, END_CLIENTPROTOPORT + OO },
578 { "dnskeyondemand", no_argument, NULL, END_DNSKEYONDEMAND + OO },
579 { "srcip", required_argument, NULL, END_SRCIP + OO },
580 { "hostaccess", no_argument, NULL, END_HOSTACCESS + OO },
581 { "updown", required_argument, NULL, END_UPDOWN + OO },
582
583 /* options for a connection description */
584
585 { "to", no_argument, NULL, CD_TO + OO },
586
587 { "psk", no_argument, NULL, CD_PSK + OO },
588 { "rsasig", no_argument, NULL, CD_RSASIG + OO },
589
590 { "encrypt", no_argument, NULL, CD_ENCRYPT + OO },
591 { "authenticate", no_argument, NULL, CD_AUTHENTICATE + OO },
592 { "compress", no_argument, NULL, CD_COMPRESS + OO },
593 { "tunnel", no_argument, NULL, CD_TUNNEL + OO },
594 { "tunnelipv4", no_argument, NULL, CD_TUNNELIPV4 + OO },
595 { "tunnelipv6", no_argument, NULL, CD_TUNNELIPV6 + OO },
596 { "pfs", no_argument, NULL, CD_PFS + OO },
597 { "disablearrivalcheck", no_argument, NULL, CD_DISABLEARRIVALCHECK + OO },
598 { "initiateontraffic", no_argument, NULL
599 , CD_SHUNT0 + (POLICY_SHUNT_TRAP >> POLICY_SHUNT_SHIFT << AUX_SHIFT) + OO },
600 { "pass", no_argument, NULL
601 , CD_SHUNT0 + (POLICY_SHUNT_PASS >> POLICY_SHUNT_SHIFT << AUX_SHIFT) + OO },
602 { "drop", no_argument, NULL
603 , CD_SHUNT0 + (POLICY_SHUNT_DROP >> POLICY_SHUNT_SHIFT << AUX_SHIFT) + OO },
604 { "reject", no_argument, NULL
605 , CD_SHUNT0 + (POLICY_SHUNT_REJECT >> POLICY_SHUNT_SHIFT << AUX_SHIFT) + OO },
606 { "failnone", no_argument, NULL
607 , CD_FAIL0 + (POLICY_FAIL_NONE >> POLICY_FAIL_SHIFT << AUX_SHIFT) + OO },
608 { "failpass", no_argument, NULL
609 , CD_FAIL0 + (POLICY_FAIL_PASS >> POLICY_FAIL_SHIFT << AUX_SHIFT) + OO },
610 { "faildrop", no_argument, NULL
611 , CD_FAIL0 + (POLICY_FAIL_DROP >> POLICY_FAIL_SHIFT << AUX_SHIFT) + OO },
612 { "failreject", no_argument, NULL
613 , CD_FAIL0 + (POLICY_FAIL_REJECT >> POLICY_FAIL_SHIFT << AUX_SHIFT) + OO },
614 { "dontrekey", no_argument, NULL, CD_DONT_REKEY + OO },
615 { "ipv4", no_argument, NULL, CD_CONNIPV4 + OO },
616 { "ipv6", no_argument, NULL, CD_CONNIPV6 + OO },
617
618 { "ikelifetime", required_argument, NULL, CD_IKELIFETIME + OO + NUMERIC_ARG },
619 { "ipseclifetime", required_argument, NULL, CD_IPSECLIFETIME + OO + NUMERIC_ARG },
620 { "rekeymargin", required_argument, NULL, CD_RKMARGIN + OO + NUMERIC_ARG },
621 { "rekeywindow", required_argument, NULL, CD_RKMARGIN + OO + NUMERIC_ARG }, /* OBSOLETE */
622 { "rekeyfuzz", required_argument, NULL, CD_RKFUZZ + OO + NUMERIC_ARG },
623 { "keyingtries", required_argument, NULL, CD_KTRIES + OO + NUMERIC_ARG },
624 { "dpdaction", required_argument, NULL, CD_DPDACTION + OO },
625 { "dpddelay", required_argument, NULL, CD_DPDDELAY + OO + NUMERIC_ARG },
626 { "dpdtimeout", required_argument, NULL, CD_DPDTIMEOUT + OO + NUMERIC_ARG },
627 { "ike", required_argument, NULL, CD_IKE + OO },
628 { "pfsgroup", required_argument, NULL, CD_PFSGROUP + OO },
629 { "esp", required_argument, NULL, CD_ESP + OO },
630
631 /* options for a ca description */
632
633 { "caname", required_argument, NULL, CA_NAME + OO },
634 { "cacert", required_argument, NULL, CA_CERT + OO },
635 { "ldaphost", required_argument, NULL, CA_LDAPHOST + OO },
636 { "ldapbase", required_argument, NULL, CA_LDAPBASE + OO },
637 { "crluri", required_argument, NULL, CA_CRLURI + OO },
638 { "crluri2", required_argument, NULL, CA_CRLURI2 + OO },
639 { "ocspuri", required_argument, NULL, CA_OCSPURI + OO },
640 { "strictcrlpolicy", no_argument, NULL, CA_STRICT + OO },
641
642 #ifdef DEBUG
643 { "debug-none", no_argument, NULL, DBGOPT_NONE + OO },
644 { "debug-all]", no_argument, NULL, DBGOPT_ALL + OO },
645 { "debug-raw", no_argument, NULL, DBGOPT_RAW + OO },
646 { "debug-crypt", no_argument, NULL, DBGOPT_CRYPT + OO },
647 { "debug-parsing", no_argument, NULL, DBGOPT_PARSING + OO },
648 { "debug-emitting", no_argument, NULL, DBGOPT_EMITTING + OO },
649 { "debug-control", no_argument, NULL, DBGOPT_CONTROL + OO },
650 { "debug-lifecycle", no_argument, NULL, DBGOPT_LIFECYCLE + OO },
651 { "debug-klips", no_argument, NULL, DBGOPT_KLIPS + OO },
652 { "debug-dns", no_argument, NULL, DBGOPT_DNS + OO },
653 { "debug-natt", no_argument, NULL, DBGOPT_NATT + OO },
654 { "debug-oppo", no_argument, NULL, DBGOPT_OPPO + OO },
655 { "debug-controlmore", no_argument, NULL, DBGOPT_CONTROLMORE + OO },
656 { "debug-private", no_argument, NULL, DBGOPT_PRIVATE + OO },
657
658 { "impair-delay-adns-key-answer", no_argument, NULL, DBGOPT_IMPAIR_DELAY_ADNS_KEY_ANSWER + OO },
659 { "impair-delay-adns-txt-answer", no_argument, NULL, DBGOPT_IMPAIR_DELAY_ADNS_TXT_ANSWER + OO },
660 { "impair-bust-mi2", no_argument, NULL, DBGOPT_IMPAIR_BUST_MI2 + OO },
661 { "impair-bust-mr2", no_argument, NULL, DBGOPT_IMPAIR_BUST_MR2 + OO },
662 #endif
663 # undef OO
664 { 0,0,0,0 }
665 };
666
667 struct sockaddr_un ctl_addr = { AF_UNIX, DEFAULT_CTLBASE CTL_SUFFIX };
668
669 /* helper variables and function to encode strings from whack message */
670
671 static char *next_str,*str_roof;
672
673 static bool pack_str(char **p)
674 {
675 const char *s = *p == NULL? "" : *p; /* note: NULL becomes ""! */
676 size_t len = strlen(s) + 1;
677
678 if (str_roof - next_str < (ptrdiff_t)len)
679 {
680 return FALSE; /* fishy: no end found */
681 }
682 else
683 {
684 strcpy(next_str, s);
685 next_str += len;
686 *p = NULL; /* don't send pointers on the wire! */
687 return TRUE;
688 }
689 }
690
691 static void check_life_time(time_t life, time_t limit, const char *which,
692 const whack_message_t *msg)
693 {
694 time_t mint = msg->sa_rekey_margin * (100 + msg->sa_rekey_fuzz) / 100;
695
696 if (life > limit)
697 {
698 char buf[200]; /* arbitrary limit */
699
700 snprintf(buf, sizeof(buf)
701 , "%s [%lu seconds] must be less than %lu seconds"
702 , which, (unsigned long)life, (unsigned long)limit);
703 diag(buf);
704 }
705 if ((msg->policy & POLICY_DONT_REKEY) == LEMPTY && life <= mint)
706 {
707 char buf[200]; /* arbitrary limit */
708
709 snprintf(buf, sizeof(buf)
710 , "%s [%lu] must be greater than"
711 " rekeymargin*(100+rekeyfuzz)/100 [%lu*(100+%lu)/100 = %lu]"
712 , which
713 , (unsigned long)life
714 , (unsigned long)msg->sa_rekey_margin
715 , (unsigned long)msg->sa_rekey_fuzz
716 , (unsigned long)mint);
717 diag(buf);
718 }
719 }
720
721 static void clear_end(whack_end_t *e)
722 {
723 zero(e);
724 e->id = NULL;
725 e->cert = NULL;
726 e->ca = NULL;
727 e->updown = NULL;
728 e->host_port = IKE_UDP_PORT;
729 }
730
731 static void update_ports(whack_message_t *m)
732 {
733 int port;
734
735 if (m->left.port != 0) {
736 port = htons(m->left.port);
737 setportof(port, &m->left.host_addr);
738 setportof(port, &m->left.client.addr);
739 }
740 if (m->right.port != 0) {
741 port = htons(m->right.port);
742 setportof(port, &m->right.host_addr);
743 setportof(port, &m->right.client.addr);
744 }
745 }
746
747 static void check_end(whack_end_t *this, whack_end_t *that,
748 bool default_nexthop, sa_family_t caf, sa_family_t taf)
749 {
750 if (caf != addrtypeof(&this->host_addr))
751 diag("address family of host inconsistent");
752
753 if (default_nexthop)
754 {
755 if (isanyaddr(&that->host_addr))
756 diag("our nexthop must be specified when other host is a %any or %opportunistic");
757 this->host_nexthop = that->host_addr;
758 }
759
760 if (caf != addrtypeof(&this->host_nexthop))
761 diag("address family of nexthop inconsistent");
762
763 if (this->has_client)
764 {
765 if (taf != subnettypeof(&this->client))
766 diag("address family of client subnet inconsistent");
767 }
768 else
769 {
770 /* fill in anyaddr-anyaddr as (missing) client subnet */
771 ip_address cn;
772
773 diagq(anyaddr(caf, &cn), NULL);
774 diagq(rangetosubnet(&cn, &cn, &this->client), NULL);
775 }
776
777 /* fill in anyaddr if source IP is not defined */
778 if (!this->has_srcip)
779 diagq(anyaddr(caf, &this->host_srcip), optarg);
780
781 /* check protocol */
782 if (this->protocol != that->protocol)
783 diag("the protocol for leftprotoport and rightprotoport must be the same");
784 }
785
786 static void get_secret(int sock)
787 {
788 const char *buf, *secret;
789 int len;
790
791 fflush(stdout);
792 usleep(20000); /* give fflush time for flushing */
793 buf = getpass("Enter: ");
794 secret = (buf == NULL)? "" : buf;
795
796 /* send the secret to pluto */
797 len = strlen(secret) + 1;
798 if (write(sock, secret, len) != len)
799 {
800 int e = errno;
801
802 fprintf(stderr, "whack: write() failed (%d %s)\n", e, strerror(e));
803 exit(RC_WHACK_PROBLEM);
804 }
805 }
806
807 /* This is a hack for initiating ISAKMP exchanges. */
808
809 int main(int argc, char **argv)
810 {
811 whack_message_t msg;
812 char esp_buf[256]; /* uses snprintf */
813 lset_t
814 opts_seen = LEMPTY,
815 sc_seen = LEMPTY,
816 lst_seen = LEMPTY,
817 cd_seen = LEMPTY,
818 ca_seen = LEMPTY,
819 end_seen = LEMPTY,
820 end_seen_before_to = LEMPTY;
821 const char
822 *af_used_by = NULL,
823 *tunnel_af_used_by = NULL;
824
825 /* check division of numbering space */
826 #ifdef DEBUG
827 assert(OPTION_OFFSET + DBGOPT_LAST < NUMERIC_ARG);
828 #else
829 assert(OPTION_OFFSET + CA_LAST < NUMERIC_ARG);
830 #endif
831 assert(OPT_LAST - OPT_FIRST < (sizeof opts_seen * BITS_PER_BYTE));
832 assert(SC_LAST - SC_FIRST < (sizeof sc_seen * BITS_PER_BYTE));
833 assert(LST_LAST - LST_FIRST < (sizeof lst_seen * BITS_PER_BYTE));
834 assert(END_LAST - END_FIRST < (sizeof end_seen * BITS_PER_BYTE));
835 assert(CD_LAST - CD_FIRST < (sizeof cd_seen * BITS_PER_BYTE));
836 assert(CA_LAST - CA_FIRST < (sizeof ca_seen * BITS_PER_BYTE));
837 #ifdef DEBUG /* must be last so others are less than (sizeof cd_seen * BITS_PER_BYTE) to fit in lset_t */
838 assert(DBGOPT_LAST - DBGOPT_FIRST < (sizeof cd_seen * BITS_PER_BYTE));
839 #endif
840 /* check that POLICY bit assignment matches with CD_ */
841 assert(LELEM(CD_DONT_REKEY - CD_POLICY_FIRST) == POLICY_DONT_REKEY);
842
843 zero(&msg);
844
845 clear_end(&msg.right); /* left set from this after --to */
846
847 msg.name = NULL;
848 msg.keyid = NULL;
849 msg.keyval.ptr = NULL;
850 msg.esp = NULL;
851 msg.ike = NULL;
852 msg.pfsgroup = NULL;
853
854 /* if a connection is added via whack then we assume IKEv1 */
855 msg.ikev1 = TRUE;
856
857 msg.sa_ike_life_seconds = OAKLEY_ISAKMP_SA_LIFETIME_DEFAULT;
858 msg.sa_ipsec_life_seconds = PLUTO_SA_LIFE_DURATION_DEFAULT;
859 msg.sa_rekey_margin = SA_REPLACEMENT_MARGIN_DEFAULT;
860 msg.sa_rekey_fuzz = SA_REPLACEMENT_FUZZ_DEFAULT;
861 msg.sa_keying_tries = SA_REPLACEMENT_RETRIES_DEFAULT;
862
863 msg.addr_family = AF_INET;
864 msg.tunnel_addr_family = AF_INET;
865
866 msg.cacert = NULL;
867 msg.ldaphost = NULL;
868 msg.ldapbase = NULL;
869 msg.crluri = NULL;
870 msg.crluri2 = NULL;
871 msg.ocspuri = NULL;
872
873 options = options_create();
874
875 for (;;)
876 {
877 int long_index;
878 unsigned long opt_whole = 0; /* numeric argument for some flags */
879
880 /* Note: we don't like the way short options get parsed
881 * by getopt_long, so we simply pass an empty string as
882 * the list. It could be "hp:d:c:o:eatfs" "NARXPECK".
883 */
884 int c = getopt_long(argc, argv, "", long_opts, &long_index) - OPTION_OFFSET;
885 int aux = 0;
886
887 /* decode a numeric argument, if expected */
888 if (0 <= c)
889 {
890 if (c & NUMERIC_ARG)
891 {
892 char *endptr;
893
894 c -= NUMERIC_ARG;
895 opt_whole = strtoul(optarg, &endptr, 0);
896
897 if (*endptr != '\0' || endptr == optarg)
898 diagq("badly formed numeric argument", optarg);
899 }
900 if (c >= (1 << AUX_SHIFT))
901 {
902 aux = c >> AUX_SHIFT;
903 c -= aux << AUX_SHIFT;
904 }
905 }
906
907 /* per-class option processing */
908 if (0 <= c && c <= OPT_LAST)
909 {
910 /* OPT_* options get added to opts_seen.
911 * Reject repeated options (unless later code intervenes).
912 */
913 lset_t f = LELEM(c);
914
915 if (opts_seen & f)
916 diagq("duplicated flag", long_opts[long_index].name);
917 opts_seen |= f;
918 }
919 else if (SC_FIRST <= c && c <= SC_LAST)
920 {
921 /* SC_* options get added to sc_seen.
922 * Reject repeated options (unless later code intervenes).
923 */
924 lset_t f = LELEM(c - SC_FIRST);
925
926 if (sc_seen & f)
927 diagq("duplicated flag", long_opts[long_index].name);
928 sc_seen |= f;
929 }
930 else if (LST_FIRST <= c && c <= LST_LAST)
931 {
932 /* LST_* options get added to lst_seen.
933 * Reject repeated options (unless later code intervenes).
934 */
935 lset_t f = LELEM(c - LST_FIRST);
936
937 if (lst_seen & f)
938 diagq("duplicated flag", long_opts[long_index].name);
939 lst_seen |= f;
940 }
941 #ifdef DEBUG
942 else if (DBGOPT_FIRST <= c && c <= DBGOPT_LAST)
943 {
944 msg.whack_options = TRUE;
945 }
946 #endif
947 else if (END_FIRST <= c && c <= END_LAST)
948 {
949 /* END_* options are added to end_seen.
950 * Reject repeated options (unless later code intervenes).
951 */
952 lset_t f = LELEM(c - END_FIRST);
953
954 if (end_seen & f)
955 diagq("duplicated flag", long_opts[long_index].name);
956 end_seen |= f;
957 opts_seen |= LELEM(OPT_CD);
958 }
959 else if (CD_FIRST <= c && c <= CD_LAST)
960 {
961 /* CD_* options are added to cd_seen.
962 * Reject repeated options (unless later code intervenes).
963 */
964 lset_t f = LELEM(c - CD_FIRST);
965
966 if (cd_seen & f)
967 diagq("duplicated flag", long_opts[long_index].name);
968 cd_seen |= f;
969 opts_seen |= LELEM(OPT_CD);
970 }
971 else if (CA_FIRST <= c && c <= CA_LAST)
972 {
973 /* CA_* options are added to ca_seen.
974 * Reject repeated options (unless later code intervenes).
975 */
976 lset_t f = LELEM(c - CA_FIRST);
977
978 if (ca_seen & f)
979 diagq("duplicated flag", long_opts[long_index].name);
980 ca_seen |= f;
981 }
982
983 /* Note: "break"ing from switch terminates loop.
984 * most cases should end with "continue".
985 */
986 switch (c)
987 {
988 case EOF - OPTION_OFFSET: /* end of flags */
989 break;
990
991 case 0 - OPTION_OFFSET: /* long option already handled */
992 continue;
993
994 case ':' - OPTION_OFFSET: /* diagnostic already printed by getopt_long */
995 case '?' - OPTION_OFFSET: /* diagnostic already printed by getopt_long */
996 diag(NULL); /* print no additional diagnostic, but exit sadly */
997 break; /* not actually reached */
998
999 case 'h' - OPTION_OFFSET: /* --help */
1000 help();
1001 whack_exit(0); /* GNU coding standards say to stop here */
1002
1003 case 'v' - OPTION_OFFSET: /* --version */
1004 {
1005 const char **sp = ipsec_copyright_notice();
1006
1007 printf("%s\n", ipsec_version_string());
1008 for (; *sp != NULL; sp++)
1009 puts(*sp);
1010 }
1011 whack_exit(0); /* GNU coding standards say to stop here */
1012
1013 case 'l' - OPTION_OFFSET: /* --label <string> */
1014 label = optarg; /* remember for diagnostics */
1015 continue;
1016
1017 case '+' - OPTION_OFFSET: /* --optionsfrom <filename> */
1018 if (!options->from(options, optarg, &argc, &argv, optind))
1019 {
1020 fprintf(stderr, "optionsfrom failed");
1021 whack_exit(RC_WHACK_PROBLEM);
1022 }
1023 continue;
1024
1025 /* the rest of the options combine in complex ways */
1026
1027 case OPT_CTLBASE: /* --port <ctlbase> */
1028 if (snprintf(ctl_addr.sun_path, sizeof(ctl_addr.sun_path)
1029 , "%s%s", optarg, CTL_SUFFIX) == -1)
1030 diag("<ctlbase>" CTL_SUFFIX " must be fit in a sun_addr");
1031 continue;
1032
1033 case OPT_NAME: /* --name <connection-name> */
1034 name = optarg;
1035 msg.name = optarg;
1036 continue;
1037
1038 case OPT_KEYID: /* --keyid <identity> */
1039 msg.whack_key = !msg.whack_sc_op;
1040 msg.keyid = optarg; /* decoded by Pluto */
1041 continue;
1042
1043 case OPT_MYID: /* --myid <identity> */
1044 msg.whack_myid = TRUE;
1045 msg.myid = optarg; /* decoded by Pluto */
1046 continue;
1047
1048 case OPT_ADDKEY: /* --addkey */
1049 msg.whack_addkey = TRUE;
1050 continue;
1051
1052 case OPT_PUBKEYRSA: /* --pubkeyrsa <key> */
1053 {
1054 static char keyspace[RSA_MAX_ENCODING_BYTES]; /* room for 8K bit key */
1055 char diag_space[TTODATAV_BUF];
1056 const char *ugh = ttodatav(optarg, 0, 0
1057 , keyspace, sizeof(keyspace)
1058 , &msg.keyval.len, diag_space, sizeof(diag_space)
1059 , TTODATAV_SPACECOUNTS);
1060
1061 if (ugh != NULL)
1062 {
1063 char ugh_space[80]; /* perhaps enough space */
1064
1065 snprintf(ugh_space, sizeof(ugh_space)
1066 , "RSA public-key data malformed (%s)", ugh);
1067 diagq(ugh_space, optarg);
1068 }
1069 msg.pubkey_alg = PUBKEY_ALG_RSA;
1070 msg.keyval.ptr = keyspace;
1071 }
1072 continue;
1073
1074 case OPT_ROUTE: /* --route */
1075 msg.whack_route = TRUE;
1076 continue;
1077
1078 case OPT_UNROUTE: /* --unroute */
1079 msg.whack_unroute = TRUE;
1080 continue;
1081
1082 case OPT_INITIATE: /* --initiate */
1083 msg.whack_initiate = TRUE;
1084 continue;
1085
1086 case OPT_TERMINATE: /* --terminate */
1087 msg.whack_terminate = TRUE;
1088 continue;
1089
1090 case OPT_DELETE: /* --delete */
1091 msg.whack_delete = TRUE;
1092 continue;
1093
1094 case OPT_DELETESTATE: /* --deletestate <state_object_number> */
1095 msg.whack_deletestate = TRUE;
1096 msg.whack_deletestateno = opt_whole;
1097 continue;
1098
1099 case OPT_DELETECRASH: /* --crash <ip-address> */
1100 msg.whack_crash = TRUE;
1101 tunnel_af_used_by = long_opts[long_index].name;
1102 diagq(ttoaddr(optarg, 0, msg.tunnel_addr_family, &msg.whack_crash_peer), optarg);
1103 if (isanyaddr(&msg.whack_crash_peer))
1104 diagq("0.0.0.0 or 0::0 isn't a valid client address", optarg);
1105 continue;
1106
1107 case OPT_LISTEN: /* --listen */
1108 msg.whack_listen = TRUE;
1109 continue;
1110
1111 case OPT_UNLISTEN: /* --unlisten */
1112 msg.whack_unlisten = TRUE;
1113 continue;
1114
1115 case OPT_PURGEOCSP: /* --purgeocsp */
1116 msg.whack_purgeocsp = TRUE;
1117 continue;
1118
1119 case OPT_REREADSECRETS: /* --rereadsecrets */
1120 case OPT_REREADCACERTS: /* --rereadcacerts */
1121 case OPT_REREADAACERTS: /* --rereadaacerts */
1122 case OPT_REREADOCSPCERTS: /* --rereadocspcerts */
1123 case OPT_REREADACERTS: /* --rereadacerts */
1124 case OPT_REREADCRLS: /* --rereadcrls */
1125 msg.whack_reread |= LELEM(c-OPT_REREADSECRETS);
1126 continue;
1127
1128 case OPT_REREADALL: /* --rereadall */
1129 msg.whack_reread = REREAD_ALL;
1130 continue;
1131
1132 case OPT_STATUSALL: /* --statusall */
1133 msg.whack_statusall = TRUE;
1134
1135 case OPT_STATUS: /* --status */
1136 msg.whack_status = TRUE;
1137 continue;
1138
1139 case OPT_SHUTDOWN: /* --shutdown */
1140 msg.whack_shutdown = TRUE;
1141 continue;
1142
1143 case OPT_OPPO_HERE: /* --oppohere <ip-address> */
1144 tunnel_af_used_by = long_opts[long_index].name;
1145 diagq(ttoaddr(optarg, 0, msg.tunnel_addr_family, &msg.oppo_my_client), optarg);
1146 if (isanyaddr(&msg.oppo_my_client))
1147 diagq("0.0.0.0 or 0::0 isn't a valid client address", optarg);
1148 continue;
1149
1150 case OPT_OPPO_THERE: /* --oppohere <ip-address> */
1151 tunnel_af_used_by = long_opts[long_index].name;
1152 diagq(ttoaddr(optarg, 0, msg.tunnel_addr_family, &msg.oppo_peer_client), optarg);
1153 if (isanyaddr(&msg.oppo_peer_client))
1154 diagq("0.0.0.0 or 0::0 isn't a valid client address", optarg);
1155 continue;
1156
1157 case OPT_ASYNC:
1158 msg.whack_async = TRUE;
1159 continue;
1160
1161 /* Smartcard options */
1162
1163 case SC_ENCRYPT: /* --scencrypt <plaintext data> */
1164 case SC_DECRYPT: /* --scdecrypt <encrypted data> */
1165 msg.whack_sc_op = 1 + c - SC_ENCRYPT;
1166 msg.whack_key = FALSE;
1167 msg.sc_data = optarg;
1168 continue;
1169
1170 case SC_INBASE: /* --inform <format> */
1171 case SC_OUTBASE: /* --outform <format> */
1172 {
1173 int base = 0;
1174
1175 if (streq(optarg, "16") || strcaseeq(optarg, "hex"))
1176 base = 16;
1177 else if (streq(optarg, "64") || strcaseeq(optarg, "base64"))
1178 base = 64;
1179 else if (streq(optarg, "256") || strcaseeq(optarg, "text")
1180 || strcaseeq(optarg, "ascii"))
1181 base = 256;
1182 else
1183 diagq("not a valid base", optarg);
1184
1185 if (c == SC_INBASE)
1186 msg.inbase = base;
1187 else
1188 msg.outbase = base;
1189 }
1190 continue;
1191
1192 /* List options */
1193
1194 case LST_UTC: /* --utc */
1195 msg.whack_utc = TRUE;
1196 continue;
1197
1198 case LST_ALGS: /* --listalgs */
1199 case LST_PUBKEYS: /* --listpubkeys */
1200 case LST_CERTS: /* --listcerts */
1201 case LST_CACERTS: /* --listcacerts */
1202 case LST_ACERTS: /* --listacerts */
1203 case LST_AACERTS: /* --listaacerts */
1204 case LST_OCSPCERTS: /* --listocspcerts */
1205 case LST_GROUPS: /* --listgroups */
1206 case LST_CAINFOS: /* --listcainfos */
1207 case LST_CRLS: /* --listcrls */
1208 case LST_OCSP: /* --listocsp */
1209 case LST_CARDS: /* --listcards */
1210 msg.whack_list |= LELEM(c - LST_ALGS);
1211 continue;
1212
1213 case LST_ALL: /* --listall */
1214 msg.whack_list = LIST_ALL;
1215 continue;
1216
1217 /* Connection Description options */
1218
1219 case END_HOST: /* --host <ip-address> */
1220 {
1221 lset_t new_policy = LEMPTY;
1222
1223 af_used_by = long_opts[long_index].name;
1224 diagq(anyaddr(msg.addr_family, &msg.right.host_addr), optarg);
1225 if (streq(optarg, "%any"))
1226 {
1227 }
1228 else if (streq(optarg, "%opportunistic"))
1229 {
1230 /* always use tunnel mode; mark as opportunistic */
1231 new_policy |= POLICY_TUNNEL | POLICY_OPPO;
1232 }
1233 else if (streq(optarg, "%group"))
1234 {
1235 /* always use tunnel mode; mark as group */
1236 new_policy |= POLICY_TUNNEL | POLICY_GROUP;
1237 }
1238 else if (streq(optarg, "%opportunisticgroup"))
1239 {
1240 /* always use tunnel mode; mark as opportunistic */
1241 new_policy |= POLICY_TUNNEL | POLICY_OPPO | POLICY_GROUP;
1242 }
1243 else
1244 {
1245 diagq(ttoaddr(optarg, 0, msg.addr_family
1246 , &msg.right.host_addr), optarg);
1247 }
1248
1249 msg.policy |= new_policy;
1250
1251 if (new_policy & (POLICY_OPPO | POLICY_GROUP))
1252 {
1253 if (!LHAS(end_seen, END_CLIENT - END_FIRST))
1254 {
1255 /* set host to 0.0.0 and --client to 0.0.0.0/0
1256 * or IPV6 equivalent
1257 */
1258 ip_address any;
1259
1260 tunnel_af_used_by = optarg;
1261 diagq(anyaddr(msg.tunnel_addr_family, &any), optarg);
1262 diagq(initsubnet(&any, 0, '0', &msg.right.client), optarg);
1263 }
1264 msg.right.has_client = TRUE;
1265 }
1266 if (new_policy & POLICY_GROUP)
1267 {
1268 /* client subnet must not be specified by user:
1269 * it will come from the group's file.
1270 */
1271 if (LHAS(end_seen, END_CLIENT - END_FIRST))
1272 diag("--host %group clashes with --client");
1273
1274 end_seen |= LELEM(END_CLIENT - END_FIRST);
1275 }
1276 if (new_policy & POLICY_OPPO)
1277 msg.right.key_from_DNS_on_demand = TRUE;
1278 continue;
1279 }
1280 case END_ID: /* --id <identity> */
1281 msg.right.id = optarg; /* decoded by Pluto */
1282 continue;
1283
1284 case END_CERT: /* --cert <path> */
1285 msg.right.cert = optarg; /* decoded by Pluto */
1286 continue;
1287
1288 case END_CA: /* --ca <distinguished name> */
1289 msg.right.ca = optarg; /* decoded by Pluto */
1290 continue;
1291
1292 case END_SENDCERT:
1293 if (streq(optarg, "yes") || streq(optarg, "always"))
1294 {
1295 msg.right.sendcert = CERT_ALWAYS_SEND;
1296 }
1297 else if (streq(optarg, "no") || streq(optarg, "never"))
1298 {
1299 msg.right.sendcert = CERT_NEVER_SEND;
1300 }
1301 else if (streq(optarg, "ifasked"))
1302 {
1303 msg.right.sendcert = CERT_SEND_IF_ASKED;
1304 }
1305 else
1306 {
1307 diagq("whack sendcert value is not legal", optarg);
1308 }
1309 continue;
1310
1311 case END_GROUPS:/* --groups <access control groups> */
1312 msg.right.groups = optarg; /* decoded by Pluto */
1313 continue;
1314
1315 case END_IKEPORT: /* --ikeport <port-number> */
1316 if (opt_whole<=0 || opt_whole >= 0x10000)
1317 diagq("<port-number> must be a number between 1 and 65535", optarg);
1318 msg.right.host_port = opt_whole;
1319 continue;
1320
1321 case END_NEXTHOP: /* --nexthop <ip-address> */
1322 af_used_by = long_opts[long_index].name;
1323 if (streq(optarg, "%direct"))
1324 diagq(anyaddr(msg.addr_family
1325 , &msg.right.host_nexthop), optarg);
1326 else
1327 diagq(ttoaddr(optarg, 0, msg.addr_family
1328 , &msg.right.host_nexthop), optarg);
1329 continue;
1330
1331 case END_SRCIP: /* --srcip <ip-address> */
1332 af_used_by = long_opts[long_index].name;
1333 if (streq(optarg, "%modeconfig") || streq(optarg, "%modecfg"))
1334 {
1335 msg.right.modecfg = TRUE;
1336 }
1337 else
1338 {
1339 diagq(ttoaddr(optarg, 0, msg.addr_family
1340 , &msg.right.host_srcip), optarg);
1341 msg.right.has_srcip = TRUE;
1342 }
1343 msg.policy |= POLICY_TUNNEL; /* srcip => tunnel */
1344 continue;
1345
1346 case END_CLIENT: /* --client <subnet> */
1347 if (end_seen & LELEM(END_CLIENTWITHIN - END_FIRST))
1348 diag("--client conflicts with --clientwithin");
1349 tunnel_af_used_by = long_opts[long_index].name;
1350 if ((strlen(optarg) >= 6 && strncmp(optarg,"vhost:",6) == 0)
1351 || (strlen(optarg) >= 5 && strncmp(optarg,"vnet:",5) == 0))
1352 {
1353 msg.right.virt = optarg;
1354 }
1355 else
1356 {
1357 diagq(ttosubnet(optarg, 0, msg.tunnel_addr_family, &msg.right.client), optarg);
1358 msg.right.has_client = TRUE;
1359 }
1360 msg.policy |= POLICY_TUNNEL; /* client => tunnel */
1361 continue;
1362
1363 case END_CLIENTWITHIN: /* --clienwithin <address range> */
1364 if (end_seen & LELEM(END_CLIENT - END_FIRST))
1365 diag("--clientwithin conflicts with --client");
1366 tunnel_af_used_by = long_opts[long_index].name;
1367 diagq(ttosubnet(optarg, 0, msg.tunnel_addr_family, &msg.right.client), optarg);
1368 msg.right.has_client = TRUE;
1369 msg.policy |= POLICY_TUNNEL; /* client => tunnel */
1370 msg.right.has_client_wildcard = TRUE;
1371 continue;
1372
1373 case END_CLIENTPROTOPORT: /* --clientprotoport <protocol>/<port> */
1374 diagq(ttoprotoport(optarg, 0, &msg.right.protocol, &msg.right.port
1375 , &msg.right.has_port_wildcard), optarg);
1376 continue;
1377
1378 case END_DNSKEYONDEMAND: /* --dnskeyondemand */
1379 msg.right.key_from_DNS_on_demand = TRUE;
1380 continue;
1381
1382 case END_HOSTACCESS: /* --hostaccess */
1383 msg.right.hostaccess = TRUE;
1384 continue;
1385
1386 case END_UPDOWN: /* --updown <updown> */
1387 msg.right.updown = optarg;
1388 continue;
1389
1390 case CD_TO: /* --to */
1391 /* process right end, move it to left, reset it */
1392 if (!LHAS(end_seen, END_HOST - END_FIRST))
1393 diag("connection missing --host before --to");
1394 msg.left = msg.right;
1395 clear_end(&msg.right);
1396 end_seen_before_to = end_seen;
1397 end_seen = LEMPTY;
1398 continue;
1399
1400 case CD_PSK: /* --psk */
1401 case CD_RSASIG: /* --rsasig */
1402 case CD_ENCRYPT: /* --encrypt */
1403 case CD_AUTHENTICATE: /* --authenticate */
1404 case CD_COMPRESS: /* --compress */
1405 case CD_TUNNEL: /* --tunnel */
1406 case CD_PFS: /* --pfs */
1407 case CD_DISABLEARRIVALCHECK: /* --disablearrivalcheck */
1408 case CD_DONT_REKEY: /* --donotrekey */
1409 msg.policy |= LELEM(c - CD_POLICY_FIRST);
1410 continue;
1411
1412 /* --initiateontraffic
1413 * --pass
1414 * --drop
1415 * --reject
1416 */
1417 case CD_SHUNT0:
1418 msg.policy = (msg.policy & ~POLICY_SHUNT_MASK)
1419 | ((lset_t)aux << POLICY_SHUNT_SHIFT);
1420 continue;
1421
1422 /* --failnone
1423 * --failpass
1424 * --faildrop
1425 * --failreject
1426 */
1427 case CD_FAIL0:
1428 msg.policy = (msg.policy & ~POLICY_FAIL_MASK)
1429 | ((lset_t)aux << POLICY_FAIL_SHIFT);
1430 continue;
1431
1432 case CD_IKELIFETIME: /* --ikelifetime <seconds> */
1433 msg.sa_ike_life_seconds = opt_whole;
1434 continue;
1435
1436 case CD_IPSECLIFETIME: /* --ipseclifetime <seconds> */
1437 msg.sa_ipsec_life_seconds = opt_whole;
1438 continue;
1439
1440 case CD_RKMARGIN: /* --rekeymargin <seconds> */
1441 msg.sa_rekey_margin = opt_whole;
1442 continue;
1443
1444 case CD_RKFUZZ: /* --rekeyfuzz <percentage> */
1445 msg.sa_rekey_fuzz = opt_whole;
1446 continue;
1447
1448 case CD_KTRIES: /* --keyingtries <count> */
1449 msg.sa_keying_tries = opt_whole;
1450 continue;
1451
1452 case CD_DPDACTION:
1453 if (streq(optarg, "none"))
1454 msg.dpd_action = DPD_ACTION_NONE;
1455 else if (streq(optarg, "clear"))
1456 msg.dpd_action = DPD_ACTION_CLEAR;
1457 else if (streq(optarg, "hold"))
1458 msg.dpd_action = DPD_ACTION_HOLD;
1459 else if (streq(optarg, "restart"))
1460 msg.dpd_action = DPD_ACTION_RESTART;
1461 else
1462 msg.dpd_action = DPD_ACTION_UNKNOWN;
1463 continue;
1464
1465 case CD_DPDDELAY:
1466 msg.dpd_delay = opt_whole;
1467 continue;
1468
1469 case CD_DPDTIMEOUT:
1470 msg.dpd_timeout = opt_whole;
1471 continue;
1472
1473 case CD_IKE: /* --ike <ike_alg1,ike_alg2,...> */
1474 msg.ike = optarg;
1475 continue;
1476
1477 case CD_PFSGROUP: /* --pfsgroup modpXXXX */
1478 msg.pfsgroup = optarg;
1479 continue;
1480
1481 case CD_ESP: /* --esp <esp_alg1,esp_alg2,...> */
1482 msg.esp = optarg;
1483 continue;
1484
1485 case CD_CONNIPV4:
1486 if (LHAS(cd_seen, CD_CONNIPV6 - CD_FIRST))
1487 diag("--ipv4 conflicts with --ipv6");
1488
1489 /* Since this is the default, the flag is redundant.
1490 * So we don't need to set msg.addr_family
1491 * and we don't need to check af_used_by
1492 * and we don't have to consider defaulting tunnel_addr_family.
1493 */
1494 continue;
1495
1496 case CD_CONNIPV6:
1497 if (LHAS(cd_seen, CD_CONNIPV4 - CD_FIRST))
1498 diag("--ipv6 conflicts with --ipv4");
1499
1500 if (af_used_by != NULL)
1501 diagq("--ipv6 must precede", af_used_by);
1502
1503 af_used_by = long_opts[long_index].name;
1504 msg.addr_family = AF_INET6;
1505
1506 /* Consider defaulting tunnel_addr_family to AF_INET6.
1507 * Do so only if it hasn't yet been specified or used.
1508 */
1509 if (LDISJOINT(cd_seen, LELEM(CD_TUNNELIPV4 - CD_FIRST) | LELEM(CD_TUNNELIPV6 - CD_FIRST))
1510 && tunnel_af_used_by == NULL)
1511 msg.tunnel_addr_family = AF_INET6;
1512 continue;
1513
1514 case CD_TUNNELIPV4:
1515 if (LHAS(cd_seen, CD_TUNNELIPV6 - CD_FIRST))
1516 diag("--tunnelipv4 conflicts with --tunnelipv6");
1517
1518 if (tunnel_af_used_by != NULL)
1519 diagq("--tunnelipv4 must precede", af_used_by);
1520
1521 msg.tunnel_addr_family = AF_INET;
1522 continue;
1523
1524 case CD_TUNNELIPV6:
1525 if (LHAS(cd_seen, CD_TUNNELIPV4 - CD_FIRST))
1526 diag("--tunnelipv6 conflicts with --tunnelipv4");
1527
1528 if (tunnel_af_used_by != NULL)
1529 diagq("--tunnelipv6 must precede", af_used_by);
1530
1531 msg.tunnel_addr_family = AF_INET6;
1532 continue;
1533
1534 case CA_NAME: /* --caname <name> */
1535 msg.name = optarg;
1536 msg.whack_ca = TRUE;
1537 continue;
1538 case CA_CERT: /* --cacert <path> */
1539 msg.cacert = optarg;
1540 continue;
1541 case CA_LDAPHOST: /* --ldaphost <hostname> */
1542 msg.ldaphost = optarg;
1543 continue;
1544 case CA_LDAPBASE: /* --ldapbase <base> */
1545 msg.ldapbase = optarg;
1546 continue;
1547 case CA_CRLURI: /* --crluri <uri> */
1548 msg.crluri = optarg;
1549 continue;
1550 case CA_CRLURI2: /* --crluri2 <uri> */
1551 msg.crluri2 = optarg;
1552 continue;
1553 case CA_OCSPURI: /* --ocspuri <uri> */
1554 msg.ocspuri = optarg;
1555 continue;
1556 case CA_STRICT: /* --strictcrlpolicy */
1557 msg.whack_strict = TRUE;
1558 continue;
1559
1560 #ifdef DEBUG
1561 case DBGOPT_NONE: /* --debug-none */
1562 msg.debugging = DBG_NONE;
1563 continue;
1564
1565 case DBGOPT_ALL: /* --debug-all */
1566 msg.debugging |= DBG_ALL; /* note: does not include PRIVATE */
1567 continue;
1568
1569 case DBGOPT_RAW: /* --debug-raw */
1570 case DBGOPT_CRYPT: /* --debug-crypt */
1571 case DBGOPT_PARSING: /* --debug-parsing */
1572 case DBGOPT_EMITTING: /* --debug-emitting */
1573 case DBGOPT_CONTROL: /* --debug-control */
1574 case DBGOPT_LIFECYCLE: /* --debug-lifecycle */
1575 case DBGOPT_KLIPS: /* --debug-klips */
1576 case DBGOPT_DNS: /* --debug-dns */
1577 case DBGOPT_NATT: /* --debug-natt */
1578 case DBGOPT_OPPO: /* --debug-oppo */
1579 case DBGOPT_CONTROLMORE: /* --debug-controlmore */
1580 case DBGOPT_PRIVATE: /* --debug-private */
1581 case DBGOPT_IMPAIR_DELAY_ADNS_KEY_ANSWER: /* --impair-delay-adns-key-answer */
1582 case DBGOPT_IMPAIR_DELAY_ADNS_TXT_ANSWER: /* --impair-delay-adns-txt-answer */
1583 case DBGOPT_IMPAIR_BUST_MI2: /* --impair_bust_mi2 */
1584 case DBGOPT_IMPAIR_BUST_MR2: /* --impair_bust_mr2 */
1585 msg.debugging |= LELEM(c-DBGOPT_RAW);
1586 continue;
1587 #endif
1588 default:
1589 assert(FALSE); /* unknown return value */
1590 }
1591 break;
1592 }
1593
1594 if (optind != argc)
1595 {
1596 /* If you see this message unexpectedly, perhaps the
1597 * case for the previous option ended with "break"
1598 * instead of "continue"
1599 */
1600 diagq("unexpected argument", argv[optind]);
1601 }
1602
1603 /* For each possible form of the command, figure out if an argument
1604 * suggests whether that form was intended, and if so, whether all
1605 * required information was supplied.
1606 */
1607
1608 /* check opportunistic initiation simulation request */
1609 switch (opts_seen & (LELEM(OPT_OPPO_HERE) | LELEM(OPT_OPPO_THERE)))
1610 {
1611 case LELEM(OPT_OPPO_HERE):
1612 case LELEM(OPT_OPPO_THERE):
1613 diag("--oppohere and --oppothere must be used together");
1614 /*NOTREACHED*/
1615 case LELEM(OPT_OPPO_HERE) | LELEM(OPT_OPPO_THERE):
1616 msg.whack_oppo_initiate = TRUE;
1617 if (LIN(cd_seen, LELEM(CD_TUNNELIPV4 - CD_FIRST) | LELEM(CD_TUNNELIPV6 - CD_FIRST)))
1618 opts_seen &= ~LELEM(OPT_CD);
1619 break;
1620 }
1621
1622 /* check connection description */
1623 if (LHAS(opts_seen, OPT_CD))
1624 {
1625 if (!LHAS(cd_seen, CD_TO-CD_FIRST))
1626 diag("connection description option, but no --to");
1627
1628 if (!LHAS(end_seen, END_HOST-END_FIRST))
1629 diag("connection missing --host after --to");
1630
1631 if (isanyaddr(&msg.left.host_addr)
1632 && isanyaddr(&msg.right.host_addr))
1633 diag("hosts cannot both be 0.0.0.0 or 0::0");
1634
1635 if (msg.policy & POLICY_OPPO)
1636 {
1637 if ((msg.policy & (POLICY_PSK | POLICY_PUBKEY)) != POLICY_PUBKEY)
1638 diag("only PUBKEY is supported for opportunism");
1639 if ((msg.policy & POLICY_PFS) == 0)
1640 diag("PFS required for opportunism");
1641 if ((msg.policy & POLICY_ENCRYPT) == 0)
1642 diag("encryption required for opportunism");
1643 }
1644
1645 check_end(&msg.left, &msg.right, !LHAS(end_seen_before_to, END_NEXTHOP-END_FIRST)
1646 , msg.addr_family, msg.tunnel_addr_family);
1647
1648 check_end(&msg.right, &msg.left, !LHAS(end_seen, END_NEXTHOP-END_FIRST)
1649 , msg.addr_family, msg.tunnel_addr_family);
1650
1651 if (subnettypeof(&msg.left.client) != subnettypeof(&msg.right.client))
1652 diag("endpoints clash: one is IPv4 and the other is IPv6");
1653
1654 if (NEVER_NEGOTIATE(msg.policy))
1655 {
1656 /* we think this is just a shunt (because he didn't specify
1657 * a host authentication method). If he didn't specify a
1658 * shunt type, he's probably gotten it wrong.
1659 */
1660 if ((msg.policy & POLICY_SHUNT_MASK) == POLICY_SHUNT_TRAP)
1661 diag("non-shunt connection must have --psk or --rsasig or both");
1662 }
1663 else
1664 {
1665 /* not just a shunt: a real ipsec connection */
1666 if ((msg.policy & POLICY_ID_AUTH_MASK) == LEMPTY)
1667 diag("must specify --rsasig or --psk for a connection");
1668
1669 if (!HAS_IPSEC_POLICY(msg.policy)
1670 && (msg.left.has_client || msg.right.has_client))
1671 diag("must not specify clients for ISAKMP-only connection");
1672 }
1673
1674 msg.whack_connection = TRUE;
1675 }
1676
1677 /* decide whether --name is mandatory or forbidden */
1678 if (!LDISJOINT(opts_seen
1679 , LELEM(OPT_ROUTE) | LELEM(OPT_UNROUTE)
1680 | LELEM(OPT_INITIATE) | LELEM(OPT_TERMINATE)
1681 | LELEM(OPT_DELETE) | LELEM(OPT_CD)))
1682 {
1683 if (!LHAS(opts_seen, OPT_NAME) && !msg.whack_ca)
1684 diag("missing --name <connection_name>");
1685 }
1686 else if (!msg.whack_options && !msg.whack_status)
1687 {
1688 if (LHAS(opts_seen, OPT_NAME))
1689 diag("no reason for --name");
1690 }
1691
1692 if (!LDISJOINT(opts_seen, LELEM(OPT_PUBKEYRSA) | LELEM(OPT_ADDKEY)))
1693 {
1694 if (!LHAS(opts_seen, OPT_KEYID))
1695 diag("--addkey and --pubkeyrsa require --keyid");
1696 }
1697
1698 if (!(msg.whack_connection || msg.whack_key || msg.whack_myid
1699 || msg.whack_delete || msg.whack_deletestate
1700 || msg.whack_initiate || msg.whack_oppo_initiate || msg.whack_terminate
1701 || msg.whack_route || msg.whack_unroute || msg.whack_listen
1702 || msg.whack_unlisten || msg.whack_list || msg.whack_purgeocsp || msg.whack_reread
1703 || msg.whack_ca || msg.whack_status || msg.whack_options || msg.whack_shutdown
1704 || msg.whack_sc_op))
1705 {
1706 diag("no action specified; try --help for hints");
1707 }
1708
1709 update_ports(&msg);
1710
1711 /* tricky quick and dirty check for wild values */
1712 if (msg.sa_rekey_margin != 0
1713 && msg.sa_rekey_fuzz * msg.sa_rekey_margin * 4 / msg.sa_rekey_margin / 4
1714 != msg.sa_rekey_fuzz)
1715 diag("rekeymargin or rekeyfuzz values are so large that they cause oveflow");
1716
1717 check_life_time (msg.sa_ike_life_seconds, OAKLEY_ISAKMP_SA_LIFETIME_MAXIMUM
1718 , "ikelifetime", &msg);
1719
1720 check_life_time(msg.sa_ipsec_life_seconds, SA_LIFE_DURATION_MAXIMUM
1721 , "ipseclifetime", &msg);
1722
1723 if (msg.dpd_action == DPD_ACTION_UNKNOWN)
1724 diag("dpdaction must be \"none\", \"clear\", \"hold\" or \"restart\"");
1725
1726 if (msg.dpd_action != DPD_ACTION_NONE)
1727 {
1728 if (msg.dpd_delay <= 0)
1729 diag("dpddelay must be larger than zero");
1730
1731 if (msg.dpd_timeout <= 0)
1732 diag("dpdtimeout must be larger than zero");
1733
1734 if (msg.dpd_timeout <= msg.dpd_delay)
1735 diag("dpdtimeout must be larger than dpddelay");
1736 }
1737
1738 /* pack strings for inclusion in message */
1739 next_str = msg.string;
1740 str_roof = &msg.string[sizeof(msg.string)];
1741
1742 /* build esp message as esp="<esp>;<pfsgroup>" */
1743 if (msg.pfsgroup) {
1744 snprintf(esp_buf, sizeof (esp_buf), "%s;%s",
1745 msg.esp ? msg.esp : "",
1746 msg.pfsgroup ? msg.pfsgroup : "");
1747 msg.esp=esp_buf;
1748 }
1749 if (!pack_str(&msg.name) /* string 1 */
1750 || !pack_str(&msg.left.id) /* string 2 */
1751 || !pack_str(&msg.left.cert) /* string 3 */
1752 || !pack_str(&msg.left.ca) /* string 4 */
1753 || !pack_str(&msg.left.groups) /* string 5 */
1754 || !pack_str(&msg.left.updown) /* string 6 */
1755 || !pack_str(&msg.left.virt) /* string 7 */
1756 || !pack_str(&msg.right.id) /* string 8 */
1757 || !pack_str(&msg.right.cert) /* string 9 */
1758 || !pack_str(&msg.right.ca) /* string 10 */
1759 || !pack_str(&msg.right.groups) /* string 11 */
1760 || !pack_str(&msg.right.updown) /* string 12 */
1761 || !pack_str(&msg.right.virt) /* string 13 */
1762 || !pack_str(&msg.keyid) /* string 14 */
1763 || !pack_str(&msg.myid) /* string 15 */
1764 || !pack_str(&msg.cacert) /* string 16 */
1765 || !pack_str(&msg.ldaphost) /* string 17 */
1766 || !pack_str(&msg.ldapbase) /* string 18 */
1767 || !pack_str(&msg.crluri) /* string 19 */
1768 || !pack_str(&msg.crluri2) /* string 20 */
1769 || !pack_str(&msg.ocspuri) /* string 21 */
1770 || !pack_str(&msg.ike) /* string 22 */
1771 || !pack_str(&msg.esp) /* string 23 */
1772 || !pack_str(&msg.sc_data) /* string 24 */
1773 || str_roof - next_str < (ptrdiff_t)msg.keyval.len) /* chunk (sort of string 5) */
1774 diag("too many bytes of strings to fit in message to pluto");
1775
1776 memcpy(next_str, msg.keyval.ptr, msg.keyval.len);
1777 msg.keyval.ptr = NULL;
1778 next_str += msg.keyval.len;
1779
1780 msg.magic = ((opts_seen & ~LELEM(OPT_SHUTDOWN))
1781 | sc_seen | lst_seen | cd_seen | ca_seen) != LEMPTY
1782 || msg.whack_options
1783 ? WHACK_MAGIC : WHACK_BASIC_MAGIC;
1784
1785 /* send message to Pluto */
1786 if (access(ctl_addr.sun_path, R_OK | W_OK) < 0)
1787 {
1788 int e = errno;
1789
1790 switch (e)
1791 {
1792 case EACCES:
1793 fprintf(stderr, "whack: no right to communicate with pluto (access(\"%s\"))\n"
1794 , ctl_addr.sun_path);
1795 break;
1796 case ENOENT:
1797 fprintf(stderr, "whack: Pluto is not running (no \"%s\")\n"
1798 , ctl_addr.sun_path);
1799 break;
1800 default:
1801 fprintf(stderr, "whack: access(\"%s\") failed with %d %s\n"
1802 , ctl_addr.sun_path, errno, strerror(e));
1803 break;
1804 }
1805 whack_exit(RC_WHACK_PROBLEM);
1806 }
1807 else
1808 {
1809 int sock = socket(AF_UNIX, SOCK_STREAM, 0);
1810 int exit_status = 0;
1811 ssize_t len = next_str - (char *)&msg;
1812
1813 if (sock == -1)
1814 {
1815 int e = errno;
1816
1817 fprintf(stderr, "whack: socket() failed (%d %s)\n", e, strerror(e));
1818 whack_exit(RC_WHACK_PROBLEM);
1819 }
1820
1821 if (connect(sock, (struct sockaddr *)&ctl_addr
1822 , offsetof(struct sockaddr_un, sun_path) + strlen(ctl_addr.sun_path)) < 0)
1823 {
1824 int e = errno;
1825
1826 fprintf(stderr, "whack:%s connect() for \"%s\" failed (%d %s)\n"
1827 , e == ECONNREFUSED? " is Pluto running? " : ""
1828 , ctl_addr.sun_path, e, strerror(e));
1829 whack_exit(RC_WHACK_PROBLEM);
1830 }
1831
1832 if (write(sock, &msg, len) != len)
1833 {
1834 int e = errno;
1835
1836 fprintf(stderr, "whack: write() failed (%d %s)\n", e, strerror(e));
1837 whack_exit(RC_WHACK_PROBLEM);
1838 }
1839
1840 /* for now, just copy reply back to stdout */
1841
1842 {
1843 char buf[4097]; /* arbitrary limit on log line length */
1844 char *be = buf;
1845
1846 for (;;)
1847 {
1848 char *ls = buf;
1849 ssize_t rl = read(sock, be, (buf + sizeof(buf)-1) - be);
1850
1851 if (rl < 0)
1852 {
1853 int e = errno;
1854
1855 fprintf(stderr, "whack: read() failed (%d %s)\n", e, strerror(e));
1856 whack_exit(RC_WHACK_PROBLEM);
1857 }
1858 if (rl == 0)
1859 {
1860 if (be != buf)
1861 fprintf(stderr, "whack: last line from pluto too long or unterminated\n");
1862 break;
1863 }
1864
1865 be += rl;
1866 *be = '\0';
1867
1868 for (;;)
1869 {
1870 char *le = strchr(ls, '\n');
1871
1872 if (le == NULL)
1873 {
1874 /* move last, partial line to start of buffer */
1875 memmove(buf, ls, be-ls);
1876 be -= ls - buf;
1877 break;
1878 }
1879
1880 le++; /* include NL in line */
1881 ignore_result(write(1, ls, le - ls));
1882
1883 /* figure out prefix number
1884 * and how it should affect our exit status
1885 */
1886 {
1887 unsigned long s = strtoul(ls, NULL, 10);
1888
1889 switch (s)
1890 {
1891 case RC_COMMENT:
1892 case RC_LOG:
1893 /* ignore */
1894 break;
1895 case RC_SUCCESS:
1896 /* be happy */
1897 exit_status = 0;
1898 break;
1899 case RC_ENTERSECRET:
1900 get_secret(sock);
1901 break;
1902 /* case RC_LOG_SERIOUS: */
1903 default:
1904 /* pass through */
1905 exit_status = s;
1906 break;
1907 }
1908 }
1909 ls = le;
1910 }
1911 }
1912 }
1913 whack_exit(exit_status);
1914 }
1915 return -1; /* should never be reached */
1916 }