dict.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) Daniel Stenberg, <[email protected]>, 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 "curl_memory.h"
  58. /* The last #include file should be: */
  59. #include "memdebug.h"
  60. #define DICT_MATCH "/MATCH:"
  61. #define DICT_MATCH2 "/M:"
  62. #define DICT_MATCH3 "/FIND:"
  63. #define DICT_DEFINE "/DEFINE:"
  64. #define DICT_DEFINE2 "/D:"
  65. #define DICT_DEFINE3 "/LOOKUP:"
  66. /*
  67. * Forward declarations.
  68. */
  69. static CURLcode dict_do(struct Curl_easy *data, bool *done);
  70. /*
  71. * DICT protocol handler.
  72. */
  73. const struct Curl_handler Curl_handler_dict = {
  74. "dict", /* scheme */
  75. ZERO_NULL, /* setup_connection */
  76. dict_do, /* do_it */
  77. ZERO_NULL, /* done */
  78. ZERO_NULL, /* do_more */
  79. ZERO_NULL, /* connect_it */
  80. ZERO_NULL, /* connecting */
  81. ZERO_NULL, /* doing */
  82. ZERO_NULL, /* proto_pollset */
  83. ZERO_NULL, /* doing_pollset */
  84. ZERO_NULL, /* domore_pollset */
  85. ZERO_NULL, /* perform_pollset */
  86. ZERO_NULL, /* disconnect */
  87. ZERO_NULL, /* write_resp */
  88. ZERO_NULL, /* write_resp_hd */
  89. ZERO_NULL, /* connection_check */
  90. ZERO_NULL, /* attach connection */
  91. ZERO_NULL, /* follow */
  92. PORT_DICT, /* defport */
  93. CURLPROTO_DICT, /* protocol */
  94. CURLPROTO_DICT, /* family */
  95. PROTOPT_NONE | PROTOPT_NOURLQUERY /* flags */
  96. };
  97. #define DYN_DICT_WORD 10000
  98. static char *unescape_word(const char *input)
  99. {
  100. struct dynbuf out;
  101. const char *ptr;
  102. CURLcode result = CURLE_OK;
  103. curlx_dyn_init(&out, DYN_DICT_WORD);
  104. /* According to RFC2229 section 2.2, these letters need to be escaped with
  105. \[letter] */
  106. for(ptr = input; *ptr; ptr++) {
  107. char ch = *ptr;
  108. if((ch <= 32) || (ch == 127) ||
  109. (ch == '\'') || (ch == '\"') || (ch == '\\'))
  110. result = curlx_dyn_addn(&out, "\\", 1);
  111. if(!result)
  112. result = curlx_dyn_addn(&out, ptr, 1);
  113. if(result)
  114. return NULL;
  115. }
  116. return curlx_dyn_ptr(&out);
  117. }
  118. /* sendf() sends formatted data to the server */
  119. static CURLcode sendf(struct Curl_easy *data,
  120. const char *fmt, ...) CURL_PRINTF(2, 3);
  121. static CURLcode sendf(struct Curl_easy *data, const char *fmt, ...)
  122. {
  123. size_t bytes_written;
  124. size_t write_len;
  125. CURLcode result = CURLE_OK;
  126. char *s;
  127. char *sptr;
  128. va_list ap;
  129. va_start(ap, fmt);
  130. s = vaprintf(fmt, ap); /* returns an allocated string */
  131. va_end(ap);
  132. if(!s)
  133. return CURLE_OUT_OF_MEMORY; /* failure */
  134. bytes_written = 0;
  135. write_len = strlen(s);
  136. sptr = s;
  137. for(;;) {
  138. /* Write the buffer to the socket */
  139. result = Curl_xfer_send(data, sptr, write_len, FALSE, &bytes_written);
  140. if(result)
  141. break;
  142. Curl_debug(data, CURLINFO_DATA_OUT, sptr, (size_t)bytes_written);
  143. if((size_t)bytes_written != write_len) {
  144. /* if not all was written at once, we must advance the pointer, decrease
  145. the size left and try again! */
  146. write_len -= bytes_written;
  147. sptr += bytes_written;
  148. }
  149. else
  150. break;
  151. }
  152. free(s); /* free the output string */
  153. return result;
  154. }
  155. static CURLcode dict_do(struct Curl_easy *data, bool *done)
  156. {
  157. char *word;
  158. char *eword = NULL;
  159. char *ppath;
  160. char *database = NULL;
  161. char *strategy = NULL;
  162. char *nthdef = NULL; /* This is not part of the protocol, but required
  163. by RFC 2229 */
  164. CURLcode result;
  165. char *path;
  166. *done = TRUE; /* unconditionally */
  167. /* url-decode path before further evaluation */
  168. result = Curl_urldecode(data->state.up.path, 0, &path, NULL, REJECT_CTRL);
  169. if(result)
  170. return result;
  171. if(curl_strnequal(path, DICT_MATCH, sizeof(DICT_MATCH)-1) ||
  172. curl_strnequal(path, DICT_MATCH2, sizeof(DICT_MATCH2)-1) ||
  173. curl_strnequal(path, DICT_MATCH3, sizeof(DICT_MATCH3)-1)) {
  174. word = strchr(path, ':');
  175. if(word) {
  176. word++;
  177. database = strchr(word, ':');
  178. if(database) {
  179. *database++ = (char)0;
  180. strategy = strchr(database, ':');
  181. if(strategy) {
  182. *strategy++ = (char)0;
  183. nthdef = strchr(strategy, ':');
  184. if(nthdef) {
  185. *nthdef = (char)0;
  186. }
  187. }
  188. }
  189. }
  190. if(!word || (*word == (char)0)) {
  191. infof(data, "lookup word is missing");
  192. }
  193. eword = unescape_word((!word || (*word == (char)0)) ? "default" : word);
  194. if(!eword) {
  195. result = CURLE_OUT_OF_MEMORY;
  196. goto error;
  197. }
  198. result = sendf(data,
  199. "CLIENT " LIBCURL_NAME " " LIBCURL_VERSION "\r\n"
  200. "MATCH "
  201. "%s " /* database */
  202. "%s " /* strategy */
  203. "%s\r\n" /* word */
  204. "QUIT\r\n",
  205. (!database || (*database == (char)0)) ? "!" : database,
  206. (!strategy || (*strategy == (char)0)) ? "." : strategy,
  207. eword);
  208. if(result) {
  209. failf(data, "Failed sending DICT request");
  210. goto error;
  211. }
  212. Curl_xfer_setup_recv(data, FIRSTSOCKET, -1);
  213. }
  214. else if(curl_strnequal(path, DICT_DEFINE, sizeof(DICT_DEFINE)-1) ||
  215. curl_strnequal(path, DICT_DEFINE2, sizeof(DICT_DEFINE2)-1) ||
  216. curl_strnequal(path, DICT_DEFINE3, sizeof(DICT_DEFINE3)-1)) {
  217. word = strchr(path, ':');
  218. if(word) {
  219. word++;
  220. database = strchr(word, ':');
  221. if(database) {
  222. *database++ = (char)0;
  223. nthdef = strchr(database, ':');
  224. if(nthdef) {
  225. *nthdef = (char)0;
  226. }
  227. }
  228. }
  229. if(!word || (*word == (char)0)) {
  230. infof(data, "lookup word is missing");
  231. }
  232. eword = unescape_word((!word || (*word == (char)0)) ? "default" : word);
  233. if(!eword) {
  234. result = CURLE_OUT_OF_MEMORY;
  235. goto error;
  236. }
  237. result = sendf(data,
  238. "CLIENT " LIBCURL_NAME " " LIBCURL_VERSION "\r\n"
  239. "DEFINE "
  240. "%s " /* database */
  241. "%s\r\n" /* word */
  242. "QUIT\r\n",
  243. (!database || (*database == (char)0)) ? "!" : database,
  244. eword);
  245. if(result) {
  246. failf(data, "Failed sending DICT request");
  247. goto error;
  248. }
  249. Curl_xfer_setup_recv(data, FIRSTSOCKET, -1);
  250. }
  251. else {
  252. ppath = strchr(path, '/');
  253. if(ppath) {
  254. int i;
  255. ppath++;
  256. for(i = 0; ppath[i]; i++) {
  257. if(ppath[i] == ':')
  258. ppath[i] = ' ';
  259. }
  260. result = sendf(data,
  261. "CLIENT " LIBCURL_NAME " " LIBCURL_VERSION "\r\n"
  262. "%s\r\n"
  263. "QUIT\r\n", ppath);
  264. if(result) {
  265. failf(data, "Failed sending DICT request");
  266. goto error;
  267. }
  268. Curl_xfer_setup_recv(data, FIRSTSOCKET, -1);
  269. }
  270. }
  271. error:
  272. free(eword);
  273. free(path);
  274. return result;
  275. }
  276. #endif /* CURL_DISABLE_DICT */