blob: 1c13550774477055965e0028d3a56f7c7e076570 [file] [log] [blame]
Kristian Monsen5ab50182010-05-14 18:53:44 +01001#ifndef HEADER_CURL_SSH_H
2#define HEADER_CURL_SSH_H
3/***************************************************************************
4 * _ _ ____ _
5 * Project ___| | | | _ \| |
6 * / __| | | | |_) | |
7 * | (__| |_| | _ <| |___
8 * \___|\___/|_| \_\_____|
9 *
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -070010 * Copyright (C) 1998 - 2015, Daniel Stenberg, <daniel@haxx.se>, et al.
Kristian Monsen5ab50182010-05-14 18:53:44 +010011 *
12 * This software is licensed as described in the file COPYING, which
13 * you should have received as part of this distribution. The terms
Alex Deymod15eaac2016-06-28 14:49:26 -070014 * are also available at https://curl.haxx.se/docs/copyright.html.
Kristian Monsen5ab50182010-05-14 18:53:44 +010015 *
16 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
17 * copies of the Software, and permit persons to whom the Software is
18 * furnished to do so, under the terms of the COPYING file.
19 *
20 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
21 * KIND, either express or implied.
22 *
23 ***************************************************************************/
24
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -070025#include "curl_setup.h"
Kristian Monsen5ab50182010-05-14 18:53:44 +010026
Elliott Hughes0128fe42018-02-27 14:57:55 -080027#if defined(HAVE_LIBSSH2_H)
Kristian Monsen5ab50182010-05-14 18:53:44 +010028#include <libssh2.h>
29#include <libssh2_sftp.h>
Elliott Hughes0128fe42018-02-27 14:57:55 -080030#elif defined(HAVE_LIBSSH_LIBSSH_H)
31#include <libssh/libssh.h>
32#include <libssh/sftp.h>
Kristian Monsen5ab50182010-05-14 18:53:44 +010033#endif /* HAVE_LIBSSH2_H */
34
35/****************************************************************************
36 * SSH unique setup
37 ***************************************************************************/
38typedef enum {
39 SSH_NO_STATE = -1, /* Used for "nextState" so say there is none */
40 SSH_STOP = 0, /* do nothing state, stops the state machine */
41
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -070042 SSH_INIT, /* First state in SSH-CONNECT */
43 SSH_S_STARTUP, /* Session startup */
Kristian Monsen5ab50182010-05-14 18:53:44 +010044 SSH_HOSTKEY, /* verify hostkey */
45 SSH_AUTHLIST,
46 SSH_AUTH_PKEY_INIT,
47 SSH_AUTH_PKEY,
48 SSH_AUTH_PASS_INIT,
49 SSH_AUTH_PASS,
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -070050 SSH_AUTH_AGENT_INIT, /* initialize then wait for connection to agent */
51 SSH_AUTH_AGENT_LIST, /* ask for list then wait for entire list to come */
52 SSH_AUTH_AGENT, /* attempt one key at a time */
Kristian Monsen5ab50182010-05-14 18:53:44 +010053 SSH_AUTH_HOST_INIT,
54 SSH_AUTH_HOST,
55 SSH_AUTH_KEY_INIT,
56 SSH_AUTH_KEY,
Elliott Hughes0128fe42018-02-27 14:57:55 -080057 SSH_AUTH_GSSAPI,
Kristian Monsen5ab50182010-05-14 18:53:44 +010058 SSH_AUTH_DONE,
59 SSH_SFTP_INIT,
60 SSH_SFTP_REALPATH, /* Last state in SSH-CONNECT */
61
62 SSH_SFTP_QUOTE_INIT, /* First state in SFTP-DO */
63 SSH_SFTP_POSTQUOTE_INIT, /* (Possibly) First state in SFTP-DONE */
64 SSH_SFTP_QUOTE,
65 SSH_SFTP_NEXT_QUOTE,
66 SSH_SFTP_QUOTE_STAT,
67 SSH_SFTP_QUOTE_SETSTAT,
68 SSH_SFTP_QUOTE_SYMLINK,
69 SSH_SFTP_QUOTE_MKDIR,
70 SSH_SFTP_QUOTE_RENAME,
71 SSH_SFTP_QUOTE_RMDIR,
72 SSH_SFTP_QUOTE_UNLINK,
Alex Deymod15eaac2016-06-28 14:49:26 -070073 SSH_SFTP_QUOTE_STATVFS,
74 SSH_SFTP_GETINFO,
75 SSH_SFTP_FILETIME,
Kristian Monsen5ab50182010-05-14 18:53:44 +010076 SSH_SFTP_TRANS_INIT,
77 SSH_SFTP_UPLOAD_INIT,
78 SSH_SFTP_CREATE_DIRS_INIT,
79 SSH_SFTP_CREATE_DIRS,
80 SSH_SFTP_CREATE_DIRS_MKDIR,
81 SSH_SFTP_READDIR_INIT,
82 SSH_SFTP_READDIR,
83 SSH_SFTP_READDIR_LINK,
84 SSH_SFTP_READDIR_BOTTOM,
85 SSH_SFTP_READDIR_DONE,
86 SSH_SFTP_DOWNLOAD_INIT,
87 SSH_SFTP_DOWNLOAD_STAT, /* Last state in SFTP-DO */
88 SSH_SFTP_CLOSE, /* Last state in SFTP-DONE */
89 SSH_SFTP_SHUTDOWN, /* First state in SFTP-DISCONNECT */
90 SSH_SCP_TRANS_INIT, /* First state in SCP-DO */
91 SSH_SCP_UPLOAD_INIT,
92 SSH_SCP_DOWNLOAD_INIT,
Elliott Hughes0128fe42018-02-27 14:57:55 -080093 SSH_SCP_DOWNLOAD,
Kristian Monsen5ab50182010-05-14 18:53:44 +010094 SSH_SCP_DONE,
95 SSH_SCP_SEND_EOF,
96 SSH_SCP_WAIT_EOF,
97 SSH_SCP_WAIT_CLOSE,
98 SSH_SCP_CHANNEL_FREE, /* Last state in SCP-DONE */
99 SSH_SESSION_DISCONNECT, /* First state in SCP-DISCONNECT */
100 SSH_SESSION_FREE, /* Last state in SCP/SFTP-DISCONNECT */
101 SSH_QUIT,
102 SSH_LAST /* never used */
103} sshstate;
104
105/* this struct is used in the HandleData struct which is part of the
Alex Deymoe3149cc2016-10-05 11:18:42 -0700106 Curl_easy, which means this is used on a per-easy handle basis.
Kristian Monsen5ab50182010-05-14 18:53:44 +0100107 Everything that is strictly related to a connection is banned from this
108 struct. */
109struct SSHPROTO {
110 char *path; /* the path we operate on */
111};
112
113/* ssh_conn is used for struct connection-oriented data in the connectdata
114 struct */
115struct ssh_conn {
116 const char *authlist; /* List of auth. methods, managed by libssh2 */
Elliott Hughes0128fe42018-02-27 14:57:55 -0800117
118 /* common */
Kristian Monsen5ab50182010-05-14 18:53:44 +0100119 const char *passphrase; /* pass-phrase to use */
120 char *rsa_pub; /* path name */
121 char *rsa; /* path name */
122 bool authed; /* the connection has been authenticated fine */
123 sshstate state; /* always use ssh.c:state() to change state! */
124 sshstate nextstate; /* the state to goto after stopping */
125 CURLcode actualcode; /* the actual error code */
126 struct curl_slist *quote_item; /* for the quote option */
127 char *quote_path1; /* two generic pointers for the QUOTE stuff */
128 char *quote_path2;
Elliott Hughes0128fe42018-02-27 14:57:55 -0800129
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700130 bool acceptfail; /* used by the SFTP_QUOTE (continue if
131 quote command fails) */
Kristian Monsen5ab50182010-05-14 18:53:44 +0100132 char *homedir; /* when doing SFTP we figure out home dir in the
133 connect phase */
Kristian Monsen5ab50182010-05-14 18:53:44 +0100134 int readdir_len, readdir_totalLen, readdir_currLen;
135 char *readdir_line;
136 char *readdir_linkPath;
137 /* end of READDIR stuff */
138
139 int secondCreateDirs; /* counter use by the code to see if the
140 second attempt has been made to change
141 to/create a directory */
142 char *slash_pos; /* used by the SFTP_CREATE_DIRS state */
Elliott Hughes0128fe42018-02-27 14:57:55 -0800143
144 int orig_waitfor; /* default READ/WRITE bits wait for */
145
146#if defined(USE_LIBSSH)
147/* our variables */
148 unsigned kbd_state; /* 0 or 1 */
149 ssh_key privkey;
150 ssh_key pubkey;
151 int auth_methods;
152 ssh_session ssh_session;
153 ssh_scp scp_session;
154 sftp_session sftp_session;
155 sftp_file sftp_file;
156 sftp_dir sftp_dir;
157
158 unsigned sftp_recv_state; /* 0 or 1 */
159 int sftp_file_index; /* for async read */
160 sftp_attributes readdir_attrs; /* used by the SFTP readdir actions */
161 sftp_attributes readdir_link_attrs; /* used by the SFTP readdir actions */
162 sftp_attributes quote_attrs; /* used by the SFTP_QUOTE state */
163
164 const char *readdir_filename; /* points within readdir_attrs */
165 const char *readdir_longentry;
166 char *readdir_tmp;
167#elif defined(USE_LIBSSH2)
168 char *readdir_filename;
169 char *readdir_longentry;
170
171 LIBSSH2_SFTP_ATTRIBUTES quote_attrs; /* used by the SFTP_QUOTE state */
172
173 /* Here's a set of struct members used by the SFTP_READDIR state */
174 LIBSSH2_SFTP_ATTRIBUTES readdir_attrs;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100175 LIBSSH2_SESSION *ssh_session; /* Secure Shell session */
176 LIBSSH2_CHANNEL *ssh_channel; /* Secure Shell channel handle */
177 LIBSSH2_SFTP *sftp_session; /* SFTP handle */
178 LIBSSH2_SFTP_HANDLE *sftp_handle;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100179
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700180#ifdef HAVE_LIBSSH2_AGENT_API
181 LIBSSH2_AGENT *ssh_agent; /* proxy to ssh-agent/pageant */
182 struct libssh2_agent_publickey *sshagent_identity,
183 *sshagent_prev_identity;
184#endif
185
Kristian Monsen5ab50182010-05-14 18:53:44 +0100186 /* note that HAVE_LIBSSH2_KNOWNHOST_API is a define set in the libssh2.h
187 header */
188#ifdef HAVE_LIBSSH2_KNOWNHOST_API
189 LIBSSH2_KNOWNHOSTS *kh;
190#endif
Elliott Hughes0128fe42018-02-27 14:57:55 -0800191#endif /* USE_LIBSSH */
Kristian Monsen5ab50182010-05-14 18:53:44 +0100192};
193
Elliott Hughes0128fe42018-02-27 14:57:55 -0800194#if defined(USE_LIBSSH)
195
196#define CURL_LIBSSH_VERSION ssh_version(0)
197
198extern const struct Curl_handler Curl_handler_scp;
199extern const struct Curl_handler Curl_handler_sftp;
200
201#elif defined(USE_LIBSSH2)
Kristian Monsen5ab50182010-05-14 18:53:44 +0100202
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700203/* Feature detection based on version numbers to better work with
204 non-configure platforms */
205
Kristian Monsen5ab50182010-05-14 18:53:44 +0100206#if !defined(LIBSSH2_VERSION_NUM) || (LIBSSH2_VERSION_NUM < 0x001000)
207# error "SCP/SFTP protocols require libssh2 0.16 or later"
208#endif
209
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700210#if LIBSSH2_VERSION_NUM >= 0x010000
211#define HAVE_LIBSSH2_SFTP_SEEK64 1
Kristian Monsen5ab50182010-05-14 18:53:44 +0100212#endif
213
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700214#if LIBSSH2_VERSION_NUM >= 0x010100
215#define HAVE_LIBSSH2_VERSION 1
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700216#endif
217
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700218#if LIBSSH2_VERSION_NUM >= 0x010205
219#define HAVE_LIBSSH2_INIT 1
220#define HAVE_LIBSSH2_EXIT 1
221#endif
222
223#if LIBSSH2_VERSION_NUM >= 0x010206
224#define HAVE_LIBSSH2_KNOWNHOST_CHECKP 1
225#define HAVE_LIBSSH2_SCP_SEND64 1
226#endif
227
228#if LIBSSH2_VERSION_NUM >= 0x010208
229#define HAVE_LIBSSH2_SESSION_HANDSHAKE 1
230#endif
Kristian Monsen5ab50182010-05-14 18:53:44 +0100231
Elliott Hughes0128fe42018-02-27 14:57:55 -0800232#ifdef HAVE_LIBSSH2_VERSION
233/* get it run-time if possible */
234#define CURL_LIBSSH2_VERSION libssh2_version(0)
235#else
236/* use build-time if run-time not possible */
237#define CURL_LIBSSH2_VERSION LIBSSH2_VERSION
238#endif
239
Kristian Monsen5ab50182010-05-14 18:53:44 +0100240extern const struct Curl_handler Curl_handler_scp;
241extern const struct Curl_handler Curl_handler_sftp;
242
Kristian Monsen5ab50182010-05-14 18:53:44 +0100243#endif /* USE_LIBSSH2 */
244
245#endif /* HEADER_CURL_SSH_H */