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