HTTelnet.c
上传用户:zlh9724
上传日期:2007-01-04
资源大小:1991k
文件大小:7k
源码类别:

浏览器

开发平台:

Unix_Linux

  1. /*      HTTelnet.c
  2. ** TELNET ACCESS, ROLIGIN, etc.
  3. **
  4. ** (c) COPYRIGHT MIT 1995.
  5. ** Please first read the full copyright statement in the file COPYRIGH.
  6. **
  7. ** Authors
  8. ** TBL Tim Berners-Lee timbl@w3.org
  9. ** JFG Jean-Francois Groff jgh@next.com
  10. ** DD Denis DeLaRoca (310) 825-4580  <CSP1DWD@mvs.oac.ucla.edu>
  11. ** HFN Henrik Frystyk
  12. ** History
  13. **       8 Jun 92 Telnet hopping prohibited as telnet is not secure (TBL)
  14. ** 26 Jun 92 When over DECnet, suppressed FTP, Gopher and News. (JFG)
  15. **  6 Oct 92 Moved HTClientHost and logfile into here. (TBL)
  16. ** 17 Dec 92 Tn3270 added, bug fix. (DD)
  17. **  2 Feb 93 Split from HTAccess.c. Registration.(TBL)
  18. **  2 May 94 Fixed security hole with illegal characters in host
  19. **   and user names (code from Mosaic/Eric Bina).
  20. */
  21. /* Library include files */
  22. #include "tcp.h"
  23. #include "HTUtils.h"
  24. #include "HTString.h"
  25. #include "HTParse.h"
  26. #include "HTAccess.h"
  27. #include "HTAnchor.h"
  28. #include "HTChunk.h"
  29. #include "HTReqMan.h"
  30. #include "HTAlert.h"
  31. #include "HTTelnet.h"  /* Implemented here */
  32. /* ------------------------------------------------------------------------- */
  33. /* make a string secure for passage to the
  34. ** system() command.  Make it contain only alphanumneric
  35. ** characters, or the characters '.', '-', '_', '+'.
  36. ** Also remove leading '-' or '+'.
  37. ** -----------------------------------------------------
  38. ** Function taken from Mosaic's HTTelnet.c.
  39. */
  40. PRIVATE void make_system_secure (char * str)
  41. {
  42.     char *ptr1, *ptr2;
  43.     if ((str == NULL)||(*str == ''))
  44. return;
  45.     /*
  46.      * remove leading '-' or '+' by making it into whitespace that
  47.      * will be stripped later.
  48.      */
  49.     if (*str=='-' || *str=='+')
  50. *str = ' ';
  51.     ptr1 = ptr2 = str;
  52.     while (*ptr1) {
  53. if ((!isalpha((int)*ptr1))&&(!isdigit((int)*ptr1))&&
  54.     (*ptr1 != '.')&&(*ptr1 != '_')&&
  55.     (*ptr1 != '+')&&(*ptr1 != '-')) {
  56.     ptr1++;
  57. } else {
  58.     *ptr2 = *ptr1;
  59.     ptr2++;
  60.     ptr1++;
  61. }
  62.     }
  63.     *ptr2 = *ptr1;
  64. }
  65. /* Telnet or "rlogin" access
  66. ** -------------------------
  67. ** Returns HT_NO_DATA OK
  68. ** HT_ERROR Error
  69. */
  70. PRIVATE int remote_session (HTRequest * request, char * url)
  71. {
  72.     int status = HT_NO_DATA;
  73.     HTChunk *cmd = HTChunk_new(64);
  74.     char *access = HTParse(url, "", PARSE_ACCESS);
  75.     char *host = HTParse(url, "", PARSE_HOST);
  76.     char *hostname = strchr(host, '@');
  77.     char *user = NULL;
  78.     char *passwd = NULL;
  79.     char *port = NULL;
  80.     /* We must be in interactive mode */
  81.     if (!HTAlert_interactive()) {
  82. if (PROT_TRACE) TTYPrint(TDEST, "Telnet...... Not interactiven");
  83. HT_FREE(access);
  84. HT_FREE(host);
  85. HTChunk_delete(cmd);
  86. return HT_ERROR;
  87.     }
  88.     /* Look for user name, password, and port number */
  89.     if (hostname) {
  90. *hostname++ = '';
  91. user = host;
  92. if ((passwd = strchr(host, ':')) != NULL) {
  93.     *passwd++ = '';
  94.     HTUnEscape(passwd);
  95. }
  96. HTUnEscape(user); /* Might have a funny userid */
  97.     } else {
  98. hostname = host;
  99.     }
  100.     if ((port = strchr(hostname, ':')) != NULL)
  101. *port++ = '';
  102.     /* If the person is already telnetting etc, forbid hopping */
  103.     if (HTLib_secure()) {
  104. HTRequest_addError(request, ERR_FATAL, NO,
  105.    HTERR_ACCESS, NULL, 0, "HTLoadTelnet");
  106. HT_FREE(access);
  107. HT_FREE(host);
  108. HTChunk_delete(cmd);
  109. return HT_NO_DATA;
  110.     }
  111.     /*
  112.     ** Make user and hostname secure by removing leading '-' or '+'.
  113.     ** and allowing only alphanumeric, '.', '_', '+', and '-'.
  114.     */
  115.     make_system_secure(user);
  116.     make_system_secure(passwd);
  117.     make_system_secure(hostname);
  118.     make_system_secure(port);
  119.     if (!strcasecomp(access, "telnet")) {
  120. #ifdef SIMPLE_TELNET
  121. HTChunk_puts(cmd, "TELNET ");
  122. HTChunk_puts(cmd, hostname);   /* Port is ignored */
  123. #else
  124. #ifdef FULL_TELNET     /* User and port */
  125. HTChunk_puts(cmd, "telnet ");
  126. HTChunk_puts(cmd, hostname);
  127. if (user) {
  128.     HTChunk_puts(cmd, " -l ");
  129.     HTChunk_puts(cmd, user);
  130. }
  131. if (port) {
  132.     HTChunk_putc(cmd, ' ');
  133.     HTChunk_puts(cmd,  port);
  134. }
  135. #else
  136. #ifdef MULTINET
  137. HTChunk_puts(cmd, "TELNET ");
  138. if (port) {
  139.     HTChunk_puts(cmd, "/PORT=");
  140.     HTChunk_puts(cmd, port);
  141.     HTChunk_putc(cmd, ' ');
  142. }
  143. HTChunk_puts(cmd, hostname);
  144. #else   /* User is ignored */
  145. HTChunk_puts(cmd, "telnet ");
  146. HTChunk_puts(cmd, hostname);
  147. if (port) {
  148.     HTChunk_putc(cmd, ' ');
  149.     HTChunk_puts(cmd,  port);
  150. }
  151. #endif /* MULTINET */
  152. #endif /* FULL_TELNET */
  153. #endif /* SIMPLE_TELNET */
  154.     } else if (!strcasecomp(access, "rlogin")) {
  155. #ifdef MULTINET
  156. HTChunk_puts(cmd, "RLOGIN ");
  157. if (user) {
  158.     HTChunk_puts(cmd, "/USERNAME=");
  159.     HTChunk_puts(cmd, user);
  160.     HTChunk_putc(cmd, ' ');
  161. }
  162. if (port) {
  163.     HTChunk_puts(cmd, "/PORT=");
  164.     HTChunk_puts(cmd, port);
  165.     HTChunk_putc(cmd, ' ');
  166. }
  167. HTChunk_puts(cmd, hostname);
  168. #else
  169. #ifdef RLOGIN_USER        /* format: "hostname -l user" */
  170. HTChunk_puts(cmd, "rlogin ");
  171. HTChunk_puts(cmd, hostname);
  172. if (user) {
  173.     HTChunk_puts(cmd, " -l ");
  174.     HTChunk_puts(cmd, user);
  175. }
  176. #else        /* format: "-l user hostname" */
  177. HTChunk_puts(cmd, "rlogin ");
  178. if (user) {
  179.     HTChunk_puts(cmd, "-l ");
  180.     HTChunk_puts(cmd, user);
  181.     HTChunk_putc(cmd, ' ');
  182. }
  183. HTChunk_puts(cmd, hostname);
  184. #endif /* RLOGIN_AFTER */
  185. #endif /* MULTINET */
  186.     } else if (!strcasecomp(access, "tn3270")) {
  187. #ifdef MULTINET
  188. HTChunk_puts(cmd, "TELNET/TN3270 ");
  189. if (port) {
  190.     HTChunk_puts(cmd, "/PORT=");
  191.     HTChunk_puts(cmd, port);
  192.     HTChunk_putc(cmd, ' ');
  193. }
  194. HTChunk_puts(cmd, hostname);
  195. #else
  196. HTChunk_puts(cmd, "tn3270 ");
  197. HTChunk_puts(cmd, hostname);   /* Port is ignored */
  198. #endif /* MULTINET */
  199.     } else {
  200. if (PROT_TRACE)
  201.     TTYPrint(TDEST, "Telnet...... Unknown access method: `%s'n",
  202.     access);
  203. status = HT_ERROR;
  204.     }
  205.     /* Now we are ready to execute the command */
  206.     if (PROT_TRACE)
  207. TTYPrint(TDEST, "Telnet...... Command is `%s'n", cmd->data);
  208.     if (user) {
  209. HTChunk *msg = HTChunk_new(128);
  210. if (strcasecomp(access, "rlogin")) {
  211.     HTChunk_puts(msg, "user <");
  212.     HTChunk_puts(msg, user);
  213.     HTChunk_putc(msg, '>');
  214. }
  215. if (passwd) {
  216.     HTChunk_puts(msg, " and password <");
  217.     HTChunk_puts(msg, passwd);
  218.     HTChunk_putc(msg, '>');
  219. }
  220. HTRequest_addError(request, ERR_INFO, NO,
  221.    HTERR_LOGIN, HTChunk_data(msg), HTChunk_size(msg),
  222.    "HTLoadTelnet");
  223. HTChunk_delete(msg);
  224.     }
  225. #ifdef GOT_SYSTEM
  226.     system(cmd->data);
  227. #endif
  228.     HT_FREE(access);
  229.     HT_FREE(host);
  230.     HTChunk_delete(cmd);
  231.     return status;
  232. }
  233. /* "Load a document" -- establishes a session
  234. ** ==========================================
  235. **
  236. ** On entry,
  237. **      request This is the request structure
  238. ** On exit,
  239. ** returns HT_ERROR Error has occured or interrupted
  240. ** HT_NO_DATA if return status 204 No Response
  241. */
  242. PUBLIC int HTLoadTelnet (SOCKET soc, HTRequest * request, SockOps ops)
  243. {
  244.     char *url = HTAnchor_physical(request->anchor);
  245.     /* This is a trick as we don't have any socket! */
  246.     if (ops == FD_NONE) {
  247. if (PROT_TRACE) TTYPrint(TDEST, "Telnet...... Looking for `%s'n",url);
  248. HTCleanTelnetString(url);
  249. {
  250.     int status = remote_session(request, url);
  251.     HTNet_delete(request->net, status);
  252. }
  253.     } else if (ops == FD_CLOSE)       /* Interrupted */
  254. HTNet_delete(request->net, HT_INTERRUPTED);
  255.     else
  256. HTNet_delete(request->net, HT_ERROR);
  257.     return HT_OK;
  258. }