Coverage Report

Created: 2025-12-14 06:23

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