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 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944
|
/*
* proxycnf.c - mDNS Proxy, configuration
*/
/*
* Copyright (c) 2000 Japan Network Information Center. All rights reserved.
*
* By using this file, you agree to the terms and conditions set forth bellow.
*
* LICENSE TERMS AND CONDITIONS
*
* The following License Terms and Conditions apply, unless a different
* license is obtained from Japan Network Information Center ("JPNIC"),
* a Japanese association, Kokusai-Kougyou-Kanda Bldg 6F, 2-3-4 Uchi-Kanda,
* Chiyoda-ku, Tokyo 101-0047, Japan.
*
* 1. Use, Modification and Redistribution (including distribution of any
* modified or derived work) in source and/or binary forms is permitted
* under this License Terms and Conditions.
*
* 2. Redistribution of source code must retain the copyright notices as they
* appear in each source code file, this License Terms and Conditions.
*
* 3. Redistribution in binary form must reproduce the Copyright Notice,
* this License Terms and Conditions, in the documentation and/or other
* materials provided with the distribution. For the purposes of binary
* distribution the "Copyright Notice" refers to the following language:
* "Copyright (c) Japan Network Information Center. All rights reserved."
*
* 4. Neither the name of JPNIC may be used to endorse or promote products
* derived from this Software without specific prior written approval of
* JPNIC.
*
* 5. Disclaimer/Limitation of Liability: THIS SOFTWARE IS PROVIDED BY JPNIC
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JPNIC BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
*
* 6. Indemnification by Licensee
* Any person or entities using and/or redistributing this Software under
* this License Terms and Conditions shall defend indemnify and hold
* harmless JPNIC from and against any and all judgements damages,
* expenses, settlement liabilities, cost and other liabilities of any
* kind as a result of use and redistribution of this Software or any
* claim, suite, action, litigation or proceeding by any third party
* arising out of or relates to this License Terms and Conditions.
*
* 7. Governing Law, Jurisdiction and Venue
* This License Terms and Conditions shall be governed by and and
* construed in accordance with the law of Japan. Any person or entities
* using and/or redistributing this Software under this License Terms and
* Conditions hereby agrees and consent to the personal and exclusive
* jurisdiction and venue of Tokyo District Court of Japan.
*/
#ifndef lint
static char *rcsid = "$Id: proxycnf.c,v 1.25 2001/10/31 08:51:29 m-kasahr Exp $";
#endif
#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#ifdef WIN32
#include <windows.h>
#include <winsock.h>
#else /* for normal systems */
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#endif
#include "mdnsproxy.h" /* Common definitions for mDNS proxy */
/*
* Note that logging macros (FATAL, WARN and TRACE) cannot be used
* until logging file is configured, i.e. log_configure() is called.
* Be careful.
*/
/*
* default config file (path & basename), depend on MACHINE
*/
#ifndef CONFIG_PATH
#warning "no \"CONFIG_PATH\", use "\"./\" as default"
#define CONFIG_PATH "./"
#endif
#ifndef CONFIG_FILE
#warning "no \"CONFIG_FILE\", use "mdnsproxy.ini" as default"
#define CONFIG_FILE "mdnsproxy.ini"
#endif
#ifndef CONFIG_HOME
#warning "no \"CONFIG_HOME\", use "\"./\" as default"
#define CONFIG_HOME "./"
#endif
static u_char *confFile = CONFIG_FILE ;
static u_char *confPath[] = {
#if defined(DEBUG) && !defined(UNIX)
"./",
CONFIG_HOME,
#endif
CONFIG_PATH,
NULL
} ;
static u_char *confCommands[] = {
KW_LISTEN,
KW_FORWARD,
KW_CLIENT_ENCODING,
KW_MDN_CONF_FILE,
KW_LOG_FILE,
KW_LOG_LEVEL,
KW_MDN_LOG_LEVEL,
KW_SYSLOG_FACILITY,
KW_USER_ID,
KW_GROUP_ID,
KW_ROOT_DIRECTORY,
KW_ALLOW_ACCESS,
KW_LOG_ON_DENIED,
NULL
};
static u_char *confObsoleteCommands[] = {
KW_CLIENT_TRANSLATION,
KW_ALTERNATE_ENCODING,
KW_NORMALIZE,
KW_SERVER_TRANSLATION,
KW_ENCODING_ALIAS_FILE,
NULL
};
static u_char *expandName(u_char *name, u_char *buff)
{
#ifndef UNIX
int inEnv = FALSE ;
u_char *bp, *ep ;
u_char env[512] ;
for (bp = buff, ep = env ; *name != '\0' ; name++) {
if (inEnv == FALSE) {
if (*name == '$') {
inEnv = TRUE ;
ep = env ;
} else {
*bp++ = *name ;
*bp = '\0' ;
}
} else {
if (*name == '(') {
/* skip this */
} else if (*name != ')') {
*ep++ = *name ;
*ep = '\0' ;
} else if ((ep = getenv(env)) == NULL) {
return NULL ;
} else {
while (*ep != '\0') {
*bp++ = *ep++ ;
}
*bp = '\0' ;
inEnv = FALSE ;
}
}
}
#else /* UNIX */
strcpy(buff, name);
#endif /* UNIX */
return buff ;
}
static FILE *openConfig(u_char *fname)
{
int i ;
FILE *fp = NULL ;
u_char path[512] ;
u_char name[512] ;
/*
* if coinfiguration file specified, open it
*/
if (fname != NULL) {
if (expandName(fname, name) == NULL) {
return NULL ;
}
if ((fp = fopen(name, "r")) == NULL) {
return NULL ;
}
return fp ;
}
/*
* otherwise, look for configuration file in search path
*/
for (i = 0 ; confPath[i] != NULL ; i++) {
if (expandName(confPath[i], path) == NULL) {
continue ;
}
sprintf(name, "%s%s", path, confFile) ;
if ((fp = fopen(name, "r")) != NULL) {
return fp ;
}
}
return NULL ;
}
/*
* configuration data in file consists of
*
* key value ...
*
* lines. This module hold them as following list.
*/
typedef struct _CONF *CNFPTR ;
typedef struct _CONF {
int lineNo ;
CNFPTR prev ;
CNFPTR next ;
u_char *key ; /* really, buffer for key & val */
int nVal ;
u_char *aVal[1] ; /* really, follows 'nVal' entries */
} CNFREC ;
static CNFREC confList = { 0 } ;
static void disposeData(void)
{
CNFPTR p ;
if (confList.prev == NULL || confList.next == NULL) {
return ;
}
while ((p = confList.next) != &confList) {
confList.next = p->next ;
free(p->key) ;
free(p) ;
}
}
static u_char *getString(u_char *p, CNFPTR pCnf)
{
/*
* mark start of value string
*/
pCnf->aVal[pCnf->nVal++] = p ;
/*
* look for end of string, any space
*/
for ( ; *p != '\0' ; p++) {
if (isspace(*p)) {
break ;
}
}
if (*p != '\0') {
*p++ = '\0' ;
}
return p ;
}
static u_char *getQuoted(u_char *p, CNFPTR pCnf)
{
if (*p != '"') { /* Oh, something wrong !! */
return p ;
}
p++ ; /* skip leading '"' */
/*
* mark start of value string
*/
pCnf->aVal[pCnf->nVal++] = p ;
/*
* look for terminating '"', may be escaped with '\'
*/
while (*p != '\0' && *p != '\n' && *p != '\r') {
if (*p == '"') {
break ;
} else if (*p == '\\') { /* quoted pair */
p += 2 ;
} else {
p += 1 ;
}
}
if (*p != '\0') {
*p++ = '\0' ;
}
return p ;
}
static BOOL findCommand(u_char *name)
{
u_char **cmd;
for (cmd = (u_char **)confCommands; *cmd != NULL; cmd++) {
if (strcmp(*cmd, name) == 0)
return TRUE;
}
return FALSE;
}
static BOOL findObsoleteCommand(u_char *name)
{
u_char **cmd;
for (cmd = (u_char **)confObsoleteCommands; *cmd != NULL; cmd++) {
if (strcmp(*cmd, name) == 0)
return TRUE;
}
return FALSE;
}
static BOOL appendData(u_char *line, int lineNo)
{
int len ;
u_char *pStr, *p ;
CNFPTR pCnf, pNew ;
CNFPTR prev, next ;
/*
* list is not initialized, initialize it
*/
if (confList.prev == NULL || confList.next == NULL) {
confList.prev = &confList ;
confList.next = &confList ;
}
/*
* prepare buffers
*/
len = strlen(line) ;
pCnf = (CNFPTR) malloc(sizeof(CNFREC) + sizeof(u_char *) * len) ;
pStr = malloc(len + 2) ;
if (pStr == NULL || pCnf == NULL) {
fprintf(stderr, "appendData - cannot allocate memory\n") ;
if (pStr != NULL) free(pStr) ;
if (pCnf != NULL) free(pCnf) ;
return FALSE ;
}
memset(pCnf, 0, sizeof(CNFREC)) ;
for (p = pStr ; *line != '\0' ; ) {
if (*line == '\n' || *line == '\r') {
break ;
}
*p++ = *line++ ;
}
*p = '\0' ;
/*
* parse line (save results into pCnf)
*
* term may be string or quoted-string
*/
for (p = pStr ; *p != '\0' ; ) {
if (isspace(*p)) {
p += 1 ;
} else if (*p == '#') {
break ;
} else if (*p == '"') {
p = getQuoted(p, pCnf) ;
} else {
p = getString(p, pCnf) ;
}
}
/*
* if no term found, skip the line
*/
if (pCnf->nVal == 0) {
free(pStr) ;
free(pCnf) ;
return TRUE ;
}
if (findObsoleteCommand(pStr)) {
fprintf(stderr, "appendData - obsolete command \"%.100s\", line %d\n",
pStr, lineNo) ;
return FALSE;
}
if (!findCommand(pStr)) {
fprintf(stderr, "appendData - unknown command \"%.100s\", line %d\n",
pStr, lineNo) ;
return FALSE;
}
/*
* create resulting CNF record
*/
pNew = (CNFPTR) malloc(sizeof(CNFREC) + sizeof(u_char *) * pCnf->nVal) ;
if (pNew == NULL) {
fprintf(stderr, "appendData - cannot allocate memory\n") ;
free(pStr) ;
free(pCnf) ;
return FALSE ;
}
memset(pNew, 0, sizeof(CNFREC)) ;
pNew->lineNo = lineNo;
pNew->key = pStr ;
pNew->nVal = pCnf->nVal ;
memcpy(pNew->aVal, pCnf->aVal, sizeof(u_char *) * pCnf->nVal) ;
free(pCnf) ;
/*
* link to list
*/
prev = confList.prev ;
next = prev->next ;
pNew->prev = confList.prev ;
pNew->next = &confList ;
prev->next = pNew ;
next->prev = pNew ;
return TRUE ;
}
/*
* config_load - load mDNS Proxy configuration data
*/
BOOL config_load(int ac, char *av[])
{
int i ;
u_char *conf ;
FILE *fp ;
u_char line[512] ;
int lineNo ;
/*
* check if alternate config file specified
*/
for (i = 1, conf = CONFIG_PATH "/" CONFIG_FILE ; i < ac ; i++) {
if (strcmp(av[i], "-config") == 0) {
if ((i + 1) < ac) {
conf = av[i+=1] ;
}
}
}
/*
* open configuration file
*/
if ((fp = openConfig(conf)) == NULL) {
fprintf(stderr, "config_load - cannot open the configuration file, "
"%s, \"%.100s\"\n", strerror(errno), conf) ;
return FALSE ;
}
/*
* read and parse configuration data (per line)
*/
lineNo = 0;
while (fgets(line, 512, fp) != NULL) {
lineNo++;
if (appendData(line, lineNo) != TRUE) {
fprintf(stderr, "config_load - error in \"%s\"\n", conf) ;
fclose(fp) ;
return FALSE ;
}
}
fclose(fp) ;
return TRUE ;
}
/*
* config_dump - dump contents of mDNS Proxy configuration data
*/
static BOOL haveSpace(u_char *str)
{
for ( ; *str != '\0' ; str++) {
if (isspace(*str)) {
return TRUE ;
}
}
return FALSE ;
}
void config_dump(FILE *ofp)
{
CNFPTR p ;
int i ;
if (confList.next == NULL || confList.prev == NULL) {
return ;
}
for (p = confList.next ; p != &confList ; p = p->next) {
if (haveSpace(p->key)) {
fprintf(ofp, "<%s>", p->key) ;
} else {
fprintf(ofp, "<%s>", p->key) ;
}
for (i = 0 ; i < p->nVal ; i++) {
if (haveSpace(p->aVal[i])) {
fprintf(ofp, " \"%s\"", p->aVal[i]) ;
} else {
fprintf(ofp, " %s", p->aVal[i]) ;
}
}
fprintf(ofp, "\n") ;
}
}
/*
* config_free - dispose mDNS configuration data
*/
void config_free(void)
{
disposeData() ;
}
/*
* config_query_value - query configuration data
*
* this is generic interface to access configuration data
* but note, this function cannot work with multiple
* configuration data, such as client-translation
*/
BOOL config_query_value(char *key, int *count, char ***array, int *lineNo)
{
CNFPTR p ;
for (p = confList.next ; p != &confList ; p = p->next) {
if (strcmp(p->key, key) != 0) {
continue ;
}
*count = p->nVal ;
*array = (char **) p->aVal ;
*lineNo = p->lineNo;
return TRUE ;
}
return FALSE ;
}
/*
* config_query_listen, config_query_forward
*
* queries sockaddr (really sockaddr_in) of proxy to listen,
* and DNS server to which proxy forwards the requests.
* These function set
*
* addr->sin_family <- AF_INET
* addr->sin_addr <- specified/default
* addr->sin_port <- specified/default
*
* for 'listen' address, both sin_addr/sin_port have default values,
* but for 'forward' address, there is no default for 'sin_addr',
* and result error on such case.
*
* NOTE: for DNS proxy, both listen/forward address should be
* specified with xx.xx.xx.xx notation, never be host name
*/
#define DEFAULT_ADDR INADDR_ANY
#define DEFAULT_PORT 53
static CNFPTR queryData(u_char *key)
{
CNFPTR p ;
for (p = confList.next ; p != &confList ; p = p->next) {
if (strcmp(p->key, key) == 0) {
return p ;
}
}
return NULL ;
}
/*
* Parse `host:port'. Either `host' or `port' can be omitted.
*/
static BOOL getHostPort(u_char *arg, struct sockaddr_in *iaddr, int lineNo)
{
unsigned int octets[4];
unsigned short port;
int digit_count;
int octet_count;
const char *p = arg;
octets[0] = (DEFAULT_ADDR >> 24) & 0xff;
octets[1] = (DEFAULT_ADDR >> 16) & 0xff;
octets[2] = (DEFAULT_ADDR >> 8) & 0xff;
octets[3] = (DEFAULT_ADDR) & 0xff;
port = DEFAULT_PORT;
/*
* Parse an dot noted IP address.
*/
if ('0' <= *p && *p <= '9') {
octet_count = 0;
while (octet_count < 4) {
octets[octet_count] = 0;
if (*p == '0' && '0' <= *(p + 1) && *(p + 1) <= '9') {
WARN("getHostPort - invalid address \"%.100s\", line %d\n",
arg, lineNo);
return FALSE;
}
for (digit_count = 0; '0' <= *p && *p <= '9'; p++, digit_count++)
octets[octet_count] = octets[octet_count] * 10 + (*p - '0');
if (digit_count == 0 || digit_count > 3 ||
octets[octet_count] > 255) {
WARN("getHostPort - invalid address \"%.100s\" line %d\n",
arg, lineNo);
return FALSE;
}
octet_count++;
if (octet_count != 4) {
if (*p != '.') {
WARN("getHostPort - malformed address \"%.100s\", "
"line %d\n", arg, lineNo);
return FALSE;
}
p++;
}
}
}
/*
* Parse an optional port number preceded by `:'.
*/
if (*p == ':') {
port = 0;
p++;
if (*p == '0' && '0' <= *(p + 1) && *(p + 1) <= '9') {
WARN("getHostPort - invalid port number \"%.100s\", line %d\n",
arg, lineNo);
return FALSE;
}
for (digit_count = 0; '0' <= *p && *p <= '9'; p++, digit_count++)
port = port * 10 + (*p - '0');
if (digit_count == 0 && *p == '\0') {
port = DEFAULT_PORT;
} else if (digit_count == 0 || digit_count > 5 || port == 0 ||
port > 65535) {
WARN("getHostPort - invalid port number \"%.100s\", line %d\n",
arg, lineNo);
return FALSE;
}
}
if (*p != '\0') {
WARN("getHostPort - invalid address \"%.100s\", line %d\n",
arg, lineNo);
return FALSE;
}
/*
* Put the result into `address' and `port'.
*/
iaddr->sin_addr.s_addr = htonl((octets[0] << 24) + (octets[1] << 16)
+ (octets[2] << 8) + octets[3]);
iaddr->sin_port = htons(port);
return TRUE;
}
/*
* Parse `host:port'. `port' can be omitted.
*/
static BOOL getHostPort2(u_char *arg, struct sockaddr_in *iaddr,
int lineNo)
{
if (*arg == ':') {
WARN("getHostPort2 - missing host name \"%.100s\", line %d\n",
arg, lineNo);
return FALSE;
}
return getHostPort(arg, iaddr, lineNo);
}
BOOL config_query_listen(struct sockaddr *addr)
{
CNFPTR pListen ;
struct sockaddr_in *iaddr ;
memset(addr, 0, sizeof(struct sockaddr)) ;
iaddr = (struct sockaddr_in *) addr ;
if ((pListen = queryData(KW_LISTEN)) == NULL) {
iaddr->sin_addr.s_addr = htonl(DEFAULT_ADDR) ;
iaddr->sin_port = htons(DEFAULT_PORT) ;
return TRUE ;
}
if (pListen->nVal != 2) {
WARN("config_query_listen - wrong # of args for \"%s\", line %d\n",
KW_LISTEN, pListen->lineNo) ;
return FALSE ;
}
if (!getHostPort(pListen->aVal[1], iaddr, pListen->lineNo)) {
return FALSE;
}
iaddr->sin_family = AF_INET ;
return TRUE ;
}
BOOL config_query_forward(struct sockaddr *addr)
{
CNFPTR pForward ;
struct sockaddr_in *iaddr ;
memset(addr, 0, sizeof(struct sockaddr)) ;
iaddr = (struct sockaddr_in *) addr ;
iaddr->sin_family = AF_INET ;
if ((pForward = queryData(KW_FORWARD)) == NULL) {
WARN("config_query_forward - \"%s\" not found in the configuration "
"file\n", KW_FORWARD) ;
return FALSE ;
}
if (pForward->nVal != 2 && pForward->nVal != 3) {
WARN("config_query_forward - wrong # of args for \"%s\", line %d\n",
KW_FORWARD, pForward->lineNo) ;
return FALSE ;
}
if (!getHostPort2(pForward->aVal[1], iaddr, pForward->lineNo)) {
return FALSE ;
}
iaddr->sin_family = AF_INET ;
return TRUE ;
}
/*
* config_query_restrict - query 'source-restrict' flag
*/
BOOL config_query_restrict(BOOL *src_restrict)
{
CNFPTR pForward ;
if ((pForward = queryData(KW_FORWARD)) == NULL) {
*src_restrict = FALSE ;
} else if (pForward->nVal < 3) {
*src_restrict = FALSE ;
} else if (strcmp(pForward->aVal[2], "bind4compat") != 0) {
*src_restrict = FALSE ;
} else {
*src_restrict = TRUE ;
}
return TRUE ;
}
/*
* config_query_log_on_denied - query 'log-on-denied' flag
*/
BOOL config_query_log_on_denied(BOOL *flag)
{
CNFPTR pForward ;
if ((pForward = queryData("log-on-denied")) == NULL) {
*flag = FALSE ;
return TRUE ;
}
if (pForward->nVal != 2)
return FALSE ;
if (strcmp(pForward->aVal[1], "yes") == 0)
*flag = TRUE ;
else if (strcmp(pForward->aVal[1], "no") == 0)
*flag = FALSE ;
else
return FALSE ;
return TRUE;
}
/*
* config_query_open, config_query_more, config_query_close
*
* this is generic interface to access configuration data
* for multiple entires for same key value.
*/
config_ctx_t config_query_open(char *key, int *count, char ***array,
int *lineNo)
{
CNFPTR p ;
for (p = confList.next ; p != &confList ; p = p->next) {
if (strcmp(p->key, key) != 0) {
continue ;
}
*count = p->nVal ;
*array = (char **) p->aVal ;
*lineNo = p->lineNo;
return (config_ctx_t) p ;
}
return NULL ;
}
config_ctx_t config_query_more(config_ctx_t ctx, int *count, char ***array,
int *lineNo)
{
CNFPTR p = (CNFPTR) ctx ;
CNFPTR np ;
for (np = p->next ; np != &confList ; np = np->next) {
if (strcmp(np->key, p->key) != 0) {
continue ;
}
*count = np->nVal ;
*array = (char **) np->aVal ;
*lineNo = np->lineNo;
return (config_ctx_t) np ;
}
return NULL ;
}
void config_query_close(config_ctx_t ctx)
{
/* nothing to do */
}
#ifdef TEST
/*
* test driver for 'config' module
*/
static void dumpaddr(u_char *str, struct sockaddr *addr)
{
u_char *p ;
struct sockaddr_in *iaddr = (struct sockaddr_in *) addr ;
printf("%s ", str) ;
p = (u_char *) &iaddr->sin_addr ;
printf("address %d.%d.%d.%d",
(p[0] & 0xff), (p[1] & 0xff), (p[2] & 0xff), (p[3] & 0xff)) ;
p = (u_char *) &iaddr->sin_port ;
printf(" port %d", ((p[0] & 0xff) * 256 + (p[1] & 0xff))) ;
printf("\n") ;
}
static void dumpvalue(char *key, int count, char **array)
{
int i ;
for (i = 0 ; i < count ; i++) {
printf("%s ", array[i]) ;
}
printf("\n") ; fflush(stdout) ;
}
int main(int ac, char *av[])
{
int stat ;
struct sockaddr addr ;
int count ;
char **array ;
config_ctx_t ctx ;
int lineNo;
if ((stat = config_load(ac, av)) != TRUE) {
printf("config_load failed %d\n", stat) ;
return 1 ;
}
config_dump(stdout) ;
if (config_query_value(KW_LISTEN, &count, &array, &lineNo) == TRUE) {
dumpvalue("listen", count, array) ;
}
if (config_query_value(KW_FORWARD, &count, &array, &lineNo) == TRUE) {
dumpvalue("forward", count, array) ;
}
ctx = config_query_open("client-translation", &count, &array, &lineNo) ;
while (ctx != NULL) {
dumpvalue("client-translation", count, array) ;
ctx = config_query_more(ctx, &count, &array, &lineNo) ;
}
config_query_close(ctx) ;
if (config_query_listen(&addr) != TRUE) {
printf("no \"listen\" data\n") ;
} else {
dumpaddr("listen", &addr) ;
}
if (config_query_forward(&addr) != TRUE) {
printf("no \"forward\" data\n") ;
} else {
dumpaddr("forward", &addr) ;
}
config_free() ;
return 0 ;
}
#endif /* TEST */
|