check_openvpn.cpp
3.85 KB
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
#include "check_openvpn.h"
using namespace std;
char *servicename = (char*)"OpenVPN";
void printVersion()
{
cout << "check_openvpn v" << VERSION << endl << endl;
}
void printHelp(bool longVersion)
{
if(longVersion)
{
printVersion();
cout << "Check OpenVPN server" << endl << endl;
printHelp(false);
cout << "Options:" << endl;
cout << " -h" << endl;
cout << " Print detailed help screen" << endl;
cout << " -V" << endl;
cout << " Print version information" << endl;
cout << " -H HOSTADDRESS" << endl;
cout << " Host where the UPnP server is running" << endl;
cout << " -p PORT" << endl;
cout << " OpenVPN Administration port" << endl;
cout << " -P PASSWORD" << endl;
cout << " OpenVPN Administration password" << endl;
return;
}
cout << "Usage: " << endl << "check_openvpn [-hV] -H HOSTADDRESS -p PORT -P PASSWORD" << endl << endl;
}
int check_openvpn(uint16_t port, char *hostname, char *password, string *serverinfo)
{
size_t numClients = 0;
string ipList = "";
char buffer[MAX_TCP+1];
int s;
int timeout = 10;
int bytes = 0;
string output = "";
s = createSocket();
connect(s,port,hostname,timeout);
while(output != "ENTER PASSWORD:")
{
recvMsg(s,buffer,MAX_TCP);
output += buffer;
memset(buffer,0x00,MAX_TCP+1);
}
sendMsg(s,strcat(password,"\n"),strlen(password)+1);
output = "";
do
{
bytes = recvMsg(s,buffer,MAX_TCP);
output += buffer;
memset(buffer,0x00,MAX_TCP+1);
}while(bytes);
if(output == "ENTER PASSWORD:")
{
*serverinfo = "Incorrect password";
close(s);
return 3;
}
output = "";
sendMsg(s,(char*)"status 2\n",9);
do
{
bytes = recvMsg(s,buffer,MAX_TCP);
output += buffer;
memset(buffer,0x00,MAX_TCP+1);
}while(output.rfind("END") != output.size()-5);
vector<string> splitstr;
vector<string> fields;
split(output,"\n",splitstr);
for(vector<string>::iterator it = splitstr.begin() ; it != splitstr.end(); ++it)
{
if(it->substr(0,11) == "CLIENT_LIST")
{
split(*it,",",fields);
ipList += fields[3] + ",";
numClients++;
}
}
if(numClients)
{
ipList.pop_back();
}
*serverinfo = int2str(numClients) + " clients connected (" + ipList + ")" ;
close(s);
return 0;
}
int main(int argc, char **argv)
{
struct itimerval timer;
timer.it_value.tv_sec = 10;
timer.it_value.tv_usec = 0;
timer.it_interval.tv_sec = 0;
timer.it_interval.tv_usec = 0;
setitimer (ITIMER_VIRTUAL, &timer, 0);
struct sigaction sa;
memset (&sa, 0, sizeof (sa));
sa.sa_handler = &timer_handler;
sigaction (SIGVTALRM, &sa, 0);
uint16_t port = 1195;
char *hostname = NULL;
char *password = NULL;
int c;
while ((c = getopt (argc, argv, "H:p:P:Vh")) != -1)
{
switch(c)
{
case 'H':
hostname = optarg;
break;
case 'p':
port = (uint16_t) str2int(optarg);
break;
case 'P':
password = optarg;
break;
case 'V':
printVersion();
return 0;
case 'h':
printHelp(true);
return 0;
case '?':
printHelp(false);
return 3;
}
}
if(hostname == NULL)
{
cout << "No HOSTADDRESS specified. Exiting." << endl;
return 3;
}
if(password == NULL)
{
cout << "No PASSWORD specified. Exiting." << endl;
return 3;
}
string serverinfo = "";
int returnCode = check_openvpn(port,hostname,password,&serverinfo);
cout << servicename;
switch(returnCode)
{
case 0:
cout << " OK - " << serverinfo << endl ;
break;
case 2:
cout << " CRITICAL - No response" << endl;
break;
case 3:
cout << " UNKNOWN - " << serverinfo << endl;
}
return returnCode;
}