1 /*
2 +----------------------------------------------------------------------+
3 | Copyright (c) The PHP Group |
4 +----------------------------------------------------------------------+
5 | This source file is subject to version 3.01 of the PHP license, |
6 | that is bundled with this package in the file LICENSE, and is |
7 | available through the world-wide-web at the following url: |
8 | http://www.php.net/license/3_01.txt |
9 | If you did not receive a copy of the PHP license and are unable to |
10 | obtain it through the world-wide-web, please send a note to |
11 | license@php.net so we can mail you a copy immediately. |
12 +----------------------------------------------------------------------+
13 | Authors: Andrey Hristov <andrey@php.net> |
14 | Ulf Wendel <uw@php.net> |
15 | Georg Richter <georg@php.net> |
16 +----------------------------------------------------------------------+
17 */
18 #include "php.h"
19 #include "mysqlnd.h"
20 #include "mysqlnd_wireprotocol.h"
21 #include "mysqlnd_priv.h"
22 #include "mysqlnd_debug.h"
23
24 /* {{{ mysqlnd_local_infile_init */
25 static
mysqlnd_local_infile_init(void ** ptr,const char * const filename)26 int mysqlnd_local_infile_init(void ** ptr, const char * const filename)
27 {
28 MYSQLND_INFILE_INFO *info;
29 php_stream_context *context = NULL;
30
31 DBG_ENTER("mysqlnd_local_infile_init");
32
33 info = ((MYSQLND_INFILE_INFO *)mnd_ecalloc(1, sizeof(MYSQLND_INFILE_INFO)));
34 if (!info) {
35 DBG_RETURN(1);
36 }
37
38 *ptr = info;
39
40 /* check open_basedir */
41 if (PG(open_basedir)) {
42 if (php_check_open_basedir_ex(filename, 0) == -1) {
43 strcpy(info->error_msg, "open_basedir restriction in effect. Unable to open file");
44 info->error_no = CR_UNKNOWN_ERROR;
45 DBG_RETURN(1);
46 }
47 }
48
49 info->filename = filename;
50 info->fd = php_stream_open_wrapper_ex((char *)filename, "r", 0, NULL, context);
51
52 if (info->fd == NULL) {
53 snprintf((char *)info->error_msg, sizeof(info->error_msg), "Can't find file '%-.64s'.", filename);
54 info->error_no = MYSQLND_EE_FILENOTFOUND;
55 DBG_RETURN(1);
56 }
57
58 DBG_RETURN(0);
59 }
60 /* }}} */
61
62
63 /* {{{ mysqlnd_local_infile_read */
64 static
mysqlnd_local_infile_read(void * ptr,zend_uchar * buf,unsigned int buf_len)65 int mysqlnd_local_infile_read(void * ptr, zend_uchar * buf, unsigned int buf_len)
66 {
67 MYSQLND_INFILE_INFO *info = (MYSQLND_INFILE_INFO *)ptr;
68 int count;
69
70 DBG_ENTER("mysqlnd_local_infile_read");
71
72 count = (int) php_stream_read(info->fd, (char *) buf, buf_len);
73
74 if (count < 0) {
75 strcpy(info->error_msg, "Error reading file");
76 info->error_no = CR_UNKNOWN_ERROR;
77 }
78
79 DBG_RETURN(count);
80 }
81 /* }}} */
82
83
84 /* {{{ mysqlnd_local_infile_error */
85 static
mysqlnd_local_infile_error(void * ptr,char * error_buf,unsigned int error_buf_len)86 int mysqlnd_local_infile_error(void * ptr, char *error_buf, unsigned int error_buf_len)
87 {
88 MYSQLND_INFILE_INFO *info = (MYSQLND_INFILE_INFO *)ptr;
89
90 DBG_ENTER("mysqlnd_local_infile_error");
91
92 if (info) {
93 strlcpy(error_buf, info->error_msg, error_buf_len);
94 DBG_INF_FMT("have info, %d", info->error_no);
95 DBG_RETURN(info->error_no);
96 }
97
98 strlcpy(error_buf, "Unknown error", error_buf_len);
99 DBG_INF_FMT("no info, %d", CR_UNKNOWN_ERROR);
100 DBG_RETURN(CR_UNKNOWN_ERROR);
101 }
102 /* }}} */
103
104
105 /* {{{ mysqlnd_local_infile_end */
106 static
mysqlnd_local_infile_end(void * ptr)107 void mysqlnd_local_infile_end(void * ptr)
108 {
109 MYSQLND_INFILE_INFO *info = (MYSQLND_INFILE_INFO *)ptr;
110
111 if (info) {
112 /* php_stream_close segfaults on NULL */
113 if (info->fd) {
114 php_stream_close(info->fd);
115 info->fd = NULL;
116 }
117 mnd_efree(info);
118 }
119 }
120 /* }}} */
121
122
123 /* {{{ mysqlnd_local_infile_default */
124 PHPAPI void
mysqlnd_local_infile_default(MYSQLND_CONN_DATA * conn)125 mysqlnd_local_infile_default(MYSQLND_CONN_DATA * conn)
126 {
127 conn->infile.local_infile_init = mysqlnd_local_infile_init;
128 conn->infile.local_infile_read = mysqlnd_local_infile_read;
129 conn->infile.local_infile_error = mysqlnd_local_infile_error;
130 conn->infile.local_infile_end = mysqlnd_local_infile_end;
131 }
132 /* }}} */
133
134
135 static const char *lost_conn = "Lost connection to MySQL server during LOAD DATA of a local file";
136
137
138 /* {{{ mysqlnd_handle_local_infile */
139 enum_func_status
mysqlnd_handle_local_infile(MYSQLND_CONN_DATA * conn,const char * const filename,zend_bool * is_warning)140 mysqlnd_handle_local_infile(MYSQLND_CONN_DATA * conn, const char * const filename, zend_bool * is_warning)
141 {
142 zend_uchar *buf = NULL;
143 zend_uchar empty_packet[MYSQLND_HEADER_SIZE];
144 enum_func_status result = FAIL;
145 unsigned int buflen = 4096;
146 void *info = NULL;
147 int bufsize;
148 size_t ret;
149 MYSQLND_INFILE infile;
150 MYSQLND_PFC * net = conn->protocol_frame_codec;
151 MYSQLND_VIO * vio = conn->vio;
152
153 DBG_ENTER("mysqlnd_handle_local_infile");
154
155 if (!(conn->options->flags & CLIENT_LOCAL_FILES)) {
156 php_error_docref(NULL, E_WARNING, "LOAD DATA LOCAL INFILE forbidden");
157 SET_CLIENT_ERROR(conn->error_info, CR_UNKNOWN_ERROR, UNKNOWN_SQLSTATE,
158 "LOAD DATA LOCAL INFILE is forbidden, check mysqli.allow_local_infile");
159 /* write empty packet to server */
160 ret = net->data->m.send(net, vio, empty_packet, 0, conn->stats, conn->error_info);
161 *is_warning = TRUE;
162 goto infile_error;
163 }
164
165 infile = conn->infile;
166 /* allocate buffer for reading data */
167 buf = (zend_uchar *) mnd_ecalloc(1, buflen);
168
169 *is_warning = FALSE;
170
171 /* init handler: allocate read buffer and open file */
172 if (infile.local_infile_init(&info, (char *)filename)) {
173 char tmp_buf[sizeof(conn->error_info->error)];
174 int tmp_error_no;
175 *is_warning = TRUE;
176 /* error occurred */
177 tmp_error_no = infile.local_infile_error(info, tmp_buf, sizeof(tmp_buf));
178 SET_CLIENT_ERROR(conn->error_info, tmp_error_no, UNKNOWN_SQLSTATE, tmp_buf);
179 /* write empty packet to server */
180 ret = net->data->m.send(net, vio, empty_packet, 0, conn->stats, conn->error_info);
181 goto infile_error;
182 }
183
184 /* read data */
185 while ((bufsize = infile.local_infile_read (info, buf + MYSQLND_HEADER_SIZE, buflen - MYSQLND_HEADER_SIZE)) > 0) {
186 if ((ret = net->data->m.send(net, vio, buf, bufsize, conn->stats, conn->error_info)) == 0) {
187 DBG_ERR_FMT("Error during read : %d %s %s", CR_SERVER_LOST, UNKNOWN_SQLSTATE, lost_conn);
188 SET_CLIENT_ERROR(conn->error_info, CR_SERVER_LOST, UNKNOWN_SQLSTATE, lost_conn);
189 goto infile_error;
190 }
191 }
192
193 /* send empty packet for eof */
194 if ((ret = net->data->m.send(net, vio, empty_packet, 0, conn->stats, conn->error_info)) == 0) {
195 SET_CLIENT_ERROR(conn->error_info, CR_SERVER_LOST, UNKNOWN_SQLSTATE, lost_conn);
196 goto infile_error;
197 }
198
199 /* error during read occurred */
200 if (bufsize < 0) {
201 char tmp_buf[sizeof(conn->error_info->error)];
202 int tmp_error_no;
203 *is_warning = TRUE;
204 DBG_ERR_FMT("Bufsize < 0, warning, %d %s %s", CR_SERVER_LOST, UNKNOWN_SQLSTATE, lost_conn);
205 tmp_error_no = infile.local_infile_error(info, tmp_buf, sizeof(tmp_buf));
206 SET_CLIENT_ERROR(conn->error_info, tmp_error_no, UNKNOWN_SQLSTATE, tmp_buf);
207 goto infile_error;
208 }
209
210 result = PASS;
211
212 infile_error:
213 /* get response from server and update upsert values */
214 if (FAIL == conn->payload_decoder_factory->m.send_command_handle_response(
215 conn->payload_decoder_factory,
216 PROT_OK_PACKET, FALSE, COM_QUERY, FALSE,
217 conn->error_info,
218 conn->upsert_status,
219 &conn->last_message)) {
220 result = FAIL;
221 }
222
223 (*conn->infile.local_infile_end)(info);
224 if (buf) {
225 mnd_efree(buf);
226 }
227 DBG_INF_FMT("%s", result == PASS? "PASS":"FAIL");
228 DBG_RETURN(result);
229 }
230 /* }}} */
231