dict.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2020, 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 https://curl.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. #elif defined(HAVE_UNISTD_H)
  45. #include <unistd.h>
  46. #endif
  47. #include "urldata.h"
  48. #include <curl/curl.h>
  49. #include "transfer.h"
  50. #include "sendf.h"
  51. #include "escape.h"
  52. #include "progress.h"
  53. #include "dict.h"
  54. #include "curl_printf.h"
  55. #include "strcase.h"
  56. #include "curl_memory.h"
  57. /* The last #include file should be: */
  58. #include "memdebug.h"
  59. /*
  60. * Forward declarations.
  61. */
  62. static CURLcode dict_do(struct connectdata *conn, bool *done);
  63. /*
  64. * DICT protocol handler.
  65. */
  66. const struct Curl_handler Curl_handler_dict = {
  67. "DICT", /* scheme */
  68. ZERO_NULL, /* setup_connection */
  69. dict_do, /* do_it */
  70. ZERO_NULL, /* done */
  71. ZERO_NULL, /* do_more */
  72. ZERO_NULL, /* connect_it */
  73. ZERO_NULL, /* connecting */
  74. ZERO_NULL, /* doing */
  75. ZERO_NULL, /* proto_getsock */
  76. ZERO_NULL, /* doing_getsock */
  77. ZERO_NULL, /* domore_getsock */
  78. ZERO_NULL, /* perform_getsock */
  79. ZERO_NULL, /* disconnect */
  80. ZERO_NULL, /* readwrite */
  81. ZERO_NULL, /* connection_check */
  82. PORT_DICT, /* defport */
  83. CURLPROTO_DICT, /* protocol */
  84. CURLPROTO_DICT, /* family */
  85. PROTOPT_NONE | PROTOPT_NOURLQUERY /* flags */
  86. };
  87. static char *unescape_word(struct Curl_easy *data, const char *inputbuff)
  88. {
  89. char *newp = NULL;
  90. char *dictp;
  91. size_t len;
  92. CURLcode result = Curl_urldecode(data, inputbuff, 0, &newp, &len,
  93. REJECT_NADA);
  94. if(!newp || result)
  95. return NULL;
  96. dictp = malloc(len*2 + 1); /* add one for terminating zero */
  97. if(dictp) {
  98. char *ptr;
  99. char ch;
  100. int olen = 0;
  101. /* According to RFC2229 section 2.2, these letters need to be escaped with
  102. \[letter] */
  103. for(ptr = newp;
  104. (ch = *ptr) != 0;
  105. ptr++) {
  106. if((ch <= 32) || (ch == 127) ||
  107. (ch == '\'') || (ch == '\"') || (ch == '\\')) {
  108. dictp[olen++] = '\\';
  109. }
  110. dictp[olen++] = ch;
  111. }
  112. dictp[olen] = 0;
  113. }
  114. free(newp);
  115. return dictp;
  116. }
  117. /* sendf() sends formatted data to the server */
  118. static CURLcode sendf(curl_socket_t sockfd, struct connectdata *conn,
  119. const char *fmt, ...)
  120. {
  121. struct Curl_easy *data = conn->data;
  122. ssize_t bytes_written;
  123. size_t write_len;
  124. CURLcode result = CURLE_OK;
  125. char *s;
  126. char *sptr;
  127. va_list ap;
  128. va_start(ap, fmt);
  129. s = vaprintf(fmt, ap); /* returns an allocated string */
  130. va_end(ap);
  131. if(!s)
  132. return CURLE_OUT_OF_MEMORY; /* failure */
  133. bytes_written = 0;
  134. write_len = strlen(s);
  135. sptr = s;
  136. for(;;) {
  137. /* Write the buffer to the socket */
  138. result = Curl_write(conn, sockfd, sptr, write_len, &bytes_written);
  139. if(result)
  140. break;
  141. Curl_debug(data, CURLINFO_DATA_OUT, sptr, (size_t)bytes_written);
  142. if((size_t)bytes_written != write_len) {
  143. /* if not all was written at once, we must advance the pointer, decrease
  144. the size left and try again! */
  145. write_len -= bytes_written;
  146. sptr += bytes_written;
  147. }
  148. else
  149. break;
  150. }
  151. free(s); /* free the output string */
  152. return result;
  153. }
  154. static CURLcode dict_do(struct connectdata *conn, bool *done)
  155. {
  156. char *word;
  157. char *eword;
  158. char *ppath;
  159. char *database = NULL;
  160. char *strategy = NULL;
  161. char *nthdef = NULL; /* This is not part of the protocol, but required
  162. by RFC 2229 */
  163. CURLcode result = CURLE_OK;
  164. struct Curl_easy *data = conn->data;
  165. curl_socket_t sockfd = conn->sock[FIRSTSOCKET];
  166. char *path = data->state.up.path;
  167. *done = TRUE; /* unconditionally */
  168. if(conn->bits.user_passwd) {
  169. /* AUTH is missing */
  170. }
  171. if(strncasecompare(path, DICT_MATCH, sizeof(DICT_MATCH)-1) ||
  172. strncasecompare(path, DICT_MATCH2, sizeof(DICT_MATCH2)-1) ||
  173. strncasecompare(path, DICT_MATCH3, sizeof(DICT_MATCH3)-1)) {
  174. word = strchr(path, ':');
  175. if(word) {
  176. word++;
  177. database = strchr(word, ':');
  178. if(database) {
  179. *database++ = (char)0;
  180. strategy = strchr(database, ':');
  181. if(strategy) {
  182. *strategy++ = (char)0;
  183. nthdef = strchr(strategy, ':');
  184. if(nthdef) {
  185. *nthdef = (char)0;
  186. }
  187. }
  188. }
  189. }
  190. if((word == NULL) || (*word == (char)0)) {
  191. infof(data, "lookup word is missing\n");
  192. word = (char *)"default";
  193. }
  194. if((database == NULL) || (*database == (char)0)) {
  195. database = (char *)"!";
  196. }
  197. if((strategy == NULL) || (*strategy == (char)0)) {
  198. strategy = (char *)".";
  199. }
  200. eword = unescape_word(data, word);
  201. if(!eword)
  202. return CURLE_OUT_OF_MEMORY;
  203. result = sendf(sockfd, conn,
  204. "CLIENT " LIBCURL_NAME " " LIBCURL_VERSION "\r\n"
  205. "MATCH "
  206. "%s " /* database */
  207. "%s " /* strategy */
  208. "%s\r\n" /* word */
  209. "QUIT\r\n",
  210. database,
  211. strategy,
  212. eword);
  213. free(eword);
  214. if(result) {
  215. failf(data, "Failed sending DICT request");
  216. return result;
  217. }
  218. Curl_setup_transfer(data, FIRSTSOCKET, -1, FALSE, -1); /* no upload */
  219. }
  220. else if(strncasecompare(path, DICT_DEFINE, sizeof(DICT_DEFINE)-1) ||
  221. strncasecompare(path, DICT_DEFINE2, sizeof(DICT_DEFINE2)-1) ||
  222. strncasecompare(path, DICT_DEFINE3, sizeof(DICT_DEFINE3)-1)) {
  223. word = strchr(path, ':');
  224. if(word) {
  225. word++;
  226. database = strchr(word, ':');
  227. if(database) {
  228. *database++ = (char)0;
  229. nthdef = strchr(database, ':');
  230. if(nthdef) {
  231. *nthdef = (char)0;
  232. }
  233. }
  234. }
  235. if((word == NULL) || (*word == (char)0)) {
  236. infof(data, "lookup word is missing\n");
  237. word = (char *)"default";
  238. }
  239. if((database == NULL) || (*database == (char)0)) {
  240. database = (char *)"!";
  241. }
  242. eword = unescape_word(data, word);
  243. if(!eword)
  244. return CURLE_OUT_OF_MEMORY;
  245. result = sendf(sockfd, conn,
  246. "CLIENT " LIBCURL_NAME " " LIBCURL_VERSION "\r\n"
  247. "DEFINE "
  248. "%s " /* database */
  249. "%s\r\n" /* word */
  250. "QUIT\r\n",
  251. database,
  252. eword);
  253. free(eword);
  254. if(result) {
  255. failf(data, "Failed sending DICT request");
  256. return result;
  257. }
  258. Curl_setup_transfer(data, FIRSTSOCKET, -1, FALSE, -1);
  259. }
  260. else {
  261. ppath = strchr(path, '/');
  262. if(ppath) {
  263. int i;
  264. ppath++;
  265. for(i = 0; ppath[i]; i++) {
  266. if(ppath[i] == ':')
  267. ppath[i] = ' ';
  268. }
  269. result = sendf(sockfd, conn,
  270. "CLIENT " LIBCURL_NAME " " LIBCURL_VERSION "\r\n"
  271. "%s\r\n"
  272. "QUIT\r\n", ppath);
  273. if(result) {
  274. failf(data, "Failed sending DICT request");
  275. return result;
  276. }
  277. Curl_setup_transfer(data, FIRSTSOCKET, -1, FALSE, -1);
  278. }
  279. }
  280. return CURLE_OK;
  281. }
  282. #endif /*CURL_DISABLE_DICT*/