dict.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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, FALSE);
  91. if(!newp || result)
  92. return NULL;
  93. dictp = malloc(len*2 + 1); /* add one for terminating zero */
  94. if(dictp) {
  95. char *ptr;
  96. char ch;
  97. int olen = 0;
  98. /* According to RFC2229 section 2.2, these letters need to be escaped with
  99. \[letter] */
  100. for(ptr = newp;
  101. (ch = *ptr) != 0;
  102. ptr++) {
  103. if((ch <= 32) || (ch == 127) ||
  104. (ch == '\'') || (ch == '\"') || (ch == '\\')) {
  105. dictp[olen++] = '\\';
  106. }
  107. dictp[olen++] = ch;
  108. }
  109. dictp[olen] = 0;
  110. }
  111. free(newp);
  112. return dictp;
  113. }
  114. static CURLcode dict_do(struct connectdata *conn, bool *done)
  115. {
  116. char *word;
  117. char *eword;
  118. char *ppath;
  119. char *database = NULL;
  120. char *strategy = NULL;
  121. char *nthdef = NULL; /* This is not part of the protocol, but required
  122. by RFC 2229 */
  123. CURLcode result = CURLE_OK;
  124. struct Curl_easy *data = conn->data;
  125. curl_socket_t sockfd = conn->sock[FIRSTSOCKET];
  126. char *path = data->state.up.path;
  127. *done = TRUE; /* unconditionally */
  128. if(conn->bits.user_passwd) {
  129. /* AUTH is missing */
  130. }
  131. if(strncasecompare(path, DICT_MATCH, sizeof(DICT_MATCH)-1) ||
  132. strncasecompare(path, DICT_MATCH2, sizeof(DICT_MATCH2)-1) ||
  133. strncasecompare(path, DICT_MATCH3, sizeof(DICT_MATCH3)-1)) {
  134. word = strchr(path, ':');
  135. if(word) {
  136. word++;
  137. database = strchr(word, ':');
  138. if(database) {
  139. *database++ = (char)0;
  140. strategy = strchr(database, ':');
  141. if(strategy) {
  142. *strategy++ = (char)0;
  143. nthdef = strchr(strategy, ':');
  144. if(nthdef) {
  145. *nthdef = (char)0;
  146. }
  147. }
  148. }
  149. }
  150. if((word == NULL) || (*word == (char)0)) {
  151. infof(data, "lookup word is missing\n");
  152. word = (char *)"default";
  153. }
  154. if((database == NULL) || (*database == (char)0)) {
  155. database = (char *)"!";
  156. }
  157. if((strategy == NULL) || (*strategy == (char)0)) {
  158. strategy = (char *)".";
  159. }
  160. eword = unescape_word(data, word);
  161. if(!eword)
  162. return CURLE_OUT_OF_MEMORY;
  163. result = Curl_sendf(sockfd, conn,
  164. "CLIENT " LIBCURL_NAME " " LIBCURL_VERSION "\r\n"
  165. "MATCH "
  166. "%s " /* database */
  167. "%s " /* strategy */
  168. "%s\r\n" /* word */
  169. "QUIT\r\n",
  170. database,
  171. strategy,
  172. eword
  173. );
  174. free(eword);
  175. if(result) {
  176. failf(data, "Failed sending DICT request");
  177. return result;
  178. }
  179. Curl_setup_transfer(data, FIRSTSOCKET, -1, FALSE, -1); /* no upload */
  180. }
  181. else if(strncasecompare(path, DICT_DEFINE, sizeof(DICT_DEFINE)-1) ||
  182. strncasecompare(path, DICT_DEFINE2, sizeof(DICT_DEFINE2)-1) ||
  183. strncasecompare(path, DICT_DEFINE3, sizeof(DICT_DEFINE3)-1)) {
  184. word = strchr(path, ':');
  185. if(word) {
  186. word++;
  187. database = strchr(word, ':');
  188. if(database) {
  189. *database++ = (char)0;
  190. nthdef = strchr(database, ':');
  191. if(nthdef) {
  192. *nthdef = (char)0;
  193. }
  194. }
  195. }
  196. if((word == NULL) || (*word == (char)0)) {
  197. infof(data, "lookup word is missing\n");
  198. word = (char *)"default";
  199. }
  200. if((database == NULL) || (*database == (char)0)) {
  201. database = (char *)"!";
  202. }
  203. eword = unescape_word(data, word);
  204. if(!eword)
  205. return CURLE_OUT_OF_MEMORY;
  206. result = Curl_sendf(sockfd, conn,
  207. "CLIENT " LIBCURL_NAME " " LIBCURL_VERSION "\r\n"
  208. "DEFINE "
  209. "%s " /* database */
  210. "%s\r\n" /* word */
  211. "QUIT\r\n",
  212. database,
  213. eword);
  214. free(eword);
  215. if(result) {
  216. failf(data, "Failed sending DICT request");
  217. return result;
  218. }
  219. Curl_setup_transfer(data, FIRSTSOCKET, -1, FALSE, -1);
  220. }
  221. else {
  222. ppath = strchr(path, '/');
  223. if(ppath) {
  224. int i;
  225. ppath++;
  226. for(i = 0; ppath[i]; i++) {
  227. if(ppath[i] == ':')
  228. ppath[i] = ' ';
  229. }
  230. result = Curl_sendf(sockfd, conn,
  231. "CLIENT " LIBCURL_NAME " " LIBCURL_VERSION "\r\n"
  232. "%s\r\n"
  233. "QUIT\r\n", ppath);
  234. if(result) {
  235. failf(data, "Failed sending DICT request");
  236. return result;
  237. }
  238. Curl_setup_transfer(data, FIRSTSOCKET, -1, FALSE, -1);
  239. }
  240. }
  241. return CURLE_OK;
  242. }
  243. #endif /*CURL_DISABLE_DICT*/