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 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052
|
/* execute.c
Support for executable statements. */
/*
* Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
* Copyright (c) 1998-2003 by Internet Software Consortium
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
* OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
* Internet Systems Consortium, Inc.
* 950 Charter Street
* Redwood City, CA 94063
* <info@isc.org>
* http://www.isc.org/
*
* This software has been written for Internet Systems Consortium
* by Ted Lemon in cooperation with Vixie Enterprises and Nominum, Inc.
* To learn more about Internet Systems Consortium, see
* ``http://www.isc.org/''. To learn more about Vixie Enterprises,
* see ``http://www.vix.com''. To learn more about Nominum, Inc., see
* ``http://www.nominum.com''.
*/
#ifndef lint
static char copyright[] =
"$Id: execute.c,v 1.44.2.10 2004/06/10 17:59:17 dhankins Exp $ Copyright (c) 2004 Internet Systems Consortium. All rights reserved.\n";
#endif /* not lint */
#include "dhcpd.h"
#include <omapip/omapip_p.h>
int execute_statements (result, packet, lease, client_state,
in_options, out_options, scope, statements)
struct binding_value **result;
struct packet *packet;
struct lease *lease;
struct client_state *client_state;
struct option_state *in_options;
struct option_state *out_options;
struct binding_scope **scope;
struct executable_statement *statements;
{
struct executable_statement *r, *e, *next;
int rc;
int status;
unsigned long num;
struct binding_scope *outer;
struct binding *binding;
struct data_string ds;
struct binding_scope *ns;
if (!statements)
return 1;
r = (struct executable_statement *)0;
next = (struct executable_statement *)0;
e = (struct executable_statement *)0;
executable_statement_reference (&r, statements, MDL);
while (r && !(result && *result)) {
if (r -> next)
executable_statement_reference (&next, r -> next, MDL);
switch (r -> op) {
case statements_statement:
#if defined (DEBUG_EXPRESSIONS)
log_debug ("exec: statements");
#endif
status = execute_statements (result, packet, lease,
client_state, in_options,
out_options, scope,
r -> data.statements);
#if defined (DEBUG_EXPRESSIONS)
log_debug ("exec: statements returns %d", status);
#endif
if (!status)
return 0;
break;
case on_statement:
if (lease) {
if (r -> data.on.evtypes & ON_EXPIRY) {
#if defined (DEBUG_EXPRESSIONS)
log_debug ("exec: on expiry");
#endif
if (lease -> on_expiry)
executable_statement_dereference
(&lease -> on_expiry, MDL);
if (r -> data.on.statements)
executable_statement_reference
(&lease -> on_expiry,
r -> data.on.statements, MDL);
}
if (r -> data.on.evtypes & ON_RELEASE) {
#if defined (DEBUG_EXPRESSIONS)
log_debug ("exec: on release");
#endif
if (lease -> on_release)
executable_statement_dereference
(&lease -> on_release, MDL);
if (r -> data.on.statements)
executable_statement_reference
(&lease -> on_release,
r -> data.on.statements, MDL);
}
if (r -> data.on.evtypes & ON_COMMIT) {
#if defined (DEBUG_EXPRESSIONS)
log_debug ("exec: on commit");
#endif
if (lease -> on_commit)
executable_statement_dereference
(&lease -> on_commit, MDL);
if (r -> data.on.statements)
executable_statement_reference
(&lease -> on_commit,
r -> data.on.statements, MDL);
}
}
break;
case switch_statement:
#if defined (DEBUG_EXPRESSIONS)
log_debug ("exec: switch");
#endif
status = (find_matching_case
(&e, packet, lease, client_state,
in_options, out_options, scope,
r -> data.s_switch.expr,
r -> data.s_switch.statements));
#if defined (DEBUG_EXPRESSIONS)
log_debug ("exec: switch: case %lx", (unsigned long)e);
#endif
if (status) {
if (!(execute_statements
(result, packet, lease, client_state,
in_options, out_options, scope, e))) {
executable_statement_dereference
(&e, MDL);
return 0;
}
executable_statement_dereference (&e, MDL);
}
break;
/* These have no effect when executed. */
case case_statement:
case default_statement:
break;
case if_statement:
status = (evaluate_boolean_expression
(&rc, packet,
lease, client_state, in_options,
out_options, scope, r -> data.ie.expr));
#if defined (DEBUG_EXPRESSIONS)
log_debug ("exec: if %s", (status
? (rc ? "true" : "false")
: "NULL"));
#endif
/* XXX Treat NULL as false */
if (!status)
rc = 0;
if (!execute_statements
(result, packet, lease, client_state,
in_options, out_options, scope,
rc ? r -> data.ie.tc : r -> data.ie.fc))
return 0;
break;
case eval_statement:
status = evaluate_expression
((struct binding_value **)0,
packet, lease, client_state, in_options,
out_options, scope, r -> data.eval, MDL);
#if defined (DEBUG_EXPRESSIONS)
log_debug ("exec: evaluate: %s",
(status ? "succeeded" : "failed"));
#endif
break;
case return_statement:
status = evaluate_expression
(result, packet,
lease, client_state, in_options,
out_options, scope, r -> data.retval, MDL);
#if defined (DEBUG_EXPRESSIONS)
log_debug ("exec: return: %s",
(status ? "succeeded" : "failed"));
#endif
break;
case add_statement:
#if defined (DEBUG_EXPRESSIONS)
log_debug ("exec: add %s", (r -> data.add -> name
? r -> data.add -> name
: "<unnamed class>"));
#endif
classify (packet, r -> data.add);
break;
case break_statement:
#if defined (DEBUG_EXPRESSIONS)
log_debug ("exec: break");
#endif
return 1;
case supersede_option_statement:
case send_option_statement:
#if defined (DEBUG_EXPRESSIONS)
log_debug ("exec: %s option %s.%s",
(r -> op == supersede_option_statement
? "supersede" : "send"),
r -> data.option -> option -> universe -> name,
r -> data.option -> option -> name);
goto option_statement;
#endif
case default_option_statement:
#if defined (DEBUG_EXPRESSIONS)
log_debug ("exec: default option %s.%s",
r -> data.option -> option -> universe -> name,
r -> data.option -> option -> name);
goto option_statement;
#endif
case append_option_statement:
#if defined (DEBUG_EXPRESSIONS)
log_debug ("exec: append option %s.%s",
r -> data.option -> option -> universe -> name,
r -> data.option -> option -> name);
goto option_statement;
#endif
case prepend_option_statement:
#if defined (DEBUG_EXPRESSIONS)
log_debug ("exec: prepend option %s.%s",
r -> data.option -> option -> universe -> name,
r -> data.option -> option -> name);
option_statement:
#endif
set_option (r -> data.option -> option -> universe,
out_options, r -> data.option, r -> op);
break;
case set_statement:
case define_statement:
if (!scope) {
log_error ("set %s: no scope",
r -> data.set.name);
status = 0;
break;
}
if (!*scope) {
if (!binding_scope_allocate (scope, MDL)) {
log_error ("set %s: can't allocate scope",
r -> data.set.name);
status = 0;
break;
}
}
binding = find_binding (*scope, r -> data.set.name);
#if defined (DEBUG_EXPRESSIONS)
log_debug ("exec: set %s", r -> data.set.name);
#endif
if (!binding) {
binding = dmalloc (sizeof *binding, MDL);
if (binding) {
memset (binding, 0, sizeof *binding);
binding -> name =
dmalloc (strlen
(r -> data.set.name) + 1,
MDL);
if (binding -> name) {
strcpy (binding -> name,
r -> data.set.name);
binding -> next = (*scope) -> bindings;
(*scope) -> bindings = binding;
} else {
badalloc:
dfree (binding, MDL);
binding = (struct binding *)0;
}
}
}
if (binding) {
if (binding -> value)
binding_value_dereference
(&binding -> value, MDL);
if (r -> op == set_statement) {
status = (evaluate_expression
(&binding -> value, packet,
lease, client_state,
in_options, out_options,
scope, r -> data.set.expr,
MDL));
} else {
if (!(binding_value_allocate
(&binding -> value, MDL))) {
dfree (binding, MDL);
binding = (struct binding *)0;
}
if (binding -> value) {
binding -> value -> type =
binding_function;
(fundef_reference
(&binding -> value -> value.fundef,
r -> data.set.expr -> data.func,
MDL));
}
}
}
#if defined (DEBUG_EXPRESSIONS)
log_debug ("exec: set %s%s", r -> data.set.name,
(binding && status ? "" : " (failed)"));
#endif
break;
case unset_statement:
if (!scope || !*scope) {
status = 0;
break;
}
binding = find_binding (*scope, r -> data.unset);
if (binding) {
if (binding -> value)
binding_value_dereference
(&binding -> value, MDL);
status = 1;
} else
status = 0;
#if defined (DEBUG_EXPRESSIONS)
log_debug ("exec: unset %s: %s", r -> data.unset,
(status ? "found" : "not found"));
#endif
break;
case let_statement:
#if defined (DEBUG_EXPRESSIONS)
log_debug ("exec: let %s", r -> data.let.name);
#endif
ns = (struct binding_scope *)0;
binding_scope_allocate (&ns, MDL);
e = r;
next_let:
if (ns) {
binding = dmalloc (sizeof *binding, MDL);
memset (binding, 0, sizeof *binding);
if (!binding) {
blb:
binding_scope_dereference (&ns, MDL);
} else {
binding -> name =
dmalloc (strlen
(e -> data.let.name + 1),
MDL);
if (binding -> name)
strcpy (binding -> name,
e -> data.let.name);
else {
dfree (binding, MDL);
binding = (struct binding *)0;
goto blb;
}
}
}
if (ns && binding) {
status = (evaluate_expression
(&binding -> value, packet, lease,
client_state,
in_options, out_options,
scope, e -> data.set.expr, MDL));
binding -> next = ns -> bindings;
ns -> bindings = binding;
}
#if defined (DEBUG_EXPRESSIONS)
log_debug ("exec: let %s%s", e -> data.let.name,
(binding && status ? "" : "failed"));
#endif
if (!e -> data.let.statements) {
} else if (e -> data.let.statements -> op ==
let_statement) {
e = e -> data.let.statements;
goto next_let;
} else if (ns) {
if (scope && *scope)
binding_scope_reference (&ns -> outer,
*scope, MDL);
execute_statements
(result, packet, lease,
client_state,
in_options, out_options,
&ns, e -> data.let.statements);
}
if (ns)
binding_scope_dereference (&ns, MDL);
break;
case log_statement:
memset (&ds, 0, sizeof ds);
status = (evaluate_data_expression
(&ds, packet,
lease, client_state, in_options,
out_options, scope, r -> data.log.expr,
MDL));
#if defined (DEBUG_EXPRESSIONS)
log_debug ("exec: log");
#endif
if (status) {
switch (r -> data.log.priority) {
case log_priority_fatal:
log_fatal ("%.*s", (int)ds.len,
ds.buffer -> data);
break;
case log_priority_error:
log_error ("%.*s", (int)ds.len,
ds.buffer -> data);
break;
case log_priority_debug:
log_debug ("%.*s", (int)ds.len,
ds.buffer -> data);
break;
case log_priority_info:
log_info ("%.*s", (int)ds.len,
ds.buffer -> data);
break;
}
data_string_forget (&ds, MDL);
}
break;
default:
log_error ("bogus statement type %d", r -> op);
break;
}
executable_statement_dereference (&r, MDL);
if (next) {
executable_statement_reference (&r, next, MDL);
executable_statement_dereference (&next, MDL);
}
}
return 1;
}
/* Execute all the statements in a particular scope, and all statements in
scopes outer from that scope, but if a particular limiting scope is
reached, do not execute statements in that scope or in scopes outer
from it. More specific scopes need to take precedence over less
specific scopes, so we recursively traverse the scope list, executing
the most outer scope first. */
void execute_statements_in_scope (result, packet,
lease, client_state, in_options, out_options,
scope, group, limiting_group)
struct binding_value **result;
struct packet *packet;
struct lease *lease;
struct client_state *client_state;
struct option_state *in_options;
struct option_state *out_options;
struct binding_scope **scope;
struct group *group;
struct group *limiting_group;
{
struct group *limit;
/* If we've recursed as far as we can, return. */
if (!group)
return;
/* As soon as we get to a scope that is outer than the limiting
scope, we are done. This is so that if somebody does something
like this, it does the expected thing:
domain-name "fugue.com";
shared-network FOO {
host bar {
domain-name "othello.fugue.com";
fixed-address 10.20.30.40;
}
subnet 10.20.30.0 netmask 255.255.255.0 {
domain-name "manhattan.fugue.com";
}
}
The problem with the above arrangement is that the host's
group nesting will be host -> shared-network -> top-level,
and the limiting scope when we evaluate the host's scope
will be the subnet -> shared-network -> top-level, so we need
to know when we evaluate the host's scope to stop before we
evaluate the shared-networks scope, because it's outer than
the limiting scope, which means we've already evaluated it. */
for (limit = limiting_group; limit; limit = limit -> next) {
if (group == limit)
return;
}
if (group -> next)
execute_statements_in_scope (result, packet,
lease, client_state,
in_options, out_options, scope,
group -> next, limiting_group);
execute_statements (result, packet, lease, client_state, in_options,
out_options, scope, group -> statements);
}
/* Dereference or free any subexpressions of a statement being freed. */
int executable_statement_dereference (ptr, file, line)
struct executable_statement **ptr;
const char *file;
int line;
{
struct executable_statement *bp;
if (!ptr || !*ptr) {
log_error ("%s(%d): null pointer", file, line);
#if defined (POINTER_DEBUG)
abort ();
#else
return 0;
#endif
}
(*ptr) -> refcnt--;
rc_register (file, line, ptr, *ptr, (*ptr) -> refcnt, 1, RC_MISC);
if ((*ptr) -> refcnt > 0) {
*ptr = (struct executable_statement *)0;
return 1;
}
if ((*ptr) -> refcnt < 0) {
log_error ("%s(%d): negative refcnt!", file, line);
#if defined (DEBUG_RC_HISTORY)
dump_rc_history (*ptr);
#endif
#if defined (POINTER_DEBUG)
abort ();
#else
return 0;
#endif
}
if ((*ptr) -> next)
executable_statement_dereference (&(*ptr) -> next, file, line);
switch ((*ptr) -> op) {
case statements_statement:
if ((*ptr) -> data.statements)
executable_statement_dereference
(&(*ptr) -> data.statements, file, line);
break;
case on_statement:
if ((*ptr) -> data.on.statements)
executable_statement_dereference
(&(*ptr) -> data.on.statements, file, line);
break;
case switch_statement:
if ((*ptr) -> data.s_switch.statements)
executable_statement_dereference
(&(*ptr) -> data.on.statements, file, line);
if ((*ptr) -> data.s_switch.expr)
expression_dereference (&(*ptr) -> data.s_switch.expr,
file, line);
break;
case case_statement:
if ((*ptr) -> data.s_switch.expr)
expression_dereference (&(*ptr) -> data.c_case,
file, line);
break;
case if_statement:
if ((*ptr) -> data.ie.expr)
expression_dereference (&(*ptr) -> data.ie.expr,
file, line);
if ((*ptr) -> data.ie.tc)
executable_statement_dereference
(&(*ptr) -> data.ie.tc, file, line);
if ((*ptr) -> data.ie.fc)
executable_statement_dereference
(&(*ptr) -> data.ie.fc, file, line);
break;
case eval_statement:
if ((*ptr) -> data.eval)
expression_dereference (&(*ptr) -> data.eval,
file, line);
break;
case return_statement:
if ((*ptr) -> data.eval)
expression_dereference (&(*ptr) -> data.eval,
file, line);
break;
case set_statement:
if ((*ptr)->data.set.name)
dfree ((*ptr)->data.set.name, file, line);
if ((*ptr)->data.set.expr)
expression_dereference (&(*ptr) -> data.set.expr,
file, line);
break;
case unset_statement:
if ((*ptr)->data.unset)
dfree ((*ptr)->data.unset, file, line);
break;
case supersede_option_statement:
case send_option_statement:
case default_option_statement:
case append_option_statement:
case prepend_option_statement:
if ((*ptr) -> data.option)
option_cache_dereference (&(*ptr) -> data.option,
file, line);
break;
default:
/* Nothing to do. */
break;
}
dfree ((*ptr), file, line);
*ptr = (struct executable_statement *)0;
return 1;
}
void write_statements (file, statements, indent)
FILE *file;
struct executable_statement *statements;
int indent;
{
struct executable_statement *r, *x;
int result;
int status;
const char *s, *t, *dot;
int col;
if (!statements)
return;
for (r = statements; r; r = r -> next) {
switch (r -> op) {
case statements_statement:
write_statements (file, r -> data.statements, indent);
break;
case on_statement:
indent_spaces (file, indent);
fprintf (file, "on ");
s = "";
if (r -> data.on.evtypes & ON_EXPIRY) {
fprintf (file, "%sexpiry", s);
s = " or ";
}
if (r -> data.on.evtypes & ON_COMMIT) {
fprintf (file, "%scommit", s);
s = "or";
}
if (r -> data.on.evtypes & ON_RELEASE) {
fprintf (file, "%srelease", s);
s = "or";
}
if (r -> data.on.statements) {
fprintf (file, " {");
write_statements (file,
r -> data.on.statements,
indent + 2);
indent_spaces (file, indent);
fprintf (file, "}");
} else {
fprintf (file, ";");
}
break;
case switch_statement:
indent_spaces (file, indent);
fprintf (file, "switch (");
col = write_expression (file,
r -> data.s_switch.expr,
indent + 7, indent + 7, 1);
col = token_print_indent (file, col, indent + 7,
"", "", ")");
token_print_indent (file,
col, indent, " ", "", "{");
write_statements (file, r -> data.s_switch.statements,
indent + 2);
indent_spaces (file, indent);
fprintf (file, "}");
break;
case case_statement:
indent_spaces (file, indent - 1);
fprintf (file, "case ");
col = write_expression (file,
r -> data.s_switch.expr,
indent + 5, indent + 5, 1);
token_print_indent (file, col, indent + 5,
"", "", ":");
break;
case default_statement:
indent_spaces (file, indent - 1);
fprintf (file, "default: ");
break;
case if_statement:
indent_spaces (file, indent);
fprintf (file, "if ");
x = r;
col = write_expression (file,
x -> data.ie.expr,
indent + 3, indent + 3, 1);
else_if:
token_print_indent (file, col, indent, " ", "", "{");
write_statements (file, x -> data.ie.tc, indent + 2);
if (x -> data.ie.fc &&
x -> data.ie.fc -> op == if_statement &&
!x -> data.ie.fc -> next) {
indent_spaces (file, indent);
fprintf (file, "} elsif ");
x = x -> data.ie.fc;
col = write_expression (file,
x -> data.ie.expr,
indent + 6,
indent + 6, 1);
goto else_if;
}
if (x -> data.ie.fc) {
indent_spaces (file, indent);
fprintf (file, "} else {");
write_statements (file, x -> data.ie.fc,
indent + 2);
}
indent_spaces (file, indent);
fprintf (file, "}");
break;
case eval_statement:
indent_spaces (file, indent);
fprintf (file, "eval ");
col = write_expression (file, r -> data.eval,
indent + 5, indent + 5, 1);
fprintf (file, ";");
break;
case return_statement:
indent_spaces (file, indent);
fprintf (file, "return;");
break;
case add_statement:
indent_spaces (file, indent);
fprintf (file, "add \"%s\"", r -> data.add -> name);
break;
case break_statement:
indent_spaces (file, indent);
fprintf (file, "break;");
break;
case supersede_option_statement:
case send_option_statement:
s = "supersede";
goto option_statement;
case default_option_statement:
s = "default";
goto option_statement;
case append_option_statement:
s = "append";
goto option_statement;
case prepend_option_statement:
s = "prepend";
option_statement:
/* Note: the reason we don't try to pretty print
the option here is that the format of the option
may change in dhcpd.conf, and then when this
statement was read back, it would cause a syntax
error. */
if (r -> data.option -> option -> universe ==
&dhcp_universe) {
t = "";
dot = "";
} else {
t = (r -> data.option -> option ->
universe -> name);
dot = ".";
}
indent_spaces (file, indent);
fprintf (file, "%s %s%s%s = ", s, t, dot,
r -> data.option -> option -> name);
col = (indent + strlen (s) + strlen (t) +
strlen (dot) + strlen (r -> data.option ->
option -> name) + 4);
if (r -> data.option -> expression)
write_expression
(file,
r -> data.option -> expression,
col, indent + 8, 1);
else
token_indent_data_string
(file, col, indent + 8, "", "",
&r -> data.option -> data);
fprintf (file, ";"); /* XXX */
break;
case set_statement:
indent_spaces (file, indent);
fprintf (file, "set ");
col = token_print_indent (file, indent + 4, indent + 4,
"", "", r -> data.set.name);
col = token_print_indent (file, col, indent + 4,
" ", " ", "=");
col = write_expression (file, r -> data.set.expr,
indent + 3, indent + 3, 0);
col = token_print_indent (file, col, indent + 4,
" ", "", ";");
break;
case unset_statement:
indent_spaces (file, indent);
fprintf (file, "unset ");
col = token_print_indent (file, indent + 6, indent + 6,
"", "", r -> data.set.name);
col = token_print_indent (file, col, indent + 6,
" ", "", ";");
break;
case log_statement:
indent_spaces (file, indent);
fprintf (file, "log ");
col = token_print_indent (file, col, indent + 4,
"", "", "(");
switch (r -> data.log.priority) {
case log_priority_fatal:
col = token_print_indent
(file, col, indent + 4, "",
" ", "fatal,");
break;
case log_priority_error:
col = token_print_indent
(file, col, indent + 4, "",
" ", "error,");
break;
case log_priority_debug:
col = token_print_indent
(file, col, indent + 4, "",
" ", "debug,");
break;
case log_priority_info:
col = token_print_indent
(file, col, indent + 4, "",
" ", "info,");
break;
}
col = write_expression (file, r -> data.log.expr,
indent + 4, indent + 4, 0);
col = token_print_indent (file, col, indent + 4,
"", "", ");");
break;
default:
log_fatal ("bogus statement type %d\n", r -> op);
}
}
}
/* Find a case statement in the sequence of executable statements that
matches the expression, and if found, return the following statement.
If no case statement matches, try to find a default statement and
return that (the default statement can precede all the case statements).
Otherwise, return the null statement. */
int find_matching_case (struct executable_statement **ep,
struct packet *packet, struct lease *lease,
struct client_state *client_state,
struct option_state *in_options,
struct option_state *out_options,
struct binding_scope **scope,
struct expression *expr,
struct executable_statement *stmt)
{
int status, sub;
struct executable_statement *s;
unsigned long foo;
if (is_data_expression (expr)) {
struct executable_statement *e;
struct data_string cd, ds;
memset (&ds, 0, sizeof ds);
memset (&cd, 0, sizeof cd);
status = (evaluate_data_expression (&ds, packet, lease,
client_state, in_options,
out_options, scope, expr,
MDL));
if (status) {
for (s = stmt; s; s = s -> next) {
if (s -> op == case_statement) {
sub = (evaluate_data_expression
(&cd, packet, lease, client_state,
in_options, out_options,
scope, s -> data.c_case, MDL));
if (sub && cd.len == ds.len &&
!memcmp (cd.data, ds.data, cd.len))
{
data_string_forget (&cd, MDL);
data_string_forget (&ds, MDL);
executable_statement_reference
(ep, s -> next, MDL);
return 1;
}
data_string_forget (&cd, MDL);
}
}
data_string_forget (&ds, MDL);
}
} else {
unsigned long n, c;
status = evaluate_numeric_expression (&n, packet, lease,
client_state,
in_options, out_options,
scope, expr);
if (status) {
for (s = stmt; s; s = s -> next) {
if (s -> op == case_statement) {
sub = (evaluate_numeric_expression
(&c, packet, lease, client_state,
in_options, out_options,
scope, s -> data.c_case));
if (sub && n == c) {
executable_statement_reference
(ep, s -> next, MDL);
return 1;
}
}
}
}
}
/* If we didn't find a matching case statement, look for a default
statement and return the statement following it. */
for (s = stmt; s; s = s -> next)
if (s -> op == default_statement)
break;
if (s) {
executable_statement_reference (ep, s -> next, MDL);
return 1;
}
return 0;
}
int executable_statement_foreach (struct executable_statement *stmt,
int (*callback) (struct
executable_statement *,
void *, int),
void *vp, int condp)
{
struct executable_statement *foo;
int ok = 0;
int result;
for (foo = stmt; foo; foo = foo -> next) {
if ((*callback) (foo, vp, condp) != 0)
ok = 1;
switch (foo -> op) {
case null_statement:
break;
case if_statement:
if (executable_statement_foreach (foo -> data.ie.tc,
callback, vp, 1))
ok = 1;
if (executable_statement_foreach (foo -> data.ie.fc,
callback, vp, 1))
ok = 1;
break;
case add_statement:
break;
case eval_statement:
break;
case break_statement:
break;
case default_option_statement:
break;
case supersede_option_statement:
break;
case append_option_statement:
break;
case prepend_option_statement:
break;
case send_option_statement:
break;
case statements_statement:
if ((executable_statement_foreach
(foo -> data.statements, callback, vp, condp)))
ok = 1;
break;
case on_statement:
if ((executable_statement_foreach
(foo -> data.on.statements, callback, vp, 1)))
ok = 1;
break;
case switch_statement:
if ((executable_statement_foreach
(foo -> data.s_switch.statements, callback, vp, 1)))
ok = 1;
break;
case case_statement:
break;
case default_statement:
break;
case set_statement:
break;
case unset_statement:
break;
case let_statement:
if ((executable_statement_foreach
(foo -> data.let.statements, callback, vp, 0)))
ok = 1;
break;
case define_statement:
break;
case log_statement:
case return_statement:
break;
}
}
return ok;
}
|