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