Coverage Report

Created: 2026-09-01 06:58

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