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