2
0

dict.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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. ***************************************************************************/
  22. #include "curl_setup.h"
  23. #ifndef CURL_DISABLE_DICT
  24. #ifdef HAVE_NETINET_IN_H
  25. #include <netinet/in.h>
  26. #endif
  27. #ifdef HAVE_NETDB_H
  28. #include <netdb.h>
  29. #endif
  30. #ifdef HAVE_ARPA_INET_H
  31. #include <arpa/inet.h>
  32. #endif
  33. #ifdef HAVE_NET_IF_H
  34. #include <net/if.h>
  35. #endif
  36. #ifdef HAVE_SYS_IOCTL_H
  37. #include <sys/ioctl.h>
  38. #endif
  39. #ifdef HAVE_SYS_PARAM_H
  40. #include <sys/param.h>
  41. #endif
  42. #ifdef HAVE_SYS_SELECT_H
  43. #include <sys/select.h>
  44. #elif defined(HAVE_UNISTD_H)
  45. #include <unistd.h>
  46. #endif
  47. #include "urldata.h"
  48. #include <curl/curl.h>
  49. #include "transfer.h"
  50. #include "sendf.h"
  51. #include "escape.h"
  52. #include "progress.h"
  53. #include "dict.h"
  54. #include "curl_printf.h"
  55. #include "strcase.h"
  56. #include "curl_memory.h"
  57. /* The last #include file should be: */
  58. #include "memdebug.h"
  59. /*
  60. * Forward declarations.
  61. */
  62. static CURLcode dict_do(struct Curl_easy *data, bool *done);
  63. /*
  64. * DICT protocol handler.
  65. */
  66. const struct Curl_handler Curl_handler_dict = {
  67. "DICT", /* scheme */
  68. ZERO_NULL, /* setup_connection */
  69. dict_do, /* do_it */
  70. ZERO_NULL, /* done */
  71. ZERO_NULL, /* do_more */
  72. ZERO_NULL, /* connect_it */
  73. ZERO_NULL, /* connecting */
  74. ZERO_NULL, /* doing */
  75. ZERO_NULL, /* proto_getsock */
  76. ZERO_NULL, /* doing_getsock */
  77. ZERO_NULL, /* domore_getsock */
  78. ZERO_NULL, /* perform_getsock */
  79. ZERO_NULL, /* disconnect */
  80. ZERO_NULL, /* readwrite */
  81. ZERO_NULL, /* connection_check */
  82. ZERO_NULL, /* attach connection */
  83. PORT_DICT, /* defport */
  84. CURLPROTO_DICT, /* protocol */
  85. CURLPROTO_DICT, /* family */
  86. PROTOPT_NONE | PROTOPT_NOURLQUERY /* flags */
  87. };
  88. static char *unescape_word(const char *inputbuff)
  89. {
  90. char *newp = NULL;
  91. char *dictp;
  92. size_t len;
  93. CURLcode result = Curl_urldecode(inputbuff, 0, &newp, &len,
  94. REJECT_NADA);
  95. if(!newp || result)
  96. return NULL;
  97. dictp = malloc(len*2 + 1); /* add one for terminating zero */
  98. if(dictp) {
  99. char *ptr;
  100. char ch;
  101. int olen = 0;
  102. /* According to RFC2229 section 2.2, these letters need to be escaped with
  103. \[letter] */
  104. for(ptr = newp;
  105. (ch = *ptr) != 0;
  106. ptr++) {
  107. if((ch <= 32) || (ch == 127) ||
  108. (ch == '\'') || (ch == '\"') || (ch == '\\')) {
  109. dictp[olen++] = '\\';
  110. }
  111. dictp[olen++] = ch;
  112. }
  113. dictp[olen] = 0;
  114. }
  115. free(newp);
  116. return dictp;
  117. }
  118. /* sendf() sends formatted data to the server */
  119. static CURLcode sendf(curl_socket_t sockfd, struct Curl_easy *data,
  120. const char *fmt, ...)
  121. {
  122. ssize_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 = vaprintf(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_write(data, sockfd, sptr, write_len, &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;
  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 = CURLE_OK;
  164. struct connectdata *conn = data->conn;
  165. curl_socket_t sockfd = conn->sock[FIRSTSOCKET];
  166. char *path = data->state.up.path;
  167. *done = TRUE; /* unconditionally */
  168. if(strncasecompare(path, DICT_MATCH, sizeof(DICT_MATCH)-1) ||
  169. strncasecompare(path, DICT_MATCH2, sizeof(DICT_MATCH2)-1) ||
  170. strncasecompare(path, DICT_MATCH3, sizeof(DICT_MATCH3)-1)) {
  171. word = strchr(path, ':');
  172. if(word) {
  173. word++;
  174. database = strchr(word, ':');
  175. if(database) {
  176. *database++ = (char)0;
  177. strategy = strchr(database, ':');
  178. if(strategy) {
  179. *strategy++ = (char)0;
  180. nthdef = strchr(strategy, ':');
  181. if(nthdef) {
  182. *nthdef = (char)0;
  183. }
  184. }
  185. }
  186. }
  187. if(!word || (*word == (char)0)) {
  188. infof(data, "lookup word is missing");
  189. word = (char *)"default";
  190. }
  191. if(!database || (*database == (char)0)) {
  192. database = (char *)"!";
  193. }
  194. if(!strategy || (*strategy == (char)0)) {
  195. strategy = (char *)".";
  196. }
  197. eword = unescape_word(word);
  198. if(!eword)
  199. return CURLE_OUT_OF_MEMORY;
  200. result = sendf(sockfd, data,
  201. "CLIENT " LIBCURL_NAME " " LIBCURL_VERSION "\r\n"
  202. "MATCH "
  203. "%s " /* database */
  204. "%s " /* strategy */
  205. "%s\r\n" /* word */
  206. "QUIT\r\n",
  207. database,
  208. strategy,
  209. eword);
  210. free(eword);
  211. if(result) {
  212. failf(data, "Failed sending DICT request");
  213. return result;
  214. }
  215. Curl_setup_transfer(data, FIRSTSOCKET, -1, FALSE, -1); /* no upload */
  216. }
  217. else if(strncasecompare(path, DICT_DEFINE, sizeof(DICT_DEFINE)-1) ||
  218. strncasecompare(path, DICT_DEFINE2, sizeof(DICT_DEFINE2)-1) ||
  219. strncasecompare(path, DICT_DEFINE3, sizeof(DICT_DEFINE3)-1)) {
  220. word = strchr(path, ':');
  221. if(word) {
  222. word++;
  223. database = strchr(word, ':');
  224. if(database) {
  225. *database++ = (char)0;
  226. nthdef = strchr(database, ':');
  227. if(nthdef) {
  228. *nthdef = (char)0;
  229. }
  230. }
  231. }
  232. if(!word || (*word == (char)0)) {
  233. infof(data, "lookup word is missing");
  234. word = (char *)"default";
  235. }
  236. if(!database || (*database == (char)0)) {
  237. database = (char *)"!";
  238. }
  239. eword = unescape_word(word);
  240. if(!eword)
  241. return CURLE_OUT_OF_MEMORY;
  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. free(eword);
  251. if(result) {
  252. failf(data, "Failed sending DICT request");
  253. return result;
  254. }
  255. Curl_setup_transfer(data, FIRSTSOCKET, -1, FALSE, -1);
  256. }
  257. else {
  258. ppath = strchr(path, '/');
  259. if(ppath) {
  260. int i;
  261. ppath++;
  262. for(i = 0; ppath[i]; i++) {
  263. if(ppath[i] == ':')
  264. ppath[i] = ' ';
  265. }
  266. result = sendf(sockfd, data,
  267. "CLIENT " LIBCURL_NAME " " LIBCURL_VERSION "\r\n"
  268. "%s\r\n"
  269. "QUIT\r\n", ppath);
  270. if(result) {
  271. failf(data, "Failed sending DICT request");
  272. return result;
  273. }
  274. Curl_setup_transfer(data, FIRSTSOCKET, -1, FALSE, -1);
  275. }
  276. }
  277. return CURLE_OK;
  278. }
  279. #endif /*CURL_DISABLE_DICT*/