1
1
Этот коммит содержится в:
Aris Adamantiadis 2009-10-09 21:26:15 +02:00
родитель 9c667ebc26
Коммит 2e9c13dad0
3 изменённых файлов: 38 добавлений и 52 удалений

Просмотреть файл

@ -114,7 +114,7 @@ LIBSSH_API void ssh_bind_fd_toaccept(SSH_BIND *ssh_bind);
*
* @return A newly allocated ssh session, NULL on error.
*/
LIBSSH_API ssh_session ssh_bind_accept(SSH_BIND *ssh_bind);
LIBSSH_API int ssh_bind_accept(SSH_BIND *ssh_bind, ssh_session session);
/**
* @brief Free a ssh servers bind.

Просмотреть файл

@ -180,8 +180,7 @@ void ssh_bind_fd_toaccept(SSH_BIND *sshbind) {
sshbind->toaccept = 1;
}
ssh_session ssh_bind_accept(SSH_BIND *sshbind) {
ssh_session session;
int ssh_bind_accept(SSH_BIND *sshbind, ssh_session session) {
ssh_private_key dsa = NULL;
ssh_private_key rsa = NULL;
int fd = -1;
@ -190,19 +189,22 @@ ssh_session ssh_bind_accept(SSH_BIND *sshbind) {
if (sshbind->bindfd < 0) {
ssh_set_error(sshbind, SSH_FATAL,
"Can't accept new clients on a not bound socket.");
return NULL;
return SSH_ERROR;
}
if(session == NULL){
ssh_set_error(sshbind, SSH_FATAL,"session is null");
return SSH_ERROR;
}
if (sshbind->dsakey == NULL || sshbind->rsakey == NULL) {
ssh_set_error(sshbind, SSH_FATAL,
"DSA or RSA host key file must be set before accept()");
return NULL;
return SSH_ERROR;
}
if (sshbind->dsakey) {
dsa = _privatekey_from_file(sshbind, sshbind->dsakey, TYPE_DSS);
if (dsa == NULL) {
return NULL;
return SSH_ERROR;
}
}
@ -210,7 +212,7 @@ ssh_session ssh_bind_accept(SSH_BIND *sshbind) {
rsa = _privatekey_from_file(sshbind, sshbind->rsakey, TYPE_RSA);
if (rsa == NULL) {
privatekey_free(dsa);
return NULL;
return SSH_ERROR;
}
}
@ -221,16 +223,9 @@ ssh_session ssh_bind_accept(SSH_BIND *sshbind) {
strerror(errno));
privatekey_free(dsa);
privatekey_free(rsa);
return NULL;
return SSH_ERROR;
}
session = ssh_new();
if (session == NULL) {
ssh_set_error(sshbind, SSH_FATAL, "Not enough space");
privatekey_free(dsa);
privatekey_free(rsa);
return NULL;
}
session->server = 1;
session->version = 2;
@ -246,7 +241,7 @@ ssh_session ssh_bind_accept(SSH_BIND *sshbind) {
privatekey_free(dsa);
privatekey_free(rsa);
ssh_cleanup(session);
return NULL;
return SSH_ERROR;
}
}
}
@ -259,7 +254,7 @@ ssh_session ssh_bind_accept(SSH_BIND *sshbind) {
privatekey_free(dsa);
privatekey_free(rsa);
ssh_cleanup(session);
return NULL;
return SSH_ERROR;
}
}
/* TODO FIXME this doesn't work
@ -271,13 +266,13 @@ ssh_session ssh_bind_accept(SSH_BIND *sshbind) {
privatekey_free(dsa);
privatekey_free(rsa);
ssh_cleanup(session);
return NULL;
return SSH_ERROR;
}
ssh_socket_set_fd(session->socket, fd);
session->dsa_key = dsa;
session->rsa_key = rsa;
return session;
return SSH_OK;
}
void ssh_bind_free(SSH_BIND *sshbind){

Просмотреть файл

@ -1,24 +1,16 @@
/* sshd.c */
/* yet another ssh daemon (Yawn!) */
/* This is a sample implementation of a libssh based SSH server */
/*
Copyright 2004 Aris Adamantiadis
Copyright 2003-2009 Aris Adamantiadis
This file is part of the SSH Library
The SSH Library is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or (at your
option) any later version.
The SSH Library is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
License for more details.
You should have received a copy of the GNU Lesser General Public License
along with the SSH Library; see the file COPYING. If not, write to
the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
MA 02111-1307, USA. */
You are free to copy this file, modify it in any way, consider it being public
domain. This does not apply to the rest of the library though, but it is
allowed to cut-and-paste working code from this file to any license of
program.
The goal is to show the API in action. It's not a reference on how terminal
clients must be made or how a client should react.
*/
#include <libssh/libssh.h>
#include <libssh/server.h>
@ -38,35 +30,34 @@ static int auth_password(char *user, char *password){
return 0;
if(strcmp(password,"lala"))
return 0;
return 1; // authenticated
return 1; // authenticated
}
int main(int argc, char **argv){
ssh_session session;
ssh_bind ssh_bind_o;
ssh_bind sshbind;
ssh_message message;
ssh_channel chan=0;
ssh_buffer buf;
int auth=0;
int sftp=0;
int i;
int r;
ssh_bind_o=ssh_bind_new();
// ssh_options_getopt(options, &argc, argv);
ssh_bind_options_set(ssh_bind_o, SSH_BIND_OPTIONS_DSAKEY, KEYS_FOLDER "ssh_host_dsa_key");
ssh_bind_options_set(ssh_bind_o, SSH_BIND_OPTIONS_RSAKEY, KEYS_FOLDER "ssh_host_rsa_key");
sshbind=ssh_bind_new();
session=ssh_new();
ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_DSAKEY, KEYS_FOLDER "ssh_host_dsa_key");
ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_RSAKEY, KEYS_FOLDER "ssh_host_rsa_key");
// ssh_bind_set_options(ssh_bind_o,options);
if(ssh_bind_listen(ssh_bind_o)<0){
printf("Error listening to socket: %s\n",ssh_get_error(ssh_bind_o));
if(ssh_bind_listen(sshbind)<0){
printf("Error listening to socket: %s\n",ssh_get_error(sshbind));
return 1;
}
session=ssh_bind_accept(ssh_bind_o);
if(!session){
printf("error accepting a connection : %s\n",ssh_get_error(ssh_bind_o));
r=ssh_bind_accept(sshbind,session);
if(r==SSH_ERROR){
printf("error accepting a connection : %s\n",ssh_get_error(sshbind));
return 1;
}
printf("Socket connected: fd = %d\n", ssh_get_fd(session));
if(ssh_accept(session)){
printf("ssh_accept: %s\n",ssh_get_error(session));
return 1;
@ -103,7 +94,7 @@ int main(int argc, char **argv){
} while (!auth);
if(!auth){
printf("auth error: %s\n",ssh_get_error(session));
ssh_finalize();
ssh_disconnect(session);
return 1;
}
do {
@ -154,7 +145,7 @@ int main(int argc, char **argv){
} while (i>0);
buffer_free(buf);
ssh_disconnect(session);
ssh_bind_free(ssh_bind_o);
ssh_bind_free(sshbind);
ssh_finalize();
return 0;
}