113e3f4d6SMark Murray /*
213e3f4d6SMark Murray * Copyright (c) 1989, 1993
313e3f4d6SMark Murray * The Regents of the University of California. All rights reserved.
413e3f4d6SMark Murray *
513e3f4d6SMark Murray * Redistribution and use in source and binary forms, with or without
613e3f4d6SMark Murray * modification, are permitted provided that the following conditions
713e3f4d6SMark Murray * are met:
813e3f4d6SMark Murray * 1. Redistributions of source code must retain the above copyright
913e3f4d6SMark Murray * notice, this list of conditions and the following disclaimer.
1013e3f4d6SMark Murray * 2. Redistributions in binary form must reproduce the above copyright
1113e3f4d6SMark Murray * notice, this list of conditions and the following disclaimer in the
1213e3f4d6SMark Murray * documentation and/or other materials provided with the distribution.
1313e3f4d6SMark Murray * 3. All advertising materials mentioning features or use of this software
1413e3f4d6SMark Murray * must display the following acknowledgement:
1513e3f4d6SMark Murray * This product includes software developed by the University of
1613e3f4d6SMark Murray * California, Berkeley and its contributors.
1713e3f4d6SMark Murray * 4. Neither the name of the University nor the names of its contributors
1813e3f4d6SMark Murray * may be used to endorse or promote products derived from this software
1913e3f4d6SMark Murray * without specific prior written permission.
2013e3f4d6SMark Murray *
2113e3f4d6SMark Murray * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2213e3f4d6SMark Murray * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2313e3f4d6SMark Murray * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2413e3f4d6SMark Murray * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2513e3f4d6SMark Murray * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2613e3f4d6SMark Murray * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2713e3f4d6SMark Murray * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2813e3f4d6SMark Murray * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2913e3f4d6SMark Murray * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3013e3f4d6SMark Murray * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3113e3f4d6SMark Murray * SUCH DAMAGE.
3213e3f4d6SMark Murray */
3313e3f4d6SMark Murray
3413e3f4d6SMark Murray #include "telnetd.h"
3513e3f4d6SMark Murray
36*ae771770SStanislav Sedov RCSID("$Id$");
3713e3f4d6SMark Murray
3813e3f4d6SMark Murray #ifdef _SC_CRAY_SECURE_SYS
3913e3f4d6SMark Murray #include <sys/sysv.h>
4013e3f4d6SMark Murray #include <sys/secdev.h>
4113e3f4d6SMark Murray #include <sys/secparm.h>
4213e3f4d6SMark Murray #include <sys/usrv.h>
4313e3f4d6SMark Murray int secflag;
4413e3f4d6SMark Murray char tty_dev[16];
4513e3f4d6SMark Murray struct secdev dv;
4613e3f4d6SMark Murray struct sysv sysv;
4713e3f4d6SMark Murray struct socksec ss;
4813e3f4d6SMark Murray #endif /* _SC_CRAY_SECURE_SYS */
4913e3f4d6SMark Murray
5013e3f4d6SMark Murray #ifdef AUTHENTICATION
5113e3f4d6SMark Murray int auth_level = 0;
5213e3f4d6SMark Murray #endif
5313e3f4d6SMark Murray
54c19800e8SDoug Rabson #ifdef KRB5
55c19800e8SDoug Rabson #define Authenticator k5_Authenticator
56c19800e8SDoug Rabson #include <krb5.h>
57c19800e8SDoug Rabson #undef Authenticator
58c19800e8SDoug Rabson #endif
59c19800e8SDoug Rabson
6013e3f4d6SMark Murray extern int utmp_len;
6113e3f4d6SMark Murray int registerd_host_only = 0;
62c19800e8SDoug Rabson #ifdef ENCRYPTION
63c19800e8SDoug Rabson int require_encryption = 0;
64c19800e8SDoug Rabson #endif
654137ff4cSJacques Vidrine
6613e3f4d6SMark Murray #ifdef STREAMSPTY
674137ff4cSJacques Vidrine
6813e3f4d6SMark Murray #ifdef _AIX
6913e3f4d6SMark Murray #include <sys/termio.h>
7013e3f4d6SMark Murray #endif
7113e3f4d6SMark Murray # ifdef HAVE_SYS_STRTTY_H
7213e3f4d6SMark Murray # include <sys/strtty.h>
7313e3f4d6SMark Murray # endif
7413e3f4d6SMark Murray # ifdef HAVE_SYS_STR_TTY_H
7513e3f4d6SMark Murray # include <sys/str_tty.h>
7613e3f4d6SMark Murray # endif
7713e3f4d6SMark Murray /* make sure we don't get the bsd version */
7813e3f4d6SMark Murray /* what is this here for? solaris? /joda */
7913e3f4d6SMark Murray # ifdef HAVE_SYS_TTY_H
8013e3f4d6SMark Murray # include "/usr/include/sys/tty.h"
8113e3f4d6SMark Murray # endif
8213e3f4d6SMark Murray # ifdef HAVE_SYS_PTYVAR_H
8313e3f4d6SMark Murray # include <sys/ptyvar.h>
8413e3f4d6SMark Murray # endif
8513e3f4d6SMark Murray
8613e3f4d6SMark Murray /*
8713e3f4d6SMark Murray * Because of the way ptyibuf is used with streams messages, we need
8813e3f4d6SMark Murray * ptyibuf+1 to be on a full-word boundary. The following wierdness
8913e3f4d6SMark Murray * is simply to make that happen.
9013e3f4d6SMark Murray */
9113e3f4d6SMark Murray long ptyibufbuf[BUFSIZ/sizeof(long)+1];
9213e3f4d6SMark Murray char *ptyibuf = ((char *)&ptyibufbuf[1])-1;
9313e3f4d6SMark Murray char *ptyip = ((char *)&ptyibufbuf[1])-1;
9413e3f4d6SMark Murray char ptyibuf2[BUFSIZ];
9513e3f4d6SMark Murray unsigned char ctlbuf[BUFSIZ];
9613e3f4d6SMark Murray struct strbuf strbufc, strbufd;
9713e3f4d6SMark Murray
9813e3f4d6SMark Murray int readstream(int, char*, int);
9913e3f4d6SMark Murray
10013e3f4d6SMark Murray #else /* ! STREAMPTY */
10113e3f4d6SMark Murray
10213e3f4d6SMark Murray /*
10313e3f4d6SMark Murray * I/O data buffers,
10413e3f4d6SMark Murray * pointers, and counters.
10513e3f4d6SMark Murray */
10613e3f4d6SMark Murray char ptyibuf[BUFSIZ], *ptyip = ptyibuf;
10713e3f4d6SMark Murray char ptyibuf2[BUFSIZ];
10813e3f4d6SMark Murray
10913e3f4d6SMark Murray #endif /* ! STREAMPTY */
11013e3f4d6SMark Murray
11113e3f4d6SMark Murray int hostinfo = 1; /* do we print login banner? */
11213e3f4d6SMark Murray
11313e3f4d6SMark Murray #ifdef _CRAY
11413e3f4d6SMark Murray extern int newmap; /* nonzero if \n maps to ^M^J */
11513e3f4d6SMark Murray int lowpty = 0, highpty; /* low, high pty numbers */
11613e3f4d6SMark Murray #endif /* CRAY */
11713e3f4d6SMark Murray
11813e3f4d6SMark Murray int debug = 0;
11913e3f4d6SMark Murray int keepalive = 1;
12013e3f4d6SMark Murray char *progname;
12113e3f4d6SMark Murray
122c19800e8SDoug Rabson static void usage (int error_code);
12313e3f4d6SMark Murray
12413e3f4d6SMark Murray /*
12513e3f4d6SMark Murray * The string to pass to getopt(). We do it this way so
12613e3f4d6SMark Murray * that only the actual options that we support will be
12713e3f4d6SMark Murray * passed off to getopt().
12813e3f4d6SMark Murray */
12913e3f4d6SMark Murray char valid_opts[] = "Bd:hklnS:u:UL:y"
13013e3f4d6SMark Murray #ifdef AUTHENTICATION
13113e3f4d6SMark Murray "a:X:z"
13213e3f4d6SMark Murray #endif
133c19800e8SDoug Rabson #ifdef ENCRYPTION
134c19800e8SDoug Rabson "e"
135c19800e8SDoug Rabson #endif
13613e3f4d6SMark Murray #ifdef DIAGNOSTICS
13713e3f4d6SMark Murray "D:"
13813e3f4d6SMark Murray #endif
13913e3f4d6SMark Murray #ifdef _CRAY
14013e3f4d6SMark Murray "r:"
14113e3f4d6SMark Murray #endif
14213e3f4d6SMark Murray ;
14313e3f4d6SMark Murray
14413e3f4d6SMark Murray static void doit(struct sockaddr*, int);
14513e3f4d6SMark Murray
14613e3f4d6SMark Murray int
main(int argc,char ** argv)14713e3f4d6SMark Murray main(int argc, char **argv)
14813e3f4d6SMark Murray {
14913e3f4d6SMark Murray struct sockaddr_storage __ss;
15013e3f4d6SMark Murray struct sockaddr *sa = (struct sockaddr *)&__ss;
1515e9cd1aeSAssar Westerlund int on = 1;
1525e9cd1aeSAssar Westerlund socklen_t sa_size;
15313e3f4d6SMark Murray int ch;
15413e3f4d6SMark Murray #if defined(IPPROTO_IP) && defined(IP_TOS)
15513e3f4d6SMark Murray int tos = -1;
15613e3f4d6SMark Murray #endif
15713e3f4d6SMark Murray pfrontp = pbackp = ptyobuf;
15813e3f4d6SMark Murray netip = netibuf;
15913e3f4d6SMark Murray nfrontp = nbackp = netobuf;
16013e3f4d6SMark Murray
1611c43270aSJacques Vidrine setprogname(argv[0]);
1621c43270aSJacques Vidrine
16313e3f4d6SMark Murray progname = *argv;
16413e3f4d6SMark Murray #ifdef ENCRYPTION
16513e3f4d6SMark Murray nclearto = 0;
16613e3f4d6SMark Murray #endif
16713e3f4d6SMark Murray
16813e3f4d6SMark Murray #ifdef _CRAY
16913e3f4d6SMark Murray /*
17013e3f4d6SMark Murray * Get number of pty's before trying to process options,
17113e3f4d6SMark Murray * which may include changing pty range.
17213e3f4d6SMark Murray */
17313e3f4d6SMark Murray highpty = getnpty();
17413e3f4d6SMark Murray #endif /* CRAY */
17513e3f4d6SMark Murray
1768373020dSJacques Vidrine if (argc == 2 && strcmp(argv[1], "--version") == 0) {
1778373020dSJacques Vidrine print_version(NULL);
1788373020dSJacques Vidrine exit(0);
1798373020dSJacques Vidrine }
180c19800e8SDoug Rabson if (argc == 2 && strcmp(argv[1], "--help") == 0)
181c19800e8SDoug Rabson usage(0);
1828373020dSJacques Vidrine
18313e3f4d6SMark Murray while ((ch = getopt(argc, argv, valid_opts)) != -1) {
18413e3f4d6SMark Murray switch(ch) {
18513e3f4d6SMark Murray
18613e3f4d6SMark Murray #ifdef AUTHENTICATION
18713e3f4d6SMark Murray case 'a':
18813e3f4d6SMark Murray /*
18913e3f4d6SMark Murray * Check for required authentication level
19013e3f4d6SMark Murray */
19113e3f4d6SMark Murray if (strcmp(optarg, "debug") == 0) {
19213e3f4d6SMark Murray auth_debug_mode = 1;
19313e3f4d6SMark Murray } else if (strcasecmp(optarg, "none") == 0) {
19413e3f4d6SMark Murray auth_level = 0;
19513e3f4d6SMark Murray } else if (strcasecmp(optarg, "otp") == 0) {
19613e3f4d6SMark Murray auth_level = 0;
19713e3f4d6SMark Murray require_otp = 1;
19813e3f4d6SMark Murray } else if (strcasecmp(optarg, "other") == 0) {
19913e3f4d6SMark Murray auth_level = AUTH_OTHER;
20013e3f4d6SMark Murray } else if (strcasecmp(optarg, "user") == 0) {
20113e3f4d6SMark Murray auth_level = AUTH_USER;
20213e3f4d6SMark Murray } else if (strcasecmp(optarg, "valid") == 0) {
20313e3f4d6SMark Murray auth_level = AUTH_VALID;
20413e3f4d6SMark Murray } else if (strcasecmp(optarg, "off") == 0) {
20513e3f4d6SMark Murray /*
20613e3f4d6SMark Murray * This hack turns off authentication
20713e3f4d6SMark Murray */
20813e3f4d6SMark Murray auth_level = -1;
20913e3f4d6SMark Murray } else {
21013e3f4d6SMark Murray fprintf(stderr,
21113e3f4d6SMark Murray "telnetd: unknown authorization level for -a\n");
21213e3f4d6SMark Murray }
21313e3f4d6SMark Murray break;
21413e3f4d6SMark Murray #endif /* AUTHENTICATION */
21513e3f4d6SMark Murray
21613e3f4d6SMark Murray case 'B': /* BFTP mode is not supported any more */
21713e3f4d6SMark Murray break;
21813e3f4d6SMark Murray case 'd':
21913e3f4d6SMark Murray if (strcmp(optarg, "ebug") == 0) {
22013e3f4d6SMark Murray debug++;
22113e3f4d6SMark Murray break;
22213e3f4d6SMark Murray }
223c19800e8SDoug Rabson usage(1);
22413e3f4d6SMark Murray /* NOTREACHED */
22513e3f4d6SMark Murray break;
22613e3f4d6SMark Murray
22713e3f4d6SMark Murray #ifdef DIAGNOSTICS
22813e3f4d6SMark Murray case 'D':
22913e3f4d6SMark Murray /*
23013e3f4d6SMark Murray * Check for desired diagnostics capabilities.
23113e3f4d6SMark Murray */
23213e3f4d6SMark Murray if (!strcmp(optarg, "report")) {
23313e3f4d6SMark Murray diagnostic |= TD_REPORT|TD_OPTIONS;
23413e3f4d6SMark Murray } else if (!strcmp(optarg, "exercise")) {
23513e3f4d6SMark Murray diagnostic |= TD_EXERCISE;
23613e3f4d6SMark Murray } else if (!strcmp(optarg, "netdata")) {
23713e3f4d6SMark Murray diagnostic |= TD_NETDATA;
23813e3f4d6SMark Murray } else if (!strcmp(optarg, "ptydata")) {
23913e3f4d6SMark Murray diagnostic |= TD_PTYDATA;
24013e3f4d6SMark Murray } else if (!strcmp(optarg, "options")) {
24113e3f4d6SMark Murray diagnostic |= TD_OPTIONS;
24213e3f4d6SMark Murray } else {
243c19800e8SDoug Rabson usage(1);
24413e3f4d6SMark Murray /* NOT REACHED */
24513e3f4d6SMark Murray }
24613e3f4d6SMark Murray break;
24713e3f4d6SMark Murray #endif /* DIAGNOSTICS */
24813e3f4d6SMark Murray
249c19800e8SDoug Rabson #ifdef ENCRYPTION
250c19800e8SDoug Rabson case 'e':
251c19800e8SDoug Rabson require_encryption = 1;
252c19800e8SDoug Rabson break;
253c19800e8SDoug Rabson #endif
25413e3f4d6SMark Murray
25513e3f4d6SMark Murray case 'h':
25613e3f4d6SMark Murray hostinfo = 0;
25713e3f4d6SMark Murray break;
25813e3f4d6SMark Murray
25913e3f4d6SMark Murray case 'k': /* Linemode is not supported any more */
26013e3f4d6SMark Murray case 'l':
26113e3f4d6SMark Murray break;
26213e3f4d6SMark Murray
26313e3f4d6SMark Murray case 'n':
26413e3f4d6SMark Murray keepalive = 0;
26513e3f4d6SMark Murray break;
26613e3f4d6SMark Murray
26713e3f4d6SMark Murray #ifdef _CRAY
26813e3f4d6SMark Murray case 'r':
26913e3f4d6SMark Murray {
27013e3f4d6SMark Murray char *strchr();
27113e3f4d6SMark Murray char *c;
27213e3f4d6SMark Murray
27313e3f4d6SMark Murray /*
27413e3f4d6SMark Murray * Allow the specification of alterations
27513e3f4d6SMark Murray * to the pty search range. It is legal to
27613e3f4d6SMark Murray * specify only one, and not change the
27713e3f4d6SMark Murray * other from its default.
27813e3f4d6SMark Murray */
27913e3f4d6SMark Murray c = strchr(optarg, '-');
28013e3f4d6SMark Murray if (c) {
28113e3f4d6SMark Murray *c++ = '\0';
28213e3f4d6SMark Murray highpty = atoi(c);
28313e3f4d6SMark Murray }
28413e3f4d6SMark Murray if (*optarg != '\0')
28513e3f4d6SMark Murray lowpty = atoi(optarg);
28613e3f4d6SMark Murray if ((lowpty > highpty) || (lowpty < 0) ||
28713e3f4d6SMark Murray (highpty > 32767)) {
288c19800e8SDoug Rabson usage(1);
28913e3f4d6SMark Murray /* NOT REACHED */
29013e3f4d6SMark Murray }
29113e3f4d6SMark Murray break;
29213e3f4d6SMark Murray }
29313e3f4d6SMark Murray #endif /* CRAY */
29413e3f4d6SMark Murray
29513e3f4d6SMark Murray case 'S':
29613e3f4d6SMark Murray #ifdef HAVE_PARSETOS
29713e3f4d6SMark Murray if ((tos = parsetos(optarg, "tcp")) < 0)
29813e3f4d6SMark Murray fprintf(stderr, "%s%s%s\n",
29913e3f4d6SMark Murray "telnetd: Bad TOS argument '", optarg,
30013e3f4d6SMark Murray "'; will try to use default TOS");
30113e3f4d6SMark Murray #else
30213e3f4d6SMark Murray fprintf(stderr, "%s%s\n", "TOS option unavailable; ",
30313e3f4d6SMark Murray "-S flag not supported\n");
30413e3f4d6SMark Murray #endif
30513e3f4d6SMark Murray break;
30613e3f4d6SMark Murray
307adb0ddaeSAssar Westerlund case 'u': {
308adb0ddaeSAssar Westerlund char *eptr;
309adb0ddaeSAssar Westerlund
310adb0ddaeSAssar Westerlund utmp_len = strtol(optarg, &eptr, 0);
311adb0ddaeSAssar Westerlund if (optarg == eptr)
312adb0ddaeSAssar Westerlund fprintf(stderr, "telnetd: unknown utmp len (%s)\n", optarg);
31313e3f4d6SMark Murray break;
314adb0ddaeSAssar Westerlund }
31513e3f4d6SMark Murray
31613e3f4d6SMark Murray case 'U':
31713e3f4d6SMark Murray registerd_host_only = 1;
31813e3f4d6SMark Murray break;
31913e3f4d6SMark Murray
32013e3f4d6SMark Murray #ifdef AUTHENTICATION
32113e3f4d6SMark Murray case 'X':
32213e3f4d6SMark Murray /*
32313e3f4d6SMark Murray * Check for invalid authentication types
32413e3f4d6SMark Murray */
32513e3f4d6SMark Murray auth_disable_name(optarg);
32613e3f4d6SMark Murray break;
32713e3f4d6SMark Murray #endif
32813e3f4d6SMark Murray case 'y':
32913e3f4d6SMark Murray no_warn = 1;
33013e3f4d6SMark Murray break;
33113e3f4d6SMark Murray #ifdef AUTHENTICATION
33213e3f4d6SMark Murray case 'z':
33313e3f4d6SMark Murray log_unauth = 1;
33413e3f4d6SMark Murray break;
33513e3f4d6SMark Murray
33613e3f4d6SMark Murray #endif /* AUTHENTICATION */
33713e3f4d6SMark Murray
33813e3f4d6SMark Murray case 'L':
33913e3f4d6SMark Murray new_login = optarg;
34013e3f4d6SMark Murray break;
34113e3f4d6SMark Murray
34213e3f4d6SMark Murray default:
34313e3f4d6SMark Murray fprintf(stderr, "telnetd: %c: unknown option\n", ch);
34413e3f4d6SMark Murray /* FALLTHROUGH */
34513e3f4d6SMark Murray case '?':
346c19800e8SDoug Rabson usage(0);
34713e3f4d6SMark Murray /* NOTREACHED */
34813e3f4d6SMark Murray }
34913e3f4d6SMark Murray }
35013e3f4d6SMark Murray
35113e3f4d6SMark Murray argc -= optind;
35213e3f4d6SMark Murray argv += optind;
35313e3f4d6SMark Murray
35413e3f4d6SMark Murray if (debug) {
35513e3f4d6SMark Murray int port = 0;
35613e3f4d6SMark Murray struct servent *sp;
35713e3f4d6SMark Murray
35813e3f4d6SMark Murray if (argc > 1) {
359c19800e8SDoug Rabson usage (1);
36013e3f4d6SMark Murray } else if (argc == 1) {
36113e3f4d6SMark Murray sp = roken_getservbyname (*argv, "tcp");
36213e3f4d6SMark Murray if (sp)
36313e3f4d6SMark Murray port = sp->s_port;
36413e3f4d6SMark Murray else
36513e3f4d6SMark Murray port = htons(atoi(*argv));
36613e3f4d6SMark Murray } else {
36713e3f4d6SMark Murray #ifdef KRB5
36813e3f4d6SMark Murray port = krb5_getportbyname (NULL, "telnet", "tcp", 23);
36913e3f4d6SMark Murray #else
37013e3f4d6SMark Murray port = k_getportbyname("telnet", "tcp", htons(23));
37113e3f4d6SMark Murray #endif
37213e3f4d6SMark Murray }
373*ae771770SStanislav Sedov mini_inetd (port, NULL);
37413e3f4d6SMark Murray } else if (argc > 0) {
375c19800e8SDoug Rabson usage(1);
37613e3f4d6SMark Murray /* NOT REACHED */
37713e3f4d6SMark Murray }
37813e3f4d6SMark Murray
37913e3f4d6SMark Murray #ifdef _SC_CRAY_SECURE_SYS
38013e3f4d6SMark Murray secflag = sysconf(_SC_CRAY_SECURE_SYS);
38113e3f4d6SMark Murray
38213e3f4d6SMark Murray /*
38313e3f4d6SMark Murray * Get socket's security label
38413e3f4d6SMark Murray */
38513e3f4d6SMark Murray if (secflag) {
3865e9cd1aeSAssar Westerlund socklen_t szss = sizeof(ss);
38713e3f4d6SMark Murray int sock_multi;
3885e9cd1aeSAssar Westerlund socklen_t szi = sizeof(int);
38913e3f4d6SMark Murray
39013e3f4d6SMark Murray memset(&dv, 0, sizeof(dv));
39113e3f4d6SMark Murray
39213e3f4d6SMark Murray if (getsysv(&sysv, sizeof(struct sysv)) != 0)
39313e3f4d6SMark Murray fatalperror(net, "getsysv");
39413e3f4d6SMark Murray
39513e3f4d6SMark Murray /*
39613e3f4d6SMark Murray * Get socket security label and set device values
39713e3f4d6SMark Murray * {security label to be set on ttyp device}
39813e3f4d6SMark Murray */
39913e3f4d6SMark Murray #ifdef SO_SEC_MULTI /* 8.0 code */
40013e3f4d6SMark Murray if ((getsockopt(0, SOL_SOCKET, SO_SECURITY,
40113e3f4d6SMark Murray (void *)&ss, &szss) < 0) ||
40213e3f4d6SMark Murray (getsockopt(0, SOL_SOCKET, SO_SEC_MULTI,
40313e3f4d6SMark Murray (void *)&sock_multi, &szi) < 0))
40413e3f4d6SMark Murray fatalperror(net, "getsockopt");
40513e3f4d6SMark Murray else {
40613e3f4d6SMark Murray dv.dv_actlvl = ss.ss_actlabel.lt_level;
40713e3f4d6SMark Murray dv.dv_actcmp = ss.ss_actlabel.lt_compart;
40813e3f4d6SMark Murray if (!sock_multi) {
40913e3f4d6SMark Murray dv.dv_minlvl = dv.dv_maxlvl = dv.dv_actlvl;
41013e3f4d6SMark Murray dv.dv_valcmp = dv.dv_actcmp;
41113e3f4d6SMark Murray } else {
41213e3f4d6SMark Murray dv.dv_minlvl = ss.ss_minlabel.lt_level;
41313e3f4d6SMark Murray dv.dv_maxlvl = ss.ss_maxlabel.lt_level;
41413e3f4d6SMark Murray dv.dv_valcmp = ss.ss_maxlabel.lt_compart;
41513e3f4d6SMark Murray }
41613e3f4d6SMark Murray dv.dv_devflg = 0;
41713e3f4d6SMark Murray }
41813e3f4d6SMark Murray #else /* SO_SEC_MULTI */ /* 7.0 code */
41913e3f4d6SMark Murray if (getsockopt(0, SOL_SOCKET, SO_SECURITY,
42013e3f4d6SMark Murray (void *)&ss, &szss) >= 0) {
42113e3f4d6SMark Murray dv.dv_actlvl = ss.ss_slevel;
42213e3f4d6SMark Murray dv.dv_actcmp = ss.ss_compart;
42313e3f4d6SMark Murray dv.dv_minlvl = ss.ss_minlvl;
42413e3f4d6SMark Murray dv.dv_maxlvl = ss.ss_maxlvl;
42513e3f4d6SMark Murray dv.dv_valcmp = ss.ss_maxcmp;
42613e3f4d6SMark Murray }
42713e3f4d6SMark Murray #endif /* SO_SEC_MULTI */
42813e3f4d6SMark Murray }
42913e3f4d6SMark Murray #endif /* _SC_CRAY_SECURE_SYS */
43013e3f4d6SMark Murray
43113e3f4d6SMark Murray roken_openlog("telnetd", LOG_PID | LOG_ODELAY, LOG_DAEMON);
43213e3f4d6SMark Murray sa_size = sizeof (__ss);
43313e3f4d6SMark Murray if (getpeername(STDIN_FILENO, sa, &sa_size) < 0) {
43413e3f4d6SMark Murray fprintf(stderr, "%s: ", progname);
43513e3f4d6SMark Murray perror("getpeername");
43613e3f4d6SMark Murray _exit(1);
43713e3f4d6SMark Murray }
43813e3f4d6SMark Murray if (keepalive &&
43913e3f4d6SMark Murray setsockopt(STDIN_FILENO, SOL_SOCKET, SO_KEEPALIVE,
44013e3f4d6SMark Murray (void *)&on, sizeof (on)) < 0) {
44113e3f4d6SMark Murray syslog(LOG_WARNING, "setsockopt (SO_KEEPALIVE): %m");
44213e3f4d6SMark Murray }
44313e3f4d6SMark Murray
44413e3f4d6SMark Murray #if defined(IPPROTO_IP) && defined(IP_TOS) && defined(HAVE_SETSOCKOPT)
44513e3f4d6SMark Murray {
44613e3f4d6SMark Murray # ifdef HAVE_GETTOSBYNAME
44713e3f4d6SMark Murray struct tosent *tp;
44813e3f4d6SMark Murray if (tos < 0 && (tp = gettosbyname("telnet", "tcp")))
44913e3f4d6SMark Murray tos = tp->t_tos;
45013e3f4d6SMark Murray # endif
45113e3f4d6SMark Murray if (tos < 0)
45213e3f4d6SMark Murray tos = 020; /* Low Delay bit */
45313e3f4d6SMark Murray if (tos
45413e3f4d6SMark Murray && sa->sa_family == AF_INET
45513e3f4d6SMark Murray && (setsockopt(STDIN_FILENO, IPPROTO_IP, IP_TOS,
45613e3f4d6SMark Murray (void *)&tos, sizeof(tos)) < 0)
45713e3f4d6SMark Murray && (errno != ENOPROTOOPT) )
45813e3f4d6SMark Murray syslog(LOG_WARNING, "setsockopt (IP_TOS): %m");
45913e3f4d6SMark Murray }
46013e3f4d6SMark Murray #endif /* defined(IPPROTO_IP) && defined(IP_TOS) */
46113e3f4d6SMark Murray net = STDIN_FILENO;
46213e3f4d6SMark Murray doit(sa, sa_size);
46313e3f4d6SMark Murray /* NOTREACHED */
46413e3f4d6SMark Murray return 0;
46513e3f4d6SMark Murray } /* end of main */
46613e3f4d6SMark Murray
46713e3f4d6SMark Murray static void
usage(int exit_code)468c19800e8SDoug Rabson usage(int exit_code)
46913e3f4d6SMark Murray {
47013e3f4d6SMark Murray fprintf(stderr, "Usage: telnetd");
471c19800e8SDoug Rabson fprintf(stderr, " [--help]");
472c19800e8SDoug Rabson fprintf(stderr, " [--version]");
47313e3f4d6SMark Murray #ifdef AUTHENTICATION
47413e3f4d6SMark Murray fprintf(stderr, " [-a (debug|other|otp|user|valid|off|none)]\n\t");
47513e3f4d6SMark Murray #endif
47613e3f4d6SMark Murray fprintf(stderr, " [-debug]");
47713e3f4d6SMark Murray #ifdef DIAGNOSTICS
47813e3f4d6SMark Murray fprintf(stderr, " [-D (options|report|exercise|netdata|ptydata)]\n\t");
47913e3f4d6SMark Murray #endif
48013e3f4d6SMark Murray #ifdef AUTHENTICATION
48113e3f4d6SMark Murray fprintf(stderr, " [-edebug]");
48213e3f4d6SMark Murray #endif
48313e3f4d6SMark Murray fprintf(stderr, " [-h]");
48413e3f4d6SMark Murray fprintf(stderr, " [-L login]");
48513e3f4d6SMark Murray fprintf(stderr, " [-n]");
48613e3f4d6SMark Murray #ifdef _CRAY
48713e3f4d6SMark Murray fprintf(stderr, " [-r[lowpty]-[highpty]]");
48813e3f4d6SMark Murray #endif
48913e3f4d6SMark Murray fprintf(stderr, "\n\t");
49013e3f4d6SMark Murray #ifdef HAVE_GETTOSBYNAME
49113e3f4d6SMark Murray fprintf(stderr, " [-S tos]");
49213e3f4d6SMark Murray #endif
49313e3f4d6SMark Murray #ifdef AUTHENTICATION
49413e3f4d6SMark Murray fprintf(stderr, " [-X auth-type] [-y] [-z]");
49513e3f4d6SMark Murray #endif
49613e3f4d6SMark Murray fprintf(stderr, " [-u utmp_hostname_length] [-U]");
49713e3f4d6SMark Murray fprintf(stderr, " [port]\n");
498c19800e8SDoug Rabson exit(exit_code);
49913e3f4d6SMark Murray }
50013e3f4d6SMark Murray
50113e3f4d6SMark Murray /*
50213e3f4d6SMark Murray * getterminaltype
50313e3f4d6SMark Murray *
50413e3f4d6SMark Murray * Ask the other end to send along its terminal type and speed.
50513e3f4d6SMark Murray * Output is the variable terminaltype filled in.
50613e3f4d6SMark Murray */
50713e3f4d6SMark Murray static unsigned char ttytype_sbbuf[] = {
50813e3f4d6SMark Murray IAC, SB, TELOPT_TTYPE, TELQUAL_SEND, IAC, SE
50913e3f4d6SMark Murray };
51013e3f4d6SMark Murray
51113e3f4d6SMark Murray int
getterminaltype(char * name,size_t name_sz)51213e3f4d6SMark Murray getterminaltype(char *name, size_t name_sz)
51313e3f4d6SMark Murray {
51413e3f4d6SMark Murray int retval = -1;
51513e3f4d6SMark Murray
51613e3f4d6SMark Murray settimer(baseline);
51713e3f4d6SMark Murray #ifdef AUTHENTICATION
51813e3f4d6SMark Murray /*
51913e3f4d6SMark Murray * Handle the Authentication option before we do anything else.
52013e3f4d6SMark Murray */
52113e3f4d6SMark Murray send_do(TELOPT_AUTHENTICATION, 1);
52213e3f4d6SMark Murray while (his_will_wont_is_changing(TELOPT_AUTHENTICATION))
52313e3f4d6SMark Murray ttloop();
52413e3f4d6SMark Murray if (his_state_is_will(TELOPT_AUTHENTICATION)) {
52513e3f4d6SMark Murray retval = auth_wait(name, name_sz);
52613e3f4d6SMark Murray }
52713e3f4d6SMark Murray #endif
52813e3f4d6SMark Murray
52913e3f4d6SMark Murray #ifdef ENCRYPTION
53013e3f4d6SMark Murray send_will(TELOPT_ENCRYPT, 1);
53113e3f4d6SMark Murray send_do(TELOPT_ENCRYPT, 1); /* esc@magic.fi */
53213e3f4d6SMark Murray #endif
53313e3f4d6SMark Murray send_do(TELOPT_TTYPE, 1);
53413e3f4d6SMark Murray send_do(TELOPT_TSPEED, 1);
53513e3f4d6SMark Murray send_do(TELOPT_XDISPLOC, 1);
53613e3f4d6SMark Murray send_do(TELOPT_NEW_ENVIRON, 1);
53713e3f4d6SMark Murray send_do(TELOPT_OLD_ENVIRON, 1);
53813e3f4d6SMark Murray while (
53913e3f4d6SMark Murray #ifdef ENCRYPTION
54013e3f4d6SMark Murray his_do_dont_is_changing(TELOPT_ENCRYPT) ||
54113e3f4d6SMark Murray #endif
54213e3f4d6SMark Murray his_will_wont_is_changing(TELOPT_TTYPE) ||
54313e3f4d6SMark Murray his_will_wont_is_changing(TELOPT_TSPEED) ||
54413e3f4d6SMark Murray his_will_wont_is_changing(TELOPT_XDISPLOC) ||
54513e3f4d6SMark Murray his_will_wont_is_changing(TELOPT_NEW_ENVIRON) ||
54613e3f4d6SMark Murray his_will_wont_is_changing(TELOPT_OLD_ENVIRON)) {
54713e3f4d6SMark Murray ttloop();
54813e3f4d6SMark Murray }
54913e3f4d6SMark Murray #ifdef ENCRYPTION
55013e3f4d6SMark Murray /*
55113e3f4d6SMark Murray * Wait for the negotiation of what type of encryption we can
55213e3f4d6SMark Murray * send with. If autoencrypt is not set, this will just return.
55313e3f4d6SMark Murray */
55413e3f4d6SMark Murray if (his_state_is_will(TELOPT_ENCRYPT)) {
55513e3f4d6SMark Murray encrypt_wait();
55613e3f4d6SMark Murray }
557c19800e8SDoug Rabson if (require_encryption) {
558c19800e8SDoug Rabson
559c19800e8SDoug Rabson while (encrypt_delay())
560c19800e8SDoug Rabson if (telnet_spin())
561c19800e8SDoug Rabson fatal(net, "Failed while waiting for encryption");
562c19800e8SDoug Rabson
563c19800e8SDoug Rabson if (!encrypt_is_encrypting())
564c19800e8SDoug Rabson fatal(net, "Encryption required but not turned on by client");
565c19800e8SDoug Rabson }
56613e3f4d6SMark Murray #endif
56713e3f4d6SMark Murray if (his_state_is_will(TELOPT_TSPEED)) {
56813e3f4d6SMark Murray static unsigned char sb[] =
56913e3f4d6SMark Murray { IAC, SB, TELOPT_TSPEED, TELQUAL_SEND, IAC, SE };
57013e3f4d6SMark Murray
57113e3f4d6SMark Murray telnet_net_write (sb, sizeof sb);
57213e3f4d6SMark Murray DIAG(TD_OPTIONS, printsub('>', sb + 2, sizeof sb - 2););
57313e3f4d6SMark Murray }
57413e3f4d6SMark Murray if (his_state_is_will(TELOPT_XDISPLOC)) {
57513e3f4d6SMark Murray static unsigned char sb[] =
57613e3f4d6SMark Murray { IAC, SB, TELOPT_XDISPLOC, TELQUAL_SEND, IAC, SE };
57713e3f4d6SMark Murray
57813e3f4d6SMark Murray telnet_net_write (sb, sizeof sb);
57913e3f4d6SMark Murray DIAG(TD_OPTIONS, printsub('>', sb + 2, sizeof sb - 2););
58013e3f4d6SMark Murray }
58113e3f4d6SMark Murray if (his_state_is_will(TELOPT_NEW_ENVIRON)) {
58213e3f4d6SMark Murray static unsigned char sb[] =
58313e3f4d6SMark Murray { IAC, SB, TELOPT_NEW_ENVIRON, TELQUAL_SEND, IAC, SE };
58413e3f4d6SMark Murray
58513e3f4d6SMark Murray telnet_net_write (sb, sizeof sb);
58613e3f4d6SMark Murray DIAG(TD_OPTIONS, printsub('>', sb + 2, sizeof sb - 2););
58713e3f4d6SMark Murray }
58813e3f4d6SMark Murray else if (his_state_is_will(TELOPT_OLD_ENVIRON)) {
58913e3f4d6SMark Murray static unsigned char sb[] =
59013e3f4d6SMark Murray { IAC, SB, TELOPT_OLD_ENVIRON, TELQUAL_SEND, IAC, SE };
59113e3f4d6SMark Murray
59213e3f4d6SMark Murray telnet_net_write (sb, sizeof sb);
59313e3f4d6SMark Murray DIAG(TD_OPTIONS, printsub('>', sb + 2, sizeof sb - 2););
59413e3f4d6SMark Murray }
59513e3f4d6SMark Murray if (his_state_is_will(TELOPT_TTYPE)) {
59613e3f4d6SMark Murray
59713e3f4d6SMark Murray telnet_net_write (ttytype_sbbuf, sizeof ttytype_sbbuf);
59813e3f4d6SMark Murray DIAG(TD_OPTIONS, printsub('>', ttytype_sbbuf + 2,
59913e3f4d6SMark Murray sizeof ttytype_sbbuf - 2););
60013e3f4d6SMark Murray }
60113e3f4d6SMark Murray if (his_state_is_will(TELOPT_TSPEED)) {
60213e3f4d6SMark Murray while (sequenceIs(tspeedsubopt, baseline))
60313e3f4d6SMark Murray ttloop();
60413e3f4d6SMark Murray }
60513e3f4d6SMark Murray if (his_state_is_will(TELOPT_XDISPLOC)) {
60613e3f4d6SMark Murray while (sequenceIs(xdisplocsubopt, baseline))
60713e3f4d6SMark Murray ttloop();
60813e3f4d6SMark Murray }
60913e3f4d6SMark Murray if (his_state_is_will(TELOPT_NEW_ENVIRON)) {
61013e3f4d6SMark Murray while (sequenceIs(environsubopt, baseline))
61113e3f4d6SMark Murray ttloop();
61213e3f4d6SMark Murray }
61313e3f4d6SMark Murray if (his_state_is_will(TELOPT_OLD_ENVIRON)) {
61413e3f4d6SMark Murray while (sequenceIs(oenvironsubopt, baseline))
61513e3f4d6SMark Murray ttloop();
61613e3f4d6SMark Murray }
61713e3f4d6SMark Murray if (his_state_is_will(TELOPT_TTYPE)) {
61813e3f4d6SMark Murray char first[256], last[256];
61913e3f4d6SMark Murray
62013e3f4d6SMark Murray while (sequenceIs(ttypesubopt, baseline))
62113e3f4d6SMark Murray ttloop();
62213e3f4d6SMark Murray
62313e3f4d6SMark Murray /*
62413e3f4d6SMark Murray * If the other side has already disabled the option, then
62513e3f4d6SMark Murray * we have to just go with what we (might) have already gotten.
62613e3f4d6SMark Murray */
62713e3f4d6SMark Murray if (his_state_is_will(TELOPT_TTYPE) && !terminaltypeok(terminaltype)) {
62813e3f4d6SMark Murray strlcpy(first, terminaltype, sizeof(first));
62913e3f4d6SMark Murray for(;;) {
63013e3f4d6SMark Murray /*
63113e3f4d6SMark Murray * Save the unknown name, and request the next name.
63213e3f4d6SMark Murray */
63313e3f4d6SMark Murray strlcpy(last, terminaltype, sizeof(last));
63413e3f4d6SMark Murray _gettermname();
63513e3f4d6SMark Murray if (terminaltypeok(terminaltype))
63613e3f4d6SMark Murray break;
63713e3f4d6SMark Murray if ((strncmp(last, terminaltype, sizeof(last)) == 0) ||
63813e3f4d6SMark Murray his_state_is_wont(TELOPT_TTYPE)) {
63913e3f4d6SMark Murray /*
64013e3f4d6SMark Murray * We've hit the end. If this is the same as
64113e3f4d6SMark Murray * the first name, just go with it.
64213e3f4d6SMark Murray */
64313e3f4d6SMark Murray if (strncmp(first, terminaltype, sizeof(first)) == 0)
64413e3f4d6SMark Murray break;
64513e3f4d6SMark Murray /*
64613e3f4d6SMark Murray * Get the terminal name one more time, so that
64713e3f4d6SMark Murray * RFC1091 compliant telnets will cycle back to
64813e3f4d6SMark Murray * the start of the list.
64913e3f4d6SMark Murray */
65013e3f4d6SMark Murray _gettermname();
65113e3f4d6SMark Murray if (strncmp(first, terminaltype, sizeof(first)) != 0)
652c19800e8SDoug Rabson strlcpy(terminaltype, first, sizeof(terminaltype));
65313e3f4d6SMark Murray break;
65413e3f4d6SMark Murray }
65513e3f4d6SMark Murray }
65613e3f4d6SMark Murray }
65713e3f4d6SMark Murray }
65813e3f4d6SMark Murray return(retval);
65913e3f4d6SMark Murray } /* end of getterminaltype */
66013e3f4d6SMark Murray
66113e3f4d6SMark Murray void
_gettermname(void)662adb0ddaeSAssar Westerlund _gettermname(void)
66313e3f4d6SMark Murray {
66413e3f4d6SMark Murray /*
66513e3f4d6SMark Murray * If the client turned off the option,
66613e3f4d6SMark Murray * we can't send another request, so we
66713e3f4d6SMark Murray * just return.
66813e3f4d6SMark Murray */
66913e3f4d6SMark Murray if (his_state_is_wont(TELOPT_TTYPE))
67013e3f4d6SMark Murray return;
67113e3f4d6SMark Murray settimer(baseline);
67213e3f4d6SMark Murray telnet_net_write (ttytype_sbbuf, sizeof ttytype_sbbuf);
67313e3f4d6SMark Murray DIAG(TD_OPTIONS, printsub('>', ttytype_sbbuf + 2,
67413e3f4d6SMark Murray sizeof ttytype_sbbuf - 2););
67513e3f4d6SMark Murray while (sequenceIs(ttypesubopt, baseline))
67613e3f4d6SMark Murray ttloop();
67713e3f4d6SMark Murray }
67813e3f4d6SMark Murray
67913e3f4d6SMark Murray int
terminaltypeok(char * s)68013e3f4d6SMark Murray terminaltypeok(char *s)
68113e3f4d6SMark Murray {
68213e3f4d6SMark Murray return 1;
68313e3f4d6SMark Murray }
68413e3f4d6SMark Murray
68513e3f4d6SMark Murray
68613e3f4d6SMark Murray char host_name[MaxHostNameLen];
68713e3f4d6SMark Murray char remote_host_name[MaxHostNameLen];
688adb0ddaeSAssar Westerlund char remote_utmp_name[MaxHostNameLen];
68913e3f4d6SMark Murray
69013e3f4d6SMark Murray /*
69113e3f4d6SMark Murray * Get a pty, scan input lines.
69213e3f4d6SMark Murray */
69313e3f4d6SMark Murray static void
doit(struct sockaddr * who,int who_len)69413e3f4d6SMark Murray doit(struct sockaddr *who, int who_len)
69513e3f4d6SMark Murray {
69613e3f4d6SMark Murray int level;
69713e3f4d6SMark Murray int ptynum;
69813e3f4d6SMark Murray char user_name[256];
69913e3f4d6SMark Murray int error;
70013e3f4d6SMark Murray
70113e3f4d6SMark Murray /*
70213e3f4d6SMark Murray * Find an available pty to use.
70313e3f4d6SMark Murray */
70413e3f4d6SMark Murray ourpty = getpty(&ptynum);
70513e3f4d6SMark Murray if (ourpty < 0)
70613e3f4d6SMark Murray fatal(net, "All network ports in use");
70713e3f4d6SMark Murray
70813e3f4d6SMark Murray #ifdef _SC_CRAY_SECURE_SYS
70913e3f4d6SMark Murray /*
71013e3f4d6SMark Murray * set ttyp line security label
71113e3f4d6SMark Murray */
71213e3f4d6SMark Murray if (secflag) {
71313e3f4d6SMark Murray char slave_dev[16];
71413e3f4d6SMark Murray
71513e3f4d6SMark Murray snprintf(tty_dev, sizeof(tty_dev), "/dev/pty/%03d", ptynum);
71613e3f4d6SMark Murray if (setdevs(tty_dev, &dv) < 0)
71713e3f4d6SMark Murray fatal(net, "cannot set pty security");
71813e3f4d6SMark Murray snprintf(slave_dev, sizeof(slave_dev), "/dev/ttyp%03d", ptynum);
71913e3f4d6SMark Murray if (setdevs(slave_dev, &dv) < 0)
72013e3f4d6SMark Murray fatal(net, "cannot set tty security");
72113e3f4d6SMark Murray }
72213e3f4d6SMark Murray #endif /* _SC_CRAY_SECURE_SYS */
72313e3f4d6SMark Murray
724adb0ddaeSAssar Westerlund error = getnameinfo_verified (who, who_len,
725adb0ddaeSAssar Westerlund remote_host_name,
726adb0ddaeSAssar Westerlund sizeof(remote_host_name),
72713e3f4d6SMark Murray NULL, 0,
72813e3f4d6SMark Murray registerd_host_only ? NI_NAMEREQD : 0);
72913e3f4d6SMark Murray if (error)
73013e3f4d6SMark Murray fatal(net, "Couldn't resolve your address into a host name.\r\n\
73113e3f4d6SMark Murray Please contact your net administrator");
73213e3f4d6SMark Murray
73313e3f4d6SMark Murray gethostname(host_name, sizeof (host_name));
734adb0ddaeSAssar Westerlund
735adb0ddaeSAssar Westerlund strlcpy (remote_utmp_name, remote_host_name, sizeof(remote_utmp_name));
73613e3f4d6SMark Murray
73713e3f4d6SMark Murray /* Only trim if too long (and possible) */
738adb0ddaeSAssar Westerlund if (strlen(remote_utmp_name) > utmp_len) {
73913e3f4d6SMark Murray char *domain = strchr(host_name, '.');
740adb0ddaeSAssar Westerlund char *p = strchr(remote_utmp_name, '.');
741adb0ddaeSAssar Westerlund if (domain != NULL && p != NULL && (strcmp(p, domain) == 0))
742adb0ddaeSAssar Westerlund *p = '\0'; /* remove domain part */
74313e3f4d6SMark Murray }
74413e3f4d6SMark Murray
74513e3f4d6SMark Murray /*
74613e3f4d6SMark Murray * If hostname still doesn't fit utmp, use ipaddr.
74713e3f4d6SMark Murray */
748adb0ddaeSAssar Westerlund if (strlen(remote_utmp_name) > utmp_len) {
749adb0ddaeSAssar Westerlund error = getnameinfo (who, who_len,
750adb0ddaeSAssar Westerlund remote_utmp_name,
751adb0ddaeSAssar Westerlund sizeof(remote_utmp_name),
752adb0ddaeSAssar Westerlund NULL, 0,
753adb0ddaeSAssar Westerlund NI_NUMERICHOST);
754adb0ddaeSAssar Westerlund if (error)
755adb0ddaeSAssar Westerlund fatal(net, "Couldn't get numeric address\r\n");
756adb0ddaeSAssar Westerlund }
75713e3f4d6SMark Murray
75813e3f4d6SMark Murray #ifdef AUTHENTICATION
759adb0ddaeSAssar Westerlund auth_encrypt_init(host_name, remote_host_name, "TELNETD", 1);
76013e3f4d6SMark Murray #endif
76113e3f4d6SMark Murray
76213e3f4d6SMark Murray init_env();
763c19800e8SDoug Rabson
764c19800e8SDoug Rabson /* begin server processing */
765c19800e8SDoug Rabson
766c19800e8SDoug Rabson /*
767c19800e8SDoug Rabson * Initialize the slc mapping table.
768c19800e8SDoug Rabson */
769c19800e8SDoug Rabson
770c19800e8SDoug Rabson get_slc_defaults();
771c19800e8SDoug Rabson
77213e3f4d6SMark Murray /*
77313e3f4d6SMark Murray * get terminal type.
77413e3f4d6SMark Murray */
77513e3f4d6SMark Murray *user_name = 0;
77613e3f4d6SMark Murray level = getterminaltype(user_name, sizeof(user_name));
777c19800e8SDoug Rabson esetenv("TERM", terminaltype[0] ? terminaltype : "network", 1);
77813e3f4d6SMark Murray
77913e3f4d6SMark Murray #ifdef _SC_CRAY_SECURE_SYS
78013e3f4d6SMark Murray if (secflag) {
78113e3f4d6SMark Murray if (setulvl(dv.dv_actlvl) < 0)
78213e3f4d6SMark Murray fatal(net,"cannot setulvl()");
78313e3f4d6SMark Murray if (setucmp(dv.dv_actcmp) < 0)
78413e3f4d6SMark Murray fatal(net, "cannot setucmp()");
78513e3f4d6SMark Murray }
78613e3f4d6SMark Murray #endif /* _SC_CRAY_SECURE_SYS */
78713e3f4d6SMark Murray
788adb0ddaeSAssar Westerlund my_telnet(net, ourpty, remote_host_name, remote_utmp_name,
789adb0ddaeSAssar Westerlund level, user_name);
79013e3f4d6SMark Murray /*NOTREACHED*/
79113e3f4d6SMark Murray } /* end of doit */
79213e3f4d6SMark Murray
79313e3f4d6SMark Murray /* output contents of /etc/issue.net, or /etc/issue */
79413e3f4d6SMark Murray static void
show_issue(void)79513e3f4d6SMark Murray show_issue(void)
79613e3f4d6SMark Murray {
79713e3f4d6SMark Murray FILE *f;
79813e3f4d6SMark Murray char buf[128];
7998373020dSJacques Vidrine f = fopen(SYSCONFDIR "/issue.net", "r");
80013e3f4d6SMark Murray if(f == NULL)
8018373020dSJacques Vidrine f = fopen(SYSCONFDIR "/issue", "r");
80213e3f4d6SMark Murray if(f){
803c19800e8SDoug Rabson while(fgets(buf, sizeof(buf), f) != NULL) {
804c19800e8SDoug Rabson size_t len = strcspn(buf, "\r\n");
805c19800e8SDoug Rabson if(len == strlen(buf)) {
806c19800e8SDoug Rabson /* there's no newline */
807c19800e8SDoug Rabson writenet(buf, len);
808c19800e8SDoug Rabson } else {
809c19800e8SDoug Rabson /* replace newline with \r\n */
810c19800e8SDoug Rabson buf[len] = '\0';
811c19800e8SDoug Rabson writenet(buf, len);
812c19800e8SDoug Rabson writenet("\r\n", 2);
813c19800e8SDoug Rabson }
81413e3f4d6SMark Murray }
81513e3f4d6SMark Murray fclose(f);
81613e3f4d6SMark Murray }
81713e3f4d6SMark Murray }
81813e3f4d6SMark Murray
81913e3f4d6SMark Murray /*
82013e3f4d6SMark Murray * Main loop. Select from pty and network, and
82113e3f4d6SMark Murray * hand data to telnet receiver finite state machine.
82213e3f4d6SMark Murray */
82313e3f4d6SMark Murray void
my_telnet(int f,int p,const char * host,const char * utmp_host,int level,char * autoname)824adb0ddaeSAssar Westerlund my_telnet(int f, int p, const char *host, const char *utmp_host,
825adb0ddaeSAssar Westerlund int level, char *autoname)
82613e3f4d6SMark Murray {
82713e3f4d6SMark Murray int on = 1;
82813e3f4d6SMark Murray char *he;
82913e3f4d6SMark Murray char *IM;
83013e3f4d6SMark Murray int nfd;
83113e3f4d6SMark Murray int startslave_called = 0;
83213e3f4d6SMark Murray time_t timeout;
83313e3f4d6SMark Murray
83413e3f4d6SMark Murray /*
83513e3f4d6SMark Murray * Do some tests where it is desireable to wait for a response.
83613e3f4d6SMark Murray * Rather than doing them slowly, one at a time, do them all
83713e3f4d6SMark Murray * at once.
83813e3f4d6SMark Murray */
83913e3f4d6SMark Murray if (my_state_is_wont(TELOPT_SGA))
84013e3f4d6SMark Murray send_will(TELOPT_SGA, 1);
84113e3f4d6SMark Murray /*
84213e3f4d6SMark Murray * Is the client side a 4.2 (NOT 4.3) system? We need to know this
84313e3f4d6SMark Murray * because 4.2 clients are unable to deal with TCP urgent data.
84413e3f4d6SMark Murray *
84513e3f4d6SMark Murray * To find out, we send out a "DO ECHO". If the remote system
84613e3f4d6SMark Murray * answers "WILL ECHO" it is probably a 4.2 client, and we note
84713e3f4d6SMark Murray * that fact ("WILL ECHO" ==> that the client will echo what
84813e3f4d6SMark Murray * WE, the server, sends it; it does NOT mean that the client will
84913e3f4d6SMark Murray * echo the terminal input).
85013e3f4d6SMark Murray */
85113e3f4d6SMark Murray send_do(TELOPT_ECHO, 1);
85213e3f4d6SMark Murray
85313e3f4d6SMark Murray /*
85413e3f4d6SMark Murray * Send along a couple of other options that we wish to negotiate.
85513e3f4d6SMark Murray */
85613e3f4d6SMark Murray send_do(TELOPT_NAWS, 1);
85713e3f4d6SMark Murray send_will(TELOPT_STATUS, 1);
85813e3f4d6SMark Murray flowmode = 1; /* default flow control state */
85913e3f4d6SMark Murray restartany = -1; /* uninitialized... */
86013e3f4d6SMark Murray send_do(TELOPT_LFLOW, 1);
86113e3f4d6SMark Murray
86213e3f4d6SMark Murray /*
86313e3f4d6SMark Murray * Spin, waiting for a response from the DO ECHO. However,
86413e3f4d6SMark Murray * some REALLY DUMB telnets out there might not respond
86513e3f4d6SMark Murray * to the DO ECHO. So, we spin looking for NAWS, (most dumb
86613e3f4d6SMark Murray * telnets so far seem to respond with WONT for a DO that
86713e3f4d6SMark Murray * they don't understand...) because by the time we get the
86813e3f4d6SMark Murray * response, it will already have processed the DO ECHO.
86913e3f4d6SMark Murray * Kludge upon kludge.
87013e3f4d6SMark Murray */
87113e3f4d6SMark Murray while (his_will_wont_is_changing(TELOPT_NAWS))
87213e3f4d6SMark Murray ttloop();
87313e3f4d6SMark Murray
87413e3f4d6SMark Murray /*
87513e3f4d6SMark Murray * But...
87613e3f4d6SMark Murray * The client might have sent a WILL NAWS as part of its
87713e3f4d6SMark Murray * startup code; if so, we'll be here before we get the
87813e3f4d6SMark Murray * response to the DO ECHO. We'll make the assumption
87913e3f4d6SMark Murray * that any implementation that understands about NAWS
88013e3f4d6SMark Murray * is a modern enough implementation that it will respond
88113e3f4d6SMark Murray * to our DO ECHO request; hence we'll do another spin
88213e3f4d6SMark Murray * waiting for the ECHO option to settle down, which is
88313e3f4d6SMark Murray * what we wanted to do in the first place...
88413e3f4d6SMark Murray */
88513e3f4d6SMark Murray if (his_want_state_is_will(TELOPT_ECHO) &&
88613e3f4d6SMark Murray his_state_is_will(TELOPT_NAWS)) {
88713e3f4d6SMark Murray while (his_will_wont_is_changing(TELOPT_ECHO))
88813e3f4d6SMark Murray ttloop();
88913e3f4d6SMark Murray }
89013e3f4d6SMark Murray /*
89113e3f4d6SMark Murray * On the off chance that the telnet client is broken and does not
89213e3f4d6SMark Murray * respond to the DO ECHO we sent, (after all, we did send the
89313e3f4d6SMark Murray * DO NAWS negotiation after the DO ECHO, and we won't get here
89413e3f4d6SMark Murray * until a response to the DO NAWS comes back) simulate the
89513e3f4d6SMark Murray * receipt of a will echo. This will also send a WONT ECHO
89613e3f4d6SMark Murray * to the client, since we assume that the client failed to
89713e3f4d6SMark Murray * respond because it believes that it is already in DO ECHO
89813e3f4d6SMark Murray * mode, which we do not want.
89913e3f4d6SMark Murray */
90013e3f4d6SMark Murray if (his_want_state_is_will(TELOPT_ECHO)) {
90113e3f4d6SMark Murray DIAG(TD_OPTIONS,
90213e3f4d6SMark Murray {output_data("td: simulating recv\r\n");
90313e3f4d6SMark Murray });
90413e3f4d6SMark Murray willoption(TELOPT_ECHO);
90513e3f4d6SMark Murray }
90613e3f4d6SMark Murray
90713e3f4d6SMark Murray /*
90813e3f4d6SMark Murray * Finally, to clean things up, we turn on our echo. This
90913e3f4d6SMark Murray * will break stupid 4.2 telnets out of local terminal echo.
91013e3f4d6SMark Murray */
91113e3f4d6SMark Murray
91213e3f4d6SMark Murray if (my_state_is_wont(TELOPT_ECHO))
91313e3f4d6SMark Murray send_will(TELOPT_ECHO, 1);
91413e3f4d6SMark Murray
91513e3f4d6SMark Murray #ifdef TIOCPKT
91613e3f4d6SMark Murray #ifdef STREAMSPTY
91713e3f4d6SMark Murray if (!really_stream)
91813e3f4d6SMark Murray #endif
91913e3f4d6SMark Murray /*
92013e3f4d6SMark Murray * Turn on packet mode
92113e3f4d6SMark Murray */
92213e3f4d6SMark Murray ioctl(p, TIOCPKT, (char *)&on);
92313e3f4d6SMark Murray #endif
92413e3f4d6SMark Murray
92513e3f4d6SMark Murray
92613e3f4d6SMark Murray /*
92713e3f4d6SMark Murray * Call telrcv() once to pick up anything received during
92813e3f4d6SMark Murray * terminal type negotiation, 4.2/4.3 determination, and
92913e3f4d6SMark Murray * linemode negotiation.
93013e3f4d6SMark Murray */
93113e3f4d6SMark Murray telrcv();
93213e3f4d6SMark Murray
93313e3f4d6SMark Murray ioctl(f, FIONBIO, (char *)&on);
93413e3f4d6SMark Murray ioctl(p, FIONBIO, (char *)&on);
93513e3f4d6SMark Murray
93613e3f4d6SMark Murray #if defined(SO_OOBINLINE) && defined(HAVE_SETSOCKOPT)
93713e3f4d6SMark Murray setsockopt(net, SOL_SOCKET, SO_OOBINLINE,
93813e3f4d6SMark Murray (void *)&on, sizeof on);
93913e3f4d6SMark Murray #endif /* defined(SO_OOBINLINE) */
94013e3f4d6SMark Murray
94113e3f4d6SMark Murray #ifdef SIGTSTP
94213e3f4d6SMark Murray signal(SIGTSTP, SIG_IGN);
94313e3f4d6SMark Murray #endif
94413e3f4d6SMark Murray #ifdef SIGTTOU
94513e3f4d6SMark Murray /*
94613e3f4d6SMark Murray * Ignoring SIGTTOU keeps the kernel from blocking us
94713e3f4d6SMark Murray * in ttioct() in /sys/tty.c.
94813e3f4d6SMark Murray */
94913e3f4d6SMark Murray signal(SIGTTOU, SIG_IGN);
95013e3f4d6SMark Murray #endif
95113e3f4d6SMark Murray
95213e3f4d6SMark Murray signal(SIGCHLD, cleanup);
95313e3f4d6SMark Murray
95413e3f4d6SMark Murray #ifdef TIOCNOTTY
95513e3f4d6SMark Murray {
95613e3f4d6SMark Murray int t;
95713e3f4d6SMark Murray t = open(_PATH_TTY, O_RDWR);
95813e3f4d6SMark Murray if (t >= 0) {
95913e3f4d6SMark Murray ioctl(t, TIOCNOTTY, (char *)0);
96013e3f4d6SMark Murray close(t);
96113e3f4d6SMark Murray }
96213e3f4d6SMark Murray }
96313e3f4d6SMark Murray #endif
96413e3f4d6SMark Murray
96513e3f4d6SMark Murray show_issue();
96613e3f4d6SMark Murray /*
96713e3f4d6SMark Murray * Show banner that getty never gave.
96813e3f4d6SMark Murray *
96913e3f4d6SMark Murray * We put the banner in the pty input buffer. This way, it
97013e3f4d6SMark Murray * gets carriage return null processing, etc., just like all
97113e3f4d6SMark Murray * other pty --> client data.
97213e3f4d6SMark Murray */
97313e3f4d6SMark Murray
97413e3f4d6SMark Murray if (getenv("USER"))
97513e3f4d6SMark Murray hostinfo = 0;
97613e3f4d6SMark Murray
97713e3f4d6SMark Murray IM = DEFAULT_IM;
97813e3f4d6SMark Murray he = 0;
97913e3f4d6SMark Murray edithost(he, host_name);
98013e3f4d6SMark Murray if (hostinfo && *IM)
98113e3f4d6SMark Murray putf(IM, ptyibuf2);
98213e3f4d6SMark Murray
98313e3f4d6SMark Murray if (pcc)
98413e3f4d6SMark Murray strncat(ptyibuf2, ptyip, pcc+1);
98513e3f4d6SMark Murray ptyip = ptyibuf2;
98613e3f4d6SMark Murray pcc = strlen(ptyip);
98713e3f4d6SMark Murray
98813e3f4d6SMark Murray DIAG(TD_REPORT, {
98913e3f4d6SMark Murray output_data("td: Entering processing loop\r\n");
99013e3f4d6SMark Murray });
99113e3f4d6SMark Murray
99213e3f4d6SMark Murray
99313e3f4d6SMark Murray nfd = ((f > p) ? f : p) + 1;
99413e3f4d6SMark Murray timeout = time(NULL) + 5;
99513e3f4d6SMark Murray for (;;) {
99613e3f4d6SMark Murray fd_set ibits, obits, xbits;
99713e3f4d6SMark Murray int c;
99813e3f4d6SMark Murray
99913e3f4d6SMark Murray /* wait for encryption to be turned on, but don't wait
100013e3f4d6SMark Murray indefinitely */
100113e3f4d6SMark Murray if(!startslave_called && (!encrypt_delay() || timeout > time(NULL))){
100213e3f4d6SMark Murray startslave_called = 1;
1003adb0ddaeSAssar Westerlund startslave(host, utmp_host, level, autoname);
100413e3f4d6SMark Murray }
100513e3f4d6SMark Murray
100613e3f4d6SMark Murray if (ncc < 0 && pcc < 0)
100713e3f4d6SMark Murray break;
100813e3f4d6SMark Murray
100913e3f4d6SMark Murray FD_ZERO(&ibits);
101013e3f4d6SMark Murray FD_ZERO(&obits);
101113e3f4d6SMark Murray FD_ZERO(&xbits);
10125e9cd1aeSAssar Westerlund
10135e9cd1aeSAssar Westerlund if (f >= FD_SETSIZE
10145e9cd1aeSAssar Westerlund || p >= FD_SETSIZE)
10155e9cd1aeSAssar Westerlund fatal(net, "fd too large");
10165e9cd1aeSAssar Westerlund
101713e3f4d6SMark Murray /*
101813e3f4d6SMark Murray * Never look for input if there's still
101913e3f4d6SMark Murray * stuff in the corresponding output buffer
102013e3f4d6SMark Murray */
102113e3f4d6SMark Murray if (nfrontp - nbackp || pcc > 0) {
102213e3f4d6SMark Murray FD_SET(f, &obits);
102313e3f4d6SMark Murray } else {
102413e3f4d6SMark Murray FD_SET(p, &ibits);
102513e3f4d6SMark Murray }
102613e3f4d6SMark Murray if (pfrontp - pbackp || ncc > 0) {
102713e3f4d6SMark Murray FD_SET(p, &obits);
102813e3f4d6SMark Murray } else {
102913e3f4d6SMark Murray FD_SET(f, &ibits);
103013e3f4d6SMark Murray }
103113e3f4d6SMark Murray if (!SYNCHing) {
103213e3f4d6SMark Murray FD_SET(f, &xbits);
103313e3f4d6SMark Murray }
103413e3f4d6SMark Murray if ((c = select(nfd, &ibits, &obits, &xbits,
103513e3f4d6SMark Murray (struct timeval *)0)) < 1) {
103613e3f4d6SMark Murray if (c == -1) {
103713e3f4d6SMark Murray if (errno == EINTR) {
103813e3f4d6SMark Murray continue;
103913e3f4d6SMark Murray }
104013e3f4d6SMark Murray }
104113e3f4d6SMark Murray sleep(5);
104213e3f4d6SMark Murray continue;
104313e3f4d6SMark Murray }
104413e3f4d6SMark Murray
104513e3f4d6SMark Murray /*
104613e3f4d6SMark Murray * Any urgent data?
104713e3f4d6SMark Murray */
104813e3f4d6SMark Murray if (FD_ISSET(net, &xbits)) {
104913e3f4d6SMark Murray SYNCHing = 1;
105013e3f4d6SMark Murray }
105113e3f4d6SMark Murray
105213e3f4d6SMark Murray /*
105313e3f4d6SMark Murray * Something to read from the network...
105413e3f4d6SMark Murray */
105513e3f4d6SMark Murray if (FD_ISSET(net, &ibits)) {
105613e3f4d6SMark Murray #ifndef SO_OOBINLINE
105713e3f4d6SMark Murray /*
105813e3f4d6SMark Murray * In 4.2 (and 4.3 beta) systems, the
105913e3f4d6SMark Murray * OOB indication and data handling in the kernel
106013e3f4d6SMark Murray * is such that if two separate TCP Urgent requests
106113e3f4d6SMark Murray * come in, one byte of TCP data will be overlaid.
106213e3f4d6SMark Murray * This is fatal for Telnet, but we try to live
106313e3f4d6SMark Murray * with it.
106413e3f4d6SMark Murray *
106513e3f4d6SMark Murray * In addition, in 4.2 (and...), a special protocol
106613e3f4d6SMark Murray * is needed to pick up the TCP Urgent data in
106713e3f4d6SMark Murray * the correct sequence.
106813e3f4d6SMark Murray *
106913e3f4d6SMark Murray * What we do is: if we think we are in urgent
107013e3f4d6SMark Murray * mode, we look to see if we are "at the mark".
107113e3f4d6SMark Murray * If we are, we do an OOB receive. If we run
107213e3f4d6SMark Murray * this twice, we will do the OOB receive twice,
107313e3f4d6SMark Murray * but the second will fail, since the second
107413e3f4d6SMark Murray * time we were "at the mark", but there wasn't
107513e3f4d6SMark Murray * any data there (the kernel doesn't reset
107613e3f4d6SMark Murray * "at the mark" until we do a normal read).
107713e3f4d6SMark Murray * Once we've read the OOB data, we go ahead
107813e3f4d6SMark Murray * and do normal reads.
107913e3f4d6SMark Murray *
108013e3f4d6SMark Murray * There is also another problem, which is that
108113e3f4d6SMark Murray * since the OOB byte we read doesn't put us
108213e3f4d6SMark Murray * out of OOB state, and since that byte is most
108313e3f4d6SMark Murray * likely the TELNET DM (data mark), we would
108413e3f4d6SMark Murray * stay in the TELNET SYNCH (SYNCHing) state.
108513e3f4d6SMark Murray * So, clocks to the rescue. If we've "just"
108613e3f4d6SMark Murray * received a DM, then we test for the
108713e3f4d6SMark Murray * presence of OOB data when the receive OOB
108813e3f4d6SMark Murray * fails (and AFTER we did the normal mode read
108913e3f4d6SMark Murray * to clear "at the mark").
109013e3f4d6SMark Murray */
109113e3f4d6SMark Murray if (SYNCHing) {
109213e3f4d6SMark Murray int atmark;
109313e3f4d6SMark Murray
109413e3f4d6SMark Murray ioctl(net, SIOCATMARK, (char *)&atmark);
109513e3f4d6SMark Murray if (atmark) {
109613e3f4d6SMark Murray ncc = recv(net, netibuf, sizeof (netibuf), MSG_OOB);
109713e3f4d6SMark Murray if ((ncc == -1) && (errno == EINVAL)) {
109813e3f4d6SMark Murray ncc = read(net, netibuf, sizeof (netibuf));
109913e3f4d6SMark Murray if (sequenceIs(didnetreceive, gotDM)) {
110013e3f4d6SMark Murray SYNCHing = stilloob(net);
110113e3f4d6SMark Murray }
110213e3f4d6SMark Murray }
110313e3f4d6SMark Murray } else {
110413e3f4d6SMark Murray ncc = read(net, netibuf, sizeof (netibuf));
110513e3f4d6SMark Murray }
110613e3f4d6SMark Murray } else {
110713e3f4d6SMark Murray ncc = read(net, netibuf, sizeof (netibuf));
110813e3f4d6SMark Murray }
110913e3f4d6SMark Murray settimer(didnetreceive);
111013e3f4d6SMark Murray #else /* !defined(SO_OOBINLINE)) */
111113e3f4d6SMark Murray ncc = read(net, netibuf, sizeof (netibuf));
111213e3f4d6SMark Murray #endif /* !defined(SO_OOBINLINE)) */
111313e3f4d6SMark Murray if (ncc < 0 && errno == EWOULDBLOCK)
111413e3f4d6SMark Murray ncc = 0;
111513e3f4d6SMark Murray else {
111613e3f4d6SMark Murray if (ncc <= 0) {
111713e3f4d6SMark Murray break;
111813e3f4d6SMark Murray }
111913e3f4d6SMark Murray netip = netibuf;
112013e3f4d6SMark Murray }
112113e3f4d6SMark Murray DIAG((TD_REPORT | TD_NETDATA), {
112213e3f4d6SMark Murray output_data("td: netread %d chars\r\n", ncc);
112313e3f4d6SMark Murray });
112413e3f4d6SMark Murray DIAG(TD_NETDATA, printdata("nd", netip, ncc));
112513e3f4d6SMark Murray }
112613e3f4d6SMark Murray
112713e3f4d6SMark Murray /*
112813e3f4d6SMark Murray * Something to read from the pty...
112913e3f4d6SMark Murray */
113013e3f4d6SMark Murray if (FD_ISSET(p, &ibits)) {
113113e3f4d6SMark Murray #ifdef STREAMSPTY
113213e3f4d6SMark Murray if (really_stream)
113313e3f4d6SMark Murray pcc = readstream(p, ptyibuf, BUFSIZ);
113413e3f4d6SMark Murray else
113513e3f4d6SMark Murray #endif
113613e3f4d6SMark Murray pcc = read(p, ptyibuf, BUFSIZ);
113713e3f4d6SMark Murray
113813e3f4d6SMark Murray /*
113913e3f4d6SMark Murray * On some systems, if we try to read something
114013e3f4d6SMark Murray * off the master side before the slave side is
114113e3f4d6SMark Murray * opened, we get EIO.
114213e3f4d6SMark Murray */
114313e3f4d6SMark Murray if (pcc < 0 && (errno == EWOULDBLOCK ||
114413e3f4d6SMark Murray #ifdef EAGAIN
114513e3f4d6SMark Murray errno == EAGAIN ||
114613e3f4d6SMark Murray #endif
114713e3f4d6SMark Murray errno == EIO)) {
114813e3f4d6SMark Murray pcc = 0;
114913e3f4d6SMark Murray } else {
115013e3f4d6SMark Murray if (pcc <= 0)
115113e3f4d6SMark Murray break;
115213e3f4d6SMark Murray if (ptyibuf[0] & TIOCPKT_FLUSHWRITE) {
115313e3f4d6SMark Murray netclear(); /* clear buffer back */
115413e3f4d6SMark Murray #ifndef NO_URGENT
115513e3f4d6SMark Murray /*
115613e3f4d6SMark Murray * There are client telnets on some
115713e3f4d6SMark Murray * operating systems get screwed up
115813e3f4d6SMark Murray * royally if we send them urgent
115913e3f4d6SMark Murray * mode data.
116013e3f4d6SMark Murray */
116113e3f4d6SMark Murray output_data ("%c%c", IAC, DM);
116213e3f4d6SMark Murray
116313e3f4d6SMark Murray neturg = nfrontp-1; /* off by one XXX */
116413e3f4d6SMark Murray DIAG(TD_OPTIONS,
116513e3f4d6SMark Murray printoption("td: send IAC", DM));
116613e3f4d6SMark Murray
116713e3f4d6SMark Murray #endif
116813e3f4d6SMark Murray }
116913e3f4d6SMark Murray if (his_state_is_will(TELOPT_LFLOW) &&
117013e3f4d6SMark Murray (ptyibuf[0] &
117113e3f4d6SMark Murray (TIOCPKT_NOSTOP|TIOCPKT_DOSTOP))) {
117213e3f4d6SMark Murray int newflow =
117313e3f4d6SMark Murray ptyibuf[0] & TIOCPKT_DOSTOP ? 1 : 0;
117413e3f4d6SMark Murray if (newflow != flowmode) {
117513e3f4d6SMark Murray flowmode = newflow;
117613e3f4d6SMark Murray output_data("%c%c%c%c%c%c",
117713e3f4d6SMark Murray IAC, SB, TELOPT_LFLOW,
117813e3f4d6SMark Murray flowmode ? LFLOW_ON
117913e3f4d6SMark Murray : LFLOW_OFF,
118013e3f4d6SMark Murray IAC, SE);
118113e3f4d6SMark Murray DIAG(TD_OPTIONS, printsub('>',
118213e3f4d6SMark Murray (unsigned char *)nfrontp-4,
118313e3f4d6SMark Murray 4););
118413e3f4d6SMark Murray }
118513e3f4d6SMark Murray }
118613e3f4d6SMark Murray pcc--;
118713e3f4d6SMark Murray ptyip = ptyibuf+1;
118813e3f4d6SMark Murray }
118913e3f4d6SMark Murray }
119013e3f4d6SMark Murray
119113e3f4d6SMark Murray while (pcc > 0) {
119213e3f4d6SMark Murray if ((&netobuf[BUFSIZ] - nfrontp) < 3)
119313e3f4d6SMark Murray break;
119413e3f4d6SMark Murray c = *ptyip++ & 0377, pcc--;
119513e3f4d6SMark Murray if (c == IAC)
119613e3f4d6SMark Murray *nfrontp++ = c;
119713e3f4d6SMark Murray *nfrontp++ = c;
119813e3f4d6SMark Murray if ((c == '\r') && (my_state_is_wont(TELOPT_BINARY))) {
119913e3f4d6SMark Murray if (pcc > 0 && ((*ptyip & 0377) == '\n')) {
120013e3f4d6SMark Murray *nfrontp++ = *ptyip++ & 0377;
120113e3f4d6SMark Murray pcc--;
120213e3f4d6SMark Murray } else
120313e3f4d6SMark Murray *nfrontp++ = '\0';
120413e3f4d6SMark Murray }
120513e3f4d6SMark Murray }
120613e3f4d6SMark Murray
120713e3f4d6SMark Murray if (FD_ISSET(f, &obits) && (nfrontp - nbackp) > 0)
120813e3f4d6SMark Murray netflush();
120913e3f4d6SMark Murray if (ncc > 0)
121013e3f4d6SMark Murray telrcv();
121113e3f4d6SMark Murray if (FD_ISSET(p, &obits) && (pfrontp - pbackp) > 0)
121213e3f4d6SMark Murray ptyflush();
121313e3f4d6SMark Murray }
121413e3f4d6SMark Murray cleanup(0);
121513e3f4d6SMark Murray }
121613e3f4d6SMark Murray
121713e3f4d6SMark Murray #ifndef TCSIG
121813e3f4d6SMark Murray # ifdef TIOCSIG
121913e3f4d6SMark Murray # define TCSIG TIOCSIG
122013e3f4d6SMark Murray # endif
122113e3f4d6SMark Murray #endif
122213e3f4d6SMark Murray
122313e3f4d6SMark Murray #ifdef STREAMSPTY
122413e3f4d6SMark Murray
122513e3f4d6SMark Murray int flowison = -1; /* current state of flow: -1 is unknown */
122613e3f4d6SMark Murray
122713e3f4d6SMark Murray int
readstream(int p,char * ibuf,int bufsize)122813e3f4d6SMark Murray readstream(int p, char *ibuf, int bufsize)
122913e3f4d6SMark Murray {
123013e3f4d6SMark Murray int flags = 0;
123113e3f4d6SMark Murray int ret = 0;
123213e3f4d6SMark Murray struct termios *tsp;
123313e3f4d6SMark Murray #if 0
123413e3f4d6SMark Murray struct termio *tp;
123513e3f4d6SMark Murray #endif
123613e3f4d6SMark Murray struct iocblk *ip;
123713e3f4d6SMark Murray char vstop, vstart;
123813e3f4d6SMark Murray int ixon;
123913e3f4d6SMark Murray int newflow;
124013e3f4d6SMark Murray
124113e3f4d6SMark Murray strbufc.maxlen = BUFSIZ;
124213e3f4d6SMark Murray strbufc.buf = (char *)ctlbuf;
124313e3f4d6SMark Murray strbufd.maxlen = bufsize-1;
124413e3f4d6SMark Murray strbufd.len = 0;
124513e3f4d6SMark Murray strbufd.buf = ibuf+1;
124613e3f4d6SMark Murray ibuf[0] = 0;
124713e3f4d6SMark Murray
124813e3f4d6SMark Murray ret = getmsg(p, &strbufc, &strbufd, &flags);
124913e3f4d6SMark Murray if (ret < 0) /* error of some sort -- probably EAGAIN */
125013e3f4d6SMark Murray return(-1);
125113e3f4d6SMark Murray
125213e3f4d6SMark Murray if (strbufc.len <= 0 || ctlbuf[0] == M_DATA) {
125313e3f4d6SMark Murray /* data message */
125413e3f4d6SMark Murray if (strbufd.len > 0) { /* real data */
125513e3f4d6SMark Murray return(strbufd.len + 1); /* count header char */
125613e3f4d6SMark Murray } else {
125713e3f4d6SMark Murray /* nothing there */
125813e3f4d6SMark Murray errno = EAGAIN;
125913e3f4d6SMark Murray return(-1);
126013e3f4d6SMark Murray }
126113e3f4d6SMark Murray }
126213e3f4d6SMark Murray
126313e3f4d6SMark Murray /*
126413e3f4d6SMark Murray * It's a control message. Return 1, to look at the flag we set
126513e3f4d6SMark Murray */
126613e3f4d6SMark Murray
126713e3f4d6SMark Murray switch (ctlbuf[0]) {
126813e3f4d6SMark Murray case M_FLUSH:
126913e3f4d6SMark Murray if (ibuf[1] & FLUSHW)
127013e3f4d6SMark Murray ibuf[0] = TIOCPKT_FLUSHWRITE;
127113e3f4d6SMark Murray return(1);
127213e3f4d6SMark Murray
127313e3f4d6SMark Murray case M_IOCTL:
127413e3f4d6SMark Murray ip = (struct iocblk *) (ibuf+1);
127513e3f4d6SMark Murray
127613e3f4d6SMark Murray switch (ip->ioc_cmd) {
127713e3f4d6SMark Murray #ifdef TCSETS
127813e3f4d6SMark Murray case TCSETS:
127913e3f4d6SMark Murray case TCSETSW:
128013e3f4d6SMark Murray case TCSETSF:
128113e3f4d6SMark Murray tsp = (struct termios *)
128213e3f4d6SMark Murray (ibuf+1 + sizeof(struct iocblk));
128313e3f4d6SMark Murray vstop = tsp->c_cc[VSTOP];
128413e3f4d6SMark Murray vstart = tsp->c_cc[VSTART];
128513e3f4d6SMark Murray ixon = tsp->c_iflag & IXON;
128613e3f4d6SMark Murray break;
128713e3f4d6SMark Murray #endif
128813e3f4d6SMark Murray #if 0
128913e3f4d6SMark Murray case TCSETA:
129013e3f4d6SMark Murray case TCSETAW:
129113e3f4d6SMark Murray case TCSETAF:
129213e3f4d6SMark Murray tp = (struct termio *) (ibuf+1 + sizeof(struct iocblk));
129313e3f4d6SMark Murray vstop = tp->c_cc[VSTOP];
129413e3f4d6SMark Murray vstart = tp->c_cc[VSTART];
129513e3f4d6SMark Murray ixon = tp->c_iflag & IXON;
129613e3f4d6SMark Murray break;
129713e3f4d6SMark Murray #endif
129813e3f4d6SMark Murray default:
129913e3f4d6SMark Murray errno = EAGAIN;
130013e3f4d6SMark Murray return(-1);
130113e3f4d6SMark Murray }
130213e3f4d6SMark Murray
130313e3f4d6SMark Murray newflow = (ixon && (vstart == 021) && (vstop == 023)) ? 1 : 0;
130413e3f4d6SMark Murray if (newflow != flowison) { /* it's a change */
130513e3f4d6SMark Murray flowison = newflow;
130613e3f4d6SMark Murray ibuf[0] = newflow ? TIOCPKT_DOSTOP : TIOCPKT_NOSTOP;
130713e3f4d6SMark Murray return(1);
130813e3f4d6SMark Murray }
130913e3f4d6SMark Murray }
131013e3f4d6SMark Murray
131113e3f4d6SMark Murray /* nothing worth doing anything about */
131213e3f4d6SMark Murray errno = EAGAIN;
131313e3f4d6SMark Murray return(-1);
131413e3f4d6SMark Murray }
131513e3f4d6SMark Murray #endif /* STREAMSPTY */
131613e3f4d6SMark Murray
131713e3f4d6SMark Murray /*
131813e3f4d6SMark Murray * Send interrupt to process on other side of pty.
131913e3f4d6SMark Murray * If it is in raw mode, just write NULL;
132013e3f4d6SMark Murray * otherwise, write intr char.
132113e3f4d6SMark Murray */
132213e3f4d6SMark Murray void
interrupt()132313e3f4d6SMark Murray interrupt()
132413e3f4d6SMark Murray {
132513e3f4d6SMark Murray ptyflush(); /* half-hearted */
132613e3f4d6SMark Murray
132713e3f4d6SMark Murray #if defined(STREAMSPTY) && defined(TIOCSIGNAL)
132813e3f4d6SMark Murray /* Streams PTY style ioctl to post a signal */
132913e3f4d6SMark Murray if (really_stream)
133013e3f4d6SMark Murray {
133113e3f4d6SMark Murray int sig = SIGINT;
133213e3f4d6SMark Murray ioctl(ourpty, TIOCSIGNAL, &sig);
133313e3f4d6SMark Murray ioctl(ourpty, I_FLUSH, FLUSHR);
133413e3f4d6SMark Murray }
133513e3f4d6SMark Murray #else
133613e3f4d6SMark Murray #ifdef TCSIG
133713e3f4d6SMark Murray ioctl(ourpty, TCSIG, (char *)SIGINT);
133813e3f4d6SMark Murray #else /* TCSIG */
133913e3f4d6SMark Murray init_termbuf();
134013e3f4d6SMark Murray *pfrontp++ = slctab[SLC_IP].sptr ?
134113e3f4d6SMark Murray (unsigned char)*slctab[SLC_IP].sptr : '\177';
134213e3f4d6SMark Murray #endif /* TCSIG */
134313e3f4d6SMark Murray #endif
134413e3f4d6SMark Murray }
134513e3f4d6SMark Murray
134613e3f4d6SMark Murray /*
134713e3f4d6SMark Murray * Send quit to process on other side of pty.
134813e3f4d6SMark Murray * If it is in raw mode, just write NULL;
134913e3f4d6SMark Murray * otherwise, write quit char.
135013e3f4d6SMark Murray */
135113e3f4d6SMark Murray void
sendbrk()135213e3f4d6SMark Murray sendbrk()
135313e3f4d6SMark Murray {
135413e3f4d6SMark Murray ptyflush(); /* half-hearted */
135513e3f4d6SMark Murray #ifdef TCSIG
135613e3f4d6SMark Murray ioctl(ourpty, TCSIG, (char *)SIGQUIT);
135713e3f4d6SMark Murray #else /* TCSIG */
135813e3f4d6SMark Murray init_termbuf();
135913e3f4d6SMark Murray *pfrontp++ = slctab[SLC_ABORT].sptr ?
136013e3f4d6SMark Murray (unsigned char)*slctab[SLC_ABORT].sptr : '\034';
136113e3f4d6SMark Murray #endif /* TCSIG */
136213e3f4d6SMark Murray }
136313e3f4d6SMark Murray
136413e3f4d6SMark Murray void
sendsusp()136513e3f4d6SMark Murray sendsusp()
136613e3f4d6SMark Murray {
136713e3f4d6SMark Murray #ifdef SIGTSTP
136813e3f4d6SMark Murray ptyflush(); /* half-hearted */
136913e3f4d6SMark Murray # ifdef TCSIG
137013e3f4d6SMark Murray ioctl(ourpty, TCSIG, (char *)SIGTSTP);
137113e3f4d6SMark Murray # else /* TCSIG */
137213e3f4d6SMark Murray *pfrontp++ = slctab[SLC_SUSP].sptr ?
137313e3f4d6SMark Murray (unsigned char)*slctab[SLC_SUSP].sptr : '\032';
137413e3f4d6SMark Murray # endif /* TCSIG */
137513e3f4d6SMark Murray #endif /* SIGTSTP */
137613e3f4d6SMark Murray }
137713e3f4d6SMark Murray
137813e3f4d6SMark Murray /*
137913e3f4d6SMark Murray * When we get an AYT, if ^T is enabled, use that. Otherwise,
138013e3f4d6SMark Murray * just send back "[Yes]".
138113e3f4d6SMark Murray */
138213e3f4d6SMark Murray void
recv_ayt()138313e3f4d6SMark Murray recv_ayt()
138413e3f4d6SMark Murray {
138513e3f4d6SMark Murray #if defined(SIGINFO) && defined(TCSIG)
138613e3f4d6SMark Murray if (slctab[SLC_AYT].sptr && *slctab[SLC_AYT].sptr != _POSIX_VDISABLE) {
138713e3f4d6SMark Murray ioctl(ourpty, TCSIG, (char *)SIGINFO);
138813e3f4d6SMark Murray return;
138913e3f4d6SMark Murray }
139013e3f4d6SMark Murray #endif
139113e3f4d6SMark Murray output_data("\r\n[Yes]\r\n");
139213e3f4d6SMark Murray }
139313e3f4d6SMark Murray
139413e3f4d6SMark Murray void
doeof()139513e3f4d6SMark Murray doeof()
139613e3f4d6SMark Murray {
139713e3f4d6SMark Murray init_termbuf();
139813e3f4d6SMark Murray
139913e3f4d6SMark Murray *pfrontp++ = slctab[SLC_EOF].sptr ?
140013e3f4d6SMark Murray (unsigned char)*slctab[SLC_EOF].sptr : '\004';
140113e3f4d6SMark Murray }
1402