2 * Copyright (c) 1995 Danny Gasparovski.
4 * Please read the file COPYRIGHT for the
5 * terms and conditions of the copyright.
8 #define WANT_SYS_IOCTL_H
12 u_int curtime
, time_fasttimo
, last_slowtimo
, detach_time
;
13 u_int detach_wait
= 600000; /* 10 minutes */
23 lprint("X Redir: X not being redirected.\r\n");
25 lprint("X Redir: In sh/bash/zsh/etc. type: DISPLAY=%s:%d.%d; export DISPLAY\r\n",
26 inet_ntoa(our_addr
), x_port
, x_screen
);
27 lprint("X Redir: In csh/tcsh/etc. type: setenv DISPLAY %s:%d.%d\r\n",
28 inet_ntoa(our_addr
), x_port
, x_screen
);
30 lprint("X Redir: Redirecting to display %d\r\n", x_display
);
38 * XXX Allow more than one X redirection?
41 redir_x(inaddr
, start_port
, display
, screen
)
50 lprint("X Redir: X already being redirected.\r\n");
53 for (i
= 6001 + (start_port
-1); i
<= 6100; i
++) {
54 if (solisten(htons(i
), inaddr
, htons(6000 + display
), 0)) {
63 lprint("X Redir: Error: Couldn't redirect a port for X. Weird.\r\n");
67 void redir_tcp(inaddr
, port
, lport
)
74 if ((so
=solisten(htons(lport
), inaddr
, htons(port
), 0)) != NULL
) {
75 lprint("Redirecting TCP port %d to %s:%d\r\n",
76 ntohs(so
->so_fport
), inet_ntoa(so
->so_laddr
), port
);
78 lprint("Redirection failed: %s\r\n", strerror(errno
));
81 #ifndef HAVE_INET_ATON
87 u_int32_t addr
= inet_addr(cp
);
88 if (addr
== 0xffffffff)
96 * Get our IP address and put it in our_addr
104 if (gethostname(buff
,256) < 0)
107 if ((he
= gethostbyname(buff
)) == NULL
)
110 our_addr
= *(struct in_addr
*)he
->h_addr
;
113 #if SIZEOF_CHAR_P == 8
125 register struct quehead_32
*element
= (struct quehead_32
*) a
;
126 register struct quehead_32
*head
= (struct quehead_32
*) b
;
127 element
->qh_link
= head
->qh_link
;
128 head
->qh_link
= (u_int32_t
)element
;
129 element
->qh_rlink
= (u_int32_t
)head
;
130 ((struct quehead_32
*)(element
->qh_link
))->qh_rlink
131 = (u_int32_t
)element
;
137 register struct quehead_32
*element
= (struct quehead_32
*) a
;
138 ((struct quehead_32
*)(element
->qh_link
))->qh_rlink
= element
->qh_rlink
;
139 ((struct quehead_32
*)(element
->qh_rlink
))->qh_link
= element
->qh_link
;
140 element
->qh_rlink
= 0;
143 #endif /* SIZEOF_CHAR_P == 8 */
146 struct quehead
*qh_link
;
147 struct quehead
*qh_rlink
;
154 register struct quehead
*element
= (struct quehead
*) a
;
155 register struct quehead
*head
= (struct quehead
*) b
;
156 element
->qh_link
= head
->qh_link
;
157 head
->qh_link
= (struct quehead
*)element
;
158 element
->qh_rlink
= (struct quehead
*)head
;
159 ((struct quehead
*)(element
->qh_link
))->qh_rlink
160 = (struct quehead
*)element
;
167 register struct quehead
*element
= (struct quehead
*) a
;
168 ((struct quehead
*)(element
->qh_link
))->qh_rlink
= element
->qh_rlink
;
169 ((struct quehead
*)(element
->qh_rlink
))->qh_link
= element
->qh_link
;
170 element
->qh_rlink
= NULL
;
171 /* element->qh_link = NULL; TCP FIN1 crashes if you do this. Why ? */
178 add_exec(ex_ptr
, do_pty
, exec
, addr
, port
)
179 struct ex_list
**ex_ptr
;
185 struct ex_list
*tmp_ptr
;
187 /* First, check if the port is "bound" */
188 for (tmp_ptr
= *ex_ptr
; tmp_ptr
; tmp_ptr
= tmp_ptr
->ex_next
) {
189 if (port
== tmp_ptr
->ex_fport
&& addr
== tmp_ptr
->ex_addr
)
194 *ex_ptr
= (struct ex_list
*)malloc(sizeof(struct ex_list
));
195 (*ex_ptr
)->ex_fport
= port
;
196 (*ex_ptr
)->ex_addr
= addr
;
197 (*ex_ptr
)->ex_pty
= do_pty
;
198 (*ex_ptr
)->ex_exec
= strdup(exec
);
199 (*ex_ptr
)->ex_next
= tmp_ptr
;
203 #ifndef HAVE_STRERROR
206 * For systems with no strerror
210 extern char *sys_errlist
[];
216 if (error
< sys_nerr
)
217 return sys_errlist
[error
];
219 return "Unknown error.";
227 openpty(amaster
, aslave
)
228 int *amaster
, *aslave
;
230 register int master
, slave
;
235 if ((master
= open("/dev/ptmx", O_RDWR
)) < 0 ||
236 grantpt(master
) < 0 ||
237 unlockpt(master
) < 0 ||
238 (ptr
= ptsname(master
)) == NULL
) {
243 if ((slave
= open(ptr
, O_RDWR
)) < 0 ||
244 ioctl(slave
, I_PUSH
, "ptem") < 0 ||
245 ioctl(slave
, I_PUSH
, "ldterm") < 0 ||
246 ioctl(slave
, I_PUSH
, "ttcompat") < 0) {
258 static char line
[] = "/dev/ptyXX";
259 register const char *cp1
, *cp2
;
261 for (cp1
= "pqrsPQRS"; *cp1
; cp1
++) {
263 for (cp2
= "0123456789abcdefghijklmnopqrstuv"; *cp2
; cp2
++) {
265 if ((master
= open(line
, O_RDWR
, 0)) == -1) {
267 return (-1); /* out of ptys */
270 /* These will fail */
271 (void) chown(line
, getuid(), 0);
272 (void) chmod(line
, S_IRUSR
|S_IWUSR
|S_IWGRP
);
276 if ((slave
= open(line
, O_RDWR
, 0)) != -1) {
281 (void) close(master
);
286 errno
= ENOENT
; /* out of ptys */
293 * We create and bind a socket, then fork off to another
294 * process, which connects to this socket, after which we
295 * exec the wanted program. If something (strange) happens,
296 * the accept() call could block us forever.
298 * do_pty = 0 Fork/exec inetd style
299 * do_pty = 1 Fork/exec using slirp.telnetd
300 * do_ptr = 2 Fork/exec using pty
303 fork_exec(so
, ex
, do_pty
)
309 struct sockaddr_in addr
;
310 int addrlen
= sizeof(addr
);
315 /* don't want to clobber the original */
320 DEBUG_CALL("fork_exec");
321 DEBUG_ARG("so = %lx", (long)so
);
322 DEBUG_ARG("ex = %lx", (long)ex
);
323 DEBUG_ARG("do_pty = %lx", (long)do_pty
);
326 if (openpty(&master
, &s
) == -1) {
327 lprint("Error: openpty failed: %s\n", strerror(errno
));
331 addr
.sin_family
= AF_INET
;
333 addr
.sin_addr
.s_addr
= INADDR_ANY
;
335 if ((s
= socket(AF_INET
, SOCK_STREAM
, 0)) < 0 ||
336 bind(s
, (struct sockaddr
*)&addr
, addrlen
) < 0 ||
338 lprint("Error: inet socket: %s\n", strerror(errno
));
347 lprint("Error: fork failed: %s\n", strerror(errno
));
354 /* Set the DISPLAY */
356 (void) close(master
);
357 #ifdef TIOCSCTTY /* XXXXX */
359 ioctl(s
, TIOCSCTTY
, (char *)NULL
);
362 getsockname(s
, (struct sockaddr
*)&addr
, &addrlen
);
365 * Connect to the socket
366 * XXX If any of these fail, we're in trouble!
368 s
= socket(AF_INET
, SOCK_STREAM
, 0);
369 addr
.sin_addr
= loopback_addr
;
370 connect(s
, (struct sockaddr
*)&addr
, addrlen
);
375 sprintf(buff
, "%s:%d.%d", inet_ntoa(our_addr
), x_port
, x_screen
);
376 setenv("DISPLAY", buff
, 1);
378 sprintf(buff
, "DISPLAY=%s:%d.%d", inet_ntoa(our_addr
), x_port
, x_screen
);
386 for (s
= 3; s
<= 255; s
++)
390 bptr
= strdup(ex
); /* No need to free() this */
392 /* Setup "slirp.telnetd -x" */
393 argv
[i
++] = "slirp.telnetd";
398 /* Change the string into argv[] */
400 while (*bptr
!= ' ' && *bptr
!= (char)0)
404 argv
[i
++] = strdup(curarg
);
408 execvp(argv
[0], argv
);
410 /* Ooops, failed, let's tell the user why */
414 sprintf(buff
, "Error: execvp of %s failed: %s\n",
415 argv
[0], strerror(errno
));
416 write(2, buff
, strlen(buff
)+1);
418 close(0); close(1); close(2); /* XXX */
427 * XXX this could block us...
428 * XXX Should set a timer here, and if accept() doesn't
429 * return after X seconds, declare it a failure
430 * The only reason this will block forever is if socket()
431 * of connect() fail in the child process
433 so
->s
= accept(s
, (struct sockaddr
*)&addr
, &addrlen
);
436 setsockopt(so
->s
,SOL_SOCKET
,SO_REUSEADDR
,(char *)&opt
,sizeof(int));
438 setsockopt(so
->s
,SOL_SOCKET
,SO_OOBINLINE
,(char *)&opt
,sizeof(int));
442 /* Append the telnet options now */
443 if (so
->so_m
!= 0 && do_pty
== 1) {
444 sbappend(so
, so
->so_m
);
460 bptr
= (char *)malloc(strlen(str
)+1);
473 #ifndef NO_UNIX_SOCKETS
474 struct sockaddr_un sock_un
;
476 struct sockaddr_in sock_in
;
480 if (slirp_socket_passwd
) {
481 s
= socket(AF_INET
, SOCK_STREAM
, 0);
484 sock_in
.sin_family
= AF_INET
;
485 sock_in
.sin_addr
.s_addr
= slirp_socket_addr
;
486 sock_in
.sin_port
= htons(slirp_socket_port
);
487 if (connect(s
, (struct sockaddr
*)&sock_in
, sizeof(sock_in
)) != 0)
488 slirp_exit(1); /* just exit...*/
489 sprintf(buff
, "kill %s:%d", slirp_socket_passwd
, slirp_socket_unit
);
490 write(s
, buff
, strlen(buff
)+1);
492 #ifndef NO_UNIX_SOCKETS
494 s
= socket(AF_UNIX
, SOCK_STREAM
, 0);
497 sock_un
.sun_family
= AF_UNIX
;
498 strcpy(sock_un
.sun_path
, socket_path
);
499 if (connect(s
, (struct sockaddr
*)&sock_un
,
500 sizeof(sock_un
.sun_family
) + sizeof(sock_un
.sun_path
)) != 0)
502 sprintf(buff
, "kill none:%d", slirp_socket_unit
);
503 write(s
, buff
, strlen(buff
)+1);
516 /* Don't need our data anymore */
517 /* XXX This makes SunOS barf */
521 for (i
= 255; i
>= 0; i
--)
524 signal(SIGQUIT
, slirp_exit
);
525 signal(SIGHUP
, snooze_hup
);
528 /* Wait for any signal */
531 /* Just in case ... */
544 /* Don't need our data anymore */
545 /* XXX This makes SunOS barf */
548 signal(SIGQUIT
, slirp_exit
);
549 signal(SIGHUP
, slirp_exit
);
550 signal(SIGINT
, slirp_exit
);
551 signal(SIGTERM
, slirp_exit
);
553 /* Fudge to get term_raw and term_restore to work */
554 if (NULL
== (ttyp
= tty_attach (0, slirp_tty
))) {
555 lprint ("Error: tty_attach failed in misc.c:relay()\r\n");
559 ttyp
->flags
|= TTY_CTTY
;
568 n
= select(s
+1, &readfds
, (fd_set
*)0, (fd_set
*)0, (struct timeval
*)0);
573 if (FD_ISSET(0, &readfds
)) {
574 n
= read(0, buf
, 8192);
577 n
= writen(s
, buf
, n
);
582 if (FD_ISSET(s
, &readfds
)) {
583 n
= read(s
, buf
, 8192);
586 n
= writen(0, buf
, n
);
592 /* Just in case.... */
597 int (*lprint_print
) _P((void *, const char *, va_list));
598 char *lprint_ptr
, *lprint_ptr2
, **lprint_arg
;
602 lprint(const char *format
, ...)
604 lprint(va_alist
) va_dcl
610 va_start(args
, format
);
614 format
= va_arg(args
, char *);
617 /* If we're printing to an sbuf, make sure there's enough room */
620 if ((lprint_ptr
- lprint_sb
->sb_wptr
) >=
621 (lprint_sb
->sb_datalen
- (strlen(format
) + 100))) {
622 int deltaw
= lprint_sb
->sb_wptr
- lprint_sb
->sb_data
;
623 int deltar
= lprint_sb
->sb_rptr
- lprint_sb
->sb_data
;
624 int deltap
= lprint_ptr
- lprint_sb
->sb_data
;
626 lprint_sb
->sb_data
= (char *)realloc(lprint_sb
->sb_data
,
627 lprint_sb
->sb_datalen
+ TCP_SNDSPACE
);
629 /* Adjust all values */
630 lprint_sb
->sb_wptr
= lprint_sb
->sb_data
+ deltaw
;
631 lprint_sb
->sb_rptr
= lprint_sb
->sb_data
+ deltar
;
632 lprint_ptr
= lprint_sb
->sb_data
+ deltap
;
634 lprint_sb
->sb_datalen
+= TCP_SNDSPACE
;
639 lprint_ptr
+= (*lprint_print
)(*lprint_arg
, format
, args
);
641 /* Check if they want output to be logged to file as well */
645 * otherwise you'll get ^M all over the file
647 int len
= strlen(format
);
650 bptr1
= bptr2
= strdup(format
);
654 memcpy(bptr1
, bptr1
+1, len
+1);
658 vfprintf(lfd
, bptr2
, args
);
669 u_int8_t tos
= 0, emu
= 0;
670 char buff1
[256], buff2
[256], buff4
[128];
675 if (sscanf(buff
, "%256s %256s", buff2
, buff1
) != 2) {
676 lprint("Error: Bad arguments\r\n");
680 if (sscanf(buff1
, "%d:%d", &lport
, &fport
) != 2) {
682 if (sscanf(buff1
, "%d", &fport
) != 1) {
683 lprint("Error: Bad first argument\r\n");
688 if (sscanf(buff2
, "%128[^:]:%128s", buff1
, buff3
) != 2) {
690 if (sscanf(buff2
, "%256s", buff1
) != 1) {
691 lprint("Error: Bad second argument\r\n");
697 if (strcmp(buff3
, "lowdelay") == 0)
698 tos
= IPTOS_LOWDELAY
;
699 else if (strcmp(buff3
, "throughput") == 0)
700 tos
= IPTOS_THROUGHPUT
;
702 lprint("Error: Expecting \"lowdelay\"/\"throughput\"\r\n");
707 if (strcmp(buff1
, "ftp") == 0)
709 else if (strcmp(buff1
, "irc") == 0)
711 else if (strcmp(buff1
, "none") == 0)
712 emu
= EMU_NONE
; /* ie: no emulation */
714 lprint("Error: Unknown service\r\n");
718 /* First, check that it isn't already emulated */
719 for (emup
= tcpemu
; emup
; emup
= emup
->next
) {
720 if (emup
->lport
== lport
&& emup
->fport
== fport
) {
721 lprint("Error: port already emulated\r\n");
727 emup
= (struct emu_t
*)malloc(sizeof (struct emu_t
));
728 emup
->lport
= (u_int16_t
)lport
;
729 emup
->fport
= (u_int16_t
)fport
;
735 /* And finally, mark all current sessions, if any, as being emulated */
736 for (so
= tcb
.so_next
; so
!= &tcb
; so
= so
->so_next
) {
737 if ((lport
&& lport
== ntohs(so
->so_lport
)) ||
738 (fport
&& fport
== ntohs(so
->so_fport
))) {
746 lprint("Adding emulation for %s to port %d/%d\r\n", buff1
, emup
->lport
, emup
->fport
);
755 * Some BSD-derived systems have a sprintf which returns char *
759 vsprintf_len(string
, format
, args
)
764 vsprintf(string
, format
, args
);
765 return strlen(string
);
770 sprintf_len(char *string
, const char *format
, ...)
772 sprintf_len(va_alist
) va_dcl
777 va_start(args
, format
);
782 string
= va_arg(args
, char *);
783 format
= va_arg(args
, char *);
785 vsprintf(string
, format
, args
);
786 return strlen(string
);
801 t
.tv_usec
= usec
* 1000;
803 select(0, &fdset
, &fdset
, &fdset
, &t
);
807 * Set fd blocking and non-blocking
817 ioctl(fd
, FIONBIO
, &opt
);
821 opt
= fcntl(fd
, F_GETFL
, 0);
823 fcntl(fd
, F_SETFL
, opt
);
834 ioctl(fd
, FIONBIO
, &opt
);
838 opt
= fcntl(fd
, F_GETFL
, 0);
840 fcntl(fd
, F_SETFL
, opt
);
850 rsh_exec(so
,ns
, user
, host
, args
)
862 DEBUG_CALL("rsh_exec");
863 DEBUG_ARG("so = %lx", (long)so
);
866 lprint("Error: pipe failed: %s\n", strerror(errno
));
869 /* #ifdef HAVE_SOCKETPAIR */
871 if (socketpair(PF_UNIX
,SOCK_STREAM
,0, fd0
) == -1) {
874 lprint("Error: openpty failed: %s\n", strerror(errno
));
878 if (openpty(&fd0
[0], &fd0
[1]) == -1) {
881 lprint("Error: openpty failed: %s\n", strerror(errno
));
888 lprint("Error: fork failed: %s\n", strerror(errno
));
899 /* Set the DISPLAY */
902 sprintf(buff
, "%s:%d.%d", inet_ntoa(our_addr
), x_port
, x_screen
);
903 setenv("DISPLAY", buff
, 1);
905 sprintf(buff
, "DISPLAY=%s:%d.%d", inet_ntoa(our_addr
), x_port
, x_screen
);
913 for (s
= 3; s
<= 255; s
++)
916 execlp("rsh","rsh","-l", user
, host
, args
, NULL
);
918 /* Ooops, failed, let's tell the user why */
920 sprintf(buff
, "Error: execlp of %s failed: %s\n",
921 "rsh", strerror(errno
));
922 write(2, buff
, strlen(buff
)+1);
923 close(0); close(1); close(2); /* XXX */