Annotation of libwww/Library/src/HTTelnet.c, revision 2.30

2.14      frystyk     1: /*                                                                  HTTelnet.c
                      2: **     TELNET ACCESS, ROLIGIN, etc.
                      3: **
2.17      frystyk     4: **     (c) COPYRIGHT MIT 1995.
2.14      frystyk     5: **     Please first read the full copyright statement in the file COPYRIGH.
1.1       timbl       6: **
                      7: ** Authors
2.20      frystyk     8: **     TBL     Tim Berners-Lee timbl@w3.org
1.1       timbl       9: **     JFG     Jean-Francois Groff jgh@next.com
                     10: **     DD      Denis DeLaRoca (310) 825-4580  <CSP1DWD@mvs.oac.ucla.edu>
2.21      frystyk    11: **     HFN     Henrik Frystyk
1.1       timbl      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)
2.11      luotonen   18: **      2 May 94 Fixed security hole with illegal characters in host
                     19: **               and user names (code from Mosaic/Eric Bina).
1.1       timbl      20: */
                     21: 
2.16      frystyk    22: /* Library include files */
                     23: #include "tcp.h"
                     24: #include "HTUtils.h"
2.27      frystyk    25: #include "HTString.h"
1.1       timbl      26: #include "HTParse.h"
2.30    ! frystyk    27: #include "HTAccess.h"
1.1       timbl      28: #include "HTAnchor.h"
2.21      frystyk    29: #include "HTChunk.h"
2.27      frystyk    30: #include "HTReqMan.h"
1.3       timbl      31: #include "HTAlert.h"
2.12      frystyk    32: #include "HTTelnet.h"                                   /* Implemented here */
1.1       timbl      33: 
2.12      frystyk    34: /* ------------------------------------------------------------------------- */
1.1       timbl      35: 
2.11      luotonen   36: /*     make a string secure for passage to the
                     37: **     system() command.  Make it contain only alphanumneric
                     38: **     characters, or the characters '.', '-', '_', '+'.
                     39: **     Also remove leading '-' or '+'.
                     40: **     -----------------------------------------------------
                     41: **     Function taken from Mosaic's HTTelnet.c.
                     42: */
2.28      frystyk    43: PRIVATE void make_system_secure (char * str)
2.11      luotonen   44: {
2.21      frystyk    45:     char *ptr1, *ptr2;
                     46:     if ((str == NULL)||(*str == '\0'))
                     47:        return;
2.11      luotonen   48: 
2.21      frystyk    49:     /*
                     50:      * remove leading '-' or '+' by making it into whitespace that
                     51:      * will be stripped later.
                     52:      */
                     53:     if (*str=='-' || *str=='+')
                     54:        *str = ' ';
                     55:     ptr1 = ptr2 = str;
                     56:     while (*ptr1) {
                     57:        if ((!isalpha((int)*ptr1))&&(!isdigit((int)*ptr1))&&
                     58:            (*ptr1 != '.')&&(*ptr1 != '_')&&
                     59:            (*ptr1 != '+')&&(*ptr1 != '-')) {
                     60:            ptr1++;
                     61:        } else {
                     62:            *ptr2 = *ptr1;
                     63:            ptr2++;
                     64:            ptr1++;
2.11      luotonen   65:        }
2.21      frystyk    66:     }
                     67:     *ptr2 = *ptr1;
2.11      luotonen   68: }
                     69: 
