package documentation

General utilities

From __init__.py:

Exception ValidationError Undocumented
Function check_password Check if the given password matches the given shadow password hash.
Function generic_error Generate a generic error for the APIs.
Function get_all_by_option Return all sections with the given option value
Function get_all_by_type Return all sections of the given utype from the given config
Function get_all_device_ips Retrieve all device IPs
Function get_all_devices_by_zone Retrieve all devices associated to the given zone
Function get_all_lan_devices Retrieve all devices associated to the lan zone, except for VPN ones
Function get_all_wan_devices Retrieve all devices associated to the wan zone
Function get_all_wan_ips List all WAN interfaces with their IP addresses Exclude bond management IPs.
Function get_all_zone_devices_with_interfaces Retrieve all devices associated to firewall zones, along with the logical interfaces bound to them
Function get_device_name Retrieve the physical device name given the MAC address
Function get_id Return a valid UCI id based on the given string. All auto-generated NethSecurity ids:
Function get_interface_from_device Retrieve the logical UCI interface name given the device name
Function get_interface_from_mac Retrieve the logical UCI interface name given the MAC address
Function get_interface_l3_device Retrieve the network device that carries the IP traffic of the given logical interface. Alias interfaces are resolved to the device of their parent interface, aliases of aliases are not supported.
Function get_public_ip_addresses Return the public addresses associated to a private IP address.
Function get_random_id Return a random valid UCI id.
Function get_unassigned_devices Retrieve all unused/unassigned devices.
Function get_user_by_username Retrieve the user object id (UCI section) given the username
Function sanitize Replace illegal chars with _ char. UCI identifiers and config file names may contain only the characters a-z, 0-9 and _
Function set_if_changed Drop-in replacement for uci.set that skips the write when the stored value already matches, so UCI records no change
Function shadow_password Generates a shadow password hash using SHA-512 algorithm.
Function validate_mac_address Ensure the given MAC address is lowercase and colon-separated (e.g. '00:1a:2b:3c:4d:5e').
Function validate_uci_name Ensure the given name is safe to use as a UCI section identifier, i.e. it would not be altered by sanitize().
Function validate_username Ensure the given user/group name is safe to store and to use as a filesystem/certificate component. Allows dotted and email-style logins (e.g. 'first.last', 'user@example.com') coming from local or LDAP databases.
Function validation_error Generate a validation error for the APIs.
Function validation_errors Generate a validation error for the APIs from an array.
Constant _VALID_MAC_ADDRESS Undocumented
Constant _VALID_USERNAME Undocumented
def check_password(password, shadow): (source)

Check if the given password matches the given shadow password hash.

Arguments:
  • password (str) - the password to be checked.
  • shadow (str) - the shadow password hash to be checked.
Returns:
  • True if the password matches the hash, False otherwise
def generic_error(error): (source)

Generate a generic error for the APIs.

Arguments:
  • message -- An error message
Returns:
  • A validation error object
def get_all_by_option(uci, config, option, value, deep=True): (source)

Return all sections with the given option value

Arguments:
  • uci -- EUci pointer
  • config -- Configuration database name
  • option -- Option name
  • value -- Option value
  • deep - If true, return a dict of all matched keys, otherwise return a list of section names
Returns:
  • A dictionary or a list of all matched sections
def get_all_by_type(uci, config, utype): (source)

Return all sections of the given utype from the given config

Arguments:
  • uci -- EUci pointer
  • config -- Configuration database name
  • utype -- Section type
Returns:
  • A dictionary of all matched sections, None in case of error
def get_all_device_ips(): (source)

Retrieve all device IPs

def get_all_devices_by_zone(uci, zone, exclude_aliases=False): (source)

Retrieve all devices associated to the given zone

Arguments:
  • uci -- EUci pointer
  • zone -- Firewall zone name
Returns:
  • A list of device names
def get_all_lan_devices(uci): (source)

Retrieve all devices associated to the lan zone, except for VPN ones

Arguments:
  • uci -- EUci pointer
Returns:
  • A list of device names
def get_all_wan_devices(uci, exclude_aliases=False): (source)

Retrieve all devices associated to the wan zone

Arguments:
  • uci -- EUci pointer
  • exclude_aliases -- If true, exclude devices starting with @
Returns:
  • A list of device names
def get_all_wan_ips(uci): (source)

List all WAN interfaces with their IP addresses Exclude bond management IPs.

Arguments:
  • uci -- EUci pointer
Returns:
  • A list of dictionaries with device and IP address
def get_all_zone_devices_with_interfaces(uci): (source)

Retrieve all devices associated to firewall zones, along with the logical interfaces bound to them

Arguments:
  • uci -- EUci pointer
Returns:
  • A dictionary with device names as keys and lists of interface names as values, interfaces named like the device itself are not listed
def get_device_name(hwaddr): (source)

Retrieve the physical device name given the MAC address

Aarguments:
  • hwaddr -- MAC address string
Returns:
  • The device name as a string if the network interface has been found, None otherwise.
