Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1 | /* |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2 | * Author: Tatu Ylonen <ylo@cs.hut.fi> |
| 3 | * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland |
| 4 | * All rights reserved |
| 5 | * Created: Fri Mar 17 17:09:28 1995 ylo |
| 6 | * This program is the ssh daemon. It listens for connections from clients, and |
| 7 | * performs authentication, executes use commands or shell, and forwards |
| 8 | * information to/from the application to the user client over an encrypted |
| 9 | * connection. This can also handle forwarding of X11, TCP/IP, and authentication |
| 10 | * agent connections. |
| 11 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 12 | |
| 13 | #include "includes.h" |
Damien Miller | f052aaf | 2000-01-22 19:47:21 +1100 | [diff] [blame] | 14 | RCSID("$OpenBSD: sshd.c,v 1.80 2000/01/20 15:19:22 markus Exp $"); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 15 | |
| 16 | #include "xmalloc.h" |
| 17 | #include "rsa.h" |
| 18 | #include "ssh.h" |
| 19 | #include "pty.h" |
| 20 | #include "packet.h" |
| 21 | #include "buffer.h" |
| 22 | #include "cipher.h" |
| 23 | #include "mpaux.h" |
| 24 | #include "servconf.h" |
| 25 | #include "uidswap.h" |
| 26 | #include "compat.h" |
| 27 | |
| 28 | #ifdef LIBWRAP |
| 29 | #include <tcpd.h> |
| 30 | #include <syslog.h> |
| 31 | int allow_severity = LOG_INFO; |
| 32 | int deny_severity = LOG_WARNING; |
| 33 | #endif /* LIBWRAP */ |
| 34 | |
| 35 | #ifndef O_NOCTTY |
| 36 | #define O_NOCTTY 0 |
| 37 | #endif |
| 38 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 39 | /* Local Xauthority file. */ |
Damien Miller | 5ce662a | 1999-11-11 17:57:39 +1100 | [diff] [blame] | 40 | static char *xauthfile = NULL; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 41 | |
| 42 | /* Server configuration options. */ |
| 43 | ServerOptions options; |
| 44 | |
| 45 | /* Name of the server configuration file. */ |
| 46 | char *config_file_name = SERVER_CONFIG_FILE; |
| 47 | |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 48 | /* |
| 49 | * Flag indicating whether IPv4 or IPv6. This can be set on the command line. |
| 50 | * Default value is AF_UNSPEC means both IPv4 and IPv6. |
| 51 | */ |
Damien Miller | 7d80e34 | 2000-01-19 14:36:49 +1100 | [diff] [blame] | 52 | #ifdef IPV4_DEFAULT |
| 53 | int IPv4or6 = AF_INET; |
| 54 | #else |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 55 | int IPv4or6 = AF_UNSPEC; |
Damien Miller | 7d80e34 | 2000-01-19 14:36:49 +1100 | [diff] [blame] | 56 | #endif |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 57 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 58 | /* |
| 59 | * Debug mode flag. This can be set on the command line. If debug |
| 60 | * mode is enabled, extra debugging output will be sent to the system |
| 61 | * log, the daemon will not go to background, and will exit after processing |
| 62 | * the first connection. |
| 63 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 64 | int debug_flag = 0; |
| 65 | |
| 66 | /* Flag indicating that the daemon is being started from inetd. */ |
| 67 | int inetd_flag = 0; |
| 68 | |
Damien Miller | 5ce662a | 1999-11-11 17:57:39 +1100 | [diff] [blame] | 69 | /* debug goes to stderr unless inetd_flag is set */ |
| 70 | int log_stderr = 0; |
| 71 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 72 | /* argv[0] without path. */ |
| 73 | char *av0; |
| 74 | |
| 75 | /* Saved arguments to main(). */ |
| 76 | char **saved_argv; |
| 77 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 78 | /* |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 79 | * The sockets that the server is listening; this is used in the SIGHUP |
| 80 | * signal handler. |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 81 | */ |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 82 | #define MAX_LISTEN_SOCKS 16 |
| 83 | int listen_socks[MAX_LISTEN_SOCKS]; |
| 84 | int num_listen_socks = 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 85 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 86 | /* |
| 87 | * the client's version string, passed by sshd2 in compat mode. if != NULL, |
| 88 | * sshd will skip the version-number exchange |
| 89 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 90 | char *client_version_string = NULL; |
| 91 | |
| 92 | /* Flags set in auth-rsa from authorized_keys flags. These are set in auth-rsa.c. */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 93 | int no_port_forwarding_flag = 0; |
| 94 | int no_agent_forwarding_flag = 0; |
| 95 | int no_x11_forwarding_flag = 0; |
| 96 | int no_pty_flag = 0; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 97 | |
| 98 | /* RSA authentication "command=" option. */ |
| 99 | char *forced_command = NULL; |
| 100 | |
| 101 | /* RSA authentication "environment=" options. */ |
| 102 | struct envstring *custom_environment = NULL; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 103 | |
| 104 | /* Session id for the current session. */ |
| 105 | unsigned char session_id[16]; |
| 106 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 107 | /* |
| 108 | * Any really sensitive data in the application is contained in this |
| 109 | * structure. The idea is that this structure could be locked into memory so |
| 110 | * that the pages do not get written into swap. However, there are some |
| 111 | * problems. The private key contains BIGNUMs, and we do not (in principle) |
| 112 | * have access to the internals of them, and locking just the structure is |
| 113 | * not very useful. Currently, memory locking is not implemented. |
| 114 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 115 | struct { |
| 116 | RSA *private_key; /* Private part of server key. */ |
| 117 | RSA *host_key; /* Private part of host key. */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 118 | } sensitive_data; |
| 119 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 120 | /* |
| 121 | * Flag indicating whether the current session key has been used. This flag |
| 122 | * is set whenever the key is used, and cleared when the key is regenerated. |
| 123 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 124 | int key_used = 0; |
| 125 | |
| 126 | /* This is set to true when SIGHUP is received. */ |
| 127 | int received_sighup = 0; |
| 128 | |
| 129 | /* Public side of the server key. This value is regenerated regularly with |
| 130 | the private key. */ |
| 131 | RSA *public_key; |
| 132 | |
| 133 | /* Prototypes for various functions defined later in this file. */ |
Damien Miller | 396691a | 2000-01-20 22:44:08 +1100 | [diff] [blame] | 134 | void do_ssh_kex(); |
| 135 | void do_authentication(); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 136 | void do_authloop(struct passwd * pw); |
Damien Miller | 2ccf661 | 1999-11-15 15:25:10 +1100 | [diff] [blame] | 137 | void do_fake_authloop(char *user); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 138 | void do_authenticated(struct passwd * pw); |
| 139 | void do_exec_pty(const char *command, int ptyfd, int ttyfd, |
| 140 | const char *ttyname, struct passwd * pw, const char *term, |
| 141 | const char *display, const char *auth_proto, |
| 142 | const char *auth_data); |
| 143 | void do_exec_no_pty(const char *command, struct passwd * pw, |
| 144 | const char *display, const char *auth_proto, |
| 145 | const char *auth_data); |
| 146 | void do_child(const char *command, struct passwd * pw, const char *term, |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 147 | const char *display, const char *auth_proto, |
| 148 | const char *auth_data, const char *ttyname); |
Damien Miller | 2ccf661 | 1999-11-15 15:25:10 +1100 | [diff] [blame] | 149 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 150 | /* |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 151 | * Close all listening sockets |
| 152 | */ |
| 153 | void |
| 154 | close_listen_socks(void) |
| 155 | { |
| 156 | int i; |
| 157 | for (i = 0; i < num_listen_socks; i++) |
| 158 | close(listen_socks[i]); |
| 159 | num_listen_socks = -1; |
| 160 | } |
| 161 | |
| 162 | /* |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 163 | * Signal handler for SIGHUP. Sshd execs itself when it receives SIGHUP; |
| 164 | * the effect is to reread the configuration file (and to regenerate |
| 165 | * the server key). |
| 166 | */ |
| 167 | void |
| 168 | sighup_handler(int sig) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 169 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 170 | received_sighup = 1; |
| 171 | signal(SIGHUP, sighup_handler); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 172 | } |
| 173 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 174 | /* |
| 175 | * Called from the main program after receiving SIGHUP. |
| 176 | * Restarts the server. |
| 177 | */ |
| 178 | void |
| 179 | sighup_restart() |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 180 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 181 | log("Received SIGHUP; restarting."); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 182 | close_listen_socks(); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 183 | execv(saved_argv[0], saved_argv); |
| 184 | log("RESTART FAILED: av0='%s', error: %s.", av0, strerror(errno)); |
| 185 | exit(1); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 186 | } |
| 187 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 188 | /* |
| 189 | * Generic signal handler for terminating signals in the master daemon. |
| 190 | * These close the listen socket; not closing it seems to cause "Address |
| 191 | * already in use" problems on some machines, which is inconvenient. |
| 192 | */ |
| 193 | void |
| 194 | sigterm_handler(int sig) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 195 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 196 | log("Received signal %d; terminating.", sig); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 197 | close_listen_socks(); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 198 | exit(255); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 199 | } |
| 200 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 201 | /* |
| 202 | * SIGCHLD handler. This is called whenever a child dies. This will then |
| 203 | * reap any zombies left by exited c. |
| 204 | */ |
| 205 | void |
| 206 | main_sigchld_handler(int sig) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 207 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 208 | int save_errno = errno; |
| 209 | int status; |
Damien Miller | 431f66b | 1999-11-21 18:31:57 +1100 | [diff] [blame] | 210 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 211 | while (waitpid(-1, &status, WNOHANG) > 0) |
| 212 | ; |
Damien Miller | 431f66b | 1999-11-21 18:31:57 +1100 | [diff] [blame] | 213 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 214 | signal(SIGCHLD, main_sigchld_handler); |
| 215 | errno = save_errno; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 216 | } |
| 217 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 218 | /* |
| 219 | * Signal handler for the alarm after the login grace period has expired. |
| 220 | */ |
| 221 | void |
| 222 | grace_alarm_handler(int sig) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 223 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 224 | /* Close the connection. */ |
| 225 | packet_close(); |
| 226 | |
| 227 | /* Log error and exit. */ |
| 228 | fatal("Timeout before authentication for %s.", get_remote_ipaddr()); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 229 | } |
| 230 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 231 | /* |
| 232 | * convert ssh auth msg type into description |
| 233 | */ |
| 234 | char * |
| 235 | get_authname(int type) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 236 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 237 | switch (type) { |
| 238 | case SSH_CMSG_AUTH_PASSWORD: |
| 239 | return "password"; |
| 240 | case SSH_CMSG_AUTH_RSA: |
| 241 | return "rsa"; |
| 242 | case SSH_CMSG_AUTH_RHOSTS_RSA: |
| 243 | return "rhosts-rsa"; |
| 244 | case SSH_CMSG_AUTH_RHOSTS: |
| 245 | return "rhosts"; |
| 246 | #ifdef KRB4 |
| 247 | case SSH_CMSG_AUTH_KERBEROS: |
| 248 | return "kerberos"; |
| 249 | #endif |
| 250 | #ifdef SKEY |
| 251 | case SSH_CMSG_AUTH_TIS_RESPONSE: |
| 252 | return "s/key"; |
| 253 | #endif |
| 254 | } |
| 255 | fatal("get_authname: unknown auth %d: internal error", type); |
| 256 | return NULL; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 257 | } |
| 258 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 259 | /* |
| 260 | * Signal handler for the key regeneration alarm. Note that this |
| 261 | * alarm only occurs in the daemon waiting for connections, and it does not |
| 262 | * do anything with the private key or random state before forking. |
| 263 | * Thus there should be no concurrency control/asynchronous execution |
| 264 | * problems. |
| 265 | */ |
| 266 | void |
| 267 | key_regeneration_alarm(int sig) |
| 268 | { |
| 269 | int save_errno = errno; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 270 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 271 | /* Check if we should generate a new key. */ |
| 272 | if (key_used) { |
| 273 | /* This should really be done in the background. */ |
| 274 | log("Generating new %d bit RSA key.", options.server_key_bits); |
| 275 | |
| 276 | if (sensitive_data.private_key != NULL) |
| 277 | RSA_free(sensitive_data.private_key); |
| 278 | sensitive_data.private_key = RSA_new(); |
| 279 | |
| 280 | if (public_key != NULL) |
| 281 | RSA_free(public_key); |
| 282 | public_key = RSA_new(); |
| 283 | |
| 284 | rsa_generate_key(sensitive_data.private_key, public_key, |
| 285 | options.server_key_bits); |
| 286 | arc4random_stir(); |
| 287 | key_used = 0; |
| 288 | log("RSA key generation complete."); |
| 289 | } |
| 290 | /* Reschedule the alarm. */ |
| 291 | signal(SIGALRM, key_regeneration_alarm); |
| 292 | alarm(options.key_regeneration_time); |
| 293 | errno = save_errno; |
| 294 | } |
| 295 | |
| 296 | /* |
| 297 | * Main program for the daemon. |
| 298 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 299 | int |
| 300 | main(int ac, char **av) |
| 301 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 302 | extern char *optarg; |
| 303 | extern int optind; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 304 | int opt, sock_in = 0, sock_out = 0, newsock, i, fdsetsz, pid, on = 1; |
| 305 | socklen_t fromlen; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 306 | int remote_major, remote_minor; |
| 307 | int silentrsa = 0; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 308 | fd_set *fdset; |
| 309 | struct sockaddr_storage from; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 310 | char buf[100]; /* Must not be larger than remote_version. */ |
| 311 | char remote_version[100]; /* Must be at least as big as buf. */ |
| 312 | const char *remote_ip; |
| 313 | int remote_port; |
| 314 | char *comment; |
| 315 | FILE *f; |
| 316 | struct linger linger; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 317 | struct addrinfo *ai; |
| 318 | char ntop[NI_MAXHOST], strport[NI_MAXSERV]; |
| 319 | int listen_sock, maxfd; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 320 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 321 | /* Save argv[0]. */ |
| 322 | saved_argv = av; |
| 323 | if (strchr(av[0], '/')) |
| 324 | av0 = strrchr(av[0], '/') + 1; |
| 325 | else |
| 326 | av0 = av[0]; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 327 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 328 | /* Initialize configuration options to their default values. */ |
| 329 | initialize_server_options(&options); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 330 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 331 | /* Parse command-line arguments. */ |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 332 | while ((opt = getopt(ac, av, "f:p:b:k:h:g:V:diqQ46")) != EOF) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 333 | switch (opt) { |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 334 | case '4': |
| 335 | IPv4or6 = AF_INET; |
| 336 | break; |
| 337 | case '6': |
| 338 | IPv4or6 = AF_INET6; |
| 339 | break; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 340 | case 'f': |
| 341 | config_file_name = optarg; |
| 342 | break; |
| 343 | case 'd': |
| 344 | debug_flag = 1; |
| 345 | options.log_level = SYSLOG_LEVEL_DEBUG; |
| 346 | break; |
| 347 | case 'i': |
| 348 | inetd_flag = 1; |
| 349 | break; |
| 350 | case 'Q': |
| 351 | silentrsa = 1; |
| 352 | break; |
| 353 | case 'q': |
| 354 | options.log_level = SYSLOG_LEVEL_QUIET; |
| 355 | break; |
| 356 | case 'b': |
| 357 | options.server_key_bits = atoi(optarg); |
| 358 | break; |
| 359 | case 'p': |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 360 | options.ports_from_cmdline = 1; |
| 361 | if (options.num_ports >= MAX_PORTS) |
| 362 | fatal("too many ports.\n"); |
| 363 | options.ports[options.num_ports++] = atoi(optarg); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 364 | break; |
| 365 | case 'g': |
| 366 | options.login_grace_time = atoi(optarg); |
| 367 | break; |
| 368 | case 'k': |
| 369 | options.key_regeneration_time = atoi(optarg); |
| 370 | break; |
| 371 | case 'h': |
| 372 | options.host_key_file = optarg; |
| 373 | break; |
| 374 | case 'V': |
| 375 | client_version_string = optarg; |
| 376 | /* only makes sense with inetd_flag, i.e. no listen() */ |
| 377 | inetd_flag = 1; |
| 378 | break; |
| 379 | case '?': |
| 380 | default: |
| 381 | fprintf(stderr, "sshd version %s\n", SSH_VERSION); |
Damien Miller | d00d161 | 1999-12-29 10:17:09 +1100 | [diff] [blame] | 382 | #ifdef RSAREF |
| 383 | fprintf(stderr, "Compiled with RSAref.\n"); |
| 384 | #endif |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 385 | fprintf(stderr, "Usage: %s [options]\n", av0); |
| 386 | fprintf(stderr, "Options:\n"); |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 387 | fprintf(stderr, " -f file Configuration file (default %s)\n", SERVER_CONFIG_FILE); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 388 | fprintf(stderr, " -d Debugging mode\n"); |
| 389 | fprintf(stderr, " -i Started from inetd\n"); |
| 390 | fprintf(stderr, " -q Quiet (no logging)\n"); |
| 391 | fprintf(stderr, " -p port Listen on the specified port (default: 22)\n"); |
| 392 | fprintf(stderr, " -k seconds Regenerate server key every this many seconds (default: 3600)\n"); |
| 393 | fprintf(stderr, " -g seconds Grace period for authentication (default: 300)\n"); |
| 394 | fprintf(stderr, " -b bits Size of server RSA key (default: 768 bits)\n"); |
| 395 | fprintf(stderr, " -h file File from which to read host key (default: %s)\n", |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 396 | HOST_KEY_FILE); |
| 397 | fprintf(stderr, " -4 Use IPv4 only\n"); |
| 398 | fprintf(stderr, " -6 Use IPv6 only\n"); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 399 | exit(1); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 400 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 401 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 402 | |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 403 | /* |
| 404 | * Force logging to stderr until we have loaded the private host |
| 405 | * key (unless started from inetd) |
| 406 | */ |
| 407 | log_init(av0, |
| 408 | options.log_level == -1 ? SYSLOG_LEVEL_INFO : options.log_level, |
| 409 | options.log_facility == -1 ? SYSLOG_FACILITY_AUTH : options.log_facility, |
| 410 | !inetd_flag); |
| 411 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 412 | /* check if RSA support exists */ |
| 413 | if (rsa_alive() == 0) { |
| 414 | if (silentrsa == 0) |
| 415 | printf("sshd: no RSA support in libssl and libcrypto -- exiting. See ssl(8)\n"); |
| 416 | log("no RSA support in libssl and libcrypto -- exiting. See ssl(8)"); |
| 417 | exit(1); |
| 418 | } |
| 419 | /* Read server configuration options from the configuration file. */ |
| 420 | read_server_config(&options, config_file_name); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 421 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 422 | /* Fill in default values for those options not explicitly set. */ |
| 423 | fill_default_server_options(&options); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 424 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 425 | /* Check certain values for sanity. */ |
| 426 | if (options.server_key_bits < 512 || |
| 427 | options.server_key_bits > 32768) { |
| 428 | fprintf(stderr, "Bad server key size.\n"); |
| 429 | exit(1); |
| 430 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 431 | /* Check that there are no remaining arguments. */ |
| 432 | if (optind < ac) { |
| 433 | fprintf(stderr, "Extra argument %s.\n", av[optind]); |
| 434 | exit(1); |
| 435 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 436 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 437 | debug("sshd version %.100s", SSH_VERSION); |
Damien Miller | 2ccf661 | 1999-11-15 15:25:10 +1100 | [diff] [blame] | 438 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 439 | sensitive_data.host_key = RSA_new(); |
| 440 | errno = 0; |
| 441 | /* Load the host key. It must have empty passphrase. */ |
| 442 | if (!load_private_key(options.host_key_file, "", |
| 443 | sensitive_data.host_key, &comment)) { |
| 444 | error("Could not load host key: %.200s: %.100s", |
| 445 | options.host_key_file, strerror(errno)); |
| 446 | exit(1); |
| 447 | } |
| 448 | xfree(comment); |
| 449 | |
| 450 | /* Initialize the log (it is reinitialized below in case we |
| 451 | forked). */ |
| 452 | if (debug_flag && !inetd_flag) |
| 453 | log_stderr = 1; |
| 454 | log_init(av0, options.log_level, options.log_facility, log_stderr); |
| 455 | |
| 456 | /* If not in debugging mode, and not started from inetd, |
| 457 | disconnect from the controlling terminal, and fork. The |
| 458 | original process exits. */ |
| 459 | if (!debug_flag && !inetd_flag) { |
| 460 | #ifdef TIOCNOTTY |
| 461 | int fd; |
| 462 | #endif /* TIOCNOTTY */ |
| 463 | if (daemon(0, 0) < 0) |
| 464 | fatal("daemon() failed: %.200s", strerror(errno)); |
| 465 | |
| 466 | /* Disconnect from the controlling tty. */ |
| 467 | #ifdef TIOCNOTTY |
| 468 | fd = open("/dev/tty", O_RDWR | O_NOCTTY); |
| 469 | if (fd >= 0) { |
| 470 | (void) ioctl(fd, TIOCNOTTY, NULL); |
| 471 | close(fd); |
| 472 | } |
| 473 | #endif /* TIOCNOTTY */ |
| 474 | } |
| 475 | /* Reinitialize the log (because of the fork above). */ |
| 476 | log_init(av0, options.log_level, options.log_facility, log_stderr); |
| 477 | |
| 478 | /* Check that server and host key lengths differ sufficiently. |
| 479 | This is necessary to make double encryption work with rsaref. |
| 480 | Oh, I hate software patents. I dont know if this can go? Niels */ |
| 481 | if (options.server_key_bits > |
| 482 | BN_num_bits(sensitive_data.host_key->n) - SSH_KEY_BITS_RESERVED && |
| 483 | options.server_key_bits < |
| 484 | BN_num_bits(sensitive_data.host_key->n) + SSH_KEY_BITS_RESERVED) { |
| 485 | options.server_key_bits = |
| 486 | BN_num_bits(sensitive_data.host_key->n) + SSH_KEY_BITS_RESERVED; |
| 487 | debug("Forcing server key to %d bits to make it differ from host key.", |
| 488 | options.server_key_bits); |
| 489 | } |
| 490 | /* Do not display messages to stdout in RSA code. */ |
| 491 | rsa_set_verbose(0); |
| 492 | |
| 493 | /* Initialize the random number generator. */ |
| 494 | arc4random_stir(); |
| 495 | |
| 496 | /* Chdir to the root directory so that the current disk can be |
| 497 | unmounted if desired. */ |
| 498 | chdir("/"); |
| 499 | |
| 500 | /* Close connection cleanly after attack. */ |
| 501 | cipher_attack_detected = packet_disconnect; |
| 502 | |
| 503 | /* Start listening for a socket, unless started from inetd. */ |
| 504 | if (inetd_flag) { |
| 505 | int s1, s2; |
| 506 | s1 = dup(0); /* Make sure descriptors 0, 1, and 2 are in use. */ |
| 507 | s2 = dup(s1); |
| 508 | sock_in = dup(0); |
| 509 | sock_out = dup(1); |
| 510 | /* We intentionally do not close the descriptors 0, 1, and 2 |
| 511 | as our code for setting the descriptors won\'t work |
| 512 | if ttyfd happens to be one of those. */ |
| 513 | debug("inetd sockets after dupping: %d, %d", sock_in, sock_out); |
| 514 | |
| 515 | public_key = RSA_new(); |
| 516 | sensitive_data.private_key = RSA_new(); |
| 517 | |
| 518 | log("Generating %d bit RSA key.", options.server_key_bits); |
| 519 | rsa_generate_key(sensitive_data.private_key, public_key, |
| 520 | options.server_key_bits); |
| 521 | arc4random_stir(); |
| 522 | log("RSA key generation complete."); |
| 523 | } else { |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 524 | for (ai = options.listen_addrs; ai; ai = ai->ai_next) { |
| 525 | if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6) |
| 526 | continue; |
| 527 | if (num_listen_socks >= MAX_LISTEN_SOCKS) |
| 528 | fatal("Too many listen sockets. " |
| 529 | "Enlarge MAX_LISTEN_SOCKS"); |
| 530 | if (getnameinfo(ai->ai_addr, ai->ai_addrlen, |
| 531 | ntop, sizeof(ntop), strport, sizeof(strport), |
| 532 | NI_NUMERICHOST|NI_NUMERICSERV) != 0) { |
| 533 | error("getnameinfo failed"); |
| 534 | continue; |
| 535 | } |
| 536 | /* Create socket for listening. */ |
| 537 | listen_sock = socket(ai->ai_family, SOCK_STREAM, 0); |
| 538 | if (listen_sock < 0) { |
| 539 | /* kernel may not support ipv6 */ |
| 540 | verbose("socket: %.100s", strerror(errno)); |
| 541 | continue; |
| 542 | } |
| 543 | if (fcntl(listen_sock, F_SETFL, O_NONBLOCK) < 0) { |
| 544 | error("listen_sock O_NONBLOCK: %s", strerror(errno)); |
| 545 | close(listen_sock); |
| 546 | continue; |
| 547 | } |
| 548 | /* |
| 549 | * Set socket options. We try to make the port |
| 550 | * reusable and have it close as fast as possible |
| 551 | * without waiting in unnecessary wait states on |
| 552 | * close. |
| 553 | */ |
| 554 | setsockopt(listen_sock, SOL_SOCKET, SO_REUSEADDR, |
| 555 | (void *) &on, sizeof(on)); |
| 556 | linger.l_onoff = 1; |
| 557 | linger.l_linger = 5; |
| 558 | setsockopt(listen_sock, SOL_SOCKET, SO_LINGER, |
| 559 | (void *) &linger, sizeof(linger)); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 560 | |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 561 | debug("Bind to port %s on %s.", strport, ntop); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 562 | |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 563 | /* Bind the socket to the desired port. */ |
| 564 | if (bind(listen_sock, ai->ai_addr, ai->ai_addrlen) < 0) { |
| 565 | error("Bind to port %s on %s failed: %.200s.", |
| 566 | strport, ntop, strerror(errno)); |
| 567 | close(listen_sock); |
| 568 | continue; |
| 569 | } |
| 570 | listen_socks[num_listen_socks] = listen_sock; |
| 571 | num_listen_socks++; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 572 | |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 573 | /* Start listening on the port. */ |
| 574 | log("Server listening on %s port %s.", ntop, strport); |
| 575 | if (listen(listen_sock, 5) < 0) |
| 576 | fatal("listen: %.100s", strerror(errno)); |
| 577 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 578 | } |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 579 | freeaddrinfo(options.listen_addrs); |
| 580 | |
| 581 | if (!num_listen_socks) |
| 582 | fatal("Cannot bind any address."); |
| 583 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 584 | if (!debug_flag) { |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 585 | /* |
| 586 | * Record our pid in /etc/sshd_pid to make it easier |
| 587 | * to kill the correct sshd. We don\'t want to do |
| 588 | * this before the bind above because the bind will |
| 589 | * fail if there already is a daemon, and this will |
| 590 | * overwrite any old pid in the file. |
| 591 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 592 | f = fopen(SSH_DAEMON_PID_FILE, "w"); |
| 593 | if (f) { |
| 594 | fprintf(f, "%u\n", (unsigned int) getpid()); |
| 595 | fclose(f); |
| 596 | } |
| 597 | } |
| 598 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 599 | public_key = RSA_new(); |
| 600 | sensitive_data.private_key = RSA_new(); |
| 601 | |
| 602 | log("Generating %d bit RSA key.", options.server_key_bits); |
| 603 | rsa_generate_key(sensitive_data.private_key, public_key, |
| 604 | options.server_key_bits); |
| 605 | arc4random_stir(); |
| 606 | log("RSA key generation complete."); |
| 607 | |
| 608 | /* Schedule server key regeneration alarm. */ |
| 609 | signal(SIGALRM, key_regeneration_alarm); |
| 610 | alarm(options.key_regeneration_time); |
| 611 | |
| 612 | /* Arrange to restart on SIGHUP. The handler needs listen_sock. */ |
| 613 | signal(SIGHUP, sighup_handler); |
| 614 | signal(SIGTERM, sigterm_handler); |
| 615 | signal(SIGQUIT, sigterm_handler); |
| 616 | |
| 617 | /* Arrange SIGCHLD to be caught. */ |
| 618 | signal(SIGCHLD, main_sigchld_handler); |
| 619 | |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 620 | /* setup fd set for listen */ |
| 621 | maxfd = 0; |
| 622 | for (i = 0; i < num_listen_socks; i++) |
| 623 | if (listen_socks[i] > maxfd) |
| 624 | maxfd = listen_socks[i]; |
| 625 | fdsetsz = howmany(maxfd, NFDBITS) * sizeof(fd_mask); |
| 626 | fdset = (fd_set *)xmalloc(fdsetsz); |
| 627 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 628 | /* |
| 629 | * Stay listening for connections until the system crashes or |
| 630 | * the daemon is killed with a signal. |
| 631 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 632 | for (;;) { |
| 633 | if (received_sighup) |
| 634 | sighup_restart(); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 635 | /* Wait in select until there is a connection. */ |
| 636 | memset(fdset, 0, fdsetsz); |
| 637 | for (i = 0; i < num_listen_socks; i++) |
| 638 | FD_SET(listen_socks[i], fdset); |
| 639 | if (select(maxfd + 1, fdset, NULL, NULL, NULL) < 0) { |
| 640 | if (errno != EINTR) |
| 641 | error("select: %.100s", strerror(errno)); |
Damien Miller | 50945fa | 1999-12-09 10:31:37 +1100 | [diff] [blame] | 642 | continue; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 643 | } |
| 644 | for (i = 0; i < num_listen_socks; i++) { |
| 645 | if (!FD_ISSET(listen_socks[i], fdset)) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 646 | continue; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 647 | fromlen = sizeof(from); |
| 648 | newsock = accept(listen_socks[i], (struct sockaddr *)&from, |
| 649 | &fromlen); |
| 650 | if (newsock < 0) { |
| 651 | if (errno != EINTR && errno != EWOULDBLOCK) |
| 652 | error("accept: %.100s", strerror(errno)); |
| 653 | continue; |
| 654 | } |
| 655 | if (fcntl(newsock, F_SETFL, 0) < 0) { |
| 656 | error("newsock del O_NONBLOCK: %s", strerror(errno)); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 657 | continue; |
| 658 | } |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 659 | /* |
| 660 | * Got connection. Fork a child to handle it, unless |
| 661 | * we are in debugging mode. |
| 662 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 663 | if (debug_flag) { |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 664 | /* |
| 665 | * In debugging mode. Close the listening |
| 666 | * socket, and start processing the |
| 667 | * connection without forking. |
| 668 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 669 | debug("Server will not fork when running in debugging mode."); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 670 | close_listen_socks(); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 671 | sock_in = newsock; |
| 672 | sock_out = newsock; |
| 673 | pid = getpid(); |
| 674 | break; |
| 675 | } else { |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 676 | /* |
| 677 | * Normal production daemon. Fork, and have |
| 678 | * the child process the connection. The |
| 679 | * parent continues listening. |
| 680 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 681 | if ((pid = fork()) == 0) { |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 682 | /* |
| 683 | * Child. Close the listening socket, and start using the |
| 684 | * accepted socket. Reinitialize logging (since our pid has |
| 685 | * changed). We break out of the loop to handle the connection. |
| 686 | */ |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 687 | close_listen_socks(); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 688 | sock_in = newsock; |
| 689 | sock_out = newsock; |
| 690 | log_init(av0, options.log_level, options.log_facility, log_stderr); |
| 691 | break; |
| 692 | } |
| 693 | } |
| 694 | |
| 695 | /* Parent. Stay in the loop. */ |
| 696 | if (pid < 0) |
| 697 | error("fork: %.100s", strerror(errno)); |
| 698 | else |
| 699 | debug("Forked child %d.", pid); |
| 700 | |
| 701 | /* Mark that the key has been used (it was "given" to the child). */ |
| 702 | key_used = 1; |
| 703 | |
| 704 | arc4random_stir(); |
| 705 | |
| 706 | /* Close the new socket (the child is now taking care of it). */ |
| 707 | close(newsock); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 708 | } /* for (i = 0; i < num_listen_socks; i++) */ |
| 709 | /* child process check (or debug mode) */ |
| 710 | if (num_listen_socks < 0) |
| 711 | break; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 712 | } |
| 713 | } |
| 714 | |
| 715 | /* This is the child processing a new connection. */ |
| 716 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 717 | /* |
| 718 | * Disable the key regeneration alarm. We will not regenerate the |
| 719 | * key since we are no longer in a position to give it to anyone. We |
| 720 | * will not restart on SIGHUP since it no longer makes sense. |
| 721 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 722 | alarm(0); |
| 723 | signal(SIGALRM, SIG_DFL); |
| 724 | signal(SIGHUP, SIG_DFL); |
| 725 | signal(SIGTERM, SIG_DFL); |
| 726 | signal(SIGQUIT, SIG_DFL); |
| 727 | signal(SIGCHLD, SIG_DFL); |
| 728 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 729 | /* |
| 730 | * Set socket options for the connection. We want the socket to |
| 731 | * close as fast as possible without waiting for anything. If the |
| 732 | * connection is not a socket, these will do nothing. |
| 733 | */ |
| 734 | /* setsockopt(sock_in, SOL_SOCKET, SO_REUSEADDR, (void *)&on, sizeof(on)); */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 735 | linger.l_onoff = 1; |
| 736 | linger.l_linger = 5; |
| 737 | setsockopt(sock_in, SOL_SOCKET, SO_LINGER, (void *) &linger, sizeof(linger)); |
| 738 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 739 | /* |
| 740 | * Register our connection. This turns encryption off because we do |
| 741 | * not have a key. |
| 742 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 743 | packet_set_connection(sock_in, sock_out); |
| 744 | |
| 745 | remote_port = get_remote_port(); |
| 746 | remote_ip = get_remote_ipaddr(); |
| 747 | |
| 748 | /* Check whether logins are denied from this host. */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 749 | #ifdef LIBWRAP |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 750 | /* XXX LIBWRAP noes not know about IPv6 */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 751 | { |
| 752 | struct request_info req; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 753 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 754 | request_init(&req, RQ_DAEMON, av0, RQ_FILE, sock_in, NULL); |
| 755 | fromhost(&req); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 756 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 757 | if (!hosts_access(&req)) { |
| 758 | close(sock_in); |
| 759 | close(sock_out); |
| 760 | refuse(&req); |
| 761 | } |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 762 | /*XXX IPv6 verbose("Connection from %.500s port %d", eval_client(&req), remote_port); */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 763 | } |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 764 | #endif /* LIBWRAP */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 765 | /* Log the connection. */ |
| 766 | verbose("Connection from %.500s port %d", remote_ip, remote_port); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 767 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 768 | /* |
| 769 | * We don\'t want to listen forever unless the other side |
| 770 | * successfully authenticates itself. So we set up an alarm which is |
| 771 | * cleared after successful authentication. A limit of zero |
| 772 | * indicates no limit. Note that we don\'t set the alarm in debugging |
| 773 | * mode; it is just annoying to have the server exit just when you |
| 774 | * are about to discover the bug. |
| 775 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 776 | signal(SIGALRM, grace_alarm_handler); |
| 777 | if (!debug_flag) |
| 778 | alarm(options.login_grace_time); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 779 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 780 | if (client_version_string != NULL) { |
| 781 | /* we are exec'ed by sshd2, so skip exchange of protocol version */ |
| 782 | strlcpy(buf, client_version_string, sizeof(buf)); |
| 783 | } else { |
| 784 | /* Send our protocol version identification. */ |
| 785 | snprintf(buf, sizeof buf, "SSH-%d.%d-%.100s\n", |
| 786 | PROTOCOL_MAJOR, PROTOCOL_MINOR, SSH_VERSION); |
Damien Miller | f052aaf | 2000-01-22 19:47:21 +1100 | [diff] [blame] | 787 | if (atomicio(write, sock_out, buf, strlen(buf)) != strlen(buf)) { |
| 788 | log("Could not write ident string to %s.", remote_ip); |
| 789 | fatal_cleanup(); |
| 790 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 791 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 792 | /* Read other side\'s version identification. */ |
| 793 | for (i = 0; i < sizeof(buf) - 1; i++) { |
Damien Miller | f052aaf | 2000-01-22 19:47:21 +1100 | [diff] [blame] | 794 | if (read(sock_in, &buf[i], 1) != 1) { |
| 795 | log("Did not receive ident string from %s.", remote_ip); |
| 796 | fatal_cleanup(); |
| 797 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 798 | if (buf[i] == '\r') { |
| 799 | buf[i] = '\n'; |
| 800 | buf[i + 1] = 0; |
| 801 | break; |
| 802 | } |
| 803 | if (buf[i] == '\n') { |
| 804 | /* buf[i] == '\n' */ |
| 805 | buf[i + 1] = 0; |
| 806 | break; |
| 807 | } |
| 808 | } |
| 809 | buf[sizeof(buf) - 1] = 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 810 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 811 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 812 | /* |
| 813 | * Check that the versions match. In future this might accept |
| 814 | * several versions and set appropriate flags to handle them. |
| 815 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 816 | if (sscanf(buf, "SSH-%d.%d-%[^\n]\n", &remote_major, &remote_minor, |
Damien Miller | 037a0dc | 1999-12-07 15:38:31 +1100 | [diff] [blame] | 817 | remote_version) != 3) { |
| 818 | char *s = "Protocol mismatch.\n"; |
| 819 | |
| 820 | (void) atomicio(write, sock_out, s, strlen(s)); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 821 | close(sock_in); |
| 822 | close(sock_out); |
Damien Miller | f052aaf | 2000-01-22 19:47:21 +1100 | [diff] [blame] | 823 | log("Bad protocol version identification '%.100s' from %s", |
| 824 | buf, remote_ip); |
| 825 | fatal_cleanup(); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 826 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 827 | debug("Client protocol version %d.%d; client software version %.100s", |
| 828 | remote_major, remote_minor, remote_version); |
| 829 | if (remote_major != PROTOCOL_MAJOR) { |
Damien Miller | 037a0dc | 1999-12-07 15:38:31 +1100 | [diff] [blame] | 830 | char *s = "Protocol major versions differ.\n"; |
| 831 | |
| 832 | (void) atomicio(write, sock_out, s, strlen(s)); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 833 | close(sock_in); |
| 834 | close(sock_out); |
Damien Miller | f052aaf | 2000-01-22 19:47:21 +1100 | [diff] [blame] | 835 | log("Protocol major versions differ for %s: %d vs. %d", |
| 836 | remote_ip, PROTOCOL_MAJOR, remote_major); |
| 837 | fatal_cleanup(); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 838 | } |
| 839 | /* Check that the client has sufficiently high software version. */ |
| 840 | if (remote_major == 1 && remote_minor < 3) |
| 841 | packet_disconnect("Your ssh version is too old and is no longer supported. Please install a newer version."); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 842 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 843 | if (remote_major == 1 && remote_minor == 3) { |
Damien Miller | 396691a | 2000-01-20 22:44:08 +1100 | [diff] [blame] | 844 | /* note that this disables agent-forwarding */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 845 | enable_compat13(); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 846 | } |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 847 | /* |
| 848 | * Check that the connection comes from a privileged port. Rhosts- |
| 849 | * and Rhosts-RSA-Authentication only make sense from priviledged |
| 850 | * programs. Of course, if the intruder has root access on his local |
| 851 | * machine, he can connect from any port. So do not use these |
| 852 | * authentication methods from machines that you do not trust. |
| 853 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 854 | if (remote_port >= IPPORT_RESERVED || |
| 855 | remote_port < IPPORT_RESERVED / 2) { |
| 856 | options.rhosts_authentication = 0; |
| 857 | options.rhosts_rsa_authentication = 0; |
| 858 | } |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 859 | #ifdef KRB4 |
| 860 | if (!packet_connection_is_ipv4() && |
| 861 | options.kerberos_authentication) { |
| 862 | debug("Kerberos Authentication disabled, only available for IPv4."); |
| 863 | options.kerberos_authentication = 0; |
| 864 | } |
| 865 | #endif /* KRB4 */ |
| 866 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 867 | packet_set_nonblocking(); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 868 | |
Damien Miller | 396691a | 2000-01-20 22:44:08 +1100 | [diff] [blame] | 869 | /* perform the key exchange */ |
| 870 | do_ssh_kex(); |
| 871 | |
| 872 | /* authenticate user and start session */ |
| 873 | do_authentication(); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 874 | |
| 875 | #ifdef KRB4 |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 876 | /* Cleanup user's ticket cache file. */ |
| 877 | if (options.kerberos_ticket_cleanup) |
| 878 | (void) dest_tkt(); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 879 | #endif /* KRB4 */ |
| 880 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 881 | /* Cleanup user's local Xauthority file. */ |
| 882 | if (xauthfile) |
| 883 | unlink(xauthfile); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 884 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 885 | /* The connection has been terminated. */ |
| 886 | verbose("Closing connection to %.100s", remote_ip); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 887 | |
Damien Miller | beb4ba5 | 1999-12-28 15:09:35 +1100 | [diff] [blame] | 888 | #ifdef USE_PAM |
Damien Miller | e72b7af | 1999-12-30 15:08:44 +1100 | [diff] [blame] | 889 | finish_pam(); |
Damien Miller | beb4ba5 | 1999-12-28 15:09:35 +1100 | [diff] [blame] | 890 | #endif /* USE_PAM */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 891 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 892 | packet_close(); |
| 893 | exit(0); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 894 | } |
| 895 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 896 | /* |
Damien Miller | 396691a | 2000-01-20 22:44:08 +1100 | [diff] [blame] | 897 | * SSH1 key exchange |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 898 | */ |
Damien Miller | 2ccf661 | 1999-11-15 15:25:10 +1100 | [diff] [blame] | 899 | void |
Damien Miller | 396691a | 2000-01-20 22:44:08 +1100 | [diff] [blame] | 900 | do_ssh_kex() |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 901 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 902 | int i, len; |
Damien Miller | 396691a | 2000-01-20 22:44:08 +1100 | [diff] [blame] | 903 | int plen, slen; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 904 | BIGNUM *session_key_int; |
| 905 | unsigned char session_key[SSH_SESSION_KEY_LENGTH]; |
Damien Miller | 396691a | 2000-01-20 22:44:08 +1100 | [diff] [blame] | 906 | unsigned char cookie[8]; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 907 | unsigned int cipher_type, auth_mask, protocol_flags; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 908 | u_int32_t rand = 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 909 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 910 | /* |
| 911 | * Generate check bytes that the client must send back in the user |
| 912 | * packet in order for it to be accepted; this is used to defy ip |
| 913 | * spoofing attacks. Note that this only works against somebody |
| 914 | * doing IP spoofing from a remote machine; any machine on the local |
| 915 | * network can still see outgoing packets and catch the random |
| 916 | * cookie. This only affects rhosts authentication, and this is one |
| 917 | * of the reasons why it is inherently insecure. |
| 918 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 919 | for (i = 0; i < 8; i++) { |
| 920 | if (i % 4 == 0) |
| 921 | rand = arc4random(); |
Damien Miller | 396691a | 2000-01-20 22:44:08 +1100 | [diff] [blame] | 922 | cookie[i] = rand & 0xff; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 923 | rand >>= 8; |
| 924 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 925 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 926 | /* |
| 927 | * Send our public key. We include in the packet 64 bits of random |
| 928 | * data that must be matched in the reply in order to prevent IP |
| 929 | * spoofing. |
| 930 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 931 | packet_start(SSH_SMSG_PUBLIC_KEY); |
| 932 | for (i = 0; i < 8; i++) |
Damien Miller | 396691a | 2000-01-20 22:44:08 +1100 | [diff] [blame] | 933 | packet_put_char(cookie[i]); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 934 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 935 | /* Store our public server RSA key. */ |
| 936 | packet_put_int(BN_num_bits(public_key->n)); |
| 937 | packet_put_bignum(public_key->e); |
| 938 | packet_put_bignum(public_key->n); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 939 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 940 | /* Store our public host RSA key. */ |
| 941 | packet_put_int(BN_num_bits(sensitive_data.host_key->n)); |
| 942 | packet_put_bignum(sensitive_data.host_key->e); |
| 943 | packet_put_bignum(sensitive_data.host_key->n); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 944 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 945 | /* Put protocol flags. */ |
| 946 | packet_put_int(SSH_PROTOFLAG_HOST_IN_FWD_OPEN); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 947 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 948 | /* Declare which ciphers we support. */ |
| 949 | packet_put_int(cipher_mask()); |
| 950 | |
| 951 | /* Declare supported authentication types. */ |
| 952 | auth_mask = 0; |
| 953 | if (options.rhosts_authentication) |
| 954 | auth_mask |= 1 << SSH_AUTH_RHOSTS; |
| 955 | if (options.rhosts_rsa_authentication) |
| 956 | auth_mask |= 1 << SSH_AUTH_RHOSTS_RSA; |
| 957 | if (options.rsa_authentication) |
| 958 | auth_mask |= 1 << SSH_AUTH_RSA; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 959 | #ifdef KRB4 |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 960 | if (options.kerberos_authentication) |
| 961 | auth_mask |= 1 << SSH_AUTH_KERBEROS; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 962 | #endif |
| 963 | #ifdef AFS |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 964 | if (options.kerberos_tgt_passing) |
| 965 | auth_mask |= 1 << SSH_PASS_KERBEROS_TGT; |
| 966 | if (options.afs_token_passing) |
| 967 | auth_mask |= 1 << SSH_PASS_AFS_TOKEN; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 968 | #endif |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 969 | #ifdef SKEY |
| 970 | if (options.skey_authentication == 1) |
| 971 | auth_mask |= 1 << SSH_AUTH_TIS; |
| 972 | #endif |
| 973 | if (options.password_authentication) |
| 974 | auth_mask |= 1 << SSH_AUTH_PASSWORD; |
| 975 | packet_put_int(auth_mask); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 976 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 977 | /* Send the packet and wait for it to be sent. */ |
| 978 | packet_send(); |
| 979 | packet_write_wait(); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 980 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 981 | debug("Sent %d bit public key and %d bit host key.", |
| 982 | BN_num_bits(public_key->n), BN_num_bits(sensitive_data.host_key->n)); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 983 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 984 | /* Read clients reply (cipher type and session key). */ |
| 985 | packet_read_expect(&plen, SSH_CMSG_SESSION_KEY); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 986 | |
Damien Miller | 50945fa | 1999-12-09 10:31:37 +1100 | [diff] [blame] | 987 | /* Get cipher type and check whether we accept this. */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 988 | cipher_type = packet_get_char(); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 989 | |
Damien Miller | 50945fa | 1999-12-09 10:31:37 +1100 | [diff] [blame] | 990 | if (!(cipher_mask() & (1 << cipher_type))) |
| 991 | packet_disconnect("Warning: client selects unsupported cipher."); |
| 992 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 993 | /* Get check bytes from the packet. These must match those we |
| 994 | sent earlier with the public key packet. */ |
| 995 | for (i = 0; i < 8; i++) |
Damien Miller | 396691a | 2000-01-20 22:44:08 +1100 | [diff] [blame] | 996 | if (cookie[i] != packet_get_char()) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 997 | packet_disconnect("IP Spoofing check bytes do not match."); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 998 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 999 | debug("Encryption type: %.200s", cipher_name(cipher_type)); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1000 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1001 | /* Get the encrypted integer. */ |
| 1002 | session_key_int = BN_new(); |
| 1003 | packet_get_bignum(session_key_int, &slen); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1004 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1005 | protocol_flags = packet_get_int(); |
| 1006 | packet_set_protocol_flags(protocol_flags); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1007 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1008 | packet_integrity_check(plen, 1 + 8 + slen + 4, SSH_CMSG_SESSION_KEY); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1009 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1010 | /* |
| 1011 | * Decrypt it using our private server key and private host key (key |
| 1012 | * with larger modulus first). |
| 1013 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1014 | if (BN_cmp(sensitive_data.private_key->n, sensitive_data.host_key->n) > 0) { |
| 1015 | /* Private key has bigger modulus. */ |
| 1016 | if (BN_num_bits(sensitive_data.private_key->n) < |
| 1017 | BN_num_bits(sensitive_data.host_key->n) + SSH_KEY_BITS_RESERVED) { |
| 1018 | fatal("do_connection: %s: private_key %d < host_key %d + SSH_KEY_BITS_RESERVED %d", |
| 1019 | get_remote_ipaddr(), |
| 1020 | BN_num_bits(sensitive_data.private_key->n), |
| 1021 | BN_num_bits(sensitive_data.host_key->n), |
| 1022 | SSH_KEY_BITS_RESERVED); |
| 1023 | } |
| 1024 | rsa_private_decrypt(session_key_int, session_key_int, |
| 1025 | sensitive_data.private_key); |
| 1026 | rsa_private_decrypt(session_key_int, session_key_int, |
| 1027 | sensitive_data.host_key); |
| 1028 | } else { |
| 1029 | /* Host key has bigger modulus (or they are equal). */ |
| 1030 | if (BN_num_bits(sensitive_data.host_key->n) < |
| 1031 | BN_num_bits(sensitive_data.private_key->n) + SSH_KEY_BITS_RESERVED) { |
| 1032 | fatal("do_connection: %s: host_key %d < private_key %d + SSH_KEY_BITS_RESERVED %d", |
| 1033 | get_remote_ipaddr(), |
| 1034 | BN_num_bits(sensitive_data.host_key->n), |
| 1035 | BN_num_bits(sensitive_data.private_key->n), |
| 1036 | SSH_KEY_BITS_RESERVED); |
| 1037 | } |
| 1038 | rsa_private_decrypt(session_key_int, session_key_int, |
| 1039 | sensitive_data.host_key); |
| 1040 | rsa_private_decrypt(session_key_int, session_key_int, |
| 1041 | sensitive_data.private_key); |
| 1042 | } |
Damien Miller | 356a0b0 | 1999-11-08 15:30:59 +1100 | [diff] [blame] | 1043 | |
Damien Miller | 396691a | 2000-01-20 22:44:08 +1100 | [diff] [blame] | 1044 | compute_session_id(session_id, cookie, |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1045 | sensitive_data.host_key->n, |
| 1046 | sensitive_data.private_key->n); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1047 | |
Damien Miller | 396691a | 2000-01-20 22:44:08 +1100 | [diff] [blame] | 1048 | /* Destroy the private and public keys. They will no longer be needed. */ |
| 1049 | RSA_free(public_key); |
| 1050 | RSA_free(sensitive_data.private_key); |
| 1051 | RSA_free(sensitive_data.host_key); |
| 1052 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1053 | /* |
| 1054 | * Extract session key from the decrypted integer. The key is in the |
| 1055 | * least significant 256 bits of the integer; the first byte of the |
| 1056 | * key is in the highest bits. |
| 1057 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1058 | BN_mask_bits(session_key_int, sizeof(session_key) * 8); |
| 1059 | len = BN_num_bytes(session_key_int); |
| 1060 | if (len < 0 || len > sizeof(session_key)) |
| 1061 | fatal("do_connection: bad len from %s: session_key_int %d > sizeof(session_key) %d", |
| 1062 | get_remote_ipaddr(), |
| 1063 | len, sizeof(session_key)); |
| 1064 | memset(session_key, 0, sizeof(session_key)); |
| 1065 | BN_bn2bin(session_key_int, session_key + sizeof(session_key) - len); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1066 | |
Damien Miller | 396691a | 2000-01-20 22:44:08 +1100 | [diff] [blame] | 1067 | /* Destroy the decrypted integer. It is no longer needed. */ |
| 1068 | BN_clear_free(session_key_int); |
| 1069 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1070 | /* Xor the first 16 bytes of the session key with the session id. */ |
| 1071 | for (i = 0; i < 16; i++) |
| 1072 | session_key[i] ^= session_id[i]; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1073 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1074 | /* Set the session key. From this on all communications will be encrypted. */ |
| 1075 | packet_set_encryption_key(session_key, SSH_SESSION_KEY_LENGTH, cipher_type); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1076 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1077 | /* Destroy our copy of the session key. It is no longer needed. */ |
| 1078 | memset(session_key, 0, sizeof(session_key)); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1079 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1080 | debug("Received session key; encryption turned on."); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1081 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1082 | /* Send an acknowledgement packet. Note that this packet is sent encrypted. */ |
| 1083 | packet_start(SSH_SMSG_SUCCESS); |
| 1084 | packet_send(); |
| 1085 | packet_write_wait(); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1086 | } |
| 1087 | |
Damien Miller | 396691a | 2000-01-20 22:44:08 +1100 | [diff] [blame] | 1088 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1089 | /* |
| 1090 | * Check if the user is allowed to log in via ssh. If user is listed in |
| 1091 | * DenyUsers or user's primary group is listed in DenyGroups, false will |
| 1092 | * be returned. If AllowUsers isn't empty and user isn't listed there, or |
| 1093 | * if AllowGroups isn't empty and user isn't listed there, false will be |
| 1094 | * returned. Otherwise true is returned. |
| 1095 | * XXX This function should also check if user has a valid shell |
| 1096 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1097 | static int |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1098 | allowed_user(struct passwd * pw) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1099 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1100 | struct group *grp; |
| 1101 | int i; |
Damien Miller | 1fa154b | 2000-01-23 10:32:03 +1100 | [diff] [blame] | 1102 | #ifdef WITH_AIXAUTHENTICATE |
| 1103 | char *loginmsg; |
| 1104 | #endif /* WITH_AIXAUTHENTICATE */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1105 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1106 | /* Shouldn't be called if pw is NULL, but better safe than sorry... */ |
| 1107 | if (!pw) |
| 1108 | return 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1109 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1110 | /* XXX Should check for valid login shell */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1111 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1112 | /* Return false if user is listed in DenyUsers */ |
| 1113 | if (options.num_deny_users > 0) { |
| 1114 | if (!pw->pw_name) |
| 1115 | return 0; |
| 1116 | for (i = 0; i < options.num_deny_users; i++) |
| 1117 | if (match_pattern(pw->pw_name, options.deny_users[i])) |
| 1118 | return 0; |
| 1119 | } |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1120 | /* Return false if AllowUsers isn't empty and user isn't listed there */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1121 | if (options.num_allow_users > 0) { |
| 1122 | if (!pw->pw_name) |
| 1123 | return 0; |
| 1124 | for (i = 0; i < options.num_allow_users; i++) |
| 1125 | if (match_pattern(pw->pw_name, options.allow_users[i])) |
| 1126 | break; |
| 1127 | /* i < options.num_allow_users iff we break for loop */ |
| 1128 | if (i >= options.num_allow_users) |
| 1129 | return 0; |
| 1130 | } |
| 1131 | /* Get the primary group name if we need it. Return false if it fails */ |
| 1132 | if (options.num_deny_groups > 0 || options.num_allow_groups > 0) { |
| 1133 | grp = getgrgid(pw->pw_gid); |
| 1134 | if (!grp) |
| 1135 | return 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1136 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1137 | /* Return false if user's group is listed in DenyGroups */ |
| 1138 | if (options.num_deny_groups > 0) { |
| 1139 | if (!grp->gr_name) |
| 1140 | return 0; |
| 1141 | for (i = 0; i < options.num_deny_groups; i++) |
| 1142 | if (match_pattern(grp->gr_name, options.deny_groups[i])) |
| 1143 | return 0; |
| 1144 | } |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1145 | /* |
| 1146 | * Return false if AllowGroups isn't empty and user's group |
| 1147 | * isn't listed there |
| 1148 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1149 | if (options.num_allow_groups > 0) { |
| 1150 | if (!grp->gr_name) |
| 1151 | return 0; |
| 1152 | for (i = 0; i < options.num_allow_groups; i++) |
| 1153 | if (match_pattern(grp->gr_name, options.allow_groups[i])) |
| 1154 | break; |
| 1155 | /* i < options.num_allow_groups iff we break for |
| 1156 | loop */ |
| 1157 | if (i >= options.num_allow_groups) |
| 1158 | return 0; |
| 1159 | } |
| 1160 | } |
Damien Miller | 1fa154b | 2000-01-23 10:32:03 +1100 | [diff] [blame] | 1161 | |
| 1162 | #ifdef WITH_AIXAUTHENTICATE |
| 1163 | if (loginrestrictions(pw->pw_name,S_LOGIN,NULL,&loginmsg) != 0) |
| 1164 | return 0; |
| 1165 | #endif /* WITH_AIXAUTHENTICATE */ |
| 1166 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1167 | /* We found no reason not to let this user try to log on... */ |
| 1168 | return 1; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1169 | } |
| 1170 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1171 | /* |
| 1172 | * Performs authentication of an incoming connection. Session key has already |
Damien Miller | 396691a | 2000-01-20 22:44:08 +1100 | [diff] [blame] | 1173 | * been exchanged and encryption is enabled. |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1174 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1175 | void |
Damien Miller | 396691a | 2000-01-20 22:44:08 +1100 | [diff] [blame] | 1176 | do_authentication() |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1177 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1178 | struct passwd *pw, pwcopy; |
Damien Miller | 396691a | 2000-01-20 22:44:08 +1100 | [diff] [blame] | 1179 | int plen, ulen; |
| 1180 | char *user; |
| 1181 | |
| 1182 | /* Get the name of the user that we wish to log in as. */ |
| 1183 | packet_read_expect(&plen, SSH_CMSG_USER); |
| 1184 | |
| 1185 | /* Get the user name. */ |
| 1186 | user = packet_get_string(&ulen); |
| 1187 | packet_integrity_check(plen, (4 + ulen), SSH_CMSG_USER); |
| 1188 | |
| 1189 | setproctitle("%s", user); |
Damien Miller | 2ccf661 | 1999-11-15 15:25:10 +1100 | [diff] [blame] | 1190 | |
Damien Miller | 1fa154b | 2000-01-23 10:32:03 +1100 | [diff] [blame] | 1191 | #ifdef WITH_AIXAUTHENTICATE |
| 1192 | char *loginmsg; |
| 1193 | #endif /* WITH_AIXAUTHENTICATE */ |
| 1194 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1195 | #ifdef AFS |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1196 | /* If machine has AFS, set process authentication group. */ |
| 1197 | if (k_hasafs()) { |
| 1198 | k_setpag(); |
| 1199 | k_unlog(); |
| 1200 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1201 | #endif /* AFS */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1202 | |
| 1203 | /* Verify that the user is a valid user. */ |
| 1204 | pw = getpwnam(user); |
| 1205 | if (!pw || !allowed_user(pw)) |
| 1206 | do_fake_authloop(user); |
| 1207 | |
| 1208 | /* Take a copy of the returned structure. */ |
| 1209 | memset(&pwcopy, 0, sizeof(pwcopy)); |
| 1210 | pwcopy.pw_name = xstrdup(pw->pw_name); |
| 1211 | pwcopy.pw_passwd = xstrdup(pw->pw_passwd); |
| 1212 | pwcopy.pw_uid = pw->pw_uid; |
| 1213 | pwcopy.pw_gid = pw->pw_gid; |
| 1214 | pwcopy.pw_dir = xstrdup(pw->pw_dir); |
| 1215 | pwcopy.pw_shell = xstrdup(pw->pw_shell); |
| 1216 | pw = &pwcopy; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1217 | |
Damien Miller | beb4ba5 | 1999-12-28 15:09:35 +1100 | [diff] [blame] | 1218 | #ifdef USE_PAM |
Damien Miller | e72b7af | 1999-12-30 15:08:44 +1100 | [diff] [blame] | 1219 | start_pam(pw); |
Damien Miller | 0623076 | 1999-10-28 14:03:14 +1000 | [diff] [blame] | 1220 | #endif |
Damien Miller | 3d112ef | 1999-10-28 13:20:30 +1000 | [diff] [blame] | 1221 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1222 | /* |
| 1223 | * If we are not running as root, the user must have the same uid as |
| 1224 | * the server. |
| 1225 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1226 | if (getuid() != 0 && pw->pw_uid != getuid()) |
| 1227 | packet_disconnect("Cannot change user when server not running as root."); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1228 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1229 | debug("Attempting authentication for %.100s.", user); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1230 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1231 | /* If the user has no password, accept authentication immediately. */ |
| 1232 | if (options.password_authentication && |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1233 | #ifdef KRB4 |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1234 | (!options.kerberos_authentication || options.kerberos_or_local_passwd) && |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1235 | #endif /* KRB4 */ |
Damien Miller | beb4ba5 | 1999-12-28 15:09:35 +1100 | [diff] [blame] | 1236 | #ifdef USE_PAM |
Damien Miller | e72b7af | 1999-12-30 15:08:44 +1100 | [diff] [blame] | 1237 | auth_pam_password(pw, "")) { |
Damien Miller | beb4ba5 | 1999-12-28 15:09:35 +1100 | [diff] [blame] | 1238 | #else /* USE_PAM */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1239 | auth_password(pw, "")) { |
Damien Miller | beb4ba5 | 1999-12-28 15:09:35 +1100 | [diff] [blame] | 1240 | #endif /* USE_PAM */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1241 | /* Authentication with empty password succeeded. */ |
| 1242 | log("Login for user %s from %.100s, accepted without authentication.", |
| 1243 | pw->pw_name, get_remote_ipaddr()); |
| 1244 | } else { |
| 1245 | /* Loop until the user has been authenticated or the |
| 1246 | connection is closed, do_authloop() returns only if |
| 1247 | authentication is successfull */ |
| 1248 | do_authloop(pw); |
| 1249 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1250 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1251 | /* Check if the user is logging in as root and root logins are disallowed. */ |
| 1252 | if (pw->pw_uid == 0 && !options.permit_root_login) { |
| 1253 | if (forced_command) |
| 1254 | log("Root login accepted for forced command."); |
| 1255 | else |
| 1256 | packet_disconnect("ROOT LOGIN REFUSED FROM %.200s", |
| 1257 | get_canonical_hostname()); |
| 1258 | } |
| 1259 | /* The user has been authenticated and accepted. */ |
Damien Miller | 1fa154b | 2000-01-23 10:32:03 +1100 | [diff] [blame] | 1260 | #ifdef WITH_AIXAUTHENTICATE |
| 1261 | loginsuccess(user,get_canonical_hostname(),"ssh",&loginmsg); |
| 1262 | #endif /* WITH_AIXAUTHENTICATE */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1263 | packet_start(SSH_SMSG_SUCCESS); |
| 1264 | packet_send(); |
| 1265 | packet_write_wait(); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1266 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1267 | /* Perform session preparation. */ |
| 1268 | do_authenticated(pw); |
Damien Miller | 2ccf661 | 1999-11-15 15:25:10 +1100 | [diff] [blame] | 1269 | } |
| 1270 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1271 | #define AUTH_FAIL_MAX 6 |
| 1272 | #define AUTH_FAIL_LOG (AUTH_FAIL_MAX/2) |
| 1273 | #define AUTH_FAIL_MSG "Too many authentication failures for %.100s" |
Damien Miller | 2ccf661 | 1999-11-15 15:25:10 +1100 | [diff] [blame] | 1274 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1275 | /* |
| 1276 | * read packets and try to authenticate local user *pw. |
| 1277 | * return if authentication is successfull |
| 1278 | */ |
Damien Miller | 2ccf661 | 1999-11-15 15:25:10 +1100 | [diff] [blame] | 1279 | void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1280 | do_authloop(struct passwd * pw) |
Damien Miller | 2ccf661 | 1999-11-15 15:25:10 +1100 | [diff] [blame] | 1281 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1282 | int attempt = 0; |
| 1283 | unsigned int bits; |
| 1284 | BIGNUM *client_host_key_e, *client_host_key_n; |
| 1285 | BIGNUM *n; |
| 1286 | char *client_user = NULL, *password = NULL; |
| 1287 | char user[1024]; |
| 1288 | int plen, dlen, nlen, ulen, elen; |
| 1289 | int type = 0; |
| 1290 | void (*authlog) (const char *fmt,...) = verbose; |
Damien Miller | 2ccf661 | 1999-11-15 15:25:10 +1100 | [diff] [blame] | 1291 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1292 | /* Indicate that authentication is needed. */ |
| 1293 | packet_start(SSH_SMSG_FAILURE); |
| 1294 | packet_send(); |
| 1295 | packet_write_wait(); |
Damien Miller | 2ccf661 | 1999-11-15 15:25:10 +1100 | [diff] [blame] | 1296 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1297 | for (attempt = 1;; attempt++) { |
| 1298 | int authenticated = 0; |
| 1299 | strlcpy(user, "", sizeof user); |
Damien Miller | 2ccf661 | 1999-11-15 15:25:10 +1100 | [diff] [blame] | 1300 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1301 | /* Get a packet from the client. */ |
| 1302 | type = packet_read(&plen); |
| 1303 | |
| 1304 | /* Process the packet. */ |
| 1305 | switch (type) { |
Damien Miller | 2ccf661 | 1999-11-15 15:25:10 +1100 | [diff] [blame] | 1306 | #ifdef AFS |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1307 | case SSH_CMSG_HAVE_KERBEROS_TGT: |
| 1308 | if (!options.kerberos_tgt_passing) { |
| 1309 | /* packet_get_all(); */ |
| 1310 | verbose("Kerberos tgt passing disabled."); |
| 1311 | break; |
| 1312 | } else { |
| 1313 | /* Accept Kerberos tgt. */ |
| 1314 | char *tgt = packet_get_string(&dlen); |
| 1315 | packet_integrity_check(plen, 4 + dlen, type); |
| 1316 | if (!auth_kerberos_tgt(pw, tgt)) |
| 1317 | verbose("Kerberos tgt REFUSED for %s", pw->pw_name); |
| 1318 | xfree(tgt); |
| 1319 | } |
| 1320 | continue; |
| 1321 | |
| 1322 | case SSH_CMSG_HAVE_AFS_TOKEN: |
| 1323 | if (!options.afs_token_passing || !k_hasafs()) { |
| 1324 | /* packet_get_all(); */ |
| 1325 | verbose("AFS token passing disabled."); |
| 1326 | break; |
| 1327 | } else { |
| 1328 | /* Accept AFS token. */ |
| 1329 | char *token_string = packet_get_string(&dlen); |
| 1330 | packet_integrity_check(plen, 4 + dlen, type); |
| 1331 | if (!auth_afs_token(pw, token_string)) |
| 1332 | verbose("AFS token REFUSED for %s", pw->pw_name); |
| 1333 | xfree(token_string); |
| 1334 | } |
| 1335 | continue; |
Damien Miller | 2ccf661 | 1999-11-15 15:25:10 +1100 | [diff] [blame] | 1336 | #endif /* AFS */ |
Damien Miller | 2ccf661 | 1999-11-15 15:25:10 +1100 | [diff] [blame] | 1337 | #ifdef KRB4 |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1338 | case SSH_CMSG_AUTH_KERBEROS: |
| 1339 | if (!options.kerberos_authentication) { |
| 1340 | /* packet_get_all(); */ |
| 1341 | verbose("Kerberos authentication disabled."); |
| 1342 | break; |
| 1343 | } else { |
| 1344 | /* Try Kerberos v4 authentication. */ |
| 1345 | KTEXT_ST auth; |
| 1346 | char *tkt_user = NULL; |
| 1347 | char *kdata = packet_get_string((unsigned int *) &auth.length); |
| 1348 | packet_integrity_check(plen, 4 + auth.length, type); |
Damien Miller | 2ccf661 | 1999-11-15 15:25:10 +1100 | [diff] [blame] | 1349 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1350 | if (auth.length < MAX_KTXT_LEN) |
| 1351 | memcpy(auth.dat, kdata, auth.length); |
| 1352 | xfree(kdata); |
| 1353 | |
| 1354 | authenticated = auth_krb4(pw->pw_name, &auth, &tkt_user); |
| 1355 | |
| 1356 | if (authenticated) { |
| 1357 | snprintf(user, sizeof user, " tktuser %s", tkt_user); |
| 1358 | xfree(tkt_user); |
| 1359 | } |
| 1360 | } |
| 1361 | break; |
Damien Miller | 2ccf661 | 1999-11-15 15:25:10 +1100 | [diff] [blame] | 1362 | #endif /* KRB4 */ |
Damien Miller | 2ccf661 | 1999-11-15 15:25:10 +1100 | [diff] [blame] | 1363 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1364 | case SSH_CMSG_AUTH_RHOSTS: |
| 1365 | if (!options.rhosts_authentication) { |
| 1366 | verbose("Rhosts authentication disabled."); |
| 1367 | break; |
| 1368 | } |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1369 | /* |
| 1370 | * Get client user name. Note that we just have to |
| 1371 | * trust the client; this is one reason why rhosts |
| 1372 | * authentication is insecure. (Another is |
| 1373 | * IP-spoofing on a local network.) |
| 1374 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1375 | client_user = packet_get_string(&ulen); |
| 1376 | packet_integrity_check(plen, 4 + ulen, type); |
| 1377 | |
| 1378 | /* Try to authenticate using /etc/hosts.equiv and |
| 1379 | .rhosts. */ |
| 1380 | authenticated = auth_rhosts(pw, client_user); |
| 1381 | |
| 1382 | snprintf(user, sizeof user, " ruser %s", client_user); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1383 | break; |
Damien Miller | 7e8e820 | 1999-11-16 13:37:16 +1100 | [diff] [blame] | 1384 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1385 | case SSH_CMSG_AUTH_RHOSTS_RSA: |
| 1386 | if (!options.rhosts_rsa_authentication) { |
| 1387 | verbose("Rhosts with RSA authentication disabled."); |
| 1388 | break; |
| 1389 | } |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1390 | /* |
| 1391 | * Get client user name. Note that we just have to |
| 1392 | * trust the client; root on the client machine can |
| 1393 | * claim to be any user. |
| 1394 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1395 | client_user = packet_get_string(&ulen); |
| 1396 | |
| 1397 | /* Get the client host key. */ |
| 1398 | client_host_key_e = BN_new(); |
| 1399 | client_host_key_n = BN_new(); |
| 1400 | bits = packet_get_int(); |
| 1401 | packet_get_bignum(client_host_key_e, &elen); |
| 1402 | packet_get_bignum(client_host_key_n, &nlen); |
| 1403 | |
| 1404 | if (bits != BN_num_bits(client_host_key_n)) |
| 1405 | error("Warning: keysize mismatch for client_host_key: " |
| 1406 | "actual %d, announced %d", BN_num_bits(client_host_key_n), bits); |
| 1407 | packet_integrity_check(plen, (4 + ulen) + 4 + elen + nlen, type); |
| 1408 | |
| 1409 | authenticated = auth_rhosts_rsa(pw, client_user, |
| 1410 | client_host_key_e, client_host_key_n); |
| 1411 | BN_clear_free(client_host_key_e); |
| 1412 | BN_clear_free(client_host_key_n); |
| 1413 | |
| 1414 | snprintf(user, sizeof user, " ruser %s", client_user); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1415 | break; |
Damien Miller | 81428f9 | 1999-11-18 09:28:11 +1100 | [diff] [blame] | 1416 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1417 | case SSH_CMSG_AUTH_RSA: |
| 1418 | if (!options.rsa_authentication) { |
| 1419 | verbose("RSA authentication disabled."); |
| 1420 | break; |
| 1421 | } |
| 1422 | /* RSA authentication requested. */ |
| 1423 | n = BN_new(); |
| 1424 | packet_get_bignum(n, &nlen); |
| 1425 | packet_integrity_check(plen, nlen, type); |
| 1426 | authenticated = auth_rsa(pw, n); |
| 1427 | BN_clear_free(n); |
| 1428 | break; |
| 1429 | |
| 1430 | case SSH_CMSG_AUTH_PASSWORD: |
| 1431 | if (!options.password_authentication) { |
| 1432 | verbose("Password authentication disabled."); |
| 1433 | break; |
| 1434 | } |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1435 | /* |
| 1436 | * Read user password. It is in plain text, but was |
| 1437 | * transmitted over the encrypted channel so it is |
| 1438 | * not visible to an outside observer. |
| 1439 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1440 | password = packet_get_string(&dlen); |
| 1441 | packet_integrity_check(plen, 4 + dlen, type); |
| 1442 | |
Damien Miller | beb4ba5 | 1999-12-28 15:09:35 +1100 | [diff] [blame] | 1443 | #ifdef USE_PAM |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1444 | /* Do PAM auth with password */ |
Damien Miller | e72b7af | 1999-12-30 15:08:44 +1100 | [diff] [blame] | 1445 | authenticated = auth_pam_password(pw, password); |
Damien Miller | beb4ba5 | 1999-12-28 15:09:35 +1100 | [diff] [blame] | 1446 | #else /* USE_PAM */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1447 | /* Try authentication with the password. */ |
| 1448 | authenticated = auth_password(pw, password); |
Damien Miller | beb4ba5 | 1999-12-28 15:09:35 +1100 | [diff] [blame] | 1449 | #endif /* USE_PAM */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1450 | memset(password, 0, strlen(password)); |
| 1451 | xfree(password); |
| 1452 | break; |
Damien Miller | 2ccf661 | 1999-11-15 15:25:10 +1100 | [diff] [blame] | 1453 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1454 | #ifdef SKEY |
| 1455 | case SSH_CMSG_AUTH_TIS: |
| 1456 | debug("rcvd SSH_CMSG_AUTH_TIS"); |
| 1457 | if (options.skey_authentication == 1) { |
| 1458 | char *skeyinfo = skey_keyinfo(pw->pw_name); |
| 1459 | if (skeyinfo == NULL) { |
| 1460 | debug("generating fake skeyinfo for %.100s.", pw->pw_name); |
| 1461 | skeyinfo = skey_fake_keyinfo(pw->pw_name); |
| 1462 | } |
| 1463 | if (skeyinfo != NULL) { |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1464 | /* we send our s/key- in tis-challenge messages */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1465 | debug("sending challenge '%s'", skeyinfo); |
| 1466 | packet_start(SSH_SMSG_AUTH_TIS_CHALLENGE); |
| 1467 | packet_put_string(skeyinfo, strlen(skeyinfo)); |
| 1468 | packet_send(); |
| 1469 | packet_write_wait(); |
| 1470 | continue; |
| 1471 | } |
| 1472 | } |
| 1473 | break; |
| 1474 | case SSH_CMSG_AUTH_TIS_RESPONSE: |
| 1475 | debug("rcvd SSH_CMSG_AUTH_TIS_RESPONSE"); |
| 1476 | if (options.skey_authentication == 1) { |
| 1477 | char *response = packet_get_string(&dlen); |
| 1478 | debug("skey response == '%s'", response); |
| 1479 | packet_integrity_check(plen, 4 + dlen, type); |
| 1480 | authenticated = (skey_haskey(pw->pw_name) == 0 && |
| 1481 | skey_passcheck(pw->pw_name, response) != -1); |
| 1482 | xfree(response); |
| 1483 | } |
| 1484 | break; |
| 1485 | #else |
| 1486 | case SSH_CMSG_AUTH_TIS: |
| 1487 | /* TIS Authentication is unsupported */ |
| 1488 | log("TIS authentication unsupported."); |
| 1489 | break; |
| 1490 | #endif |
| 1491 | |
| 1492 | default: |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1493 | /* |
| 1494 | * Any unknown messages will be ignored (and failure |
| 1495 | * returned) during authentication. |
| 1496 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1497 | log("Unknown message during authentication: type %d", type); |
| 1498 | break; |
| 1499 | } |
| 1500 | |
| 1501 | /* Raise logging level */ |
| 1502 | if (authenticated || |
| 1503 | attempt == AUTH_FAIL_LOG || |
| 1504 | type == SSH_CMSG_AUTH_PASSWORD) |
| 1505 | authlog = log; |
| 1506 | |
| 1507 | authlog("%s %s for %.200s from %.200s port %d%s", |
| 1508 | authenticated ? "Accepted" : "Failed", |
| 1509 | get_authname(type), |
| 1510 | pw->pw_uid == 0 ? "ROOT" : pw->pw_name, |
| 1511 | get_remote_ipaddr(), |
| 1512 | get_remote_port(), |
| 1513 | user); |
Damien Miller | 2ccf661 | 1999-11-15 15:25:10 +1100 | [diff] [blame] | 1514 | |
Damien Miller | e72b7af | 1999-12-30 15:08:44 +1100 | [diff] [blame] | 1515 | if (authenticated) { |
| 1516 | #ifdef USE_PAM |
Damien Miller | 1fa154b | 2000-01-23 10:32:03 +1100 | [diff] [blame] | 1517 | if (!do_pam_account(pw->pw_name, client_user)) { |
Damien Miller | e72b7af | 1999-12-30 15:08:44 +1100 | [diff] [blame] | 1518 | if (client_user != NULL) |
| 1519 | xfree(client_user); |
| 1520 | |
| 1521 | do_fake_authloop(pw->pw_name); |
| 1522 | } |
| 1523 | #endif /* USE_PAM */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1524 | return; |
Damien Miller | e72b7af | 1999-12-30 15:08:44 +1100 | [diff] [blame] | 1525 | } |
| 1526 | |
| 1527 | if (client_user != NULL) |
| 1528 | xfree(client_user); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1529 | |
| 1530 | if (attempt > AUTH_FAIL_MAX) |
| 1531 | packet_disconnect(AUTH_FAIL_MSG, pw->pw_name); |
| 1532 | |
| 1533 | /* Send a message indicating that the authentication attempt failed. */ |
| 1534 | packet_start(SSH_SMSG_FAILURE); |
| 1535 | packet_send(); |
| 1536 | packet_write_wait(); |
| 1537 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1538 | } |
| 1539 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1540 | /* |
| 1541 | * The user does not exist or access is denied, |
| 1542 | * but fake indication that authentication is needed. |
| 1543 | */ |
Damien Miller | 2ccf661 | 1999-11-15 15:25:10 +1100 | [diff] [blame] | 1544 | void |
| 1545 | do_fake_authloop(char *user) |
Damien Miller | 3d112ef | 1999-10-28 13:20:30 +1000 | [diff] [blame] | 1546 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1547 | int attempt = 0; |
Damien Miller | 2ccf661 | 1999-11-15 15:25:10 +1100 | [diff] [blame] | 1548 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1549 | log("Faking authloop for illegal user %.200s from %.200s port %d", |
| 1550 | user, |
| 1551 | get_remote_ipaddr(), |
| 1552 | get_remote_port()); |
Damien Miller | 3d112ef | 1999-10-28 13:20:30 +1000 | [diff] [blame] | 1553 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1554 | /* Indicate that authentication is needed. */ |
| 1555 | packet_start(SSH_SMSG_FAILURE); |
| 1556 | packet_send(); |
| 1557 | packet_write_wait(); |
| 1558 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1559 | /* |
| 1560 | * Keep reading packets, and always respond with a failure. This is |
| 1561 | * to avoid disclosing whether such a user really exists. |
| 1562 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1563 | for (attempt = 1;; attempt++) { |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1564 | /* Read a packet. This will not return if the client disconnects. */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1565 | int plen; |
Damien Miller | e72b7af | 1999-12-30 15:08:44 +1100 | [diff] [blame] | 1566 | #ifndef SKEY |
| 1567 | (void)packet_read(&plen); |
| 1568 | #else /* SKEY */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1569 | int type = packet_read(&plen); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1570 | int dlen; |
| 1571 | char *password, *skeyinfo; |
Damien Miller | a34a28b | 1999-12-14 10:47:15 +1100 | [diff] [blame] | 1572 | /* Try to send a fake s/key challenge. */ |
| 1573 | if (options.skey_authentication == 1 && |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1574 | (skeyinfo = skey_fake_keyinfo(user)) != NULL) { |
Damien Miller | a34a28b | 1999-12-14 10:47:15 +1100 | [diff] [blame] | 1575 | if (type == SSH_CMSG_AUTH_TIS) { |
| 1576 | packet_start(SSH_SMSG_AUTH_TIS_CHALLENGE); |
| 1577 | packet_put_string(skeyinfo, strlen(skeyinfo)); |
| 1578 | packet_send(); |
| 1579 | packet_write_wait(); |
| 1580 | continue; |
| 1581 | } else if (type == SSH_CMSG_AUTH_PASSWORD && |
| 1582 | options.password_authentication && |
| 1583 | (password = packet_get_string(&dlen)) != NULL && |
| 1584 | dlen == 5 && |
| 1585 | strncasecmp(password, "s/key", 5) == 0 ) { |
| 1586 | packet_send_debug(skeyinfo); |
| 1587 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1588 | } |
Damien Miller | 2ccf661 | 1999-11-15 15:25:10 +1100 | [diff] [blame] | 1589 | #endif |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1590 | if (attempt > AUTH_FAIL_MAX) |
| 1591 | packet_disconnect(AUTH_FAIL_MSG, user); |
| 1592 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1593 | /* |
| 1594 | * Send failure. This should be indistinguishable from a |
| 1595 | * failed authentication. |
| 1596 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1597 | packet_start(SSH_SMSG_FAILURE); |
| 1598 | packet_send(); |
| 1599 | packet_write_wait(); |
Damien Miller | 1fa154b | 2000-01-23 10:32:03 +1100 | [diff] [blame] | 1600 | #ifdef WITH_AIXAUTHENTICATE |
| 1601 | if (strncmp(get_authname(type),"password", |
| 1602 | strlen(get_authname(type))) == 0) |
| 1603 | loginfailed(pw->pw_name,get_canonical_hostname(),"ssh"); |
| 1604 | #endif /* WITH_AIXAUTHENTICATE */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1605 | } |
| 1606 | /* NOTREACHED */ |
| 1607 | abort(); |
Damien Miller | 3d112ef | 1999-10-28 13:20:30 +1000 | [diff] [blame] | 1608 | } |
| 1609 | |
Damien Miller | 2ccf661 | 1999-11-15 15:25:10 +1100 | [diff] [blame] | 1610 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1611 | /* |
| 1612 | * Remove local Xauthority file. |
| 1613 | */ |
Damien Miller | 5ce662a | 1999-11-11 17:57:39 +1100 | [diff] [blame] | 1614 | static void |
| 1615 | xauthfile_cleanup_proc(void *ignore) |
| 1616 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1617 | debug("xauthfile_cleanup_proc called"); |
Damien Miller | 5ce662a | 1999-11-11 17:57:39 +1100 | [diff] [blame] | 1618 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1619 | if (xauthfile != NULL) { |
| 1620 | unlink(xauthfile); |
| 1621 | xfree(xauthfile); |
| 1622 | xauthfile = NULL; |
| 1623 | } |
Damien Miller | 5ce662a | 1999-11-11 17:57:39 +1100 | [diff] [blame] | 1624 | } |
| 1625 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1626 | /* |
| 1627 | * Prepares for an interactive session. This is called after the user has |
| 1628 | * been successfully authenticated. During this message exchange, pseudo |
| 1629 | * terminals are allocated, X11, TCP/IP, and authentication agent forwardings |
| 1630 | * are requested, etc. |
| 1631 | */ |
| 1632 | void |
| 1633 | do_authenticated(struct passwd * pw) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1634 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1635 | int type; |
| 1636 | int compression_level = 0, enable_compression_after_reply = 0; |
| 1637 | int have_pty = 0, ptyfd = -1, ttyfd = -1, xauthfd = -1; |
| 1638 | int row, col, xpixel, ypixel, screen; |
| 1639 | char ttyname[64]; |
| 1640 | char *command, *term = NULL, *display = NULL, *proto = NULL, |
| 1641 | *data = NULL; |
| 1642 | struct group *grp; |
| 1643 | gid_t tty_gid; |
| 1644 | mode_t tty_mode; |
| 1645 | int n_bytes; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1646 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1647 | /* |
| 1648 | * Cancel the alarm we set to limit the time taken for |
| 1649 | * authentication. |
| 1650 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1651 | alarm(0); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1652 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1653 | /* |
| 1654 | * Inform the channel mechanism that we are the server side and that |
| 1655 | * the client may request to connect to any port at all. (The user |
| 1656 | * could do it anyway, and we wouldn\'t know what is permitted except |
| 1657 | * by the client telling us, so we can equally well trust the client |
| 1658 | * not to request anything bogus.) |
| 1659 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1660 | channel_permit_all_opens(); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1661 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1662 | /* |
| 1663 | * We stay in this loop until the client requests to execute a shell |
| 1664 | * or a command. |
| 1665 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1666 | while (1) { |
| 1667 | int plen, dlen; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1668 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1669 | /* Get a packet from the client. */ |
| 1670 | type = packet_read(&plen); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1671 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1672 | /* Process the packet. */ |
| 1673 | switch (type) { |
| 1674 | case SSH_CMSG_REQUEST_COMPRESSION: |
| 1675 | packet_integrity_check(plen, 4, type); |
| 1676 | compression_level = packet_get_int(); |
| 1677 | if (compression_level < 1 || compression_level > 9) { |
| 1678 | packet_send_debug("Received illegal compression level %d.", |
| 1679 | compression_level); |
| 1680 | goto fail; |
| 1681 | } |
| 1682 | /* Enable compression after we have responded with SUCCESS. */ |
| 1683 | enable_compression_after_reply = 1; |
| 1684 | break; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1685 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1686 | case SSH_CMSG_REQUEST_PTY: |
| 1687 | if (no_pty_flag) { |
| 1688 | debug("Allocating a pty not permitted for this authentication."); |
| 1689 | goto fail; |
| 1690 | } |
| 1691 | if (have_pty) |
| 1692 | packet_disconnect("Protocol error: you already have a pty."); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1693 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1694 | debug("Allocating pty."); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1695 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1696 | /* Allocate a pty and open it. */ |
Damien Miller | 037a0dc | 1999-12-07 15:38:31 +1100 | [diff] [blame] | 1697 | if (!pty_allocate(&ptyfd, &ttyfd, ttyname, |
| 1698 | sizeof(ttyname))) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1699 | error("Failed to allocate pty."); |
| 1700 | goto fail; |
| 1701 | } |
| 1702 | /* Determine the group to make the owner of the tty. */ |
| 1703 | grp = getgrnam("tty"); |
| 1704 | if (grp) { |
| 1705 | tty_gid = grp->gr_gid; |
| 1706 | tty_mode = S_IRUSR | S_IWUSR | S_IWGRP; |
| 1707 | } else { |
| 1708 | tty_gid = pw->pw_gid; |
| 1709 | tty_mode = S_IRUSR | S_IWUSR | S_IWGRP | S_IWOTH; |
| 1710 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1711 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1712 | /* Change ownership of the tty. */ |
| 1713 | if (chown(ttyname, pw->pw_uid, tty_gid) < 0) |
| 1714 | fatal("chown(%.100s, %d, %d) failed: %.100s", |
| 1715 | ttyname, pw->pw_uid, tty_gid, strerror(errno)); |
| 1716 | if (chmod(ttyname, tty_mode) < 0) |
| 1717 | fatal("chmod(%.100s, 0%o) failed: %.100s", |
| 1718 | ttyname, tty_mode, strerror(errno)); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1719 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1720 | /* Get TERM from the packet. Note that the value may be of arbitrary length. */ |
| 1721 | term = packet_get_string(&dlen); |
| 1722 | packet_integrity_check(dlen, strlen(term), type); |
| 1723 | /* packet_integrity_check(plen, 4 + dlen + 4*4 + n_bytes, type); */ |
| 1724 | /* Remaining bytes */ |
| 1725 | n_bytes = plen - (4 + dlen + 4 * 4); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1726 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1727 | if (strcmp(term, "") == 0) |
| 1728 | term = NULL; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1729 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1730 | /* Get window size from the packet. */ |
| 1731 | row = packet_get_int(); |
| 1732 | col = packet_get_int(); |
| 1733 | xpixel = packet_get_int(); |
| 1734 | ypixel = packet_get_int(); |
| 1735 | pty_change_window_size(ptyfd, row, col, xpixel, ypixel); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1736 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1737 | /* Get tty modes from the packet. */ |
| 1738 | tty_parse_modes(ttyfd, &n_bytes); |
| 1739 | packet_integrity_check(plen, 4 + dlen + 4 * 4 + n_bytes, type); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1740 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1741 | /* Indicate that we now have a pty. */ |
| 1742 | have_pty = 1; |
Damien Miller | bf1c9b2 | 1999-12-09 10:16:54 +1100 | [diff] [blame] | 1743 | |
Damien Miller | beb4ba5 | 1999-12-28 15:09:35 +1100 | [diff] [blame] | 1744 | #ifdef USE_PAM |
Damien Miller | bf1c9b2 | 1999-12-09 10:16:54 +1100 | [diff] [blame] | 1745 | /* do the pam_open_session since we have the pty */ |
Damien Miller | e72b7af | 1999-12-30 15:08:44 +1100 | [diff] [blame] | 1746 | do_pam_session(pw->pw_name, ttyname); |
Damien Miller | beb4ba5 | 1999-12-28 15:09:35 +1100 | [diff] [blame] | 1747 | #endif /* USE_PAM */ |
Damien Miller | bf1c9b2 | 1999-12-09 10:16:54 +1100 | [diff] [blame] | 1748 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1749 | break; |
| 1750 | |
| 1751 | case SSH_CMSG_X11_REQUEST_FORWARDING: |
| 1752 | if (!options.x11_forwarding) { |
| 1753 | packet_send_debug("X11 forwarding disabled in server configuration file."); |
| 1754 | goto fail; |
| 1755 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1756 | #ifdef XAUTH_PATH |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1757 | if (no_x11_forwarding_flag) { |
| 1758 | packet_send_debug("X11 forwarding not permitted for this authentication."); |
| 1759 | goto fail; |
| 1760 | } |
| 1761 | debug("Received request for X11 forwarding with auth spoofing."); |
| 1762 | if (display) |
| 1763 | packet_disconnect("Protocol error: X11 display already set."); |
| 1764 | { |
| 1765 | int proto_len, data_len; |
| 1766 | proto = packet_get_string(&proto_len); |
| 1767 | data = packet_get_string(&data_len); |
| 1768 | packet_integrity_check(plen, 4 + proto_len + 4 + data_len + 4, type); |
| 1769 | } |
| 1770 | if (packet_get_protocol_flags() & SSH_PROTOFLAG_SCREEN_NUMBER) |
| 1771 | screen = packet_get_int(); |
| 1772 | else |
| 1773 | screen = 0; |
Damien Miller | a34a28b | 1999-12-14 10:47:15 +1100 | [diff] [blame] | 1774 | display = x11_create_display_inet(screen, options.x11_display_offset); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1775 | if (!display) |
| 1776 | goto fail; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1777 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1778 | /* Setup to always have a local .Xauthority. */ |
| 1779 | xauthfile = xmalloc(MAXPATHLEN); |
| 1780 | snprintf(xauthfile, MAXPATHLEN, "/tmp/XauthXXXXXX"); |
| 1781 | |
| 1782 | if ((xauthfd = mkstemp(xauthfile)) != -1) { |
| 1783 | fchown(xauthfd, pw->pw_uid, pw->pw_gid); |
| 1784 | close(xauthfd); |
| 1785 | fatal_add_cleanup(xauthfile_cleanup_proc, NULL); |
| 1786 | } else { |
| 1787 | xfree(xauthfile); |
| 1788 | xauthfile = NULL; |
| 1789 | } |
| 1790 | break; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1791 | #else /* XAUTH_PATH */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1792 | packet_send_debug("No xauth program; cannot forward with spoofing."); |
| 1793 | goto fail; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1794 | #endif /* XAUTH_PATH */ |
| 1795 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1796 | case SSH_CMSG_AGENT_REQUEST_FORWARDING: |
Damien Miller | 396691a | 2000-01-20 22:44:08 +1100 | [diff] [blame] | 1797 | if (no_agent_forwarding_flag || compat13) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1798 | debug("Authentication agent forwarding not permitted for this authentication."); |
| 1799 | goto fail; |
| 1800 | } |
| 1801 | debug("Received authentication agent forwarding request."); |
| 1802 | auth_input_request_forwarding(pw); |
| 1803 | break; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1804 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1805 | case SSH_CMSG_PORT_FORWARD_REQUEST: |
| 1806 | if (no_port_forwarding_flag) { |
| 1807 | debug("Port forwarding not permitted for this authentication."); |
| 1808 | goto fail; |
| 1809 | } |
| 1810 | debug("Received TCP/IP port forwarding request."); |
| 1811 | channel_input_port_forward_request(pw->pw_uid == 0); |
| 1812 | break; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1813 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1814 | case SSH_CMSG_MAX_PACKET_SIZE: |
| 1815 | if (packet_set_maxsize(packet_get_int()) < 0) |
| 1816 | goto fail; |
| 1817 | break; |
Damien Miller | 6162d12 | 1999-11-21 13:23:52 +1100 | [diff] [blame] | 1818 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1819 | case SSH_CMSG_EXEC_SHELL: |
| 1820 | /* Set interactive/non-interactive mode. */ |
| 1821 | packet_set_interactive(have_pty || display != NULL, |
| 1822 | options.keepalives); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1823 | |
Damien Miller | ac3a4b4 | 1999-12-29 10:25:40 +1100 | [diff] [blame] | 1824 | #ifdef USE_PAM |
| 1825 | do_pam_setcred(); |
Damien Miller | e72b7af | 1999-12-30 15:08:44 +1100 | [diff] [blame] | 1826 | #endif /* USE_PAM */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1827 | if (forced_command != NULL) |
| 1828 | goto do_forced_command; |
| 1829 | debug("Forking shell."); |
| 1830 | packet_integrity_check(plen, 0, type); |
| 1831 | if (have_pty) |
| 1832 | do_exec_pty(NULL, ptyfd, ttyfd, ttyname, pw, term, display, proto, data); |
| 1833 | else |
| 1834 | do_exec_no_pty(NULL, pw, display, proto, data); |
| 1835 | return; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1836 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1837 | case SSH_CMSG_EXEC_CMD: |
| 1838 | /* Set interactive/non-interactive mode. */ |
| 1839 | packet_set_interactive(have_pty || display != NULL, |
| 1840 | options.keepalives); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1841 | |
Damien Miller | ac3a4b4 | 1999-12-29 10:25:40 +1100 | [diff] [blame] | 1842 | #ifdef USE_PAM |
| 1843 | do_pam_setcred(); |
Damien Miller | e72b7af | 1999-12-30 15:08:44 +1100 | [diff] [blame] | 1844 | #endif /* USE_PAM */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1845 | if (forced_command != NULL) |
| 1846 | goto do_forced_command; |
| 1847 | /* Get command from the packet. */ |
| 1848 | { |
| 1849 | int dlen; |
| 1850 | command = packet_get_string(&dlen); |
| 1851 | debug("Executing command '%.500s'", command); |
| 1852 | packet_integrity_check(plen, 4 + dlen, type); |
| 1853 | } |
| 1854 | if (have_pty) |
| 1855 | do_exec_pty(command, ptyfd, ttyfd, ttyname, pw, term, display, proto, data); |
| 1856 | else |
| 1857 | do_exec_no_pty(command, pw, display, proto, data); |
| 1858 | xfree(command); |
| 1859 | return; |
| 1860 | |
| 1861 | default: |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1862 | /* |
| 1863 | * Any unknown messages in this phase are ignored, |
| 1864 | * and a failure message is returned. |
| 1865 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1866 | log("Unknown packet type received after authentication: %d", type); |
| 1867 | goto fail; |
| 1868 | } |
| 1869 | |
| 1870 | /* The request was successfully processed. */ |
| 1871 | packet_start(SSH_SMSG_SUCCESS); |
| 1872 | packet_send(); |
| 1873 | packet_write_wait(); |
| 1874 | |
| 1875 | /* Enable compression now that we have replied if appropriate. */ |
| 1876 | if (enable_compression_after_reply) { |
| 1877 | enable_compression_after_reply = 0; |
| 1878 | packet_start_compression(compression_level); |
| 1879 | } |
| 1880 | continue; |
| 1881 | |
| 1882 | fail: |
| 1883 | /* The request failed. */ |
| 1884 | packet_start(SSH_SMSG_FAILURE); |
| 1885 | packet_send(); |
| 1886 | packet_write_wait(); |
| 1887 | continue; |
| 1888 | |
| 1889 | do_forced_command: |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1890 | /* |
| 1891 | * There is a forced command specified for this login. |
| 1892 | * Execute it. |
| 1893 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1894 | debug("Executing forced command: %.900s", forced_command); |
| 1895 | if (have_pty) |
| 1896 | do_exec_pty(forced_command, ptyfd, ttyfd, ttyname, pw, term, display, proto, data); |
| 1897 | else |
| 1898 | do_exec_no_pty(forced_command, pw, display, proto, data); |
| 1899 | return; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1900 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1901 | } |
| 1902 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1903 | /* |
| 1904 | * This is called to fork and execute a command when we have no tty. This |
| 1905 | * will call do_child from the child, and server_loop from the parent after |
| 1906 | * setting up file descriptors and such. |
| 1907 | */ |
| 1908 | void |
| 1909 | do_exec_no_pty(const char *command, struct passwd * pw, |
| 1910 | const char *display, const char *auth_proto, |
| 1911 | const char *auth_data) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1912 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1913 | int pid; |
| 1914 | |
| 1915 | #ifdef USE_PIPES |
| 1916 | int pin[2], pout[2], perr[2]; |
| 1917 | /* Allocate pipes for communicating with the program. */ |
| 1918 | if (pipe(pin) < 0 || pipe(pout) < 0 || pipe(perr) < 0) |
| 1919 | packet_disconnect("Could not create pipes: %.100s", |
| 1920 | strerror(errno)); |
| 1921 | #else /* USE_PIPES */ |
| 1922 | int inout[2], err[2]; |
| 1923 | /* Uses socket pairs to communicate with the program. */ |
| 1924 | if (socketpair(AF_UNIX, SOCK_STREAM, 0, inout) < 0 || |
| 1925 | socketpair(AF_UNIX, SOCK_STREAM, 0, err) < 0) |
| 1926 | packet_disconnect("Could not create socket pairs: %.100s", |
| 1927 | strerror(errno)); |
| 1928 | #endif /* USE_PIPES */ |
| 1929 | |
| 1930 | setproctitle("%s@notty", pw->pw_name); |
| 1931 | |
| 1932 | /* Fork the child. */ |
| 1933 | if ((pid = fork()) == 0) { |
| 1934 | /* Child. Reinitialize the log since the pid has changed. */ |
| 1935 | log_init(av0, options.log_level, options.log_facility, log_stderr); |
| 1936 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1937 | /* |
| 1938 | * Create a new session and process group since the 4.4BSD |
| 1939 | * setlogin() affects the entire process group. |
| 1940 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1941 | if (setsid() < 0) |
| 1942 | error("setsid failed: %.100s", strerror(errno)); |
| 1943 | |
| 1944 | #ifdef USE_PIPES |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1945 | /* |
| 1946 | * Redirect stdin. We close the parent side of the socket |
| 1947 | * pair, and make the child side the standard input. |
| 1948 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1949 | close(pin[1]); |
| 1950 | if (dup2(pin[0], 0) < 0) |
| 1951 | perror("dup2 stdin"); |
| 1952 | close(pin[0]); |
| 1953 | |
| 1954 | /* Redirect stdout. */ |
| 1955 | close(pout[0]); |
| 1956 | if (dup2(pout[1], 1) < 0) |
| 1957 | perror("dup2 stdout"); |
| 1958 | close(pout[1]); |
| 1959 | |
| 1960 | /* Redirect stderr. */ |
| 1961 | close(perr[0]); |
| 1962 | if (dup2(perr[1], 2) < 0) |
| 1963 | perror("dup2 stderr"); |
| 1964 | close(perr[1]); |
| 1965 | #else /* USE_PIPES */ |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1966 | /* |
| 1967 | * Redirect stdin, stdout, and stderr. Stdin and stdout will |
| 1968 | * use the same socket, as some programs (particularly rdist) |
| 1969 | * seem to depend on it. |
| 1970 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1971 | close(inout[1]); |
| 1972 | close(err[1]); |
| 1973 | if (dup2(inout[0], 0) < 0) /* stdin */ |
| 1974 | perror("dup2 stdin"); |
| 1975 | if (dup2(inout[0], 1) < 0) /* stdout. Note: same socket as stdin. */ |
| 1976 | perror("dup2 stdout"); |
| 1977 | if (dup2(err[0], 2) < 0) /* stderr */ |
| 1978 | perror("dup2 stderr"); |
| 1979 | #endif /* USE_PIPES */ |
| 1980 | |
| 1981 | /* Do processing for the child (exec command etc). */ |
| 1982 | do_child(command, pw, NULL, display, auth_proto, auth_data, NULL); |
| 1983 | /* NOTREACHED */ |
| 1984 | } |
| 1985 | if (pid < 0) |
| 1986 | packet_disconnect("fork failed: %.100s", strerror(errno)); |
| 1987 | #ifdef USE_PIPES |
| 1988 | /* We are the parent. Close the child sides of the pipes. */ |
| 1989 | close(pin[0]); |
| 1990 | close(pout[1]); |
| 1991 | close(perr[1]); |
| 1992 | |
| 1993 | /* Enter the interactive session. */ |
| 1994 | server_loop(pid, pin[1], pout[0], perr[0]); |
| 1995 | /* server_loop has closed pin[1], pout[1], and perr[1]. */ |
| 1996 | #else /* USE_PIPES */ |
| 1997 | /* We are the parent. Close the child sides of the socket pairs. */ |
| 1998 | close(inout[0]); |
| 1999 | close(err[0]); |
| 2000 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 2001 | /* |
| 2002 | * Enter the interactive session. Note: server_loop must be able to |
| 2003 | * handle the case that fdin and fdout are the same. |
| 2004 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2005 | server_loop(pid, inout[1], inout[1], err[1]); |
| 2006 | /* server_loop has closed inout[1] and err[1]. */ |
| 2007 | #endif /* USE_PIPES */ |
| 2008 | } |
| 2009 | |
| 2010 | struct pty_cleanup_context { |
| 2011 | const char *ttyname; |
| 2012 | int pid; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2013 | }; |
| 2014 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2015 | /* |
| 2016 | * Function to perform cleanup if we get aborted abnormally (e.g., due to a |
| 2017 | * dropped connection). |
| 2018 | */ |
| 2019 | void |
| 2020 | pty_cleanup_proc(void *context) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2021 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2022 | struct pty_cleanup_context *cu = context; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2023 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2024 | debug("pty_cleanup_proc called"); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2025 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2026 | /* Record that the user has logged out. */ |
| 2027 | record_logout(cu->pid, cu->ttyname); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2028 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2029 | /* Release the pseudo-tty. */ |
| 2030 | pty_release(cu->ttyname); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2031 | } |
| 2032 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2033 | /* |
| 2034 | * This is called to fork and execute a command when we have a tty. This |
| 2035 | * will call do_child from the child, and server_loop from the parent after |
| 2036 | * setting up file descriptors, controlling tty, updating wtmp, utmp, |
| 2037 | * lastlog, and other such operations. |
| 2038 | */ |
| 2039 | void |
| 2040 | do_exec_pty(const char *command, int ptyfd, int ttyfd, |
| 2041 | const char *ttyname, struct passwd * pw, const char *term, |
| 2042 | const char *display, const char *auth_proto, |
| 2043 | const char *auth_data) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2044 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2045 | int pid, fdout; |
| 2046 | const char *hostname; |
| 2047 | time_t last_login_time; |
| 2048 | char buf[100], *time_string; |
| 2049 | FILE *f; |
| 2050 | char line[256]; |
| 2051 | struct stat st; |
| 2052 | int quiet_login; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 2053 | struct sockaddr_storage from; |
| 2054 | socklen_t fromlen; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2055 | struct pty_cleanup_context cleanup_context; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2056 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2057 | /* Get remote host name. */ |
| 2058 | hostname = get_canonical_hostname(); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2059 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 2060 | /* |
| 2061 | * Get the time when the user last logged in. Buf will be set to |
| 2062 | * contain the hostname the last login was from. |
| 2063 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2064 | if (!options.use_login) { |
| 2065 | last_login_time = get_last_login_time(pw->pw_uid, pw->pw_name, |
| 2066 | buf, sizeof(buf)); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2067 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2068 | setproctitle("%s@%s", pw->pw_name, strrchr(ttyname, '/') + 1); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2069 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2070 | /* Fork the child. */ |
| 2071 | if ((pid = fork()) == 0) { |
| 2072 | pid = getpid(); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2073 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2074 | /* Child. Reinitialize the log because the pid has |
| 2075 | changed. */ |
| 2076 | log_init(av0, options.log_level, options.log_facility, log_stderr); |
| 2077 | |
| 2078 | /* Close the master side of the pseudo tty. */ |
| 2079 | close(ptyfd); |
| 2080 | |
| 2081 | /* Make the pseudo tty our controlling tty. */ |
| 2082 | pty_make_controlling_tty(&ttyfd, ttyname); |
| 2083 | |
| 2084 | /* Redirect stdin from the pseudo tty. */ |
| 2085 | if (dup2(ttyfd, fileno(stdin)) < 0) |
| 2086 | error("dup2 stdin failed: %.100s", strerror(errno)); |
| 2087 | |
| 2088 | /* Redirect stdout to the pseudo tty. */ |
| 2089 | if (dup2(ttyfd, fileno(stdout)) < 0) |
| 2090 | error("dup2 stdin failed: %.100s", strerror(errno)); |
| 2091 | |
| 2092 | /* Redirect stderr to the pseudo tty. */ |
| 2093 | if (dup2(ttyfd, fileno(stderr)) < 0) |
| 2094 | error("dup2 stdin failed: %.100s", strerror(errno)); |
| 2095 | |
| 2096 | /* Close the extra descriptor for the pseudo tty. */ |
| 2097 | close(ttyfd); |
| 2098 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 2099 | /* |
| 2100 | * Get IP address of client. This is needed because we want |
| 2101 | * to record where the user logged in from. If the |
| 2102 | * connection is not a socket, let the ip address be 0.0.0.0. |
| 2103 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2104 | memset(&from, 0, sizeof(from)); |
| 2105 | if (packet_get_connection_in() == packet_get_connection_out()) { |
| 2106 | fromlen = sizeof(from); |
| 2107 | if (getpeername(packet_get_connection_in(), |
| 2108 | (struct sockaddr *) & from, &fromlen) < 0) { |
| 2109 | debug("getpeername: %.100s", strerror(errno)); |
| 2110 | fatal_cleanup(); |
| 2111 | } |
| 2112 | } |
| 2113 | /* Record that there was a login on that terminal. */ |
| 2114 | record_login(pid, ttyname, pw->pw_name, pw->pw_uid, hostname, |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 2115 | (struct sockaddr *)&from); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2116 | |
| 2117 | /* Check if .hushlogin exists. */ |
| 2118 | snprintf(line, sizeof line, "%.200s/.hushlogin", pw->pw_dir); |
| 2119 | quiet_login = stat(line, &st) >= 0; |
Damien Miller | 356a0b0 | 1999-11-08 15:30:59 +1100 | [diff] [blame] | 2120 | |
Damien Miller | beb4ba5 | 1999-12-28 15:09:35 +1100 | [diff] [blame] | 2121 | #ifdef USE_PAM |
Damien Miller | e72b7af | 1999-12-30 15:08:44 +1100 | [diff] [blame] | 2122 | if (!quiet_login) |
| 2123 | print_pam_messages(); |
| 2124 | #endif /* USE_PAM */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2125 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 2126 | /* |
| 2127 | * If the user has logged in before, display the time of last |
| 2128 | * login. However, don't display anything extra if a command |
| 2129 | * has been specified (so that ssh can be used to execute |
| 2130 | * commands on a remote machine without users knowing they |
| 2131 | * are going to another machine). Login(1) will do this for |
| 2132 | * us as well, so check if login(1) is used |
| 2133 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2134 | if (command == NULL && last_login_time != 0 && !quiet_login && |
| 2135 | !options.use_login) { |
| 2136 | /* Convert the date to a string. */ |
| 2137 | time_string = ctime(&last_login_time); |
| 2138 | /* Remove the trailing newline. */ |
| 2139 | if (strchr(time_string, '\n')) |
| 2140 | *strchr(time_string, '\n') = 0; |
| 2141 | /* Display the last login time. Host if displayed |
| 2142 | if known. */ |
| 2143 | if (strcmp(buf, "") == 0) |
| 2144 | printf("Last login: %s\r\n", time_string); |
| 2145 | else |
| 2146 | printf("Last login: %s from %s\r\n", time_string, buf); |
| 2147 | } |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 2148 | /* |
| 2149 | * Print /etc/motd unless a command was specified or printing |
| 2150 | * it was disabled in server options or login(1) will be |
| 2151 | * used. Note that some machines appear to print it in |
| 2152 | * /etc/profile or similar. |
| 2153 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2154 | if (command == NULL && options.print_motd && !quiet_login && |
| 2155 | !options.use_login) { |
| 2156 | /* Print /etc/motd if it exists. */ |
| 2157 | f = fopen("/etc/motd", "r"); |
| 2158 | if (f) { |
| 2159 | while (fgets(line, sizeof(line), f)) |
| 2160 | fputs(line, stdout); |
| 2161 | fclose(f); |
| 2162 | } |
| 2163 | } |
| 2164 | /* Do common processing for the child, such as execing the command. */ |
| 2165 | do_child(command, pw, term, display, auth_proto, auth_data, ttyname); |
| 2166 | /* NOTREACHED */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2167 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2168 | if (pid < 0) |
| 2169 | packet_disconnect("fork failed: %.100s", strerror(errno)); |
| 2170 | /* Parent. Close the slave side of the pseudo tty. */ |
| 2171 | close(ttyfd); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2172 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 2173 | /* |
| 2174 | * Create another descriptor of the pty master side for use as the |
| 2175 | * standard input. We could use the original descriptor, but this |
| 2176 | * simplifies code in server_loop. The descriptor is bidirectional. |
| 2177 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2178 | fdout = dup(ptyfd); |
| 2179 | if (fdout < 0) |
| 2180 | packet_disconnect("dup failed: %.100s", strerror(errno)); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2181 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 2182 | /* |
| 2183 | * Add a cleanup function to clear the utmp entry and record logout |
| 2184 | * time in case we call fatal() (e.g., the connection gets closed). |
| 2185 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2186 | cleanup_context.pid = pid; |
| 2187 | cleanup_context.ttyname = ttyname; |
| 2188 | fatal_add_cleanup(pty_cleanup_proc, (void *) &cleanup_context); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2189 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2190 | /* Enter interactive session. */ |
| 2191 | server_loop(pid, ptyfd, fdout, -1); |
| 2192 | /* server_loop has not closed ptyfd and fdout. */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2193 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2194 | /* Cancel the cleanup function. */ |
| 2195 | fatal_remove_cleanup(pty_cleanup_proc, (void *) &cleanup_context); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2196 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2197 | /* Record that the user has logged out. */ |
| 2198 | record_logout(pid, ttyname); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2199 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2200 | /* Release the pseudo-tty. */ |
| 2201 | pty_release(ttyname); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2202 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 2203 | /* |
| 2204 | * Close the server side of the socket pairs. We must do this after |
| 2205 | * the pty cleanup, so that another process doesn't get this pty |
| 2206 | * while we're still cleaning up. |
| 2207 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2208 | close(ptyfd); |
| 2209 | close(fdout); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2210 | } |
| 2211 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2212 | /* |
| 2213 | * Sets the value of the given variable in the environment. If the variable |
| 2214 | * already exists, its value is overriden. |
| 2215 | */ |
| 2216 | void |
| 2217 | child_set_env(char ***envp, unsigned int *envsizep, const char *name, |
| 2218 | const char *value) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2219 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2220 | unsigned int i, namelen; |
| 2221 | char **env; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2222 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 2223 | /* |
| 2224 | * Find the slot where the value should be stored. If the variable |
| 2225 | * already exists, we reuse the slot; otherwise we append a new slot |
| 2226 | * at the end of the array, expanding if necessary. |
| 2227 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2228 | env = *envp; |
| 2229 | namelen = strlen(name); |
| 2230 | for (i = 0; env[i]; i++) |
| 2231 | if (strncmp(env[i], name, namelen) == 0 && env[i][namelen] == '=') |
| 2232 | break; |
| 2233 | if (env[i]) { |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 2234 | /* Reuse the slot. */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2235 | xfree(env[i]); |
| 2236 | } else { |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 2237 | /* New variable. Expand if necessary. */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2238 | if (i >= (*envsizep) - 1) { |
| 2239 | (*envsizep) += 50; |
| 2240 | env = (*envp) = xrealloc(env, (*envsizep) * sizeof(char *)); |
| 2241 | } |
| 2242 | /* Need to set the NULL pointer at end of array beyond the new slot. */ |
| 2243 | env[i + 1] = NULL; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2244 | } |
| 2245 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2246 | /* Allocate space and format the variable in the appropriate slot. */ |
| 2247 | env[i] = xmalloc(strlen(name) + 1 + strlen(value) + 1); |
| 2248 | snprintf(env[i], strlen(name) + 1 + strlen(value) + 1, "%s=%s", name, value); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2249 | } |
| 2250 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2251 | /* |
| 2252 | * Reads environment variables from the given file and adds/overrides them |
| 2253 | * into the environment. If the file does not exist, this does nothing. |
| 2254 | * Otherwise, it must consist of empty lines, comments (line starts with '#') |
| 2255 | * and assignments of the form name=value. No other forms are allowed. |
| 2256 | */ |
| 2257 | void |
| 2258 | read_environment_file(char ***env, unsigned int *envsize, |
| 2259 | const char *filename) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2260 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2261 | FILE *f; |
| 2262 | char buf[4096]; |
| 2263 | char *cp, *value; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2264 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2265 | f = fopen(filename, "r"); |
| 2266 | if (!f) |
| 2267 | return; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2268 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2269 | while (fgets(buf, sizeof(buf), f)) { |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 2270 | for (cp = buf; *cp == ' ' || *cp == '\t'; cp++) |
| 2271 | ; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2272 | if (!*cp || *cp == '#' || *cp == '\n') |
| 2273 | continue; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2274 | if (strchr(cp, '\n')) |
| 2275 | *strchr(cp, '\n') = '\0'; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2276 | value = strchr(cp, '='); |
| 2277 | if (value == NULL) { |
| 2278 | fprintf(stderr, "Bad line in %.100s: %.200s\n", filename, buf); |
| 2279 | continue; |
| 2280 | } |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 2281 | /* Replace the equals sign by nul, and advance value to the value string. */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2282 | *value = '\0'; |
| 2283 | value++; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2284 | child_set_env(env, envsize, cp, value); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2285 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2286 | fclose(f); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2287 | } |
| 2288 | |
Damien Miller | e72b7af | 1999-12-30 15:08:44 +1100 | [diff] [blame] | 2289 | #ifdef USE_PAM |
| 2290 | /* |
| 2291 | * Sets any environment variables which have been specified by PAM |
| 2292 | */ |
| 2293 | void do_pam_environment(char ***env, int *envsize) |
| 2294 | { |
| 2295 | char *equals, var_name[512], var_val[512]; |
| 2296 | char **pam_env; |
| 2297 | int i; |
| 2298 | |
| 2299 | if ((pam_env = fetch_pam_environment()) == NULL) |
| 2300 | return; |
| 2301 | |
| 2302 | for(i = 0; pam_env[i] != NULL; i++) { |
| 2303 | if ((equals = strstr(pam_env[i], "=")) == NULL) |
| 2304 | continue; |
| 2305 | |
| 2306 | if (strlen(pam_env[i]) < (sizeof(var_name) - 1)) |
| 2307 | { |
| 2308 | memset(var_name, '\0', sizeof(var_name)); |
| 2309 | memset(var_val, '\0', sizeof(var_val)); |
| 2310 | |
| 2311 | strncpy(var_name, pam_env[i], equals - pam_env[i]); |
| 2312 | strcpy(var_val, equals + 1); |
| 2313 | |
| 2314 | debug("PAM environment: %s=%s", var_name, var_val); |
| 2315 | |
| 2316 | child_set_env(env, envsize, var_name, var_val); |
| 2317 | } |
| 2318 | } |
| 2319 | } |
| 2320 | #endif /* USE_PAM */ |
| 2321 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2322 | /* |
| 2323 | * Performs common processing for the child, such as setting up the |
| 2324 | * environment, closing extra file descriptors, setting the user and group |
| 2325 | * ids, and executing the command or shell. |
| 2326 | */ |
| 2327 | void |
| 2328 | do_child(const char *command, struct passwd * pw, const char *term, |
| 2329 | const char *display, const char *auth_proto, |
| 2330 | const char *auth_data, const char *ttyname) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2331 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2332 | const char *shell, *cp = NULL; |
| 2333 | char buf[256]; |
| 2334 | FILE *f; |
| 2335 | unsigned int envsize, i; |
| 2336 | char **env; |
| 2337 | extern char **environ; |
| 2338 | struct stat st; |
| 2339 | char *argv[10]; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2340 | |
Damien Miller | beb4ba5 | 1999-12-28 15:09:35 +1100 | [diff] [blame] | 2341 | #ifndef USE_PAM /* pam_nologin handles this */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2342 | /* Check /etc/nologin. */ |
| 2343 | f = fopen("/etc/nologin", "r"); |
| 2344 | if (f) { |
| 2345 | /* /etc/nologin exists. Print its contents and exit. */ |
| 2346 | while (fgets(buf, sizeof(buf), f)) |
| 2347 | fputs(buf, stderr); |
| 2348 | fclose(f); |
| 2349 | if (pw->pw_uid != 0) |
| 2350 | exit(254); |
| 2351 | } |
Damien Miller | beb4ba5 | 1999-12-28 15:09:35 +1100 | [diff] [blame] | 2352 | #endif /* USE_PAM */ |
Damien Miller | 776af5d | 1999-11-12 08:49:09 +1100 | [diff] [blame] | 2353 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2354 | /* Set login name in the kernel. */ |
| 2355 | if (setlogin(pw->pw_name) < 0) |
| 2356 | error("setlogin failed: %s", strerror(errno)); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2357 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2358 | /* Set uid, gid, and groups. */ |
| 2359 | /* Login(1) does this as well, and it needs uid 0 for the "-h" |
| 2360 | switch, so we let login(1) to this for us. */ |
| 2361 | if (!options.use_login) { |
| 2362 | if (getuid() == 0 || geteuid() == 0) { |
| 2363 | if (setgid(pw->pw_gid) < 0) { |
| 2364 | perror("setgid"); |
| 2365 | exit(1); |
| 2366 | } |
| 2367 | /* Initialize the group list. */ |
| 2368 | if (initgroups(pw->pw_name, pw->pw_gid) < 0) { |
| 2369 | perror("initgroups"); |
| 2370 | exit(1); |
| 2371 | } |
| 2372 | endgrent(); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2373 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2374 | /* Permanently switch to the desired uid. */ |
| 2375 | permanently_set_uid(pw->pw_uid); |
| 2376 | } |
| 2377 | if (getuid() != pw->pw_uid || geteuid() != pw->pw_uid) |
| 2378 | fatal("Failed to set uids to %d.", (int) pw->pw_uid); |
| 2379 | } |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 2380 | /* |
| 2381 | * Get the shell from the password data. An empty shell field is |
| 2382 | * legal, and means /bin/sh. |
| 2383 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2384 | shell = (pw->pw_shell[0] == '\0') ? _PATH_BSHELL : pw->pw_shell; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2385 | |
| 2386 | #ifdef AFS |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2387 | /* Try to get AFS tokens for the local cell. */ |
| 2388 | if (k_hasafs()) { |
| 2389 | char cell[64]; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2390 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2391 | if (k_afs_cell_of_file(pw->pw_dir, cell, sizeof(cell)) == 0) |
| 2392 | krb_afslog(cell, 0); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2393 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2394 | krb_afslog(0, 0); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2395 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2396 | #endif /* AFS */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2397 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 2398 | /* Initialize the environment. */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2399 | envsize = 100; |
| 2400 | env = xmalloc(envsize * sizeof(char *)); |
| 2401 | env[0] = NULL; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2402 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2403 | if (!options.use_login) { |
| 2404 | /* Set basic environment. */ |
| 2405 | child_set_env(&env, &envsize, "USER", pw->pw_name); |
| 2406 | child_set_env(&env, &envsize, "LOGNAME", pw->pw_name); |
| 2407 | child_set_env(&env, &envsize, "HOME", pw->pw_dir); |
| 2408 | child_set_env(&env, &envsize, "PATH", _PATH_STDPATH); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2409 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2410 | snprintf(buf, sizeof buf, "%.200s/%.50s", |
| 2411 | _PATH_MAILDIR, pw->pw_name); |
| 2412 | child_set_env(&env, &envsize, "MAIL", buf); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2413 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2414 | /* Normal systems set SHELL by default. */ |
| 2415 | child_set_env(&env, &envsize, "SHELL", shell); |
| 2416 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2417 | if (getenv("TZ")) |
| 2418 | child_set_env(&env, &envsize, "TZ", getenv("TZ")); |
| 2419 | |
| 2420 | /* Set custom environment options from RSA authentication. */ |
| 2421 | while (custom_environment) { |
| 2422 | struct envstring *ce = custom_environment; |
| 2423 | char *s = ce->s; |
| 2424 | int i; |
| 2425 | for (i = 0; s[i] != '=' && s[i]; i++); |
| 2426 | if (s[i] == '=') { |
| 2427 | s[i] = 0; |
| 2428 | child_set_env(&env, &envsize, s, s + i + 1); |
| 2429 | } |
| 2430 | custom_environment = ce->next; |
| 2431 | xfree(ce->s); |
| 2432 | xfree(ce); |
| 2433 | } |
| 2434 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2435 | snprintf(buf, sizeof buf, "%.50s %d %d", |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 2436 | get_remote_ipaddr(), get_remote_port(), get_local_port()); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2437 | child_set_env(&env, &envsize, "SSH_CLIENT", buf); |
| 2438 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2439 | if (ttyname) |
| 2440 | child_set_env(&env, &envsize, "SSH_TTY", ttyname); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2441 | if (term) |
| 2442 | child_set_env(&env, &envsize, "TERM", term); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2443 | if (display) |
| 2444 | child_set_env(&env, &envsize, "DISPLAY", display); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2445 | |
Damien Miller | 1fa154b | 2000-01-23 10:32:03 +1100 | [diff] [blame] | 2446 | #ifdef _AIX |
| 2447 | { |
| 2448 | char *authstate,*krb5cc; |
| 2449 | |
| 2450 | if ((authstate = getenv("AUTHSTATE")) != NULL) |
| 2451 | child_set_env(&env,&envsize,"AUTHSTATE",authstate); |
| 2452 | |
| 2453 | if ((krb5cc = getenv("KRB5CCNAME")) != NULL) |
| 2454 | child_set_env(&env,&envsize,"KRB5CCNAME",krb5cc); |
| 2455 | } |
| 2456 | #endif |
| 2457 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2458 | #ifdef KRB4 |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2459 | { |
| 2460 | extern char *ticket; |
| 2461 | |
| 2462 | if (ticket) |
| 2463 | child_set_env(&env, &envsize, "KRBTKFILE", ticket); |
| 2464 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2465 | #endif /* KRB4 */ |
| 2466 | |
Damien Miller | beb4ba5 | 1999-12-28 15:09:35 +1100 | [diff] [blame] | 2467 | #ifdef USE_PAM |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2468 | /* Pull in any environment variables that may have been set by PAM. */ |
Damien Miller | e72b7af | 1999-12-30 15:08:44 +1100 | [diff] [blame] | 2469 | do_pam_environment(&env, &envsize); |
Damien Miller | beb4ba5 | 1999-12-28 15:09:35 +1100 | [diff] [blame] | 2470 | #endif /* USE_PAM */ |
Damien Miller | 9438816 | 1999-10-29 09:57:31 +1000 | [diff] [blame] | 2471 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2472 | if (xauthfile) |
| 2473 | child_set_env(&env, &envsize, "XAUTHORITY", xauthfile); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2474 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2475 | if (auth_get_socket_name() != NULL) |
| 2476 | child_set_env(&env, &envsize, SSH_AUTHSOCKET_ENV_NAME, |
| 2477 | auth_get_socket_name()); |
Damien Miller | 776af5d | 1999-11-12 08:49:09 +1100 | [diff] [blame] | 2478 | |
Damien Miller | 1fa154b | 2000-01-23 10:32:03 +1100 | [diff] [blame] | 2479 | read_environment_file(&env,&envsize,"/etc/environment"); |
| 2480 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 2481 | /* read $HOME/.ssh/environment. */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2482 | if (!options.use_login) { |
| 2483 | snprintf(buf, sizeof buf, "%.200s/.ssh/environment", pw->pw_dir); |
| 2484 | read_environment_file(&env, &envsize, buf); |
| 2485 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2486 | if (debug_flag) { |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 2487 | /* dump the environment */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2488 | fprintf(stderr, "Environment:\n"); |
| 2489 | for (i = 0; env[i]; i++) |
| 2490 | fprintf(stderr, " %.200s\n", env[i]); |
| 2491 | } |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 2492 | /* |
| 2493 | * Close the connection descriptors; note that this is the child, and |
| 2494 | * the server will still have the socket open, and it is important |
| 2495 | * that we do not shutdown it. Note that the descriptors cannot be |
| 2496 | * closed before building the environment, as we call |
| 2497 | * get_remote_ipaddr there. |
| 2498 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2499 | if (packet_get_connection_in() == packet_get_connection_out()) |
| 2500 | close(packet_get_connection_in()); |
| 2501 | else { |
| 2502 | close(packet_get_connection_in()); |
| 2503 | close(packet_get_connection_out()); |
| 2504 | } |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 2505 | /* |
| 2506 | * Close all descriptors related to channels. They will still remain |
| 2507 | * open in the parent. |
| 2508 | */ |
| 2509 | /* XXX better use close-on-exec? -markus */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2510 | channel_close_all(); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2511 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 2512 | /* |
| 2513 | * Close any extra file descriptors. Note that there may still be |
| 2514 | * descriptors left by system functions. They will be closed later. |
| 2515 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2516 | endpwent(); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2517 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 2518 | /* |
| 2519 | * Close any extra open file descriptors so that we don\'t have them |
| 2520 | * hanging around in clients. Note that we want to do this after |
| 2521 | * initgroups, because at least on Solaris 2.3 it leaves file |
| 2522 | * descriptors open. |
| 2523 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2524 | for (i = 3; i < 64; i++) |
| 2525 | close(i); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2526 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2527 | /* Change current directory to the user\'s home directory. */ |
| 2528 | if (chdir(pw->pw_dir) < 0) |
| 2529 | fprintf(stderr, "Could not chdir to home directory %s: %s\n", |
| 2530 | pw->pw_dir, strerror(errno)); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2531 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 2532 | /* |
| 2533 | * Must take new environment into use so that .ssh/rc, /etc/sshrc and |
| 2534 | * xauth are run in the proper environment. |
| 2535 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2536 | environ = env; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2537 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 2538 | /* |
| 2539 | * Run $HOME/.ssh/rc, /etc/sshrc, or xauth (whichever is found first |
| 2540 | * in this order). |
| 2541 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2542 | if (!options.use_login) { |
| 2543 | if (stat(SSH_USER_RC, &st) >= 0) { |
| 2544 | if (debug_flag) |
| 2545 | fprintf(stderr, "Running /bin/sh %s\n", SSH_USER_RC); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2546 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2547 | f = popen("/bin/sh " SSH_USER_RC, "w"); |
| 2548 | if (f) { |
| 2549 | if (auth_proto != NULL && auth_data != NULL) |
| 2550 | fprintf(f, "%s %s\n", auth_proto, auth_data); |
| 2551 | pclose(f); |
| 2552 | } else |
| 2553 | fprintf(stderr, "Could not run %s\n", SSH_USER_RC); |
| 2554 | } else if (stat(SSH_SYSTEM_RC, &st) >= 0) { |
| 2555 | if (debug_flag) |
| 2556 | fprintf(stderr, "Running /bin/sh %s\n", SSH_SYSTEM_RC); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2557 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2558 | f = popen("/bin/sh " SSH_SYSTEM_RC, "w"); |
| 2559 | if (f) { |
| 2560 | if (auth_proto != NULL && auth_data != NULL) |
| 2561 | fprintf(f, "%s %s\n", auth_proto, auth_data); |
| 2562 | pclose(f); |
| 2563 | } else |
| 2564 | fprintf(stderr, "Could not run %s\n", SSH_SYSTEM_RC); |
| 2565 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2566 | #ifdef XAUTH_PATH |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2567 | else { |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 2568 | /* Add authority data to .Xauthority if appropriate. */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2569 | if (auth_proto != NULL && auth_data != NULL) { |
| 2570 | if (debug_flag) |
| 2571 | fprintf(stderr, "Running %.100s add %.100s %.100s %.100s\n", |
| 2572 | XAUTH_PATH, display, auth_proto, auth_data); |
| 2573 | |
| 2574 | f = popen(XAUTH_PATH " -q -", "w"); |
| 2575 | if (f) { |
| 2576 | fprintf(f, "add %s %s %s\n", display, auth_proto, auth_data); |
| 2577 | fclose(f); |
| 2578 | } else |
| 2579 | fprintf(stderr, "Could not run %s -q -\n", XAUTH_PATH); |
| 2580 | } |
| 2581 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2582 | #endif /* XAUTH_PATH */ |
| 2583 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2584 | /* Get the last component of the shell name. */ |
| 2585 | cp = strrchr(shell, '/'); |
| 2586 | if (cp) |
| 2587 | cp++; |
| 2588 | else |
| 2589 | cp = shell; |
| 2590 | } |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 2591 | /* |
| 2592 | * If we have no command, execute the shell. In this case, the shell |
| 2593 | * name to be passed in argv[0] is preceded by '-' to indicate that |
| 2594 | * this is a login shell. |
| 2595 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2596 | if (!command) { |
| 2597 | if (!options.use_login) { |
| 2598 | char buf[256]; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2599 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 2600 | /* |
| 2601 | * Check for mail if we have a tty and it was enabled |
| 2602 | * in server options. |
| 2603 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2604 | if (ttyname && options.check_mail) { |
| 2605 | char *mailbox; |
| 2606 | struct stat mailstat; |
| 2607 | mailbox = getenv("MAIL"); |
| 2608 | if (mailbox != NULL) { |
| 2609 | if (stat(mailbox, &mailstat) != 0 || mailstat.st_size == 0) |
| 2610 | printf("No mail.\n"); |
| 2611 | else if (mailstat.st_mtime < mailstat.st_atime) |
| 2612 | printf("You have mail.\n"); |
| 2613 | else |
| 2614 | printf("You have new mail.\n"); |
| 2615 | } |
| 2616 | } |
| 2617 | /* Start the shell. Set initial character to '-'. */ |
| 2618 | buf[0] = '-'; |
| 2619 | strncpy(buf + 1, cp, sizeof(buf) - 1); |
| 2620 | buf[sizeof(buf) - 1] = 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2621 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2622 | /* Execute the shell. */ |
| 2623 | argv[0] = buf; |
| 2624 | argv[1] = NULL; |
| 2625 | execve(shell, argv, env); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2626 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2627 | /* Executing the shell failed. */ |
| 2628 | perror(shell); |
| 2629 | exit(1); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2630 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2631 | } else { |
| 2632 | /* Launch login(1). */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2633 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2634 | execl(LOGIN_PROGRAM, "login", "-h", get_remote_ipaddr(), |
| 2635 | "-p", "-f", "--", pw->pw_name, NULL); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2636 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2637 | /* Login couldn't be executed, die. */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2638 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2639 | perror("login"); |
| 2640 | exit(1); |
| 2641 | } |
| 2642 | } |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 2643 | /* |
| 2644 | * Execute the command using the user's shell. This uses the -c |
| 2645 | * option to execute the command. |
| 2646 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2647 | argv[0] = (char *) cp; |
| 2648 | argv[1] = "-c"; |
| 2649 | argv[2] = (char *) command; |
| 2650 | argv[3] = NULL; |
| 2651 | execve(shell, argv, env); |
| 2652 | perror(shell); |
| 2653 | exit(1); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2654 | } |