diff --git a/CMakeLists.txt b/CMakeLists.txt index 4ed88ae0..821bed4c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -55,6 +55,8 @@ configure_file(config.h.cmake ${CMAKE_CURRENT_BINARY_DIR}/config.h) add_subdirectory(doc) add_subdirectory(include) add_subdirectory(libssh) +add_subdirectory(examples) +add_subdirectory(tests) # build samples include_directories(${CMAKE_SOURCE_DIR}/include) diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt new file mode 100644 index 00000000..3f6dd063 --- /dev/null +++ b/examples/CMakeLists.txt @@ -0,0 +1,15 @@ +project(libssh-library C) + +set(examples_SRCS + libssh_scp.c + authentication.c + knownhosts.c +) + +include_directories( + ${LIBSSH_PUBLIC_INCLUDE_DIRS} +) + +add_executable(libssh_scp ${examples_SRCS}) + +target_link_libraries(libssh_scp ${LIBSSH_SHARED_LIBRARY} ) diff --git a/examples/authentication.c b/examples/authentication.c new file mode 100644 index 00000000..06a0b713 --- /dev/null +++ b/examples/authentication.c @@ -0,0 +1,114 @@ +/* + * authentication.c + * This file contains an example of how to do an authentication to a + * SSH server using libssh + */ + +/* +Copyright 2003-2009 Aris Adamantiadis + +This file is part of the SSH Library + +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 +#include +#include +#include + +#include +#include "examples_common.h" + +int authenticate_kbdint(ssh_session session){ + int err=ssh_userauth_kbdint(session,NULL,NULL); + const char *name, *instruction, *prompt; + char *ptr; + char buffer[128]; + int i,n; + char echo; + while (err==SSH_AUTH_INFO){ + name=ssh_userauth_kbdint_getname(session); + instruction=ssh_userauth_kbdint_getinstruction(session); + n=ssh_userauth_kbdint_getnprompts(session); + if(strlen(name)>0) + printf("%s\n",name); + if(strlen(instruction)>0) + printf("%s\n",instruction); + for(i=0;i +#include +#include +#include +#include + +#include +#include "examples_common.h" + +int verify_knownhost(ssh_session session){ + char *hexa; + int state; + char buf[10]; + unsigned char *hash = NULL; + int hlen; + + state=ssh_is_server_known(session); + + hlen = ssh_get_pubkey_hash(session, &hash); + if (hlen < 0) { + ssh_disconnect(session); + ssh_finalize(); + return 1; + } + switch(state){ + case SSH_SERVER_KNOWN_OK: + break; /* ok */ + case SSH_SERVER_KNOWN_CHANGED: + fprintf(stderr,"Host key for server changed : server's one is now :\n"); + ssh_print_hexa("Public key hash",hash, hlen); + free(hash); + fprintf(stderr,"For security reason, connection will be stopped\n"); + ssh_disconnect(session); + ssh_finalize(); + exit(-1); + case SSH_SERVER_FOUND_OTHER: + fprintf(stderr,"The host key for this server was not found but an other type of key exists.\n"); + fprintf(stderr,"An attacker might change the default server key to confuse your client" + "into thinking the key does not exist\n" + "We advise you to rerun the client with -d or -r for more safety.\n"); + ssh_disconnect(session); + ssh_finalize(); + exit(-1); + case SSH_SERVER_FILE_NOT_FOUND: + fprintf(stderr,"Could not find known host file. If you accept the host key here,\n"); + fprintf(stderr,"the file will be automatically created.\n"); + /* fallback to SSH_SERVER_NOT_KNOWN behavior */ + case SSH_SERVER_NOT_KNOWN: + hexa = ssh_get_hexa(hash, hlen); + fprintf(stderr,"The server is unknown. Do you trust the host key ?\n"); + fprintf(stderr, "Public key hash: %s\n", hexa); + free(hexa); + fgets(buf,sizeof(buf),stdin); + if(strncasecmp(buf,"yes",3)!=0){ + ssh_disconnect(session); + exit(-1); + } + fprintf(stderr,"This new key will be written on disk for further usage. do you agree ?\n"); + fgets(buf,sizeof(buf),stdin); + if(strncasecmp(buf,"yes",3)==0){ + if (ssh_write_knownhost(session) < 0) { + free(hash); + fprintf(stderr, "error %s\n", strerror(errno)); + exit(-1); + } + } + + break; + case SSH_SERVER_ERROR: + free(hash); + fprintf(stderr,"%s",ssh_get_error(session)); + ssh_disconnect(session); + ssh_finalize(); + exit(-1); + } + free(hash); + return 0; +} diff --git a/examples/libssh_scp.c b/examples/libssh_scp.c new file mode 100644 index 00000000..b31c6c65 --- /dev/null +++ b/examples/libssh_scp.c @@ -0,0 +1,220 @@ +/* libssh_scp.c + * Sample implementation of a SCP client + */ + +/* +Copyright 2009 Aris Adamantiadis + +This file is part of the SSH Library + +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. + */ + +#include +#include +#include +#include + +#include +#include "examples_common.h" + +char *host; +char *user; +int sftp; + +static void usage(const char *argv0){ + fprintf(stderr,"Usage : %s [options] [login@]hostname\n" + "sample scp client - libssh-%s\n" + "Options :\n" + " -l user : log in as user\n" + " -p port : connect to port\n" + " -d : use DSS to verify host public key\n" + " -r : use RSA to verify host public key\n", + argv0, + ssh_version(0)); + exit(0); +} + +static int opts(int argc, char **argv){ + int i; + if(strstr(argv[0],"sftp")) + sftp=1; + // for(i=0;i