2005-07-05 05:21:44 +04:00
|
|
|
/* sshd.c */
|
|
|
|
/* yet another ssh daemon (Yawn!) */
|
|
|
|
/*
|
|
|
|
Copyright 2004 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. */
|
|
|
|
|
|
|
|
#include <libssh/libssh.h>
|
|
|
|
#include <libssh/server.h>
|
|
|
|
#include <unistd.h>
|
2005-08-10 17:22:52 +04:00
|
|
|
#include <string.h>
|
2005-10-05 02:11:19 +04:00
|
|
|
#include <stdio.h>
|
2008-03-07 05:11:40 +03:00
|
|
|
#ifdef _WIN32
|
|
|
|
#define KEYS_FOLDER
|
|
|
|
#else
|
|
|
|
#define KEYS_FOLDER "/etc/ssh/"
|
|
|
|
#endif
|
2005-08-10 17:22:52 +04:00
|
|
|
|
2009-02-06 12:59:54 +03:00
|
|
|
static int auth_password(char *user, char *password){
|
2005-08-10 17:22:52 +04:00
|
|
|
if(strcmp(user,"aris"))
|
|
|
|
return 0;
|
|
|
|
if(strcmp(password,"lala"))
|
|
|
|
return 0;
|
|
|
|
return 1; // authenticated
|
|
|
|
}
|
|
|
|
|
2005-07-05 05:21:44 +04:00
|
|
|
int main(int argc, char **argv){
|
2009-09-24 00:13:19 +04:00
|
|
|
ssh_options options=ssh_options_new();
|
2009-09-23 23:55:54 +04:00
|
|
|
ssh_session session;
|
2005-07-06 18:35:03 +04:00
|
|
|
SSH_BIND *ssh_bind;
|
2009-09-24 00:19:11 +04:00
|
|
|
ssh_message message;
|
2009-07-25 00:15:33 +04:00
|
|
|
ssh_channel chan=0;
|
2009-07-25 00:03:36 +04:00
|
|
|
ssh_buffer buf;
|
2005-08-10 17:22:52 +04:00
|
|
|
int auth=0;
|
|
|
|
int sftp=0;
|
2005-08-13 16:58:41 +04:00
|
|
|
int i;
|
2009-09-22 22:34:23 +04:00
|
|
|
ssh_options_getopt(options, &argc, argv);
|
|
|
|
|
|
|
|
ssh_options_set(options, SSH_OPTIONS_SERVER_DSAKEY, KEYS_FOLDER "ssh_host_dsa_key");
|
|
|
|
ssh_options_set(options, SSH_OPTIONS_SERVER_RSAKEY, KEYS_FOLDER "ssh_host_rsa_key");
|
|
|
|
|
2005-07-06 18:35:03 +04:00
|
|
|
ssh_bind=ssh_bind_new();
|
|
|
|
ssh_bind_set_options(ssh_bind,options);
|
|
|
|
if(ssh_bind_listen(ssh_bind)<0){
|
|
|
|
printf("Error listening to socket: %s\n",ssh_get_error(ssh_bind));
|
|
|
|
return 1;
|
2005-07-05 05:21:44 +04:00
|
|
|
}
|
2005-07-06 18:35:03 +04:00
|
|
|
session=ssh_bind_accept(ssh_bind);
|
|
|
|
if(!session){
|
|
|
|
printf("error accepting a connection : %s\n",ssh_get_error(ssh_bind));
|
|
|
|
return 1;
|
|
|
|
}
|
2009-05-11 16:38:21 +04:00
|
|
|
printf("Socket connected: fd = %d\n", ssh_get_fd(session));
|
2005-08-07 14:48:08 +04:00
|
|
|
if(ssh_accept(session)){
|
2009-05-11 16:38:21 +04:00
|
|
|
printf("ssh_accept: %s\n",ssh_get_error(session));
|
2005-08-07 14:48:08 +04:00
|
|
|
return 1;
|
2005-07-05 05:21:44 +04:00
|
|
|
}
|
2005-08-07 14:48:08 +04:00
|
|
|
do {
|
|
|
|
message=ssh_message_get(session);
|
2005-08-10 17:22:52 +04:00
|
|
|
if(!message)
|
|
|
|
break;
|
|
|
|
switch(ssh_message_type(message)){
|
2009-07-31 14:16:25 +04:00
|
|
|
case SSH_REQUEST_AUTH:
|
2005-08-10 17:22:52 +04:00
|
|
|
switch(ssh_message_subtype(message)){
|
2009-07-31 14:16:25 +04:00
|
|
|
case SSH_AUTH_METHOD_PASSWORD:
|
2005-08-10 17:22:52 +04:00
|
|
|
printf("User %s wants to auth with pass %s\n",
|
|
|
|
ssh_message_auth_user(message),
|
|
|
|
ssh_message_auth_password(message));
|
|
|
|
if(auth_password(ssh_message_auth_user(message),
|
|
|
|
ssh_message_auth_password(message))){
|
|
|
|
auth=1;
|
|
|
|
ssh_message_auth_reply_success(message,0);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// not authenticated, send default message
|
2009-07-31 14:16:25 +04:00
|
|
|
case SSH_AUTH_METHOD_NONE:
|
2005-08-10 17:22:52 +04:00
|
|
|
default:
|
2009-07-31 14:16:25 +04:00
|
|
|
ssh_message_auth_set_methods(message,SSH_AUTH_METHOD_PASSWORD);
|
2005-08-10 17:22:52 +04:00
|
|
|
ssh_message_reply_default(message);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ssh_message_reply_default(message);
|
|
|
|
}
|
|
|
|
ssh_message_free(message);
|
|
|
|
} while (!auth);
|
|
|
|
if(!auth){
|
2009-05-11 16:38:21 +04:00
|
|
|
printf("auth error: %s\n",ssh_get_error(session));
|
2006-07-09 14:36:44 +04:00
|
|
|
ssh_finalize();
|
2005-08-10 17:22:52 +04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
do {
|
|
|
|
message=ssh_message_get(session);
|
|
|
|
if(message){
|
|
|
|
switch(ssh_message_type(message)){
|
2009-07-31 14:16:25 +04:00
|
|
|
case SSH_REQUEST_CHANNEL_OPEN:
|
2005-08-10 17:22:52 +04:00
|
|
|
if(ssh_message_subtype(message)==SSH_CHANNEL_SESSION){
|
|
|
|
chan=ssh_message_channel_request_open_reply_accept(message);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
ssh_message_reply_default(message);
|
|
|
|
}
|
|
|
|
ssh_message_free(message);
|
|
|
|
}
|
|
|
|
} while(message && !chan);
|
|
|
|
if(!chan){
|
|
|
|
printf("error : %s\n",ssh_get_error(session));
|
2008-03-07 05:11:40 +03:00
|
|
|
ssh_finalize();
|
2005-08-10 17:22:52 +04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
do {
|
|
|
|
message=ssh_message_get(session);
|
2009-07-31 14:16:25 +04:00
|
|
|
if(message && ssh_message_type(message)==SSH_REQUEST_CHANNEL &&
|
2005-08-13 16:58:41 +04:00
|
|
|
ssh_message_subtype(message)==SSH_CHANNEL_REQUEST_SHELL){
|
|
|
|
// if(!strcmp(ssh_message_channel_request_subsystem(message),"sftp")){
|
2005-08-10 17:22:52 +04:00
|
|
|
sftp=1;
|
|
|
|
ssh_message_channel_request_reply_success(message);
|
|
|
|
break;
|
2005-08-13 16:58:41 +04:00
|
|
|
// }
|
2005-08-10 17:22:52 +04:00
|
|
|
}
|
|
|
|
if(!sftp){
|
|
|
|
ssh_message_reply_default(message);
|
|
|
|
}
|
|
|
|
ssh_message_free(message);
|
|
|
|
} while (message && !sftp);
|
|
|
|
if(!sftp){
|
|
|
|
printf("error : %s\n",ssh_get_error(session));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
printf("it works !\n");
|
2009-02-06 12:59:54 +03:00
|
|
|
buf=buffer_new();
|
2005-08-13 16:58:41 +04:00
|
|
|
do{
|
2009-05-04 14:06:49 +04:00
|
|
|
i=channel_read_buffer(chan,buf,0,0);
|
2005-08-13 16:58:41 +04:00
|
|
|
if(i>0)
|
|
|
|
write(1,buffer_get(buf),buffer_get_len(buf));
|
|
|
|
} while (i>0);
|
2008-07-01 02:28:11 +04:00
|
|
|
buffer_free(buf);
|
2005-08-18 14:08:20 +04:00
|
|
|
ssh_disconnect(session);
|
2008-07-01 02:28:11 +04:00
|
|
|
ssh_bind_free(ssh_bind);
|
2006-07-09 14:36:44 +04:00
|
|
|
ssh_finalize();
|
2005-07-05 05:21:44 +04:00
|
|
|
return 0;
|
|
|
|
}
|
2005-08-07 14:48:08 +04:00
|
|
|
|