܍~cp.0ddlZddlZddlZddlZddlZddlZddlmZddlZddl m Z m Z ddl m Z mZejeZdZGddeZGdd eZGd d eZGd d eZddZdZddZdZddZddZdZdS)N)StringIO)subputil)find_fallback_nicget_devicelistz/run/systemd/netif/leasesceZdZdZdS)NoDHCPLeaseErrorz'Raised when unable to get a DHCP lease.N__name__ __module__ __qualname____doc__4/usr/lib/python3/dist-packages/cloudinit/net/dhcp.pyr r s1111rr ceZdZdZdS)InvalidDHCPLeaseFileErrorzRaised when parsing an empty or invalid dhclient.lease file. Current uses are DataSourceAzure and DataSourceEc2 during ephemeral boot to scrape metadata. Nr rrrrrsrrceZdZdZdS)NoDHCPLeaseInterfaceErrorz7Raised when unable to find a viable interface for DHCP.Nr rrrrr%sAAAArrceZdZdZdS)NoDHCPLeaseMissingDhclientErrorz$Raised when unable to find dhclient.Nr rrrrr)s....rrc|9t}|(tdtn9|t vr)td|tt jd}|s(tdtt|||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.) rLOGdebugrrrwhichrdhcp_discovery)nic dhcp_log_functmp_dir dhclient_paths rmaybe_perform_dhcp_discoveryr"-s {!! ; III J J J+-- -  N$$ $ $ F   ()))Jz**M 0 KLLL-/// -m < <.*?)}\nrz&Cannot parse empty dhcp lease file {0};"zoption  z1Cannot parse dhcp lease file {0}. No leases found)recompileDOTALLr load_filelenrformatfindallsplitstripreplaceappenddict) lease_file lease_regex dhcp_leases lease_contentlease lease_optionslines rparse_dhcp_lease_filer<LsU*8")DDKKN:..M =Q' 4 ; ;J G G   $$]3300 KK$$ 5 5D::<<''R0088BGGD   C!3!3 4 4 4 44 ..////  ' ? F F     rc *td|d}d}tjt5t j|t j|dddn #1swxYwYtjdddd|d gd |d d d|d||ddg }tj|d \}}tj ||gdd}|r:t dd d|DgSd} d} tddD]} tj |} t| } tj| } | dkr>td| t j| t$jd } n&n#t($rYnwxYwt+jd| std| | d| |||t1|S) aRun 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.leaseNiplinksetdevupT)capturez-1z-vz-lfz-pfz-sfz /bin/trueg{Gz?)maxwaitnaplenz+dhclient did not produce expected files: %sz, c3TK|]#}tj|V$dSN)ospathbasename).0fs r z!dhcp_discovery..s2;;abg&&q));;;;;;runknownFrir(zkilling dhclient with pid=%szCdhclient(pid=%s, parentpid=%s) failed to daemonize after %s secondsg$@)rr contextlibsuppressFileNotFoundErrorrIremoverrwait_for_fileswarningjoinranger,r1int get_proc_ppidkillsignalSIGKILL ValueErrortimesleeperrorr<)dhclient_cmd_path interfacerpid_filer5cmdouterrmissingppid daemonized_ pid_contentpids rrrnsII19=== #H&J  . / / ( * ItVUE9d;TJJJJ      Cyd+++HC! :$G 9 II;;7;;; ; ;    DJ 1d^^  nX..4466  k""C%c**Dqyy 8#>>>V^,,,!       D  4             c3  , ,,s#)A..A25A2 F44 GGcbttjt|dS)zParse 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)r4 configobj ConfigObjr)contents rnetworkd_parse_leaserrs*  #HW$5$55III J JJrc |t}i}tj|s|Stj|D]D}t t jtj||||<E|S)zReturn 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.) NETWORKD_LEASES_DIRrIrJisdirlistdirrrrr,rV)leases_dretlfiles rnetworkd_load_leasesrzs & C 7== " " H%%  ) N27<<%88 9 9  E  Jrc|t}t|}t|D]$\}}||r ||cS%dS)N)rw)rtrzsorteditemsget)keynamerwleases_ifindexdatas rnetworkd_get_option_from_leasesrsn& !8 4 4 4F 00!!$ 88G   !=  ! 4rc dddtjdD}g}fd}d}t|D]`\}}||kr t |}|t ddvrd}t ||d |kr&|||t ||d |cSd ||d z|d z} d ||d z||z} ||z}n|t d dvrd}t ||d |kr&|||t ||d |cSd ||d z|dzdgz} d ||dz||z} ||z}n|t dd vrd}t ||d |kr&|||t ||d |cSd ||d z|dzddgz} d ||dz||z} ||z}n9|t d dvrd}t ||d |kr&|||t ||d |cSd ||d z|dzgdz} d ||dz||z} ||z}n|dkrnd }t ||d |kr&|||t ||d |cSd} d ||d z||z} ||z}nt d||cS| | d|| fb|S)aparse 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 r$cg|]}||Srr)rLtoks r z'parse_static_routes.. s @ @ @cC @c @ @ @rz[, .]cXd|d|d|d}t|dS)Nz5RFC3442 string malformed. Current route has CIDR of z and requires z significant octets, but only z< remain. Verify DHCP rfc3442-classless-static-routes value: )rr`)cidrrequiredremainmsgrfc3442s r _trunc_errorz)parse_static_routes.._trunc_errors= ttXXXvvvww 0 #rr! N.r(rD0)rrrz0.0.0.0zSParsed invalid net length "%s". Verify DHCP rfc3442-classless-static-routes value./) rstripr)r0 enumeraterXrWr-rVrr`r3) rtokens static_routesr current_idxidxr net_lengthreq_toks net_addressgateways ` rparse_static_routesrs^4nnS!!G @ @RXh88 @ @ @FMKf%%4M4MS    XX r2 & &H6#$$<  8++ Z3vcdd|3D3DEEE$$$$((6#'C!G*;#<==KhhvcAgh&>?@@G.KK 5R== ( (H6#$$<  8++ Z3vcdd|3D3DEEE$$$$((6#'C!G*;#<u#DEEKhhvcAgh&>?@@G.KK 5B<< ' 'H6#$$<  8++ Z3vcdd|3D3DEEE$$$$((6#'C!G*;#<Sz#IJJKhhvcAgh&>?@@G.KK 5A;; & &H6#$$<  8++ Z3vcdd|3D3DEEE$$$$((6#'C!G*;#<#NOOKhhvcAgh&>?@@G.KK 1__H6#$$<  8++ Z3vcdd|3D3DEEE$$$$#KhhvcAgh&>?@@G.KK II9    ! jjA7KLLLL r)NNNrH)rPloggingrIr)r[r^iorro cloudinitrr cloudinit.netrr getLoggerr rrt Exceptionr rrrr"r<rrrrzrrrrrrs  ;;;;;;;;g!!122222y222 0BBBBB 0BBB/////&6///====>DX-X-X-X-vKKK&^^^^^r