1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2015, 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>
60 #include "curl_memory.h"
61 /* The last #include file should be: */
65 * Forward declarations.
68 static CURLcode dict_do(struct connectdata *conn, bool *done);
71 * DICT protocol handler.
74 const struct Curl_handler Curl_handler_dict = {
76 ZERO_NULL, /* setup_connection */
79 ZERO_NULL, /* do_more */
80 ZERO_NULL, /* connect_it */
81 ZERO_NULL, /* connecting */
82 ZERO_NULL, /* doing */
83 ZERO_NULL, /* proto_getsock */
84 ZERO_NULL, /* doing_getsock */
85 ZERO_NULL, /* domore_getsock */
86 ZERO_NULL, /* perform_getsock */
87 ZERO_NULL, /* disconnect */
88 ZERO_NULL, /* readwrite */
89 PORT_DICT, /* defport */
90 CURLPROTO_DICT, /* protocol */
91 PROTOPT_NONE | PROTOPT_NOURLQUERY /* flags */
94 static char *unescape_word(struct SessionHandle *data, const char *inputbuff)
103 newp = curl_easy_unescape(data, inputbuff, 0, &len);
107 dictp = malloc(((size_t)len)*2 + 1); /* add one for terminating zero */
109 /* According to RFC2229 section 2.2, these letters need to be escaped with
114 if((ch <= 32) || (ch == 127) ||
115 (ch == '\'') || (ch == '\"') || (ch == '\\')) {
116 dictp[olen++] = '\\';
126 static CURLcode dict_do(struct connectdata *conn, bool *done)
131 char *database = NULL;
132 char *strategy = NULL;
133 char *nthdef = NULL; /* This is not part of the protocol, but required
135 CURLcode result=CURLE_OK;
136 struct SessionHandle *data=conn->data;
137 curl_socket_t sockfd = conn->sock[FIRSTSOCKET];
139 char *path = data->state.path;
140 curl_off_t *bytecount = &data->req.bytecount;
142 *done = TRUE; /* unconditionally */
144 if(conn->bits.user_passwd) {
145 /* AUTH is missing */
148 if(Curl_raw_nequal(path, DICT_MATCH, sizeof(DICT_MATCH)-1) ||
149 Curl_raw_nequal(path, DICT_MATCH2, sizeof(DICT_MATCH2)-1) ||
150 Curl_raw_nequal(path, DICT_MATCH3, sizeof(DICT_MATCH3)-1)) {
152 word = strchr(path, ':');
155 database = strchr(word, ':');
157 *database++ = (char)0;
158 strategy = strchr(database, ':');
160 *strategy++ = (char)0;
161 nthdef = strchr(strategy, ':');
169 if((word == NULL) || (*word == (char)0)) {
170 infof(data, "lookup word is missing\n");
171 word=(char *)"default";
173 if((database == NULL) || (*database == (char)0)) {
174 database = (char *)"!";
176 if((strategy == NULL) || (*strategy == (char)0)) {
177 strategy = (char *)".";
180 eword = unescape_word(data, word);
182 return CURLE_OUT_OF_MEMORY;
184 result = Curl_sendf(sockfd, conn,
185 "CLIENT " LIBCURL_NAME " " LIBCURL_VERSION "\r\n"
200 failf(data, "Failed sending DICT request");
203 Curl_setup_transfer(conn, FIRSTSOCKET, -1, FALSE, bytecount,
204 -1, NULL); /* no upload */
206 else if(Curl_raw_nequal(path, DICT_DEFINE, sizeof(DICT_DEFINE)-1) ||
207 Curl_raw_nequal(path, DICT_DEFINE2, sizeof(DICT_DEFINE2)-1) ||
208 Curl_raw_nequal(path, DICT_DEFINE3, sizeof(DICT_DEFINE3)-1)) {
210 word = strchr(path, ':');
213 database = strchr(word, ':');
215 *database++ = (char)0;
216 nthdef = strchr(database, ':');
223 if((word == NULL) || (*word == (char)0)) {
224 infof(data, "lookup word is missing\n");
225 word=(char *)"default";
227 if((database == NULL) || (*database == (char)0)) {
228 database = (char *)"!";
231 eword = unescape_word(data, word);
233 return CURLE_OUT_OF_MEMORY;
235 result = Curl_sendf(sockfd, conn,
236 "CLIENT " LIBCURL_NAME " " LIBCURL_VERSION "\r\n"
247 failf(data, "Failed sending DICT request");
250 Curl_setup_transfer(conn, FIRSTSOCKET, -1, FALSE, bytecount,
251 -1, NULL); /* no upload */
255 ppath = strchr(path, '/');
260 for(i = 0; ppath[i]; i++) {
264 result = Curl_sendf(sockfd, conn,
265 "CLIENT " LIBCURL_NAME " " LIBCURL_VERSION "\r\n"
269 failf(data, "Failed sending DICT request");
273 Curl_setup_transfer(conn, FIRSTSOCKET, -1, FALSE, bytecount, -1, NULL);
279 #endif /*CURL_DISABLE_DICT*/