blob: 4fc85521d770cb8317c14956f20538d6fd0397b6 [file] [log] [blame]
Kristian Monsen5ab50182010-05-14 18:53:44 +01001/***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
Alex Deymo486467e2017-12-19 19:04:07 +01008 * Copyright (C) 1998 - 2017, Daniel Stenberg, <daniel@haxx.se>, et al.
Kristian Monsen5ab50182010-05-14 18:53:44 +01009 *
10 * This software is licensed as described in the file COPYING, which
11 * you should have received as part of this distribution. The terms
Alex Deymod15eaac2016-06-28 14:49:26 -070012 * are also available at https://curl.haxx.se/docs/copyright.html.
Kristian Monsen5ab50182010-05-14 18:53:44 +010013 *
14 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15 * copies of the Software, and permit persons to whom the Software is
16 * furnished to do so, under the terms of the COPYING file.
17 *
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
20 *
21 ***************************************************************************/
22
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -070023#include "curl_setup.h"
Kristian Monsen5ab50182010-05-14 18:53:44 +010024
25#ifndef CURL_DISABLE_DICT
26
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -070027#ifdef HAVE_NETINET_IN_H
Kristian Monsen5ab50182010-05-14 18:53:44 +010028#include <netinet/in.h>
Kristian Monsen5ab50182010-05-14 18:53:44 +010029#endif
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -070030#ifdef HAVE_NETDB_H
Kristian Monsen5ab50182010-05-14 18:53:44 +010031#include <netdb.h>
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -070032#endif
Kristian Monsen5ab50182010-05-14 18:53:44 +010033#ifdef HAVE_ARPA_INET_H
34#include <arpa/inet.h>
35#endif
36#ifdef HAVE_NET_IF_H
37#include <net/if.h>
38#endif
39#ifdef HAVE_SYS_IOCTL_H
40#include <sys/ioctl.h>
41#endif
42
43#ifdef HAVE_SYS_PARAM_H
44#include <sys/param.h>
45#endif
46
47#ifdef HAVE_SYS_SELECT_H
48#include <sys/select.h>
49#endif
50
Kristian Monsen5ab50182010-05-14 18:53:44 +010051#include "urldata.h"
52#include <curl/curl.h>
53#include "transfer.h"
54#include "sendf.h"
Elliott Hughescee03382017-06-23 12:17:18 -070055#include "escape.h"
Kristian Monsen5ab50182010-05-14 18:53:44 +010056#include "progress.h"
Kristian Monsen5ab50182010-05-14 18:53:44 +010057#include "dict.h"
Elliott Hughescee03382017-06-23 12:17:18 -070058#include "strcase.h"
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -070059#include "curl_memory.h"
Kristian Monsen5ab50182010-05-14 18:53:44 +010060/* The last #include file should be: */
61#include "memdebug.h"
62
Kristian Monsen5ab50182010-05-14 18:53:44 +010063/*
64 * Forward declarations.
65 */
66
67static CURLcode dict_do(struct connectdata *conn, bool *done);
68
69/*
70 * DICT protocol handler.
71 */
72
73const struct Curl_handler Curl_handler_dict = {
74 "DICT", /* scheme */
75 ZERO_NULL, /* setup_connection */
76 dict_do, /* do_it */
77 ZERO_NULL, /* done */
78 ZERO_NULL, /* do_more */
79 ZERO_NULL, /* connect_it */
80 ZERO_NULL, /* connecting */
81 ZERO_NULL, /* doing */
82 ZERO_NULL, /* proto_getsock */
83 ZERO_NULL, /* doing_getsock */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -070084 ZERO_NULL, /* domore_getsock */
Kristian Monsen5ab50182010-05-14 18:53:44 +010085 ZERO_NULL, /* perform_getsock */
86 ZERO_NULL, /* disconnect */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -070087 ZERO_NULL, /* readwrite */
Elliott Hughes82be86d2017-09-20 17:00:17 -070088 ZERO_NULL, /* connection_check */
Kristian Monsen5ab50182010-05-14 18:53:44 +010089 PORT_DICT, /* defport */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -070090 CURLPROTO_DICT, /* protocol */
91 PROTOPT_NONE | PROTOPT_NOURLQUERY /* flags */
Kristian Monsen5ab50182010-05-14 18:53:44 +010092};
93
Alex Deymoe3149cc2016-10-05 11:18:42 -070094static char *unescape_word(struct Curl_easy *data, const char *inputbuff)
Kristian Monsen5ab50182010-05-14 18:53:44 +010095{
Elliott Hughes82be86d2017-09-20 17:00:17 -070096 char *newp = NULL;
Kristian Monsen5ab50182010-05-14 18:53:44 +010097 char *dictp;
98 char *ptr;
Elliott Hughescee03382017-06-23 12:17:18 -070099 size_t len;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700100 char ch;
Alex Deymo486467e2017-12-19 19:04:07 +0100101 int olen = 0;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100102
Elliott Hughescee03382017-06-23 12:17:18 -0700103 CURLcode result = Curl_urldecode(data, inputbuff, 0, &newp, &len, FALSE);
104 if(!newp || result)
Kristian Monsen5ab50182010-05-14 18:53:44 +0100105 return NULL;
106
107 dictp = malloc(((size_t)len)*2 + 1); /* add one for terminating zero */
108 if(dictp) {
109 /* According to RFC2229 section 2.2, these letters need to be escaped with
110 \[letter] */
111 for(ptr = newp;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700112 (ch = *ptr) != 0;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100113 ptr++) {
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700114 if((ch <= 32) || (ch == 127) ||
115 (ch == '\'') || (ch == '\"') || (ch == '\\')) {
Kristian Monsen5ab50182010-05-14 18:53:44 +0100116 dictp[olen++] = '\\';
117 }
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700118 dictp[olen++] = ch;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100119 }
Alex Deymo486467e2017-12-19 19:04:07 +0100120 dictp[olen] = 0;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100121 }
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700122 free(newp);
Kristian Monsen5ab50182010-05-14 18:53:44 +0100123 return dictp;
124}
125
126static CURLcode dict_do(struct connectdata *conn, bool *done)
127{
128 char *word;
129 char *eword;
130 char *ppath;
131 char *database = NULL;
132 char *strategy = NULL;
133 char *nthdef = NULL; /* This is not part of the protocol, but required
134 by RFC 2229 */
Alex Deymo486467e2017-12-19 19:04:07 +0100135 CURLcode result = CURLE_OK;
136 struct Curl_easy *data = conn->data;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100137 curl_socket_t sockfd = conn->sock[FIRSTSOCKET];
138
139 char *path = data->state.path;
140 curl_off_t *bytecount = &data->req.bytecount;
141
142 *done = TRUE; /* unconditionally */
143
144 if(conn->bits.user_passwd) {
145 /* AUTH is missing */
146 }
147
Elliott Hughescee03382017-06-23 12:17:18 -0700148 if(strncasecompare(path, DICT_MATCH, sizeof(DICT_MATCH)-1) ||
149 strncasecompare(path, DICT_MATCH2, sizeof(DICT_MATCH2)-1) ||
150 strncasecompare(path, DICT_MATCH3, sizeof(DICT_MATCH3)-1)) {
Kristian Monsen5ab50182010-05-14 18:53:44 +0100151
152 word = strchr(path, ':');
153 if(word) {
154 word++;
155 database = strchr(word, ':');
156 if(database) {
157 *database++ = (char)0;
158 strategy = strchr(database, ':');
159 if(strategy) {
160 *strategy++ = (char)0;
161 nthdef = strchr(strategy, ':');
162 if(nthdef) {
163 *nthdef = (char)0;
164 }
165 }
166 }
167 }
168
169 if((word == NULL) || (*word == (char)0)) {
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700170 infof(data, "lookup word is missing\n");
Alex Deymo486467e2017-12-19 19:04:07 +0100171 word = (char *)"default";
Kristian Monsen5ab50182010-05-14 18:53:44 +0100172 }
173 if((database == NULL) || (*database == (char)0)) {
174 database = (char *)"!";
175 }
176 if((strategy == NULL) || (*strategy == (char)0)) {
177 strategy = (char *)".";
178 }
179
180 eword = unescape_word(data, word);
181 if(!eword)
182 return CURLE_OUT_OF_MEMORY;
183
184 result = Curl_sendf(sockfd, conn,
185 "CLIENT " LIBCURL_NAME " " LIBCURL_VERSION "\r\n"
186 "MATCH "
187 "%s " /* database */
188 "%s " /* strategy */
189 "%s\r\n" /* word */
190 "QUIT\r\n",
191
192 database,
193 strategy,
194 eword
195 );
196
197 free(eword);
198
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700199 if(result) {
Kristian Monsen5ab50182010-05-14 18:53:44 +0100200 failf(data, "Failed sending DICT request");
Kristian Monsen5ab50182010-05-14 18:53:44 +0100201 return result;
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700202 }
203 Curl_setup_transfer(conn, FIRSTSOCKET, -1, FALSE, bytecount,
204 -1, NULL); /* no upload */
Kristian Monsen5ab50182010-05-14 18:53:44 +0100205 }
Elliott Hughescee03382017-06-23 12:17:18 -0700206 else if(strncasecompare(path, DICT_DEFINE, sizeof(DICT_DEFINE)-1) ||
207 strncasecompare(path, DICT_DEFINE2, sizeof(DICT_DEFINE2)-1) ||
208 strncasecompare(path, DICT_DEFINE3, sizeof(DICT_DEFINE3)-1)) {
Kristian Monsen5ab50182010-05-14 18:53:44 +0100209
210 word = strchr(path, ':');
211 if(word) {
212 word++;
213 database = strchr(word, ':');
214 if(database) {
215 *database++ = (char)0;
216 nthdef = strchr(database, ':');
217 if(nthdef) {
218 *nthdef = (char)0;
219 }
220 }
221 }
222
223 if((word == NULL) || (*word == (char)0)) {
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700224 infof(data, "lookup word is missing\n");
Alex Deymo486467e2017-12-19 19:04:07 +0100225 word = (char *)"default";
Kristian Monsen5ab50182010-05-14 18:53:44 +0100226 }
227 if((database == NULL) || (*database == (char)0)) {
228 database = (char *)"!";
229 }
230
231 eword = unescape_word(data, word);
232 if(!eword)
233 return CURLE_OUT_OF_MEMORY;
234
235 result = Curl_sendf(sockfd, conn,
236 "CLIENT " LIBCURL_NAME " " LIBCURL_VERSION "\r\n"
237 "DEFINE "
238 "%s " /* database */
239 "%s\r\n" /* word */
240 "QUIT\r\n",
241 database,
242 eword);
243
244 free(eword);
245
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700246 if(result) {
Kristian Monsen5ab50182010-05-14 18:53:44 +0100247 failf(data, "Failed sending DICT request");
Kristian Monsen5ab50182010-05-14 18:53:44 +0100248 return result;
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700249 }
250 Curl_setup_transfer(conn, FIRSTSOCKET, -1, FALSE, bytecount,
251 -1, NULL); /* no upload */
Kristian Monsen5ab50182010-05-14 18:53:44 +0100252 }
253 else {
254
255 ppath = strchr(path, '/');
256 if(ppath) {
257 int i;
258
259 ppath++;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700260 for(i = 0; ppath[i]; i++) {
Kristian Monsen5ab50182010-05-14 18:53:44 +0100261 if(ppath[i] == ':')
262 ppath[i] = ' ';
263 }
264 result = Curl_sendf(sockfd, conn,
265 "CLIENT " LIBCURL_NAME " " LIBCURL_VERSION "\r\n"
266 "%s\r\n"
267 "QUIT\r\n", ppath);
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700268 if(result) {
Kristian Monsen5ab50182010-05-14 18:53:44 +0100269 failf(data, "Failed sending DICT request");
Kristian Monsen5ab50182010-05-14 18:53:44 +0100270 return result;
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700271 }
272
273 Curl_setup_transfer(conn, FIRSTSOCKET, -1, FALSE, bytecount, -1, NULL);
Kristian Monsen5ab50182010-05-14 18:53:44 +0100274 }
275 }
276
277 return CURLE_OK;
278}
279#endif /*CURL_DISABLE_DICT*/