OpenVPN
log.c
Go to the documentation of this file.
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 /*
25  * This plugin is similar to simple.c, except it also logs extra information
26  * to stdout for every plugin method called by OpenVPN.
27  *
28  * See the README file for build instructions.
29  */
30 
31 #include <stdio.h>
32 #include <string.h>
33 #include <stdlib.h>
34 
35 #include "openvpn-plugin.h"
36 
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 /*
46  * Given an environmental variable name, search
47  * the envp array for its value, returning it
48  * if found or NULL otherwise.
49  */
50 static const char *
51 get_env(const char *name, const char *envp[])
52 {
53  if (envp)
54  {
55  int i;
56  const int namelen = strlen(name);
57  for (i = 0; envp[i]; ++i)
58  {
59  if (!strncmp(envp[i], name, namelen))
60  {
61  const char *cp = envp[i] + namelen;
62  if (*cp == '=')
63  {
64  return cp + 1;
65  }
66  }
67  }
68  }
69  return NULL;
70 }
71 
73 openvpn_plugin_open_v1(unsigned int *type_mask, const char *argv[], const char *envp[])
74 {
75  struct plugin_context *context;
76 
77  /*
78  * Allocate our context
79  */
80  context = (struct plugin_context *) calloc(1, sizeof(struct plugin_context));
81 
82  /*
83  * Set the username/password we will require.
84  */
85  context->username = "foo";
86  context->password = "bar";
87 
88  /*
89  * Which callbacks to intercept.
90  */
91  *type_mask =
102 
103  return (openvpn_plugin_handle_t) context;
104 }
105 
106 void
107 show(const int type, const char *argv[], const char *envp[])
108 {
109  size_t i;
110  switch (type)
111  {
112  case OPENVPN_PLUGIN_UP:
113  printf("OPENVPN_PLUGIN_UP\n");
114  break;
115 
116  case OPENVPN_PLUGIN_DOWN:
117  printf("OPENVPN_PLUGIN_DOWN\n");
118  break;
119 
121  printf("OPENVPN_PLUGIN_ROUTE_UP\n");
122  break;
123 
125  printf("OPENVPN_PLUGIN_IPCHANGE\n");
126  break;
127 
129  printf("OPENVPN_PLUGIN_TLS_VERIFY\n");
130  break;
131 
133  printf("OPENVPN_PLUGIN_AUTH_USER_PASS_VERIFY\n");
134  break;
135 
137  printf("OPENVPN_PLUGIN_CLIENT_CONNECT_V2\n");
138  break;
139 
141  printf("OPENVPN_PLUGIN_CLIENT_DISCONNECT\n");
142  break;
143 
145  printf("OPENVPN_PLUGIN_LEARN_ADDRESS\n");
146  break;
147 
149  printf("OPENVPN_PLUGIN_TLS_FINAL\n");
150  break;
151 
152  default:
153  printf("OPENVPN_PLUGIN_?\n");
154  break;
155  }
156 
157  printf("ARGV\n");
158  for (i = 0; argv[i] != NULL; ++i)
159  {
160  printf("%d '%s'\n", (int)i, argv[i]);
161  }
162 
163  printf("ENVP\n");
164  for (i = 0; envp[i] != NULL; ++i)
165  {
166  printf("%d '%s'\n", (int)i, envp[i]);
167  }
168 }
169 
170 OPENVPN_EXPORT int
171 openvpn_plugin_func_v1(openvpn_plugin_handle_t handle, const int type, const char *argv[], const char *envp[])
172 {
173  struct plugin_context *context = (struct plugin_context *) handle;
174 
175  show(type, argv, envp);
176 
177  /* check entered username/password against what we require */
179  {
180  /* get username/password from envp string array */
181  const char *username = get_env("username", envp);
182  const char *password = get_env("password", envp);
183 
184  if (username && !strcmp(username, context->username)
185  && password && !strcmp(password, context->password))
186  {
188  }
189  else
190  {
192  }
193  }
194  else
195  {
197  }
198 }
199 
200 OPENVPN_EXPORT void
202 {
203  struct plugin_context *context = (struct plugin_context *) handle;
204  free(context);
205 }
const char * password
Definition: log.c:42
Contains all state information for one tunnel.
Definition: openvpn.h:500
OPENVPN_EXPORT openvpn_plugin_handle_t openvpn_plugin_open_v1(unsigned int *type_mask, const char *argv[], const char *envp[])
Definition: log.c:73
#define OPENVPN_PLUGIN_DOWN
#define OPENVPN_PLUGIN_ROUTE_UP
#define OPENVPN_EXPORT
#define OPENVPN_PLUGIN_FUNC_SUCCESS
static const char * get_env(const char *name, const char *envp[])
Definition: log.c:51
OPENVPN_EXPORT int openvpn_plugin_func_v1(openvpn_plugin_handle_t handle, const int type, const char *argv[], const char *envp[])
Definition: log.c:171
#define OPENVPN_PLUGIN_LEARN_ADDRESS
void show(const int type, const char *argv[], const char *envp[])
Definition: log.c:107
const char * username
Definition: log.c:41
#define OPENVPN_PLUGIN_IPCHANGE
#define OPENVPN_PLUGIN_TLS_VERIFY
#define OPENVPN_PLUGIN_FUNC_ERROR
#define OPENVPN_PLUGIN_TLS_FINAL
#define OPENVPN_PLUGIN_CLIENT_DISCONNECT
void * openvpn_plugin_handle_t
#define OPENVPN_PLUGIN_AUTH_USER_PASS_VERIFY
#define free
Definition: cmocka.c:1850
OPENVPN_EXPORT void openvpn_plugin_close_v1(openvpn_plugin_handle_t handle)
Definition: log.c:201
Definition: argv.h:35
#define OPENVPN_PLUGIN_CLIENT_CONNECT_V2
#define OPENVPN_PLUGIN_UP
#define OPENVPN_PLUGIN_MASK(x)