1
0
mirror of https://git.libssh.org/projects/libssh.git synced 2025-08-10 06:23:01 +03:00

Modified server API

This commit is contained in:
Aris Adamantiadis
2009-10-09 21:26:15 +02:00
parent 9c667ebc26
commit 2e9c13dad0
3 changed files with 38 additions and 52 deletions

View File

@@ -114,7 +114,7 @@ LIBSSH_API void ssh_bind_fd_toaccept(SSH_BIND *ssh_bind);
* *
* @return A newly allocated ssh session, NULL on error. * @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. * @brief Free a ssh servers bind.

View File

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

View File

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