Coverage Report

Created: 2026-10-15 06:20

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
88
#define DICT_MATCH   "/MATCH:"
63
88
#define DICT_MATCH2  "/M:"
64
53
#define DICT_MATCH3  "/FIND:"
65
53
#define DICT_DEFINE  "/DEFINE:"
66
53
#define DICT_DEFINE2 "/D:"
67
44
#define DICT_DEFINE3 "/LOOKUP:"
68
69
44
#define DYN_DICT_WORD 10000
70
static char *unescape_word(const char *input)
71
44
{
72
44
  struct dynbuf out;
73
44
  const char *ptr;
74
44
  CURLcode result = CURLE_OK;
75
44
  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
1.35k
  for(ptr = input; *ptr; ptr++) {
80
1.31k
    char ch = *ptr;
81
1.31k
    if((ch <= 32) || (ch == 127) ||
82
994
       (ch == '\'') || (ch == '\"') || (ch == '\\'))
83
975
      result = curlx_dyn_addn(&out, "\\", 1);
84
1.31k
    if(!result)
85
1.31k
      result = curlx_dyn_addn(&out, ptr, 1);
86
1.31k
    if(result)
87
0
      return NULL;
88
1.31k
  }
89
44
  return curlx_dyn_ptr(&out);
90
44
}
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
88
{
98
88
  curl_socket_t sockfd = data->conn->sock[FIRSTSOCKET];
99
88
  size_t bytes_written;
100
88
  size_t write_len;
101
88
  CURLcode result = CURLE_OK;
102
88
  timediff_t timeout_ms;
103
88
  int what;
104
88
  char *s;
105
88
  char *sptr;
106
88
  va_list ap;
107
88
  va_start(ap, fmt);
108
88
  s = curl_mvaprintf(fmt, ap); /* returns an allocated string */
109
88
  va_end(ap);
110
88
  if(!s)
111
0
    return CURLE_OUT_OF_MEMORY; /* failure */
112
113
88
  bytes_written = 0;
114
88
  write_len = strlen(s);
115
88
  sptr = s;
116
117
88
  for(;;) {
118
    /* Write the buffer to the socket */
119
88
    result = Curl_xfer_send(data, sptr, write_len, FALSE, &bytes_written);
120
121
88
    if(result)
122
0
      break;
123
124
88
    Curl_debug(data, CURLINFO_DATA_OUT, sptr, bytes_written);
125
126
88
    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
88
    else
133
88
      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
88
  curlx_free(s); /* free the output string */
160
161
88
  return result;
162
88
}
163
164
static CURLcode dict_do(struct Curl_easy *data, bool *done)
165
90
{
166
90
  char *word;
167
90
  char *eword = NULL;
168
90
  char *ppath;
169
90
  char *database = NULL;
170
90
  char *strategy = NULL;
171
90
  char *nthdef = NULL; /* This is not part of the protocol, but required
172
                          by RFC 2229 */
173
90
  CURLcode result;
174
175
90
  char *path;
176
177
90
  *done = TRUE; /* unconditionally */
178
179
  /* URL-decode path before further evaluation */
180
90
  result = Curl_urldecode(data->state.up.path, 0, &path, NULL, REJECT_CTRL);
181
90
  if(result)
182
2
    return result;
183
184
88
  if(curl_strnequal(path, DICT_MATCH, CURL_CSTRLEN(DICT_MATCH)) ||
185
88
     curl_strnequal(path, DICT_MATCH2, CURL_CSTRLEN(DICT_MATCH2)) ||
186
53
     curl_strnequal(path, DICT_MATCH3, CURL_CSTRLEN(DICT_MATCH3))) {
187
188
35
    word = strchr(path, ':');
189
35
    if(word) {
190
35
      word++;
191
35
      database = strchr(word, ':');
192
35
      if(database) {
193
14
        *database++ = (char)0;
194
14
        strategy = strchr(database, ':');
195
14
        if(strategy) {
196
6
          *strategy++ = (char)0;
197
6
          nthdef = strchr(strategy, ':');
198
6
          if(nthdef) {
199
1
            *nthdef = (char)0;
200
1
          }
201
6
        }
202
14
      }
203
35
    }
204
205
35
    if(!word || (*word == (char)0)) {
206
1
      infof(data, "lookup word is missing");
207
1
    }
208
35
    eword = unescape_word((!word || (*word == (char)0)) ? "default" : word);
209
35
    if(!eword) {
210
0
      result = CURLE_OUT_OF_MEMORY;
211
0
      goto error;
212
0
    }
213
214
35
    result = sendf(data,
215
35
                   "CLIENT " LIBCURL_NAME " " LIBCURL_VERSION "\r\n"
216
35
                   "MATCH "
217
35
                   "%s "    /* database */
218
35
                   "%s "    /* strategy */
219
35
                   "%s\r\n" /* word */
220
35
                   "QUIT\r\n",
221
35
                   (!database || (*database == (char)0)) ? "!" : database,
222
35
                   (!strategy || (*strategy == (char)0)) ? "." : strategy,
223
35
                   eword);
224
225
35
    if(result) {
226
0
      failf(data, "Failed sending DICT request");
227
0
      goto error;
228
0
    }
229
35
    Curl_xfer_setup_recv(data, FIRSTSOCKET, -1);
230
35
  }
231
53
  else if(curl_strnequal(path, DICT_DEFINE, CURL_CSTRLEN(DICT_DEFINE)) ||
232
53
          curl_strnequal(path, DICT_DEFINE2, CURL_CSTRLEN(DICT_DEFINE2)) ||
233
44
          curl_strnequal(path, DICT_DEFINE3, CURL_CSTRLEN(DICT_DEFINE3))) {
234
235
9
    word = strchr(path, ':');
236
9
    if(word) {
237
9
      word++;
238
9
      database = strchr(word, ':');
239
9
      if(database) {
240
4
        *database++ = (char)0;
241
4
        nthdef = strchr(database, ':');
242
4
        if(nthdef) {
243
1
          *nthdef = (char)0;
244
1
        }
245
4
      }
246
9
    }
247
248
9
    if(!word || (*word == (char)0)) {
249
1
      infof(data, "lookup word is missing");
250
1
    }
251
9
    eword = unescape_word((!word || (*word == (char)0)) ? "default" : word);
252
9
    if(!eword) {
253
0
      result = CURLE_OUT_OF_MEMORY;
254
0
      goto error;
255
0
    }
256
257
9
    result = sendf(data,
258
9
                   "CLIENT " LIBCURL_NAME " " LIBCURL_VERSION "\r\n"
259
9
                   "DEFINE "
260
9
                   "%s "     /* database */
261
9
                   "%s\r\n"  /* word */
262
9
                   "QUIT\r\n",
263
9
                   (!database || (*database == (char)0)) ? "!" : database,
264
9
                   eword);
265
266
9
    if(result) {
267
0
      failf(data, "Failed sending DICT request");
268
0
      goto error;
269
0
    }
270
9
    Curl_xfer_setup_recv(data, FIRSTSOCKET, -1);
271
9
  }
272
44
  else {
273
274
44
    ppath = strchr(path, '/');
275
44
    if(ppath) {
276
44
      int i;
277
278
44
      ppath++;
279
633
      for(i = 0; ppath[i]; i++) {
280
589
        if(ppath[i] == ':')
281
274
          ppath[i] = ' ';
282
589
      }
283
44
      result = sendf(data,
284
44
                     "CLIENT " LIBCURL_NAME " " LIBCURL_VERSION "\r\n"
285
44
                     "%s\r\n"
286
44
                     "QUIT\r\n", ppath);
287
44
      if(result) {
288
0
        failf(data, "Failed sending DICT request");
289
0
        goto error;
290
0
      }
291
292
44
      Curl_xfer_setup_recv(data, FIRSTSOCKET, -1);
293
44
    }
294
44
  }
295
296
88
error:
297
88
  curlx_free(eword);
298
88
  curlx_free(path);
299
88
  return result;
300
88
}
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 */