ν`4XddlZddlZddlZddlZddlmZmZmZmZGddZ dS)N)DictListOptionalAnyc eZdZdZdejfdZdeefdZ defdZ de efdZ de eeffdZd e ede efd Zd e ede eeffd Zde eeffd Z ddede eefdeddfdZdS)ApacheProcessorz? Collects Apache configuration and module information. loggercbgd|_gd|_||_d|_d|_d|_dS)N)z/etc/apache2/conf/z/etc/apache2/conf-available/z/etc/apache2/conf.d/z/etc/apache2/conf.modules.d/)z$/etc/apache2/conf.d/userdata/ssl/2_4z$/etc/apache2/conf.d/userdata/std/2_4z$/etc/apache2/conf.d/userdata/ssl/2_2z$/etc/apache2/conf.d/userdata/std/2_2)apache_config_locationsuser_config_base_pathsr _apache_binary_cache_apache_version_cache_apache_modules_cache)selfr s +py/cl_website_collector/apache_processor.py__init__zApacheProcessor.__init__sQ( ( ( $' ' ' # $(!%)"%)"""returnc`|j|jSd} dD]]}tj|}|rEtj|tjr&|jd|||_|cS^n#t$r }|}Yd}~nd}~wwxYw|jd|d|_dS)zQ Find Apache binary executable using which command with caching. N)httpdapache2zFound Apache binary: %sz"Apache binary not found in PATH %s) r shutilwhichosaccessX_OKr debug Exception)rerror binary_name binary_pathes r_find_apache_binaryz#ApacheProcessor._find_apache_binary+s  $ 0, , 3 ' ' $l;77 '29["'#B#B'K%%&?MMM0;D-&&&&  '    EEEEEE  >FFF$(!tsAA31A33 B =BB c|j|jS|}|sd|_|jS tj|dgdddd}|jdkrF|j}|r#|d d}||_n)d |_n!d |j|_n8#tj $r d |_Yn t$r}d ||_Yd}~nd}~wwxYw|jS)z> Get Apache version information with caching. NzApache binary not foundz-vT Fcapture_outputtexttimeoutcheckr zVersion info emptyzError getting version: zVersion check timed outzVersion check failed: ) rr# subprocessrun returncodestdoutstripsplitstderrTimeoutExpiredr)r apache_binaryresultversion_output first_liner"s r_get_apache_versionz#ApacheProcessor._get_apache_versionAs\  % 1- -0022  .)BD &- - F^%# F A%%!'!4!4!6!6!F!/!5!5d!;!;A!>J1;D..1ED..-^v}GZGZG\G\-^-^*( C C C)BD & & & F F F)E!)E)ED & & & & & & F))sB CC7 C7# C22C7c|j|jS|}|s(|jdg|_|jS t j|dgdddd}|jdkrg}|jd }|d dD]s}| s | d d}|r| |]#ttf$rYpwxYw|jd t||||_|S|jd |j g|_|jS#tj$r+|jdg|_|jcYSt"$r3}|jd|g|_|jcYd}~Sd}~wwxYw)z Get Apache modules using httpd -M command with caching. Returns: List of module names (e.g., ['core_module', 'so_module', 'http_module', ...]) Nz+Apache binary not found, cannot get modulesz-MTr%Fr&rr+ z!Found %d Apache modules via %s -Mz!Apache modules command failed: %sz4[WEBSITE-COLLECTOR] Apache modules command timed outz4[WEBSITE-COLLECTOR] Failed to get Apache modules: %s)rr#r rr,r-r.r/r1r0append IndexErrorAttributeErrorlenr2r3rr)rr4r5moduleslinesline module_namer"s r_get_apache_modulesz#ApacheProcessor._get_apache_modulesisF  % 1- -0022  . K  K L L L)+D &- -) .^%# F A%% ++D11"!""I ! !D::<<! !&*jjll&8&8&=&=a&@ &8#NN;777&7!!! ! !!"Es7||Ubccc-4* K  A6=CVCVCXCX Y Y Y)+D &- -( . . . K  T U U U)+D &- - - - . . . K  TVW X X X)+D &- - - - - - - .sPA#F2AC76F7D F D  4F?F7G59 G5(G0*G50G5c`|}|}||d}|S)z Get Apache system information for export as separate record. Returns: Dict with system information to be exported as single record )apache_versionloaded_modules)rDr8)rr@version system_infos rget_apache_system_infoz&ApacheProcessor.get_apache_system_infosB**,,**,,&%  r usernamescg}|D]{}|jD]q}tj||}tj|r0|||jd|r||S)a Get Apache configuration directories for specific users only. Args: usernames: List of usernames to get configurations for Returns: List of directories containing user-specific Apache configurations zFound user config directory: %s)r rpathjoinisdirr<r r)rrKuser_locationsusername base_path user_paths r_get_user_config_locationsz*ApacheProcessor._get_user_config_locationss! T TH!8 T T GLLH== 7==++T")))444K%%&GSSS  T rcdgi}|s|S ||}|jdt|t||D]9}tj|r|||d:n2#t$r%}|j d|Yd}~nd}~wwxYw|jdt|d|S)z Collect paths to user-specific Apache configuration files. Args: usernames: List of usernames to collect configs for Returns: Dict with collected config file paths as simple list config_pathsz0Scanning %d user-specific locations for %d usersFexclude_user_dirszC[WEBSITE-COLLECTOR] Error in collect_user_specific_config_paths: %sNz*Found %d user-specific Apache config files) rTr rr?rrMrO$_collect_config_paths_from_directoryrr)rrK config_datarPlocationr"s r"collect_user_specific_config_pathsz2ApacheProcessor.collect_user_specific_config_pathss6 B    h!<> C C C+ n n7==**n==h gl=mmm n h h h K  cef g g g g g g g g h FKXfLgHhHhiiisBB CB<<CcFdgi} |jD]}tj|r |dd}|||||jd||#t$r&}|j d||Yd}~d}~wwxYw|jdt|dn2#t$r%}|j d |Yd}~nd}~wwxYw|S) z Collect paths to general Apache configuration files (non-user-specific). Returns: Dict with collected config file paths as simple list rV/z/conf.drWz5Collected config paths from %s (exclude_user_dirs=%s)z:[WEBSITE-COLLECTOR] Error scanning config directory %s: %sNz$Found %d general Apache config filesz<[WEBSITE-COLLECTOR] Error in collect_global_config_paths: %s) r rrMrOrstripendswithrYr rrrr?)rrZr[ exclude_usersr"s rcollect_global_config_pathsz+ApacheProcessor.collect_global_config_pathss B  a 8 u u7==** uu(0(<(<(E(Ei(P(P AA(KTaBccc ))*a*2MCCCC$uuu ))*fhprsttttttttu u K  Dc+VdJeFfFf g g g g a a a K  \^_ ` ` ` ` ` ` ` ` asA(C/AB  C/ B<B72C/7B<<2C// D9DDFr[rZrXNcRd}tj|D]\}}}|t|dtj}||krg|dd<I|r4d|vr0|d|jd||D] } | drtj || } tj | rtj | stj | r[tj| tjr<|d| tj | d|jd|  dS) ao Helper method to collect configuration file paths from a specific directory. Args: location: Directory path to scan for .conf files config_data: Dictionary with 'config_paths' list to store collected paths exclude_user_dirs: If True, skip userdata subdirectories to avoid collecting user-specific configs r%Nuserdataz@Excluded userdata directory from general config collection in %sz.confrV) file_path is_symlinkzCannot access config file: %s)rwalkr?countsepremover rr`rMrNexistsisfileislinkrR_OKr<) rr[rZrX max_depthrootdirsfilesdepthfilenameres rrYz4ApacheProcessor._collect_config_paths_from_directory s !#!2!2 V V D$X(..rv66E !!QQQ! lZ4%7%7 J''' !!"dfjkkk! V V$$W-- V " T8 < rs  ,,,,,,,,,,,,aVaVaVaVaVaVaVaVaVaVr