describe tests
[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.2 2005/05/10 06:48:16 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_init(struct pluginfuncs *funcs)
20 {
21 if (!funcs)
22 return 0;
23
24 p = funcs;
25 return 1;
26 }
27
28 int plugin_control(struct param_control *data)
29 {
30 sessionidt session;
31 sessiont *s = 0;
32 char *end;
33 char *reason;
34
35 if (data->argc < 1)
36 return PLUGIN_RET_OK;
37
38 if (strcmp(data->argv[0], "drop") && strcmp(data->argv[0], "kill"))
39 return PLUGIN_RET_OK; // not for us
40
41 if (!data->iam_master)
42 return PLUGIN_RET_NOTMASTER;
43
44 if (data->argc < 2 || data->argc > 3)
45 {
46 data->response = NSCTL_RES_ERR;
47 data->additional = "requires username or session id and optional reason";
48 return PLUGIN_RET_STOP;
49 }
50
51 if (!(session = strtol(data->argv[1], &end, 10)) || *end)
52 session = p->get_session_by_username(data->argv[1]);
53
54 if (session)
55 s = p->get_session_by_id(session);
56
57 if (!s || !s->ip)
58 {
59 data->response = NSCTL_RES_ERR;
60 data->additional = "session not found";
61 return PLUGIN_RET_STOP;
62 }
63
64 if (data->argc > 2)
65 reason = data->argv[2];
66 else
67 reason = "Requested by administrator.";
68
69 if (data->argv[0][0] == 'd')
70 p->sessionshutdown(session, reason, 3, 0);
71 else
72 p->sessionkill(session, reason);
73
74 data->response = NSCTL_RES_OK;
75 data->additional = 0;
76
77 return PLUGIN_RET_STOP;
78 }