1 #ifndef HEADER_CURL_SSH_H
2 #define HEADER_CURL_SSH_H
3 /***************************************************************************
4  *                                  _   _ ____  _
5  *  Project                     ___| | | |  _ \| |
6  *                             / __| | | | |_) | |
7  *                            | (__| |_| |  _ <| |___
8  *                             \___|\___/|_| \_\_____|
9  *
10  * Copyright (C) 1998 - 2020, Daniel Stenberg, <daniel@haxx.se>, et al.
11  *
12  * This software is licensed as described in the file COPYING, which
13  * you should have received as part of this distribution. The terms
14  * are also available at https://curl.haxx.se/docs/copyright.html.
15  *
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 
25 #include "curl_setup.h"
26 
27 #if defined(HAVE_LIBSSH2_H)
28 #include <libssh2.h>
29 #include <libssh2_sftp.h>
30 #elif defined(HAVE_LIBSSH_LIBSSH_H)
31 #include <libssh/libssh.h>
32 #include <libssh/sftp.h>
33 #elif defined(USE_WOLFSSH)
34 #include <wolfssh/ssh.h>
35 #include <wolfssh/wolfsftp.h>
36 #endif
37 
38 /****************************************************************************
39  * SSH unique setup
40  ***************************************************************************/
41 typedef enum {
42   SSH_NO_STATE = -1,  /* Used for "nextState" so say there is none */
43   SSH_STOP = 0,       /* do nothing state, stops the state machine */
44 
45   SSH_INIT,           /* First state in SSH-CONNECT */
46   SSH_S_STARTUP,      /* Session startup */
47   SSH_HOSTKEY,        /* verify hostkey */
48   SSH_AUTHLIST,
49   SSH_AUTH_PKEY_INIT,
50   SSH_AUTH_PKEY,
51   SSH_AUTH_PASS_INIT,
52   SSH_AUTH_PASS,
53   SSH_AUTH_AGENT_INIT, /* initialize then wait for connection to agent */
54   SSH_AUTH_AGENT_LIST, /* ask for list then wait for entire list to come */
55   SSH_AUTH_AGENT,      /* attempt one key at a time */
56   SSH_AUTH_HOST_INIT,
57   SSH_AUTH_HOST,
58   SSH_AUTH_KEY_INIT,
59   SSH_AUTH_KEY,
60   SSH_AUTH_GSSAPI,
61   SSH_AUTH_DONE,
62   SSH_SFTP_INIT,
63   SSH_SFTP_REALPATH,   /* Last state in SSH-CONNECT */
64 
65   SSH_SFTP_QUOTE_INIT, /* First state in SFTP-DO */
66   SSH_SFTP_POSTQUOTE_INIT, /* (Possibly) First state in SFTP-DONE */
67   SSH_SFTP_QUOTE,
68   SSH_SFTP_NEXT_QUOTE,
69   SSH_SFTP_QUOTE_STAT,
70   SSH_SFTP_QUOTE_SETSTAT,
71   SSH_SFTP_QUOTE_SYMLINK,
72   SSH_SFTP_QUOTE_MKDIR,
73   SSH_SFTP_QUOTE_RENAME,
74   SSH_SFTP_QUOTE_RMDIR,
75   SSH_SFTP_QUOTE_UNLINK,
76   SSH_SFTP_QUOTE_STATVFS,
77   SSH_SFTP_GETINFO,
78   SSH_SFTP_FILETIME,
79   SSH_SFTP_TRANS_INIT,
80   SSH_SFTP_UPLOAD_INIT,
81   SSH_SFTP_CREATE_DIRS_INIT,
82   SSH_SFTP_CREATE_DIRS,
83   SSH_SFTP_CREATE_DIRS_MKDIR,
84   SSH_SFTP_READDIR_INIT,
85   SSH_SFTP_READDIR,
86   SSH_SFTP_READDIR_LINK,
87   SSH_SFTP_READDIR_BOTTOM,
88   SSH_SFTP_READDIR_DONE,
89   SSH_SFTP_DOWNLOAD_INIT,
90   SSH_SFTP_DOWNLOAD_STAT, /* Last state in SFTP-DO */
91   SSH_SFTP_CLOSE,    /* Last state in SFTP-DONE */
92   SSH_SFTP_SHUTDOWN, /* First state in SFTP-DISCONNECT */
93   SSH_SCP_TRANS_INIT, /* First state in SCP-DO */
94   SSH_SCP_UPLOAD_INIT,
95   SSH_SCP_DOWNLOAD_INIT,
96   SSH_SCP_DOWNLOAD,
97   SSH_SCP_DONE,
98   SSH_SCP_SEND_EOF,
99   SSH_SCP_WAIT_EOF,
100   SSH_SCP_WAIT_CLOSE,
101   SSH_SCP_CHANNEL_FREE,   /* Last state in SCP-DONE */
102   SSH_SESSION_DISCONNECT, /* First state in SCP-DISCONNECT */
103   SSH_SESSION_FREE,       /* Last state in SCP/SFTP-DISCONNECT */
104   SSH_QUIT,
105   SSH_LAST  /* never used */
106 } sshstate;
107 
108 /* this struct is used in the HandleData struct which is part of the
109    Curl_easy, which means this is used on a per-easy handle basis.
110    Everything that is strictly related to a connection is banned from this
111    struct. */
112 struct SSHPROTO {
113   char *path;                  /* the path we operate on */
114 };
115 
116 /* ssh_conn is used for struct connection-oriented data in the connectdata
117    struct */
118 struct ssh_conn {
119   const char *authlist;       /* List of auth. methods, managed by libssh2 */
120 
121   /* common */
122   const char *passphrase;     /* pass-phrase to use */
123   char *rsa_pub;              /* path name */
124   char *rsa;                  /* path name */
125   bool authed;                /* the connection has been authenticated fine */
126   sshstate state;             /* always use ssh.c:state() to change state! */
127   sshstate nextstate;         /* the state to goto after stopping */
128   CURLcode actualcode;        /* the actual error code */
129   struct curl_slist *quote_item; /* for the quote option */
130   char *quote_path1;          /* two generic pointers for the QUOTE stuff */
131   char *quote_path2;
132 
133   bool acceptfail;            /* used by the SFTP_QUOTE (continue if
134                                  quote command fails) */
135   char *homedir;              /* when doing SFTP we figure out home dir in the
136                                  connect phase */
137   char *readdir_line;
138   /* end of READDIR stuff */
139 
140   int secondCreateDirs;         /* counter use by the code to see if the
141                                    second attempt has been made to change
142                                    to/create a directory */
143   char *slash_pos;              /* used by the SFTP_CREATE_DIRS state */
144 
145   int orig_waitfor;             /* default READ/WRITE bits wait for */
146 
147 #if defined(USE_LIBSSH)
148   char *readdir_linkPath;
149   size_t readdir_len, readdir_totalLen, readdir_currLen;
150 /* our variables */
151   unsigned kbd_state; /* 0 or 1 */
152   ssh_key privkey;
153   ssh_key pubkey;
154   int auth_methods;
155   ssh_session ssh_session;
156   ssh_scp scp_session;
157   sftp_session sftp_session;
158   sftp_file sftp_file;
159   sftp_dir sftp_dir;
160 
161   unsigned sftp_recv_state; /* 0 or 1 */
162   int sftp_file_index; /* for async read */
163   sftp_attributes readdir_attrs; /* used by the SFTP readdir actions */
164   sftp_attributes readdir_link_attrs; /* used by the SFTP readdir actions */
165   sftp_attributes quote_attrs; /* used by the SFTP_QUOTE state */
166 
167   const char *readdir_filename; /* points within readdir_attrs */
168   const char *readdir_longentry;
169   char *readdir_tmp;
170 #elif defined(USE_LIBSSH2)
171   struct dynbuf readdir_link;
172   struct dynbuf readdir;
173   char *readdir_filename;
174   char *readdir_longentry;
175 
176   LIBSSH2_SFTP_ATTRIBUTES quote_attrs; /* used by the SFTP_QUOTE state */
177 
178   /* Here's a set of struct members used by the SFTP_READDIR state */
179   LIBSSH2_SFTP_ATTRIBUTES readdir_attrs;
180   LIBSSH2_SESSION *ssh_session; /* Secure Shell session */
181   LIBSSH2_CHANNEL *ssh_channel; /* Secure Shell channel handle */
182   LIBSSH2_SFTP *sftp_session;   /* SFTP handle */
183   LIBSSH2_SFTP_HANDLE *sftp_handle;
184 
185 #ifdef HAVE_LIBSSH2_AGENT_API
186   LIBSSH2_AGENT *ssh_agent;     /* proxy to ssh-agent/pageant */
187   struct libssh2_agent_publickey *sshagent_identity,
188                                  *sshagent_prev_identity;
189 #endif
190 
191   /* note that HAVE_LIBSSH2_KNOWNHOST_API is a define set in the libssh2.h
192      header */
193 #ifdef HAVE_LIBSSH2_KNOWNHOST_API
194   LIBSSH2_KNOWNHOSTS *kh;
195 #endif
196 #elif defined(USE_WOLFSSH)
197   WOLFSSH *ssh_session;
198   WOLFSSH_CTX *ctx;
199   word32 handleSz;
200   byte handle[WOLFSSH_MAX_HANDLE];
201   curl_off_t offset;
202 #endif /* USE_LIBSSH */
203 };
204 
205 #if defined(USE_LIBSSH)
206 
207 #define CURL_LIBSSH_VERSION ssh_version(0)
208 
209 #elif defined(USE_LIBSSH2)
210 
211 /* Feature detection based on version numbers to better work with
212    non-configure platforms */
213 
214 #if !defined(LIBSSH2_VERSION_NUM) || (LIBSSH2_VERSION_NUM < 0x001000)
215 #  error "SCP/SFTP protocols require libssh2 0.16 or later"
216 #endif
217 
218 #if LIBSSH2_VERSION_NUM >= 0x010000
219 #define HAVE_LIBSSH2_SFTP_SEEK64 1
220 #endif
221 
222 #if LIBSSH2_VERSION_NUM >= 0x010100
223 #define HAVE_LIBSSH2_VERSION 1
224 #endif
225 
226 #if LIBSSH2_VERSION_NUM >= 0x010205
227 #define HAVE_LIBSSH2_INIT 1
228 #define HAVE_LIBSSH2_EXIT 1
229 #endif
230 
231 #if LIBSSH2_VERSION_NUM >= 0x010206
232 #define HAVE_LIBSSH2_KNOWNHOST_CHECKP 1
233 #define HAVE_LIBSSH2_SCP_SEND64 1
234 #endif
235 
236 #if LIBSSH2_VERSION_NUM >= 0x010208
237 #define HAVE_LIBSSH2_SESSION_HANDSHAKE 1
238 #endif
239 
240 #ifdef HAVE_LIBSSH2_VERSION
241 /* get it run-time if possible */
242 #define CURL_LIBSSH2_VERSION libssh2_version(0)
243 #else
244 /* use build-time if run-time not possible */
245 #define CURL_LIBSSH2_VERSION LIBSSH2_VERSION
246 #endif
247 
248 #endif /* USE_LIBSSH2 */
249 
250 #ifdef USE_SSH
251 
252 extern const struct Curl_handler Curl_handler_scp;
253 extern const struct Curl_handler Curl_handler_sftp;
254 
255 /* generic SSH backend functions */
256 CURLcode Curl_ssh_init(void);
257 void Curl_ssh_cleanup(void);
258 size_t Curl_ssh_version(char *buffer, size_t buflen);
259 #else
260 /* for non-SSH builds */
261 #define Curl_ssh_cleanup()
262 #endif
263 
264 #endif /* HEADER_CURL_SSH_H */
265