Skip to contentSkip to conversation

About making plugins for openvpn

Openvpn-devel Jose Sanchez 1 participant 4 messages Sep 19, 2008 – Sep 22, 2008 3 messages not in the archive

lore
Share & tools
lore
#0 lore raw

Hello, I am new at openvpn, I am trying to do a very simple plugin to see connections/disconnections and to be able to notify to another system in "real" time.

The problem is that I haven't found any documentation about the code or about how to make plugins except on the code (I have been reading the comments and I have some code of my plugin).

The main problem is that there are two functions: OPENVPN_PLUGIN_DEF int OPENVPN_PLUGIN_FUNC(openvpn_plugin_func_v1)
(openvpn_plugin_handle_t handle, const int type, const char
*argv[], const char *envp[]);
and
OPENVPN_PLUGIN_DEF int OPENVPN_PLUGIN_FUNC(openvpn_plugin_func_v2)

     (openvpn_plugin_handle_t handle,
      const int type,
      const char *argv[],
      const char *envp[],
      void *per_client_context,
      struct openvpn_plugin_string_list **return_list);

What I need is to obtain the public (inet IP) and the private (vpn ip) when a client connects/disconnect to notify it to another program (I know I can make a ping or similar things but it seems to be too slow when there are a lot of clients connected (> 100)).

What I need is to know in which variable is that data and which function I need to implement (v1 or v2) and how these functions works (Is any documentation on any site?).

In openvpn_plugin_open_v1 I have registered the events: *type_mask = OPENVPN_PLUGIN_MASK(OPENVPN_PLUGIN_CLIENT_CONNECT) | OPENVPN_PLUGIN_MASK(OPENVPN_PLUGIN_CLIENT_DISCONNECT); and even on openvpn_plugin_func_v1 I can know if there is if (type == OPENVPN_PLUGIN_CLIENT_CONNECT) or OPENVPN_PLUGIN_CLIENT_DISCONNECT but I don't know how to recover IP information.

If somebody could help me or saying me where the documentation is would be great.

Thanks in advance and excuse my english (I have written this too quickly) and excuse me if what i am asking is answered on another place that I haven't found.

Signature · 1 line
Jose Sánchez <jose dot informatica at centrolamilagrosa dot org>

A message that is not in the archive (sent off-list or not yet received): [email protected]...

#2 lore raw
Quoted 46 lines · Giancarlo Razzolini

Jose Sanchez escreveu:

Quoted 37 lines

Hello, I am new at openvpn, I am trying to do a very simple plugin to see connections/disconnections and to be able to notify to another system in "real" time.

The problem is that I haven't found any documentation about the code or about how to make plugins except on the code (I have been reading the comments and I have some code of my plugin).

The main problem is that there are two functions: OPENVPN_PLUGIN_DEF int OPENVPN_PLUGIN_FUNC(openvpn_plugin_func_v1)
(openvpn_plugin_handle_t handle, const int type, const char
*argv[], const char *envp[]);
and
OPENVPN_PLUGIN_DEF int OPENVPN_PLUGIN_FUNC(openvpn_plugin_func_v2)

     (openvpn_plugin_handle_t handle,
      const int type,
      const char *argv[],
      const char *envp[],
      void *per_client_context,
      struct openvpn_plugin_string_list **return_list);

What I need is to obtain the public (inet IP) and the private (vpn ip) when a client connects/disconnect to notify it to another program (I know I can make a ping or similar things but it seems to be too slow when there are a lot of clients connected (> 100)).

What I need is to know in which variable is that data and which function I need to implement (v1 or v2) and how these functions works (Is any documentation on any site?).

In openvpn_plugin_open_v1 I have registered the events: *type_mask = OPENVPN_PLUGIN_MASK(OPENVPN_PLUGIN_CLIENT_CONNECT) | OPENVPN_PLUGIN_MASK(OPENVPN_PLUGIN_CLIENT_DISCONNECT); and even on openvpn_plugin_func_v1 I can know if there is if (type == OPENVPN_PLUGIN_CLIENT_CONNECT) or OPENVPN_PLUGIN_CLIENT_DISCONNECT but I don't know how to recover IP information.

Quoted 8 lines

If somebody could help me or saying me where the documentation is would be great.

Thanks in advance and excuse my english (I have written this too quickly) and excuse me if what i am asking is answered on another place that I haven't found.

Quoted 7 lines