def get_id(name, length=100): (source)

Return a valid UCI id based on the given string. All auto-generated NethSecurity ids:

  • have a maximum length of 100 characters
  • start with ns_ prefix
  • are sanitized accordingly to UCI conventions
Arguments:
  • name -- the name of the section
  • length -- maximum id length, default is 100. Maximum length for firewall zones is 15.
Returns:
  • a valid UCI identifier as string
def get_interface_from_device(uci, device): (source)

Retrieve the logical UCI interface name given the device name

Arguments:
  • uci -- EUci pointer
  • device -- Device name
Returns:
  • The device name as a string if the interface has been found, None otherwise
def get_interface_from_mac(uci, hwaddr): (source)

Retrieve the logical UCI interface name given the MAC address

Arguments:
  • uci -- EUci pointer
  • hwaddr -- MAC address string
Returns:
  • The device name as a string if the interface has been found, None otherwise
def get_interface_l3_device(uci, interface): (source)

Retrieve the network device that carries the IP traffic of the given logical interface. Alias interfaces are resolved to the device of their parent interface, aliases of aliases are not supported.

Arguments:
  • uci -- EUci pointer
  • interface -- Logical interface name
Returns:
  • The device name as a string, None if it can't be resolved
def get_public_ip_addresses(ip_address=''): (source)

Return the public addresses associated to a private IP address.

Arguments:
  • ip_address -- the private IP address of a network interface. If not specified, the default network interface is used.
Returns:
  • a list of public addresses. Usually, the list contains only one element.
def get_random_id(): (source)

Return a random valid UCI id.

Random ids:
  • have a length of 11 characters
  • are sanitized accordingly to UCI conventions (see 'sanitize' function)
  • start with ns_ prefix
Arguments:
  • name -- the name of the section
Returns:
  • a valid UCI identifier as string
def get_unassigned_devices(uci): (source)

Retrieve all unused/unassigned devices.

Arguments:
  • uci -- EUci pointer
Returns:
  • A list of devices
def get_user_by_username(uci, username): (source)

Retrieve the user object id (UCI section) given the username

Arguments:
  • uci -- EUci pointer
  • username -- Username
Returns:
  • The user object id (UCI section) if the user has been found, None otherwise
def sanitize(name): (source)

Replace illegal chars with _ char. UCI identifiers and config file names may contain only the characters a-z, 0-9 and _

Arguments:
  • name -- the name of the section
Returns:
  • a string with valid chachars for UCI
def set_if_changed(uci, config: str, section: str, option: str, value: str | list[str] | tuple[str, ...]): (source)

Drop-in replacement for uci.set that skips the write when the stored value already matches, so UCI records no change

Arguments:
  • uci -- EUci pointer
  • config -- Configuration database name
  • section -- Section name
  • option -- Option name
  • value -- Value to set, either a scalar or a list
Returns:
  • Whatever uci.set returns
def shadow_password(password): (source)

Generates a shadow password hash using SHA-512 algorithm.

Arguments:
  • password (str) - the password to be hashed.
Returns:
  • the shadow password hash as strin
def validate_mac_address(mac, parameter='mac'): (source)

Ensure the given MAC address is lowercase and colon-separated (e.g. '00:1a:2b:3c:4d:5e').

Arguments:
  • mac -- the MAC address to validate
  • parameter -- the parameter name to report in the ValidationError (default: 'mac')
Raises:
  • ValidationError if the MAC address is not valid
def validate_uci_name(name, parameter='name'): (source)

Ensure the given name is safe to use as a UCI section identifier, i.e. it would not be altered by sanitize().

Arguments:
  • name -- the name to validate
  • parameter -- the parameter name to report in the ValidationError (default: 'name')
Raises:
  • ValidationError if the name is empty or would be altered by sanitize()
def validate_username(name, parameter='name'): (source)

Ensure the given user/group name is safe to store and to use as a filesystem/certificate component. Allows dotted and email-style logins (e.g. 'first.last', 'user@example.com') coming from local or LDAP databases.

Arguments:
  • name -- the name to validate
  • parameter -- the parameter name to report in the ValidationError (default: 'name')
Raises:
  • ValidationError if the name is empty or contains disallowed characters
def validation_error(parameter, message='', value=''): (source)

Generate a validation error for the APIs.

Arguments:
  • parameter -- The name of the parameter
  • message -- The validation error reason, default is empty
  • value -- The parmeter original value that caused the error
Returns:
  • A validation error object
def validation_errors(errors): (source)

Generate a validation error for the APIs from an array.

Arguments:
  • errors -- An array of array errors
Each array element is an array composed by 3 parameters:
  • the name of the parameter
  • the validation error reason
  • the parameter original value that caused the error
Returns:
  • A validation error object
_VALID_MAC_ADDRESS = (source)

Undocumented

Value
re.compile(r'([0-9a-f]{2}:){5}[0-9a-f]{2}')
_VALID_USERNAME = (source)

Undocumented

Value
re.compile(r'^[a-zA-Z0-9\._@-]+$')