| 1 |
/* |
| 2 |
* Copyright (c) 2014 Alexander Golovko <[email protected]> |
| 3 |
* All rights reserved. |
| 4 |
* |
| 5 |
* Redistribution and use in source and binary forms, with or without |
| 6 |
* modification, are permitted provided that the following conditions are met: |
| 7 |
* |
| 8 |
* 1. Redistributions of source code must retain the above copyright notice, |
| 9 |
* this list of conditions and the following disclaimer. |
| 10 |
* |
| 11 |
* 2. Redistributions in binary form must reproduce the above copyright notice, |
| 12 |
* this list of conditions and the following disclaimer in the documentation |
| 13 |
* and/or other materials provided with the distribution. |
| 14 |
* |
| 15 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 16 |
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 17 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 18 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 19 |
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 20 |
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 21 |
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 22 |
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 23 |
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 24 |
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 25 |
* POSSIBILITY OF SUCH DAMAGE. |
| 26 |
*/ |
| 27 |
|
| 28 |
#include <openssl/ssl.h> |
| 29 |
#include <openssl/err.h> |
| 30 |
#include <string.h> |
| 31 |
#include <time.h> |
| 32 |
|
| 33 |
#include "client.h" |
| 34 |
#include "uthash.h" |
| 35 |
|
| 36 |
static nscang_client_t *nscang_client_instances; |
| 37 |
|
| 38 |
unsigned int |
| 39 |
set_psk(SSL *ssl, const char *hint, char *identity, |
| 40 |
unsigned int max_identity_length, unsigned char *psk, |
| 41 |
unsigned int max_psk_length) |
| 42 |
{ |
| 43 |
nscang_client_t *c = NULL; |
| 44 |
|
| 45 |
HASH_FIND_PTR(nscang_client_instances, &ssl, c); |
| 46 |
if (c != NULL) { |
| 47 |
strncpy(identity, c->identity, max_identity_length); |
| 48 |
identity[max_identity_length - 1] = 0x00; |
| 49 |
|
| 50 |
strncpy((char *)psk, c->psk, max_psk_length); |
| 51 |
psk[max_psk_length - 1] = 0x00; |
| 52 |
|
| 53 |
return strlen((char *)psk); |
| 54 |
} |
| 55 |
identity[0] = 0x00; |
| 56 |
psk[0] = 0x00; |
| 57 |
return 0; |
| 58 |
} |
| 59 |
|
| 60 |
int |
| 61 |
nscang_client_init(nscang_client_t *c, char *host, int port, char *ciphers, |
| 62 |
char *identity, char *psk) |
| 63 |
{ |
| 64 |
char port_str[6]; |
| 65 |
|
| 66 |
snprintf(port_str, sizeof(port_str), "%d", port); |
| 67 |
memset(c, 0x00, sizeof(nscang_client_t)); |
| 68 |
|
| 69 |
c->ssl_ctx = SSL_CTX_new(SSLv23_client_method()); |
| 70 |
if (c->ssl_ctx == NULL) { |
| 71 |
c->_errno = NSCANG_ERROR_SSL_CTX_CREATE; |
| 72 |
return 0; |
| 73 |
} |
| 74 |
|
| 75 |
if (ciphers != NULL) { |
| 76 |
if (SSL_CTX_set_cipher_list(c->ssl_ctx, ciphers) != 1) { |
| 77 |
c->_errno = NSCANG_ERROR_SSL_CIPHERS; |
| 78 |
return 0; |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
SSL_CTX_set_options(c->ssl_ctx, SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3); |
| 83 |
|
| 84 |
c->bio = BIO_new(BIO_s_connect()); |
| 85 |
if (c->bio == NULL) { |
| 86 |
c->_errno = NSCANG_ERROR_SSL_BIO_CREATE; |
| 87 |
return 0; |
| 88 |
} |
| 89 |
BIO_set_conn_hostname(c->bio, host); |
| 90 |
BIO_set_conn_port(c->bio, port_str); |
| 91 |
BIO_set_nbio(c->bio, 1); |
| 92 |
|
| 93 |
c->ssl = SSL_new(c->ssl_ctx); |
| 94 |
if (c->ssl == NULL) { |
| 95 |
nscang_client_free(c); |
| 96 |
c->_errno = NSCANG_ERROR_SSL_CREATE; |
| 97 |
return 0; |
| 98 |
} |
| 99 |
|
| 100 |
SSL_set_bio(c->ssl, c->bio, c->bio); |
| 101 |
SSL_set_connect_state(c->ssl); |
| 102 |
SSL_set_psk_client_callback(c->ssl, set_psk); |
| 103 |
|
| 104 |
c->identity = malloc(strlen(identity) + 1); |
| 105 |
c->psk = malloc(strlen(psk) + 1); |
| 106 |
|
| 107 |
if ((c->identity == NULL) || (c->psk == NULL)) { |
| 108 |
c->_errno = NSCANG_ERROR_MALLOC; |
| 109 |
return 0; |
| 110 |
} |
| 111 |
|
| 112 |
strcpy(c->identity, identity); |
| 113 |
strcpy(c->psk, psk); |
| 114 |
|
| 115 |
HASH_ADD_PTR(nscang_client_instances, ssl, c); |
| 116 |
|
| 117 |
c->state = NSCANG_STATE_NEW; |
| 118 |
|
| 119 |
return 1; |
| 120 |
} |
| 121 |
|
| 122 |
void |
| 123 |
nscang_client_free(nscang_client_t *c) |
| 124 |
{ |
| 125 |
if (c->state != NSCANG_STATE_NONE) |
| 126 |
nscang_client_disconnect(c); |
| 127 |
|
| 128 |
if (c->ssl != NULL) { |
| 129 |
HASH_DEL(nscang_client_instances, c); |
| 130 |
} |
| 131 |
if (c->ssl != NULL) { |
| 132 |
SSL_free(c->ssl); |
| 133 |
c->ssl = NULL; |
| 134 |
c->bio = NULL; |
| 135 |
} |
| 136 |
if (c->ssl_ctx != NULL) { |
| 137 |
SSL_CTX_free(c->ssl_ctx); |
| 138 |
c->ssl_ctx = NULL; |
| 139 |
} |
| 140 |
if (c->identity != NULL) { |
| 141 |
free(c->identity); |
| 142 |
c->identity = NULL; |
| 143 |
} |
| 144 |
if (c->psk != NULL) { |
| 145 |
free(c->psk); |
| 146 |
c->psk = NULL; |
| 147 |
} |
| 148 |
|
| 149 |
c->state = NSCANG_STATE_NONE; |
| 150 |
} |
| 151 |
|
| 152 |
int |
| 153 |
nscang_client_write(nscang_client_t *c, void *buf, int len, int timeout) |
| 154 |
{ |
| 155 |
int rc; |
| 156 |
time_t endtime; |
| 157 |
fd_set fdset; |
| 158 |
struct timeval tv; |
| 159 |
|
| 160 |
tv.tv_sec = 0; |
| 161 |
tv.tv_usec = 100000; |
| 162 |
|
| 163 |
endtime = time(NULL) + timeout; |
| 164 |
|
| 165 |
while (1) { |
| 166 |
if ((rc = SSL_write(c->ssl, buf, len)) > 0) |
| 167 |
return 1; |
| 168 |
|
| 169 |
if (!BIO_should_retry(c->bio)) { |
| 170 |
c->_errno = NSCANG_ERROR_SSL; |
| 171 |
return 0; |
| 172 |
} |
| 173 |
|
| 174 |
if (time(NULL) > endtime) { |
| 175 |
c->_errno = NSCANG_ERROR_TIMEOUT; |
| 176 |
return 0; |
| 177 |
} |
| 178 |
|
| 179 |
FD_ZERO(&fdset); |
| 180 |
FD_SET(BIO_get_fd(c->bio, NULL), &fdset); |
| 181 |
select(1, NULL, &fdset, NULL, &tv); |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
int |
| 186 |
nscang_client_response(nscang_client_t *c, int timeout) |
| 187 |
{ |
| 188 |
int rc; |
| 189 |
time_t endtime; |
| 190 |
fd_set fdset; |
| 191 |
char buf[1024]; |
| 192 |
int was_read = 0; |
| 193 |
char *ptr = buf; |
| 194 |
struct timeval tv; |
| 195 |
|
| 196 |
tv.tv_sec = 0; |
| 197 |
tv.tv_usec = 100000; |
| 198 |
|
| 199 |
endtime = time(NULL) + timeout; |
| 200 |
|
| 201 |
while (1) { |
| 202 |
rc = SSL_read(c->ssl, ptr, 1024 - was_read); |
| 203 |
|
| 204 |
if (rc > 0) { |
| 205 |
was_read = was_read + rc; |
| 206 |
ptr = buf + was_read; |
| 207 |
|
| 208 |
if (buf[was_read - 1] == '\n') { |
| 209 |
if (buf[was_read - 2] == '\r') |
| 210 |
buf[was_read - 2] = 0x00; |
| 211 |
else |
| 212 |
buf[was_read - 1] = 0x00; |
| 213 |
|
| 214 |
break; |
| 215 |
} |
| 216 |
|
| 217 |
if (was_read == 1024) { |
| 218 |
c->_errno = NSCANG_ERROR_TOO_LONG_RESPONSE; |
| 219 |
return 0; |
| 220 |
} |
| 221 |
} else if (!BIO_should_retry(c->bio)) { |
| 222 |
c->_errno = NSCANG_ERROR_SSL; |
| 223 |
return 0; |
| 224 |
} |
| 225 |
|
| 226 |
if (time(NULL) > endtime) { |
| 227 |
c->_errno = NSCANG_ERROR_TIMEOUT; |
| 228 |
return 0; |
| 229 |
} |
| 230 |
|
| 231 |
FD_ZERO(&fdset); |
| 232 |
FD_SET(BIO_get_fd(c->bio, NULL), &fdset); |
| 233 |
select(1, &fdset, NULL, NULL, &tv); |
| 234 |
} |
| 235 |
|
| 236 |
if (strncmp(buf, "MOIN", 4) == 0) { |
| 237 |
if (strcmp(buf, "MOIN 1") == 0) { |
| 238 |
return NSCANG_RESP_MOIN; |
| 239 |
} else { |
| 240 |
c->_errno = NSCANG_ERROR_BAD_PROTO_VERSION; |
| 241 |
strncpy(c->errstr, buf + 5, sizeof(c->errstr) - 1); |
| 242 |
c->errstr[sizeof(c->errstr) - 1] = 0x00; |
| 243 |
return 0; |
| 244 |
} |
| 245 |
} else if (strncmp(buf, "OKAY", 4) == 0) { |
| 246 |
return NSCANG_RESP_OKAY; |
| 247 |
} else if (strncmp(buf, "FAIL", 4) == 0) { |
| 248 |
c->_errno = NSCANG_ERROR_FAIL; |
| 249 |
strncpy(c->errstr, buf + 5, sizeof(c->errstr) - 1); |
| 250 |
c->errstr[sizeof(c->errstr) - 1] = 0x00; |
| 251 |
return 0; |
| 252 |
} else if (strncmp(buf, "BAIL", 4) == 0) { |
| 253 |
nscang_client_disconnect(c); |
| 254 |
c->_errno = NSCANG_ERROR_BAIL; |
| 255 |
strncpy(c->errstr, buf + 5, sizeof(c->errstr) - 1); |
| 256 |
c->errstr[sizeof(c->errstr) - 1] = 0x00; |
| 257 |
return 0; |
| 258 |
} else { |
| 259 |
char *msg = "BAIL Unknown response!"; |
| 260 |
|
| 261 |
nscang_client_write(c, msg, sizeof(msg), 0); |
| 262 |
nscang_client_disconnect(c); |
| 263 |
c->_errno = NSCANG_ERROR_UNKNOWN_RESPONSE; |
| 264 |
strncpy(c->errstr, buf, sizeof(c->errstr) - 1); |
| 265 |
c->errstr[sizeof(c->errstr) - 1] = 0x00; |
| 266 |
return 0; |
| 267 |
} |
| 268 |
} |
| 269 |
|
| 270 |
void |
| 271 |
nscang_client_disconnect(nscang_client_t *c) |
| 272 |
{ |
| 273 |
fd_set fdset; |
| 274 |
struct timeval tv; |
| 275 |
|
| 276 |
tv.tv_sec = 0; |
| 277 |
tv.tv_usec = 100000; |
| 278 |
|
| 279 |
if (c->state == NSCANG_STATE_MOIN) |
| 280 |
nscang_client_send_quit(c); |
| 281 |
|
| 282 |
if ((SSL_shutdown(c->ssl) == -1) && BIO_should_retry(c->bio)) { |
| 283 |
FD_ZERO(&fdset); |
| 284 |
FD_SET(BIO_get_fd(c->bio, NULL), &fdset); |
| 285 |
select(1, NULL, &fdset, NULL, &tv); |
| 286 |
SSL_shutdown(c->ssl); |
| 287 |
} |
| 288 |
|
| 289 |
SSL_clear(c->ssl); |
| 290 |
BIO_reset(c->bio); |
| 291 |
|
| 292 |
c->state = NSCANG_STATE_NEW; |
| 293 |
} |
| 294 |
|
| 295 |
int |
| 296 |
nscang_client_send_moin(nscang_client_t *c, int timeout) |
| 297 |
{ |
| 298 |
char cmd[64]; |
| 299 |
int len, rc; |
| 300 |
|
| 301 |
if (c->state == NSCANG_STATE_MOIN) |
| 302 |
return 1; |
| 303 |
|
| 304 |
if (c->state != NSCANG_STATE_NEW) { |
| 305 |
c->_errno = NSCANG_ERROR_BAD_STATE; |
| 306 |
return 0; |
| 307 |
} |
| 308 |
|
| 309 |
srandom(time(NULL)); |
| 310 |
len = snprintf(cmd, sizeof(cmd), "MOIN 1 %08ld%08ld\r\n", random(), |
| 311 |
random()); |
| 312 |
|
| 313 |
if (!nscang_client_write(c, cmd, len, timeout)) |
| 314 |
return 0; |
| 315 |
|
| 316 |
rc = nscang_client_response(c, timeout); |
| 317 |
|
| 318 |
if (!rc) |
| 319 |
return 0; |
| 320 |
|
| 321 |
if (rc != NSCANG_RESP_MOIN) { |
| 322 |
c->_errno = NSCANG_ERROR_PROTOCOL_MISMATCH; |
| 323 |
return 0; |
| 324 |
} |
| 325 |
|
| 326 |
c->state = NSCANG_STATE_MOIN; |
| 327 |
return 1; |
| 328 |
} |
| 329 |
|
| 330 |
/* Check whether a string's prefix matches /\[\d{1,9}\]/ |
| 331 |
* Returns 1 for match, 0 for no match, -1 for incomplete/bad match |
| 332 |
*/ |
| 333 |
static int |
| 334 |
has_timestamp_prefix(const char *s) |
| 335 |
{ |
| 336 |
char *end; |
| 337 |
|
| 338 |
if(*s++ != '[') return 0; |
| 339 |
if((*s < '0') || (*s > '9')) return -1; // counter strtol's skip-whitespace and sign permissiveness |
| 340 |
(void)strtol(s, &end, 10); // discard result |
| 341 |
if(errno) return -1; |
| 342 |
if(*end == ']') return 1; |
| 343 |
return -1; |
| 344 |
} |
| 345 |
|
| 346 |
int |
| 347 |
nscang_client_send_command(nscang_client_t *c, const char *command, int timeout) |
| 348 |
{ |
| 349 |
const int command_len = strlen(command); |
| 350 |
char command_buf[1024]; |
| 351 |
char cmd[32]; |
| 352 |
int rc, len; |
| 353 |
|
| 354 |
switch( has_timestamp_prefix(command) ) { |
| 355 |
case 0 : |
| 356 |
// There was no timestamp. Add one. |
| 357 |
snprintf(command_buf, sizeof(command_buf)-1, "[%u] %s", (unsigned int)time(NULL), command); |
| 358 |
break; |
| 359 |
case 1 : |
| 360 |
// Timestamp found, just copy everything |
| 361 |
strncpy(command_buf, command, sizeof(command_buf)-1); |
| 362 |
command_buf[sizeof(command_buf)-1] = '\0'; |
| 363 |
break; |
| 364 |
default : |
| 365 |
// Malformed timestamp |
| 366 |
c->_errno = NSCANG_ERROR_FAIL; |
| 367 |
snprintf(c->errstr, sizeof(c->errstr), "invalid time stamp format in command `%s'", command); |
| 368 |
return 0; |
| 369 |
} |
| 370 |
|
| 371 |
/* make sure command_buf is newline-terminated */ |
| 372 |
len = strlen(command_buf); |
| 373 |
if(command_buf[len-1] != '\n') { |
| 374 |
command_buf[len] = '\n'; |
| 375 |
len++; |
| 376 |
command_buf[len] = '\0'; |
| 377 |
} |
| 378 |
|
| 379 |
if (!nscang_client_send_moin(c, timeout)) |
| 380 |
return 0; |
| 381 |
|
| 382 |
snprintf(cmd, sizeof(cmd), "PUSH %d\r\n", len); |
| 383 |
if (!nscang_client_write(c, cmd, strlen(cmd), timeout)) |
| 384 |
return 0; |
| 385 |
|
| 386 |
rc = nscang_client_response(c, timeout); |
| 387 |
|
| 388 |
if (!rc) |
| 389 |
return 0; |
| 390 |
|
| 391 |
if (rc != NSCANG_RESP_OKAY) { |
| 392 |
c->_errno = NSCANG_ERROR_PROTOCOL_MISMATCH; |
| 393 |
return 0; |
| 394 |
} |
| 395 |
|
| 396 |
if (!nscang_client_write(c, command_buf, len, timeout)) |
| 397 |
return 0; |
| 398 |
|
| 399 |
rc = nscang_client_response(c, timeout); |
| 400 |
|
| 401 |
if (!rc) |
| 402 |
return 0; |
| 403 |
|
| 404 |
if (rc != NSCANG_RESP_OKAY) { |
| 405 |
c->_errno = NSCANG_ERROR_PROTOCOL_MISMATCH; |
| 406 |
return 0; |
| 407 |
} |
| 408 |
|
| 409 |
return 1; |
| 410 |
} |
| 411 |
|
| 412 |
int |
| 413 |
nscang_client_send_push(nscang_client_t *c, char *host, char *service, |
| 414 |
int status, char *message, int timeout) |
| 415 |
{ |
| 416 |
char command[1024]; |
| 417 |
|
| 418 |
if (service == NULL) |
| 419 |
snprintf(command, sizeof(command) - 1, |
| 420 |
"PROCESS_HOST_CHECK_RESULT;%s;%d;%s", |
| 421 |
host, status, message); |
| 422 |
else |
| 423 |
snprintf(command, sizeof(command) - 1, |
| 424 |
"PROCESS_SERVICE_CHECK_RESULT;%s;%s;%d;%s", |
| 425 |
host, service, status, message); |
| 426 |
|
| 427 |
return nscang_client_send_command(c, command, timeout); |
| 428 |
} |
| 429 |
|
| 430 |
int |
| 431 |
nscang_client_send_quit(nscang_client_t *c) |
| 432 |
{ |
| 433 |
if (c->state != NSCANG_STATE_MOIN) { |
| 434 |
c->_errno = NSCANG_ERROR_BAD_STATE; |
| 435 |
return 0; |
| 436 |
} |
| 437 |
|
| 438 |
if (!nscang_client_write(c, "QUIT\n", 5, 0)) |
| 439 |
return 0; |
| 440 |
|
| 441 |
return 1; |
| 442 |
} |
| 443 |
|
| 444 |
void |
| 445 |
nscang_client_ssl_error(char *buf, int buf_size, const char *msg) |
| 446 |
{ |
| 447 |
const char *ssl_err; |
| 448 |
|
| 449 |
strncpy(buf, msg, buf_size); |
| 450 |
buf_size--; |
| 451 |
|
| 452 |
ssl_err = ERR_reason_error_string(ERR_peek_error()); |
| 453 |
if (ssl_err != NULL) { |
| 454 |
strncat(buf, ":", buf_size); |
| 455 |
strncat(buf, ssl_err, buf_size); |
| 456 |
} |
| 457 |
} |
| 458 |
|
| 459 |
char * |
| 460 |
nscang_client_errstr(nscang_client_t *c, char *buf, int buf_size) |
| 461 |
{ |
| 462 |
switch (c->_errno) { |
| 463 |
case NSCANG_ERROR_SSL_CTX_CREATE: |
| 464 |
nscang_client_ssl_error(buf, buf_size, "Can't create SSL context"); |
| 465 |
break; |
| 466 |
case NSCANG_ERROR_SSL_CIPHERS: |
| 467 |
nscang_client_ssl_error(buf, buf_size, "Bad ciphers list"); |
| 468 |
break; |
| 469 |
case NSCANG_ERROR_SSL_BIO_CREATE: |
| 470 |
nscang_client_ssl_error(buf, buf_size, "Can't create BIO socket"); |
| 471 |
break; |
| 472 |
case NSCANG_ERROR_SSL_CREATE: |
| 473 |
nscang_client_ssl_error(buf, buf_size, "Can't create SSL"); |
| 474 |
break; |
| 475 |
case NSCANG_ERROR_SSL: |
| 476 |
nscang_client_ssl_error(buf, buf_size, "SSL error"); |
| 477 |
break; |
| 478 |
case NSCANG_ERROR_MALLOC: |
| 479 |
strncpy(buf, "Can't allocate memory", buf_size); |
| 480 |
break; |
| 481 |
case NSCANG_ERROR_TIMEOUT: |
| 482 |
strncpy(buf, "Timeout was reached", buf_size); |
| 483 |
break; |
| 484 |
case NSCANG_ERROR_TOO_LONG_RESPONSE: |
| 485 |
strncpy(buf, "Protocol mismatch - too long response", buf_size); |
| 486 |
break; |
| 487 |
case NSCANG_ERROR_BAD_PROTO_VERSION: |
| 488 |
snprintf(buf, buf_size, "Protocol mismatch - bad version '%s'", |
| 489 |
c->errstr); |
| 490 |
break; |
| 491 |
case NSCANG_ERROR_PROTOCOL_MISMATCH: |
| 492 |
strncpy(buf, "Protocol mismatch - unexpected server response", |
| 493 |
buf_size); |
| 494 |
break; |
| 495 |
case NSCANG_ERROR_UNKNOWN_RESPONSE: |
| 496 |
strncpy(buf, "Protocol mismatch - unknown server response", |
| 497 |
buf_size); |
| 498 |
break; |
| 499 |
case NSCANG_ERROR_BAIL: |
| 500 |
snprintf(buf, buf_size, "BAIL: %s", c->errstr); |
| 501 |
break; |
| 502 |
case NSCANG_ERROR_FAIL: |
| 503 |
snprintf(buf, buf_size, "FAIL: %s", c->errstr); |
| 504 |
break; |
| 505 |
case NSCANG_ERROR_BAD_STATE: |
| 506 |
snprintf(buf, buf_size, "Operation not permitted in state %d", |
| 507 |
c->state); |
| 508 |
break; |
| 509 |
default: |
| 510 |
buf[0] = 0x00; |
| 511 |
} |
| 512 |
buf[buf_size - 1] = 0x00; |
| 513 |
return buf; |
| 514 |
} |