Coverage Report

Created: 2026-02-26 06:32

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/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
68
0
#define DYN_DICT_WORD 10000
69
static char *unescape_word(const char *input)
70
0
{
71
0
  struct dynbuf out;
72
0
  const char *ptr;
73
0
  CURLcode result = CURLE_OK;
74
0
  curlx_dyn_init(&out, DYN_DICT_WORD);
75
76
  /* According to RFC2229 section 2.2, these letters need to be escaped with
77
     \[letter] */
78
0
  for(ptr = input; *ptr; ptr++) {
79
0
    char ch = *ptr;
80
0
    if((ch <= 32) || (ch == 127) ||
81
0
       (ch == '\'') || (ch == '\"') || (ch == '\\'))
82
0
      result = curlx_dyn_addn(&out, "\\", 1);
83
0
    if(!result)
84
0
      result = curlx_dyn_addn(&out, ptr, 1);
85
0
    if(result)
86
0
      return NULL;
87
0
  }
88
0
  return curlx_dyn_ptr(&out);
89
0
}
90
91
/* sendf() sends formatted data to the server */
92
static CURLcode sendf(struct Curl_easy *data,
93
                      const char *fmt, ...) CURL_PRINTF(2, 3);
94
95
static CURLcode sendf(struct Curl_easy *data, const char *fmt, ...)
96
0
{
97
0
  size_t bytes_written;
98
0
  size_t write_len;
99
0
  CURLcode result = CURLE_OK;
100
0
  char *s;
101
0
  char *sptr;
102
0
  va_list ap;
103
0
  va_start(ap, fmt);
104
0
  s = curl_mvaprintf(fmt, ap); /* returns an allocated string */
105
0
  va_end(ap);
106
0
  if(!s)
107
0
    return CURLE_OUT_OF_MEMORY; /* failure */
108
109
0
  bytes_written = 0;
110
0
  write_len = strlen(s);
111
0
  sptr = s;
112
113
0
  for(;;) {
114
    /* Write the buffer to the socket */
115
0
    result = Curl_xfer_send(data, sptr, write_len, FALSE, &bytes_written);
116
117
0
    if(result)
118
0
      break;
119
120
0
    Curl_debug(data, CURLINFO_DATA_OUT, sptr, bytes_written);
121
122
0
    if(bytes_written != write_len) {
123
      /* if not all was written at once, we must advance the pointer, decrease
124
         the size left and try again! */
125
0
      write_len -= bytes_written;
126
0
      sptr += bytes_written;
127
0
    }
128
0
    else
129
0
      break;
130
0
  }
131
132
0
  curlx_free(s); /* free the output string */
133
134
0
  return result;
135
0
}
136
137
static CURLcode dict_do(struct Curl_easy *data, bool *done)
138
0
{
139
0
  char *word;
140
0
  char *eword = NULL;
141
0
  char *ppath;
142
0
  char *database = NULL;
143
0
  char *strategy = NULL;
144
0
  char *nthdef = NULL; /* This is not part of the protocol, but required
145
                          by RFC 2229 */
146
0
  CURLcode result;
147
148
0
  char *path;
149
150
0
  *done = TRUE; /* unconditionally */
151
152
  /* url-decode path before further evaluation */
153
0
  result = Curl_urldecode(data->state.up.path, 0, &path, NULL, REJECT_CTRL);
154
0
  if(result)
155
0
    return result;
156
157
0
  if(curl_strnequal(path, DICT_MATCH, sizeof(DICT_MATCH) - 1) ||
158
0
     curl_strnequal(path, DICT_MATCH2, sizeof(DICT_MATCH2) - 1) ||
159
0
     curl_strnequal(path, DICT_MATCH3, sizeof(DICT_MATCH3) - 1)) {
160
161
0
    word = strchr(path, ':');
162
0
    if(word) {
163
0
      word++;
164
0
      database = strchr(word, ':');
165
0
      if(database) {
166
0
        *database++ = (char)0;
167
0
        strategy = strchr(database, ':');
168
0
        if(strategy) {
169
0
          *strategy++ = (char)0;
170
0
          nthdef = strchr(strategy, ':');
171
0
          if(nthdef) {
172
0
            *nthdef = (char)0;
173
0
          }
174
0
        }
175
0
      }
176
0
    }
177
178
0
    if(!word || (*word == (char)0)) {
179
0
      infof(data, "lookup word is missing");
180
0
    }
181
0
    eword = unescape_word((!word || (*word == (char)0)) ? "default" : word);
182
0
    if(!eword) {
183
0
      result = CURLE_OUT_OF_MEMORY;
184
0
      goto error;
185
0
    }
186
187
0
    result = sendf(data,
188
0
                   "CLIENT " LIBCURL_NAME " " LIBCURL_VERSION "\r\n"
189
0
                   "MATCH "
190
0
                   "%s "    /* database */
191
0
                   "%s "    /* strategy */
192
0
                   "%s\r\n" /* word */
193
0
                   "QUIT\r\n",
194
0
                   (!database || (*database == (char)0)) ? "!" : database,
195
0
                   (!strategy || (*strategy == (char)0)) ? "." : strategy,
196
0
                   eword);
197
198
0
    if(result) {
199
0
      failf(data, "Failed sending DICT request");
200
0
      goto error;
201
0
    }
202
0
    Curl_xfer_setup_recv(data, FIRSTSOCKET, -1);
203
0
  }
204
0
  else if(curl_strnequal(path, DICT_DEFINE, sizeof(DICT_DEFINE) - 1) ||
205
0
          curl_strnequal(path, DICT_DEFINE2, sizeof(DICT_DEFINE2) - 1) ||
206
0
          curl_strnequal(path, DICT_DEFINE3, sizeof(DICT_DEFINE3) - 1)) {
207
208
0
    word = strchr(path, ':');
209
0
    if(word) {
210
0
      word++;
211
0
      database = strchr(word, ':');
212
0
      if(database) {
213
0
        *database++ = (char)0;
214
0
        nthdef = strchr(database, ':');
215
0
        if(nthdef) {
216
0
          *nthdef = (char)0;
217
0
        }
218
0
      }
219
0
    }
220
221
0
    if(!word || (*word == (char)0)) {
222
0
      infof(data, "lookup word is missing");
223
0
    }
224
0
    eword = unescape_word((!word || (*word == (char)0)) ? "default" : word);
225
0
    if(!eword) {
226
0
      result = CURLE_OUT_OF_MEMORY;
227
0
      goto error;
228
0
    }
229
230
0
    result = sendf(data,
231
0
                   "CLIENT " LIBCURL_NAME " " LIBCURL_VERSION "\r\n"
232
0
                   "DEFINE "
233
0
                   "%s "     /* database */
234
0
                   "%s\r\n"  /* word */
235
0
                   "QUIT\r\n",
236
0
                   (!database || (*database == (char)0)) ? "!" : database,
237
0
                   eword);
238
239
0
    if(result) {
240
0
      failf(data, "Failed sending DICT request");
241
0
      goto error;
242
0
    }
243
0
    Curl_xfer_setup_recv(data, FIRSTSOCKET, -1);
244
0
  }
245
0
  else {
246
247
0
    ppath = strchr(path, '/');
248
0
    if(ppath) {
249
0
      int i;
250
251
0
      ppath++;
252
0
      for(i = 0; ppath[i]; i++) {
253
0
        if(ppath[i] == ':')
254
0
          ppath[i] = ' ';
255
0
      }
256
0
      result = sendf(data,
257
0
                     "CLIENT " LIBCURL_NAME " " LIBCURL_VERSION "\r\n"
258
0
                     "%s\r\n"
259
0
                     "QUIT\r\n", ppath);
260
0
      if(result) {
261
0
        failf(data, "Failed sending DICT request");
262
0
        goto error;
263
0
      }
264
265
0
      Curl_xfer_setup_recv(data, FIRSTSOCKET, -1);
266
0
    }
267
0
  }
268
269
0
error:
270
0
  curlx_free(eword);
271
0
  curlx_free(path);
272
0
  return result;
273
0
}
274
275
/*
276
 * DICT protocol
277
 */
