├── python_module_src └── openvpn │ ├── __init__.py │ └── plugin.py ├── AUTHORS ├── Makefile ├── README.md ├── CONTRIBUTING.md ├── embedpython.h ├── setup.py ├── embedpython.c ├── openvpn-plugin-python.c └── LICENSE /python_module_src/openvpn/__init__.py: -------------------------------------------------------------------------------- 1 | __all__ = ['plugin'] 2 | from plugin import * 3 | -------------------------------------------------------------------------------- /AUTHORS: -------------------------------------------------------------------------------- 1 | The following authors have created the source code of 2 | "Python plug-in for OpenVPN" published and distributed by YANDEX LLC as 3 | the owner: 4 | 5 | Boris Lytochkin 6 | Mikhail Arefiev 7 | Artem Rudenko 8 | 9 | This project is based on source code by Kaltashkin Eugene 10 | published at https://github.com/aborche/openvpn-plugin-python-proxy 11 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | all: openvpn-plugin-python.so 2 | 3 | WARNINGS=-Wall 4 | CC=gcc 5 | GCC=gcc 6 | #WARNINGS=-Wunreachable-code # Gcc 4.1 .. 4.4 are too buggy to make this useful 7 | 8 | LIBDIR ?= /usr/local/lib/openvpn/plugins 9 | 10 | openvpn-plugin-python.so: openvpn-plugin-python.c embedpython.c Makefile 11 | @echo rm -f "$@" 12 | @[ ! -e build -o build/lib.*/$@ -nt setup.py -a build/lib.*/$@ -nt Makefile ] || rm -r build 13 | CFLAGS="$(WARNINGS) -I/usr/local/lib/" ./setup.py build 14 | @#CFLAGS="-O0 $(WARNINGS)" ./setup.py build 15 | ln -sf build/lib.*/$@ . 16 | 17 | .PHONY: install install-lib 18 | install: install-lib 19 | install-lib: 20 | mkdir -p $(DESTDIR)$(LIBDIR) 21 | cp build/lib.*/openvpn-plugin-python.so $(DESTDIR)$(LIBDIR) 22 | CFLAGS="$(WARNINGS) -I/usr/local/lib/" ./setup.py install 23 | 24 | .PHONY: clean 25 | clean: 26 | rm -rf openvpn-plugin-python.so build dist core 27 | CFLAGS="$(WARNINGS) -I/usr/local/lib/" ./setup.py clean 28 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Description 2 | 3 | Persistent Python3 plugin for OpenVPN. It loads the interpreter instance and lets it run indefinitely, calling Python functions from the C code, thus avoiding the overhead of starting up an interpreter complete with the Python startup library. 4 | 5 | Tests show that 6 | 7 | ``` 8 | Full reload: 281 9 | Persistent interpreter: 3 10 | ``` 11 | 12 | for a realistically CPU-intensive script. 13 | 14 | 15 | # Installation 16 | 17 | Either copy the `.so` file and the example script to your relevant destination dirs, or use the FreeBSD port. 18 | 19 | 20 | # Usage 21 | 22 | Add this to your openvpn config (e. g. `/usr/local/etc/openvpn/openvpn_tun1.conf`) 23 | 24 | ``` 25 | plugin /usr/local/lib/openvpn-python-plugin.so /usr/local/etc/openvpn/scripts 26 | ``` 27 | 28 | where the first argument is the plugin `.so` and the second argument is the script dir with the Python scripts of your liking. 29 | 30 | 31 | # Script examples 32 | 33 | You can take the provided example as is and customize it according to your needs. Be sure to leave the main script file name intact, and from it, feel free to `import` or even `reload()` any other Python module as required. 34 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Notice to external contributors 2 | 3 | 4 | ## General info 5 | 6 | Hello! In order for us (YANDEX LLC) to accept patches and other contributions from you, you will have to adopt our Yandex Contributor License Agreement (the "**CLA**"). The current version of the CLA can be found here: 7 | 1) https://yandex.ru/legal/cla/?lang=en (in English) and 8 | 2) https://yandex.ru/legal/cla/?lang=ru (in Russian). 9 | 10 | By adopting the CLA, you state the following: 11 | 12 | * You obviously wish and are willingly licensing your contributions to us for our open source projects under the terms of the CLA, 13 | * You have read the terms and conditions of the CLA and agree with them in full, 14 | * You are legally able to provide and license your contributions as stated, 15 | * We may use your contributions for our open source projects and for any other our project too, 16 | * We rely on your assurances concerning the rights of third parties in relation to your contributions. 17 | 18 | If you agree with these principles, please read and adopt our CLA. By providing us your contributions, you hereby declare that you have already read and adopt our CLA, and we may freely merge your contributions with our corresponding open source project and use it in further in accordance with terms and conditions of the CLA. 19 | 20 | ## Provide contributions 21 | 22 | If you have already adopted terms and conditions of the CLA, you are able to provide your contributions. When you submit your pull request, please add the following information into it: 23 | 24 | ``` 25 | I hereby agree to the terms of the CLA available at: [link]. 26 | ``` 27 | 28 | Replace the bracketed text as follows: 29 | * [link] is the link to the current version of the CLA: https://yandex.ru/legal/cla/?lang=en (in English) or https://yandex.ru/legal/cla/?lang=ru (in Russian). 30 | 31 | It is enough to provide us such notification once. 32 | 33 | ## Other questions 34 | 35 | If you have any questions, please mail us at opensource@yandex-team.ru. 36 | -------------------------------------------------------------------------------- /embedpython.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2020 Yandex, LLC 3 | * 4 | * This program is free software; you can redistribute it and/or modify it 5 | * under the terms of the GNU General Public License version 2 as published by 6 | * the Free Software Foundation. 7 | * 8 | * This program is distributed in the hope that it will be useful, but WITHOUT 9 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 10 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 11 | * more details. 12 | * 13 | * You should have received a copy of the GNU General Public License along with 14 | * this program; if not, write to the Free Software Foundation, Inc., 51 15 | * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 16 | */ 17 | 18 | #ifndef __PYTHON_H_ALSO_THE_GAME__ 19 | #define __PYTHON_H_ALSO_THE_GAME__ 20 | 21 | #include 22 | 23 | #include 24 | 25 | #define GLOBALS_KEY "_GLOBALS" 26 | 27 | enum PYTHON_Result { 28 | PYTHON_OK = 0, 29 | PYTHON_GENERIC_ERROR, 30 | PYTHON_INIT_FAIL, 31 | PYTHON_UNINIT_FAIL, 32 | PYTHON_IMPORT_FAIL, 33 | PYTHON_NOT_INITIALIZED, 34 | PYTHON_UNKNOWN_FUNCTION, 35 | PYTHON_RUNTIME_EXCEPTION, 36 | }; 37 | 38 | struct Python_Inventory { 39 | char *script_dir; 40 | char *script_module; 41 | char *program_name; 42 | PyObject *globals; 43 | PyObject *module; 44 | enum PYTHON_Result last_error; 45 | bool initialized; 46 | } Python_Inventory; 47 | 48 | int python_init(struct Python_Inventory *inv); 49 | 50 | PyObject* python_call_function(struct Python_Inventory *inv, const char* name, int numargs, ...); 51 | 52 | bool python_is_function_defined(struct Python_Inventory *inv, const char* name); 53 | 54 | int python_uninit(struct Python_Inventory *inv); 55 | 56 | const char* python_error(const enum PYTHON_Result res); 57 | 58 | PyObject* python_int(long long int); 59 | PyObject* python_str(const char*); 60 | long long int python_from_int(PyObject*); 61 | const char* python_from_str(PyObject*); 62 | 63 | #endif 64 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | 3 | """ 4 | Copyright (C) 2018 Kaltashkin Eugene 5 | Copyright (C) 2019 Boris Lytochkin 6 | Copyright (C) 2020 Yandex, LLC 7 | 8 | This program is free software; you can redistribute it and/or modify it 9 | under the terms of the GNU General Public License version 2 as published by 10 | the Free Software Foundation. 11 | 12 | This program is distributed in the hope that it will be useful, but WITHOUT 13 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 14 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 15 | more details. 16 | 17 | You should have received a copy of the GNU General Public License along with 18 | this program; if not, write to the Free Software Foundation, Inc., 51 19 | Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 20 | """ 21 | 22 | """ Persistent Python3 plugin for OpenVPN. It loads the interpreter 23 | instance and lets it run forever, calling Python functions from the C 24 | code, thus avoiding the overhead of firing up an interpreter complete 25 | with the Python startup library. 26 | """ 27 | import warnings; warnings.simplefilter('default') 28 | 29 | import distutils.sysconfig 30 | import os 31 | import sys 32 | 33 | try: 34 | from setuptools import setup, Extension 35 | except ImportError: 36 | from distutils.core import setup, Extension 37 | 38 | if "Py_DEBUG" not in os.environ: 39 | Py_DEBUG = [] 40 | else: 41 | Py_DEBUG = [('Py_DEBUG', 1)] 42 | 43 | libpython_so = distutils.sysconfig.get_config_var('INSTSONAME') 44 | ext_modules = [ 45 | Extension( 46 | "openvpn-plugin-python", 47 | sources=["openvpn-plugin-python.c", "embedpython.c"], 48 | include_dirs = ["/usr/local/include", "/usr/include/openvpn"], 49 | library_dirs=["/usr/local/lib", "/usr/lib"], 50 | define_macros=[('LIBPYTHON_SO', '"' + libpython_so + '"')] + Py_DEBUG, 51 | ), 52 | ] 53 | 54 | setup( 55 | name="openvpn-plugin-python", 56 | version="0.1.0", 57 | description="Script OpenVPN with Python 3", 58 | keywords="authentication,security", 59 | platforms="Unix", 60 | long_description=__doc__, 61 | author="Yandex", 62 | author_email="noc@yandex.net", 63 | url="http://yandex.ru/", 64 | license="GPLv2", 65 | classifiers=["Topic :: System :: Systems Administration :: "], 66 | ext_modules=ext_modules, 67 | packages=['openvpn'], 68 | package_dir={'openvpn': 'python_module_src/openvpn/'}, 69 | ) 70 | -------------------------------------------------------------------------------- /python_module_src/openvpn/plugin.py: -------------------------------------------------------------------------------- 1 | #!/usr/local/bin/python2 2 | 3 | ''' 4 | Copyright (C) 2018 Kaltashkin Eugene 5 | Copyright (C) 2019 Boris Lytochkin 6 | Copyright (C) 2020 Yandex, LLC 7 | 8 | This program is free software; you can redistribute it and/or modify it 9 | under the terms of the GNU General Public License version 2 as published by 10 | the Free Software Foundation. 11 | 12 | This program is distributed in the hope that it will be useful, but WITHOUT 13 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 14 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 15 | more details. 16 | 17 | You should have received a copy of the GNU General Public License along with 18 | this program; if not, write to the Free Software Foundation, Inc., 51 19 | Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 20 | ''' 21 | 22 | ''' 23 | For detailed description of plug-in functions take a look at 24 | https://github.com/OpenVPN/openvpn/blob/master/include/openvpn-plugin.h.in 25 | 26 | Each plug-in method declaration must accept single argument. This argument 27 | holds a set of variables to check as a dict. 28 | 29 | Plug-in core checks each method for existence upon startup. Comment out 30 | all methods you do not need, this will prevent plug-in core from registering 31 | for that method thus saving some precious time for packet processing. 32 | ''' 33 | 34 | import sys, os 35 | 36 | OPENVPN_PLUGIN_FUNC_SUCCESS = 0 37 | OPENVPN_PLUGIN_FUNC_ERROR = 1 38 | OPENVPN_PLUGIN_FUNC_DEFERRED = 2 39 | 40 | _GLOBALS = {} 41 | 42 | def print_dict(env): 43 | for k, v in env.items(): 44 | print("%s -> %s :: %s" % (k, v, type(v))) 45 | print("=" * 30) 46 | 47 | 48 | """ 49 | Method OPENVPN_PLUGIN_UP called when plug-in started first time from OpenVPN main process 50 | """ 51 | def PLUGIN_UP(env): 52 | print("OPENVPN_PLUGIN_UP") 53 | return OPENVPN_PLUGIN_FUNC_SUCCESS 54 | 55 | """ 56 | Method OPENVPN_PLUGIN_DOWN called when OpenVPN main process is shutting down 57 | """ 58 | def PLUGIN_DOWN(env): 59 | print("OPENVPN_PLUGIN_DOWN") 60 | print_dict(env) 61 | return OPENVPN_PLUGIN_FUNC_SUCCESS 62 | 63 | def PLUGIN_ROUTE_UP(env): 64 | print_dict(env) 65 | return OPENVPN_PLUGIN_FUNC_SUCCESS 66 | 67 | def PLUGIN_IPCHANGE(env): 68 | print("OPENVPN_PLUGIN_IPCHANGE") 69 | print_dict(env) 70 | return OPENVPN_PLUGIN_FUNC_SUCCESS 71 | 72 | def PLUGIN_TLS_VERIFY(env): 73 | print("OPENVPN_PLUGIN_TLS_VERIFY") 74 | print_dict(env) 75 | return OPENVPN_PLUGIN_FUNC_SUCCESS 76 | 77 | def PLUGIN_AUTH_USER_PASS_VERIFY(env): 78 | """ 79 | Method OPENVPN_PLUGIN_AUTH_USER_PASS_VERIFY used for checking username/password pair. 80 | in OpenVPN debug log password field is not included due security purposes, but exists in argv array 81 | """ 82 | print("OPENVPN_PLUGIN_AUTH_USER_PASS_VERIFY") 83 | print_dict(env) 84 | return OPENVPN_PLUGIN_FUNC_SUCCESS 85 | 86 | def PLUGIN_CLIENT_CONNECT(env): 87 | print("OPENVPN_PLUGIN_CLIENT_CONNECT\n") 88 | print_dict(env) 89 | return OPENVPN_PLUGIN_FUNC_SUCCESS 90 | 91 | def PLUGIN_CLIENT_DISCONNECT(env): 92 | print("OPENVPN_PLUGIN_CLIENT_DISCONNECT\n") 93 | print_dict(env) 94 | 95 | def PLUGIN_LEARN_ADDRESS(env): 96 | print("OPENVPN_PLUGIN_LEARN_ADDRESS\n") 97 | print_dict(env) 98 | ''' 99 | # Uncomment this block for activate packet filter file creation 100 | if 'pf_file' in env: 101 | print('PF_File is %s'%(env['pf_file'])) 102 | rules = ["[CLIENTS DROP]", 103 | "+fa56bf61-90da-11e8-bf33-005056a12a82-1234567", 104 | "+12345678-90da-11e8-bf33-005056a12a82-1234567", 105 | "[SUBNETS DROP]", 106 | "+10.150.0.1", 107 | "[END]"] 108 | with open(env['pf_file'], 'w') as f: 109 | f.write('\n'.join(rules)) 110 | ''' 111 | return OPENVPN_PLUGIN_FUNC_SUCCESS 112 | 113 | def PLUGIN_CLIENT_CONNECT_V2(env): 114 | print("OPENVPN_PLUGIN_CLIENT_CONNECT_V2") 115 | print_dict(env) 116 | return OPENVPN_PLUGIN_FUNC_SUCCESS 117 | 118 | def PLUGIN_TLS_FINAL(env): 119 | print("OPENVPN_PLUGIN_TLS_FINAL\n") 120 | print_dict(env) 121 | return OPENVPN_PLUGIN_FUNC_SUCCESS 122 | 123 | def PLUGIN_ENABLE_PF(env): 124 | """ 125 | Method OPENVPN_ENABLE_PF used for enable personal firewall rules for each client. 126 | If OPENVPN_ENABLE_PF is enabled, each called plug-ins part checks pf_file environment file 127 | """ 128 | print("OPENVPN_PLUGIN_ENABLE_PF") 129 | print_dict(env) 130 | return OPENVPN_PLUGIN_FUNC_SUCCESS 131 | 132 | def PLUGIN_ROUTE_PREDOWN(env): 133 | print("OPENVPN_PLUGIN_ROUTE_PREDOWN") 134 | print_dict(env) 135 | return OPENVPN_PLUGIN_FUNC_SUCCESS 136 | 137 | def PLUGIN_N(env): 138 | print("OPENVPN_PLUGIN_N") 139 | print_dict(env) 140 | return OPENVPN_PLUGIN_FUNC_SUCCESS 141 | 142 | def OPENVPN_UNKNOWN_PLUGIN_TYPE(env): 143 | print("OPENVPN_UNKNOWN_PLUGIN_TYPE") 144 | print_dict(env) 145 | return OPENVPN_PLUGIN_FUNC_SUCCESS 146 | 147 | -------------------------------------------------------------------------------- /embedpython.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2020 Yandex, LLC 3 | * 4 | * This program is free software; you can redistribute it and/or modify it 5 | * under the terms of the GNU General Public License version 2 as published by 6 | * the Free Software Foundation. 7 | * 8 | * This program is distributed in the hope that it will be useful, but WITHOUT 9 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 10 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 11 | * more details. 12 | * 13 | * You should have received a copy of the GNU General Public License along with 14 | * this program; if not, write to the Free Software Foundation, Inc., 51 15 | * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 16 | */ 17 | 18 | #include 19 | 20 | #include "embedpython.h" 21 | 22 | 23 | int python_init(struct Python_Inventory* inv) 24 | { 25 | inv->initialized = false; 26 | wchar_t* program_name = Py_DecodeLocale(inv->program_name, NULL); 27 | Py_SetProgramName(program_name); 28 | Py_Initialize(); 29 | 30 | PyObject *sys = PyImport_ImportModule("sys"); 31 | PyObject *path = PyObject_GetAttrString(sys, "path"); 32 | PyList_Append(path, PyUnicode_FromString(inv->script_dir)); 33 | Py_XDECREF(sys); 34 | Py_XDECREF(path); 35 | 36 | PyObject *tb_module_name = PyUnicode_FromString("traceback"); 37 | if (NULL == PyImport_Import(tb_module_name)) { 38 | PyErr_Print(); 39 | inv->last_error = PYTHON_IMPORT_FAIL; 40 | return PYTHON_IMPORT_FAIL; 41 | } 42 | Py_XDECREF(tb_module_name); 43 | 44 | PyObject *module_name = PyUnicode_FromString(inv->script_module); 45 | if (module_name == NULL) { 46 | inv->last_error = PYTHON_GENERIC_ERROR; 47 | return PYTHON_GENERIC_ERROR; 48 | } 49 | 50 | inv->module = PyImport_Import(module_name); 51 | if (inv->module == NULL) { 52 | PyErr_Print(); 53 | inv->last_error = PYTHON_IMPORT_FAIL; 54 | return PYTHON_IMPORT_FAIL; 55 | } 56 | 57 | PyObject* script_globals = PyObject_GetAttrString(inv->module, GLOBALS_KEY); 58 | if (NULL == script_globals) { 59 | inv->globals = PyDict_New(); 60 | PyObject_SetAttrString(inv->module, GLOBALS_KEY, inv->globals); 61 | } else { 62 | inv->globals = script_globals; 63 | } 64 | 65 | inv->initialized = true; 66 | inv->last_error = PYTHON_OK; 67 | return PYTHON_OK; 68 | } 69 | 70 | int python_uninit(struct Python_Inventory* inv) 71 | { 72 | inv->initialized = false; 73 | 74 | Py_XDECREF(inv->globals); 75 | Py_XDECREF(inv->module); 76 | 77 | Py_Finalize(); 78 | 79 | inv->last_error = PYTHON_OK; 80 | return PYTHON_OK; 81 | } 82 | 83 | // Accepts a list of PyObject*'s which can be created 84 | // from char* and int/long with python_str and python_int respectively. 85 | PyObject* python_call_function(struct Python_Inventory* inv, const char* name, int numargs, ...) 86 | { 87 | if (!Py_IsInitialized()) { 88 | inv->last_error = PYTHON_NOT_INITIALIZED; 89 | return NULL; 90 | } 91 | 92 | PyObject *function = PyObject_GetAttrString(inv->module, name); 93 | if (function == NULL) { 94 | inv->last_error = PYTHON_UNKNOWN_FUNCTION; 95 | return NULL; 96 | } 97 | 98 | PyObject* args; 99 | if (0 == numargs) { 100 | args = PyTuple_New(0); 101 | } else { 102 | args = PyTuple_New(numargs); 103 | va_list arguments; 104 | va_start(arguments, numargs); 105 | for (int i = 0; i < numargs; i++) { 106 | PyTuple_SetItem(args, i, va_arg(arguments, PyObject*)); 107 | } 108 | va_end(arguments); 109 | } 110 | // Note: has to be cast and Py_DECREF'd later with one of the unboxers 111 | // (python_from_int, python_from_str). 112 | PyObject* res = PyObject_CallObject(function, args); 113 | 114 | Py_XDECREF(args); Py_XDECREF(function); 115 | PyObject* err = PyErr_Occurred(); 116 | if (err != NULL) { 117 | PyErr_Print(); 118 | inv->last_error = PYTHON_RUNTIME_EXCEPTION; 119 | Py_XDECREF(res); 120 | Py_XDECREF(err); 121 | return NULL; 122 | } 123 | inv->last_error = PYTHON_OK; 124 | return res; 125 | } 126 | 127 | bool python_is_function_defined(struct Python_Inventory* inv, const char* name) 128 | { 129 | if (!Py_IsInitialized()) { 130 | inv->last_error = PYTHON_NOT_INITIALIZED; 131 | return NULL; 132 | } 133 | 134 | if (!PyObject_HasAttrString(inv->module, name)) { 135 | return false; 136 | } 137 | PyObject* func = PyObject_GetAttrString(inv->module, name); 138 | return PyCallable_Check(func); 139 | } 140 | 141 | PyObject* python_str(const char* arg) 142 | { 143 | PyObject* res = PyUnicode_FromString(arg); 144 | assert(NULL != res); 145 | return res; 146 | } 147 | 148 | PyObject* python_int(const long long int arg) 149 | { 150 | PyObject* res = PyLong_FromLongLong(arg); 151 | assert(NULL != res); 152 | return res; 153 | } 154 | 155 | long long int python_from_int(PyObject* arg) 156 | { 157 | long long int result = PyLong_AsLongLong(arg); 158 | Py_XDECREF(arg); 159 | return result; 160 | } 161 | 162 | const char* python_from_str(PyObject* arg) 163 | { 164 | const char* result = PyUnicode_AsUTF8(arg); 165 | Py_XDECREF(arg); 166 | return result; 167 | } 168 | 169 | const char* python_error(const enum PYTHON_Result res) 170 | { 171 | switch (res) { 172 | case PYTHON_IMPORT_FAIL: 173 | return "Python importing error"; 174 | case PYTHON_INIT_FAIL: 175 | return "Python interpreter initialization failed"; 176 | case PYTHON_NOT_INITIALIZED: 177 | return "Python interpterer was not initialized properly"; 178 | case PYTHON_UNINIT_FAIL: 179 | return "Python interpreter unitialization failed"; 180 | case PYTHON_UNKNOWN_FUNCTION: 181 | return "unknown function name"; 182 | case PYTHON_RUNTIME_EXCEPTION: 183 | return "runtime Python exception raised"; 184 | case PYTHON_OK: 185 | return "no error"; 186 | case PYTHON_GENERIC_ERROR: 187 | default: 188 | return "generic PYTHON error"; 189 | } 190 | } 191 | -------------------------------------------------------------------------------- /openvpn-plugin-python.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 Kaltashkin Eugene 3 | * Copyright (C) 2019 Boris Lytochkin 4 | * Copyright (C) 2020 Yandex, LLC 5 | * 6 | * This program is free software; you can redistribute it and/or modify it 7 | * under the terms of the GNU General Public License version 2 as published by 8 | * the Free Software Foundation. 9 | * 10 | * This program is distributed in the hope that it will be useful, but WITHOUT 11 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 12 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 13 | * more details. 14 | * 15 | * You should have received a copy of the GNU General Public License along with 16 | * this program; if not, write to the Free Software Foundation, Inc., 51 17 | * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 18 | */ 19 | 20 | /* 21 | * This file implements a python interpreter to handle various events though 22 | * OpenVPN plugin calls. 23 | * 24 | * See the README file for build instructions. 25 | */ 26 | 27 | #define __EXTENSIONS__ 28 | 29 | #include 30 | #include 31 | #include 32 | #include 33 | #include 34 | 35 | #include "embedpython.h" 36 | 37 | #include 38 | 39 | #define PLUGIN_NAME "python" 40 | 41 | struct hook_item { 42 | int hook_id; 43 | char *hook_name; 44 | }; 45 | 46 | #define OPENVPN_HOOK(a) { OPENVPN_##a, #a } 47 | 48 | const struct hook_item plugin_hooks[] = { 49 | OPENVPN_HOOK(PLUGIN_UP), 50 | OPENVPN_HOOK(PLUGIN_DOWN), 51 | OPENVPN_HOOK(PLUGIN_ROUTE_UP), 52 | OPENVPN_HOOK(PLUGIN_IPCHANGE), 53 | OPENVPN_HOOK(PLUGIN_TLS_VERIFY), 54 | OPENVPN_HOOK(PLUGIN_AUTH_USER_PASS_VERIFY), 55 | OPENVPN_HOOK(PLUGIN_CLIENT_CONNECT), 56 | OPENVPN_HOOK(PLUGIN_CLIENT_DISCONNECT), 57 | OPENVPN_HOOK(PLUGIN_LEARN_ADDRESS), 58 | OPENVPN_HOOK(PLUGIN_CLIENT_CONNECT_V2), 59 | OPENVPN_HOOK(PLUGIN_TLS_FINAL), 60 | OPENVPN_HOOK(PLUGIN_ENABLE_PF), 61 | OPENVPN_HOOK(PLUGIN_ROUTE_PREDOWN), 62 | }; 63 | 64 | /* Our context, where we keep our state. */ 65 | struct plugin_context { 66 | plugin_log_t log; 67 | char *config_param; 68 | 69 | char *plugin_func_names[OPENVPN_PLUGIN_N]; 70 | struct Python_Inventory inv; 71 | }; 72 | 73 | OPENVPN_EXPORT int 74 | openvpn_plugin_open_v3(const int v3structver, 75 | struct openvpn_plugin_args_open_in const *args, 76 | struct openvpn_plugin_args_open_return *ret) 77 | { 78 | struct plugin_context *context = NULL; 79 | 80 | /* Allocate our context */ 81 | context = (struct plugin_context *)calloc(1, sizeof(struct plugin_context)); 82 | if (!context) { 83 | return OPENVPN_PLUGIN_FUNC_ERROR; 84 | } 85 | struct Python_Inventory *inv = &context->inv; 86 | 87 | /* consistent logging */ 88 | plugin_log_t log = args->callbacks->plugin_log; 89 | context->log = log; 90 | 91 | /* Define plugin types which our script can serve */ 92 | ret->type_mask = 0; 93 | 94 | /* Save parameters for plugin from openvpn config */ 95 | if (args->argv[1]) 96 | context->config_param = strdup(args->argv[1]); 97 | 98 | log(PLOG_DEBUG, PLUGIN_NAME, "openvpn-plugin-" PLUGIN_NAME ": config_param=%s", context->config_param); 99 | 100 | /* Point the global context handle to our newly created context */ 101 | ret->handle = (void *)context; 102 | 103 | /* Init Python interpreter */ 104 | inv->script_dir = context->config_param; 105 | inv->program_name = "openvpn-plugin-" PLUGIN_NAME; 106 | inv->script_module = "plugin"; 107 | enum PYTHON_Result load_res = python_init(inv); 108 | 109 | if (load_res != PYTHON_OK) { 110 | log(PLOG_ERR, PLUGIN_NAME, "Error loading the Python plugin (see stderr)"); 111 | return OPENVPN_PLUGIN_FUNC_ERROR; 112 | } 113 | 114 | /* Scan module for methods available and register them */ 115 | for (int hook_num = 0; hook_num < OPENVPN_PLUGIN_N; hook_num++) { 116 | log(PLOG_DEBUG, PLUGIN_NAME, "Looking for function %s in plugin.py", plugin_hooks[hook_num].hook_name); 117 | if (python_is_function_defined(inv, plugin_hooks[hook_num].hook_name)) { 118 | context->plugin_func_names[plugin_hooks[hook_num].hook_id] = plugin_hooks[hook_num].hook_name; 119 | ret->type_mask |= OPENVPN_PLUGIN_MASK(plugin_hooks[hook_num].hook_id); 120 | log(PLOG_DEBUG, PLUGIN_NAME, "hook %s is enabled", plugin_hooks[hook_num].hook_name); 121 | } else { 122 | context->plugin_func_names[plugin_hooks[hook_num].hook_id] = NULL; 123 | log(PLOG_DEBUG, PLUGIN_NAME, "hook %s is disabled", plugin_hooks[hook_num].hook_name); 124 | } 125 | } 126 | 127 | return OPENVPN_PLUGIN_FUNC_SUCCESS; 128 | } 129 | 130 | int split_string_by_char(const char* splittee, char sep, char** left, char** right) 131 | { 132 | size_t len = strnlen(splittee, 1024); 133 | size_t i; 134 | 135 | for (i = 0; i < len; i++) { 136 | if (splittee[i] == sep) { 137 | *left = strndup(splittee, i); 138 | *right = strndup(splittee + i + 1, len - i - 1); 139 | break; 140 | } 141 | } 142 | if (i == len) { 143 | return -1; 144 | } 145 | return i; 146 | } 147 | 148 | PyObject* env_to_dict(struct openvpn_plugin_args_func_in const *args) 149 | { 150 | /* Build a Dict out of envp */ 151 | struct plugin_context *context = (struct plugin_context *)args->handle; 152 | plugin_log_t log = context->log; 153 | PyObject *env_dict = PyDict_New(); 154 | PyObject *d_key, *d_value; 155 | 156 | for (const char **env_item = args->envp; *env_item != NULL; env_item++) { 157 | char *env_key, *env_value; 158 | ssize_t res = split_string_by_char(*env_item, '=', &env_key, &env_value); 159 | 160 | if (res < 0) { 161 | log(PLOG_ERR, PLUGIN_NAME, "Environment variable parse error, = is not found in '%s'", *env_item); 162 | continue; 163 | } 164 | d_key = PyUnicode_FromString(env_key); 165 | d_value = PyUnicode_FromString(env_value); 166 | PyDict_SetItem(env_dict, d_key, d_value); 167 | Py_DECREF(d_key); 168 | Py_DECREF(d_value); 169 | 170 | // Python3's Objects/unicodeobject.c PU_FS does not steal the pointer 171 | free(env_key); 172 | free(env_value); 173 | } 174 | return env_dict; 175 | } 176 | 177 | PyObject* argv_to_list(struct openvpn_plugin_args_func_in const *args) 178 | { 179 | /* 180 | struct plugin_context *context = (struct plugin_context *)args->handle; 181 | plugin_log_t log = context->log; 182 | */ 183 | 184 | PyObject *argList = PyList_New(0); 185 | PyObject *dItem; 186 | 187 | for (const char **arg_item = args->argv; *arg_item != NULL; arg_item++) { 188 | dItem = PyUnicode_FromString(*arg_item); 189 | PyList_Append(argList, dItem); 190 | } 191 | return argList; 192 | } 193 | 194 | #define NS_IN_S 1000000000 195 | #define NS_IN_MS 1000000 196 | 197 | int get_duration_time(struct timespec *start, 198 | struct timespec *stop) 199 | { 200 | stop->tv_nsec += NS_IN_S * (stop->tv_sec - start->tv_sec); 201 | return (stop->tv_nsec - start->tv_nsec) / NS_IN_MS; 202 | } 203 | 204 | OPENVPN_EXPORT int 205 | openvpn_plugin_func_v3(const int version, 206 | struct openvpn_plugin_args_func_in const *args, 207 | struct openvpn_plugin_args_func_return *retptr) 208 | { 209 | struct timespec start_time, stop_time; 210 | clock_gettime(CLOCK_MONOTONIC, &start_time); 211 | 212 | struct plugin_context *context = (struct plugin_context *)args->handle; 213 | plugin_log_t log = context->log; 214 | 215 | /* Python function name for calling */ 216 | int PyReturn = OPENVPN_PLUGIN_FUNC_ERROR; 217 | 218 | char *func_name = context->plugin_func_names[args->type]; 219 | if (func_name == NULL) { 220 | log(PLOG_DEBUG, PLUGIN_NAME, "function was not registered"); 221 | return OPENVPN_PLUGIN_FUNC_ERROR; 222 | } 223 | 224 | PyObject *envarg = env_to_dict(args); 225 | PyDict_SetItemString(envarg, "__ARGV", argv_to_list(args)); 226 | int retval = python_from_int( 227 | python_call_function(&context->inv, func_name, 1, envarg)); 228 | Py_DECREF(envarg); 229 | if (context->inv.last_error == PYTHON_OK) { 230 | log(PLOG_DEBUG, PLUGIN_NAME, "Result of call: %ld", retval); 231 | switch (retval) { 232 | case 0: 233 | PyReturn = OPENVPN_PLUGIN_FUNC_SUCCESS; 234 | break; 235 | case 1: 236 | PyReturn = OPENVPN_PLUGIN_FUNC_ERROR; 237 | break; 238 | case 2: 239 | PyReturn = OPENVPN_PLUGIN_FUNC_DEFERRED; 240 | break; 241 | default: 242 | PyReturn = OPENVPN_PLUGIN_FUNC_ERROR; 243 | } 244 | } else { 245 | PyErr_Print(); 246 | log(PLOG_ERR, PLUGIN_NAME, python_error(retval)); 247 | log(PLOG_DEBUG, PLUGIN_NAME, "Call failed"); 248 | return OPENVPN_PLUGIN_FUNC_ERROR; 249 | } 250 | 251 | clock_gettime(CLOCK_MONOTONIC, &stop_time); 252 | log(PLOG_NOTE, PLUGIN_NAME, "function %s elapsed %d ms", func_name, get_duration_time(&start_time, &stop_time)); 253 | 254 | return PyReturn; 255 | } 256 | 257 | OPENVPN_EXPORT void 258 | openvpn_plugin_close_v1(openvpn_plugin_handle_t handle) 259 | { 260 | struct plugin_context *context = (struct plugin_context *)handle; 261 | python_uninit(&context->inv); 262 | free(context); 263 | } 264 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | (C) YANDEX LLC, 2020 2 | 3 | GNU GENERAL PUBLIC LICENSE 4 | Version 2, June 1991 5 | 6 | Copyright (C) 1989, 1991 Free Software Foundation, Inc., 7 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 8 | Everyone is permitted to copy and distribute verbatim copies 9 | of this license document, but changing it is not allowed. 10 | 11 | Preamble 12 | 13 | The licenses for most software are designed to take away your 14 | freedom to share and change it. By contrast, the GNU General Public 15 | License is intended to guarantee your freedom to share and change free 16 | software--to make sure the software is free for all its users. This 17 | General Public License applies to most of the Free Software 18 | Foundation's software and to any other program whose authors commit to 19 | using it. (Some other Free Software Foundation software is covered by 20 | the GNU Lesser General Public License instead.) You can apply it to 21 | your programs, too. 22 | 23 | When we speak of free software, we are referring to freedom, not 24 | price. Our General Public Licenses are designed to make sure that you 25 | have the freedom to distribute copies of free software (and charge for 26 | this service if you wish), that you receive source code or can get it 27 | if you want it, that you can change the software or use pieces of it 28 | in new free programs; and that you know you can do these things. 29 | 30 | To protect your rights, we need to make restrictions that forbid 31 | anyone to deny you these rights or to ask you to surrender the rights. 32 | These restrictions translate to certain responsibilities for you if you 33 | distribute copies of the software, or if you modify it. 34 | 35 | For example, if you distribute copies of such a program, whether 36 | gratis or for a fee, you must give the recipients all the rights that 37 | you have. You must make sure that they, too, receive or can get the 38 | source code. And you must show them these terms so they know their 39 | rights. 40 | 41 | We protect your rights with two steps: (1) copyright the software, and 42 | (2) offer you this license which gives you legal permission to copy, 43 | distribute and/or modify the software. 44 | 45 | Also, for each author's protection and ours, we want to make certain 46 | that everyone understands that there is no warranty for this free 47 | software. If the software is modified by someone else and passed on, we 48 | want its recipients to know that what they have is not the original, so 49 | that any problems introduced by others will not reflect on the original 50 | authors' reputations. 51 | 52 | Finally, any free program is threatened constantly by software 53 | patents. We wish to avoid the danger that redistributors of a free 54 | program will individually obtain patent licenses, in effect making the 55 | program proprietary. To prevent this, we have made it clear that any 56 | patent must be licensed for everyone's free use or not licensed at all. 57 | 58 | The precise terms and conditions for copying, distribution and 59 | modification follow. 60 | 61 | GNU GENERAL PUBLIC LICENSE 62 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 63 | 64 | 0. This License applies to any program or other work which contains 65 | a notice placed by the copyright holder saying it may be distributed 66 | under the terms of this General Public License. The "Program", below, 67 | refers to any such program or work, and a "work based on the Program" 68 | means either the Program or any derivative work under copyright law: 69 | that is to say, a work containing the Program or a portion of it, 70 | either verbatim or with modifications and/or translated into another 71 | language. (Hereinafter, translation is included without limitation in 72 | the term "modification".) Each licensee is addressed as "you". 73 | 74 | Activities other than copying, distribution and modification are not 75 | covered by this License; they are outside its scope. The act of 76 | running the Program is not restricted, and the output from the Program 77 | is covered only if its contents constitute a work based on the 78 | Program (independent of having been made by running the Program). 79 | Whether that is true depends on what the Program does. 80 | 81 | 1. You may copy and distribute verbatim copies of the Program's 82 | source code as you receive it, in any medium, provided that you 83 | conspicuously and appropriately publish on each copy an appropriate 84 | copyright notice and disclaimer of warranty; keep intact all the 85 | notices that refer to this License and to the absence of any warranty; 86 | and give any other recipients of the Program a copy of this License 87 | along with the Program. 88 | 89 | You may charge a fee for the physical act of transferring a copy, and 90 | you may at your option offer warranty protection in exchange for a fee. 91 | 92 | 2. You may modify your copy or copies of the Program or any portion 93 | of it, thus forming a work based on the Program, and copy and 94 | distribute such modifications or work under the terms of Section 1 95 | above, provided that you also meet all of these conditions: 96 | 97 | a) You must cause the modified files to carry prominent notices 98 | stating that you changed the files and the date of any change. 99 | 100 | b) You must cause any work that you distribute or publish, that in 101 | whole or in part contains or is derived from the Program or any 102 | part thereof, to be licensed as a whole at no charge to all third 103 | parties under the terms of this License. 104 | 105 | c) If the modified program normally reads commands interactively 106 | when run, you must cause it, when started running for such 107 | interactive use in the most ordinary way, to print or display an 108 | announcement including an appropriate copyright notice and a 109 | notice that there is no warranty (or else, saying that you provide 110 | a warranty) and that users may redistribute the program under 111 | these conditions, and telling the user how to view a copy of this 112 | License. (Exception: if the Program itself is interactive but 113 | does not normally print such an announcement, your work based on 114 | the Program is not required to print an announcement.) 115 | 116 | These requirements apply to the modified work as a whole. If 117 | identifiable sections of that work are not derived from the Program, 118 | and can be reasonably considered independent and separate works in 119 | themselves, then this License, and its terms, do not apply to those 120 | sections when you distribute them as separate works. But when you 121 | distribute the same sections as part of a whole which is a work based 122 | on the Program, the distribution of the whole must be on the terms of 123 | this License, whose permissions for other licensees extend to the 124 | entire whole, and thus to each and every part regardless of who wrote it. 125 | 126 | Thus, it is not the intent of this section to claim rights or contest 127 | your rights to work written entirely by you; rather, the intent is to 128 | exercise the right to control the distribution of derivative or 129 | collective works based on the Program. 130 | 131 | In addition, mere aggregation of another work not based on the Program 132 | with the Program (or with a work based on the Program) on a volume of 133 | a storage or distribution medium does not bring the other work under 134 | the scope of this License. 135 | 136 | 3. You may copy and distribute the Program (or a work based on it, 137 | under Section 2) in object code or executable form under the terms of 138 | Sections 1 and 2 above provided that you also do one of the following: 139 | 140 | a) Accompany it with the complete corresponding machine-readable 141 | source code, which must be distributed under the terms of Sections 142 | 1 and 2 above on a medium customarily used for software interchange; or, 143 | 144 | b) Accompany it with a written offer, valid for at least three 145 | years, to give any third party, for a charge no more than your 146 | cost of physically performing source distribution, a complete 147 | machine-readable copy of the corresponding source code, to be 148 | distributed under the terms of Sections 1 and 2 above on a medium 149 | customarily used for software interchange; or, 150 | 151 | c) Accompany it with the information you received as to the offer 152 | to distribute corresponding source code. (This alternative is 153 | allowed only for noncommercial distribution and only if you 154 | received the program in object code or executable form with such 155 | an offer, in accord with Subsection b above.) 156 | 157 | The source code for a work means the preferred form of the work for 158 | making modifications to it. For an executable work, complete source 159 | code means all the source code for all modules it contains, plus any 160 | associated interface definition files, plus the scripts used to 161 | control compilation and installation of the executable. However, as a 162 | special exception, the source code distributed need not include 163 | anything that is normally distributed (in either source or binary 164 | form) with the major components (compiler, kernel, and so on) of the 165 | operating system on which the executable runs, unless that component 166 | itself accompanies the executable. 167 | 168 | If distribution of executable or object code is made by offering 169 | access to copy from a designated place, then offering equivalent 170 | access to copy the source code from the same place counts as 171 | distribution of the source code, even though third parties are not 172 | compelled to copy the source along with the object code. 173 | 174 | 4. You may not copy, modify, sublicense, or distribute the Program 175 | except as expressly provided under this License. Any attempt 176 | otherwise to copy, modify, sublicense or distribute the Program is 177 | void, and will automatically terminate your rights under this License. 178 | However, parties who have received copies, or rights, from you under 179 | this License will not have their licenses terminated so long as such 180 | parties remain in full compliance. 181 | 182 | 5. You are not required to accept this License, since you have not 183 | signed it. However, nothing else grants you permission to modify or 184 | distribute the Program or its derivative works. These actions are 185 | prohibited by law if you do not accept this License. Therefore, by 186 | modifying or distributing the Program (or any work based on the 187 | Program), you indicate your acceptance of this License to do so, and 188 | all its terms and conditions for copying, distributing or modifying 189 | the Program or works based on it. 190 | 191 | 6. Each time you redistribute the Program (or any work based on the 192 | Program), the recipient automatically receives a license from the 193 | original licensor to copy, distribute or modify the Program subject to 194 | these terms and conditions. You may not impose any further 195 | restrictions on the recipients' exercise of the rights granted herein. 196 | You are not responsible for enforcing compliance by third parties to 197 | this License. 198 | 199 | 7. If, as a consequence of a court judgment or allegation of patent 200 | infringement or for any other reason (not limited to patent issues), 201 | conditions are imposed on you (whether by court order, agreement or 202 | otherwise) that contradict the conditions of this License, they do not 203 | excuse you from the conditions of this License. If you cannot 204 | distribute so as to satisfy simultaneously your obligations under this 205 | License and any other pertinent obligations, then as a consequence you 206 | may not distribute the Program at all. For example, if a patent 207 | license would not permit royalty-free redistribution of the Program by 208 | all those who receive copies directly or indirectly through you, then 209 | the only way you could satisfy both it and this License would be to 210 | refrain entirely from distribution of the Program. 211 | 212 | If any portion of this section is held invalid or unenforceable under 213 | any particular circumstance, the balance of the section is intended to 214 | apply and the section as a whole is intended to apply in other 215 | circumstances. 216 | 217 | It is not the purpose of this section to induce you to infringe any 218 | patents or other property right claims or to contest validity of any 219 | such claims; this section has the sole purpose of protecting the 220 | integrity of the free software distribution system, which is 221 | implemented by public license practices. Many people have made 222 | generous contributions to the wide range of software distributed 223 | through that system in reliance on consistent application of that 224 | system; it is up to the author/donor to decide if he or she is willing 225 | to distribute software through any other system and a licensee cannot 226 | impose that choice. 227 | 228 | This section is intended to make thoroughly clear what is believed to 229 | be a consequence of the rest of this License. 230 | 231 | 8. If the distribution and/or use of the Program is restricted in 232 | certain countries either by patents or by copyrighted interfaces, the 233 | original copyright holder who places the Program under this License 234 | may add an explicit geographical distribution limitation excluding 235 | those countries, so that distribution is permitted only in or among 236 | countries not thus excluded. In such case, this License incorporates 237 | the limitation as if written in the body of this License. 238 | 239 | 9. The Free Software Foundation may publish revised and/or new versions 240 | of the General Public License from time to time. Such new versions will 241 | be similar in spirit to the present version, but may differ in detail to 242 | address new problems or concerns. 243 | 244 | Each version is given a distinguishing version number. If the Program 245 | specifies a version number of this License which applies to it and "any 246 | later version", you have the option of following the terms and conditions 247 | either of that version or of any later version published by the Free 248 | Software Foundation. If the Program does not specify a version number of 249 | this License, you may choose any version ever published by the Free Software 250 | Foundation. 251 | 252 | 10. If you wish to incorporate parts of the Program into other free 253 | programs whose distribution conditions are different, write to the author 254 | to ask for permission. For software which is copyrighted by the Free 255 | Software Foundation, write to the Free Software Foundation; we sometimes 256 | make exceptions for this. Our decision will be guided by the two goals 257 | of preserving the free status of all derivatives of our free software and 258 | of promoting the sharing and reuse of software generally. 259 | 260 | NO WARRANTY 261 | 262 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY 263 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN 264 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES 265 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED 266 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 267 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS 268 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE 269 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, 270 | REPAIR OR CORRECTION. 271 | 272 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 273 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR 274 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, 275 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING 276 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED 277 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY 278 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER 279 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE 280 | POSSIBILITY OF SUCH DAMAGES. 281 | 282 | END OF TERMS AND CONDITIONS 283 | 284 | How to Apply These Terms to Your New Programs 285 | 286 | If you develop a new program, and you want it to be of the greatest 287 | possible use to the public, the best way to achieve this is to make it 288 | free software which everyone can redistribute and change under these terms. 289 | 290 | To do so, attach the following notices to the program. It is safest 291 | to attach them to the start of each source file to most effectively 292 | convey the exclusion of warranty; and each file should have at least 293 | the "copyright" line and a pointer to where the full notice is found. 294 | 295 | 296 | Copyright (C) 297 | 298 | This program is free software; you can redistribute it and/or modify 299 | it under the terms of the GNU General Public License as published by 300 | the Free Software Foundation; either version 2 of the License, or 301 | (at your option) any later version. 302 | 303 | This program is distributed in the hope that it will be useful, 304 | but WITHOUT ANY WARRANTY; without even the implied warranty of 305 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 306 | GNU General Public License for more details. 307 | 308 | You should have received a copy of the GNU General Public License along 309 | with this program; if not, write to the Free Software Foundation, Inc., 310 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 311 | 312 | Also add information on how to contact you by electronic and paper mail. 313 | 314 | If the program is interactive, make it output a short notice like this 315 | when it starts in an interactive mode: 316 | 317 | Gnomovision version 69, Copyright (C) year name of author 318 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 319 | This is free software, and you are welcome to redistribute it 320 | under certain conditions; type `show c' for details. 321 | 322 | The hypothetical commands `show w' and `show c' should show the appropriate 323 | parts of the General Public License. Of course, the commands you use may 324 | be called something other than `show w' and `show c'; they could even be 325 | mouse-clicks or menu items--whatever suits your program. 326 | 327 | You should also get your employer (if you work as a programmer) or your 328 | school, if any, to sign a "copyright disclaimer" for the program, if 329 | necessary. Here is a sample; alter the names: 330 | 331 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program 332 | `Gnomovision' (which makes passes at compilers) written by James Hacker. 333 | 334 | , 1 April 1989 335 | Ty Coon, President of Vice 336 | 337 | This General Public License does not permit incorporating your program into 338 | proprietary programs. If your program is a subroutine library, you may 339 | consider it more useful to permit linking proprietary applications with the 340 | library. If this is what you want to do, use the GNU Lesser General 341 | Public License instead of this License. 342 | --------------------------------------------------------------------------------