@ -1,4 +1,4 @@
# -*- coding: utf-8 -*-
# -*- coding: utf-8 -*-
# Copyright 2012-2016 Mir Calculate. http://www.calculate-linux.org
#
@ -20,12 +20,16 @@ from sudsds.transport import TransportError
from client_class import Client_suds
import traceback as tb
import time , logging
import os , sys
import threading , urllib2
import time
import logging
import os
import sys
import threading
import urllib2
from pid_information import client_list_methods
from cert_func import client_post_auth , client_post_request , client_get_cert , \
client_post_cert , get_password_from_daemon , clear_password
from cert_func import ( client_post_auth , client_post_request , client_get_cert ,
client_post_cert , get_password_from_daemon ,
clear_password )
from sid_func import session_clean , client_session_info , client_session_list
from cert_verify import get_CRL , VerifyError
@ -33,11 +37,13 @@ import M2Crypto, OpenSSL
from calculate . core . datavars import DataVarsCore
from client_class import HTTPSClientCertTransport
from methods_func import call_method , get_method_argparser , parse , get_view
from function import MessageReceiver , MessageDispatcher , clear , _print , \
get_view_params
from function import ( MessageReceiver , MessageDispatcher , clear , _print ,
get_view_params )
from calculate . lib . utils . files import makeDirectory , readLinesFile
from calculate . lib . cl_lang import setLocalTranslate
setLocalTranslate ( ' cl_console3 ' , sys . modules [ __name__ ] )
_ = lambda x : x
setLocalTranslate ( ' cl_console3 ' , sys . modules [ __name__ ] )
def get_message_receiver ( client ) :
@ -48,6 +54,7 @@ def get_entire_message_receiver(client, pid):
return MessageReceiver . from_entire ( client , pid ,
MessageDispatcher ( Methods ( ) ) , Display ( ) )
def client_signal ( client ) :
Vars = DataVarsCore ( )
Vars . importCore ( )
@ -57,7 +64,7 @@ def client_signal(client):
except :
client_active = 15
while True :
if os . path . exists ( client . SID_FILE ) :
if os . path . exists ( client . SID_FILE ) :
fi = open ( client . SID_FILE , ' r ' )
temp = fi . read ( )
fi . close ( )
@ -71,6 +78,7 @@ def client_signal(client):
raise Exception ( 1 )
time . sleep ( float ( client_active ) )
class StoppableThread ( threading . Thread ) :
def __init__ ( self ) :
super ( StoppableThread , self ) . __init__ ( )
@ -79,8 +87,7 @@ class StoppableThread(threading.Thread):
self . _paused = threading . Event ( )
def run ( self ) :
l = [ ' | ' , ' / ' , ' - ' , ' \\ ' , ' | ' , ' / ' , ' - ' , ' \\ ' ]
i = 0
l = [ ' | ' , ' / ' , ' - ' , ' \\ ' , ' | ' , ' / ' , ' - ' , ' \\ ' ]
while True :
for i in l :
sys . stdout . write ( " \r \r " + i )
@ -93,7 +100,6 @@ class StoppableThread(threading.Thread):
sys . stdout . flush ( )
return 0
def pause ( self ) :
self . _pause . set ( )
while not self . _paused . is_set ( ) :
@ -113,6 +119,7 @@ class StoppableThread(threading.Thread):
def paused ( self ) :
return self . _pause . isSet ( )
def connect_with_cert ( cert , path_to_cert , url , args , wait_thread , clVarsCore ,
crypto_Error , Connect_Error ) :
flag_thread_start = False
@ -121,41 +128,44 @@ def connect_with_cert(cert, path_to_cert, url, args, wait_thread, clVarsCore,
CERT_KEY = os . path . join ( path_to_cert , cert_name + ' .key ' )
if not os . path . isfile ( CERT_FILE ) or not os . path . isfile ( CERT_KEY ) :
Connect_Error = 1
return ( None , 1 , crypto_Error , False , None )
return None , 1 , crypto_Error , False , None
client = None
bio = M2Crypto . BIO . openfile ( CERT_KEY )
rsa = M2Crypto . m2 . rsa_read_key ( bio . _ptr ( ) , lambda * unused : None )
rsa = M2Crypto . m2 . rsa_read_key ( bio . _ptr ( ) , lambda * unused : None )
store_passwd = None
if not rsa :
port = args . port or clVarsCore . Get ( ' core.cl_core_port ' )
store_passwd = get_password_from_daemon ( args . host , args . port ,
wait_thread )
if ' store_passwd ' in locals ( ) :
key_passwd = store_passwd
else :
key_passwd = None
store_passwd = get_password_from_daemon ( args . host , port , wait_thread )
key_passwd = store_passwd
er = None
try :
ca_certs = os . path . join ( path_to_cert , ' ca/ca_root.crt ' )
client = Client_suds ( url , transport = HTTPSClientCertTransport \
( CERT_KEY , CERT_FILE , path_to_cert , password = key_passwd ,
ca_certs = ca_certs , wait_thread = wait_thread ) )
client = Client_suds (
url , transport = HTTPSClientCertTransport (
CERT_KEY , CERT_FILE , path_to_cert , password = key_passwd ,
ca_certs = ca_certs , wait_thread = wait_thread ) )
if not wait_thread . isAlive ( ) :
wait_thread = StoppableThread ( )
flag_thread_start = True
wait_thread . start ( )
client . wsdl . services [ 0 ] . setlocation ( url )
client . set_parameters ( path_to_cert , CERT_FILE , CERT_KEY )
client . set_parameters ( path_to_cert , CERT_FILE , CERT_KEY )
wait_thread . stop ( )
client_post_cert ( client , clVarsCore )
Connect_Error = 0
except VerifyError , e :
Connect_Error = 1
er = e
except OpenSSL . crypto . Error , e :
Connect_Error = 1
crypto_Error = 1
er = e
except urllib2 . URLError , e :
Connect_Error = 1
except Exception , e :
er = e
except Exception as e :
er = e
if e . message == 3 :
wait_thread . stop ( )
sys . exit ( 1 )
@ -163,8 +173,8 @@ def connect_with_cert(cert, path_to_cert, url, args, wait_thread, clVarsCore,
if flag_thread_start :
wait_thread . stop ( )
return ( client , Connect_Error , crypto_Error ,
True if ' store_passwd ' in locals ( ) else False ,
e if ' e ' in locals ( ) else None )
True if store_passwd is not None else False , er )
def get_server_hostname ( host , path_to_cert ) :
compliance_file = os . path . join ( path_to_cert , ' compliance_server_names ' )
@ -172,11 +182,12 @@ def get_server_hostname(host, path_to_cert):
fd = open ( compliance_file , ' w ' )
fd . close ( )
for line in readLinesFile ( compliance_file ) :
adress , server_hostname = line . split ( ' ' , 1 )
adress , server_hostname = line . split ( ' ' , 1 )
if adress == host :
return server_hostname
return None
def add_server_hostname ( host , path_to_cert , server_hostname ) :
try :
compliance_file = os . path . join ( path_to_cert , ' compliance_server_names ' )
@ -186,14 +197,14 @@ def add_server_hostname(host, path_to_cert, server_hostname):
temp_file = ' '
find_flag = False
for line in readLinesFile ( compliance_file ) :
adress , temp_server_hostname = line . split ( ' ' , 1 )
adress , temp_server_hostname = line . split ( ' ' , 1 )
if adress == host :
temp_file + = " %s %s \n " % ( adress , server_hostname )
temp_file + = " %s %s \n " % ( adress , server_hostname )
find_flag = True
else :
temp_file + = line + ' \n '
temp_file + = line + ' \n '
if not find_flag :
temp_file + = " %s %s \n " % ( host , server_hostname )
temp_file + = " %s %s \n " % ( host , server_hostname )
fd = open ( compliance_file , ' w ' )
fd . write ( temp_file )
fd . close ( )
@ -202,23 +213,10 @@ def add_server_hostname(host, path_to_cert, server_hostname):
print e
return False
def https_server ( client , args , unknown_args , url , clVarsCore , wait_thread ) :
client_post_auth ( client )
# sym_link = os.path.basename(sys.argv[0])
# if sym_link != 'cl-console':
# wait_thread.stop()
# results = client.service.get_methods(client.sid, 'console')
# find_flag = False
# if hasattr (results, 'stringArray'):
# for _array in results.stringArray:
# if _array.string[0] == sym_link:
# args.method = _array.string[1]
# find_flag = True
# break
# if not find_flag:
# _print (_('Method not found for %s') %sym_link)
if args . stop_consoled :
wait_thread . stop ( )
os . system ( ' cl-consoled --stop ' )
@ -242,9 +240,11 @@ def https_server(client, args, unknown_args, url, clVarsCore, wait_thread):
wait_thread . stop ( )
if args . dump :
from pid_information import client_pid_info
client_pid_info ( client )
else :
from pid_information import client_list_pid
client_list_pid ( client )
return 0
@ -267,6 +267,7 @@ def https_server(client, args, unknown_args, url, clVarsCore, wait_thread):
if args . pid_kill :
wait_thread . stop ( )
from pid_information import client_pid_kill
return client_pid_kill ( client , args . pid_kill )
retCode = 0
@ -289,7 +290,8 @@ def https_server(client, args, unknown_args, url, clVarsCore, wait_thread):
_unknown_args = method_parser . fixBoolVariables ( unknown_args )
_args , _unknown_args = method_parser . parse_known_args ( _unknown_args )
_args , _unknown_args = method_parser . parse_known_args (
_unknown_args )
if dispatch_usenew == _args . no_questions :
method_parser . print_help ( )
@ -308,16 +310,16 @@ def https_server(client, args, unknown_args, url, clVarsCore, wait_thread):
client . no_progress = args . no_progress
try :
mr . analysis ( method_result )
# analysis(client, client.sid, method_result)
# analysis(client, client.sid, method_result)
except urllib2 . URLError , e :
_print ( e )
_print ( e )
except KeyboardInterrupt :
try :
print
mess = method_result [ 0 ] [ 0 ]
pid = int ( mess . message )
result = client . service . pid_kill ( pid , client . sid )
if result in [ 0 , 2 ] :
if result in [ 0 , 2 ] :
print _ ( ' Process terminated ' )
elif result == - 1 :
print _ ( " Certificate not found on the server " )
@ -334,14 +336,15 @@ def https_server(client, args, unknown_args, url, clVarsCore, wait_thread):
pid = int ( mess . message )
except :
return 1
retCode = \
1 if int ( client . service . pid_info ( client . sid , pid ) [ 0 ] [ 1 ] ) else 0
retCode = 1 if int (
client . service . pid_info ( client . sid , pid ) [ 0 ] [ 1 ] ) else 0
if not args . keep_result :
client . service . clear_pid_cache ( client . sid , pid )
client . service . clear_method_cache ( client . sid , args . method )
wait_thread . stop ( )
return retCode
def main ( wait_thread ) :
parser = parse ( )
args , unknown_args = parser . parse_known_args ( )
@ -361,6 +364,7 @@ def main(wait_thread):
cl_wsdl = ob . Get ( ' cl_wsdl_available ' )
# создать симлинки на команды
from calculate . core . server . func import initialization
initialization ( cl_wsdl )
ob . close ( )
if sym_link in loaded_methods . LoadedMethods . conMethods . keys ( ) :
@ -398,7 +402,7 @@ def main(wait_thread):
path_to_cert = args . path_to_cert
if not path_to_cert :
path_to_cert = clVarsCore . Get ( ' core.cl_client_cert_dir ' )
path_to_cert = path_to_cert . replace ( " ~ " , homePath )
path_to_cert = path_to_cert . replace ( " ~ " , homePath )
for dirs in [ ' ' , ' ca ' , ' trusted ' ] :
dir_path = os . path . join ( path_to_cert , dirs )
@ -407,12 +411,12 @@ def main(wait_thread):
wait_thread . stop ( )
sys . stdout . write ( ' \r ' )
sys . stdout . flush ( )
print _ ( " Failed to create directory %s " ) % dir_path
print _ ( " Failed to create directory %s " ) % dir_path
return 1
if args . update_crl :
wait_thread . stop ( )
getCRL = threading . Thread ( target = get_CRL , args = ( path_to_cert , ) )
getCRL = threading . Thread ( target = get_CRL , args = ( path_to_cert , ) )
getCRL . start ( )
getCRL . join ( )
print ' CRL updated '
@ -420,24 +424,26 @@ def main(wait_thread):
if args . by_host :
wait_thread . stop ( )
client_post_request ( path_to_cert , args )
client_post_request ( path_to_cert , args )
return 0
if args . from_host :
wait_thread . stop ( )
client_get_cert ( path_to_cert , args )
client_get_cert ( path_to_cert , args )
return 0
url = " https:// %s : %s /?wsdl " % ( host , port )
url = " https:// %s : %s /?wsdl " % ( host , port )
clear ( )
serv_hostname = get_server_hostname ( host , path_to_cert )
get_name_flag = False
client = None
if serv_hostname :
Connect_Error = 1
crypto_Error = 0
client , Connect_Error , crypto_Error , passwd_flag , e = \
connect_with_cert ( serv_hostname , path_to_cert , url , args ,
wait_thread , clVarsCore , crypto_Error , Connect_Error )
connect_with_cert ( serv_hostname , path_to_cert , url , args ,
wait_thread , clVarsCore , crypto_Error ,
Connect_Error )
if not wait_thread . isAlive ( ) :
wait_thread = StoppableThread ( )
wait_thread . start ( )
@ -457,62 +463,64 @@ def main(wait_thread):
get_name_flag = False
if get_name_flag :
try :
client . port = port
return_val = 1
if client :
try :
return_val = https_server ( client , args , unknown_args , url , \
clVarsCore , wait_thread )
except urllib2 . URLError , e :
print _ ( ' Error: ' ) , e
client . port = port
return_val = 1
try :
return_val = https_server ( client , args , unknown_args , url ,
clVarsCore , wait_thread )
except urllib2 . URLError , e :
print _ ( ' Error: ' ) , e
except KeyboardInterrupt :
wait_thread . stop ( )
red = ' \n ' + ' \033 [31m * \033 [0m '
print red + _ ( " Manually interrupted " )
except Exception , e :
wait_thread . stop ( )
if type ( e . message ) != int :
if e . message :
print e . message
elif e . args :
print e
return 1
wait_thread . stop ( )
return return_val
except WebFault , f :
print _ ( " Exception: %s " ) % f
_print ( f . fault )
except TransportError , te :
print _ ( " Exception: %s " ) % te
except KeyboardInterrupt :
wait_thread . stop ( )
red = ' \n ' + ' \033 [31m * \033 [0m '
red = ' \n ' + ' \033 [31m * \033 [0m '
print red + _ ( " Manually interrupted " )
except Exception , e :
wait_thread . stop ( )
if type ( e . message ) != int :
if e . message :
print e . message
elif e . args :
print e
# tb.print_exc()
return 1
wait_thread . stop ( )
return return_val
except WebFault , f :
print _ ( " Exception: %s " ) % f
_print ( f . fault )
except TransportError , te :
print _ ( " Exception: %s " ) % te
except KeyboardInterrupt :
wait_thread . stop ( )
red = ' \n ' + ' \033 [31m * \033 [0m '
print red + _ ( " Manually interrupted " )
except Exception , e :
print _ ( " Exception: %s " ) % e
tb . print_exc ( )
print _ ( " Exception: %s " ) % e
tb . print_exc ( )
wait_thread . stop ( )
server_host_name = " "
try :
client = Client_suds ( url , \
transport = HTTPSClientCertTransport ( None , None , path_to_cert ) )
client = Client_suds (
url , transport = HTTPSClientCertTransport ( None , None , path_to_cert ) )
client . wsdl . services [ 0 ] . setlocation ( url )
server_host_name = client . service . get_server_host_name ( )
if not add_server_hostname ( host , path_to_cert , server_host_name ) :
print ' compliance_file write error! '
del ( client )
del client
except urllib2 . URLError , e :
wait_thread . stop ( )
print ' \b ' + _ ( ' Failed to connect ' ) + ' : ' , e
print ' \b ' + _ ( ' Failed to connect ' ) + ' : ' , e
return 1
except KeyboardInterrupt :
wait_thread . stop ( )
red = ' \n ' + ' \033 [31m * \033 [0m '
red = ' \n ' + ' \033 [31m * \033 [0m '
print red + _ ( " Manually interrupted " )
try :
import glob
all_cert_list = glob . glob ( os . path . join ( path_to_cert , ' *.crt ' ) )
fit_cert_list = [ ]
for client_cert_path in all_cert_list :
@ -520,16 +528,18 @@ def main(wait_thread):
client_cert_name = client_cert . replace ( ' .crt ' , ' ' )
if server_host_name . endswith ( client_cert_name ) :
fit_cert_list . append ( client_cert_name )
fit_cert_list . sort ( key = len )
fit_cert_list . sort ( key = len )
Connect_Error = 1
crypto_Error = 0
e = None
passwd_flag = False
for i in range ( 0 , len ( fit_cert_list ) ) :
for i in range ( 0 , len ( fit_cert_list ) ) :
cert_name = fit_cert_list . pop ( )
client , Connect_Error , crypto_Error , passwd_flag , e = \
connect_with_cert ( cert_name , path_to_cert , url , args ,
wait_thread , clVarsCore , crypto_Error , Connect_Error )
connect_with_cert ( cert_name , path_to_cert , url , args ,
wait_thread , clVarsCore , crypto_Error ,
Connect_Error )
if not wait_thread . isAlive ( ) :
wait_thread = StoppableThread ( )
wait_thread . start ( )
@ -537,7 +547,7 @@ def main(wait_thread):
if Connect_Error == 0 :
break
# If the certificate file misses
# If the certificate file misses
if Connect_Error :
if crypto_Error and passwd_flag :
wait_thread . stop ( )
@ -555,21 +565,22 @@ def main(wait_thread):
CERT_FILE = None
CERT_KEY = None
client = Client_suds ( url , transport = HTTPSClientCertTransport \
( CERT_KEY , CERT_FILE , path_to_cert ) )
client = Client_suds (
url , transport = HTTPSClientCertTransport ( CERT_KEY , CERT_FILE ,
path_to_cert ) )
client . wsdl . services [ 0 ] . setlocation ( url )
client . set_parameters ( path_to_cert , CERT_FILE , CERT_KEY )
client . set_parameters ( path_to_cert , CERT_FILE , CERT_KEY )
client . port = port
return_val = 1
try :
return_val = https_server ( client , args , unknown_args , url , \
return_val = https_server ( client , args , unknown_args , url ,
clVarsCore , wait_thread )
except urllib2 . URLError , e :
print _ ( ' Error: ' ) , e
except KeyboardInterrupt :
wait_thread . stop ( )
red = ' \n ' + ' \033 [31m * \033 [0m '
red = ' \n ' + ' \033 [31m * \033 [0m '
print red + _ ( " Manually interrupted " )
except Exception , e :
wait_thread . stop ( )
@ -578,21 +589,21 @@ def main(wait_thread):
print e . message
elif e . args :
print e
# tb.print_exc()
# tb.print_exc()
return 1
wait_thread . stop ( )
return return_val
# ----------------------------------------------------
# ----------------------------------------------------
except WebFault , f :
print _ ( " Exception: %s " ) % f
_print ( f . fault )
print _ ( " Exception: %s " ) % f
_print ( f . fault )
except TransportError , te :
print _ ( " Exception: %s " ) % te
print _ ( " Exception: %s " ) % te
except KeyboardInterrupt :
wait_thread . stop ( )
red = ' \n ' + ' \033 [31m * \033 [0m '
red = ' \n ' + ' \033 [31m * \033 [0m '
print red + _ ( " Manually interrupted " )
except Exception , e :
print _ ( " Exception: %s " ) % e
print _ ( " Exception: %s " ) % e
tb . print_exc ( )
wait_thread . stop ( )