1 /***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) 1998 - 2011, Daniel Stenberg, <daniel@haxx.se>, et al.
9 *
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.
13 *
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.
17 *
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
20 *
21 ***************************************************************************/
22
23 #include "setup.h"
24
25 #ifndef CURL_DISABLE_DICT
26
27 /* -- WIN32 approved -- */
28 #include <stdio.h>
29 #include <string.h>
30 #include <stdarg.h>
31 #include <stdlib.h>
32 #include <ctype.h>
33
34 #ifdef WIN32
35 #include <time.h>
36 #include <io.h>
37 #else
38 #ifdef HAVE_SYS_SOCKET_H
39 #include <sys/socket.h>
40 #endif
41 #include <netinet/in.h>
42 #ifdef HAVE_SYS_TIME_H
43 #include <sys/time.h>
44 #endif
45 #ifdef HAVE_UNISTD_H
46 #include <unistd.h>
47 #endif
48 #include <netdb.h>
49 #ifdef HAVE_ARPA_INET_H
50 #include <arpa/inet.h>
51 #endif
52 #ifdef HAVE_NET_IF_H
53 #include <net/if.h>
54 #endif
55 #ifdef HAVE_SYS_IOCTL_H
56 #include <sys/ioctl.h>
57 #endif
58
59 #ifdef HAVE_SYS_PARAM_H
60 #include <sys/param.h>
61 #endif
62
63 #ifdef HAVE_SYS_SELECT_H
64 #include <sys/select.h>
65 #endif
66
67
68 #endif
69
70 #include "urldata.h"
71 #include <curl/curl.h>
72 #include "transfer.h"
73 #include "sendf.h"
74
75 #include "progress.h"
76 #include "strequal.h"
77 #include "dict.h"
78 #include "rawstr.h"
79
80 #define _MPRINTF_REPLACE /* use our functions only */
81 #include <curl/mprintf.h>
82
83 /* The last #include file should be: */
84 #include "memdebug.h"
85
86
87 /*
88 * Forward declarations.
89 */
90
91 static CURLcode dict_do(struct connectdata *conn, bool *done);
92
93 /*
94 * DICT protocol handler.
95 */
96
97 const struct Curl_handler Curl_handler_dict = {
98 "DICT", /* scheme */
99 ZERO_NULL, /* setup_connection */
100 dict_do, /* do_it */
101 ZERO_NULL, /* done */
102 ZERO_NULL, /* do_more */
103 ZERO_NULL, /* connect_it */
104 ZERO_NULL, /* connecting */
105 ZERO_NULL, /* doing */
106 ZERO_NULL, /* proto_getsock */
107 ZERO_NULL, /* doing_getsock */
108 ZERO_NULL, /* perform_getsock */
109 ZERO_NULL, /* disconnect */
110 PORT_DICT, /* defport */
111 CURLPROTO_DICT, /* protocol */
112 PROTOPT_NONE /* flags */
113 };
114
unescape_word(struct SessionHandle * data,const char * inputbuff)115 static char *unescape_word(struct SessionHandle *data, const char *inputbuff)
116 {
117 char *newp;
118 char *dictp;
119 char *ptr;
120 int len;
121 char byte;
122 int olen=0;
123
124 newp = curl_easy_unescape(data, inputbuff, 0, &len);
125 if(!newp)
126 return NULL;
127
128 dictp = malloc(((size_t)len)*2 + 1); /* add one for terminating zero */
129 if(dictp) {
130 /* According to RFC2229 section 2.2, these letters need to be escaped with
131 \[letter] */
132 for(ptr = newp;
133 (byte = *ptr) != 0;
134 ptr++) {
135 if((byte <= 32) || (byte == 127) ||
136 (byte == '\'') || (byte == '\"') || (byte == '\\')) {
137 dictp[olen++] = '\\';
138 }
139 dictp[olen++] = byte;
140 }
141 dictp[olen]=0;
142
143 free(newp);
144 }
145 return dictp;
146 }
147
dict_do(struct connectdata * conn,bool * done)148 static CURLcode dict_do(struct connectdata *conn, bool *done)
149 {
150 char *word;
151 char *eword;
152 char *ppath;
153 char *database = NULL;
154 char *strategy = NULL;
155 char *nthdef = NULL; /* This is not part of the protocol, but required
156 by RFC 2229 */
157 CURLcode result=CURLE_OK;
158 struct SessionHandle *data=conn->data;
159 curl_socket_t sockfd = conn->sock[FIRSTSOCKET];
160
161 char *path = data->state.path;
162 curl_off_t *bytecount = &data->req.bytecount;
163
164 *done = TRUE; /* unconditionally */
165
166 if(conn->bits.user_passwd) {
167 /* AUTH is missing */
168 }
169
170 if(Curl_raw_nequal(path, DICT_MATCH, sizeof(DICT_MATCH)-1) ||
171 Curl_raw_nequal(path, DICT_MATCH2, sizeof(DICT_MATCH2)-1) ||
172 Curl_raw_nequal(path, DICT_MATCH3, sizeof(DICT_MATCH3)-1)) {
173
174 word = strchr(path, ':');
175 if(word) {
176 word++;
177 database = strchr(word, ':');
178 if(database) {
179 *database++ = (char)0;
180 strategy = strchr(database, ':');
181 if(strategy) {
182 *strategy++ = (char)0;
183 nthdef = strchr(strategy, ':');
184 if(nthdef) {
185 *nthdef = (char)0;
186 }
187 }
188 }
189 }
190
191 if((word == NULL) || (*word == (char)0)) {
192 infof(data, "lookup word is missing");
193 word=(char *)"default";
194 }
195 if((database == NULL) || (*database == (char)0)) {
196 database = (char *)"!";
197 }
198 if((strategy == NULL) || (*strategy == (char)0)) {
199 strategy = (char *)".";
200 }
201
202 eword = unescape_word(data, word);
203 if(!eword)
204 return CURLE_OUT_OF_MEMORY;
205
206 result = Curl_sendf(sockfd, conn,
207 "CLIENT " LIBCURL_NAME " " LIBCURL_VERSION "\r\n"
208 "MATCH "
209 "%s " /* database */
210 "%s " /* strategy */
211 "%s\r\n" /* word */
212 "QUIT\r\n",
213
214 database,
215 strategy,
216 eword
217 );
218
219 free(eword);
220
221 if(result) {
222 failf(data, "Failed sending DICT request");
223 return result;
224 }
225 Curl_setup_transfer(conn, FIRSTSOCKET, -1, FALSE, bytecount,
226 -1, NULL); /* no upload */
227 }
228 else if(Curl_raw_nequal(path, DICT_DEFINE, sizeof(DICT_DEFINE)-1) ||
229 Curl_raw_nequal(path, DICT_DEFINE2, sizeof(DICT_DEFINE2)-1) ||
230 Curl_raw_nequal(path, DICT_DEFINE3, sizeof(DICT_DEFINE3)-1)) {
231
232 word = strchr(path, ':');
233 if(word) {
234 word++;
235 database = strchr(word, ':');
236 if(database) {
237 *database++ = (char)0;
238 nthdef = strchr(database, ':');
239 if(nthdef) {
240 *nthdef = (char)0;
241 }
242 }
243 }
244
245 if((word == NULL) || (*word == (char)0)) {
246 infof(data, "lookup word is missing");
247 word=(char *)"default";
248 }
249 if((database == NULL) || (*database == (char)0)) {
250 database = (char *)"!";
251 }
252
253 eword = unescape_word(data, word);
254 if(!eword)
255 return CURLE_OUT_OF_MEMORY;
256
257 result = Curl_sendf(sockfd, conn,
258 "CLIENT " LIBCURL_NAME " " LIBCURL_VERSION "\r\n"
259 "DEFINE "
260 "%s " /* database */
261 "%s\r\n" /* word */
262 "QUIT\r\n",
263 database,
264 eword);
265
266 free(eword);
267
268 if(result) {
269 failf(data, "Failed sending DICT request");
270 return result;
271 }
272 Curl_setup_transfer(conn, FIRSTSOCKET, -1, FALSE, bytecount,
273 -1, NULL); /* no upload */
274 }
275 else {
276
277 ppath = strchr(path, '/');
278 if(ppath) {
279 int i;
280
281 ppath++;
282 for (i = 0; ppath[i]; i++) {
283 if(ppath[i] == ':')
284 ppath[i] = ' ';
285 }
286 result = Curl_sendf(sockfd, conn,
287 "CLIENT " LIBCURL_NAME " " LIBCURL_VERSION "\r\n"
288 "%s\r\n"
289 "QUIT\r\n", ppath);
290 if(result) {
291 failf(data, "Failed sending DICT request");
292 return result;
293 }
294
295 Curl_setup_transfer(conn, FIRSTSOCKET, -1, FALSE, bytecount, -1, NULL);
296 }
297 }
298
299 return CURLE_OK;
300 }
301 #endif /*CURL_DISABLE_DICT*/
302