diff options
| author | David M. Syzdek <david@syzdek.net> | 2022-05-26 20:10:42 -0800 |
|---|---|---|
| committer | David M. Syzdek <david@syzdek.net> | 2022-05-26 20:10:42 -0800 |
| commit | 7c1ca5b98ddc99b4836066e2332508e59184737e (patch) | |
| tree | 1597a147e1efda01428c430ea096eb2164488ec2 /lib | |
| parent | 3621746545555aefa5aa0c6d479039f994131595 (diff) | |
| download | tinytac-7c1ca5b98ddc99b4836066e2332508e59184737e.tar.gz tinytac-7c1ca5b98ddc99b4836066e2332508e59184737e.tar.bz2 tinytac-7c1ca5b98ddc99b4836066e2332508e59184737e.tar.xz tinytac-7c1ca5b98ddc99b4836066e2332508e59184737e.zip | |
libtinytac: refactoring tinytac_set_option_host()
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/libtinytac/lmemory.c | 110 | ||||
| -rw-r--r-- | lib/libtinytac/lmemory.h | 2 |
2 files changed, 101 insertions, 11 deletions
diff --git a/lib/libtinytac/lmemory.c b/lib/libtinytac/lmemory.c index 983d903..aafb95d 100644 --- a/lib/libtinytac/lmemory.c +++ b/lib/libtinytac/lmemory.c | |||
| @@ -51,6 +51,7 @@ | |||
| 51 | #include <arpa/inet.h> | 51 | #include <arpa/inet.h> |
| 52 | #include <netinet/in.h> | 52 | #include <netinet/in.h> |
| 53 | #include <netdb.h> | 53 | #include <netdb.h> |
| 54 | #include <errno.h> | ||
| 54 | #include <assert.h> | 55 | #include <assert.h> |
| 55 | 56 | ||
| 56 | 57 | ||
| @@ -124,7 +125,7 @@ tinytac_verify_is_obj( | |||
| 124 | #pragma mark - Variables | 125 | #pragma mark - Variables |
| 125 | 126 | ||
| 126 | const char * tinytac_dflt_hosts = TTAC_DFLT_HOSTS; | 127 | const char * tinytac_dflt_hosts = TTAC_DFLT_HOSTS; |
| 127 | char tinytac_dflt_hosts_buff[128]; | 128 | char * tinytac_dflt_hosts_buff = NULL; |
| 128 | unsigned tinytac_dflt_opts = TTAC_DFLT_OPTS; | 129 | unsigned tinytac_dflt_opts = TTAC_DFLT_OPTS; |
| 129 | unsigned tinytac_dflt_opts_neg = TTAC_DFLT_OPTS_NEG; | 130 | unsigned tinytac_dflt_opts_neg = TTAC_DFLT_OPTS_NEG; |
| 130 | char ** tinytac_dflt_keys = NULL; | 131 | char ** tinytac_dflt_keys = NULL; |
| @@ -418,23 +419,112 @@ tinytac_set_option_host( | |||
| 418 | TinyTac * tt, | 419 | TinyTac * tt, |
| 419 | const char * invalue ) | 420 | const char * invalue ) |
| 420 | { | 421 | { |
| 421 | char * ostr; | 422 | int rc; |
| 423 | char * buff; | ||
| 424 | char * ostr; | ||
| 425 | const char * dflt; | ||
| 426 | size_t size; | ||
| 427 | char * eol; | ||
| 428 | char * str; | ||
| 429 | void * ptr; | ||
| 430 | size_t budps_len; | ||
| 431 | BindleURLDesc ** budps; | ||
| 422 | 432 | ||
| 423 | TinyTacDebugTrace(); | 433 | TinyTacDebugTrace(); |
| 424 | 434 | ||
| 425 | if ((tt)) | 435 | dflt = ((tt)) ? tinytac_dflt_hosts : TTAC_DFLT_HOSTS; |
| 436 | invalue = ((invalue)) ? invalue : dflt; | ||
| 437 | size = 0; | ||
| 438 | budps = NULL; | ||
| 439 | budps_len = 0; | ||
| 440 | |||
| 441 | if ((buff = tinytacb_strdup(invalue)) == NULL) | ||
| 442 | return(TTAC_ENOMEM); | ||
| 443 | |||
| 444 | str = buff; | ||
| 445 | while( ((str)) && ((str[0])) ) | ||
| 426 | { | 446 | { |
| 427 | invalue = ((invalue)) ? invalue : TTAC_DFLT_HOSTS; | 447 | // find next whitespace |
| 428 | tinytacb_strlcpy(tinytac_dflt_hosts_buff, invalue, sizeof(tinytac_dflt_hosts_buff)); | 448 | eol = strchr(str, ' '); |
| 429 | tinytac_dflt_hosts = tinytac_dflt_hosts_buff; | 449 | eol = ((eol)) ? eol : strchr(str, '\t'); |
| 430 | return(TTAC_SUCCESS); | 450 | if ((eol)) |
| 451 | eol[0] = '\0'; | ||
| 452 | |||
| 453 | // skip empty host | ||
| 454 | if (str[0] == '\0') | ||
| 455 | { | ||
| 456 | str = ((eol)) ? &eol[1] : NULL; | ||
| 457 | continue; | ||
| 458 | }; | ||
| 459 | |||
| 460 | // increase size of URL list | ||
| 461 | if ((ptr = realloc(budps, sizeof(BindleURLDesc *)*(budps_len+2))) == NULL) | ||
| 462 | { | ||
| 463 | free(buff); | ||
| 464 | tinytac_tinytac_free_budps(budps); | ||
| 465 | return(TTAC_ENOMEM); | ||
| 466 | }; | ||
| 467 | budps[budps_len+0] = NULL; | ||
| 468 | budps[budps_len+1] = NULL; | ||
| 469 | |||
| 470 | // parse URL | ||
| 471 | if ((rc = tinytacb_urldesc_parse(str, &budps[budps_len])) != 0) | ||
| 472 | { | ||
| 473 | free(buff); | ||
| 474 | tinytac_tinytac_free_budps(budps); | ||
| 475 | return((rc = ENOMEM) ? TTAC_ENOMEM : TTAC_EINVAL); | ||
| 476 | }; | ||
| 477 | |||
| 478 | // check URL result | ||
| 479 | if ( ((budps[budps_len]->bud_scheme)) && (!(strcasecmp("tacacs+", budps[budps_len]->bud_scheme))) ) | ||
| 480 | { | ||
| 481 | free(buff); | ||
| 482 | tinytac_tinytac_free_budps(budps); | ||
| 483 | return(TTAC_EINVAL); | ||
| 484 | }; | ||
| 485 | if ( ((budps[budps_len]->bud_userinfo)) || ((budps[budps_len]->bud_path)) || | ||
| 486 | ((budps[budps_len]->bud_query)) || ((budps[budps_len]->bud_fragment)) ) | ||
| 487 | { | ||
| 488 | free(buff); | ||
| 489 | tinytac_tinytac_free_budps(budps); | ||
| 490 | return(TTAC_EINVAL); | ||
| 491 | }; | ||
| 492 | |||
| 493 | // resolve URL host | ||
| 494 | if ((rc = tinytacb_urldesc_resolve(budps[budps_len], AF_UNSPEC, TTAC_DFLT_PORT)) != 0) | ||
| 495 | { | ||
| 496 | free(buff); | ||
| 497 | tinytac_tinytac_free_budps(budps); | ||
| 498 | return((rc = ENOMEM) ? TTAC_ENOMEM : TTAC_EINVAL); | ||
| 499 | }; | ||
| 500 | |||
| 501 | // shift string | ||
| 502 | str = ((eol)) ? &eol[1] : NULL; | ||
| 431 | }; | 503 | }; |
| 432 | 504 | ||
| 433 | invalue = ((invalue)) ? invalue : tinytac_dflt_hosts; | 505 | free(buff); |
| 506 | |||
| 507 | // saves host string | ||
| 434 | if ((ostr = tinytacb_strdup(invalue)) == NULL) | 508 | if ((ostr = tinytacb_strdup(invalue)) == NULL) |
| 509 | { | ||
| 510 | tinytac_tinytac_free_budps(budps); | ||
| 435 | return(TTAC_ENOMEM); | 511 | return(TTAC_ENOMEM); |
| 436 | free(tt->hosts); | 512 | }; |
| 437 | tt->hosts = ostr; | 513 | if (!(tt)) |
| 514 | { | ||
| 515 | tinytac_tinytac_free_budps(budps); | ||
| 516 | if ((tinytac_dflt_hosts_buff)) | ||
| 517 | free(tinytac_dflt_hosts_buff); | ||
| 518 | tinytac_dflt_hosts_buff = ostr; | ||
| 519 | tinytac_dflt_hosts = tinytac_dflt_hosts_buff; | ||
| 520 | } else | ||
| 521 | { | ||
| 522 | tinytac_tinytac_free_budps(tt->budps); | ||
| 523 | tt->budps = budps; | ||
| 524 | if ((tt->hosts)) | ||
| 525 | free(tt->hosts); | ||
| 526 | tt->hosts = ostr; | ||
| 527 | }; | ||
| 438 | 528 | ||
| 439 | return(TTAC_SUCCESS); | 529 | return(TTAC_SUCCESS); |
| 440 | } | 530 | } |
diff --git a/lib/libtinytac/lmemory.h b/lib/libtinytac/lmemory.h index 150c69f..2641c8b 100644 --- a/lib/libtinytac/lmemory.h +++ b/lib/libtinytac/lmemory.h | |||
| @@ -63,7 +63,7 @@ | |||
| 63 | #pragma mark - Variables | 63 | #pragma mark - Variables |
| 64 | 64 | ||
| 65 | extern const char * tinytac_dflt_hosts; | 65 | extern const char * tinytac_dflt_hosts; |
| 66 | extern char tinytac_dflt_hosts_buff[128]; | 66 | extern char * tinytac_dflt_hosts_buff; |
| 67 | extern unsigned tinytac_dflt_opts; | 67 | extern unsigned tinytac_dflt_opts; |
| 68 | extern unsigned tinytac_dflt_opts_neg; | 68 | extern unsigned tinytac_dflt_opts_neg; |
| 69 | extern char ** tinytac_dflt_keys; | 69 | extern char ** tinytac_dflt_keys; |