You don't need a plugin for that. That's just why there is a openvpn-status file. It won't show you things in real time but, it didn't seemed as a requirement, based on what you are saying. Take a look at the openvpn-status option.

My regards,

Unfortunately, yes, it is a requirement, I know openvpn-status, but thanks anyway for your answer.

Signature · 1 line
Jose Sánchez <jose dot informatica at centrolamilagrosa dot org>

A message that is not in the archive (sent off-list or not yet received): [email protected]...

#4 lore raw

Thank you, I have figured out those things (but not all of them, so thanks for the detailed explanation).

The problem that I have is that I have not found good examples about the structure of the environment variable (I have seen the: get_env function in simple.c so I will try to print that variable to the log or to the stdout to study its structure and data stored on it).

Thanks to all for the help. As a suggestion, I would upload a mini-howto about making plugins at the website, description made by David Sommerset in this email can be very helpful and if it was a wiki, all people could collaborate.

Thanks again, I will tell you about my success or not about making the plugin.

Signature · 1 line
Jose Sánchez <jose dot informatica at centrolamilagrosa dot org>
Quoted 100 lines · David Sommerseth
Quoted 37 lines · Jose Sanchez

Hello, I am new at openvpn, I am trying to do a very simple plugin to see connections/disconnections and to be able to notify to another system in "real" time.

The problem is that I haven't found any documentation about the code or about how to make plugins except on the code (I have been reading the comments and I have some code of my plugin).

The main problem is that there are two functions: OPENVPN_PLUGIN_DEF int OPENVPN_PLUGIN_FUNC(openvpn_plugin_func_v1)
(openvpn_plugin_handle_t handle, const int type, const char
*argv[], const char *envp[]);
and
OPENVPN_PLUGIN_DEF int OPENVPN_PLUGIN_FUNC(openvpn_plugin_func_v2)

     (openvpn_plugin_handle_t handle,
      const int type,
      const char *argv[],
      const char *envp[],
      void *per_client_context,
      struct openvpn_plugin_string_list **return_list);

What I need is to obtain the public (inet IP) and the private (vpn ip) when a client connects/disconnect to notify it to another program (I know I can make a ping or similar things but it seems to be too slow when there are a lot of clients connected (> 100)).

What I need is to know in which variable is that data and which function I need to implement (v1 or v2) and how these functions works (Is any documentation on any site?).

In openvpn_plugin_open_v1 I have registered the events: *type_mask = OPENVPN_PLUGIN_MASK(OPENVPN_PLUGIN_CLIENT_CONNECT) | OPENVPN_PLUGIN_MASK(OPENVPN_PLUGIN_CLIENT_DISCONNECT); and even on openvpn_plugin_func_v1 I can know if there is if (type == OPENVPN_PLUGIN_CLIENT_CONNECT) or OPENVPN_PLUGIN_CLIENT_DISCONNECT but I don't know how to recover IP information.

Quoted 7 lines

If somebody could help me or saying me where the documentation is would be great.

Thanks in advance and excuse my english (I have written this too quickly) and excuse me if what i am asking is answered on another place that I haven't found.

Hi!

I'm writing a plugin as well, and I have been through the same as you're facing now. It's not hard or difficult at all, but it requires some testing before you figure out all the pieces. Anyway, have a look on those plugins available in the plugin directory in the OpenVPN source code and you might find some help here. Here it is several different approaches. I used auth-pam as a starting point for my project. This is btw. using the v1 plugin interface. I see that a v2. version is available, but have not ported my project to this interface yet.

But I'll give you a quick overview here now. You basically need 3 OpenVPN based function in your plugin:

OPENVPN_EXPORT openvpn_plugin_handle_t
openvpn_plugin_open_v1(unsigned int *type_mask, const char *argv[], const char *envp[])

OPENVPN_EXPORT int openvpn_plugin_func_v1(openvpn_plugin_handle_t handle,
const int type,
const char *argv[], const char *envp[])

OPENVPN_EXPORT void openvpn_plugin_close_v1(openvpn_plugin_handle_t handle)

The first one initialises your plugin, you set a side a memory buffer for you plugin data (which is not per connection based, afaik). Here is also where you set the OPENVPN_PLUGIN_MASK ... f.ex.

	*type_mask = OPENVPN_PLUGIN_MASK(OPENVPN_PLUGIN_CLIENT_CONNECT)
                    | OPENVPN_PLUGIN_MASK(OPENVPN_PLUGIN_CLIENT_DISCONNECT);

