dict.c 8.5 KB

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