HEX
Server: Apache/2.4.52 (Ubuntu)
System: Linux ip-10-0-8-47 6.8.0-1021-aws #23~22.04.1-Ubuntu SMP Tue Dec 10 16:31:58 UTC 2024 aarch64
User: ubuntu (1000)
PHP: 8.1.2-1ubuntu2.22
Disabled: NONE
Upload Files
File: //lib/python3/dist-packages/cloudinit/net/__pycache__/dhcp.cpython-310.pyc
o

�Adp.�@s�ddlZddlZddlZddlZddlZddlZddlmZddlZddl	m
Z
mZddlm
Z
mZe�e�ZdZGdd�de�ZGdd	�d	e�ZGd
d�de�ZGdd
�d
e�Zddd�Zdd�Zddd�Zdd�Zddd�Zddd�Zdd�ZdS)�N)�StringIO)�subp�util)�find_fallback_nic�get_devicelistz/run/systemd/netif/leasesc@�eZdZdZdS)�NoDHCPLeaseErrorz'Raised when unable to get a DHCP lease.N��__name__�
__module__�__qualname__�__doc__�rr�4/usr/lib/python3/dist-packages/cloudinit/net/dhcp.pyr�rc@r)�InvalidDHCPLeaseFileErrorz�Raised when parsing an empty or invalid dhclient.lease file.

    Current uses are DataSourceAzure and DataSourceEc2 during ephemeral
    boot to scrape metadata.
    Nr	rrrrrrrc@r)�NoDHCPLeaseInterfaceErrorz7Raised when unable to find a viable interface for DHCP.Nr	rrrrr%rrc@r)�NoDHCPLeaseMissingDhclientErrorz$Raised when unable to find dhclient.Nr	rrrrr)rrcCsn|durt�}|durt�d�t��n|t�vr"t�d|�t��t�d�}|s1t�d�t��t|||�S)aJPerform dhcp discovery if nic valid and dhclient command exists.

    If the nic is invalid or undiscoverable or dhclient command is not found,
    skip dhcp_discovery and return an empty dict.

    @param nic: Name of the network interface we want to run dhclient on.
    @param dhcp_log_func: A callable accepting the dhclient output and error
        streams.
    @param tmp_dir: Tmp dir with exec permissions.
    @return: A list of dicts representing dhcp options for each lease obtained
        from the dhclient discovery if run, otherwise an empty list is
        returned.
    Nz1Skip dhcp_discovery: Unable to find fallback nic.z8Skip dhcp_discovery: nic %s not found in get_devicelist.�dhclientz7Skip dhclient configuration: No dhclient command found.)	r�LOG�debugrrr�whichr�dhcp_discovery)�nic�
dhcp_log_func�tmp_dir�
dhclient_pathrrr�maybe_perform_dhcp_discovery-s 
�
�

rcCs�t�dtj�}g}t�|�}t|�dkrtd�|���|�|�D]+}g}|�	d�D]}|�
��dd��dd�}|s:q)|�|�	dd	��q)|�t
|��q |sUtd
�|���|S)aParse the given dhcp lease file for the most recent lease.

    Return a list of dicts of dhcp options. Each dict contains key value pairs
    a specific lease in order from oldest to newest.

    @raises: InvalidDHCPLeaseFileError on empty of unparseable leasefile
        content.
    zlease {(?P<lease>.*?)}\nrz&Cannot parse empty dhcp lease file {0}�;�"�zoption � �z1Cannot parse dhcp lease file {0}. No leases found)�re�compile�DOTALLr�	load_file�lenr�format�findall�split�strip�replace�append�dict)�
lease_file�lease_regex�dhcp_leases�
lease_content�lease�
lease_options�linerrr�parse_dhcp_lease_fileLs,	
���r6c
Cs�t�d|�d}d}t�t��t�|�t�|�Wd�n1s$wYtjdddd|d	gd
d�|dd
d|d||ddg
}tj|d
d�\}}tj	||gddd�}|rgt�
dd�dd�|D���gSd}	d}
tdd�D]8}t�
|���}zt|�}
Wn	ty�Ynwt�|
�}	|	dkr�t�d|
�t�|
tj�d
}
nt�d�qp|
s�t�d||	d �|dur�|||�t|�S)!a�Run dhclient on the interface without scripts or filesystem artifacts.

    @param dhclient_cmd_path: Full path to the dhclient used.
    @param interface: Name of the network interface on which to dhclient.
    @param dhcp_log_func: A callable accepting the dhclient output and error
        streams.

    @return: A list of dicts of representing the dhcp leases parsed from the
        dhclient.lease file or empty list.
    z!Performing a dhcp discovery on %sz/run/dhclient.pidz/run/dhclient.leaseN�ip�link�set�dev�upT)�capturez-1z-vz-lfz-pfz-sfz	/bin/true�g{�G�z�?)�maxwait�naplenz+dhclient did not produce expected files: %sz, css�|]	}tj�|�VqdS�N)�os�path�basename)�.0�frrr�	<genexpr>�s�z!dhcp_discovery.<locals>.<genexpr>�unknownFri�r"zkilling dhclient with pid=%szCdhclient(pid=%s, parentpid=%s) failed to daemonize after %s secondsg$@)rr�
