simple.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 file implements a simple OpenVPN plugin module which
  25. * will examine the username/password provided by a client,
  26. * and make an accept/deny determination. Will run
  27. * on Windows or *nix.
  28. *
  29. * See the README file for build instructions.
  30. */
  31. #include <stdio.h>
  32. #include <string.h>
  33. #include <stdlib.h>
  34. #include "openvpn-plugin.h"
  35. /*
  36. * Our context, where we keep our state.
  37. */
  38. struct plugin_context {
  39. const char *username;
  40. const char *password;
  41. };
  42. /*
  43. * Given an environmental variable name, search
  44. * the envp array for its value, returning it
  45. * if found or NULL otherwise.
  46. */
  47. static const char *
  48. get_env(const char *name, const char *envp[])
  49. {
  50. if (envp)
  51. {
  52. int i;
  53. const int namelen = strlen(name);
  54. for (i = 0; envp[i]; ++i)
  55. {
  56. if (!strncmp(envp[i], name, namelen))
  57. {
  58. const char *cp = envp[i] + namelen;
  59. if (*cp == '=')
  60. {
  61. return cp + 1;
  62. }
  63. }
  64. }
  65. }
  66. return NULL;
  67. }
  68. OPENVPN_EXPORT openvpn_plugin_handle_t
  69. openvpn_plugin_open_v1(unsigned int *type_mask, const char *argv[], const char *envp[])
  70. {
  71. struct plugin_context *context;
  72. /*
  73. * Allocate our context
  74. */
  75. context = (struct plugin_context *) calloc(1, sizeof(struct plugin_context));
  76. /*
  77. * Set the username/password we will require.
  78. */
  79. context->username = "foo";
  80. context->password = "bar";
  81. /*
  82. * We are only interested in intercepting the
  83. * --auth-user-pass-verify callback.
  84. */
  85. *type_mask = OPENVPN_PLUGIN_MASK(OPENVPN_PLUGIN_AUTH_USER_PASS_VERIFY);
  86. return (openvpn_plugin_handle_t) context;
  87. }
  88. OPENVPN_EXPORT int
  89. openvpn_plugin_func_v1(openvpn_plugin_handle_t handle, const int type, const char *argv[], const char *envp[])
  90. {
  91. struct plugin_context *context = (struct plugin_context *) handle;
  92. /* get username/password from envp string array */
  93. const char *username = get_env("username", envp);
  94. const char *password = get_env("password", envp);
  95. /* check entered username/password against what we require */
  96. if (username && !strcmp(username, context->username)
  97. && password && !strcmp(password, context->password))
  98. {
  99. return OPENVPN_PLUGIN_FUNC_SUCCESS;
  100. }
  101. else
  102. {
  103. return OPENVPN_PLUGIN_FUNC_ERROR;
  104. }
  105. }
  106. OPENVPN_EXPORT void
  107. openvpn_plugin_close_v1(openvpn_plugin_handle_t handle)
  108. {
  109. struct plugin_context *context = (struct plugin_context *) handle;
  110. free(context);
  111. }