1 /* Self test, based on examples/ssh2.c. */
2 
3 #include "libssh2_config.h"
4 #include <libssh2.h>
5 #include <libssh2_sftp.h>
6 
7 #ifdef HAVE_WINDOWS_H
8 # include <windows.h>
9 #endif
10 #ifdef HAVE_WINSOCK2_H
11 # include <winsock2.h>
12 #endif
13 #ifdef HAVE_SYS_SOCKET_H
14 # include <sys/socket.h>
15 #endif
16 #ifdef HAVE_NETINET_IN_H
17 # include <netinet/in.h>
18 #endif
19 # ifdef HAVE_UNISTD_H
20 #include <unistd.h>
21 #endif
22 # ifdef HAVE_ARPA_INET_H
23 #include <arpa/inet.h>
24 #endif
25 
26 #include <sys/types.h>
27 #include <fcntl.h>
28 #include <errno.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <ctype.h>
32 
main(int argc,char * argv[])33 int main(int argc, char *argv[])
34 {
35     unsigned long hostaddr;
36     int sock, i, auth_pw = 0;
37     struct sockaddr_in sin;
38     const char *fingerprint;
39     char *userauthlist;
40     LIBSSH2_SESSION *session;
41     LIBSSH2_CHANNEL *channel;
42     const char *pubkeyfile = "etc/user.pub";
43     const char *privkeyfile = "etc/user";
44     const char *username = "username";
45     const char *password = "password";
46     int ec = 1;
47 
48 #ifdef WIN32
49     WSADATA wsadata;
50     int err;
51 
52     err = WSAStartup(MAKEWORD(2, 0), &wsadata);
53     if(err != 0) {
54         fprintf(stderr, "WSAStartup failed with error: %d\n", err);
55         return -1;
56     }
57 #endif
58 
59     (void)argc;
60     (void)argv;
61 
62     if(getenv("USER"))
63       username = getenv("USER");
64 
65     if(getenv ("PRIVKEY"))
66       privkeyfile = getenv("PRIVKEY");
67 
68     if(getenv("PUBKEY"))
69       pubkeyfile = getenv("PUBKEY");
70 
71     hostaddr = htonl(0x7F000001);
72 
73     sock = socket(AF_INET, SOCK_STREAM, 0);
74 #ifndef WIN32
75     fcntl(sock, F_SETFL, 0);
76 #endif
77     sin.sin_family = AF_INET;
78     sin.sin_port = htons(4711);
79     sin.sin_addr.s_addr = hostaddr;
80     if(connect(sock, (struct sockaddr*)(&sin),
81                 sizeof(struct sockaddr_in)) != 0) {
82         fprintf(stderr, "failed to connect!\n");
83         return 1;
84     }
85 
86     /* Create a session instance and start it up
87      * This will trade welcome banners, exchange keys,
88      * and setup crypto, compression, and MAC layers
89      */
90     session = libssh2_session_init();
91     if(libssh2_session_startup(session, sock)) {
92         fprintf(stderr, "Failure establishing SSH session\n");
93         return 1;
94     }
95 
96     /* At this point we haven't authenticated,
97      * The first thing to do is check the hostkey's
98      * fingerprint against our known hosts
99      * Your app may have it hard coded, may go to a file,
100      * may present it to the user, that's your call
101      */
102     fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_SHA1);
103     printf("Fingerprint: ");
104     for(i = 0; i < 20; i++) {
105         printf("%02X ", (unsigned char)fingerprint[i]);
106     }
107     printf("\n");
108 
109     /* check what authentication methods are available */
110     userauthlist = libssh2_userauth_list(session, username, strlen(username));
111     printf("Authentication methods: %s\n", userauthlist);
112     if(strstr(userauthlist, "password") != NULL) {
113         auth_pw |= 1;
114     }
115     if(strstr(userauthlist, "keyboard-interactive") != NULL) {
116         auth_pw |= 2;
117     }
118     if(strstr(userauthlist, "publickey") != NULL) {
119         auth_pw |= 4;
120     }
121 
122     if(auth_pw & 4) {
123         /* Authenticate by public key */
124         if(libssh2_userauth_publickey_fromfile(session, username, pubkeyfile,
125                                                privkeyfile, password)) {
126             printf("\tAuthentication by public key failed!\n");
127             goto shutdown;
128         }
129         else {
130             printf("\tAuthentication by public key succeeded.\n");
131         }
132     }
133     else {
134         printf("No supported authentication methods found!\n");
135         goto shutdown;
136     }
137 
138     /* Request a shell */
139     channel = libssh2_channel_open_session(session);
140     if(!channel) {
141         fprintf(stderr, "Unable to open a session\n");
142         goto shutdown;
143     }
144 
145     /* Some environment variables may be set,
146      * It's up to the server which ones it'll allow though
147      */
148     libssh2_channel_setenv(channel, "FOO", "bar");
149 
150     /* Request a terminal with 'vanilla' terminal emulation
151      * See /etc/termcap for more options
152      */
153     if(libssh2_channel_request_pty(channel, "vanilla")) {
154         fprintf(stderr, "Failed requesting pty\n");
155         goto skip_shell;
156     }
157 
158     /* Open a SHELL on that pty */
159     if(libssh2_channel_shell(channel)) {
160         fprintf(stderr, "Unable to request shell on allocated pty\n");
161         goto shutdown;
162     }
163 
164     ec = 0;
165 
166   skip_shell:
167     if(channel) {
168         libssh2_channel_free(channel);
169         channel = NULL;
170     }
171 
172   shutdown:
173 
174     libssh2_session_disconnect(session, "Normal Shutdown");
175     libssh2_session_free(session);
176 
177 #ifdef WIN32
178     Sleep(1000);
179     closesocket(sock);
180 #else
181     sleep(1);
182     close(sock);
183 #endif
184 
185     return ec;
186 }
187