....................................../////.===Shadow-Here===./////................................................ > < > < > < > < > < > < > < > < > < > < > < > < > < > < > < > < > < > < > < > < > < > < > < > < > < > < > < > < > < ------------------------------------------------------------------------------------------------------------------- /////////////////////////////////////////////////////////////////////////////////////////////////////////////////// RIFF¤ WEBPVP8 ˜ ðÑ *ôô>‘HŸK¥¤"§£±¨àð enü¹%½_F‘åè¿2ºQú³íªú`N¿­3ÿƒügµJžaÿ¯ÿ°~¼ÎùnúîÞÖô•òíôÁÉß®Sm¥Ü/ ‡ó˜f£Ùà<˜„xëJ¢Ù€SO3x<ªÔ©4¿+ç¶A`q@Ì“Úñè™ÍÿJÌ´ª-˜ÆtÊÛL]Ïq*‘Ý”ì#ŸÌÏãY]@ê`¿ /ªfkØB4·®£ó z—Üw¥Pxù–ÞLШKÇN¾AkÙTf½è'‰g gÆv›Øuh~ a˜Z— ïj*á¥t d£“uÒ ¨`K˜¹ßþ]b>˜]_ÏÔ6W—è2r4x•íÖ…"ƒÖNîä!¦å Ú}ýxGøÌ —@ ;ÆÚŠ=ɾ1ý8lªË¥ô ^yf®Œ¢u&2©nÙÇ›ñÂñŒ³ aPo['½»øFùà­+4ê“$!lövlüÞ=;N®3ð‚õ›DÉKòÞ>ÄÍ ¥ˆuߤ#ˆ$6ù™¥îŠ‡y’ÍB¼ çxÛ;X"WL£R÷͝*ó-¶Zu}º.s¸sšXqù–DþÿvªhüïwyŸ ¯é³lÀ:KCûÄ£Ëá\…­ ~—ýóî ¼ûûÜTÓüÇy…ŽÆvc»¾×U ñ¸žþоP÷¦ó:Ò¨¨5;Ð#&#ÖúñläÿÁœ GxÉ­/ñ‡áQðìYÉtÒwŽ¼GÔ´zàÒò ð*ëzƒ•4~H]Ø‹f ñÓÈñ`NåWçs'ÆÏW^ø¹!XžµmQ5ÃËoLœÎ: ÞËÍ¥J ù…î èo£ßPÎñ¶ž8.Œ]ʵ~5›ÙË-ù*8ÙÖß±~ ©¹rÓê‚j¶d¸{^Q'˜±Crß ÚH—#¥¥QlÀ×ëã‡DÜ«èî þ&Çæžî;ŽÏºò6ÒLÃXy&ZŒ'j‚¢Ù€IßÚù+–MGi‰*jE€‘JcÜ ÓÌ EÏÚj]o˜ Þr <¾U ûŪæÍ/šÝH¥˜b”¼ ÁñßX GP›ï2›4WŠÏà×£…íÓk†¦H·ÅíMh–*nó÷à]ÁjCº€b7<ب‹¨5車bp2:Á[UªM„QŒçiNMa#<5›áËó¸HýÊ"…×Éw¹¦ì2º–x<›»a±¸3Weü®FÝ⑱ö–î–³|LPÈ~çð~Çå‡|º kD¢µÏàÆAI %1À% ¹Ò – ”ϝS¦‰4&¶£°à Öý”û_Ò Áw°A«Å€?mÇÛgHÉ/8)á¾ÛìáöŽP í¨PŸNÙµº¦‡§Ùš"ÿ«>+ªÕ`Ê÷‡‚ß Õû˜þãÇ-PÍ.¾XV‘€ dÜ"þ4¹ ±Oú‘©t¥¦FªÄÃÄ•b‚znýu½—#cDs˜ÃiÑOˆñ×QO=*IAÊ,¶ŽZƒ;‡wøXè%EÐk:F±Ú” .Ѽ+Áu&Ç`."pÈÉw o&¿dE6‘’EqTuK@Ì¥ã™À(Êk(h‰,H}RÀIXÛš3µ1©_OqÚÒJAñ$ÊÙÜ;D3çŒ[þùœh¬Ã³™ö6ç†NY".Ú‰ï[ªŸŒ '²Ð öø_¨ÂÉ9u鶳ÒŠõTàîMØ#û¯gN‡bÙ놚X„ö …ÉeüÌ^J ‹€.œ$Æ)βÄeæW#óüßĺŸ€ ÀzwV 9oä»f4V*uB «Ë†¹ì¯žR霓æHXa=&“I4K;¯ç‹h×·"UŠ~<•â•ªVêª&ÍSÃÆÅ?ÔqÎ*mTM ˜›µwêd#[C¡©§‘D<©àb†–ÁœøvH/,í:¯( ²£|4-„Æövv„Yͼ™^Á$ˆ„¢Û[6yB.åH*V¨æ?$=˜Ñ€•î¦ñ·­(VlŸ‘ nÀt8W÷´Bûba?q9ú¶Xƒl«ÿ\ù¶’þòUÐj/õ¢Ìµ³g$ƒÎR!¸»|Oߍë’BhîÚÑ¢ñåŒJ„®„£2Ð3•ô02Nt…!£Í]Ïc½Qÿ?ˆ<&ÃA¾Ú,JˆijÌ#5yz„‰Î|ÊŽ5QÏ:‹ÐaóVÔxW—CpeÏzÐïíçôÿÅ_[hãsÐ_/ŽTÝ?BîˆííV$<¿i>²F¬_Eß¿ †bÊŒº­ÿ®Z H“C}”¬,Mp ý/Bá£w>˜YV°aƒúh+cŠ- r/[%|üUMHäQ°X»|û/@|°¥Ð !BÔ Ç¢Ä©š+Õì D«7ìN¶ŽðÔ " ƶ’ÖçtA‰Û×}{tþz­¾GÍ›k¹OEJR$ Â׃ «ëÁ"oÉôž$oUK(Ä)Ãz³Ê-‹êN[Ò3Œñbï8P 4ƒ×q¢bo|?<ÛX¬òÄÍ°L–±›(™ûG?ýË©ÚÄ–ÂDØÐ_Ç¡ô ¾–ÄÏø ×e8Ë©$ÄF¹Å‹ì[©óìl:F¾f´‹‹Xì²ï®\¬ôùƒ ÿat¥óèÒùHß0äe‚;ü×h:ÆWðHž=Ã8骣"kœ'Y?³}Tûè€>?0l›e1Lòñ„aæKÆw…hÖŠùW…ÈÆÄ0ši·›[pcwËþñiêíY/~-Á5˜!¿†A›™Mÿþ(±“t@â“ö2­´TG5yé]çå僳 .·ÍïçÝ7UÚ±Ð/Nè»,_Ï ùdj7\ï Wì4›„»c¸àešg#ÒÊ⥭áØo5‘?ÌdÝô¯ ¹kzsƒ=´#ëÉK›Ø´±-¥eW?‡çßtòTã…$Ý+qÿ±ƒ÷_3Ô¥í÷:æ–ž<·Ö‡‰Å¢ š‡%Ô—utÌÈìðžgÖÀz²À—ï÷Óîäõ{K'´È÷³yaÏÁjƒô}ž§®æÊydÕÈë5¯èˆõvÕ©ã*çD„ “z„Ó‡^^xÂ3M§A´JG‚öï 3W'ˆ.OvXè¡ÊÕª?5º7†˜(˜Ç¶#çê’¶!ÌdZK§æ 0fãaN]òY³RV ™î$®K2R¨`W!1Ôó\;Ý ýB%qæK•&ÓÈe9È0êI±žeŸß -ú@žQr¦ ö4»M¼Áè¹µmw 9 EÆE_°2ó„ŸXKWÁ×Hóì^´²GѝF©óäR†¦‰ç"V»eØ<3ùd3ÿÚ¤Žú“Gi" —‘_ÙËÎ~Üö¯¥½Î»üŸEÚŽåmÞþí ;ÞólËΦMzA"Âf(´òá;Éï(/7½ûñÌ­cïÕçлþÝz¾-ÍvÑ“pH­–ðÓj$¸Äû¤‚‘ãUBË-n“2åPkS5&‹Â|+g^œ®Ì͆d!OïäîU«c;{Û!ÅŽ«ëZ9Ókóˆ]¯ƒ›né `ÇÒ+tÆš (ØKá¾—=3œ®•vuMñg²\ï Ec€ 05±d™‡×iÇ×›UúvÌ¢£Èþ¡ÕØô¶ßÎA"ß±#Ö²ˆÊŸ¦*Ä~ij|àø.-¼'»Ú¥£h ofº¦‡VsR=N½„Î v˜Z*SÌ{=jÑB‹tê…;’HžH¯8–îDù8ñ¢|Q•bÛçš–‹m³“ê¨ åÏ^m¬Žãþ©ïêO‡½6] µÆ„Ooòü ²x}N¦Ë3ïé¿»€›HA˜m%çÞ/¿í7Fø“‹léUk)É°Œµ8Q8›:ÀŠeT*šõ~ôڝG6 ¢}`ùH­–”¡k ‰P1>š†®9z11!X wKfmÁ¦xÑ,N1Q”–æB¶M…ÒÃv6SMˆhU¬ÊPŽï‘öj=·CŒ¯u¹ƒVIЃsx4’ömÛýc塶7ßŠß 57^\wÒÐÆ k§h,Œý î«q^R½3]J¸ÇðN ‚çU¬ôº^Áì} ³f©Õœ§ˆã:FÄÈ‚é(€™?àýÓüè1Gô£¼éj‚OÅñ  #>×—ßtà 0G¥Åa뀐kßhc™À_ÉñÞ#±)GD" YîäË-ÿÙ̪ ¹™a¯´¢E\ÝÒö‚;™„ë]_ p8‰o¡ñ+^÷ 3‘'dT4œŽ ðVë½° :¬víÑ«£tßÚS-3¶“þ2 †üüʨòrš¹M{É_¤`Û¨0ìjœøJ‡:÷ÃáZ˜†@GP&œÑDGÏs¡þ¦þDGú‘1Yá9Ôþ¼ ûø…§÷8&–ÜÑnÄ_m®^üÆ`;ÉVÁJ£?â€-ßê}suÍ2sõA NÌúA磸‘îÿÚ»ƒìö·á¿±tÑÐ"Tÿü˜[@/äj¬€uüªìù¥Ý˜á8Ý´sõj 8@rˆð äþZÇD®ÿUÏ2ùôõrBzÆÏÞž>Ì™xœ“ wiÎ×7_… ¸ \#€MɁV¶¥üÕÿPÔ9Z‡ø§É8#H:ƒ5ÀÝå9ÍIŒ5åKÙŠ÷qÄ>1AÈøžj"µÂд/ªnÀ qªã}"iŸBå˜ÓÛŽ¦…&ݧ;G@—³b¯“•"´4í¨ôM¨åñC‹ïùÉó¯ÓsSH2Ý@ßáM‡ˆKÀªÛUeø/4\gnm¥‹ŸŒ qÄ b9ÞwÒNÏ_4Ég³ú=܆‚´ •â¥õeíþkjz>éÚyU«Íӝ݃6"8/ø{=Ô¢»G¥ äUw°W«,ô—¿ãㆅү¢³xŠUû™yŒ (øSópÐ 9\åTâ»—*oG$/×ÍT†Y¿1¤Þ¢_‡ ¼ „±ÍçèSaÓ 3ÛMÁBkxs‰’R/¡¤ˆÙçª(*õ„üXÌ´ƒ E§´¬EF"Ù”R/ÐNyÆÂ^°?™6¡œïJ·±$§?º>ÖüœcNÌù¯G ‹ñ2ŠBB„^·úìaz¨k:#¨Æ¨8LÎõލ£^§S&cŒÐU€ü(‡F±Š¼&P>8ÙÁ ‰ p5?0ÊƃZl¸aô š¼¡}gÿ¶zÆC²¹¬ÎÖG*HB¡O<º2#ñŒAƒ–¡B˜´É$¥›É:FÀÔx¾u?XÜÏÓvN©RS{2ʈãk9rmP¼Qq̳ è¼ÐFׄ^¡Öì fE“F4A…!ì/…¦Lƒ… … $%´¾yã@CI¬ á—3PþBÏNÿ<ý°4Ü ËÃ#ØÍ~âW«rEñw‹eùMMHß²`¬Öó½íf³:‹k˜¯÷}Z!ã¿<¥,\#öµÀ¯aÒNÆIé,Ћ–lŽ#Àæ9ÀÒS·I’½-Ïp Äz¤Š Â* ­íÄ9­< h>׍3ZkËU¹§˜ŒŠ±f­’¤º³Q ÏB?‹#µíÃ¥®@(Gs«†vI¥Mµ‹Á©e~2ú³ÁP4ìÕi‚²Ê^ö@-DþÓàlÜOÍ]n"µã:žpsŽ¢:! Aõ.ç~ÓBûH÷JCÌ]õVƒd «ú´QÙEA–¯¯Œ!.ˆˆëQ±ù œ·Ì!Õâ )ùL„ÅÀlÚè5@B…o´Æ¸XÓ&Û…O«˜”_#‡ƒ„ûÈt!¤ÁÏ›ÎÝŠ?c9 â\>lÓÁVÄÑ™£eØY]:fÝ–—ù+p{™ðè û³”g±OƒÚSù£áÁÊ„ä,ï7š²G ÕÌBk)~ÑiCµ|h#u¤¶îK¨² #²vݯGãeÖ϶ú…¾múÀ¶þÔñ‚Š9'^($¤§ò “š½{éúp÷J›ušS¹áªCÂubÃH9™D™/ZöØÁ‡¦ÝÙŸ·kð*_”.C‹{áXó€‡c¡c€§/šò/&éš÷,àéJþ‰X›fµ“C¨œ®r¬"kL‰Â_q…Z–.ÉL~O µ›zn‚¹À¦Öª7\àHµšÖ %»ÇníV[¥*Õ;ƒ#½¾HK-ÖIÊdÏEÚ#=o÷Óò³´Š: Ç?{¾+9›–‘OEáU·S€˜j"ÄaÜ ŒÛWt› á–c#a»pÔZÞdŽtWê=9éöÊ¢µ~ ë ;Öe‡Œ®:bî3±ýê¢wà¼îpêñ¹¾4 zc¾ðÖÿzdêŒÑÒŝÀ‰s6¤í³ÎÙB¿OZ”+F¤á‡3@Ñëäg©·Ž ˆèª<ù@É{&S„œÕúÀA)‰h:YÀ5^ÂÓŒ°õäU\ ùËÍû#²?Xe¬tu‰^zÒÔãë¼ÛWtEtû …‚g¶Úüâî*moGè¨7%u!]PhÏd™Ý%Îx: VÒ¦ôÊD3ÀŽKÛËãvÆî…N¯ä>Eró–ð`5 Œ%u5XkñÌ*NU%¶áœÊ:Qÿú»“úzyÏ6å-၇¾ ´ ÒÊ]y žO‘w2Äøæ…H’²f±ÎÇ.ª|¥'gîV•Ü .̘¯€šòü¤U~Ù†*¢!?ò wý,}´°ÔÞnïoKq5µb!áÓ3"vAßH¡³¡·G(ÐÎ0Îò¼MG!/ài®@—¬04*`…«é8ªøøló“ˆÊ”èù¤…ßÊoÿé'ËuÌÖ5×È¡§ˆˆfŽë9}hìâ_!!¯  B&Ëö¶‰ÀAÙNVŸ Wh›¸®XÑJì¨ú“¿÷3uj²˜¨ÍÎìë±aúŠÝå¯ð*Ó¨ôJ“yºØ)m°WýOè68†ŸÏ2—‰Ïüꪫٚ¥‹l1 ø ÏÄFjêµvÌbü¦èÝx:X±¢H=MÐß—,ˆÉÇ´(9ú¾^ÅÚ4¿m‡$âX‘å%(AlZo@½¨UOÌÕ”1ø¸jÎÀÃÃ_ µ‘Ü.œº¦Ut: Æï’!=¯uwû#,“pþÇúŒø(é@?³ü¥‘Mo §—s@Œ#)§ŒùkL}NOÆêA›¸~r½¼ÙA—HJ«eˆÖ´*¡ÓpÌŸö.m<-"³ûÈ$¬_6­åf£ïÚâj1y§ÕJ½@dÞÁr&Í\Z%D£Íñ·AZ Û³øüd/ªAi†/Й~  ‡âĮҮÏh§°b—›Û«mJžòG'[ÈYýŒ¦9psl ýÁ ®±f¦x,‰½tN ‚Xª9 ÙÖH.«Lo0×?͹m¡å†Ѽ+›2ƒF ±Ê8 7Hցϓ²Æ–m9…òŸï]Â1äN†VLâCˆU .ÿ‰Ts +ÅÎx(%¦u]6AF Š ØF鈄‘ |¢¶c±soŒ/t[a¾–û:s·`i햍ê›ËchÈ…8ßÀUÜewŒðNOƒõD%q#éû\9¤x¹&UE×G¥ Í—™$ð E6-‡¼!ýpãÔM˜ Âsìe¯ñµK¢Ç¡ùôléœ4Ö£”À Š®Ðc ^¨À}ÙËŸ§›ºê{ÊuÉC ×Sr€¤’fÉ*j!úÓ’Gsùìoîßîn%ò· àc Wp÷$¨˜)û»H ×8ŽÒ€Zj¤3ÀÙºY'Ql¦py{-6íÔCeiØp‘‡XÊîÆUߢ܂ž£Xé¼Y8þ©ëgñß}é.ÎógÒ„ÃØËø¯»™§Xýy M%@NŠ À(~áÐvu7&•,Ù˜ó€uP‡^^®=_E„jt’ 403WebShell
403Webshell
Server IP : 66.235.200.170  /  Your IP : 18.222.99.206
Web Server : Apache
System : Linux gator4410.hostgator.com 5.14.0-162.23.1.9991722448259.nf.el9.x86_64 #1 SMP PREEMPT_DYNAMIC Wed Jul 31 18:11:45 UTC 2024 x86_64
User : bmgxafte ( 1214)
PHP Version : 8.2.28
Disable Function : NONE
MySQL : OFF  |  cURL : ON  |  WGET : ON  |  Perl : ON  |  Python : OFF  |  Sudo : ON  |  Pkexec : ON
Directory :  /scripts/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Command :


