1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2022, Daniel Stenberg, <daniel@haxx.se>, et al.
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.
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.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ***************************************************************************/
23 #include "curl_setup.h"
25 #ifndef CURL_DISABLE_DICT
27 #ifdef HAVE_NETINET_IN_H
28 #include <netinet/in.h>
33 #ifdef HAVE_ARPA_INET_H
34 #include <arpa/inet.h>
39 #ifdef HAVE_SYS_IOCTL_H
40 #include <sys/ioctl.h>
43 #ifdef HAVE_SYS_PARAM_H
44 #include <sys/param.h>
47 #ifdef HAVE_SYS_SELECT_H
48 #include <sys/select.h>
49 #elif defined(HAVE_UNISTD_H)
54 #include <curl/curl.h>
60 #include "curl_printf.h"
62 #include "curl_memory.h"
63 /* The last #include file should be: */
67 * Forward declarations.
70 static CURLcode dict_do(struct Curl_easy *data, bool *done);
73 * DICT protocol handler.
76 const struct Curl_handler Curl_handler_dict = {
78 ZERO_NULL, /* setup_connection */
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 ZERO_NULL, /* attach connection */
93 PORT_DICT, /* defport */
94 CURLPROTO_DICT, /* protocol */
95 CURLPROTO_DICT, /* family */
96 PROTOPT_NONE | PROTOPT_NOURLQUERY /* flags */
99 static char *unescape_word(const char *inputbuff)
105 CURLcode result = Curl_urldecode(inputbuff, 0, &newp, &len,
110 dictp = malloc(len*2 + 1); /* add one for terminating zero */
115 /* According to RFC2229 section 2.2, these letters need to be escaped with
120 if((ch <= 32) || (ch == 127) ||
121 (ch == '\'') || (ch == '\"') || (ch == '\\')) {
122 dictp[olen++] = '\\';
132 /* sendf() sends formatted data to the server */
133 static CURLcode sendf(curl_socket_t sockfd, struct Curl_easy *data,
134 const char *fmt, ...)
136 ssize_t bytes_written;
138 CURLcode result = CURLE_OK;
143 s = vaprintf(fmt, ap); /* returns an allocated string */
146 return CURLE_OUT_OF_MEMORY; /* failure */
149 write_len = strlen(s);
153 /* Write the buffer to the socket */
154 result = Curl_write(data, sockfd, sptr, write_len, &bytes_written);
159 Curl_debug(data, CURLINFO_DATA_OUT, sptr, (size_t)bytes_written);
161 if((size_t)bytes_written != write_len) {
162 /* if not all was written at once, we must advance the pointer, decrease
163 the size left and try again! */
164 write_len -= bytes_written;
165 sptr += bytes_written;
171 free(s); /* free the output string */
176 static CURLcode dict_do(struct Curl_easy *data, bool *done)
181 char *database = NULL;
182 char *strategy = NULL;
183 char *nthdef = NULL; /* This is not part of the protocol, but required
185 CURLcode result = CURLE_OK;
186 struct connectdata *conn = data->conn;
187 curl_socket_t sockfd = conn->sock[FIRSTSOCKET];
189 char *path = data->state.up.path;
191 *done = TRUE; /* unconditionally */
193 if(strncasecompare(path, DICT_MATCH, sizeof(DICT_MATCH)-1) ||
194 strncasecompare(path, DICT_MATCH2, sizeof(DICT_MATCH2)-1) ||
195 strncasecompare(path, DICT_MATCH3, sizeof(DICT_MATCH3)-1)) {
197 word = strchr(path, ':');
200 database = strchr(word, ':');
202 *database++ = (char)0;
203 strategy = strchr(database, ':');
205 *strategy++ = (char)0;
206 nthdef = strchr(strategy, ':');
214 if(!word || (*word == (char)0)) {
215 infof(data, "lookup word is missing");
216 word = (char *)"default";
218 if(!database || (*database == (char)0)) {
219 database = (char *)"!";
221 if(!strategy || (*strategy == (char)0)) {
222 strategy = (char *)".";
225 eword = unescape_word(word);
227 return CURLE_OUT_OF_MEMORY;
229 result = sendf(sockfd, data,
230 "CLIENT " LIBCURL_NAME " " LIBCURL_VERSION "\r\n"
243 failf(data, "Failed sending DICT request");
246 Curl_setup_transfer(data, FIRSTSOCKET, -1, FALSE, -1); /* no upload */
248 else if(strncasecompare(path, DICT_DEFINE, sizeof(DICT_DEFINE)-1) ||
249 strncasecompare(path, DICT_DEFINE2, sizeof(DICT_DEFINE2)-1) ||
250 strncasecompare(path, DICT_DEFINE3, sizeof(DICT_DEFINE3)-1)) {
252 word = strchr(path, ':');
255 database = strchr(word, ':');
257 *database++ = (char)0;
258 nthdef = strchr(database, ':');
265 if(!word || (*word == (char)0)) {
266 infof(data, "lookup word is missing");
267 word = (char *)"default";
269 if(!database || (*database == (char)0)) {
270 database = (char *)"!";
273 eword = unescape_word(word);
275 return CURLE_OUT_OF_MEMORY;
277 result = sendf(sockfd, data,
278 "CLIENT " LIBCURL_NAME " " LIBCURL_VERSION "\r\n"
289 failf(data, "Failed sending DICT request");
292 Curl_setup_transfer(data, FIRSTSOCKET, -1, FALSE, -1);
296 ppath = strchr(path, '/');
301 for(i = 0; ppath[i]; i++) {
305 result = sendf(sockfd, data,
306 "CLIENT " LIBCURL_NAME " " LIBCURL_VERSION "\r\n"
310 failf(data, "Failed sending DICT request");
314 Curl_setup_transfer(data, FIRSTSOCKET, -1, FALSE, -1);
320 #endif /*CURL_DISABLE_DICT*/