1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2016, 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>
52 #include <curl/curl.h>
59 #include "curl_memory.h"
60 /* The last #include file should be: */
64 * Forward declarations.
67 static CURLcode dict_do(struct connectdata *conn, bool *done);
70 * DICT protocol handler.
73 const struct Curl_handler Curl_handler_dict = {
75 ZERO_NULL, /* setup_connection */
78 ZERO_NULL, /* do_more */
79 ZERO_NULL, /* connect_it */
80 ZERO_NULL, /* connecting */
81 ZERO_NULL, /* doing */
82 ZERO_NULL, /* proto_getsock */
83 ZERO_NULL, /* doing_getsock */
84 ZERO_NULL, /* domore_getsock */
85 ZERO_NULL, /* perform_getsock */
86 ZERO_NULL, /* disconnect */
87 ZERO_NULL, /* readwrite */
88 PORT_DICT, /* defport */
89 CURLPROTO_DICT, /* protocol */
90 PROTOPT_NONE | PROTOPT_NOURLQUERY /* flags */
93 static char *unescape_word(struct Curl_easy *data, const char *inputbuff)
102 CURLcode result = Curl_urldecode(data, inputbuff, 0, &newp, &len, FALSE);
106 dictp = malloc(((size_t)len)*2 + 1); /* add one for terminating zero */
108 /* According to RFC2229 section 2.2, these letters need to be escaped with
113 if((ch <= 32) || (ch == 127) ||
114 (ch == '\'') || (ch == '\"') || (ch == '\\')) {
115 dictp[olen++] = '\\';
125 static CURLcode dict_do(struct connectdata *conn, bool *done)
130 char *database = NULL;
131 char *strategy = NULL;
132 char *nthdef = NULL; /* This is not part of the protocol, but required
134 CURLcode result=CURLE_OK;
135 struct Curl_easy *data=conn->data;
136 curl_socket_t sockfd = conn->sock[FIRSTSOCKET];
138 char *path = data->state.path;
139 curl_off_t *bytecount = &data->req.bytecount;
141 *done = TRUE; /* unconditionally */
143 if(conn->bits.user_passwd) {
144 /* AUTH is missing */
147 if(strncasecompare(path, DICT_MATCH, sizeof(DICT_MATCH)-1) ||
148 strncasecompare(path, DICT_MATCH2, sizeof(DICT_MATCH2)-1) ||
149 strncasecompare(path, DICT_MATCH3, sizeof(DICT_MATCH3)-1)) {
151 word = strchr(path, ':');
154 database = strchr(word, ':');
156 *database++ = (char)0;
157 strategy = strchr(database, ':');
159 *strategy++ = (char)0;
160 nthdef = strchr(strategy, ':');
168 if((word == NULL) || (*word == (char)0)) {
169 infof(data, "lookup word is missing\n");
170 word=(char *)"default";
172 if((database == NULL) || (*database == (char)0)) {
173 database = (char *)"!";
175 if((strategy == NULL) || (*strategy == (char)0)) {
176 strategy = (char *)".";
179 eword = unescape_word(data, word);
181 return CURLE_OUT_OF_MEMORY;
183 result = Curl_sendf(sockfd, conn,
184 "CLIENT " LIBCURL_NAME " " LIBCURL_VERSION "\r\n"
199 failf(data, "Failed sending DICT request");
202 Curl_setup_transfer(conn, FIRSTSOCKET, -1, FALSE, bytecount,
203 -1, NULL); /* no upload */
205 else if(strncasecompare(path, DICT_DEFINE, sizeof(DICT_DEFINE)-1) ||
206 strncasecompare(path, DICT_DEFINE2, sizeof(DICT_DEFINE2)-1) ||
207 strncasecompare(path, DICT_DEFINE3, sizeof(DICT_DEFINE3)-1)) {
209 word = strchr(path, ':');
212 database = strchr(word, ':');
214 *database++ = (char)0;
215 nthdef = strchr(database, ':');
222 if((word == NULL) || (*word == (char)0)) {
223 infof(data, "lookup word is missing\n");
224 word=(char *)"default";
226 if((database == NULL) || (*database == (char)0)) {
227 database = (char *)"!";
230 eword = unescape_word(data, word);
232 return CURLE_OUT_OF_MEMORY;
234 result = Curl_sendf(sockfd, conn,
235 "CLIENT " LIBCURL_NAME " " LIBCURL_VERSION "\r\n"
246 failf(data, "Failed sending DICT request");
249 Curl_setup_transfer(conn, FIRSTSOCKET, -1, FALSE, bytecount,
250 -1, NULL); /* no upload */
254 ppath = strchr(path, '/');
259 for(i = 0; ppath[i]; i++) {
263 result = Curl_sendf(sockfd, conn,
264 "CLIENT " LIBCURL_NAME " " LIBCURL_VERSION "\r\n"
268 failf(data, "Failed sending DICT request");
272 Curl_setup_transfer(conn, FIRSTSOCKET, -1, FALSE, bytecount, -1, NULL);
278 #endif /*CURL_DISABLE_DICT*/