starterstroke.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. /*
  2. * Copyright (C) 2007-2015 Tobias Brunner
  3. * Copyright (C) 2006 Martin Willi
  4. * HSR Hochschule fuer Technik Rapperswil
  5. *
  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>.
  10. *
  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
  14. * for more details.
  15. */
  16. #include <unistd.h>
  17. #include <stdlib.h>
  18. #include <stdint.h>
  19. #include <string.h>
  20. #include <credentials/auth_cfg.h>
  21. #include <library.h>
  22. #include <utils/debug.h>
  23. #include <stroke_msg.h>
  24. #include "starterstroke.h"
  25. #include "confread.h"
  26. #include "files.h"
  27. #define IPV4_LEN 4
  28. #define IPV6_LEN 16
  29. static stroke_msg_t *create_stroke_msg(int type)
  30. {
  31. stroke_msg_t *msg;
  32. INIT(msg,
  33. .type = type,
  34. .length = offsetof(stroke_msg_t, buffer),
  35. );
  36. return msg;
  37. }
  38. #define push_string(msg, field, str) \
  39. push_string_impl(msg, offsetof(stroke_msg_t, field), str)
  40. #define push_string_end(msg, offset, field, str) \
  41. push_string_impl(msg, offset + offsetof(stroke_end_t, field), str)
  42. static void push_string_impl(stroke_msg_t **msg, size_t offset, char *string)
  43. {
  44. size_t cur_len = (*msg)->length, str_len;
  45. if (!string)
  46. {
  47. return;
  48. }
  49. str_len = strlen(string) + 1;
  50. if (cur_len + str_len >= UINT16_MAX)
  51. {
  52. (*msg)->length = UINT16_MAX;
  53. return;
  54. }
  55. while (cur_len + str_len > sizeof(stroke_msg_t) + (*msg)->buflen)
  56. {
  57. *msg = realloc(*msg, sizeof(stroke_msg_t) + (*msg)->buflen +
  58. STROKE_BUF_LEN_INC);
  59. (*msg)->buflen += STROKE_BUF_LEN_INC;
  60. }
  61. (*msg)->length += str_len;
  62. strcpy((char*)*msg + cur_len, string);
  63. *(char**)((char*)*msg + offset) = (char*)cur_len;
  64. }
  65. static int send_stroke_msg(stroke_msg_t *msg)
  66. {
  67. stream_t *stream;
  68. char *uri, buffer[64];
  69. int count;
  70. if (msg->length == UINT16_MAX)
  71. {
  72. DBG1(DBG_APP, "stroke message exceeds maximum buffer size");
  73. free(msg);
  74. return -1;
  75. }
  76. /* starter is not called from commandline, and therefore absolutely silent */
  77. msg->output_verbosity = -1;
  78. uri = lib->settings->get_str(lib->settings, "%s.plugins.stroke.socket",
  79. "unix://" CHARON_CTL_FILE, daemon_name);
  80. stream = lib->streams->connect(lib->streams, uri);
  81. if (!stream)
  82. {
  83. DBG1(DBG_APP, "failed to connect to stroke socket '%s'", uri);
  84. free(msg);
  85. return -1;
  86. }
  87. if (!stream->write_all(stream, msg, msg->length))
  88. {
  89. DBG1(DBG_APP, "sending stroke message failed");
  90. stream->destroy(stream);
  91. free(msg);
  92. return -1;
  93. }
  94. while ((count = stream->read(stream, buffer, sizeof(buffer)-1, TRUE)) > 0)
  95. {
  96. buffer[count] = '\0';
  97. DBG1(DBG_APP, "%s", buffer);
  98. }
  99. if (count < 0)
  100. {
  101. DBG1(DBG_APP, "reading stroke response failed");
  102. }
  103. stream->destroy(stream);
  104. free(msg);
  105. return 0;
  106. }
  107. static char* connection_name(starter_conn_t *conn)
  108. {
  109. /* if connection name is '%auto', create a new name like conn_xxxxx */
  110. static char buf[32];
  111. if (streq(conn->name, "%auto"))
  112. {
  113. sprintf(buf, "conn_%lu", conn->id);
  114. return buf;
  115. }
  116. return conn->name;
  117. }
  118. static void add_end(stroke_msg_t **msg, size_t offset, starter_end_t *conn_end)
  119. {
  120. stroke_end_t *msg_end;
  121. push_string_end(msg, offset, auth, conn_end->auth);
  122. push_string_end(msg, offset, auth2, conn_end->auth2);
  123. push_string_end(msg, offset, id, conn_end->id);
  124. push_string_end(msg, offset, id2, conn_end->id2);
  125. push_string_end(msg, offset, rsakey, conn_end->rsakey);
  126. push_string_end(msg, offset, cert, conn_end->cert);
  127. push_string_end(msg, offset, cert2, conn_end->cert2);
  128. push_string_end(msg, offset, cert_policy, conn_end->cert_policy);
  129. push_string_end(msg, offset, ca, conn_end->ca);
  130. push_string_end(msg, offset, ca2, conn_end->ca2);
  131. push_string_end(msg, offset, groups, conn_end->groups);
  132. push_string_end(msg, offset, groups2, conn_end->groups2);
  133. push_string_end(msg, offset, updown, conn_end->updown);
  134. if (conn_end->host)
  135. {
  136. push_string_end(msg, offset, address, conn_end->host);
  137. }
  138. else
  139. {
  140. push_string_end(msg, offset, address, "%any");
  141. }
  142. push_string_end(msg, offset, subnets, conn_end->subnet);
  143. push_string_end(msg, offset, sourceip, conn_end->sourceip);
  144. push_string_end(msg, offset, dns, conn_end->dns);
  145. /* we can't assign it earlier as msg might change */
  146. msg_end = (stroke_end_t*)((char*)(*msg) + offset);
  147. msg_end->ikeport = conn_end->ikeport;
  148. msg_end->sendcert = conn_end->sendcert;
  149. msg_end->hostaccess = conn_end->hostaccess;
  150. msg_end->tohost = !conn_end->subnet;
  151. msg_end->allow_any = conn_end->allow_any;
  152. msg_end->protocol = conn_end->protocol;
  153. msg_end->from_port = conn_end->from_port;
  154. msg_end->to_port = conn_end->to_port;
  155. }
  156. int starter_stroke_add_conn(starter_config_t *cfg, starter_conn_t *conn)
  157. {
  158. stroke_msg_t *msg;
  159. msg = create_stroke_msg(STR_ADD_CONN);
  160. msg->add_conn.version = conn->keyexchange;
  161. push_string(&msg, add_conn.name, connection_name(conn));
  162. push_string(&msg, add_conn.eap_identity, conn->eap_identity);
  163. push_string(&msg, add_conn.aaa_identity, conn->aaa_identity);
  164. push_string(&msg, add_conn.xauth_identity, conn->xauth_identity);
  165. msg->add_conn.mode = conn->mode;
  166. msg->add_conn.proxy_mode = conn->proxy_mode;
  167. if (!(conn->options & SA_OPTION_DONT_REKEY))
  168. {
  169. msg->add_conn.rekey.reauth = !(conn->options & SA_OPTION_DONT_REAUTH);
  170. msg->add_conn.rekey.ipsec_lifetime = conn->sa_ipsec_life_seconds;
  171. msg->add_conn.rekey.ike_lifetime = conn->sa_ike_life_seconds;
  172. msg->add_conn.rekey.margin = conn->sa_rekey_margin;
  173. msg->add_conn.rekey.life_bytes = conn->sa_ipsec_life_bytes;
  174. msg->add_conn.rekey.margin_bytes = conn->sa_ipsec_margin_bytes;
  175. msg->add_conn.rekey.life_packets = conn->sa_ipsec_life_packets;
  176. msg->add_conn.rekey.margin_packets = conn->sa_ipsec_margin_packets;
  177. msg->add_conn.rekey.fuzz = conn->sa_rekey_fuzz;
  178. }
  179. msg->add_conn.rekey.tries = conn->sa_keying_tries;
  180. msg->add_conn.mobike = conn->options & SA_OPTION_MOBIKE;
  181. msg->add_conn.force_encap = conn->options & SA_OPTION_FORCE_ENCAP;
  182. msg->add_conn.fragmentation = conn->fragmentation;
  183. msg->add_conn.ikedscp = conn->ikedscp;
  184. msg->add_conn.ipcomp = conn->options & SA_OPTION_COMPRESS;
  185. msg->add_conn.install_policy = conn->install_policy;
  186. msg->add_conn.aggressive = conn->aggressive;
  187. msg->add_conn.pushmode = conn->options & SA_OPTION_MODECFG_PUSH;
  188. msg->add_conn.crl_policy = (crl_policy_t)cfg->setup.strictcrlpolicy;
  189. msg->add_conn.unique = cfg->setup.uniqueids;
  190. push_string(&msg, add_conn.algorithms.ike, conn->ike);
  191. push_string(&msg, add_conn.algorithms.esp, conn->esp);
  192. push_string(&msg, add_conn.algorithms.ah, conn->ah);
  193. msg->add_conn.dpd.delay = conn->dpd_delay;
  194. msg->add_conn.dpd.timeout = conn->dpd_timeout;
  195. msg->add_conn.dpd.action = conn->dpd_action;
  196. msg->add_conn.close_action = conn->close_action;
  197. msg->add_conn.sha256_96 = conn->sha256_96;
  198. msg->add_conn.inactivity = conn->inactivity;
  199. msg->add_conn.ikeme.mediation = conn->me_mediation;
  200. push_string(&msg, add_conn.ikeme.mediated_by, conn->me_mediated_by);
  201. push_string(&msg, add_conn.ikeme.peerid, conn->me_peerid);
  202. msg->add_conn.reqid = conn->reqid;
  203. msg->add_conn.replay_window = conn->replay_window;
  204. msg->add_conn.mark_in.value = conn->mark_in.value;
  205. msg->add_conn.mark_in.mask = conn->mark_in.mask;
  206. msg->add_conn.mark_out.value = conn->mark_out.value;
  207. msg->add_conn.mark_out.mask = conn->mark_out.mask;
  208. msg->add_conn.tfc = conn->tfc;
  209. add_end(&msg, offsetof(stroke_msg_t, add_conn.me), &conn->left);
  210. add_end(&msg, offsetof(stroke_msg_t, add_conn.other), &conn->right);
  211. if (!msg->add_conn.me.auth && !msg->add_conn.other.auth &&
  212. conn->authby)
  213. { /* leftauth/rightauth not set, use legacy options */
  214. if (streq(conn->authby, "rsa") || streq(conn->authby, "rsasig") ||
  215. streq(conn->authby, "ecdsa") || streq(conn->authby, "ecdsasig") ||
  216. streq(conn->authby, "pubkey"))
  217. {
  218. push_string(&msg, add_conn.me.auth, "pubkey");
  219. push_string(&msg, add_conn.other.auth, "pubkey");
  220. }
  221. else if (streq(conn->authby, "secret") || streq(conn->authby, "psk"))
  222. {
  223. push_string(&msg, add_conn.me.auth, "psk");
  224. push_string(&msg, add_conn.other.auth, "psk");
  225. }
  226. else if (streq(conn->authby, "xauthrsasig"))
  227. {
  228. push_string(&msg, add_conn.me.auth, "pubkey");
  229. push_string(&msg, add_conn.other.auth, "pubkey");
  230. if (conn->options & SA_OPTION_XAUTH_SERVER)
  231. {
  232. push_string(&msg, add_conn.other.auth2, "xauth");
  233. }
  234. else
  235. {
  236. push_string(&msg, add_conn.me.auth2, "xauth");
  237. }
  238. }
  239. else if (streq(conn->authby, "xauthpsk"))
  240. {
  241. push_string(&msg, add_conn.me.auth, "psk");
  242. push_string(&msg, add_conn.other.auth, "psk");
  243. if (conn->options & SA_OPTION_XAUTH_SERVER)
  244. {
  245. push_string(&msg, add_conn.other.auth2, "xauth");
  246. }
  247. else
  248. {
  249. push_string(&msg, add_conn.me.auth2, "xauth");
  250. }
  251. }
  252. }
  253. return send_stroke_msg(msg);
  254. }
  255. int starter_stroke_del_conn(starter_conn_t *conn)
  256. {
  257. stroke_msg_t *msg;
  258. msg = create_stroke_msg(STR_DEL_CONN);
  259. push_string(&msg, del_conn.name, connection_name(conn));
  260. return send_stroke_msg(msg);
  261. }
  262. int starter_stroke_route_conn(starter_conn_t *conn)
  263. {
  264. stroke_msg_t *msg;
  265. msg = create_stroke_msg(STR_ROUTE);
  266. push_string(&msg, route.name, connection_name(conn));
  267. return send_stroke_msg(msg);
  268. }
  269. int starter_stroke_unroute_conn(starter_conn_t *conn)
  270. {
  271. stroke_msg_t *msg;
  272. msg = create_stroke_msg(STR_UNROUTE);
  273. push_string(&msg, route.name, connection_name(conn));
  274. return send_stroke_msg(msg);
  275. }
  276. int starter_stroke_initiate_conn(starter_conn_t *conn)
  277. {
  278. stroke_msg_t *msg;
  279. msg = create_stroke_msg(STR_INITIATE);
  280. push_string(&msg, initiate.name, connection_name(conn));
  281. return send_stroke_msg(msg);
  282. }
  283. int starter_stroke_add_ca(starter_ca_t *ca)
  284. {
  285. stroke_msg_t *msg;
  286. msg = create_stroke_msg(STR_ADD_CA);
  287. push_string(&msg, add_ca.name, ca->name);
  288. push_string(&msg, add_ca.cacert, ca->cacert);
  289. push_string(&msg, add_ca.crluri, ca->crluri);
  290. push_string(&msg, add_ca.crluri2, ca->crluri2);
  291. push_string(&msg, add_ca.ocspuri, ca->ocspuri);
  292. push_string(&msg, add_ca.ocspuri2, ca->ocspuri2);
  293. push_string(&msg, add_ca.certuribase, ca->certuribase);
  294. return send_stroke_msg(msg);
  295. }
  296. int starter_stroke_del_ca(starter_ca_t *ca)
  297. {
  298. stroke_msg_t *msg;
  299. msg = create_stroke_msg(STR_DEL_CA);
  300. push_string(&msg, del_ca.name, ca->name);
  301. return send_stroke_msg(msg);
  302. }
  303. int starter_stroke_configure(starter_config_t *cfg)
  304. {
  305. stroke_msg_t *msg;
  306. if (cfg->setup.cachecrls)
  307. {
  308. msg = create_stroke_msg(STR_CONFIG);
  309. msg->config.cachecrl = 1;
  310. return send_stroke_msg(msg);
  311. }
  312. return 0;
  313. }