1/***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) 1998 - 2021, Daniel Stenberg, <daniel@haxx.se>, et al.
9 *
10 * This software is licensed as described in the file COPYING, which
11 * you should have received as part of this distribution. The terms
12 * are also available at https://curl.se/docs/copyright.html.
13 *
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
23#include "curl_setup.h"
24
25#ifndef CURL_DISABLE_DICT
26
27#ifdef HAVE_NETINET_IN_H
28#include <netinet/in.h>
29#endif
30#ifdef HAVE_NETDB_H
31#include <netdb.h>
32#endif
33#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#elif defined(HAVE_UNISTD_H)
50#include <unistd.h>
51#endif
52
53#include "urldata.h"
54#include <curl/curl.h>
55#include "transfer.h"
56#include "sendf.h"
57#include "escape.h"
58#include "progress.h"
59#include "dict.h"
60#include "curl_printf.h"
61#include "strcase.h"
62#include "curl_memory.h"
63/* The last #include file should be: */
64#include "memdebug.h"
65
66/*
67 * Forward declarations.
68 */
69
70static CURLcode dict_do(struct Curl_easy *data, bool *done);
71
72/*
73 * DICT protocol handler.
74 */
75
76const struct Curl_handler Curl_handler_dict = {
77 "DICT", /* scheme */
78 ZERO_NULL, /* setup_connection */
79 dict_do, /* do_it */
80 ZERO_NULL, /* done */
81 ZERO_NULL, /* do_more */
82 ZERO_NULL, /* connect_it */
83 ZERO_NULL, /* connecting */
84 ZERO_NULL, /* doing */
85 ZERO_NULL, /* proto_getsock */
86 ZERO_NULL, /* doing_getsock */
87 ZERO_NULL, /* domore_getsock */
88 ZERO_NULL, /* perform_getsock */
89 ZERO_NULL, /* disconnect */
90 ZERO_NULL, /* readwrite */
91 ZERO_NULL, /* connection_check */
92 ZERO_NULL, /* attach connection */
93 PORT_DICT, /* defport */
94 CURLPROTO_DICT, /* protocol */
95 CURLPROTO_DICT, /* family */
96 PROTOPT_NONE | PROTOPT_NOURLQUERY /* flags */
97};
98
99static char *unescape_word(struct Curl_easy *data, const char *inputbuff)
100{
101 char *newp = NULL;
102 char *dictp;
103 size_t len;
104
105 CURLcode result = Curl_urldecode(data, inputbuff, 0, &newp, &len,
106 REJECT_NADA);
107 if(!newp || result)
108 return NULL;
109
110 dictp = malloc(len*2 + 1); /* add one for terminating zero */
111 if(dictp) {
112 char *ptr;
113 char ch;
114 int olen = 0;
115 /* According to RFC2229 section 2.2, these letters need to be escaped with
116 \[letter] */
117 for(ptr = newp;
118 (ch = *ptr) != 0;
119 ptr++) {
120 if((ch <= 32) || (ch == 127) ||
121 (ch == '\'') || (ch == '\"') || (ch == '\\')) {
122 dictp[olen++] = '\\';
123 }
124 dictp[olen++] = ch;
125 }
126 dictp[olen] = 0;
127 }
128 free(newp);
129 return dictp;
130}
131
132/* sendf() sends formatted data to the server */
133static CURLcode sendf(curl_socket_t sockfd, struct Curl_easy *data,
134 const char *fmt, ...)
135{
136 ssize_t bytes_written;
137 size_t write_len;
138 CURLcode result = CURLE_OK;
139 char *s;
140 char *sptr;
141 va_list ap;
142 va_start(ap, fmt);
143 s = vaprintf(fmt, ap); /* returns an allocated string */
144 va_end(ap);
145 if(!s)
146 return CURLE_OUT_OF_MEMORY; /* failure */
147
148 bytes_written = 0;
149 write_len = strlen(s);
150 sptr = s;
151
152 for(;;) {
153 /* Write the buffer to the socket */
154 result = Curl_write(data, sockfd, sptr, write_len, &bytes_written);
155
156 if(result)
157 break;
158
159 Curl_debug(data, CURLINFO_DATA_OUT, sptr, (size_t)bytes_written);
160
161 if((size_t)bytes_written != write_len) {
162 /* if not all was written at once, we must advance the pointer, decrease
163 the size left and try again! */
164 write_len -= bytes_written;
165 sptr += bytes_written;
166 }
167 else
168 break;
169 }
170
171 free(s); /* free the output string */
172
173 return result;
174}
175
176static CURLcode dict_do(struct Curl_easy *data, bool *done)
177{
178 char *word;
179 char *eword;
180 char *ppath;
181 char *database = NULL;
182 char *strategy = NULL;
183 char *nthdef = NULL; /* This is not part of the protocol, but required
184 by RFC 2229 */
185 CURLcode result = CURLE_OK;
186 struct connectdata *conn = data->conn;
187 curl_socket_t sockfd = conn->sock[FIRSTSOCKET];
188
189 char *path = data->state.up.path;
190
191 *done = TRUE; /* unconditionally */
192
193 if(conn->bits.user_passwd) {
194 /* AUTH is missing */
195 }
196
197 if(strncasecompare(path, DICT_MATCH, sizeof(DICT_MATCH)-1) ||
198 strncasecompare(path, DICT_MATCH2, sizeof(DICT_MATCH2)-1) ||
199 strncasecompare(path, DICT_MATCH3, sizeof(DICT_MATCH3)-1)) {
200
201 word = strchr(path, ':');
202 if(word) {
203 word++;
204 database = strchr(word, ':');
205 if(database) {
206 *database++ = (char)0;
207 strategy = strchr(database, ':');
208 if(strategy) {
209 *strategy++ = (char)0;
210 nthdef = strchr(strategy, ':');
211 if(nthdef) {
212 *nthdef = (char)0;
213 }
214 }
215 }
216 }
217
218 if(!word || (*word == (char)0)) {
219 infof(data, "lookup word is missing");
220 word = (char *)"default";
221 }
222 if(!database || (*database == (char)0)) {
223 database = (char *)"!";
224 }
225 if(!strategy || (*strategy == (char)0)) {
226 strategy = (char *)".";
227 }
228
229 eword = unescape_word(data, word);
230 if(!eword)
231 return CURLE_OUT_OF_MEMORY;
232
233 result = sendf(sockfd, data,
234 "CLIENT " LIBCURL_NAME " " LIBCURL_VERSION "\r\n"
235 "MATCH "
236 "%s " /* database */
237 "%s " /* strategy */
238 "%s\r\n" /* word */
239 "QUIT\r\n",
240 database,
241 strategy,
242 eword);
243
244 free(eword);
245
246 if(result) {
247 failf(data, "Failed sending DICT request");
248 return result;
249 }
250 Curl_setup_transfer(data, FIRSTSOCKET, -1, FALSE, -1); /* no upload */
251 }
252 else if(strncasecompare(path, DICT_DEFINE, sizeof(DICT_DEFINE)-1) ||
253 strncasecompare(path, DICT_DEFINE2, sizeof(DICT_DEFINE2)-1) ||
254 strncasecompare(path, DICT_DEFINE3, sizeof(DICT_DEFINE3)-1)) {
255
256 word = strchr(path, ':');
257 if(word) {
258 word++;
259 database = strchr(word, ':');
260 if(database) {
261 *database++ = (char)0;
262 nthdef = strchr(database, ':');
263 if(nthdef) {
264 *nthdef = (char)0;
265 }
266 }
267 }
268
269 if(!word || (*word == (char)0)) {
270 infof(data, "lookup word is missing");
271 word = (char *)"default";
272 }
273 if(!database || (*database == (char)0)) {
274 database = (char *)"!";
275 }
276
277 eword = unescape_word(data, word);
278 if(!eword)
279 return CURLE_OUT_OF_MEMORY;
280
281 result = sendf(sockfd, data,
282 "CLIENT " LIBCURL_NAME " " LIBCURL_VERSION "\r\n"
283 "DEFINE "
284 "%s " /* database */
285 "%s\r\n" /* word */
286 "QUIT\r\n",
287 database,
288 eword);
289
290 free(eword);
291
292 if(result) {
293 failf(data, "Failed sending DICT request");
294 return result;
295 }
296 Curl_setup_transfer(data, FIRSTSOCKET, -1, FALSE, -1);
297 }
298 else {
299
300 ppath = strchr(path, '/');
301 if(ppath) {
302 int i;
303
304 ppath++;
305 for(i = 0; ppath[i]; i++) {
306 if(ppath[i] == ':')
307 ppath[i] = ' ';
308 }
309 result = sendf(sockfd, data,
310 "CLIENT " LIBCURL_NAME " " LIBCURL_VERSION "\r\n"
311 "%s\r\n"
312 "QUIT\r\n", ppath);
313 if(result) {
314 failf(data, "Failed sending DICT request");
315 return result;
316 }
317
318 Curl_setup_transfer(data, FIRSTSOCKET, -1, FALSE, -1);
319 }
320 }
321
322 return CURLE_OK;
323}
324#endif /*CURL_DISABLE_DICT*/
325