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