3 ** Copyright (C) 2004-2016 Victor Kirhenshtein
5 ** This program is free software; you can redistribute it and/or modify
6 ** it under the terms of the GNU General Public License as published by
7 ** the Free Software Foundation; either version 2 of the License, or
8 ** (at your option) any later version.
10 ** This program is distributed in the hope that it will be useful,
11 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ** GNU General Public License for more details.
15 ** You should have received a copy of the GNU General Public License
16 ** along with this program; if not, write to the Free Software
17 ** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #include "ssh_subagent.h"
26 * SSH session constructor
28 SSHSession::SSHSession(const InetAddress
& addr
, UINT16 port
)
36 * SSH session destructor
38 SSHSession::~SSHSession()
46 bool SSHSession::connect(const TCHAR
*user
, const TCHAR
*password
)
48 if (m_session
!= NULL
)
49 return false; // already connected
51 m_session
= ssh_new();
52 if (m_session
== NULL
)
53 return false; // cannot create session
58 ssh_options_set(m_session
, SSH_OPTIONS_HOST
, m_addr
.toStringA(hostname
));
59 ssh_options_set(m_session
, SSH_OPTIONS_PORT
, &m_port
);
61 ssh_options_set(m_session
, SSH_OPTIONS_TIMEOUT
, &timeout
);
64 WideCharToMultiByte(CP_UTF8
, 0, user
, -1, mbuser
, 256, NULL
, NULL
);
65 ssh_options_set(m_session
, SSH_OPTIONS_USER
, mbuser
);
67 ssh_options_set(m_session
, SSH_OPTIONS_USER
, user
);
70 if (ssh_connect(m_session
) == SSH_OK
)
74 WideCharToMultiByte(CP_UTF8
, 0, password
, -1, mbpassword
, 256, NULL
, NULL
);
75 if (ssh_userauth_password(m_session
, NULL
, mbpassword
) == SSH_AUTH_SUCCESS
)
77 if (ssh_userauth_password(m_session
, NULL
, password
) == SSH_AUTH_SUCCESS
)
84 nxlog_debug(6, _T("SSH: login as %s on %s:%d failed"), user
, (const TCHAR
*)m_addr
.toString(), m_port
);
89 nxlog_debug(6, _T("SSH: connect to %s:%d failed"), (const TCHAR
*)m_addr
.toString(), m_port
);
94 if (ssh_is_connected(m_session
))
95 ssh_disconnect(m_session
);
103 * Disconnect from server
105 void SSHSession::disconnect()
107 if (m_session
== NULL
)
110 if (ssh_is_connected(m_session
))
111 ssh_disconnect(m_session
);
117 * Execute command and capture output
119 StringList
*SSHSession::execute(const TCHAR
*command
)
121 if ((m_session
== NULL
) || !ssh_is_connected(m_session
))
124 ssh_channel channel
= ssh_channel_new(m_session
);
128 StringList
*output
= NULL
;
129 if (ssh_channel_open_session(channel
) == SSH_OK
)
132 char *mbcmd
= UTF8StringFromWideString(command
);
133 if (ssh_channel_request_exec(channel
, mbcmd
) == SSH_OK
)
135 if (ssh_channel_request_exec(channel
, command
) == SSH_OK
)
138 output
= new StringList();
140 int nbytes
= ssh_channel_read(channel
, buffer
, sizeof(buffer
) - 1, 0);
144 buffer
[nbytes
+ offset
] = 0;
146 char *eol
= strchr(curr
, '\n');
150 char *cr
= strchr(curr
, '\r');
153 output
->addMBString(curr
);
155 eol
= strchr(curr
, '\n');
157 offset
= strlen(curr
);
159 memmove(buffer
, curr
, offset
);
160 nbytes
= ssh_channel_read(channel
, &buffer
[offset
], sizeof(buffer
) - offset
- 1, 0);
167 char *cr
= strchr(buffer
, '\r');
170 output
->addMBString(buffer
);
172 ssh_channel_send_eof(channel
);
176 delete_and_null(output
);
181 nxlog_debug(6, _T("SSH: command \"%s\" execution on %s:%d failed"), command
, (const TCHAR
*)m_addr
.toString(), m_port
);
183 ssh_channel_close(channel
);
187 nxlog_debug(6, _T("SSH: cannot open channel on %s:%d"), (const TCHAR
*)m_addr
.toString(), m_port
);
189 ssh_channel_free(channel
);