log_v3.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /*
  2. * OpenVPN -- An application to securely tunnel IP networks
  3. * over a single TCP/UDP port, with support for SSL/TLS-based
  4. * session authentication and key exchange,
  5. * packet encryption, packet authentication, and
  6. * packet compression.
  7. *
  8. * Copyright (C) 2002-2018 OpenVPN Inc <sales@openvpn.net>
  9. * Copyright (C) 2010 David Sommerseth <dazo@users.sourceforge.net>
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License version 2
  13. * as published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License along
  21. * with this program; if not, write to the Free Software Foundation, Inc.,
  22. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  23. */
  24. /*
  25. * This plugin is similar to simple.c, except it also logs extra information
  26. * to stdout for every plugin method called by OpenVPN. The only difference
  27. * between this (log_v3.c) and log.c is that this module uses the v3 plug-in
  28. * API.
  29. *
  30. * See the README file for build instructions.
  31. */
  32. #include <stdio.h>
  33. #include <string.h>
  34. #include <stdlib.h>
  35. #define ENABLE_CRYPTO
  36. #include "openvpn-plugin.h"
  37. /*
  38. * Our context, where we keep our state.
  39. */
  40. struct plugin_context {
  41. const char *username;
  42. const char *password;
  43. };
  44. /*
  45. * Given an environmental variable name, search
  46. * the envp array for its value, returning it
  47. * if found or NULL otherwise.
  48. */
  49. static const char *
  50. get_env(const char *name, const char *envp[])
  51. {
  52. if (envp)
  53. {
  54. int i;
  55. const int namelen = strlen(name);
  56. for (i = 0; envp[i]; ++i)
  57. {
  58. if (!strncmp(envp[i], name, namelen))
  59. {
  60. const char *cp = envp[i] + namelen;
  61. if (*cp == '=')
  62. {
  63. return cp + 1;
  64. }
  65. }
  66. }
  67. }
  68. return NULL;
  69. }
  70. OPENVPN_EXPORT int
  71. openvpn_plugin_open_v3(const int v3structver,
  72. struct openvpn_plugin_args_open_in const *args,
  73. struct openvpn_plugin_args_open_return *ret)
  74. {
  75. struct plugin_context *context = NULL;
  76. /* Check that we are API compatible */
  77. if (v3structver != OPENVPN_PLUGINv3_STRUCTVER)
  78. {
  79. printf("log_v3: ** ERROR ** Incompatible plug-in interface between this plug-in and OpenVPN\n");
  80. return OPENVPN_PLUGIN_FUNC_ERROR;
  81. }
  82. if (args->ssl_api != SSLAPI_OPENSSL)
  83. {
  84. printf("This plug-in can only be used against OpenVPN with OpenSSL\n");
  85. return OPENVPN_PLUGIN_FUNC_ERROR;
  86. }
  87. /* Print some version information about the OpenVPN process using this plug-in */
  88. printf("log_v3: OpenVPN %s (Major: %i, Minor: %i, Patch: %s)\n",
  89. args->ovpn_version, args->ovpn_version_major,
  90. args->ovpn_version_minor, args->ovpn_version_patch);
  91. /* Which callbacks to intercept. */
  92. ret->type_mask =
  93. OPENVPN_PLUGIN_MASK(OPENVPN_PLUGIN_UP)
  94. |OPENVPN_PLUGIN_MASK(OPENVPN_PLUGIN_DOWN)
  95. |OPENVPN_PLUGIN_MASK(OPENVPN_PLUGIN_ROUTE_UP)
  96. |OPENVPN_PLUGIN_MASK(OPENVPN_PLUGIN_IPCHANGE)
  97. |OPENVPN_PLUGIN_MASK(OPENVPN_PLUGIN_TLS_VERIFY)
  98. |OPENVPN_PLUGIN_MASK(OPENVPN_PLUGIN_AUTH_USER_PASS_VERIFY)
  99. |OPENVPN_PLUGIN_MASK(OPENVPN_PLUGIN_CLIENT_CONNECT_V2)
  100. |OPENVPN_PLUGIN_MASK(OPENVPN_PLUGIN_CLIENT_DISCONNECT)
  101. |OPENVPN_PLUGIN_MASK(OPENVPN_PLUGIN_LEARN_ADDRESS)
  102. |OPENVPN_PLUGIN_MASK(OPENVPN_PLUGIN_TLS_FINAL);
  103. /* Allocate our context */
  104. context = (struct plugin_context *) calloc(1, sizeof(struct plugin_context));
  105. /* Set the username/password we will require. */
  106. context->username = "foo";
  107. context->password = "bar";
  108. /* Point the global context handle to our newly created context */
  109. ret->handle = (void *) context;
  110. return OPENVPN_PLUGIN_FUNC_SUCCESS;
  111. }
  112. void
  113. show(const int type, const char *argv[], const char *envp[])
  114. {
  115. size_t i;
  116. switch (type)
  117. {
  118. case OPENVPN_PLUGIN_UP:
  119. printf("OPENVPN_PLUGIN_UP\n");
  120. break;
  121. case OPENVPN_PLUGIN_DOWN:
  122. printf("OPENVPN_PLUGIN_DOWN\n");
  123. break;
  124. case OPENVPN_PLUGIN_ROUTE_UP:
  125. printf("OPENVPN_PLUGIN_ROUTE_UP\n");
  126. break;
  127. case OPENVPN_PLUGIN_IPCHANGE:
  128. printf("OPENVPN_PLUGIN_IPCHANGE\n");
  129. break;
  130. case OPENVPN_PLUGIN_TLS_VERIFY:
  131. printf("OPENVPN_PLUGIN_TLS_VERIFY\n");
  132. break;
  133. case OPENVPN_PLUGIN_AUTH_USER_PASS_VERIFY:
  134. printf("OPENVPN_PLUGIN_AUTH_USER_PASS_VERIFY\n");
  135. break;
  136. case OPENVPN_PLUGIN_CLIENT_CONNECT_V2:
  137. printf("OPENVPN_PLUGIN_CLIENT_CONNECT_V2\n");
  138. break;
  139. case OPENVPN_PLUGIN_CLIENT_DISCONNECT:
  140. printf("OPENVPN_PLUGIN_CLIENT_DISCONNECT\n");
  141. break;
  142. case OPENVPN_PLUGIN_LEARN_ADDRESS:
  143. printf("OPENVPN_PLUGIN_LEARN_ADDRESS\n");
  144. break;
  145. case OPENVPN_PLUGIN_TLS_FINAL:
  146. printf("OPENVPN_PLUGIN_TLS_FINAL\n");
  147. break;
  148. default:
  149. printf("OPENVPN_PLUGIN_?\n");
  150. break;
  151. }
  152. printf("ARGV\n");
  153. for (i = 0; argv[i] != NULL; ++i)
  154. printf("%d '%s'\n", (int)i, argv[i]);
  155. printf("ENVP\n");
  156. for (i = 0; envp[i] != NULL; ++i)
  157. printf("%d '%s'\n", (int)i, envp[i]);
  158. }
  159. static void
  160. x509_print_info(X509 *x509crt)
  161. {
  162. int i, n;
  163. int fn_nid;
  164. ASN1_OBJECT *fn;
  165. ASN1_STRING *val;
  166. X509_NAME *x509_name;
  167. X509_NAME_ENTRY *ent;
  168. const char *objbuf;
  169. unsigned char *buf;
  170. x509_name = X509_get_subject_name(x509crt);
  171. n = X509_NAME_entry_count(x509_name);
  172. for (i = 0; i < n; ++i)
  173. {
  174. ent = X509_NAME_get_entry(x509_name, i);
  175. if (!ent)
  176. {
  177. continue;
  178. }
  179. fn = X509_NAME_ENTRY_get_object(ent);
  180. if (!fn)
  181. {
  182. continue;
  183. }
  184. val = X509_NAME_ENTRY_get_data(ent);
  185. if (!val)
  186. {
  187. continue;
  188. }
  189. fn_nid = OBJ_obj2nid(fn);
  190. if (fn_nid == NID_undef)
  191. {
  192. continue;
  193. }
  194. objbuf = OBJ_nid2sn(fn_nid);
  195. if (!objbuf)
  196. {
  197. continue;
  198. }
  199. if (ASN1_STRING_to_UTF8(&buf, val) < 0)
  200. {
  201. continue;
  202. }
  203. printf("X509 %s: %s\n", objbuf, (char *)buf);
  204. OPENSSL_free(buf);
  205. }
  206. }
  207. OPENVPN_EXPORT int
  208. openvpn_plugin_func_v3(const int version,
  209. struct openvpn_plugin_args_func_in const *args,
  210. struct openvpn_plugin_args_func_return *retptr)
  211. {
  212. struct plugin_context *context = (struct plugin_context *) args->handle;
  213. printf("\nopenvpn_plugin_func_v3() :::::>> ");
  214. show(args->type, args->argv, args->envp);
  215. /* Dump some X509 information if we're in the TLS_VERIFY phase */
  216. if ((args->type == OPENVPN_PLUGIN_TLS_VERIFY) && args->current_cert)
  217. {
  218. printf("---- X509 Subject information ----\n");
  219. printf("Certificate depth: %i\n", args->current_cert_depth);
  220. x509_print_info(args->current_cert);
  221. printf("----------------------------------\n");
  222. }
  223. /* check entered username/password against what we require */
  224. if (args->type == OPENVPN_PLUGIN_AUTH_USER_PASS_VERIFY)
  225. {
  226. /* get username/password from envp string array */
  227. const char *username = get_env("username", args->envp);
  228. const char *password = get_env("password", args->envp);
  229. if (username && !strcmp(username, context->username)
  230. && password && !strcmp(password, context->password))
  231. {
  232. return OPENVPN_PLUGIN_FUNC_SUCCESS;
  233. }
  234. else
  235. {
  236. return OPENVPN_PLUGIN_FUNC_ERROR;
  237. }
  238. }
  239. else
  240. {
  241. return OPENVPN_PLUGIN_FUNC_SUCCESS;
  242. }
  243. }
  244. OPENVPN_EXPORT void
  245. openvpn_plugin_close_v1(openvpn_plugin_handle_t handle)
  246. {
  247. struct plugin_context *context = (struct plugin_context *) handle;
  248. free(context);
  249. }