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