1 /* Stroke for charon is the counterpart to whack from pluto
2 * Copyright (C) 2006 Martin Willi - Hochschule fuer Technik Rapperswil
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 #include <sys/types.h>
18 #include <sys/socket.h>
20 #include <sys/fcntl.h>
28 #include "stroke_keywords.h"
35 static char* push_string(stroke_msg_t
*msg
, char *string
)
37 unsigned long string_start
= msg
->length
;
39 if (string
== NULL
|| msg
->length
+ strlen(string
) >= sizeof(stroke_msg_t
))
45 msg
->length
+= strlen(string
) + 1;
46 strcpy((char*)msg
+ string_start
, string
);
47 return (char*)string_start
;
51 static int send_stroke_msg (stroke_msg_t
*msg
)
53 struct sockaddr_un ctl_addr
= { AF_UNIX
, STROKE_SOCKET
};
58 msg
->output_verbosity
= 1; /* CONTROL */
60 sock
= socket(AF_UNIX
, SOCK_STREAM
, 0);
63 fprintf(stderr
, "Opening unix socket %s: %s\n", STROKE_SOCKET
, strerror(errno
));
66 if (connect(sock
, (struct sockaddr
*)&ctl_addr
,
67 offsetof(struct sockaddr_un
, sun_path
) + strlen(ctl_addr
.sun_path
)) < 0)
69 fprintf(stderr
, "Connect to socket failed: %s\n", strerror(errno
));
75 if (write(sock
, msg
, msg
->length
) != msg
->length
)
77 fprintf(stderr
, "writing to socket failed: %s\n", strerror(errno
));
82 while ((byte_count
= read(sock
, buffer
, sizeof(buffer
)-1)) > 0)
84 buffer
[byte_count
] = '\0';
89 fprintf(stderr
, "reading from socket failed: %s\n", strerror(errno
));
96 static int add_connection(char *name
,
97 char *my_id
, char *other_id
,
98 char *my_addr
, char *other_addr
,
99 char *my_net
, char *other_net
,
100 u_int my_netmask
, u_int other_netmask
)
104 msg
.length
= offsetof(stroke_msg_t
, buffer
);
105 msg
.type
= STR_ADD_CONN
;
107 msg
.add_conn
.name
= push_string(&msg
, name
);
108 msg
.add_conn
.ikev2
= 1;
110 msg
.add_conn
.rekey
.reauth
= 0;
111 msg
.add_conn
.rekey
.ipsec_lifetime
= 0;
112 msg
.add_conn
.rekey
.ike_lifetime
= 0;
113 msg
.add_conn
.rekey
.margin
= 0;
114 msg
.add_conn
.rekey
.tries
= 0;
115 msg
.add_conn
.rekey
.fuzz
= 0;
117 msg
.add_conn
.algorithms
.ike
= NULL
;
118 msg
.add_conn
.algorithms
.esp
= NULL
;
120 msg
.add_conn
.dpd
.delay
= 0;
121 msg
.add_conn
.dpd
.action
= 1;
123 msg
.add_conn
.me
.id
= push_string(&msg
, my_id
);
124 msg
.add_conn
.me
.address
= push_string(&msg
, my_addr
);
125 msg
.add_conn
.me
.subnet
= push_string(&msg
, my_net
);
126 msg
.add_conn
.me
.subnet_mask
= my_netmask
;
127 msg
.add_conn
.me
.cert
= NULL
;
128 msg
.add_conn
.me
.ca
= NULL
;
129 msg
.add_conn
.me
.sendcert
= 1;
130 msg
.add_conn
.me
.hostaccess
= 0;
131 msg
.add_conn
.me
.protocol
= 0;
132 msg
.add_conn
.me
.port
= 0;
134 msg
.add_conn
.other
.id
= push_string(&msg
, other_id
);
135 msg
.add_conn
.other
.address
= push_string(&msg
, other_addr
);
136 msg
.add_conn
.other
.subnet
= push_string(&msg
, other_net
);
137 msg
.add_conn
.other
.subnet_mask
= other_netmask
;
138 msg
.add_conn
.other
.cert
= NULL
;
139 msg
.add_conn
.other
.ca
= NULL
;
140 msg
.add_conn
.other
.sendcert
= 1;
141 msg
.add_conn
.other
.hostaccess
= 0;
142 msg
.add_conn
.other
.protocol
= 0;
143 msg
.add_conn
.other
.port
= 0;
145 return send_stroke_msg(&msg
);
148 static int del_connection(char *name
)
152 msg
.length
= offsetof(stroke_msg_t
, buffer
);
153 msg
.type
= STR_DEL_CONN
;
154 msg
.initiate
.name
= push_string(&msg
, name
);
155 return send_stroke_msg(&msg
);
158 static int initiate_connection(char *name
)
162 msg
.length
= offsetof(stroke_msg_t
, buffer
);
163 msg
.type
= STR_INITIATE
;
164 msg
.initiate
.name
= push_string(&msg
, name
);
165 return send_stroke_msg(&msg
);
168 static int terminate_connection(char *name
)
172 msg
.type
= STR_TERMINATE
;
173 msg
.length
= offsetof(stroke_msg_t
, buffer
);
174 msg
.initiate
.name
= push_string(&msg
, name
);
175 return send_stroke_msg(&msg
);
178 static int route_connection(char *name
)
182 msg
.type
= STR_ROUTE
;
183 msg
.length
= offsetof(stroke_msg_t
, buffer
);
184 msg
.route
.name
= push_string(&msg
, name
);
185 return send_stroke_msg(&msg
);
188 static int unroute_connection(char *name
)
192 msg
.type
= STR_UNROUTE
;
193 msg
.length
= offsetof(stroke_msg_t
, buffer
);
194 msg
.unroute
.name
= push_string(&msg
, name
);
195 return send_stroke_msg(&msg
);
198 static int show_status(stroke_keyword_t kw
, char *connection
)
202 msg
.type
= (kw
== STROKE_STATUS
)? STR_STATUS
:STR_STATUS_ALL
;
203 msg
.length
= offsetof(stroke_msg_t
, buffer
);
204 msg
.status
.name
= push_string(&msg
, connection
);
205 return send_stroke_msg(&msg
);
208 static int list_flags
[] = {
215 static int list(stroke_keyword_t kw
, int utc
)
220 msg
.length
= offsetof(stroke_msg_t
, buffer
);
222 msg
.list
.flags
= list_flags
[kw
- STROKE_LIST_FIRST
];
223 return send_stroke_msg(&msg
);
226 static int reread_flags
[] = {
232 static int reread(stroke_keyword_t kw
)
236 msg
.type
= STR_REREAD
;
237 msg
.length
= offsetof(stroke_msg_t
, buffer
);
238 msg
.reread
.flags
= reread_flags
[kw
- STROKE_REREAD_FIRST
];
239 return send_stroke_msg(&msg
);
242 static int set_loglevel(char *type
, u_int level
)
246 msg
.type
= STR_LOGLEVEL
;
247 msg
.length
= offsetof(stroke_msg_t
, buffer
);
248 msg
.loglevel
.type
= push_string(&msg
, type
);
249 msg
.loglevel
.level
= level
;
250 return send_stroke_msg(&msg
);
253 static void exit_error(char *error
)
257 fprintf(stderr
, "%s\n", error
);
262 static void exit_usage(char *error
)
265 printf(" Add a connection:\n");
266 printf(" stroke add NAME MY_ID OTHER_ID MY_ADDR OTHER_ADDR\\\n");
267 printf(" MY_NET OTHER_NET MY_NETBITS OTHER_NETBITS\n");
268 printf(" where: ID is any IKEv2 ID \n");
269 printf(" ADDR is a IPv4 address\n");
270 printf(" NET is a IPv4 address of the subnet to tunnel\n");
271 printf(" NETBITS is the size of the subnet, as the \"24\" in 192.168.0.0/24\n");
272 printf(" Delete a connection:\n");
273 printf(" stroke delete NAME\n");
274 printf(" where: NAME is a connection name added with \"stroke add\"\n");
275 printf(" Initiate a connection:\n");
276 printf(" stroke up NAME\n");
277 printf(" where: NAME is a connection name added with \"stroke add\"\n");
278 printf(" Terminate a connection:\n");
279 printf(" stroke down NAME\n");
280 printf(" where: NAME is a connection name added with \"stroke add\"\n");
281 printf(" Set loglevel for a logging type:\n");
282 printf(" stroke loglevel TYPE LEVEL\n");
283 printf(" where: TYPE is any|dmn|mgr|ike|chd|job|cfg|knl|net|enc|lib\n");
284 printf(" LEVEL is -1|0|1|2|3|4\n");
285 printf(" Show connection status:\n");
286 printf(" stroke status\n");
287 printf(" Show list of locally loaded certificates and crls:\n");
288 printf(" stroke listcerts|listcacerts|listcrls|listall\n");
289 printf(" Reload ca certificates and crls:\n");
290 printf(" stroke rereadcacerts|rereadcrls|rereadall\n");
294 int main(int argc
, char *argv
[])
296 const stroke_token_t
*token
;
304 token
= in_word_set(argv
[1], strlen(argv
[1]));
308 exit_usage("unknown keyword");
316 exit_usage("\"add\" needs more parameters...");
318 res
= add_connection(argv
[2],
322 atoi(argv
[9]), atoi(argv
[10]));
328 exit_usage("\"delete\" needs a connection name");
330 res
= del_connection(argv
[2]);
335 exit_usage("\"up\" needs a connection name");
337 res
= initiate_connection(argv
[2]);
342 exit_usage("\"down\" needs a connection name");
344 res
= terminate_connection(argv
[2]);
349 exit_usage("\"route\" needs a connection name");
351 res
= route_connection(argv
[2]);
356 exit_usage("\"unroute\" needs a connection name");
358 res
= unroute_connection(argv
[2]);
360 case STROKE_LOGLEVEL
:
363 exit_usage("\"logtype\" needs more parameters...");
365 res
= set_loglevel(argv
[2], atoi(argv
[3]));
368 case STROKE_STATUSALL
:
369 res
= show_status(token
->kw
, argc
> 2 ? argv
[2] : NULL
);
371 case STROKE_LIST_CERTS
:
372 case STROKE_LIST_CACERTS
:
373 case STROKE_LIST_CRLS
:
374 case STROKE_LIST_ALL
:
375 res
= list(token
->kw
, argc
> 2 && strcmp(argv
[2], "--utc") == 0);
377 case STROKE_REREAD_CACERTS
:
378 case STROKE_REREAD_CRLS
:
379 case STROKE_REREAD_ALL
:
380 res
= reread(token
->kw
);