stroke now uses constant size string buffer
[strongswan.git] / src / starter / starterstroke.c
1 /* Stroke for charon is the counterpart to whack from pluto
2 * Copyright (C) 2006 Martin Willi - Hochschule fuer Technik Rapperswil
3 *
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>.
8 *
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
12 * for more details.
13 *
14 * RCSID $Id: starterstroke.c $
15 */
16
17 #include <sys/types.h>
18 #include <sys/socket.h>
19 #include <sys/un.h>
20 #include <linux/stddef.h>
21 #include <unistd.h>
22 #include <stdlib.h>
23 #include <errno.h>
24 #include <netinet/in.h>
25 #include <arpa/inet.h>
26
27 #include <freeswan.h>
28
29 #include <constants.h>
30 #include <defs.h>
31 #include <log.h>
32
33 #include <stroke.h>
34
35 #include "starterstroke.h"
36 #include "confread.h"
37 #include "files.h"
38
39 static char* push_string(stroke_msg_t *msg, char *string)
40 {
41 u_int string_start = msg->length;
42
43 if (string == NULL || msg->length + strlen(string) >= sizeof(stroke_msg_t))
44 {
45 return NULL;
46 }
47 else
48 {
49 msg->length += strlen(string) + 1;
50 strcpy((char*)msg + string_start, string);
51 return (char*)string_start;
52 }
53 }
54
55 static int send_stroke_msg (stroke_msg_t *msg)
56 {
57 struct sockaddr_un ctl_addr = { AF_UNIX, CHARON_CTL_FILE };
58 int byte_count;
59 char buffer[64];
60
61 int sock = socket(AF_UNIX, SOCK_STREAM, 0);
62
63 if (sock < 0)
64 {
65 plog("socket() failed: %s", strerror(errno));
66 return -1;
67 }
68 if (connect(sock, (struct sockaddr *)&ctl_addr, offsetof(struct sockaddr_un, sun_path) + strlen(ctl_addr.sun_path)) < 0)
69 {
70 plog("connect(charon_ctl) failed: %s", strerror(errno));
71 close(sock);
72 return -1;
73 }
74
75 /* send message */
76 if (write(sock, msg, msg->length) != msg->length)
77 {
78 plog("write(charon_ctl) failed: %s", strerror(errno));
79 close(sock);
80 return -1;
81 }
82 while ((byte_count = read(sock, buffer, sizeof(buffer)-1)) > 0)
83 {
84 buffer[byte_count] = '\0';
85 plog("%s", buffer);
86 }
87 if (byte_count < 0)
88 {
89 plog("read() failed: %s", strerror(errno));
90 }
91
92 close(sock);
93 return 0;
94 }
95
96 static char* connection_name(starter_conn_t *conn)
97 {
98 /* if connection name is '%auto', create a new name like conn_xxxxx */
99 static char buf[32];
100
101 if (streq(conn->name, "%auto"))
102 {
103 sprintf(buf, "conn_%ld", conn->id);
104 return buf;
105 }
106 return conn->name;
107 }
108
109 int starter_stroke_add_conn(starter_conn_t *conn)
110 {
111 stroke_msg_t msg;
112
113 msg.type = STR_ADD_CONN;
114 msg.length = offsetof(stroke_msg_t, buffer);
115 msg.add_conn.ikev2 = conn->keyexchange == KEY_EXCHANGE_IKEV2;
116 msg.add_conn.name = push_string(&msg, connection_name(conn));
117
118 msg.add_conn.me.id = push_string(&msg, conn->left.id);
119 msg.add_conn.me.cert = push_string(&msg, conn->left.cert);
120 msg.add_conn.me.address = push_string(&msg, inet_ntoa(conn->left.addr.u.v4.sin_addr));
121 msg.add_conn.me.subnet = push_string(&msg, inet_ntoa(conn->left.subnet.addr.u.v4.sin_addr));
122 msg.add_conn.me.subnet_mask = conn->left.subnet.maskbits;
123
124 msg.add_conn.other.id = push_string(&msg, conn->right.id);
125 msg.add_conn.other.cert = push_string(&msg, conn->right.cert);
126 msg.add_conn.other.address = push_string(&msg, inet_ntoa(conn->right.addr.u.v4.sin_addr));
127 msg.add_conn.other.subnet = push_string(&msg, inet_ntoa(conn->right.subnet.addr.u.v4.sin_addr));
128 msg.add_conn.other.subnet_mask = conn->right.subnet.maskbits;
129
130 return send_stroke_msg(&msg);
131 }
132
133 int starter_stroke_del_conn(starter_conn_t *conn)
134 {
135 return 0;
136 }
137
138 int starter_stroke_route_conn(starter_conn_t *conn)
139 {
140 stroke_msg_t msg;
141
142 msg.type = STR_INSTALL;
143 msg.length = offsetof(stroke_msg_t, buffer);
144 msg.install.name = push_string(&msg, connection_name(conn));
145 return send_stroke_msg(&msg);
146 }
147
148 int starter_stroke_initiate_conn(starter_conn_t *conn)
149 {
150 stroke_msg_t msg;
151
152 msg.type = STR_INITIATE;
153 msg.length = offsetof(stroke_msg_t, buffer);
154 msg.initiate.name = push_string(&msg, connection_name(conn));
155 return send_stroke_msg(&msg);
156 }