f0dee5d729e764bcc555c8998226e51ea310ccf3
[rahunas] / src / rh-xmlrpc-server.c
1 /**
2  * RahuNAS XML-RPC Server implementation
3  * Author: Neutron Soutmun <neo.neutron@gmail.com>
4  *         Suriya Soutmun <darksolar@gmail.com>
5  * Date:   2008-08-07
6  */
7
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include "rahunasd.h"
11 #include "rh-xmlrpc-server.h"
12 #include "rh-radius.h"
13 #include "rh-ipset.h"
14 #include "rh-utils.h"
15 #include "rh-task.h"
16
17 extern const char* termstring;
18
19 int do_startsession(GNetXmlRpcServer *server,
20                     const gchar *command,
21                                                                                 const gchar *param,
22                                                                                 gpointer user_data,
23                                                                                 gchar **reply_string) 
24 {
25   struct rahunas_map *map = (struct rahunas_map *)user_data;
26         struct rahunas_member *members = NULL;
27   unsigned char ethernet[ETH_ALEN] = {0,0,0,0,0,0};
28   struct task_req req;
29         gchar *ip = NULL;
30         gchar *username = NULL;
31         gchar *session_id = NULL;
32   gchar *mac_address = NULL;
33   gchar *session_timeout = NULL;
34   gchar *bandwidth_max_down = NULL;
35   gchar *bandwidth_max_up = NULL;
36         uint32_t id;
37
38         if (!map)
39           goto out;
40
41   if (!map->members)
42     goto out;
43
44   if (param == NULL)
45     goto out;
46
47         members = map->members;
48
49         ip          = rh_string_get_sep(param, "|", 1);
50         username    = rh_string_get_sep(param, "|", 2);
51         session_id  = rh_string_get_sep(param, "|", 3);
52   mac_address = rh_string_get_sep(param, "|", 4);
53   session_timeout    = rh_string_get_sep(param, "|", 5);
54   bandwidth_max_down = rh_string_get_sep(param, "|", 6);
55   bandwidth_max_up   = rh_string_get_sep(param, "|", 7);
56
57   if (ip == NULL || username == NULL 
58         || session_id == NULL)
59     goto out;
60
61   if (mac_address == NULL)
62     mac_address = g_strdup(DEFAULT_MAC);
63
64         id = iptoid(map, ip);
65
66         if (id < 0) {
67     *reply_string = g_strdup("Invalid IP Address");
68                 goto cleanup;
69         }
70
71   req.id = id;
72   req.username = username;
73   req.session_id = session_id;
74   parse_mac(mac_address, &ethernet);
75   memcpy(req.mac_address, &ethernet, ETH_ALEN);
76   req.session_timeout = 0;
77
78   if (session_timeout != NULL) {
79     if (atol(session_timeout) != 0)
80       req.session_timeout = time(NULL) + atol(session_timeout);
81   }
82
83   if (bandwidth_max_down != NULL)
84     req.bandwidth_max_down = atol(bandwidth_max_down);
85   else
86     req.bandwidth_max_down = 0;
87
88   if (bandwidth_max_up != NULL)
89     req.bandwidth_max_up = atol(bandwidth_max_up);
90   else
91     req.bandwidth_max_up = 0;
92
93   rh_task_startsess(map, &req);
94
95   *reply_string = g_strdup_printf("Greeting! Got: IP %s, User %s, ID %s", 
96                                          ip, members[id].username, 
97                                                                                                                                          members[id].session_id);
98   goto cleanup;
99
100 out:
101     *reply_string = g_strdup("Invalid input parameters");
102     goto cleanup;
103
104 cleanup:
105   g_free(ip);
106   g_free(username);
107   g_free(session_id);
108   g_free(mac_address);
109   g_free(session_timeout);
110   g_free(bandwidth_max_down);
111   g_free(bandwidth_max_up);
112   return 0;
113 }
114
115 int do_stopsession(GNetXmlRpcServer *server,
116                    const gchar *command,
117                                                                          const gchar *param,
118                                                                          gpointer user_data,
119                                                                          gchar **reply_string)
120 {
121   struct rahunas_map *map = (struct rahunas_map *)user_data;
122         struct rahunas_member *members;
123   struct task_req req;
124         gchar *ip = NULL;
125         gchar *mac_address = NULL;
126         uint32_t   id;
127   int res = 0;
128   unsigned char ethernet[ETH_ALEN] = {0,0,0,0,0,0};
129
130         if (!map)
131           goto out;
132
133   if (!map->members)
134     goto out;
135
136         members = map->members;
137
138         if (param == NULL)
139           goto out;
140
141   DP("RPC Receive: %s", param);
142
143   ip          = rh_string_get_sep(param, "|", 1);
144   mac_address = rh_string_get_sep(param, "|", 2);
145
146   if (ip == NULL)
147     goto out;
148
149   if (mac_address == NULL)
150     mac_address = g_strdup(DEFAULT_MAC);
151
152         id = iptoid(map, ip);
153
154         if (id < 0) {
155     *reply_string = g_strdup("Invalid IP Address");
156     goto cleanup;
157         }
158
159   parse_mac(mac_address, &ethernet);
160   memcpy(req.mac_address, &ethernet, ETH_ALEN);
161
162         if (members[id].flags) {
163     if (memcmp(&ethernet, &members[id].mac_address, ETH_ALEN) == 0) {
164       req.id = id;
165       req.req_opt = RH_RADIUS_TERM_USER_REQUEST;
166
167       res = rh_task_stopsess(map, &req);
168       if (res == 0) {
169         *reply_string = g_strdup_printf("Client IP %s was removed!", 
170                                                           idtoip(map, id));
171       } else {
172          *reply_string = g_strdup_printf("Client IP %s error remove!", 
173                                                         idtoip(map, id));
174       }
175       goto cleanup;
176                 }       
177   }
178
179   *reply_string = g_strdup_printf("%s", ip);
180   goto cleanup;
181
182 out:
183   *reply_string = g_strdup("Invalid input parameters");
184   goto cleanup;
185
186 cleanup:
187   g_free(ip);
188   g_free(mac_address);
189   return 0;
190 }
191
192 int do_getsessioninfo(GNetXmlRpcServer *server,
193                       const gchar *command,
194                                                                             const gchar *param,
195                                                                             gpointer user_data,
196                                                                             gchar **reply_string)
197 {
198   struct rahunas_map *map = (struct rahunas_map *)user_data;
199         struct rahunas_member *members = NULL;
200         gchar *ip = NULL;
201         uint32_t   id;
202
203         if (!map)
204           goto out;
205
206   if (!map->members)
207     goto out;
208
209         members = map->members;
210
211         if (param == NULL)
212           goto out;
213
214   ip = param;
215         id = iptoid(map, ip);
216
217         if (id < 0) {
218     *reply_string = g_strdup("Invalid IP Address");
219     return 0;
220         }
221
222         if (members[id].flags) {
223     if (!members[id].username) {
224       *reply_string = g_strdup("Invalid Username");
225       return 0;
226     }
227
228     if (!members[id].session_id) {
229       *reply_string = g_strdup("Invalid Session ID");
230       return 0;
231     }
232     
233     *reply_string = g_strdup_printf("%s|%s|%s|%d|%s|%d", 
234                                       ip, 
235                                                   members[id].username,
236                                                                                                                                                   members[id].session_id,
237                                                                                                                                                   members[id].session_start,
238                                       mac_tostring(members[id].mac_address),
239                                       members[id].session_timeout);
240                 return 0;
241         }
242
243   *reply_string = g_strdup_printf("%s", ip);
244         return 0;
245
246 out:
247     *reply_string = g_strdup("Invalid input parameters");
248                 return 0;
249 }