2 * Author: Tatu Ylonen <ylo@cs.hut.fi>
3 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
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
14 RCSID("$OpenBSD: sshd.c,v 1.79 2000/01/18 13:45:05 markus Exp $");
31 int allow_severity = LOG_INFO;
32 int deny_severity = LOG_WARNING;
39 /* Local Xauthority file. */
40 static char *xauthfile = NULL;
42 /* Server configuration options. */
43 ServerOptions options;
45 /* Name of the server configuration file. */
46 char *config_file_name = SERVER_CONFIG_FILE;
49 * Flag indicating whether IPv4 or IPv6. This can be set on the command line.
50 * Default value is AF_UNSPEC means both IPv4 and IPv6.
53 int IPv4or6 = AF_INET;
55 int IPv4or6 = AF_UNSPEC;
59 * Debug mode flag. This can be set on the command line. If debug
60 * mode is enabled, extra debugging output will be sent to the system
61 * log, the daemon will not go to background, and will exit after processing
62 * the first connection.
66 /* Flag indicating that the daemon is being started from inetd. */
69 /* debug goes to stderr unless inetd_flag is set */
72 /* argv[0] without path. */
75 /* Saved arguments to main(). */
79 * The sockets that the server is listening; this is used in the SIGHUP
82 #define MAX_LISTEN_SOCKS 16
83 int listen_socks[MAX_LISTEN_SOCKS];
84 int num_listen_socks = 0;
87 * the client's version string, passed by sshd2 in compat mode. if != NULL,
88 * sshd will skip the version-number exchange
90 char *client_version_string = NULL;
92 /* Flags set in auth-rsa from authorized_keys flags. These are set in auth-rsa.c. */
93 int no_port_forwarding_flag = 0;
94 int no_agent_forwarding_flag = 0;
95 int no_x11_forwarding_flag = 0;
98 /* RSA authentication "command=" option. */
99 char *forced_command = NULL;
101 /* RSA authentication "environment=" options. */
102 struct envstring *custom_environment = NULL;
104 /* Session id for the current session. */
105 unsigned char session_id[16];
108 * Any really sensitive data in the application is contained in this
109 * structure. The idea is that this structure could be locked into memory so
110 * that the pages do not get written into swap. However, there are some
111 * problems. The private key contains BIGNUMs, and we do not (in principle)
112 * have access to the internals of them, and locking just the structure is
113 * not very useful. Currently, memory locking is not implemented.
116 RSA *private_key; /* Private part of server key. */
117 RSA *host_key; /* Private part of host key. */
121 * Flag indicating whether the current session key has been used. This flag
122 * is set whenever the key is used, and cleared when the key is regenerated.
126 /* This is set to true when SIGHUP is received. */
127 int received_sighup = 0;
129 /* Public side of the server key. This value is regenerated regularly with
133 /* Prototypes for various functions defined later in this file. */
135 void do_authentication();
136 void do_authloop(struct passwd * pw);
137 void do_fake_authloop(char *user);
138 void do_authenticated(struct passwd * pw);
139 void do_exec_pty(const char *command, int ptyfd, int ttyfd,
140 const char *ttyname, struct passwd * pw, const char *term,
141 const char *display, const char *auth_proto,
142 const char *auth_data);
143 void do_exec_no_pty(const char *command, struct passwd * pw,
144 const char *display, const char *auth_proto,
145 const char *auth_data);
146 void do_child(const char *command, struct passwd * pw, const char *term,
147 const char *display, const char *auth_proto,
148 const char *auth_data, const char *ttyname);
151 * Close all listening sockets
154 close_listen_socks(void)
157 for (i = 0; i < num_listen_socks; i++)
158 close(listen_socks[i]);
159 num_listen_socks = -1;
163 * Signal handler for SIGHUP. Sshd execs itself when it receives SIGHUP;
164 * the effect is to reread the configuration file (and to regenerate
168 sighup_handler(int sig)
171 signal(SIGHUP, sighup_handler);
175 * Called from the main program after receiving SIGHUP.
176 * Restarts the server.
181 log("Received SIGHUP; restarting.");
182 close_listen_socks();
183 execv(saved_argv[0], saved_argv);
184 log("RESTART FAILED: av0='%s', error: %s.", av0, strerror(errno));
189 * Generic signal handler for terminating signals in the master daemon.
190 * These close the listen socket; not closing it seems to cause "Address
191 * already in use" problems on some machines, which is inconvenient.
194 sigterm_handler(int sig)
196 log("Received signal %d; terminating.", sig);
197 close_listen_socks();
202 * SIGCHLD handler. This is called whenever a child dies. This will then
203 * reap any zombies left by exited c.
206 main_sigchld_handler(int sig)
208 int save_errno = errno;
211 while (waitpid(-1, &status, WNOHANG) > 0)
214 signal(SIGCHLD, main_sigchld_handler);
219 * Signal handler for the alarm after the login grace period has expired.
222 grace_alarm_handler(int sig)
224 /* Close the connection. */
227 /* Log error and exit. */
228 fatal("Timeout before authentication for %s.", get_remote_ipaddr());
232 * convert ssh auth msg type into description
235 get_authname(int type)
238 case SSH_CMSG_AUTH_PASSWORD:
240 case SSH_CMSG_AUTH_RSA:
242 case SSH_CMSG_AUTH_RHOSTS_RSA:
244 case SSH_CMSG_AUTH_RHOSTS:
247 case SSH_CMSG_AUTH_KERBEROS:
251 case SSH_CMSG_AUTH_TIS_RESPONSE:
255 fatal("get_authname: unknown auth %d: internal error", type);
260 * Signal handler for the key regeneration alarm. Note that this
261 * alarm only occurs in the daemon waiting for connections, and it does not
262 * do anything with the private key or random state before forking.
263 * Thus there should be no concurrency control/asynchronous execution
267 key_regeneration_alarm(int sig)
269 int save_errno = errno;
271 /* Check if we should generate a new key. */
273 /* This should really be done in the background. */
274 log("Generating new %d bit RSA key.", options.server_key_bits);
276 if (sensitive_data.private_key != NULL)
277 RSA_free(sensitive_data.private_key);
278 sensitive_data.private_key = RSA_new();
280 if (public_key != NULL)
281 RSA_free(public_key);
282 public_key = RSA_new();
284 rsa_generate_key(sensitive_data.private_key, public_key,
285 options.server_key_bits);
288 log("RSA key generation complete.");
290 /* Reschedule the alarm. */
291 signal(SIGALRM, key_regeneration_alarm);
292 alarm(options.key_regeneration_time);
297 * Main program for the daemon.
300 main(int ac, char **av)
304 int opt, sock_in = 0, sock_out = 0, newsock, i, fdsetsz, pid, on = 1;
306 int remote_major, remote_minor;
309 struct sockaddr_storage from;
310 char buf[100]; /* Must not be larger than remote_version. */
311 char remote_version[100]; /* Must be at least as big as buf. */
312 const char *remote_ip;
316 struct linger linger;
318 char ntop[NI_MAXHOST], strport[NI_MAXSERV];
319 int listen_sock, maxfd;
323 if (strchr(av[0], '/'))
324 av0 = strrchr(av[0], '/') + 1;
328 /* Initialize configuration options to their default values. */
329 initialize_server_options(&options);
331 /* Parse command-line arguments. */
332 while ((opt = getopt(ac, av, "f:p:b:k:h:g:V:diqQ46")) != EOF) {
341 config_file_name = optarg;
345 options.log_level = SYSLOG_LEVEL_DEBUG;
354 options.log_level = SYSLOG_LEVEL_QUIET;
357 options.server_key_bits = atoi(optarg);
360 options.ports_from_cmdline = 1;
361 if (options.num_ports >= MAX_PORTS)
362 fatal("too many ports.\n");
363 options.ports[options.num_ports++] = atoi(optarg);
366 options.login_grace_time = atoi(optarg);
369 options.key_regeneration_time = atoi(optarg);
372 options.host_key_file = optarg;
375 client_version_string = optarg;
376 /* only makes sense with inetd_flag, i.e. no listen() */
381 fprintf(stderr, "sshd version %s\n", SSH_VERSION);
383 fprintf(stderr, "Compiled with RSAref.\n");
385 fprintf(stderr, "Usage: %s [options]\n", av0);
386 fprintf(stderr, "Options:\n");
387 fprintf(stderr, " -f file Configuration file (default %s)\n", SERVER_CONFIG_FILE);
388 fprintf(stderr, " -d Debugging mode\n");
389 fprintf(stderr, " -i Started from inetd\n");
390 fprintf(stderr, " -q Quiet (no logging)\n");
391 fprintf(stderr, " -p port Listen on the specified port (default: 22)\n");
392 fprintf(stderr, " -k seconds Regenerate server key every this many seconds (default: 3600)\n");
393 fprintf(stderr, " -g seconds Grace period for authentication (default: 300)\n");
394 fprintf(stderr, " -b bits Size of server RSA key (default: 768 bits)\n");
395 fprintf(stderr, " -h file File from which to read host key (default: %s)\n",
397 fprintf(stderr, " -4 Use IPv4 only\n");
398 fprintf(stderr, " -6 Use IPv6 only\n");
404 * Force logging to stderr until we have loaded the private host
405 * key (unless started from inetd)
408 options.log_level == -1 ? SYSLOG_LEVEL_INFO : options.log_level,
409 options.log_facility == -1 ? SYSLOG_FACILITY_AUTH : options.log_facility,
412 /* check if RSA support exists */
413 if (rsa_alive() == 0) {
415 printf("sshd: no RSA support in libssl and libcrypto -- exiting. See ssl(8)\n");
416 log("no RSA support in libssl and libcrypto -- exiting. See ssl(8)");
419 /* Read server configuration options from the configuration file. */
420 read_server_config(&options, config_file_name);
422 /* Fill in default values for those options not explicitly set. */
423 fill_default_server_options(&options);
425 /* Check certain values for sanity. */
426 if (options.server_key_bits < 512 ||
427 options.server_key_bits > 32768) {
428 fprintf(stderr, "Bad server key size.\n");
431 /* Check that there are no remaining arguments. */
433 fprintf(stderr, "Extra argument %s.\n", av[optind]);
437 debug("sshd version %.100s", SSH_VERSION);
439 sensitive_data.host_key = RSA_new();
441 /* Load the host key. It must have empty passphrase. */
442 if (!load_private_key(options.host_key_file, "",
443 sensitive_data.host_key, &comment)) {
444 error("Could not load host key: %.200s: %.100s",
445 options.host_key_file, strerror(errno));
450 /* Initialize the log (it is reinitialized below in case we
452 if (debug_flag && !inetd_flag)
454 log_init(av0, options.log_level, options.log_facility, log_stderr);
456 /* If not in debugging mode, and not started from inetd,
457 disconnect from the controlling terminal, and fork. The
458 original process exits. */
459 if (!debug_flag && !inetd_flag) {
462 #endif /* TIOCNOTTY */
463 if (daemon(0, 0) < 0)
464 fatal("daemon() failed: %.200s", strerror(errno));
466 /* Disconnect from the controlling tty. */
468 fd = open("/dev/tty", O_RDWR | O_NOCTTY);
470 (void) ioctl(fd, TIOCNOTTY, NULL);
473 #endif /* TIOCNOTTY */
475 /* Reinitialize the log (because of the fork above). */
476 log_init(av0, options.log_level, options.log_facility, log_stderr);
478 /* Check that server and host key lengths differ sufficiently.
479 This is necessary to make double encryption work with rsaref.
480 Oh, I hate software patents. I dont know if this can go? Niels */
481 if (options.server_key_bits >
482 BN_num_bits(sensitive_data.host_key->n) - SSH_KEY_BITS_RESERVED &&
483 options.server_key_bits <
484 BN_num_bits(sensitive_data.host_key->n) + SSH_KEY_BITS_RESERVED) {
485 options.server_key_bits =
486 BN_num_bits(sensitive_data.host_key->n) + SSH_KEY_BITS_RESERVED;
487 debug("Forcing server key to %d bits to make it differ from host key.",
488 options.server_key_bits);
490 /* Do not display messages to stdout in RSA code. */
493 /* Initialize the random number generator. */
496 /* Chdir to the root directory so that the current disk can be
497 unmounted if desired. */
500 /* Close connection cleanly after attack. */
501 cipher_attack_detected = packet_disconnect;
503 /* Start listening for a socket, unless started from inetd. */
506 s1 = dup(0); /* Make sure descriptors 0, 1, and 2 are in use. */
510 /* We intentionally do not close the descriptors 0, 1, and 2
511 as our code for setting the descriptors won\'t work
512 if ttyfd happens to be one of those. */
513 debug("inetd sockets after dupping: %d, %d", sock_in, sock_out);
515 public_key = RSA_new();
516 sensitive_data.private_key = RSA_new();
518 log("Generating %d bit RSA key.", options.server_key_bits);
519 rsa_generate_key(sensitive_data.private_key, public_key,
520 options.server_key_bits);
522 log("RSA key generation complete.");
524 for (ai = options.listen_addrs; ai; ai = ai->ai_next) {
525 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
527 if (num_listen_socks >= MAX_LISTEN_SOCKS)
528 fatal("Too many listen sockets. "
529 "Enlarge MAX_LISTEN_SOCKS");
530 if (getnameinfo(ai->ai_addr, ai->ai_addrlen,
531 ntop, sizeof(ntop), strport, sizeof(strport),
532 NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
533 error("getnameinfo failed");
536 /* Create socket for listening. */
537 listen_sock = socket(ai->ai_family, SOCK_STREAM, 0);
538 if (listen_sock < 0) {
539 /* kernel may not support ipv6 */
540 verbose("socket: %.100s", strerror(errno));
543 if (fcntl(listen_sock, F_SETFL, O_NONBLOCK) < 0) {
544 error("listen_sock O_NONBLOCK: %s", strerror(errno));
549 * Set socket options. We try to make the port
550 * reusable and have it close as fast as possible
551 * without waiting in unnecessary wait states on
554 setsockopt(listen_sock, SOL_SOCKET, SO_REUSEADDR,
555 (void *) &on, sizeof(on));
558 setsockopt(listen_sock, SOL_SOCKET, SO_LINGER,
559 (void *) &linger, sizeof(linger));
561 debug("Bind to port %s on %s.", strport, ntop);
563 /* Bind the socket to the desired port. */
564 if (bind(listen_sock, ai->ai_addr, ai->ai_addrlen) < 0) {
565 error("Bind to port %s on %s failed: %.200s.",
566 strport, ntop, strerror(errno));
570 listen_socks[num_listen_socks] = listen_sock;
573 /* Start listening on the port. */
574 log("Server listening on %s port %s.", ntop, strport);
575 if (listen(listen_sock, 5) < 0)
576 fatal("listen: %.100s", strerror(errno));
579 freeaddrinfo(options.listen_addrs);
581 if (!num_listen_socks)
582 fatal("Cannot bind any address.");
586 * Record our pid in /etc/sshd_pid to make it easier
587 * to kill the correct sshd. We don\'t want to do
588 * this before the bind above because the bind will
589 * fail if there already is a daemon, and this will
590 * overwrite any old pid in the file.
592 f = fopen(SSH_DAEMON_PID_FILE, "w");
594 fprintf(f, "%u\n", (unsigned int) getpid());
599 public_key = RSA_new();
600 sensitive_data.private_key = RSA_new();
602 log("Generating %d bit RSA key.", options.server_key_bits);
603 rsa_generate_key(sensitive_data.private_key, public_key,
604 options.server_key_bits);
606 log("RSA key generation complete.");
608 /* Schedule server key regeneration alarm. */
609 signal(SIGALRM, key_regeneration_alarm);
610 alarm(options.key_regeneration_time);
612 /* Arrange to restart on SIGHUP. The handler needs listen_sock. */
613 signal(SIGHUP, sighup_handler);
614 signal(SIGTERM, sigterm_handler);
615 signal(SIGQUIT, sigterm_handler);
617 /* Arrange SIGCHLD to be caught. */
618 signal(SIGCHLD, main_sigchld_handler);
620 /* setup fd set for listen */
622 for (i = 0; i < num_listen_socks; i++)
623 if (listen_socks[i] > maxfd)
624 maxfd = listen_socks[i];
625 fdsetsz = howmany(maxfd, NFDBITS) * sizeof(fd_mask);
626 fdset = (fd_set *)xmalloc(fdsetsz);
629 * Stay listening for connections until the system crashes or
630 * the daemon is killed with a signal.
635 /* Wait in select until there is a connection. */
636 memset(fdset, 0, fdsetsz);
637 for (i = 0; i < num_listen_socks; i++)
638 FD_SET(listen_socks[i], fdset);
639 if (select(maxfd + 1, fdset, NULL, NULL, NULL) < 0) {
641 error("select: %.100s", strerror(errno));
644 for (i = 0; i < num_listen_socks; i++) {
645 if (!FD_ISSET(listen_socks[i], fdset))
647 fromlen = sizeof(from);
648 newsock = accept(listen_socks[i], (struct sockaddr *)&from,
651 if (errno != EINTR && errno != EWOULDBLOCK)
652 error("accept: %.100s", strerror(errno));
655 if (fcntl(newsock, F_SETFL, 0) < 0) {
656 error("newsock del O_NONBLOCK: %s", strerror(errno));
660 * Got connection. Fork a child to handle it, unless
661 * we are in debugging mode.
665 * In debugging mode. Close the listening
666 * socket, and start processing the
667 * connection without forking.
669 debug("Server will not fork when running in debugging mode.");
670 close_listen_socks();
677 * Normal production daemon. Fork, and have
678 * the child process the connection. The
679 * parent continues listening.
681 if ((pid = fork()) == 0) {
683 * Child. Close the listening socket, and start using the
684 * accepted socket. Reinitialize logging (since our pid has
685 * changed). We break out of the loop to handle the connection.
687 close_listen_socks();
690 log_init(av0, options.log_level, options.log_facility, log_stderr);
695 /* Parent. Stay in the loop. */
697 error("fork: %.100s", strerror(errno));
699 debug("Forked child %d.", pid);
701 /* Mark that the key has been used (it was "given" to the child). */
706 /* Close the new socket (the child is now taking care of it). */
708 } /* for (i = 0; i < num_listen_socks; i++) */
709 /* child process check (or debug mode) */
710 if (num_listen_socks < 0)
715 /* This is the child processing a new connection. */
718 * Disable the key regeneration alarm. We will not regenerate the
719 * key since we are no longer in a position to give it to anyone. We
720 * will not restart on SIGHUP since it no longer makes sense.
723 signal(SIGALRM, SIG_DFL);
724 signal(SIGHUP, SIG_DFL);
725 signal(SIGTERM, SIG_DFL);
726 signal(SIGQUIT, SIG_DFL);
727 signal(SIGCHLD, SIG_DFL);
730 * Set socket options for the connection. We want the socket to
731 * close as fast as possible without waiting for anything. If the
732 * connection is not a socket, these will do nothing.
734 /* setsockopt(sock_in, SOL_SOCKET, SO_REUSEADDR, (void *)&on, sizeof(on)); */
737 setsockopt(sock_in, SOL_SOCKET, SO_LINGER, (void *) &linger, sizeof(linger));
740 * Register our connection. This turns encryption off because we do
743 packet_set_connection(sock_in, sock_out);
745 remote_port = get_remote_port();
746 remote_ip = get_remote_ipaddr();
748 /* Check whether logins are denied from this host. */
750 /* XXX LIBWRAP noes not know about IPv6 */
752 struct request_info req;
754 request_init(&req, RQ_DAEMON, av0, RQ_FILE, sock_in, NULL);
757 if (!hosts_access(&req)) {
762 /*XXX IPv6 verbose("Connection from %.500s port %d", eval_client(&req), remote_port); */
765 /* Log the connection. */
766 verbose("Connection from %.500s port %d", remote_ip, remote_port);
769 * We don\'t want to listen forever unless the other side
770 * successfully authenticates itself. So we set up an alarm which is
771 * cleared after successful authentication. A limit of zero
772 * indicates no limit. Note that we don\'t set the alarm in debugging
773 * mode; it is just annoying to have the server exit just when you
774 * are about to discover the bug.
776 signal(SIGALRM, grace_alarm_handler);
778 alarm(options.login_grace_time);
780 if (client_version_string != NULL) {
781 /* we are exec'ed by sshd2, so skip exchange of protocol version */
782 strlcpy(buf, client_version_string, sizeof(buf));
784 /* Send our protocol version identification. */
785 snprintf(buf, sizeof buf, "SSH-%d.%d-%.100s\n",
786 PROTOCOL_MAJOR, PROTOCOL_MINOR, SSH_VERSION);
787 if (atomicio(write, sock_out, buf, strlen(buf)) != strlen(buf))
788 fatal("Could not write ident string to %s.", remote_ip);
790 /* Read other side\'s version identification. */
791 for (i = 0; i < sizeof(buf) - 1; i++) {
792 if (read(sock_in, &buf[i], 1) != 1)
793 fatal("Did not receive ident string from %s.", remote_ip);
794 if (buf[i] == '\r') {
799 if (buf[i] == '\n') {
805 buf[sizeof(buf) - 1] = 0;
809 * Check that the versions match. In future this might accept
810 * several versions and set appropriate flags to handle them.
812 if (sscanf(buf, "SSH-%d.%d-%[^\n]\n", &remote_major, &remote_minor,
813 remote_version) != 3) {
814 char *s = "Protocol mismatch.\n";
816 (void) atomicio(write, sock_out, s, strlen(s));
819 fatal("Bad protocol version identification '%.100s' from %s",
822 debug("Client protocol version %d.%d; client software version %.100s",
823 remote_major, remote_minor, remote_version);
824 if (remote_major != PROTOCOL_MAJOR) {
825 char *s = "Protocol major versions differ.\n";
827 (void) atomicio(write, sock_out, s, strlen(s));
830 fatal("Protocol major versions differ for %s: %d vs. %d",
831 remote_ip, PROTOCOL_MAJOR, remote_major);
833 /* Check that the client has sufficiently high software version. */
834 if (remote_major == 1 && remote_minor < 3)
835 packet_disconnect("Your ssh version is too old and is no longer supported. Please install a newer version.");
837 if (remote_major == 1 && remote_minor == 3) {
838 /* note that this disables agent-forwarding */
842 * Check that the connection comes from a privileged port. Rhosts-
843 * and Rhosts-RSA-Authentication only make sense from priviledged
844 * programs. Of course, if the intruder has root access on his local
845 * machine, he can connect from any port. So do not use these
846 * authentication methods from machines that you do not trust.
848 if (remote_port >= IPPORT_RESERVED ||
849 remote_port < IPPORT_RESERVED / 2) {
850 options.rhosts_authentication = 0;
851 options.rhosts_rsa_authentication = 0;
854 if (!packet_connection_is_ipv4() &&
855 options.kerberos_authentication) {
856 debug("Kerberos Authentication disabled, only available for IPv4.");
857 options.kerberos_authentication = 0;
861 packet_set_nonblocking();
863 /* perform the key exchange */
866 /* authenticate user and start session */
870 /* Cleanup user's ticket cache file. */
871 if (options.kerberos_ticket_cleanup)
875 /* Cleanup user's local Xauthority file. */
879 /* The connection has been terminated. */
880 verbose("Closing connection to %.100s", remote_ip);
898 BIGNUM *session_key_int;
899 unsigned char session_key[SSH_SESSION_KEY_LENGTH];
900 unsigned char cookie[8];
901 unsigned int cipher_type, auth_mask, protocol_flags;
905 * Generate check bytes that the client must send back in the user
906 * packet in order for it to be accepted; this is used to defy ip
907 * spoofing attacks. Note that this only works against somebody
908 * doing IP spoofing from a remote machine; any machine on the local
909 * network can still see outgoing packets and catch the random
910 * cookie. This only affects rhosts authentication, and this is one
911 * of the reasons why it is inherently insecure.
913 for (i = 0; i < 8; i++) {
916 cookie[i] = rand & 0xff;
921 * Send our public key. We include in the packet 64 bits of random
922 * data that must be matched in the reply in order to prevent IP
925 packet_start(SSH_SMSG_PUBLIC_KEY);
926 for (i = 0; i < 8; i++)
927 packet_put_char(cookie[i]);
929 /* Store our public server RSA key. */
930 packet_put_int(BN_num_bits(public_key->n));
931 packet_put_bignum(public_key->e);
932 packet_put_bignum(public_key->n);
934 /* Store our public host RSA key. */
935 packet_put_int(BN_num_bits(sensitive_data.host_key->n));
936 packet_put_bignum(sensitive_data.host_key->e);
937 packet_put_bignum(sensitive_data.host_key->n);
939 /* Put protocol flags. */
940 packet_put_int(SSH_PROTOFLAG_HOST_IN_FWD_OPEN);
942 /* Declare which ciphers we support. */
943 packet_put_int(cipher_mask());
945 /* Declare supported authentication types. */
947 if (options.rhosts_authentication)
948 auth_mask |= 1 << SSH_AUTH_RHOSTS;
949 if (options.rhosts_rsa_authentication)
950 auth_mask |= 1 << SSH_AUTH_RHOSTS_RSA;
951 if (options.rsa_authentication)
952 auth_mask |= 1 << SSH_AUTH_RSA;
954 if (options.kerberos_authentication)
955 auth_mask |= 1 << SSH_AUTH_KERBEROS;
958 if (options.kerberos_tgt_passing)
959 auth_mask |= 1 << SSH_PASS_KERBEROS_TGT;
960 if (options.afs_token_passing)
961 auth_mask |= 1 << SSH_PASS_AFS_TOKEN;
964 if (options.skey_authentication == 1)
965 auth_mask |= 1 << SSH_AUTH_TIS;
967 if (options.password_authentication)
968 auth_mask |= 1 << SSH_AUTH_PASSWORD;
969 packet_put_int(auth_mask);
971 /* Send the packet and wait for it to be sent. */
975 debug("Sent %d bit public key and %d bit host key.",
976 BN_num_bits(public_key->n), BN_num_bits(sensitive_data.host_key->n));
978 /* Read clients reply (cipher type and session key). */
979 packet_read_expect(&plen, SSH_CMSG_SESSION_KEY);
981 /* Get cipher type and check whether we accept this. */
982 cipher_type = packet_get_char();
984 if (!(cipher_mask() & (1 << cipher_type)))
985 packet_disconnect("Warning: client selects unsupported cipher.");
987 /* Get check bytes from the packet. These must match those we
988 sent earlier with the public key packet. */
989 for (i = 0; i < 8; i++)
990 if (cookie[i] != packet_get_char())
991 packet_disconnect("IP Spoofing check bytes do not match.");
993 debug("Encryption type: %.200s", cipher_name(cipher_type));
995 /* Get the encrypted integer. */
996 session_key_int = BN_new();
997 packet_get_bignum(session_key_int, &slen);
999 protocol_flags = packet_get_int();
1000 packet_set_protocol_flags(protocol_flags);
1002 packet_integrity_check(plen, 1 + 8 + slen + 4, SSH_CMSG_SESSION_KEY);
1005 * Decrypt it using our private server key and private host key (key
1006 * with larger modulus first).
1008 if (BN_cmp(sensitive_data.private_key->n, sensitive_data.host_key->n) > 0) {
1009 /* Private key has bigger modulus. */
1010 if (BN_num_bits(sensitive_data.private_key->n) <
1011 BN_num_bits(sensitive_data.host_key->n) + SSH_KEY_BITS_RESERVED) {
1012 fatal("do_connection: %s: private_key %d < host_key %d + SSH_KEY_BITS_RESERVED %d",
1013 get_remote_ipaddr(),
1014 BN_num_bits(sensitive_data.private_key->n),
1015 BN_num_bits(sensitive_data.host_key->n),
1016 SSH_KEY_BITS_RESERVED);
1018 rsa_private_decrypt(session_key_int, session_key_int,
1019 sensitive_data.private_key);
1020 rsa_private_decrypt(session_key_int, session_key_int,
1021 sensitive_data.host_key);
1023 /* Host key has bigger modulus (or they are equal). */
1024 if (BN_num_bits(sensitive_data.host_key->n) <
1025 BN_num_bits(sensitive_data.private_key->n) + SSH_KEY_BITS_RESERVED) {
1026 fatal("do_connection: %s: host_key %d < private_key %d + SSH_KEY_BITS_RESERVED %d",
1027 get_remote_ipaddr(),
1028 BN_num_bits(sensitive_data.host_key->n),
1029 BN_num_bits(sensitive_data.private_key->n),
1030 SSH_KEY_BITS_RESERVED);
1032 rsa_private_decrypt(session_key_int, session_key_int,
1033 sensitive_data.host_key);
1034 rsa_private_decrypt(session_key_int, session_key_int,
1035 sensitive_data.private_key);
1038 compute_session_id(session_id, cookie,
1039 sensitive_data.host_key->n,
1040 sensitive_data.private_key->n);
1042 /* Destroy the private and public keys. They will no longer be needed. */
1043 RSA_free(public_key);
1044 RSA_free(sensitive_data.private_key);
1045 RSA_free(sensitive_data.host_key);
1048 * Extract session key from the decrypted integer. The key is in the
1049 * least significant 256 bits of the integer; the first byte of the
1050 * key is in the highest bits.
1052 BN_mask_bits(session_key_int, sizeof(session_key) * 8);
1053 len = BN_num_bytes(session_key_int);
1054 if (len < 0 || len > sizeof(session_key))
1055 fatal("do_connection: bad len from %s: session_key_int %d > sizeof(session_key) %d",
1056 get_remote_ipaddr(),
1057 len, sizeof(session_key));
1058 memset(session_key, 0, sizeof(session_key));
1059 BN_bn2bin(session_key_int, session_key + sizeof(session_key) - len);
1061 /* Destroy the decrypted integer. It is no longer needed. */
1062 BN_clear_free(session_key_int);
1064 /* Xor the first 16 bytes of the session key with the session id. */
1065 for (i = 0; i < 16; i++)
1066 session_key[i] ^= session_id[i];
1068 /* Set the session key. From this on all communications will be encrypted. */
1069 packet_set_encryption_key(session_key, SSH_SESSION_KEY_LENGTH, cipher_type);
1071 /* Destroy our copy of the session key. It is no longer needed. */
1072 memset(session_key, 0, sizeof(session_key));
1074 debug("Received session key; encryption turned on.");
1076 /* Send an acknowledgement packet. Note that this packet is sent encrypted. */
1077 packet_start(SSH_SMSG_SUCCESS);
1079 packet_write_wait();
1084 * Check if the user is allowed to log in via ssh. If user is listed in
1085 * DenyUsers or user's primary group is listed in DenyGroups, false will
1086 * be returned. If AllowUsers isn't empty and user isn't listed there, or
1087 * if AllowGroups isn't empty and user isn't listed there, false will be
1088 * returned. Otherwise true is returned.
1089 * XXX This function should also check if user has a valid shell
1092 allowed_user(struct passwd * pw)
1097 /* Shouldn't be called if pw is NULL, but better safe than sorry... */
1101 /* XXX Should check for valid login shell */
1103 /* Return false if user is listed in DenyUsers */
1104 if (options.num_deny_users > 0) {
1107 for (i = 0; i < options.num_deny_users; i++)
1108 if (match_pattern(pw->pw_name, options.deny_users[i]))
1111 /* Return false if AllowUsers isn't empty and user isn't listed there */
1112 if (options.num_allow_users > 0) {
1115 for (i = 0; i < options.num_allow_users; i++)
1116 if (match_pattern(pw->pw_name, options.allow_users[i]))
1118 /* i < options.num_allow_users iff we break for loop */
1119 if (i >= options.num_allow_users)
1122 /* Get the primary group name if we need it. Return false if it fails */
1123 if (options.num_deny_groups > 0 || options.num_allow_groups > 0) {
1124 grp = getgrgid(pw->pw_gid);
1128 /* Return false if user's group is listed in DenyGroups */
1129 if (options.num_deny_groups > 0) {
1132 for (i = 0; i < options.num_deny_groups; i++)
1133 if (match_pattern(grp->gr_name, options.deny_groups[i]))
1137 * Return false if AllowGroups isn't empty and user's group
1138 * isn't listed there
1140 if (options.num_allow_groups > 0) {
1143 for (i = 0; i < options.num_allow_groups; i++)
1144 if (match_pattern(grp->gr_name, options.allow_groups[i]))
1146 /* i < options.num_allow_groups iff we break for
1148 if (i >= options.num_allow_groups)
1152 /* We found no reason not to let this user try to log on... */
1157 * Performs authentication of an incoming connection. Session key has already
1158 * been exchanged and encryption is enabled.
1163 struct passwd *pw, pwcopy;
1167 /* Get the name of the user that we wish to log in as. */
1168 packet_read_expect(&plen, SSH_CMSG_USER);
1170 /* Get the user name. */
1171 user = packet_get_string(&ulen);
1172 packet_integrity_check(plen, (4 + ulen), SSH_CMSG_USER);
1174 setproctitle("%s", user);
1177 /* If machine has AFS, set process authentication group. */
1184 /* Verify that the user is a valid user. */
1185 pw = getpwnam(user);
1186 if (!pw || !allowed_user(pw))
1187 do_fake_authloop(user);
1189 /* Take a copy of the returned structure. */
1190 memset(&pwcopy, 0, sizeof(pwcopy));
1191 pwcopy.pw_name = xstrdup(pw->pw_name);
1192 pwcopy.pw_passwd = xstrdup(pw->pw_passwd);
1193 pwcopy.pw_uid = pw->pw_uid;
1194 pwcopy.pw_gid = pw->pw_gid;
1195 pwcopy.pw_dir = xstrdup(pw->pw_dir);
1196 pwcopy.pw_shell = xstrdup(pw->pw_shell);
1204 * If we are not running as root, the user must have the same uid as
1207 if (getuid() != 0 && pw->pw_uid != getuid())
1208 packet_disconnect("Cannot change user when server not running as root.");
1210 debug("Attempting authentication for %.100s.", user);
1212 /* If the user has no password, accept authentication immediately. */
1213 if (options.password_authentication &&
1215 (!options.kerberos_authentication || options.kerberos_or_local_passwd) &&
1218 auth_pam_password(pw, "")) {
1220 auth_password(pw, "")) {
1221 #endif /* USE_PAM */
1222 /* Authentication with empty password succeeded. */
1223 log("Login for user %s from %.100s, accepted without authentication.",
1224 pw->pw_name, get_remote_ipaddr());
1226 /* Loop until the user has been authenticated or the
1227 connection is closed, do_authloop() returns only if
1228 authentication is successfull */
1232 /* Check if the user is logging in as root and root logins are disallowed. */
1233 if (pw->pw_uid == 0 && !options.permit_root_login) {
1235 log("Root login accepted for forced command.");
1237 packet_disconnect("ROOT LOGIN REFUSED FROM %.200s",
1238 get_canonical_hostname());
1240 /* The user has been authenticated and accepted. */
1241 packet_start(SSH_SMSG_SUCCESS);
1243 packet_write_wait();
1245 /* Perform session preparation. */
1246 do_authenticated(pw);
1249 #define AUTH_FAIL_MAX 6
1250 #define AUTH_FAIL_LOG (AUTH_FAIL_MAX/2)
1251 #define AUTH_FAIL_MSG "Too many authentication failures for %.100s"
1254 * read packets and try to authenticate local user *pw.
1255 * return if authentication is successfull
1258 do_authloop(struct passwd * pw)
1262 BIGNUM *client_host_key_e, *client_host_key_n;
1264 char *client_user = NULL, *password = NULL;
1266 int plen, dlen, nlen, ulen, elen;
1268 void (*authlog) (const char *fmt,...) = verbose;
1270 /* Indicate that authentication is needed. */
1271 packet_start(SSH_SMSG_FAILURE);
1273 packet_write_wait();
1275 for (attempt = 1;; attempt++) {
1276 int authenticated = 0;
1277 strlcpy(user, "", sizeof user);
1279 /* Get a packet from the client. */
1280 type = packet_read(&plen);
1282 /* Process the packet. */
1285 case SSH_CMSG_HAVE_KERBEROS_TGT:
1286 if (!options.kerberos_tgt_passing) {
1287 /* packet_get_all(); */
1288 verbose("Kerberos tgt passing disabled.");
1291 /* Accept Kerberos tgt. */
1292 char *tgt = packet_get_string(&dlen);
1293 packet_integrity_check(plen, 4 + dlen, type);
1294 if (!auth_kerberos_tgt(pw, tgt))
1295 verbose("Kerberos tgt REFUSED for %s", pw->pw_name);
1300 case SSH_CMSG_HAVE_AFS_TOKEN:
1301 if (!options.afs_token_passing || !k_hasafs()) {
1302 /* packet_get_all(); */
1303 verbose("AFS token passing disabled.");
1306 /* Accept AFS token. */
1307 char *token_string = packet_get_string(&dlen);
1308 packet_integrity_check(plen, 4 + dlen, type);
1309 if (!auth_afs_token(pw, token_string))
1310 verbose("AFS token REFUSED for %s", pw->pw_name);
1311 xfree(token_string);
1316 case SSH_CMSG_AUTH_KERBEROS:
1317 if (!options.kerberos_authentication) {
1318 /* packet_get_all(); */
1319 verbose("Kerberos authentication disabled.");
1322 /* Try Kerberos v4 authentication. */
1324 char *tkt_user = NULL;
1325 char *kdata = packet_get_string((unsigned int *) &auth.length);
1326 packet_integrity_check(plen, 4 + auth.length, type);
1328 if (auth.length < MAX_KTXT_LEN)
1329 memcpy(auth.dat, kdata, auth.length);
1332 authenticated = auth_krb4(pw->pw_name, &auth, &tkt_user);
1334 if (authenticated) {
1335 snprintf(user, sizeof user, " tktuser %s", tkt_user);
1342 case SSH_CMSG_AUTH_RHOSTS:
1343 if (!options.rhosts_authentication) {
1344 verbose("Rhosts authentication disabled.");
1348 * Get client user name. Note that we just have to
1349 * trust the client; this is one reason why rhosts
1350 * authentication is insecure. (Another is
1351 * IP-spoofing on a local network.)
1353 client_user = packet_get_string(&ulen);
1354 packet_integrity_check(plen, 4 + ulen, type);
1356 /* Try to authenticate using /etc/hosts.equiv and
1358 authenticated = auth_rhosts(pw, client_user);
1360 snprintf(user, sizeof user, " ruser %s", client_user);
1363 case SSH_CMSG_AUTH_RHOSTS_RSA:
1364 if (!options.rhosts_rsa_authentication) {
1365 verbose("Rhosts with RSA authentication disabled.");
1369 * Get client user name. Note that we just have to
1370 * trust the client; root on the client machine can
1371 * claim to be any user.
1373 client_user = packet_get_string(&ulen);
1375 /* Get the client host key. */
1376 client_host_key_e = BN_new();
1377 client_host_key_n = BN_new();
1378 bits = packet_get_int();
1379 packet_get_bignum(client_host_key_e, &elen);
1380 packet_get_bignum(client_host_key_n, &nlen);
1382 if (bits != BN_num_bits(client_host_key_n))
1383 error("Warning: keysize mismatch for client_host_key: "
1384 "actual %d, announced %d", BN_num_bits(client_host_key_n), bits);
1385 packet_integrity_check(plen, (4 + ulen) + 4 + elen + nlen, type);
1387 authenticated = auth_rhosts_rsa(pw, client_user,
1388 client_host_key_e, client_host_key_n);
1389 BN_clear_free(client_host_key_e);
1390 BN_clear_free(client_host_key_n);
1392 snprintf(user, sizeof user, " ruser %s", client_user);
1395 case SSH_CMSG_AUTH_RSA:
1396 if (!options.rsa_authentication) {
1397 verbose("RSA authentication disabled.");
1400 /* RSA authentication requested. */
1402 packet_get_bignum(n, &nlen);
1403 packet_integrity_check(plen, nlen, type);
1404 authenticated = auth_rsa(pw, n);
1408 case SSH_CMSG_AUTH_PASSWORD:
1409 if (!options.password_authentication) {
1410 verbose("Password authentication disabled.");
1414 * Read user password. It is in plain text, but was
1415 * transmitted over the encrypted channel so it is
1416 * not visible to an outside observer.
1418 password = packet_get_string(&dlen);
1419 packet_integrity_check(plen, 4 + dlen, type);
1422 /* Do PAM auth with password */
1423 authenticated = auth_pam_password(pw, password);
1425 /* Try authentication with the password. */
1426 authenticated = auth_password(pw, password);
1427 #endif /* USE_PAM */
1428 memset(password, 0, strlen(password));
1433 case SSH_CMSG_AUTH_TIS:
1434 debug("rcvd SSH_CMSG_AUTH_TIS");
1435 if (options.skey_authentication == 1) {
1436 char *skeyinfo = skey_keyinfo(pw->pw_name);
1437 if (skeyinfo == NULL) {
1438 debug("generating fake skeyinfo for %.100s.", pw->pw_name);
1439 skeyinfo = skey_fake_keyinfo(pw->pw_name);
1441 if (skeyinfo != NULL) {
1442 /* we send our s/key- in tis-challenge messages */
1443 debug("sending challenge '%s'", skeyinfo);
1444 packet_start(SSH_SMSG_AUTH_TIS_CHALLENGE);
1445 packet_put_string(skeyinfo, strlen(skeyinfo));
1447 packet_write_wait();
1452 case SSH_CMSG_AUTH_TIS_RESPONSE:
1453 debug("rcvd SSH_CMSG_AUTH_TIS_RESPONSE");
1454 if (options.skey_authentication == 1) {
1455 char *response = packet_get_string(&dlen);
1456 debug("skey response == '%s'", response);
1457 packet_integrity_check(plen, 4 + dlen, type);
1458 authenticated = (skey_haskey(pw->pw_name) == 0 &&
1459 skey_passcheck(pw->pw_name, response) != -1);
1464 case SSH_CMSG_AUTH_TIS:
1465 /* TIS Authentication is unsupported */
1466 log("TIS authentication unsupported.");
1472 * Any unknown messages will be ignored (and failure
1473 * returned) during authentication.
1475 log("Unknown message during authentication: type %d", type);
1479 /* Raise logging level */
1480 if (authenticated ||
1481 attempt == AUTH_FAIL_LOG ||
1482 type == SSH_CMSG_AUTH_PASSWORD)
1485 authlog("%s %s for %.200s from %.200s port %d%s",
1486 authenticated ? "Accepted" : "Failed",
1488 pw->pw_uid == 0 ? "ROOT" : pw->pw_name,
1489 get_remote_ipaddr(),
1493 if (authenticated) {
1495 if (!do_pam_account(pw->pw_name, client_user))
1497 if (client_user != NULL)
1500 do_fake_authloop(pw->pw_name);
1502 #endif /* USE_PAM */
1506 if (client_user != NULL)
1509 if (attempt > AUTH_FAIL_MAX)
1510 packet_disconnect(AUTH_FAIL_MSG, pw->pw_name);
1512 /* Send a message indicating that the authentication attempt failed. */
1513 packet_start(SSH_SMSG_FAILURE);
1515 packet_write_wait();
1520 * The user does not exist or access is denied,
1521 * but fake indication that authentication is needed.
1524 do_fake_authloop(char *user)
1528 log("Faking authloop for illegal user %.200s from %.200s port %d",
1530 get_remote_ipaddr(),
1533 /* Indicate that authentication is needed. */
1534 packet_start(SSH_SMSG_FAILURE);
1536 packet_write_wait();
1539 * Keep reading packets, and always respond with a failure. This is
1540 * to avoid disclosing whether such a user really exists.
1542 for (attempt = 1;; attempt++) {
1543 /* Read a packet. This will not return if the client disconnects. */
1546 (void)packet_read(&plen);
1548 int type = packet_read(&plen);
1550 char *password, *skeyinfo;
1551 /* Try to send a fake s/key challenge. */
1552 if (options.skey_authentication == 1 &&
1553 (skeyinfo = skey_fake_keyinfo(user)) != NULL) {
1554 if (type == SSH_CMSG_AUTH_TIS) {
1555 packet_start(SSH_SMSG_AUTH_TIS_CHALLENGE);
1556 packet_put_string(skeyinfo, strlen(skeyinfo));
1558 packet_write_wait();
1560 } else if (type == SSH_CMSG_AUTH_PASSWORD &&
1561 options.password_authentication &&
1562 (password = packet_get_string(&dlen)) != NULL &&
1564 strncasecmp(password, "s/key", 5) == 0 ) {
1565 packet_send_debug(skeyinfo);
1569 if (attempt > AUTH_FAIL_MAX)
1570 packet_disconnect(AUTH_FAIL_MSG, user);
1573 * Send failure. This should be indistinguishable from a
1574 * failed authentication.
1576 packet_start(SSH_SMSG_FAILURE);
1578 packet_write_wait();
1586 * Remove local Xauthority file.
1589 xauthfile_cleanup_proc(void *ignore)
1591 debug("xauthfile_cleanup_proc called");
1593 if (xauthfile != NULL) {
1601 * Prepares for an interactive session. This is called after the user has
1602 * been successfully authenticated. During this message exchange, pseudo
1603 * terminals are allocated, X11, TCP/IP, and authentication agent forwardings
1604 * are requested, etc.
1607 do_authenticated(struct passwd * pw)
1610 int compression_level = 0, enable_compression_after_reply = 0;
1611 int have_pty = 0, ptyfd = -1, ttyfd = -1, xauthfd = -1;
1612 int row, col, xpixel, ypixel, screen;
1614 char *command, *term = NULL, *display = NULL, *proto = NULL,
1622 * Cancel the alarm we set to limit the time taken for
1628 * Inform the channel mechanism that we are the server side and that
1629 * the client may request to connect to any port at all. (The user
1630 * could do it anyway, and we wouldn\'t know what is permitted except
1631 * by the client telling us, so we can equally well trust the client
1632 * not to request anything bogus.)
1634 channel_permit_all_opens();
1637 * We stay in this loop until the client requests to execute a shell
1643 /* Get a packet from the client. */
1644 type = packet_read(&plen);
1646 /* Process the packet. */
1648 case SSH_CMSG_REQUEST_COMPRESSION:
1649 packet_integrity_check(plen, 4, type);
1650 compression_level = packet_get_int();
1651 if (compression_level < 1 || compression_level > 9) {
1652 packet_send_debug("Received illegal compression level %d.",
1656 /* Enable compression after we have responded with SUCCESS. */
1657 enable_compression_after_reply = 1;
1660 case SSH_CMSG_REQUEST_PTY:
1662 debug("Allocating a pty not permitted for this authentication.");
1666 packet_disconnect("Protocol error: you already have a pty.");
1668 debug("Allocating pty.");
1670 /* Allocate a pty and open it. */
1671 if (!pty_allocate(&ptyfd, &ttyfd, ttyname,
1673 error("Failed to allocate pty.");
1676 /* Determine the group to make the owner of the tty. */
1677 grp = getgrnam("tty");
1679 tty_gid = grp->gr_gid;
1680 tty_mode = S_IRUSR | S_IWUSR | S_IWGRP;
1682 tty_gid = pw->pw_gid;
1683 tty_mode = S_IRUSR | S_IWUSR | S_IWGRP | S_IWOTH;
1686 /* Change ownership of the tty. */
1687 if (chown(ttyname, pw->pw_uid, tty_gid) < 0)
1688 fatal("chown(%.100s, %d, %d) failed: %.100s",
1689 ttyname, pw->pw_uid, tty_gid, strerror(errno));
1690 if (chmod(ttyname, tty_mode) < 0)
1691 fatal("chmod(%.100s, 0%o) failed: %.100s",
1692 ttyname, tty_mode, strerror(errno));
1694 /* Get TERM from the packet. Note that the value may be of arbitrary length. */
1695 term = packet_get_string(&dlen);
1696 packet_integrity_check(dlen, strlen(term), type);
1697 /* packet_integrity_check(plen, 4 + dlen + 4*4 + n_bytes, type); */
1698 /* Remaining bytes */
1699 n_bytes = plen - (4 + dlen + 4 * 4);
1701 if (strcmp(term, "") == 0)
1704 /* Get window size from the packet. */
1705 row = packet_get_int();
1706 col = packet_get_int();
1707 xpixel = packet_get_int();
1708 ypixel = packet_get_int();
1709 pty_change_window_size(ptyfd, row, col, xpixel, ypixel);
1711 /* Get tty modes from the packet. */
1712 tty_parse_modes(ttyfd, &n_bytes);
1713 packet_integrity_check(plen, 4 + dlen + 4 * 4 + n_bytes, type);
1715 /* Indicate that we now have a pty. */
1719 /* do the pam_open_session since we have the pty */
1720 do_pam_session(pw->pw_name, ttyname);
1721 #endif /* USE_PAM */
1725 case SSH_CMSG_X11_REQUEST_FORWARDING:
1726 if (!options.x11_forwarding) {
1727 packet_send_debug("X11 forwarding disabled in server configuration file.");
1731 if (no_x11_forwarding_flag) {
1732 packet_send_debug("X11 forwarding not permitted for this authentication.");
1735 debug("Received request for X11 forwarding with auth spoofing.");
1737 packet_disconnect("Protocol error: X11 display already set.");
1739 int proto_len, data_len;
1740 proto = packet_get_string(&proto_len);
1741 data = packet_get_string(&data_len);
1742 packet_integrity_check(plen, 4 + proto_len + 4 + data_len + 4, type);
1744 if (packet_get_protocol_flags() & SSH_PROTOFLAG_SCREEN_NUMBER)
1745 screen = packet_get_int();
1748 display = x11_create_display_inet(screen, options.x11_display_offset);
1752 /* Setup to always have a local .Xauthority. */
1753 xauthfile = xmalloc(MAXPATHLEN);
1754 snprintf(xauthfile, MAXPATHLEN, "/tmp/XauthXXXXXX");
1756 if ((xauthfd = mkstemp(xauthfile)) != -1) {
1757 fchown(xauthfd, pw->pw_uid, pw->pw_gid);
1759 fatal_add_cleanup(xauthfile_cleanup_proc, NULL);
1765 #else /* XAUTH_PATH */
1766 packet_send_debug("No xauth program; cannot forward with spoofing.");
1768 #endif /* XAUTH_PATH */
1770 case SSH_CMSG_AGENT_REQUEST_FORWARDING:
1771 if (no_agent_forwarding_flag || compat13) {
1772 debug("Authentication agent forwarding not permitted for this authentication.");
1775 debug("Received authentication agent forwarding request.");
1776 auth_input_request_forwarding(pw);
1779 case SSH_CMSG_PORT_FORWARD_REQUEST:
1780 if (no_port_forwarding_flag) {
1781 debug("Port forwarding not permitted for this authentication.");
1784 debug("Received TCP/IP port forwarding request.");
1785 channel_input_port_forward_request(pw->pw_uid == 0);
1788 case SSH_CMSG_MAX_PACKET_SIZE:
1789 if (packet_set_maxsize(packet_get_int()) < 0)
1793 case SSH_CMSG_EXEC_SHELL:
1794 /* Set interactive/non-interactive mode. */
1795 packet_set_interactive(have_pty || display != NULL,
1796 options.keepalives);
1800 #endif /* USE_PAM */
1801 if (forced_command != NULL)
1802 goto do_forced_command;
1803 debug("Forking shell.");
1804 packet_integrity_check(plen, 0, type);
1806 do_exec_pty(NULL, ptyfd, ttyfd, ttyname, pw, term, display, proto, data);
1808 do_exec_no_pty(NULL, pw, display, proto, data);
1811 case SSH_CMSG_EXEC_CMD:
1812 /* Set interactive/non-interactive mode. */
1813 packet_set_interactive(have_pty || display != NULL,
1814 options.keepalives);
1818 #endif /* USE_PAM */
1819 if (forced_command != NULL)
1820 goto do_forced_command;
1821 /* Get command from the packet. */
1824 command = packet_get_string(&dlen);
1825 debug("Executing command '%.500s'", command);
1826 packet_integrity_check(plen, 4 + dlen, type);
1829 do_exec_pty(command, ptyfd, ttyfd, ttyname, pw, term, display, proto, data);
1831 do_exec_no_pty(command, pw, display, proto, data);
1837 * Any unknown messages in this phase are ignored,
1838 * and a failure message is returned.
1840 log("Unknown packet type received after authentication: %d", type);
1844 /* The request was successfully processed. */
1845 packet_start(SSH_SMSG_SUCCESS);
1847 packet_write_wait();
1849 /* Enable compression now that we have replied if appropriate. */
1850 if (enable_compression_after_reply) {
1851 enable_compression_after_reply = 0;
1852 packet_start_compression(compression_level);
1857 /* The request failed. */
1858 packet_start(SSH_SMSG_FAILURE);
1860 packet_write_wait();
1865 * There is a forced command specified for this login.
1868 debug("Executing forced command: %.900s", forced_command);
1870 do_exec_pty(forced_command, ptyfd, ttyfd, ttyname, pw, term, display, proto, data);
1872 do_exec_no_pty(forced_command, pw, display, proto, data);
1878 * This is called to fork and execute a command when we have no tty. This
1879 * will call do_child from the child, and server_loop from the parent after
1880 * setting up file descriptors and such.
1883 do_exec_no_pty(const char *command, struct passwd * pw,
1884 const char *display, const char *auth_proto,
1885 const char *auth_data)
1890 int pin[2], pout[2], perr[2];
1891 /* Allocate pipes for communicating with the program. */
1892 if (pipe(pin) < 0 || pipe(pout) < 0 || pipe(perr) < 0)
1893 packet_disconnect("Could not create pipes: %.100s",
1895 #else /* USE_PIPES */
1896 int inout[2], err[2];
1897 /* Uses socket pairs to communicate with the program. */
1898 if (socketpair(AF_UNIX, SOCK_STREAM, 0, inout) < 0 ||
1899 socketpair(AF_UNIX, SOCK_STREAM, 0, err) < 0)
1900 packet_disconnect("Could not create socket pairs: %.100s",
1902 #endif /* USE_PIPES */
1904 setproctitle("%s@notty", pw->pw_name);
1906 /* Fork the child. */
1907 if ((pid = fork()) == 0) {
1908 /* Child. Reinitialize the log since the pid has changed. */
1909 log_init(av0, options.log_level, options.log_facility, log_stderr);
1912 * Create a new session and process group since the 4.4BSD
1913 * setlogin() affects the entire process group.
1916 error("setsid failed: %.100s", strerror(errno));
1920 * Redirect stdin. We close the parent side of the socket
1921 * pair, and make the child side the standard input.
1924 if (dup2(pin[0], 0) < 0)
1925 perror("dup2 stdin");
1928 /* Redirect stdout. */
1930 if (dup2(pout[1], 1) < 0)
1931 perror("dup2 stdout");
1934 /* Redirect stderr. */
1936 if (dup2(perr[1], 2) < 0)
1937 perror("dup2 stderr");
1939 #else /* USE_PIPES */
1941 * Redirect stdin, stdout, and stderr. Stdin and stdout will
1942 * use the same socket, as some programs (particularly rdist)
1943 * seem to depend on it.
1947 if (dup2(inout[0], 0) < 0) /* stdin */
1948 perror("dup2 stdin");
1949 if (dup2(inout[0], 1) < 0) /* stdout. Note: same socket as stdin. */
1950 perror("dup2 stdout");
1951 if (dup2(err[0], 2) < 0) /* stderr */
1952 perror("dup2 stderr");
1953 #endif /* USE_PIPES */
1955 /* Do processing for the child (exec command etc). */
1956 do_child(command, pw, NULL, display, auth_proto, auth_data, NULL);
1960 packet_disconnect("fork failed: %.100s", strerror(errno));
1962 /* We are the parent. Close the child sides of the pipes. */
1967 /* Enter the interactive session. */
1968 server_loop(pid, pin[1], pout[0], perr[0]);
1969 /* server_loop has closed pin[1], pout[1], and perr[1]. */
1970 #else /* USE_PIPES */
1971 /* We are the parent. Close the child sides of the socket pairs. */
1976 * Enter the interactive session. Note: server_loop must be able to
1977 * handle the case that fdin and fdout are the same.
1979 server_loop(pid, inout[1], inout[1], err[1]);
1980 /* server_loop has closed inout[1] and err[1]. */
1981 #endif /* USE_PIPES */
1984 struct pty_cleanup_context {
1985 const char *ttyname;
1990 * Function to perform cleanup if we get aborted abnormally (e.g., due to a
1991 * dropped connection).
1994 pty_cleanup_proc(void *context)
1996 struct pty_cleanup_context *cu = context;
1998 debug("pty_cleanup_proc called");
2000 /* Record that the user has logged out. */
2001 record_logout(cu->pid, cu->ttyname);
2003 /* Release the pseudo-tty. */
2004 pty_release(cu->ttyname);
2008 * This is called to fork and execute a command when we have a tty. This
2009 * will call do_child from the child, and server_loop from the parent after
2010 * setting up file descriptors, controlling tty, updating wtmp, utmp,
2011 * lastlog, and other such operations.
2014 do_exec_pty(const char *command, int ptyfd, int ttyfd,
2015 const char *ttyname, struct passwd * pw, const char *term,
2016 const char *display, const char *auth_proto,
2017 const char *auth_data)
2020 const char *hostname;
2021 time_t last_login_time;
2022 char buf[100], *time_string;
2027 struct sockaddr_storage from;
2029 struct pty_cleanup_context cleanup_context;
2031 /* Get remote host name. */
2032 hostname = get_canonical_hostname();
2035 * Get the time when the user last logged in. Buf will be set to
2036 * contain the hostname the last login was from.
2038 if (!options.use_login) {
2039 last_login_time = get_last_login_time(pw->pw_uid, pw->pw_name,
2042 setproctitle("%s@%s", pw->pw_name, strrchr(ttyname, '/') + 1);
2044 /* Fork the child. */
2045 if ((pid = fork()) == 0) {
2048 /* Child. Reinitialize the log because the pid has
2050 log_init(av0, options.log_level, options.log_facility, log_stderr);
2052 /* Close the master side of the pseudo tty. */
2055 /* Make the pseudo tty our controlling tty. */
2056 pty_make_controlling_tty(&ttyfd, ttyname);
2058 /* Redirect stdin from the pseudo tty. */
2059 if (dup2(ttyfd, fileno(stdin)) < 0)
2060 error("dup2 stdin failed: %.100s", strerror(errno));
2062 /* Redirect stdout to the pseudo tty. */
2063 if (dup2(ttyfd, fileno(stdout)) < 0)
2064 error("dup2 stdin failed: %.100s", strerror(errno));
2066 /* Redirect stderr to the pseudo tty. */
2067 if (dup2(ttyfd, fileno(stderr)) < 0)
2068 error("dup2 stdin failed: %.100s", strerror(errno));
2070 /* Close the extra descriptor for the pseudo tty. */
2074 * Get IP address of client. This is needed because we want
2075 * to record where the user logged in from. If the
2076 * connection is not a socket, let the ip address be 0.0.0.0.
2078 memset(&from, 0, sizeof(from));
2079 if (packet_get_connection_in() == packet_get_connection_out()) {
2080 fromlen = sizeof(from);
2081 if (getpeername(packet_get_connection_in(),
2082 (struct sockaddr *) & from, &fromlen) < 0) {
2083 debug("getpeername: %.100s", strerror(errno));
2087 /* Record that there was a login on that terminal. */
2088 record_login(pid, ttyname, pw->pw_name, pw->pw_uid, hostname,
2089 (struct sockaddr *)&from);
2091 /* Check if .hushlogin exists. */
2092 snprintf(line, sizeof line, "%.200s/.hushlogin", pw->pw_dir);
2093 quiet_login = stat(line, &st) >= 0;
2097 print_pam_messages();
2098 #endif /* USE_PAM */
2101 * If the user has logged in before, display the time of last
2102 * login. However, don't display anything extra if a command
2103 * has been specified (so that ssh can be used to execute
2104 * commands on a remote machine without users knowing they
2105 * are going to another machine). Login(1) will do this for
2106 * us as well, so check if login(1) is used
2108 if (command == NULL && last_login_time != 0 && !quiet_login &&
2109 !options.use_login) {
2110 /* Convert the date to a string. */
2111 time_string = ctime(&last_login_time);
2112 /* Remove the trailing newline. */
2113 if (strchr(time_string, '\n'))
2114 *strchr(time_string, '\n') = 0;
2115 /* Display the last login time. Host if displayed
2117 if (strcmp(buf, "") == 0)
2118 printf("Last login: %s\r\n", time_string);
2120 printf("Last login: %s from %s\r\n", time_string, buf);
2123 * Print /etc/motd unless a command was specified or printing
2124 * it was disabled in server options or login(1) will be
2125 * used. Note that some machines appear to print it in
2126 * /etc/profile or similar.
2128 if (command == NULL && options.print_motd && !quiet_login &&
2129 !options.use_login) {
2130 /* Print /etc/motd if it exists. */
2131 f = fopen("/etc/motd", "r");
2133 while (fgets(line, sizeof(line), f))
2134 fputs(line, stdout);
2138 /* Do common processing for the child, such as execing the command. */
2139 do_child(command, pw, term, display, auth_proto, auth_data, ttyname);
2143 packet_disconnect("fork failed: %.100s", strerror(errno));
2144 /* Parent. Close the slave side of the pseudo tty. */
2148 * Create another descriptor of the pty master side for use as the
2149 * standard input. We could use the original descriptor, but this
2150 * simplifies code in server_loop. The descriptor is bidirectional.
2154 packet_disconnect("dup failed: %.100s", strerror(errno));
2157 * Add a cleanup function to clear the utmp entry and record logout
2158 * time in case we call fatal() (e.g., the connection gets closed).
2160 cleanup_context.pid = pid;
2161 cleanup_context.ttyname = ttyname;
2162 fatal_add_cleanup(pty_cleanup_proc, (void *) &cleanup_context);
2164 /* Enter interactive session. */
2165 server_loop(pid, ptyfd, fdout, -1);
2166 /* server_loop has not closed ptyfd and fdout. */
2168 /* Cancel the cleanup function. */
2169 fatal_remove_cleanup(pty_cleanup_proc, (void *) &cleanup_context);
2171 /* Record that the user has logged out. */
2172 record_logout(pid, ttyname);
2174 /* Release the pseudo-tty. */
2175 pty_release(ttyname);
2178 * Close the server side of the socket pairs. We must do this after
2179 * the pty cleanup, so that another process doesn't get this pty
2180 * while we're still cleaning up.
2187 * Sets the value of the given variable in the environment. If the variable
2188 * already exists, its value is overriden.
2191 child_set_env(char ***envp, unsigned int *envsizep, const char *name,
2194 unsigned int i, namelen;
2198 * Find the slot where the value should be stored. If the variable
2199 * already exists, we reuse the slot; otherwise we append a new slot
2200 * at the end of the array, expanding if necessary.
2203 namelen = strlen(name);
2204 for (i = 0; env[i]; i++)
2205 if (strncmp(env[i], name, namelen) == 0 && env[i][namelen] == '=')
2208 /* Reuse the slot. */
2211 /* New variable. Expand if necessary. */
2212 if (i >= (*envsizep) - 1) {
2214 env = (*envp) = xrealloc(env, (*envsizep) * sizeof(char *));
2216 /* Need to set the NULL pointer at end of array beyond the new slot. */
2220 /* Allocate space and format the variable in the appropriate slot. */
2221 env[i] = xmalloc(strlen(name) + 1 + strlen(value) + 1);
2222 snprintf(env[i], strlen(name) + 1 + strlen(value) + 1, "%s=%s", name, value);
2226 * Reads environment variables from the given file and adds/overrides them
2227 * into the environment. If the file does not exist, this does nothing.
2228 * Otherwise, it must consist of empty lines, comments (line starts with '#')
2229 * and assignments of the form name=value. No other forms are allowed.
2232 read_environment_file(char ***env, unsigned int *envsize,
2233 const char *filename)
2239 f = fopen(filename, "r");
2243 while (fgets(buf, sizeof(buf), f)) {
2244 for (cp = buf; *cp == ' ' || *cp == '\t'; cp++)
2246 if (!*cp || *cp == '#' || *cp == '\n')
2248 if (strchr(cp, '\n'))
2249 *strchr(cp, '\n') = '\0';
2250 value = strchr(cp, '=');
2251 if (value == NULL) {
2252 fprintf(stderr, "Bad line in %.100s: %.200s\n", filename, buf);
2255 /* Replace the equals sign by nul, and advance value to the value string. */
2258 child_set_env(env, envsize, cp, value);
2265 * Sets any environment variables which have been specified by PAM
2267 void do_pam_environment(char ***env, int *envsize)
2269 char *equals, var_name[512], var_val[512];
2273 if ((pam_env = fetch_pam_environment()) == NULL)
2276 for(i = 0; pam_env[i] != NULL; i++) {
2277 if ((equals = strstr(pam_env[i], "=")) == NULL)
2280 if (strlen(pam_env[i]) < (sizeof(var_name) - 1))
2282 memset(var_name, '\0', sizeof(var_name));
2283 memset(var_val, '\0', sizeof(var_val));
2285 strncpy(var_name, pam_env[i], equals - pam_env[i]);
2286 strcpy(var_val, equals + 1);
2288 debug("PAM environment: %s=%s", var_name, var_val);
2290 child_set_env(env, envsize, var_name, var_val);
2294 #endif /* USE_PAM */
2297 * Performs common processing for the child, such as setting up the
2298 * environment, closing extra file descriptors, setting the user and group
2299 * ids, and executing the command or shell.
2302 do_child(const char *command, struct passwd * pw, const char *term,
2303 const char *display, const char *auth_proto,
2304 const char *auth_data, const char *ttyname)
2306 const char *shell, *cp = NULL;
2309 unsigned int envsize, i;
2311 extern char **environ;
2315 #ifndef USE_PAM /* pam_nologin handles this */
2316 /* Check /etc/nologin. */
2317 f = fopen("/etc/nologin", "r");
2319 /* /etc/nologin exists. Print its contents and exit. */
2320 while (fgets(buf, sizeof(buf), f))
2323 if (pw->pw_uid != 0)
2326 #endif /* USE_PAM */
2328 /* Set login name in the kernel. */
2329 if (setlogin(pw->pw_name) < 0)
2330 error("setlogin failed: %s", strerror(errno));
2332 /* Set uid, gid, and groups. */
2333 /* Login(1) does this as well, and it needs uid 0 for the "-h"
2334 switch, so we let login(1) to this for us. */
2335 if (!options.use_login) {
2336 if (getuid() == 0 || geteuid() == 0) {
2337 if (setgid(pw->pw_gid) < 0) {
2341 /* Initialize the group list. */
2342 if (initgroups(pw->pw_name, pw->pw_gid) < 0) {
2343 perror("initgroups");
2348 /* Permanently switch to the desired uid. */
2349 permanently_set_uid(pw->pw_uid);
2351 if (getuid() != pw->pw_uid || geteuid() != pw->pw_uid)
2352 fatal("Failed to set uids to %d.", (int) pw->pw_uid);
2355 * Get the shell from the password data. An empty shell field is
2356 * legal, and means /bin/sh.
2358 shell = (pw->pw_shell[0] == '\0') ? _PATH_BSHELL : pw->pw_shell;
2361 /* Try to get AFS tokens for the local cell. */
2365 if (k_afs_cell_of_file(pw->pw_dir, cell, sizeof(cell)) == 0)
2366 krb_afslog(cell, 0);
2372 /* Initialize the environment. */
2374 env = xmalloc(envsize * sizeof(char *));
2377 if (!options.use_login) {
2378 /* Set basic environment. */
2379 child_set_env(&env, &envsize, "USER", pw->pw_name);
2380 child_set_env(&env, &envsize, "LOGNAME", pw->pw_name);
2381 child_set_env(&env, &envsize, "HOME", pw->pw_dir);
2382 child_set_env(&env, &envsize, "PATH", _PATH_STDPATH);
2384 snprintf(buf, sizeof buf, "%.200s/%.50s",
2385 _PATH_MAILDIR, pw->pw_name);
2386 child_set_env(&env, &envsize, "MAIL", buf);
2388 /* Normal systems set SHELL by default. */
2389 child_set_env(&env, &envsize, "SHELL", shell);
2392 child_set_env(&env, &envsize, "TZ", getenv("TZ"));
2394 /* Set custom environment options from RSA authentication. */
2395 while (custom_environment) {
2396 struct envstring *ce = custom_environment;
2399 for (i = 0; s[i] != '=' && s[i]; i++);
2402 child_set_env(&env, &envsize, s, s + i + 1);
2404 custom_environment = ce->next;
2409 snprintf(buf, sizeof buf, "%.50s %d %d",
2410 get_remote_ipaddr(), get_remote_port(), get_local_port());
2411 child_set_env(&env, &envsize, "SSH_CLIENT", buf);
2414 child_set_env(&env, &envsize, "SSH_TTY", ttyname);
2416 child_set_env(&env, &envsize, "TERM", term);
2418 child_set_env(&env, &envsize, "DISPLAY", display);
2422 extern char *ticket;
2425 child_set_env(&env, &envsize, "KRBTKFILE", ticket);
2430 /* Pull in any environment variables that may have been set by PAM. */
2431 do_pam_environment(&env, &envsize);
2432 #endif /* USE_PAM */
2435 child_set_env(&env, &envsize, "XAUTHORITY", xauthfile);
2437 if (auth_get_socket_name() != NULL)
2438 child_set_env(&env, &envsize, SSH_AUTHSOCKET_ENV_NAME,
2439 auth_get_socket_name());
2441 /* read $HOME/.ssh/environment. */
2442 if (!options.use_login) {
2443 snprintf(buf, sizeof buf, "%.200s/.ssh/environment", pw->pw_dir);
2444 read_environment_file(&env, &envsize, buf);
2447 /* dump the environment */
2448 fprintf(stderr, "Environment:\n");
2449 for (i = 0; env[i]; i++)
2450 fprintf(stderr, " %.200s\n", env[i]);
2453 * Close the connection descriptors; note that this is the child, and
2454 * the server will still have the socket open, and it is important
2455 * that we do not shutdown it. Note that the descriptors cannot be
2456 * closed before building the environment, as we call
2457 * get_remote_ipaddr there.
2459 if (packet_get_connection_in() == packet_get_connection_out())
2460 close(packet_get_connection_in());
2462 close(packet_get_connection_in());
2463 close(packet_get_connection_out());
2466 * Close all descriptors related to channels. They will still remain
2467 * open in the parent.
2469 /* XXX better use close-on-exec? -markus */
2470 channel_close_all();
2473 * Close any extra file descriptors. Note that there may still be
2474 * descriptors left by system functions. They will be closed later.
2479 * Close any extra open file descriptors so that we don\'t have them
2480 * hanging around in clients. Note that we want to do this after
2481 * initgroups, because at least on Solaris 2.3 it leaves file
2484 for (i = 3; i < 64; i++)
2487 /* Change current directory to the user\'s home directory. */
2488 if (chdir(pw->pw_dir) < 0)
2489 fprintf(stderr, "Could not chdir to home directory %s: %s\n",
2490 pw->pw_dir, strerror(errno));
2493 * Must take new environment into use so that .ssh/rc, /etc/sshrc and
2494 * xauth are run in the proper environment.
2499 * Run $HOME/.ssh/rc, /etc/sshrc, or xauth (whichever is found first
2502 if (!options.use_login) {
2503 if (stat(SSH_USER_RC, &st) >= 0) {
2505 fprintf(stderr, "Running /bin/sh %s\n", SSH_USER_RC);
2507 f = popen("/bin/sh " SSH_USER_RC, "w");
2509 if (auth_proto != NULL && auth_data != NULL)
2510 fprintf(f, "%s %s\n", auth_proto, auth_data);
2513 fprintf(stderr, "Could not run %s\n", SSH_USER_RC);
2514 } else if (stat(SSH_SYSTEM_RC, &st) >= 0) {
2516 fprintf(stderr, "Running /bin/sh %s\n", SSH_SYSTEM_RC);
2518 f = popen("/bin/sh " SSH_SYSTEM_RC, "w");
2520 if (auth_proto != NULL && auth_data != NULL)
2521 fprintf(f, "%s %s\n", auth_proto, auth_data);
2524 fprintf(stderr, "Could not run %s\n", SSH_SYSTEM_RC);
2528 /* Add authority data to .Xauthority if appropriate. */
2529 if (auth_proto != NULL && auth_data != NULL) {
2531 fprintf(stderr, "Running %.100s add %.100s %.100s %.100s\n",
2532 XAUTH_PATH, display, auth_proto, auth_data);
2534 f = popen(XAUTH_PATH " -q -", "w");
2536 fprintf(f, "add %s %s %s\n", display, auth_proto, auth_data);
2539 fprintf(stderr, "Could not run %s -q -\n", XAUTH_PATH);
2542 #endif /* XAUTH_PATH */
2544 /* Get the last component of the shell name. */
2545 cp = strrchr(shell, '/');
2552 * If we have no command, execute the shell. In this case, the shell
2553 * name to be passed in argv[0] is preceded by '-' to indicate that
2554 * this is a login shell.
2557 if (!options.use_login) {
2561 * Check for mail if we have a tty and it was enabled
2562 * in server options.
2564 if (ttyname && options.check_mail) {
2566 struct stat mailstat;
2567 mailbox = getenv("MAIL");
2568 if (mailbox != NULL) {
2569 if (stat(mailbox, &mailstat) != 0 || mailstat.st_size == 0)
2570 printf("No mail.\n");
2571 else if (mailstat.st_mtime < mailstat.st_atime)
2572 printf("You have mail.\n");
2574 printf("You have new mail.\n");
2577 /* Start the shell. Set initial character to '-'. */
2579 strncpy(buf + 1, cp, sizeof(buf) - 1);
2580 buf[sizeof(buf) - 1] = 0;
2582 /* Execute the shell. */
2585 execve(shell, argv, env);
2587 /* Executing the shell failed. */
2592 /* Launch login(1). */
2594 execl(LOGIN_PROGRAM, "login", "-h", get_remote_ipaddr(),
2595 "-p", "-f", "--", pw->pw_name, NULL);
2597 /* Login couldn't be executed, die. */
2604 * Execute the command using the user's shell. This uses the -c
2605 * option to execute the command.
2607 argv[0] = (char *) cp;
2609 argv[2] = (char *) command;
2611 execve(shell, argv, env);