dict.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2012, 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 http://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 "progress.h"
  50. #include "strequal.h"
  51. #include "dict.h"
  52. #include "rawstr.h"
  53. #define _MPRINTF_REPLACE /* use our functions only */
  54. #include <curl/mprintf.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. PORT_DICT, /* defport */
  81. CURLPROTO_DICT, /* protocol */
  82. PROTOPT_NONE | PROTOPT_NOURLQUERY /* flags */
  83. };
  84. static char *unescape_word(struct SessionHandle *data, const char *inputbuff)
  85. {
  86. char *newp;
  87. char *dictp;
  88. char *ptr;
  89. int len;
  90. char byte;
  91. int olen=0;
  92. newp = curl_easy_unescape(data, inputbuff, 0, &len);
  93. if(!newp)
  94. return NULL;
  95. dictp = malloc(((size_t)len)*2 + 1); /* add one for terminating zero */
  96. if(dictp) {
  97. /* According to RFC2229 section 2.2, these letters need to be escaped with
  98. \[letter] */
  99. for(ptr = newp;
  100. (byte = *ptr) != 0;
  101. ptr++) {
  102. if((byte <= 32) || (byte == 127) ||
  103. (byte == '\'') || (byte == '\"') || (byte == '\\')) {
  104. dictp[olen++] = '\\';
  105. }
  106. dictp[olen++] = byte;
  107. }
  108. dictp[olen]=0;
  109. free(newp);
  110. }
  111. return dictp;
  112. }
  113. static CURLcode dict_do(struct connectdata *conn, bool *done)
  114. {
  115. char *word;
  116. char *eword;
  117. char *ppath;
  118. char *database = NULL;
  119. char *strategy = NULL;
  120. char *nthdef = NULL; /* This is not part of the protocol, but required
  121. by RFC 2229 */
  122. CURLcode result=CURLE_OK;
  123. struct SessionHandle *data=conn->data;
  124. curl_socket_t sockfd = conn->sock[FIRSTSOCKET];
  125. char *path = data->state.path;
  126. curl_off_t *bytecount = &data->req.bytecount;
  127. *done = TRUE; /* unconditionally */
  128. if(conn->bits.user_passwd) {
  129. /* AUTH is missing */
  130. }
  131. if(Curl_raw_nequal(path, DICT_MATCH, sizeof(DICT_MATCH)-1) ||
  132. Curl_raw_nequal(path, DICT_MATCH2, sizeof(DICT_MATCH2)-1) ||
  133. Curl_raw_nequal(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(conn, FIRSTSOCKET, -1, FALSE, bytecount,
  180. -1, NULL); /* no upload */
  181. }
  182. else if(Curl_raw_nequal(path, DICT_DEFINE, sizeof(DICT_DEFINE)-1) ||
  183. Curl_raw_nequal(path, DICT_DEFINE2, sizeof(DICT_DEFINE2)-1) ||
  184. Curl_raw_nequal(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(conn, FIRSTSOCKET, -1, FALSE, bytecount,
  221. -1, NULL); /* no upload */
  222. }
  223. else {
  224. ppath = strchr(path, '/');
  225. if(ppath) {
  226. int i;
  227. ppath++;
  228. for(i = 0; ppath[i]; i++) {
  229. if(ppath[i] == ':')
  230. ppath[i] = ' ';
  231. }
  232. result = Curl_sendf(sockfd, conn,
  233. "CLIENT " LIBCURL_NAME " " LIBCURL_VERSION "\r\n"
  234. "%s\r\n"
  235. "QUIT\r\n", ppath);
  236. if(result) {
  237. failf(data, "Failed sending DICT request");
  238. return result;
  239. }
  240. Curl_setup_transfer(conn, FIRSTSOCKET, -1, FALSE, bytecount, -1, NULL);
  241. }
  242. }
  243. return CURLE_OK;
  244. }
  245. #endif /*CURL_DISABLE_DICT*/