[ Back ]     

Current File : /scripts/setup_greylist_db
#!/usr/local/cpanel/3rdparty/bin/perl

# cpanel - scripts/manage_greylisting              Copyright 2022 cPanel, L.L.C.
#                                                           All rights reserved.
# copyright@cpanel.net                                         http://cpanel.net
# This code is subject to the cPanel license. Unauthorized copying is prohibited

package scripts::manage_greylisting;

use strict;

use Try::Tiny;
use Getopt::Long ();

use Cpanel::JSON                          ();
use Cpanel::ForkAsync                     ();
use Cpanel::SafeDir::MK                   ();
use Cpanel::IP::GreyList                  ();
use Cpanel::LoadModule                    ();
use Cpanel::GreyList::DB                  ();
use Cpanel::GreyList::Client              ();
use Cpanel::GreyList::Config              ();
use Cpanel::GreyList::CommonMailProviders ();

exit run(@ARGV) unless caller();

sub run {
    my @cmdline_args = @_;
    return usage(1) if !@cmdline_args;

    unless ( $> == 0 && $< == 0 ) {
        return usage( 1, "[!] This program can only be run by root!\n" );
    }

    my $opts = {};
    Getopt::Long::GetOptionsFromArray(
        \@cmdline_args,
        'init|initialize'               => \$opts->{'initialize'},
        'reset'                         => \$opts->{'reset'},
        'help|h'                        => \$opts->{'help'},
        'trust|t=s@'                    => \$opts->{'trust_common'},
        'import|import_trusted_hosts=s' => \$opts->{'import_trusted_hosts'},
        'export|export_trusted_hosts'   => \$opts->{'export_trusted_hosts'},
        'export_to=s'                   => \$opts->{'export_to'},
        'update_common_mail_providers'  => \$opts->{'update_common_mail_providers'},
        'force'                         => \$opts->{'force'},
    );

    return usage(0) if $opts->{'help'};

    my $conf_dir = Cpanel::GreyList::Config::get_conf_dir();
    if ( !-d $conf_dir ) {
        require File::Path;
        File::Path::make_path($conf_dir);
    }

    my ( $opts_passed, $exit_code );

    if ( $opts->{'initialize'} || $opts->{'reset'} ) {
        $exit_code += initialize_db( $opts->{'reset'} );
        $opts->{'update_common_mail_providers'} = 1;
        $opts_passed++;
    }
    if ( $opts->{'import_trusted_hosts'} ) {
        _ensure_db_is_initialized();
        return import_trusted_hosts( $opts->{'import_trusted_hosts'} );
    }
    if ( $opts->{'export_trusted_hosts'} ) {
        _ensure_db_is_initialized();
        return export_trusted_hosts( $opts->{'export_to'} );
    }

    if ( $opts->{'update_common_mail_providers'} ) {
        _ensure_db_is_initialized();
        $exit_code += update_common_mail_providers( $opts->{'force'} );
        $opts_passed++;
    }
    if ( ref $opts->{'trust_common'} eq 'ARRAY' && scalar @{ $opts->{'trust_common'} } ) {
        _ensure_db_is_initialized();
        $exit_code += trust_common_email_service( $opts->{'trust_common'} );
        $opts_passed++;
    }

    return ( $exit_code ? 1 : 0 ) if $opts_passed;

    return usage(1);
}

