dict.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2010, 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 "setup.h"
  23. #ifndef CURL_DISABLE_DICT
  24. /* -- WIN32 approved -- */
  25. #include <stdio.h>
  26. #include <string.h>
  27. #include <stdarg.h>
  28. #include <stdlib.h>
  29. #include <ctype.h>
  30. #ifdef WIN32
  31. #include <time.h>
  32. #include <io.h>
  33. #else
  34. #ifdef HAVE_SYS_SOCKET_H
  35. #include <sys/socket.h>
  36. #endif
  37. #include <netinet/in.h>
  38. #ifdef HAVE_SYS_TIME_H
  39. #include <sys/time.h>
  40. #endif
  41. #ifdef HAVE_UNISTD_H
  42. #include <unistd.h>
  43. #endif
  44. #include <netdb.h>
  45. #ifdef HAVE_ARPA_INET_H
  46. #include <arpa/inet.h>
  47. #endif
  48. #ifdef HAVE_NET_IF_H
  49. #include <net/if.h>
  50. #endif
  51. #ifdef HAVE_SYS_IOCTL_H
  52. #include <sys/ioctl.h>
  53. #endif
  54. #ifdef HAVE_SYS_PARAM_H
  55. #include <sys/param.h>
  56. #endif
  57. #ifdef HAVE_SYS_SELECT_H
  58. #include <sys/select.h>
  59. #endif
  60. #endif
  61. #include "urldata.h"
  62. #include <curl/curl.h>
  63. #include "transfer.h"
  64. #include "sendf.h"
  65. #include "progress.h"
  66. #include "strequal.h"
  67. #include "dict.h"
  68. #include "rawstr.h"
  69. #define _MPRINTF_REPLACE /* use our functions only */
  70. #include <curl/mprintf.h>
  71. /* The last #include file should be: */
  72. #include "memdebug.h"
  73. /*
  74. * Forward declarations.
  75. */
  76. static CURLcode dict_do(struct connectdata *conn, bool *done);
  77. /*
  78. * DICT protocol handler.
  79. */
  80. const struct Curl_handler Curl_handler_dict = {
  81. "DICT", /* scheme */
  82. ZERO_NULL, /* setup_connection */
  83. dict_do, /* do_it */
  84. ZERO_NULL, /* done */
  85. ZERO_NULL, /* do_more */
  86. ZERO_NULL, /* connect_it */
  87. ZERO_NULL, /* connecting */
  88. ZERO_NULL, /* doing */
  89. ZERO_NULL, /* proto_getsock */
  90. ZERO_NULL, /* doing_getsock */
  91. ZERO_NULL, /* perform_getsock */
  92. ZERO_NULL, /* disconnect */
  93. PORT_DICT, /* defport */
  94. PROT_DICT /* protocol */
  95. };
  96. static char *unescape_word(struct SessionHandle *data, const char *inputbuff)
  97. {
  98. char *newp;
  99. char *dictp;
  100. char *ptr;
  101. int len;
  102. char byte;
  103. int olen=0;
  104. newp = curl_easy_unescape(data, inputbuff, 0, &len);
  105. if(!newp)
  106. return NULL;
  107. dictp = malloc(((size_t)len)*2 + 1); /* add one for terminating zero */
  108. if(dictp) {
  109. /* According to RFC2229 section 2.2, these letters need to be escaped with
  110. \[letter] */
  111. for(ptr = newp;
  112. (byte = *ptr) != 0;
  113. ptr++) {
  114. if((byte <= 32) || (byte == 127) ||
  115. (byte == '\'') || (byte == '\"') || (byte == '\\')) {
  116. dictp[olen++] = '\\';
  117. }
  118. dictp[olen++] = byte;
  119. }
  120. dictp[olen]=0;
  121. free(newp);
  122. }
  123. return dictp;
  124. }
  125. static CURLcode dict_do(struct connectdata *conn, bool *done)
  126. {
  127. char *word;
  128. char *eword;
  129. char *ppath;
  130. char *database = NULL;
  131. char *strategy = NULL;
  132. char *nthdef = NULL; /* This is not part of the protocol, but required
  133. by RFC 2229 */
  134. CURLcode result=CURLE_OK;
  135. struct SessionHandle *data=conn->data;
  136. curl_socket_t sockfd = conn->sock[FIRSTSOCKET];
  137. char *path = data->state.path;
  138. curl_off_t *bytecount = &data->req.bytecount;
  139. *done = TRUE; /* unconditionally */
  140. if(conn->bits.user_passwd) {
  141. /* AUTH is missing */
  142. }
  143. if(Curl_raw_nequal(path, DICT_MATCH, sizeof(DICT_MATCH)-1) ||
  144. Curl_raw_nequal(path, DICT_MATCH2, sizeof(DICT_MATCH2)-1) ||
  145. Curl_raw_nequal(path, DICT_MATCH3, sizeof(DICT_MATCH3)-1)) {
  146. word = strchr(path, ':');
  147. if(word) {
  148. word++;
  149. database = strchr(word, ':');
  150. if(database) {
  151. *database++ = (char)0;
  152. strategy = strchr(database, ':');
  153. if(strategy) {
  154. *strategy++ = (char)0;
  155. nthdef = strchr(strategy, ':');
  156. if(nthdef) {
  157. *nthdef = (char)0;
  158. }
  159. }
  160. }
  161. }
  162. if((word == NULL) || (*word == (char)0)) {
  163. infof(data, "lookup word is missing");
  164. word=(char *)"default";
  165. }
  166. if((database == NULL) || (*database == (char)0)) {
  167. database = (char *)"!";
  168. }
  169. if((strategy == NULL) || (*strategy == (char)0)) {
  170. strategy = (char *)".";
  171. }
  172. eword = unescape_word(data, word);
  173. if(!eword)
  174. return CURLE_OUT_OF_MEMORY;
  175. result = Curl_sendf(sockfd, conn,
  176. "CLIENT " LIBCURL_NAME " " LIBCURL_VERSION "\r\n"
  177. "MATCH "
  178. "%s " /* database */
  179. "%s " /* strategy */
  180. "%s\r\n" /* word */
  181. "QUIT\r\n",
  182. database,
  183. strategy,
  184. eword
  185. );
  186. free(eword);
  187. if(result) {
  188. failf(data, "Failed sending DICT request");
  189. return result;
  190. }
  191. Curl_setup_transfer(conn, FIRSTSOCKET, -1, FALSE, bytecount,
  192. -1, NULL); /* no upload */
  193. }
  194. else if(Curl_raw_nequal(path, DICT_DEFINE, sizeof(DICT_DEFINE)-1) ||
  195. Curl_raw_nequal(path, DICT_DEFINE2, sizeof(DICT_DEFINE2)-1) ||
  196. Curl_raw_nequal(path, DICT_DEFINE3, sizeof(DICT_DEFINE3)-1)) {
  197. word = strchr(path, ':');
  198. if(word) {
  199. word++;
  200. database = strchr(word, ':');
  201. if(database) {
  202. *database++ = (char)0;
  203. nthdef = strchr(database, ':');
  204. if(nthdef) {
  205. *nthdef = (char)0;
  206. }
  207. }
  208. }
  209. if((word == NULL) || (*word == (char)0)) {
  210. infof(data, "lookup word is missing");
  211. word=(char *)"default";
  212. }
  213. if((database == NULL) || (*database == (char)0)) {
  214. database = (char *)"!";
  215. }
  216. eword = unescape_word(data, word);
  217. if(!eword)
  218. return CURLE_OUT_OF_MEMORY;
  219. result = Curl_sendf(sockfd, conn,
  220. "CLIENT " LIBCURL_NAME " " LIBCURL_VERSION "\r\n"
  221. "DEFINE "
  222. "%s " /* database */
  223. "%s\r\n" /* word */
  224. "QUIT\r\n",
  225. database,
  226. eword);
  227. free(eword);
  228. if(result) {
  229. failf(data, "Failed sending DICT request");
  230. return result;
  231. }
  232. Curl_setup_transfer(conn, FIRSTSOCKET, -1, FALSE, bytecount,
  233. -1, NULL); /* no upload */
  234. }
  235. else {
  236. ppath = strchr(path, '/');
  237. if(ppath) {
  238. int i;
  239. ppath++;
  240. for (i = 0; ppath[i]; i++) {
  241. if(ppath[i] == ':')
  242. ppath[i] = ' ';
  243. }
  244. result = Curl_sendf(sockfd, conn,
  245. "CLIENT " LIBCURL_NAME " " LIBCURL_VERSION "\r\n"
  246. "%s\r\n"
  247. "QUIT\r\n", ppath);
  248. if(result) {
  249. failf(data, "Failed sending DICT request");
  250. return result;
  251. }
  252. Curl_setup_transfer(conn, FIRSTSOCKET, -1, FALSE, bytecount, -1, NULL);
  253. }
  254. }
  255. return CURLE_OK;
  256. }
  257. #endif /*CURL_DISABLE_DICT*/