| 1 |
/* |
| 2 |
Copyright (C) 2002-2026 Free Software Foundation, Inc. |
| 3 |
|
| 4 |
This file is part of GNU Inetutils. |
| 5 |
|
| 6 |
GNU Inetutils is free software: you can redistribute it and/or modify |
| 7 |
it under the terms of the GNU General Public License as published by |
| 8 |
the Free Software Foundation, either version 3 of the License, or (at |
| 9 |
your option) any later version. |
| 10 |
|
| 11 |
GNU Inetutils is distributed in the hope that it will be useful, but |
| 12 |
WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 |
General Public License for more details. |
| 15 |
|
| 16 |
You should have received a copy of the GNU General Public License |
| 17 |
along with this program. If not, see `http://www.gnu.org/licenses/'. */ |
| 18 |
|
| 19 |
#include <config.h> |
| 20 |
|
| 21 |
#include "telnetd.h" |
| 22 |
#include <sys/wait.h> |
| 23 |
#include <pty.h> |
| 24 |
|
| 25 |
#ifdef AUTHENTICATION |
| 26 |
# include <libtelnet/auth.h> |
| 27 |
#endif |
| 28 |
#include <libinetutils.h> |
| 29 |
|
| 30 |
void |
| 31 |
setup_utmp (char *line, char *host) |
| 32 |
{ |
| 33 |
char *ut_id = utmp_ptsid (line, "tn"); |
| 34 |
utmp_init (line + sizeof (PATH_TTY_PFX) - 1, ".telnet", ut_id, host); |
| 35 |
} |
| 36 |
|
| 37 |
|
| 38 |
int |
| 39 |
startslave (char *host, int autologin, char *autoname) |
| 40 |
{ |
| 41 |
pid_t pid; |
| 42 |
int master; |
| 43 |
|
| 44 |
#ifdef AUTHENTICATION |
| 45 |
if (!autoname || !autoname[0]) |
| 46 |
autologin = 0; |
| 47 |
|
| 48 |
if (autologin < auth_level) |
| 49 |
{ |
| 50 |
fatal (net, "Authorization failed"); |
| 51 |
exit (EXIT_FAILURE); |
| 52 |
} |
| 53 |
#else /* !AUTHENTICATION */ |
| 54 |
(void) autoname; /* Silence warnings. */ |
| 55 |
#endif |
| 56 |
|
| 57 |
pid = forkpty (&master, line, NULL, NULL); |
| 58 |
if (pid < 0) |
| 59 |
{ |
| 60 |
if (errno == ENOENT) |
| 61 |
{ |
| 62 |
syslog (LOG_ERR, "Out of ptys"); |
| 63 |
fatal (net, "Out of ptys"); |
| 64 |
} |
| 65 |
else |
| 66 |
{ |
| 67 |
syslog (LOG_ERR, "forkpty: %m"); |
| 68 |
fatal (net, "Forkpty"); |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
if (pid == 0) |
| 73 |
{ |
| 74 |
/* Child */ |
| 75 |
if (net > 2) |
| 76 |
close (net); |
| 77 |
|
| 78 |
setup_utmp (line, host); |
| 79 |
start_login (host, autologin, line); |
| 80 |
} |
| 81 |
|
| 82 |
/* Master */ |
| 83 |
return master; |
| 84 |
} |
| 85 |
|
| 86 |
void |
| 87 |
start_login (char *host, int autologin, char *name) |
| 88 |
{ |
| 89 |
char *cmd; |
| 90 |
int argc; |
| 91 |
char **argv; |
| 92 |
|
| 93 |
(void) host; /* Silence warnings. Diagnostic use? */ |
| 94 |
(void) autologin; |
| 95 |
(void) name; |
| 96 |
|
| 97 |
/* Set the environment variable "LINEMODE" to indicate our linemode */ |
| 98 |
if (lmodetype == REAL_LINEMODE) |
| 99 |
setenv ("LINEMODE", "real", 1); |
| 100 |
else if (lmodetype == KLUDGE_LINEMODE || lmodetype == KLUDGE_OK) |
| 101 |
setenv ("LINEMODE", "kludge", 1); |
| 102 |
|
| 103 |
cmd = expand_line (login_invocation); |
| 104 |
if (!cmd) |
| 105 |
fatal (net, "can't expand login command line"); |
| 106 |
argcv_get (cmd, "", &argc, &argv); |
| 107 |
|
| 108 |
execv (argv[0], argv); |
| 109 |
syslog (LOG_ERR, "%s: %m\n", cmd); |
| 110 |
fatalperror (net, cmd); |
| 111 |
} |
| 112 |
|
| 113 |
/* SIG is generally naught every time the server itself |
| 114 |
* decides to close the connection out of an error condition. |
| 115 |
* In response to TELOPT_LOGOUT from the client, SIG is set |
| 116 |
* to SIGHUP, so we consider the exit as a success. In other |
| 117 |
* cases, when the forked client process is caught exiting, |
| 118 |
* then SIG will be SIGCHLD. Then we deliver the clients's |
| 119 |
* reported exit code. |
| 120 |
*/ |
| 121 |
void |
| 122 |
cleanup (int sig) |
| 123 |
{ |
| 124 |
int status = EXIT_FAILURE; |
| 125 |
char *p; |
| 126 |
|
| 127 |
if (sig == SIGCHLD) |
| 128 |
{ |
| 129 |
pid_t pid = waitpid ((pid_t) - 1, &status, WNOHANG); |
| 130 |
syslog (LOG_INFO, "child process %ld exited: %d", |
| 131 |
(long) pid, WEXITSTATUS (status)); |
| 132 |
|
| 133 |
status = WEXITSTATUS (status); |
| 134 |
} |
| 135 |
else if (sig == SIGHUP) |
| 136 |
status = EXIT_SUCCESS; /* Response to TELOPT_LOGOUT. */ |
| 137 |
|
| 138 |
p = line + sizeof (PATH_TTY_PFX) - 1; |
| 139 |
utmp_logout (p); |
| 140 |
lchmod (line, 0644); |
| 141 |
lchown (line, 0, 0); |
| 142 |
shutdown (net, 2); |
| 143 |
exit (status); |
| 144 |
} |