dict.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2022, 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. static char *unescape_word(const char *inputbuff)
  91. {
  92. char *newp = NULL;
  93. char *dictp;
  94. size_t len;
  95. CURLcode result = Curl_urldecode(inputbuff, 0, &newp, &len,
  96. REJECT_NADA);
  97. if(!newp || result)
  98. return NULL;
  99. dictp = malloc(len*2 + 1); /* add one for terminating zero */
  100. if(dictp) {
  101. char *ptr;
  102. char ch;
  103. int olen = 0;
  104. /* According to RFC2229 section 2.2, these letters need to be escaped with
  105. \[letter] */
  106. for(ptr = newp;
  107. (ch = *ptr) != 0;
  108. ptr++) {
  109. if((ch <= 32) || (ch == 127) ||
  110. (ch == '\'') || (ch == '\"') || (ch == '\\')) {
  111. dictp[olen++] = '\\';
  112. }
  113. dictp[olen++] = ch;
  114. }
  115. dictp[olen] = 0;
  116. }
  117. free(newp);
  118. return dictp;
  119. }
  120. /* sendf() sends formatted data to the server */
  121. static CURLcode sendf(curl_socket_t sockfd, struct Curl_easy *data,
  122. const char *fmt, ...)
  123. {
  124. ssize_t bytes_written;
  125. size_t write_len;
  126. CURLcode result = CURLE_OK;
  127. char *s;
  128. char *sptr;
  129. va_list ap;
  130. va_start(ap, fmt);
  131. s = vaprintf(fmt, ap); /* returns an allocated string */
  132. va_end(ap);
  133. if(!s)
  134. return CURLE_OUT_OF_MEMORY; /* failure */
  135. bytes_written = 0;
  136. write_len = strlen(s);
  137. sptr = s;
  138. for(;;) {
  139. /* Write the buffer to the socket */
  140. result = Curl_write(data, sockfd, sptr, write_len, &bytes_written);
  141. if(result)
  142. break;
  143. Curl_debug(data, CURLINFO_DATA_OUT, sptr, (size_t)bytes_written);
  144. if((size_t)bytes_written != write_len) {
  145. /* if not all was written at once, we must advance the pointer, decrease
  146. the size left and try again! */
  147. write_len -= bytes_written;
  148. sptr += bytes_written;
  149. }
  150. else
  151. break;
  152. }
  153. free(s); /* free the output string */
  154. return result;
  155. }
  156. static CURLcode dict_do(struct Curl_easy *data, bool *done)
  157. {
  158. char *word;
  159. char *eword;
  160. char *ppath;
  161. char *database = NULL;
  162. char *strategy = NULL;
  163. char *nthdef = NULL; /* This is not part of the protocol, but required
  164. by RFC 2229 */
  165. CURLcode result = CURLE_OK;
  166. struct connectdata *conn = data->conn;
  167. curl_socket_t sockfd = conn->sock[FIRSTSOCKET];
  168. char *path = data->state.up.path;
  169. *done = TRUE; /* unconditionally */
  170. if(strncasecompare(path, DICT_MATCH, sizeof(DICT_MATCH)-1) ||
  171. strncasecompare(path, DICT_MATCH2, sizeof(DICT_MATCH2)-1) ||
  172. strncasecompare(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. word = (char *)"default";
  192. }
  193. if(!database || (*database == (char)0)) {
  194. database = (char *)"!";
  195. }
  196. if(!strategy || (*strategy == (char)0)) {
  197. strategy = (char *)".";
  198. }
  199. eword = unescape_word(word);
  200. if(!eword)
  201. return CURLE_OUT_OF_MEMORY;
  202. result = sendf(sockfd, data,
  203. "CLIENT " LIBCURL_NAME " " LIBCURL_VERSION "\r\n"
  204. "MATCH "
  205. "%s " /* database */
  206. "%s " /* strategy */
  207. "%s\r\n" /* word */
  208. "QUIT\r\n",
  209. database,
  210. strategy,
  211. eword);
  212. free(eword);
  213. if(result) {
  214. failf(data, "Failed sending DICT request");
  215. return result;
  216. }
  217. Curl_setup_transfer(data, FIRSTSOCKET, -1, FALSE, -1); /* no upload */
  218. }
  219. else if(strncasecompare(path, DICT_DEFINE, sizeof(DICT_DEFINE)-1) ||
  220. strncasecompare(path, DICT_DEFINE2, sizeof(DICT_DEFINE2)-1) ||
  221. strncasecompare(path, DICT_DEFINE3, sizeof(DICT_DEFINE3)-1)) {
  222. word = strchr(path, ':');
  223. if(word) {
  224. word++;
  225. database = strchr(word, ':');
  226. if(database) {
  227. *database++ = (char)0;
  228. nthdef = strchr(database, ':');
  229. if(nthdef) {
  230. *nthdef = (char)0;
  231. }
  232. }
  233. }
  234. if(!word || (*word == (char)0)) {
  235. infof(data, "lookup word is missing");
  236. word = (char *)"default";
  237. }
  238. if(!database || (*database == (char)0)) {
  239. database = (char *)"!";
  240. }
  241. eword = unescape_word(word);
  242. if(!eword)
  243. return CURLE_OUT_OF_MEMORY;
  244. result = sendf(sockfd, data,
  245. "CLIENT " LIBCURL_NAME " " LIBCURL_VERSION "\r\n"
  246. "DEFINE "
  247. "%s " /* database */
  248. "%s\r\n" /* word */
  249. "QUIT\r\n",
  250. database,
  251. eword);
  252. free(eword);
  253. if(result) {
  254. failf(data, "Failed sending DICT request");
  255. return result;
  256. }
  257. Curl_setup_transfer(data, FIRSTSOCKET, -1, FALSE, -1);
  258. }
  259. else {
  260. ppath = strchr(path, '/');
  261. if(ppath) {
  262. int i;
  263. ppath++;
  264. for(i = 0; ppath[i]; i++) {
  265. if(ppath[i] == ':')
  266. ppath[i] = ' ';
  267. }
  268. result = sendf(sockfd, data,
  269. "CLIENT " LIBCURL_NAME " " LIBCURL_VERSION "\r\n"
  270. "%s\r\n"
  271. "QUIT\r\n", ppath);
  272. if(result) {
  273. failf(data, "Failed sending DICT request");
  274. return result;
  275. }
  276. Curl_setup_transfer(data, FIRSTSOCKET, -1, FALSE, -1);
  277. }
  278. }
  279. return CURLE_OK;
  280. }
  281. #endif /*CURL_DISABLE_DICT*/