dict.c 7.5 KB

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