contextlib�suppress�FileNotFoundErrorrA�removerr�wait_for_files�warning�join�ranger&r+�int�
ValueError�
get_proc_ppid�kill�signal�SIGKILL�time�sleep�errorr6)�dhclient_cmd_path�	interfacer�pid_filer/�cmd�out�err�missing�ppid�
daemonized�_�pid_content�pidrrrrnsn
��	
���
�
rcCsttjt|�dd��S)z�Parse a systemd lease file content as in /run/systemd/netif/leases/

    Parse this (almost) ini style file even though it says:
      # This is private data. Do not parse.

    Simply return a dictionary of key/values.F)�list_values)r.�	configobj�	ConfigObjr)�contentrrr�networkd_parse_lease�sricCsP|durt}i}tj�|�s|St�|�D]}tt�tj�||���||<q|S)z�Return a dictionary of dictionaries representing each lease
    found in lease_d.i

    The top level key will be the filename, which is typically the ifindex.N)	�NETWORKD_LEASES_DIRrArB�isdir�listdirrirr&rN)�leases_d�ret�lfilerrr�networkd_load_leases�s
�rpcCsF|durt}t|d�}t|���D]\}}|�|�r ||SqdS)N)rm)rjrp�sorted�items�get)�keynamerm�leases�_ifindex�datarrr�networkd_get_option_from_leases�s

�rxcs ��d��dd�t�d��D�}g}�fdd�}d}t|�D�]n\}}||kr(qt|�}|tdd	�vrod
}t||d��|krO|||t||d���|Sd�||d
|d��}	d�||d||��}
||}�n|tdd�vr�d}t||d��|kr�|||t||d���|Sd�||d
|d�dg�}	d�||d||��}
||}n�|td
d�vr�d}t||d��|kr�|||t||d���|Sd�||d
|d�ddg�}	d�||d||��}
||}n�|td
d
�v�rBd}t||d��|k�r|||t||d���|Sd�||d
|d�gd��}	d�||d||��}
||}n@|dk�rxd}t||d��|k�rd|||t||d���|Sd}	d�||d
||��}
||}n
t�	d|�|S|�
d|	|f|
f�q|S)a�parse rfc3442 format and return a list containing tuple of strings.

    The tuple is composed of the network_address (including net length) and
    gateway for a parsed static route.  It can parse two formats of rfc3442,
    one from dhcpcd and one from dhclient (isc).

    @param rfc3442: string in rfc3442 format (isc or dhcpd)
    @returns: list of tuple(str, str) for all valid parsed routes until the
              first parsing error.

    E.g.
    sr=parse_static_routes("32,169,254,169,254,130,56,248,255,0,130,56,240,1")
    sr=[
        ("169.254.169.254/32", "130.56.248.255"), ("0.0.0.0/0", "130.56.240.1")
    ]

    sr2 = parse_static_routes("24.191.168.128 192.168.128.1,0 192.168.128.1")
    sr2 = [
        ("191.168.128.0/24", "192.168.128.1"), ("0.0.0.0/0", "192.168.128.1")
    ]

    Python version of isc-dhclient's hooks:
       /etc/dhcp/dhclient-exit-hooks.d/rfc3442-classless-routes
    rcSsg|]}|r|�qSrr)rD�tokrrr�
<listcomp>sz'parse_static_routes.<locals>.<listcomp>z[, .]csd|||�f}t�|�dS)Nz�RFC3442 string malformed.  Current route has CIDR of %s and requires %s significant octets, but only %s remain. Verify DHCP rfc3442-classless-static-routes value: %s)rrX)�cidr�required�remain�msg��rfc3442rr�_trunc_errors

��z)parse_static_routes.<locals>._trunc_errorr��!�	N�.r"r=����0����)r�r�r�z0.0.0.0zSParsed invalid net length "%s".  Verify DHCP rfc3442-classless-static-routes value.z%s/%s)�rstripr#r*�	enumeraterPrOr'rNrrXr-)r��tokens�
static_routesr��current_idx�idxry�
net_length�req_toks�net_address�gatewayrrr�parse_static_routes�sp
	 
"
"


�r�)NNNr@)rH�loggingrAr#rTrV�iorrf�	cloudinitrr�
cloudinit.netrr�	getLoggerr
rrj�	Exceptionrrrrrr6rrirprxr�rrrr�<module>s.


"[