sub initialize_db {
    my $force  = shift;
    my $action = "Initializing";
    my $saved_trusted_hosts;

    if ($force) {
        $action              = "Resetting";
        $saved_trusted_hosts = _save_trusted_hosts();
    }

    print "[*] $action database for the cPanel Greylist service.\n";
    Cpanel::SafeDir::MK::safemkdir( Cpanel::GreyList::Config::get_conf_dir() ) if !-d Cpanel::GreyList::Config::get_conf_dir();

    my $success = 0;
    try {
        my $db_obj = Cpanel::GreyList::DB->new( Cpanel::GreyList::Config::get_sqlite_db() );
        $success = $db_obj->initialize_db($force);
    }
    catch {
        print "[!] $action database failed: $_\n";
    };
    return 1 if !$success;

    print "[+] $action database successfully completed.\n";
    _restore_trusted_hosts($saved_trusted_hosts) if $saved_trusted_hosts;

    if ( Cpanel::GreyList::Config::is_enabled() ) {
        Cpanel::LoadModule::load_perl_module('Cpanel::ServerTasks');
        print "[*] Restarting cPGreyListd service …";
        Cpanel::ServerTasks::queue_task( ['CpServicesTasks'], "restartsrv cpgreylistd" );
        print " Done.\n";
    }
    return;
}

