2
0

dict.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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, /* readwrite */
  83. ZERO_NULL, /* connection_check */
  84. ZERO_NULL, /* attach connection */
  85. PORT_DICT, /* defport */
  86. CURLPROTO_DICT, /* protocol */
  87. CURLPROTO_DICT, /* family */
  88. PROTOPT_NONE | PROTOPT_NOURLQUERY /* flags */
  89. };
  90. #define DYN_DICT_WORD 10000
  91. static char *unescape_word(const char *input)
  92. {
  93. struct dynbuf out;
  94. const char *ptr;
  95. CURLcode result = CURLE_OK;
  96. Curl_dyn_init(&out, DYN_DICT_WORD);
  97. /* According to RFC2229 section 2.2, these letters need to be escaped with
  98. \[letter] */
  99. for(ptr = input; *ptr; ptr++) {
  100. char ch = *ptr;
  101. if((ch <= 32) || (ch == 127) ||
  102. (ch == '\'') || (ch == '\"') || (ch == '\\'))
  103. result = Curl_dyn_addn(&out, "\\", 1);
  104. if(!result)
  105. result = Curl_dyn_addn(&out, ptr, 1);
  106. if(result)
  107. return NULL;
  108. }
  109. return Curl_dyn_ptr(&out);
  110. }
  111. /* sendf() sends formatted data to the server */
  112. static CURLcode sendf(curl_socket_t sockfd, struct Curl_easy *data,
  113. const char *fmt, ...)
  114. {
  115. ssize_t bytes_written;
  116. size_t write_len;
  117. CURLcode result = CURLE_OK;
  118. char *s;
  119. char *sptr;
  120. va_list ap;
  121. va_start(ap, fmt);
  122. s = vaprintf(fmt, ap); /* returns an allocated string */
  123. va_end(ap);
  124. if(!s)
  125. return CURLE_OUT_OF_MEMORY; /* failure */
  126. bytes_written = 0;
  127. write_len = strlen(s);
  128. sptr = s;
  129. for(;;) {
  130. /* Write the buffer to the socket */
  131. result = Curl_write(data, sockfd, sptr, write_len, &bytes_written);
  132. if(result)
  133. break;
  134. Curl_debug(data, CURLINFO_DATA_OUT, sptr, (size_t)bytes_written);
  135. if((size_t)bytes_written != write_len) {
  136. /* if not all was written at once, we must advance the pointer, decrease
  137. the size left and try again! */
  138. write_len -= bytes_written;
  139. sptr += bytes_written;
  140. }
  141. else
  142. break;
  143. }
  144. free(s); /* free the output string */
  145. return result;
  146. }
  147. static CURLcode dict_do(struct Curl_easy *data, bool *done)
  148. {
  149. char *word;
  150. char *eword = NULL;
  151. char *ppath;
  152. char *database = NULL;
  153. char *strategy = NULL;
  154. char *nthdef = NULL; /* This is not part of the protocol, but required
  155. by RFC 2229 */
  156. CURLcode result;
  157. struct connectdata *conn = data->conn;
  158. curl_socket_t sockfd = conn->sock[FIRSTSOCKET];
  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(sockfd, 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_setup_transfer(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(sockfd, 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_setup_transfer(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(sockfd, 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_setup_transfer(data, FIRSTSOCKET, -1, FALSE, -1);
  274. }
  275. }
  276. error:
  277. free(eword);
  278. free(path);
  279. return result;
  280. }
  281. #endif /* CURL_DISABLE_DICT */