diff options
Diffstat (limited to 'libssh2-sys/libssh2-1.5.0/example')
26 files changed, 7549 insertions, 0 deletions
diff --git a/libssh2-sys/libssh2-1.5.0/example/Makefile.am b/libssh2-sys/libssh2-1.5.0/example/Makefile.am new file mode 100644 index 0000000..8c6636f --- /dev/null +++ b/libssh2-sys/libssh2-1.5.0/example/Makefile.am @@ -0,0 +1,17 @@ +AUTOMAKE_OPTIONS = foreign nostdinc + +EXTRA_DIST = libssh2_config.h.in + +# samples +noinst_PROGRAMS = direct_tcpip ssh2 scp scp_nonblock scp_write \ + scp_write_nonblock sftp sftp_nonblock sftp_write sftp_write_nonblock \ + sftp_mkdir sftp_mkdir_nonblock sftp_RW_nonblock sftp_write_sliding \ + sftpdir sftpdir_nonblock ssh2_exec ssh2_agent ssh2_echo sftp_append \ + subsystem_netconf tcpip-forward + +if HAVE_SYS_UN_H +noinst_PROGRAMS += x11 +endif + +AM_CPPFLAGS = -I$(top_srcdir)/include -I$(top_builddir)/example +LDADD = $(top_builddir)/src/libssh2.la diff --git a/libssh2-sys/libssh2-1.5.0/example/Makefile.in b/libssh2-sys/libssh2-1.5.0/example/Makefile.in new file mode 100644 index 0000000..ed7001e --- /dev/null +++ b/libssh2-sys/libssh2-1.5.0/example/Makefile.in @@ -0,0 +1,834 @@ +# Makefile.in generated by automake 1.14.1 from Makefile.am. +# @configure_input@ + +# Copyright (C) 1994-2013 Free Software Foundation, Inc. + +# This Makefile.in is free software; the Free Software Foundation +# gives unlimited permission to copy and/or distribute it, +# with or without modifications, as long as this notice is preserved. + +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY, to the extent permitted by law; without +# even the implied warranty of MERCHANTABILITY or FITNESS FOR A +# PARTICULAR PURPOSE. + +@SET_MAKE@ + +VPATH = @srcdir@ +am__is_gnu_make = test -n '$(MAKEFILE_LIST)' && test -n '$(MAKELEVEL)' +am__make_running_with_option = \ + case $${target_option-} in \ + ?) ;; \ + *) echo "am__make_running_with_option: internal error: invalid" \ + "target option '$${target_option-}' specified" >&2; \ + exit 1;; \ + esac; \ + has_opt=no; \ + sane_makeflags=$$MAKEFLAGS; \ + if $(am__is_gnu_make); then \ + sane_makeflags=$$MFLAGS; \ + else \ + case $$MAKEFLAGS in \ + *\\[\ \ ]*) \ + bs=\\; \ + sane_makeflags=`printf '%s\n' "$$MAKEFLAGS" \ + | sed "s/$$bs$$bs[$$bs $$bs ]*//g"`;; \ + esac; \ + fi; \ + skip_next=no; \ + strip_trailopt () \ + { \ + flg=`printf '%s\n' "$$flg" | sed "s/$$1.*$$//"`; \ + }; \ + for flg in $$sane_makeflags; do \ + test $$skip_next = yes && { skip_next=no; continue; }; \ + case $$flg in \ + *=*|--*) continue;; \ + -*I) strip_trailopt 'I'; skip_next=yes;; \ + -*I?*) strip_trailopt 'I';; \ + -*O) strip_trailopt 'O'; skip_next=yes;; \ + -*O?*) strip_trailopt 'O';; \ + -*l) strip_trailopt 'l'; skip_next=yes;; \ + -*l?*) strip_trailopt 'l';; \ + -[dEDm]) skip_next=yes;; \ + -[JT]) skip_next=yes;; \ + esac; \ + case $$flg in \ + *$$target_option*) has_opt=yes; break;; \ + esac; \ + done; \ + test $$has_opt = yes +am__make_dryrun = (target_option=n; $(am__make_running_with_option)) +am__make_keepgoing = (target_option=k; $(am__make_running_with_option)) +pkgdatadir = $(datadir)/@PACKAGE@ +pkgincludedir = $(includedir)/@PACKAGE@ +pkglibdir = $(libdir)/@PACKAGE@ +pkglibexecdir = $(libexecdir)/@PACKAGE@ +am__cd = CDPATH="$${ZSH_VERSION+.}$(PATH_SEPARATOR)" && cd +install_sh_DATA = $(install_sh) -c -m 644 +install_sh_PROGRAM = $(install_sh) -c +install_sh_SCRIPT = $(install_sh) -c +INSTALL_HEADER = $(INSTALL_DATA) +transform = $(program_transform_name) +NORMAL_INSTALL = : +PRE_INSTALL = : +POST_INSTALL = : +NORMAL_UNINSTALL = : +PRE_UNINSTALL = : +POST_UNINSTALL = : +build_triplet = @build@ +host_triplet = @host@ +noinst_PROGRAMS = direct_tcpip$(EXEEXT) ssh2$(EXEEXT) scp$(EXEEXT) \ + scp_nonblock$(EXEEXT) scp_write$(EXEEXT) \ + scp_write_nonblock$(EXEEXT) sftp$(EXEEXT) \ + sftp_nonblock$(EXEEXT) sftp_write$(EXEEXT) \ + sftp_write_nonblock$(EXEEXT) sftp_mkdir$(EXEEXT) \ + sftp_mkdir_nonblock$(EXEEXT) sftp_RW_nonblock$(EXEEXT) \ + sftp_write_sliding$(EXEEXT) sftpdir$(EXEEXT) \ + sftpdir_nonblock$(EXEEXT) ssh2_exec$(EXEEXT) \ + ssh2_agent$(EXEEXT) ssh2_echo$(EXEEXT) sftp_append$(EXEEXT) \ + subsystem_netconf$(EXEEXT) tcpip-forward$(EXEEXT) \ + $(am__EXEEXT_1) +@HAVE_SYS_UN_H_TRUE@am__append_1 = x11 +subdir = example +DIST_COMMON = $(srcdir)/Makefile.in $(srcdir)/Makefile.am \ + $(srcdir)/libssh2_config.h.in $(top_srcdir)/depcomp +ACLOCAL_M4 = $(top_srcdir)/aclocal.m4 +am__aclocal_m4_deps = $(top_srcdir)/m4/autobuild.m4 \ + $(top_srcdir)/m4/lib-ld.m4 $(top_srcdir)/m4/lib-link.m4 \ + $(top_srcdir)/m4/lib-prefix.m4 $(top_srcdir)/m4/libtool.m4 \ + $(top_srcdir)/m4/ltoptions.m4 $(top_srcdir)/m4/ltsugar.m4 \ + $(top_srcdir)/m4/ltversion.m4 $(top_srcdir)/m4/lt~obsolete.m4 \ + $(top_srcdir)/acinclude.m4 $(top_srcdir)/configure.ac +am__configure_deps = $(am__aclocal_m4_deps) $(CONFIGURE_DEPENDENCIES) \ + $(ACLOCAL_M4) +mkinstalldirs = $(install_sh) -d +CONFIG_HEADER = $(top_builddir)/src/libssh2_config.h libssh2_config.h +CONFIG_CLEAN_FILES = +CONFIG_CLEAN_VPATH_FILES = +@HAVE_SYS_UN_H_TRUE@am__EXEEXT_1 = x11$(EXEEXT) +PROGRAMS = $(noinst_PROGRAMS) +direct_tcpip_SOURCES = direct_tcpip.c +direct_tcpip_OBJECTS = direct_tcpip.$(OBJEXT) +direct_tcpip_LDADD = $(LDADD) +direct_tcpip_DEPENDENCIES = $(top_builddir)/src/libssh2.la +AM_V_lt = $(am__v_lt_@AM_V@) +am__v_lt_ = $(am__v_lt_@AM_DEFAULT_V@) +am__v_lt_0 = --silent +am__v_lt_1 = +scp_SOURCES = scp.c +scp_OBJECTS = scp.$(OBJEXT) +scp_LDADD = $(LDADD) +scp_DEPENDENCIES = $(top_builddir)/src/libssh2.la +scp_nonblock_SOURCES = scp_nonblock.c +scp_nonblock_OBJECTS = scp_nonblock.$(OBJEXT) +scp_nonblock_LDADD = $(LDADD) +scp_nonblock_DEPENDENCIES = $(top_builddir)/src/libssh2.la +scp_write_SOURCES = scp_write.c +scp_write_OBJECTS = scp_write.$(OBJEXT) +scp_write_LDADD = $(LDADD) +scp_write_DEPENDENCIES = $(top_builddir)/src/libssh2.la +scp_write_nonblock_SOURCES = scp_write_nonblock.c +scp_write_nonblock_OBJECTS = scp_write_nonblock.$(OBJEXT) +scp_write_nonblock_LDADD = $(LDADD) +scp_write_nonblock_DEPENDENCIES = $(top_builddir)/src/libssh2.la +sftp_SOURCES = sftp.c +sftp_OBJECTS = sftp.$(OBJEXT) +sftp_LDADD = $(LDADD) +sftp_DEPENDENCIES = $(top_builddir)/src/libssh2.la +sftp_RW_nonblock_SOURCES = sftp_RW_nonblock.c +sftp_RW_nonblock_OBJECTS = sftp_RW_nonblock.$(OBJEXT) +sftp_RW_nonblock_LDADD = $(LDADD) +sftp_RW_nonblock_DEPENDENCIES = $(top_builddir)/src/libssh2.la +sftp_append_SOURCES = sftp_append.c +sftp_append_OBJECTS = sftp_append.$(OBJEXT) +sftp_append_LDADD = $(LDADD) +sftp_append_DEPENDENCIES = $(top_builddir)/src/libssh2.la +sftp_mkdir_SOURCES = sftp_mkdir.c +sftp_mkdir_OBJECTS = sftp_mkdir.$(OBJEXT) +sftp_mkdir_LDADD = $(LDADD) +sftp_mkdir_DEPENDENCIES = $(top_builddir)/src/libssh2.la +sftp_mkdir_nonblock_SOURCES = sftp_mkdir_nonblock.c +sftp_mkdir_nonblock_OBJECTS = sftp_mkdir_nonblock.$(OBJEXT) +sftp_mkdir_nonblock_LDADD = $(LDADD) +sftp_mkdir_nonblock_DEPENDENCIES = $(top_builddir)/src/libssh2.la +sftp_nonblock_SOURCES = sftp_nonblock.c +sftp_nonblock_OBJECTS = sftp_nonblock.$(OBJEXT) +sftp_nonblock_LDADD = $(LDADD) +sftp_nonblock_DEPENDENCIES = $(top_builddir)/src/libssh2.la +sftp_write_SOURCES = sftp_write.c +sftp_write_OBJECTS = sftp_write.$(OBJEXT) +sftp_write_LDADD = $(LDADD) +sftp_write_DEPENDENCIES = $(top_builddir)/src/libssh2.la +sftp_write_nonblock_SOURCES = sftp_write_nonblock.c +sftp_write_nonblock_OBJECTS = sftp_write_nonblock.$(OBJEXT) +sftp_write_nonblock_LDADD = $(LDADD) +sftp_write_nonblock_DEPENDENCIES = $(top_builddir)/src/libssh2.la +sftp_write_sliding_SOURCES = sftp_write_sliding.c +sftp_write_sliding_OBJECTS = sftp_write_sliding.$(OBJEXT) +sftp_write_sliding_LDADD = $(LDADD) +sftp_write_sliding_DEPENDENCIES = $(top_builddir)/src/libssh2.la +sftpdir_SOURCES = sftpdir.c +sftpdir_OBJECTS = sftpdir.$(OBJEXT) +sftpdir_LDADD = $(LDADD) +sftpdir_DEPENDENCIES = $(top_builddir)/src/libssh2.la +sftpdir_nonblock_SOURCES = sftpdir_nonblock.c +sftpdir_nonblock_OBJECTS = sftpdir_nonblock.$(OBJEXT) +sftpdir_nonblock_LDADD = $(LDADD) +sftpdir_nonblock_DEPENDENCIES = $(top_builddir)/src/libssh2.la +ssh2_SOURCES = ssh2.c +ssh2_OBJECTS = ssh2.$(OBJEXT) +ssh2_LDADD = $(LDADD) +ssh2_DEPENDENCIES = $(top_builddir)/src/libssh2.la +ssh2_agent_SOURCES = ssh2_agent.c +ssh2_agent_OBJECTS = ssh2_agent.$(OBJEXT) +ssh2_agent_LDADD = $(LDADD) +ssh2_agent_DEPENDENCIES = $(top_builddir)/src/libssh2.la +ssh2_echo_SOURCES = ssh2_echo.c +ssh2_echo_OBJECTS = ssh2_echo.$(OBJEXT) +ssh2_echo_LDADD = $(LDADD) +ssh2_echo_DEPENDENCIES = $(top_builddir)/src/libssh2.la +ssh2_exec_SOURCES = ssh2_exec.c +ssh2_exec_OBJECTS = ssh2_exec.$(OBJEXT) +ssh2_exec_LDADD = $(LDADD) +ssh2_exec_DEPENDENCIES = $(top_builddir)/src/libssh2.la +subsystem_netconf_SOURCES = subsystem_netconf.c +subsystem_netconf_OBJECTS = subsystem_netconf.$(OBJEXT) +subsystem_netconf_LDADD = $(LDADD) +subsystem_netconf_DEPENDENCIES = $(top_builddir)/src/libssh2.la +tcpip_forward_SOURCES = tcpip-forward.c +tcpip_forward_OBJECTS = tcpip-forward.$(OBJEXT) +tcpip_forward_LDADD = $(LDADD) +tcpip_forward_DEPENDENCIES = $(top_builddir)/src/libssh2.la +x11_SOURCES = x11.c +x11_OBJECTS = x11.$(OBJEXT) +x11_LDADD = $(LDADD) +x11_DEPENDENCIES = $(top_builddir)/src/libssh2.la +AM_V_P = $(am__v_P_@AM_V@) +am__v_P_ = $(am__v_P_@AM_DEFAULT_V@) +am__v_P_0 = false +am__v_P_1 = : +AM_V_GEN = $(am__v_GEN_@AM_V@) +am__v_GEN_ = $(am__v_GEN_@AM_DEFAULT_V@) +am__v_GEN_0 = @echo " GEN " $@; +am__v_GEN_1 = +AM_V_at = $(am__v_at_@AM_V@) +am__v_at_ = $(am__v_at_@AM_DEFAULT_V@) +am__v_at_0 = @ +am__v_at_1 = +DEFAULT_INCLUDES = +depcomp = $(SHELL) $(top_srcdir)/depcomp +am__depfiles_maybe = depfiles +am__mv = mv -f +COMPILE = $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) \ + $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) +LTCOMPILE = $(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) \ + $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) \ + $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) \ + $(AM_CFLAGS) $(CFLAGS) +AM_V_CC = $(am__v_CC_@AM_V@) +am__v_CC_ = $(am__v_CC_@AM_DEFAULT_V@) +am__v_CC_0 = @echo " CC " $@; +am__v_CC_1 = +CCLD = $(CC) +LINK = $(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) \ + $(LIBTOOLFLAGS) --mode=link $(CCLD) $(AM_CFLAGS) $(CFLAGS) \ + $(AM_LDFLAGS) $(LDFLAGS) -o $@ +AM_V_CCLD = $(am__v_CCLD_@AM_V@) +am__v_CCLD_ = $(am__v_CCLD_@AM_DEFAULT_V@) +am__v_CCLD_0 = @echo " CCLD " $@; +am__v_CCLD_1 = +SOURCES = direct_tcpip.c scp.c scp_nonblock.c scp_write.c \ + scp_write_nonblock.c sftp.c sftp_RW_nonblock.c sftp_append.c \ + sftp_mkdir.c sftp_mkdir_nonblock.c sftp_nonblock.c \ + sftp_write.c sftp_write_nonblock.c sftp_write_sliding.c \ + sftpdir.c sftpdir_nonblock.c ssh2.c ssh2_agent.c ssh2_echo.c \ + ssh2_exec.c subsystem_netconf.c tcpip-forward.c x11.c +DIST_SOURCES = direct_tcpip.c scp.c scp_nonblock.c scp_write.c \ + scp_write_nonblock.c sftp.c sftp_RW_nonblock.c sftp_append.c \ + sftp_mkdir.c sftp_mkdir_nonblock.c sftp_nonblock.c \ + sftp_write.c sftp_write_nonblock.c sftp_write_sliding.c \ + sftpdir.c sftpdir_nonblock.c ssh2.c ssh2_agent.c ssh2_echo.c \ + ssh2_exec.c subsystem_netconf.c tcpip-forward.c x11.c +am__can_run_installinfo = \ + case $$AM_UPDATE_INFO_DIR in \ + n|no|NO) false;; \ + *) (install-info --version) >/dev/null 2>&1;; \ + esac +am__tagged_files = $(HEADERS) $(SOURCES) $(TAGS_FILES) \ + $(LISP)libssh2_config.h.in +# Read a list of newline-separated strings from the standard input, +# and print each of them once, without duplicates. Input order is +# *not* preserved. +am__uniquify_input = $(AWK) '\ + BEGIN { nonempty = 0; } \ + { items[$$0] = 1; nonempty = 1; } \ + END { if (nonempty) { for (i in items) print i; }; } \ +' +# Make sure the list of sources is unique. This is necessary because, +# e.g., the same source file might be shared among _SOURCES variables +# for different programs/libraries. +am__define_uniq_tagged_files = \ + list='$(am__tagged_files)'; \ + unique=`for i in $$list; do \ + if test -f "$$i"; then echo $$i; else echo $(srcdir)/$$i; fi; \ + done | $(am__uniquify_input)` +ETAGS = etags +CTAGS = ctags +DISTFILES = $(DIST_COMMON) $(DIST_SOURCES) $(TEXINFOS) $(EXTRA_DIST) +ACLOCAL = @ACLOCAL@ +ALLOCA = @ALLOCA@ +AMTAR = @AMTAR@ +AM_DEFAULT_VERBOSITY = @AM_DEFAULT_VERBOSITY@ +AR = @AR@ +AS = @AS@ +AUTOCONF = @AUTOCONF@ +AUTOHEADER = @AUTOHEADER@ +AUTOMAKE = @AUTOMAKE@ +AWK = @AWK@ +CC = @CC@ +CCDEPMODE = @CCDEPMODE@ +CFLAGS = @CFLAGS@ +CPP = @CPP@ +CPPFLAGS = @CPPFLAGS@ +CYGPATH_W = @CYGPATH_W@ +DEFS = @DEFS@ +DEPDIR = @DEPDIR@ +DLLTOOL = @DLLTOOL@ +DSYMUTIL = @DSYMUTIL@ +DUMPBIN = @DUMPBIN@ +ECHO_C = @ECHO_C@ +ECHO_N = @ECHO_N@ +ECHO_T = @ECHO_T@ +EGREP = @EGREP@ +EXEEXT = @EXEEXT@ +FGREP = @FGREP@ +GREP = @GREP@ +HAVE_LIBBCRYPT = @HAVE_LIBBCRYPT@ +HAVE_LIBCRYPT32 = @HAVE_LIBCRYPT32@ +HAVE_LIBGCRYPT = @HAVE_LIBGCRYPT@ +HAVE_LIBSSL = @HAVE_LIBSSL@ +HAVE_LIBZ = @HAVE_LIBZ@ +INSTALL = @INSTALL@ +INSTALL_DATA = @INSTALL_DATA@ +INSTALL_PROGRAM = @INSTALL_PROGRAM@ +INSTALL_SCRIPT = @INSTALL_SCRIPT@ +INSTALL_STRIP_PROGRAM = @INSTALL_STRIP_PROGRAM@ +LD = @LD@ +LDFLAGS = @LDFLAGS@ +LIBBCRYPT = @LIBBCRYPT@ +LIBBCRYPT_PREFIX = @LIBBCRYPT_PREFIX@ +LIBCRYPT32 = @LIBCRYPT32@ +LIBCRYPT32_PREFIX = @LIBCRYPT32_PREFIX@ +LIBGCRYPT = @LIBGCRYPT@ +LIBGCRYPT_PREFIX = @LIBGCRYPT_PREFIX@ +LIBOBJS = @LIBOBJS@ +LIBS = @LIBS@ +LIBSREQUIRED = @LIBSREQUIRED@ +LIBSSH2VER = @LIBSSH2VER@ +LIBSSL = @LIBSSL@ +LIBSSL_PREFIX = @LIBSSL_PREFIX@ +LIBTOOL = @LIBTOOL@ +LIBZ = @LIBZ@ +LIBZ_PREFIX = @LIBZ_PREFIX@ +LIPO = @LIPO@ +LN_S = @LN_S@ +LTLIBBCRYPT = @LTLIBBCRYPT@ +LTLIBCRYPT32 = @LTLIBCRYPT32@ +LTLIBGCRYPT = @LTLIBGCRYPT@ +LTLIBOBJS = @LTLIBOBJS@ +LTLIBSSL = @LTLIBSSL@ +LTLIBZ = @LTLIBZ@ +MAINT = @MAINT@ +MAKEINFO = @MAKEINFO@ +MANIFEST_TOOL = @MANIFEST_TOOL@ +MKDIR_P = @MKDIR_P@ +NM = @NM@ +NMEDIT = @NMEDIT@ +OBJDUMP = @OBJDUMP@ +OBJEXT = @OBJEXT@ +OTOOL = @OTOOL@ +OTOOL64 = @OTOOL64@ +PACKAGE = @PACKAGE@ +PACKAGE_BUGREPORT = @PACKAGE_BUGREPORT@ +PACKAGE_NAME = @PACKAGE_NAME@ +PACKAGE_STRING = @PACKAGE_STRING@ +PACKAGE_TARNAME = @PACKAGE_TARNAME@ +PACKAGE_URL = @PACKAGE_URL@ +PACKAGE_VERSION = @PACKAGE_VERSION@ +PATH_SEPARATOR = @PATH_SEPARATOR@ +RANLIB = @RANLIB@ +SED = @SED@ +SET_MAKE = @SET_MAKE@ +SHELL = @SHELL@ +SSHD = @SSHD@ +STRIP = @STRIP@ +VERSION = @VERSION@ +abs_builddir = @abs_builddir@ +abs_srcdir = @abs_srcdir@ +abs_top_builddir = @abs_top_builddir@ +abs_top_srcdir = @abs_top_srcdir@ +ac_ct_AR = @ac_ct_AR@ +ac_ct_CC = @ac_ct_CC@ +ac_ct_DUMPBIN = @ac_ct_DUMPBIN@ +am__include = @am__include@ +am__leading_dot = @am__leading_dot@ +am__quote = @am__quote@ +am__tar = @am__tar@ +am__untar = @am__untar@ +bindir = @bindir@ +build = @build@ +build_alias = @build_alias@ +build_cpu = @build_cpu@ +build_os = @build_os@ +build_vendor = @build_vendor@ +builddir = @builddir@ +datadir = @datadir@ +datarootdir = @datarootdir@ +docdir = @docdir@ +dvidir = @dvidir@ +exec_prefix = @exec_prefix@ +host = @host@ +host_alias = @host_alias@ +host_cpu = @host_cpu@ +host_os = @host_os@ +host_vendor = @host_vendor@ +htmldir = @htmldir@ +includedir = @includedir@ +infodir = @infodir@ +install_sh = @install_sh@ +libdir = @libdir@ +libexecdir = @libexecdir@ +localedir = @localedir@ +localstatedir = @localstatedir@ +mandir = @mandir@ +mkdir_p = @mkdir_p@ +oldincludedir = @oldincludedir@ +pdfdir = @pdfdir@ +prefix = @prefix@ +program_transform_name = @program_transform_name@ +psdir = @psdir@ +sbindir = @sbindir@ +sharedstatedir = @sharedstatedir@ +srcdir = @srcdir@ +sysconfdir = @sysconfdir@ +target_alias = @target_alias@ +top_build_prefix = @top_build_prefix@ +top_builddir = @top_builddir@ +top_srcdir = @top_srcdir@ +AUTOMAKE_OPTIONS = foreign nostdinc +EXTRA_DIST = libssh2_config.h.in +AM_CPPFLAGS = -I$(top_srcdir)/include -I$(top_builddir)/example +LDADD = $(top_builddir)/src/libssh2.la +all: libssh2_config.h + $(MAKE) $(AM_MAKEFLAGS) all-am + +.SUFFIXES: +.SUFFIXES: .c .lo .o .obj +$(srcdir)/Makefile.in: @MAINTAINER_MODE_TRUE@ $(srcdir)/Makefile.am $(am__configure_deps) + @for dep in $?; do \ + case '$(am__configure_deps)' in \ + *$$dep*) \ + ( cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh ) \ + && { if test -f $@; then exit 0; else break; fi; }; \ + exit 1;; \ + esac; \ + done; \ + echo ' cd $(top_srcdir) && $(AUTOMAKE) --foreign example/Makefile'; \ + $(am__cd) $(top_srcdir) && \ + $(AUTOMAKE) --foreign example/Makefile +.PRECIOUS: Makefile +Makefile: $(srcdir)/Makefile.in $(top_builddir)/config.status + @case '$?' in \ + *config.status*) \ + cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh;; \ + *) \ + echo ' cd $(top_builddir) && $(SHELL) ./config.status $(subdir)/$@ $(am__depfiles_maybe)'; \ + cd $(top_builddir) && $(SHELL) ./config.status $(subdir)/$@ $(am__depfiles_maybe);; \ + esac; + +$(top_builddir)/config.status: $(top_srcdir)/configure $(CONFIG_STATUS_DEPENDENCIES) + cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh + +$(top_srcdir)/configure: @MAINTAINER_MODE_TRUE@ $(am__configure_deps) + cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh +$(ACLOCAL_M4): @MAINTAINER_MODE_TRUE@ $(am__aclocal_m4_deps) + cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh +$(am__aclocal_m4_deps): + +libssh2_config.h: stamp-h2 + @test -f $@ || rm -f stamp-h2 + @test -f $@ || $(MAKE) $(AM_MAKEFLAGS) stamp-h2 + +stamp-h2: $(srcdir)/libssh2_config.h.in $(top_builddir)/config.status + @rm -f stamp-h2 + cd $(top_builddir) && $(SHELL) ./config.status example/libssh2_config.h + +distclean-hdr: + -rm -f libssh2_config.h stamp-h2 + +clean-noinstPROGRAMS: + @list='$(noinst_PROGRAMS)'; test -n "$$list" || exit 0; \ + echo " rm -f" $$list; \ + rm -f $$list || exit $$?; \ + test -n "$(EXEEXT)" || exit 0; \ + list=`for p in $$list; do echo "$$p"; done | sed 's/$(EXEEXT)$$//'`; \ + echo " rm -f" $$list; \ + rm -f $$list + +direct_tcpip$(EXEEXT): $(direct_tcpip_OBJECTS) $(direct_tcpip_DEPENDENCIES) $(EXTRA_direct_tcpip_DEPENDENCIES) + @rm -f direct_tcpip$(EXEEXT) + $(AM_V_CCLD)$(LINK) $(direct_tcpip_OBJECTS) $(direct_tcpip_LDADD) $(LIBS) + +scp$(EXEEXT): $(scp_OBJECTS) $(scp_DEPENDENCIES) $(EXTRA_scp_DEPENDENCIES) + @rm -f scp$(EXEEXT) + $(AM_V_CCLD)$(LINK) $(scp_OBJECTS) $(scp_LDADD) $(LIBS) + +scp_nonblock$(EXEEXT): $(scp_nonblock_OBJECTS) $(scp_nonblock_DEPENDENCIES) $(EXTRA_scp_nonblock_DEPENDENCIES) + @rm -f scp_nonblock$(EXEEXT) + $(AM_V_CCLD)$(LINK) $(scp_nonblock_OBJECTS) $(scp_nonblock_LDADD) $(LIBS) + +scp_write$(EXEEXT): $(scp_write_OBJECTS) $(scp_write_DEPENDENCIES) $(EXTRA_scp_write_DEPENDENCIES) + @rm -f scp_write$(EXEEXT) + $(AM_V_CCLD)$(LINK) $(scp_write_OBJECTS) $(scp_write_LDADD) $(LIBS) + +scp_write_nonblock$(EXEEXT): $(scp_write_nonblock_OBJECTS) $(scp_write_nonblock_DEPENDENCIES) $(EXTRA_scp_write_nonblock_DEPENDENCIES) + @rm -f scp_write_nonblock$(EXEEXT) + $(AM_V_CCLD)$(LINK) $(scp_write_nonblock_OBJECTS) $(scp_write_nonblock_LDADD) $(LIBS) + +sftp$(EXEEXT): $(sftp_OBJECTS) $(sftp_DEPENDENCIES) $(EXTRA_sftp_DEPENDENCIES) + @rm -f sftp$(EXEEXT) + $(AM_V_CCLD)$(LINK) $(sftp_OBJECTS) $(sftp_LDADD) $(LIBS) + +sftp_RW_nonblock$(EXEEXT): $(sftp_RW_nonblock_OBJECTS) $(sftp_RW_nonblock_DEPENDENCIES) $(EXTRA_sftp_RW_nonblock_DEPENDENCIES) + @rm -f sftp_RW_nonblock$(EXEEXT) + $(AM_V_CCLD)$(LINK) $(sftp_RW_nonblock_OBJECTS) $(sftp_RW_nonblock_LDADD) $(LIBS) + +sftp_append$(EXEEXT): $(sftp_append_OBJECTS) $(sftp_append_DEPENDENCIES) $(EXTRA_sftp_append_DEPENDENCIES) + @rm -f sftp_append$(EXEEXT) + $(AM_V_CCLD)$(LINK) $(sftp_append_OBJECTS) $(sftp_append_LDADD) $(LIBS) + +sftp_mkdir$(EXEEXT): $(sftp_mkdir_OBJECTS) $(sftp_mkdir_DEPENDENCIES) $(EXTRA_sftp_mkdir_DEPENDENCIES) + @rm -f sftp_mkdir$(EXEEXT) + $(AM_V_CCLD)$(LINK) $(sftp_mkdir_OBJECTS) $(sftp_mkdir_LDADD) $(LIBS) + +sftp_mkdir_nonblock$(EXEEXT): $(sftp_mkdir_nonblock_OBJECTS) $(sftp_mkdir_nonblock_DEPENDENCIES) $(EXTRA_sftp_mkdir_nonblock_DEPENDENCIES) + @rm -f sftp_mkdir_nonblock$(EXEEXT) + $(AM_V_CCLD)$(LINK) $(sftp_mkdir_nonblock_OBJECTS) $(sftp_mkdir_nonblock_LDADD) $(LIBS) + +sftp_nonblock$(EXEEXT): $(sftp_nonblock_OBJECTS) $(sftp_nonblock_DEPENDENCIES) $(EXTRA_sftp_nonblock_DEPENDENCIES) + @rm -f sftp_nonblock$(EXEEXT) + $(AM_V_CCLD)$(LINK) $(sftp_nonblock_OBJECTS) $(sftp_nonblock_LDADD) $(LIBS) + +sftp_write$(EXEEXT): $(sftp_write_OBJECTS) $(sftp_write_DEPENDENCIES) $(EXTRA_sftp_write_DEPENDENCIES) + @rm -f sftp_write$(EXEEXT) + $(AM_V_CCLD)$(LINK) $(sftp_write_OBJECTS) $(sftp_write_LDADD) $(LIBS) + +sftp_write_nonblock$(EXEEXT): $(sftp_write_nonblock_OBJECTS) $(sftp_write_nonblock_DEPENDENCIES) $(EXTRA_sftp_write_nonblock_DEPENDENCIES) + @rm -f sftp_write_nonblock$(EXEEXT) + $(AM_V_CCLD)$(LINK) $(sftp_write_nonblock_OBJECTS) $(sftp_write_nonblock_LDADD) $(LIBS) + +sftp_write_sliding$(EXEEXT): $(sftp_write_sliding_OBJECTS) $(sftp_write_sliding_DEPENDENCIES) $(EXTRA_sftp_write_sliding_DEPENDENCIES) + @rm -f sftp_write_sliding$(EXEEXT) + $(AM_V_CCLD)$(LINK) $(sftp_write_sliding_OBJECTS) $(sftp_write_sliding_LDADD) $(LIBS) + +sftpdir$(EXEEXT): $(sftpdir_OBJECTS) $(sftpdir_DEPENDENCIES) $(EXTRA_sftpdir_DEPENDENCIES) + @rm -f sftpdir$(EXEEXT) + $(AM_V_CCLD)$(LINK) $(sftpdir_OBJECTS) $(sftpdir_LDADD) $(LIBS) + +sftpdir_nonblock$(EXEEXT): $(sftpdir_nonblock_OBJECTS) $(sftpdir_nonblock_DEPENDENCIES) $(EXTRA_sftpdir_nonblock_DEPENDENCIES) + @rm -f sftpdir_nonblock$(EXEEXT) + $(AM_V_CCLD)$(LINK) $(sftpdir_nonblock_OBJECTS) $(sftpdir_nonblock_LDADD) $(LIBS) + +ssh2$(EXEEXT): $(ssh2_OBJECTS) $(ssh2_DEPENDENCIES) $(EXTRA_ssh2_DEPENDENCIES) + @rm -f ssh2$(EXEEXT) + $(AM_V_CCLD)$(LINK) $(ssh2_OBJECTS) $(ssh2_LDADD) $(LIBS) + +ssh2_agent$(EXEEXT): $(ssh2_agent_OBJECTS) $(ssh2_agent_DEPENDENCIES) $(EXTRA_ssh2_agent_DEPENDENCIES) + @rm -f ssh2_agent$(EXEEXT) + $(AM_V_CCLD)$(LINK) $(ssh2_agent_OBJECTS) $(ssh2_agent_LDADD) $(LIBS) + +ssh2_echo$(EXEEXT): $(ssh2_echo_OBJECTS) $(ssh2_echo_DEPENDENCIES) $(EXTRA_ssh2_echo_DEPENDENCIES) + @rm -f ssh2_echo$(EXEEXT) + $(AM_V_CCLD)$(LINK) $(ssh2_echo_OBJECTS) $(ssh2_echo_LDADD) $(LIBS) + +ssh2_exec$(EXEEXT): $(ssh2_exec_OBJECTS) $(ssh2_exec_DEPENDENCIES) $(EXTRA_ssh2_exec_DEPENDENCIES) + @rm -f ssh2_exec$(EXEEXT) + $(AM_V_CCLD)$(LINK) $(ssh2_exec_OBJECTS) $(ssh2_exec_LDADD) $(LIBS) + +subsystem_netconf$(EXEEXT): $(subsystem_netconf_OBJECTS) $(subsystem_netconf_DEPENDENCIES) $(EXTRA_subsystem_netconf_DEPENDENCIES) + @rm -f subsystem_netconf$(EXEEXT) + $(AM_V_CCLD)$(LINK) $(subsystem_netconf_OBJECTS) $(subsystem_netconf_LDADD) $(LIBS) + +tcpip-forward$(EXEEXT): $(tcpip_forward_OBJECTS) $(tcpip_forward_DEPENDENCIES) $(EXTRA_tcpip_forward_DEPENDENCIES) + @rm -f tcpip-forward$(EXEEXT) + $(AM_V_CCLD)$(LINK) $(tcpip_forward_OBJECTS) $(tcpip_forward_LDADD) $(LIBS) + +x11$(EXEEXT): $(x11_OBJECTS) $(x11_DEPENDENCIES) $(EXTRA_x11_DEPENDENCIES) + @rm -f x11$(EXEEXT) + $(AM_V_CCLD)$(LINK) $(x11_OBJECTS) $(x11_LDADD) $(LIBS) + +mostlyclean-compile: + -rm -f *.$(OBJEXT) + +distclean-compile: + -rm -f *.tab.c + +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/direct_tcpip.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/scp.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/scp_nonblock.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/scp_write.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/scp_write_nonblock.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/sftp.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/sftp_RW_nonblock.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/sftp_append.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/sftp_mkdir.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/sftp_mkdir_nonblock.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/sftp_nonblock.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/sftp_write.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/sftp_write_nonblock.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/sftp_write_sliding.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/sftpdir.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/sftpdir_nonblock.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/ssh2.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/ssh2_agent.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/ssh2_echo.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/ssh2_exec.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/subsystem_netconf.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/tcpip-forward.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/x11.Po@am__quote@ + +.c.o: +@am__fastdepCC_TRUE@ $(AM_V_CC)$(COMPILE) -MT $@ -MD -MP -MF $(DEPDIR)/$*.Tpo -c -o $@ $< +@am__fastdepCC_TRUE@ $(AM_V_at)$(am__mv) $(DEPDIR)/$*.Tpo $(DEPDIR)/$*.Po +@AMDEP_TRUE@@am__fastdepCC_FALSE@ $(AM_V_CC)source='$<' object='$@' libtool=no @AMDEPBACKSLASH@ +@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@ +@am__fastdepCC_FALSE@ $(AM_V_CC@am__nodep@)$(COMPILE) -c -o $@ $< + +.c.obj: +@am__fastdepCC_TRUE@ $(AM_V_CC)$(COMPILE) -MT $@ -MD -MP -MF $(DEPDIR)/$*.Tpo -c -o $@ `$(CYGPATH_W) '$<'` +@am__fastdepCC_TRUE@ $(AM_V_at)$(am__mv) $(DEPDIR)/$*.Tpo $(DEPDIR)/$*.Po +@AMDEP_TRUE@@am__fastdepCC_FALSE@ $(AM_V_CC)source='$<' object='$@' libtool=no @AMDEPBACKSLASH@ +@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@ +@am__fastdepCC_FALSE@ $(AM_V_CC@am__nodep@)$(COMPILE) -c -o $@ `$(CYGPATH_W) '$<'` + +.c.lo: +@am__fastdepCC_TRUE@ $(AM_V_CC)$(LTCOMPILE) -MT $@ -MD -MP -MF $(DEPDIR)/$*.Tpo -c -o $@ $< +@am__fastdepCC_TRUE@ $(AM_V_at)$(am__mv) $(DEPDIR)/$*.Tpo $(DEPDIR)/$*.Plo +@AMDEP_TRUE@@am__fastdepCC_FALSE@ $(AM_V_CC)source='$<' object='$@' libtool=yes @AMDEPBACKSLASH@ +@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@ +@am__fastdepCC_FALSE@ $(AM_V_CC@am__nodep@)$(LTCOMPILE) -c -o $@ $< + +mostlyclean-libtool: + -rm -f *.lo + +clean-libtool: + -rm -rf .libs _libs + +ID: $(am__tagged_files) + $(am__define_uniq_tagged_files); mkid -fID $$unique +tags: tags-am +TAGS: tags + +tags-am: $(TAGS_DEPENDENCIES) $(am__tagged_files) + set x; \ + here=`pwd`; \ + $(am__define_uniq_tagged_files); \ + shift; \ + if test -z "$(ETAGS_ARGS)$$*$$unique"; then :; else \ + test -n "$$unique" || unique=$$empty_fix; \ + if test $$# -gt 0; then \ + $(ETAGS) $(ETAGSFLAGS) $(AM_ETAGSFLAGS) $(ETAGS_ARGS) \ + "$$@" $$unique; \ + else \ + $(ETAGS) $(ETAGSFLAGS) $(AM_ETAGSFLAGS) $(ETAGS_ARGS) \ + $$unique; \ + fi; \ + fi +ctags: ctags-am + +CTAGS: ctags +ctags-am: $(TAGS_DEPENDENCIES) $(am__tagged_files) + $(am__define_uniq_tagged_files); \ + test -z "$(CTAGS_ARGS)$$unique" \ + || $(CTAGS) $(CTAGSFLAGS) $(AM_CTAGSFLAGS) $(CTAGS_ARGS) \ + $$unique + +GTAGS: + here=`$(am__cd) $(top_builddir) && pwd` \ + && $(am__cd) $(top_srcdir) \ + && gtags -i $(GTAGS_ARGS) "$$here" +cscopelist: cscopelist-am + +cscopelist-am: $(am__tagged_files) + list='$(am__tagged_files)'; \ + case "$(srcdir)" in \ + [\\/]* | ?:[\\/]*) sdir="$(srcdir)" ;; \ + *) sdir=$(subdir)/$(srcdir) ;; \ + esac; \ + for i in $$list; do \ + if test -f "$$i"; then \ + echo "$(subdir)/$$i"; \ + else \ + echo "$$sdir/$$i"; \ + fi; \ + done >> $(top_builddir)/cscope.files + +distclean-tags: + -rm -f TAGS ID GTAGS GRTAGS GSYMS GPATH tags + +distdir: $(DISTFILES) + @srcdirstrip=`echo "$(srcdir)" | sed 's/[].[^$$\\*]/\\\\&/g'`; \ + topsrcdirstrip=`echo "$(top_srcdir)" | sed 's/[].[^$$\\*]/\\\\&/g'`; \ + list='$(DISTFILES)'; \ + dist_files=`for file in $$list; do echo $$file; done | \ + sed -e "s|^$$srcdirstrip/||;t" \ + -e "s|^$$topsrcdirstrip/|$(top_builddir)/|;t"`; \ + case $$dist_files in \ + */*) $(MKDIR_P) `echo "$$dist_files" | \ + sed '/\//!d;s|^|$(distdir)/|;s,/[^/]*$$,,' | \ + sort -u` ;; \ + esac; \ + for file in $$dist_files; do \ + if test -f $$file || test -d $$file; then d=.; else d=$(srcdir); fi; \ + if test -d $$d/$$file; then \ + dir=`echo "/$$file" | sed -e 's,/[^/]*$$,,'`; \ + if test -d "$(distdir)/$$file"; then \ + find "$(distdir)/$$file" -type d ! -perm -700 -exec chmod u+rwx {} \;; \ + fi; \ + if test -d $(srcdir)/$$file && test $$d != $(srcdir); then \ + cp -fpR $(srcdir)/$$file "$(distdir)$$dir" || exit 1; \ + find "$(distdir)/$$file" -type d ! -perm -700 -exec chmod u+rwx {} \;; \ + fi; \ + cp -fpR $$d/$$file "$(distdir)$$dir" || exit 1; \ + else \ + test -f "$(distdir)/$$file" \ + || cp -p $$d/$$file "$(distdir)/$$file" \ + || exit 1; \ + fi; \ + done +check-am: all-am +check: check-am +all-am: Makefile $(PROGRAMS) libssh2_config.h +installdirs: +install: install-am +install-exec: install-exec-am +install-data: install-data-am +uninstall: uninstall-am + +install-am: all-am + @$(MAKE) $(AM_MAKEFLAGS) install-exec-am install-data-am + +installcheck: installcheck-am +install-strip: + if test -z '$(STRIP)'; then \ + $(MAKE) $(AM_MAKEFLAGS) INSTALL_PROGRAM="$(INSTALL_STRIP_PROGRAM)" \ + install_sh_PROGRAM="$(INSTALL_STRIP_PROGRAM)" INSTALL_STRIP_FLAG=-s \ + install; \ + else \ + $(MAKE) $(AM_MAKEFLAGS) INSTALL_PROGRAM="$(INSTALL_STRIP_PROGRAM)" \ + install_sh_PROGRAM="$(INSTALL_STRIP_PROGRAM)" INSTALL_STRIP_FLAG=-s \ + "INSTALL_PROGRAM_ENV=STRIPPROG='$(STRIP)'" install; \ + fi +mostlyclean-generic: + +clean-generic: + +distclean-generic: + -test -z "$(CONFIG_CLEAN_FILES)" || rm -f $(CONFIG_CLEAN_FILES) + -test . = "$(srcdir)" || test -z "$(CONFIG_CLEAN_VPATH_FILES)" || rm -f $(CONFIG_CLEAN_VPATH_FILES) + +maintainer-clean-generic: + @echo "This command is intended for maintainers to use" + @echo "it deletes files that may require special tools to rebuild." +clean: clean-am + +clean-am: clean-generic clean-libtool clean-noinstPROGRAMS \ + mostlyclean-am + +distclean: distclean-am + -rm -rf ./$(DEPDIR) + -rm -f Makefile +distclean-am: clean-am distclean-compile distclean-generic \ + distclean-hdr distclean-tags + +dvi: dvi-am + +dvi-am: + +html: html-am + +html-am: + +info: info-am + +info-am: + +install-data-am: + +install-dvi: install-dvi-am + +install-dvi-am: + +install-exec-am: + +install-html: install-html-am + +install-html-am: + +install-info: install-info-am + +install-info-am: + +install-man: + +install-pdf: install-pdf-am + +install-pdf-am: + +install-ps: install-ps-am + +install-ps-am: + +installcheck-am: + +maintainer-clean: maintainer-clean-am + -rm -rf ./$(DEPDIR) + -rm -f Makefile +maintainer-clean-am: distclean-am maintainer-clean-generic + +mostlyclean: mostlyclean-am + +mostlyclean-am: mostlyclean-compile mostlyclean-generic \ + mostlyclean-libtool + +pdf: pdf-am + +pdf-am: + +ps: ps-am + +ps-am: + +uninstall-am: + +.MAKE: all install-am install-strip + +.PHONY: CTAGS GTAGS TAGS all all-am check check-am clean clean-generic \ + clean-libtool clean-noinstPROGRAMS cscopelist-am ctags \ + ctags-am distclean distclean-compile distclean-generic \ + distclean-hdr distclean-libtool distclean-tags distdir dvi \ + dvi-am html html-am info info-am install install-am \ + install-data install-data-am install-dvi install-dvi-am \ + install-exec install-exec-am install-html install-html-am \ + install-info install-info-am install-man install-pdf \ + install-pdf-am install-ps install-ps-am install-strip \ + installcheck installcheck-am installdirs maintainer-clean \ + maintainer-clean-generic mostlyclean mostlyclean-compile \ + mostlyclean-generic mostlyclean-libtool pdf pdf-am ps ps-am \ + tags tags-am uninstall uninstall-am + + +# Tell versions [3.59,3.63) of GNU make to not export all variables. +# Otherwise a system limit (for SysV at least) may be exceeded. +.NOEXPORT: diff --git a/libssh2-sys/libssh2-1.5.0/example/direct_tcpip.c b/libssh2-sys/libssh2-1.5.0/example/direct_tcpip.c new file mode 100644 index 0000000..49a3737 --- /dev/null +++ b/libssh2-sys/libssh2-1.5.0/example/direct_tcpip.c @@ -0,0 +1,331 @@ +#include "libssh2_config.h" +#include <libssh2.h> + +#ifdef WIN32 +#include <windows.h> +#include <winsock2.h> +#include <ws2tcpip.h> +#else +#include <sys/socket.h> +#include <netinet/in.h> +#include <arpa/inet.h> +#include <sys/time.h> +#endif + +#include <fcntl.h> +#include <errno.h> +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> +#include <sys/types.h> + +#ifdef HAVE_SYS_SELECT_H +#include <sys/select.h> +#endif + +#ifndef INADDR_NONE +#define INADDR_NONE (in_addr_t)-1 +#endif + +const char *keyfile1 = "/home/username/.ssh/id_rsa.pub"; +const char *keyfile2 = "/home/username/.ssh/id_rsa"; +const char *username = "username"; +const char *password = ""; + +const char *server_ip = "127.0.0.1"; + +const char *local_listenip = "127.0.0.1"; +unsigned int local_listenport = 2222; + +const char *remote_desthost = "localhost"; /* resolved by the server */ +unsigned int remote_destport = 22; + +enum { + AUTH_NONE = 0, + AUTH_PASSWORD, + AUTH_PUBLICKEY +}; + +int main(int argc, char *argv[]) +{ + int rc, i, auth = AUTH_NONE; + struct sockaddr_in sin; + socklen_t sinlen; + const char *fingerprint; + char *userauthlist; + LIBSSH2_SESSION *session; + LIBSSH2_CHANNEL *channel = NULL; + const char *shost; + unsigned int sport; + fd_set fds; + struct timeval tv; + ssize_t len, wr; + char buf[16384]; + +#ifdef WIN32 + char sockopt; + SOCKET sock = INVALID_SOCKET; + SOCKET listensock = INVALID_SOCKET, forwardsock = INVALID_SOCKET; + WSADATA wsadata; + int err; + + err = WSAStartup(MAKEWORD(2,0), &wsadata); + if (err != 0) { + fprintf(stderr, "WSAStartup failed with error: %d\n", err); + return 1; + } +#else + int sockopt, sock = -1; + int listensock = -1, forwardsock = -1; +#endif + + if (argc > 1) + server_ip = argv[1]; + if (argc > 2) + username = argv[2]; + if (argc > 3) + password = argv[3]; + if (argc > 4) + local_listenip = argv[4]; + if (argc > 5) + local_listenport = atoi(argv[5]); + if (argc > 6) + remote_desthost = argv[6]; + if (argc > 7) + remote_destport = atoi(argv[7]); + + rc = libssh2_init (0); + if (rc != 0) { + fprintf (stderr, "libssh2 initialization failed (%d)\n", rc); + return 1; + } + + /* Connect to SSH server */ + sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); +#ifdef WIN32 + if (sock == INVALID_SOCKET) { + fprintf(stderr, "failed to open socket!\n"); + return -1; + } +#else + if (sock == -1) { + perror("socket"); + return -1; + } +#endif + + sin.sin_family = AF_INET; + if (INADDR_NONE == (sin.sin_addr.s_addr = inet_addr(server_ip))) { + perror("inet_addr"); + return -1; + } + sin.sin_port = htons(22); + if (connect(sock, (struct sockaddr*)(&sin), + sizeof(struct sockaddr_in)) != 0) { + fprintf(stderr, "failed to connect!\n"); + return -1; + } + + /* Create a session instance */ + session = libssh2_session_init(); + if(!session) { + fprintf(stderr, "Could not initialize SSH session!\n"); + return -1; + } + + /* ... start it up. This will trade welcome banners, exchange keys, + * and setup crypto, compression, and MAC layers + */ + rc = libssh2_session_handshake(session, sock); + if(rc) { + fprintf(stderr, "Error when starting up SSH session: %d\n", rc); + return -1; + } + + /* At this point we havn't yet authenticated. The first thing to do + * is check the hostkey's fingerprint against our known hosts Your app + * may have it hard coded, may go to a file, may present it to the + * user, that's your call + */ + fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_SHA1); + fprintf(stderr, "Fingerprint: "); + for(i = 0; i < 20; i++) + fprintf(stderr, "%02X ", (unsigned char)fingerprint[i]); + fprintf(stderr, "\n"); + + /* check what authentication methods are available */ + userauthlist = libssh2_userauth_list(session, username, strlen(username)); + fprintf(stderr, "Authentication methods: %s\n", userauthlist); + if (strstr(userauthlist, "password")) + auth |= AUTH_PASSWORD; + if (strstr(userauthlist, "publickey")) + auth |= AUTH_PUBLICKEY; + + /* check for options */ + if(argc > 8) { + if ((auth & AUTH_PASSWORD) && !strcasecmp(argv[8], "-p")) + auth = AUTH_PASSWORD; + if ((auth & AUTH_PUBLICKEY) && !strcasecmp(argv[8], "-k")) + auth = AUTH_PUBLICKEY; + } + + if (auth & AUTH_PASSWORD) { + if (libssh2_userauth_password(session, username, password)) { + fprintf(stderr, "Authentication by password failed.\n"); + goto shutdown; + } + } else if (auth & AUTH_PUBLICKEY) { + if (libssh2_userauth_publickey_fromfile(session, username, keyfile1, + keyfile2, password)) { + fprintf(stderr, "\tAuthentication by public key failed!\n"); + goto shutdown; + } + fprintf(stderr, "\tAuthentication by public key succeeded.\n"); + } else { + fprintf(stderr, "No supported authentication methods found!\n"); + goto shutdown; + } + + listensock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); +#ifdef WIN32 + if (listensock == INVALID_SOCKET) { + fprintf(stderr, "failed to open listen socket!\n"); + return -1; + } +#else + if (listensock == -1) { + perror("socket"); + return -1; + } +#endif + + sin.sin_family = AF_INET; + sin.sin_port = htons(local_listenport); + if (INADDR_NONE == (sin.sin_addr.s_addr = inet_addr(local_listenip))) { + perror("inet_addr"); + goto shutdown; + } + sockopt = 1; + setsockopt(listensock, SOL_SOCKET, SO_REUSEADDR, &sockopt, sizeof(sockopt)); + sinlen=sizeof(sin); + if (-1 == bind(listensock, (struct sockaddr *)&sin, sinlen)) { + perror("bind"); + goto shutdown; + } + if (-1 == listen(listensock, 2)) { + perror("listen"); + goto shutdown; + } + + fprintf(stderr, "Waiting for TCP connection on %s:%d...\n", + inet_ntoa(sin.sin_addr), ntohs(sin.sin_port)); + + forwardsock = accept(listensock, (struct sockaddr *)&sin, &sinlen); +#ifdef WIN32 + if (forwardsock == INVALID_SOCKET) { + fprintf(stderr, "failed to accept forward socket!\n"); + goto shutdown; + } +#else + if (forwardsock == -1) { + perror("accept"); + goto shutdown; + } +#endif + + shost = inet_ntoa(sin.sin_addr); + sport = ntohs(sin.sin_port); + + fprintf(stderr, "Forwarding connection from %s:%d here to remote %s:%d\n", + shost, sport, remote_desthost, remote_destport); + + channel = libssh2_channel_direct_tcpip_ex(session, remote_desthost, + remote_destport, shost, sport); + if (!channel) { + fprintf(stderr, "Could not open the direct-tcpip channel!\n" + "(Note that this can be a problem at the server!" + " Please review the server logs.)\n"); + goto shutdown; + } + + /* Must use non-blocking IO hereafter due to the current libssh2 API */ + libssh2_session_set_blocking(session, 0); + + while (1) { + FD_ZERO(&fds); + FD_SET(forwardsock, &fds); + tv.tv_sec = 0; + tv.tv_usec = 100000; + rc = select(forwardsock + 1, &fds, NULL, NULL, &tv); + if (-1 == rc) { + perror("select"); + goto shutdown; + } + if (rc && FD_ISSET(forwardsock, &fds)) { + len = recv(forwardsock, buf, sizeof(buf), 0); + if (len < 0) { + perror("read"); + goto shutdown; + } else if (0 == len) { + fprintf(stderr, "The client at %s:%d disconnected!\n", shost, + sport); + goto shutdown; + } + wr = 0; + do { + i = libssh2_channel_write(channel, buf, len); + if (i < 0) { + fprintf(stderr, "libssh2_channel_write: %d\n", i); + goto shutdown; + } + wr += i; + } while(i > 0 && wr < len); + } + while (1) { + len = libssh2_channel_read(channel, buf, sizeof(buf)); + if (LIBSSH2_ERROR_EAGAIN == len) + break; + else if (len < 0) { + fprintf(stderr, "libssh2_channel_read: %d", (int)len); + goto shutdown; + } + wr = 0; + while (wr < len) { + i = send(forwardsock, buf + wr, len - wr, 0); + if (i <= 0) { + perror("write"); + goto shutdown; + } + wr += i; + } + if (libssh2_channel_eof(channel)) { + fprintf(stderr, "The server at %s:%d disconnected!\n", + remote_desthost, remote_destport); + goto shutdown; + } + } + } + +shutdown: +#ifdef WIN32 + closesocket(forwardsock); + closesocket(listensock); +#else + close(forwardsock); + close(listensock); +#endif + if (channel) + libssh2_channel_free(channel); + libssh2_session_disconnect(session, "Client disconnecting normally"); + libssh2_session_free(session); + +#ifdef WIN32 + closesocket(sock); +#else + close(sock); +#endif + + libssh2_exit(); + + return 0; +} diff --git a/libssh2-sys/libssh2-1.5.0/example/libssh2_config.h.in b/libssh2-sys/libssh2-1.5.0/example/libssh2_config.h.in new file mode 100644 index 0000000..fa111a0 --- /dev/null +++ b/libssh2-sys/libssh2-1.5.0/example/libssh2_config.h.in @@ -0,0 +1,234 @@ +/* src/libssh2_config.h.in. Generated from configure.ac by autoheader. */ + +/* Define if building universal (internal helper macro) */ +#undef AC_APPLE_UNIVERSAL_BUILD + +/* Define to one of `_getb67', `GETB67', `getb67' for Cray-2 and Cray-YMP + systems. This function is required for `alloca.c' support on those systems. + */ +#undef CRAY_STACKSEG_END + +/* Define to 1 if using `alloca.c'. */ +#undef C_ALLOCA + +/* Define to 1 if you have `alloca', as a function or macro. */ +#undef HAVE_ALLOCA + +/* Define to 1 if you have <alloca.h> and it should be used (not on Ultrix). + */ +#undef HAVE_ALLOCA_H + +/* Define to 1 if you have the <arpa/inet.h> header file. */ +#undef HAVE_ARPA_INET_H + +/* disabled non-blocking sockets */ +#undef HAVE_DISABLED_NONBLOCKING + +/* Define to 1 if you have the <dlfcn.h> header file. */ +#undef HAVE_DLFCN_H + +/* Define to 1 if you have the <errno.h> header file. */ +#undef HAVE_ERRNO_H + +/* Define to 1 if you have the `EVP_aes_128_ctr' function. */ +#undef HAVE_EVP_AES_128_CTR + +/* Define to 1 if you have the <fcntl.h> header file. */ +#undef HAVE_FCNTL_H + +/* use FIONBIO for non-blocking sockets */ +#undef HAVE_FIONBIO + +/* Define to 1 if you have the `gettimeofday' function. */ +#undef HAVE_GETTIMEOFDAY + +/* Define to 1 if you have the <inttypes.h> header file. */ +#undef HAVE_INTTYPES_H + +/* use ioctlsocket() for non-blocking sockets */ +#undef HAVE_IOCTLSOCKET + +/* use Ioctlsocket() for non-blocking sockets */ +#undef HAVE_IOCTLSOCKET_CASE + +/* Define if you have the gcrypt library. */ +#undef HAVE_LIBGCRYPT + +/* Define if you have the ssl library. */ +#undef HAVE_LIBSSL + +/* Define if you have the z library. */ +#undef HAVE_LIBZ + +/* Define to 1 if the compiler supports the 'long long' data type. */ +#undef HAVE_LONGLONG + +/* Define to 1 if you have the <memory.h> header file. */ +#undef HAVE_MEMORY_H + +/* Define to 1 if you have the <netinet/in.h> header file. */ +#undef HAVE_NETINET_IN_H + +/* use O_NONBLOCK for non-blocking sockets */ +#undef HAVE_O_NONBLOCK + +/* Define to 1 if you have the `poll' function. */ +#undef HAVE_POLL + +/* Define to 1 if you have the select function. */ +#undef HAVE_SELECT + +/* use SO_NONBLOCK for non-blocking sockets */ +#undef HAVE_SO_NONBLOCK + +/* Define to 1 if you have the <stdint.h> header file. */ +#undef HAVE_STDINT_H + +/* Define to 1 if you have the <stdio.h> header file. */ +#undef HAVE_STDIO_H + +/* Define to 1 if you have the <stdlib.h> header file. */ +#undef HAVE_STDLIB_H + +/* Define to 1 if you have the <strings.h> header file. */ +#undef HAVE_STRINGS_H + +/* Define to 1 if you have the <string.h> header file. */ +#undef HAVE_STRING_H + +/* Define to 1 if you have the `strtoll' function. */ +#undef HAVE_STRTOLL + +/* Define to 1 if you have the <sys/ioctl.h> header file. */ +#undef HAVE_SYS_IOCTL_H + +/* Define to 1 if you have the <sys/select.h> header file. */ +#undef HAVE_SYS_SELECT_H + +/* Define to 1 if you have the <sys/socket.h> header file. */ +#undef HAVE_SYS_SOCKET_H + +/* Define to 1 if you have the <sys/stat.h> header file. */ +#undef HAVE_SYS_STAT_H + +/* Define to 1 if you have the <sys/time.h> header file. */ +#undef HAVE_SYS_TIME_H + +/* Define to 1 if you have the <sys/types.h> header file. */ +#undef HAVE_SYS_TYPES_H + +/* Define to 1 if you have the <sys/uio.h> header file. */ +#undef HAVE_SYS_UIO_H + +/* Define to 1 if you have the <sys/un.h> header file. */ +#undef HAVE_SYS_UN_H + +/* Define to 1 if you have the <unistd.h> header file. */ +#undef HAVE_UNISTD_H + +/* Define to 1 if you have the <windows.h> header file. */ +#undef HAVE_WINDOWS_H + +/* Define to 1 if you have the <winsock2.h> header file. */ +#undef HAVE_WINSOCK2_H + +/* Define to 1 if you have the <ws2tcpip.h> header file. */ +#undef HAVE_WS2TCPIP_H + +/* to make a symbol visible */ +#undef LIBSSH2_API + +/* Enable "none" cipher -- NOT RECOMMENDED */ +#undef LIBSSH2_CRYPT_NONE + +/* Enable newer diffie-hellman-group-exchange-sha1 syntax */ +#undef LIBSSH2_DH_GEX_NEW + +/* Compile in zlib support */ +#undef LIBSSH2_HAVE_ZLIB + +/* Use libgcrypt */ +#undef LIBSSH2_LIBGCRYPT + +/* Enable "none" MAC -- NOT RECOMMENDED */ +#undef LIBSSH2_MAC_NONE + +/* Use OpenSSL */ +#undef LIBSSH2_OPENSSL + +/* Define to the sub-directory in which libtool stores uninstalled libraries. + */ +#undef LT_OBJDIR + +/* Define to 1 if _REENTRANT preprocessor symbol must be defined. */ +#undef NEED_REENTRANT + +/* Name of package */ +#undef PACKAGE + +/* Define to the address where bug reports for this package should be sent. */ +#undef PACKAGE_BUGREPORT + +/* Define to the full name of this package. */ +#undef PACKAGE_NAME + +/* Define to the full name and version of this package. */ +#undef PACKAGE_STRING + +/* Define to the one symbol short name of this package. */ +#undef PACKAGE_TARNAME + +/* Define to the home page for this package. */ +#undef PACKAGE_URL + +/* Define to the version of this package. */ +#undef PACKAGE_VERSION + +/* If using the C implementation of alloca, define if you know the + direction of stack growth for your system; otherwise it will be + automatically deduced at runtime. + STACK_DIRECTION > 0 => grows toward higher addresses + STACK_DIRECTION < 0 => grows toward lower addresses + STACK_DIRECTION = 0 => direction of growth unknown */ +#undef STACK_DIRECTION + +/* Define to 1 if you have the ANSI C header files. */ +#undef STDC_HEADERS + +/* Version number of package */ +#undef VERSION + +/* Define WORDS_BIGENDIAN to 1 if your processor stores words with the most + significant byte first (like Motorola and SPARC, unlike Intel). */ +#if defined AC_APPLE_UNIVERSAL_BUILD +# if defined __BIG_ENDIAN__ +# define WORDS_BIGENDIAN 1 +# endif +#else +# ifndef WORDS_BIGENDIAN +# undef WORDS_BIGENDIAN +# endif +#endif + +/* Enable large inode numbers on Mac OS X 10.5. */ +#ifndef _DARWIN_USE_64_BIT_INODE +# define _DARWIN_USE_64_BIT_INODE 1 +#endif + +/* Number of bits in a file offset, on hosts where this is settable. */ +#undef _FILE_OFFSET_BITS + +/* Define for large files, on AIX-style hosts. */ +#undef _LARGE_FILES + +/* Define to empty if `const' does not conform to ANSI C. */ +#undef const + +/* Define to `__inline__' or `__inline' if that's what the C compiler + calls it, or to nothing if 'inline' is not supported under any name. */ +#ifndef __cplusplus +#undef inline +#endif + +/* Define to `unsigned int' if <sys/types.h> does not define. */ +#undef size_t diff --git a/libssh2-sys/libssh2-1.5.0/example/scp.c b/libssh2-sys/libssh2-1.5.0/example/scp.c new file mode 100644 index 0000000..5e7eb99 --- /dev/null +++ b/libssh2-sys/libssh2-1.5.0/example/scp.c @@ -0,0 +1,186 @@ +/* + * Sample showing how to do a simple SCP transfer. + */ + +#include "libssh2_config.h" +#include <libssh2.h> + +#ifdef HAVE_WINSOCK2_H +# include <winsock2.h> +#endif +#ifdef HAVE_SYS_SOCKET_H +# include <sys/socket.h> +#endif +#ifdef HAVE_NETINET_IN_H +# include <netinet/in.h> +#endif +# ifdef HAVE_UNISTD_H +#include <unistd.h> +#endif +#ifdef HAVE_ARPA_INET_H +# include <arpa/inet.h> +#endif +#ifdef HAVE_SYS_TIME_H +# include <sys/time.h> +#endif + +#include <sys/types.h> +#include <fcntl.h> +#include <errno.h> +#include <stdio.h> +#include <ctype.h> + +int main(int argc, char *argv[]) +{ + unsigned long hostaddr; + int sock, i, auth_pw = 1; + struct sockaddr_in sin; + const char *fingerprint; + LIBSSH2_SESSION *session; + LIBSSH2_CHANNEL *channel; + const char *username="username"; + const char *password="password"; + const char *scppath="/tmp/TEST"; + struct stat fileinfo; + int rc; + off_t got=0; + +#ifdef WIN32 + WSADATA wsadata; + int err; + + err = WSAStartup(MAKEWORD(2,0), &wsadata); + if (err != 0) { + fprintf(stderr, "WSAStartup failed with error: %d\n", err); + return 1; + } +#endif + + if (argc > 1) { + hostaddr = inet_addr(argv[1]); + } else { + hostaddr = htonl(0x7F000001); + } + if (argc > 2) { + username = argv[2]; + } + if (argc > 3) { + password = argv[3]; + } + if (argc > 4) { + scppath = argv[4]; + } + + rc = libssh2_init (0); + if (rc != 0) { + fprintf (stderr, "libssh2 initialization failed (%d)\n", rc); + return 1; + } + + /* Ultra basic "connect to port 22 on localhost" + * Your code is responsible for creating the socket establishing the + * connection + */ + sock = socket(AF_INET, SOCK_STREAM, 0); + + sin.sin_family = AF_INET; + sin.sin_port = htons(22); + sin.sin_addr.s_addr = hostaddr; + if (connect(sock, (struct sockaddr*)(&sin), + sizeof(struct sockaddr_in)) != 0) { + fprintf(stderr, "failed to connect!\n"); + return -1; + } + + /* Create a session instance + */ + session = libssh2_session_init(); + if(!session) + return -1; + + /* ... start it up. This will trade welcome banners, exchange keys, + * and setup crypto, compression, and MAC layers + */ + rc = libssh2_session_handshake(session, sock); + if(rc) { + fprintf(stderr, "Failure establishing SSH session: %d\n", rc); + return -1; + } + + /* At this point we havn't yet authenticated. The first thing to do + * is check the hostkey's fingerprint against our known hosts Your app + * may have it hard coded, may go to a file, may present it to the + * user, that's your call + */ + fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_SHA1); + fprintf(stderr, "Fingerprint: "); + for(i = 0; i < 20; i++) { + fprintf(stderr, "%02X ", (unsigned char)fingerprint[i]); + } + fprintf(stderr, "\n"); + + if (auth_pw) { + /* We could authenticate via password */ + if (libssh2_userauth_password(session, username, password)) { + fprintf(stderr, "Authentication by password failed.\n"); + goto shutdown; + } + } else { + /* Or by public key */ + if (libssh2_userauth_publickey_fromfile(session, username, + "/home/username/.ssh/id_rsa.pub", + "/home/username/.ssh/id_rsa", + password)) { + fprintf(stderr, "\tAuthentication by public key failed\n"); + goto shutdown; + } + } + + /* Request a file via SCP */ + channel = libssh2_scp_recv(session, scppath, &fileinfo); + + if (!channel) { + fprintf(stderr, "Unable to open a session: %d\n", + libssh2_session_last_errno(session)); + goto shutdown; + } + + + while(got < fileinfo.st_size) { + char mem[1024]; + int amount=sizeof(mem); + + if((fileinfo.st_size -got) < amount) { + amount = fileinfo.st_size -got; + } + + rc = libssh2_channel_read(channel, mem, amount); + if(rc > 0) { + write(1, mem, rc); + } + else if(rc < 0) { + fprintf(stderr, "libssh2_channel_read() failed: %d\n", rc); + break; + } + got += rc; + } + + libssh2_channel_free(channel); + channel = NULL; + + shutdown: + + libssh2_session_disconnect(session, "Normal Shutdown, Thank you for playing"); + libssh2_session_free(session); + +#ifdef WIN32 + closesocket(sock); +#else + close(sock); +#endif + fprintf(stderr, "all done\n"); + + libssh2_exit(); + + return 0; +} diff --git a/libssh2-sys/libssh2-1.5.0/example/scp_nonblock.c b/libssh2-sys/libssh2-1.5.0/example/scp_nonblock.c new file mode 100644 index 0000000..1c7028a --- /dev/null +++ b/libssh2-sys/libssh2-1.5.0/example/scp_nonblock.c @@ -0,0 +1,280 @@ +/* + * Sample showing how to do SCP transfers in a non-blocking manner. + * + * The sample code has default values for host name, user name, password + * and path to copy, but you can specify them on the command line like: + * + * "scp_nonblock 192.168.0.1 user password /tmp/secrets" + */ + +#include "libssh2_config.h" +#include <libssh2.h> + +#ifdef HAVE_WINSOCK2_H +# include <winsock2.h> +#endif +#ifdef HAVE_SYS_SOCKET_H +# include <sys/socket.h> +#endif +#ifdef HAVE_NETINET_IN_H +# include <netinet/in.h> +#endif +#ifdef HAVE_SYS_SELECT_H +# include <sys/select.h> +#endif +# ifdef HAVE_UNISTD_H +#include <unistd.h> +#endif +#ifdef HAVE_ARPA_INET_H +# include <arpa/inet.h> +#endif +#ifdef HAVE_SYS_TIME_H +# include <sys/time.h> +#endif + +#include <sys/types.h> +#include <fcntl.h> +#include <errno.h> +#include <stdio.h> +#include <ctype.h> + +/* diff in ms */ +static long tvdiff(struct timeval newer, struct timeval older) +{ + return (newer.tv_sec-older.tv_sec)*1000+ + (newer.tv_usec-older.tv_usec)/1000; +} + +static int waitsocket(int socket_fd, LIBSSH2_SESSION *session) +{ + struct timeval timeout; + int rc; + fd_set fd; + fd_set *writefd = NULL; + fd_set *readfd = NULL; + int dir; + + timeout.tv_sec = 10; + timeout.tv_usec = 0; + + FD_ZERO(&fd); + + FD_SET(socket_fd, &fd); + + /* now make sure we wait in the correct direction */ + dir = libssh2_session_block_directions(session); + + if(dir & LIBSSH2_SESSION_BLOCK_INBOUND) + readfd = &fd; + + if(dir & LIBSSH2_SESSION_BLOCK_OUTBOUND) + writefd = &fd; + + rc = select(socket_fd + 1, readfd, writefd, NULL, &timeout); + + return rc; +} + +int main(int argc, char *argv[]) +{ + unsigned long hostaddr; + int sock, i, auth_pw = 1; + struct sockaddr_in sin; + const char *fingerprint; + LIBSSH2_SESSION *session; + LIBSSH2_CHANNEL *channel; + const char *username="username"; + const char *password="password"; + const char *scppath="/tmp/TEST"; + struct stat fileinfo; + struct timeval start; + struct timeval end; + int rc; + int total = 0; + long time_ms; + int spin = 0; + off_t got=0; + +#ifdef WIN32 + WSADATA wsadata; + int err; + + err = WSAStartup(MAKEWORD(2,0), &wsadata); + if (err != 0) { + fprintf(stderr, "WSAStartup failed with error: %d\n", err); + return 1; + } +#endif + + if (argc > 1) { + hostaddr = inet_addr(argv[1]); + } else { + hostaddr = htonl(0x7F000001); + } + if (argc > 2) { + username = argv[2]; + } + if (argc > 3) { + password = argv[3]; + } + if (argc > 4) { + scppath = argv[4]; + } + + rc = libssh2_init (0); + if (rc != 0) { + fprintf (stderr, "libssh2 initialization failed (%d)\n", rc); + return 1; + } + + /* Ultra basic "connect to port 22 on localhost" + * Your code is responsible for creating the socket establishing the + * connection + */ + sock = socket(AF_INET, SOCK_STREAM, 0); + + sin.sin_family = AF_INET; + sin.sin_port = htons(22); + sin.sin_addr.s_addr = hostaddr; + if (connect(sock, (struct sockaddr*)(&sin), sizeof(struct sockaddr_in)) != 0) { + fprintf(stderr, "failed to connect!\n"); + return -1; + } + + /* Create a session instance */ + session = libssh2_session_init(); + if (!session) + return -1; + + /* Since we have set non-blocking, tell libssh2 we are non-blocking */ + libssh2_session_set_blocking(session, 0); + + gettimeofday(&start, NULL); + + /* ... start it up. This will trade welcome banners, exchange keys, + * and setup crypto, compression, and MAC layers + */ + while ((rc = libssh2_session_handshake(session, sock)) == + LIBSSH2_ERROR_EAGAIN); + if (rc) { + fprintf(stderr, "Failure establishing SSH session: %d\n", rc); + return -1; + } + + /* At this point we havn't yet authenticated. The first thing to do + * is check the hostkey's fingerprint against our known hosts Your app + * may have it hard coded, may go to a file, may present it to the + * user, that's your call + */ + fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_SHA1); + fprintf(stderr, "Fingerprint: "); + for(i = 0; i < 20; i++) { + fprintf(stderr, "%02X ", (unsigned char)fingerprint[i]); + } + fprintf(stderr, "\n"); + + if (auth_pw) { + /* We could authenticate via password */ + while ((rc = libssh2_userauth_password(session, username, password)) == + LIBSSH2_ERROR_EAGAIN); + if (rc) { + fprintf(stderr, "Authentication by password failed.\n"); + goto shutdown; + } + } else { + /* Or by public key */ + while ((rc = libssh2_userauth_publickey_fromfile(session, username, + "/home/username/" + ".ssh/id_rsa.pub", + "/home/username/" + ".ssh/id_rsa", + password)) == + LIBSSH2_ERROR_EAGAIN); + if (rc) { + fprintf(stderr, "\tAuthentication by public key failed\n"); + goto shutdown; + } + } + +#if 0 + libssh2_trace(session, LIBSSH2_TRACE_CONN); +#endif + + /* Request a file via SCP */ + fprintf(stderr, "libssh2_scp_recv()!\n"); + do { + channel = libssh2_scp_recv(session, scppath, &fileinfo); + + if (!channel) { + if(libssh2_session_last_errno(session) != LIBSSH2_ERROR_EAGAIN) { + char *err_msg; + + libssh2_session_last_error(session, &err_msg, NULL, 0); + fprintf(stderr, "%s\n", err_msg); + goto shutdown; + } + else { + fprintf(stderr, "libssh2_scp_recv() spin\n"); + waitsocket(sock, session); + } + } + } while (!channel); + fprintf(stderr, "libssh2_scp_recv() is done, now receive data!\n"); + + while(got < fileinfo.st_size) { + char mem[1024*24]; + int rc; + + do { + int amount=sizeof(mem); + + if ((fileinfo.st_size -got) < amount) { + amount = fileinfo.st_size - got; + } + + /* loop until we block */ + rc = libssh2_channel_read(channel, mem, amount); + if (rc > 0) { + write(1, mem, rc); + got += rc; + total += rc; + } + } while (rc > 0); + + if ((rc == LIBSSH2_ERROR_EAGAIN) && (got < fileinfo.st_size)) { + /* this is due to blocking that would occur otherwise + so we loop on this condition */ + + spin++; + waitsocket(sock, session); /* now we wait */ + continue; + } + break; + } + + gettimeofday(&end, NULL); + + time_ms = tvdiff(end, start); + fprintf(stderr, "Got %d bytes in %ld ms = %.1f bytes/sec spin: %d\n", total, + time_ms, total/(time_ms/1000.0), spin ); + + libssh2_channel_free(channel); + channel = NULL; + +shutdown: + + libssh2_session_disconnect(session, + "Normal Shutdown, Thank you for playing"); + libssh2_session_free(session); + +#ifdef WIN32 + closesocket(sock); +#else + close(sock); +#endif + fprintf(stderr, "all done\n"); + + libssh2_exit(); + + return 0; +} diff --git a/libssh2-sys/libssh2-1.5.0/example/scp_write.c b/libssh2-sys/libssh2-1.5.0/example/scp_write.c new file mode 100644 index 0000000..eef6e81 --- /dev/null +++ b/libssh2-sys/libssh2-1.5.0/example/scp_write.c @@ -0,0 +1,225 @@ +/* + * Sample showing how to do an SCP upload. + */ + +#include "libssh2_config.h" +#include <libssh2.h> + +#ifdef HAVE_WINSOCK2_H +# include <winsock2.h> +#endif +#ifdef HAVE_SYS_SOCKET_H +# include <sys/socket.h> +#endif +#ifdef HAVE_NETINET_IN_H +# include <netinet/in.h> +#endif +# ifdef HAVE_UNISTD_H +#include <unistd.h> +#endif +#ifdef HAVE_ARPA_INET_H +# include <arpa/inet.h> +#endif +#ifdef HAVE_SYS_TIME_H +# include <sys/time.h> +#endif + +#include <sys/types.h> +#include <fcntl.h> +#include <errno.h> +#include <stdio.h> +#include <ctype.h> + +int main(int argc, char *argv[]) +{ + unsigned long hostaddr; + int sock, i, auth_pw = 1; + struct sockaddr_in sin; + const char *fingerprint; + LIBSSH2_SESSION *session = NULL; + LIBSSH2_CHANNEL *channel; + const char *username="username"; + const char *password="password"; + const char *loclfile="scp_write.c"; + const char *scppath="/tmp/TEST"; + FILE *local; + int rc; + char mem[1024]; + size_t nread; + char *ptr; + struct stat fileinfo; + +#ifdef WIN32 + WSADATA wsadata; + int err; + + err = WSAStartup(MAKEWORD(2,0), &wsadata); + if (err != 0) { + fprintf(stderr, "WSAStartup failed with error: %d\n", err); + return 1; + } +#endif + + if (argc > 1) { + hostaddr = inet_addr(argv[1]); + } else { + hostaddr = htonl(0x7F000001); + } + if (argc > 2) { + username = argv[2]; + } + if (argc > 3) { + password = argv[3]; + } + if(argc > 4) { + loclfile = argv[4]; + } + if (argc > 5) { + scppath = argv[5]; + } + + rc = libssh2_init (0); + if (rc != 0) { + fprintf (stderr, "libssh2 initialization failed (%d)\n", rc); + return 1; + } + + local = fopen(loclfile, "rb"); + if (!local) { + fprintf(stderr, "Can't open local file %s\n", loclfile); + return -1; + } + + stat(loclfile, &fileinfo); + + /* Ultra basic "connect to port 22 on localhost" + * Your code is responsible for creating the socket establishing the + * connection + */ + sock = socket(AF_INET, SOCK_STREAM, 0); + if(-1 == sock) { + fprintf(stderr, "failed to create socket!\n"); + return -1; + } + + sin.sin_family = AF_INET; + sin.sin_port = htons(22); + sin.sin_addr.s_addr = hostaddr; + if (connect(sock, (struct sockaddr*)(&sin), + sizeof(struct sockaddr_in)) != 0) { + fprintf(stderr, "failed to connect!\n"); + return -1; + } + + /* Create a session instance + */ + session = libssh2_session_init(); + if(!session) + return -1; + + /* ... start it up. This will trade welcome banners, exchange keys, + * and setup crypto, compression, and MAC layers + */ + rc = libssh2_session_handshake(session, sock); + if(rc) { + fprintf(stderr, "Failure establishing SSH session: %d\n", rc); + return -1; + } + + /* At this point we havn't yet authenticated. The first thing to do + * is check the hostkey's fingerprint against our known hosts Your app + * may have it hard coded, may go to a file, may present it to the + * user, that's your call + */ + fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_SHA1); + fprintf(stderr, "Fingerprint: "); + for(i = 0; i < 20; i++) { + fprintf(stderr, "%02X ", (unsigned char)fingerprint[i]); + } + fprintf(stderr, "\n"); + + if (auth_pw) { + /* We could authenticate via password */ + if (libssh2_userauth_password(session, username, password)) { + fprintf(stderr, "Authentication by password failed.\n"); + goto shutdown; + } + } else { + /* Or by public key */ + if (libssh2_userauth_publickey_fromfile(session, username, + "/home/username/.ssh/id_rsa.pub", + "/home/username/.ssh/id_rsa", + password)) { + fprintf(stderr, "\tAuthentication by public key failed\n"); + goto shutdown; + } + } + + /* Send a file via scp. The mode parameter must only have permissions! */ + channel = libssh2_scp_send(session, scppath, fileinfo.st_mode & 0777, + (unsigned long)fileinfo.st_size); + + if (!channel) { + char *errmsg; + int errlen; + int err = libssh2_session_last_error(session, &errmsg, &errlen, 0); + fprintf(stderr, "Unable to open a session: (%d) %s\n", err, errmsg); + goto shutdown; + } + + fprintf(stderr, "SCP session waiting to send file\n"); + do { + nread = fread(mem, 1, sizeof(mem), local); + if (nread <= 0) { + /* end of file */ + break; + } + ptr = mem; + + do { + /* write the same data over and over, until error or completion */ + rc = libssh2_channel_write(channel, ptr, nread); + if (rc < 0) { + fprintf(stderr, "ERROR %d\n", rc); + break; + } + else { + /* rc indicates how many bytes were written this time */ + ptr += rc; + nread -= rc; + } + } while (nread); + + } while (1); + + fprintf(stderr, "Sending EOF\n"); + libssh2_channel_send_eof(channel); + + fprintf(stderr, "Waiting for EOF\n"); + libssh2_channel_wait_eof(channel); + + fprintf(stderr, "Waiting for channel to close\n"); + libssh2_channel_wait_closed(channel); + + libssh2_channel_free(channel); + channel = NULL; + + shutdown: + + if(session) { + libssh2_session_disconnect(session, "Normal Shutdown, Thank you for playing"); + libssh2_session_free(session); + } +#ifdef WIN32 + closesocket(sock); +#else + close(sock); +#endif + if (local) + fclose(local); + fprintf(stderr, "all done\n"); + + libssh2_exit(); + + return 0; +} diff --git a/libssh2-sys/libssh2-1.5.0/example/scp_write_nonblock.c b/libssh2-sys/libssh2-1.5.0/example/scp_write_nonblock.c new file mode 100644 index 0000000..bb8e39d --- /dev/null +++ b/libssh2-sys/libssh2-1.5.0/example/scp_write_nonblock.c @@ -0,0 +1,283 @@ +/* + * Sample showing how to do an SCP non-blocking upload transfer. + */ + +#include "libssh2_config.h" + +#include <libssh2.h> + +#ifdef HAVE_WINSOCK2_H +# include <winsock2.h> +#endif +#ifdef HAVE_SYS_SOCKET_H +# include <sys/socket.h> +#endif +#ifdef HAVE_NETINET_IN_H +# include <netinet/in.h> +#endif +#ifdef HAVE_SYS_SELECT_H +# include <sys/select.h> +#endif +# ifdef HAVE_UNISTD_H +#include <unistd.h> +#endif +#ifdef HAVE_ARPA_INET_H +# include <arpa/inet.h> +#endif +#ifdef HAVE_SYS_TIME_H +# include <sys/time.h> +#endif + +#include <sys/types.h> +#include <fcntl.h> +#include <errno.h> +#include <stdio.h> +#include <ctype.h> +#include <time.h> + +static int waitsocket(int socket_fd, LIBSSH2_SESSION *session) +{ + struct timeval timeout; + int rc; + fd_set fd; + fd_set *writefd = NULL; + fd_set *readfd = NULL; + int dir; + + timeout.tv_sec = 10; + timeout.tv_usec = 0; + + FD_ZERO(&fd); + + FD_SET(socket_fd, &fd); + + /* now make sure we wait in the correct direction */ + dir = libssh2_session_block_directions(session); + + if(dir & LIBSSH2_SESSION_BLOCK_INBOUND) + readfd = &fd; + + if(dir & LIBSSH2_SESSION_BLOCK_OUTBOUND) + writefd = &fd; + + rc = select(socket_fd + 1, readfd, writefd, NULL, &timeout); + + return rc; +} + +int main(int argc, char *argv[]) +{ + unsigned long hostaddr; + int sock, i, auth_pw = 1; + struct sockaddr_in sin; + const char *fingerprint; + LIBSSH2_SESSION *session = NULL; + LIBSSH2_CHANNEL *channel; + const char *username="username"; + const char *password="password"; + const char *loclfile="scp_write.c"; + const char *scppath="/tmp/TEST"; + FILE *local; + int rc; + char mem[1024*100]; + size_t nread; + char *ptr; + struct stat fileinfo; + time_t start; + long total = 0; + int duration; + size_t prev; + +#ifdef WIN32 + WSADATA wsadata; + int err; + + err = WSAStartup(MAKEWORD(2,0), &wsadata); + if (err != 0) { + fprintf(stderr, "WSAStartup failed with error: %d\n", err); + return 1; + } +#endif + + if (argc > 1) { + hostaddr = inet_addr(argv[1]); + } else { + hostaddr = htonl(0x7F000001); + } + if (argc > 2) { + username = argv[2]; + } + if (argc > 3) { + password = argv[3]; + } + if(argc > 4) { + loclfile = argv[4]; + } + if (argc > 5) { + scppath = argv[5]; + } + + rc = libssh2_init (0); + if (rc != 0) { + fprintf (stderr, "libssh2 initialization failed (%d)\n", rc); + return 1; + } + + local = fopen(loclfile, "rb"); + if (!local) { + fprintf(stderr, "Can't local file %s\n", loclfile); + return -1; + } + + stat(loclfile, &fileinfo); + + /* Ultra basic "connect to port 22 on localhost" + * Your code is responsible for creating the socket establishing the + * connection + */ + sock = socket(AF_INET, SOCK_STREAM, 0); + + sin.sin_family = AF_INET; + sin.sin_port = htons(22); + sin.sin_addr.s_addr = hostaddr; + if (connect(sock, (struct sockaddr*)(&sin), + sizeof(struct sockaddr_in)) != 0) { + fprintf(stderr, "failed to connect!\n"); + return -1; + } + + /* Create a session instance + */ + session = libssh2_session_init(); + if(!session) + return -1; + + /* Since we have set non-blocking, tell libssh2 we are non-blocking */ + libssh2_session_set_blocking(session, 0); + + /* ... start it up. This will trade welcome banners, exchange keys, + * and setup crypto, compression, and MAC layers + */ + while ((rc = libssh2_session_handshake(session, sock)) + == LIBSSH2_ERROR_EAGAIN); + if(rc) { + fprintf(stderr, "Failure establishing SSH session: %d\n", rc); + return -1; + } + + /* At this point we havn't yet authenticated. The first thing to do + * is check the hostkey's fingerprint against our known hosts Your app + * may have it hard coded, may go to a file, may present it to the + * user, that's your call + */ + fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_SHA1); + fprintf(stderr, "Fingerprint: "); + for(i = 0; i < 20; i++) { + fprintf(stderr, "%02X ", (unsigned char)fingerprint[i]); + } + fprintf(stderr, "\n"); + + if (auth_pw) { + /* We could authenticate via password */ + while ((rc = libssh2_userauth_password(session, username, password)) == + LIBSSH2_ERROR_EAGAIN); + if (rc) { + fprintf(stderr, "Authentication by password failed.\n"); + goto shutdown; + } + } else { + /* Or by public key */ + while ((rc = libssh2_userauth_publickey_fromfile(session, username, + "/home/username/.ssh/id_rsa.pub", + "/home/username/.ssh/id_rsa", + password)) == LIBSSH2_ERROR_EAGAIN); + if (rc) { + fprintf(stderr, "\tAuthentication by public key failed\n"); + goto shutdown; + } + } + + /* Send a file via scp. The mode parameter must only have permissions! */ + do { + channel = libssh2_scp_send(session, scppath, fileinfo.st_mode & 0777, + (unsigned long)fileinfo.st_size); + + if ((!channel) && (libssh2_session_last_errno(session) != + LIBSSH2_ERROR_EAGAIN)) { + char *err_msg; + + libssh2_session_last_error(session, &err_msg, NULL, 0); + fprintf(stderr, "%s\n", err_msg); + goto shutdown; + } + } while (!channel); + + fprintf(stderr, "SCP session waiting to send file\n"); + start = time(NULL); + do { + nread = fread(mem, 1, sizeof(mem), local); + if (nread <= 0) { + /* end of file */ + break; + } + ptr = mem; + + total += nread; + + prev = 0; + do { + while ((rc = libssh2_channel_write(channel, ptr, nread)) == + LIBSSH2_ERROR_EAGAIN) { + waitsocket(sock, session); + prev = 0; + } + if (rc < 0) { + fprintf(stderr, "ERROR %d total %ld / %d prev %d\n", rc, + total, (int)nread, (int)prev); + break; + } + else { + prev = nread; + + /* rc indicates how many bytes were written this time */ + nread -= rc; + ptr += rc; + } + } while (nread); + } while (!nread); /* only continue if nread was drained */ + + duration = (int)(time(NULL)-start); + + fprintf(stderr, "%ld bytes in %d seconds makes %.1f bytes/sec\n", + total, duration, total/(double)duration); + + fprintf(stderr, "Sending EOF\n"); + while (libssh2_channel_send_eof(channel) == LIBSSH2_ERROR_EAGAIN); + + fprintf(stderr, "Waiting for EOF\n"); + while (libssh2_channel_wait_eof(channel) == LIBSSH2_ERROR_EAGAIN); + + fprintf(stderr, "Waiting for channel to close\n"); + while (libssh2_channel_wait_closed(channel) == LIBSSH2_ERROR_EAGAIN); + + libssh2_channel_free(channel); + channel = NULL; + + shutdown: + + while (libssh2_session_disconnect(session, + "Normal Shutdown, Thank you for playing") == + LIBSSH2_ERROR_EAGAIN); + libssh2_session_free(session); + +#ifdef WIN32 + closesocket(sock); +#else + close(sock); +#endif + fprintf(stderr, "all done\n"); + + libssh2_exit(); + + return 0; +} diff --git a/libssh2-sys/libssh2-1.5.0/example/sftp.c b/libssh2-sys/libssh2-1.5.0/example/sftp.c new file mode 100644 index 0000000..0feb534 --- /dev/null +++ b/libssh2-sys/libssh2-1.5.0/example/sftp.c @@ -0,0 +1,290 @@ +/* + * Sample showing how to do SFTP transfers. + * + * The sample code has default values for host name, user name, password + * and path to copy, but you can specify them on the command line like: + * + * "sftp 192.168.0.1 user password /tmp/secrets -p|-i|-k" + */ + +#include "libssh2_config.h" +#include <libssh2.h> +#include <libssh2_sftp.h> + +#ifdef HAVE_WINSOCK2_H +# include <winsock2.h> +#endif +#ifdef HAVE_SYS_SOCKET_H +# include <sys/socket.h> +#endif +#ifdef HAVE_NETINET_IN_H +# include <netinet/in.h> +#endif +# ifdef HAVE_UNISTD_H +#include <unistd.h> +#endif +#ifdef HAVE_ARPA_INET_H +# include <arpa/inet.h> +#endif +#ifdef HAVE_SYS_TIME_H +# include <sys/time.h> +#endif + +#include <sys/types.h> +#include <fcntl.h> +#include <errno.h> +#include <stdio.h> +#include <ctype.h> + + +const char *keyfile1="~/.ssh/id_rsa.pub"; +const char *keyfile2="~/.ssh/id_rsa"; +const char *username="username"; +const char *password="password"; +const char *sftppath="/tmp/TEST"; + + +static void kbd_callback(const char *name, int name_len, + const char *instruction, int instruction_len, int num_prompts, + const LIBSSH2_USERAUTH_KBDINT_PROMPT *prompts, + LIBSSH2_USERAUTH_KBDINT_RESPONSE *responses, + void **abstract) +{ + int i; + size_t n; + char buf[1024]; + (void)abstract; + + fprintf(stderr, "Performing keyboard-interactive authentication.\n"); + + fprintf(stderr, "Authentication name: '"); + fwrite(name, 1, name_len, stderr); + fprintf(stderr, "'\n"); + + fprintf(stderr, "Authentication instruction: '"); + fwrite(instruction, 1, instruction_len, stderr); + fprintf(stderr, "'\n"); + + fprintf(stderr, "Number of prompts: %d\n\n", num_prompts); + + for (i = 0; i < num_prompts; i++) { + fprintf(stderr, "Prompt %d from server: '", i); + fwrite(prompts[i].text, 1, prompts[i].length, stderr); + fprintf(stderr, "'\n"); + + fprintf(stderr, "Please type response: "); + fgets(buf, sizeof(buf), stdin); + n = strlen(buf); + while (n > 0 && strchr("\r\n", buf[n - 1])) + n--; + buf[n] = 0; + + responses[i].text = strdup(buf); + responses[i].length = n; + + fprintf(stderr, "Response %d from user is '", i); + fwrite(responses[i].text, 1, responses[i].length, stderr); + fprintf(stderr, "'\n\n"); + } + + fprintf(stderr, + "Done. Sending keyboard-interactive responses to server now.\n"); +} + + +int main(int argc, char *argv[]) +{ + unsigned long hostaddr; + int sock, i, auth_pw = 0; + struct sockaddr_in sin; + const char *fingerprint; + char *userauthlist; + LIBSSH2_SESSION *session; + int rc; + LIBSSH2_SFTP *sftp_session; + LIBSSH2_SFTP_HANDLE *sftp_handle; + +#ifdef WIN32 + WSADATA wsadata; + int err; + + err = WSAStartup(MAKEWORD(2,0), &wsadata); + if (err != 0) { + fprintf(stderr, "WSAStartup failed with error: %d\n", err); + return 1; + } +#endif + + if (argc > 1) { + hostaddr = inet_addr(argv[1]); + } else { + hostaddr = htonl(0x7F000001); + } + + if(argc > 2) { + username = argv[2]; + } + if(argc > 3) { + password = argv[3]; + } + if(argc > 4) { + sftppath = argv[4]; + } + + rc = libssh2_init (0); + if (rc != 0) { + fprintf(stderr, "libssh2 initialization failed (%d)\n", rc); + return 1; + } + + /* + * The application code is responsible for creating the socket + * and establishing the connection + */ + sock = socket(AF_INET, SOCK_STREAM, 0); + + sin.sin_family = AF_INET; + sin.sin_port = htons(22); + sin.sin_addr.s_addr = hostaddr; + if (connect(sock, (struct sockaddr*)(&sin), + sizeof(struct sockaddr_in)) != 0) { + fprintf(stderr, "failed to connect!\n"); + return -1; + } + + /* Create a session instance + */ + session = libssh2_session_init(); + if(!session) + return -1; + + /* Since we have set non-blocking, tell libssh2 we are blocking */ + libssh2_session_set_blocking(session, 1); + + /* ... start it up. This will trade welcome banners, exchange keys, + * and setup crypto, compression, and MAC layers + */ + rc = libssh2_session_handshake(session, sock); + if(rc) { + fprintf(stderr, "Failure establishing SSH session: %d\n", rc); + return -1; + } + + /* At this point we havn't yet authenticated. The first thing to do + * is check the hostkey's fingerprint against our known hosts Your app + * may have it hard coded, may go to a file, may present it to the + * user, that's your call + */ + fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_SHA1); + fprintf(stderr, "Fingerprint: "); + for(i = 0; i < 20; i++) { + fprintf(stderr, "%02X ", (unsigned char)fingerprint[i]); + } + fprintf(stderr, "\n"); + + /* check what authentication methods are available */ + userauthlist = libssh2_userauth_list(session, username, strlen(username)); + fprintf(stderr, "Authentication methods: %s\n", userauthlist); + if (strstr(userauthlist, "password") != NULL) { + auth_pw |= 1; + } + if (strstr(userauthlist, "keyboard-interactive") != NULL) { + auth_pw |= 2; + } + if (strstr(userauthlist, "publickey") != NULL) { + auth_pw |= 4; + } + + /* if we got an 4. argument we set this option if supported */ + if(argc > 5) { + if ((auth_pw & 1) && !strcasecmp(argv[5], "-p")) { + auth_pw = 1; + } + if ((auth_pw & 2) && !strcasecmp(argv[5], "-i")) { + auth_pw = 2; + } + if ((auth_pw & 4) && !strcasecmp(argv[5], "-k")) { + auth_pw = 4; + } + } + + if (auth_pw & 1) { + /* We could authenticate via password */ + if (libssh2_userauth_password(session, username, password)) { + fprintf(stderr, "Authentication by password failed.\n"); + goto shutdown; + } + } else if (auth_pw & 2) { + /* Or via keyboard-interactive */ + if (libssh2_userauth_keyboard_interactive(session, username, &kbd_callback) ) { + fprintf(stderr, + "\tAuthentication by keyboard-interactive failed!\n"); + goto shutdown; + } else { + fprintf(stderr, + "\tAuthentication by keyboard-interactive succeeded.\n"); + } + } else if (auth_pw & 4) { + /* Or by public key */ + if (libssh2_userauth_publickey_fromfile(session, username, keyfile1, keyfile2, password)) { + fprintf(stderr, "\tAuthentication by public key failed!\n"); + goto shutdown; + } else { + fprintf(stderr, "\tAuthentication by public key succeeded.\n"); + } + } else { + fprintf(stderr, "No supported authentication methods found!\n"); + goto shutdown; + } + + fprintf(stderr, "libssh2_sftp_init()!\n"); + sftp_session = libssh2_sftp_init(session); + + if (!sftp_session) { + fprintf(stderr, "Unable to init SFTP session\n"); + goto shutdown; + } + + fprintf(stderr, "libssh2_sftp_open()!\n"); + /* Request a file via SFTP */ + sftp_handle = + libssh2_sftp_open(sftp_session, sftppath, LIBSSH2_FXF_READ, 0); + + if (!sftp_handle) { + fprintf(stderr, "Unable to open file with SFTP: %ld\n", + libssh2_sftp_last_error(sftp_session)); + goto shutdown; + } + fprintf(stderr, "libssh2_sftp_open() is done, now receive data!\n"); + do { + char mem[1024]; + + /* loop until we fail */ + fprintf(stderr, "libssh2_sftp_read()!\n"); + rc = libssh2_sftp_read(sftp_handle, mem, sizeof(mem)); + if (rc > 0) { + write(1, mem, rc); + } else { + break; + } + } while (1); + + libssh2_sftp_close(sftp_handle); + libssh2_sftp_shutdown(sftp_session); + + shutdown: + + libssh2_session_disconnect(session, "Normal Shutdown, Thank you for playing"); + libssh2_session_free(session); + +#ifdef WIN32 + closesocket(sock); +#else + close(sock); +#endif + fprintf(stderr, "all done\n"); + + libssh2_exit(); + + return 0; +} diff --git a/libssh2-sys/libssh2-1.5.0/example/sftp_RW_nonblock.c b/libssh2-sys/libssh2-1.5.0/example/sftp_RW_nonblock.c new file mode 100644 index 0000000..133815a --- /dev/null +++ b/libssh2-sys/libssh2-1.5.0/example/sftp_RW_nonblock.c @@ -0,0 +1,354 @@ +/* + * Sample showing how to do SFTP transfers in a non-blocking manner. + * + * It will first download a given source file, store it locally and then + * upload the file again to a given destination file. + * + * Using the SFTP server running on 127.0.0.1 + */ + +#include "libssh2_config.h" +#include <libssh2.h> +#include <libssh2_sftp.h> + +#ifdef HAVE_WINSOCK2_H +# include <winsock2.h> +#endif +#ifdef HAVE_SYS_SOCKET_H +# include <sys/socket.h> +#endif +#ifdef HAVE_NETINET_IN_H +# include <netinet/in.h> +#endif +#ifdef HAVE_SYS_SELECT_H +# include <sys/select.h> +#endif +# ifdef HAVE_UNISTD_H +#include <unistd.h> +#endif +#ifdef HAVE_ARPA_INET_H +# include <arpa/inet.h> +#endif +#ifdef HAVE_SYS_TIME_H +# include <sys/time.h> +#endif + +#include <sys/types.h> +#include <fcntl.h> +#include <errno.h> +#include <stdio.h> +#include <ctype.h> + +#define STORAGE "/tmp/sftp-storage" /* this is the local file name this + example uses to store the downloaded + file in */ + +static int waitsocket(int socket_fd, LIBSSH2_SESSION *session) +{ + struct timeval timeout; + int rc; + fd_set fd; + fd_set *writefd = NULL; + fd_set *readfd = NULL; + int dir; + + timeout.tv_sec = 10; + timeout.tv_usec = 0; + + FD_ZERO(&fd); + + FD_SET(socket_fd, &fd); + + /* now make sure we wait in the correct direction */ + dir = libssh2_session_block_directions(session); + + if(dir & LIBSSH2_SESSION_BLOCK_INBOUND) + readfd = &fd; + + if(dir & LIBSSH2_SESSION_BLOCK_OUTBOUND) + writefd = &fd; + + rc = select(socket_fd + 1, readfd, writefd, NULL, &timeout); + + return rc; +} + +int main(int argc, char *argv[]) +{ + int sock, i, auth_pw = 1; + struct sockaddr_in sin; + const char *fingerprint; + LIBSSH2_SESSION *session; + const char *username="username"; + const char *password="password"; + const char *sftppath="/tmp/TEST"; /* source path */ + const char *dest="/tmp/TEST2"; /* destination path */ + int rc; + LIBSSH2_SFTP *sftp_session; + LIBSSH2_SFTP_HANDLE *sftp_handle; + FILE *tempstorage; + char mem[1000]; + struct timeval timeout; + fd_set fd; + +#ifdef WIN32 + WSADATA wsadata; + int err; + + err = WSAStartup(MAKEWORD(2,0), &wsadata); + if (err != 0) { + fprintf(stderr, "WSAStartup failed with error: %d\n", err); + return 1; + } +#endif + + rc = libssh2_init (0); + if (rc != 0) { + fprintf (stderr, "libssh2 initialization failed (%d)\n", rc); + return 1; + } + + /* Ultra basic "connect to port 22 on localhost" + * The application is responsible for creating the socket establishing + * the connection + */ + sock = socket(AF_INET, SOCK_STREAM, 0); + + sin.sin_family = AF_INET; + sin.sin_port = htons(22); + sin.sin_addr.s_addr = htonl(0x7F000001); + if (connect(sock, (struct sockaddr*)(&sin), + sizeof(struct sockaddr_in)) != 0) { + fprintf(stderr, "failed to connect!\n"); + return -1; + } + + /* Create a session instance + */ + session = libssh2_session_init(); + if(!session) + return -1; + + /* ... start it up. This will trade welcome banners, exchange keys, + * and setup crypto, compression, and MAC layers + */ + rc = libssh2_session_handshake(session, sock); + if(rc) { + fprintf(stderr, "Failure establishing SSH session: %d\n", rc); + return -1; + } + + libssh2_session_set_blocking(session, 0); + + /* At this point we havn't yet authenticated. The first thing to do + * is check the hostkey's fingerprint against our known hosts Your app + * may have it hard coded, may go to a file, may present it to the + * user, that's your call + */ + fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_SHA1); + fprintf(stderr, "Fingerprint: "); + for(i = 0; i < 20; i++) { + fprintf(stderr, "%02X ", (unsigned char)fingerprint[i]); + } + fprintf(stderr, "\n"); + + if(argc > 1) { + username = argv[1]; + } + if(argc > 2) { + password = argv[2]; + } + if(argc > 3) { + sftppath = argv[3]; + } + if(argc > 4) { + dest = argv[4]; + } + + tempstorage = fopen(STORAGE, "wb"); + if(!tempstorage) { + fprintf(stderr, "Can't open temp storage file %s\n", STORAGE); + goto shutdown; + } + + if (auth_pw) { + /* We could authenticate via password */ + while ((rc = libssh2_userauth_password(session, username, password)) + == LIBSSH2_ERROR_EAGAIN); + if (rc) { + fprintf(stderr, "Authentication by password failed.\n"); + goto shutdown; + } + } else { + /* Or by public key */ + while ((rc = + libssh2_userauth_publickey_fromfile(session, username, + "/home/username/" + ".ssh/id_rsa.pub", + "/home/username/" + ".ssh/id_rsa", + password)) == + LIBSSH2_ERROR_EAGAIN); + if (rc) { + fprintf(stderr, "\tAuthentication by public key failed\n"); + goto shutdown; + } + } + + do { + sftp_session = libssh2_sftp_init(session); + + if(!sftp_session) { + if(libssh2_session_last_errno(session) == + LIBSSH2_ERROR_EAGAIN) { + fprintf(stderr, "non-blocking init\n"); + waitsocket(sock, session); /* now we wait */ + } + else { + fprintf(stderr, "Unable to init SFTP session\n"); + goto shutdown; + } + } + } while (!sftp_session); + + /* Request a file via SFTP */ + do { + sftp_handle = libssh2_sftp_open(sftp_session, sftppath, + LIBSSH2_FXF_READ, 0); + + if (!sftp_handle) { + if (libssh2_session_last_errno(session) != LIBSSH2_ERROR_EAGAIN) { + fprintf(stderr, "Unable to open file with SFTP\n"); + goto shutdown; + } + else { + fprintf(stderr, "non-blocking open\n"); + waitsocket(sock, session); /* now we wait */ + } + } + } while (!sftp_handle); + + fprintf(stderr, "libssh2_sftp_open() is done, now receive data!\n"); + do { + do { + /* read in a loop until we block */ + rc = libssh2_sftp_read(sftp_handle, mem, sizeof(mem)); + fprintf(stderr, "libssh2_sftp_read returned %d\n", + rc); + + if(rc > 0) { + /* write to stderr */ + write(2, mem, rc); + /* write to temporary storage area */ + fwrite(mem, rc, 1, tempstorage); + } + } while (rc > 0); + + if(rc != LIBSSH2_ERROR_EAGAIN) { + /* error or end of file */ + break; + } + + timeout.tv_sec = 10; + timeout.tv_usec = 0; + + FD_ZERO(&fd); + + FD_SET(sock, &fd); + + /* wait for readable or writeable */ + rc = select(sock+1, &fd, &fd, NULL, &timeout); + if(rc <= 0) { + /* negative is error + 0 is timeout */ + fprintf(stderr, "SFTP download timed out: %d\n", rc); + break; + } + + } while (1); + + libssh2_sftp_close(sftp_handle); + fclose(tempstorage); + + tempstorage = fopen(STORAGE, "rb"); + if(!tempstorage) { + /* weird, we can't read the file we just wrote to... */ + fprintf(stderr, "can't open %s for reading\n", STORAGE); + goto shutdown; + } + + /* we're done downloading, now reverse the process and upload the + temporarily stored data to the destination path */ + sftp_handle = + libssh2_sftp_open(sftp_session, dest, + LIBSSH2_FXF_WRITE|LIBSSH2_FXF_CREAT, + LIBSSH2_SFTP_S_IRUSR|LIBSSH2_SFTP_S_IWUSR| + LIBSSH2_SFTP_S_IRGRP|LIBSSH2_SFTP_S_IROTH); + if(sftp_handle) { + size_t nread; + char *ptr; + do { + nread = fread(mem, 1, sizeof(mem), tempstorage); + if(nread <= 0) { + /* end of file */ + break; + } + ptr = mem; + + do { + /* write data in a loop until we block */ + rc = libssh2_sftp_write(sftp_handle, ptr, + nread); + ptr += rc; + nread -= nread; + } while (rc >= 0); + + if(rc != LIBSSH2_ERROR_EAGAIN) { + /* error or end of file */ + break; + } + + timeout.tv_sec = 10; + timeout.tv_usec = 0; + + FD_ZERO(&fd); + + FD_SET(sock, &fd); + + /* wait for readable or writeable */ + rc = select(sock+1, &fd, &fd, NULL, &timeout); + if(rc <= 0) { + /* negative is error + 0 is timeout */ + fprintf(stderr, "SFTP upload timed out: %d\n", + rc); + break; + } + } while (1); + fprintf(stderr, "SFTP upload done!\n"); + } + else { + fprintf(stderr, "SFTP failed to open destination path: %s\n", + dest); + } + + libssh2_sftp_shutdown(sftp_session); + + shutdown: + + libssh2_session_disconnect(session, "Normal Shutdown, Thank you for playing"); + libssh2_session_free(session); + +#ifdef WIN32 + closesocket(sock); +#else + close(sock); +#endif + if (tempstorage) + fclose(tempstorage); + fprintf(stderr, "all done\n"); + + libssh2_exit(); + + return 0; +} diff --git a/libssh2-sys/libssh2-1.5.0/example/sftp_append.c b/libssh2-sys/libssh2-1.5.0/example/sftp_append.c new file mode 100644 index 0000000..788c51f --- /dev/null +++ b/libssh2-sys/libssh2-1.5.0/example/sftp_append.c @@ -0,0 +1,235 @@ +/* + * Sample showing how to do SFTP append of a local file onto a remote one. + * + * The sample code has default values for host name, user name, password + * and path to copy, but you can specify them on the command line like: + * + * sftp_append 192.168.0.1 user password localfile /tmp/remotefile + */ + +#include "libssh2_config.h" +#include <libssh2.h> +#include <libssh2_sftp.h> + +#ifdef HAVE_WINSOCK2_H +# include <winsock2.h> +#endif +#ifdef HAVE_SYS_SOCKET_H +# include <sys/socket.h> +#endif +#ifdef HAVE_NETINET_IN_H +# include <netinet/in.h> +#endif +# ifdef HAVE_UNISTD_H +#include <unistd.h> +#endif +#ifdef HAVE_ARPA_INET_H +# include <arpa/inet.h> +#endif + +#include <sys/types.h> +#include <fcntl.h> +#include <errno.h> +#include <stdio.h> +#include <ctype.h> + +int main(int argc, char *argv[]) +{ + unsigned long hostaddr; + int sock, i, auth_pw = 1; + struct sockaddr_in sin; + const char *fingerprint; + LIBSSH2_SESSION *session; + const char *username="username"; + const char *password="password"; + const char *loclfile="sftp_write.c"; + const char *sftppath="/tmp/TEST"; + int rc; + FILE *local; + LIBSSH2_SFTP *sftp_session; + LIBSSH2_SFTP_HANDLE *sftp_handle; + LIBSSH2_SFTP_ATTRIBUTES attrs; + char mem[1024*100]; + size_t nread; + char *ptr; + +#ifdef WIN32 + WSADATA wsadata; + int err; + + err = WSAStartup(MAKEWORD(2,0), &wsadata); + if (err != 0) { + fprintf(stderr, "WSAStartup failed with error: %d\n", err); + return 1; + } +#endif + + if (argc > 1) { + hostaddr = inet_addr(argv[1]); + } else { + hostaddr = htonl(0x7F000001); + } + + if(argc > 2) { + username = argv[2]; + } + if(argc > 3) { + password = argv[3]; + } + if(argc > 4) { + loclfile = argv[4]; + } + if(argc > 5) { + sftppath = argv[5]; + } + + rc = libssh2_init (0); + if (rc != 0) { + fprintf (stderr, "libssh2 initialization failed (%d)\n", rc); + return 1; + } + + local = fopen(loclfile, "rb"); + if (!local) { + fprintf(stderr, "Can't open local file %s\n", loclfile); + return -1; + } + + /* + * The application code is responsible for creating the socket + * and establishing the connection + */ + sock = socket(AF_INET, SOCK_STREAM, 0); + + sin.sin_family = AF_INET; + sin.sin_port = htons(22); + sin.sin_addr.s_addr = hostaddr; + if (connect(sock, (struct sockaddr*)(&sin), + sizeof(struct sockaddr_in)) != 0) { + fprintf(stderr, "failed to connect!\n"); + return -1; + } + + /* Create a session instance + */ + session = libssh2_session_init(); + if(!session) + return -1; + + /* Since we have set non-blocking, tell libssh2 we are blocking */ + libssh2_session_set_blocking(session, 1); + + /* ... start it up. This will trade welcome banners, exchange keys, + * and setup crypto, compression, and MAC layers + */ + rc = libssh2_session_handshake(session, sock); + if(rc) { + fprintf(stderr, "Failure establishing SSH session: %d\n", rc); + return -1; + } + + /* At this point we havn't yet authenticated. The first thing to do + * is check the hostkey's fingerprint against our known hosts Your app + * may have it hard coded, may go to a file, may present it to the + * user, that's your call + */ + fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_SHA1); + fprintf(stderr, "Fingerprint: "); + for(i = 0; i < 20; i++) { + fprintf(stderr, "%02X ", (unsigned char)fingerprint[i]); + } + fprintf(stderr, "\n"); + + if (auth_pw) { + /* We could authenticate via password */ + if (libssh2_userauth_password(session, username, password)) { + fprintf(stderr, "Authentication by password failed.\n"); + goto shutdown; + } + } else { + /* Or by public key */ + if (libssh2_userauth_publickey_fromfile(session, username, + "/home/username/.ssh/id_rsa.pub", + "/home/username/.ssh/id_rsa", + password)) { + fprintf(stderr, "\tAuthentication by public key failed\n"); + goto shutdown; + } + } + + fprintf(stderr, "libssh2_sftp_init()!\n"); + sftp_session = libssh2_sftp_init(session); + + if (!sftp_session) { + fprintf(stderr, "Unable to init SFTP session\n"); + goto shutdown; + } + + fprintf(stderr, "libssh2_sftp_open() for READ and WRITE!\n"); + /* Request a file via SFTP */ + + sftp_handle = + libssh2_sftp_open(sftp_session, sftppath, + LIBSSH2_FXF_WRITE|LIBSSH2_FXF_READ, + LIBSSH2_SFTP_S_IRUSR|LIBSSH2_SFTP_S_IWUSR| + LIBSSH2_SFTP_S_IRGRP|LIBSSH2_SFTP_S_IROTH); + if (!sftp_handle) { + fprintf(stderr, "Unable to open file with SFTP\n"); + goto shutdown; + } + + if(libssh2_sftp_fstat_ex(sftp_handle, &attrs, 0) < 0) { + fprintf(stderr, "libssh2_sftp_fstat_ex failed\n"); + goto shutdown; + } + else + libssh2_sftp_seek64(sftp_handle, attrs.filesize); + fprintf(stderr, "Did a seek to position %ld\n", (long) attrs.filesize); + + fprintf(stderr, "libssh2_sftp_open() a handle for APPEND\n"); + + if (!sftp_handle) { + fprintf(stderr, "Unable to open file with SFTP\n"); + goto shutdown; + } + fprintf(stderr, "libssh2_sftp_open() is done, now send data!\n"); + do { + nread = fread(mem, 1, sizeof(mem), local); + if (nread <= 0) { + /* end of file */ + break; + } + ptr = mem; + + do { + /* write data in a loop until we block */ + rc = libssh2_sftp_write(sftp_handle, ptr, nread); + if(rc < 0) + break; + ptr += rc; + nread -= rc; + } while (nread); + + } while (rc > 0); + + libssh2_sftp_close(sftp_handle); + libssh2_sftp_shutdown(sftp_session); + +shutdown: + libssh2_session_disconnect(session, + "Normal Shutdown, Thank you for playing"); + libssh2_session_free(session); + +#ifdef WIN32 + closesocket(sock); +#else + close(sock); +#endif + if (local) + fclose(local); + fprintf(stderr, "all done\n"); + + libssh2_exit(); + + return 0; +} diff --git a/libssh2-sys/libssh2-1.5.0/example/sftp_mkdir.c b/libssh2-sys/libssh2-1.5.0/example/sftp_mkdir.c new file mode 100644 index 0000000..1270adb --- /dev/null +++ b/libssh2-sys/libssh2-1.5.0/example/sftp_mkdir.c @@ -0,0 +1,177 @@ +/* + * Sample showing how to do SFTP mkdir + * + * The sample code has default values for host name, user name, password + * and path to copy, but you can specify them on the command line like: + * + * "sftp 192.168.0.1 user password /tmp/sftp_mkdir" + */ + +#include "libssh2_config.h" +#include <libssh2.h> +#include <libssh2_sftp.h> + +#ifdef HAVE_WINSOCK2_H +# include <winsock2.h> +#endif +#ifdef HAVE_SYS_SOCKET_H +# include <sys/socket.h> +#endif +#ifdef HAVE_NETINET_IN_H +# include <netinet/in.h> +#endif +# ifdef HAVE_UNISTD_H +#include <unistd.h> +#endif +#ifdef HAVE_ARPA_INET_H +# include <arpa/inet.h> +#endif + +#include <sys/types.h> +#include <fcntl.h> +#include <errno.h> +#include <stdio.h> +#include <ctype.h> + +int main(int argc, char *argv[]) +{ + unsigned long hostaddr; + int sock, i, auth_pw = 1; + struct sockaddr_in sin; + const char *fingerprint; + LIBSSH2_SESSION *session; + const char *username="username"; + const char *password="password"; + const char *sftppath="/tmp/sftp_mkdir"; + int rc; + LIBSSH2_SFTP *sftp_session; + +#ifdef WIN32 + WSADATA wsadata; + int err; + + err = WSAStartup(MAKEWORD(2,0), &wsadata); + if (err != 0) { + fprintf(stderr, "WSAStartup failed with error: %d\n", err); + return 1; + } +#endif + + if (argc > 1) { + hostaddr = inet_addr(argv[1]); + } else { + hostaddr = htonl(0x7F000001); + } + + if(argc > 2) { + username = argv[2]; + } + if(argc > 3) { + password = argv[3]; + } + if(argc > 4) { + sftppath = argv[4]; + } + + rc = libssh2_init (0); + if (rc != 0) { + fprintf (stderr, "libssh2 initialization failed (%d)\n", rc); + return 1; + } + + /* + * The application code is responsible for creating the socket + * and establishing the connection + */ + sock = socket(AF_INET, SOCK_STREAM, 0); + + sin.sin_family = AF_INET; + sin.sin_port = htons(22); + sin.sin_addr.s_addr = hostaddr; + if (connect(sock, (struct sockaddr*)(&sin), + sizeof(struct sockaddr_in)) != 0) { + fprintf(stderr, "failed to connect!\n"); + return -1; + } + + /* Create a session instance + */ + session = libssh2_session_init(); + if(!session) + return -1; + + /* ... start it up. This will trade welcome banners, exchange keys, + * and setup crypto, compression, and MAC layers + */ + rc = libssh2_session_handshake(session, sock); + if(rc) { + fprintf(stderr, "Failure establishing SSH session: %d\n", rc); + return -1; + } + + /* At this point we havn't yet authenticated. The first thing to do + * is check the hostkey's fingerprint against our known hosts Your app + * may have it hard coded, may go to a file, may present it to the + * user, that's your call + */ + fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_SHA1); + fprintf(stderr, "Fingerprint: "); + for(i = 0; i < 20; i++) { + fprintf(stderr, "%02X ", (unsigned char)fingerprint[i]); + } + fprintf(stderr, "\n"); + + if (auth_pw) { + /* We could authenticate via password */ + if (libssh2_userauth_password(session, username, password)) { + fprintf(stderr, "Authentication by password failed.\n"); + goto shutdown; + } + } else { + /* Or by public key */ + if (libssh2_userauth_publickey_fromfile(session, username, + "/home/username/.ssh/id_rsa.pub", + "/home/username/.ssh/id_rsa", + password)) { + fprintf(stderr, "\tAuthentication by public key failed\n"); + goto shutdown; + } + } + + sftp_session = libssh2_sftp_init(session); + + if (!sftp_session) { + fprintf(stderr, "Unable to init SFTP session\n"); + goto shutdown; + } + + /* Since we have not set non-blocking, tell libssh2 we are blocking */ + libssh2_session_set_blocking(session, 1); + + /* Make a directory via SFTP */ + rc = libssh2_sftp_mkdir(sftp_session, sftppath, + LIBSSH2_SFTP_S_IRWXU| + LIBSSH2_SFTP_S_IRGRP|LIBSSH2_SFTP_S_IXGRP| + LIBSSH2_SFTP_S_IROTH|LIBSSH2_SFTP_S_IXOTH); + + if(rc) + fprintf(stderr, "libssh2_sftp_mkdir failed: %d\n", rc); + + libssh2_sftp_shutdown(sftp_session); + + shutdown: + + libssh2_session_disconnect(session, "Normal Shutdown, Thank you for playing"); + libssh2_session_free(session); + +#ifdef WIN32 + closesocket(sock); +#else + close(sock); +#endif + fprintf(stderr, "all done\n"); + + libssh2_exit(); + + return 0; +} diff --git a/libssh2-sys/libssh2-1.5.0/example/sftp_mkdir_nonblock.c b/libssh2-sys/libssh2-1.5.0/example/sftp_mkdir_nonblock.c new file mode 100644 index 0000000..db366d2 --- /dev/null +++ b/libssh2-sys/libssh2-1.5.0/example/sftp_mkdir_nonblock.c @@ -0,0 +1,177 @@ +/* + * Sample showing how to do SFTP non-blocking mkdir. + * + * The sample code has default values for host name, user name, password + * and path to copy, but you can specify them on the command line like: + * + * "sftp 192.168.0.1 user password /tmp/sftp_write_nonblock.c" + */ + +#include "libssh2_config.h" +#include <libssh2.h> +#include <libssh2_sftp.h> + +#ifdef HAVE_WINSOCK2_H +# include <winsock2.h> +#endif +#ifdef HAVE_SYS_SOCKET_H +# include <sys/socket.h> +#endif +#ifdef HAVE_NETINET_IN_H +# include <netinet/in.h> +#endif +# ifdef HAVE_UNISTD_H +#include <unistd.h> +#endif +#ifdef HAVE_ARPA_INET_H +# include <arpa/inet.h> +#endif + +#include <sys/types.h> +#include <fcntl.h> +#include <errno.h> +#include <stdio.h> +#include <ctype.h> + +int main(int argc, char *argv[]) +{ + unsigned long hostaddr; + int sock, i, auth_pw = 1; + struct sockaddr_in sin; + const char *fingerprint; + LIBSSH2_SESSION *session; + const char *username="username"; + const char *password="password"; + const char *sftppath="/tmp/sftp_mkdir_nonblock"; + int rc; + LIBSSH2_SFTP *sftp_session; + +#ifdef WIN32 + WSADATA wsadata; + int err; + + err = WSAStartup(MAKEWORD(2,0), &wsadata); + if (err != 0) { + fprintf(stderr, "WSAStartup failed with error: %d\n", err); + return 1; + } +#endif + + if (argc > 1) { + hostaddr = inet_addr(argv[1]); + } else { + hostaddr = htonl(0x7F000001); + } + + if(argc > 2) { + username = argv[2]; + } + if(argc > 3) { + password = argv[3]; + } + if(argc > 4) { + sftppath = argv[4]; + } + + rc = libssh2_init (0); + if (rc != 0) { + fprintf (stderr, "libssh2 initialization failed (%d)\n", rc); + return 1; + } + + /* + * The application code is responsible for creating the socket + * and establishing the connection + */ + sock = socket(AF_INET, SOCK_STREAM, 0); + + sin.sin_family = AF_INET; + sin.sin_port = htons(22); + sin.sin_addr.s_addr = hostaddr; + if (connect(sock, (struct sockaddr*)(&sin), + sizeof(struct sockaddr_in)) != 0) { + fprintf(stderr, "failed to connect!\n"); + return -1; + } + + /* Create a session instance + */ + session = libssh2_session_init(); + if(!session) + return -1; + + /* ... start it up. This will trade welcome banners, exchange keys, + * and setup crypto, compression, and MAC layers + */ + rc = libssh2_session_handshake(session, sock); + if(rc) { + fprintf(stderr, "Failure establishing SSH session: %d\n", rc); + return -1; + } + + /* At this point we havn't yet authenticated. The first thing to do + * is check the hostkey's fingerprint against our known hosts Your app + * may have it hard coded, may go to a file, may present it to the + * user, that's your call + */ + fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_SHA1); + fprintf(stderr, "Fingerprint: "); + for(i = 0; i < 20; i++) { + fprintf(stderr, "%02X ", (unsigned char)fingerprint[i]); + } + fprintf(stderr, "\n"); + + if (auth_pw) { + /* We could authenticate via password */ + if (libssh2_userauth_password(session, username, password)) { + fprintf(stderr, "Authentication by password failed.\n"); + goto shutdown; + } + } else { + /* Or by public key */ + if (libssh2_userauth_publickey_fromfile(session, username, + "/home/username/.ssh/id_rsa.pub", + "/home/username/.ssh/id_rsa", + password)) { + fprintf(stderr, "\tAuthentication by public key failed\n"); + goto shutdown; + } + } + + fprintf(stderr, "libssh2_sftp_init()!\n"); + sftp_session = libssh2_sftp_init(session); + + if (!sftp_session) { + fprintf(stderr, "Unable to init SFTP session\n"); + goto shutdown; + } + + /* Since we have set non-blocking, tell libssh2 we are non-blocking */ + libssh2_session_set_blocking(session, 0); + + fprintf(stderr, "libssh2_sftp_mkdirnb()!\n"); + /* Make a directory via SFTP */ + while (libssh2_sftp_mkdir(sftp_session, sftppath, + LIBSSH2_SFTP_S_IRWXU| + LIBSSH2_SFTP_S_IRGRP|LIBSSH2_SFTP_S_IXGRP| + LIBSSH2_SFTP_S_IROTH|LIBSSH2_SFTP_S_IXOTH) + == LIBSSH2_ERROR_EAGAIN); + + libssh2_sftp_shutdown(sftp_session); + + shutdown: + + libssh2_session_disconnect(session, "Normal Shutdown, Thank you for playing"); + libssh2_session_free(session); + +#ifdef WIN32 + closesocket(sock); +#else + close(sock); +#endif + fprintf(stderr, "all done\n"); + + libssh2_exit(); + + return 0; +} diff --git a/libssh2-sys/libssh2-1.5.0/example/sftp_nonblock.c b/libssh2-sys/libssh2-1.5.0/example/sftp_nonblock.c new file mode 100644 index 0000000..10a6d55 --- /dev/null +++ b/libssh2-sys/libssh2-1.5.0/example/sftp_nonblock.c @@ -0,0 +1,283 @@ +/* + * Sample showing how to do SFTP non-blocking transfers. + * + * The sample code has default values for host name, user name, password + * and path to copy, but you can specify them on the command line like: + * + * "sftp_nonblock 192.168.0.1 user password /tmp/secrets" + */ + +#include "libssh2_config.h" +#include <libssh2.h> +#include <libssh2_sftp.h> + +#ifdef HAVE_WINSOCK2_H +# include <winsock2.h> +#endif +#ifdef HAVE_SYS_SOCKET_H +# include <sys/socket.h> +#endif +#ifdef HAVE_NETINET_IN_H +# include <netinet/in.h> +#endif +#ifdef HAVE_SYS_SELECT_H +# include <sys/select.h> +#endif +# ifdef HAVE_UNISTD_H +#include <unistd.h> +#endif +#ifdef HAVE_ARPA_INET_H +# include <arpa/inet.h> +#endif +#ifdef HAVE_SYS_TIME_H +# include <sys/time.h> +#endif + +#include <sys/types.h> +#include <fcntl.h> +#include <errno.h> +#include <stdio.h> +#include <ctype.h> + +/* diff in ms */ +static long tvdiff(struct timeval newer, struct timeval older) +{ + return (newer.tv_sec-older.tv_sec)*1000+ + (newer.tv_usec-older.tv_usec)/1000; +} + +static int waitsocket(int socket_fd, LIBSSH2_SESSION *session) +{ + struct timeval timeout; + int rc; + fd_set fd; + fd_set *writefd = NULL; + fd_set *readfd = NULL; + int dir; + + timeout.tv_sec = 10; + timeout.tv_usec = 0; + + FD_ZERO(&fd); + + FD_SET(socket_fd, &fd); + + /* now make sure we wait in the correct direction */ + dir = libssh2_session_block_directions(session); + + if(dir & LIBSSH2_SESSION_BLOCK_INBOUND) + readfd = &fd; + + if(dir & LIBSSH2_SESSION_BLOCK_OUTBOUND) + writefd = &fd; + + rc = select(socket_fd + 1, readfd, writefd, NULL, &timeout); + + return rc; +} + +int main(int argc, char *argv[]) +{ + unsigned long hostaddr; + int sock, i, auth_pw = 1; + struct sockaddr_in sin; + const char *fingerprint; + LIBSSH2_SESSION *session; + const char *username="username"; + const char *password="password"; + const char *sftppath="/tmp/TEST"; + struct timeval start; + struct timeval end; + int rc; + int total = 0; + long time_ms; + int spin = 0; + LIBSSH2_SFTP *sftp_session; + LIBSSH2_SFTP_HANDLE *sftp_handle; + +#ifdef WIN32 + WSADATA wsadata; + int err; + + err = WSAStartup(MAKEWORD(2,0), &wsadata); + if (err != 0) { + fprintf(stderr, "WSAStartup failed with error: %d\n", err); + return 1; + } +#endif + + if (argc > 1) { + hostaddr = inet_addr(argv[1]); + } else { + hostaddr = htonl(0x7F000001); + } + + if (argc > 2) { + username = argv[2]; + } + if (argc > 3) { + password = argv[3]; + } + if (argc > 4) { + sftppath = argv[4]; + } + + rc = libssh2_init (0); + if (rc != 0) { + fprintf (stderr, "libssh2 initialization failed (%d)\n", rc); + return 1; + } + + /* + * The application code is responsible for creating the socket + * and establishing the connection + */ + sock = socket(AF_INET, SOCK_STREAM, 0); + + sin.sin_family = AF_INET; + sin.sin_port = htons(22); + sin.sin_addr.s_addr = hostaddr; + if (connect(sock, (struct sockaddr*)(&sin), + sizeof(struct sockaddr_in)) != 0) { + fprintf(stderr, "failed to connect!\n"); + return -1; + } + + /* Create a session instance */ + session = libssh2_session_init(); + if (!session) + return -1; + + /* Since we have set non-blocking, tell libssh2 we are non-blocking */ + libssh2_session_set_blocking(session, 0); + + gettimeofday(&start, NULL); + + /* ... start it up. This will trade welcome banners, exchange keys, + * and setup crypto, compression, and MAC layers + */ + while ((rc = libssh2_session_handshake(session, sock)) == + LIBSSH2_ERROR_EAGAIN); + if (rc) { + fprintf(stderr, "Failure establishing SSH session: %d\n", rc); + return -1; + } + + /* At this point we havn't yet authenticated. The first thing to do + * is check the hostkey's fingerprint against our known hosts Your app + * may have it hard coded, may go to a file, may present it to the + * user, that's your call + */ + fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_SHA1); + fprintf(stderr, "Fingerprint: "); + for(i = 0; i < 20; i++) { + fprintf(stderr, "%02X ", (unsigned char)fingerprint[i]); + } + fprintf(stderr, "\n"); + + if (auth_pw) { + /* We could authenticate via password */ + while ((rc = libssh2_userauth_password(session, username, password)) + == LIBSSH2_ERROR_EAGAIN); + if (rc) { + fprintf(stderr, "Authentication by password failed.\n"); + goto shutdown; + } + } else { + /* Or by public key */ + while ((rc = + libssh2_userauth_publickey_fromfile(session, username, + "/home/username/" + ".ssh/id_rsa.pub", + "/home/username/" + ".ssh/id_rsa", + password)) == + LIBSSH2_ERROR_EAGAIN); + if (rc) { + fprintf(stderr, "\tAuthentication by public key failed\n"); + goto shutdown; + } + } +#if 0 + libssh2_trace(session, LIBSSH2_TRACE_CONN); +#endif + fprintf(stderr, "libssh2_sftp_init()!\n"); + do { + sftp_session = libssh2_sftp_init(session); + + if(!sftp_session) { + if(libssh2_session_last_errno(session) == + LIBSSH2_ERROR_EAGAIN) { + fprintf(stderr, "non-blocking init\n"); + waitsocket(sock, session); /* now we wait */ + } + else { + fprintf(stderr, "Unable to init SFTP session\n"); + goto shutdown; + } + } + } while (!sftp_session); + + fprintf(stderr, "libssh2_sftp_open()!\n"); + /* Request a file via SFTP */ + do { + sftp_handle = libssh2_sftp_open(sftp_session, sftppath, + LIBSSH2_FXF_READ, 0); + + if (!sftp_handle) { + if (libssh2_session_last_errno(session) != LIBSSH2_ERROR_EAGAIN) { + fprintf(stderr, "Unable to open file with SFTP\n"); + goto shutdown; + } + else { + fprintf(stderr, "non-blocking open\n"); + waitsocket(sock, session); /* now we wait */ + } + } + } while (!sftp_handle); + + fprintf(stderr, "libssh2_sftp_open() is done, now receive data!\n"); + do { + char mem[1024*24]; + + /* loop until we fail */ + while ((rc = libssh2_sftp_read(sftp_handle, mem, + sizeof(mem))) == LIBSSH2_ERROR_EAGAIN) { + spin++; + waitsocket(sock, session); /* now we wait */ + } + if (rc > 0) { + total += rc; + write(1, mem, rc); + } else { + break; + } + } while (1); + + gettimeofday(&end, NULL); + time_ms = tvdiff(end, start); + fprintf(stderr, "Got %d bytes in %ld ms = %.1f bytes/sec spin: %d\n", total, + time_ms, total/(time_ms/1000.0), spin ); + + libssh2_sftp_close(sftp_handle); + libssh2_sftp_shutdown(sftp_session); + +shutdown: + + fprintf(stderr, "libssh2_session_disconnect\n"); + while (libssh2_session_disconnect(session, + "Normal Shutdown, Thank you") == + LIBSSH2_ERROR_EAGAIN); + libssh2_session_free(session); + +#ifdef WIN32 + closesocket(sock); +#else + close(sock); +#endif + fprintf(stderr, "all done\n"); + + libssh2_exit(); + + return 0; +} diff --git a/libssh2-sys/libssh2-1.5.0/example/sftp_write.c b/libssh2-sys/libssh2-1.5.0/example/sftp_write.c new file mode 100644 index 0000000..7afc187 --- /dev/null +++ b/libssh2-sys/libssh2-1.5.0/example/sftp_write.c @@ -0,0 +1,219 @@ +/* + * Sample showing how to do SFTP write transfers. + * + * The sample code has default values for host name, user name, password + * and path to copy, but you can specify them on the command line like: + * + * "sftp 192.168.0.1 user password sftp_write.c /tmp/secrets" + */ + +#include "libssh2_config.h" +#include <libssh2.h> +#include <libssh2_sftp.h> + +#ifdef HAVE_WINSOCK2_H +# include <winsock2.h> +#endif +#ifdef HAVE_SYS_SOCKET_H +# include <sys/socket.h> +#endif +#ifdef HAVE_NETINET_IN_H +# include <netinet/in.h> +#endif +# ifdef HAVE_UNISTD_H +#include <unistd.h> +#endif +#ifdef HAVE_ARPA_INET_H +# include <arpa/inet.h> +#endif + +#include <sys/types.h> +#include <fcntl.h> +#include <errno.h> +#include <stdio.h> +#include <ctype.h> + +int main(int argc, char *argv[]) +{ + unsigned long hostaddr; + int sock, i, auth_pw = 1; + struct sockaddr_in sin; + const char *fingerprint; + LIBSSH2_SESSION *session; + const char *username="username"; + const char *password="password"; + const char *loclfile="sftp_write.c"; + const char *sftppath="/tmp/TEST"; + int rc; + FILE *local; + LIBSSH2_SFTP *sftp_session; + LIBSSH2_SFTP_HANDLE *sftp_handle; + char mem[1024*100]; + size_t nread; + char *ptr; + +#ifdef WIN32 + WSADATA wsadata; + int err; + + err = WSAStartup(MAKEWORD(2,0), &wsadata); + if (err != 0) { + fprintf(stderr, "WSAStartup failed with error: %d\n", err); + return 1; + } +#endif + + if (argc > 1) { + hostaddr = inet_addr(argv[1]); + } else { + hostaddr = htonl(0x7F000001); + } + + if(argc > 2) { + username = argv[2]; + } + if(argc > 3) { + password = argv[3]; + } + if(argc > 4) { + loclfile = argv[4]; + } + if(argc > 5) { + sftppath = argv[5]; + } + + rc = libssh2_init (0); + if (rc != 0) { + fprintf (stderr, "libssh2 initialization failed (%d)\n", rc); + return 1; + } + + local = fopen(loclfile, "rb"); + if (!local) { + fprintf(stderr, "Can't open local file %s\n", loclfile); + return -1; + } + + /* + * The application code is responsible for creating the socket + * and establishing the connection + */ + sock = socket(AF_INET, SOCK_STREAM, 0); + + sin.sin_family = AF_INET; + sin.sin_port = htons(22); + sin.sin_addr.s_addr = hostaddr; + if (connect(sock, (struct sockaddr*)(&sin), + sizeof(struct sockaddr_in)) != 0) { + fprintf(stderr, "failed to connect!\n"); + return -1; + } + + /* Create a session instance + */ + session = libssh2_session_init(); + if(!session) + return -1; + + /* Since we have set non-blocking, tell libssh2 we are blocking */ + libssh2_session_set_blocking(session, 1); + + /* ... start it up. This will trade welcome banners, exchange keys, + * and setup crypto, compression, and MAC layers + */ + rc = libssh2_session_handshake(session, sock); + if(rc) { + fprintf(stderr, "Failure establishing SSH session: %d\n", rc); + return -1; + } + + /* At this point we havn't yet authenticated. The first thing to do + * is check the hostkey's fingerprint against our known hosts Your app + * may have it hard coded, may go to a file, may present it to the + * user, that's your call + */ + fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_SHA1); + fprintf(stderr, "Fingerprint: "); + for(i = 0; i < 20; i++) { + fprintf(stderr, "%02X ", (unsigned char)fingerprint[i]); + } + fprintf(stderr, "\n"); + + if (auth_pw) { + /* We could authenticate via password */ + if (libssh2_userauth_password(session, username, password)) { + fprintf(stderr, "Authentication by password failed.\n"); + goto shutdown; + } + } else { + /* Or by public key */ + if (libssh2_userauth_publickey_fromfile(session, username, + "/home/username/.ssh/id_rsa.pub", + "/home/username/.ssh/id_rsa", + password)) { + fprintf(stderr, "\tAuthentication by public key failed\n"); + goto shutdown; + } + } + + fprintf(stderr, "libssh2_sftp_init()!\n"); + sftp_session = libssh2_sftp_init(session); + + if (!sftp_session) { + fprintf(stderr, "Unable to init SFTP session\n"); + goto shutdown; + } + + fprintf(stderr, "libssh2_sftp_open()!\n"); + /* Request a file via SFTP */ + sftp_handle = + libssh2_sftp_open(sftp_session, sftppath, + LIBSSH2_FXF_WRITE|LIBSSH2_FXF_CREAT|LIBSSH2_FXF_TRUNC, + LIBSSH2_SFTP_S_IRUSR|LIBSSH2_SFTP_S_IWUSR| + LIBSSH2_SFTP_S_IRGRP|LIBSSH2_SFTP_S_IROTH); + + if (!sftp_handle) { + fprintf(stderr, "Unable to open file with SFTP\n"); + goto shutdown; + } + fprintf(stderr, "libssh2_sftp_open() is done, now send data!\n"); + do { + nread = fread(mem, 1, sizeof(mem), local); + if (nread <= 0) { + /* end of file */ + break; + } + ptr = mem; + + do { + /* write data in a loop until we block */ + rc = libssh2_sftp_write(sftp_handle, ptr, nread); + if(rc < 0) + break; + ptr += rc; + nread -= rc; + } while (nread); + + } while (rc > 0); + + libssh2_sftp_close(sftp_handle); + libssh2_sftp_shutdown(sftp_session); + +shutdown: + libssh2_session_disconnect(session, + "Normal Shutdown, Thank you for playing"); + libssh2_session_free(session); + +#ifdef WIN32 + closesocket(sock); +#else + close(sock); +#endif + if (local) + fclose(local); + fprintf(stderr, "all done\n"); + + libssh2_exit(); + + return 0; +} diff --git a/libssh2-sys/libssh2-1.5.0/example/sftp_write_nonblock.c b/libssh2-sys/libssh2-1.5.0/example/sftp_write_nonblock.c new file mode 100644 index 0000000..2e22395 --- /dev/null +++ b/libssh2-sys/libssh2-1.5.0/example/sftp_write_nonblock.c @@ -0,0 +1,285 @@ +/* + * Sample showing how to do SFTP non-blocking write transfers. + * + * The sample code has default values for host name, user name, password + * and path to copy, but you can specify them on the command line like: + * + * "sftp 192.168.0.1 user password sftp_write_nonblock.c /tmp/sftp_write_nonblock.c" + */ + +#include "libssh2_config.h" +#include <libssh2.h> +#include <libssh2_sftp.h> + +#ifdef HAVE_WINSOCK2_H +# include <winsock2.h> +#endif +#ifdef HAVE_SYS_SOCKET_H +# include <sys/socket.h> +#endif +#ifdef HAVE_NETINET_IN_H +# include <netinet/in.h> +#endif +#ifdef HAVE_SYS_SELECT_H +# include <sys/select.h> +#endif +# ifdef HAVE_UNISTD_H +#include <unistd.h> +#endif +#ifdef HAVE_ARPA_INET_H +# include <arpa/inet.h> +#endif +#ifdef HAVE_SYS_TIME_H +# include <sys/time.h> +#endif + +#include <sys/types.h> +#include <fcntl.h> +#include <errno.h> +#include <stdio.h> +#include <ctype.h> +#include <time.h> + +static int waitsocket(int socket_fd, LIBSSH2_SESSION *session) +{ + struct timeval timeout; + int rc; + fd_set fd; + fd_set *writefd = NULL; + fd_set *readfd = NULL; + int dir; + + timeout.tv_sec = 10; + timeout.tv_usec = 0; + + FD_ZERO(&fd); + + FD_SET(socket_fd, &fd); + + /* now make sure we wait in the correct direction */ + dir = libssh2_session_block_directions(session); + + if(dir & LIBSSH2_SESSION_BLOCK_INBOUND) + readfd = &fd; + + if(dir & LIBSSH2_SESSION_BLOCK_OUTBOUND) + writefd = &fd; + + rc = select(socket_fd + 1, readfd, writefd, NULL, &timeout); + + return rc; +} + +int main(int argc, char *argv[]) +{ + unsigned long hostaddr; + int sock, i, auth_pw = 1; + struct sockaddr_in sin; + const char *fingerprint; + LIBSSH2_SESSION *session; + const char *username="username"; + const char *password="password"; + const char *loclfile="sftp_write_nonblock.c"; + const char *sftppath="/tmp/sftp_write_nonblock.c"; + int rc; + FILE *local; + LIBSSH2_SFTP *sftp_session; + LIBSSH2_SFTP_HANDLE *sftp_handle; + char mem[1024 * 100]; + size_t nread; + char *ptr; + time_t start; + long total = 0; + int duration; + +#ifdef WIN32 + WSADATA wsadata; + int err; + + err = WSAStartup(MAKEWORD(2,0), &wsadata); + if (err != 0) { + fprintf(stderr, "WSAStartup failed with error: %d\n", err); + return 1; + } +#endif + + if (argc > 1) { + hostaddr = inet_addr(argv[1]); + } else { + hostaddr = htonl(0x7F000001); + } + + if (argc > 2) { + username = argv[2]; + } + if (argc > 3) { + password = argv[3]; + } + if (argc > 4) { + loclfile = argv[4]; + } + if (argc > 5) { + sftppath = argv[5]; + } + + rc = libssh2_init (0); + if (rc != 0) { + fprintf (stderr, "libssh2 initialization failed (%d)\n", rc); + return 1; + } + + local = fopen(loclfile, "rb"); + if (!local) { + fprintf(stderr, "Can't open local file %s\n", loclfile); + return -1; + } + + /* + * The application code is responsible for creating the socket + * and establishing the connection + */ + sock = socket(AF_INET, SOCK_STREAM, 0); + + sin.sin_family = AF_INET; + sin.sin_port = htons(22); + sin.sin_addr.s_addr = hostaddr; + if (connect(sock, (struct sockaddr*)(&sin), + sizeof(struct sockaddr_in)) != 0) { + fprintf(stderr, "failed to connect!\n"); + return -1; + } + + /* Create a session instance + */ + session = libssh2_session_init(); + if (!session) + return -1; + + /* Since we have set non-blocking, tell libssh2 we are non-blocking */ + libssh2_session_set_blocking(session, 0); + + /* ... start it up. This will trade welcome banners, exchange keys, + * and setup crypto, compression, and MAC layers + */ + while ((rc = libssh2_session_handshake(session, sock)) + == LIBSSH2_ERROR_EAGAIN); + if (rc) { + fprintf(stderr, "Failure establishing SSH session: %d\n", rc); + return -1; + } + + /* At this point we havn't yet authenticated. The first thing to do is + * check the hostkey's fingerprint against our known hosts Your app may + * have it hard coded, may go to a file, may present it to the user, + * that's your call + */ + fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_SHA1); + fprintf(stderr, "Fingerprint: "); + for(i = 0; i < 20; i++) { + fprintf(stderr, "%02X ", (unsigned char)fingerprint[i]); + } + fprintf(stderr, "\n"); + + if (auth_pw) { + /* We could authenticate via password */ + while ((rc = libssh2_userauth_password(session, username, password)) == + LIBSSH2_ERROR_EAGAIN); + if (rc) { + fprintf(stderr, "Authentication by password failed.\n"); + goto shutdown; + } + } else { + /* Or by public key */ + while ((rc = libssh2_userauth_publickey_fromfile(session, username, + "/home/username/.ssh/id_rsa.pub", + "/home/username/.ssh/id_rsa", + password)) == + LIBSSH2_ERROR_EAGAIN); + if (rc) { + fprintf(stderr, "\tAuthentication by public key failed\n"); + goto shutdown; + } + } + + fprintf(stderr, "libssh2_sftp_init()!\n"); + do { + sftp_session = libssh2_sftp_init(session); + + if (!sftp_session && + (libssh2_session_last_errno(session) != LIBSSH2_ERROR_EAGAIN)) { + fprintf(stderr, "Unable to init SFTP session\n"); + goto shutdown; + } + } while (!sftp_session); + + fprintf(stderr, "libssh2_sftp_open()!\n"); + /* Request a file via SFTP */ + do { + sftp_handle = + libssh2_sftp_open(sftp_session, sftppath, + LIBSSH2_FXF_WRITE|LIBSSH2_FXF_CREAT|LIBSSH2_FXF_TRUNC, + LIBSSH2_SFTP_S_IRUSR|LIBSSH2_SFTP_S_IWUSR| + LIBSSH2_SFTP_S_IRGRP|LIBSSH2_SFTP_S_IROTH); + + if (!sftp_handle && + (libssh2_session_last_errno(session) != LIBSSH2_ERROR_EAGAIN)) { + fprintf(stderr, "Unable to open file with SFTP\n"); + goto shutdown; + } + } while (!sftp_handle); + + fprintf(stderr, "libssh2_sftp_open() is done, now send data!\n"); + + start = time(NULL); + + do { + nread = fread(mem, 1, sizeof(mem), local); + if (nread <= 0) { + /* end of file */ + break; + } + ptr = mem; + + total += nread; + + do { + /* write data in a loop until we block */ + while ((rc = libssh2_sftp_write(sftp_handle, ptr, nread)) == + LIBSSH2_ERROR_EAGAIN) { + waitsocket(sock, session); + } + if(rc < 0) + break; + ptr += rc; + nread -= rc; + + } while (nread); + } while (rc > 0); + + duration = (int)(time(NULL)-start); + + fprintf(stderr, "%ld bytes in %d seconds makes %.1f bytes/sec\n", + total, duration, total/(double)duration); + + + fclose(local); + libssh2_sftp_close(sftp_handle); + libssh2_sftp_shutdown(sftp_session); + +shutdown: + + while (libssh2_session_disconnect(session, "Normal Shutdown, Thank you for playing") + == LIBSSH2_ERROR_EAGAIN); + libssh2_session_free(session); + +#ifdef WIN32 + closesocket(sock); +#else + close(sock); +#endif + fprintf(stderr, "all done\n"); + + libssh2_exit(); + + return 0; +} diff --git a/libssh2-sys/libssh2-1.5.0/example/sftp_write_sliding.c b/libssh2-sys/libssh2-1.5.0/example/sftp_write_sliding.c new file mode 100644 index 0000000..19fe851 --- /dev/null +++ b/libssh2-sys/libssh2-1.5.0/example/sftp_write_sliding.c @@ -0,0 +1,294 @@ +/* + * Sample showing how to do SFTP non-blocking write transfers. + * + * The sample code has default values for host name, user name, password + * and path to copy, but you can specify them on the command line like: + * + * "sftp 192.168.0.1 user password sftp_write_nonblock.c /tmp/sftp_write_nonblock.c" + */ + +#include "libssh2_config.h" +#include <libssh2.h> +#include <libssh2_sftp.h> + +#ifdef HAVE_WINSOCK2_H +# include <winsock2.h> +#endif +#ifdef HAVE_SYS_SOCKET_H +# include <sys/socket.h> +#endif +#ifdef HAVE_NETINET_IN_H +# include <netinet/in.h> +#endif +#ifdef HAVE_SYS_SELECT_H +# include <sys/select.h> +#endif +# ifdef HAVE_UNISTD_H +#include <unistd.h> +#endif +#ifdef HAVE_ARPA_INET_H +# include <arpa/inet.h> +#endif +#ifdef HAVE_SYS_TIME_H +# include <sys/time.h> +#endif + +#include <sys/types.h> +#include <fcntl.h> +#include <errno.h> +#include <stdio.h> +#include <ctype.h> +#include <time.h> + +static int waitsocket(int socket_fd, LIBSSH2_SESSION *session) +{ + struct timeval timeout; + int rc; + fd_set fd; + fd_set *writefd = NULL; + fd_set *readfd = NULL; + int dir; + + timeout.tv_sec = 10; + timeout.tv_usec = 0; + + FD_ZERO(&fd); + + FD_SET(socket_fd, &fd); + + /* now make sure we wait in the correct direction */ + dir = libssh2_session_block_directions(session); + + if(dir & LIBSSH2_SESSION_BLOCK_INBOUND) + readfd = &fd; + + if(dir & LIBSSH2_SESSION_BLOCK_OUTBOUND) + writefd = &fd; + + rc = select(socket_fd + 1, readfd, writefd, NULL, &timeout); + + return rc; +} + +int main(int argc, char *argv[]) +{ + unsigned long hostaddr; + int sock, i, auth_pw = 1; + struct sockaddr_in sin; + const char *fingerprint; + LIBSSH2_SESSION *session; + const char *username="username"; + const char *password="password"; + const char *loclfile="sftp_write_nonblock.c"; + const char *sftppath="/tmp/sftp_write_nonblock.c"; + int rc; + FILE *local; + LIBSSH2_SFTP *sftp_session; + LIBSSH2_SFTP_HANDLE *sftp_handle; + char mem[1024 * 1000]; + size_t nread; + size_t memuse; + time_t start; + long total = 0; + int duration; + +#ifdef WIN32 + WSADATA wsadata; + int err; + + err = WSAStartup(MAKEWORD(2,0), &wsadata); + if (err != 0) { + fprintf(stderr, "WSAStartup failed with error: %d\n", err); + return 1; + } +#endif + + if (argc > 1) { + hostaddr = inet_addr(argv[1]); + } else { + hostaddr = htonl(0x7F000001); + } + + if (argc > 2) { + username = argv[2]; + } + if (argc > 3) { + password = argv[3]; + } + if (argc > 4) { + loclfile = argv[4]; + } + if (argc > 5) { + sftppath = argv[5]; + } + + rc = libssh2_init (0); + if (rc != 0) { + fprintf (stderr, "libssh2 initialization failed (%d)\n", rc); + return 1; + } + + local = fopen(loclfile, "rb"); + if (!local) { + fprintf(stderr, "Can't open local file %s\n", loclfile); + return -1; + } + + /* + * The application code is responsible for creating the socket + * and establishing the connection + */ + sock = socket(AF_INET, SOCK_STREAM, 0); + + sin.sin_family = AF_INET; + sin.sin_port = htons(22); + sin.sin_addr.s_addr = hostaddr; + if (connect(sock, (struct sockaddr*)(&sin), + sizeof(struct sockaddr_in)) != 0) { + fprintf(stderr, "failed to connect!\n"); + return -1; + } + + /* Create a session instance + */ + session = libssh2_session_init(); + if (!session) + return -1; + + /* Since we have set non-blocking, tell libssh2 we are non-blocking */ + libssh2_session_set_blocking(session, 0); + + /* ... start it up. This will trade welcome banners, exchange keys, + * and setup crypto, compression, and MAC layers + */ + while ((rc = libssh2_session_handshake(session, sock)) + == LIBSSH2_ERROR_EAGAIN); + if (rc) { + fprintf(stderr, "Failure establishing SSH session: %d\n", rc); + return -1; + } + + /* At this point we havn't yet authenticated. The first thing to do is + * check the hostkey's fingerprint against our known hosts Your app may + * have it hard coded, may go to a file, may present it to the user, + * that's your call + */ + fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_SHA1); + fprintf(stderr, "Fingerprint: "); + for(i = 0; i < 20; i++) { + fprintf(stderr, "%02X ", (unsigned char)fingerprint[i]); + } + fprintf(stderr, "\n"); + + if (auth_pw) { + /* We could authenticate via password */ + while ((rc = libssh2_userauth_password(session, username, password)) == + LIBSSH2_ERROR_EAGAIN); + if (rc) { + fprintf(stderr, "Authentication by password failed.\n"); + goto shutdown; + } + } else { + /* Or by public key */ + while ((rc = libssh2_userauth_publickey_fromfile(session, username, + "/home/username/.ssh/id_rsa.pub", + "/home/username/.ssh/id_rsa", + password)) == + LIBSSH2_ERROR_EAGAIN); + if (rc) { + fprintf(stderr, "\tAuthentication by public key failed\n"); + goto shutdown; + } + } + + fprintf(stderr, "libssh2_sftp_init()!\n"); + do { + sftp_session = libssh2_sftp_init(session); + + if (!sftp_session && + (libssh2_session_last_errno(session) != LIBSSH2_ERROR_EAGAIN)) { + fprintf(stderr, "Unable to init SFTP session\n"); + goto shutdown; + } + } while (!sftp_session); + + fprintf(stderr, "libssh2_sftp_open()!\n"); + /* Request a file via SFTP */ + do { + sftp_handle = + libssh2_sftp_open(sftp_session, sftppath, + LIBSSH2_FXF_WRITE|LIBSSH2_FXF_CREAT|LIBSSH2_FXF_TRUNC, + LIBSSH2_SFTP_S_IRUSR|LIBSSH2_SFTP_S_IWUSR| + LIBSSH2_SFTP_S_IRGRP|LIBSSH2_SFTP_S_IROTH); + + if (!sftp_handle && + (libssh2_session_last_errno(session) != LIBSSH2_ERROR_EAGAIN)) { + fprintf(stderr, "Unable to open file with SFTP\n"); + goto shutdown; + } + } while (!sftp_handle); + + fprintf(stderr, "libssh2_sftp_open() is done, now send data!\n"); + + start = time(NULL); + + memuse = 0; /* it starts blank */ + do { + nread = fread(&mem[memuse], 1, sizeof(mem)-memuse, local); + if (nread <= 0) { + /* end of file */ + if (memuse > 0) + /* the previous sending is not finished */ + nread = 0; + else + break; + } + memuse += nread; + total += nread; + + /* write data in a loop until we block */ + while ((rc = libssh2_sftp_write(sftp_handle, mem, memuse)) == + LIBSSH2_ERROR_EAGAIN) { + waitsocket(sock, session); + } + if(rc < 0) + break; + + if(memuse - rc) { + /* make room for more data at the end of the buffer */ + memmove(&mem[0], &mem[rc], memuse - rc); + memuse -= rc; + } + else + /* 'mem' was consumed fully */ + memuse = 0; + + } while (rc > 0); + + duration = (int)(time(NULL)-start); + + fprintf(stderr, "%ld bytes in %d seconds makes %.1f bytes/sec\n", + total, duration, total/(double)duration); + + + fclose(local); + libssh2_sftp_close(sftp_handle); + libssh2_sftp_shutdown(sftp_session); + +shutdown: + + while (libssh2_session_disconnect(session, "Normal Shutdown, Thank you for playing") + == LIBSSH2_ERROR_EAGAIN); + libssh2_session_free(session); + +#ifdef WIN32 + closesocket(sock); +#else + close(sock); +#endif + fprintf(stderr, "all done\n"); + + libssh2_exit(); + + return 0; +} diff --git a/libssh2-sys/libssh2-1.5.0/example/sftpdir.c b/libssh2-sys/libssh2-1.5.0/example/sftpdir.c new file mode 100644 index 0000000..c21f9b3 --- /dev/null +++ b/libssh2-sys/libssh2-1.5.0/example/sftpdir.c @@ -0,0 +1,306 @@ +/* + * Sample doing an SFTP directory listing. + * + * The sample code has default values for host name, user name, password and + * path, but you can specify them on the command line like: + * + * "sftpdir 192.168.0.1 user password /tmp/secretdir" + */ + +#include "libssh2_config.h" +#include <libssh2.h> +#include <libssh2_sftp.h> + +#ifdef HAVE_WINSOCK2_H +# include <winsock2.h> +#endif +#ifdef HAVE_SYS_SOCKET_H +# include <sys/socket.h> +#endif +#ifdef HAVE_NETINET_IN_H +# include <netinet/in.h> +#endif +#ifdef HAVE_UNISTD_H +# include <unistd.h> +#endif +#ifdef HAVE_ARPA_INET_H +# include <arpa/inet.h> +#endif +#ifdef HAVE_INTTYPES_H +# include <inttypes.h> +#endif + +#include <sys/types.h> +#include <fcntl.h> +#include <errno.h> +#include <stdio.h> +#include <ctype.h> + +/* last resort for systems not defining PRIu64 in inttypes.h */ +#ifndef __PRI64_PREFIX +#ifdef WIN32 +#define __PRI64_PREFIX "I64" +#else +#if __WORDSIZE == 64 +#define __PRI64_PREFIX "l" +#else +#define __PRI64_PREFIX "ll" +#endif /* __WORDSIZE */ +#endif /* WIN32 */ +#endif /* !__PRI64_PREFIX */ +#ifndef PRIu64 +#define PRIu64 __PRI64_PREFIX "u" +#endif /* PRIu64 */ + +const char *keyfile1="~/.ssh/id_rsa.pub"; +const char *keyfile2="~/.ssh/id_rsa"; +const char *username="username"; +const char *password="password"; + +static void kbd_callback(const char *name, int name_len, + const char *instruction, int instruction_len, + int num_prompts, + const LIBSSH2_USERAUTH_KBDINT_PROMPT *prompts, + LIBSSH2_USERAUTH_KBDINT_RESPONSE *responses, + void **abstract) +{ + (void)name; + (void)name_len; + (void)instruction; + (void)instruction_len; + if (num_prompts == 1) { + responses[0].text = strdup(password); + responses[0].length = strlen(password); + } + (void)prompts; + (void)abstract; +} /* kbd_callback */ + +int main(int argc, char *argv[]) +{ + unsigned long hostaddr; + int rc, sock, i, auth_pw = 0; + struct sockaddr_in sin; + const char *fingerprint; + char *userauthlist; + LIBSSH2_SESSION *session; + const char *sftppath="/tmp/secretdir"; + LIBSSH2_SFTP *sftp_session; + LIBSSH2_SFTP_HANDLE *sftp_handle; + +#ifdef WIN32 + WSADATA wsadata; + int err; + + err = WSAStartup(MAKEWORD(2,0), &wsadata); + if (err != 0) { + fprintf(stderr, "WSAStartup failed with error: %d\n", err); + return 1; + } +#endif + + if (argc > 1) { + hostaddr = inet_addr(argv[1]); + } else { + hostaddr = htonl(0x7F000001); + } + + if(argc > 2) { + username = argv[2]; + } + if(argc > 3) { + password = argv[3]; + } + if(argc > 4) { + sftppath = argv[4]; + } + + rc = libssh2_init (0); + if (rc != 0) { + fprintf (stderr, "libssh2 initialization failed (%d)\n", rc); + return 1; + } + + /* + * The application code is responsible for creating the socket + * and establishing the connection + */ + sock = socket(AF_INET, SOCK_STREAM, 0); + + sin.sin_family = AF_INET; + sin.sin_port = htons(22); + sin.sin_addr.s_addr = hostaddr; + if (connect(sock, (struct sockaddr*)(&sin), + sizeof(struct sockaddr_in)) != 0) { + fprintf(stderr, "failed to connect!\n"); + return -1; + } + + /* Create a session instance + */ + session = libssh2_session_init(); + if(!session) + return -1; + + /* ... start it up. This will trade welcome banners, exchange keys, + * and setup crypto, compression, and MAC layers + */ + rc = libssh2_session_handshake(session, sock); + if(rc) { + fprintf(stderr, "Failure establishing SSH session: %d\n", rc); + return -1; + } + + /* At this point we havn't yet authenticated. The first thing to do + * is check the hostkey's fingerprint against our known hosts Your app + * may have it hard coded, may go to a file, may present it to the + * user, that's your call + */ + fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_SHA1); + fprintf(stderr, "Fingerprint: "); + for(i = 0; i < 20; i++) { + fprintf(stderr, "%02X ", (unsigned char)fingerprint[i]); + } + fprintf(stderr, "\n"); + + /* check what authentication methods are available */ + userauthlist = libssh2_userauth_list(session, username, strlen(username)); + fprintf(stderr, "Authentication methods: %s\n", userauthlist); + if (strstr(userauthlist, "password") != NULL) { + auth_pw |= 1; + } + if (strstr(userauthlist, "keyboard-interactive") != NULL) { + auth_pw |= 2; + } + if (strstr(userauthlist, "publickey") != NULL) { + auth_pw |= 4; + } + + /* if we got an 5. argument we set this option if supported */ + if(argc > 5) { + if ((auth_pw & 1) && !strcasecmp(argv[5], "-p")) { + auth_pw = 1; + } + if ((auth_pw & 2) && !strcasecmp(argv[5], "-i")) { + auth_pw = 2; + } + if ((auth_pw & 4) && !strcasecmp(argv[5], "-k")) { + auth_pw = 4; + } + } + + if (auth_pw & 1) { + /* We could authenticate via password */ + if (libssh2_userauth_password(session, username, password)) { + fprintf(stderr, "\tAuthentication by password failed!\n"); + goto shutdown; + } else { + fprintf(stderr, "\tAuthentication by password succeeded.\n"); + } + } else if (auth_pw & 2) { + /* Or via keyboard-interactive */ + if (libssh2_userauth_keyboard_interactive(session, username, + &kbd_callback) ) { + fprintf(stderr, + "\tAuthentication by keyboard-interactive failed!\n"); + goto shutdown; + } else { + fprintf(stderr, + "\tAuthentication by keyboard-interactive succeeded.\n"); + } + } else if (auth_pw & 4) { + /* Or by public key */ + if (libssh2_userauth_publickey_fromfile(session, username, keyfile1, + keyfile2, password)) { + fprintf(stderr, "\tAuthentication by public key failed!\n"); + goto shutdown; + } else { + fprintf(stderr, "\tAuthentication by public key succeeded.\n"); + } + } else { + fprintf(stderr, "No supported authentication methods found!\n"); + goto shutdown; + } + + fprintf(stderr, "libssh2_sftp_init()!\n"); + sftp_session = libssh2_sftp_init(session); + + if (!sftp_session) { + fprintf(stderr, "Unable to init SFTP session\n"); + goto shutdown; + } + + /* Since we have not set non-blocking, tell libssh2 we are blocking */ + libssh2_session_set_blocking(session, 1); + + fprintf(stderr, "libssh2_sftp_opendir()!\n"); + /* Request a dir listing via SFTP */ + sftp_handle = libssh2_sftp_opendir(sftp_session, sftppath); + + if (!sftp_handle) { + fprintf(stderr, "Unable to open dir with SFTP\n"); + goto shutdown; + } + fprintf(stderr, "libssh2_sftp_opendir() is done, now receive listing!\n"); + do { + char mem[512]; + char longentry[512]; + LIBSSH2_SFTP_ATTRIBUTES attrs; + + /* loop until we fail */ + rc = libssh2_sftp_readdir_ex(sftp_handle, mem, sizeof(mem), + longentry, sizeof(longentry), &attrs); + if(rc > 0) { + /* rc is the length of the file name in the mem + buffer */ + + if (longentry[0] != '\0') { + printf("%s\n", longentry); + } else { + if(attrs.flags & LIBSSH2_SFTP_ATTR_PERMISSIONS) { + /* this should check what permissions it + is and print the output accordingly */ + printf("--fix----- "); + } + else { + printf("---------- "); + } + + if(attrs.flags & LIBSSH2_SFTP_ATTR_UIDGID) { + printf("%4ld %4ld ", attrs.uid, attrs.gid); + } + else { + printf(" - - "); + } + + if(attrs.flags & LIBSSH2_SFTP_ATTR_SIZE) { + printf("%8" PRIu64 " ", attrs.filesize); + } + + printf("%s\n", mem); + } + } + else + break; + + } while (1); + + libssh2_sftp_closedir(sftp_handle); + libssh2_sftp_shutdown(sftp_session); + + shutdown: + + libssh2_session_disconnect(session, "Normal Shutdown, Thank you for playing"); + libssh2_session_free(session); + +#ifdef WIN32 + closesocket(sock); +#else + close(sock); +#endif + fprintf(stderr, "all done\n"); + + libssh2_exit(); + + return 0; +} diff --git a/libssh2-sys/libssh2-1.5.0/example/sftpdir_nonblock.c b/libssh2-sys/libssh2-1.5.0/example/sftpdir_nonblock.c new file mode 100644 index 0000000..1950e67 --- /dev/null +++ b/libssh2-sys/libssh2-1.5.0/example/sftpdir_nonblock.c @@ -0,0 +1,252 @@ +/* + * Sample doing an SFTP directory listing. + * + * The sample code has default values for host name, user name, password and + * path, but you can specify them on the command line like: + * + * "sftpdir 192.168.0.1 user password /tmp/secretdir" + */ + +#include "libssh2_config.h" +#include <libssh2.h> +#include <libssh2_sftp.h> + +#ifdef HAVE_WINSOCK2_H +# include <winsock2.h> +#endif +#ifdef HAVE_SYS_SOCKET_H +# include <sys/socket.h> +#endif +#ifdef HAVE_NETINET_IN_H +# include <netinet/in.h> +#endif +# ifdef HAVE_UNISTD_H +#include <unistd.h> +#endif +#ifdef HAVE_ARPA_INET_H +# include <arpa/inet.h> +#endif +#ifdef HAVE_INTTYPES_H +# include <inttypes.h> +#endif + +#include <sys/types.h> +#include <fcntl.h> +#include <errno.h> +#include <stdio.h> +#include <ctype.h> + +/* last resort for systems not defining PRIu64 in inttypes.h */ +#ifndef __PRI64_PREFIX +#ifdef WIN32 +#define __PRI64_PREFIX "I64" +#else +#if __WORDSIZE == 64 +#define __PRI64_PREFIX "l" +#else +#define __PRI64_PREFIX "ll" +#endif /* __WORDSIZE */ +#endif /* WIN32 */ +#endif /* !__PRI64_PREFIX */ +#ifndef PRIu64 +#define PRIu64 __PRI64_PREFIX "u" +#endif /* PRIu64 */ + +int main(int argc, char *argv[]) +{ + unsigned long hostaddr; + int sock, i, auth_pw = 1; + struct sockaddr_in sin; + const char *fingerprint; + LIBSSH2_SESSION *session; + const char *username="username"; + const char *password="password"; + const char *sftppath="/tmp/secretdir"; + int rc; + LIBSSH2_SFTP *sftp_session; + LIBSSH2_SFTP_HANDLE *sftp_handle; + +#ifdef WIN32 + WSADATA wsadata; + int err; + + err = WSAStartup(MAKEWORD(2,0), &wsadata); + if (err != 0) { + fprintf(stderr, "WSAStartup failed with error: %d\n", err); + return 1; + } +#endif + + if (argc > 1) { + hostaddr = inet_addr(argv[1]); + } else { + hostaddr = htonl(0x7F000001); + } + + if(argc > 2) { + username = argv[2]; + } + if(argc > 3) { + password = argv[3]; + } + if(argc > 4) { + sftppath = argv[4]; + } + + rc = libssh2_init (0); + if (rc != 0) { + fprintf (stderr, "libssh2 initialization failed (%d)\n", rc); + return 1; + } + + /* + * The application code is responsible for creating the socket + * and establishing the connection + */ + sock = socket(AF_INET, SOCK_STREAM, 0); + + sin.sin_family = AF_INET; + sin.sin_port = htons(22); + sin.sin_addr.s_addr = hostaddr; + if (connect(sock, (struct sockaddr*)(&sin), + sizeof(struct sockaddr_in)) != 0) { + fprintf(stderr, "failed to connect!\n"); + return -1; + } + + /* Create a session instance + */ + session = libssh2_session_init(); + if(!session) + return -1; + + /* Since we have set non-blocking, tell libssh2 we are non-blocking */ + libssh2_session_set_blocking(session, 0); + + /* ... start it up. This will trade welcome banners, exchange keys, + * and setup crypto, compression, and MAC layers + */ + while ((rc = libssh2_session_handshake(session, sock)) == + LIBSSH2_ERROR_EAGAIN); + if(rc) { + fprintf(stderr, "Failure establishing SSH session: %d\n", rc); + return -1; + } + + /* At this point we havn't yet authenticated. The first thing to do + * is check the hostkey's fingerprint against our known hosts Your app + * may have it hard coded, may go to a file, may present it to the + * user, that's your call + */ + fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_SHA1); + fprintf(stderr, "Fingerprint: "); + for(i = 0; i < 20; i++) { + fprintf(stderr, "%02X ", (unsigned char)fingerprint[i]); + } + fprintf(stderr, "\n"); + + if (auth_pw) { + /* We could authenticate via password */ + while ((rc = libssh2_userauth_password(session, username, password)) == + LIBSSH2_ERROR_EAGAIN); + if (rc) { + fprintf(stderr, "Authentication by password failed.\n"); + goto shutdown; + } + } else { + /* Or by public key */ + while ((rc = libssh2_userauth_publickey_fromfile(session, username, + "/home/username/.ssh/id_rsa.pub", + "/home/username/.ssh/id_rsa", + password)) == LIBSSH2_ERROR_EAGAIN); + if (rc) { + fprintf(stderr, "\tAuthentication by public key failed\n"); + goto shutdown; + } + } + + fprintf(stderr, "libssh2_sftp_init()!\n"); + do { + sftp_session = libssh2_sftp_init(session); + + if ((!sftp_session) && (libssh2_session_last_errno(session) != + LIBSSH2_ERROR_EAGAIN)) { + fprintf(stderr, "Unable to init SFTP session\n"); + goto shutdown; + } + } while (!sftp_session); + + fprintf(stderr, "libssh2_sftp_opendir()!\n"); + /* Request a dir listing via SFTP */ + do { + sftp_handle = libssh2_sftp_opendir(sftp_session, sftppath); + + if ((!sftp_handle) && (libssh2_session_last_errno(session) != + LIBSSH2_ERROR_EAGAIN)) { + fprintf(stderr, "Unable to open dir with SFTP\n"); + goto shutdown; + } + } while (!sftp_handle); + + fprintf(stderr, "libssh2_sftp_opendir() is done, now receive listing!\n"); + do { + char mem[512]; + LIBSSH2_SFTP_ATTRIBUTES attrs; + + /* loop until we fail */ + while ((rc = libssh2_sftp_readdir(sftp_handle, mem, sizeof(mem), + &attrs)) == LIBSSH2_ERROR_EAGAIN) { + ; + } + if(rc > 0) { + /* rc is the length of the file name in the mem + buffer */ + + if(attrs.flags & LIBSSH2_SFTP_ATTR_PERMISSIONS) { + /* this should check what permissions it + is and print the output accordingly */ + printf("--fix----- "); + } else { + printf("---------- "); + } + + if(attrs.flags & LIBSSH2_SFTP_ATTR_UIDGID) { + printf("%4ld %4ld ", attrs.uid, attrs.gid); + } else { + printf(" - - "); + } + + if(attrs.flags & LIBSSH2_SFTP_ATTR_SIZE) { + printf("%8" PRIu64 " ", attrs.filesize); + } + + printf("%s\n", mem); + } + else if (rc == LIBSSH2_ERROR_EAGAIN) { + /* blocking */ + fprintf(stderr, "Blocking\n"); + } else { + break; + } + + } while (1); + + libssh2_sftp_closedir(sftp_handle); + libssh2_sftp_shutdown(sftp_session); + + shutdown: + + libssh2_session_disconnect(session, "Normal Shutdown, Thank you for playing"); + libssh2_session_free(session); + +#ifdef WIN32 + closesocket(sock); +#else + close(sock); +#endif + fprintf(stderr, "all done\n"); + + libssh2_exit(); + + return 0; +} diff --git a/libssh2-sys/libssh2-1.5.0/example/ssh2.c b/libssh2-sys/libssh2-1.5.0/example/ssh2.c new file mode 100644 index 0000000..d9a88ef --- /dev/null +++ b/libssh2-sys/libssh2-1.5.0/example/ssh2.c @@ -0,0 +1,266 @@ +/* + * Sample showing how to do SSH2 connect. + * + * The sample code has default values for host name, user name, password + * and path to copy, but you can specify them on the command line like: + * + * "ssh2 host user password [-p|-i|-k]" + */ + +#include "libssh2_config.h" +#include <libssh2.h> +#include <libssh2_sftp.h> + +#ifdef HAVE_WINDOWS_H +# include <windows.h> +#endif +#ifdef HAVE_WINSOCK2_H +# include <winsock2.h> +#endif +#ifdef HAVE_SYS_SOCKET_H +# include <sys/socket.h> +#endif +#ifdef HAVE_NETINET_IN_H +# include <netinet/in.h> +#endif +# ifdef HAVE_UNISTD_H +#include <unistd.h> +#endif +# ifdef HAVE_ARPA_INET_H +#include <arpa/inet.h> +#endif + +#include <sys/types.h> +#include <fcntl.h> +#include <errno.h> +#include <stdio.h> +#include <ctype.h> + + +const char *keyfile1="~/.ssh/id_rsa.pub"; +const char *keyfile2="~/.ssh/id_rsa"; +const char *username="username"; +const char *password="password"; + + +static void kbd_callback(const char *name, int name_len, + const char *instruction, int instruction_len, + int num_prompts, + const LIBSSH2_USERAUTH_KBDINT_PROMPT *prompts, + LIBSSH2_USERAUTH_KBDINT_RESPONSE *responses, + void **abstract) +{ + (void)name; + (void)name_len; + (void)instruction; + (void)instruction_len; + if (num_prompts == 1) { + responses[0].text = strdup(password); + responses[0].length = strlen(password); + } + (void)prompts; + (void)abstract; +} /* kbd_callback */ + + +int main(int argc, char *argv[]) +{ + unsigned long hostaddr; + int rc, sock, i, auth_pw = 0; + struct sockaddr_in sin; + const char *fingerprint; + char *userauthlist; + LIBSSH2_SESSION *session; + LIBSSH2_CHANNEL *channel; + +#ifdef WIN32 + WSADATA wsadata; + int err; + + err = WSAStartup(MAKEWORD(2,0), &wsadata); + if (err != 0) { + fprintf(stderr, "WSAStartup failed with error: %d\n", err); + return 1; + } +#endif + + if (argc > 1) { + hostaddr = inet_addr(argv[1]); + } else { + hostaddr = htonl(0x7F000001); + } + + if(argc > 2) { + username = argv[2]; + } + if(argc > 3) { + password = argv[3]; + } + + rc = libssh2_init (0); + if (rc != 0) { + fprintf (stderr, "libssh2 initialization failed (%d)\n", rc); + return 1; + } + + /* Ultra basic "connect to port 22 on localhost". Your code is + * responsible for creating the socket establishing the connection + */ + sock = socket(AF_INET, SOCK_STREAM, 0); + + sin.sin_family = AF_INET; + sin.sin_port = htons(22); + sin.sin_addr.s_addr = hostaddr; + if (connect(sock, (struct sockaddr*)(&sin), + sizeof(struct sockaddr_in)) != 0) { + fprintf(stderr, "failed to connect!\n"); + return -1; + } + + /* Create a session instance and start it up. This will trade welcome + * banners, exchange keys, and setup crypto, compression, and MAC layers + */ + session = libssh2_session_init(); + if (libssh2_session_handshake(session, sock)) { + fprintf(stderr, "Failure establishing SSH session\n"); + return -1; + } + + /* At this point we havn't authenticated. The first thing to do is check + * the hostkey's fingerprint against our known hosts Your app may have it + * hard coded, may go to a file, may present it to the user, that's your + * call + */ + fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_SHA1); + fprintf(stderr, "Fingerprint: "); + for(i = 0; i < 20; i++) { + fprintf(stderr, "%02X ", (unsigned char)fingerprint[i]); + } + fprintf(stderr, "\n"); + + /* check what authentication methods are available */ + userauthlist = libssh2_userauth_list(session, username, strlen(username)); + fprintf(stderr, "Authentication methods: %s\n", userauthlist); + if (strstr(userauthlist, "password") != NULL) { + auth_pw |= 1; + } + if (strstr(userauthlist, "keyboard-interactive") != NULL) { + auth_pw |= 2; + } + if (strstr(userauthlist, "publickey") != NULL) { + auth_pw |= 4; + } + + /* if we got an 4. argument we set this option if supported */ + if(argc > 4) { + if ((auth_pw & 1) && !strcasecmp(argv[4], "-p")) { + auth_pw = 1; + } + if ((auth_pw & 2) && !strcasecmp(argv[4], "-i")) { + auth_pw = 2; + } + if ((auth_pw & 4) && !strcasecmp(argv[4], "-k")) { + auth_pw = 4; + } + } + + if (auth_pw & 1) { + /* We could authenticate via password */ + if (libssh2_userauth_password(session, username, password)) { + fprintf(stderr, "\tAuthentication by password failed!\n"); + goto shutdown; + } else { + fprintf(stderr, "\tAuthentication by password succeeded.\n"); + } + } else if (auth_pw & 2) { + /* Or via keyboard-interactive */ + if (libssh2_userauth_keyboard_interactive(session, username, + &kbd_callback) ) { + fprintf(stderr, + "\tAuthentication by keyboard-interactive failed!\n"); + goto shutdown; + } else { + fprintf(stderr, + "\tAuthentication by keyboard-interactive succeeded.\n"); + } + } else if (auth_pw & 4) { + /* Or by public key */ + if (libssh2_userauth_publickey_fromfile(session, username, keyfile1, + keyfile2, password)) { + fprintf(stderr, "\tAuthentication by public key failed!\n"); + goto shutdown; + } else { + fprintf(stderr, "\tAuthentication by public key succeeded.\n"); + } + } else { + fprintf(stderr, "No supported authentication methods found!\n"); + goto shutdown; + } + + /* Request a shell */ + if (!(channel = libssh2_channel_open_session(session))) { + fprintf(stderr, "Unable to open a session\n"); + goto shutdown; + } + + /* Some environment variables may be set, + * It's up to the server which ones it'll allow though + */ + libssh2_channel_setenv(channel, "FOO", "bar"); + + /* Request a terminal with 'vanilla' terminal emulation + * See /etc/termcap for more options + */ + if (libssh2_channel_request_pty(channel, "vanilla")) { + fprintf(stderr, "Failed requesting pty\n"); + goto skip_shell; + } + + /* Open a SHELL on that pty */ + if (libssh2_channel_shell(channel)) { + fprintf(stderr, "Unable to request shell on allocated pty\n"); + goto shutdown; + } + + /* At this point the shell can be interacted with using + * libssh2_channel_read() + * libssh2_channel_read_stderr() + * libssh2_channel_write() + * libssh2_channel_write_stderr() + * + * Blocking mode may be (en|dis)abled with: libssh2_channel_set_blocking() + * If the server send EOF, libssh2_channel_eof() will return non-0 + * To send EOF to the server use: libssh2_channel_send_eof() + * A channel can be closed with: libssh2_channel_close() + * A channel can be freed with: libssh2_channel_free() + */ + + skip_shell: + if (channel) { + libssh2_channel_free(channel); + channel = NULL; + } + + /* Other channel types are supported via: + * libssh2_scp_send() + * libssh2_scp_recv() + * libssh2_channel_direct_tcpip() + */ + + shutdown: + + libssh2_session_disconnect(session, + "Normal Shutdown, Thank you for playing"); + libssh2_session_free(session); + +#ifdef WIN32 + closesocket(sock); +#else + close(sock); +#endif + fprintf(stderr, "all done!\n"); + + libssh2_exit(); + + return 0; +} diff --git a/libssh2-sys/libssh2-1.5.0/example/ssh2_agent.c b/libssh2-sys/libssh2-1.5.0/example/ssh2_agent.c new file mode 100644 index 0000000..84f3122 --- /dev/null +++ b/libssh2-sys/libssh2-1.5.0/example/ssh2_agent.c @@ -0,0 +1,248 @@ +/* + * Sample showing how to do SSH2 connect using ssh-agent. + * + * The sample code has default values for host name, user name: + * + * "ssh2_agent host user" + */ + +#include "libssh2_config.h" +#include <libssh2.h> +#include <libssh2_sftp.h> + +#ifdef HAVE_WINDOWS_H +# include <windows.h> +#endif +#ifdef HAVE_WINSOCK2_H +# include <winsock2.h> +#endif +#ifdef HAVE_SYS_SOCKET_H +# include <sys/socket.h> +#endif +#ifdef HAVE_NETINET_IN_H +# include <netinet/in.h> +#endif +# ifdef HAVE_UNISTD_H +#include <unistd.h> +#endif +# ifdef HAVE_ARPA_INET_H +#include <arpa/inet.h> +#endif + +#include <sys/types.h> +#include <fcntl.h> +#include <errno.h> +#include <stdio.h> +#include <ctype.h> +#include <stdlib.h> + +const char *username="username"; + +int main(int argc, char *argv[]) +{ + unsigned long hostaddr; + int sock = -1, i, rc; + struct sockaddr_in sin; + const char *fingerprint; + char *userauthlist; + LIBSSH2_SESSION *session = NULL; + LIBSSH2_CHANNEL *channel; + LIBSSH2_AGENT *agent = NULL; + struct libssh2_agent_publickey *identity, *prev_identity = NULL; + +#ifdef WIN32 + WSADATA wsadata; + int err; + + err = WSAStartup(MAKEWORD(2,0), &wsadata); + if (err != 0) { + fprintf(stderr, "WSAStartup failed with error: %d\n", err); + return 1; + } +#endif + + if (argc > 1) { + hostaddr = inet_addr(argv[1]); + } else { + hostaddr = htonl(0x7F000001); + } + + if(argc > 2) { + username = argv[2]; + } + + rc = libssh2_init (0); + if (rc != 0) { + fprintf (stderr, "libssh2 initialization failed (%d)\n", rc); + return 1; + } + + /* Ultra basic "connect to port 22 on localhost". Your code is + * responsible for creating the socket establishing the connection + */ + sock = socket(AF_INET, SOCK_STREAM, 0); + if (sock == -1) { + fprintf(stderr, "failed to create socket!\n"); + rc = 1; + goto shutdown; + } + + sin.sin_family = AF_INET; + sin.sin_port = htons(22); + sin.sin_addr.s_addr = hostaddr; + if (connect(sock, (struct sockaddr*)(&sin), + sizeof(struct sockaddr_in)) != 0) { + fprintf(stderr, "failed to connect!\n"); + goto shutdown; + } + + /* Create a session instance and start it up. This will trade welcome + * banners, exchange keys, and setup crypto, compression, and MAC layers + */ + session = libssh2_session_init(); + if (libssh2_session_handshake(session, sock)) { + fprintf(stderr, "Failure establishing SSH session\n"); + return 1; + } + + /* At this point we havn't authenticated. The first thing to do is check + * the hostkey's fingerprint against our known hosts Your app may have it + * hard coded, may go to a file, may present it to the user, that's your + * call + */ + fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_SHA1); + fprintf(stderr, "Fingerprint: "); + for(i = 0; i < 20; i++) { + fprintf(stderr, "%02X ", (unsigned char)fingerprint[i]); + } + fprintf(stderr, "\n"); + + /* check what authentication methods are available */ + userauthlist = libssh2_userauth_list(session, username, strlen(username)); + fprintf(stderr, "Authentication methods: %s\n", userauthlist); + if (strstr(userauthlist, "publickey") == NULL) { + fprintf(stderr, "\"publickey\" authentication is not supported\n"); + goto shutdown; + } + + /* Connect to the ssh-agent */ + agent = libssh2_agent_init(session); + if (!agent) { + fprintf(stderr, "Failure initializing ssh-agent support\n"); + rc = 1; + goto shutdown; + } + if (libssh2_agent_connect(agent)) { + fprintf(stderr, "Failure connecting to ssh-agent\n"); + rc = 1; + goto shutdown; + } + if (libssh2_agent_list_identities(agent)) { + fprintf(stderr, "Failure requesting identities to ssh-agent\n"); + rc = 1; + goto shutdown; + } + while (1) { + rc = libssh2_agent_get_identity(agent, &identity, prev_identity); + if (rc == 1) + break; + if (rc < 0) { + fprintf(stderr, + "Failure obtaining identity from ssh-agent support\n"); + rc = 1; + goto shutdown; + } + if (libssh2_agent_userauth(agent, username, identity)) { + fprintf(stderr, "\tAuthentication with username %s and " + "public key %s failed!\n", + username, identity->comment); + } else { + fprintf(stderr, "\tAuthentication with username %s and " + "public key %s succeeded!\n", + username, identity->comment); + break; + } + prev_identity = identity; + } + if (rc) { + fprintf(stderr, "Couldn't continue authentication\n"); + goto shutdown; + } + + /* We're authenticated now. */ + + /* Request a shell */ + if (!(channel = libssh2_channel_open_session(session))) { + fprintf(stderr, "Unable to open a session\n"); + goto shutdown; + } + + /* Some environment variables may be set, + * It's up to the server which ones it'll allow though + */ + libssh2_channel_setenv(channel, "FOO", "bar"); + + /* Request a terminal with 'vanilla' terminal emulation + * See /etc/termcap for more options + */ + if (libssh2_channel_request_pty(channel, "vanilla")) { + fprintf(stderr, "Failed requesting pty\n"); + goto skip_shell; + } + + /* Open a SHELL on that pty */ + if (libssh2_channel_shell(channel)) { + fprintf(stderr, "Unable to request shell on allocated pty\n"); + goto shutdown; + } + + /* At this point the shell can be interacted with using + * libssh2_channel_read() + * libssh2_channel_read_stderr() + * libssh2_channel_write() + * libssh2_channel_write_stderr() + * + * Blocking mode may be (en|dis)abled with: libssh2_channel_set_blocking() + * If the server send EOF, libssh2_channel_eof() will return non-0 + * To send EOF to the server use: libssh2_channel_send_eof() + * A channel can be closed with: libssh2_channel_close() + * A channel can be freed with: libssh2_channel_free() + */ + + skip_shell: + if (channel) { + libssh2_channel_free(channel); + channel = NULL; + } + + /* Other channel types are supported via: + * libssh2_scp_send() + * libssh2_scp_recv() + * libssh2_channel_direct_tcpip() + */ + + shutdown: + + libssh2_agent_disconnect(agent); + libssh2_agent_free(agent); + + if(session) { + libssh2_session_disconnect(session, + "Normal Shutdown, Thank you for playing"); + libssh2_session_free(session); + } + + if (sock != -1) { +#ifdef WIN32 + closesocket(sock); +#else + close(sock); +#endif + } + + fprintf(stderr, "all done!\n"); + + libssh2_exit(); + + return rc; +} diff --git a/libssh2-sys/libssh2-1.5.0/example/ssh2_echo.c b/libssh2-sys/libssh2-1.5.0/example/ssh2_echo.c new file mode 100644 index 0000000..ac155ad --- /dev/null +++ b/libssh2-sys/libssh2-1.5.0/example/ssh2_echo.c @@ -0,0 +1,366 @@ +/* + * Run it like this: + * + * $ ./ssh2_echo 127.0.0.1 user password + * + * The code sends a 'cat' command, and then writes a lot of data to it only to + * check that reading the returned data sums up to the same amount. + * + */ + +#include "libssh2_config.h" +#include <libssh2.h> + +#ifdef HAVE_WINSOCK2_H +# include <winsock2.h> +#endif +#ifdef HAVE_SYS_SOCKET_H +# include <sys/socket.h> +#endif +#ifdef HAVE_NETINET_IN_H +# include <netinet/in.h> +#endif +#ifdef HAVE_SYS_SELECT_H +# include <sys/select.h> +#endif +# ifdef HAVE_UNISTD_H +#include <unistd.h> +#endif +#ifdef HAVE_ARPA_INET_H +# include <arpa/inet.h> +#endif + +#include <sys/time.h> +#include <sys/types.h> +#include <stdlib.h> +#include <fcntl.h> +#include <errno.h> +#include <stdio.h> +#include <ctype.h> + +static int waitsocket(int socket_fd, LIBSSH2_SESSION *session) +{ + struct timeval timeout; + int rc; + fd_set fd; + fd_set *writefd = NULL; + fd_set *readfd = NULL; + int dir; + + timeout.tv_sec = 10; + timeout.tv_usec = 0; + + FD_ZERO(&fd); + + FD_SET(socket_fd, &fd); + + /* now make sure we wait in the correct direction */ + dir = libssh2_session_block_directions(session); + + if(dir & LIBSSH2_SESSION_BLOCK_INBOUND) + readfd = &fd; + + if(dir & LIBSSH2_SESSION_BLOCK_OUTBOUND) + writefd = &fd; + + rc = select(socket_fd + 1, readfd, writefd, NULL, &timeout); + + return rc; +} + +#define BUFSIZE 32000 + +int main(int argc, char *argv[]) +{ + const char *hostname = "127.0.0.1"; + const char *commandline = "cat"; + const char *username = "user"; + const char *password = "password"; + unsigned long hostaddr; + int sock; + struct sockaddr_in sin; + const char *fingerprint; + LIBSSH2_SESSION *session; + LIBSSH2_CHANNEL *channel; + int rc; + int exitcode = 0; + char *exitsignal=(char *)"none"; + size_t len; + LIBSSH2_KNOWNHOSTS *nh; + int type; + +#ifdef WIN32 + WSADATA wsadata; + int err; + + err = WSAStartup(MAKEWORD(2,0), &wsadata); + if (err != 0) { + fprintf(stderr, "WSAStartup failed with error: %d\n", err); + return 1; + } +#endif + + if (argc > 1) + /* must be ip address only */ + hostname = argv[1]; + + if (argc > 2) { + username = argv[2]; + } + if (argc > 3) { + password = argv[3]; + } + + rc = libssh2_init (0); + if (rc != 0) { + fprintf (stderr, "libssh2 initialization failed (%d)\n", rc); + return 1; + } + + hostaddr = inet_addr(hostname); + + /* Ultra basic "connect to port 22 on localhost" + * Your code is responsible for creating the socket establishing the + * connection + */ + sock = socket(AF_INET, SOCK_STREAM, 0); + + sin.sin_family = AF_INET; + sin.sin_port = htons(22); + sin.sin_addr.s_addr = hostaddr; + if (connect(sock, (struct sockaddr*)(&sin), + sizeof(struct sockaddr_in)) != 0) { + fprintf(stderr, "failed to connect!\n"); + return -1; + } + + /* Create a session instance */ + session = libssh2_session_init(); + if (!session) + return -1; + + /* tell libssh2 we want it all done non-blocking */ + libssh2_session_set_blocking(session, 0); + + /* ... start it up. This will trade welcome banners, exchange keys, + * and setup crypto, compression, and MAC layers + */ + while ((rc = libssh2_session_handshake(session, sock)) == + LIBSSH2_ERROR_EAGAIN); + if (rc) { + fprintf(stderr, "Failure establishing SSH session: %d\n", rc); + return -1; + } + + nh = libssh2_knownhost_init(session); + if(!nh) { + /* eeek, do cleanup here */ + return 2; + } + + /* read all hosts from here */ + libssh2_knownhost_readfile(nh, "known_hosts", + LIBSSH2_KNOWNHOST_FILE_OPENSSH); + + /* store all known hosts to here */ + libssh2_knownhost_writefile(nh, "dumpfile", + LIBSSH2_KNOWNHOST_FILE_OPENSSH); + + fingerprint = libssh2_session_hostkey(session, &len, &type); + if(fingerprint) { + struct libssh2_knownhost *host; + int check = libssh2_knownhost_checkp(nh, hostname, 22, + fingerprint, len, + LIBSSH2_KNOWNHOST_TYPE_PLAIN| + LIBSSH2_KNOWNHOST_KEYENC_RAW, + &host); + + fprintf(stderr, "Host check: %d, key: %s\n", check, + (check <= LIBSSH2_KNOWNHOST_CHECK_MISMATCH)? + host->key:"<none>"); + + /***** + * At this point, we could verify that 'check' tells us the key is + * fine or bail out. + *****/ + } + else { + /* eeek, do cleanup here */ + return 3; + } + libssh2_knownhost_free(nh); + + if ( strlen(password) != 0 ) { + /* We could authenticate via password */ + while ((rc = libssh2_userauth_password(session, username, password)) == + LIBSSH2_ERROR_EAGAIN); + if (rc) { + fprintf(stderr, "Authentication by password failed.\n"); + exit(1); + } + } + + libssh2_trace(session, LIBSSH2_TRACE_SOCKET); + + /* Exec non-blocking on the remove host */ + while( (channel = libssh2_channel_open_session(session)) == NULL && + libssh2_session_last_error(session,NULL,NULL,0) == + LIBSSH2_ERROR_EAGAIN ) { + waitsocket(sock, session); + } + if( channel == NULL ) { + fprintf(stderr,"Error\n"); + exit( 1 ); + } + while( (rc = libssh2_channel_exec(channel, commandline)) == + LIBSSH2_ERROR_EAGAIN ) + waitsocket(sock, session); + + if( rc != 0 ) { + fprintf(stderr, "exec error\n"); + exit( 1 ); + } + else { + LIBSSH2_POLLFD *fds = NULL; + int running = 1; + int bufsize = BUFSIZE; + char buffer[BUFSIZE]; + int totsize = 1500000; + int totwritten = 0; + int totread = 0; + int partials = 0; + int rereads = 0; + int rewrites = 0; + int i; + + for (i = 0; i < BUFSIZE; i++) + buffer[i] = 'A'; + + if ((fds = malloc (sizeof (LIBSSH2_POLLFD))) == NULL) { + fprintf(stderr, "malloc failed\n"); + exit(1); + } + + fds[0].type = LIBSSH2_POLLFD_CHANNEL; + fds[0].fd.channel = channel; + fds[0].events = LIBSSH2_POLLFD_POLLIN | LIBSSH2_POLLFD_POLLOUT; + + do { + int rc = (libssh2_poll(fds, 1, 10)); + int act = 0; + + if (rc < 1) + continue; + + if (fds[0].revents & LIBSSH2_POLLFD_POLLIN) { + int n = libssh2_channel_read(channel, buffer, sizeof(buffer)); + act++; + + if (n == LIBSSH2_ERROR_EAGAIN) { + rereads++; + fprintf(stderr, "will read again\n"); + } + else if (n < 0) { + fprintf(stderr, "read failed\n"); + exit(1); + } + else { + totread += n; + fprintf(stderr, "read %d bytes (%d in total)\n", + n, totread); + } + } + + if (fds[0].revents & LIBSSH2_POLLFD_POLLOUT) { + act++; + + if (totwritten < totsize) { + /* we have not written all data yet */ + int left = totsize - totwritten; + int size = (left < bufsize) ? left : bufsize; + int n = libssh2_channel_write_ex(channel, 0, buffer, size); + + if (n == LIBSSH2_ERROR_EAGAIN) { + rewrites++; + fprintf(stderr, "will write again\n"); + } + else if (n < 0) { + fprintf(stderr, "write failed\n"); + exit(1); + } + else { + totwritten += n; + fprintf(stderr, "wrote %d bytes (%d in total)", + n, totwritten); + if (left >= bufsize && n != bufsize) { + partials++; + fprintf(stderr, " PARTIAL"); + } + fprintf(stderr, "\n"); + } + } else { + /* all data written, send EOF */ + rc = libssh2_channel_send_eof(channel); + + if (rc == LIBSSH2_ERROR_EAGAIN) { + fprintf(stderr, "will send eof again\n"); + } + else if (rc < 0) { + fprintf(stderr, "send eof failed\n"); + exit(1); + } + else { + fprintf(stderr, "sent eof\n"); + /* we're done writing, stop listening for OUT events */ + fds[0].events &= ~LIBSSH2_POLLFD_POLLOUT; + } + } + } + + if (fds[0].revents & LIBSSH2_POLLFD_CHANNEL_CLOSED) { + if (!act) /* don't leave loop until we have read all data */ + running = 0; + } + } while(running); + + exitcode = 127; + while( (rc = libssh2_channel_close(channel)) == LIBSSH2_ERROR_EAGAIN ) + waitsocket(sock, session); + + if( rc == 0 ) { + exitcode = libssh2_channel_get_exit_status( channel ); + libssh2_channel_get_exit_signal(channel, &exitsignal, + NULL, NULL, NULL, NULL, NULL); + } + + if (exitsignal) + fprintf(stderr, "\nGot signal: %s\n", exitsignal); + + libssh2_channel_free(channel); + channel = NULL; + + fprintf(stderr, "\nrereads: %d rewrites: %d totwritten %d\n", + rereads, rewrites, totwritten); + + if (totwritten != totread) { + fprintf(stderr, "\n*** FAIL bytes written: %d bytes " + "read: %d ***\n", totwritten, totread); + exit(1); + } + } + + libssh2_session_disconnect(session, + "Normal Shutdown, Thank you for playing"); + libssh2_session_free(session); + +#ifdef WIN32 + closesocket(sock); +#else + close(sock); +#endif + fprintf(stderr, "all done\n"); + + libssh2_exit(); + + return exitcode; +} diff --git a/libssh2-sys/libssh2-1.5.0/example/ssh2_exec.c b/libssh2-sys/libssh2-1.5.0/example/ssh2_exec.c new file mode 100644 index 0000000..36a2c8c --- /dev/null +++ b/libssh2-sys/libssh2-1.5.0/example/ssh2_exec.c @@ -0,0 +1,325 @@ +/* + * Sample showing how to use libssh2 to execute a command remotely. + * + * The sample code has fixed values for host name, user name, password + * and command to run. + * + * Run it like this: + * + * $ ./ssh2_exec 127.0.0.1 user password "uptime" + * + */ + +#include "libssh2_config.h" +#include <libssh2.h> + +#ifdef HAVE_WINSOCK2_H +# include <winsock2.h> +#endif +#ifdef HAVE_SYS_SOCKET_H +# include <sys/socket.h> +#endif +#ifdef HAVE_NETINET_IN_H +# include <netinet/in.h> +#endif +#ifdef HAVE_SYS_SELECT_H +# include <sys/select.h> +#endif +# ifdef HAVE_UNISTD_H +#include <unistd.h> +#endif +#ifdef HAVE_ARPA_INET_H +# include <arpa/inet.h> +#endif + +#include <sys/time.h> +#include <sys/types.h> +#include <stdlib.h> +#include <fcntl.h> +#include <errno.h> +#include <stdio.h> +#include <ctype.h> + +static int waitsocket(int socket_fd, LIBSSH2_SESSION *session) +{ + struct timeval timeout; + int rc; + fd_set fd; + fd_set *writefd = NULL; + fd_set *readfd = NULL; + int dir; + + timeout.tv_sec = 10; + timeout.tv_usec = 0; + + FD_ZERO(&fd); + + FD_SET(socket_fd, &fd); + + /* now make sure we wait in the correct direction */ + dir = libssh2_session_block_directions(session); + + if(dir & LIBSSH2_SESSION_BLOCK_INBOUND) + readfd = &fd; + + if(dir & LIBSSH2_SESSION_BLOCK_OUTBOUND) + writefd = &fd; + + rc = select(socket_fd + 1, readfd, writefd, NULL, &timeout); + + return rc; +} + +int main(int argc, char *argv[]) +{ + const char *hostname = "127.0.0.1"; + const char *commandline = "uptime"; + const char *username = "user"; + const char *password = "password"; + unsigned long hostaddr; + int sock; + struct sockaddr_in sin; + const char *fingerprint; + LIBSSH2_SESSION *session; + LIBSSH2_CHANNEL *channel; + int rc; + int exitcode; + char *exitsignal=(char *)"none"; + int bytecount = 0; + size_t len; + LIBSSH2_KNOWNHOSTS *nh; + int type; + +#ifdef WIN32 + WSADATA wsadata; + int err; + + err = WSAStartup(MAKEWORD(2,0), &wsadata); + if (err != 0) { + fprintf(stderr, "WSAStartup failed with error: %d\n", err); + return 1; + } +#endif + + if (argc > 1) + /* must be ip address only */ + hostname = argv[1]; + + if (argc > 2) { + username = argv[2]; + } + if (argc > 3) { + password = argv[3]; + } + if (argc > 4) { + commandline = argv[4]; + } + + rc = libssh2_init (0); + if (rc != 0) { + fprintf (stderr, "libssh2 initialization failed (%d)\n", rc); + return 1; + } + + hostaddr = inet_addr(hostname); + + /* Ultra basic "connect to port 22 on localhost" + * Your code is responsible for creating the socket establishing the + * connection + */ + sock = socket(AF_INET, SOCK_STREAM, 0); + + sin.sin_family = AF_INET; + sin.sin_port = htons(22); + sin.sin_addr.s_addr = hostaddr; + if (connect(sock, (struct sockaddr*)(&sin), + sizeof(struct sockaddr_in)) != 0) { + fprintf(stderr, "failed to connect!\n"); + return -1; + } + + /* Create a session instance */ + session = libssh2_session_init(); + if (!session) + return -1; + + /* tell libssh2 we want it all done non-blocking */ + libssh2_session_set_blocking(session, 0); + + /* ... start it up. This will trade welcome banners, exchange keys, + * and setup crypto, compression, and MAC layers + */ + while ((rc = libssh2_session_handshake(session, sock)) == + LIBSSH2_ERROR_EAGAIN); + if (rc) { + fprintf(stderr, "Failure establishing SSH session: %d\n", rc); + return -1; + } + + nh = libssh2_knownhost_init(session); + if(!nh) { + /* eeek, do cleanup here */ + return 2; + } + + /* read all hosts from here */ + libssh2_knownhost_readfile(nh, "known_hosts", + LIBSSH2_KNOWNHOST_FILE_OPENSSH); + + /* store all known hosts to here */ + libssh2_knownhost_writefile(nh, "dumpfile", + LIBSSH2_KNOWNHOST_FILE_OPENSSH); + + fingerprint = libssh2_session_hostkey(session, &len, &type); + if(fingerprint) { + struct libssh2_knownhost *host; +#if LIBSSH2_VERSION_NUM >= 0x010206 + /* introduced in 1.2.6 */ + int check = libssh2_knownhost_checkp(nh, hostname, 22, + fingerprint, len, + LIBSSH2_KNOWNHOST_TYPE_PLAIN| + LIBSSH2_KNOWNHOST_KEYENC_RAW, + &host); +#else + /* 1.2.5 or older */ + int check = libssh2_knownhost_check(nh, hostname, + fingerprint, len, + LIBSSH2_KNOWNHOST_TYPE_PLAIN| + LIBSSH2_KNOWNHOST_KEYENC_RAW, + &host); +#endif + fprintf(stderr, "Host check: %d, key: %s\n", check, + (check <= LIBSSH2_KNOWNHOST_CHECK_MISMATCH)? + host->key:"<none>"); + + /***** + * At this point, we could verify that 'check' tells us the key is + * fine or bail out. + *****/ + } + else { + /* eeek, do cleanup here */ + return 3; + } + libssh2_knownhost_free(nh); + + if ( strlen(password) != 0 ) { + /* We could authenticate via password */ + while ((rc = libssh2_userauth_password(session, username, password)) == + LIBSSH2_ERROR_EAGAIN); + if (rc) { + fprintf(stderr, "Authentication by password failed.\n"); + goto shutdown; + } + } + else { + /* Or by public key */ + while ((rc = libssh2_userauth_publickey_fromfile(session, username, + "/home/user/" + ".ssh/id_rsa.pub", + "/home/user/" + ".ssh/id_rsa", + password)) == + LIBSSH2_ERROR_EAGAIN); + if (rc) { + fprintf(stderr, "\tAuthentication by public key failed\n"); + goto shutdown; + } + } + +#if 0 + libssh2_trace(session, ~0 ); +#endif + + /* Exec non-blocking on the remove host */ + while( (channel = libssh2_channel_open_session(session)) == NULL && + libssh2_session_last_error(session,NULL,NULL,0) == + LIBSSH2_ERROR_EAGAIN ) + { + waitsocket(sock, session); + } + if( channel == NULL ) + { + fprintf(stderr,"Error\n"); + exit( 1 ); + } + while( (rc = libssh2_channel_exec(channel, commandline)) == + LIBSSH2_ERROR_EAGAIN ) + { + waitsocket(sock, session); + } + if( rc != 0 ) + { + fprintf(stderr,"Error\n"); + exit( 1 ); + } + for( ;; ) + { + /* loop until we block */ + int rc; + do + { + char buffer[0x4000]; + rc = libssh2_channel_read( channel, buffer, sizeof(buffer) ); + if( rc > 0 ) + { + int i; + bytecount += rc; + fprintf(stderr, "We read:\n"); + for( i=0; i < rc; ++i ) + fputc( buffer[i], stderr); + fprintf(stderr, "\n"); + } + else { + if( rc != LIBSSH2_ERROR_EAGAIN ) + /* no need to output this for the EAGAIN case */ + fprintf(stderr, "libssh2_channel_read returned %d\n", rc); + } + } + while( rc > 0 ); + + /* this is due to blocking that would occur otherwise so we loop on + this condition */ + if( rc == LIBSSH2_ERROR_EAGAIN ) + { + waitsocket(sock, session); + } + else + break; + } + exitcode = 127; + while( (rc = libssh2_channel_close(channel)) == LIBSSH2_ERROR_EAGAIN ) + waitsocket(sock, session); + + if( rc == 0 ) + { + exitcode = libssh2_channel_get_exit_status( channel ); + libssh2_channel_get_exit_signal(channel, &exitsignal, + NULL, NULL, NULL, NULL, NULL); + } + + if (exitsignal) + fprintf(stderr, "\nGot signal: %s\n", exitsignal); + else + fprintf(stderr, "\nEXIT: %d bytecount: %d\n", exitcode, bytecount); + + libssh2_channel_free(channel); + channel = NULL; + +shutdown: + + libssh2_session_disconnect(session, + "Normal Shutdown, Thank you for playing"); + libssh2_session_free(session); + +#ifdef WIN32 + closesocket(sock); +#else + close(sock); +#endif + fprintf(stderr, "all done\n"); + + libssh2_exit(); + + return 0; +} diff --git a/libssh2-sys/libssh2-1.5.0/example/subsystem_netconf.c b/libssh2-sys/libssh2-1.5.0/example/subsystem_netconf.c new file mode 100644 index 0000000..6a45e67 --- /dev/null +++ b/libssh2-sys/libssh2-1.5.0/example/subsystem_netconf.c @@ -0,0 +1,297 @@ +#include "libssh2_config.h" +#include <libssh2.h> + +#ifdef WIN32 +#include <windows.h> +#include <winsock2.h> +#include <ws2tcpip.h> +#else +#include <sys/socket.h> +#include <netinet/in.h> +#include <arpa/inet.h> +#include <sys/time.h> +#endif + +#include <fcntl.h> +#include <errno.h> +#include <stdio.h> +#include <string.h> +#include <stdlib.h> +#include <unistd.h> +#include <sys/types.h> + +#ifdef HAVE_SYS_SELECT_H +#include <sys/select.h> +#endif + +#ifndef INADDR_NONE +#define INADDR_NONE (in_addr_t)~0 +#endif + +const char *keyfile1 = "/home/username/.ssh/id_rsa.pub"; +const char *keyfile2 = "/home/username/.ssh/id_rsa"; +const char *username = "username"; +const char *password = ""; + +const char *server_ip = "127.0.0.1"; + +enum { + AUTH_NONE = 0, + AUTH_PASSWORD, + AUTH_PUBLICKEY +}; + +static int netconf_write(LIBSSH2_CHANNEL *channel, const char *buf, size_t len) +{ + int i; + ssize_t wr = 0; + + do { + i = libssh2_channel_write(channel, buf, len); + if (i < 0) { + fprintf(stderr, "libssh2_channel_write: %d\n", i); + return -1; + } + wr += i; + } while (i > 0 && wr < (ssize_t)len); + + return 0; +} + +static int netconf_read_until(LIBSSH2_CHANNEL *channel, const char *endtag, + char *buf, size_t buflen) +{ + ssize_t len; + size_t rd = 0; + char *endreply = NULL, *specialsequence = NULL; + + memset(buf, 0, buflen); + + do { + len = libssh2_channel_read(channel, buf + rd, buflen - rd); + if (LIBSSH2_ERROR_EAGAIN == len) + continue; + else if (len < 0) { + fprintf(stderr, "libssh2_channel_read: %d\n", (int)len); + return -1; + } + rd += len; + + /* read more data until we see a rpc-reply closing tag followed by + * the special sequence ]]>]]> */ + + /* really, this MUST be replaced with proper XML parsing! */ + + endreply = strstr(buf, endtag); + if (endreply) + specialsequence = strstr(endreply, "]]>]]>"); + + } while (!specialsequence && rd < buflen); + + if (!specialsequence) { + fprintf(stderr, "%s: ]]>]]> not found! read buffer too small?\n", __func__); + return -1; + } + + /* discard the special sequence so that only XML is returned */ + rd = specialsequence - buf; + buf[rd] = 0; + + return rd; +} + +int main(int argc, char *argv[]) +{ + int rc, i, auth = AUTH_NONE; + struct sockaddr_in sin; + const char *fingerprint; + char *userauthlist; + LIBSSH2_SESSION *session; + LIBSSH2_CHANNEL *channel = NULL; + char buf[1048576]; /* avoid any buffer reallocation for simplicity */ + ssize_t len; + +#ifdef WIN32 + SOCKET sock = INVALID_SOCKET; + WSADATA wsadata; + int err; + + err = WSAStartup(MAKEWORD(2,0), &wsadata); + if (err != 0) { + fprintf(stderr, "WSAStartup failed with error: %d\n", err); + return 1; + } +#else + int sock = -1; +#endif + + if (argc > 1) + server_ip = argv[1]; + if (argc > 2) + username = argv[2]; + if (argc > 3) + password = argv[3]; + + rc = libssh2_init (0); + if (rc != 0) { + fprintf (stderr, "libssh2 initialization failed (%d)\n", rc); + return 1; + } + + /* Connect to SSH server */ + sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); +#ifdef WIN32 + if (sock == INVALID_SOCKET) { + fprintf(stderr, "failed to open socket!\n"); + return -1; + } +#else + if (sock == -1) { + perror("socket"); + return -1; + } +#endif + + sin.sin_family = AF_INET; + if (INADDR_NONE == (sin.sin_addr.s_addr = inet_addr(server_ip))) { + fprintf(stderr, "inet_addr: Invalid IP address \"%s\"\n", server_ip); + return -1; + } + sin.sin_port = htons(830); + if (connect(sock, (struct sockaddr*)(&sin), + sizeof(struct sockaddr_in)) != 0) { + fprintf(stderr, "Failed to connect to %s!\n", inet_ntoa(sin.sin_addr)); + return -1; + } + + /* Create a session instance */ + session = libssh2_session_init(); + if(!session) { + fprintf(stderr, "Could not initialize SSH session!\n"); + return -1; + } + + /* ... start it up. This will trade welcome banners, exchange keys, + * and setup crypto, compression, and MAC layers + */ + rc = libssh2_session_handshake(session, sock); + if(rc) { + fprintf(stderr, "Error when starting up SSH session: %d\n", rc); + return -1; + } + + /* At this point we havn't yet authenticated. The first thing to do + * is check the hostkey's fingerprint against our known hosts Your app + * may have it hard coded, may go to a file, may present it to the + * user, that's your call + */ + fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_SHA1); + fprintf(stderr, "Fingerprint: "); + for(i = 0; i < 20; i++) + fprintf(stderr, "%02X ", (unsigned char)fingerprint[i]); + fprintf(stderr, "\n"); + + /* check what authentication methods are available */ + userauthlist = libssh2_userauth_list(session, username, strlen(username)); + fprintf(stderr, "Authentication methods: %s\n", userauthlist); + if (strstr(userauthlist, "password")) + auth |= AUTH_PASSWORD; + if (strstr(userauthlist, "publickey")) + auth |= AUTH_PUBLICKEY; + + /* check for options */ + if(argc > 4) { + if ((auth & AUTH_PASSWORD) && !strcasecmp(argv[4], "-p")) + auth = AUTH_PASSWORD; + if ((auth & AUTH_PUBLICKEY) && !strcasecmp(argv[4], "-k")) + auth = AUTH_PUBLICKEY; + } + + if (auth & AUTH_PASSWORD) { + if (libssh2_userauth_password(session, username, password)) { + fprintf(stderr, "Authentication by password failed.\n"); + goto shutdown; + } + } else if (auth & AUTH_PUBLICKEY) { + if (libssh2_userauth_publickey_fromfile(session, username, keyfile1, + keyfile2, password)) { + fprintf(stderr, "Authentication by public key failed!\n"); + goto shutdown; + } + fprintf(stderr, "Authentication by public key succeeded.\n"); + } else { + fprintf(stderr, "No supported authentication methods found!\n"); + goto shutdown; + } + + /* open a channel */ + channel = libssh2_channel_open_session(session); + if (!channel) { + fprintf(stderr, "Could not open the channel!\n" + "(Note that this can be a problem at the server!" + " Please review the server logs.)\n"); + goto shutdown; + } + + /* execute the subsystem on our channel */ + if (libssh2_channel_subsystem(channel, "netconf")) { + fprintf(stderr, "Could not execute the \"netconf\" subsystem!\n" + "(Note that this can be a problem at the server!" + " Please review the server logs.)\n"); + goto shutdown; + } + + /* NETCONF: http://tools.ietf.org/html/draft-ietf-netconf-ssh-06 */ + + fprintf(stderr, "Sending NETCONF client <hello>\n"); + snprintf(buf, sizeof(buf), + "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<hello>" + "<capabilities>" + "<capability>urn:ietf:params:xml:ns:netconf:base:1.0</capability>" + "</capabilities>" + "</hello>\n" + "]]>]]>\n%n", (int *)&len); + if (-1 == netconf_write(channel, buf, len)) + goto shutdown; + + fprintf(stderr, "Reading NETCONF server <hello>\n"); + len = netconf_read_until(channel, "</hello>", buf, sizeof(buf)); + if (-1 == len) + goto shutdown; + + fprintf(stderr, "Got %d bytes:\n----------------------\n%s", (int)len, buf); + + fprintf(stderr, "Sending NETCONF <rpc>\n"); + snprintf(buf, sizeof(buf), + "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<rpc xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">" + "<get-interface-information><terse/></get-interface-information>" + "</rpc>\n" + "]]>]]>\n%n", (int *)&len); + if (-1 == netconf_write(channel, buf, len)) + goto shutdown; + + fprintf(stderr, "Reading NETCONF <rpc-reply>\n"); + len = netconf_read_until(channel, "</rpc-reply>", buf, sizeof(buf)); + if (-1 == len) + goto shutdown; + + fprintf(stderr, "Got %d bytes:\n----------------------\n%s", (int)len, buf); + +shutdown: + if (channel) + libssh2_channel_free(channel); + libssh2_session_disconnect(session, "Client disconnecting normally"); + libssh2_session_free(session); + +#ifdef WIN32 + closesocket(sock); +#else + close(sock); +#endif + + libssh2_exit(); + + return 0; +} diff --git a/libssh2-sys/libssh2-1.5.0/example/tcpip-forward.c b/libssh2-sys/libssh2-1.5.0/example/tcpip-forward.c new file mode 100644 index 0000000..4d20850 --- /dev/null +++ b/libssh2-sys/libssh2-1.5.0/example/tcpip-forward.c @@ -0,0 +1,320 @@ +#include "libssh2_config.h" +#include <libssh2.h> + +#ifdef WIN32 +#include <windows.h> +#include <winsock2.h> +#include <ws2tcpip.h> +#else +#include <sys/socket.h> +#include <netinet/in.h> +#include <arpa/inet.h> +#include <sys/time.h> +#endif + +#include <fcntl.h> +#include <errno.h> +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> +#include <sys/types.h> + +#ifdef HAVE_SYS_SELECT_H +#include <sys/select.h> +#endif + +#ifndef INADDR_NONE +#define INADDR_NONE (in_addr_t)-1 +#endif + +const char *keyfile1 = "/home/username/.ssh/id_rsa.pub"; +const char *keyfile2 = "/home/username/.ssh/id_rsa"; +const char *username = "username"; +const char *password = ""; + +const char *server_ip = "127.0.0.1"; + +const char *remote_listenhost = "localhost"; /* resolved by the server */ +int remote_wantport = 2222; +int remote_listenport; + +const char *local_destip = "127.0.0.1"; +int local_destport = 22; + +enum { + AUTH_NONE = 0, + AUTH_PASSWORD, + AUTH_PUBLICKEY +}; + +int main(int argc, char *argv[]) +{ + int rc, i, auth = AUTH_NONE; + struct sockaddr_in sin; + socklen_t sinlen = sizeof(sin); + const char *fingerprint; + char *userauthlist; + LIBSSH2_SESSION *session; + LIBSSH2_LISTENER *listener = NULL; + LIBSSH2_CHANNEL *channel = NULL; + fd_set fds; + struct timeval tv; + ssize_t len, wr; + char buf[16384]; + +#ifdef WIN32 + SOCKET sock = INVALID_SOCKET, forwardsock = INVALID_SOCKET; + WSADATA wsadata; + int err; + + err = WSAStartup(MAKEWORD(2,0), &wsadata); + if (err != 0) { + fprintf(stderr, "WSAStartup failed with error: %d\n", err); + return 1; + } +#else + int sock = -1, forwardsock = -1; +#endif + + if (argc > 1) + server_ip = argv[1]; + if (argc > 2) + username = argv[2]; + if (argc > 3) + password = argv[3]; + if (argc > 4) + remote_listenhost = argv[4]; + if (argc > 5) + remote_wantport = atoi(argv[5]); + if (argc > 6) + local_destip = argv[6]; + if (argc > 7) + local_destport = atoi(argv[7]); + + rc = libssh2_init (0); + if (rc != 0) { + fprintf (stderr, "libssh2 initialization failed (%d)\n", rc); + return 1; + } + + /* Connect to SSH server */ + sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); +#ifdef WIN32 + if (sock == INVALID_SOCKET) { + fprintf(stderr, "failed to open socket!\n"); + return -1; + } +#else + if (sock == -1) { + perror("socket"); + return -1; + } +#endif + + sin.sin_family = AF_INET; + if (INADDR_NONE == (sin.sin_addr.s_addr = inet_addr(server_ip))) { + perror("inet_addr"); + return -1; + } + sin.sin_port = htons(22); + if (connect(sock, (struct sockaddr*)(&sin), + sizeof(struct sockaddr_in)) != 0) { + fprintf(stderr, "failed to connect!\n"); + return -1; + } + + /* Create a session instance */ + session = libssh2_session_init(); + if(!session) { + fprintf(stderr, "Could not initialize SSH session!\n"); + return -1; + } + + /* ... start it up. This will trade welcome banners, exchange keys, + * and setup crypto, compression, and MAC layers + */ + rc = libssh2_session_handshake(session, sock); + if(rc) { + fprintf(stderr, "Error when starting up SSH session: %d\n", rc); + return -1; + } + + /* At this point we havn't yet authenticated. The first thing to do + * is check the hostkey's fingerprint against our known hosts Your app + * may have it hard coded, may go to a file, may present it to the + * user, that's your call + */ + fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_SHA1); + fprintf(stderr, "Fingerprint: "); + for(i = 0; i < 20; i++) + fprintf(stderr, "%02X ", (unsigned char)fingerprint[i]); + fprintf(stderr, "\n"); + + /* check what authentication methods are available */ + userauthlist = libssh2_userauth_list(session, username, strlen(username)); + fprintf(stderr, "Authentication methods: %s\n", userauthlist); + if (strstr(userauthlist, "password")) + auth |= AUTH_PASSWORD; + if (strstr(userauthlist, "publickey")) + auth |= AUTH_PUBLICKEY; + + /* check for options */ + if(argc > 8) { + if ((auth & AUTH_PASSWORD) && !strcasecmp(argv[8], "-p")) + auth = AUTH_PASSWORD; + if ((auth & AUTH_PUBLICKEY) && !strcasecmp(argv[8], "-k")) + auth = AUTH_PUBLICKEY; + } + + if (auth & AUTH_PASSWORD) { + if (libssh2_userauth_password(session, username, password)) { + fprintf(stderr, "Authentication by password failed.\n"); + goto shutdown; + } + } else if (auth & AUTH_PUBLICKEY) { + if (libssh2_userauth_publickey_fromfile(session, username, keyfile1, + keyfile2, password)) { + fprintf(stderr, "\tAuthentication by public key failed!\n"); + goto shutdown; + } + fprintf(stderr, "\tAuthentication by public key succeeded.\n"); + } else { + fprintf(stderr, "No supported authentication methods found!\n"); + goto shutdown; + } + + fprintf(stderr, "Asking server to listen on remote %s:%d\n", + remote_listenhost, remote_wantport); + + listener = libssh2_channel_forward_listen_ex(session, remote_listenhost, + remote_wantport, &remote_listenport, 1); + if (!listener) { + fprintf(stderr, "Could not start the tcpip-forward listener!\n" + "(Note that this can be a problem at the server!" + " Please review the server logs.)\n"); + goto shutdown; + } + + fprintf(stderr, "Server is listening on %s:%d\n", remote_listenhost, + remote_listenport); + + fprintf(stderr, "Waiting for remote connection\n"); + channel = libssh2_channel_forward_accept(listener); + if (!channel) { + fprintf(stderr, "Could not accept connection!\n" + "(Note that this can be a problem at the server!" + " Please review the server logs.)\n"); + goto shutdown; + } + + fprintf(stderr, + "Accepted remote connection. Connecting to local server %s:%d\n", + local_destip, local_destport); + forwardsock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); +#ifdef WIN32 + if (forwardsock == INVALID_SOCKET) { + fprintf(stderr, "failed to open forward socket!\n"); + goto shutdown; + } +#else + if (forwardsock == -1) { + perror("socket"); + goto shutdown; + } +#endif + + sin.sin_family = AF_INET; + sin.sin_port = htons(local_destport); + if (INADDR_NONE == (sin.sin_addr.s_addr = inet_addr(local_destip))) { + perror("inet_addr"); + goto shutdown; + } + if (-1 == connect(forwardsock, (struct sockaddr *)&sin, sinlen)) { + perror("connect"); + goto shutdown; + } + + fprintf(stderr, "Forwarding connection from remote %s:%d to local %s:%d\n", + remote_listenhost, remote_listenport, local_destip, local_destport); + + /* Must use non-blocking IO hereafter due to the current libssh2 API */ + libssh2_session_set_blocking(session, 0); + + while (1) { + FD_ZERO(&fds); + FD_SET(forwardsock, &fds); + tv.tv_sec = 0; + tv.tv_usec = 100000; + rc = select(forwardsock + 1, &fds, NULL, NULL, &tv); + if (-1 == rc) { + perror("select"); + goto shutdown; + } + if (rc && FD_ISSET(forwardsock, &fds)) { + len = recv(forwardsock, buf, sizeof(buf), 0); + if (len < 0) { + perror("read"); + goto shutdown; + } else if (0 == len) { + fprintf(stderr, "The local server at %s:%d disconnected!\n", + local_destip, local_destport); + goto shutdown; + } + wr = 0; + do { + i = libssh2_channel_write(channel, buf, len); + if (i < 0) { + fprintf(stderr, "libssh2_channel_write: %d\n", i); + goto shutdown; + } + wr += i; + } while(i > 0 && wr < len); + } + while (1) { + len = libssh2_channel_read(channel, buf, sizeof(buf)); + if (LIBSSH2_ERROR_EAGAIN == len) + break; + else if (len < 0) { + fprintf(stderr, "libssh2_channel_read: %d", (int)len); + goto shutdown; + } + wr = 0; + while (wr < len) { + i = send(forwardsock, buf + wr, len - wr, 0); + if (i <= 0) { + perror("write"); + goto shutdown; + } + wr += i; + } + if (libssh2_channel_eof(channel)) { + fprintf(stderr, "The remote client at %s:%d disconnected!\n", + remote_listenhost, remote_listenport); + goto shutdown; + } + } + } + +shutdown: +#ifdef WIN32 + closesocket(forwardsock); +#else + close(forwardsock); +#endif + if (channel) + libssh2_channel_free(channel); + if (listener) + libssh2_channel_forward_cancel(listener); + libssh2_session_disconnect(session, "Client disconnecting normally"); + libssh2_session_free(session); + +#ifdef WIN32 + closesocket(sock); +#else + close(sock); +#endif + + libssh2_exit(); + + return 0; +} diff --git a/libssh2-sys/libssh2-1.5.0/example/x11.c b/libssh2-sys/libssh2-1.5.0/example/x11.c new file mode 100644 index 0000000..dd01b3b --- /dev/null +++ b/libssh2-sys/libssh2-1.5.0/example/x11.c @@ -0,0 +1,465 @@ +/* + * + * Sample showing how to makes SSH2 with X11 Forwarding works. + * + * Usage : + * "ssh2 host user password [DEBUG]" + */ + +#include <string.h> +#include <sys/ioctl.h> +#include <netinet/in.h> +#include <sys/socket.h> +#include <sys/select.h> +#include <arpa/inet.h> +#include <unistd.h> +#include <sys/types.h> +#include <sys/un.h> +#include <fcntl.h> +#include <errno.h> +#include <stdio.h> +#include <ctype.h> +#include <stdlib.h> +#include <termios.h> + +#include <libssh2.h> + +#define _PATH_UNIX_X "/tmp/.X11-unix/X%d" + +/* + * Chained list that contains channels and associated X11 socket for each X11 + * connections + */ +struct chan_X11_list { + LIBSSH2_CHANNEL *chan; + int sock; + struct chan_X11_list *next; +}; + +struct chan_X11_list * gp_x11_chan = NULL; +struct termios _saved_tio; + +/* + * Utility function to remove a Node of the chained list + */ +static void remove_node(struct chan_X11_list *elem) +{ + struct chan_X11_list *current_node = NULL; + + current_node = gp_x11_chan; + + if (gp_x11_chan == elem) { + gp_x11_chan = gp_x11_chan->next; + free(current_node); + return; + } + + while (current_node->next != NULL) { + if (current_node->next == elem) { + current_node->next = current_node->next->next; + current_node = current_node->next; + free(current_node); + break; + } + } +} + + +static void session_shutdown(LIBSSH2_SESSION *session) +{ + libssh2_session_disconnect(session, + "Session Shutdown, Thank you for playing"); + libssh2_session_free(session); +} + +static int _raw_mode(void) +{ + int rc; + struct termios tio; + + rc = tcgetattr(fileno(stdin), &tio); + if (rc != -1) { + _saved_tio = tio; + /* do the equivalent of cfmakeraw() manually, to build on Solaris */ + tio.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON); + tio.c_oflag &= ~OPOST; + tio.c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN); + tio.c_cflag &= ~(CSIZE|PARENB); + tio.c_cflag |= CS8; + rc = tcsetattr(fileno(stdin), TCSADRAIN, &tio); + } + return rc; +} + +static int _normal_mode(void) +{ + int rc; + rc = tcsetattr(fileno(stdin), TCSADRAIN, &_saved_tio); + return rc; +} + +/* + * CallBack to initialize the forwarding. + * Save the channel to loop on it, save the X11 forwarded socket to send + * and receive info from our X server. + */ +static void x11_callback(LIBSSH2_SESSION *session, LIBSSH2_CHANNEL *channel, + char *shost, int sport, void **abstract) +{ + const char * display = NULL; + char * ptr = NULL; + char * temp_buff = NULL; + int display_port = 0; + int sock = 0; + int rc = 0; + struct sockaddr_un addr; + struct chan_X11_list *new; + struct chan_X11_list *chan_iter; + + /* + * Connect to the display + * Inspired by x11_connect_display in openssh + */ + display = getenv("DISPLAY"); + if ( display != NULL) { + if (strncmp( display, "unix:", 5) == 0 || + display[0] == ':') { + /* Connect to the local unix domain */ + ptr = strrchr(display, ':'); + temp_buff = (char *) calloc(strlen(ptr+1), sizeof(char)); + if (!temp_buff) { + perror("calloc"); + return; + } + memcpy(temp_buff, ptr+1, strlen(ptr+1)); + display_port = atoi(temp_buff); + free(temp_buff); + + sock = socket(AF_UNIX, SOCK_STREAM, 0); + if (sock < 0) + return; + memset(&addr, 0, sizeof(addr)); + addr.sun_family = AF_UNIX; + snprintf(addr.sun_path, sizeof(addr.sun_path), + _PATH_UNIX_X, display_port); + rc = connect(sock, (struct sockaddr *) &addr, sizeof(addr)); + + if (rc != -1){ + /* Connection Successfull */ + if (gp_x11_chan == NULL) { + /* Calloc ensure that gp_X11_chan is full of 0 */ + gp_x11_chan = (struct chan_X11_list *) + calloc(1, sizeof(struct chan_X11_list)); + gp_x11_chan->sock = sock; + gp_x11_chan->chan = channel; + gp_x11_chan->next = NULL; + } + else { + chan_iter = gp_x11_chan; + while (chan_iter->next != NULL) + chan_iter = chan_iter->next; + /* Create the new Node */ + new = (struct chan_X11_list *) + malloc(sizeof(struct chan_X11_list)); + new->sock = sock; + new->chan = channel; + new->next = NULL; + chan_iter->next = new; + } + } + else + close(sock); + } + } + return; +} + +/* + * Send and receive Data for the X11 channel. + * If the connection is closed, returns -1, 0 either. + */ +static int x11_send_receive(LIBSSH2_CHANNEL *channel, int sock) +{ + char * buf = NULL; + int bufsize = 8192; + int rc = 0; + int nfds = 1; + LIBSSH2_POLLFD *fds = NULL; + fd_set set; + struct timeval timeval_out; + timeval_out.tv_sec = 0; + timeval_out.tv_usec = 0; + + + FD_ZERO(&set); + FD_SET(sock,&set); + + if ((buf = calloc (bufsize, sizeof(char))) == NULL) + return 0; + + if ((fds = malloc (sizeof (LIBSSH2_POLLFD))) == NULL) { + free(buf); + return 0; + } + + fds[0].type = LIBSSH2_POLLFD_CHANNEL; + fds[0].fd.channel = channel; + fds[0].events = LIBSSH2_POLLFD_POLLIN; + fds[0].revents = LIBSSH2_POLLFD_POLLIN; + + rc = libssh2_poll(fds, nfds, 0); + if (rc >0) { + rc = libssh2_channel_read(channel, buf, bufsize); + write(sock, buf, rc); + } + + rc = select(sock+1, &set, NULL, NULL, &timeval_out); + if (rc > 0) { + memset((void *)buf, 0, bufsize); + + /* Data in sock*/ + rc = read(sock, buf, bufsize); + if (rc > 0) { + libssh2_channel_write(channel, buf, rc); + } + else { + free(buf); + return -1; + } + } + + free(fds); + free(buf); + if (libssh2_channel_eof(channel) == 1) { + return -1; + } + return 0; +} + +/* + * Main, more than inspired by ssh2.c by Bagder + */ +int +main (int argc, char *argv[]) +{ + unsigned long hostaddr = 0; + int sock = 0; + int rc = 0; + struct sockaddr_in sin; + LIBSSH2_SESSION *session; + LIBSSH2_CHANNEL *channel; + char *username = NULL; + char *password = NULL; + size_t bufsiz = 8193; + char *buf = NULL; + int set_debug_on = 0; + int nfds = 1; + LIBSSH2_POLLFD *fds = NULL; + + /* Chan List struct */ + struct chan_X11_list *current_node = NULL; + + /* Struct winsize for term size */ + struct winsize w_size; + struct winsize w_size_bck; + + /* For select on stdin */ + fd_set set; + struct timeval timeval_out; + timeval_out.tv_sec = 0; + timeval_out.tv_usec = 10; + + + if (argc > 3) { + hostaddr = inet_addr(argv[1]); + username = argv[2]; + password = argv[3]; + } + else { + fprintf(stderr, "Usage: %s destination username password", + argv[0]); + return -1; + } + + if (argc > 4) { + set_debug_on = 1; + fprintf (stderr, "DEBUG is ON: %d\n", set_debug_on); + } + + rc = libssh2_init (0); + if (rc != 0) { + fprintf (stderr, "libssh2 initialization failed (%d)\n", rc); + return 1; + } + + sock = socket (AF_INET, SOCK_STREAM, 0); + if (sock == -1) { + perror("socket"); + return -1; + } + + sin.sin_family = AF_INET; + sin.sin_port = htons (22); + sin.sin_addr.s_addr = hostaddr; + + rc = connect(sock, (struct sockaddr *) &sin, + sizeof(struct sockaddr_in)); + if (rc != 0) { + fprintf (stderr, "Failed to established connection!\n"); + return -1; + } + /* Open a session */ + session = libssh2_session_init(); + rc = libssh2_session_handshake(session, sock); + if (rc != 0) { + fprintf(stderr, "Failed Start the SSH session\n"); + return -1; + } + + if (set_debug_on == 1) + libssh2_trace(session, LIBSSH2_TRACE_CONN); + + /* Set X11 Callback */ + libssh2_session_callback_set(session, LIBSSH2_CALLBACK_X11, + (void *)x11_callback); + + /* Authenticate via password */ + rc = libssh2_userauth_password(session, username, password); + if (rc != 0) { + fprintf(stderr, "Failed to authenticate\n"); + session_shutdown(session); + close(sock); + return -1; + } + + /* Open a channel */ + channel = libssh2_channel_open_session(session); + if ( channel == NULL ) { + fprintf(stderr, "Failed to open a new channel\n"); + session_shutdown(session); + close(sock); + return -1; + } + + + /* Request a PTY */ + rc = libssh2_channel_request_pty( channel, "xterm"); + if (rc != 0) { + fprintf(stderr, "Failed to request a pty\n"); + session_shutdown(session); + close(sock); + return -1; + } + + /* Request X11 */ + rc = libssh2_channel_x11_req(channel,0); + if(rc!=0) { + fprintf(stderr, "Failed to request X11 forwarding\n"); + session_shutdown(session); + close(sock); + return -1; + } + + /* Request a shell */ + rc = libssh2_channel_shell(channel); + if (rc!=0) { + fprintf(stderr, "Failed to open a shell\n"); + session_shutdown(session); + close(sock); + return -1; + } + + rc = _raw_mode(); + if (rc != 0) { + fprintf(stderr, "Failed to entered in raw mode\n"); + session_shutdown(session); + close(sock); + return -1; + } + + memset(&w_size, 0, sizeof(struct winsize)); + memset(&w_size_bck, 0, sizeof(struct winsize)); + + while (1) { + + FD_ZERO(&set); + FD_SET(fileno(stdin),&set); + + /* Search if a resize pty has to be send */ + ioctl(fileno(stdin), TIOCGWINSZ, &w_size); + if ((w_size.ws_row != w_size_bck.ws_row) || + (w_size.ws_col != w_size_bck.ws_col)) { + w_size_bck = w_size; + + libssh2_channel_request_pty_size(channel, + w_size.ws_col, + w_size.ws_row); + } + + if ((buf = calloc (bufsiz, sizeof(char))) == NULL) + break; + + if ((fds = malloc (sizeof (LIBSSH2_POLLFD))) == NULL) { + free(buf); + break; + } + + fds[0].type = LIBSSH2_POLLFD_CHANNEL; + fds[0].fd.channel = channel; + fds[0].events = LIBSSH2_POLLFD_POLLIN; + fds[0].revents = LIBSSH2_POLLFD_POLLIN; + + rc = libssh2_poll(fds, nfds, 0); + if (rc >0) { + libssh2_channel_read(channel, buf, sizeof(buf)); + fprintf(stdout, "%s", buf); + fflush(stdout); + } + + /* Looping on X clients */ + if (gp_x11_chan != NULL) { + current_node = gp_x11_chan; + } + else + current_node = NULL; + + while (current_node != NULL) { + struct chan_X11_list *next_node; + rc = x11_send_receive(current_node->chan, current_node->sock); + next_node = current_node->next; + if (rc == -1){ + shutdown(current_node->sock,SHUT_RDWR); + close(current_node->sock); + remove_node(current_node); + } + + current_node = next_node; + } + + + rc = select(fileno(stdin)+1,&set,NULL,NULL,&timeval_out); + if (rc > 0) { + /* Data in stdin*/ + rc = read(fileno(stdin), buf,1); + if (rc > 0) + libssh2_channel_write(channel,buf, sizeof(buf)); + } + + free (fds); + free (buf); + + if (libssh2_channel_eof (channel) == 1) { + break; + } + } + + if (channel) { + libssh2_channel_free (channel); + channel = NULL; + } + _normal_mode(); + + libssh2_exit(); + + return 0; +} |