sub trust_common_email_service {
    my $trust_email_services = shift;

    print "[*] Updating Trusted Hosts List …\n";
    my $client = Cpanel::GreyList::Client->new();

    $client->{'disabled'} = 0;

    my $providers_in_db = Cpanel::GreyList::Client->new()->get_common_mail_providers();
    foreach my $common_service ( @{$trust_email_services} ) {
        if ( not exists $providers_in_db->{$common_service} ) {
            print "\t[!] Skipping '$common_service'. This email service is not registered, and cannot be trusted via this script. Add the IP address ranges for this email service manually via the WHM interface.\n";
            next;
        }

        try {
            if ( my $ips_trusted = $client->trust_entries_for_common_mail_provider($common_service) ) {
                print "\t[+] $ips_trusted IP(s)for '$common_service'\n";
            }
        }
        catch {
            print "\t[!] Failed to trust IP(s) for '$common_service': $_\n";
        };
    }
    print "[+] Updated Trusted Hosts List.\n";

    return;
}

sub update_common_mail_providers {
    my $force = shift;

    if ( !Cpanel::GreyList::Config::is_enabled() && !$force ) {

        print "[!] Greylisting is disabled. Skipping common mail providers update.\n";
        return;
    }

    print "[*] Updating Common Mail Providers List …\n";
    my $return_code = try {
        my $json_data        = Cpanel::GreyList::CommonMailProviders::fetch_latest_data();
        my $json_create_time = delete $json_data->{'create_time'};

        my $config = Cpanel::GreyList::Config::load_common_mail_providers_config( { map { $_ => 1 } keys %{$json_data} } );
        my $client = Cpanel::GreyList::Client->new();
        $client->{'disabled'} = 0;

        my $providers_in_db = $client->get_common_mail_providers();

        my $renamed = 0;
        foreach my $new_key ( sort keys %{$json_data} ) {

            # Do not rename an existing provider if the new provider already exists in the DB.
            next if exists $providers_in_db->{$new_key};

            next unless my $old_keys_ar = $json_data->{$new_key}->{'renamed_from'};

            # Only rename the first existing provider match if there are multiple.
            next unless my $old_key = ( grep { exists $providers_in_db->{$_} } @{$old_keys_ar} )[0];

            print "[*] Renaming '$old_key' to '$new_key' …\n";
            $client->rename_mail_provider( $old_key, $new_key );
            $config->{$new_key} = delete $config->{$old_key} // 1;
            $renamed = 1;
            print "[+] Renamed '$old_key' to '$new_key'.\n";
        }
        if ($renamed) {
            $providers_in_db = $client->get_common_mail_providers();
            Cpanel::GreyList::Config::save_common_mail_providers_config( { map { $_ => 1 } keys %{$config} }, $config );
        }

        # Remove any/all providers that were removed from the list
        my @providers_to_remove = grep { not exists $json_data->{$_} } keys %{$providers_in_db};
        foreach my $provider (@providers_to_remove) {
            print "[*] Removing '$provider' …\n";
            my $ips_for_provider = $client->list_entries_for_common_mail_provider($provider);
            $client->remove_mail_provider($provider);
            send_removal_notification( $providers_in_db->{$provider}->{'display_name'}, [ map { $_->{'host_ip'} } @{$ips_for_provider} ] );
            print "[+] Removed '$provider'\n";
        }

        foreach my $provider ( keys %{$json_data} ) {
            my $newly_added = 0;
            if ( !exists $providers_in_db->{$provider} ) {
                $client->add_mail_provider( $provider, $json_data->{$provider}->{'display_name'}, $json_create_time );
                $newly_added = 1;
            }
            else {
                # If an existing provider's display_name has changed, then update it.
                my $latest_display_name   = $json_data->{$provider}->{'display_name'};
                my $existing_display_name = $providers_in_db->{$provider}->{'display_name'};
                if ( $existing_display_name ne $latest_display_name ) {
                    print "[*] Changing name of '$provider' from '$existing_display_name' to '$latest_display_name' …\n";
                    $client->update_display_name_for_mail_provider( $provider, $latest_display_name );
                    print "[+] Changed name of '$provider' from '$existing_display_name' to '$latest_display_name'.\n";
                }
            }

            # If the provider is not marked for autoupdating, then skip it.
            if ( exists $config->{$provider} && !$config->{$provider} ) {
                print "[*] Skipping $provider per configuration …\n";
                next;
            }
            if ( !$force && ( $providers_in_db->{$provider}->{'last_updated'} || 0 ) >= $json_create_time ) {
                print "[*] No update necessary for '$provider'.\n";
                next;
            }

            print "[*] Updating $provider …\n";
            $client->delete_entries_for_common_mail_provider($provider);
            $client->add_entries_for_common_mail_provider( $provider, $json_data->{$provider}->{'ips'} );
            $client->bump_last_updated_for_mail_provider( $provider, $json_create_time );
            if ( $providers_in_db->{$provider}->{'is_trusted'} || ( $newly_added && $config->{'autotrust_new_common_mail_providers'} ) ) {
                print "[*] Marking $provider as a trusted Mail Provider …\n";
                $client->trust_entries_for_common_mail_provider($provider);
            }
        }

        Cpanel::ForkAsync::do_in_child(
            sub {
                Cpanel::IP::GreyList::update_common_mail_providers_or_log();    # Its ok if this fails, it just means they will get a 20s connect delay if enabled.
            }
        );

        print "[+] Updated Common Mail Providers list.\n";
        return 0;    ## no critic (TryTiny::ProhibitExitingSubroutine)
    }
    catch {
        print "[!] Failed to update Common Mail Providers list: $_\n";
        return 1;
    };

    return $return_code;
}

