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