• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2019, 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 https://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 "curl_setup.h"
24 
25 #ifndef CURL_DISABLE_DICT
26 
27 #ifdef HAVE_NETINET_IN_H
28 #include <netinet/in.h>
29 #endif
30 #ifdef HAVE_NETDB_H
31 #include <netdb.h>
32 #endif
33 #ifdef HAVE_ARPA_INET_H
34 #include <arpa/inet.h>
35 #endif
36 #ifdef HAVE_NET_IF_H
37 #include <net/if.h>
38 #endif
39 #ifdef HAVE_SYS_IOCTL_H
40 #include <sys/ioctl.h>
41 #endif
42 
43 #ifdef HAVE_SYS_PARAM_H
44 #include <sys/param.h>
45 #endif
46 
47 #ifdef HAVE_SYS_SELECT_H
48 #include <sys/select.h>
49 #endif
50 
51 #include "urldata.h"
52 #include <curl/curl.h>
53 #include "transfer.h"
54 #include "sendf.h"
55 #include "escape.h"
56 #include "progress.h"
57 #include "dict.h"
58 #include "strcase.h"
59 #include "curl_memory.h"
60 /* The last #include file should be: */
61 #include "memdebug.h"
62 
63 /*
64  * Forward declarations.
65  */
66 
67 static CURLcode dict_do(struct connectdata *conn, bool *done);
68 
69 /*
70  * DICT protocol handler.
71  */
72 
73 const struct Curl_handler Curl_handler_dict = {
74   "DICT",                               /* scheme */
75   ZERO_NULL,                            /* setup_connection */
76   dict_do,                              /* do_it */
77   ZERO_NULL,                            /* done */
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   ZERO_NULL,                            /* connection_check */
89   PORT_DICT,                            /* defport */
90   CURLPROTO_DICT,                       /* protocol */
91   PROTOPT_NONE | PROTOPT_NOURLQUERY      /* flags */
92 };
93 
unescape_word(struct Curl_easy * data,const char * inputbuff)94 static char *unescape_word(struct Curl_easy *data, const char *inputbuff)
95 {
96   char *newp = NULL;
97   char *dictp;
98   size_t len;
99 
100   CURLcode result = Curl_urldecode(data, inputbuff, 0, &newp, &len, FALSE);
101   if(!newp || result)
102     return NULL;
103 
104   dictp = malloc(len*2 + 1); /* add one for terminating zero */
105   if(dictp) {
106     char *ptr;
107     char ch;
108     int olen = 0;
109     /* According to RFC2229 section 2.2, these letters need to be escaped with
110        \[letter] */
111     for(ptr = newp;
112         (ch = *ptr) != 0;
113         ptr++) {
114       if((ch <= 32) || (ch == 127) ||
115           (ch == '\'') || (ch == '\"') || (ch == '\\')) {
116         dictp[olen++] = '\\';
117       }
118       dictp[olen++] = ch;
119     }
120     dictp[olen] = 0;
121   }
122   free(newp);
123   return dictp;
124 }
125 
dict_do(struct connectdata * conn,bool * done)126 static CURLcode dict_do(struct connectdata *conn, bool *done)
127 {
128   char *word;
129   char *eword;
130   char *ppath;
131   char *database = NULL;
132   char *strategy = NULL;
133   char *nthdef = NULL; /* This is not part of the protocol, but required
134                           by RFC 2229 */
135   CURLcode result = CURLE_OK;
136   struct Curl_easy *data = conn->data;
137   curl_socket_t sockfd = conn->sock[FIRSTSOCKET];
138 
139   char *path = data->state.up.path;
140 
141   *done = TRUE; /* unconditionally */
142 
143   if(conn->bits.user_passwd) {
144     /* AUTH is missing */
145   }
146 
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)) {
150 
151     word = strchr(path, ':');
152     if(word) {
153       word++;
154       database = strchr(word, ':');
155       if(database) {
156         *database++ = (char)0;
157         strategy = strchr(database, ':');
158         if(strategy) {
159           *strategy++ = (char)0;
160           nthdef = strchr(strategy, ':');
161           if(nthdef) {
162             *nthdef = (char)0;
163           }
164         }
165       }
166     }
167 
168     if((word == NULL) || (*word == (char)0)) {
169       infof(data, "lookup word is missing\n");
170       word = (char *)"default";
171     }
172     if((database == NULL) || (*database == (char)0)) {
173       database = (char *)"!";
174     }
175     if((strategy == NULL) || (*strategy == (char)0)) {
176       strategy = (char *)".";
177     }
178 
179     eword = unescape_word(data, word);
180     if(!eword)
181       return CURLE_OUT_OF_MEMORY;
182 
183     result = Curl_sendf(sockfd, conn,
184                         "CLIENT " LIBCURL_NAME " " LIBCURL_VERSION "\r\n"
185                         "MATCH "
186                         "%s "    /* database */
187                         "%s "    /* strategy */
188                         "%s\r\n" /* word */
189                         "QUIT\r\n",
190 
191                         database,
192                         strategy,
193                         eword
194                         );
195 
196     free(eword);
197 
198     if(result) {
199       failf(data, "Failed sending DICT request");
200       return result;
201     }
202     Curl_setup_transfer(data, FIRSTSOCKET, -1, FALSE, -1); /* no upload */
203   }
204   else if(strncasecompare(path, DICT_DEFINE, sizeof(DICT_DEFINE)-1) ||
205           strncasecompare(path, DICT_DEFINE2, sizeof(DICT_DEFINE2)-1) ||
206           strncasecompare(path, DICT_DEFINE3, sizeof(DICT_DEFINE3)-1)) {
207 
208     word = strchr(path, ':');
209     if(word) {
210       word++;
211       database = strchr(word, ':');
212       if(database) {
213         *database++ = (char)0;
214         nthdef = strchr(database, ':');
215         if(nthdef) {
216           *nthdef = (char)0;
217         }
218       }
219     }
220 
221     if((word == NULL) || (*word == (char)0)) {
222       infof(data, "lookup word is missing\n");
223       word = (char *)"default";
224     }
225     if((database == NULL) || (*database == (char)0)) {
226       database = (char *)"!";
227     }
228 
229     eword = unescape_word(data, word);
230     if(!eword)
231       return CURLE_OUT_OF_MEMORY;
232 
233     result = Curl_sendf(sockfd, conn,
234                         "CLIENT " LIBCURL_NAME " " LIBCURL_VERSION "\r\n"
235                         "DEFINE "
236                         "%s "     /* database */
237                         "%s\r\n"  /* word */
238                         "QUIT\r\n",
239                         database,
240                         eword);
241 
242     free(eword);
243 
244     if(result) {
245       failf(data, "Failed sending DICT request");
246       return result;
247     }
248     Curl_setup_transfer(data, FIRSTSOCKET, -1, FALSE, -1);
249   }
250   else {
251 
252     ppath = strchr(path, '/');
253     if(ppath) {
254       int i;
255 
256       ppath++;
257       for(i = 0; ppath[i]; i++) {
258         if(ppath[i] == ':')
259           ppath[i] = ' ';
260       }
261       result = Curl_sendf(sockfd, conn,
262                           "CLIENT " LIBCURL_NAME " " LIBCURL_VERSION "\r\n"
263                           "%s\r\n"
264                           "QUIT\r\n", ppath);
265       if(result) {
266         failf(data, "Failed sending DICT request");
267         return result;
268       }
269 
270       Curl_setup_transfer(data, FIRSTSOCKET, -1, FALSE, -1);
271     }
272   }
273 
274   return CURLE_OK;
275 }
276 #endif /*CURL_DISABLE_DICT*/
277