Added commands to add and remove a route.

This commit is contained in:
Nikos Mavrogiannopoulos
2013-10-29 11:07:03 +01:00
parent b6239749e9
commit e4d2e5803d
8 changed files with 132 additions and 3 deletions
+1 -1
View File
@@ -20,7 +20,7 @@ ocserv_SOURCES = main.c main-auth.c worker-vpn.c worker-auth.c tlslib.c \
vpn.h cookies.h tlslib.h http-parser/http_parser.h log.c tun.c tun.h \ vpn.h cookies.h tlslib.h http-parser/http_parser.h log.c tun.c tun.h \
config.c pam.c pam.h worker-resume.c worker.h main-resume.c main.h \ config.c pam.c pam.h worker-resume.c worker.h main-resume.c main.h \
worker-extras.c main-auth.h html.c html.h \ worker-extras.c main-auth.h html.c html.h \
main-user.c worker-misc.c setproctitle.h \ main-user.c worker-misc.c setproctitle.h route-add.c route-add.h \
setproctitle.c worker-privs.c plain.c plain.h common.h common.c \ setproctitle.c worker-privs.c plain.c plain.h common.h common.c \
sec-mod.c sec-mod.h script-list.h system.c system.h icmp-ping.c icmp-ping.h \ sec-mod.c sec-mod.h script-list.h system.c system.h icmp-ping.c icmp-ping.h \
str.c str.h gettime.h $(CCAN_SOURCES) str.c str.h gettime.h $(CCAN_SOURCES)
+6
View File
@@ -100,6 +100,8 @@ static struct cfg_options available_options[] = {
{ .name = "ipv6-prefix", .type = OPTION_NUMERIC, .mandatory = 0 }, { .name = "ipv6-prefix", .type = OPTION_NUMERIC, .mandatory = 0 },
{ .name = "ipv6-dns", .type = OPTION_STRING, .mandatory = 0 }, { .name = "ipv6-dns", .type = OPTION_STRING, .mandatory = 0 },
{ .name = "ipv6-nbns", .type = OPTION_STRING, .mandatory = 0 }, { .name = "ipv6-nbns", .type = OPTION_STRING, .mandatory = 0 },
{ .name = "route-add-cmd", .type = OPTION_STRING, .mandatory = 0 },
{ .name = "route-del-cmd", .type = OPTION_STRING, .mandatory = 0 },
{ .name = "config-per-user", .type = OPTION_STRING, .mandatory = 0 }, { .name = "config-per-user", .type = OPTION_STRING, .mandatory = 0 },
{ .name = "config-per-group", .type = OPTION_STRING, .mandatory = 0 }, { .name = "config-per-group", .type = OPTION_STRING, .mandatory = 0 },
}; };
@@ -361,6 +363,8 @@ unsigned prefix = 0;
READ_MULTI_LINE("route", config->network.routes, config->network.routes_size); READ_MULTI_LINE("route", config->network.routes, config->network.routes_size);
READ_STRING("route-add-cmd", config->route_add_cmd);
READ_STRING("route-del-cmd", config->route_del_cmd);
READ_STRING("config-per-user", config->per_user_dir); READ_STRING("config-per-user", config->per_user_dir);
READ_STRING("config-per-group", config->per_group_dir); READ_STRING("config-per-group", config->per_group_dir);
@@ -489,6 +493,8 @@ unsigned i;
DEL(config->xml_config_hash); DEL(config->xml_config_hash);
DEL(config->cert_hash); DEL(config->cert_hash);
#endif #endif
DEL(config->route_add_cmd);
DEL(config->route_del_cmd);
DEL(config->per_user_dir); DEL(config->per_user_dir);
DEL(config->per_group_dir); DEL(config->per_group_dir);
DEL(config->socket_file_prefix); DEL(config->socket_file_prefix);
+1 -1
View File
@@ -2,7 +2,7 @@
* *
* DO NOT EDIT THIS FILE (ocserv-args.c) * DO NOT EDIT THIS FILE (ocserv-args.c)
* *
* It has been AutoGen-ed October 29, 2013 at 10:00:37 AM by AutoGen 5.17 * It has been AutoGen-ed October 29, 2013 at 11:02:09 AM by AutoGen 5.17
* From the definitions ocserv-args.def * From the definitions ocserv-args.def
* and the template file options * and the template file options
* *
+6
View File
@@ -270,6 +270,12 @@ route = 192.168.5.0/255.255.255.0
#config-per-user = /etc/ocserv/config-per-user/ #config-per-user = /etc/ocserv/config-per-user/
#config-per-group = /etc/ocserv/config-per-group/ #config-per-group = /etc/ocserv/config-per-group/
# The system command to use to setup a route. The following
# example is from linux systems. %R will be replaced with the
# route/mask and %D with the (tun) device.
#route-add-cmd = "ip route add %R dev %D"
#route-del-cmd = "ip route delete %R dev %D"
# #
# The following options are for (experimental) AnyConnect client # The following options are for (experimental) AnyConnect client
# compatibility. # compatibility.
+1 -1
View File
@@ -2,7 +2,7 @@
* *
* DO NOT EDIT THIS FILE (ocserv-args.h) * DO NOT EDIT THIS FILE (ocserv-args.h)
* *
* It has been AutoGen-ed October 29, 2013 at 10:00:36 AM by AutoGen 5.17 * It has been AutoGen-ed October 29, 2013 at 11:02:09 AM by AutoGen 5.17
* From the definitions ocserv-args.def * From the definitions ocserv-args.def
* and the template file options * and the template file options
* *
+102
View File
@@ -0,0 +1,102 @@
/*
* Copyright (C) 2013 Nikos Mavrogiannopoulos
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <config.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <route-add.h>
#include <main.h>
#include <common.h>
static
int replace_cmd(struct main_server_st* s, char cmd[_POSIX_PATH_MAX], const char* pattern,
const char* route, const char* dev)
{
int len = strlen(pattern);
unsigned i, j;
unsigned rlen = strlen(route);
unsigned dlen = strlen(dev);
if (len + rlen + dlen >= sizeof(cmd))
return ERR_MEM;
for (i=j=0;i<len;i++) {
if (pattern[i] == '%') {
if (pattern[i+1] == 'R') {
memcpy(&cmd[j], route, rlen);
j += rlen;
i++;
} else if (pattern[i+1] == 'D') {
memcpy(&cmd[j], dev, dlen);
j += dlen;
i++;
} else {
mslog(s, NULL, LOG_WARNING, "unknown token %%%c in cmd: %s", pattern[i+1], pattern);
return ERR_PARSING;
}
} else
cmd[j++] = pattern[i];
}
return 0;
}
static
int route_adddel(struct main_server_st* s, const char* pattern, const char* route, const char* dev)
{
int ret;
char cmd[_POSIX_PATH_MAX];
ret = replace_cmd(s, cmd, pattern, route, dev);
if (ret < 0)
return ret;
ret = system(cmd);
if (ret == -1) {
int e = errno;
mslog(s, NULL, LOG_WARNING, "failed to spawn cmd: %s: %s", cmd, strerror(e));
return ERR_EXEC;
}
if (!WIFEXITED(ret)) {
mslog(s, NULL, LOG_WARNING, "cmd: %s: exited abnormally", cmd);
return ERR_EXEC;
}
if (WEXITSTATUS(ret)) {
mslog(s, NULL, LOG_WARNING, "cmd: %s: exited with error %d", cmd, WEXITSTATUS(ret));
return ERR_EXEC;
}
return 0;
}
int route_add(struct main_server_st* s, const char* route, const char* dev)
{
return route_adddel(s, s->config->route_add_cmd, route, dev);
}
int route_del(struct main_server_st* s, const char* route, const char* dev)
{
return route_adddel(s, s->config->route_del_cmd, route, dev);
}
+10
View File
@@ -0,0 +1,10 @@
#ifndef ROUTE_ADD_H
# define ROUTE_ADD_H
#include <vpn.h>
#include <main.h>
int route_del(struct main_server_st* s, const char* route, const char* dev);
int route_add(struct main_server_st* s, const char* route, const char* dev);
#endif
+5
View File
@@ -49,6 +49,8 @@ extern int syslog_open;
#define ERR_MEM -6 #define ERR_MEM -6
#define ERR_READ_CONFIG -7 #define ERR_READ_CONFIG -7
#define ERR_NO_IP -8 #define ERR_NO_IP -8
#define ERR_PARSING -9
#define ERR_EXEC -10
typedef struct typedef struct
{ {
@@ -132,6 +134,9 @@ struct cfg_st {
unsigned output_buffer; unsigned output_buffer;
unsigned default_mtu; unsigned default_mtu;
char *route_add_cmd;
char *route_del_cmd;
char *connect_script; char *connect_script;
char *disconnect_script; char *disconnect_script;