Coverage Report

Created: 2024-02-25 06:14

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