1 | /*************************************************************************** |
2 | * _ _ ____ _ |
3 | * Project ___| | | | _ \| | |
4 | * / __| | | | |_) | | |
5 | * | (__| |_| | _ <| |___ |
6 | * \___|\___/|_| \_\_____| |
7 | * |
8 | * Copyright (C) 1998 - 2022, 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, /* readwrite */ |
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 | static char *unescape_word(const char *inputbuff) |
102 | { |
103 | char *newp = NULL; |
104 | char *dictp; |
105 | size_t len; |
106 | |
107 | CURLcode result = Curl_urldecode(inputbuff, 0, &newp, &len, |
108 | REJECT_NADA); |
109 | if(!newp || result) |
110 | return NULL; |
111 | |
112 | dictp = malloc(len*2 + 1); /* add one for terminating zero */ |
113 | if(dictp) { |
114 | char *ptr; |
115 | char ch; |
116 | int olen = 0; |
117 | /* According to RFC2229 section 2.2, these letters need to be escaped with |
118 | \[letter] */ |
119 | for(ptr = newp; |
120 | (ch = *ptr) != 0; |
121 | ptr++) { |
122 | if((ch <= 32) || (ch == 127) || |
123 | (ch == '\'') || (ch == '\"') || (ch == '\\')) { |
124 | dictp[olen++] = '\\'; |
125 | } |
126 | dictp[olen++] = ch; |
127 | } |
128 | dictp[olen] = 0; |
129 | } |
130 | free(newp); |
131 | return dictp; |
132 | } |
133 | |
134 | /* sendf() sends formatted data to the server */ |
135 | static CURLcode sendf(curl_socket_t sockfd, struct Curl_easy *data, |
136 | const char *fmt, ...) |
137 | { |
138 | ssize_t bytes_written; |
139 | size_t write_len; |
140 | CURLcode result = CURLE_OK; |
141 | char *s; |
142 | char *sptr; |
143 | va_list ap; |
144 | va_start(ap, fmt); |
145 | s = vaprintf(fmt, ap); /* returns an allocated string */ |
146 | va_end(ap); |
147 | if(!s) |
148 | return CURLE_OUT_OF_MEMORY; /* failure */ |
149 | |
150 | bytes_written = 0; |
151 | write_len = strlen(s); |
152 | sptr = s; |
153 | |
154 | for(;;) { |
155 | /* Write the buffer to the socket */ |
156 | result = Curl_write(data, sockfd, sptr, write_len, &bytes_written); |
157 | |
158 | if(result) |
159 | break; |
160 | |
161 | Curl_debug(data, CURLINFO_DATA_OUT, sptr, (size_t)bytes_written); |
162 | |
163 | if((size_t)bytes_written != write_len) { |
164 | /* if not all was written at once, we must advance the pointer, decrease |
165 | the size left and try again! */ |
166 | write_len -= bytes_written; |
167 | sptr += bytes_written; |
168 | } |
169 | else |
170 | break; |
171 | } |
172 | |
173 | free(s); /* free the output string */ |
174 | |
175 | return result; |
176 | } |
177 | |
178 | static CURLcode dict_do(struct Curl_easy *data, bool *done) |
179 | { |
180 | char *word; |
181 | char *eword; |
182 | char *ppath; |
183 | char *database = NULL; |
184 | char *strategy = NULL; |
185 | char *nthdef = NULL; /* This is not part of the protocol, but required |
186 | by RFC 2229 */ |
187 | CURLcode result = CURLE_OK; |
188 | struct connectdata *conn = data->conn; |
189 | curl_socket_t sockfd = conn->sock[FIRSTSOCKET]; |
190 | |
191 | char *path = data->state.up.path; |
192 | |
193 | *done = TRUE; /* unconditionally */ |
194 | |
195 | if(strncasecompare(path, DICT_MATCH, sizeof(DICT_MATCH)-1) || |
196 | strncasecompare(path, DICT_MATCH2, sizeof(DICT_MATCH2)-1) || |
197 | strncasecompare(path, DICT_MATCH3, sizeof(DICT_MATCH3)-1)) { |
198 | |
199 | word = strchr(path, ':'); |
200 | if(word) { |
201 | word++; |
202 | database = strchr(word, ':'); |
203 | if(database) { |
204 | *database++ = (char)0; |
205 | strategy = strchr(database, ':'); |
206 | if(strategy) { |
207 | *strategy++ = (char)0; |
208 | nthdef = strchr(strategy, ':'); |
209 | if(nthdef) { |
210 | *nthdef = (char)0; |
211 | } |
212 | } |
213 | } |
214 | } |
215 | |
216 | if(!word || (*word == (char)0)) { |
217 | infof(data, "lookup word is missing" ); |
218 | word = (char *)"default" ; |
219 | } |
220 | if(!database || (*database == (char)0)) { |
221 | database = (char *)"!" ; |
222 | } |
223 | if(!strategy || (*strategy == (char)0)) { |
224 | strategy = (char *)"." ; |
225 | } |
226 | |
227 | eword = unescape_word(word); |
228 | if(!eword) |
229 | return CURLE_OUT_OF_MEMORY; |
230 | |
231 | result = sendf(sockfd, data, |
232 | "CLIENT " LIBCURL_NAME " " LIBCURL_VERSION "\r\n" |
233 | "MATCH " |
234 | "%s " /* database */ |
235 | "%s " /* strategy */ |
236 | "%s\r\n" /* word */ |
237 | "QUIT\r\n" , |
238 | database, |
239 | strategy, |
240 | eword); |
241 | |
242 | free(eword); |
243 | |
244 | if(result) { |
245 | failf(data, "Failed sending DICT request" ); |
246 | return result; |
247 | } |
248 | Curl_setup_transfer(data, FIRSTSOCKET, -1, FALSE, -1); /* no upload */ |
249 | } |
250 | else if(strncasecompare(path, DICT_DEFINE, sizeof(DICT_DEFINE)-1) || |
251 | strncasecompare(path, DICT_DEFINE2, sizeof(DICT_DEFINE2)-1) || |
252 | strncasecompare(path, DICT_DEFINE3, sizeof(DICT_DEFINE3)-1)) { |
253 | |
254 | word = strchr(path, ':'); |
255 | if(word) { |
256 | word++; |
257 | database = strchr(word, ':'); |
258 | if(database) { |
259 | *database++ = (char)0; |
260 | nthdef = strchr(database, ':'); |
261 | if(nthdef) { |
262 | *nthdef = (char)0; |
263 | } |
264 | } |
265 | } |
266 | |
267 | if(!word || (*word == (char)0)) { |
268 | infof(data, "lookup word is missing" ); |
269 | word = (char *)"default" ; |
270 | } |
271 | if(!database || (*database == (char)0)) { |
272 | database = (char *)"!" ; |
273 | } |
274 | |
275 | eword = unescape_word(word); |
276 | if(!eword) |
277 | return CURLE_OUT_OF_MEMORY; |
278 | |
279 | result = sendf(sockfd, data, |
280 | "CLIENT " LIBCURL_NAME " " LIBCURL_VERSION "\r\n" |
281 | "DEFINE " |
282 | "%s " /* database */ |
283 | "%s\r\n" /* word */ |
284 | "QUIT\r\n" , |
285 | database, |
286 | eword); |
287 | |
288 | free(eword); |
289 | |
290 | if(result) { |
291 | failf(data, "Failed sending DICT request" ); |
292 | return result; |
293 | } |
294 | Curl_setup_transfer(data, FIRSTSOCKET, -1, FALSE, -1); |
295 | } |
296 | else { |
297 | |
298 | ppath = strchr(path, '/'); |
299 | if(ppath) { |
300 | int i; |
301 | |
302 | ppath++; |
303 | for(i = 0; ppath[i]; i++) { |
304 | if(ppath[i] == ':') |
305 | ppath[i] = ' '; |
306 | } |
307 | result = sendf(sockfd, data, |
308 | "CLIENT " LIBCURL_NAME " " LIBCURL_VERSION "\r\n" |
309 | "%s\r\n" |
310 | "QUIT\r\n" , ppath); |
311 | if(result) { |
312 | failf(data, "Failed sending DICT request" ); |
313 | return result; |
314 | } |
315 | |
316 | Curl_setup_transfer(data, FIRSTSOCKET, -1, FALSE, -1); |
317 | } |
318 | } |
319 | |
320 | return CURLE_OK; |
321 | } |
322 | #endif /*CURL_DISABLE_DICT*/ |
323 | |