Coverage Report

Created: 2026-06-30 08:33

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/gdal/curl/lib/dict.c
Line
Count
Source
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
#include "curl_setup.h"
25
#include "urldata.h"
26
#include "dict.h"
27
28
#ifndef CURL_DISABLE_DICT
29
30
#ifdef HAVE_NETINET_IN_H
31
#include <netinet/in.h>
32
#endif
33
#ifdef HAVE_NETDB_H
34
#include <netdb.h>
35
#endif
36
#ifdef HAVE_ARPA_INET_H
37
#include <arpa/inet.h>
38
#endif
39
#ifdef HAVE_NET_IF_H
40
#include <net/if.h>
41
#endif
42
#ifdef HAVE_SYS_IOCTL_H
43
#include <sys/ioctl.h>
44
#endif
45
46
#ifdef HAVE_SYS_PARAM_H
47
#include <sys/param.h>
48
#endif
49
50
#ifdef HAVE_SYS_SELECT_H
51
#include <sys/select.h>
52
#elif defined(HAVE_UNISTD_H)
53
#include <unistd.h>
54
#endif
55
56
#include "transfer.h"
57
#include "curl_trc.h"
58
#include "escape.h"
59
60
0
#define DICT_MATCH   "/MATCH:"
61
0
#define DICT_MATCH2  "/M:"
62
0
#define DICT_MATCH3  "/FIND:"
63
0
#define DICT_DEFINE  "/DEFINE:"
64
0
#define DICT_DEFINE2 "/D:"
65
0
#define DICT_DEFINE3 "/LOOKUP:"
66
67
0
#define DYN_DICT_WORD 10000
68
static char *unescape_word(const char *input)
69
0
{
70
0
  struct dynbuf out;
71
0
  const char *ptr;
72
0
  CURLcode result = CURLE_OK;
73
0
  curlx_dyn_init(&out, DYN_DICT_WORD);
74
75
  /* According to RFC2229 section 2.2, these letters need to be escaped with
76
     \[letter] */
77
0
  for(ptr = input; *ptr; ptr++) {
78
0
    char ch = *ptr;
79
0
    if((ch <= 32) || (ch == 127) ||
80
0
       (ch == '\'') || (ch == '\"') || (ch == '\\'))
81
0
      result = curlx_dyn_addn(&out, "\\", 1);
82
0
    if(!result)
83
0
      result = curlx_dyn_addn(&out, ptr, 1);
84
0
    if(result)
85
0
      return NULL;
86
0
  }
87
0
  return curlx_dyn_ptr(&out);
88
0
}
89
90
/* sendf() sends formatted data to the server */
91
static CURLcode sendf(struct Curl_easy *data,
92
                      const char *fmt, ...) CURL_PRINTF(2, 3);