1.1       timbl      70: /*     Telnet or "rlogin" access
                     71: **     -------------------------
2.27      frystyk    72: **     Returns HT_NO_DATA      OK
                     73: **             HT_ERROR        Error
1.1       timbl      74: */
2.28      frystyk    75: PRIVATE int remote_session (HTRequest * request, char * url)
1.1       timbl      76: {
2.21      frystyk    77:     int status = HT_NO_DATA;
                     78:     HTChunk *cmd = HTChunkCreate(64);
                     79:     char *access = HTParse(url, "", PARSE_ACCESS);
                     80:     char *host = HTParse(url, "", PARSE_HOST);
                     81:     char *hostname = strchr(host, '@');
                     82:     char *user = NULL;
                     83:     char *passwd = NULL;
                     84:     char *port = NULL;
                     85: 
2.27      frystyk    86:     /* We must be in interactive mode */
                     87:     if (!HTPrompt_interactive()) {
                     88:        HTAlert(request,"Can't output live session - must be interactive");
                     89:        free(access);
                     90:        free(host);
                     91:        HTChunkFree(cmd);
                     92:        return HT_ERROR;
                     93:     }
                     94: 
2.21      frystyk    95:     /* Look for user name, password, and port number */
                     96:     if (hostname) {
                     97:        *hostname++ = '\0';
                     98:        user = host;
                     99:        if ((passwd = strchr(host, ':')) != NULL) {
                    100:            *passwd++ = '\0';
                    101:            HTUnEscape(passwd);
                    102:        }
                    103:        HTUnEscape(user);                       /* Might have a funny userid */
                    104:     } else {
                    105:        hostname = host;
                    106:     }
                    107:     if ((port = strchr(hostname, ':')) != NULL)
                    108:        *port++ = '\0';
                    109: 
                    110:     /* If the person is already telnetting etc, forbid hopping */
2.30    ! frystyk   111:     if (HTLib_secure()) {
2.21      frystyk   112:        HTChunk *msg = HTChunkCreate(256);
                    113:        HTChunkPuts(msg, "Sorry, but the service you have selected is one ");
                    114:        HTChunkPuts(msg, "to which you have to log in. If you were running ");
                    115:        HTChunkPuts(msg, "locally on your own computer, you would be ");
                    116:        HTChunkPuts(msg, "connected automatically. For security reasons, ");
                    117:        HTChunkPuts(msg, "this is not allowed when you log in to this ");
                    118:        HTChunkPuts(msg, "information service remotely. ");
1.1       timbl     119:        if (hostname) {
2.21      frystyk   120:            HTChunkPuts(msg,"You can manually connect to this service using ");
                    121:            HTChunkPuts(msg, access);
                    122:            HTChunkPuts(msg, " to host ");
                    123:            HTChunkPuts(msg, hostname);
                    124:            if (port) {
                    125:                HTChunkPuts(msg, " using port ");
                    126:                HTChunkPuts(msg, port);
                    127:            }
                    128:            if (user) {
                    129:                HTChunkPuts(msg, " and user name ");
                    130:                HTChunkPuts(msg, user);
                    131:            }
                    132:            if (passwd) {
                    133:                HTChunkPuts(msg, " and passwd ");
                    134:                HTChunkPuts(msg, passwd);
                    135:            }
                    136:        }
2.25      frystyk   137:        HTAlert(request, msg->data);
2.21      frystyk   138:        HTChunkFree(msg);
                    139:        free(access);
                    140:        free(host);
2.27      frystyk   141:        HTChunkFree(cmd);
2.21      frystyk   142:        return HT_NO_DATA;
                    143:     }
                    144: 
                    145:     /*
                    146:     ** Make user and hostname secure by removing leading '-' or '+'.
                    147:     ** and allowing only alphanumeric, '.', '_', '+', and '-'.
                    148:     */
                    149:     make_system_secure(user);
                    150:     make_system_secure(passwd);
                    151:     make_system_secure(hostname);
                    152:     make_system_secure(port);
                    153: 
2.27      frystyk   154:     if (!strcasecomp(access, "telnet")) {
2.21      frystyk   155: #ifdef SIMPLE_TELNET
                    156:        HTChunkPuts(cmd, "TELNET ");
                    157:        HTChunkPuts(cmd, hostname);                       /* Port is ignored */
                    158: #else
2.26      frystyk   159: #ifdef FULL_TELNET                                         /* User and port */
                    160:        HTChunkPuts(cmd, "telnet ");
                    161:        HTChunkPuts(cmd, hostname);
                    162:        if (user) {
                    163:            HTChunkPuts(cmd, " -l ");
                    164:            HTChunkPuts(cmd, user);
                    165:        }
                    166:        if (port) {
                    167:            HTChunkPutc(cmd, ' ');
                    168:            HTChunkPuts(cmd,  port);
                    169:        }
                    170: #else
2.21      frystyk   171: #ifdef MULTINET
                    172:        HTChunkPuts(cmd, "TELNET ");
                    173:        if (port) {
                    174:            HTChunkPuts(cmd, "/PORT=");
                    175:            HTChunkPuts(cmd, port);
                    176:            HTChunkPutc(cmd, ' ');
                    177:        }
                    178:        HTChunkPuts(cmd, hostname);
2.26      frystyk   179: #else                                                    /* User is ignored */
2.21      frystyk   180:        HTChunkPuts(cmd, "telnet ");
                    181:        HTChunkPuts(cmd, hostname);
                    182:        if (port) {
                    183:            HTChunkPutc(cmd, ' ');
                    184:            HTChunkPuts(cmd,  port);
                    185:        }
                    186: #endif /* MULTINET */
2.26      frystyk   187: #endif /* FULL_TELNET */
2.21      frystyk   188: #endif /* SIMPLE_TELNET */
                    189: 
2.27      frystyk   190:     } else if (!strcasecomp(access, "rlogin")) {
2.21      frystyk   191: #ifdef MULTINET
                    192:        HTChunkPuts(cmd, "RLOGIN ");
                    193:        if (user) {
                    194:            HTChunkPuts(cmd, "/USERNAME=");
                    195:            HTChunkPuts(cmd, user);
                    196:            HTChunkPutc(cmd, ' ');
                    197:        }
                    198:        if (port) {
                    199:            HTChunkPuts(cmd, "/PORT=");
                    200:            HTChunkPuts(cmd, port);
                    201:            HTChunkPutc(cmd, ' ');
                    202:        }
                    203:        HTChunkPuts(cmd, hostname);
                    204: #else
                    205: #ifdef RLOGIN_USER                            /* format: "hostname -l user" */
                    206:        HTChunkPuts(cmd, "rlogin ");
                    207:        HTChunkPuts(cmd, hostname);
                    208:        if (user) {
2.22      frystyk   209:            HTChunkPuts(cmd, " -l ");
2.21      frystyk   210:            HTChunkPuts(cmd, user);
                    211:        }
                    212: #else                                         /* format: "-l user hostname" */
                    213:        HTChunkPuts(cmd, "rlogin ");
                    214:        if (user) {
                    215:            HTChunkPuts(cmd, "-l ");
                    216:            HTChunkPuts(cmd, user);
                    217:            HTChunkPutc(cmd, ' ');
                    218:        }
                    219:        HTChunkPuts(cmd, hostname);
                    220: #endif /* RLOGIN_AFTER */
                    221: #endif /* MULTINET */
                    222: 
2.27      frystyk   223:     } else if (!strcasecomp(access, "tn3270")) {
2.21      frystyk   224: #ifdef MULTINET
                    225:        HTChunkPuts(cmd, "TELNET/TN3270 ");
                    226:        if (port) {
                    227:            HTChunkPuts(cmd, "/PORT=");
                    228:            HTChunkPuts(cmd, port);
                    229:            HTChunkPutc(cmd, ' ');
1.1       timbl     230:        }
2.21      frystyk   231:        HTChunkPuts(cmd, hostname);
                    232: #else
                    233:        HTChunkPuts(cmd, "tn3270 ");
                    234:        HTChunkPuts(cmd, hostname);                       /* Port is ignored */
                    235: #endif /* MULTINET */
                    236: 
                    237:     } else {
                    238:        if (PROT_TRACE)
2.29      frystyk   239:            TTYPrint(TDEST, "Telnet...... Unknown access method: `%s\'\n",
2.21      frystyk   240:                    access);
                    241:        status = HT_ERROR;
                    242:     }
1.1       timbl     243: 
2.21      frystyk   244:     /* Now we are ready to execute the command */
                    245:     if (PROT_TRACE)
2.29      frystyk   246:        TTYPrint(TDEST, "Telnet...... Command is `%s\'\n", cmd->data);
2.21      frystyk   247:     if (user) {
                    248:        HTChunk *msg = HTChunkCreate(128);
                    249:        HTChunkPuts(msg, "When you are connected, log in");
2.27      frystyk   250:        if (strcasecomp(access, "rlogin")) {
2.21      frystyk   251:            HTChunkPuts(msg, "as <");
                    252:            HTChunkPuts(msg, user);
                    253:            HTChunkPutc(msg, '>');
                    254:        }
                    255:        if (passwd) {
                    256:            HTChunkPuts(msg, " using password <");
                    257:            HTChunkPuts(msg, passwd);
                    258:            HTChunkPutc(msg, '>');
1.1       timbl     259:        }
2.25      frystyk   260:        HTAlert(request, msg->data);
2.21      frystyk   261:        HTChunkFree(msg);
                    262:     }
2.29      frystyk   263: #ifdef GOT_SYSTEM
2.21      frystyk   264:     system(cmd->data);
2.29      frystyk   265: #endif
2.21      frystyk   266:     free(access);
                    267:     free(host);
                    268:     HTChunkFree(cmd);
                    269:     return status;
1.1       timbl     270: 
                    271: }
                    272: 
                    273: /*     "Load a document" -- establishes a session
2.21      frystyk   274: **     ==========================================
1.1       timbl     275: **
                    276: ** On entry,
2.21      frystyk   277: **      request                This is the request structure
1.1       timbl     278: ** On exit,
2.21      frystyk   279: **     returns         HT_ERROR        Error has occured or interrupted
                    280: **                     HT_NO_DATA      if return status 204 No Response
1.1       timbl     281: */
2.28      frystyk   282: PUBLIC int HTLoadTelnet (SOCKET soc, HTRequest * request, SockOps ops)
1.1       timbl     283: {
2.27      frystyk   284:     char *url = HTAnchor_physical(request->anchor);
2.10      luotonen  285: 
2.27      frystyk   286:     /* This is a trick as we don't have any socket! */
                    287:     if (ops == FD_NONE) {
2.29      frystyk   288:        if (PROT_TRACE) TTYPrint(TDEST, "Telnet...... Looking for `%s\'\n",url);
2.27      frystyk   289:        HTCleanTelnetString(url);
                    290:        {
                    291:            int status = remote_session(request, url);
                    292:            HTNet_delete(request->net, status);
                    293:        }
                    294:     } else if (ops == FD_CLOSE)                                      /* Interrupted */
                    295:        HTNet_delete(request->net, HT_INTERRUPTED);
                    296:     else
                    297:        HTNet_delete(request->net, HT_ERROR);
                    298:     return HT_OK;
1.1       timbl     299: }
                    300: 
                    301: 

Webmaster