You also return the pointer to your memory buffer from this function.

The second one is the one which is called on every plugin event from OpenVPN. Here's where your plugin does it's main work. The handle variable is the pointer to your memory buffer with your plugin data.

The third one cleans up after you plugin when OpenVPN shuts down. Again, handle points at your plugin data.

Quoted 1 line

From here, it is pretty much straight forward for the rest of the plugin. But have a look on the

other plugins as well, as they give more ideas how to do things more advanced, like using environment variables (where you'll find your IP addresses).

I hope this could help you out.

kind regards,

David Sommerseth

A message that is not in the archive (sent off-list or not yet received): [email protected]...

#6 lore raw

Thank you very much, after printing all environment variable, here I have the environment variable complete, I send this for future reference I hope this will help some other people (I have edited for some values or even removed them for privacy reasons):

These variables are captured during OPENVPN_PLUGIN_CLIENT_CONNECT event, I don't know if there are avaiable on other events or if some others are missing.

Other question: Will be any problem with the use of threads and sockets on the plugin? (I mean, can I use normal C programming techniques or are in the plugins some functions that can't be used?).

Thanks all again for your help.

/* Interesting environment variables
* ---------------------------------
*

   * ifconfig_pool_local_ip           - "local" client gateway
   * ifconfig_pool_remote_ip          - IP in the VPN
   * time_ascii                       - In format: Mon Sep 22 13:43:22

2008
* common_name - Client's name
*
*
=========================================================================
* OTHER ENVIRONMENT VARIABLES (maybe can be useful in the future) *
=========================================================================
*
* trusted_port
* trusted_ip
* untrusted_port
* untrusted_ip
*
* tls_serial_0=3
*
tls_id_0=/C=ES/ST=State/O=Country/CN=hostname/[email protected]...
* tls_serial_1
*
tls_id_1=/C=ES/ST=State/L=Other/O=Other/CN=hostname/[email protected]...
*
* daemon_log_redirect=0
* daemon=0
*
* verb=5
*
* local_port=1194
* proto=tcp-server
*
* config=/etc/openvpn/server.conf
*
* ifconfig_local=10.8.0.1
* ifconfig_remote=10.8.0.2
*
* route_net_gateway=192.168.X.X
* route_vpn_gateway=10.8.0.2
* route_network_1=10.8.0.0
* route_netmask_1=255.255.255.0
* route_gateway_1=10.8.0.2
*
* script_context=init
* tun_mtu=1500
* link_mtu=1560
* dev=tun0
*
*/

Quoted 53 lines · David Sommerseth
Quoted 7 lines · Jose Sanchez

Thank you, I have figured out those things (but not all of them, so thanks for the detailed explanation).

The problem that I have is that I have not found good examples about the structure of the environment variable (I have seen the: get_env function in simple.c so I will try to print that variable to the log or to the stdout to study its structure and data stored on it).

I need to take things by memory now ... but if you want the "public" IP address and the VPN ip address ... I would say you'll find them in these environment variables:

* untrusted_ip
Public IP address of the client, available before the session is properly authenticated

* untrusted_port
Port used by untrusted_ip

* trusted_ip
Public IP address after the session is properly authenticated, and available in the OPENVPN_PLUGIN_CLIENT_CONNECT and OPENVPN_PLUGIN_CLIENT_DISCONNECT phases.

* trusted_port
Port used by trusted_ip

* ifconfig_pool_remote_ip, ifconfig_pool_netmask
The local VPN IP address and netmask the VPN client will be using. Available during OPENVPN_PLUGIN_CLIENT_CONNECT and OPENVPN_PLUGIN_CLIENT_DISCONNECT.

Quoted 4 lines

Thanks to all for the help. As a suggestion, I would upload a mini-howto about making plugins at the website, description made by David Sommerset in this email can be very helpful and if it was a wiki, all people could collaborate.

If my little quick description might be helpful for other, I do not mind if it is published as a mini-howto, or whatever else suitable.

Quoted 2 lines

Thanks again, I will tell you about my success or not about making the plugin.

Please do! And I am, at least, curious about what kind of work and signalling your plug-in will do.

kind regards,

David Sommerseth

Signature · 1 line
Jose Sánchez <jose dot informatica at centrolamilagrosa dot org>