Update changelog
[l2tpns.git] / sessionctl.c
1 #include <string.h>
2 #include <linux/rtnetlink.h>
3 #include <netinet/ip6.h>
4
5 #include "dhcp6.h"
6 #include "l2tpns.h"
7 #include "plugin.h"
8 #include "control.h"
9
10 /* session control */
11
12 int plugin_api_version = PLUGIN_API_VERSION;
13 static struct pluginfuncs *f = 0;
14
15 char *plugin_control_help[] = {
16 " drop USER|SID [REASON] Shutdown user session",
17 " kill USER|SID [REASON] Kill user session",
18 0
19 };
20
21 int plugin_control(struct param_control *data)
22 {
23 sessionidt session;
24 sessiont *s = 0;
25 char *end;
26 char *reason;
27
28 if (data->argc < 1)
29 return PLUGIN_RET_OK;
30
31 if (strcmp(data->argv[0], "drop") && strcmp(data->argv[0], "kill"))
32 return PLUGIN_RET_OK; // not for us
33
34 if (!data->iam_master)
35 return PLUGIN_RET_NOTMASTER;
36
37 if (data->argc < 2 || data->argc > 3)
38 {
39 data->response = NSCTL_RES_ERR;
40 data->additional = "requires username or session id and optional reason";
41 return PLUGIN_RET_STOP;
42 }
43
44 if (!(session = strtol(data->argv[1], &end, 10)) || *end)
45 session = f->get_session_by_username(data->argv[1]);
46
47 if (session)
48 s = f->get_session_by_id(session);
49
50 if (!s || !s->ip)
51 {
52 data->response = NSCTL_RES_ERR;
53 data->additional = "session not found";
54 return PLUGIN_RET_STOP;
55 }
56
57 if (data->argc > 2)
58 reason = data->argv[2];
59 else
60 reason = "Requested by administrator.";
61
62 if (data->argv[0][0] == 'd')
63 f->sessionshutdown(session, reason, CDN_ADMIN_DISC, TERM_ADMIN_RESET);
64 else
65 f->sessionkill(session, reason);
66
67 data->response = NSCTL_RES_OK;
68 data->additional = 0;
69
70 return PLUGIN_RET_STOP;
71 }
72
73 int plugin_init(struct pluginfuncs *funcs)
74 {
75 return ((f = funcs)) ? 1 : 0;
76 }