����JFIFXX�����    $.' ",#(7),01444'9=82<.342  2!!22222222222222222222222222222222222222222222222222����"��4�� ���,�PG"Z_�4�˷����kjز�Z�,F+��_z�,�© �����zh6�٨�ic�fu���#ډb���_�N�?��wQ���5-�~�I���8����TK<5o�Iv-�����k�_U_�����~b�M��d����Ӝ�U�Hh��?]��E�w��Q���k�{��_}qFW7HTՑ��Y��F�?_�'ϔ��_�Ջt��=||I ��6�έ"�����D���/[�k�9���Y�8ds|\���Ҿp6�Ҵ���]��.����6�z<�v��@]�i%��$j��~�g��J>��no����pM[me�i$[����s�o�ᘨ�˸ nɜG-�ĨU�ycP�3.DB�li�;��hj���x7Z^�N�h������N3u{�:j�x�힞��#M&��jL P@_���� P��&��o8������9�����@Sz6�t7#O�ߋ �s}Yf�T���lmr����Z)'N��k�۞p����w\�Tȯ?�8`�O��i{wﭹW�[�r�� ��Q4F�׊���3m&L�=��h3����z~��#�\�l :�F,j@�� ʱ�wQT����8�"kJO���6�֚l����}���R�>ډK���]��y����&����p�}b��;N�1�m�r$�|��7�>e�@B�TM*-iH��g�D�)� E�m�|�ؘbҗ�a��Ҿ����t4���o���G��*oCN�rP���Q��@z,|?W[0�����:�n,jWiE��W��$~/�hp\��?��{(�0���+�Y8rΟ�+����>S-S����VN;�}�s?.����� w�9��˟<���Mq4�Wv'��{)0�1mB��V����W[�����8�/<� �%���wT^�5���b��)iM� pg�N�&ݝ��VO~�q���u���9� ����!��J27����$O-���! �:�%H��� ـ����y�ΠM=t{!S�� oK8������t<����è:a������[�����ա�H���~��w��Qz`�po�^ ����Q��n� �,uu�C�$ ^���,������8�#��:�6��e�|~���!�3�3.�\0��q��o�4`.|� ����y�Q�`~;�d�ׯ,��O�Zw�������`73�v�܋�<���Ȏ�� ـ4k��5�K�a�u�=9Yd��$>x�A�&�� j0� ���vF��� Y�|�y��� ~�6�@c��1vOp�Ig����4��l�OD���L����� R���c���j�_�uX6��3?nk��Wy�f;^*B� ��@�~a�`��Eu������+���6�L��.ü>��}y���}_�O�6�͐�:�YrG�X��kG�����l^w���~㒶sy��Iu�!� W ��X��N�7BV��O��!X�2����wvG�R�f�T#�����t�/?���%8�^�W�aT��G�cL�M���I��(J����1~�8�?aT ���]����AS�E��(��*E}� 2��#I/�׍qz��^t�̔���b�Yz4x���t�){ OH��+(E��A&�N�������XT��o��"�XC��'���)}�J�z�p� ��~5�}�^����+�6����w��c��Q�|Lp�d�H��}�(�.|����k��c4^�"�����Z?ȕ ��a<�L�!039C� �Eu�C�F�Ew�ç ;�n?�*o���B�8�bʝ���'#Rqf���M}7����]����s2tcS{�\icTx;�\��7K���P���ʇ Z O-��~��c>"��?�������P��E��O�8��@�8��G��Q�g�a�Վ���󁶠�䧘��_%#r�>�1�z�a��eb��qcPѵ��n���#L��� =��׀t� L�7�`��V���A{�C:�g���e@�w1 Xp3�c3�ġ����p��M"'-�@n4���fG��B3�DJ�8[Jo�ߐ���gK)ƛ��$���� ���8�3�����+���� �����6�ʻ���� ���S�kI�*KZlT _`���?��K����QK�d����B`�s}�>���`��*�>��,*@J�d�oF*����弝��O}�k��s��]��y�ߘ��c1G�V���<=�7��7����6�q�PT��tXԀ�!9*4�4Tހ3XΛex�46���Y��D ����� �BdemDa����\�_l,��G�/���֌7���Y�](�xTt^%�GE�����4�}bT���ڹ�����;Y)���B�Q��u��>J/J �⮶.�XԄ��j�ݳ�+E��d ��r�5�_D�1 ��o�� �B�x�΢�#���<��W�����8���R6�@g�M�.��� dr�D��>(otU��@x=��~v���2� ӣ�d�oBd��3�eO�6�㣷�����ݜ6��6Y��Qz`��S��{���\P�~z m5{J/L��1������<�e�ͅPu�b�]�ϔ���'������f�b� Zpw��c`"��i���BD@:)ִ�:�]��hv�E�w���T�l��P���"Ju�}��وV J��G6��. J/�Qgl߭�e�����@�z�Zev2u�)]կ�����7x���s�M�-<ɯ�c��r�v�����@��$�ޮ}lk���a���'����>x��O\�ZFu>�����ck#��&:��`�$�ai�>2Δ����l���oF[h��lE�ܺ�Πk:)���`�� $[6�����9�����kOw�\|���8}������ބ:��񶐕��I�A1/�=�2[�,�!��.}gN#�u����b��� ~��݊��}34q����d�E��Lc��$��"�[q�U�硬g^��%B �z���r�pJ�ru%v\h1Y�ne`ǥ:g���pQM~�^�Xi� ��`S�:V29.�P���V�?B�k�� AEvw%�_�9C�Q����wKekPؠ�\�;Io d�{ ߞo�c1eP����\� `����E=���@K<�Y���eڼ�J���w����{av�F�'�M�@/J��+9p���|]�����Iw &`��8���&M�hg��[�{��Xj��%��Ӓ�$��(����ʹN���<>�I���RY���K2�NPlL�ɀ)��&e����B+ь����( � �JTx���_?EZ� }@ 6�U���뙢ط�z��dWI�n` D����噥�[��uV��"�G&Ú����2g�}&m��?ċ�"����Om#��������� ��{�ON��"S�X��Ne��ysQ���@Fn��Vg���dX�~nj�]J�<�K]:��FW��b�������62�=��5f����JKw��bf�X�55��~J �%^����:�-�QIE��P��v�nZum� z � ~ə ���� ���ة����;�f��\v���g�8�1��f24;�V���ǔ�)����9���1\��c��v�/'Ƞ�w�������$�4�R-��t���� e�6�/�ġ �̕Ecy�J���u�B���<�W�ַ~�w[B1L۲�-JS΂�{���΃������A��20�c#��@ 0!1@AP"#2Q`$3V�%45a6�FRUq��� ����^7ׅ,$n�������+��F�`��2X'��0vM��p�L=������5��8������u�p~���.�`r�����\���O��,ư�0oS ��_�M�����l���4�kv\JSd���x���SW�<��Ae�IX����������$I���w�:S���y���›R��9�Q[���,�5�;�@]�%���u�@ *ro�lbI �� ��+���%m:�͇ZV�����u�̉����θau<�fc�.����{�4Ա� �Q����*�Sm��8\ujqs]{kN���)qO�y�_*dJ�b�7���yQqI&9�ԌK!�M}�R�;������S�T���1���i[U�ɵz�]��U)V�S6���3$K{�ߊ<�(� E]Զ[ǼENg�����'�\?#)Dkf��J���o��v���'�%ƞ�&K�u�!��b�35LX�Ϸ��63$K�a�;�9>,R��W��3�3� d�JeTYE.Mϧ��-�o�j3+y��y^�c�������VO�9NV\nd�1 ��!͕_)a�v;����թ�M�lWR1��)El��P;��yوÏ�u 3�k�5Pr6<�⒲l�!˞*��u־�n�!�l:����UNW ��%��Chx8vL'��X�@��*��)���̮��ˍ��� ���D-M�+J�U�kvK����+�x8��cY������?�Ԡ��~3mo��|�u@[XeY�C�\Kp�x8�oC�C�&����N�~3-H���� ��MX�s�u<`���~"WL��$8ξ��3���a�)|:@�m�\���^�`�@ҷ)�5p+��6���p�%i)P M���ngc�����#0Aruz���RL+xSS?���ʮ}()#�t��mˇ!��0}}y����<�e� �-ή�Ԩ��X������ MF���ԙ~l L.3���}�V뽺�v�����멬��Nl�)�2����^�Iq��a��M��qG��T�����c3#������3U�Ǎ���}��לS�|qa��ڃ�+���-��2�f����/��bz��ڐ�� �ݼ[2�ç����k�X�2�* �Z�d���J�G����M*9W���s{��w���T��x��y,�in�O�v��]���n����P�$�JB@=4�OTI�n��e�22a\����q�d���%�$��(���:���: /*�K[PR�fr\nڙdN���F�n�$�4�[�� U�zƶ����� �mʋ���,�ao�u 3�z� �x��Kn����\[��VFmbE;�_U��&V�Gg�]L�۪&#n%�$ɯ�dG���D�TI=�%+AB�Ru#��b4�1�»x�cs�YzڙJG��f��Il��d�eF'T� iA��T���uC�$����Y��H?����[!G`}���ͪ� �纤Hv\������j�Ex�K���!���OiƸ�Yj�+u-<���'q����uN�*�r\��+�]���<�wOZ.fp�ێ��,-*)V?j-kÊ#�`�r��dV����(�ݽBk�����G�ƛk�QmUڗe��Z���f}|����8�8��a���i��3'J�����~G_�^���d�8w������ R�`(�~�.��u���l�s+g�bv���W���lGc}��u���afE~1�Ue������Z�0�8�=e�� f@/�jqEKQQ�J��oN��J���W5~M>$6�Lt�;$ʳ{���^��6�{����v6���ķܰg�V�cnn �~z�x�«�,2�u�?cE+Ș�H؎�%�Za�)���X>uW�Tz�Nyo����s���FQƤ��$��*�&�LLXL)�1�" L��eO��ɟ�9=���:t��Z���c��Ž���Y?�ӭV�wv�~,Y��r�ۗ�|�y��GaF�����C�����.�+� ���v1���fήJ�����]�S��T��B��n5sW}y�$��~z�'�c ��8 ��� ,! �p��VN�S��N�N�q��y8z˱�A��4��*��'������2n<�s���^ǧ˭P�Jޮɏ�U�G�L�J�*#��<�V��t7�8����TĜ>��i}K%,���)[��z�21z ?�N�i�n1?T�I�R#��m-�����������������1����lA�`��fT5+��ܐ�c�q՝��ʐ��,���3�f2U�եmab��#ŠdQ�y>\��)�SLY����w#��.���ʑ�f��� ,"+�w�~�N�'�c�O�3F�������N<���)j��&��,-� �љ���֊�_�zS���TǦ����w�>��?�������n��U仆�V���e�����0���$�C�d���rP �m�׈e�Xm�Vu� �L��.�bֹ��� �[Դaզ���*��\y�8�Է:�Ez\�0�Kq�C b��̘��cө���Q��=0Y��s�N��S.���3.���O�o:���#���v7�[#߫ ��5�܎�L���Er4���9n��COWlG�^��0k�%<���ZB���aB_���������'=��{i�v�l�$�uC���mƎҝ{�c㱼�y]���W�i ��ߧc��m�H� m�"�"�����;Y�ߝ�Z�Ǔ�����:S#��|}�y�,/k�Ld� TA�(�AI$+I3��;Y*���Z��}|��ӧO��d�v��..#:n��f>�>���ȶI�TX��� 8��y����"d�R�|�)0���=���n4��6ⲑ�+��r<�O�܂~zh�z����7ܓ�HH�Ga롏���nCo�>������a ���~]���R���̲c?�6(�q�;5%� |�uj�~z8R=X��I�V=�|{v�Gj\gc��q����z�؋%M�ߍ����1y��#��@f^���^�>N�����#x#۹��6�Y~�?�dfPO��{��P�4��V��u1E1J �*|���%���JN��`eWu�zk M6���q t[�� ��g�G���v��WIG��u_ft����5�j�"�Y�:T��ɐ���*�;� e5���4����q$C��2d�}���� _S�L#m�Yp��O�.�C�;��c����Hi#֩%+) �Ӎ��ƲV���SYź��g |���tj��3�8���r|���V��1#;.SQ�A[���S������#���`n�+���$��$I �P\[�@�s��(�ED�z���P��])8�G#��0B��[ى��X�II�q<��9�~[Z멜�Z�⊔IWU&A>�P~�#��dp<�?����7���c��'~���5 ��+$���lx@�M�dm��n<=e�dyX��?{�|Aef ,|n3�<~z�ƃ�uۧ�����P��Y,�ӥQ�*g�#먙R�\���;T��i,��[9Qi歉����c>]9�� ��"�c��P�� �Md?٥��If�ت�u��k��/����F��9�c*9��Ǎ:�ØF���z�n*�@|I�ށ9����N3{'��[�'ͬ�Ҳ4��#}��!�V� Fu��,�,mTIk���v C�7v���B�6k�T9��1�*l� '~��ƞF��lU��'�M ����][ΩũJ_�{�i�I�n��$���L�� j��O�dx�����kza۪��#�E��Cl����x˘�o�����V���ɞ�ljr��)�/,�߬h�L��#��^��L�ф�,íMƁe�̩�NB�L�����iL����q�}��(��q��6IçJ$�W�E$��:������=#����(�K�B����zђ <��K(�N�۫K�w��^O{!����)�H���>x�������lx�?>Պ�+�>�W���,Ly!_�D���Ō�l���Q�!�[ �S����J��1��Ɛ�Y}��b,+�Lo�x�ɓ)����=�y�oh�@�꥟/��I��ѭ=��P�y9��� �ۍYӘ�e+�p�Jnϱ?V\SO%�(�t� ���=?MR�[Ș�����d�/ ��n�l��B�7j� ��!�;ӥ�/�[-���A�>�dN�sLj ��,ɪv��=1c�.SQ�O3�U���ƀ�ܽ�E����������̻��9G�ϷD�7(�}��Ävӌ\�y�_0[w ���<΍>����a_��[0+�L��F.�޺��f�>oN�T����q;���y\��bՃ��y�jH�<|q-eɏ�_?_9+P���Hp$�����[ux�K w�Mw��N�ی'$Y2�=��q���KB��P��~������Yul:�[<����F1�2�O���5=d����]Y�sw:���Ϯ���E��j,_Q��X��z`H1,#II ��d�wr��P˂@�ZJV����y$�\y�{}��^~���[:N����ߌ�U�������O��d�����ؾe��${p>G��3c���Ė�lʌ�� ת��[��`ϱ�-W����dg�I��ig2��� ��}s ��ؤ(%#sS@���~���3�X�nRG�~\jc3�v��ӍL��M[JB�T��s3}��j�Nʖ��W����;7��ç?=X�F=-�=����q�ߚ���#���='�c��7���ڑW�I(O+=:uxq�������������e2�zi+�kuG�R��������0�&e�n���iT^J����~\jy���p'dtG��s����O��3����9* �b#Ɋ�� p������[Bws�T�>d4�ۧs���nv�n���U���_�~,�v����ƜJ1��s�� �QIz��)�(lv8M���U=�;����56��G���s#�K���MP�=��LvyGd��}�VwWBF�'�à �?MH�U�g2�� ����!�p�7Q��j��ڴ����=��j�u��� Jn�A s���uM������e��Ɔ�Ҕ�!)'��8Ϣ�ٔ��ޝ(��Vp���צ֖d=�IC�J�Ǡ{q������kԭ�߸���i��@K����u�|�p=..�*+����x�����z[Aqġ#s2a�Ɗ���RR�)*HRsi�~�a &f��M��P����-K�L@��Z��Xy�'x�{}��Zm+���:�)�) IJ�-i�u���� ���ܒH��'�L(7�y�GӜq���� j��� 6ߌg1�g�o���,kر���tY�?W,���p���e���f�OQS��!K�۟cҒA�|ս�j�>��=⬒��˧L[�� �߿2JaB~R��u�:��Q�] �0H~���]�7��Ƽ�I���(}��cq '�ήET���q�?f�ab���ӥvr� �)o��-Q��_'����ᴎo��K������;��V���o��%���~OK ����*��b�f:���-ťIR��`B�5!RB@���ï�� �u �̯e\�_U�_������� g�ES��3�������QT��a����x����U<~�c?�*�#]�MW,[8O�a�x��]�1bC|踤�P��lw5V%�)�{t�<��d��5���0i�XSU��m:��Z�┵�i�"��1�^B�-��P�hJ��&)O��*�D��c�W��vM��)����}���P��ܗ-q����\mmζZ-l@�}��a��E�6��F�@��&Sg@���ݚ�M����� ȹ 4����#p�\H����dYDo�H���"��\��..R�B�H�z_�/5˘����6��KhJR��P�mƶi�m���3�,#c�co��q�a)*Pt����R�m�k�7x�D�E�\Y�閣_X�<���~�)���c[[�BP����6�Yq���S��0����%_����;��Àv�~�| VS؇ ��'O0��F0��\���U�-�d@�����7�SJ*z��3n��y��P����O���������m�~�P�3|Y��ʉr#�C�<�G~�.,! ���bqx���h~0=��!ǫ�jy����l�O,�[B��~��|9��ٱ����Xly�#�i�B��g%�S��������tˋ���e���ې��\[d�t)��.+u�|1 ������#�~Oj����hS�%��i.�~X���I�H�m��0n���c�1uE�q��cF�RF�o���7� �O�ꮧ� ���ۛ{��ʛi5�rw?׌#Qn�TW��~?y$��m\�\o����%W� ?=>S�N@�� �Ʈ���R����N�)�r"C�:��:����� �����#��qb��Y�. �6[��2K����2u�Ǧ�HYR��Q�MV��� �G�$��Q+.>�����nNH��q�^��� ����q��mM��V��D�+�-�#*�U�̒ ���p욳��u:�������IB���m���PV@O���r[b= �� ��1U�E��_Nm�yKbN�O���U�}�the�`�|6֮P>�\2�P�V���I�D�i�P�O;�9�r�mAHG�W�S]��J*�_�G��+kP�2����Ka�Z���H�'K�x�W�MZ%�O�YD�Rc+o��?�q��Ghm��d�S�oh�\�D�|:W������UA�Qc yT�q������~^�H��/��#p�CZ���T�I�1�ӏT����4��"�ČZ�����}��`w�#�*,ʹ�� ��0�i��課�Om�*�da��^gJ݅{���l�e9uF#T�ֲ��̲�ٞC"�q���ߍ ոޑ�o#�XZTp����@ o�8��(jd��xw�]�,f���`~�|,s��^����f�1���t��|��m�򸄭/ctr��5s��7�9Q�4�H1꠲BB@l9@���C�����+�wp�xu�£Yc�9��?`@#�o�mH�s2��)�=��2�.�l����jg�9$�Y�S�%*L������R�Y������7Z���,*=�䷘$�������arm�o�ϰ���UW.|�r�uf����IGw�t����Zwo��~5 ��YյhO+=8fF�)�W�7�L9lM�̘·Y���֘YLf�큹�pRF���99.A �"wz��=E\Z���'a� 2��Ǚ�#;�'}�G���*��l��^"q��+2FQ� hj��kŦ��${���ޮ-�T�٭cf�|�3#~�RJ����t��$b�(R��(����r���dx� >U b�&9,>���%E\� Ά�e�$��'�q't��*�א���ެ�b��-|d���SB�O�O��$�R+�H�)�܎�K��1m`;�J�2�Y~9��O�g8=vqD`K[�F)k�[���1m޼c��n���]s�k�z$@��)!I �x՝"v��9=�ZA=`Ɠi �:�E��)`7��vI��}d�YI�_ �o�:ob���o ���3Q��&D&�2=�� �Ά��;>�h����y.*ⅥS������Ӭ�+q&����j|UƧ����}���J0��WW< ۋS�)jQR�j���Ư��rN)�Gű�4Ѷ(�S)Ǣ�8��i��W52���No˓� ۍ%�5brOn�L�;�n��\G����=�^U�dI���8$�&���h��'���+�(������cȁ߫k�l��S^���cƗjԌE�ꭔ��gF���Ȓ��@���}O���*;e�v�WV���YJ\�]X'5��ղ�k�F��b 6R�o՜m��i N�i����>J����?��lPm�U��}>_Z&�KK��q�r��I�D�Չ~�q�3fL�:S�e>���E���-G���{L�6p�e,8��������QI��h��a�Xa��U�A'���ʂ���s�+טIjP�-��y�8ۈZ?J$��W�P� ��R�s�]��|�l(�ԓ��sƊi��o(��S0��Y� 8�T97.�����WiL��c�~�dxc�E|�2!�X�K�Ƙਫ਼�$((�6�~|d9u+�qd�^3�89��Y�6L�.I�����?���iI�q���9�)O/뚅����O���X��X�V��ZF[�یgQ�L��K1���RҖr@v�#��X�l��F���Нy�S�8�7�kF!A��sM���^rkp�jP�DyS$N���q��nxҍ!U�f�!eh�i�2�m���`�Y�I�9r�6� �TF���C}/�y�^���Η���5d�'��9A-��J��>{�_l+�`��A���[�'��յ�ϛ#w:݅�%��X�}�&�PSt�Q�"�-��\縵�/����$Ɨh�Xb�*�y��BS����;W�ջ_mc�����vt?2}1�;qS�d�d~u:2k5�2�R�~�z+|HE!)�Ǟl��7`��0�<�,�2*���Hl-��x�^����'_TV�gZA�'j� ^�2Ϊ��N7t�����?w�� �x1��f��Iz�C-Ȗ��K�^q�;���-W�DvT�7��8�Z�������� hK�(P:��Q- �8�n�Z���܃e貾�<�1�YT<�,�����"�6{/ �?�͟��|1�:�#g��W�>$����d��J��d�B��=��jf[��%rE^��il:��B���x���Sּ�1հ��,�=��*�7 fcG��#q� �eh?��2�7�����,�!7x��6�n�LC�4x��},Geǝ�tC.��vS �F�43��zz\��;QYC,6����~;RYS/6���|2���5���v��T��i����������mlv��������&� �nRh^ejR�LG�f���? �ۉҬܦƩ��|��Ȱ����>3����!v��i�ʯ�>�v��オ�X3e���_1z�Kȗ\<������!�8���V��]��?b�k41�Re��T�q��mz��TiOʦ�Z��Xq���L������q"+���2ۨ��8}�&N7XU7Ap�d�X��~�׿��&4e�o�F��� �H����O���č�c�� 懴�6���͉��+)��v;j��ݷ�� �UV�� i��� j���Y9GdÒJ1��詞�����V?h��l����l�cGs�ځ�������y�Ac�����\V3�? �� ܙg�>qH�S,�E�W�[�㺨�uch�⍸�O�}���a��>�q�6�n6����N6�q������N ! 1AQaq�0@����"2BRb�#Pr���3C`��Scst���$4D���%Td�� ?���N����a��3��m���C���w��������xA�m�q�m���m������$����4n淿t'��C"w��zU=D�\R+w�p+Y�T�&�պ@��ƃ��3ޯ?�Aﶂ��aŘ���@-�����Q�=���9D��ռ�ѻ@��M�V��P��܅�G5�f�Y<�u=,EC)�<�Fy'�"�&�չ�X~f��l�KԆV��?�� �W�N����=(� �;���{�r����ٌ�Y���h{�١������jW����P���Tc�����X�K�r��}���w�R��%��?���E��m�� �Y�q|����\lEE4���r���}�lsI�Y������f�$�=�d�yO����p�����yBj8jU�o�/�S��?�U��*������ˍ�0������u�q�m [�?f����a�� )Q�>����6#������� ?����0UQ����,IX���(6ڵ[�DI�MNލ�c&���υ�j\��X�R|,4��� j������T�hA�e��^���d���b<����n�� �즇�=!���3�^�`j�h�ȓr��jẕ�c�,ٞX����-����a�ﶔ���#�$��]w�O��Ӫ�1y%��L�Y<�wg#�ǝ�̗`�x�xa�t�w��»1���o7o5��>�m뭛C���Uƃߜ}�C���y1Xνm�F8�jI���]����H���ۺиE@I�i;r�8ӭ����V�F�Շ| ��&?�3|x�B�MuS�Ge�=Ӕ�#BE5G�����Y!z��_e��q�р/W>|-�Ci߇�t�1ޯќd�R3�u��g�=0 5��[?�#͏��q�cf���H��{ ?u�=?�?ǯ���}Z��z���hmΔ�BFTW�����<�q�(v� ��!��z���iW]*�J�V�z��gX֧A�q�&��/w���u�gYӘa���; �i=����g:��?2�dž6�ى�k�4�>�Pxs����}������G�9��3 ���)gG�R<>r h�$��'nc�h�P��Bj��J�ҧH� -��N1���N��?��~��}-q!=��_2hc�M��l�vY%UE�@|�v����M2�.Y[|y�"Eï��K�ZF,�ɯ?,q�?v�M 80jx�"�;�9vk�����+ ֧�� �ȺU��?�%�vcV��mA�6��Qg^M����A}�3�nl� QRN�l8�kkn�'�����(��M�7m9و�q���%ޟ���*h$Zk"��$�9��: �?U8�Sl��,,|ɒ��xH(ѷ����Gn�/Q�4�P��G�%��Ա8�N��!� �&�7�;���eKM7�4��9R/%����l�c>�x;������>��C�:�����t��h?aKX�bhe�ᜋ^�$�Iհ �hr7%F$�E��Fd���t��5���+�(M6�t����Ü�UU|zW�=a�Ts�Tg������dqP�Q����b'�m���1{|Y����X�N��b �P~��F^F:����k6�"�j!�� �I�r�`��1&�-$�Bevk:y���#yw��I0��x��=D�4��tU���P�ZH��ڠ底taP��6����b>�xa����Q�#� WeF��ŮNj�p�J* mQ�N����*I�-*�ȩ�F�g�3 �5��V�ʊ�ɮ�a��5F���O@{���NX��?����H�]3��1�Ri_u��������ѕ�� ����0��� F��~��:60�p�͈�S��qX#a�5>���`�o&+�<2�D����: �������ڝ�$�nP���*)�N�|y�Ej�F�5ټ�e���ihy�Z �>���k�bH�a�v��h�-#���!�Po=@k̆IEN��@��}Ll?j�O������߭�ʞ���Q|A07x���wt!xf���I2?Z��<ץ�T���cU�j��]��陎Ltl �}5�ϓ��$�,��O�mˊ�;�@O��jE��j(�ا,��LX���LO���Ц�90�O �.����a��nA���7������j4 ��W��_ٓ���zW�jcB������y՗+EM�)d���N�g6�y1_x��p�$Lv:��9�"z��p���ʙ$��^��JԼ*�ϭ����o���=x�Lj�6�J��u82�A�H�3$�ٕ@�=Vv�]�'�qEz�;I˼��)��=��ɯ���x �/�W(V���p�����$ �m�������u�����񶤑Oqˎ�T����r��㠚x�sr�GC��byp�G��1ߠ�w e�8�$⿄����/�M{*}��W�]˷.�CK\�ުx���/$�WPw���r� |i���&�}�{�X� �>��$-��l���?-z���g����lΆ���(F���h�vS*���b���߲ڡn,|)mrH[���a�3�ר�[1��3o_�U�3�TC�$��(�=�)0�kgP���� ��u�^=��4 �WYCҸ:��vQ�ר�X�à��tk�m,�t*��^�,�}D*� �"(�I��9R����>`�`��[~Q]�#af��i6l��8���6�:,s�s�N6�j"�A4���IuQ��6E,�GnH��zS�HO�uk�5$�I�4��ؤ�Q9�@��C����wp�BGv[]�u�Ov���0I4���\��y�����Q�Ѹ��~>Z��8�T��a��q�ޣ;z��a���/��S��I:�ܫ_�|������>=Z����8:�S��U�I�J��"IY���8%b8���H��:�QO�6�;7�I�S��J��ҌAά3��>c���E+&jf$eC+�z�;��V����� �r���ʺ������my�e���aQ�f&��6�ND��.:��NT�vm�<- u���ǝ\MvZY�N�NT��-A�>jr!S��n�O 1�3�Ns�%�3D@���`������ܟ 1�^c<���� �a�ɽ�̲�Xë#�w�|y�cW�=�9I*H8�p�^(4���՗�k��arOcW�tO�\�ƍR��8����'�K���I�Q�����?5�>[�}��yU�ײ -h��=��% q�ThG�2�)���"ו3]�!kB��*p�FDl�A���,�eEi�H�f�Ps�����5�H:�Փ~�H�0Dت�D�I����h�F3�������c��2���E��9�H��5�zԑ�ʚ�i�X�=:m�xg�hd(�v����׊�9iS��O��d@0ڽ���:�p�5�h-��t�&���X�q�ӕ,��ie�|���7A�2���O%P��E��htj��Y1��w�Ѓ!����  ���� ࢽ��My�7�\�a�@�ţ�J �4�Ȼ�F�@o�̒?4�wx��)��]�P��~�����u�����5�����7X ��9��^ܩ�U;Iꭆ 5 �������eK2�7(�{|��Y׎ �V��\"���Z�1� Z�����}��(�Ǝ"�1S���_�vE30>���p;� ΝD��%x�W�?W?v����o�^V�i�d��r[��/&>�~`�9Wh��y�;���R��� ;;ɮT��?����r$�g1�K����A��C��c��K��l:�'��3 c�ﳯ*"t8�~l��)���m��+U,z��`(�>yJ�?����h>��]��v��ЍG*�{`��;y]��I�T� ;c��NU�fo¾h���/$���|NS���1�S�"�H��V���T���4��uhǜ�]�v;���5�͠x��'C\�SBpl���h}�N����� A�Bx���%��ޭ�l��/����T��w�ʽ]D�=����K���ž�r㻠l4�S�O?=�k �M:� ��c�C�a�#ha���)�ѐxc�s���gP�iG��{+���x���Q���I= �� z��ԫ+ �8"�k�ñ�j=|����c ��y��CF��/��*9ж�h{ �?4�o� ��k�m�Q�N�x��;�Y��4膚�a�w?�6�>e]�����Q�r�:����g�,i"�����ԩA�*M�<�G��b�if��l^M��5� �Ҩ�{����6J��ZJ�����P�*�����Y���ݛu�_4�9�I8�7���������,^ToR���m4�H��?�N�S�ѕw��/S��甍�@�9H�S�T��t�ƻ���ʒU��*{Xs�@����f�����֒Li�K{H�w^���������Ϥm�tq���s� ���ք��f:��o~s��g�r��ט� �S�ѱC�e]�x���a��) ���(b-$(�j>�7q�B?ӕ�F��hV25r[7 Y� }L�R��}����*sg+��x�r�2�U=�*'WS��ZDW]�WǞ�<��叓���{�$�9Ou4��y�90-�1�'*D`�c�^o?(�9��u���ݐ��'PI&� f�Jݮ�������:wS����jfP1F:X �H�9dԯ���˝[�_54 �}*;@�ܨ�� ð�yn�T���?�ןd�#���4rG�ͨ��H�1�|-#���Mr�S3��G�3�����)�.᧏3v�z֑��r����$G"�`j �1t��x0<Ɔ�Wh6�y�6��,œ�Ga��gA����y��b��)��h�D��ß�_�m��ü �gG;��e�v��ݝ�nQ� ��C����-�*��o���y�a��M��I�>�<���]obD��"�:���G�A��-\%LT�8���c�)��+y76���o�Q�#*{�(F�⽕�y����=���rW�\p���۩�c���A���^e6��K������ʐ�cVf5$�'->���ՉN"���F�"�UQ@�f��Gb~��#�&�M=��8�ט�JNu9��D��[̤�s�o�~������ G��9T�tW^g5y$b��Y'��س�Ǵ�=��U-2 #�MC�t(�i� �lj�@Q 5�̣i�*�O����s�x�K�f��}\��M{E�V�{�υ��Ƈ�����);�H����I��fe�Lȣr�2��>��W�I�Ȃ6������i��k�� �5�YOxȺ����>��Y�f5'��|��H+��98pj�n�.O�y�������jY��~��i�w'������l�;�s�2��Y��:'lg�ꥴ)o#'Sa�a�K��Z� �m��}�`169�n���"���x��I ��*+� }F<��cГ���F�P�������ֹ*�PqX�x۩��,� ��N�� �4<-����%����:��7����W���u�`����� $�?�I��&����o��o��`v�>��P��"��l���4��5'�Z�gE���8���?��[�X�7(��.Q�-��*���ތL@̲����v��.5���[��=�t\+�CNܛ��,g�SQnH����}*F�G16���&:�t��4ُ"A��̣��$�b �|����#rs��a�����T�� ]�<�j��BS�('$�ɻ� �wP;�/�n��?�ݜ��x�F��yUn�~mL*-�������Xf�wd^�a�}��f�,=t�׵i�.2/wpN�Ep8�OР���•��R�FJ� 55TZ��T �ɭ�<��]��/�0�r�@�f��V��V����Nz�G��^���7hZi����k��3�,kN�e|�vg�1{9]_i��X5y7� 8e]�U����'�-2,���e"����]ot�I��Y_��n�(JҼ��1�O ]bXc���Nu�No��pS���Q_���_�?i�~�x h5d'�(qw52] ��'ޤ�q��o1�R!���`ywy�A4u���h<קy���\[~�4�\ X�Wt/� 6�����n�F�a8��f���z �3$�t(���q��q�x��^�XWeN'p<-v�!�{�(>ӽDP7��ո0�y)�e$ٕv�Ih'Q�EA�m*�H��RI��=:��� ���4牢) �%_iN�ݧ�l]� �Nt���G��H�L��� ɱ�g<���1V�,�J~�ٹ�"K��Q�� 9�HS�9�?@��k����r�;we݁�]I�!{ �@�G�[�"��`���J:�n]�{�cA�E����V��ʆ���#��U9�6����j�#Y�m\��q�e4h�B�7��C�������d<�?J����1g:ٳ���=Y���D�p�ц� ׈ǔ��1�]26؜oS�'��9�V�FVu�P�h�9�xc�oq�X��p�o�5��Ա5$�9W�V(�[Ak�aY錎qf;�'�[�|���b�6�Ck��)��#a#a˙��8���=äh�4��2��C��4tm^ �n'c���]GQ$[Wҿ��i���vN�{Fu ��1�gx��1┷���N�m��{j-,��x�� Ūm�ЧS�[�s���Gna���䑴�� x�p 8<������97�Q���ϴ�v�aϚG��Rt�Һ׈�f^\r��WH�JU�7Z���y)�vg=����n��4�_)y��D'y�6�]�c�5̪�\� �PF�k����&�c;��cq�$~T�7j ���nç]�<�g ":�to�t}�159�<�/�8������m�b�K#g'I'.W�����6��I/��>v��\�MN��g���m�A�yQL�4u�Lj�j9��#44�t��l^�}L����n��R��!��t��±]��r��h6ٍ>�yҏ�N��fU�� ���� Fm@�8}�/u��jb9������he:A�y�ծw��GpΧh�5����l}�3p468��)U��d��c����;Us/�֔�YX�1�O2��uq�s��`hwg�r~�{ R��mhN��؎*q 42�*th��>�#���E����#��Hv�O����q�}�����6�e��\�,Wk�#���X��b>��p}�դ��3���T5��†��6��[��@�P�y*n��|'f�֧>�lư΂�̺����SU�'*�q�p�_S�����M�� '��c�6�����m�� ySʨ;M��r���Ƌ�m�Kxo,���Gm�P��A�G�:��i��w�9�}M(�^�V��$ǒ�ѽ�9���|���� �a����J�SQ�a���r�B;����}���ٻ֢�2�%U���c�#�g���N�a�ݕ�'�v�[�OY'��3L�3�;,p�]@�S��{ls��X�'���c�jw�k'a�.��}�}&�� �dP�*�bK=ɍ!����;3n�gΊU�ߴmt�'*{,=SzfD� A��ko~�G�aoq�_mi}#�m�������P�Xhύ����mxǍ�΂���巿zf��Q���c���|kc�����?���W��Y�$���_Lv����l߶��c���`?����l�j�ݲˏ!V��6����U�Ђ(A���4y)H���p�Z_�x��>���e��R��$�/�`^'3qˏ�-&Q�=?��CFVR �D�fV�9��{�8g�������n�h�(P"��6�[�D���< E�����~0<@�`�G�6����Hг�cc�� �c�K.5��D��d�B���`?�XQ��2��ٿyqo&+�1^� DW�0�ꊩ���G�#��Q�nL3��c���������/��x ��1�1[y�x�პCW��C�c�UĨ80�m�e�4.{�m��u���I=��f�����0QRls9���f���������9���~f�����Ǩ��a�"@�8���ȁ�Q����#c�ic������G��$���G���r/$W�(��W���V�"��m�7�[m�A�m����bo��D� j����۳� l���^�k�h׽����� ��#� iXn�v��eT�k�a�^Y�4�BN��ĕ��0 !01@Q"2AaPq3BR������?���@4�Q�����T3,���㺠�W�[=JK�Ϟ���2�r^7��vc�:�9 �E�ߴ�w�S#d���Ix��u��:��Hp��9E!�� V 2;73|F��9Y���*ʬ�F��D����u&���y؟��^EA��A��(ɩ���^��GV:ݜDy�`��Jr29ܾ�㝉��[���E;Fzx��YG��U�e�Y�C���� ����v-tx����I�sם�Ę�q��Eb�+P\ :>�i�C'�;�����k|z�رn�y]�#ǿb��Q��������w�����(�r|ӹs��[�D��2v-%��@;�8<a���[\o[ϧw��I!��*0�krs)�[�J9^��ʜ��p1)� "��/_>��o��<1����A�E�y^�C��`�x1'ܣn�p��s`l���fQ��):�l����b>�Me�jH^?�kl3(�z:���1ŠK&?Q�~�{�ٺ�h�y���/�[��V�|6��}�KbX����mn[-��7�5q�94�������dm���c^���h� X��5��<�eޘ>G���-�}�دB�ޟ� ��|�rt�M��V+�]�c?�-#ڛ��^ǂ}���Lkr���O��u�>�-D�ry� D?:ޞ�U��ǜ�7�V��?瓮�"�#���r��չģVR;�n���/_� ؉v�ݶe5d�b9��/O��009�G���5n�W����JpA�*�r9�>�1��.[t���s�F���nQ� V 77R�]�ɫ8����_0<՜�IF�u(v��4��F�k�3��E)��N:��yڮe��P�`�1}�$WS��J�SQ�N�j�ٺ��޵�#l���ј(�5=��5�lǏmoW�v-�1����v,W�mn��߀$x�<����v�j(����c]��@#��1������Ǔ���o'��u+����;G�#�޸��v-lη��/(`i⣍Pm^���ԯ̾9Z��F��������n��1��� ��]�[��)�'������:�֪�W��FC����� �B9،!?���]��V��A�Վ�M��b�w��G F>_DȬ0¤�#�QR�[V��kz���m�w�"��9ZG�7'[��=�Q����j8R?�zf�\a�=��O�U����*oB�A�|G���2�54 �p��.w7� �� ��&������ξxGHp� B%��$g�����t�Џ򤵍z���HN�u�Я�-�'4��0��;_��3 !01"@AQa2Pq#3BR������?��ʩca��en��^��8���<�u#��m*08r��y�N"�<�Ѳ0��@\�p��� �����Kv�D��J8�Fҽ� �f�Y��-m�ybX�NP����}�!*8t(�OqѢ��Q�wW�K��ZD��Δ^e��!� ��B�K��p~�����e*l}z#9ң�k���q#�Ft�o��S�R����-�w�!�S���Ӥß|M�l޶V��!eˈ�8Y���c�ЮM2��tk���� ������J�fS����Ö*i/2�����n]�k�\���|4yX�8��U�P.���Ы[���l��@"�t�<������5�lF���vU�����W��W��;�b�cД^6[#7@vU�xgZv��F�6��Q,K�v��� �+Ъ��n��Ǣ��Ft���8��0��c�@�!�Zq s�v�t�;#](B��-�nῃ~���3g������5�J�%���O������n�kB�ĺ�.r��+���#�N$?�q�/�s�6��p��a����a��J/��M�8��6�ܰ"�*������ɗud"\w���aT(����[��F��U՛����RT�b���n�*��6���O��SJ�.�ij<�v�MT��R\c��5l�sZB>F��<7�;EA��{��E���Ö��1U/�#��d1�a�n.1ě����0�ʾR�h��|�R��Ao�3�m3 ��%�� ���28Q� ��y��φ���H�To�7�lW>����#i`�q���c����a��� �m,B�-j����݋�'mR1Ήt�>��V��p���s�0IbI�C.���1R�ea�����]H�6����������4B>��o��](��$B���m�����a�!=��?�B� K�Ǿ+�Ծ"�n���K��*��+��[T#�{E�J�S����Q�����s�5�:�U�\wĐ�f�3����܆&�)����I���Ԇw��E T�lrTf6Q|R�h:��[K�� �z��c֧�G�C��%\��_�a�84��HcO�bi��ؖV��7H �)*ģK~Xhչ0��4?�0��� �E<���}3���#���u�?�� ��|g�S�6ꊤ�|�I#Hڛ� �ա��w�X��9��7���Ŀ%�SL��y6č��|�F�a 8���b��$�sק�h���b9RAu7�˨p�Č�_\*w��묦��F ����4D~�f����|(�"m���NK��i�S�>�$d7SlA��/�²����SL��|6N�}���S�˯���g��]6��; �#�.��<���q'Q�1|KQ$�����񛩶"�$r�b:���N8�w@��8$�� �AjfG|~�9F ���Y��ʺ��Bwؒ������M:I岎�G��`s�YV5����6��A �b:�W���G�q%l�����F��H���7�������Fsv7��k�� 403WebShell
403Webshell
Server IP : 51.161.54.47  /  Your IP : 216.73.216.98
Web Server : Apache/2.4.68 (Unix) OpenSSL/1.1.1k
System : Linux host.ditinformatica.ar 4.18.0-553.153.1.el8_10.x86_64 #1 SMP Thu Aug 6 00:53:12 EDT 2026 x86_64
User : kalaycom ( 1021)
PHP Version : 7.4.33
Disable Function : NONE
MySQL : OFF  |  cURL : ON  |  WGET : ON  |  Perl : ON  |  Python : OFF  |  Sudo : ON  |  Pkexec : ON
Directory :  /usr/lib64/perl5/vendor_perl/Razor2/Client/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Command :


