Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
log.debug(" gateway = %s" % opts.gateway)
# list of dns IPs
opts.dns = []
if opts.dnsstr:
log.debug(" supplied dns servers: %s" % opts.dnsstr)
dnsservers = opts.dnsstr.split(",")
allvalid = True
for server in dnsservers:
if not IPRE.match(server):
allvalid = False
log.error("DNS server is not a valid address: %s" % server)
else:
opts.dns.append(server)
if not allvalid:
raise InvalidInput("all DNS servers must be valid IPs")
log.debug(" validated dns servers = %s" % opts.dns)
# [default_lease_time, max_lease_time]
opts.leasetime = []
if opts.leasetimestr:
log.debug(" supplied leasetimes: %s" % opts.leasetimestr)
times = opts.leasetimestr.split(",")
if len(times) != 2:
raise InvalidInput("only supply two lease times: default and max")
try:
x = int(times[0])
if x < 1:
raise InvalidInput("default lease time is less than one")
y = int(times[1])
if y < 1:
raise InvalidInput("max lease time is less than one")
opts.dns.append(server)
if not allvalid:
raise InvalidInput("all DNS servers must be valid IPs")
log.debug(" validated dns servers = %s" % opts.dns)
# [default_lease_time, max_lease_time]
opts.leasetime = []
if opts.leasetimestr:
log.debug(" supplied leasetimes: %s" % opts.leasetimestr)
times = opts.leasetimestr.split(",")
if len(times) != 2:
raise InvalidInput("only supply two lease times: default and max")
try:
x = int(times[0])
if x < 1:
raise InvalidInput("default lease time is less than one")
y = int(times[1])
if y < 1:
raise InvalidInput("max lease time is less than one")
if x > y:
raise InvalidInput("max lease time is greater than default...")
opts.leasetime.append(x)
opts.leasetime.append(y)
except ValueError:
raise InvalidInput("a lease time is not an integer")
log.debug(" default lease time = %d" % opts.leasetime[0])
log.debug(" max lease time = %d" % opts.leasetime[1])
def validate(opts):
actions = [opts.add, opts.remove]
count = 0
for action in actions:
if action:
count += 1
if not count:
raise InvalidInput("You must supply an action, see help (-h).")
if count != 1:
raise InvalidInput("You may only supply one action, see help (-h).")
if opts.add:
log.debug("--- action = add")
elif opts.remove:
log.debug("--- action = remove")
if not opts.ipaddress:
raise InvalidInput("You must supply an IP address, see help (-h).")
if not IPRE.match(opts.ipaddress):
raise InvalidInput("IP is not a valid address: %s" % opts.ipaddress)
log.debug(" ipaddress = %s" % opts.ipaddress)
if opts.add:
log.debug("--- action = add")
elif opts.remove:
log.debug("--- action = remove")
if not opts.ipaddress:
raise InvalidInput("You must supply an IP address, see help (-h).")
if not IPRE.match(opts.ipaddress):
raise InvalidInput("IP is not a valid address: %s" % opts.ipaddress)
log.debug(" ipaddress = %s" % opts.ipaddress)
if not opts.policyfile:
raise InvalidInput("You must supply a DHCP configuration file to alter, see help (-h).")
if not os.path.isabs(opts.policyfile):
raise InvalidInput("You must specify the DHCP configuration file with an absolute path.")
else:
opts.policyfile = os.path.realpath(opts.policyfile)
log.debug(" policyfile = %s" % opts.policyfile)
if opts.add:
if not opts.macaddress:
raise InvalidInput("Add requires a MAC address, see help (-h).")
else:
if not MACRE.match(opts.macaddress):
raise InvalidInput("MAC is not a valid address: %s" % opts.macaddress)
log.debug(" macaddress = %s" % opts.macaddress)
if not opts.hostname or opts.hostname.lower().strip() == 'none':
raise InvalidInput("Add requires a hostname, see help (-h).")
def validate(opts):
actions = [opts.add, opts.remove]
count = 0
for action in actions:
if action:
count += 1
if not count:
raise InvalidInput("You must supply an action, see help (-h).")
if count != 1:
raise InvalidInput("You may only supply one action, see help (-h).")
if opts.add:
log.debug("--- action = add")
elif opts.remove:
log.debug("--- action = remove")
if not opts.ipaddress:
raise InvalidInput("You must supply an IP address, see help (-h).")
if not IPRE.match(opts.ipaddress):
raise InvalidInput("IP is not a valid address: %s" % opts.ipaddress)
log.debug(" ipaddress = %s" % opts.ipaddress)
if not opts.policyfile:
raise InvalidInput("You must supply a DHCP configuration file to alter, see help (-h).")
if not os.path.isabs(opts.policyfile):
raise InvalidInput("You must specify the DHCP configuration file with an absolute path.")
else:
opts.policyfile = os.path.realpath(opts.policyfile)
log.debug(" policyfile = %s" % opts.policyfile)
if opts.add:
if not opts.macaddress:
raise InvalidInput("Add requires a MAC address, see help (-h).")
else:
if not MACRE.match(opts.macaddress):
raise InvalidInput("MAC is not a valid address: %s" % opts.macaddress)
log.debug(" macaddress = %s" % opts.macaddress)
if not opts.hostname or opts.hostname.lower().strip() == 'none':
raise InvalidInput("Add requires a hostname, see help (-h).")
else:
log.debug(" hostname = %s" % opts.hostname)
if opts.subnetmask:
if not IPRE.match(opts.subnetmask):
raise InvalidInput("subnet is not a valid address (note that /# addresses are not supported yet): %s" % opts.subnetmask)
log.debug(" subnetmask = %s" % opts.subnetmask)
if opts.broadcast:
if not IPRE.match(opts.broadcast):
raise InvalidInput("broadcast is not a valid address: %s" % opts.broadcast)
log.debug(" broadcast = %s" % opts.broadcast)
if opts.gateway:
if not IPRE.match(opts.gateway):
raise InvalidInput("gateway is not a valid address: %s" % opts.gateway)