#!/usr/bin/python #-*- coding: utf-8 -*- # Copyright 2012 Calculate Ltd. http://www.calculate-linux.org # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from sudsds import WebFault from sudsds.transport import TransportError from client_class import Client_suds import traceback as tb import time, logging import os, sys import threading, urllib2 from function import analysis, clear, get_entire_frame 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 sid_func import session_clean, client_session_info, client_session_list from cert_verify import get_CRL, VerifyError import M2Crypto, OpenSSL from calculate.core.datavars import DataVarsCore from client_class import HTTPSClientCertTransport from methods_func import call_method, get_method_argparser, parse from calculate.lib.utils.files import makeDirectory from calculate.lib.cl_lang import setLocalTranslate setLocalTranslate('calculate_console',sys.modules[__name__]) def client_signal(client): Vars = DataVarsCore() Vars.importCore() Vars.flIniFile() try: client_active = Vars.Get('cl_core_client_active_period') except: client_active = 15 while True: if os.path.exists(client.SID_FILE) : fi = open(client.SID_FILE, 'r') temp = fi.read() fi.close() sid = int(temp) else: sid = 0 try: client.service.active_client(sid) except: print _('no connection to server!') raise Exception(1) time.sleep(float(client_active)) def https_server(client, args, unknown_args, url, clVarsCore, wait_thread): client_post_auth(client) if args.stop_consoled: wait_thread.stop() os.system('cl-consoled --stop') return 0 if args.session_clean: wait_thread.stop() session_clean(client) if args.session_info or args.session_num_info: wait_thread.stop() client_session_info(client, args.session_num_info) if args.session_list: wait_thread.stop() client_session_list(client) if args.list_pid: 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) if args.pid_res: wait_thread.stop() get_entire_frame(client, args.pid_res) return 0 if args.pid_kill: wait_thread.stop() from pid_information import client_pid_kill return client_pid_kill(client, args.pid_kill) if not args.method: wait_thread.stop() client_list_methods(client) return 1 elif args.method and args.help: # now = datetime.datetime.now() # print '6 ===> %ds %dms' %(now.second, now.microsecond) method_parser, view = get_method_argparser(client, args) wait_thread.stop() method_parser.print_help() # now = datetime.datetime.now() # print '7 ===> %ds %dms' %(now.second, now.microsecond) else: try: client.frame_period = clVarsCore.Get('cl_core_get_frame_period') except: client.frame_period = 2 method_result = call_method(client, args, wait_thread) if method_result: try: analysis(client, client.sid, method_result) except urllib2.URLError, 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]: print _('Process is terminated') elif result == -1: print _("Certificate not found in server database") elif result == -2: print _("Session doesn't belong to your certificate") elif result == 1: print _("It was not possible to kill process") # get_entire_frame(client, pid) analysis(client, client.sid, method_result) except Exception, e: _print (e.message) try: mess = method_result[0][0] pid = int(mess.message) except: return 1 client.service.clear_pid_cache(client.sid, pid) client.service.clear_method_cache(client.sid, args.method) wait_thread.stop() return 0 class StoppableThread(threading.Thread): """Thread class with a stop() method. The thread itself has to check regularly for the stopped() condition.""" def __init__(self): super(StoppableThread, self).__init__() self._stop = threading.Event() def run(self): l = ['|','/','-','\\','|','/','-','\\'] i = 0 while True: for i in l: sys.stdout.write("\r\r" + i) sys.stdout.flush() time.sleep(.02) if self.stopped(): sys.stdout.write("\b") sys.stdout.flush() return 0 def stop(self): self._stop.set() def stopped(self): return self._stop.isSet() def main(): # now = datetime.datetime.now() # print '1 ===> %ds %dms' %(now.second, now.microsecond) parser = parse() args, unknown_args = parser.parse_known_args() wait_thread = StoppableThread() wait_thread.start() if not args.method and args.help: wait_thread.stop() sys.stdout.write('\r') sys.stdout.flush() parser.print_help() # now = datetime.datetime.now() # print '1/2 ===> %ds %dms' %(now.second, now.microsecond) return 0 if not args.method: if unknown_args: wait_thread.stop() sys.stdout.write('\r') sys.stdout.flush() args = parser.parse_args() logging.basicConfig(level=logging.FATAL) logging.getLogger('sudsds.client').setLevel(logging.FATAL) logging.getLogger('sudsds.transport').setLevel(logging.FATAL) logging.getLogger('sudsds.transport.http').setLevel(logging.FATAL) logging.getLogger('sudsds.umx.typed').setLevel(logging.ERROR) clVarsCore = DataVarsCore() clVarsCore.importCore() clVarsCore.flIniFile() homePath = clVarsCore.Get('ur_home_path') # translate # if args.lang: # print 'code not found!!!!' port = args.port host = args.host path_to_cert = args.path_to_cert if not path_to_cert: path_to_cert = clVarsCore.Get('cl_client_cert_dir') path_to_cert = path_to_cert.replace("~",homePath) for dirs in ['', 'ca', 'trusted']: dir_path = os.path.join(path_to_cert, dirs) if not os.path.isdir(dir_path): if not makeDirectory(dir_path): wait_thread.stop() sys.stdout.write('\r') sys.stdout.flush() print _("cannot 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.start() getCRL.join() print 'GRL updated' return 0 if args.by_host: wait_thread.stop() client_post_request (path_to_cert, args) return 0 if args.from_host: wait_thread.stop() client_get_cert (path_to_cert, args) return 0 url = "https://%s:%d/?wsdl" %(host, port) # print "url = %s" %url clear() try: # now = datetime.datetime.now() # print '2 ===> %ds %dms' %(now.second, now.microsecond) client = Client_suds(url, \ transport = HTTPSClientCertTransport(None,None, path_to_cert)) client.wsdl.services[0].setlocation(url) # now = datetime.datetime.now() # print '2/1 ===> %ds %dms' %(now.second, now.microsecond) server_host_name = client.service.get_server_host_name() # print server_host_name # now = datetime.datetime.now() # print '2/2 ===> %ds %dms' %(now.second, now.microsecond) del (client) except urllib2.URLError, e: wait_thread.stop() print _('Failed to connect')+':', e return 1 # server_host_name = 'dspiridonov.local.calculate.ru' 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: client_cert = client_cert_path.replace(path_to_cert, '') 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) Connect_Error = 1 crypto_Error = 0 # now = datetime.datetime.now() # print '3 ===> %ds %dms' %(now.second, now.microsecond) for i in range (0, len(fit_cert_list)): #print 'fit_cert_list = ',fit_cert_list cert_name = fit_cert_list.pop() CERT_FILE = path_to_cert + cert_name + '.crt' CERT_KEY = path_to_cert + cert_name + '.key' bio = M2Crypto.BIO.openfile(CERT_KEY) rsa = M2Crypto.m2.rsa_read_key(bio._ptr(),lambda *unused: None) if not rsa: store_passwd = get_password_from_daemon(host,port,wait_thread) if not wait_thread.isAlive(): wait_thread = StoppableThread() wait_thread.start() # if not rsa and not 'store_passwd' in locals(): # store_passwd = getpass.getpass(_('Password: ')) # if not store_passwd: # store_passwd = None if 'store_passwd' in locals(): key_passwd = store_passwd else: key_passwd = None try: client = Client_suds(url, transport=HTTPSClientCertTransport \ (CERT_KEY, CERT_FILE, path_to_cert, password=key_passwd, wait_thread = wait_thread)) if not wait_thread.isAlive(): wait_thread = StoppableThread() wait_thread.start() client.wsdl.services[0].setlocation(url) client.set_parameters (path_to_cert, CERT_FILE, CERT_KEY) client_post_cert(client, clVarsCore) Connect_Error = 0 except VerifyError, e: # print e.value Connect_Error = 1 except OpenSSL.crypto.Error, e: Connect_Error = 1 crypto_Error = 1 except urllib2.URLError, e: Connect_Error = 1 except Exception, e: Connect_Error = 1 if Connect_Error == 0: break # now = datetime.datetime.now() # print '4 ===> %ds %dms' %(now.second, now.microsecond) #If the certificate file misses if Connect_Error: if crypto_Error and 'store_passwd' in locals(): wait_thread.stop() print _('Password is invalid') # delete password from daemon list clear_password(host, port) return 1 if 'e' in locals(): wait_thread.stop() print _('Error: '), e return 1 # print 'Connect Error' CERT_FILE = None CERT_KEY = None 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.port = port # now = datetime.datetime.now() # print '5 ===> %ds %dms' %(now.second, now.microsecond) return_val = 1 try: return_val = https_server(client, args, unknown_args, url, \ clVarsCore, wait_thread) except urllib2.URLError, e: print _('Error: '), e except Exception, e: wait_thread.stop() if type(e.message) != int: if e.message: print e.message else: print e return 1 # now = datetime.datetime.now() # print 'END ===> %ds %dms' %(now.second, now.microsecond) wait_thread.stop() return return_val #---------------------------------------------------- except WebFault, f: print _("Exception: %s") %f _print (f.fault) except TransportError, te: print _("Exception: %s") %te except Exception, e: print _("Exception: %s") %e tb.print_exc() wait_thread.stop()