dict.c 7.8 KB

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