summaryrefslogtreecommitdiff
path: root/accel-pptpd/extra
diff options
context:
space:
mode:
authorDmitry Kozlov <xeb@mail.ru>2010-10-25 16:21:25 +0400
committerDmitry Kozlov <xeb@mail.ru>2010-10-25 16:21:25 +0400
commit788923a042b90bc5ff1922b9b46648c4f4c0218c (patch)
tree70ef58296a98960975d07dbeda504999396802ef /accel-pptpd/extra
parent5d039c59acb58eb90f8d1256245b2c99aea129b0 (diff)
downloadaccel-ppp-xebd-788923a042b90bc5ff1922b9b46648c4f4c0218c.tar.gz
accel-ppp-xebd-788923a042b90bc5ff1922b9b46648c4f4c0218c.zip
implemented chap-secrets module which reads username,password and ip address from pppd compatible chap-secrets file
Diffstat (limited to 'accel-pptpd/extra')
-rw-r--r--accel-pptpd/extra/CMakeLists.txt3
-rw-r--r--accel-pptpd/extra/chap-secrets.c217
-rw-r--r--accel-pptpd/extra/pppd_compat.c17
-rw-r--r--accel-pptpd/extra/shaper_tbf.c24
4 files changed, 252 insertions, 9 deletions
diff --git a/accel-pptpd/extra/CMakeLists.txt b/accel-pptpd/extra/CMakeLists.txt
index c354c04..dc55840 100644
--- a/accel-pptpd/extra/CMakeLists.txt
+++ b/accel-pptpd/extra/CMakeLists.txt
@@ -1,8 +1,9 @@
ADD_LIBRARY(pppd_compat SHARED pppd_compat.c)
ADD_LIBRARY(ippool SHARED ippool.c)
ADD_LIBRARY(sigchld SHARED sigchld.c)
+ADD_LIBRARY(chap-secrets SHARED chap-secrets.c)
-INSTALL(TARGETS pppd_compat ippool sigchld
+INSTALL(TARGETS pppd_compat ippool sigchld chap-secrets
LIBRARY DESTINATION usr/lib/accel-pptp
)
diff --git a/accel-pptpd/extra/chap-secrets.c b/accel-pptpd/extra/chap-secrets.c
new file mode 100644
index 0000000..d60ceb7
--- /dev/null
+++ b/accel-pptpd/extra/chap-secrets.c
@@ -0,0 +1,217 @@
+#include <stdio.h>
+#include <errno.h>
+#include <string.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+
+#include "pwdb.h"
+#include "ipdb.h"
+#include "ppp.h"
+#include "events.h"
+#include "triton.h"
+#include "log.h"
+
+#include "memdebug.h"
+
+static const char *conf_chap_secrets = "/etc/ppp/chap-secrets";
+static in_addr_t conf_gw_ip_address = 0;
+
+static void *pd_key;
+static struct ipdb_t ipdb;
+
+struct cs_pd_t
+{
+ struct ppp_pd_t pd;
+ struct ipdb_item_t ip;
+ char *passwd;
+};
+
+static char *skip_word(char *ptr)
+{
+ for(; *ptr; ptr++)
+ if (*ptr == ' ' || *ptr == '\t' || *ptr == '\n')
+ break;
+ return ptr;
+}
+static char *skip_space(char *ptr)
+{
+ for(; *ptr; ptr++)
+ if (*ptr != ' ' && *ptr != '\t')
+ break;
+ return ptr;
+}
+static int split(char *buf, char **ptr)
+{
+ int i;
+
+ for (i = 0; i < 3; i++) {
+ buf = skip_word(buf);
+ if (!*buf)
+ return i;
+
+ *buf = 0;
+
+ buf = skip_space(buf + 1);
+ if (!*buf)
+ return i;
+
+ ptr[i] = buf;
+ }
+
+ buf = skip_word(buf);
+ //if (*buf == '\n')
+ *buf = 0;
+ //else if (*buf)
+ // return -1;
+
+ return i;
+}
+
+
+static struct cs_pd_t *create_pd(struct ppp_t *ppp, const char *username)
+{
+ FILE *f;
+ char *buf;
+ char *ptr[4];
+ int n;
+ struct cs_pd_t *pd;
+
+ if (!conf_chap_secrets)
+ return NULL;
+
+ f = fopen(conf_chap_secrets, "r");
+ if (!f) {
+ log_error("chap-secrets: open '%s': %s\n", conf_chap_secrets, strerror(errno));
+ return NULL;
+ }
+
+ buf = _malloc(4096);
+ if (!buf) {
+ log_emerg("chap-secrets: out of memory\n");
+ fclose(f);
+ return NULL;
+ }
+
+ while (fgets(buf, 4096, f)) {
+ n = split(buf, ptr);
+ if (n < 3)
+ continue;
+ if (!strcmp(buf, username))
+ goto found;
+ }
+
+out:
+ fclose(f);
+ _free(buf);
+ return NULL;
+
+found:
+ pd = _malloc(sizeof(*pd));
+ if (!pd) {
+ log_emerg("chap-secrets: out of memory\n");
+ goto out;
+ }
+
+ memset(pd, 0, sizeof(*pd));
+ pd->pd.key = &pd_key;
+ pd->passwd = _strdup(ptr[1]);
+ if (!pd->passwd) {
+ log_emerg("chap-secrets: out of memory\n");
+ _free(pd);
+ goto out;
+ }
+
+ pd->ip.addr = conf_gw_ip_address;
+ if (n == 3)
+ pd->ip.peer_addr = inet_addr(ptr[2]);
+ pd->ip.owner = &ipdb;
+
+ list_add_tail(&pd->pd.entry, &ppp->pd_list);
+
+ fclose(f);
+ _free(buf);
+
+ return pd;
+}
+
+static struct cs_pd_t *find_pd(struct ppp_t *ppp)
+{
+ struct ppp_pd_t *pd;
+
+ list_for_each_entry(pd, &ppp->pd_list, entry) {
+ if (pd->key == &pd_key) {
+ return container_of(pd, typeof(struct cs_pd_t), pd);
+ }
+ }
+
+ return NULL;
+}
+
+static void ev_ppp_finished(struct ppp_t *ppp)
+{
+ struct cs_pd_t *pd = find_pd(ppp);
+
+ if (!pd)
+ return;
+
+ list_del(&pd->pd.entry);
+ _free(pd->passwd);
+ _free(pd);
+}
+
+static struct ipdb_item_t *get_ip(struct ppp_t *ppp)
+{
+ struct cs_pd_t *pd;
+
+ if (!conf_gw_ip_address)
+ return NULL;
+
+ pd = find_pd(ppp);
+
+ if (!pd)
+ return NULL;
+
+ if (!pd->ip.addr)
+ return NULL;
+
+ return &pd->ip;
+}
+
+static char* get_passwd(struct pwdb_t *pwdb, struct ppp_t *ppp, const char *username)
+{
+ struct cs_pd_t *pd = find_pd(ppp);
+
+ if (!pd)
+ pd = create_pd(ppp, username);
+
+ if (!pd)
+ return NULL;
+
+ return _strdup(pd->passwd);
+}
+
+static struct ipdb_t ipdb = {
+ .get = get_ip,
+};
+
+static struct pwdb_t pwdb = {
+ .get_passwd = get_passwd,
+};
+
+static void __init init(void)
+{
+ const char *opt;
+
+ opt = conf_get_opt("chap-secrets", "chap-secrets");
+ if (opt)
+ conf_chap_secrets = opt;
+
+ opt = conf_get_opt("chap-secrets", "gw-ip-address");
+ if (opt)
+ conf_gw_ip_address = inet_addr(opt);
+
+ pwdb_register(&pwdb);
+ ipdb_register(&ipdb);
+
+ triton_event_register_handler(EV_PPP_FINISHED, (triton_event_func)ev_ppp_finished);
+}
diff --git a/accel-pptpd/extra/pppd_compat.c b/accel-pptpd/extra/pppd_compat.c
index db46dd4..d50c929 100644
--- a/accel-pptpd/extra/pppd_compat.c
+++ b/accel-pptpd/extra/pppd_compat.c
@@ -15,10 +15,13 @@
#include "events.h"
#include "ppp.h"
#include "log.h"
-#include "radius.h"
#include "utils.h"
#include "sigchld.h"
+#ifdef RADIUS
+#include "radius.h"
+#endif
+
#include "memdebug.h"
static char *conf_ip_up = "/etc/ppp/ip-up";
@@ -38,18 +41,22 @@ struct pppd_compat_pd_t
struct sigchld_handler_t ip_up_hnd;
struct sigchld_handler_t ip_change_hnd;
struct sigchld_handler_t ip_down_hnd;
+#ifdef RADIUS
int radattr_saved:1;
+#endif
int started:1;
int res;
int bytes_sent;
int bytes_rcvd;
};
-static void remove_radattr(struct ppp_t *ppp);
static struct pppd_compat_pd_t *find_pd(struct ppp_t *ppp);
static void fill_argv(char **argv, struct ppp_t *ppp, char *path);
static void fill_env(char **env, struct pppd_compat_pd_t *pd);
+#ifdef RADIUS
+static void remove_radattr(struct ppp_t *ppp);
static void write_radattr(struct ppp_t *ppp, struct rad_packet_t *pack, int save_old);
+#endif
static void ip_pre_up_handler(struct sigchld_handler_t *h, int status)
{
@@ -287,13 +294,16 @@ static void ev_ppp_finished(struct ppp_t *ppp)
pthread_mutex_unlock(&pd->ip_up_hnd.lock);
skip:
+#ifdef RADIUS
if (pd->radattr_saved)
remove_radattr(ppp);
+#endif
list_del(&pd->pd.entry);
_free(pd);
}
+#ifdef RADIUS
static void ev_radius_access_accept(struct ev_radius_t *ev)
{
struct pppd_compat_pd_t *pd = find_pd(ev->ppp);
@@ -432,6 +442,7 @@ static void write_radattr(struct ppp_t *ppp, struct rad_packet_t *pack, int save
if (save_old)
_free(fname2);
}
+#endif
static struct pppd_compat_pd_t *find_pd(struct ppp_t *ppp)
{
@@ -506,7 +517,9 @@ static void __init init(void)
triton_event_register_handler(EV_PPP_STARTED, (triton_event_func)ev_ppp_started);
triton_event_register_handler(EV_PPP_FINISHING, (triton_event_func)ev_ppp_finishing);
triton_event_register_handler(EV_PPP_FINISHED, (triton_event_func)ev_ppp_finished);
+#ifdef RADIUS
triton_event_register_handler(EV_RADIUS_ACCESS_ACCEPT, (triton_event_func)ev_radius_access_accept);
triton_event_register_handler(EV_RADIUS_COA, (triton_event_func)ev_radius_coa);
+#endif
}
diff --git a/accel-pptpd/extra/shaper_tbf.c b/accel-pptpd/extra/shaper_tbf.c
index cd1a88a..9c8e761 100644
--- a/accel-pptpd/extra/shaper_tbf.c
+++ b/accel-pptpd/extra/shaper_tbf.c
@@ -18,9 +18,13 @@
#include "triton.h"
#include "events.h"
-#include "radius.h"
#include "log.h"
#include "ppp.h"
+
+#ifdef RADIUS
+#include "radius.h"
+#endif
+
#include "memdebug.h"
#define TIME_UNITS_PER_SEC 1000000
@@ -481,6 +485,7 @@ out_err:
return -1;
}
+#ifdef RADIUS
static int parse_attr(struct rad_attr_t *attr)
{
if (attr->attr->type == ATTR_TYPE_STRING)
@@ -560,6 +565,7 @@ static void ev_radius_coa(struct ev_radius_t *ev)
}
}
}
+#endif
static void ev_ctrl_finished(struct ppp_t *ppp)
{
@@ -606,6 +612,7 @@ static int clock_init(void)
return 0;
}
+#ifdef RADIUS
static int parse_attr_opt(const char *opt)
{
struct rad_dict_attr_t *attr;
@@ -616,6 +623,7 @@ static int parse_attr_opt(const char *opt)
return atoi(opt);
}
+#endif
static void __init init(void)
{
@@ -624,6 +632,7 @@ static void __init init(void)
if (clock_init())
return;
+#ifdef RADIUS
opt = conf_get_opt("tbf", "attr");
if (opt) {
conf_attr_down = parse_attr_opt(opt);
@@ -637,6 +646,12 @@ static void __init init(void)
opt = conf_get_opt("tbf", "attr-up");
if (opt)
conf_attr_up = parse_attr_opt(opt);
+
+ if (conf_attr_up <= 0 || conf_attr_down <= 0) {
+ log_emerg("tbf: incorrect attribute(s), tbf disabled...\n");
+ return;
+ }
+#endif
opt = conf_get_opt("tbf", "burst-factor");
if (opt)
@@ -654,13 +669,10 @@ static void __init init(void)
if (opt && atoi(opt) > 0)
conf_verbose = 1;
- if (conf_attr_up <= 0 || conf_attr_down <= 0) {
- log_emerg("tbf: incorrect attribute(s), tbf disabled...\n");
- return;
- }
-
+#ifdef RADIUS
triton_event_register_handler(EV_RADIUS_ACCESS_ACCEPT, (triton_event_func)ev_radius_access_accept);
triton_event_register_handler(EV_RADIUS_COA, (triton_event_func)ev_radius_coa);
+#endif
triton_event_register_handler(EV_CTRL_FINISHED, (triton_event_func)ev_ctrl_finished);
}