log.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2
  12. * as published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License along
  20. * with this program; if not, write to the Free Software Foundation, Inc.,
  21. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  22. */
  23. /*
  24. * This plugin is similar to simple.c, except it also logs extra information
  25. * to stdout for every plugin method called by OpenVPN.
  26. *
  27. * See the README file for build instructions.
  28. */
  29. #include <stdio.h>
  30. #include <string.h>
  31. #include <stdlib.h>
  32. #include "openvpn-plugin.h"
  33. /*
  34. * Our context, where we keep our state.
  35. */
  36. struct plugin_context {
  37. const char *username;
  38. const char *password;
  39. };
  40. /*
  41. * Given an environmental variable name, search
  42. * the envp array for its value, returning it
  43. * if found or NULL otherwise.
  44. */
  45. static const char *
  46. get_env(const char *name, const char *envp[])
  47. {
  48. if (envp)
  49. {
  50. int i;
  51. const int namelen = strlen(name);
  52. for (i = 0; envp[i]; ++i)
  53. {
  54. if (!strncmp(envp[i], name, namelen))
  55. {
  56. const char *cp = envp[i] + namelen;
  57. if (*cp == '=')
  58. {
  59. return cp + 1;
  60. }
  61. }
  62. }
  63. }
  64. return NULL;
  65. }
  66. OPENVPN_EXPORT openvpn_plugin_handle_t
  67. openvpn_plugin_open_v1(unsigned int *type_mask, const char *argv[], const char *envp[])
  68. {
  69. struct plugin_context *context;
  70. /*
  71. * Allocate our context
  72. */
  73. context = (struct plugin_context *) calloc(1, sizeof(struct plugin_context));
  74. /*
  75. * Set the username/password we will require.
  76. */
  77. context->username = "foo";
  78. context->password = "bar";
  79. /*
  80. * Which callbacks to intercept.
  81. */
  82. *type_mask =
  83. OPENVPN_PLUGIN_MASK(OPENVPN_PLUGIN_UP)
  84. |OPENVPN_PLUGIN_MASK(OPENVPN_PLUGIN_DOWN)
  85. |OPENVPN_PLUGIN_MASK(OPENVPN_PLUGIN_ROUTE_UP)
  86. |OPENVPN_PLUGIN_MASK(OPENVPN_PLUGIN_IPCHANGE)
  87. |OPENVPN_PLUGIN_MASK(OPENVPN_PLUGIN_TLS_VERIFY)
  88. |OPENVPN_PLUGIN_MASK(OPENVPN_PLUGIN_AUTH_USER_PASS_VERIFY)
  89. |OPENVPN_PLUGIN_MASK(OPENVPN_PLUGIN_CLIENT_CONNECT_V2)
  90. |OPENVPN_PLUGIN_MASK(OPENVPN_PLUGIN_CLIENT_DISCONNECT)
  91. |OPENVPN_PLUGIN_MASK(OPENVPN_PLUGIN_LEARN_ADDRESS)
  92. |OPENVPN_PLUGIN_MASK(OPENVPN_PLUGIN_TLS_FINAL);
  93. return (openvpn_plugin_handle_t) context;
  94. }
  95. void
  96. show(const int type, const char *argv[], const char *envp[])
  97. {
  98. size_t i;
  99. switch (type)
  100. {
  101. case OPENVPN_PLUGIN_UP:
  102. printf("OPENVPN_PLUGIN_UP\n");
  103. break;
  104. case OPENVPN_PLUGIN_DOWN:
  105. printf("OPENVPN_PLUGIN_DOWN\n");
  106. break;
  107. case OPENVPN_PLUGIN_ROUTE_UP:
  108. printf("OPENVPN_PLUGIN_ROUTE_UP\n");
  109. break;
  110. case OPENVPN_PLUGIN_IPCHANGE:
  111. printf("OPENVPN_PLUGIN_IPCHANGE\n");
  112. break;
  113. case OPENVPN_PLUGIN_TLS_VERIFY:
  114. printf("OPENVPN_PLUGIN_TLS_VERIFY\n");
  115. break;
  116. case OPENVPN_PLUGIN_AUTH_USER_PASS_VERIFY:
  117. printf("OPENVPN_PLUGIN_AUTH_USER_PASS_VERIFY\n");
  118. break;
  119. case OPENVPN_PLUGIN_CLIENT_CONNECT_V2:
  120. printf("OPENVPN_PLUGIN_CLIENT_CONNECT_V2\n");
  121. break;
  122. case OPENVPN_PLUGIN_CLIENT_DISCONNECT:
  123. printf("OPENVPN_PLUGIN_CLIENT_DISCONNECT\n");
  124. break;
  125. case OPENVPN_PLUGIN_LEARN_ADDRESS:
  126. printf("OPENVPN_PLUGIN_LEARN_ADDRESS\n");
  127. break;
  128. case OPENVPN_PLUGIN_TLS_FINAL:
  129. printf("OPENVPN_PLUGIN_TLS_FINAL\n");
  130. break;
  131. default:
  132. printf("OPENVPN_PLUGIN_?\n");
  133. break;
  134. }
  135. printf("ARGV\n");
  136. for (i = 0; argv[i] != NULL; ++i)
  137. printf("%d '%s'\n", (int)i, argv[i]);
  138. printf("ENVP\n");
  139. for (i = 0; envp[i] != NULL; ++i)
  140. printf("%d '%s'\n", (int)i, envp[i]);
  141. }
  142. OPENVPN_EXPORT int
  143. openvpn_plugin_func_v1(openvpn_plugin_handle_t handle, const int type, const char *argv[], const char *envp[])
  144. {
  145. struct plugin_context *context = (struct plugin_context *) handle;
  146. show(type, argv, envp);
  147. /* check entered username/password against what we require */
  148. if (type == OPENVPN_PLUGIN_AUTH_USER_PASS_VERIFY)
  149. {
  150. /* get username/password from envp string array */
  151. const char *username = get_env("username", envp);
  152. const char *password = get_env("password", envp);
  153. if (username && !strcmp(username, context->username)
  154. && password && !strcmp(password, context->password))
  155. {
  156. return OPENVPN_PLUGIN_FUNC_SUCCESS;
  157. }
  158. else
  159. {
  160. return OPENVPN_PLUGIN_FUNC_ERROR;
  161. }
  162. }
  163. else
  164. {
  165. return OPENVPN_PLUGIN_FUNC_SUCCESS;
  166. }
  167. }
  168. OPENVPN_EXPORT void
  169. openvpn_plugin_close_v1(openvpn_plugin_handle_t handle)
  170. {
  171. struct plugin_context *context = (struct plugin_context *) handle;
  172. free(context);
  173. }