1 /***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) 1998 - 2020, 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.haxx.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 connectdata *conn, 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 connectdata * conn,const char * fmt,...)132 static CURLcode sendf(curl_socket_t sockfd, struct connectdata *conn,
133 const char *fmt, ...)
134 {
135 struct Curl_easy *data = conn->data;
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(conn, sockfd, sptr, write_len, &bytes_written);
155
156 if(result)
157 break;
158
159 if(data->set.verbose)
160 Curl_debug(data, CURLINFO_DATA_OUT, sptr, (size_t)bytes_written);
161
162 if((size_t)bytes_written != write_len) {
163 /* if not all was written at once, we must advance the pointer, decrease
164 the size left and try again! */
165 write_len -= bytes_written;
166 sptr += bytes_written;
167 }
168 else
169 break;
170 }
171
172 free(s); /* free the output string */
173
174 return result;
175 }
176
dict_do(struct connectdata * conn,bool * done)177 static CURLcode dict_do(struct connectdata *conn, bool *done)
178 {
179 char *word;
180 char *eword;
181 char *ppath;
182 char *database = NULL;
183 char *strategy = NULL;
184 char *nthdef = NULL; /* This is not part of the protocol, but required
185 by RFC 2229 */
186 CURLcode result = CURLE_OK;
187 struct Curl_easy *data = conn->data;
188 curl_socket_t sockfd = conn->sock[FIRSTSOCKET];
189
190 char *path = data->state.up.path;
191
192 *done = TRUE; /* unconditionally */
193
194 if(conn->bits.user_passwd) {
195 /* AUTH is missing */
196 }
197
198 if(strncasecompare(path, DICT_MATCH, sizeof(DICT_MATCH)-1) ||
199 strncasecompare(path, DICT_MATCH2, sizeof(DICT_MATCH2)-1) ||
200 strncasecompare(path, DICT_MATCH3, sizeof(DICT_MATCH3)-1)) {
201
202 word = strchr(path, ':');
203 if(word) {
204 word++;
205 database = strchr(word, ':');
206 if(database) {
207 *database++ = (char)0;
208 strategy = strchr(database, ':');
209 if(strategy) {
210 *strategy++ = (char)0;
211 nthdef = strchr(strategy, ':');
212 if(nthdef) {
213 *nthdef = (char)0;
214 }
215 }
216 }
217 }
218
219 if((word == NULL) || (*word == (char)0)) {
220 infof(data, "lookup word is missing\n");
221 word = (char *)"default";
222 }
223 if((database == NULL) || (*database == (char)0)) {
224 database = (char *)"!";
225 }
226 if((strategy == NULL) || (*strategy == (char)0)) {
227 strategy = (char *)".";
228 }
229
230 eword = unescape_word(data, word);
231 if(!eword)
232 return CURLE_OUT_OF_MEMORY;
233
234 result = sendf(sockfd, conn,
235 "CLIENT " LIBCURL_NAME " " LIBCURL_VERSION "\r\n"
236 "MATCH "
237 "%s " /* database */
238 "%s " /* strategy */
239 "%s\r\n" /* word */
240 "QUIT\r\n",
241 database,
242 strategy,
243 eword);
244
245 free(eword);
246
247 if(result) {
248 failf(data, "Failed sending DICT request");
249 return result;
250 }
251 Curl_setup_transfer(data, FIRSTSOCKET, -1, FALSE, -1); /* no upload */
252 }
253 else if(strncasecompare(path, DICT_DEFINE, sizeof(DICT_DEFINE)-1) ||
254 strncasecompare(path, DICT_DEFINE2, sizeof(DICT_DEFINE2)-1) ||
255 strncasecompare(path, DICT_DEFINE3, sizeof(DICT_DEFINE3)-1)) {
256
257 word = strchr(path, ':');
258 if(word) {
259 word++;
260 database = strchr(word, ':');
261 if(database) {
262 *database++ = (char)0;
263 nthdef = strchr(database, ':');
264 if(nthdef) {
265 *nthdef = (char)0;
266 }
267 }
268 }
269
270 if((word == NULL) || (*word == (char)0)) {
271 infof(data, "lookup word is missing\n");
272 word = (char *)"default";
273 }
274 if((database == NULL) || (*database == (char)0)) {
275 database = (char *)"!";
276 }
277
278 eword = unescape_word(data, word);
279 if(!eword)
280 return CURLE_OUT_OF_MEMORY;
281
282 result = sendf(sockfd, conn,
283 "CLIENT " LIBCURL_NAME " " LIBCURL_VERSION "\r\n"
284 "DEFINE "
285 "%s " /* database */
286 "%s\r\n" /* word */
287 "QUIT\r\n",
288 database,
289 eword);
290
291 free(eword);
292
293 if(result) {
294 failf(data, "Failed sending DICT request");
295 return result;
296 }
297 Curl_setup_transfer(data, FIRSTSOCKET, -1, FALSE, -1);
298 }
299 else {
300
301 ppath = strchr(path, '/');
302 if(ppath) {
303 int i;
304
305 ppath++;
306 for(i = 0; ppath[i]; i++) {
307 if(ppath[i] == ':')
308 ppath[i] = ' ';
309 }
310 result = sendf(sockfd, conn,
311 "CLIENT " LIBCURL_NAME " " LIBCURL_VERSION "\r\n"
312 "%s\r\n"
313 "QUIT\r\n", ppath);
314 if(result) {
315 failf(data, "Failed sending DICT request");
316 return result;
317 }
318
319 Curl_setup_transfer(data, FIRSTSOCKET, -1, FALSE, -1);
320 }
321 }
322
323 return CURLE_OK;
324 }
325 #endif /*CURL_DISABLE_DICT*/
326