Revision 651e531d
b/nfdhcpd | ||
---|---|---|
56 | 56 |
|
57 | 57 |
LOG_FORMAT = "%(asctime)-15s %(levelname)-6s %(message)s" |
58 | 58 |
|
59 |
# Configuration file specification (see configobj documentation) |
|
60 |
CONFIG_SPEC = """ |
|
61 |
[general] |
|
62 |
pidfile = string() |
|
63 |
datapath = string() |
|
64 |
logdir = string() |
|
65 |
user = string() |
|
66 |
|
|
67 |
[dhcp] |
|
68 |
enable_dhcp = boolean(default=True) |
|
69 |
lease_lifetime = integer(min=0, max=4294967295) |
|
70 |
lease_renewal = integer(min=0, max=4294967295) |
|
71 |
server_ip = ip_addr() |
|
72 |
dhcp_queue = integer(min=0, max=65535) |
|
73 |
nameservers = ip_addr_list(family=4) |
|
74 |
|
|
75 |
[ipv6] |
|
76 |
enable_ipv6 = boolean(default=True) |
|
77 |
ra_period = integer(min=1, max=4294967295) |
|
78 |
rs_queue = integer(min=0, max=65535) |
|
79 |
ns_queue = integer(min=0, max=65535) |
|
80 |
nameservers = ip_addr_list(family=6) |
|
81 |
""" |
|
82 |
|
|
83 |
|
|
59 | 84 |
DHCPDISCOVER = 1 |
60 | 85 |
DHCPOFFER = 2 |
61 | 86 |
DHCPREQUEST = 3 |
... | ... | |
581 | 606 |
|
582 | 607 |
if __name__ == "__main__": |
583 | 608 |
import optparse |
609 |
from cStringIO import StringIO |
|
584 | 610 |
from capng import * |
585 | 611 |
from pwd import getpwnam, getpwuid |
586 |
from configobj import ConfigObj |
|
612 |
from configobj import ConfigObj, flatten_errors |
|
613 |
|
|
614 |
import validate |
|
615 |
|
|
616 |
validator = validate.Validator() |
|
617 |
|
|
618 |
def is_ip_list(value, family=4): |
|
619 |
try: |
|
620 |
family = int(family) |
|
621 |
except ValueError: |
|
622 |
raise vaildate.VdtParamError(family) |
|
623 |
if isinstance(value, (str, unicode)): |
|
624 |
value = [value] |
|
625 |
if not isinstance(value, list): |
|
626 |
raise validate.VdtTypeError(value) |
|
627 |
|
|
628 |
for entry in value: |
|
629 |
try: |
|
630 |
ip = IPy.IP(entry) |
|
631 |
except ValueError: |
|
632 |
raise validate.VdtValueError(entry) |
|
633 |
|
|
634 |
if ip.version() != family: |
|
635 |
raise validate.VdtValueError(entry) |
|
636 |
return value |
|
637 |
|
|
638 |
validator.functions["ip_addr_list"] = is_ip_list |
|
639 |
config_spec = StringIO(CONFIG_SPEC) |
|
640 |
|
|
587 | 641 |
|
588 | 642 |
parser = optparse.OptionParser() |
589 | 643 |
parser.add_option("-c", "--config", dest="config_file", |
... | ... | |
602 | 656 |
d.open() |
603 | 657 |
|
604 | 658 |
try: |
605 |
config = ConfigObj(opts.config_file) |
|
659 |
config = ConfigObj(opts.config_file, configspec=config_spec)
|
|
606 | 660 |
except: |
607 | 661 |
sys.stderr.write("Failed to parse config file %s" % opts.config_file) |
608 | 662 |
sys.exit(1) |
609 | 663 |
|
664 |
results = config.validate(validator) |
|
665 |
if results != True: |
|
666 |
logging.fatal("Configuration file validation failed! See errors below:") |
|
667 |
for (section_list, key, _) in flatten_errors(config, results): |
|
668 |
if key is not None: |
|
669 |
logging.fatal(" '%s' in section '%s' failed validation" % (key, ", ".join(section_list))) |
|
670 |
else: |
|
671 |
logging.fatal(" Section '%s' is missing" % ", ".join(section_list)) |
|
672 |
sys.exit(1) |
|
610 | 673 |
|
611 | 674 |
pidfile = open(config["general"]["pidfile"], "w") |
612 | 675 |
pidfile.write("%s" % os.getpid()) |
Also available in: Unified diff