1 /* $NetBSD: db_common.c,v 1.1.1.4 2013/01/02 18:58:56 tron Exp $ */ 2 3 /*++ 4 /* NAME 5 /* db_common 3 6 /* SUMMARY 7 /* utilities common to network based dictionaries 8 /* SYNOPSIS 9 /* #include "db_common.h" 10 /* 11 /* int db_common_parse(dict, ctx, format, query) 12 /* DICT *dict; 13 /* void **ctx; 14 /* const char *format; 15 /* int query; 16 /* 17 /* void db_common_free_context(ctx) 18 /* void *ctx; 19 /* 20 /* int db_common_expand(ctx, format, value, key, buf, quote_func); 21 /* void *ctx; 22 /* const char *format; 23 /* const char *value; 24 /* const char *key; 25 /* VSTRING *buf; 26 /* void (*quote_func)(DICT *, const char *, VSTRING *); 27 /* 28 /* int db_common_check_domain(domain_list, addr); 29 /* STRING_LIST *domain_list; 30 /* const char *addr; 31 /* 32 /* void db_common_sql_build_query(query,parser); 33 /* VSTRING *query; 34 /* CFG_PARSER *parser; 35 /* 36 /* DESCRIPTION 37 /* This module implements utilities common to network based dictionaries. 38 /* 39 /* \fIdb_common_parse\fR parses query and result substitution templates. 40 /* It must be called for each template before any calls to 41 /* \fIdb_common_expand\fR. The \fIctx\fB argument must be initialized to 42 /* a reference to a (void *)0 before the first template is parsed, this 43 /* causes memory for the context to be allocated and the new pointer is 44 /* stored in *ctx. When the dictionary is closed, this memory must be 45 /* freed with a final call to \fBdb_common_free_context\fR. 46 /* 47 /* Calls for additional templates associated with the same map must use the 48 /* same ctx argument. The context accumulates run-time lookup key and result 49 /* validation information (inapplicable keys or results are skipped) and is 50 /* needed later in each call of \fIdb_common_expand\fR. A non-zero return 51 /* value indicates that data-depedent '%' expansions were found in the input 52 /* template. 53 /* 54 /* db_common_alloc() provides a way to use db_common_parse_domain() 55 /* etc. without prior db_common_parse() call. 56 /* 57 /* \fIdb_common_expand\fR expands the specifiers in \fIformat\fR. 58 /* When the input data lacks all fields needed for the expansion, zero 59 /* is returned and the query or result should be skipped. Otherwise 60 /* the expansion is appended to the result buffer (after a comma if the 61 /* the result buffer is not empty). 62 /* 63 /* If not NULL, the \fBquote_func\fR callback performs database-specific 64 /* quoting of each variable before expansion. 65 /* \fBvalue\fR is the lookup key for query expansion and result for result 66 /* expansion. \fBkey\fR is NULL for query expansion and the lookup key for 67 /* result expansion. 68 /* .PP 69 /* The following '%' expansions are performed on \fBvalue\fR: 70 /* .IP %% 71 /* A literal percent character. 72 /* .IP %s 73 /* The entire lookup key \fIaddr\fR. 74 /* .IP %u 75 /* If \fBaddr\fR is a fully qualified address, the local part of the 76 /* address. Otherwise \fIaddr\fR. 77 /* .IP %d 78 /* If \fIaddr\fR is a fully qualified address, the domain part of the 79 /* address. Otherwise the query against the database is suppressed and 80 /* the lookup returns no results. 81 /* 82 /* The following '%' expansions are performed on the lookup \fBkey\fR: 83 /* .IP %S 84 /* The entire lookup key \fIkey\fR. 85 /* .IP %U 86 /* If \fBkey\fR is a fully qualified address, the local part of the 87 /* address. Otherwise \fIkey\fR. 88 /* .IP %D 89 /* If \fIkey\fR is a fully qualified address, the domain part of the 90 /* address. Otherwise the query against the database is suppressed and 91 /* the lookup returns no results. 92 /* .PP 93 /* \fIdb_common_check_domain\fR() checks the domain list so 94 /* that query optimization can be performed. The result is >0 95 /* (match found), 0 (no match), or <0 (dictionary error code). 96 /* 97 /* .PP 98 /* \fIdb_common_sql_build_query\fR builds the "default"(backwards compatible) 99 /* query from the 'table', 'select_field', 'where_field' and 100 /* 'additional_conditions' parameters, checking for errors. 101 /* 102 /* DIAGNOSTICS 103 /* Fatal errors: invalid substitution format, invalid string_list pattern, 104 /* insufficient parameters. 105 /* SEE ALSO 106 /* dict(3) dictionary manager 107 /* string_list(3) string list pattern matching 108 /* match_ops(3) simple string or host pattern matching 109 /* LICENSE 110 /* .ad 111 /* .fi 112 /* The Secure Mailer license must be distributed with this software. 113 /* AUTHOR(S) 114 /* Wietse Venema 115 /* IBM T.J. Watson Research 116 /* P.O. Box 704 117 /* Yorktown Heights, NY 10598, USA 118 /* 119 /* Liviu Daia 120 /* Institute of Mathematics of the Romanian Academy 121 /* P.O. BOX 1-764 122 /* RO-014700 Bucharest, ROMANIA 123 /* 124 /* Jose Luis Tallon 125 /* G4 J.E. - F.I. - U.P.M. 126 /* Campus de Montegancedo, S/N 127 /* E-28660 Madrid, SPAIN 128 /* 129 /* Victor Duchovni 130 /* Morgan Stanley 131 /*--*/ 132 133 /* 134 * System library. 135 */ 136 #include "sys_defs.h" 137 #include <stddef.h> 138 #include <string.h> 139 140 /* 141 * Global library. 142 */ 143 #include "cfg_parser.h" 144 145 /* 146 * Utility library. 147 */ 148 #include <mymalloc.h> 149 #include <vstring.h> 150 #include <msg.h> 151 #include <dict.h> 152 153 /* 154 * Application specific 155 */ 156 #include "db_common.h" 157 158 #define DB_COMMON_KEY_DOMAIN (1 << 0)/* Need lookup key domain */ 159 #define DB_COMMON_KEY_USER (1 << 1)/* Need lookup key localpart */ 160 #define DB_COMMON_VALUE_DOMAIN (1 << 2)/* Need result domain */ 161 #define DB_COMMON_VALUE_USER (1 << 3)/* Need result localpart */ 162 #define DB_COMMON_KEY_PARTIAL (1 << 4)/* Key uses input substrings */ 163 164 typedef struct { 165 DICT *dict; 166 STRING_LIST *domain; 167 int flags; 168 int nparts; 169 } DB_COMMON_CTX; 170 171 /* db_common_alloc - allocate db_common context */ 172 173 void *db_common_alloc(DICT *dict) 174 { 175 DB_COMMON_CTX *ctx; 176 177 ctx = (DB_COMMON_CTX *) mymalloc(sizeof *ctx); 178 ctx->dict = dict; 179 ctx->domain = 0; 180 ctx->flags = 0; 181 ctx->nparts = 0; 182 return ((void *) ctx); 183 } 184 185 /* db_common_parse - validate query or result template */ 186 187 int db_common_parse(DICT *dict, void **ctxPtr, const char *format, int query) 188 { 189 DB_COMMON_CTX *ctx = (DB_COMMON_CTX *) *ctxPtr; 190 const char *cp; 191 int dynamic = 0; 192 193 if (ctx == 0) 194 ctx = (DB_COMMON_CTX *) (*ctxPtr = db_common_alloc(dict)); 195 196 for (cp = format; *cp; ++cp) 197 if (*cp == '%') 198 switch (*++cp) { 199 case '%': 200 break; 201 case 'u': 202 ctx->flags |= 203 query ? DB_COMMON_KEY_USER | DB_COMMON_KEY_PARTIAL 204 : DB_COMMON_VALUE_USER; 205 dynamic = 1; 206 break; 207 case 'd': 208 ctx->flags |= 209 query ? DB_COMMON_KEY_DOMAIN | DB_COMMON_KEY_PARTIAL 210 : DB_COMMON_VALUE_DOMAIN; 211 dynamic = 1; 212 break; 213 case 's': 214 case 'S': 215 dynamic = 1; 216 break; 217 case 'U': 218 ctx->flags |= DB_COMMON_KEY_PARTIAL | DB_COMMON_KEY_USER; 219 dynamic = 1; 220 break; 221 case '1': 222 case '2': 223 case '3': 224 case '4': 225 case '5': 226 case '6': 227 case '7': 228 case '8': 229 case '9': 230 231 /* 232 * Find highest %[1-9] index in query template. Input keys 233 * will be constrained to those with at least this many 234 * domain components. This makes the db_common_expand() code 235 * safe from invalid inputs. 236 */ 237 if (ctx->nparts < *cp - '0') 238 ctx->nparts = *cp - '0'; 239 /* FALLTHROUGH */ 240 case 'D': 241 ctx->flags |= DB_COMMON_KEY_PARTIAL | DB_COMMON_KEY_DOMAIN; 242 dynamic = 1; 243 break; 244 default: 245 msg_fatal("db_common_parse: %s: Invalid %s template: %s", 246 ctx->dict->name, query ? "query" : "result", format); 247 } 248 return dynamic; 249 } 250 251 /* db_common_parse_domain - parse domain matchlist*/ 252 253 void db_common_parse_domain(CFG_PARSER *parser, void *ctxPtr) 254 { 255 DB_COMMON_CTX *ctx = (DB_COMMON_CTX *) ctxPtr; 256 char *domainlist; 257 const char *myname = "db_common_parse_domain"; 258 259 domainlist = cfg_get_str(parser, "domain", "", 0, 0); 260 if (*domainlist) { 261 ctx->domain = string_list_init(MATCH_FLAG_RETURN, domainlist); 262 if (ctx->domain == 0) 263 264 /* 265 * The "domain" optimization skips input keys that may in fact 266 * have unwanted matches in the database, so failure to create 267 * the match list is fatal. 268 */ 269 msg_fatal("%s: %s: domain match list creation using '%s' failed", 270 myname, parser->name, domainlist); 271 } 272 myfree(domainlist); 273 } 274 275 /* db_common_dict_partial - Does query use partial lookup keys? */ 276 277 int db_common_dict_partial(void *ctxPtr) 278 { 279 #if 0 /* Breaks recipient_delimiter */ 280 DB_COMMON_CTX *ctx = (DB_COMMON_CTX *) ctxPtr; 281 282 return (ctx->domain || ctx->flags & DB_COMMON_KEY_PARTIAL); 283 #endif 284 return (0); 285 } 286 287 /* db_common_free_ctx - free parse context */ 288 289 void db_common_free_ctx(void *ctxPtr) 290 { 291 DB_COMMON_CTX *ctx = (DB_COMMON_CTX *) ctxPtr; 292 293 if (ctx->domain) 294 string_list_free(ctx->domain); 295 myfree((char *) ctxPtr); 296 } 297 298 /* db_common_expand - expand query and result templates */ 299 300 int db_common_expand(void *ctxArg, const char *format, const char *value, 301 const char *key, VSTRING *result, 302 db_quote_callback_t quote_func) 303 { 304 const char *myname = "db_common_expand"; 305 DB_COMMON_CTX *ctx = (DB_COMMON_CTX *) ctxArg; 306 const char *vdomain = 0; 307 const char *kdomain = 0; 308 const char *domain = 0; 309 int dflag = key ? DB_COMMON_VALUE_DOMAIN : DB_COMMON_KEY_DOMAIN; 310 char *vuser = 0; 311 char *kuser = 0; 312 ARGV *parts = 0; 313 int i; 314 const char *cp; 315 316 /* Skip NULL values, silently. */ 317 if (value == 0) 318 return (0); 319 320 /* Don't silenty skip empty query string or empty lookup results. */ 321 if (*value == 0) { 322 if (key) 323 msg_warn("table \"%s:%s\": empty lookup result for: \"%s\"" 324 " -- ignored", ctx->dict->type, ctx->dict->name, key); 325 else 326 msg_warn("table \"%s:%s\": empty query string" 327 " -- ignored", ctx->dict->type, ctx->dict->name); 328 return (0); 329 } 330 if (key) { 331 /* This is a result template and the input value is the result */ 332 if (ctx->flags & (DB_COMMON_VALUE_DOMAIN | DB_COMMON_VALUE_USER)) 333 if ((vdomain = strrchr(value, '@')) != 0) 334 ++vdomain; 335 336 if (((!vdomain || !*vdomain) && (ctx->flags & DB_COMMON_VALUE_DOMAIN) != 0) 337 || (vdomain == value + 1 && (ctx->flags & DB_COMMON_VALUE_USER) != 0)) 338 return (0); 339 340 /* The result format may use the local or domain part of the key */ 341 if (ctx->flags & (DB_COMMON_KEY_DOMAIN | DB_COMMON_KEY_USER)) 342 if ((kdomain = strrchr(key, '@')) != 0) 343 ++kdomain; 344 345 /* 346 * The key should already be checked before the query. No harm if the 347 * query did not get optimized out, so we just issue a warning. 348 */ 349 if (((!kdomain || !*kdomain) && (ctx->flags & DB_COMMON_KEY_DOMAIN) != 0) 350 || (kdomain == key + 1 && (ctx->flags & DB_COMMON_KEY_USER) != 0)) { 351 msg_warn("%s: %s: lookup key '%s' skipped after query", myname, 352 ctx->dict->name, value); 353 return (0); 354 } 355 } else { 356 /* This is a query template and the input value is the lookup key */ 357 if (ctx->flags & (DB_COMMON_KEY_DOMAIN | DB_COMMON_KEY_USER)) 358 if ((vdomain = strrchr(value, '@')) != 0) 359 ++vdomain; 360 361 if (((!vdomain || !*vdomain) && (ctx->flags & DB_COMMON_KEY_DOMAIN) != 0) 362 || (vdomain == value + 1 && (ctx->flags & DB_COMMON_KEY_USER) != 0)) 363 return (0); 364 } 365 366 if (ctx->nparts > 0) { 367 parts = argv_split(key ? kdomain : vdomain, "."); 368 369 /* 370 * Filter out input keys whose domains lack enough labels to fill-in 371 * the query template. See below and also db_common_parse() which 372 * initializes ctx->nparts. 373 */ 374 if (parts->argc < ctx->nparts) { 375 argv_free(parts); 376 return (0); 377 } 378 379 /* 380 * Skip domains with leading, consecutive or trailing '.' separators 381 * among the required labels. 382 */ 383 for (i = 0; i < ctx->nparts; i++) 384 if (*parts->argv[parts->argc - i - 1] == 0) { 385 argv_free(parts); 386 return (0); 387 } 388 } 389 if (VSTRING_LEN(result) > 0) 390 VSTRING_ADDCH(result, ','); 391 392 #define QUOTE_VAL(d, q, v, buf) do { \ 393 if (q) \ 394 q(d, v, buf); \ 395 else \ 396 vstring_strcat(buf, v); \ 397 } while (0) 398 399 /* 400 * Replace all instances of %s with the address to look up. Replace %u 401 * with the user portion, and %d with the domain portion. "%%" expands to 402 * "%". lowercase -> addr, uppercase -> key 403 */ 404 for (cp = format; *cp; cp++) { 405 if (*cp == '%') { 406 switch (*++cp) { 407 408 case '%': 409 VSTRING_ADDCH(result, '%'); 410 break; 411 412 case 's': 413 QUOTE_VAL(ctx->dict, quote_func, value, result); 414 break; 415 416 case 'u': 417 if (vdomain) { 418 if (vuser == 0) 419 vuser = mystrndup(value, vdomain - value - 1); 420 QUOTE_VAL(ctx->dict, quote_func, vuser, result); 421 } else 422 QUOTE_VAL(ctx->dict, quote_func, value, result); 423 break; 424 425 case 'd': 426 if (!(ctx->flags & dflag)) 427 msg_panic("%s: %s: %s: bad query/result template context", 428 myname, ctx->dict->name, format); 429 if (!vdomain) 430 msg_panic("%s: %s: %s: expanding domain-less key or value", 431 myname, ctx->dict->name, format); 432 QUOTE_VAL(ctx->dict, quote_func, vdomain, result); 433 break; 434 435 case 'S': 436 if (key) 437 QUOTE_VAL(ctx->dict, quote_func, key, result); 438 else 439 QUOTE_VAL(ctx->dict, quote_func, value, result); 440 break; 441 442 case 'U': 443 if (key) { 444 if (kdomain) { 445 if (kuser == 0) 446 kuser = mystrndup(key, kdomain - key - 1); 447 QUOTE_VAL(ctx->dict, quote_func, kuser, result); 448 } else 449 QUOTE_VAL(ctx->dict, quote_func, key, result); 450 } else { 451 if (vdomain) { 452 if (vuser == 0) 453 vuser = mystrndup(value, vdomain - value - 1); 454 QUOTE_VAL(ctx->dict, quote_func, vuser, result); 455 } else 456 QUOTE_VAL(ctx->dict, quote_func, value, result); 457 } 458 break; 459 460 case 'D': 461 if (!(ctx->flags & DB_COMMON_KEY_DOMAIN)) 462 msg_panic("%s: %s: %s: bad query/result template context", 463 myname, ctx->dict->name, format); 464 if ((domain = key ? kdomain : vdomain) == 0) 465 msg_panic("%s: %s: %s: expanding domain-less key or value", 466 myname, ctx->dict->name, format); 467 QUOTE_VAL(ctx->dict, quote_func, domain, result); 468 break; 469 470 case '1': 471 case '2': 472 case '3': 473 case '4': 474 case '5': 475 case '6': 476 case '7': 477 case '8': 478 case '9': 479 480 /* 481 * Interpolate %[1-9] components into the query string. By 482 * this point db_common_parse() has identified the highest 483 * component index, and (see above) keys with fewer 484 * components have been filtered out. The "parts" ARGV is 485 * guaranteed to be initialized and hold enough elements to 486 * satisfy the query template. 487 */ 488 if (!(ctx->flags & DB_COMMON_KEY_DOMAIN) 489 || ctx->nparts < *cp - '0') 490 msg_panic("%s: %s: %s: bad query/result template context", 491 myname, ctx->dict->name, format); 492 if (!parts || parts->argc < ctx->nparts) 493 msg_panic("%s: %s: %s: key has too few domain labels", 494 myname, ctx->dict->name, format); 495 QUOTE_VAL(ctx->dict, quote_func, 496 parts->argv[parts->argc - (*cp - '0')], result); 497 break; 498 499 default: 500 msg_fatal("%s: %s: invalid %s template '%s'", myname, 501 ctx->dict->name, key ? "result" : "query", 502 format); 503 } 504 } else 505 VSTRING_ADDCH(result, *cp); 506 } 507 VSTRING_TERMINATE(result); 508 509 if (vuser) 510 myfree(vuser); 511 if (kuser) 512 myfree(kuser); 513 if (parts) 514 argv_free(parts); 515 516 return (1); 517 } 518 519 520 /* db_common_check_domain - check domain list */ 521 522 int db_common_check_domain(void *ctxPtr, const char *addr) 523 { 524 DB_COMMON_CTX *ctx = (DB_COMMON_CTX *) ctxPtr; 525 char *domain; 526 527 if (ctx->domain) { 528 if ((domain = strrchr(addr, '@')) != NULL) 529 ++domain; 530 if (domain == NULL || domain == addr + 1) 531 return (0); 532 if (match_list_match(ctx->domain, domain) == 0) 533 return (ctx->domain->error); 534 } 535 return (1); 536 } 537 538 /* db_common_sql_build_query -- build query for SQL maptypes */ 539 540 void db_common_sql_build_query(VSTRING *query, CFG_PARSER *parser) 541 { 542 const char *myname = "db_common_sql_build_query"; 543 char *table; 544 char *select_field; 545 char *where_field; 546 char *additional_conditions; 547 548 /* 549 * Build "old style" query: "select %s from %s where %s" 550 */ 551 if ((table = cfg_get_str(parser, "table", NULL, 1, 0)) == 0) 552 msg_fatal("%s: 'table' parameter not defined", myname); 553 554 if ((select_field = cfg_get_str(parser, "select_field", NULL, 1, 0)) == 0) 555 msg_fatal("%s: 'select_field' parameter not defined", myname); 556 557 if ((where_field = cfg_get_str(parser, "where_field", NULL, 1, 0)) == 0) 558 msg_fatal("%s: 'where_field' parameter not defined", myname); 559 560 additional_conditions = cfg_get_str(parser, "additional_conditions", 561 "", 0, 0); 562 563 vstring_sprintf(query, "SELECT %s FROM %s WHERE %s='%%s' %s", 564 select_field, table, where_field, 565 additional_conditions); 566 567 myfree(table); 568 myfree(select_field); 569 myfree(where_field); 570 myfree(additional_conditions); 571 } 572