dict.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2016, Daniel Stenberg, <[email protected]>, 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. PORT_DICT, /* defport */
  79. CURLPROTO_DICT, /* protocol */
  80. PROTOPT_NONE | PROTOPT_NOURLQUERY /* flags */
  81. };
  82. static char *unescape_word(struct Curl_easy *data, const char *inputbuff)
  83. {
  84. char *newp;
  85. char *dictp;
  86. char *ptr;
  87. size_t len;
  88. char ch;
  89. int olen=0;
  90. CURLcode result = Curl_urldecode(data, inputbuff, 0, &newp, &len, FALSE);
  91. if(!newp || result)
  92. return NULL;
  93. dictp = malloc(((size_t)len)*2 + 1); /* add one for terminating zero */
  94. if(dictp) {
  95. /* According to RFC2229 section 2.2, these letters need to be escaped with
  96. \[letter] */
  97. for(ptr = newp;
  98. (ch = *ptr) != 0;
  99. ptr++) {
  100. if((ch <= 32) || (ch == 127) ||
  101. (ch == '\'') || (ch == '\"') || (ch == '\\')) {
  102. dictp[olen++] = '\\';
  103. }
  104. dictp[olen++] = ch;
  105. }
  106. dictp[olen]=0;
  107. }
  108. free(newp);
  109. return dictp;
  110. }
  111. static CURLcode dict_do(struct connectdata *conn, bool *done)
  112. {
  113. char *word;
  114. char *eword;
  115. char *ppath;
  116. char *database = NULL;
  117. char *strategy = NULL;
  118. char *nthdef = NULL; /* This is not part of the protocol, but required
  119. by RFC 2229 */
  120. CURLcode result=CURLE_OK;
  121. struct Curl_easy *data=conn->data;
  122. curl_socket_t sockfd = conn->sock[FIRSTSOCKET];
  123. char *path = data->state.path;
  124. curl_off_t *bytecount = &data->req.bytecount;
  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(conn, FIRSTSOCKET, -1, FALSE, bytecount,
  178. -1, NULL); /* no upload */
  179. }
  180. else if(strncasecompare(path, DICT_DEFINE, sizeof(DICT_DEFINE)-1) ||
  181. strncasecompare(path, DICT_DEFINE2, sizeof(DICT_DEFINE2)-1) ||
  182. strncasecompare(path, DICT_DEFINE3, sizeof(DICT_DEFINE3)-1)) {
  183. word = strchr(path, ':');
  184. if(word) {
  185. word++;
  186. database = strchr(word, ':');
  187. if(database) {
  188. *database++ = (char)0;
  189. nthdef = strchr(database, ':');
  190. if(nthdef) {
  191. *nthdef = (char)0;
  192. }
  193. }
  194. }
  195. if((word == NULL) || (*word == (char)0)) {
  196. infof(data, "lookup word is missing\n");
  197. word=(char *)"default";
  198. }
  199. if((database == NULL) || (*database == (char)0)) {
  200. database = (char *)"!";
  201. }
  202. eword = unescape_word(data, word);
  203. if(!eword)
  204. return CURLE_OUT_OF_MEMORY;
  205. result = Curl_sendf(sockfd, conn,
  206. "CLIENT " LIBCURL_NAME " " LIBCURL_VERSION "\r\n"
  207. "DEFINE "
  208. "%s " /* database */
  209. "%s\r\n" /* word */
  210. "QUIT\r\n",
  211. database,
  212. eword);
  213. free(eword);
  214. if(result) {
  215. failf(data, "Failed sending DICT request");
  216. return result;
  217. }
  218. Curl_setup_transfer(conn, FIRSTSOCKET, -1, FALSE, bytecount,
  219. -1, NULL); /* no upload */
  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(conn, FIRSTSOCKET, -1, FALSE, bytecount, -1, NULL);
  239. }
  240. }
  241. return CURLE_OK;
  242. }
  243. #endif /*CURL_DISABLE_DICT*/