buffers: adapt server.c to ssh_buffer_(un)pack()
Signed-off-by: Andreas Schneider <asn@samba.org>
Этот коммит содержится в:
родитель
a182926024
Коммит
5a08ddcff2
286
src/server.c
286
src/server.c
@ -251,9 +251,9 @@ int ssh_get_key_params(ssh_session session, ssh_key *privkey){
|
|||||||
|
|
||||||
static int dh_handshake_server(ssh_session session) {
|
static int dh_handshake_server(ssh_session session) {
|
||||||
ssh_key privkey;
|
ssh_key privkey;
|
||||||
//ssh_string pubkey_blob = NULL;
|
|
||||||
ssh_string sig_blob;
|
ssh_string sig_blob;
|
||||||
ssh_string f;
|
ssh_string f;
|
||||||
|
int rc;
|
||||||
|
|
||||||
if (dh_generate_y(session) < 0) {
|
if (dh_generate_y(session) < 0) {
|
||||||
ssh_set_error(session, SSH_FATAL, "Could not create y number");
|
ssh_set_error(session, SSH_FATAL, "Could not create y number");
|
||||||
@ -294,19 +294,20 @@ static int dh_handshake_server(ssh_session session) {
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (buffer_add_u8(session->out_buffer, SSH2_MSG_KEXDH_REPLY) < 0 ||
|
rc = ssh_buffer_pack(session->out_buffer,
|
||||||
buffer_add_ssh_string(session->out_buffer,
|
"bSSS",
|
||||||
session->next_crypto->server_pubkey) < 0 ||
|
SSH2_MSG_KEXDH_REPLY,
|
||||||
buffer_add_ssh_string(session->out_buffer, f) < 0 ||
|
session->next_crypto->server_pubkey,
|
||||||
buffer_add_ssh_string(session->out_buffer, sig_blob) < 0) {
|
f,
|
||||||
ssh_set_error(session, SSH_FATAL, "Not enough space");
|
sig_blob);
|
||||||
ssh_buffer_reinit(session->out_buffer);
|
|
||||||
ssh_string_free(f);
|
|
||||||
ssh_string_free(sig_blob);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
ssh_string_free(f);
|
ssh_string_free(f);
|
||||||
ssh_string_free(sig_blob);
|
ssh_string_free(sig_blob);
|
||||||
|
if(rc != SSH_OK){
|
||||||
|
ssh_set_error_oom(session);
|
||||||
|
ssh_buffer_reinit(session->out_buffer);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
if (packet_send(session) == SSH_ERROR) {
|
if (packet_send(session) == SSH_ERROR) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
@ -591,12 +592,8 @@ int ssh_handle_key_exchange(ssh_session session) {
|
|||||||
*/
|
*/
|
||||||
int ssh_auth_reply_default(ssh_session session,int partial) {
|
int ssh_auth_reply_default(ssh_session session,int partial) {
|
||||||
char methods_c[128] = {0};
|
char methods_c[128] = {0};
|
||||||
ssh_string methods = NULL;
|
|
||||||
int rc = SSH_ERROR;
|
int rc = SSH_ERROR;
|
||||||
|
|
||||||
if (buffer_add_u8(session->out_buffer, SSH2_MSG_USERAUTH_FAILURE) < 0) {
|
|
||||||
return rc;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (session->auth_methods == 0) {
|
if (session->auth_methods == 0) {
|
||||||
session->auth_methods = SSH_AUTH_METHOD_PUBLICKEY | SSH_AUTH_METHOD_PASSWORD;
|
session->auth_methods = SSH_AUTH_METHOD_PUBLICKEY | SSH_AUTH_METHOD_PASSWORD;
|
||||||
@ -632,63 +629,43 @@ int ssh_auth_reply_default(ssh_session session,int partial) {
|
|||||||
SSH_LOG(SSH_LOG_PACKET,
|
SSH_LOG(SSH_LOG_PACKET,
|
||||||
"Sending a auth failure. methods that can continue: %s", methods_c);
|
"Sending a auth failure. methods that can continue: %s", methods_c);
|
||||||
|
|
||||||
methods = ssh_string_from_char(methods_c);
|
rc = ssh_buffer_pack(session->out_buffer,
|
||||||
if (methods == NULL) {
|
"bsb",
|
||||||
goto error;
|
SSH2_MSG_USERAUTH_FAILURE,
|
||||||
|
methods_c,
|
||||||
|
partial ? 1 : 0);
|
||||||
|
if (rc != SSH_OK){
|
||||||
|
ssh_set_error_oom(session);
|
||||||
|
return SSH_ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (buffer_add_ssh_string(session->out_buffer, methods) < 0) {
|
|
||||||
goto error;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (partial) {
|
|
||||||
if (buffer_add_u8(session->out_buffer, 1) < 0) {
|
|
||||||
goto error;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if (buffer_add_u8(session->out_buffer, 0) < 0) {
|
|
||||||
goto error;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
rc = packet_send(session);
|
rc = packet_send(session);
|
||||||
error:
|
|
||||||
ssh_string_free(methods);
|
|
||||||
|
|
||||||
return rc;
|
return rc;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int ssh_message_channel_request_open_reply_default(ssh_message msg) {
|
static int ssh_message_channel_request_open_reply_default(ssh_message msg) {
|
||||||
SSH_LOG(SSH_LOG_FUNCTIONS, "Refusing a channel");
|
int rc;
|
||||||
|
|
||||||
if (buffer_add_u8(msg->session->out_buffer
|
SSH_LOG(SSH_LOG_FUNCTIONS, "Refusing a channel");
|
||||||
, SSH2_MSG_CHANNEL_OPEN_FAILURE) < 0) {
|
|
||||||
goto error;
|
|
||||||
}
|
|
||||||
if (buffer_add_u32(msg->session->out_buffer,
|
|
||||||
htonl(msg->channel_request_open.sender)) < 0) {
|
|
||||||
goto error;
|
|
||||||
}
|
|
||||||
if (buffer_add_u32(msg->session->out_buffer,
|
|
||||||
htonl(SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED)) < 0) {
|
|
||||||
goto error;
|
|
||||||
}
|
|
||||||
/* reason is an empty string */
|
|
||||||
if (buffer_add_u32(msg->session->out_buffer, 0) < 0) {
|
|
||||||
goto error;
|
|
||||||
}
|
|
||||||
/* language too */
|
|
||||||
if (buffer_add_u32(msg->session->out_buffer, 0) < 0) {
|
|
||||||
goto error;
|
|
||||||
}
|
|
||||||
|
|
||||||
return packet_send(msg->session);
|
rc = ssh_buffer_pack(msg->session->out_buffer,
|
||||||
error:
|
"bdddd",
|
||||||
return SSH_ERROR;
|
SSH2_MSG_CHANNEL_OPEN_FAILURE,
|
||||||
|
msg->channel_request_open.sender,
|
||||||
|
SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED,
|
||||||
|
0, /* reason is empty string */
|
||||||
|
0); /* language string */
|
||||||
|
if (rc != SSH_OK){
|
||||||
|
ssh_set_error_oom(msg->session);
|
||||||
|
return SSH_ERROR;
|
||||||
|
}
|
||||||
|
|
||||||
|
rc = packet_send(msg->session);
|
||||||
|
return rc;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int ssh_message_channel_request_reply_default(ssh_message msg) {
|
static int ssh_message_channel_request_reply_default(ssh_message msg) {
|
||||||
uint32_t channel;
|
uint32_t channel;
|
||||||
|
int rc;
|
||||||
|
|
||||||
if (msg->channel_request.want_reply) {
|
if (msg->channel_request.want_reply) {
|
||||||
channel = msg->channel_request.channel->remote_channel;
|
channel = msg->channel_request.channel->remote_channel;
|
||||||
@ -696,13 +673,14 @@ static int ssh_message_channel_request_reply_default(ssh_message msg) {
|
|||||||
SSH_LOG(SSH_LOG_PACKET,
|
SSH_LOG(SSH_LOG_PACKET,
|
||||||
"Sending a default channel_request denied to channel %d", channel);
|
"Sending a default channel_request denied to channel %d", channel);
|
||||||
|
|
||||||
if (buffer_add_u8(msg->session->out_buffer, SSH2_MSG_CHANNEL_FAILURE) < 0) {
|
rc = ssh_buffer_pack(msg->session->out_buffer,
|
||||||
return SSH_ERROR;
|
"bd",
|
||||||
|
SSH2_MSG_CHANNEL_FAILURE,
|
||||||
|
channel);
|
||||||
|
if (rc != SSH_OK){
|
||||||
|
ssh_set_error_oom(msg->session);
|
||||||
|
return SSH_ERROR;
|
||||||
}
|
}
|
||||||
if (buffer_add_u32(msg->session->out_buffer, htonl(channel)) < 0) {
|
|
||||||
return SSH_ERROR;
|
|
||||||
}
|
|
||||||
|
|
||||||
return packet_send(msg->session);
|
return packet_send(msg->session);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -718,33 +696,32 @@ static int ssh_message_service_request_reply_default(ssh_message msg) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int ssh_message_service_reply_success(ssh_message msg) {
|
int ssh_message_service_reply_success(ssh_message msg) {
|
||||||
struct ssh_string_struct *service;
|
ssh_session session;
|
||||||
ssh_session session;
|
int rc;
|
||||||
|
|
||||||
if (msg == NULL) {
|
if (msg == NULL) {
|
||||||
return SSH_ERROR;
|
return SSH_ERROR;
|
||||||
}
|
}
|
||||||
session = msg->session;
|
session = msg->session;
|
||||||
|
|
||||||
SSH_LOG(SSH_LOG_PACKET,
|
SSH_LOG(SSH_LOG_PACKET,
|
||||||
"Sending a SERVICE_ACCEPT for service %s", msg->service_request.service);
|
"Sending a SERVICE_ACCEPT for service %s", msg->service_request.service);
|
||||||
if (buffer_add_u8(session->out_buffer, SSH2_MSG_SERVICE_ACCEPT) < 0) {
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
service=ssh_string_from_char(msg->service_request.service);
|
|
||||||
if (service == NULL) {
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (buffer_add_ssh_string(session->out_buffer, service) < 0) {
|
rc = ssh_buffer_pack(session->out_buffer,
|
||||||
ssh_string_free(service);
|
"bs",
|
||||||
return -1;
|
SSH2_MSG_SERVICE_ACCEPT,
|
||||||
}
|
msg->service_request.service);
|
||||||
ssh_string_free(service);
|
if (rc != SSH_OK){
|
||||||
return packet_send(msg->session);
|
ssh_set_error_oom(session);
|
||||||
|
return SSH_ERROR;
|
||||||
|
}
|
||||||
|
rc = packet_send(msg->session);
|
||||||
|
return rc;
|
||||||
}
|
}
|
||||||
|
|
||||||
int ssh_message_global_request_reply_success(ssh_message msg, uint16_t bound_port) {
|
int ssh_message_global_request_reply_success(ssh_message msg, uint16_t bound_port) {
|
||||||
|
int rc;
|
||||||
|
|
||||||
SSH_LOG(SSH_LOG_FUNCTIONS, "Accepting a global request");
|
SSH_LOG(SSH_LOG_FUNCTIONS, "Accepting a global request");
|
||||||
|
|
||||||
if (msg->global_request.want_reply) {
|
if (msg->global_request.want_reply) {
|
||||||
@ -755,7 +732,9 @@ int ssh_message_global_request_reply_success(ssh_message msg, uint16_t bound_por
|
|||||||
|
|
||||||
if(msg->global_request.type == SSH_GLOBAL_REQUEST_TCPIP_FORWARD
|
if(msg->global_request.type == SSH_GLOBAL_REQUEST_TCPIP_FORWARD
|
||||||
&& msg->global_request.bind_port == 0) {
|
&& msg->global_request.bind_port == 0) {
|
||||||
if (buffer_add_u32(msg->session->out_buffer, htonl(bound_port)) < 0) {
|
rc = ssh_buffer_pack(msg->session->out_buffer, "d", bound_port);
|
||||||
|
if (rc != SSH_ERROR) {
|
||||||
|
ssh_set_error_oom(msg->session);
|
||||||
goto error;
|
goto error;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -886,9 +865,8 @@ int ssh_message_auth_set_methods(ssh_message msg, int methods) {
|
|||||||
int ssh_message_auth_interactive_request(ssh_message msg, const char *name,
|
int ssh_message_auth_interactive_request(ssh_message msg, const char *name,
|
||||||
const char *instruction, unsigned int num_prompts,
|
const char *instruction, unsigned int num_prompts,
|
||||||
const char **prompts, char *echo) {
|
const char **prompts, char *echo) {
|
||||||
int r;
|
int rc;
|
||||||
unsigned int i = 0;
|
unsigned int i = 0;
|
||||||
ssh_string tmp = NULL;
|
|
||||||
|
|
||||||
if(name == NULL || instruction == NULL) {
|
if(name == NULL || instruction == NULL) {
|
||||||
return SSH_ERROR;
|
return SSH_ERROR;
|
||||||
@ -897,71 +875,30 @@ int ssh_message_auth_interactive_request(ssh_message msg, const char *name,
|
|||||||
return SSH_ERROR;
|
return SSH_ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (buffer_add_u8(msg->session->out_buffer, SSH2_MSG_USERAUTH_INFO_REQUEST) < 0) {
|
rc = ssh_buffer_pack(msg->session->out_buffer,
|
||||||
return SSH_ERROR;
|
"bsss",
|
||||||
}
|
SSH2_MSG_USERAUTH_INFO_REQUEST,
|
||||||
|
name,
|
||||||
/* name */
|
instruction,
|
||||||
tmp = ssh_string_from_char(name);
|
"", /* language tag */
|
||||||
if (tmp == NULL) {
|
num_prompts);
|
||||||
return SSH_ERROR;
|
if (rc != SSH_OK){
|
||||||
}
|
ssh_set_error_oom(msg->session);
|
||||||
|
|
||||||
r = buffer_add_ssh_string(msg->session->out_buffer, tmp);
|
|
||||||
ssh_string_free(tmp);
|
|
||||||
if (r < 0) {
|
|
||||||
return SSH_ERROR;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* instruction */
|
|
||||||
tmp = ssh_string_from_char(instruction);
|
|
||||||
if (tmp == NULL) {
|
|
||||||
return SSH_ERROR;
|
|
||||||
}
|
|
||||||
|
|
||||||
r = buffer_add_ssh_string(msg->session->out_buffer, tmp);
|
|
||||||
ssh_string_free(tmp);
|
|
||||||
if (r < 0) {
|
|
||||||
return SSH_ERROR;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* language tag */
|
|
||||||
tmp = ssh_string_from_char("");
|
|
||||||
if (tmp == NULL) {
|
|
||||||
return SSH_ERROR;
|
|
||||||
}
|
|
||||||
|
|
||||||
r = buffer_add_ssh_string(msg->session->out_buffer, tmp);
|
|
||||||
ssh_string_free(tmp);
|
|
||||||
if (r < 0) {
|
|
||||||
return SSH_ERROR;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* num prompts */
|
|
||||||
if (buffer_add_u32(msg->session->out_buffer, ntohl(num_prompts)) < 0) {
|
|
||||||
return SSH_ERROR;
|
return SSH_ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
for(i = 0; i < num_prompts; i++) {
|
for(i = 0; i < num_prompts; i++) {
|
||||||
/* prompt[i] */
|
rc = ssh_buffer_pack(msg->session->out_buffer,
|
||||||
tmp = ssh_string_from_char(prompts[i]);
|
"sb",
|
||||||
if (tmp == NULL) {
|
prompts[i],
|
||||||
return SSH_ERROR;
|
echo[1] ? 1 : 0);
|
||||||
}
|
if (rc != SSH_OK){
|
||||||
|
ssh_set_error_oom(msg->session);
|
||||||
r = buffer_add_ssh_string(msg->session->out_buffer, tmp);
|
|
||||||
ssh_string_free(tmp);
|
|
||||||
if (r < 0) {
|
|
||||||
return SSH_ERROR;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* echo[i] */
|
|
||||||
if (buffer_add_u8(msg->session->out_buffer, echo[i]) < 0) {
|
|
||||||
return SSH_ERROR;
|
return SSH_ERROR;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
r = packet_send(msg->session);
|
rc = packet_send(msg->session);
|
||||||
|
|
||||||
/* fill in the kbdint structure */
|
/* fill in the kbdint structure */
|
||||||
if (msg->session->kbdint == NULL) {
|
if (msg->session->kbdint == NULL) {
|
||||||
@ -1027,7 +964,7 @@ int ssh_message_auth_interactive_request(ssh_message msg, const char *name,
|
|||||||
msg->session->kbdint->echo = NULL;
|
msg->session->kbdint->echo = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
return r;
|
return rc;
|
||||||
}
|
}
|
||||||
|
|
||||||
int ssh_auth_reply_success(ssh_session session, int partial) {
|
int ssh_auth_reply_success(ssh_session session, int partial) {
|
||||||
@ -1068,17 +1005,23 @@ int ssh_message_auth_reply_success(ssh_message msg, int partial) {
|
|||||||
|
|
||||||
/* Answer OK to a pubkey auth request */
|
/* Answer OK to a pubkey auth request */
|
||||||
int ssh_message_auth_reply_pk_ok(ssh_message msg, ssh_string algo, ssh_string pubkey) {
|
int ssh_message_auth_reply_pk_ok(ssh_message msg, ssh_string algo, ssh_string pubkey) {
|
||||||
if (msg == NULL) {
|
int rc;
|
||||||
return SSH_ERROR;
|
if (msg == NULL) {
|
||||||
}
|
return SSH_ERROR;
|
||||||
|
}
|
||||||
|
|
||||||
if (buffer_add_u8(msg->session->out_buffer, SSH2_MSG_USERAUTH_PK_OK) < 0 ||
|
rc = ssh_buffer_pack(msg->session->out_buffer,
|
||||||
buffer_add_ssh_string(msg->session->out_buffer, algo) < 0 ||
|
"bSS",
|
||||||
buffer_add_ssh_string(msg->session->out_buffer, pubkey) < 0) {
|
SSH2_MSG_USERAUTH_PK_OK,
|
||||||
return SSH_ERROR;
|
algo,
|
||||||
}
|
pubkey);
|
||||||
|
if(rc != SSH_OK){
|
||||||
|
ssh_set_error_oom(msg->session);
|
||||||
|
return SSH_ERROR;
|
||||||
|
}
|
||||||
|
|
||||||
return packet_send(msg->session);
|
rc = packet_send(msg->session);
|
||||||
|
return rc;
|
||||||
}
|
}
|
||||||
|
|
||||||
int ssh_message_auth_reply_pk_ok_simple(ssh_message msg) {
|
int ssh_message_auth_reply_pk_ok_simple(ssh_message msg) {
|
||||||
@ -1233,27 +1176,14 @@ int ssh_execute_message_callbacks(ssh_session session){
|
|||||||
|
|
||||||
int ssh_send_keepalive(ssh_session session)
|
int ssh_send_keepalive(ssh_session session)
|
||||||
{
|
{
|
||||||
struct ssh_string_struct *req;
|
|
||||||
int rc;
|
int rc;
|
||||||
|
|
||||||
rc = buffer_add_u8(session->out_buffer, SSH2_MSG_GLOBAL_REQUEST);
|
rc = ssh_buffer_pack(session->out_buffer,
|
||||||
if (rc < 0) {
|
"bsb",
|
||||||
goto err;
|
SSH2_MSG_GLOBAL_REQUEST,
|
||||||
}
|
"keepalive@openssh.com",
|
||||||
|
1);
|
||||||
req = ssh_string_from_char("keepalive@openssh.com");
|
if (rc != SSH_OK) {
|
||||||
if (req == NULL) {
|
|
||||||
goto err;
|
|
||||||
}
|
|
||||||
|
|
||||||
rc = buffer_add_ssh_string(session->out_buffer, req);
|
|
||||||
ssh_string_free(req);
|
|
||||||
if (rc < 0) {
|
|
||||||
goto err;
|
|
||||||
}
|
|
||||||
|
|
||||||
rc = buffer_add_u8(session->out_buffer, 1);
|
|
||||||
if (rc < 0) {
|
|
||||||
goto err;
|
goto err;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1261,7 +1191,7 @@ int ssh_send_keepalive(ssh_session session)
|
|||||||
goto err;
|
goto err;
|
||||||
}
|
}
|
||||||
|
|
||||||
ssh_handle_packets(session, 0);
|
ssh_handle_packets(session, SSH_TIMEOUT_NONBLOCKING);
|
||||||
|
|
||||||
SSH_LOG(SSH_LOG_PACKET, "Sent a keepalive");
|
SSH_LOG(SSH_LOG_PACKET, "Sent a keepalive");
|
||||||
return SSH_OK;
|
return SSH_OK;
|
||||||
|
Загрузка…
x
Ссылка в новой задаче
Block a user