1 /***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) 1998 - 2008, 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 * $Id: dict.c,v 1.60 2008-12-19 21:14:52 bagder Exp $
22 ***************************************************************************/
23
24 #include "setup.h"
25
26 #ifndef CURL_DISABLE_DICT
27
28 /* -- WIN32 approved -- */
29 #include <stdio.h>
30 #include <string.h>
31 #include <stdarg.h>
32 #include <stdlib.h>
33 #include <ctype.h>
34
35 #ifdef WIN32
36 #include <time.h>
37 #include <io.h>
38 #else
39 #ifdef HAVE_SYS_SOCKET_H
40 #include <sys/socket.h>
41 #endif
42 #include <netinet/in.h>
43 #ifdef HAVE_SYS_TIME_H
44 #include <sys/time.h>
45 #endif
46 #ifdef HAVE_UNISTD_H
47 #include <unistd.h>
48 #endif
49 #include <netdb.h>
50 #ifdef HAVE_ARPA_INET_H
51 #include <arpa/inet.h>
52 #endif
53 #ifdef HAVE_NET_IF_H
54 #include <net/if.h>
55 #endif
56 #ifdef HAVE_SYS_IOCTL_H
57 #include <sys/ioctl.h>
58 #endif
59
60 #ifdef HAVE_SYS_PARAM_H
61 #include <sys/param.h>
62 #endif
63
64 #ifdef HAVE_SYS_SELECT_H
65 #include <sys/select.h>
66 #endif
67
68
69 #endif
70
71 #include "urldata.h"
72 #include <curl/curl.h>
73 #include "transfer.h"
74 #include "sendf.h"
75
76 #include "progress.h"
77 #include "strequal.h"
78 #include "dict.h"
79 #include "rawstr.h"
80
81 #define _MPRINTF_REPLACE /* use our functions only */
82 #include <curl/mprintf.h>
83
84 /* The last #include file should be: */
85 #include "memdebug.h"
86
87
88 /*
89 * Forward declarations.
90 */
91
92 static CURLcode dict_do(struct connectdata *conn, bool *done);
93
94 /*
95 * DICT protocol handler.
96 */
97
98 const struct Curl_handler Curl_handler_dict = {
99 "DICT", /* scheme */
100 ZERO_NULL, /* setup_connection */
101 dict_do, /* do_it */
102 ZERO_NULL, /* done */
103 ZERO_NULL, /* do_more */
104 ZERO_NULL, /* connect_it */
105 ZERO_NULL, /* connecting */
106 ZERO_NULL, /* doing */
107 ZERO_NULL, /* proto_getsock */
108 ZERO_NULL, /* doing_getsock */
109 ZERO_NULL, /* perform_getsock */
110 ZERO_NULL, /* disconnect */
111 PORT_DICT, /* defport */
112 PROT_DICT /* protocol */
113 };
114
unescape_word(struct SessionHandle * data,const char * inp)115 static char *unescape_word(struct SessionHandle *data, const char *inp)
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, inp, 0, &len);
125 if(!newp)
126 return NULL;
127
128 dictp = malloc(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 else
224 result = Curl_setup_transfer(conn, FIRSTSOCKET, -1, FALSE, bytecount,
225 -1, NULL); /* no upload */
226 if(result)
227 return result;
228 }
229 else if(Curl_raw_nequal(path, DICT_DEFINE, sizeof(DICT_DEFINE)-1) ||
230 Curl_raw_nequal(path, DICT_DEFINE2, sizeof(DICT_DEFINE2)-1) ||
231 Curl_raw_nequal(path, DICT_DEFINE3, sizeof(DICT_DEFINE3)-1)) {
232
233 word = strchr(path, ':');
234 if(word) {
235 word++;
236 database = strchr(word, ':');
237 if(database) {
238 *database++ = (char)0;
239 nthdef = strchr(database, ':');
240 if(nthdef) {
241 *nthdef++ = (char)0;
242 }
243 }
244 }
245
246 if((word == NULL) || (*word == (char)0)) {
247 infof(data, "lookup word is missing");
248 word=(char *)"default";
249 }
250 if((database == NULL) || (*database == (char)0)) {
251 database = (char *)"!";
252 }
253
254 eword = unescape_word(data, word);
255 if(!eword)
256 return CURLE_OUT_OF_MEMORY;
257
258 result = Curl_sendf(sockfd, conn,
259 "CLIENT " LIBCURL_NAME " " LIBCURL_VERSION "\r\n"
260 "DEFINE "
261 "%s " /* database */
262 "%s\r\n" /* word */
263 "QUIT\r\n",
264 database,
265 eword);
266
267 free(eword);
268
269 if(result)
270 failf(data, "Failed sending DICT request");
271 else
272 result = Curl_setup_transfer(conn, FIRSTSOCKET, -1, FALSE, bytecount,
273 -1, NULL); /* no upload */
274
275 if(result)
276 return result;
277
278 }
279 else {
280
281 ppath = strchr(path, '/');
282 if(ppath) {
283 int i;
284
285 ppath++;
286 for (i = 0; ppath[i]; i++) {
287 if(ppath[i] == ':')
288 ppath[i] = ' ';
289 }
290 result = Curl_sendf(sockfd, conn,
291 "CLIENT " LIBCURL_NAME " " LIBCURL_VERSION "\r\n"
292 "%s\r\n"
293 "QUIT\r\n", ppath);
294 if(result)
295 failf(data, "Failed sending DICT request");
296 else
297 result = Curl_setup_transfer(conn, FIRSTSOCKET, -1, FALSE, bytecount,
298 -1, NULL);
299 if(result)
300 return result;
301 }
302 }
303
304 return CURLE_OK;
305 }
306 #endif /*CURL_DISABLE_DICT*/
307