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