def cli_records_list(domain, type, id): provider = Provider() zones = provider.dns_zones() records = provider.dns_records() if type is None and id is None: print("At least one of --type and --id has to be used") return False zone = zones.find(domain) if not zone: print("Could not find domain") return False results = records.get_zone_records(zone.id, order_column='type') record_deleted = False for record in results: if type is not None: if record.type == type.upper(): record_deleted = True records.delete(record) elif id is not None: if record.id == id: record_deleted = True records.delete(record) message = "Record deleted" if record_deleted else "Record not found" print(message) return True
def record_edit(dns_zone_id, dns_record_id): provider = Provider() zones = provider.dns_zones() records = provider.dns_records() if not zones.can_access(dns_zone_id, current_user.id): flash('Access Denied', 'error') return redirect(url_for('home.index')) elif dns_record_id > 0: if not records.can_access(dns_zone_id, dns_record_id): flash('Access Denied', 'error') return redirect(url_for('home.index')) zone = zones.get(dns_zone_id) if not zone: flash('Zone not found', 'error') return redirect(url_for('home.index')) record = records.get(dns_record_id, zone.id) if dns_record_id > 0: if not record: flash('Could not load record', 'error') return redirect(url_for('home.index')) dns_types = records.get_types() dns_classes = records.get_classes() return render_template('dns/zones/view.html', dns_record_id=dns_record_id, dns_types=dns_types, dns_classes=dns_classes, zone=zone, record=record, section='records_edit', tab='records')
def gather_record_data(record_type): provider = Provider() records = provider.dns_records() data = {} properties = records.get_record_type_properties(record_type) for property, type in properties.items(): value = request.form[property].strip() if type == 'int': if not value.isdigit(): flash('Invalid {0} value'.format(property), 'error') return False value = int(value) if (type == 'str') and (len(value) == 0): flash('Invalid {0} value'.format(property), 'error') return False elif (type == 'int') and (value < 0): flash('Invalid {0} value'.format(property), 'error') return False if property in ['name2', 'preference2', 'algorithm2']: # There are multiple form fields like 'name', 'name2', 'name3', # it's easier to clean them up this way than use duplicate names. property = property.rstrip('2') data[property] = value return data
def one(self, user_id, record_id, zone_id=None, domain=None): provider = Provider() zones = provider.dns_zones() records = provider.dns_records() zone = zones.get(zone_id, user_id) if zone_id is not None else zones.find(domain, user_id=user_id) if not zone: return self.send_not_found_response() record = records.get(record_id, dns_zone_id=zone_id) if not record: return self.send_not_found_response() return self.send_valid_response(self.__load_record(record))
def all(self, user_id, zone_id=None, domain=None): provider = Provider() zones = provider.dns_zones() records = provider.dns_records() zone = zones.get(zone_id, user_id) if zone_id is not None else zones.find(domain, user_id=user_id) if not zone: return self.send_not_found_response() results = records.get_zone_records(zone.id) data = [] for result in results: data.append(self.__load_record(result)) return self.send_valid_response(data)
def cli_records_add(domain, type, cls, ttl, active, property): # Convert tuple to dict. properties = {} for p in property: properties[p[0]] = p[1] provider = Provider() zones = provider.dns_zones() records = provider.dns_records() if ttl < 0: print("Invalid TTL value") return False zone = zones.find(domain) if not zone: print("Could not find domain") return False expected_properties = records.get_record_type_properties(type, clean=True) type_data = {} for property_name, property_type in expected_properties.items(): if property_name not in properties: print('Missing type property {0}'.format(property_name)) return False value = properties[property_name] if (type == 'int') and (isinstance(value, str)): if not value.isdigit(): print('Invalid {0} value'.format(property_name)) return False value = int(value) if (type == 'str') and (len(value) == 0): print('Invalid {0} value'.format(property_name)) return False elif (type == 'int') and (value < 0): print('Invalid {0} value'.format(property_name)) return False type_data[property_name] = value record = records.create() record = records.save(record, zone.id, ttl, cls, type, type_data, active) print("Record created") return True
def zone_view(dns_zone_id): provider = Provider() zones = provider.dns_zones() records = provider.dns_records() if not zones.can_access(dns_zone_id, current_user.id): flash('Access Denied', 'error') return redirect(url_for('home.index')) zone = zones.get(dns_zone_id) if not zone: flash('Zone not found', 'error') return redirect(url_for('home.index')) return render_template('dns/zones/view.html', zone=zone, records=records.get_zone_records( dns_zone_id, order_column='type'), section='records', tab='records')
def record_delete(dns_zone_id, dns_record_id): provider = Provider() zones = provider.dns_zones() records = provider.dns_records() if not zones.can_access(dns_zone_id, current_user.id): flash('Access Denied', 'error') return redirect(url_for('home.index')) zone = zones.get(dns_zone_id) record = records.get(dns_record_id, dns_zone_id=zone.id) if not zone: flash('Zone not found', 'error') return redirect(url_for('home.index')) elif not record: flash('Record not found', 'error') return redirect(url_for('home.index')) records.delete(record) flash('Record deleted', 'success') return redirect(url_for('dns.zone_view', dns_zone_id=dns_zone_id))
def cli_records_list(domain): provider = Provider() zones = provider.dns_zones() records = provider.dns_records() zone = zones.find(domain) if not zone: print("Could not find domain") return False results = records.get_zone_records(zone.id, order_column='type') headers = ['id', 'ttl', 'class', 'type', 'active', 'data'] table = [] for record in results: table.append([ record.id, record.ttl, record.cls, record.type, record.active, str(record.properties()) ]) print(tabulate.tabulate(table, headers)) return True
def cli_records_update(domain, id, type, cls, ttl, active, property): # Convert tuple to dict. properties = {} for p in property: properties[p[0]] = p[1] provider = Provider() zones = provider.dns_zones() records = provider.dns_records() record = records.get(id) if not record: print("Record not found") return False type = record.type if type is None else type cls = record.cls if cls is None else cls ttl = record.ttl if ttl is None else ttl active = record.active if active is None else active in ['true', 'yes'] data = record.data if len(properties) == 0 else properties if ttl < 0: print("Invalid TTL value") return False zone = zones.find(domain) if not zone: print("Could not find domain") return False if data != record.data: type_properties = record.properties() # This means there's come incoming data. expected_properties = records.get_record_type_properties(type, clean=True) for property_name, property_value in data.items(): if property_name not in expected_properties: print("Unsupported property '{0}' for type '{1}'".format( property_name, type)) return False property_type = expected_properties[property_name] if (property_type == 'int') and (isinstance(property_value, str)): if not property_value.isdigit(): print('Invalid {0} value'.format(property_name)) return False property_value = int(property_value) if (property_type == 'str') and (len(property_value) == 0): print('Invalid {0} value'.format(property_name)) return False elif (property_type == 'int') and (property_value < 0): print('Invalid {0} value'.format(property_name)) return False type_properties[property_name] = property_value data = type_properties record = records.save(record, zone.id, ttl, cls, type, data, active) print("Record updated") return True
def create(self, user_id, zone_id=None, domain=None): provider = Provider() zones = provider.dns_zones() records = provider.dns_records() zone = zones.get(zone_id, user_id) if zone_id is not None else zones.find( domain, user_id=user_id) if not zone: return self.send_not_found_response() # First get the mandatory fields for all record types. required_fields = [ 'class', 'type', 'ttl', 'active', 'data', 'is_conditional', 'conditional_count', 'conditional_limit', 'conditional_reset', 'conditional_data' ] data = self.get_json(required_fields) if data is False: return self.send_error_response( 5000, 'Missing fields', 'Required fields are: {0}'.format(', '.join(required_fields))) # Validate. if data['class'] not in records.get_classes(): return self.send_error_response(5005, 'Invalid class', '') elif data['type'] not in records.get_types(): return self.send_error_response(5005, 'Invalid type', '') if isinstance(data['ttl'], str) and data['ttl'].isdigit() is False: return self.send_error_response(5005, 'Invalid TTL', '') data['ttl'] = int(data['ttl']) if data['ttl'] < 0: return self.send_error_response(5005, 'Invalid TTL', '') elif data['conditional_count'] < 0: return self.send_error_response(5005, 'Invalid Conditional Count', '') elif data['conditional_limit'] < 0: return self.send_error_response(5005, 'Invalid Conditional Limit', '') # Fix types. data['active'] = True if data['active'] else False data['is_conditional'] = True if data['is_conditional'] else False # Now that we have the type, we can get the type-specific properties. record_type_properties = records.get_record_type_properties( data['type'], clean=True) record_type_conditional_properties = records.get_record_type_properties( data['type'], clean=True) all_errors = [] basic_data, errors = self.__parse_data_properties( data['data'], record_type_properties) all_errors += errors conditional_data, errors = self.__parse_data_properties( data['conditional_data'], record_type_conditional_properties) all_errors += errors if len(errors) > 0: return self.send_error_response(5005, 'Invalid type property fields', errors) # Create the record. record = records.create() record = records.save(record, zone.id, data['ttl'], data['class'], data['type'], basic_data, data['active']) record = records.save_conditions(record, enabled=data['is_conditional'], data=conditional_data, count=data['conditional_count'], limit=data['conditional_limit'], reset=data['conditional_reset']) return self.one(user_id, record.id, zone_id=zone.id)
def update(self, user_id, record_id, zone_id=None, domain=None): provider = Provider() zones = provider.dns_zones() records = provider.dns_records() zone = zones.get(zone_id, user_id) if zone_id is not None else zones.find( domain, user_id=user_id) if not zone: return self.send_not_found_response() # Get record. record = records.get(record_id, dns_zone_id=zone.id) if not record: return self.send_not_found_response() data = self.get_json([]) if 'class' in data: if data['class'] not in records.get_classes(): return self.send_error_response(5005, 'Invalid class', '') else: data['class'] = record.cls if 'type' in data: if data['type'] not in records.get_types(): return self.send_error_response(5005, 'Invalid type', '') else: data['type'] = record.type if 'ttl' in data: if isinstance(data['ttl'], str): if not data['ttl'].isdigit(): return self.send_error_response(5005, 'Invalid TTL', '') data['ttl'] = int(data['ttl']) if data['ttl'] < 0: return self.send_error_response(5005, 'Invalid TTL', '') else: data['ttl'] = record.ttl if 'active' in data: data['active'] = True if data['active'] else False else: data['active'] = record.active if 'is_conditional' in data: data['is_conditional'] = True if data['is_conditional'] else False else: data['is_conditional'] = record.has_conditional_responses data['conditional_limit'] = data[ 'conditional_limit'] if 'conditional_limit' in data else record.conditional_limit data['conditional_count'] = data[ 'conditional_count'] if 'conditional_count' in data else record.conditional_count data['conditional_reset'] = data[ 'conditional_reset'] if 'conditional_reset' in data else record.conditional_reset if 'data' in data: record_type_properties = records.get_record_type_properties( data['type'], clean=True) data['data'], errors = self.__parse_data_properties( data['data'], record_type_properties) if len(errors) > 0: return self.send_error_response( 5005, 'Invalid type property fields', errors) else: data['data'] = record.data if 'conditional_data' in data: record_type_properties = records.get_record_type_properties( data['type'], clean=True) data['conditional_data'], errors = self.__parse_data_properties( data['conditional_data'], record_type_properties) if len(errors) > 0: return self.send_error_response( 5005, 'Invalid type property fields', errors) else: data['conditional_data'] = record.conditional_data record = records.save(record, zone.id, data['ttl'], data['class'], data['type'], data['data'], data['active']) record = records.save_conditions(record, enabled=data['is_conditional'], data=data['conditional_data'], count=data['conditional_count'], limit=data['conditional_limit'], reset=data['conditional_reset']) return self.one(user_id, record.id, zone_id=zone.id)
def record_edit_save(dns_zone_id, dns_record_id): provider = Provider() zones = provider.dns_zones() records = provider.dns_records() if not zones.can_access(dns_zone_id, current_user.id): flash('Access Denied', 'error') return redirect(url_for('home.index')) elif dns_record_id > 0: if not records.can_access(dns_zone_id, dns_record_id): flash('Access Denied', 'error') return redirect(url_for('home.index')) zone = zones.get(dns_zone_id) dns_types = records.get_types() dns_classes = records.get_classes() ttl = int(request.form['ttl'].strip()) cls = request.form['class'].strip() type = request.form['type'].strip() active = True if int(request.form.get('active', 0)) == 1 else False has_conditional_responses = True if int( request.form.get('has_conditional_responses', 0)) == 1 else False if ttl < 0: flash('Invalid TTL value', 'error') return redirect( url_for('dns.record_edit', dns_zone_id=dns_zone_id, dns_record_id=dns_record_id)) elif cls not in dns_classes: flash('Invalid class value', 'error') return redirect( url_for('dns.record_edit', dns_zone_id=dns_zone_id, dns_record_id=dns_record_id)) elif type not in dns_types: flash('Invalid type value', 'error') return redirect( url_for('dns.record_edit', dns_zone_id=dns_zone_id, dns_record_id=dns_record_id)) # Depending on the type, get the right properties. data = gather_record_data(type) if data is False: # Flash errors should already be set in gather_record_data() return redirect( url_for('dns.record_edit', dns_zone_id=dns_zone_id, dns_record_id=dns_record_id)) if dns_record_id > 0: record = records.get(dns_record_id, zone.id) if not record: flash('Could not get record', 'error') return redirect( url_for('dns.record_edit', dns_zone_id=dns_zone_id, dns_record_id=dns_record_id)) else: record = records.create() record = records.save(record, zone.id, ttl, cls, type, data, active) if not record: flash('Could not save record', 'error') return redirect( url_for('dns.record_edit', dns_zone_id=dns_zone_id, dns_record_id=dns_record_id)) records.save_conditions(record, enabled=has_conditional_responses) flash('Record saved', 'success') return redirect(url_for('dns.zone_view', dns_zone_id=dns_zone_id))
def record_conditions_edit_save(dns_zone_id, dns_record_id): provider = Provider() zones = provider.dns_zones() records = provider.dns_records() if not zones.can_access(dns_zone_id, current_user.id): flash('Access Denied', 'error') return redirect(url_for('home.index')) elif dns_record_id > 0: if not records.can_access(dns_zone_id, dns_record_id): flash('Access Denied', 'error') return redirect(url_for('home.index')) elif dns_record_id == 0: flash('Please create and save your record first', 'error') return redirect(url_for('home.index')) record = records.get(dns_record_id, dns_zone_id) if not record: flash('There was an error retrieving the DNS Record', 'error') return redirect( url_for('dns.record_conditions_edit', dns_zone_id=dns_zone_id, dns_record_id=dns_record_id)) conditional_limit = int(request.form['conditional_limit'].strip()) conditional_reset = True if int(request.form.get('conditional_reset', 0)) == 1 else False conditional_count = request.form['conditional_count'].strip() conditional_count = 0 if len(conditional_count) == 0 else int( conditional_count) if conditional_limit <= 0: flash('Invalid DNS query limit', 'error') return redirect( url_for('dns.record_conditions_edit', dns_zone_id=dns_zone_id, dns_record_id=dns_record_id)) elif conditional_count < 0: flash('Invalid DNS query current count', 'error') return redirect( url_for('dns.record_conditions_edit', dns_zone_id=dns_zone_id, dns_record_id=dns_record_id)) # Depending on the type, get the right properties. data = gather_record_data(record.type) if data is False: # Flash errors should already be set in gather_record_data() return redirect( url_for('dns.record_edit', dns_zone_id=dns_zone_id, dns_record_id=dns_record_id)) record = records.save_conditions(record, data=data, count=conditional_count, limit=conditional_limit, reset=conditional_reset) if not record: flash('Could not save record', 'error') return redirect( url_for('dns.record_edit', dns_zone_id=dns_zone_id, dns_record_id=dns_record_id)) flash('Record saved', 'success') return redirect(url_for('dns.zone_view', dns_zone_id=dns_zone_id))