1 /*
2    +----------------------------------------------------------------------+
3    | PHP Version 7                                                        |
4    +----------------------------------------------------------------------+
5    | Copyright (c) 1997-2016 The PHP Group                                |
6    +----------------------------------------------------------------------+
7    | This source file is subject to version 3.01 of the PHP license,      |
8    | that is bundled with this package in the file LICENSE, and is        |
9    | available through the world-wide-web at the following url:           |
10    | http://www.php.net/license/3_01.txt                                  |
11    | If you did not receive a copy of the PHP license and are unable to   |
12    | obtain it through the world-wide-web, please send a note to          |
13    | license@php.net so we can mail you a copy immediately.               |
14    +----------------------------------------------------------------------+
15    | Author: Sara Golemon <pollita@php.net>                               |
16    +----------------------------------------------------------------------+
17 */
18 
19 #ifndef PHP_SSH2_H
20 #define PHP_SSH2_H
21 
22 #include <libssh2.h>
23 #include <libssh2_sftp.h>
24 #include "ext/standard/url.h"
25 #include "main/php_network.h"
26 
27 #define PHP_SSH2_VERSION				"1.3.1"
28 #define PHP_SSH2_DEFAULT_PORT			22
29 
30 /* Exported Constants */
31 #define PHP_SSH2_FINGERPRINT_MD5		0x0000
32 #define PHP_SSH2_FINGERPRINT_SHA1		0x0001
33 #define PHP_SSH2_FINGERPRINT_HEX		0x0000
34 #define PHP_SSH2_FINGERPRINT_RAW		0x0002
35 
36 #define PHP_SSH2_TERM_UNIT_CHARS		0x0000
37 #define PHP_SSH2_TERM_UNIT_PIXELS		0x0001
38 
39 #define PHP_SSH2_DEFAULT_TERMINAL		"vanilla"
40 #define PHP_SSH2_DEFAULT_TERM_WIDTH		80
41 #define PHP_SSH2_DEFAULT_TERM_HEIGHT	25
42 #define PHP_SSH2_DEFAULT_TERM_UNIT		PHP_SSH2_TERM_UNIT_CHARS
43 
44 #define PHP_SSH2_SESSION_RES_NAME		"SSH2 Session"
45 #define PHP_SSH2_CHANNEL_STREAM_NAME	"SSH2 Channel"
46 #define PHP_SSH2_LISTENER_RES_NAME		"SSH2 Listener"
47 #define PHP_SSH2_SFTP_RES_NAME			"SSH2 SFTP"
48 #define PHP_SSH2_PKEY_SUBSYS_RES_NAME	"SSH2 Publickey Subsystem"
49 
50 #define PHP_SSH2_SFTP_STREAM_NAME		"SSH2 SFTP File"
51 #define PHP_SSH2_SFTP_DIRSTREAM_NAME	"SSH2 SFTP Directory"
52 #define PHP_SSH2_SFTP_WRAPPER_NAME		"SSH2 SFTP"
53 
54 #define PHP_SSH2_LISTEN_MAX_QUEUED		16
55 
56 #define PHP_SSH2_DEFAULT_POLL_TIMEOUT	30
57 
58 extern zend_module_entry ssh2_module_entry;
59 #define phpext_ssh2_ptr &ssh2_module_entry
60 
61 typedef struct _php_ssh2_session_data {
62 	/* Userspace callback functions */
63 	zval *ignore_cb;
64 	zval *debug_cb;
65 	zval *macerror_cb;
66 	zval *disconnect_cb;
67 
68 	php_socket_t socket;
69 } php_ssh2_session_data;
70 
71 typedef struct _php_ssh2_sftp_data {
72 	LIBSSH2_SESSION *session;
73 	LIBSSH2_SFTP *sftp;
74 
75 	zend_resource *session_rsrc;
76 } php_ssh2_sftp_data;
77 
78 typedef struct _php_ssh2_listener_data {
79 	LIBSSH2_SESSION *session;
80 	LIBSSH2_LISTENER *listener;
81 
82 	zend_resource *session_rsrc;
83 } php_ssh2_listener_data;
84 
85 #include "libssh2_publickey.h"
86 
87 typedef struct _php_ssh2_pkey_subsys_data {
88 	LIBSSH2_SESSION *session;
89 	LIBSSH2_PUBLICKEY *pkey;
90 
91 	zend_resource *session_rsrc;
92 } php_ssh2_pkey_subsys_data;
93 
94 #define SSH2_FETCH_NONAUTHENTICATED_SESSION(session, zsession) \
95 if ((session = (LIBSSH2_SESSION *)zend_fetch_resource(Z_RES_P(zsession), PHP_SSH2_SESSION_RES_NAME, le_ssh2_session)) == NULL) { \
96 	RETURN_FALSE; \
97 } \
98 if (libssh2_userauth_authenticated(session)) { \
99 	php_error_docref(NULL, E_WARNING, "Connection already authenticated"); \
100 	RETURN_FALSE; \
101 }
102 
103 #define SSH2_FETCH_AUTHENTICATED_SESSION(session, zsession) \
104 if ((session = (LIBSSH2_SESSION *)zend_fetch_resource(Z_RES_P(zsession), PHP_SSH2_SESSION_RES_NAME, le_ssh2_session)) == NULL) { \
105 	RETURN_FALSE; \
106 } \
107 if (!libssh2_userauth_authenticated(session)) { \
108 	php_error_docref(NULL, E_WARNING, "Connection not authenticated"); \
109 	RETURN_FALSE; \
110 }
111 
112 typedef struct _php_ssh2_channel_data {
113 	LIBSSH2_CHANNEL *channel;
114 
115 	/* Distinguish which stream we should read/write from/to */
116 	unsigned int streamid;
117 	char is_blocking;
118 	long timeout;
119 
120 	/* Resource */
121 	zend_resource *session_rsrc;
122 
123 	/* Allow one stream to be closed while the other is kept open */
124 	unsigned char *refcount;
125 
126 } php_ssh2_channel_data;
127 
128 /* In ssh2_fopen_wrappers.c */
129 PHP_FUNCTION(ssh2_shell);
130 PHP_FUNCTION(ssh2_exec);
131 PHP_FUNCTION(ssh2_tunnel);
132 PHP_FUNCTION(ssh2_scp_recv);
133 PHP_FUNCTION(ssh2_scp_send);
134 PHP_FUNCTION(ssh2_fetch_stream);
135 PHP_FUNCTION(ssh2_send_eof);
136 
137 /* In ssh2_sftp.c */
138 PHP_FUNCTION(ssh2_sftp);
139 
140 PHP_FUNCTION(ssh2_sftp_rename);
141 PHP_FUNCTION(ssh2_sftp_unlink);
142 PHP_FUNCTION(ssh2_sftp_mkdir);
143 PHP_FUNCTION(ssh2_sftp_rmdir);
144 PHP_FUNCTION(ssh2_sftp_chmod);
145 PHP_FUNCTION(ssh2_sftp_stat);
146 PHP_FUNCTION(ssh2_sftp_lstat);
147 PHP_FUNCTION(ssh2_sftp_symlink);
148 PHP_FUNCTION(ssh2_sftp_readlink);
149 PHP_FUNCTION(ssh2_sftp_realpath);
150 
151 LIBSSH2_SESSION *php_ssh2_session_connect(char *host, int port, zval *methods, zval *callbacks);
152 void php_ssh2_sftp_dtor(zend_resource *rsrc);
153 php_url *php_ssh2_fopen_wraper_parse_path(const char *path, char *type, php_stream_context *context,
154 											LIBSSH2_SESSION **psession, zend_resource **presource,
155 											LIBSSH2_SFTP **psftp, zend_resource **psftp_rsrc);
156 
157 extern php_stream_ops php_ssh2_channel_stream_ops;
158 
159 extern php_stream_wrapper php_ssh2_stream_wrapper_shell;
160 extern php_stream_wrapper php_ssh2_stream_wrapper_exec;
161 extern php_stream_wrapper php_ssh2_stream_wrapper_tunnel;
162 extern php_stream_wrapper php_ssh2_stream_wrapper_scp;
163 extern php_stream_wrapper php_ssh2_sftp_wrapper;
164 
165 /* Resource list entries */
166 extern int le_ssh2_session;
167 extern int le_ssh2_sftp;
168 
169 #if PHP_VERSION_ID < 70300
170 #define SSH2_URL_STR(a) (a)
171 #define SSH2_URL_LEN(a) strlen(a)
172 #else
173 #define SSH2_URL_STR(a) ZSTR_VAL(a)
174 #define SSH2_URL_LEN(a) ZSTR_LEN(a)
175 #endif
176 
177 #endif	/* PHP_SSH2_H */
178 
179 /*
180  * Local variables:
181  * tab-width: 4
182  * c-basic-offset: 4
183  * indent-tabs-mode: t
184  * End:
185  */
186