o ;s*bO4@sdZdZdZddlZddlZddlmZmZddlm Z m Z m Z dd l m Z mZmZmZe eZeed Zd d ZGd ddZGddde ZGdddeZdS)z?Cyril Jaquier, Yaroslav Halchenko, Serg G. Brester (aka sebres)z\Copyright (c) 2004 Cyril Jaquier, 2007 Yaroslav Halchenko, 2015 Serg G. Brester (aka sebres)GPLN) NoOptionErrorNoSectionError)sysSafeConfigParserWithIncludeslogLevel) getLogger_as_bool _merge_dictssubstituteRecursiveTags)boolintccsvt|ttfr(|D]}t|dkr|\}}}n|d\}}}|||fVq dS|D]}||\}}|||fVq*dS)zIterator over the options template with default options. Each options entry is composed of an array or tuple with: [[type, name, ?default?], ...] Or it is a dict: {name: [type, default], ...} r N) isinstancelisttuplelen)optionsoptnameopttypeoptvaluer>/usr/lib/python3/dist-packages/fail2ban/client/configreader.py_OptionsTemplateGen)s   rc@seZdZdZd"ddZddZddZed d Zd#d d Z d$ddZ ddZ ddZ d#ddZ ddZddZd%ddZdifddZd d!ZdS)& ConfigReaderzkGeneric config reader class. A caching adapter which automatically reuses already shared configuration. NcKsZd|_d|_|dur ||_|dur||_||_d|_dS|jdur+tdi||_dSdS)Nr) _cfg_share_cfg_cfg_share_kwargs_cfg_share_basedirConfigReaderUnshared)self use_config share_configkwargsrrr__init__Ds  zConfigReader.__init__cCs |jr |j|dS||_dSN)r setBaseDirrr!basedirrrrr'Rs zConfigReader.setBaseDircCs|jr|jS|jSr&)r getBaseDirrr!rrrr*Xs zConfigReader.getBaseDircC|jSr&)rr+rrrr#^szConfigReader.share_configTcCsT|js|||r|jjdur|jjStd||j|j|}||j_|S)z Overloads a default (not shared) read of config reader. To prevent mutiple reads of config files with it includes, reads into the config reader, if it was not yet cached/shared by 'name'. Nz Loading configs for %s under %s )r_create_unsharedread_cfg_fileslogSysinfor*read)r!nameonceretrrrr1bs  zConfigReader.readcCs~|js4|jdur4|j||_|js2tdd|ji|j|_|jdur*|j|j|j|j|<dSdStdi|j|_dS)z Allocates and share a config file by it name. Automatically allocates unshared or reuses shared handle by given 'name' and init arguments inside a given shared storage. Nr#r)rrgetr rrr')r!r2rrrr-ws zConfigReader._create_unsharedcCs.z dd|jDWStygYSw)Ncss|] }|ds|VqdS)zKNOWN/N) startswith).0nrrr sz(ConfigReader.sections..)rsectionsAttributeErrorr+rrrr;  zConfigReader.sectionscCs$z|j|WStyYdSw)NF)r has_sectionr<)r!secrrrr>s  zConfigReader.has_sectioncCs&|r |j||S||jj|ivSr&)r has_option _sectionsr6)r!r?opt withDefaultrrrr@szConfigReader.has_optioncCs|j|dSr&)r get_defaultsupdate)r!drrrmerge_defaultsszConfigReader.merge_defaultscO4z|jj|g|Ri|WStyt|wr&)r merge_sectionr<rr!sectionargsr$rrrrI  zConfigReader.merge_sectionFcCs(z|j||WStyt|w)zReturn a list of option names for the given section name. Parameter `withDefault` controls the include of names from section `[DEFAULT]` )rrr<r)r!rKrCrrrrs  zConfigReader.optionscCs.z |jj||||dWStyt|w)N)rawvars)rr6r<r)r!r?rBrNrOrrrr6r=zConfigReader.getcOrHr&)r getOptionsr<rrJrrrrPrMzConfigReader.getOptions)NN)T)r5F)__name__ __module__ __qualname____doc__r%r'r*propertyr#r1r-r;r>r@rGrIrr6rPrrrrr>s"       rc@s@eZdZdZdZdddZddZdd Zd d ZdddZ dS)r zUnshared config reader (previously ConfigReader). Do not use this class (internal not shared/cached represenation). Use ConfigReader instead. z /etc/fail2banNcOs,tj|g|Ri|d|_||dSr&)rr%r.r')r!r)rLr$rrrr%szConfigReaderUnshared.__init__cCs|durtj}|d|_dS)N/)r DEFAULT_BASEDIRrstrip_basedirr(rrrr'szConfigReaderUnshared.setBaseDircCr,r&)rZr+rrrr*zConfigReaderUnshared.getBaseDircsTtj|jstd|j|drtj|}tj|j|}t d||j|dg}|d}|t t d|7}| |d|t t d|7}t ttjj|}t|rt d d |t||fd d |D}|rtd d |rdStd||dStd|d|dd |gtt|dS)Nz/Base configuration directory %s does not exist ./z Reading configs for %s under %s z.confz.dz %s/*.confz.localz %s/*.localzReading config files: %sz, csg|]}|vr|qSrr)r8cfconfig_files_readrr sz-ConfigReaderUnshared.read..zCould not read config files: %sTz0Found no accessible config files for %r under %sFz(Found no accessible config files for %r zunder %szamong existing ones: )ospathexistsrZ ValueErrorr7abspathjoinr/debugsortedglobappendrfilterrrr1errorr*r)r!filenamebasename config_files config_dirmissedrr^rr1sD        zConfigReaderUnshared.readFTc Cs(t}|dur i}t|D]\}}} ||vrq z#|j|||d} | ||<|r9t|} | r9| dur3Wq | | ||<Wq tyX} z|rEt| | ||<WYd} ~ q d} ~ wtyr| durptd||| f| ||<Yq t ytd|d|dt | d| ||<Yq w|S)NrOz/'%s' not defined in '%s'. Using default one: %rzWrong value for 'z' in 'z'. Using default one: '') dictrr6 CONVERTERrr/rlrwarningrdrepr) r!r?rpOptions shouldExistconvertvaluesrrrvconverrrrPsN       zConfigReaderUnshared.getOptionsr&)NFT) rRrSrTrUrXr%r'r*r1rPrrrrr s 2r c@sxeZdZdZgZddZddZddZdd Zd d Z d d Z ddZ dddZ ddZ ddZdddZddZdS)DefinitionInitConfigReaderzConfig reader for files with options grouped in [Definition] and [Init] sections. Is a base class for readers of filters and actions, where definitions in jails might provide custom values for options defined in [Init] section. cKsVtj|fi||drtj|}||||||_t |_ t |_ dS)Nr\) rr%r7rarbresetFile setJailName _initOptsrt_pOpts _defCache)r!file_jailNameinitOptsr$rrrr%/s     z#DefinitionInitConfigReader.__init__cCs||_i|_dSr&)_filer)r!fileNamerrrr9s z"DefinitionInitConfigReader.setFilecCr,r&)rr+rrrgetFile=r[z"DefinitionInitConfigReader.getFilecCs ||_dSr& _jailName)r!rrrrr@s z&DefinitionInitConfigReader.setJailNamecCr,r&rr+rrr getJailNameCr[z&DefinitionInitConfigReader.getJailNamecCst||jSr&)rr1rr+rrrr1FszDefinitionInitConfigReader.readcCs"|js ||jt|j|jSr&)rr-rrr1r+rrr readexplicitJs z'DefinitionInitConfigReader.readexplicitFcs|st}jrt|j}tjdj|dd_|_dr_fdd}j dddD]/}|dkr6q/d}| d sL|durE||}|jd |<|jvr^|durY||}|j|<q/|rdr dD]}|dksv|jvrwqk d|j|<qkdSdSdS) N DefinitionF)rzInitcs d|S)Nr)r6)rBr+rr\s z7DefinitionInitConfigReader.getOptions..)rCrRzknown/) rtrr rrP _configOpts_optsrr>rr7r6)r!pOptsallgetoptrBr|rr+rrPOs4        z%DefinitionInitConfigReader.getOptionsc Csxt|D]5\}}}t|}|r9||}|durqz||||<Wqty8td|||||||<YqwqdS)z:Convert interpolated combined options to expected type. Nz/Wrong %s value %r for %r. Using default one: %r)rrur6rdr/rv)r!opts configOptsrrrr}r|rrrconvertOptionsms     z)DefinitionInitConfigReader.convertOptionsc Csdz|j|WSty1z |jjd||jd}Wntttfy'd}Ynw||j|<|YSw)zGet combined definition option (as string) using pre-set and init options as preselection (values with higher precedence as specified in section). Can be used only after calling of getOptions. rrrN)rKeyErrorrget_exrrrrd)r!rr|rrr getCombOption|s   z(DefinitionInitConfigReader.getCombOptionrcCs|j}|jr t||j}t|siSt|}|D]}tj|}|r/| \}}| |qt |||j d}|s>t d|||j|S)N)ignoreaddreplz/recursive tag definitions unable to be resolved)rrr rsetcopyrCONDITIONAL_REmatchgroupsaddr rrdrr)r!r combinedoptsr9condrrrr getCombineds&     z&DefinitionInitConfigReader.getCombinedcCstr&)NotImplementedErrorr+rrrrzsz"DefinitionInitConfigReader.convertNrQ)r)rRrSrTrUrr%rrrrr1rrPrrrrzrrrrr$s    r) __author__ __copyright__ __license__rira configparserrrconfigparserincrrrhelpersr r r r rRr/rrurrr rrrrrs ym