sub import_trusted_hosts {
    my $input_file = shift;

    if ( !Cpanel::GreyList::Config::is_enabled() ) {

        print "[!] Greylisting is disabled\n";
        return;
    }

    my $input_file_fh;
    if ( !open( $input_file_fh, '<', $input_file ) ) {
        print "[!] Failed to read '$input_file': $!\n";
        return 1;
    }

    my ( $json, $error );
    try {
        $json = Cpanel::JSON::LoadFile( $input_file_fh, $input_file );
    }
    catch {
        $error = $_;
    };
    if ( !$json || ref $json ne 'HASH' ) {
        print "[!] Failed to read '$input_file': Invalid JSON data.\n";
        print "[!] Error: $error\n" if $error;
        return 1;
    }

    print "[*] Importing cPGreylist Trusted Hosts:\n\n";
    my $client = Cpanel::GreyList::Client->new();
    foreach my $trusted_host ( keys %{$json} ) {
        try {
            $client->create_trusted_host( $trusted_host, $json->{$trusted_host} );
            print "[+] Added '$trusted_host' to Trusted Hosts List.\n";
        }
        catch {
            print "[!] Failed to add '$trusted_host' to the Trusted Hosts List: $_\n";
        };
    }

    return 0;
}

sub export_trusted_hosts {
    my $output_file = shift;

    if ( !Cpanel::GreyList::Config::is_enabled() ) {

        print "[!] Greylisting is disabled\n";
        return;
    }

    print STDERR "[*] Exporting cPGreylist Trusted Hosts:\n\n";
    my $output_fh;
    if ($output_file) {
        if ( !open( $output_fh, '>', $output_file ) ) {
            print "[!] Failed to open '$output_file' for writing: $!\n";
            return 1;
        }
        print STDERR "[*] Saving to '$output_file'\n";
    }

    my $error;
    try {
        my $client = Cpanel::GreyList::Client->new();
        my $output = {};

        foreach my $trusted_host ( @{ $client->read_trusted_hosts() } ) {
            $output->{ $trusted_host->{'host_ip'} } = $trusted_host->{'comment'};
        }

        print { $output_fh ? $output_fh : \*STDOUT } Cpanel::JSON::pretty_dump($output);
    }
    catch {
        print "[!] Failed to export Trusted Hosts: $_\n";
        $error++;
    };
    return 1 if $error;

    return 0;
}

