forked from ValdikSS/openvpn-radiusplugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Config.cpp
618 lines (529 loc) · 14.4 KB
/
Config.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
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
/*
* radiusplugin -- An OpenVPN plugin for do radius authentication
* and accounting.
*
* Copyright (C) 2005 EWE TEL GmbH/Ralf Luebben <[email protected]>
*
* 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 "Config.h"
/** The constructor The constructor initializes all char arrays with 0.
*/
Config::Config(void)
{
this->usernameascommonname=false;
this->clientcertnotrequired=false;
this->overwriteccfiles=true;
this->useauthcontrolfile=false;
this->useclientconnectdeferfile=false;
this->accountingonly=false;
this->nonfatalaccounting=false;
this->defacctinteriminterval=0;
this->ccdPath="";
this->openvpnconfig="";
this->vsanamedpipe="";
this->vsascript="";
memset(this->subnet,0,16);
memset(this->p2p,0,16);
memset(this->p2p6,0,40);
}
/** The constructor initializes all char arrays with 0. After the initialization
* the configfile is parsed and the information which are
* found are copied to the attributes.
* @param configfile The name of the configfile.
*/
Config::Config(char * configfile)
{
memset(this->subnet,0,16);
memset(this->p2p,0,16);
memset(this->p2p6,0,40);
this->ccdPath="";
this->openvpnconfig="";
this->vsanamedpipe="";
this->vsascript="";
this->usernameascommonname=false;
this->clientcertnotrequired=false;
this->overwriteccfiles=true;
this->useauthcontrolfile=false;
this->useclientconnectdeferfile=false;
this->accountingonly=false;
this->nonfatalaccounting=false;
this->defacctinteriminterval=0;
this->parseConfigFile(configfile);
}
/** The destructur clears the serverlist. */
Config::~Config(void)
{
}
/** The method parse the configfile for attributes and
* radius server, the attributes are copied to the
* member variables.
* @param configfile The name of the configfile.
* @return An integer, 0 if everything is ok
* or PARSING_ERROR or BAD_FILE if something is wrong.*/
int Config::parseConfigFile(const char * configfile)
{
string line;
ifstream file;
file.open(configfile, ios::in);
if (file.is_open())
{
while (file.eof()==false)
{
getline(file,line);
this->deletechars(&line);
if(line.empty()==false)
{
if (strncmp(line.c_str(),"subnet=",7)==0)
{
if((line.size()-7)>15)
{
return BAD_FILE;
}
line.copy(this->subnet,line.size()-7,7);
}
if (strncmp(line.c_str(),"p2p=",4)==0)
{
if((line.size()-4)>15)
{
return BAD_FILE;
}
line.copy(this->p2p,line.size()-4,4);
}
if (strncmp(line.c_str(),"p2p6=",5)==0)
{
if((line.size()-5)>39)
{
return BAD_FILE;
}
line.copy(this->p2p6,line.size()-5,5);
}
if (strncmp(line.c_str(),"vsascript=",10)==0)
{
this->vsascript=line.substr(10,line.size()-10);
}
if (strncmp(line.c_str(),"vsanamedpipe=",13)==0)
{
this->vsanamedpipe=line.substr(13,line.size()-13);
}
if (strncmp(line.c_str(),"OpenVPNConfig=",14)==0)
{
this->openvpnconfig=line.substr(14,line.size()-14);
}
if (strncmp(line.c_str(),"overwriteccfiles=",17)==0)
{
string stmp=line.substr(17,line.size()-17);
deletechars(&stmp);
if(stmp == "true") this->overwriteccfiles=true;
else if (stmp =="false") this->overwriteccfiles=false;
else return BAD_FILE;
}
if (strncmp(line.c_str(),"useauthcontrolfile=",19)==0)
{
string stmp=line.substr(19,line.size()-19);
deletechars(&stmp);
if(stmp == "true") this->useauthcontrolfile=true;
else if (stmp =="false") this->useauthcontrolfile=false;
else return BAD_FILE;
}
if (strncmp(line.c_str(),"useclientconnectdeferfile=",26)==0)
{
string stmp=line.substr(26,line.size()-26);
deletechars(&stmp);
if(stmp == "true") this->useclientconnectdeferfile=true;
else if (stmp =="false") this->useclientconnectdeferfile=false;
else return BAD_FILE;
}
if (strncmp(line.c_str(),"accountingonly=",15)==0)
{
string stmp=line.substr(15,line.size()-15);
deletechars(&stmp);
if(stmp == "true") this->accountingonly=true;
else if (stmp =="false") this->accountingonly=false;
else return BAD_FILE;
}
if (strncmp(line.c_str(),"nonfatalaccounting=",19)==0)
{
string stmp=line.substr(19,line.size()-19);
deletechars(&stmp);
if(stmp == "true") this->nonfatalaccounting=true;
else if (stmp =="false") this->nonfatalaccounting=false;
else return BAD_FILE;
}
if (strncmp(line.c_str(),"defacctinteriminterval=",23)==0)
{
string stmp=line.substr(23,line.size()-23);
deletechars(&stmp);
char *stemp;
long defacctinteriminterval = 0;
defacctinteriminterval = strtol(stmp.c_str(), &stemp, 0);
if (stemp == stmp.c_str() || *stemp != '\0' || defacctinteriminterval < 0)
return BAD_FILE;
this->defacctinteriminterval=(int)defacctinteriminterval;
}
}
}
file.close();
// if the main files contains references to other config files
// we don't need to care about recursive includes, OpenVPN does it already
list<string> configfiles;
configfiles.push_back(this->openvpnconfig);
//open OpenVPN config
while(configfiles.size() > 0)
{
ifstream file2;
string filename=configfiles.front();
file2.open(filename.c_str(), ios::in);
char const* delims = " \t\r\n\0";
if (file2.is_open())
{
while(file2.eof()==false)
{
getline(file2,line);
if(line.empty()==false)
{
string param=line;
// trim leading whitespace
string::size_type pos = param.find_first_not_of(delims);
if (pos != string::npos) param.erase(0,pos );
pos=param.find_first_of(delims);
if (pos != string::npos) param.erase(pos);
if (param == "verify-client-cert")
{
this->deletechars(&line);
if (line == "verify-client-certoptional" || line == "verify-client-certnone")
{
this->clientcertnotrequired=true;
}
}
if (param == "username-as-common-name")
{
this->deletechars(&line);
if (line == "username-as-common-name")
{
this->usernameascommonname=true;
}
}
if (param == "client-config-dir")
{
this->deletechars(&line);
line.erase(0, 17);
this->setCcdPath(line);
}
if (param == "config")
{
this->deletechars(&line);
line.erase(0, 6);
configfiles.push_back(line);
}
if (param == "status")
{
//method deletechars don't work, entry has formet: status <file> [time]
pos = line.find_first_of("#");
if (pos != string::npos)
{
line.erase(pos);
}
// trim leading whitespace
pos = line.find_first_not_of(delims);
if (pos != string::npos) line.erase(0,pos);
line.erase(0, 6);
// trim leading whitespace again
pos = line.find_first_not_of(" \t");
if (pos != string::npos) line.erase(0,pos);
//delete the trailing version of status if there
pos = line.find_first_of(delims);
if (pos != string::npos) line.erase(pos);
this->deletechars(&line);
if(!line.empty())
{
this->statusfile=line;
}
}
}
}
file.close();
configfiles.remove(filename);
}
else
{
return BAD_FILE;
}
}
}
else
{
return BAD_FILE;
}
return 0;
}
/** The method deletes chars from a string.
* This is used to delete tabs, spaces, # and '\0'
* from a string.
* @param text The string which should be cleaned.
*/
void Config::deletechars(string * line)
{
char const* delims = " \t\r\n\0";
// trim leading whitespace
string::size_type pos = line->find_first_not_of(delims);
if (pos != string::npos) line->erase(0,pos );
// trim trailing whitespace
pos = line->find_last_not_of(delims);
if (pos != string::npos) line->erase(pos+1);
//trim whitespaces in line
pos = line->find_first_of(delims);
while (pos != string::npos)
{
line->erase(pos,1);
pos = line->find_first_of(delims);
}
// trim comments
pos = line->find_first_of("#");
if (pos != string::npos)
{
line->erase(pos);
}
}
/**The method finds the part of the string after the
* '=' and puts it in the value.
* @param text The string with the value.
* @param value The value where to put the part of the string. */
void Config::getValue(const char * text, char * value)
{
int i=0,j=0;
while (text[i]!='=' && text[i]!='\0')
{
i++;
}
i++;
while (text[i]!='\0')
{
value[j]=text[i];
i++;
j++;
}
value[j]='\0';
}
/** The getter methid for the client config dir (ccd).
* @return A string to the ccd.
*/
string Config::getCcdPath(void)
{
return this->ccdPath;
}
/** The setter method for the client config dir (ccd).
* @param path A string to the ccd path.
*/
void Config::setCcdPath(string path)
{
if(path[path.length()]!= '/')
{
path +='/';
}
this->ccdPath=path;
}
/** Returns the path to the status file.
* @param A string to path of the status file.
*/
string Config::getStatusFile(void)
{
return this->statusfile;
}
/** The setter method for thepath to the statusfile (path + filename).
* @param file A string of the filepath.
*/
void Config::setStatusFile(string file)
{
this->statusfile=file;
}
/** The setter method for the nas ip address.
* @param ip A string with ip address.
*/
void Config::setSubnet(char * ip)
{
strncpy(this->subnet,ip, 16);
}
/** The getter method for the nas ip address.
* @return A pointer to the nas ipaddress.
*/
char * Config::getSubnet(void)
{
return this->subnet;
}
/** The setter method for the p2p address.
* @param ip A string with p2p address.
*/
void Config::setP2p(char * ip)
{
strncpy(this->p2p,ip, 16);
}
/** The getter method for the p2p address.
* @return A pointer to the p2p address.
*/
char * Config::getP2p(void)
{
return this->p2p;
}
/** The setter method for the p2p6 address.
* @param ip A string with p2p6 address.
*/
void Config::setP2p6(char * ip)
{
strncpy(this->p2p6,ip, 40);
}
/** The getter method for the p2p6 address.
* @return A pointer to the p2p6 address.
*/
char * Config::getP2p6(void)
{
return this->p2p6;
}
/** The setter method for the vsascript.
* @param script A path of the script.
*/
void Config::setVsaScript(string script)
{
this->vsascript=script;
}
/** The getter method for vsascript.
* @return A pointer to the path of the script.
*/
string Config::getVsaScript(void)
{
return this->vsascript;
}
/** The setter method for the usernameascommonname value.
* @param b A boolean for option usernameascommonname.
*/
void Config::setUsernameAsCommonname(bool b)
{
this->usernameascommonname=b;
}
/** The getter method for the usernameascommonname value.
* @return A boolean for option usernameascommonname.
*/
bool Config::getUsernameAsCommonname(void)
{
return this->usernameascommonname;
}
/** The setter method for the vsanamedpipe.
* @param script A path of the pipe.
*/
void Config::setVsaNamedPipe(string pipe)
{
this->vsanamedpipe=pipe;
}
/** The getter method for vsanamedpipe.
* @return A pointer to the path of the pipe.
*/
string Config::getVsaNamedPipe(void)
{
return this->vsanamedpipe;
}
/** The setter method for the clientcertnotrequired value.
* @param b A boolean for option clientcertnotrequired.
*/
void Config::setClientCertNotRequired(bool b)
{
this->clientcertnotrequired=b;
}
/** The getter method for the clientcertnotrequired value.
* @return A boolean for option clientcertnotrequired.
*/
bool Config::getClientCertNotRequired(void)
{
return this->clientcertnotrequired;
}
/** The getter method for the path to the OpenVPN config
* @return A string to the path.
*/
string Config::getOpenVPNConfig(void)
{
return this->openvpnconfig;
}
/** The setter method for the path to the OpenVPN config
* @param conf Path to the config file.
*/
void Config::setOpenVPNConfig(string conf)
{
this->openvpnconfig=conf;
}
/** The getter method for the overwriteccfiles variable.
* @return A bool of overwriteccfiles.
*/
bool Config::getOverWriteCCFiles(void)
{
return this->overwriteccfiles;
}
/** The setter method for the overwriteccfiles varibale
* @param overwrite Set to true if the plugin is allowed to overwrite the client config files.
*/
void Config::setOverWriteCCFiles(bool overwrite)
{
this->overwriteccfiles=overwrite;
}
/** Getter method for the authcontrolfile variable.
* @return A bool of authcontrolfile .
*/
bool Config::getUseAuthControlFile(void)
{
return this->useauthcontrolfile;
}
/** The setter method for the authcontrolfile varibale
* @param overwrite Set to true if the plugin if auth control files should be if supported by the OpenVPN version.
*/
void Config::setUseAuthControlFile(bool b)
{
this->useauthcontrolfile=b;
}
/** Getter method for the clientconnectdeferfile variable.
* @return A bool of clientconnectdeferfile .
*/
bool Config::getUseClientConnectDeferFile(void)
{
return this->useclientconnectdeferfile;
}
/** The setter method for the clientconnectdeferfile varibale
* @param overwrite Set to true if the plugin if client-connect control files should be if supported by the OpenVPN version.
*/
void Config::setUseClientConnectDeferFile(bool b)
{
this->useclientconnectdeferfile=b;
}
bool Config::getAccountingOnly(void)
{
return this->accountingonly;
}
void Config::setAccountingOnly(bool b)
{
this->accountingonly=b;
}
bool Config::getNonFatalAccounting(void)
{
return this->nonfatalaccounting;
}
void Config::setNonFatalAccounting(bool b)
{
this->nonfatalaccounting=b;
}
int Config::getDefAcctInterimInterval(void)
{
return this->defacctinteriminterval;
}
void Config::setDefAcctInterimInterval(int b)
{
this->defacctinteriminterval=b;
}