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