dict.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2020, 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.haxx.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 "strcase.h"
  55. #include "curl_memory.h"
  56. /* The last #include file should be: */
  57. #include "memdebug.h"
  58. /*
  59. * Forward declarations.
  60. */
  61. static CURLcode dict_do(struct connectdata *conn, bool *done);
  62. /*
  63. * DICT protocol handler.
  64. */
  65. const struct Curl_handler Curl_handler_dict = {
  66. "DICT", /* scheme */
  67. ZERO_NULL, /* setup_connection */
  68. dict_do, /* do_it */
  69. ZERO_NULL, /* done */
  70. ZERO_NULL, /* do_more */
  71. ZERO_NULL, /* connect_it */
  72. ZERO_NULL, /* connecting */
  73. ZERO_NULL, /* doing */
  74. ZERO_NULL, /* proto_getsock */
  75. ZERO_NULL, /* doing_getsock */
  76. ZERO_NULL, /* domore_getsock */
  77. ZERO_NULL, /* perform_getsock */
  78. ZERO_NULL, /* disconnect */
  79. ZERO_NULL, /* readwrite */
  80. ZERO_NULL, /* connection_check */
  81. PORT_DICT, /* defport */
  82. CURLPROTO_DICT, /* protocol */
  83. PROTOPT_NONE | PROTOPT_NOURLQUERY /* flags */
  84. };
  85. static char *unescape_word(struct Curl_easy *data, const char *inputbuff)
  86. {
  87. char *newp = NULL;
  88. char *dictp;
  89. size_t len;
  90. CURLcode result = Curl_urldecode(data, inputbuff, 0, &newp, &len,
  91. REJECT_NADA);
  92. if(!newp || result)
  93. return NULL;
  94. dictp = malloc(len*2 + 1); /* add one for terminating zero */
  95. if(dictp) {
  96. char *ptr;
  97. char ch;
  98. int olen = 0;
  99. /* According to RFC2229 section 2.2, these letters need to be escaped with
  100. \[letter] */
  101. for(ptr = newp;
  102. (ch = *ptr) != 0;
  103. ptr++) {
  104. if((ch <= 32) || (ch == 127) ||
  105. (ch == '\'') || (ch == '\"') || (ch == '\\')) {
  106. dictp[olen++] = '\\';
  107. }
  108. dictp[olen++] = ch;
  109. }
  110. dictp[olen] = 0;
  111. }
  112. free(newp);
  113. return dictp;
  114. }
  115. static CURLcode dict_do(struct connectdata *conn, bool *done)
  116. {
  117. char *word;
  118. char *eword;
  119. char *ppath;
  120. char *database = NULL;
  121. char *strategy = NULL;
  122. char *nthdef = NULL; /* This is not part of the protocol, but required
  123. by RFC 2229 */
  124. CURLcode result = CURLE_OK;
  125. struct Curl_easy *data = conn->data;
  126. curl_socket_t sockfd = conn->sock[FIRSTSOCKET];
  127. char *path = data->state.up.path;
  128. *done = TRUE; /* unconditionally */
  129. if(conn->bits.user_passwd) {
  130. /* AUTH is missing */
  131. }
  132. if(strncasecompare(path, DICT_MATCH, sizeof(DICT_MATCH)-1) ||
  133. strncasecompare(path, DICT_MATCH2, sizeof(DICT_MATCH2)-1) ||
  134. strncasecompare(path, DICT_MATCH3, sizeof(DICT_MATCH3)-1)) {
  135. word = strchr(path, ':');
  136. if(word) {
  137. word++;
  138. database = strchr(word, ':');
  139. if(database) {
  140. *database++ = (char)0;
  141. strategy = strchr(database, ':');
  142. if(strategy) {
  143. *strategy++ = (char)0;
  144. nthdef = strchr(strategy, ':');
  145. if(nthdef) {
  146. *nthdef = (char)0;
  147. }
  148. }
  149. }
  150. }
  151. if((word == NULL) || (*word == (char)0)) {
  152. infof(data, "lookup word is missing\n");
  153. word = (char *)"default";
  154. }
  155. if((database == NULL) || (*database == (char)0)) {
  156. database = (char *)"!";
  157. }
  158. if((strategy == NULL) || (*strategy == (char)0)) {
  159. strategy = (char *)".";
  160. }
  161. eword = unescape_word(data, word);
  162. if(!eword)
  163. return CURLE_OUT_OF_MEMORY;
  164. result = Curl_sendf(sockfd, conn,
  165. "CLIENT " LIBCURL_NAME " " LIBCURL_VERSION "\r\n"
  166. "MATCH "
  167. "%s " /* database */
  168. "%s " /* strategy */
  169. "%s\r\n" /* word */
  170. "QUIT\r\n",
  171. database,
  172. strategy,
  173. eword
  174. );
  175. free(eword);
  176. if(result) {
  177. failf(data, "Failed sending DICT request");
  178. return result;
  179. }
  180. Curl_setup_transfer(data, FIRSTSOCKET, -1, FALSE, -1); /* no upload */
  181. }
  182. else if(strncasecompare(path, DICT_DEFINE, sizeof(DICT_DEFINE)-1) ||
  183. strncasecompare(path, DICT_DEFINE2, sizeof(DICT_DEFINE2)-1) ||
  184. strncasecompare(path, DICT_DEFINE3, sizeof(DICT_DEFINE3)-1)) {
  185. word = strchr(path, ':');
  186. if(word) {
  187. word++;
  188. database = strchr(word, ':');
  189. if(database) {
  190. *database++ = (char)0;
  191. nthdef = strchr(database, ':');
  192. if(nthdef) {
  193. *nthdef = (char)0;
  194. }
  195. }
  196. }
  197. if((word == NULL) || (*word == (char)0)) {
  198. infof(data, "lookup word is missing\n");
  199. word = (char *)"default";
  200. }
  201. if((database == NULL) || (*database == (char)0)) {
  202. database = (char *)"!";
  203. }
  204. eword = unescape_word(data, word);
  205. if(!eword)
  206. return CURLE_OUT_OF_MEMORY;
  207. result = Curl_sendf(sockfd, conn,
  208. "CLIENT " LIBCURL_NAME " " LIBCURL_VERSION "\r\n"
  209. "DEFINE "
  210. "%s " /* database */
  211. "%s\r\n" /* word */
  212. "QUIT\r\n",
  213. database,
  214. eword);
  215. free(eword);
  216. if(result) {
  217. failf(data, "Failed sending DICT request");
  218. return result;
  219. }
  220. Curl_setup_transfer(data, FIRSTSOCKET, -1, FALSE, -1);
  221. }
  222. else {
  223. ppath = strchr(path, '/');
  224. if(ppath) {
  225. int i;
  226. ppath++;
  227. for(i = 0; ppath[i]; i++) {
  228. if(ppath[i] == ':')
  229. ppath[i] = ' ';
  230. }
  231. result = Curl_sendf(sockfd, conn,
  232. "CLIENT " LIBCURL_NAME " " LIBCURL_VERSION "\r\n"
  233. "%s\r\n"
  234. "QUIT\r\n", ppath);
  235. if(result) {
  236. failf(data, "Failed sending DICT request");
  237. return result;
  238. }
  239. Curl_setup_transfer(data, FIRSTSOCKET, -1, FALSE, -1);
  240. }
  241. }
  242. return CURLE_OK;
  243. }
  244. #endif /*CURL_DISABLE_DICT*/