dict.c 7.0 KB

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