xref: /curl/lib/dict.c (revision 911c3166)
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 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.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  * SPDX-License-Identifier: curl
22  *
23  ***************************************************************************/
24 
25 #include "curl_setup.h"
26 
27 #ifndef CURL_DISABLE_DICT
28 
29 #ifdef HAVE_NETINET_IN_H
30 #include <netinet/in.h>
31 #endif
32 #ifdef HAVE_NETDB_H
33 #include <netdb.h>
34 #endif
35 #ifdef HAVE_ARPA_INET_H
36 #include <arpa/inet.h>
37 #endif
38 #ifdef HAVE_NET_IF_H
39 #include <net/if.h>
40 #endif
41 #ifdef HAVE_SYS_IOCTL_H
42 #include <sys/ioctl.h>
43 #endif
44 
45 #ifdef HAVE_SYS_PARAM_H
46 #include <sys/param.h>
47 #endif
48 
49 #ifdef HAVE_SYS_SELECT_H
50 #include <sys/select.h>
51 #elif defined(HAVE_UNISTD_H)
52 #include <unistd.h>
53 #endif
54 
55 #include "urldata.h"
56 #include <curl/curl.h>
57 #include "transfer.h"
58 #include "sendf.h"
59 #include "escape.h"
60 #include "progress.h"
61 #include "dict.h"
62 #include "curl_printf.h"
63 #include "strcase.h"
64 #include "curl_memory.h"
65 /* The last #include file should be: */
66 #include "memdebug.h"
67 
68 /*
69  * Forward declarations.
70  */
71 
72 static CURLcode dict_do(struct Curl_easy *data, bool *done);
73 
74 /*
75  * DICT protocol handler.
76  */
77 
78 const struct Curl_handler Curl_handler_dict = {
79   "dict",                               /* scheme */
80   ZERO_NULL,                            /* setup_connection */
81   dict_do,                              /* do_it */
82   ZERO_NULL,                            /* done */
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,                            /* write_resp */
93   ZERO_NULL,                            /* write_resp_hd */
94   ZERO_NULL,                            /* connection_check */
95   ZERO_NULL,                            /* attach connection */
96   PORT_DICT,                            /* defport */
97   CURLPROTO_DICT,                       /* protocol */
98   CURLPROTO_DICT,                       /* family */
99   PROTOPT_NONE | PROTOPT_NOURLQUERY     /* flags */
100 };
101 
102 #define DYN_DICT_WORD 10000
unescape_word(const char * input)103 static char *unescape_word(const char *input)
104 {
105   struct dynbuf out;
106   const char *ptr;
107   CURLcode result = CURLE_OK;
108   Curl_dyn_init(&out, DYN_DICT_WORD);
109 
110   /* According to RFC2229 section 2.2, these letters need to be escaped with
111      \[letter] */
112   for(ptr = input; *ptr; ptr++) {
113     char ch = *ptr;
114     if((ch <= 32) || (ch == 127) ||
115        (ch == '\'') || (ch == '\"') || (ch == '\\'))
116       result = Curl_dyn_addn(&out, "\\", 1);
117     if(!result)
118       result = Curl_dyn_addn(&out, ptr, 1);
119     if(result)
120       return NULL;
121   }
122   return Curl_dyn_ptr(&out);
123 }
124 
125 /* sendf() sends formatted data to the server */
126 static CURLcode sendf(struct Curl_easy *data,
127                       const char *fmt, ...) CURL_PRINTF(2, 3);
128 
sendf(struct Curl_easy * data,const char * fmt,...)129 static CURLcode sendf(struct Curl_easy *data, const char *fmt, ...)
130 {
131   size_t bytes_written;
132   size_t write_len;
133   CURLcode result = CURLE_OK;
134   char *s;
135   char *sptr;
136   va_list ap;
137   va_start(ap, fmt);
138   s = vaprintf(fmt, ap); /* returns an allocated string */
139   va_end(ap);
140   if(!s)
141     return CURLE_OUT_OF_MEMORY; /* failure */
142 
143   bytes_written = 0;
144   write_len = strlen(s);
145   sptr = s;
146 
147   for(;;) {
148     /* Write the buffer to the socket */
149     result = Curl_xfer_send(data, sptr, write_len, FALSE, &bytes_written);
150 
151     if(result)
152       break;
153 
154     Curl_debug(data, CURLINFO_DATA_OUT, sptr, (size_t)bytes_written);
155 
156     if((size_t)bytes_written != write_len) {
157       /* if not all was written at once, we must advance the pointer, decrease
158          the size left and try again! */
159       write_len -= bytes_written;
160       sptr += bytes_written;
161     }
162     else
163       break;
164   }
165 
166   free(s); /* free the output string */
167 
168   return result;
169 }
170 
dict_do(struct Curl_easy * data,bool * done)171 static CURLcode dict_do(struct Curl_easy *data, bool *done)
172 {
173   char *word;
174   char *eword = NULL;
175   char *ppath;
176   char *database = NULL;
177   char *strategy = NULL;
178   char *nthdef = NULL; /* This is not part of the protocol, but required
179                           by RFC 2229 */
180   CURLcode result;
181 
182   char *path;
183 
184   *done = TRUE; /* unconditionally */
185 
186   /* url-decode path before further evaluation */
187   result = Curl_urldecode(data->state.up.path, 0, &path, NULL, REJECT_CTRL);
188   if(result)
189     return result;
190 
191   if(strncasecompare(path, DICT_MATCH, sizeof(DICT_MATCH)-1) ||
192      strncasecompare(path, DICT_MATCH2, sizeof(DICT_MATCH2)-1) ||
193      strncasecompare(path, DICT_MATCH3, sizeof(DICT_MATCH3)-1)) {
194 
195     word = strchr(path, ':');
196     if(word) {
197       word++;
198       database = strchr(word, ':');
199       if(database) {
200         *database++ = (char)0;
201         strategy = strchr(database, ':');
202         if(strategy) {
203           *strategy++ = (char)0;
204           nthdef = strchr(strategy, ':');
205           if(nthdef) {
206             *nthdef = (char)0;
207           }
208         }
209       }
210     }
211 
212     if(!word || (*word == (char)0)) {
213       infof(data, "lookup word is missing");
214       word = (char *)"default";
215     }
216     if(!database || (*database == (char)0)) {
217       database = (char *)"!";
218     }
219     if(!strategy || (*strategy == (char)0)) {
220       strategy = (char *)".";
221     }
222 
223     eword = unescape_word(word);
224     if(!eword) {
225       result = CURLE_OUT_OF_MEMORY;
226       goto error;
227     }
228 
229     result = sendf(data,
230                    "CLIENT " LIBCURL_NAME " " LIBCURL_VERSION "\r\n"
231                    "MATCH "
232                    "%s "    /* database */
233                    "%s "    /* strategy */
234                    "%s\r\n" /* word */
235                    "QUIT\r\n",
236                    database,
237                    strategy,
238                    eword);
239 
240     if(result) {
241       failf(data, "Failed sending DICT request");
242       goto error;
243     }
244     Curl_xfer_setup1(data, CURL_XFER_RECV, -1, FALSE); /* no upload */
245   }
246   else if(strncasecompare(path, DICT_DEFINE, sizeof(DICT_DEFINE)-1) ||
247           strncasecompare(path, DICT_DEFINE2, sizeof(DICT_DEFINE2)-1) ||
248           strncasecompare(path, DICT_DEFINE3, sizeof(DICT_DEFINE3)-1)) {
249 
250     word = strchr(path, ':');
251     if(word) {
252       word++;
253       database = strchr(word, ':');
254       if(database) {
255         *database++ = (char)0;
256         nthdef = strchr(database, ':');
257         if(nthdef) {
258           *nthdef = (char)0;
259         }
260       }
261     }
262 
263     if(!word || (*word == (char)0)) {
264       infof(data, "lookup word is missing");
265       word = (char *)"default";
266     }
267     if(!database || (*database == (char)0)) {
268       database = (char *)"!";
269     }
270 
271     eword = unescape_word(word);
272     if(!eword) {
273       result = CURLE_OUT_OF_MEMORY;
274       goto error;
275     }
276 
277     result = sendf(data,
278                    "CLIENT " LIBCURL_NAME " " LIBCURL_VERSION "\r\n"
279                    "DEFINE "
280                    "%s "     /* database */
281                    "%s\r\n"  /* word */
282                    "QUIT\r\n",
283                    database,
284                    eword);
285 
286     if(result) {
287       failf(data, "Failed sending DICT request");
288       goto error;
289     }
290     Curl_xfer_setup1(data, CURL_XFER_RECV, -1, FALSE);
291   }
292   else {
293 
294     ppath = strchr(path, '/');
295     if(ppath) {
296       int i;
297 
298       ppath++;
299       for(i = 0; ppath[i]; i++) {
300         if(ppath[i] == ':')
301           ppath[i] = ' ';
302       }
303       result = sendf(data,
304                      "CLIENT " LIBCURL_NAME " " LIBCURL_VERSION "\r\n"
305                      "%s\r\n"
306                      "QUIT\r\n", ppath);
307       if(result) {
308         failf(data, "Failed sending DICT request");
309         goto error;
310       }
311 
312       Curl_xfer_setup1(data, CURL_XFER_RECV, -1, FALSE);
313     }
314   }
315 
316 error:
317   free(eword);
318   free(path);
319   return result;
320 }
321 #endif /* CURL_DISABLE_DICT */
322