sub _ensure_db_is_initialized {
    if ( !-s Cpanel::GreyList::Config::get_sqlite_db() ) {
        print "[!] SQLite Database for the cPanel Greylist service is not initialized...\n";
        initialize_db();
    }
    return 1;
}

sub _save_trusted_hosts {
    my $saved_trusted_hosts = [];
    return if !Cpanel::GreyList::Config::is_enabled();

    print "[*] Saving current Trusted Hosts List …\n";

    my $success = 0;
    try {
        my $client = Cpanel::GreyList::Client->new();
        $saved_trusted_hosts = $client->read_trusted_hosts();
        die "Failed to read Trusted Hosts List\n" if !( $saved_trusted_hosts && 'ARRAY' eq ref $saved_trusted_hosts );
        $success = 1;
    }
    catch {
        print "[!] Failed to save current Trusted Hosts List: $_\n";
    };
    return if !$success;

    print "[+] Saved " . scalar @{$saved_trusted_hosts} . " entries from the Trusted Host List.\n";
    return $saved_trusted_hosts;
}

sub _restore_trusted_hosts {
    my $saved_trusted_hosts = shift;
    print "[*] Restoring saved Trusted Hosts List …\n";

    my $success = 0;
    try {
        my $client = Cpanel::GreyList::Client->new();
        foreach my $entry ( @{$saved_trusted_hosts} ) {
            $client->create_trusted_host( $entry->{'host_ip'}, $entry->{'comment'} );
        }
        $success = 1;
    }
    catch {
        print "[!] Failed to restore Trusted Hosts List: $_\n";
    };
    return if !$success;

    print "[+] Restored " . scalar @{$saved_trusted_hosts} . " entries to the Trusted Host List.\n";
    return $saved_trusted_hosts;
}

