forked from ValdikSS/openvpn-radiusplugin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
AccountingProcess.cpp
518 lines (454 loc) · 19.5 KB
/
AccountingProcess.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
/*
* radiusplugin -- An OpenVPN plugin for do radius authentication
* and accounting.
*
* Copyright (C) 2005 EWE TEL GmbH/Ralf Luebben <ralfluebben@gmx.de>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "AccountingProcess.h"
/** This method is the background process for accounting. It is in a endless loop
* until it gets a exit command. In the loop the process is
* waiting for a command from the foregroundprocess (USER_ADD, USER_DEL, EXIT).
* If no command is arrived in an interval of 0,5s the accounting is done
* for all users who need a update. The interval is 0,5s because every second
* a user can connect with an unknown interval, so this interval must be shorter.
* @param context The plugin context as object from the class PluginContext.
*/
void AccountingProcess::Accounting(PluginContext * context)
{
UserAcct *user=NULL; //The user for acconting.
int command, //The command from foreground process.
result; //The result from the socket.
string key; //The unique key.
AcctScheduler scheduler; //The scheduler for the accounting.
fd_set set; //A set for the select function.
struct timeval tv; //A timeinterval for the select function.
//Tell the parent everythink is ok.
try
{
context->acctsocketforegr.send(RESPONSE_INIT_SUCCEEDED);
}
catch (Exception &e)
{
cerr << getTime() << "RADIUS-PLUGIN: BACKGROUND-ACCT:" << e <<"\n";
goto done;
}
if (DEBUG (context->getVerbosity()))
cerr << getTime() << "RADIUS-PLUGIN: BACKGROUND ACCT: Started, RESPONSE_INIT_SUCCEEDED was sent to Foreground Process.\n";
// Event loop
while (1)
{
//create the informations for the result function
tv.tv_sec = 0;
tv.tv_usec = 500000; //wait 0,5s
FD_ZERO(&set); // clear out the set
FD_SET(context->acctsocketforegr.getSocket(), &set); // wait only on the socket from the foreground process
result = select(FD_SETSIZE, &set, NULL, NULL, &tv);
//if there is a data on the socket
if (result>0)
{
// get a command from foreground process
command = context->acctsocketforegr.recvInt();
if (DEBUG (context->getVerbosity()))
cerr << getTime() << "RADIUS-PLUGIN: BACKGROUND ACCT: Get a command.\n";
switch (command)
{
//add a new user to the scheduler
case ADD_USER:
try
{
if (DEBUG (context->getVerbosity()))
cerr << getTime() << "RADIUS-PLUGIN: BACKGROUND ACCT: New User.\n";
// if accounting errors are non fatal return success and proceed with accounting
if(context->conf.getNonFatalAccounting()==true)
context->acctsocketforegr.send(RESPONSE_SUCCEEDED);
try{
//allocate memory
user= new UserAcct;
}
catch(...)
{
cerr << getTime() << "RADIUS-PLUGIN: BACKGROUND ACCT: New failed for UserAcct." << endl;
}
//get the information from the foreground process
user->setUsername(context->acctsocketforegr.recvStr());
user->setSessionId(context->acctsocketforegr.recvStr()) ;
user->setDev(context->acctsocketforegr.recvStr()) ;
user->setPortnumber(context->acctsocketforegr.recvInt());
user->setCallingStationId(context->acctsocketforegr.recvStr());
user->setFramedIp(context->acctsocketforegr.recvStr());
user->setFramedIp6(context->acctsocketforegr.recvStr());
user->setCommonname(context->acctsocketforegr.recvStr());
user->setAcctInterimInterval(context->acctsocketforegr.recvInt());
user->setFramedRoutes(context->acctsocketforegr.recvStr());
user->setFramedRoutes6(context->acctsocketforegr.recvStr());
user->setKey(context->acctsocketforegr.recvStr());
user->setStatusFileKey(context->acctsocketforegr.recvStr());
user->setUntrustedPort(context->acctsocketforegr.recvStr());
context->acctsocketforegr.recvBuf(user);
if (DEBUG (context->getVerbosity()))
cerr << getTime() << "RADIUS-PLUGIN: BACKGROUND ACCT: New user acct: username: " << user->getUsername() << ", interval: " << user->getAcctInterimInterval() << ", calling station: " << user->getCallingStationId() << ", commonname: " << user->getCommonname() << ", framed ip: " << user->getFramedIp() << ", framed ipv6: " << user->getFramedIp6() <<".\n";
//set the starttime
user->setStarttime(time(NULL));
//calculate the nextupdate
user->setNextUpdate(user->getStarttime()+user->getAcctInterimInterval());
//send the start packet
if (user->sendStartPacket(context)==0)
{
if (DEBUG (context->getVerbosity()))
cerr << getTime() << "RADIUS-PLUGIN: BACKGROUND ACCT: Start packet was send.\n";
if (DEBUG (context->getVerbosity()))
cerr << getTime() << "RADIUS-PLUGIN: BACKGROUND ACCT: User was added to accounting scheduler.\n";
//set the system routes
user->addSystemRoutes(context);
string script = context->conf.getVsaScript();
//execute vendor specific attribute script
if (script.length() > 0)
{
if (DEBUG (context->getVerbosity()))
cerr << getTime() << "RADIUS-PLUGIN: BACKGROUND ACCT: Call vendor specific attribute script.\n";
if (callVsaScript(context, user, 1, 0) != 0)
{
throw Exception("Vendor specific attribute script failed.\n");
}
}
//add the user to the scheduler
scheduler.addUser(user);
//send the ok to the parent process
if(context->conf.getNonFatalAccounting()==false)
context->acctsocketforegr.send(RESPONSE_SUCCEEDED);
}
else
{
//delete the ccd file which was created at authentication
//user->deleteCcdFile(context);
//tell the parent parent process something is wrong
throw Exception("Accounting failed.\n");
}
// free the user, he was copied to the accounting scheduler list
}
catch (Exception &e)
{
cerr << getTime() << "RADIUS-PLUGIN: BACKGROUND ACCT: "<< e << "!\n";
if(context->conf.getNonFatalAccounting()==false)
context->acctsocketforegr.send(RESPONSE_FAILED);
//close the background process, if the ipc socket is bad
if (e.getErrnum()==Exception::SOCKETSEND || e.getErrnum()==Exception::SOCKETRECV)
{
cerr << getTime() << "RADIUS-PLUGIN: BACKGROUND ACCT: Error in socket!\n";
goto done;
}
}
catch (...)
{
if(context->conf.getNonFatalAccounting()==false)
context->acctsocketforegr.send(RESPONSE_FAILED);
cerr << getTime() << "RADIUS-PLUGIN: BACKGROUND ACCT: Unknown Exception!\n";
}
delete user;
break;
//delete a user
case DEL_USER:
if (DEBUG (context->getVerbosity()))
cerr << getTime() << "RADIUS-PLUGIN: BACKGROUND-ACCT: Delete user from accounting.\n";
// Always send successful response since we don't care about disconnection status
// and we don't want to stale main thread.
context->acctsocketforegr.send(RESPONSE_SUCCEEDED);
//receive the information
try
{
key=context->acctsocketforegr.recvStr();
}
catch (Exception &e)
{
cerr << getTime() << "RADIUS-PLUGIN: BACKGROUND ACCT: "<< e << "!\n";
//close the background process, if the ipc socket is bad
if (e.getErrnum()==Exception::SOCKETSEND || e.getErrnum()==Exception::SOCKETRECV)
{
goto done;
}
}
catch (...)
{
cerr << getTime() << "RADIUS-PLUGIN: BACKGROUND ACCT: Unknown Exception!\n";
}
//find the user, he must be already there
user=scheduler.findUser(key.c_str());
if (user)
{
if (DEBUG (context->getVerbosity()))
cerr << getTime() << "RADIUS-PLUGIN: BACKGROUND ACCT: Stop acct: username: " << user->getUsername()<< ", calling station: " << user->getCallingStationId()<< ", commonname: " << user->getCommonname() << ".\n";
//delete the system routes
user->delSystemRoutes(context);
//delete the ccd file which was created at authentication
//user->deleteCcdFile(context);
string script = context->conf.getVsaScript();
//execute vendor specific attribute script
if (script.length() > 0)
{
//string command= context->conf.getVsaScript() + string(" ") + string("ACTION=CLIENT_CONNECT")+string(" ")+string("USERNAME=")+user->getUsername()+string(" ")+string("COMMONNAME=")+user->getCommonname()+string(" ")+string("UNTRUSTED_IP=")+user->getCallingStationId() + string(" ") + string("UNTRUSTED_PORT=") + user->getUntrustedPort() + user->getVsaString();
if (DEBUG (context->getVerbosity()))
cerr << getTime() << "RADIUS-PLUGIN: BACKGROUND ACCT: Call vendor specific attribute script.\n";
if (callVsaScript(context, user, 2, 0) != 0)
{
throw Exception("Vendor specific attribute script failed.\n");
}
}
try
{
//delete the user from the accounting scheduler
scheduler.delUser(context, user);
if (DEBUG (context->getVerbosity()))
cerr << getTime() << "RADIUS-PLUGIN: BACKGROUND ACCT: User with key: " << key << " was deleted from accounting.\n";
}
catch (Exception &e)
{
cerr << getTime() << "RADIUS-PLUGIN: BACKGROUND-ACCT: " << e << "\n";
goto done;
}
catch (...)
{
cerr << getTime() << "RADIUS-PLUGIN: BACKGROUND ACCT: Unknown Exception!\n";
}
}
else
{
cerr << getTime() << "RADIUS-PLUGIN: BACKGROUND ACCT: No user with this key "<< key <<".\n";
}
break;
//exit the loop
case COMMAND_EXIT:
if (DEBUG (context->getVerbosity()))
cerr << getTime() << "RADIUS-PLUGIN: BACKGROUND-ACCT: Get command exit.\n";
goto done;
case -1:
cerr << getTime() << "RADIUS-PLUGIN: BACKGROUND: read error on command channel.\n";
break;
default:
cerr << getTime() << "RADIUS-PLUGIN: BACKGROUND: unknown command code: code= "<< command <<", exiting.\n";
goto done;
}
}
//after 0,5sec without a command call the scheduler
scheduler.doAccounting(context);
}
done:
//end the process
if (1)
scheduler.delallUsers(context);
cerr << getTime() << "RADIUS-PLUGIN: BACKGROUND ACCT: EXIT\n";
return;
}
/** This method executes the program for the vendor specific attributes and pass
* attributes to the program and vendor specific attributes as a buffer
* to the program.
* Attributes Code for decoding
*
* string username => 101
* string commonname => 102
* string framedip => 103
* string callingstationid => 104
* string untrustedport => 105
* string framedroutes => 106
* Octet vsabuf => 107
* The code is used for decoding in the additional program. The vsabuf must be decode also in the program.
* Example: vsascript.pl
* @param context The PluginContext
* @param user The user for which the script is executed.
* @param action Action: 0 => Authentication, 1 => Client-Connect, 2 => Client-Disconnect
* @param rekeying If equal 1 this is a rekeying.
* @return -1 in case of error, else 0
*/
int AccountingProcess::callVsaScript(PluginContext * context, User * user, unsigned int action, unsigned int rekeying)
{
char * route;
Octet * buf;
int buflen = 3 * sizeof(int);
if (user->getUsername().length() != 0)
{
buflen=buflen+user->getUsername().length()+2*sizeof(int);
}
if (user->getCommonname().length() != 0)
{
buflen=buflen+user->getCommonname().length()+2*sizeof(int);
}
if (user->getFramedIp().length() != 0)
{
buflen=buflen+user->getFramedIp().length()+2*sizeof(int);
}
if (user->getCallingStationId().length() != 0)
{
buflen=buflen+user->getCallingStationId().length()+2*sizeof(int);
}
if (user->getUntrustedPort().length() != 0)
{
buflen=buflen+user->getUntrustedPort().length()+2*sizeof(int);
}
if (user->getVsaBufLen() != 0)
{
buflen=buflen+user->getVsaBufLen() +2*sizeof(int);
}
char routes[user->getFramedRoutes().length()+1];
strncpy(routes, user->getFramedRoutes().c_str(), user->getFramedRoutes().length());
routes[user->getFramedRoutes().length()]=0;
if ((route = strtok(routes,";")) != NULL)
{
buflen=buflen+strlen(route)+2*sizeof(int);
while ((route = strtok(NULL,";"))!= NULL)
{
buflen=buflen+strlen(route)+2*sizeof(int);
}
}
try{
buf = new Octet[buflen];
}
catch(...)
{
cerr << getTime() << "RADIUS-PLUGIN: BACKGROUND ACCT: New failed for framedroutes buf." << endl;
}
unsigned int value = htonl(action);
memcpy(buf,&value, 4);
value = htonl(rekeying);
memcpy(buf+4,&value, 4);
value = htonl(buflen);
memcpy(buf+8,&value, 4);
int i=12;
if (user->getUsername().length() != 0)
{
value = htonl(101);
memcpy(buf+i,&value, 4);
i+=4;
value = htonl(user->getUsername().length());
memcpy(buf+i,&value, 4);
i+=4;
memcpy( buf+i, user->getUsername().c_str(),user->getUsername().length());
i=i+user->getUsername().length();
}
if (user->getCommonname().length() != 0)
{
value = htonl(102);
memcpy(buf+i,&value, 4);
i+=4;
value = htonl(user->getCommonname().length());
memcpy(buf+i,&value, 4);
i+=4;
memcpy( buf+i, user->getCommonname().c_str(),user->getCommonname().length());
i=i+user->getCommonname().length();
}
if (user->getFramedIp().length() != 0)
{
value = htonl(103);
memcpy(buf+i,&value, 4);
i+=4;
value = htonl(user->getFramedIp().length());
memcpy(buf+i,&value, 4);
i+=4;
memcpy( buf+i, user->getFramedIp().c_str(),user->getFramedIp().length());
i=i+user->getFramedIp().length();
}
if (user->getCallingStationId().length() != 0)
{
value = htonl(104);
memcpy(buf+i,&value, 4);
i+=4;
value = htonl(user->getCallingStationId().length());
memcpy(buf+i,&value, 4);
i+=4;
memcpy( buf+i, user->getCallingStationId().c_str(),user->getCallingStationId().length());
i=i+user->getCallingStationId().length();
}
if (user->getUntrustedPort().length() != 0)
{
value = htonl(105);
memcpy(buf+i,&value, 4);
i+=4;
value = htonl(user->getUntrustedPort().length());
memcpy(buf+i,&value, 4);
i+=4;
memcpy( buf+i, user->getUntrustedPort().c_str(),user->getUntrustedPort().length());
i=i+user->getUntrustedPort().length();
}
strncpy(routes, user->getFramedRoutes().c_str(), user->getFramedRoutes().length());
routes[user->getFramedRoutes().length()]=0;
if ((route = strtok(routes,";")) != NULL)
{
value = htonl(106);
memcpy(buf+i,&value, 4);
i+=4;
value = htonl(strlen(route));
memcpy(buf+i,&value, 4);
i+=4;
memcpy(buf+i, route, strlen(route));
i=i+strlen(route);
while ((route = strtok(NULL,";"))!= NULL)
{
value = htonl(106);
memcpy(buf+i,&value, 4);
i+=4;
value = htonl(strlen(route));
memcpy(buf+i,&value, 4);
i+=4;
memcpy(buf+i, route, strlen(route));
i=i+strlen(route);
}
}
if (user->getVsaBufLen() != 0)
{
value = htonl(107);
memcpy(buf+i,&value, 4);
i+=4;
value = htonl(user->getVsaBufLen());
memcpy(buf+i,&value, 4);
i+=4;
memcpy(buf+i, user->getVsaBuf(),user->getVsaBufLen());
i=i+user->getVsaBufLen();
}
if (mkfifo(context->conf.getVsaNamedPipe().c_str(), 0600)== -1)
{
/* FIFO bereits vorhanden - kein fataler Fehler */
if (errno == EEXIST)
{
cerr << getTime() << "RADIUS-PLUGIN:FIFO already exist.";
}
else
{
cerr << getTime() <<"RADIUS-PLUGIN: Error in mkfifio()";
return -1;
}
}
int fd_fifo=open(context->conf.getVsaNamedPipe().c_str(), O_RDWR | O_NONBLOCK);
if (fd_fifo == -1)
{
cerr << getTime() <<"RADIUS-PLUGIN: Error in opening pipe to VSAScript.";
return -1;
}
string exe=string(context->conf.getVsaScript()) + " " + string(context->conf.getVsaNamedPipe());
if (write (fd_fifo, buf, buflen) != buflen)
{
close(fd_fifo);
cerr << getTime() << "RADIUS-PLUGIN: Could not write in Pipe to VSAScript!";
return -1;
}
if (system(exe.c_str())!=0)
{
close(fd_fifo);
cerr << getTime() << "RADIUS-PLUGIN: Error in VSAScript!";
return -1;
}
close(fd_fifo);
delete [] buf;
return 0;
}