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