278
static const struct Curl_protocol Curl_protocol_dict = {
279
  ZERO_NULL,                            /* setup_connection */
280
  dict_do,                              /* do_it */
281
  ZERO_NULL,                            /* done */
282
  ZERO_NULL,                            /* do_more */
283
  ZERO_NULL,                            /* connect_it */
284
  ZERO_NULL,                            /* connecting */
285
  ZERO_NULL,                            /* doing */
286
  ZERO_NULL,                            /* proto_pollset */
287
  ZERO_NULL,                            /* doing_pollset */
288
  ZERO_NULL,                            /* domore_pollset */
289
  ZERO_NULL,                            /* perform_pollset */
290
  ZERO_NULL,                            /* disconnect */
291
  ZERO_NULL,                            /* write_resp */
292
  ZERO_NULL,                            /* write_resp_hd */
293
  ZERO_NULL,                            /* connection_check */
294
  ZERO_NULL,                            /* attach connection */
295
  ZERO_NULL,                            /* follow */
296
};
297
298
#endif /* CURL_DISABLE_DICT */
299
300
/*
301
 * DICT protocol handler.
302
 */
303
const struct Curl_scheme Curl_scheme_dict = {
304
  "dict",                               /* scheme */
305
#ifdef CURL_DISABLE_DICT
306
  ZERO_NULL,
307
#else
308
  &Curl_protocol_dict,
309
#endif
310
  CURLPROTO_DICT,                       /* protocol */
311
  CURLPROTO_DICT,                       /* family */
312
  PROTOPT_NONE | PROTOPT_NOURLQUERY,    /* flags */
313
  PORT_DICT,                            /* defport */
314
};