1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2014, 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 http://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>
61 #define _MPRINTF_REPLACE /* use our functions only */
62 #include <curl/mprintf.h>
64 #include "curl_memory.h"
65 /* The last #include file should be: */
69 * Forward declarations.
72 static CURLcode dict_do(struct connectdata *conn, bool *done);
75 * DICT protocol handler.
78 const struct Curl_handler Curl_handler_dict = {
80 ZERO_NULL, /* setup_connection */
83 ZERO_NULL, /* do_more */
84 ZERO_NULL, /* connect_it */
85 ZERO_NULL, /* connecting */
86 ZERO_NULL, /* doing */
87 ZERO_NULL, /* proto_getsock */
88 ZERO_NULL, /* doing_getsock */
89 ZERO_NULL, /* domore_getsock */
90 ZERO_NULL, /* perform_getsock */
91 ZERO_NULL, /* disconnect */
92 ZERO_NULL, /* readwrite */
93 PORT_DICT, /* defport */
94 CURLPROTO_DICT, /* protocol */
95 PROTOPT_NONE | PROTOPT_NOURLQUERY /* flags */
98 static char *unescape_word(struct SessionHandle *data, const char *inputbuff)
107 newp = curl_easy_unescape(data, inputbuff, 0, &len);
111 dictp = malloc(((size_t)len)*2 + 1); /* add one for terminating zero */
113 /* According to RFC2229 section 2.2, these letters need to be escaped with
118 if((byte <= 32) || (byte == 127) ||
119 (byte == '\'') || (byte == '\"') || (byte == '\\')) {
120 dictp[olen++] = '\\';
122 dictp[olen++] = byte;
130 static CURLcode dict_do(struct connectdata *conn, bool *done)
135 char *database = NULL;
136 char *strategy = NULL;
137 char *nthdef = NULL; /* This is not part of the protocol, but required
139 CURLcode result=CURLE_OK;
140 struct SessionHandle *data=conn->data;
141 curl_socket_t sockfd = conn->sock[FIRSTSOCKET];
143 char *path = data->state.path;
144 curl_off_t *bytecount = &data->req.bytecount;
146 *done = TRUE; /* unconditionally */
148 if(conn->bits.user_passwd) {
149 /* AUTH is missing */
152 if(Curl_raw_nequal(path, DICT_MATCH, sizeof(DICT_MATCH)-1) ||
153 Curl_raw_nequal(path, DICT_MATCH2, sizeof(DICT_MATCH2)-1) ||
154 Curl_raw_nequal(path, DICT_MATCH3, sizeof(DICT_MATCH3)-1)) {
156 word = strchr(path, ':');
159 database = strchr(word, ':');
161 *database++ = (char)0;
162 strategy = strchr(database, ':');
164 *strategy++ = (char)0;
165 nthdef = strchr(strategy, ':');
173 if((word == NULL) || (*word == (char)0)) {
174 infof(data, "lookup word is missing\n");
175 word=(char *)"default";
177 if((database == NULL) || (*database == (char)0)) {
178 database = (char *)"!";
180 if((strategy == NULL) || (*strategy == (char)0)) {
181 strategy = (char *)".";
184 eword = unescape_word(data, word);
186 return CURLE_OUT_OF_MEMORY;
188 result = Curl_sendf(sockfd, conn,
189 "CLIENT " LIBCURL_NAME " " LIBCURL_VERSION "\r\n"
204 failf(data, "Failed sending DICT request");
207 Curl_setup_transfer(conn, FIRSTSOCKET, -1, FALSE, bytecount,
208 -1, NULL); /* no upload */
210 else if(Curl_raw_nequal(path, DICT_DEFINE, sizeof(DICT_DEFINE)-1) ||
211 Curl_raw_nequal(path, DICT_DEFINE2, sizeof(DICT_DEFINE2)-1) ||
212 Curl_raw_nequal(path, DICT_DEFINE3, sizeof(DICT_DEFINE3)-1)) {
214 word = strchr(path, ':');
217 database = strchr(word, ':');
219 *database++ = (char)0;
220 nthdef = strchr(database, ':');
227 if((word == NULL) || (*word == (char)0)) {
228 infof(data, "lookup word is missing\n");
229 word=(char *)"default";
231 if((database == NULL) || (*database == (char)0)) {
232 database = (char *)"!";
235 eword = unescape_word(data, word);
237 return CURLE_OUT_OF_MEMORY;
239 result = Curl_sendf(sockfd, conn,
240 "CLIENT " LIBCURL_NAME " " LIBCURL_VERSION "\r\n"
251 failf(data, "Failed sending DICT request");
254 Curl_setup_transfer(conn, FIRSTSOCKET, -1, FALSE, bytecount,
255 -1, NULL); /* no upload */
259 ppath = strchr(path, '/');
264 for(i = 0; ppath[i]; i++) {
268 result = Curl_sendf(sockfd, conn,
269 "CLIENT " LIBCURL_NAME " " LIBCURL_VERSION "\r\n"
273 failf(data, "Failed sending DICT request");
277 Curl_setup_transfer(conn, FIRSTSOCKET, -1, FALSE, bytecount, -1, NULL);
283 #endif /*CURL_DISABLE_DICT*/