93
94
static CURLcode sendf(struct Curl_easy *data, const char *fmt, ...)
95
0
{
96
0
  size_t bytes_written;
97
0
  size_t write_len;
98
0
  CURLcode result = CURLE_OK;
99
0
  char *s;
100
0
  char *sptr;
101
0
  va_list ap;
102
0
  va_start(ap, fmt);
103
0
  s = curl_mvaprintf(fmt, ap); /* returns an allocated string */
104
0
  va_end(ap);
105
0
  if(!s)
106
0
    return CURLE_OUT_OF_MEMORY; /* failure */
107
108
0
  bytes_written = 0;
109
0
  write_len = strlen(s);
110
0
  sptr = s;
111
112
0
  for(;;) {
113
    /* Write the buffer to the socket */
114
0
    result = Curl_xfer_send(data, sptr, write_len, FALSE, &bytes_written);
115
116
0
    if(result)
117
0
      break;
118
119
0
    Curl_debug(data, CURLINFO_DATA_OUT, sptr, bytes_written);
120
121
0
    if(bytes_written != write_len) {
122
      /* if not all was written at once, we must advance the pointer, decrease
123
         the size left and try again! */
124
0
      write_len -= bytes_written;
125
0
      sptr += bytes_written;
126
0
    }
127
0
    else
128
0
      break;
129
0
  }
130
131
0
  curlx_free(s); /* free the output string */
132
133
0
  return result;
134
0
}
135
136
static CURLcode dict_do(struct Curl_easy *data, bool *done)
137
0
{
138
0
  char *word;
139
0
  char *eword = NULL;
140
0
  char *ppath;
141
0
  char *database = NULL;
142
0
  char *strategy = NULL;
143
0
  char *nthdef = NULL; /* This is not part of the protocol, but required
144
                          by RFC 2229 */
145
0
  CURLcode result;
146
147
0
  char *path;
148
149
0
  *done = TRUE; /* unconditionally */
150
151
  /* URL-decode path before further evaluation */
152
0
  result = Curl_urldecode(data->state.up.path, 0, &path, NULL, REJECT_CTRL);
153
0
  if(result)
154
0
    return result;
155
156
0
  if(curl_strnequal(path, DICT_MATCH, sizeof(DICT_MATCH) - 1) ||
157
0
     curl_strnequal(path, DICT_MATCH2, sizeof(DICT_MATCH2) - 1) ||
158
0
     curl_strnequal(path, DICT_MATCH3, sizeof(DICT_MATCH3) - 1)) {
159
160
0
    word = strchr(path, ':');
161
0
    if(word) {
162
0
      word++;
163
0
      database = strchr(word, ':');
164
0
      if(database) {
165
0
        *database++ = (char)0;
166
0
        strategy = strchr(database, ':');
167
0
        if(strategy) {
168
0
          *strategy++ = (char)0;
169
0
          nthdef = strchr(strategy, ':');
170
0
          if(nthdef) {
171
0
            *nthdef = (char)0;
172
0
          }
173
0
        }
174
0
      }
175
0
    }
176
177
0
    if(!word || (*word == (char)0)) {
178
0
      infof(data, "lookup word is missing");
179
0
    }
180
0
    eword = unescape_word((!word || (*word == (char)0)) ? "default" : word);
181
0
    if(!eword) {
182
0
      result = CURLE_OUT_OF_MEMORY;
183
0
      goto error;
184
0
    }
185
186
0
    result = sendf(data,
187
0
                   "CLIENT " LIBCURL_NAME " " LIBCURL_VERSION "\r\n"
188
0
                   "MATCH "
189
0
                   "%s "    /* database */
190
0
                   "%s "    /* strategy */
191
0
                   "%s\r\n" /* word */
192
0
                   "QUIT\r\n",
193
0
                   (!database || (*database == (char)0)) ? "!" : database,
194
0
                   (!strategy || (*strategy == (char)0)) ? "." : strategy,
195
0
                   eword);
196
197
0
    if(result) {
198
0
      failf(data, "Failed sending DICT request");
199
0
      goto error;
200
0
    }
201
0
    Curl_xfer_setup_recv(data, FIRSTSOCKET, -1);
202
0
  }
203
0
  else if(curl_strnequal(path, DICT_DEFINE, sizeof(DICT_DEFINE) - 1) ||
204
0
          curl_strnequal(path, DICT_DEFINE2, sizeof(DICT_DEFINE2) - 1) ||
205
0
          curl_strnequal(path, DICT_DEFINE3, sizeof(DICT_DEFINE3) - 1)) {
206
207
0
    word = strchr(path, ':');
208
0
    if(word) {
209
0
      word++;
210
0
      database = strchr(word, ':');
211
0
      if(database) {
212
0
        *database++ = (char)0;
213
0
        nthdef = strchr(database, ':');
214
0
        if(nthdef) {
215
0
          *nthdef = (char)0;
216
0
        }
217
0
      }
218
0
    }
219
220
0
    if(!word || (*word == (char)0)) {
221
0
      infof(data, "lookup word is missing");
222
0
    }
223
0
    eword = unescape_word((!word || (*word == (char)0)) ? "default" : word);
224
0
    if(!eword) {
225
0
      result = CURLE_OUT_OF_MEMORY;
226
0
      goto error;
227
0
    }
228
229
0
    result = sendf(data,
230
0
                   "CLIENT " LIBCURL_NAME " " LIBCURL_VERSION "\r\n"
231
0
                   "DEFINE "
232
0
                   "%s "     /* database */
233
0
                   "%s\r\n"  /* word */
234
0
                   "QUIT\r\n",
235
0
                   (!database || (*database == (char)0)) ? "!" : database,
236
0
                   eword);
237
238
0
    if(result) {
239
0
      failf(data, "Failed sending DICT request");
240
0
      goto error;
241
0
    }
242
0
    Curl_xfer_setup_recv(data, FIRSTSOCKET, -1);
243
0
  }
244
0
  else {
245
246
0
    ppath = strchr(path, '/');
247
0
    if(ppath) {
248
0
      int i;
249
250
0
      ppath++;
251
0
      for(i = 0; ppath[i]; i++) {
252
0
        if(ppath[i] == ':')
253
0
          ppath[i] = ' ';
254
0
      }
255
0
      result = sendf(data,
256
0
                     "CLIENT " LIBCURL_NAME " " LIBCURL_VERSION "\r\n"
257
0
                     "%s\r\n"
258
0
                     "QUIT\r\n", ppath);
259
0
      if(result) {
260
0
        failf(data, "Failed sending DICT request");
261
0
        goto error;
262
0
      }
263
264
0
      Curl_xfer_setup_recv(data, FIRSTSOCKET, -1);
265
0
    }
266
0
  }
267
268
0
error:
269
0
  curlx_free(eword);
270
0
  curlx_free(path);
271
0
  return result;
272
0
}
273
274
/*
275
 * DICT protocol
276
 */
277
const struct Curl_protocol Curl_protocol_dict = {
278
  ZERO_NULL,                            /* setup_connection */
279
  dict_do,                              /* do_it */
280
  ZERO_NULL,                            /* done */
281
  ZERO_NULL,                            /* do_more */
282
  ZERO_NULL,                            /* connect_it */
283
  ZERO_NULL,                            /* connecting */
284
  ZERO_NULL,                            /* doing */
285
  ZERO_NULL,                            /* proto_pollset */
286
  ZERO_NULL,                            /* doing_pollset */
287
  ZERO_NULL,                            /* domore_pollset */
288
  ZERO_NULL,                            /* perform_pollset */
289
  ZERO_NULL,                            /* disconnect */
290
  ZERO_NULL,                            /* write_resp */
291
  ZERO_NULL,                            /* write_resp_hd */
292
  ZERO_NULL,                            /* connection_is_dead */
293
  ZERO_NULL,                            /* attach connection */
294
  ZERO_NULL,                            /* follow */
295
};
296
297
#endif /* CURL_DISABLE_DICT */