From d66ba9d6c6c919175a5a5a401fa73ba2204fe271 Mon Sep 17 00:00:00 2001 From: zheng Date: Wed, 11 May 2016 11:22:08 +0800 Subject: [PATCH 1/4] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E4=B8=BB=E6=9C=BA?= =?UTF-8?q?=E7=BB=84=E7=BC=96=E8=BE=91=E6=97=B6=E5=9B=9E=E8=BD=A6=E5=AF=BC?= =?UTF-8?q?=E8=87=B4=E4=B8=BB=E6=9C=BA=E4=B8=A2=E5=A4=B1=E9=97=AE=E9=A2=98?= =?UTF-8?q?=20(#230)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 在主机组编辑页面,如果直接执行回车会导致主机组中主机信息丢失。 本修复方法是关闭回车提交 --- templates/jasset/group_edit.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/templates/jasset/group_edit.html b/templates/jasset/group_edit.html index 249db163d..f99e5f50c 100644 --- a/templates/jasset/group_edit.html +++ b/templates/jasset/group_edit.html @@ -57,7 +57,7 @@ {% if smg %}
{{ smg }}
{% endif %} -
+
From 93e08a6e29a4f1b382b7c2510ee3a7ece7bef627 Mon Sep 17 00:00:00 2001 From: ibuler Date: Wed, 11 May 2016 17:41:46 +0800 Subject: [PATCH 2/4] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=88=9B=E5=BB=BAtty?= =?UTF-8?q?=E6=97=A5=E5=BF=97=E6=96=87=E4=BB=B6=E5=A4=B1=E8=B4=A5,=20?= =?UTF-8?q?=E8=AF=B7=E4=BF=AE=E6=94=B9=E7=9B=AE=E5=BD=95=20bug=20(#231)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix(api) 修改建立目录的bug 使用bash代替python完成建立777目录的功能 * fix passwd input * fix(mkdir) 修改mkdirs策略 修改原来导致的bug * fix passwd input (#232) 修复记录敏感密码bug * fix passwd input * fix passwd input --- connect.py | 59 +++++++++++++++++++---------------------------- jperm/utils.py | 2 +- jumpserver/api.py | 11 ++++----- juser/user_api.py | 4 ++-- 4 files changed, 32 insertions(+), 44 deletions(-) diff --git a/connect.py b/connect.py index 8e138c6c5..f0f8effd3 100755 --- a/connect.py +++ b/connect.py @@ -93,9 +93,7 @@ class Tty(object): self.remote_ip = '' self.login_type = login_type self.vim_flag = False - self.vim_end_flag = False self.vim_end_pattern = re.compile(r'\x1b\[\?1049', re.X) - self.vim_pattern = re.compile(r'\W?vi[m]?\s.* | \W?fg\s.*', re.X) self.vim_data = '' self.stream = None self.screen = None @@ -117,7 +115,8 @@ class Tty(object): return True return False - def command_parser(self, command): + @staticmethod + def command_parser(command): """ 处理命令中如果有ps1或者mysql的特殊情况,极端情况下会有ps1和mysql :param command:要处理的字符传 @@ -157,14 +156,10 @@ class Tty(object): else: command = line_data break - if command != '': - # 判断用户输入的是否是vim 或者fg命令 - if self.vim_pattern.search(command): - self.vim_flag = True - # 虚拟屏幕清空 - self.screen.reset() except Exception: pass + # 虚拟屏幕清空 + self.screen.reset() return command def get_log(self): @@ -180,8 +175,8 @@ class Tty(object): log_file_path = os.path.join(today_connect_log_dir, '%s_%s_%s' % (self.username, self.asset_name, time_start)) try: - mkdir(os.path.dirname(today_connect_log_dir), mode=0777) - mkdir(today_connect_log_dir, mode=0777) + mkdir(os.path.dirname(today_connect_log_dir), mode=777) + mkdir(today_connect_log_dir, mode=777) except OSError: logger.debug('创建目录 %s 失败,请修改%s目录权限' % (today_connect_log_dir, tty_log_dir)) raise ServerError('创建目录 %s 失败,请修改%s目录权限' % (today_connect_log_dir, tty_log_dir)) @@ -305,7 +300,6 @@ class SshTty(Tty): old_tty = termios.tcgetattr(sys.stdin) pre_timestamp = time.time() data = '' - input_str = '' input_mode = False try: tty.setraw(sys.stdin.fileno()) @@ -325,8 +319,7 @@ class SshTty(Tty): x = self.channel.recv(10240) if len(x) == 0: break - if self.vim_flag: - self.vim_data += x + index = 0 len_x = len(x) while index < len_x: @@ -347,11 +340,10 @@ class SshTty(Tty): pre_timestamp = now_timestamp log_file_f.flush() - if input_mode and not self.is_output(x): + self.vim_data += x + if input_mode: data += x - input_str = '' - except socket.timeout: pass @@ -362,25 +354,22 @@ class SshTty(Tty): pass termlog.recoder = True input_mode = True - input_str += x - if str(x) in ['\r', '\n', '\r\n']: - # 这个是用来处理用户的复制操作 - if input_str != x: - data += input_str - if self.vim_flag: - match = self.vim_end_pattern.findall(self.vim_data) - if match: - if self.vim_end_flag or len(match) == 2: - self.vim_flag = False - self.vim_end_flag = False - else: - self.vim_end_flag = True - else: + if self.is_output(str(x)): + # 如果len(str(x)) > 1 说明是复制输入的 + if len(str(x)) > 1: + data = x + match = self.vim_end_pattern.findall(self.vim_data) + if match: + if self.vim_flag or len(match) == 2: + self.vim_flag = False + else: + self.vim_flag = True + elif not self.vim_flag: + self.vim_flag = False data = self.deal_command(data)[0:200] - if len(data) > 0: + if data is not None: TtyLog(log=log, datetime=datetime.datetime.now(), cmd=data).save() data = '' - input_str = '' self.vim_data = '' input_mode = False @@ -406,7 +395,7 @@ class SshTty(Tty): """ # 发起ssh连接请求 Make a ssh connection ssh = self.get_connection() - + transport = ssh.get_transport() transport.set_keepalive(30) transport.use_compression(True) @@ -422,7 +411,7 @@ class SshTty(Tty): signal.signal(signal.SIGWINCH, self.set_win_size) except: pass - + self.posix_shell() # Shutdown channel socket diff --git a/jperm/utils.py b/jperm/utils.py index d011125c3..1e4966852 100644 --- a/jperm/utils.py +++ b/jperm/utils.py @@ -42,7 +42,7 @@ def gen_keys(key="", key_path_dir=""): key_path_dir = os.path.join(KEY_DIR, 'role_key', key_basename) private_key = os.path.join(key_path_dir, 'id_rsa') public_key = os.path.join(key_path_dir, 'id_rsa.pub') - mkdir(key_path_dir, mode=0755) + mkdir(key_path_dir, mode=755) if not key: key = RSAKey.generate(2048) key.write_private_key_file(private_key) diff --git a/jumpserver/api.py b/jumpserver/api.py index a4840f206..ca1972532 100644 --- a/jumpserver/api.py +++ b/jumpserver/api.py @@ -91,7 +91,7 @@ def get_role_key(user, role): """ user_role_key_dir = os.path.join(KEY_DIR, 'user') user_role_key_path = os.path.join(user_role_key_dir, '%s_%s.pem' % (user.username, role.name)) - mkdir(user_role_key_dir, mode=0777) + mkdir(user_role_key_dir, mode=777) if not os.path.isfile(user_role_key_path): with open(os.path.join(role.key_path, 'id_rsa')) as fk: with open(user_role_key_path, 'w') as fu: @@ -458,14 +458,13 @@ def bash(cmd): return subprocess.call(cmd, shell=True) -def mkdir(dir_name, username='', mode=0755): +def mkdir(dir_name, username='', mode=755): """ insure the dir exist and mode ok 目录存在,如果不存在就建立,并且权限正确 """ - if not os.path.isdir(dir_name): - os.makedirs(dir_name) - os.chmod(dir_name, mode) + cmd = '[ ! -d %s ] && mkdir -p %s && chmod %s %s' % (dir_name, dir_name, mode, dir_name) + bash(cmd) if username: chown(dir_name, username) @@ -486,7 +485,7 @@ def my_render(template, data, request): def get_tmp_dir(): seed = uuid.uuid4().hex[:4] dir_name = os.path.join('/tmp', '%s-%s' % (datetime.datetime.now().strftime('%Y%m%d-%H%M%S'), seed)) - mkdir(dir_name, mode=0777) + mkdir(dir_name, mode=777) return dir_name diff --git a/juser/user_api.py b/juser/user_api.py index 03ed65938..9ad22d25e 100644 --- a/juser/user_api.py +++ b/juser/user_api.py @@ -130,14 +130,14 @@ def gen_ssh_key(username, password='', """ logger.debug('生成ssh key, 并设置authorized_keys') private_key_file = os.path.join(key_dir, username+'.pem') - mkdir(key_dir, mode=0777) + mkdir(key_dir, mode=777) if os.path.isfile(private_key_file): os.unlink(private_key_file) ret = bash('echo -e "y\n"|ssh-keygen -t rsa -f %s -b %s -P "%s"' % (private_key_file, length, password)) if authorized_keys: auth_key_dir = os.path.join(home, username, '.ssh') - mkdir(auth_key_dir, username=username, mode=0700) + mkdir(auth_key_dir, username=username, mode=700) authorized_key_file = os.path.join(auth_key_dir, 'authorized_keys') with open(private_key_file+'.pub') as pub_f: with open(authorized_key_file, 'w') as auth_f: From 32ab8a1646724cf2cdef94d4eabdda7cff95b7dd Mon Sep 17 00:00:00 2001 From: ibuler Date: Wed, 11 May 2016 18:48:38 +0800 Subject: [PATCH 4/4] =?UTF-8?q?=E5=AE=8C=E7=BE=8E=E4=BF=AE=E5=A4=8Dvim?= =?UTF-8?q?=E7=AD=89=E4=BA=A4=E4=BA=92=E5=BC=8F=E5=91=BD=E4=BB=A4=E8=AE=B0?= =?UTF-8?q?=E5=BD=95=20(#236)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix(api) 修改建立目录的bug 使用bash代替python完成建立777目录的功能 * fix passwd input * fix(mkdir) 修改mkdirs策略 修改原来导致的bug * fix passwd input (#232) 修复记录敏感密码bug * fix passwd input * fix passwd input * fix passwd input --- run_server.py | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/run_server.py b/run_server.py index 416c86f70..51c080529 100755 --- a/run_server.py +++ b/run_server.py @@ -373,15 +373,13 @@ class WebTerminalHandler(tornado.websocket.WebSocketHandler): self.termlog.recoder = True self.term.input_mode = True if str(jsondata['data']) in ['\r', '\n', '\r\n']: - if self.term.vim_flag: - match = re.compile(r'\x1b\[\?1049', re.X).findall(self.term.vim_data) - if match: - if self.term.vim_end_flag or len(match) == 2: - self.term.vim_flag = False - self.term.vim_end_flag = False - else: - self.term.vim_end_flag = True - else: + match = re.compile(r'\x1b\[\?1049', re.X).findall(self.term.vim_data) + if match: + if self.term.vim_flag or len(match) == 2: + self.term.vim_flag = False + else: + self.term.vim_flag = True + elif not self.term.vim_flag: result = self.term.deal_command(self.term.data)[0:200] if len(result) > 0: TtyLog(log=self.log, datetime=datetime.datetime.now(), cmd=result).save() @@ -424,8 +422,7 @@ class WebTerminalHandler(tornado.websocket.WebSocketHandler): if not len(recv): return data += recv - if self.term.vim_flag: - self.term.vim_data += recv + self.term.vim_data += recv try: self.write_message(data.decode('utf-8', 'replace')) self.termlog.write(data) @@ -436,7 +433,7 @@ class WebTerminalHandler(tornado.websocket.WebSocketHandler): pre_timestamp = now_timestamp self.log_file_f.flush() self.log_time_f.flush() - if self.term.input_mode and not self.term.is_output(data): + if self.term.input_mode: self.term.data += data data = '' except UnicodeDecodeError: