1 /* Stroke for charon is the counterpart to whack from pluto
2 * Copyright (C) 2007 Tobias Brunner
3 * Copyright (C) 2006 Martin Willi
4 * Hochschule fuer Technik Rapperswil
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 #include <sys/types.h>
22 #include <sys/socket.h>
24 #include <sys/fcntl.h>
31 #include "stroke_msg.h"
32 #include "stroke_keywords.h"
39 static char* push_string(stroke_msg_t
*msg
, char *string
)
41 unsigned long string_start
= msg
->length
;
43 if (string
== NULL
|| msg
->length
+ strlen(string
) >= sizeof(stroke_msg_t
))
49 msg
->length
+= strlen(string
) + 1;
50 strcpy((char*)msg
+ string_start
, string
);
51 return (char*)string_start
;
55 static int send_stroke_msg (stroke_msg_t
*msg
)
57 struct sockaddr_un ctl_addr
= { AF_UNIX
, STROKE_SOCKET
};
62 msg
->output_verbosity
= 1; /* CONTROL */
64 sock
= socket(AF_UNIX
, SOCK_STREAM
, 0);
67 fprintf(stderr
, "Opening unix socket %s: %s\n", STROKE_SOCKET
, strerror(errno
));
70 if (connect(sock
, (struct sockaddr
*)&ctl_addr
,
71 offsetof(struct sockaddr_un
, sun_path
) + strlen(ctl_addr
.sun_path
)) < 0)
73 fprintf(stderr
, "Connect to socket failed: %s\n", strerror(errno
));
79 if (write(sock
, msg
, msg
->length
) != msg
->length
)
81 fprintf(stderr
, "writing to socket failed: %s\n", strerror(errno
));
86 while ((byte_count
= read(sock
, buffer
, sizeof(buffer
)-1)) > 0)
88 buffer
[byte_count
] = '\0';
93 fprintf(stderr
, "reading from socket failed: %s\n", strerror(errno
));
100 static int add_connection(char *name
,
101 char *my_id
, char *other_id
,
102 char *my_addr
, char *other_addr
,
103 char *my_nets
, char *other_nets
)
107 memset(&msg
, 0, sizeof(msg
));
108 msg
.length
= offsetof(stroke_msg_t
, buffer
);
109 msg
.type
= STR_ADD_CONN
;
111 msg
.add_conn
.name
= push_string(&msg
, name
);
112 msg
.add_conn
.ikev2
= 1;
113 msg
.add_conn
.auth_method
= 2;
114 msg
.add_conn
.mode
= 1;
115 msg
.add_conn
.mobike
= 1;
116 msg
.add_conn
.dpd
.action
= 1;
118 msg
.add_conn
.me
.id
= push_string(&msg
, my_id
);
119 msg
.add_conn
.me
.address
= push_string(&msg
, my_addr
);
120 msg
.add_conn
.me
.subnets
= push_string(&msg
, my_nets
);
121 msg
.add_conn
.me
.sendcert
= 1;
123 msg
.add_conn
.other
.id
= push_string(&msg
, other_id
);
124 msg
.add_conn
.other
.address
= push_string(&msg
, other_addr
);
125 msg
.add_conn
.other
.subnets
= push_string(&msg
, other_nets
);
126 msg
.add_conn
.other
.sendcert
= 1;
128 return send_stroke_msg(&msg
);
131 static int del_connection(char *name
)
135 msg
.length
= offsetof(stroke_msg_t
, buffer
);
136 msg
.type
= STR_DEL_CONN
;
137 msg
.initiate
.name
= push_string(&msg
, name
);
138 return send_stroke_msg(&msg
);
141 static int initiate_connection(char *name
)
145 msg
.length
= offsetof(stroke_msg_t
, buffer
);
146 msg
.type
= STR_INITIATE
;
147 msg
.initiate
.name
= push_string(&msg
, name
);
148 return send_stroke_msg(&msg
);
151 static int terminate_connection(char *name
)
155 msg
.type
= STR_TERMINATE
;
156 msg
.length
= offsetof(stroke_msg_t
, buffer
);
157 msg
.initiate
.name
= push_string(&msg
, name
);
158 return send_stroke_msg(&msg
);
161 static int route_connection(char *name
)
165 msg
.type
= STR_ROUTE
;
166 msg
.length
= offsetof(stroke_msg_t
, buffer
);
167 msg
.route
.name
= push_string(&msg
, name
);
168 return send_stroke_msg(&msg
);
171 static int unroute_connection(char *name
)
175 msg
.type
= STR_UNROUTE
;
176 msg
.length
= offsetof(stroke_msg_t
, buffer
);
177 msg
.unroute
.name
= push_string(&msg
, name
);
178 return send_stroke_msg(&msg
);
181 static int show_status(stroke_keyword_t kw
, char *connection
)
185 msg
.type
= (kw
== STROKE_STATUS
)? STR_STATUS
:STR_STATUS_ALL
;
186 msg
.length
= offsetof(stroke_msg_t
, buffer
);
187 msg
.status
.name
= push_string(&msg
, connection
);
188 return send_stroke_msg(&msg
);
191 static int list_flags
[] = {
204 static int list(stroke_keyword_t kw
, int utc
)
209 msg
.length
= offsetof(stroke_msg_t
, buffer
);
211 msg
.list
.flags
= list_flags
[kw
- STROKE_LIST_FIRST
];
212 return send_stroke_msg(&msg
);
215 static int reread_flags
[] = {
225 static int reread(stroke_keyword_t kw
)
229 msg
.type
= STR_REREAD
;
230 msg
.length
= offsetof(stroke_msg_t
, buffer
);
231 msg
.reread
.flags
= reread_flags
[kw
- STROKE_REREAD_FIRST
];
232 return send_stroke_msg(&msg
);
235 static int purge_flags
[] = {
239 static int purge(stroke_keyword_t kw
)
243 msg
.type
= STR_PURGE
;
244 msg
.length
= offsetof(stroke_msg_t
, buffer
);
245 msg
.purge
.flags
= purge_flags
[kw
- STROKE_PURGE_FIRST
];
246 return send_stroke_msg(&msg
);
249 static int set_loglevel(char *type
, u_int level
)
253 msg
.type
= STR_LOGLEVEL
;
254 msg
.length
= offsetof(stroke_msg_t
, buffer
);
255 msg
.loglevel
.type
= push_string(&msg
, type
);
256 msg
.loglevel
.level
= level
;
257 return send_stroke_msg(&msg
);
260 static void exit_error(char *error
)
264 fprintf(stderr
, "%s\n", error
);
269 static void exit_usage(char *error
)
272 printf(" Add a connection:\n");
273 printf(" stroke add NAME MY_ID OTHER_ID MY_ADDR OTHER_ADDR\\\n");
274 printf(" MY_NET OTHER_NET MY_NETBITS OTHER_NETBITS\n");
275 printf(" where: ID is any IKEv2 ID \n");
276 printf(" ADDR is a IPv4 address\n");
277 printf(" NET is a IPv4 subnet in CIDR notation\n");
278 printf(" Delete a connection:\n");
279 printf(" stroke delete NAME\n");
280 printf(" where: NAME is a connection name added with \"stroke add\"\n");
281 printf(" Initiate a connection:\n");
282 printf(" stroke up NAME\n");
283 printf(" where: NAME is a connection name added with \"stroke add\"\n");
284 printf(" Terminate a connection:\n");
285 printf(" stroke down NAME\n");
286 printf(" where: NAME is a connection name added with \"stroke add\"\n");
287 printf(" Set loglevel for a logging type:\n");
288 printf(" stroke loglevel TYPE LEVEL\n");
289 printf(" where: TYPE is any|dmn|mgr|ike|chd|job|cfg|knl|net|enc|lib\n");
290 printf(" LEVEL is -1|0|1|2|3|4\n");
291 printf(" Show connection status:\n");
292 printf(" stroke status\n");
293 printf(" Show list of authority and attribute certificates:\n");
294 printf(" stroke listcacerts|listocspcerts|listaacerts|listacerts\n");
295 printf(" Show list of end entity certificates, ca info records and crls:\n");
296 printf(" stroke listcerts|listcainfos|listcrls|listall\n");
297 printf(" Reload authority and attribute certificates:\n");
298 printf(" stroke rereadcacerts|rereadocspcerts|rereadaacerts|rereadacerts\n");
299 printf(" Reload secrets and crls:\n");
300 printf(" stroke rereadsecrets|rereadcrls|rereadall\n");
301 printf(" Purge ocsp cache entries:\n");
302 printf(" stroke purgeocsp\n");
306 int main(int argc
, char *argv
[])
308 const stroke_token_t
*token
;
316 token
= in_word_set(argv
[1], strlen(argv
[1]));
320 exit_usage("unknown keyword");
328 exit_usage("\"add\" needs more parameters...");
330 res
= add_connection(argv
[2],
339 exit_usage("\"delete\" needs a connection name");
341 res
= del_connection(argv
[2]);
346 exit_usage("\"up\" needs a connection name");
348 res
= initiate_connection(argv
[2]);
353 exit_usage("\"down\" needs a connection name");
355 res
= terminate_connection(argv
[2]);
360 exit_usage("\"route\" needs a connection name");
362 res
= route_connection(argv
[2]);
367 exit_usage("\"unroute\" needs a connection name");
369 res
= unroute_connection(argv
[2]);
371 case STROKE_LOGLEVEL
:
374 exit_usage("\"logtype\" needs more parameters...");
376 res
= set_loglevel(argv
[2], atoi(argv
[3]));
379 case STROKE_STATUSALL
:
380 res
= show_status(token
->kw
, argc
> 2 ? argv
[2] : NULL
);
382 case STROKE_LIST_CERTS
:
383 case STROKE_LIST_CACERTS
:
384 case STROKE_LIST_OCSPCERTS
:
385 case STROKE_LIST_AACERTS
:
386 case STROKE_LIST_ACERTS
:
387 case STROKE_LIST_CAINFOS
:
388 case STROKE_LIST_CRLS
:
389 case STROKE_LIST_OCSP
:
390 case STROKE_LIST_ALL
:
391 res
= list(token
->kw
, argc
> 2 && strcmp(argv
[2], "--utc") == 0);
393 case STROKE_REREAD_SECRETS
:
394 case STROKE_REREAD_CACERTS
:
395 case STROKE_REREAD_OCSPCERTS
:
396 case STROKE_REREAD_AACERTS
:
397 case STROKE_REREAD_ACERTS
:
398 case STROKE_REREAD_CRLS
:
399 case STROKE_REREAD_ALL
:
400 res
= reread(token
->kw
);
402 case STROKE_PURGE_OCSP
:
403 res
= purge(token
->kw
);