e23e661912a7469e3fc5c90dab2b3261251b9dad
[platform/upstream/cmake.git] / Utilities / cmcurl / lib / dict.c
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2022, 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  ***************************************************************************/
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 "curl_printf.h"
61 #include "strcase.h"
62 #include "curl_memory.h"
63 /* The last #include file should be: */
64 #include "memdebug.h"
65
66 /*
67  * Forward declarations.
68  */
69
70 static CURLcode dict_do(struct Curl_easy *data, bool *done);
71
72 /*
73  * DICT protocol handler.
74  */
75
76 const struct Curl_handler Curl_handler_dict = {
77   "DICT",                               /* scheme */
78   ZERO_NULL,                            /* setup_connection */
79   dict_do,                              /* do_it */
80   ZERO_NULL,                            /* done */
81   ZERO_NULL,                            /* do_more */
82   ZERO_NULL,                            /* connect_it */
83   ZERO_NULL,                            /* connecting */
84   ZERO_NULL,                            /* doing */
85   ZERO_NULL,                            /* proto_getsock */
86   ZERO_NULL,                            /* doing_getsock */
87   ZERO_NULL,                            /* domore_getsock */
88   ZERO_NULL,                            /* perform_getsock */
89   ZERO_NULL,                            /* disconnect */
90   ZERO_NULL,                            /* readwrite */
91   ZERO_NULL,                            /* connection_check */
92   ZERO_NULL,                            /* attach connection */
93   PORT_DICT,                            /* defport */
94   CURLPROTO_DICT,                       /* protocol */
95   CURLPROTO_DICT,                       /* family */
96   PROTOPT_NONE | PROTOPT_NOURLQUERY     /* flags */
97 };
98
99 static char *unescape_word(const char *inputbuff)
100 {
101   char *newp = NULL;
102   char *dictp;
103   size_t len;
104
105   CURLcode result = Curl_urldecode(inputbuff, 0, &newp, &len,
106                                    REJECT_NADA);
107   if(!newp || result)
108     return NULL;
109
110   dictp = malloc(len*2 + 1); /* add one for terminating zero */
111   if(dictp) {
112     char *ptr;
113     char ch;
114     int olen = 0;
115     /* According to RFC2229 section 2.2, these letters need to be escaped with
116        \[letter] */
117     for(ptr = newp;
118         (ch = *ptr) != 0;
119         ptr++) {
120       if((ch <= 32) || (ch == 127) ||
121           (ch == '\'') || (ch == '\"') || (ch == '\\')) {
122         dictp[olen++] = '\\';
123       }
124       dictp[olen++] = ch;
125     }
126     dictp[olen] = 0;
127   }
128   free(newp);
129   return dictp;
130 }
131
132 /* sendf() sends formatted data to the server */
133 static CURLcode sendf(curl_socket_t sockfd, struct Curl_easy *data,
134                       const char *fmt, ...)
135 {
136   ssize_t bytes_written;
137   size_t write_len;
138   CURLcode result = CURLE_OK;
139   char *s;
140   char *sptr;
141   va_list ap;
142   va_start(ap, fmt);
143   s = vaprintf(fmt, ap); /* returns an allocated string */
144   va_end(ap);
145   if(!s)
146     return CURLE_OUT_OF_MEMORY; /* failure */
147
148   bytes_written = 0;
149   write_len = strlen(s);
150   sptr = s;
151
152   for(;;) {
153     /* Write the buffer to the socket */
154     result = Curl_write(data, sockfd, sptr, write_len, &bytes_written);
155
156     if(result)
157       break;
158
159     Curl_debug(data, CURLINFO_DATA_OUT, sptr, (size_t)bytes_written);
160
161     if((size_t)bytes_written != write_len) {
162       /* if not all was written at once, we must advance the pointer, decrease
163          the size left and try again! */
164       write_len -= bytes_written;
165       sptr += bytes_written;
166     }
167     else
168       break;
169   }
170
171   free(s); /* free the output string */
172
173   return result;
174 }
175
176 static CURLcode dict_do(struct Curl_easy *data, bool *done)
177 {
178   char *word;
179   char *eword;
180   char *ppath;
181   char *database = NULL;
182   char *strategy = NULL;
183   char *nthdef = NULL; /* This is not part of the protocol, but required
184                           by RFC 2229 */
185   CURLcode result = CURLE_OK;
186   struct connectdata *conn = data->conn;
187   curl_socket_t sockfd = conn->sock[FIRSTSOCKET];
188
189   char *path = data->state.up.path;
190
191   *done = TRUE; /* unconditionally */
192
193   if(strncasecompare(path, DICT_MATCH, sizeof(DICT_MATCH)-1) ||
194      strncasecompare(path, DICT_MATCH2, sizeof(DICT_MATCH2)-1) ||
195      strncasecompare(path, DICT_MATCH3, sizeof(DICT_MATCH3)-1)) {
196
197     word = strchr(path, ':');
198     if(word) {
199       word++;
200       database = strchr(word, ':');
201       if(database) {
202         *database++ = (char)0;
203         strategy = strchr(database, ':');
204         if(strategy) {
205           *strategy++ = (char)0;
206           nthdef = strchr(strategy, ':');
207           if(nthdef) {
208             *nthdef = (char)0;
209           }
210         }
211       }
212     }
213
214     if(!word || (*word == (char)0)) {
215       infof(data, "lookup word is missing");
216       word = (char *)"default";
217     }
218     if(!database || (*database == (char)0)) {
219       database = (char *)"!";
220     }
221     if(!strategy || (*strategy == (char)0)) {
222       strategy = (char *)".";
223     }
224
225     eword = unescape_word(word);
226     if(!eword)
227       return CURLE_OUT_OF_MEMORY;
228
229     result = sendf(sockfd, 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     free(eword);
241
242     if(result) {
243       failf(data, "Failed sending DICT request");
244       return result;
245     }
246     Curl_setup_transfer(data, FIRSTSOCKET, -1, FALSE, -1); /* no upload */
247   }
248   else if(strncasecompare(path, DICT_DEFINE, sizeof(DICT_DEFINE)-1) ||
249           strncasecompare(path, DICT_DEFINE2, sizeof(DICT_DEFINE2)-1) ||
250           strncasecompare(path, DICT_DEFINE3, sizeof(DICT_DEFINE3)-1)) {
251
252     word = strchr(path, ':');
253     if(word) {
254       word++;
255       database = strchr(word, ':');
256       if(database) {
257         *database++ = (char)0;
258         nthdef = strchr(database, ':');
259         if(nthdef) {
260           *nthdef = (char)0;
261         }
262       }
263     }
264
265     if(!word || (*word == (char)0)) {
266       infof(data, "lookup word is missing");
267       word = (char *)"default";
268     }
269     if(!database || (*database == (char)0)) {
270       database = (char *)"!";
271     }
272
273     eword = unescape_word(word);
274     if(!eword)
275       return CURLE_OUT_OF_MEMORY;
276
277     result = sendf(sockfd, 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     free(eword);
287
288     if(result) {
289       failf(data, "Failed sending DICT request");
290       return result;
291     }
292     Curl_setup_transfer(data, FIRSTSOCKET, -1, FALSE, -1);
293   }
294   else {
295
296     ppath = strchr(path, '/');
297     if(ppath) {
298       int i;
299
300       ppath++;
301       for(i = 0; ppath[i]; i++) {
302         if(ppath[i] == ':')
303           ppath[i] = ' ';
304       }
305       result = sendf(sockfd, data,
306                      "CLIENT " LIBCURL_NAME " " LIBCURL_VERSION "\r\n"
307                      "%s\r\n"
308                      "QUIT\r\n", ppath);
309       if(result) {
310         failf(data, "Failed sending DICT request");
311         return result;
312       }
313
314       Curl_setup_transfer(data, FIRSTSOCKET, -1, FALSE, -1);
315     }
316   }
317
318   return CURLE_OK;
319 }
320 #endif /*CURL_DISABLE_DICT*/