1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2020, 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.haxx.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 connectdata *conn, 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 PORT_DICT, /* defport */
93 CURLPROTO_DICT, /* protocol */
94 CURLPROTO_DICT, /* family */
95 PROTOPT_NONE | PROTOPT_NOURLQUERY /* flags */
98 static char *unescape_word(struct Curl_easy *data, const char *inputbuff)
104 CURLcode result = Curl_urldecode(data, inputbuff, 0, &newp, &len,
109 dictp = malloc(len*2 + 1); /* add one for terminating zero */
114 /* According to RFC2229 section 2.2, these letters need to be escaped with
119 if((ch <= 32) || (ch == 127) ||
120 (ch == '\'') || (ch == '\"') || (ch == '\\')) {
121 dictp[olen++] = '\\';
131 /* sendf() sends formatted data to the server */
132 static CURLcode sendf(curl_socket_t sockfd, struct connectdata *conn,
133 const char *fmt, ...)
135 struct Curl_easy *data = conn->data;
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(conn, sockfd, sptr, write_len, &bytes_written);
159 if(data->set.verbose)
160 Curl_debug(data, CURLINFO_DATA_OUT, sptr, (size_t)bytes_written);
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;
172 free(s); /* free the output string */
177 static CURLcode dict_do(struct connectdata *conn, bool *done)
182 char *database = NULL;
183 char *strategy = NULL;
184 char *nthdef = NULL; /* This is not part of the protocol, but required
186 CURLcode result = CURLE_OK;
187 struct Curl_easy *data = conn->data;
188 curl_socket_t sockfd = conn->sock[FIRSTSOCKET];
190 char *path = data->state.up.path;
192 *done = TRUE; /* unconditionally */
194 if(conn->bits.user_passwd) {
195 /* AUTH is missing */
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)) {
202 word = strchr(path, ':');
205 database = strchr(word, ':');
207 *database++ = (char)0;
208 strategy = strchr(database, ':');
210 *strategy++ = (char)0;
211 nthdef = strchr(strategy, ':');
219 if((word == NULL) || (*word == (char)0)) {
220 infof(data, "lookup word is missing\n");
221 word = (char *)"default";
223 if((database == NULL) || (*database == (char)0)) {
224 database = (char *)"!";
226 if((strategy == NULL) || (*strategy == (char)0)) {
227 strategy = (char *)".";
230 eword = unescape_word(data, word);
232 return CURLE_OUT_OF_MEMORY;
234 result = sendf(sockfd, conn,
235 "CLIENT " LIBCURL_NAME " " LIBCURL_VERSION "\r\n"
248 failf(data, "Failed sending DICT request");
251 Curl_setup_transfer(data, FIRSTSOCKET, -1, FALSE, -1); /* no upload */
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)) {
257 word = strchr(path, ':');
260 database = strchr(word, ':');
262 *database++ = (char)0;
263 nthdef = strchr(database, ':');
270 if((word == NULL) || (*word == (char)0)) {
271 infof(data, "lookup word is missing\n");
272 word = (char *)"default";
274 if((database == NULL) || (*database == (char)0)) {
275 database = (char *)"!";
278 eword = unescape_word(data, word);
280 return CURLE_OUT_OF_MEMORY;
282 result = sendf(sockfd, conn,
283 "CLIENT " LIBCURL_NAME " " LIBCURL_VERSION "\r\n"
294 failf(data, "Failed sending DICT request");
297 Curl_setup_transfer(data, FIRSTSOCKET, -1, FALSE, -1);
301 ppath = strchr(path, '/');
306 for(i = 0; ppath[i]; i++) {
310 result = sendf(sockfd, conn,
311 "CLIENT " LIBCURL_NAME " " LIBCURL_VERSION "\r\n"
315 failf(data, "Failed sending DICT request");
319 Curl_setup_transfer(data, FIRSTSOCKET, -1, FALSE, -1);
325 #endif /*CURL_DISABLE_DICT*/