sub usage {
    my ( $retval, $msg ) = @_;
    my $fh = $retval ? \*STDERR : \*STDOUT;

    if ( !defined $msg ) {
        $msg = <<USAGE;
$0

Utility to manage the cPanel Greylist service. Available options:

    --init   => Initialize the SQLite DB with the basic data structure as needed.
    --reset  => Forceably reset the DB.
                Note: This will attempt to preserve the Trusted Hosts List, if the greylisting service is enabled on the server.

    --trust  => Trust the IPs for the common email services specified. Specify this switch more than once to trust multiple services at the same time.
                The following common services are recognized by this script:

COMMON_EMAIL_SERVICES

    --import =>  [/path/to/json/file]
                 Imports the Trusted Hosts contained in the specified JSON file:
                 $0 --import import.json

    --export => Export the current Trusted Hosts List.
                To export the list to a file, specify the 'export_to' switch:
                    $0 --export --export_to  export.json
                Or redirect stdout:
                    $0 --export > export.json

    --update_common_mail_providers => Update the Common Mail Provider data in the database.
                                      Fetches the latest data from a cPanel update mirror, and updates the database
                                      based on current Common Mail Provider configuration.

                                      You can specify '--force', in order to forceably update the IP data in the database.

The SQLite file is located at: ${ \Cpanel::GreyList::Config::get_sqlite_db() }.
USAGE
        my $providers_in_db           = Cpanel::GreyList::Client->new()->get_common_mail_providers();
        my $common_email_services_str = join( "\n", map { "\t\t\t$_" } ( sort keys %{$providers_in_db} ) );
        $msg =~ s/COMMON_EMAIL_SERVICES/$common_email_services_str/;
    }

    print {$fh} $msg;
    return $retval;
}

sub send_removal_notification {
    my ( $provider, $ips_ar ) = @_;

    require Cpanel::Notify;
    Cpanel::Notify::notification_class(
        'class'            => 'Greylist::CommonProviderRemoval',
        'application'      => 'Greylist::CommonProviderRemoval',
        'constructor_args' => [
            'origin'           => 'manage_greylisting',
            'provider_removed' => $provider,
            'provider_ips'     => $ips_ar,
        ]
    );

    return;
}

Youez - 2016 - github.com/yon3zu
LinuXploit