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