[ Back ]     

Current File : /usr/lib64/perl5/vendor_perl/Razor2/Client/Core.pm
#!/usr/bin/perl -sw
##
## Razor2::Client::Core - Vipul's Razor Client API
##
## Copyright (c) 2001, Vipul Ved Prakash.  All rights reserved.
## This code is free software; you can redistribute it and/or modify
## it under the same terms as Perl itself.
##
## $Id: Core.pm,v 1.92 2006/05/27 00:00:53 rsoderberg Exp $

package Razor2::Client::Core;

use strict;
use IO::Socket;
use IO::Select;
use Errno qw(:POSIX);

use Razor2::Client::Version;
use Data::Dumper;
use base qw(Razor2::String);
use base qw(Razor2::Logger);
use base qw(Razor2::Client::Engine);
use base qw(Razor2::Errorhandler);
use Razor2::Client::Version;
use Razor2::String qw(hextobase64 makesis parsesis hmac_sha1 xor_key
  prep_mail debugobj to_batched_query
  from_batched_query hexbits2hash
  fisher_yates_shuffle);

our ($VERSION) = do { my @r = ( q$Revision: 1.92 $ =~ /\d+/g ); sprintf "%d." . "%02d" x $#r, @r };
our $PROTOCOL = $Razor2::Client::Version::PROTOCOL;

sub new {
    my ( $class, $conf, %params ) = @_;
    my $self = {};
    bless $self, $class;
    $self->debug("Razor Agents $VERSION, protocol version $PROTOCOL.");
    return $self;
}

#
# We store server-specific config info for each server we know about.
# All info about razor servers is stored in $self->{s}.
#
# Basically we get the server name/ip from {list},
# load that server's specific info from {allconfs} into {conf},
# and do stuff.  If server is no good, we get nextserver from {list}
#
# $self->{s}->{list}      ptr to {nomination} if report,revoke; or {catalogue} if check
#                         or the cmd-line server (-rs server)
# $self->{s}->{new_list}  set to 1 when discover gets new lists
# $self->{s}->{catalogue} array ref containing catalogue servers
# $self->{s}->{nomination}array ref containing nomination servers
# $self->{s}->{discovery} array ref containing discovery servers
#
# $self->{s}->{modified}  array ref containing servers whose .conf needs updating
# $self->{s}->{modified_lst} array ref containing which .lst files need updating
#
# $self->{s}->{ip}        string containing ip (or dns name) of current server from {list})
# $self->{s}->{port}      string containing port, taken from server:port from {list}
# $self->{s}->{engines}   engines supported, derived from {conf}->{se}
# $self->{s}->{conf}      hash ref containing current server's config params
#                         read from $razorhome/server.$ip.conf
#
# $self->{s}->{allconfs}  hash ref of all servers' configs.  key={ip}, val={conf}
#                         as read from server.*.conf file
#
# $self->{s}->{listfile}  string containing path/file of server.lst, either
#                         nomination or catalogue depending $self->{breed}
# $self->{conf}->{listfile_discovery}  string containing path/file of discovery server
#
# NOTE: if we are razor-check, server is Catalogue Server
#       otherwise server is Nomination server.
#
# everytime we update our server list, $self->{s}->{list};
# we want to write that to disk - $self->{s}->{listfile}
#
sub nextserver {
    my ($self) = @_;
    $self->log( 16, "entered nextserver" );

    # see if we need to discover (.lst files might be too old)
    $self->discover() or return $self->errprefix("nextserver");

    # first time we don't remove from list
    # or if we've rediscovered.
    shift @{ $self->{s}->{list} } unless ( $self->{s}->{new_list} || !$self->{s}->{ip} );
    $self->{s}->{new_list} = 0;

    my $next = ${ $self->{s}->{list} }[0];

    # do we ever want to put current back on the end of list?
    # push @{$self->{s}->{list}}, $self->{s}->{ip};

    if ($next) {
        ( $self->{s}->{port} ) = $next =~ /:(.*)$/;
        $next =~ s/:.*$//;    # optional
        $self->{s}->{ip} = $next;    # ip can be IP or DNS name
        $self->{s}->{port} ||= $self->{conf}->{port} || 2703;
        $self->{s}->{conf} = $self->{s}->{allconfs}->{$next};

        my $svrport = "$self->{s}->{ip}:$self->{s}->{port}";

        # get rid of server specific stuff
        delete $self->{s}->{greeting};

        unless ( ref( $self->{s}->{conf} ) ) {

            # never used this server before, no cached info. go get it!
            $self->{s}->{conf} = {};
            $self->connect;    # calls parse_greeting which calls compute_server_conf
        }
        else {
            $self->compute_server_conf(1);    # computes supported engines, logs info
        }
        $self->writeservers();

        my $srl = defined( $self->{s}->{conf}->{srl} ) ? $self->{s}->{conf}->{srl} : "<unknown>";
        $self->log( 8, "Using next closest server $svrport, cached info srl $srl" );

        #$self->logobj(11, "Using next closest server $svrport, cached info", $self->{s}->{conf});
        return 1;

    }
    else {
        return $self->error("Razor server $self->{opt}->{server} not available at this time")
          if $self->{opt}->{server};
        $self->{force_discovery} = 1;
        if ( $self->{done_discovery} && !( $self->discover ) ) {
            return $self->errprefix("No Razor servers available at this time");
        }
        return $self->nextserver;
    }
}

sub load_at_runtime {
    my ( $self, $class, $sub, $args ) = @_;

    $sub  = 'new' unless defined $sub;
    $args = ""    unless defined $args;

    eval "use $class";
    if ($@) {
        $self->log( 2, "$class not found, please to fix." );
        return $self->error("\n\n$@");
    }
    my $evalstr;
    if ( $sub && $sub ne "new" ) {
        $evalstr = $class . "::$sub($args);";
    }
    else {
        $evalstr = $class . "->new($args)";
    }
    if ( my $dude = eval $evalstr ) {
        $self->log( 12, "Found and evaled $evalstr ==> $dude" );
        return $dude;
    }
    else {
        $self->log( 5, "Found but problem (bad args?) with $evalstr" );
        return $self->error("Problem with $evalstr");
    }
}

#
# uses DNS to find Discovery servers
# puts discovery servers in $self->{s}->{discovery}
#
sub bootstrap_discovery {
    my ($self) = @_;
    $self->log( 16, "entered bootstrap_discovery" );

    if ( $self->{conf}->{server} ) {
        $self->log( 8, "no bootstap_discovery when cmd-line server specified" );
        return 1;
    }
    unless ( $self->{force_bootstrap_discovery} ) {
        if ( ref( $self->{s}->{discovery} ) && scalar( @{ $self->{s}->{discovery} } ) ) {
            $self->log( 8, "already have " . scalar( @{ $self->{s}->{discovery} } ) . " discovery servers" );
            return 1;
        }
        elsif ( $self->{done_bootstrap} ) {

            # if we've done it before {s}->{discovery} should be set
            $self->log( 8, "already have done bootstrap_discovery" );
            return 1;
        }
    }
    unless ( defined $self->{conf}->{listfile_discovery} ) {
        $self->log( 6, "discovery listfile not defined!" );
    }
    elsif ( -s $self->{conf}->{listfile_discovery} ) {
        my $wait      = $self->{conf}->{rediscovery_wait_dns} || 604800;                                      # 604800 secs == 7 days
        my $randomize = int( rand( $wait / 7 ) );
        my $timeleft  = ( ( stat( $self->{conf}->{listfile_discovery} ) )[9] + $wait - $randomize ) - time;
        if ( $timeleft > 0 ) {
            $self->log( 7, "$timeleft seconds before soonest DNS discovery" );
            return 1 unless $self->{force_bootstrap_discovery};
            $self->log( 5, "forcing DNS discovery" );
        }
        else {
            $self->log( 5, "DNS discovery overdue by " . ( 0 - $timeleft ) . " seconds" );
        }
    }
    else {
        if ( -e $self->{conf}->{listfile_discovery} ) {
            $self->log( 6, "empty discovery listfile: $self->{conf}->{listfile_discovery}" );
        }
        else {
            $self->log( 6, "no discovery listfile: $self->{conf}->{listfile_discovery}" );
        }
    }

    $self->{s}->{discovery} = [ $self->{conf}->{razordiscovery} ];
    push @{ $self->{s}->{modified_lst} }, "discovery";
    return 1;
}

#
# uses Discovery Servers to find closest Nomination/Catalogue Servers.
# called every day or so of if .lst file is empty
#
# puts servers in $self->{s}->{list}
#
sub discover {
    my ($self) = @_;
    $self->log( 16, "entered discover" );

    #
    # do we need to discover?
    #

    # no discover if cmd-line server
    return 1 if $self->{opt}->{server};

    #
    # don't discover if conf says turn_off_discovery (unless force_discovery)
    #
    return 1 if $self->{conf}->{turn_off_discovery} && ( !( $self->{force_discovery} ) );
    return $self->error("No Razor servers available at this time")
      if $self->{done_discovery};

    # so if user has their own servers, and they are temporarily down, force_discovery.
    # good: shit will work
    #  bad: it will erase their custom server*.lst file
    #
    unless ( defined $self->{s}->{listfile} ) {
        $self->debug("listfile not defined!");
    }
    elsif ( -s $self->{s}->{listfile} ) {
        my $randomize = int( rand( $self->{conf}->{rediscovery_wait} / 7 ) );
        my $timeleft = ( ( stat( $self->{s}->{listfile} ) )[9] + $self->{conf}->{rediscovery_wait} - $randomize ) - time;
        if ( $timeleft > 0 ) {
            $self->debug("$timeleft seconds before closest server discovery");
            return 1 unless $self->{force_discovery};
            $self->debug("forcing discovery");
        }
        else {
            $self->debug( "server discovery overdue by " . ( 0 - $timeleft ) . " seconds" );
        }
    }
    else {
        if ( -e $self->{s}->{listfile} ) {
            $self->debug("empty listfile: $self->{s}->{listfile}");
        }
        else {
            $self->debug("no listfile: $self->{s}->{listfile}");
        }
    }

    #
    # we need to discover.
    #

    return $self->errprefix("discover0") unless $self->bootstrap_discovery();

    #
    # Go ahead and do discovery for both csl and nsl.
    #
    my %stype = ( csl => 'catalogue', nsl => 'nomination' );

    my $srvs = { csl => {}, nsl => {} };

    my $list_orig = $self->{s}->{list};
    $self->{s}->{list} = $self->{s}->{discovery};

    foreach ( @{ $self->{s}->{discovery} } ) {
        unless ( defined $_ ) {
            $self->log( 5, "Razor Discovery Server not defined!" );
            next;
        }

        $self->log( 8, "Checking with Razor Discovery Server $_" );

        unless ( $self->connect( server => $_, discovery_server => 1 ) ) {
            $self->log( 5, "Razor Discovery Server $_ is unreachable" );
            next;
        }

        foreach my $querytype (qw(csl nsl)) {
            my $query = "a=g&pm=$querytype\r\n";
            my $resp = $self->_send( [$query] );

            unless ($resp) {
                $self->{s}->{list} = $list_orig;
                return $self->errprefix("discover1");
            }

            # from_batched_query wants "-" in beginning, but not ".\r\n" at end
            $resp->[0] =~ s/\.\r\n$//sg;
            my $h = from_batched_query( $resp->[0], {} );

            foreach my $href (@$h) {
                next unless $href->{$querytype};
                $self->log( 8, "Discovery Server $_ replying with $querytype=$href->{$querytype}" );
                $srvs->{$querytype}->{ $href->{$querytype} } = 1;
            }
            unless ( keys %{ $srvs->{$querytype} } ) {
                $self->log( 5, "Razor Discovery Server $_ had no valid $querytype servers" );
                next;
            }
        }
    }

    $self->{s}->{list} = $list_orig;

    foreach my $querytype (qw(csl nsl)) {

        my @list = keys %{ $srvs->{$querytype} };

        #return $self->error("Could not get valid info from Discovery Servers")
        #    unless @list;
        unless (@list) {
            if ( $self->{force_bootstrap_discovery} ) {
                return $self->error("Bootstrap discovery failed. Giving up.");
            }
            $self->log( 5, "Couldn't talk to discovery servers.  Will force a bootstrap..." );
            $self->{force_bootstrap_discovery} = 1;
            return $self->error("Bootstrap discovery failed. Giving up.") unless $self->bootstrap_discovery();
            return $self->discover();
        }

        fisher_yates_shuffle( \@list ) if @list > 1;

        $self->{s}->{ $stype{$querytype} } = \@list;
        push @{ $self->{s}->{modified_lst} }, $stype{$querytype};

    }

    $self->disconnect();

    unless ( $self->{opt}->{server} ) {
        if ( $self->{breed} =~ /^check/ ) {
            $self->{s}->{list}     = $self->{s}->{catalogue};
            $self->{s}->{listfile} = $self->{conf}->{listfile_catalogue};    # for discovery()
        }
        else {
            $self->{s}->{list}     = $self->{s}->{nomination};
            $self->{s}->{listfile} = $self->{conf}->{listfile_nomination};    # for discovery()
        }
    }

    $self->{s}->{new_list} = 1;

    $self->{done_discovery} = 1;
    $self->writeservers();

    return $self;
}

# only for debugging and errorchecking
#
sub logobj {
    my ( $self, $loglevel, $prefix, @objs ) = @_;

    return unless $self->logll($loglevel);

    foreach my $obj (@objs) {
        my $line = debugobj($obj);
        $self->log( $loglevel, "$prefix:\n $line" );
    }
}

#
# Mail Object
#
# Main data type used by check and report is the Mail Object.
# an array of hash ref's, where array order matches mails in mbox (or stdin).
#
#             key = value  (not all defined)
#
#              id = integer  NOTE: only key guaranteed to exist
#       orig_mail = ref to string containing orig email (headers+body)
#         headers = headers of orig_email
#            spam = 0, not spam, >1 spam
#          skipme = 0|1  (not checked against server, usually whitelisted mail)
#               p = array ref to mimeparts. see below
#              e1 = similar to p, but special for engine 1
#
# e1: each mail obj contains a special part for engine 1
#
#             skipme = 0|1  (ex: 1 if cleaned body goes to 0 len)
#               spam = 0, not spam, >1 spam
#               body = body of orig_mail
#            cleaned = body sent thru razor 1 preproc
#                 e1 = hash using engine 1
#               sent = hash ref sent to server
#               resp = hash ref of server response
#
# p: each mail obj contains 1 or more mimeparts, which can contain:
#
#                 id = string - mailid.part
#             skipme = 0|1  (ex: 1 if cleaned body goes to 0 len)
#               spam = 0, not spam, >1 spam
#               body = bodyparts (mimeparts) of orig_email, has X-Razor & Content-* headers
#            cleaned = body sent through preprocessors (deHtml, deQP, etc..), debugging use only
#                 e2 = hash using engine 2
#                 e3 = hash using engine 2
#                 e4 = hash using engine 2
#               sent = array ref of hash ref's sent to server
#               resp = array ref of hash ref's, where hash is parsed sis of server response
#
#
sub prepare_objects {
    my ( $self, $objs ) = @_;
    my @objects;

    unless ( $self->{s}->{engines}
        || ( $self->{s}->{engines} = $self->compute_supported_engines() ) ) {
        $self->log( 1, "ALLBAD. supported engines not defined" );
    }

    my $i = 1;
    if ( ref( $objs->[0] ) eq 'HASH' ) {    # checking cmd-line signatures
        foreach my $o (@$objs) {
            my $obj = { id => $i++ };
            $obj->{p}->[0]->{id} = "$obj->{id}.0";
            $obj->{p}->[0]->{"e$o->{eng}"} = $o->{sig};
            $obj->{ep4} = $o->{ep4} if $o->{ep4};
            push @objects, $obj;
        }

    }
    elsif ( ref( $objs->[0] ) eq 'SCALAR' ) {    # checking/reporting mail
        foreach my $o (@$objs) {
            my $obj = { id => $i++ };
            $obj->{orig_mail} = $o;
            $self->log2file( 16, $o, "$obj->{id}.orig_mail" );    # includes headers and all
            push @objects, $obj;
        }
        $self->prepare_parts( \@objects );
    }
    $self->logobj( 14, "prepared objs", \@objects );

    return \@objects;
}

sub prepare_parts {
    my ( $self, $objs ) = @_;

    my $prep_mail_debug = 0;    # debug print, 0=none, 1=split_mime stuff, 2=more verbose
    $prep_mail_debug++ if $self->{conf}->{debuglevel} > 15;
    $prep_mail_debug++ if $self->{conf}->{debuglevel} > 16;

    foreach my $obj (@$objs) {
        next if ( $obj->{skipme} || !$obj->{orig_mail} );

        #
        # now split up mime parts from orig mail
        #
        my ( $headers, @bodyparts ) = prep_mail(
            $obj->{orig_mail},
            $self->{conf}->{report_headers},
            4 * 1024,
            60 * 1024,
            15 * 1024,
            $self->{name_version},
            $prep_mail_debug,    # $debug,
        );

        my $lines = " prep_mail done: mail $obj->{id} headers=" . length($$headers);
        foreach ( 0 .. $#bodyparts ) { $lines .= ", mime$_=" . length( ${ $bodyparts[$_] } ); }
        $self->log( 8, $lines );

        unless (@bodyparts) {
            $self->log( 2, "empty body in mail $obj->{id}, skipping" );
            next;
        }

        $$headers =~ s/\r\n/\n/gs;
        $obj->{headers} = $headers;

        # $obj->{e1} = {
        #     id => "$obj->{id}.e1",
        #   body => $obj->{orig_mail},
        # };

        $obj->{p} = [];
        foreach ( 0 .. $#bodyparts ) {
            $bodyparts[$_] =~ s/\r\n/\n/gs;
            $obj->{p}->[$_] = {
                id   => "$obj->{id}.$_",
                body => $bodyparts[$_],
            };
        }
    }
    return 1;
}

# given mail objects, fills out
#
# - e1
#
# and for each body part of mail object, fills out
#
# - cleaned
# - e2
# - e3
# - e4
#
# also returns array ref of sigs suitable for printing
#
sub compute_sigs {
    my ( $self, $objects ) = @_;
    my @printable_sigs;

    foreach my $obj (@$objects) {
        next if ( $obj->{skipme} || !$obj->{orig_mail} );

        if ( ${ $obj->{orig_mail} } =~ /\n(Subject: [^\n]+)\n/ ) {
            my $subj = substr $1, 0, 70;
            $self->log( 8, "mail " . $obj->{id} . " $subj" );
        }
        else {
            $self->log( 8, "mail " . $obj->{id} . " has no subject" );
        }

        #
        # clean each bodypart, removing if new length is 0
        #
        next unless $obj->{p};
        foreach my $objp ( @{ $obj->{p} } ) {

            next if $objp->{skipme};
            my $olen    = length( ${ $objp->{body} } );
            my $clnpart = ${ $objp->{body} };

            # We'll do a VR8 preproc to determine emptiness
            # of email, and store it so VR8 can use it.

            my $clnpart_vr8 = $clnpart;
            $self->{preproc_vr8}->preproc( \$clnpart_vr8 );    # in da future: $self->{s}->{conf}->{dre}
            $objp->{cleaned_vr8} = \$clnpart_vr8;

            # This for VR4 (the only other signature scheme
            # supported at this time.

            $self->{preproc}->preproc( \$clnpart );
            $objp->{cleaned} = \$clnpart;

            my $clen = length($clnpart_vr8);

            $self->log2file( 15, $objp->{body},    "$objp->{id}.before_preproc.as_reported" );
            $self->log2file( 15, $objp->{cleaned}, "$objp->{id}.after_preproc" );

            if ( $clen eq 0 ) {
                $self->log( 6, "preproc: mail $objp->{id} went from $olen bytes to 0, erasing" );
                $objp->{skipme} = 1;
                next;
            }
            elsif ( ( $clen < 128 ) and ( $clnpart =~ /^(Content\S*:[^\n]*\n\r?)+(Content\S*:[^\n]*)?\s*$/s ) ) {
                $self->log( 6, "preproc: mail $objp->{id} seems empty, erasing" );
                $objp->{skipme} = 1;
                next;
            }
            elsif ( $clnpart_vr8 !~ /\S/ ) {
                $self->log( 6, "preproc: mail $objp->{id} went to all whitespace, erasing" );
                $objp->{skipme} = 1;
                next;
            }
            elsif ( $clen eq $olen ) {
                $self->log( 6, "preproc: mail $objp->{id} unchanged, bytes=$olen" );
            }
            else {
                $self->log( 6, "preproc: mail $objp->{id} went from $olen bytes to $clen " );
            }
        }

        #
        # compute sig for bodyparts that are cleaned.
        #
        if ( $self->{s}->{conf}->{ep4} ) {
            $obj->{ep4} = $self->{s}->{conf}->{ep4};
        }
        else {
            $obj->{ep4} = '7542-10';
            $self->log( 8, "warning: no ep4 for server $self->{s}->{ip}, using $obj->{ep4}" );
        }

        foreach my $objp ( @{ $obj->{p} } ) {

            next if $objp->{skipme};

            $self->log( 15, "mail part is [${$objp->{cleaned}}]" );

            if ( ${ $objp->{cleaned} } =~ /^\s+$/ ) {
                $self->log( 6, "mail $objp->{id} is whitespace only; skipping!" );
            }

            $self->log( 6, "computing sigs for mail $objp->{id}, len " . length( ${ $objp->{cleaned} } ) );

            foreach ( sort keys %{ $self->{s}->{engines} } ) {

                my $engine_no = $_;
                my $sig;

                if ( $engine_no == 4 ) {

                    $sig = $self->compute_engine(
                        $engine_no,
                        $objp->{cleaned},
                        $obj->{ep4}
                    );

                }
                elsif ( $engine_no == 8 ) {

                    $sig = $self->compute_engine(
                        $engine_no,
                        $objp->{cleaned_vr8}
                    );

                }
                else {

                    # Unsupported signature type, don't calculate.
                    next;    # handled above

                }

                if ($sig) {

                    $objp->{"e$engine_no"} = $sig;

                    my @sigs;
                    if ( ref $sig eq 'ARRAY' ) {
                        @sigs = @$sig;
                    }
                    else {
                        push @sigs, $sig;
                    }

                    for (@sigs) {

                        my $line = "$objp->{id} e$engine_no: $_";
                        $line .= ", ep4: $obj->{ep4}" if ( $engine_no eq '4' );
                        push @printable_sigs, $line;

                    }

                }
                else {

                    $self->log( 6, "Engine ($engine_no) didn't produce a signature for mail $objp->{id}" );

                }

            }

        }

        $self->logobj( 14, "computed sigs for obj", $obj );

    }

    return \@printable_sigs;

}

#
# this function is the only one that has to be aware
# of razor protocol syntax.  (not including random logging)
# the hashes generated here are eventually sent to to_batched_query.
#
sub make_query {
    my ( $self, $params ) = @_;

    if ( $params->{action} =~ /^check/ ) {

        if ( ref $params->{sig} eq 'ARRAY' ) {    # Multiple signature per part, VR8

            my $sigs = $params->{sig};
            my @queries;
            for (@$sigs) {
                my %query = ( a => 'c', e => $params->{eng}, s => $_ );
                push @queries, \%query;
            }
            return \@queries;

        }
        else {

            my %query = ( a => 'c', e => $params->{eng}, s => $params->{sig} );
            $query{ep4} = $params->{ep4} if $query{e} eq '4';
            return \%query;

        }

    }
    elsif ( $params->{action} =~ /^rcheck/ ) {

        my %query = (
            a => 'r',
            e => $params->{eng},
            s => $params->{sig},
        );
        $query{ep4} = $params->{ep4} if $query{e} eq '4';
        return \%query;

    }
    elsif ( $params->{action} =~ /(report)/ ) {

        # prep_mail already truncated headers and body parts > 64K
        my @dudes;
        my $n = 0;
        while ( $params->{obj}->{p}->[$n] ) {
            my $line = ${ $params->{obj}->{headers} };
            while (1) {
                my $body = $params->{obj}->{p}->[$n]->{body};
                last unless ( ( length($$body) + length($line) < $self->{s}->{conf}->{bqs} * 1024 ) );

                $self->log( 11, "bqs=" . ( $self->{s}->{conf}->{bqs} * 1024 ) . " adding to line [len=" . length($line) . "] mail $params->{obj}->{p}->[$n]->{id}" . " [len=" . length($$body) . "], total len=" . ( length($$body) + length($line) ) );

                $line .= "\r\n" . $$body;
                $n++;
                last unless $params->{obj}->{p}->[$n];
            }
            push @dudes, $line;
        }

        my @queries;
        foreach (@dudes) {
            push @queries, {
                a => $params->{action} eq 'report' ? 'r' : 'revoke',
                message => $_,
            };
        }
        return @queries;

    }
    elsif ( $params->{action} =~ /revoke/ ) {

        # Never send messages on revoke. Revoke all signature
        # that we were able to compute.

        my $n = 0;
        my @queries;
        while ( $params->{obj}->{p}->[$n] ) {
            for my $engine ( keys %{ $self->{s}->{engines} } ) {
                my $sigs;
                if ( $sigs = $params->{obj}->{p}->[$n]->{"e$engine"} ) {
                    if ( ref $sigs eq 'ARRAY' ) {
                        for my $sig (@$sigs) {
                            push @queries, { a => 'revoke', e => $engine, s => $sig };
                        }
                    }
                    else {
                        push @queries, { a => 'revoke', e => $engine, s => $sigs };
                    }
                }
            }
            $n++;
        }

        return @queries;

    }

}

#
# prepare queries in correct syntax for sending over network
#
sub obj2queries {
    my ( $self, $objects, $action ) = @_;
    my @queries = ();

    foreach my $obj (@$objects) {
        next if $obj->{skipme};
        push @queries, $obj->{e1}->{sent} if $obj->{e1}->{sent};
        foreach my $objp ( @{ $obj->{p} } ) {
            next if $objp->{skipme};

            #$self->log(8,"not skipping mail part $objp->{id}, sent: ". scalar(@{$objp->{sent}}));
            push @queries, @{ $objp->{sent} } if $objp->{sent};
        }
    }

    if ( scalar(@queries) ) {
        $self->log( 8, "preparing " . scalar(@queries) . " queries" );
    }
    else {
        $self->log( 8, "objects yielded no valid queries" );
        return [];
    }
    my $qbatched = to_batched_query(
        \@queries,
        $self->{s}->{conf}->{bql},
        $self->{s}->{conf}->{bqs},
        1
    );
    $self->log( 8, "sending " . scalar(@$qbatched) . " batches" );

    return $qbatched;
}

#
# Parse response syntax, add info to appropriate object
#
sub queries2obj {
    my ( $self, $objs, $responses, $action ) = @_;

    my @resp;
    foreach (@$responses) {

        # from_batched_query wants "-" in beginning, but not ".\r\n" at end
        s/\.\r\n$//sg;
        my $arrayref = from_batched_query($_);
        push @resp, @$arrayref;
    }

    $self->log( 12, "processing " . scalar(@resp) . " responses" );
    $self->logobj( 14, "from_batched_query", \@resp );

    my $j = 0;
    while (@resp) {
        my $obj = $objs->[ $j++ ];
        return $self->error("more responses than mail objs!") unless $obj;
        next if $obj->{skipme};

        if ( $obj->{e1}->{sent} && !$obj->{e1}->{skipme} ) {
            $obj->{e1}->{resp} = shift @resp;
            $self->log( 12, "adding a resp to mail $obj->{e1}->{id}" );
        }
        foreach my $objp ( @{ $obj->{p} } ) {
            next unless $objp->{sent};

            # for each part, shift out as many responses as there were queries
            foreach ( @{ $objp->{sent} } ) {
                push @{ $objp->{resp} }, shift @resp;
                $self->log( 12, "adding a resp to mail $objp->{id}" );
            }
        }

        #$self->logobj(13,"end of queries2obj",$obj);
    }
    return 1;
}

sub check_resp {
    my ( $self, $me, $sent, $resp, $objp ) = @_;

    # default is no contention
    $objp->{ct} = 0;
    $objp->{ct} = $resp->{ct} if exists $resp->{ct};

    if ( exists $resp->{err} ) {
        $self->logobj( 4, "$me: got err $resp->{err} for query", $sent );
        return 0;
    }
    if ( $resp->{p} eq '1' ) {
        if ( exists $resp->{cf} ) {
            if ( $resp->{cf} < $self->{s}->{min_cf} ) {
                $self->log( 6, "$me: Not spam: cf $resp->{cf} < min_cf $self->{s}->{min_cf}" );
                return 0;
            }
            else {
                $self->log( 6, "$me: Is spam: cf $resp->{cf} >= min_cf $self->{s}->{min_cf}" );
                return 1;
            }
        }
        $self->log( 6, "$me: sig found, no cf, ok." );
        return 1;
    }
    if ( $resp->{p} eq '0' ) {
        $self->log( 6, "$me: sig not found." );
        return 0;
    }

    # should never get here
    $self->logobj(
        2, "$me: got bad response from server - sent obj, resp obj",
        [ $sent, $resp ]
    );
    return 0;
}

sub rcheck_resp {
    my ( $self, $me, $sent, $resp ) = @_;

    $self->log( 8, "$me: invalid $sent" ) unless ref($sent);
    $self->log( 8, "$me: invalid $resp" ) unless ref($resp);

    if ( exists $resp->{err} ) {
        if ( $resp->{err} eq '230' ) {
            $self->log( 8, "$me: err 230 - server wants mail" );
            return 1;
        }
        $self->logobj( 4, "$me: got err $resp->{err} for query", $sent );
        return 0;
    }
    if ( $resp->{res} eq '1' ) {
        $self->log( 5, "$me: Server accepted report." );
        return 0;
    }
    if ( $resp->{res} eq '0' ) {
        $self->log( 1, "$me: Server did not accept report.  Shame on the server." );
        return 0;
    }

    # should never get here
    $self->logobj(
        2, "$me: got bad response from server - sent obj, resp obj",
        [ $sent, $resp ]
    );
    return 0;
}

sub check {
    my ( $self, $objects ) = @_;

    my $valid = 0;
    foreach my $obj (@$objects) {
        next if $obj->{skipme};

        #
        # Logic used in ordering of check queries
        #
        # queries should go like this: (e=engine, p=part)
        # e1, p0e2, p0e3, p0e4, p1e2, p1e3, p1e4, etc..
        # unless cmd-line sigs are passed.
        #

        # engine 1 is for entire mail, not parts
        if (
            $obj->{e1}    # cmd-line sig checks don't have this
            && $self->{s}->{engines}->{1}
        ) {
            $obj->{e1}->{sent} = $self->make_query(
                {
                    action => 'check',
                    sig    => $obj->{e1}->{e1},
                    eng    => 1
                }
            );
        }

        # rest of engines and mime parts
        foreach my $objp ( @{ $obj->{p} } ) {

            if ( $objp->{skipme} ) {
                $self->log( 8, "mail $objp->{id} skipped in check" );
                next;
            }
            $objp->{sent} = [];
            foreach ( sort keys %{ $self->{s}->{engines} } ) {

                my $engine_save = $_;
                next if $_ eq 1;    # engine 1 done above
                my $sig = $objp->{"e$_"};
                unless ($sig) {
                    $self->log( 5, "mail $objp->{id} e$_ got no sig" );
                    next;
                }

                unless ( $self->{s}->{engines}->{$_} ) {

                    # warn if cmd-lig sig check is not supported
                    $self->log( 5, "mail $objp->{id} engine $_ is not supported, sig check skipped" )
                      if ( $sig && !$obj->{orig_mail} );
                    next;
                }

                if ( ref $sig ) {
                    for (@$sig) {
                        $self->log( 8, "mail $objp->{id} e$engine_save sig: $_" );
                    }
                }
                else {
                    $self->log( 8, "mail $objp->{id} e$engine_save sig: $sig" );
                }

                my $query = $self->make_query(
                    {
                        action => 'check',
                        sig    => $sig,
                        ep4    => $obj->{ep4},
                        eng    => $_
                    }
                );

                $valid++ if $query;
                if ( ref $query eq 'ARRAY' ) {
                    push @{ $objp->{sent} }, @$query;
                }
                else {
                    push @{ $objp->{sent} }, $query;
                }
            }
        }
    }
    unless ($valid) {
        $self->log( 5, "No queries, no spam" );
        return 1;
    }

    $self->{s}->{list} = $self->{s}->{catalogue};
    $self->connect;

    # Build query text strings
    #
    my $queries = $self->obj2queries( $objects, 'check' ) or return $self->errprefix("check 1");

    # send to server and store answers in mail obj
    #
    my $response = $self->_send($queries) or return $self->errprefix("check 2");
    $self->queries2obj( $objects, $response, 'check' ) or return $self->errprefix("check 3");

    foreach my $obj (@$objects) {

        # check_logic will parse response for each object, decide if its spam
        #
        $self->check_logic($obj);

        $self->log( 3, "mail $obj->{id} is " . ( $obj->{spam} ? '' : 'not ' ) . "known spam." );
    }
    return 1;
}

sub check_logic {
    my ( $self, $obj ) = @_;

    # default is not spam
    $obj->{spam} = 0;
    if ( $obj->{skipme} ) {
        next;
    }

    #
    # Logic for Spam
    #
    #
    my $logic_method  = $self->{conf}->{logic_method}  || 4;
    my $logic_engines = $self->{conf}->{logic_engines} || 'any';

    # cmd-line sig checks default to logic_method 1
    $logic_method = 1 unless $obj->{orig_mail};

    my $leng;
    if ( $logic_engines eq 'any' ) {
        $leng = "";    # not a hash ref, implies 'any' logic_engine
    }
    elsif ( $logic_engines eq 'all' ) {
        $leng = $self->{s}->{engines};
    }
    elsif ( $logic_engines =~ /^(\d\,)+$/ ) {
        $leng = {};
        foreach ( split /,/, $logic_engines ) {
            unless ( $self->{s}->{engines}->{$_} ) {
                $self->log( 3, "logic_engine $_ not supported, skipping" );
                next;
            }
            $leng->{$_} = 1;
        }
    }
    else {
        $self->log( 3, "invalid logic_engines: $logic_engines, defaulting to 'any'" );
        $leng = "";    # not a hash ref, implies 'any' logic_engine
    }

    # iterate through sent queries and responses,
    # perform engine analysis (logic_engines).
    #
    # engine 1 case
    my $sent = $obj->{e1}->{sent};
    my $resp = $obj->{e1}->{resp};
    if ( $resp && $sent ) {

        # if skipme, there would be no resp
        my $logmsg = "mail $obj->{id} e=1 sig=$sent->{s}";
        $obj->{e1}->{spam} = $self->check_resp( $logmsg, $sent, $resp, $obj->{e1} );
    }

    # all other engines for all parts
    foreach my $objp ( @{ $obj->{p} } ) {
        $objp->{spam} = 0;
        if ( $objp->{skipme} ) {
            $self->log( 8, "doh. $objp->{id} is skipped, yet has sent" ) if $objp->{sent};
            next;
        }
        next unless $objp->{sent};
        my $not_spam = 0;
        foreach ( 0 .. ( scalar( @{ $objp->{sent} } ) - 1 ) ) {
            $sent = $objp->{sent}->[$_];
            $resp = $objp->{resp}->[$_];
            unless ($resp) {
                $self->log( 5, "doh. more sent queries than responses" );
                next;
            }
            my $logmsg = "mail $objp->{id} e=$sent->{e} sig=$sent->{s}";
            my $is_spam = $self->check_resp( $logmsg, $sent, $resp, $objp );

            if ( ref($leng) ) {
                if ( $leng->{ $sent->{e} } && $is_spam ) {
                    $self->log( 8, "logic_engines requires $sent->{e}, and it is. cool." );
                    $objp->{spam} = 1;
                }
                elsif ( $leng->{ $sent->{e} } && !$is_spam ) {
                    $self->log( 8, "logic_engines requires $sent->{e}, and it is not, part not spam" );
                    $not_spam = 1;
                }
                else {
                    $self->log( 8, "logic_engines doesn't care about $sent->{e}, skipping" );
                }
            }
            else {
                # not a hash ref, implies 'any' logic_engine
                $objp->{spam} += $is_spam;
            }
            $objp->{spam} = 0 if $not_spam;
        }
    }

    # mime part analysis (logic_methods)
    #
    if ( $logic_method == 1 ) {

        $obj->{spam} = 0;
        if ( $obj->{e1} ) {
            $obj->{spam} += $obj->{e1}->{spam} if $obj->{e1}->{spam};
        }
        foreach my $objp ( @{ $obj->{p} } ) {
            $obj->{spam} += $objp->{spam} if $objp->{spam};
        }

    }
    elsif ( $logic_method =~ /^(2|3)$/ ) {

        # logic_methods > 1

        foreach my $objp ( @{ $obj->{p} } ) {
            next if $objp->{skipme};
            next unless $objp->{body};
            my ( $hdrs, $body ) = split /\n\n/, ${ $objp->{body} }, 2;
            $hdrs .= "\n";

            #$self->log(8,"$objp->{id} hdrs:\n$hdrs");
            my $type = "<type unknown>";
            $objp->{is_text}   = 0;
            $objp->{is_inline} = 0;
            $objp->{is_inline} = 1 if $hdrs =~ /Content-Disposition: inline/i;

            #$type = $1 if $hdrs =~ /Content-Type:\s([^\;\n]+)/i;
            $type            = $1 if $hdrs =~ /Content-Type:\s([^\n]+)/i;
            $objp->{is_text} = 1  if $type =~ /text\//i;
            $objp->{is_text} = 1  if $type =~ /type unknown/;               # assume text ?

            $self->log( 8, "mail $objp->{id} Type $objp->{is_text},$objp->{is_inline} $type" );
        }
    }

    if ( $logic_method == 2 ) {

        # in this method, only 1 dude decides if mail is spam. decider.

        # the first part is the default decider. can be overwritten, tho.
        my $decider = $obj->{p}->[0];

        # basically the first inline text/* becomes the decider.
        # however, if no inline, the first text/* is used
        my $found = 0;
        foreach my $objp ( @{ $obj->{p} } ) {
            next if $objp->{skipme};

            if ( $objp->{is_inline} && $objp->{is_text} ) {
                $decider = $objp;
                last;
            }
            if ( !$found && $objp->{is_text} ) {
                $decider = $objp;
                $found   = 1;
            }
        }
        $self->log( 7, "method 2: $decider->{id} is the spam decider" );
        $obj->{spam} = $decider->{spam};

    }
    elsif ( $logic_method == 3 ) {

        # in this method, all text/* parts must be spam for obj to be spam
        # non-text parts are ignored

        my $found = 0;
        foreach my $objp ( @{ $obj->{p} } ) {
            next if $objp->{skipme};
            next unless $objp->{is_text};
            $found = 1;
            $obj->{spam} = $objp->{spam};
            unless ( $objp->{spam} ) {
                $self->log( 7, "method 3: $objp->{id} is_text but not spam, mail not spam" );
                last;
            }
        }
        $self->log( 7, "method 3: mail $obj->{id}: all is_text parts spam, mail spam" ) if $obj->{spam};

        # if no parts where text, use the first part as spam indicator
        unless ($found) {
            $self->log( 6, "method 3: mail $obj->{id}: no is_text, using part 1" );
            $obj->{spam} = 1 if $obj->{p}->[0]->{spam};
        }

    }
    elsif ( $logic_method == 4 ) {

        # in this method, if any non-contention parts is spam, mail obj is spam
        # contention parts are ignored.

        $obj->{spam} = 0;
        foreach my $objp ( @{ $obj->{p} } ) {
            next if $objp->{skipme};
            if ( $objp->{ct} ) {
                $self->log( 7, "method 4: mail $objp->{id}: contention part, skipping" );
            }
            else {
                $self->log( 7, "method 4: mail $objp->{id}: no-contention part, spam=$objp->{spam}" );
                $obj->{spam} = 1 if $objp->{spam};
            }
        }
        if ( $obj->{spam} ) {
            $self->log( 7, "method 4: mail $obj->{id}: a non-contention part was spam, mail spam" );
        }
        else {
            $self->log( 7, "method 4: mail $obj->{id}: all non-contention parts not spam, mail not spam" );
        }

    }
    elsif ( $logic_method == 5 ) {

        # in this method, all non-contention parts must be spam for obj to be spam
        # contention parts are ignored.

        my $not_spam = 0;
        my $is_spam  = 0;
        foreach my $objp ( @{ $obj->{p} } ) {
            next if $objp->{skipme};
            if ( $objp->{ct} ) {
                $self->log( 7, "method 5: mail $objp->{id}: contention part, skipping" );
                next;
            }
            else {
                $self->log( 7, "method 5: mail $objp->{id}: no-contention part, spam=$objp->{spam}" );
            }
            if ( $objp->{spam} ) {
                $is_spam = 1;
            }
            else {
                $not_spam = 1;
            }
        }
        if ( $is_spam && !$not_spam ) {
            $obj->{spam} = 1;
            $self->log( 7, "method 5: mail $obj->{id}: all non-contention parts spam, mail spam" );
        }
        else {
            $self->log( 7, "method 5: mail $obj->{id}: a non-contention part not spam, mail not spam" );
            $obj->{spam} = 0;
        }

    }
    return 1;
}

# returns hash ref if successfully registered
# returns 0 if not
sub register {
    my ( $self, $p, ) = @_;
    my @queries;

    my $registrar = $self->{name_version};
    my %qr = ( a => 'reg', registrar => $registrar );
    $qr{user} = $p->{user} if $p->{user};
    $qr{pass} = $p->{pass} if $p->{pass};
    $queries[0] = makesis(%qr);

    $self->{s}->{list} = $self->{s}->{nomination};
    $self->connect;

    my $response = $self->_send( \@queries ) or return $self->errprefix("register");

    my %resp = parsesis( $$response[0] );

    if ( $resp{err} && $resp{err} eq '210' ) {
        if ( $qr{user} && $qr{pass} ) {
            my ($creds) = { user => $qr{user}, pass => $qr{pass} };
            if ( $self->authenticate($creds) ) {
                $self->log( 6, "Successfully registered provided credentials.\n" );
                return $creds;
            }
        }
        return $self->error("Error $resp{err}: User exists. Try another name. aborting.\n");
    }
    return $self->error("Error $resp{err} while performing register, aborting.\n")
      if ( $resp{err} );

    return $self->error("No success (res=$resp{res}) while performing register, aborting.\n")
      if ( $resp{res} ne '1' );

    $self->log( 6, "Successfully registered with $self->{s}->{ip} identity: $resp{user}" );

    # otherwise return hash containing 'user' and 'pass'
    delete $resp{res};
    return \%resp;
}

sub authenticate {
    my ( $self, $options ) = @_;
    my @queries;

    unless ( ( $options->{user} =~ /\S/ ) && ( $options->{pass} =~ /\S/ ) ) {
        return $self->error("authenticate did not get valid user + pass");
    }

    my %qr = ( a => 'ai', user => $options->{user}, cn => 'razor-agents', cv => $Razor2::Client::Version::VERSION );
    $queries[0] = makesis(%qr);

    $self->{s}->{list} = $self->{s}->{nomination};
    $self->connect;

    my $response = $self->_send( \@queries ) or return $self->errprefix("authenticate 1");

    my %resp = parsesis( $$response[0] );
    if ( $resp{err} ) {
        if ( ( $resp{err} eq '213' ) && !defined( $self->{reregistered} ) ) {

            # 213 = unknown user.
            # Try to register with current user+pass and continue with authenticate
            $self->log( 8, "unknown user, attempting to re-register" );

            my $id = $self->register($options);
            $self->{reregistered} = 1;
            if (   ( $id->{user} eq $options->{user} )
                && ( $id->{pass} eq $options->{pass} ) ) {
                $self->log( 5, "re-registered user $id->{user} with $self->{s}->{ip}" );
                return $self->authenticate($options);
            }
            else {
                return $self->error("Error 213 while authenticating, aborting.\n");
            }
        }
        else {
            return $self->error("Error $resp{err} while authenticating, aborting.\n");
        }
    }
    my ( $iv1, $iv2 ) = xor_key( $options->{pass} );
    my ($my_digest) = hmac_sha1( $resp{achal}, $iv1, $iv2 );

    %qr = ( a => 'auth', aresp => $my_digest );
    $queries[0] = makesis(%qr);

    $response = $self->_send( \@queries ) or return $self->errprefix("authenticate 2");

    %resp = parsesis( $$response[0] );
    return $self->error("Error $resp{err} while authenticating, aborting.\n") if ( $resp{err} );
    return $self->error("Authentication failed for user=$options->{user}")
      if ( $resp{res} ne '1' );

    $self->log( 5, "Authenticated user=$options->{user}" );
    $self->{authenticated} = 1;
    return 1;
}

#
# handles report and revoke
#
sub report {

    my ( $self, $objs ) = @_;
    return $self->error("report: Not Authenticated") unless $self->{authenticated};

    return $self->error("report/revoke for engine 1 not supported")
      if ( $self->{s}->{conf}->{dre} == 1 );

    $self->{s}->{list} = $self->{s}->{nomination};
    $self->connect;

    my @robjs;
    my $valid = 0;
    if ( $self->{breed} eq 'report' ) {

        #
        # Before reporting entire email, check to see if server already has it
        #

        unless ( $self->{s}->{conf}->{dre} ) {
            $self->logobj( 8, "server has no default dre, using 4", $self->{s}->{conf} );
            $self->{s}->{conf}->{dre} = 4;
        }

        foreach my $obj (@$objs) {
            next if $obj->{skipme};

            # handle special case for engine  1
            # note: razor 1 does not store emails in its db, just sigs.
            # so we should never get a res=230 for e=1 a=r sig=xxx
            #
            #$obj->{e1}->{sent} = $self->make_query( {
            #             action => 'rcheck',
            #                sig => $obj->{e1}->{e1},
            #                eng => 1, } );
            #$valid++ if $obj->{e1}->{sent};

            # rest of engines and mime parts
            foreach my $objp ( @{ $obj->{p} } ) {

                if ( $objp->{skipme} ) {
                    $self->log( 13, "mail $objp->{id} skipped in report" );
                    next;
                }
                my $q = $self->make_query(
                    {
                        action => 'rcheck',
                        sig    => $objp->{"e$self->{s}->{conf}->{dre}"},
                        ep4    => $obj->{ep4},
                        eng    => $self->{s}->{conf}->{dre},
                    }
                );
                $objp->{sent} = [$q];
                $valid++;
            }
        }
        unless ($valid) {
            $self->log( 5, "No report check queries, no spam" );
            return 1;
        }
        $valid = 0;

        # Build query text strings - signatures computed already (see reportit)
        my $queries = $self->obj2queries( $objs, 'rcheck' ) or return $self->errprefix("report1");

        # send to server and store answers in mail obj
        my $response = $self->_send($queries) or return $self->errprefix("report2");
        $self->queries2obj( $objs, $response, 'rcheck' ) or return $self->errprefix("report3");

        #
        # If server wants email or certain body parts,
        # create new {sent} and add obj to @robjs
        #
        foreach my $obj (@$objs) {

            next if $obj->{skipme};

            #$self->log(12,"mail $obj->{id} read ". scalar(@{$obj->{resp}}) ." queries");
            # handle engine 1 special case
            #if ( !$obj->{e1}->{skipme} && $self->rcheck_resp(
            #        "mail ". $obj->{id} .", orig_email, special case eng 1",
            #        $obj->{e1}->{sent},
            #        $obj->{e1}->{resp}
            #        ) ) {
            #    $self->log(5,"doh.  Server should not send res=230 for eng=1 report");
            #}
            #delete $obj->{e1}->{sent};

            my $wants_orig_mail = 0;
            foreach my $objp ( @{ $obj->{p} } ) {
                next if $objp->{skipme};

                $self->logobj( 14, "checking response for $objp->{id}", $objp );
                unless (
                    $self->rcheck_resp(
                        "mail $objp->{id}, eng $self->{s}->{conf}->{dre}",
                        $objp->{sent}->[0], $objp->{resp}->[0]
                    )
                ) {
                    $objp->{skipme} = 1;
                }
                else {
                    $wants_orig_mail++;
                }
                $objp->{resp} = [];    # clear responses from rcheck
                $objp->{sent} = [];
            }
            if ($wants_orig_mail) {

                # reports are special, all parts need to be together, so use part 0's sent
                my $objp = $obj->{p}->[0];
                $objp->{skipme} = 0 if $objp->{skipme};
                push @{ $objp->{sent} }, $self->make_query(
                    {
                        action => 'report',
                        obj    => $obj,
                    }
                );
                push @robjs, $obj;
            }
            $valid += $wants_orig_mail;
        }

    }
    else {    # revoke

        foreach my $obj (@$objs) {

            # don't revoke eng 1

            # engines > 1 we send all the body parts, use part 0 to store sent
            my $objp = $obj->{p}->[0];
            $objp->{sent} = [];

            push @{ $objp->{sent} }, $self->make_query(
                {
                    action => 'revoke',
                    obj    => $obj,
                }
            );
            $valid++ if scalar( @{ $objp->{sent} } );
            $self->log( 9, "revoke sent:" . scalar( @{ $objp->{sent} } ) );
            push @robjs, $obj;
        }

    }

    unless ( $valid && scalar(@robjs) ) {
        $self->log( 3, "Finished $self->{breed}." );
        return 1;
    }

    #$self->logobj(14,"report objs", \@robjs);

    #
    # send server mails/body parts either
    # revoked, or requested if reporting
    #
    my $queries = $self->obj2queries( \@robjs, $self->{breed} ) or return $self->errprefix("report4");
    my $response = $self->_send($queries) or return $self->errprefix("report5");
    $self->queries2obj( \@robjs, $response, $self->{breed} ) or return $self->errprefix("report6");

    # we just do this to log server's response
    #
    foreach my $obj (@robjs) {
        my $objp = $obj->{p}->[0];
        my $cur  = -1;
        while ( $objp->{sent}->[ ++$cur ] ) {
            $self->rcheck_resp(
                "$self->{breed}: mail $obj->{id}, $cur",
                $objp->{sent}->[$cur],
                $objp->{resp}->[$cur]
            ) unless ( $objp->{skipme} );
        }
    }
    $self->logobj( 14, "report objs", \@robjs );
    $self->log( 3, "Sent $self->{breed}." );
    return 1;

}

sub _send {
    my ( $self, $msg, $closesock, $skipread ) = @_;
    $self->log( 16, "entered _send" );

    unless ( $self->{connected_to} ) {
        $self->connect() or return $self->errprefix("_send");
    }

    my @response;
    my $select = $self->{select};
    my $sock   = ( $select->handles )[0];
    $self->{sent_cnt} = 0 unless $self->{sent_cnt};
    foreach my $i ( 0 .. ( ( scalar @$msg ) - 1 ) ) {
        my @handles = $select->can_write(15);
        if ( $handles[0] ) {
            $self->log( 4, "$self->{connected_to} << " . length( $$msg[$i] ) );
            if ( $$msg[$i] =~ /message/ ) {
                my $line = debugobj( $$msg[$i] );
                $self->log( 6, $line );
                $self->log2file( 16, \$$msg[$i], "sent_to." . $self->{sent_cnt} );
            }
            else {
                $self->log( 6, $$msg[$i] );
            }
            local $\;
            undef $\;
            print $sock $$msg[$i];
            $self->{sent_cnt}++;
        }
        else {
            return $self->error("Timed out (15 sec) while writing to $self->{s}->{ip}");
        }
        next if $skipread;
        @handles = $select->can_read(15);
        if ( $sock = $handles[0] ) {
            local $/;
            undef $/;
            $response[$i] = $self->_read($sock) or return $self->error("Error reading socket");
            $self->log( 4, "$self->{connected_to} >> " . length( $response[$i] ) );
            $self->log( 6, "response to sent.$self->{sent_cnt}\n" . $response[$i] );
        }
        else {
            return $self->error("Timed out (15 sec) while reading from $self->{s}->{ip}");
        }
    }
    if ($closesock) {
        $select->remove($sock);
        close $sock;
    }
    return \@response;
}

sub _read {
    my ( $self, $socket ) = @_;
    my ( $query, $read );

    # fixme - need to trim this down (copied from server)
    #

    unless ( $read = sysread( $socket, $query, 1024 ) ) {

        # There was an error on sysread(), could be a real error or a
        # blocking error.

        if ( $! == EWOULDBLOCK ) {

            # write would block, so we try again later
            $self->debug("_read: EWOULDBLOCK");
            return;
        }
        elsif ( $! == EINTR or $! == EIO ) {

            # sysread() got interupted by a signal.
            # we will process this socket on next wheelwalk.
            $self->debug("_read: EINTR");
            return;
        }
        elsif ( $! == EPIPE or $! == EISDIR or $! == EBADF or $! == EINVAL or $! == EFAULT ) {
            $self->debug("_read: EPIPE");
            return;
        }
        else {
            # This happens when client breaks the connection.
            # Find out why we don't get an EPIPE instead. FIX!
            $self->debug("_read: connection_closed");
            return;
        }
    }

    if ( $read > 0 ) {

        # Now we are absolutely sure there is data on the socket.

        return $query;

    }
    else {

        # Otherwise we got an EOF, expire the socket

        $self->debug("_read: EOF, connection_closed");
        return;

    }
}

sub connect {
    my ( $self, %params ) = @_;
    my $sock;
    $self->log( 16, "entered connect" );

    if ( $self->{simulate} ) {
        return $self->error("Razor Error 4: This is a simulation. Won't connect to $self->{s}->{ip}.");
    }

    my $server = $params{server} || $self->{s}->{ip};

    unless ( $self->{s}->{ip} ) {
        $self->{s}->{ip} = $server;
    }

    if ( $self->{sock} && $self->{connected_to} ) {
        unless ($server) {
            $self->log( 13, "no server specified, using already connected server $self->{connected_to}" );
            return 1;
        }
        if ( $server eq $self->{connected_to} ) {
            $self->log( 15, "already connected to server $self->{connected_to}" );
            return 1;
        }
        return 1 if $self->{disconnecting};
        $self->log( 6, "losing old server connection, $self->{connected_to}, for new server, $server" );
        $self->disconnect;
    }
    unless ($server) {
        $self->log( 6, "no server specified, not connecting" );
        return;
    }

    my $port = $params{port} || $self->{s}->{port};
    unless ( defined($port) && $port =~ /^\d+$/ ) {
        my $portlog = defined($port) ? " ($port)" : "";
        $self->log( 6, "No port specified$portlog, using 2703" );    # bootstrap_discovery will come here
        $port = 2703;
    }
    $self->log( 5, "Connecting to $server ..." );
    if ( my $proxy = $self->{conf}->{proxy} ) {

        #
        # Proxy stuff never been tested
        #
        $proxy =~ s!^http://!!;
        $proxy =~ s!:(\d+)/?$!!;
        my $pport = $1 || 80;
        $self->debug("HTTP tunneling through $proxy:$pport.");
        $sock = IO::Socket::INET->new(
            PeerAddr => $proxy,
            PeerPort => $pport,
            Proto    => 'tcp',
            Timeout  => 20,
        );
        unless ($sock) {
            $self->debug("Unable to connect to proxy $proxy:$pport; Reason: $!.");
        }
        else {
            $sock->printf( "CONNECT %s:%d HTTP/1.0\r\n\r\n", $server, $port );
            if ( $sock->getline =~ m!^HTTP/1\.\d+ 200 ! ) {

                # Skip through remaining part of MIME header.
                while ( $sock->getline !~ m!^\r! ) { ; }
            }
            else {
                $self->log( 4, "HTTP tunneling is disabled at $proxy." );
                $sock = undef;
            }
        }
    }

    # if proxy, we already might have a $sock.
    # if proxy failed to connect, try without proxy.
    #

    if ( $self->{conf}->{socks_server} ) {

        my $socks_module = "Net::SOCKS";
        eval "require $socks_module";

        $self->log( 6, "Will try to connect through the SOCKS server on $$self{conf}{socks_server}..." );

        my $socks_sock = Net::SOCKS->new(
            socks_addr       => $$self{conf}{socks_server},
            socks_port       => 1080,
            protocol_version => 4
        );

        if ($socks_sock) {

            $sock = $socks_sock->connect( peer_addr => $server, peer_port => $port );
            if ($sock) {
                $self->log( 6, "Connected to $server via SOCKS server $$self{conf}{socks_server}." );
            }

        }

    }

    unless ($sock) {
        $sock = IO::Socket::INET->new(
            PeerAddr => $server,
            PeerPort => $port,
            Proto    => 'tcp',
            Timeout  => 20,
        );
        unless ($sock) {
            $self->log( 3, "Unable to connect to $server:$port; Reason: $!." );
            return if $params{discovery_server};
            $self->nextserver or do { return $self->errprefix("connect1"); };
            return $self->connect;
        }
    }

    my $select  = new IO::Select($sock);
    my @handles = $select->can_read(15);
    if ( $handles[0] ) {
        $self->log( 8, "Connection established" );
        my $greeting = <$sock>;

        # $sock->autoflush; # this is on by default as of IO::Socket 1.18
        $self->{sock}         = $sock;
        $self->{connected_to} = $server;
        $self->{select}       = $select;
        $self->log( 4, "$server >> " . length($greeting) . " server greeting: $greeting" );

        return 1 if $params{discovery_server};
        unless ( $self->parse_greeting($greeting) ) {
            $self->nextserver or return $self->errprefix("connect2");
            return $self->connect;
        }
        return 1;
    }
    else {
        $self->log( 3, "Timed out (15 sec) while reading from $self->{s}->{ip}." );
        $select->remove($sock);
        $sock->close();
        return $self->errprefix("connect3") if $params{skip_greeting};
        $self->nextserver or return $self->errprefix("connect4");
        return $self->connect;
    }
}

sub disconnect {
    my $self = shift;

    unless ( $self->{sock} ) {
        $self->log( 5, "already disconnected from server " . $self->{connected_to} );
        return 1;
    }

    $self->log( 5, "disconnecting from server " . $self->{connected_to} );

    $self->{disconnecting} = 1;
    $self->_send( ["a=q\r\n"], 0, 1 );
    delete $self->{disconnecting};

    delete $self->{sock};    # _send closes socket

    return 1;
}

sub parse_greeting {
    my ( $self, $greeting ) = @_;
    $self->log( 16, "entered parse_greeting($greeting)" );

    my %server_greeting = parsesis($greeting);
    $self->{s}->{greeting} = \%server_greeting;

    unless ( $self->{s}->{greeting} && $self->{s}->{greeting}->{sn} ) {
        $self->log( 1, "Couldn't parse server greeting\n" );
        return;
    }

    # server greeting must contain: sn, srl
    # server greeting may  contain: ep4, redirect, a,

    #
    # fixme - add support for redirect, etc.
    #

    #
    # current server config info is stored in $self->{s}->{conf}
    # see nextserver for more info
    #
    # If server greeting says there are new values
    # (which we know if greeting's srl > conf's srl)
    # we ask server for new values, update conf, then
    # put that server on modified list so it gets recorded to disk
    #
    # fixme - in the future, we could have a key with no value
    #         in .conf file - forcing client to ask server 'a=g&pm=key'
    #

    if ( $self->{s}->{greeting}->{a} eq 'cg' ) {
        my $version = $Razor2::Client::Version::VERSION;
        my @cg      = ("cn=razor-agents&cv=$version\r\n");
        $self->_send( \@cg, 0, 1 );
    }

    if (   defined( $self->{s}->{greeting}->{srl} )
        && defined( $self->{s}->{conf}->{srl} )
        && $self->{s}->{greeting}->{srl} <= $self->{s}->{conf}->{srl} ) {

        $self->compute_server_conf;
        return 1;
    }

    # srl > our cached srl, request update  (a=g&pm=state)
    # and rediscover
    #

    my @queries = ("a=g&pm=state\r\n");

    my $response = $self->_send( \@queries ) or return $self->errprefix("parse_greeting");

    # should be just one response
    # from_batched_query wants "-" in beginning, but not ".\r\n" at end
    $response->[0] =~ s/\.\r\n$//sg;
    my $h = from_batched_query( $response->[0], {} );

    foreach my $href (@$h) {
        foreach ( sort keys %$href ) {
            $self->{s}->{conf}->{$_} = $href->{$_};

            #$self->log(8,"updated: $_=$href->{$_}");
        }
    }
    $self->log( 1, "Bad info while trying to get server state (a=g&pm=state)" )
      unless scalar(@$h);

    $self->{s}->{conf}->{srl} = $self->{s}->{greeting}->{srl};
    push @{ $self->{s}->{modified} }, $self->{s}->{ip};
    $self->{s}->{allconfs}->{ $self->{s}->{ip} } = $self->{s}->{conf};    # in case new server

    # now we're up to date
    $self->log( 5, "Updated to new server state srl " . $self->{s}->{conf}->{srl} . " for server " . $self->{s}->{ip} );

    $self->compute_server_conf();
    $self->writeservers;                                                  # writes to disk servers listed in $self->{s}->{modified}

    $self->log( 5, "srl was updated, forcing discovery ..." );
    $self->{done_discovery}  = 0;
    $self->{force_discovery} = 1;
    $self->discover();

    return 1;
}

# Returns engines supported
#
# can be called with no paramaters or
# with hash of server supported engines
sub compute_supported_engines {
    my ( $self, $orig ) = @_;

    my %all;
    my $se = $self->supported_engines();    # local supported engines
    foreach ( @{ $self->{conf}->{use_engines} } ) {
        if ($orig) {
            $all{$_} = 1 if ( exists $se->{$_} ) && ( exists $orig->{$_} );
        }
        else {
            $all{$_} = 1 if exists $se->{$_};
        }
    }
    if ($orig) {
        $self->log( 8, "Computed supported_engines: " . join( ' ', sort( keys %all ) ) );
    }
    else {
        $self->log( 8, "Client supported_engines: " . join( ' ', sort( keys %all ) ) );
    }
    return \%all;
}

# called when we need to parse server conf
# - after initial parse_greeting
# - if state (srl) changes
# - when we switch to cached server conf info in nextserver
#
sub compute_server_conf {
    my ( $self, $cached ) = @_;

    #
    # compute a confindence (cf) from razor-agent.conf's 'min_cf'
    # and server's average confidence (ac)
    #
    # min_cf can be 'n', 'ac', 'ac + n', or 'ac - n'
    # where 'n' can be 1..100
    #
    my $cf     = $self->{s}->{conf}->{ac};    # default is server's ac
    my $min_cf = $self->{conf}->{min_cf};
    $min_cf =~ s/\s//g;

    if ( $min_cf =~ /^ac\+(\d+)$/ ) {
        $cf = $self->{s}->{conf}->{ac} + $1;

    }
    elsif ( $min_cf =~ /^ac-(\d+)$/ ) {
        $cf = $self->{s}->{conf}->{ac} - $1;

    }
    elsif ( $min_cf =~ /^ac$/ ) {
        $cf = $self->{s}->{conf}->{ac};

    }
    elsif ( $min_cf =~ /^(\d+)$/ ) {
        $cf = $min_cf;
    }
    else {
        $self->log( 5, "Invalid min_cf $self->{conf}->{min_cf}" );
    }
    $cf = 100 if $cf > 100;
    $cf = 0   if $cf < 0;
    $self->{s}->{min_cf} = $cf;

    #
    # ep4 - special for vr4
    #
    $self->{s}->{conf}->{ep4} = $self->{s}->{greeting}->{ep4}
      if $self->{s}->{greeting}->{ep4};

    my $info = $cached ? $self->{s}->{conf} : $self->{s}->{greeting};
    my $name = "Unknown-Type: ";
    if ( $info->{sn} ) {
        $name .= $info->{sn};
        $name = "Nomination" if $info->{sn} =~ /N/;
        $name = "Catalogue"  if $info->{sn} =~ /C/;
        $name = "Catalogue"  if $info->{sn} =~ /S/;
        $name = "Discovery"  if $info->{sn} =~ /D/;
    }
    $self->log( 6, $self->{s}->{ip} . " is a $name Server srl " . $self->{s}->{conf}->{srl} . "; computed min_cf=$cf, Server se: $self->{s}->{conf}->{se}" );

    #
    # Supported Engines - greeting contains hex of bits
    # we turn into a hash so we can just quickly do
    # do_eng3_stuff if $self->{s}->{engines}->{3};
    #

    # if we're just computing hashes locally, ignore what engines server currently supports
    # fixme - this prolly should be done somewhere else
    if ( $self->{opt}->{printhash} ) {
        $self->log( 6, "Ignore what engines server supports for -H" );
        $self->{s}->{engines} = $self->compute_supported_engines();
    }
    else {
        my $se = hexbits2hash( $self->{s}->{conf}->{se} );
        $self->{s}->{engines} = $self->compute_supported_engines($se);
    }
}

# sub log2file moved to Agent.pm

sub debug {
    my ( $self, $message ) = @_;
    $self->log( 5, $message );
}

sub DESTROY {
    my $self = shift;

    #$self->debug ("Agent terminated");
}

sub zonename {
    my ( $zone, $type ) = @_;
    my ( $sub, $dom ) = split /\./, $zone, 2;
    return "$sub-$type.$dom";
}

1;


Youez - 2016 - github.com/yon3zu
LinuXploit