1 /*
2  *  radiusplugin -- An OpenVPN plugin for do radius authentication
3  *					and accounting.
4  *
5  *  Copyright (C) 2005 EWE TEL GmbH/Ralf Luebben <ralfluebben@gmx.de>
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation; either version 2 of the License, or
10  *  any later version.
11  *
12  *  This program is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with this program; if not, write to the Free Software
19  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20  */
21 
22 #include "UserAcct.h"
23 #include "radiusplugin.h"
24 
25 /** The constructor calls the super constructor of the class User and the variables
26  * sessionid, bytesin, bytesout, nextupdate and starttime are set to 0.*/
UserAcct()27 UserAcct::UserAcct():User()
28 {
29 	gigain=0;
30 	gigaout=0;
31 	bytesin=0;
32 	bytesout=0;
33 	nextupdate=0;
34 	starttime=0;
35 }
36 
37 /** The destructor. Nothing happens here.*/
~UserAcct()38 UserAcct::~UserAcct()
39 {
40 }
41 
42 /** The assignment-operator.
43  * @param u A refernece to a UserAcct.*/
operator =(const UserAcct & u)44 UserAcct & UserAcct::operator=(const UserAcct &u)
45 {
46 
47 	if (this!=&u)
48 	{
49 		this->User::operator=(u);
50 		this->gigain=u.gigain;
51 		this->gigaout=u.gigaout;
52 		this->bytesin=u.bytesin;
53 		this->bytesout=u.bytesout;
54 		this->nextupdate=u.nextupdate;
55 		this->starttime=u.starttime;
56 	}
57 	return *this;
58 }
59 
60 
61 
62 
63 /**The copy constructor, it calls first the copy constructor
64  * of the User class.
65  * @param UserAcct u : A reference to an UserAcct object.*/
UserAcct(const UserAcct & u)66 UserAcct::UserAcct(const UserAcct &u):User(u)
67 {
68 	this->gigain=u.gigain;
69 	this->gigaout=u.gigaout;
70 	this->bytesin=u.bytesin;
71 	this->bytesout=u.bytesout;
72 	this->nextupdate=u.nextupdate;
73 	this->starttime=u.starttime;
74 
75 }
76 
77 /** The method sends an accounting update packet for the user to the radius server.
78  * The accounting information are read from the OpenVpn
79  * status file. The following attributes are sent to the radius server:
80  * - User_Name,
81  * - Framed_IP_Address,
82  * - NAS_Port,
83  * - Calling_Station_Id,
84  * - NAS_Identifier,
85  * - NAS_IP_Address,
86  * - NAS_Port_Type,
87  * - Service_Type,
88  * - Acct_Session_ID,
89  * - Acct_Status_Type,
90  * - Framed_Protocol,
91  * - Acct_Input_Octets,
92  * - Acct_Output_Octets,
93  * - Acct_Session_Time,
94  * - Acct_Input_Gigawords,
95  * - Acct_Output_Gigawords
96  * @param context The context of the plugin.
97  * @return An integer, 0 is everything is ok, else 1.*/
sendUpdatePacket(PluginContext * context)98 int UserAcct::sendUpdatePacket(PluginContext *context)
99 {
100 
101 	list<RadiusServer> * serverlist;
102 	list<RadiusServer>::iterator server;
103 
104 	RadiusPacket		packet(ACCOUNTING_REQUEST);
105 	RadiusAttribute		ra1(ATTRIB_User_Name,this->getUsername()),
106 				ra2(ATTRIB_Framed_IP_Address,this->getFramedIp()),
107 				ra3(ATTRIB_NAS_Port,this->getPortnumber()),
108 				ra4(ATTRIB_Calling_Station_Id,this->getCallingStationId()),
109 				ra5(ATTRIB_NAS_Identifier),
110 				ra6(ATTRIB_NAS_IP_Address),
111 				ra7(ATTRIB_NAS_Port_Type),
112 				ra8(ATTRIB_Service_Type),
113 				ra9(ATTRIB_Acct_Session_ID, this->getSessionId()),
114 		                ra10(ATTRIB_Acct_Status_Type,string("3")), // "Alive"
115 				ra11(ATTRIB_Framed_Protocol),
116 				ra12(ATTRIB_Acct_Input_Octets, this->bytesin),
117 				ra13(ATTRIB_Acct_Output_Octets, this->bytesout),
118 				ra14(ATTRIB_Acct_Session_Time),
119 				ra15(ATTRIB_Acct_Input_Gigawords, this->gigain),
120 				ra16(ATTRIB_Acct_Output_Gigawords, this->gigaout);
121 
122 
123 
124 	//get the server list
125 	serverlist=context->radiusconf.getRadiusServer();
126 
127 	//set server on the first server
128 	server=serverlist->begin();
129 
130 	//add the attributes to the radius packet
131 	if(packet.addRadiusAttribute(&ra1))
132 	{
133 		cerr << getTime() << "RADIUS-PLUGIN: BACKGROUND-ACCT: Fail to add attribute ATTRIB_User_Name.\n";
134 	}
135 
136 	if (packet.addRadiusAttribute(&ra2))
137 	{
138 		cerr << getTime() << "RADIUS-PLUGIN: BACKGROUND-ACCT:  Fail to add attribute ATTRIB_User_Password.\n";
139 	}
140 
141 	if (packet.addRadiusAttribute(&ra3))
142 	{
143 		cerr << getTime() << "RADIUS-PLUGIN: BACKGROUND-ACCT:  Fail to add attribute ATTRIB_NAS_Port.\n";
144 	}
145 
146 	if (packet.addRadiusAttribute(&ra4))
147 	{
148 		cerr << getTime() << "RADIUS-PLUGIN: BACKGROUND-ACCT:  Fail to add attribute ATTRIB_Calling_Station_Id.\n";
149 	}
150 
151 	//get the values from the config and add them to the packet
152 	if(strcmp(context->radiusconf.getNASIdentifier(),""))
153 	{
154 		ra5.setValue(context->radiusconf.getNASIdentifier());
155 		if (packet.addRadiusAttribute(&ra5))
156 		{
157 			cerr << getTime() << "RADIUS-PLUGIN: BACKGROUND-ACCT:  Fail to add attribute ATTRIB_NAS_Identifier.\n";
158 		}
159 	}
160 
161 	if(strcmp(context->radiusconf.getNASIpAddress(),""))
162 	{
163 			if(ra6.setValue(context->radiusconf.getNASIpAddress())!=0)
164 			{
165 				cerr << getTime() << "RADIUS-PLUGIN: BACKGROUND-ACCT:  Fail to set value ATTRIB_NAS_Ip_Address.\n";
166 			}
167 			if (packet.addRadiusAttribute(&ra6))
168 			{
169 				cerr << getTime() << "RADIUS-PLUGIN: BACKGROUND-ACCT:  Fail to add attribute ATTRIB_NAS_Ip_Address.\n";
170 			}
171 	}
172 
173 	if(strcmp(context->radiusconf.getNASPortType(),""))
174 	{
175 			ra7.setValue(context->radiusconf.getNASPortType());
176 			if (packet.addRadiusAttribute(&ra7))
177 			{
178 				cerr << getTime() << "RADIUS-PLUGIN: BACKGROUND-ACCT:  Fail to add attribute ATTRIB_NAS_Port_Type.\n";
179 			}
180 	}
181 
182 	if(strcmp(context->radiusconf.getServiceType(),""))
183 	{
184 			ra8.setValue(context->radiusconf.getServiceType());
185 			if (packet.addRadiusAttribute(&ra8))
186 			{
187 				cerr << getTime() << "RADIUS-PLUGIN: BACKGROUND-ACCT:  Fail to add attribute ATTRIB_Service_Type.\n";
188 			}
189 	}
190 
191 	if (packet.addRadiusAttribute(&ra9))
192 	{
193 		cerr << getTime() << "RADIUS-PLUGIN: BACKGROUND-ACCT:  Fail to add attribute ATTRIB_Acct_Session_ID.\n";
194 	}
195 
196 	if (packet.addRadiusAttribute(&ra10))
197 	{
198 		cerr << getTime() << "RADIUS-PLUGIN: BACKGROUND-ACCT:  Fail to add attribute ATTRIB_Acct_Session_ID.\n";
199 	}
200 
201 	if(strcmp(context->radiusconf.getFramedProtocol(),""))
202 	{
203 			ra11.setValue(context->radiusconf.getFramedProtocol());
204 			if (packet.addRadiusAttribute(&ra11))
205 			{
206 				cerr << getTime() << "RADIUS-PLUGIN: BACKGROUND-ACCT:  Fail to add attribute ATTRIB_Framed_Protocol.\n";
207 			}
208 	}
209 
210 	if (packet.addRadiusAttribute(&ra12))
211 	{
212 		cerr << getTime() << "RADIUS-PLUGIN: BACKGROUND-ACCT:  Fail to add attribute ATTRIB_Acct_Input_Packets.\n";
213 	}
214 
215 	if (packet.addRadiusAttribute(&ra13))
216 	{
217 		cerr << getTime() << "RADIUS-PLUGIN: BACKGROUND-ACCT:  Fail to add attribute ATTRIB_Acct_Output_Packets.\n";
218 	}
219 	//calculate the session time
220 	ra14.setValue((time(NULL)-this->starttime));
221 	if (packet.addRadiusAttribute(&ra14)) {
222 		cerr << getTime() << "RADIUS-PLUGIN: BACKGROUND-ACCT:  Fail to add attribute ATTRIB_Acct_Session_Time.\n";
223 	}
224 
225 	if (packet.addRadiusAttribute(&ra15)) {
226 		cerr << getTime() << "RADIUS-PLUGIN: BACKGROUND-ACCT:  Fail to add attribute ATTRIB_Acct_Input_Gigawords.\n";
227 	}
228 
229 	if (packet.addRadiusAttribute(&ra16)) {
230 		cerr << getTime() << "RADIUS-PLUGIN: BACKGROUND-ACCT:  Fail to add attribute ATTRIB_Acct_Output_Gigawords.\n";
231 	}
232 
233 	//send the packet to the server
234 	if (packet.radiusSend(server)<0)
235 	{
236 		cerr << getTime() << "RADIUS-PLUGIN: BACKGROUND-ACCT:  Packet was not sent.\n";
237 	}
238 
239 	//get the response
240 	if (packet.radiusReceive(serverlist)>=0)
241 	{
242 		//is the packet a ACCOUNTING_RESPONSE?
243 		if(packet.getCode()==ACCOUNTING_RESPONSE)
244 		{
245 			if (DEBUG (context->getVerbosity()))
246 				cerr << getTime() << "RADIUS-PLUGIN: BACKGROUND-ACCT: Get ACCOUNTING_RESPONSE-Packet.\n";
247 
248 
249 			return 0;
250 
251 		}
252 		else
253 		{
254 			if (DEBUG (context->getVerbosity()))
255 				cerr << getTime() << "RADIUS-PLUGIN: BACKGROUND-ACCT: No response on accounting request.\n";
256 			return 1;
257 		}
258 
259 
260 	}
261 	return 1;
262 }
263 
264 /** The method sends an accouting start packet for the user to the radius server.
265  *  The following attributes are sent to the radius server:
266  * - User_Name,
267  * - Framed_IP_Address,
268  * - NAS_Port,
269  * - Calling_Station_Id,
270  * - NAS_Identifier,
271  * - NAS_IP_Address,
272  * - NAS_Port_Type,
273  * - Service_Type,
274  * - Acct_Session_ID,
275  * - Acct_Status_Type,
276  * - Framed_Protocol,
277  * @param  context The context of the plugin.
278  * @return An integer, 0 is everything is ok, else 1.*/
sendStartPacket(PluginContext * context)279 int UserAcct::sendStartPacket(PluginContext * context)
280 {
281 	list<RadiusServer>* serverlist;
282 	list<RadiusServer>::iterator server;
283 	RadiusPacket		packet(ACCOUNTING_REQUEST);
284 	RadiusAttribute		ra1(ATTRIB_User_Name,this->getUsername()),
285 						ra2(ATTRIB_Framed_IP_Address,this->getFramedIp()),
286 						ra3(ATTRIB_NAS_Port,this->getPortnumber()),
287 						ra4(ATTRIB_Calling_Station_Id,this->getCallingStationId()),
288 						ra5(ATTRIB_NAS_Identifier),
289 						ra6(ATTRIB_NAS_IP_Address),
290 						ra7(ATTRIB_NAS_Port_Type),
291 						ra8(ATTRIB_Service_Type),
292 						ra9(ATTRIB_Acct_Session_ID, this->getSessionId()),
293 		                                ra10(ATTRIB_Acct_Status_Type,string("1")), // "Start"
294 						ra11(ATTRIB_Framed_Protocol);
295 
296 
297 
298 	//get the radius server from the config
299 	serverlist=context->radiusconf.getRadiusServer();
300 
301 	//set server to the first from the list
302 	server=serverlist->begin();
303 
304 	//add the attributes to the packet
305 	if(packet.addRadiusAttribute(&ra1))
306 	{
307 		cerr << getTime() << "RADIUS-PLUGIN: BACKGROUND-ACCT:  Fail to add attribute ATTRIB_User_Name.\n";
308 	}
309 
310 	if (packet.addRadiusAttribute(&ra2))
311 	{
312 		cerr << getTime() << "RADIUS-PLUGIN: BACKGROUND-ACCT:  Fail to add attribute ATTRIB_User_Password.\n";
313 	}
314 	if (packet.addRadiusAttribute(&ra3))
315 	{
316 		cerr << getTime() << "RADIUS-PLUGIN: BACKGROUND-ACCT:  Fail to add attribute ATTRIB_NAS_Port.\n";
317 	}
318 	if (packet.addRadiusAttribute(&ra4))
319 	{
320 		cerr << getTime() << "RADIUS-PLUGIN: BACKGROUND-ACCT:  Fail to add attribute ATTRIB_Calling_Station_Id.\n";
321 	}
322 
323 	//get information from the config and add the attributes to the packet
324 	if(strcmp(context->radiusconf.getNASIdentifier(),""))
325 	{
326 			ra5.setValue(context->radiusconf.getNASIdentifier());
327 			if (packet.addRadiusAttribute(&ra5))
328 			{
329 				cerr << getTime() << "RADIUS-PLUGIN: BACKGROUND-ACCT:  Fail to add attribute ATTRIB_NAS_Identifier.\n";
330 			}
331 	}
332 
333 	if(strcmp(context->radiusconf.getNASIpAddress(),""))
334 	{
335 			if(ra6.setValue(context->radiusconf.getNASIpAddress())!=0)
336 			{
337 				cerr << getTime() << "RADIUS-PLUGIN: BACKGROUND-ACCT:  Fail to set value ATTRIB_NAS_Ip_Address.\n";
338 			}
339 
340 			if (packet.addRadiusAttribute(&ra6))
341 			{
342 				cerr << getTime() << "RADIUS-PLUGIN: BACKGROUND-ACCT:  Fail to add attribute ATTRIB_NAS_Ip_Address.\n";
343 			}
344 	}
345 	if(strcmp(context->radiusconf.getNASPortType(),""))
346 	{
347 			ra7.setValue(context->radiusconf.getNASPortType());
348 			if (packet.addRadiusAttribute(&ra7))
349 			{
350 				cerr << getTime() << "RADIUS-PLUGIN: BACKGROUND-ACCT:  Fail to add attribute ATTRIB_NAS_Port_Type.\n";
351 			}
352 	}
353 
354 	if(strcmp(context->radiusconf.getServiceType(),""))
355 	{
356 			ra8.setValue(context->radiusconf.getServiceType());
357 			if (packet.addRadiusAttribute(&ra8))
358 			{
359 				cerr << getTime() << "RADIUS-PLUGIN: BACKGROUND-ACCT:  Fail to add attribute ATTRIB_Service_Type.\n";
360 			}
361 	}
362 
363 	if (packet.addRadiusAttribute(&ra9)) {
364 		cerr << getTime() << "RADIUS-PLUGIN: BACKGROUND-ACCT:  Fail to add attribute ATTRIB_Acct_Session_ID.\n";
365 	}
366 
367 	if (packet.addRadiusAttribute(&ra10)) {
368 		cerr << getTime() << "RADIUS-PLUGIN: BACKGROUND-ACCT:  Fail to add attribute ATTRIB_Acct_Session_ID.\n";
369 	}
370 
371 	if(strcmp(context->radiusconf.getFramedProtocol(),""))
372 	{
373 			ra11.setValue(context->radiusconf.getFramedProtocol());
374 			if (packet.addRadiusAttribute(&ra11))
375 			{
376 				cerr << getTime() << "RADIUS-PLUGIN: BACKGROUND-ACCT:  Fail to add attribute ATTRIB_Framed_Protocol.\n";
377 			}
378 	}
379 
380 	//send the packet
381 	if (packet.radiusSend(server)<0)
382 	{
383 		cerr << getTime() << "RADIUS-PLUGIN: BACKGROUND-ACCT:  Packet was not sent.\n";
384 	}
385 
386 	//receive the response
387 	if (packet.radiusReceive(serverlist)>=0)
388 	{
389 		//is is a accounting resopnse ?
390 		if(packet.getCode()==ACCOUNTING_RESPONSE)
391 		{
392 			if (DEBUG (context->getVerbosity()))
393 				cerr << getTime() << "RADIUS-PLUGIN: BACKGROUND-ACCT:  Get ACCOUNTING_RESPONSE-Packet.\n";
394 
395 			return 0;
396 
397 		}
398 		else
399 		{
400 			if (DEBUG (context->getVerbosity()))
401 				cerr << getTime() << "RADIUS-PLUGIN: BACKGROUND-ACCT:  No response on accounting request.\n";
402 			return 1;
403 		}
404 
405 	}
406 
407 	return 1;
408 }
409 
410 
411 
412 /** The method sends an accounting stop packet for the user to the radius server.
413  * The accounting information are read from the OpenVpn
414  * status file. The following attributes are sent to the radius server:
415  * - User_Name,
416  * - Framed_IP_Address,
417  * - NAS_Port,
418  * - Calling_Station_Id,
419  * - NAS_Identifier,
420  * - NAS_IP_Address,
421  * - NAS_Port_Type,
422  * - Service_Type,
423  * - Acct_Session_ID,
424  * - Acct_Status_Type,
425  * - Framed_Protocol,
426  * - Acct_Input_Octets,
427  * - Acct_Output_Octets,
428  * - Acct_Session_Time
429  * @param context The context of the plugin.
430  * @return An integer, 0 is everything is ok, else 1.*/
sendStopPacket(PluginContext * context)431 int UserAcct::sendStopPacket(PluginContext * context)
432 {
433 	list<RadiusServer> * serverlist;
434 	list<RadiusServer>::iterator server;
435 	RadiusPacket		packet(ACCOUNTING_REQUEST);
436 	RadiusAttribute		ra1(ATTRIB_User_Name,this->getUsername()),
437 				ra2(ATTRIB_Framed_IP_Address,this->getFramedIp()),
438 				ra3(ATTRIB_NAS_Port,this->portnumber),
439 				ra4(ATTRIB_Calling_Station_Id,this->getCallingStationId()),
440 				ra5(ATTRIB_NAS_Identifier),
441 				ra6(ATTRIB_NAS_IP_Address),
442 				ra7(ATTRIB_NAS_Port_Type),
443 				ra8(ATTRIB_Service_Type),
444 				ra9(ATTRIB_Acct_Session_ID, this->getSessionId()),
445 		                ra10(ATTRIB_Acct_Status_Type,string("2")), // "Stop"
446 				ra11(ATTRIB_Framed_Protocol),
447 				ra12(ATTRIB_Acct_Input_Octets, this->bytesin),
448 				ra13(ATTRIB_Acct_Output_Octets, this->bytesout),
449 				ra14(ATTRIB_Acct_Session_Time),
450 				ra15(ATTRIB_Acct_Input_Gigawords, this->gigain),
451 				ra16(ATTRIB_Acct_Output_Gigawords, this->gigaout);
452 
453 
454 
455 	//get the server from the config
456 	serverlist=context->radiusconf.getRadiusServer();
457 
458 	//set server to the first server
459 	server=serverlist->begin();
460 
461 	//add the attributes to the packet
462 	if(packet.addRadiusAttribute(&ra1))
463 	{
464 		cerr << getTime() << "RADIUS-PLUGIN: BACKGROUND-ACCT:  Fail to add attribute ATTRIB_User_Name.\n";
465 	}
466 
467 	if (packet.addRadiusAttribute(&ra2))
468 	{
469 		cerr << getTime() << "RADIUS-PLUGIN: BACKGROUND-ACCT:  Fail to add attribute ATTRIB_FramedIP_Adress.\n";
470 	}
471 	if (packet.addRadiusAttribute(&ra3))
472 	{
473 		cerr << getTime() << "RADIUS-PLUGIN: BACKGROUND-ACCT:  Fail to add attribute ATTRIB_NAS_Port.\n";
474 	}
475 	if (packet.addRadiusAttribute(&ra4))
476 	{
477 		cerr << getTime() << "RADIUS-PLUGIN: BACKGROUND-ACCT:  Fail to add attribute ATTRIB_Calling_Station_Id.\n";
478 	}
479 
480 	//get information from th config and ad it to the packet
481 	if(strcmp(context->radiusconf.getNASIdentifier(),""))
482 	{
483 			ra5.setValue(context->radiusconf.getNASIdentifier());
484 			if (packet.addRadiusAttribute(&ra5))
485 			{
486 				cerr << getTime() << "RADIUS-PLUGIN: BACKGROUND-ACCT:  Fail to add attribute ATTRIB_NAS_Identifier.\n";
487 			}
488 	}
489 
490 	if(strcmp(context->radiusconf.getNASIpAddress(),""))
491 	{
492 			if(ra6.setValue(context->radiusconf.getNASIpAddress())!=0)
493 			{
494 				cerr << getTime() << "RADIUS-PLUGIN: BACKGROUND-ACCT:  Fail to set value ATTRIB_NAS_Ip_Address.\n";
495 			}
496 			else
497 			if (packet.addRadiusAttribute(&ra6))
498 			{
499 				cerr << getTime() << "RADIUS-PLUGIN: BACKGROUND-ACCT:  Fail to add attribute ATTRIB_NAS_Ip_Address.\n";
500 			}
501 	}
502 	if(strcmp(context->radiusconf.getNASPortType(),""))
503 	{
504 			ra7.setValue(context->radiusconf.getNASPortType());
505 			if (packet.addRadiusAttribute(&ra7))
506 			{
507 				cerr << getTime() << "RADIUS-PLUGIN: BACKGROUND-ACCT:  Fail to add attribute ATTRIB_NAS_Port_Type.\n";
508 			}
509 	}
510 
511 	if(strcmp(context->radiusconf.getServiceType(),""))
512 	{
513 			ra8.setValue(context->radiusconf.getServiceType());
514 			if (packet.addRadiusAttribute(&ra8))
515 			{
516 				cerr << getTime() << "RADIUS-PLUGIN: BACKGROUND-ACCT:  Fail to add attribute ATTRIB_Service_Type.\n";
517 			}
518 	}
519 	if (packet.addRadiusAttribute(&ra9))
520 	{
521 		cerr << getTime() << "RADIUS-PLUGIN: BACKGROUND-ACCT:  Fail to add attribute ATTRIB_Acct_Session_ID.\n";
522 	}
523 	if (packet.addRadiusAttribute(&ra10))
524 	{
525 		cerr << getTime() << "RADIUS-PLUGIN: BACKGROUND-ACCT:  Fail to add attribute ATTRIB_Acct_Session_ID.\n";
526 	}
527 
528 	if(strcmp(context->radiusconf.getFramedProtocol(),""))
529 	{
530 			ra11.setValue(context->radiusconf.getFramedProtocol());
531 			if (packet.addRadiusAttribute(&ra11))
532 			{
533 				cerr << getTime() << "RADIUS-PLUGIN: BACKGROUND-ACCT:  Fail to add attribute ATTRIB_Framed_Protocol.\n";
534 			}
535 	}
536 
537 
538 
539 	if (packet.addRadiusAttribute(&ra12))
540 	{
541 		cerr << getTime() << "RADIUS-PLUGIN: BACKGROUND-ACCT:  Fail to add attribute ATTRIB_Acct_Input_Packets.\n";
542 	}
543 	if (packet.addRadiusAttribute(&ra13))
544 	{
545 		cerr << getTime() << "RADIUS-PLUGIN: BACKGROUND-ACCT:  Fail to add attribute ATTRIB_Acct_Output_Packets.\n";
546 	}
547 
548 	//calculate the session time
549 	ra14.setValue(time(NULL)-this->starttime);
550 	if (packet.addRadiusAttribute(&ra14)) {
551 		cerr << getTime() << "RADIUS-PLUGIN: BACKGROUND-ACCT:  Fail to add attribute ATTRIB_Acct_Session_Time.\n";
552 	}
553 
554 	if (packet.addRadiusAttribute(&ra15)) {
555 		cerr << getTime() << "RADIUS-PLUGIN: BACKGROUND-ACCT:  Fail to add attribute ATTRIB_Acct_Input_Gigawords.\n";
556 	}
557 
558 	if (packet.addRadiusAttribute(&ra16)) {
559 		cerr << getTime() << "RADIUS-PLUGIN: BACKGROUND-ACCT:  Fail to add attribute ATTRIB_Acct_Output_Gigawords.\n";
560 	}
561 
562 	//send the packet
563 	if (packet.radiusSend(server)<0)
564 	{
565 		cerr << getTime() << "RADIUS-PLUGIN: BACKGROUND-ACCT:  Packet was not sent.\n";
566 	}
567 
568 	//get the response
569 	if (packet.radiusReceive(serverlist)>=0)
570 	{
571 		//is it an accounting response
572 		if(packet.getCode()==ACCOUNTING_RESPONSE)
573 		{
574 			if (DEBUG (context->getVerbosity()))
575 				cerr << getTime() << "RADIUS-PLUGIN: BACKGROUND-ACCT:  Get ACCOUNTING_RESPONSE-Packet.\n";
576 
577 			return 0;
578 
579 		}
580 		else
581 		{
582 			if (DEBUG (context->getVerbosity()))
583 				cerr << getTime() << "RADIUS-PLUGIN: BACKGROUND-ACCT:  No response on accounting request.\n";
584 			return 1;
585 		}
586 	}
587 
588 	return 1;
589 }
590 
591 /** The method deletes ths systemroutes of the user.
592  * @param context The context of the plugin.
593  */
delSystemRoutes(PluginContext * context)594 void UserAcct::delSystemRoutes(PluginContext * context)
595 {
596 	char * route;
597 	char framedip[16];
598 
599 	char routestring[100];
600 	char framednetmask_cidr[3];
601 	char framedgw[16];
602 	char framedmetric[5];
603 	char * framedroutes;
604 	int j=0,k=0,len=0;
605 
606 	//copy the framed route string to an char array, it is easier to
607 	//analyse
608 	framedroutes=new char[this->getFramedRoutes().size()+1];
609 	memset(framedroutes,0,this->getFramedRoutes().size()+1);
610 
611 	// copy in a temp-string, because strtok deletes the delimiter, if it used anywhere
612 	strncpy(framedroutes,this->getFramedRoutes().c_str(),this->getFramedRoutes().size());
613 
614 	//are there framed routes
615 	if (framedroutes[0]!='\0')
616 	{
617 		//get the first route
618 		route=strtok(framedroutes,";");
619 		len=strlen(route);
620 		if (len > 50) //this is too big!! but the length is variable
621 		{
622 			cerr << getTime() <<"RADIUS-PLUGIN: BACKGROUND-ACCT:  Argument for Framed Route is too long (>50 Characters).\n";
623 		}
624 		else
625 		{
626 			while (route!=NULL)
627 			{
628 				//set the arrays to 0
629 				memset(routestring,0,100);
630 				memset(framednetmask_cidr,0,3);
631 				memset(framedip,0,16);
632 				memset(framedgw,0,16);
633 				memset(framedmetric,0,5);
634 
635 				j=0;k=0;
636 				//get ip address and add it to framedip
637 				while(route[j]!='/' && j<len)
638 				{
639 					if (route[j]!=' ')
640 					{
641 						framedip[k]=route[j];
642 						k++;
643 					}
644 					j++;
645 				}
646 				k=0;
647 				j++;
648 				//get the framednetmask and add it to framednetmack_cidr
649 				while(route[j]!=' ' && j<=len)
650 				{
651 					framednetmask_cidr[k]=route[j];
652 					k++;
653 					j++;
654 				}
655 				k=0;
656 				//jump spaces
657 				while(route[j]==' ' && j<=len)
658 				{
659 					j++;
660 				}
661 				//get the gateway
662 				while(route[j]!='/' && j<=len)
663 				{
664 					if (route[j]!=' ')
665 					{
666 						framedgw[k]=route[j];
667 						k++;
668 					}
669 					j++;
670 				}
671 				j++;
672 				//find gateway netmask (this isn't used
673 				//at the command route under linux)
674 				while(route[j]!=' ' && j<=len)
675 				{
676 					j++;
677 				}
678 				//jump spaces
679 				while(route[j]==' ' && j<=len)
680 				{
681 					j++;
682 				}
683 				k=0;
684 				if (j<=len) //is there a metric (optional)
685 				{
686 					k=0;
687 					//find the metric
688 					while(route[j]!=' ' && j<=len)
689 					{
690 							framedmetric[k]=route[j];
691 							k++;
692 							j++;
693 					}
694 				}
695 
696 				//create system call
697 				strncat(routestring, "route del -net ",15);
698 				strncat(routestring, framedip ,16);
699 				strncat(routestring, "/" ,1);
700 				strncat(routestring, framednetmask_cidr, 2);
701 				strncat(routestring, " gw ", 4);
702 				strncat(routestring, framedgw, 16);
703 				if (framedmetric[0]!='\0')
704 				{
705 					strncat(routestring, " metric ", 8);
706 					strncat(routestring, framedmetric , 5);
707 				}
708 				//redirect the output stderr to /dev/null
709 				strncat(routestring," 2> /dev/null",13);
710 
711 
712 				if (DEBUG (context->getVerbosity()))
713 	    			cerr << getTime() << "RADIUS-PLUGIN: BACKGROUND-ACCT:  Create route string "<< routestring <<".\n";
714 
715 				//system call
716 				if(system(routestring)!=0)
717 				//if(1)//-> the debugg can't context system()
718 				{
719 					cerr << getTime() << "RADIUS-PLUGIN: BACKGROUND-ACCT:  Route " << routestring << " could not set. Route already set or bad route string.\n";
720 				}
721 				else
722 				{
723 					if (DEBUG (context->getVerbosity()))
724 	    				cerr << getTime() << "RADIUS-PLUGIN: BACKGROUND-ACCT:  Add route to system routing table.\n";
725 
726 				}
727 				//get the next route
728 				route=strtok(NULL,";");
729 			}
730 		}
731 
732 	}
733 	else
734 	{
735 		if (DEBUG (context->getVerbosity()))
736     		cerr << getTime() << "RADIUS-PLUGIN: BACKGROUND-ACCT:  No routes for user in AccessAcceptPacket.\n";
737 	}
738 	//free the char array
739 	delete [] framedroutes;
740 
741 
742 }
743 
744 /** The method adds ths routes of the user to the system routing table.
745  * @param context The context of the plugin.
746  */
addSystemRoutes(PluginContext * context)747 void UserAcct::addSystemRoutes(PluginContext * context)
748 {
749 	char * route;
750 	char framedip[16];
751 
752 	char routestring[100];
753 	char framednetmask_cidr[3];
754 	char framedgw[16];
755 	char framedmetric[5];
756 	char * framedroutes;
757 	int j=0,k=0,len=0;
758 
759 	//copy the framed route string to an char array, it is easier to
760 	//analyse
761 	framedroutes=new char[this->getFramedRoutes().size()+1];
762 	memset(framedroutes,0,this->getFramedRoutes().size()+1);
763 
764 	// copy in a temp-string, becaue strtok deletes the delimiter, if it used anywhere
765 	strncpy(framedroutes,this->getFramedRoutes().c_str(),this->getFramedRoutes().size());
766 
767 	//are there framed routes
768 	if (framedroutes[0]!='\0')
769 	{
770 		//get the first route
771 		route=strtok(framedroutes,";");
772 		len=strlen(route);
773 		if (len > 50) //this is to big!! but the length is variable
774 		{
775 			cerr << getTime() <<"RADIUS-PLUGIN: BACKGROUND-ACCT:  Argument for Framed Route is to long (>50 Characters).\n";
776 		}
777 		else
778 		{
779 			while (route!=NULL)
780 			{
781 				//set the arrays to 0
782 				memset(routestring,0,100);
783 				memset(framednetmask_cidr,0,3);
784 				memset(framedip,0,16);
785 				memset(framedgw,0,16);
786 				memset(framedmetric,0,5);
787 
788 				j=0;k=0;
789 				//get ip address and add it to framedip
790 				while(route[j]!='/' && j<len)
791 				{
792 					if (route[j]!=' ')
793 					{
794 						framedip[k]=route[j];
795 						k++;
796 					}
797 					j++;
798 				}
799 				k=0;
800 				j++;
801 				//get the framednetmask and add it to framednetmask_cidr
802 				while(route[j]!=' ' && j<=len)
803 				{
804 					framednetmask_cidr[k]=route[j];
805 					k++;
806 					j++;
807 				}
808 				k=0;
809 				//jump spaces
810 				while(route[j]==' ' && j<=len)
811 				{
812 					j++;
813 				}
814 				//get the gateway
815 				while(route[j]!='/' && j<=len)
816 				{
817 					if (route[j]!=' ')
818 					{
819 						framedgw[k]=route[j];
820 						k++;
821 					}
822 					j++;
823 				}
824 				j++;
825 				//find gateway netmask (this isn't used
826 				//at the command route under linux)
827 				while(route[j]!=' ' && j<=len)
828 				{
829 					j++;
830 				}
831 				//jump spaces
832 				while(route[j]==' ' && j<=len)
833 				{
834 					j++;
835 				}
836 				k=0;
837 				if (j<=len) //is there a metric (optional)
838 				{
839 					k=0;
840 					//find the metric
841 					while(route[j]!=' ' && j<=len)
842 					{
843 							framedmetric[k]=route[j];
844 							k++;
845 							j++;
846 					}
847 				}
848 
849 
850 				//create system call
851 				strncat(routestring, "route add -net ",15);
852 				strncat(routestring, framedip ,16);
853 				strncat(routestring, "/" ,1);
854 				strncat(routestring, framednetmask_cidr, 2);
855 				strncat(routestring, " gw ", 4);
856 				strncat(routestring, framedgw, 16);
857 				if (framedmetric[0]!='\0')
858 				{
859 					strncat(routestring, " metric ", 8);
860 					strncat(routestring, framedmetric , 5);
861 				}
862 				//redirect the output stderr to /dev/null
863 				strncat(routestring," 2> /dev/null",13);
864 
865 
866 				if (DEBUG (context->getVerbosity()))
867 	    			cerr << getTime() << "RADIUS-PLUGIN: BACKGROUND-ACCT:  Create route string "<< routestring << ".\n";
868 
869 				//system call route
870 				if(system(routestring)!=0)
871 				//if(1)//-> the debugg can't context system()
872 				{
873 					cerr << getTime() << "RADIUS-PLUGIN: BACKGROUND-ACCT:  Route " << routestring << " could not set. Route already set or bad route string.\n";
874 				}
875 				else
876 				{
877 					if (DEBUG (context->getVerbosity()))
878 	    				cerr << getTime() << "RADIUS-PLUGIN: BACKGROUND-ACCT:  Add route to system routing table.\n";
879 
880 				}
881 				//get the next route
882 				route=strtok(NULL,";");
883 			}
884 		}
885 	}
886 	else
887 	{
888 		if (DEBUG (context->getVerbosity()))
889     		cerr << getTime() << "RADIUS-PLUGIN: BACKGROUND-ACCT:  No routes for user.\n";
890 	}
891 	//fre the chat array
892 	delete [] framedroutes;
893 
894 }
895 
896 
897 
898 
899 /** The getter method for the gigain variable.
900  * @return The number of received giga.*/
getGigaIn(void)901 uint32_t UserAcct::getGigaIn(void)
902 {
903 	return this->gigain;
904 }
905 /**The setter method for the gigain variable.
906  * @param giga The received giga.*/
setGigaIn(uint32_t giga)907 void UserAcct::setGigaIn(uint32_t giga)
908 {
909 	this->gigain=giga;
910 }
911 
912 /** The getter method for the gigaout variable.
913  * @return The number of sent giga.*/
getGigaOut(void)914 uint32_t UserAcct::getGigaOut(void)
915 {
916 	return this->gigaout;
917 }
918 /**The setter method for the gigaout variable.
919  * @param giga  The sended giga.*/
setGigaOut(uint32_t giga)920 void UserAcct::setGigaOut(uint32_t giga)
921 {
922 	this->gigaout=giga;
923 }
924 
925 /** The getter method for the bytesin variable.
926  * @return The number of received bytes.*/
getBytesIn(void)927 uint32_t UserAcct::getBytesIn(void)
928 {
929 	return this->bytesin;
930 }
931 /**The setter method for the bytesin variable.
932  * @param bytes The received bytes.*/
setBytesIn(uint32_t bytes)933 void UserAcct::setBytesIn(uint32_t bytes)
934 {
935 	this->bytesin=bytes;
936 }
937 
938 /** The getter method for the bytesout variable.
939  * @return The number of sent bytes.*/
getBytesOut(void)940 uint32_t UserAcct::getBytesOut(void)
941 {
942 	return this->bytesout;
943 }
944 /**The setter method for the bytesout variable.
945  * @param bytes  The sended bytes.*/
setBytesOut(uint32_t bytes)946 void UserAcct::setBytesOut(uint32_t bytes)
947 {
948 	this->bytesout=bytes;
949 }
950 
951 /** The getter method for the startime.
952  * @return The starttime.*/
getStarttime(void)953 time_t UserAcct::getStarttime(void)
954 {
955 	return this->starttime;
956 }
957 /**The setter method for the nextupdate.
958  * @param t The starttime*/
setStarttime(time_t t)959 void UserAcct::setStarttime(time_t t)
960 {
961 	this->starttime=t;
962 }
963 
964 /** The getter method for the nextupdate.
965  * @return A struct of the nextupdate.*/
getNextUpdate(void)966 time_t UserAcct::getNextUpdate(void)
967 {
968 	return this->nextupdate;
969 }
970 /**The setter method for the nextupdate.
971  * @param t The nextupdate.*/
setNextUpdate(time_t t)972 void UserAcct::setNextUpdate(time_t t)
973 {
974 	this->nextupdate=t;
975 }
976 
deleteCcdFile(PluginContext * context)977 int UserAcct::deleteCcdFile(PluginContext * context)
978 {
979 	string filename;
980 	filename = context->conf.getCcdPath()+ this->getCommonname();
981 	if(context->conf.getOverWriteCCFiles()==true && (this->getFramedIp().length() > 0 || this->getFramedRoutes().length() > 0))
982 	{
983 		remove(filename.c_str());
984 	}
985 	else
986 	{
987 		cerr << getTime() << "RADIUS-PLUGIN: Client config file was not deleted, overwriteccfiles is false \n.";
988 	}
989 	return 0;
990 }
991 
992