Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 1 | /* $OpenBSD: ssh-agent.c,v 1.185 2014/04/29 18:01:49 markus Exp $ */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2 | /* |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 3 | * Author: Tatu Ylonen <ylo@cs.hut.fi> |
| 4 | * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland |
| 5 | * All rights reserved |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 6 | * The authentication agent program. |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 7 | * |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 8 | * As far as I am concerned, the code I have written for this software |
| 9 | * can be used freely for any purpose. Any derived versions of this |
| 10 | * software must be clearly marked as such, and if the derived work is |
| 11 | * incompatible with the protocol description in the RFC file, it must be |
| 12 | * called by a name other than "ssh" or "Secure Shell". |
| 13 | * |
Ben Lindstrom | 4469723 | 2001-07-04 03:32:30 +0000 | [diff] [blame] | 14 | * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved. |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 15 | * |
| 16 | * Redistribution and use in source and binary forms, with or without |
| 17 | * modification, are permitted provided that the following conditions |
| 18 | * are met: |
| 19 | * 1. Redistributions of source code must retain the above copyright |
| 20 | * notice, this list of conditions and the following disclaimer. |
| 21 | * 2. Redistributions in binary form must reproduce the above copyright |
| 22 | * notice, this list of conditions and the following disclaimer in the |
| 23 | * documentation and/or other materials provided with the distribution. |
| 24 | * |
| 25 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 26 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 27 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 28 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 29 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 30 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 31 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 32 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 33 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 34 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 35 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 36 | |
| 37 | #include "includes.h" |
Damien Miller | 574c41f | 2006-03-15 11:40:10 +1100 | [diff] [blame] | 38 | |
| 39 | #include <sys/types.h> |
Damien Miller | 8dbffe7 | 2006-08-05 11:02:17 +1000 | [diff] [blame] | 40 | #include <sys/param.h> |
| 41 | #include <sys/resource.h> |
Damien Miller | 42fb068 | 2006-03-15 14:03:06 +1100 | [diff] [blame] | 42 | #include <sys/stat.h> |
Damien Miller | e3b60b5 | 2006-07-10 21:08:03 +1000 | [diff] [blame] | 43 | #include <sys/socket.h> |
Damien Miller | 9aec919 | 2006-08-05 10:57:45 +1000 | [diff] [blame] | 44 | #ifdef HAVE_SYS_TIME_H |
| 45 | # include <sys/time.h> |
| 46 | #endif |
Damien Miller | 574c41f | 2006-03-15 11:40:10 +1100 | [diff] [blame] | 47 | #ifdef HAVE_SYS_UN_H |
| 48 | # include <sys/un.h> |
| 49 | #endif |
Damien Miller | 9b48151 | 2002-09-12 10:43:29 +1000 | [diff] [blame] | 50 | #include "openbsd-compat/sys-queue.h" |
Damien Miller | e3b60b5 | 2006-07-10 21:08:03 +1000 | [diff] [blame] | 51 | |
Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 52 | #ifdef WITH_OPENSSL |
Damien Miller | e3476ed | 2006-07-24 14:13:33 +1000 | [diff] [blame] | 53 | #include <openssl/evp.h> |
Darren Tucker | bfaaf96 | 2008-02-28 19:13:52 +1100 | [diff] [blame] | 54 | #include "openbsd-compat/openssl-compat.h" |
Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 55 | #endif |
Damien Miller | e3476ed | 2006-07-24 14:13:33 +1000 | [diff] [blame] | 56 | |
Darren Tucker | 3997249 | 2006-07-12 22:22:46 +1000 | [diff] [blame] | 57 | #include <errno.h> |
Damien Miller | 57cf638 | 2006-07-10 21:13:46 +1000 | [diff] [blame] | 58 | #include <fcntl.h> |
Damien Miller | 03e2003 | 2006-03-15 11:16:59 +1100 | [diff] [blame] | 59 | #ifdef HAVE_PATHS_H |
Damien Miller | a9263d0 | 2006-03-15 11:18:26 +1100 | [diff] [blame] | 60 | # include <paths.h> |
Damien Miller | 03e2003 | 2006-03-15 11:16:59 +1100 | [diff] [blame] | 61 | #endif |
Damien Miller | 6ff3cad | 2006-03-15 11:52:09 +1100 | [diff] [blame] | 62 | #include <signal.h> |
Damien Miller | ded319c | 2006-09-01 15:38:36 +1000 | [diff] [blame] | 63 | #include <stdarg.h> |
Damien Miller | a7a73ee | 2006-08-05 11:37:59 +1000 | [diff] [blame] | 64 | #include <stdio.h> |
Damien Miller | e7a1e5c | 2006-08-05 11:34:19 +1000 | [diff] [blame] | 65 | #include <stdlib.h> |
Damien Miller | 5598b4f | 2006-07-24 14:09:40 +1000 | [diff] [blame] | 66 | #include <time.h> |
Damien Miller | e3476ed | 2006-07-24 14:13:33 +1000 | [diff] [blame] | 67 | #include <string.h> |
Damien Miller | e6b3b61 | 2006-07-24 14:01:23 +1000 | [diff] [blame] | 68 | #include <unistd.h> |
Damien Miller | 6ff3cad | 2006-03-15 11:52:09 +1100 | [diff] [blame] | 69 | |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 70 | #include "xmalloc.h" |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 71 | #include "ssh.h" |
| 72 | #include "rsa.h" |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 73 | #include "buffer.h" |
Damien Miller | 994cf14 | 2000-07-21 10:19:44 +1000 | [diff] [blame] | 74 | #include "key.h" |
| 75 | #include "authfd.h" |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 76 | #include "compat.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 77 | #include "log.h" |
Damien Miller | 6c71179 | 2003-01-24 11:36:23 +1100 | [diff] [blame] | 78 | #include "misc.h" |
Damien Miller | 4a1c7aa | 2014-02-04 11:03:36 +1100 | [diff] [blame] | 79 | #include "digest.h" |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 80 | |
Damien Miller | 7ea845e | 2010-02-12 09:21:02 +1100 | [diff] [blame] | 81 | #ifdef ENABLE_PKCS11 |
| 82 | #include "ssh-pkcs11.h" |
Ben Lindstrom | bcc1808 | 2001-08-06 21:59:25 +0000 | [diff] [blame] | 83 | #endif |
Ben Lindstrom | 3f47163 | 2001-07-04 03:53:15 +0000 | [diff] [blame] | 84 | |
Damien Miller | 6c4914a | 2004-03-03 11:08:59 +1100 | [diff] [blame] | 85 | #if defined(HAVE_SYS_PRCTL_H) |
| 86 | #include <sys/prctl.h> /* For prctl() and PR_SET_DUMPABLE */ |
| 87 | #endif |
| 88 | |
Ben Lindstrom | 65366a8 | 2001-12-06 16:32:47 +0000 | [diff] [blame] | 89 | typedef enum { |
| 90 | AUTH_UNUSED, |
| 91 | AUTH_SOCKET, |
| 92 | AUTH_CONNECTION |
| 93 | } sock_type; |
| 94 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 95 | typedef struct { |
| 96 | int fd; |
Ben Lindstrom | 65366a8 | 2001-12-06 16:32:47 +0000 | [diff] [blame] | 97 | sock_type type; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 98 | Buffer input; |
| 99 | Buffer output; |
Ben Lindstrom | 21d1ed8 | 2002-06-06 21:48:57 +0000 | [diff] [blame] | 100 | Buffer request; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 101 | } SocketEntry; |
| 102 | |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 103 | u_int sockets_alloc = 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 104 | SocketEntry *sockets = NULL; |
| 105 | |
Damien Miller | 1a534ae | 2002-01-22 23:26:13 +1100 | [diff] [blame] | 106 | typedef struct identity { |
| 107 | TAILQ_ENTRY(identity) next; |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 108 | Key *key; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 109 | char *comment; |
Damien Miller | 7ea845e | 2010-02-12 09:21:02 +1100 | [diff] [blame] | 110 | char *provider; |
Darren Tucker | 5511925 | 2013-06-02 07:43:59 +1000 | [diff] [blame] | 111 | time_t death; |
Damien Miller | 6c71179 | 2003-01-24 11:36:23 +1100 | [diff] [blame] | 112 | u_int confirm; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 113 | } Identity; |
| 114 | |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 115 | typedef struct { |
| 116 | int nentries; |
Damien Miller | 1a534ae | 2002-01-22 23:26:13 +1100 | [diff] [blame] | 117 | TAILQ_HEAD(idqueue, identity) idlist; |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 118 | } Idtab; |
| 119 | |
| 120 | /* private key table, one per protocol version */ |
| 121 | Idtab idtable[3]; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 122 | |
| 123 | int max_fd = 0; |
| 124 | |
| 125 | /* pid of shell == parent of agent */ |
Damien Miller | 166fca8 | 2000-04-20 07:42:21 +1000 | [diff] [blame] | 126 | pid_t parent_pid = -1; |
Darren Tucker | 073f795 | 2013-06-02 23:47:11 +1000 | [diff] [blame] | 127 | time_t parent_alive_interval = 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 128 | |
| 129 | /* pathname and directory for AUTH_SOCKET */ |
Damien Miller | 07d86be | 2006-03-26 14:19:21 +1100 | [diff] [blame] | 130 | char socket_name[MAXPATHLEN]; |
| 131 | char socket_dir[MAXPATHLEN]; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 132 | |
Ben Lindstrom | 2f71704 | 2002-06-06 21:52:03 +0000 | [diff] [blame] | 133 | /* locking */ |
| 134 | int locked = 0; |
| 135 | char *lock_passwd = NULL; |
| 136 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 137 | extern char *__progname; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 138 | |
Darren Tucker | 5511925 | 2013-06-02 07:43:59 +1000 | [diff] [blame] | 139 | /* Default lifetime in seconds (0 == forever) */ |
| 140 | static long lifetime = 0; |
Damien Miller | 53d8148 | 2003-01-22 11:47:19 +1100 | [diff] [blame] | 141 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 142 | static void |
Damien Miller | 58f3486 | 2002-09-04 16:31:21 +1000 | [diff] [blame] | 143 | close_socket(SocketEntry *e) |
| 144 | { |
Damien Miller | 58f3486 | 2002-09-04 16:31:21 +1000 | [diff] [blame] | 145 | close(e->fd); |
| 146 | e->fd = -1; |
| 147 | e->type = AUTH_UNUSED; |
| 148 | buffer_free(&e->input); |
| 149 | buffer_free(&e->output); |
| 150 | buffer_free(&e->request); |
| 151 | } |
| 152 | |
| 153 | static void |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 154 | idtab_init(void) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 155 | { |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 156 | int i; |
Ben Lindstrom | 822b634 | 2002-06-23 21:38:49 +0000 | [diff] [blame] | 157 | |
Ben Lindstrom | 1c37c6a | 2001-12-06 18:00:18 +0000 | [diff] [blame] | 158 | for (i = 0; i <=2; i++) { |
Damien Miller | 1a534ae | 2002-01-22 23:26:13 +1100 | [diff] [blame] | 159 | TAILQ_INIT(&idtable[i].idlist); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 160 | idtable[i].nentries = 0; |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | /* return private key table for requested protocol version */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 165 | static Idtab * |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 166 | idtab_lookup(int version) |
| 167 | { |
| 168 | if (version < 1 || version > 2) |
| 169 | fatal("internal error, bad protocol version %d", version); |
| 170 | return &idtable[version]; |
| 171 | } |
| 172 | |
Ben Lindstrom | 61d328a | 2002-06-06 21:54:57 +0000 | [diff] [blame] | 173 | static void |
| 174 | free_identity(Identity *id) |
| 175 | { |
| 176 | key_free(id->key); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 177 | free(id->provider); |
| 178 | free(id->comment); |
| 179 | free(id); |
Ben Lindstrom | 61d328a | 2002-06-06 21:54:57 +0000 | [diff] [blame] | 180 | } |
| 181 | |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 182 | /* return matching private key for given public key */ |
Damien Miller | 1a534ae | 2002-01-22 23:26:13 +1100 | [diff] [blame] | 183 | static Identity * |
| 184 | lookup_identity(Key *key, int version) |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 185 | { |
Damien Miller | 1a534ae | 2002-01-22 23:26:13 +1100 | [diff] [blame] | 186 | Identity *id; |
| 187 | |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 188 | Idtab *tab = idtab_lookup(version); |
Damien Miller | 1a534ae | 2002-01-22 23:26:13 +1100 | [diff] [blame] | 189 | TAILQ_FOREACH(id, &tab->idlist, next) { |
| 190 | if (key_equal(key, id->key)) |
| 191 | return (id); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 192 | } |
Damien Miller | 1a534ae | 2002-01-22 23:26:13 +1100 | [diff] [blame] | 193 | return (NULL); |
| 194 | } |
| 195 | |
Damien Miller | 6c71179 | 2003-01-24 11:36:23 +1100 | [diff] [blame] | 196 | /* Check confirmation of keysign request */ |
| 197 | static int |
| 198 | confirm_key(Identity *id) |
| 199 | { |
Darren Tucker | ce327b6 | 2004-11-05 20:38:03 +1100 | [diff] [blame] | 200 | char *p; |
Damien Miller | 6c71179 | 2003-01-24 11:36:23 +1100 | [diff] [blame] | 201 | int ret = -1; |
| 202 | |
| 203 | p = key_fingerprint(id->key, SSH_FP_MD5, SSH_FP_HEX); |
Darren Tucker | ce327b6 | 2004-11-05 20:38:03 +1100 | [diff] [blame] | 204 | if (ask_permission("Allow use of key %s?\nKey fingerprint %s.", |
| 205 | id->comment, p)) |
| 206 | ret = 0; |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 207 | free(p); |
Darren Tucker | ce327b6 | 2004-11-05 20:38:03 +1100 | [diff] [blame] | 208 | |
Damien Miller | 6c71179 | 2003-01-24 11:36:23 +1100 | [diff] [blame] | 209 | return (ret); |
| 210 | } |
| 211 | |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 212 | /* send list of supported public keys to 'client' */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 213 | static void |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 214 | process_request_identities(SocketEntry *e, int version) |
| 215 | { |
| 216 | Idtab *tab = idtab_lookup(version); |
Damien Miller | 1a534ae | 2002-01-22 23:26:13 +1100 | [diff] [blame] | 217 | Identity *id; |
Ben Lindstrom | 822b634 | 2002-06-23 21:38:49 +0000 | [diff] [blame] | 218 | Buffer msg; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 219 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 220 | buffer_init(&msg); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 221 | buffer_put_char(&msg, (version == 1) ? |
| 222 | SSH_AGENT_RSA_IDENTITIES_ANSWER : SSH2_AGENT_IDENTITIES_ANSWER); |
| 223 | buffer_put_int(&msg, tab->nentries); |
Damien Miller | 1a534ae | 2002-01-22 23:26:13 +1100 | [diff] [blame] | 224 | TAILQ_FOREACH(id, &tab->idlist, next) { |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 225 | if (id->key->type == KEY_RSA1) { |
Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 226 | #ifdef WITH_SSH1 |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 227 | buffer_put_int(&msg, BN_num_bits(id->key->rsa->n)); |
| 228 | buffer_put_bignum(&msg, id->key->rsa->e); |
| 229 | buffer_put_bignum(&msg, id->key->rsa->n); |
Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 230 | #endif |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 231 | } else { |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 232 | u_char *blob; |
| 233 | u_int blen; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 234 | key_to_blob(id->key, &blob, &blen); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 235 | buffer_put_string(&msg, blob, blen); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 236 | free(blob); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 237 | } |
| 238 | buffer_put_cstring(&msg, id->comment); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 239 | } |
| 240 | buffer_put_int(&e->output, buffer_len(&msg)); |
| 241 | buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg)); |
| 242 | buffer_free(&msg); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 243 | } |
| 244 | |
Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 245 | #ifdef WITH_SSH1 |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 246 | /* ssh1 only */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 247 | static void |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 248 | process_authentication_challenge1(SocketEntry *e) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 249 | { |
Ben Lindstrom | 822b634 | 2002-06-23 21:38:49 +0000 | [diff] [blame] | 250 | u_char buf[32], mdbuf[16], session_id[16]; |
| 251 | u_int response_type; |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 252 | BIGNUM *challenge; |
Ben Lindstrom | 822b634 | 2002-06-23 21:38:49 +0000 | [diff] [blame] | 253 | Identity *id; |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 254 | int i, len; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 255 | Buffer msg; |
Damien Miller | 4a1c7aa | 2014-02-04 11:03:36 +1100 | [diff] [blame] | 256 | struct ssh_digest_ctx *md; |
Ben Lindstrom | 822b634 | 2002-06-23 21:38:49 +0000 | [diff] [blame] | 257 | Key *key; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 258 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 259 | buffer_init(&msg); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 260 | key = key_new(KEY_RSA1); |
Damien Miller | da75516 | 2002-01-22 23:09:22 +1100 | [diff] [blame] | 261 | if ((challenge = BN_new()) == NULL) |
| 262 | fatal("process_authentication_challenge1: BN_new failed"); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 263 | |
Ben Lindstrom | c5a7f4f | 2002-06-25 23:19:13 +0000 | [diff] [blame] | 264 | (void) buffer_get_int(&e->request); /* ignored */ |
Ben Lindstrom | 21d1ed8 | 2002-06-06 21:48:57 +0000 | [diff] [blame] | 265 | buffer_get_bignum(&e->request, key->rsa->e); |
| 266 | buffer_get_bignum(&e->request, key->rsa->n); |
| 267 | buffer_get_bignum(&e->request, challenge); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 268 | |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 269 | /* Only protocol 1.1 is supported */ |
Ben Lindstrom | 21d1ed8 | 2002-06-06 21:48:57 +0000 | [diff] [blame] | 270 | if (buffer_len(&e->request) == 0) |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 271 | goto failure; |
Ben Lindstrom | 21d1ed8 | 2002-06-06 21:48:57 +0000 | [diff] [blame] | 272 | buffer_get(&e->request, session_id, 16); |
| 273 | response_type = buffer_get_int(&e->request); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 274 | if (response_type != 1) |
| 275 | goto failure; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 276 | |
Damien Miller | 1a534ae | 2002-01-22 23:26:13 +1100 | [diff] [blame] | 277 | id = lookup_identity(key, 1); |
Damien Miller | 6c71179 | 2003-01-24 11:36:23 +1100 | [diff] [blame] | 278 | if (id != NULL && (!id->confirm || confirm_key(id) == 0)) { |
Damien Miller | 1a534ae | 2002-01-22 23:26:13 +1100 | [diff] [blame] | 279 | Key *private = id->key; |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 280 | /* Decrypt the challenge using the private key. */ |
Damien Miller | 7650bc6 | 2001-01-30 09:27:26 +1100 | [diff] [blame] | 281 | if (rsa_private_decrypt(challenge, challenge, private->rsa) <= 0) |
| 282 | goto failure; |
Damien Miller | fd7c911 | 1999-11-08 16:15:55 +1100 | [diff] [blame] | 283 | |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 284 | /* The response is MD5 of decrypted challenge plus session id. */ |
| 285 | len = BN_num_bytes(challenge); |
| 286 | if (len <= 0 || len > 32) { |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 287 | logit("process_authentication_challenge: bad challenge length %d", len); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 288 | goto failure; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 289 | } |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 290 | memset(buf, 0, 32); |
| 291 | BN_bn2bin(challenge, buf + 32 - len); |
Damien Miller | 4a1c7aa | 2014-02-04 11:03:36 +1100 | [diff] [blame] | 292 | if ((md = ssh_digest_start(SSH_DIGEST_MD5)) == NULL || |
| 293 | ssh_digest_update(md, buf, 32) < 0 || |
| 294 | ssh_digest_update(md, session_id, 16) < 0 || |
| 295 | ssh_digest_final(md, mdbuf, sizeof(mdbuf)) < 0) |
| 296 | fatal("%s: md5 failed", __func__); |
| 297 | ssh_digest_free(md); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 298 | |
| 299 | /* Send the response. */ |
| 300 | buffer_put_char(&msg, SSH_AGENT_RSA_RESPONSE); |
| 301 | for (i = 0; i < 16; i++) |
| 302 | buffer_put_char(&msg, mdbuf[i]); |
| 303 | goto send; |
| 304 | } |
| 305 | |
| 306 | failure: |
| 307 | /* Unknown identity or protocol error. Send failure. */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 308 | buffer_put_char(&msg, SSH_AGENT_FAILURE); |
| 309 | send: |
| 310 | buffer_put_int(&e->output, buffer_len(&msg)); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 311 | buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg)); |
| 312 | key_free(key); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 313 | BN_clear_free(challenge); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 314 | buffer_free(&msg); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 315 | } |
Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 316 | #endif |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 317 | |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 318 | /* ssh2 only */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 319 | static void |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 320 | process_sign_request2(SocketEntry *e) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 321 | { |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 322 | u_char *blob, *data, *signature = NULL; |
| 323 | u_int blen, dlen, slen = 0; |
Ben Lindstrom | 822b634 | 2002-06-23 21:38:49 +0000 | [diff] [blame] | 324 | extern int datafellows; |
Damien Miller | 471db5c | 2008-06-30 00:05:48 +1000 | [diff] [blame] | 325 | int odatafellows; |
Ben Lindstrom | 822b634 | 2002-06-23 21:38:49 +0000 | [diff] [blame] | 326 | int ok = -1, flags; |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 327 | Buffer msg; |
Ben Lindstrom | 822b634 | 2002-06-23 21:38:49 +0000 | [diff] [blame] | 328 | Key *key; |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 329 | |
| 330 | datafellows = 0; |
Kevin Steves | fa72dda | 2000-12-15 18:39:12 +0000 | [diff] [blame] | 331 | |
Ben Lindstrom | 21d1ed8 | 2002-06-06 21:48:57 +0000 | [diff] [blame] | 332 | blob = buffer_get_string(&e->request, &blen); |
| 333 | data = buffer_get_string(&e->request, &dlen); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 334 | |
Ben Lindstrom | 21d1ed8 | 2002-06-06 21:48:57 +0000 | [diff] [blame] | 335 | flags = buffer_get_int(&e->request); |
Damien Miller | 471db5c | 2008-06-30 00:05:48 +1000 | [diff] [blame] | 336 | odatafellows = datafellows; |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 337 | if (flags & SSH_AGENT_OLD_SIGNATURE) |
| 338 | datafellows = SSH_BUG_SIGBLOB; |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 339 | |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 340 | key = key_from_blob(blob, blen); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 341 | if (key != NULL) { |
Damien Miller | 1a534ae | 2002-01-22 23:26:13 +1100 | [diff] [blame] | 342 | Identity *id = lookup_identity(key, 2); |
Damien Miller | 6c71179 | 2003-01-24 11:36:23 +1100 | [diff] [blame] | 343 | if (id != NULL && (!id->confirm || confirm_key(id) == 0)) |
Damien Miller | 1a534ae | 2002-01-22 23:26:13 +1100 | [diff] [blame] | 344 | ok = key_sign(id->key, &signature, &slen, data, dlen); |
Damien Miller | 40b5985 | 2006-06-13 13:00:25 +1000 | [diff] [blame] | 345 | key_free(key); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 346 | } |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 347 | buffer_init(&msg); |
| 348 | if (ok == 0) { |
| 349 | buffer_put_char(&msg, SSH2_AGENT_SIGN_RESPONSE); |
| 350 | buffer_put_string(&msg, signature, slen); |
| 351 | } else { |
| 352 | buffer_put_char(&msg, SSH_AGENT_FAILURE); |
| 353 | } |
| 354 | buffer_put_int(&e->output, buffer_len(&msg)); |
| 355 | buffer_append(&e->output, buffer_ptr(&msg), |
| 356 | buffer_len(&msg)); |
| 357 | buffer_free(&msg); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 358 | free(data); |
| 359 | free(blob); |
| 360 | free(signature); |
Damien Miller | 471db5c | 2008-06-30 00:05:48 +1000 | [diff] [blame] | 361 | datafellows = odatafellows; |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | /* shared */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 365 | static void |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 366 | process_remove_identity(SocketEntry *e, int version) |
| 367 | { |
Ben Lindstrom | 822b634 | 2002-06-23 21:38:49 +0000 | [diff] [blame] | 368 | u_int blen, bits; |
| 369 | int success = 0; |
Damien Miller | 1a534ae | 2002-01-22 23:26:13 +1100 | [diff] [blame] | 370 | Key *key = NULL; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 371 | u_char *blob; |
Damien Miller | 7e8e820 | 1999-11-16 13:37:16 +1100 | [diff] [blame] | 372 | |
Ben Lindstrom | 1c37c6a | 2001-12-06 18:00:18 +0000 | [diff] [blame] | 373 | switch (version) { |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 374 | case 1: |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 375 | key = key_new(KEY_RSA1); |
Ben Lindstrom | 21d1ed8 | 2002-06-06 21:48:57 +0000 | [diff] [blame] | 376 | bits = buffer_get_int(&e->request); |
| 377 | buffer_get_bignum(&e->request, key->rsa->e); |
| 378 | buffer_get_bignum(&e->request, key->rsa->n); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 379 | |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 380 | if (bits != key_size(key)) |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 381 | logit("Warning: identity keysize mismatch: actual %u, announced %u", |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 382 | key_size(key), bits); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 383 | break; |
| 384 | case 2: |
Ben Lindstrom | 21d1ed8 | 2002-06-06 21:48:57 +0000 | [diff] [blame] | 385 | blob = buffer_get_string(&e->request, &blen); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 386 | key = key_from_blob(blob, blen); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 387 | free(blob); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 388 | break; |
| 389 | } |
| 390 | if (key != NULL) { |
Damien Miller | 1a534ae | 2002-01-22 23:26:13 +1100 | [diff] [blame] | 391 | Identity *id = lookup_identity(key, version); |
| 392 | if (id != NULL) { |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 393 | /* |
| 394 | * We have this key. Free the old key. Since we |
Damien Miller | 788f212 | 2005-11-05 15:14:59 +1100 | [diff] [blame] | 395 | * don't want to leave empty slots in the middle of |
Ben Lindstrom | 1492029 | 2000-11-21 21:24:55 +0000 | [diff] [blame] | 396 | * the array, we actually free the key there and move |
| 397 | * all the entries between the empty slot and the end |
| 398 | * of the array. |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 399 | */ |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 400 | Idtab *tab = idtab_lookup(version); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 401 | if (tab->nentries < 1) |
| 402 | fatal("process_remove_identity: " |
| 403 | "internal error: tab->nentries %d", |
| 404 | tab->nentries); |
Damien Miller | 1a534ae | 2002-01-22 23:26:13 +1100 | [diff] [blame] | 405 | TAILQ_REMOVE(&tab->idlist, id, next); |
| 406 | free_identity(id); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 407 | tab->nentries--; |
| 408 | success = 1; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 409 | } |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 410 | key_free(key); |
| 411 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 412 | buffer_put_int(&e->output, 1); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 413 | buffer_put_char(&e->output, |
| 414 | success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 415 | } |
| 416 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 417 | static void |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 418 | process_remove_all_identities(SocketEntry *e, int version) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 419 | { |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 420 | Idtab *tab = idtab_lookup(version); |
Damien Miller | 1a534ae | 2002-01-22 23:26:13 +1100 | [diff] [blame] | 421 | Identity *id; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 422 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 423 | /* Loop over all identities and clear the keys. */ |
Damien Miller | 1a534ae | 2002-01-22 23:26:13 +1100 | [diff] [blame] | 424 | for (id = TAILQ_FIRST(&tab->idlist); id; |
| 425 | id = TAILQ_FIRST(&tab->idlist)) { |
| 426 | TAILQ_REMOVE(&tab->idlist, id, next); |
| 427 | free_identity(id); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 428 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 429 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 430 | /* Mark that there are no identities. */ |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 431 | tab->nentries = 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 432 | |
| 433 | /* Send success. */ |
| 434 | buffer_put_int(&e->output, 1); |
| 435 | buffer_put_char(&e->output, SSH_AGENT_SUCCESS); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 436 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 437 | |
Darren Tucker | 2812dc9 | 2007-03-21 20:45:06 +1100 | [diff] [blame] | 438 | /* removes expired keys and returns number of seconds until the next expiry */ |
Darren Tucker | 5511925 | 2013-06-02 07:43:59 +1000 | [diff] [blame] | 439 | static time_t |
Ben Lindstrom | 61d328a | 2002-06-06 21:54:57 +0000 | [diff] [blame] | 440 | reaper(void) |
| 441 | { |
Darren Tucker | b759c9c | 2013-06-02 07:46:16 +1000 | [diff] [blame] | 442 | time_t deadline = 0, now = monotime(); |
Ben Lindstrom | 61d328a | 2002-06-06 21:54:57 +0000 | [diff] [blame] | 443 | Identity *id, *nxt; |
| 444 | int version; |
Ben Lindstrom | 822b634 | 2002-06-23 21:38:49 +0000 | [diff] [blame] | 445 | Idtab *tab; |
Ben Lindstrom | 61d328a | 2002-06-06 21:54:57 +0000 | [diff] [blame] | 446 | |
| 447 | for (version = 1; version < 3; version++) { |
| 448 | tab = idtab_lookup(version); |
| 449 | for (id = TAILQ_FIRST(&tab->idlist); id; id = nxt) { |
| 450 | nxt = TAILQ_NEXT(id, next); |
Darren Tucker | 2812dc9 | 2007-03-21 20:45:06 +1100 | [diff] [blame] | 451 | if (id->death == 0) |
| 452 | continue; |
| 453 | if (now >= id->death) { |
Darren Tucker | cf0d2db | 2007-02-28 21:19:58 +1100 | [diff] [blame] | 454 | debug("expiring key '%s'", id->comment); |
Ben Lindstrom | 61d328a | 2002-06-06 21:54:57 +0000 | [diff] [blame] | 455 | TAILQ_REMOVE(&tab->idlist, id, next); |
| 456 | free_identity(id); |
| 457 | tab->nentries--; |
Darren Tucker | 2812dc9 | 2007-03-21 20:45:06 +1100 | [diff] [blame] | 458 | } else |
| 459 | deadline = (deadline == 0) ? id->death : |
| 460 | MIN(deadline, id->death); |
Ben Lindstrom | 61d328a | 2002-06-06 21:54:57 +0000 | [diff] [blame] | 461 | } |
| 462 | } |
Darren Tucker | 2812dc9 | 2007-03-21 20:45:06 +1100 | [diff] [blame] | 463 | if (deadline == 0 || deadline <= now) |
| 464 | return 0; |
| 465 | else |
| 466 | return (deadline - now); |
Ben Lindstrom | 61d328a | 2002-06-06 21:54:57 +0000 | [diff] [blame] | 467 | } |
| 468 | |
| 469 | static void |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 470 | process_add_identity(SocketEntry *e, int version) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 471 | { |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 472 | Idtab *tab = idtab_lookup(version); |
Damien Miller | 4c7728c | 2007-10-26 14:25:31 +1000 | [diff] [blame] | 473 | Identity *id; |
Darren Tucker | 5511925 | 2013-06-02 07:43:59 +1000 | [diff] [blame] | 474 | int type, success = 0, confirm = 0; |
Damien Miller | f0e9060 | 2013-12-07 10:40:26 +1100 | [diff] [blame] | 475 | char *comment; |
Darren Tucker | 5511925 | 2013-06-02 07:43:59 +1000 | [diff] [blame] | 476 | time_t death = 0; |
Ben Lindstrom | 822b634 | 2002-06-23 21:38:49 +0000 | [diff] [blame] | 477 | Key *k = NULL; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 478 | |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 479 | switch (version) { |
| 480 | case 1: |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 481 | k = key_new_private(KEY_RSA1); |
Ben Lindstrom | c5a7f4f | 2002-06-25 23:19:13 +0000 | [diff] [blame] | 482 | (void) buffer_get_int(&e->request); /* ignored */ |
Ben Lindstrom | 21d1ed8 | 2002-06-06 21:48:57 +0000 | [diff] [blame] | 483 | buffer_get_bignum(&e->request, k->rsa->n); |
| 484 | buffer_get_bignum(&e->request, k->rsa->e); |
| 485 | buffer_get_bignum(&e->request, k->rsa->d); |
| 486 | buffer_get_bignum(&e->request, k->rsa->iqmp); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 487 | |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 488 | /* SSH and SSL have p and q swapped */ |
Ben Lindstrom | 21d1ed8 | 2002-06-06 21:48:57 +0000 | [diff] [blame] | 489 | buffer_get_bignum(&e->request, k->rsa->q); /* p */ |
| 490 | buffer_get_bignum(&e->request, k->rsa->p); /* q */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 491 | |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 492 | /* Generate additional parameters */ |
Ben Lindstrom | f7297dd | 2001-07-04 05:02:23 +0000 | [diff] [blame] | 493 | rsa_generate_additional_parameters(k->rsa); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 494 | |
Damien Miller | 5be9d9e | 2013-12-07 11:24:01 +1100 | [diff] [blame] | 495 | /* enable blinding */ |
Damien Miller | c51d073 | 2003-03-15 11:37:09 +1100 | [diff] [blame] | 496 | if (RSA_blinding_on(k->rsa, NULL) != 1) { |
| 497 | error("process_add_identity: RSA_blinding_on failed"); |
| 498 | key_free(k); |
| 499 | goto send; |
| 500 | } |
| 501 | break; |
Damien Miller | f0e9060 | 2013-12-07 10:40:26 +1100 | [diff] [blame] | 502 | case 2: |
| 503 | k = key_private_deserialize(&e->request); |
| 504 | if (k == NULL) { |
| 505 | buffer_clear(&e->request); |
| 506 | goto send; |
| 507 | } |
| 508 | break; |
Damien Miller | c51d073 | 2003-03-15 11:37:09 +1100 | [diff] [blame] | 509 | } |
Ben Lindstrom | 21d1ed8 | 2002-06-06 21:48:57 +0000 | [diff] [blame] | 510 | comment = buffer_get_string(&e->request, NULL); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 511 | if (k == NULL) { |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 512 | free(comment); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 513 | goto send; |
| 514 | } |
Ben Lindstrom | 4eb4c4e | 2002-06-21 00:04:48 +0000 | [diff] [blame] | 515 | while (buffer_len(&e->request)) { |
Damien Miller | 1cfadab | 2008-06-30 00:05:21 +1000 | [diff] [blame] | 516 | switch ((type = buffer_get_char(&e->request))) { |
Ben Lindstrom | c90f8a9 | 2002-06-21 00:06:54 +0000 | [diff] [blame] | 517 | case SSH_AGENT_CONSTRAIN_LIFETIME: |
Darren Tucker | b759c9c | 2013-06-02 07:46:16 +1000 | [diff] [blame] | 518 | death = monotime() + buffer_get_int(&e->request); |
Ben Lindstrom | 4eb4c4e | 2002-06-21 00:04:48 +0000 | [diff] [blame] | 519 | break; |
Damien Miller | 6c71179 | 2003-01-24 11:36:23 +1100 | [diff] [blame] | 520 | case SSH_AGENT_CONSTRAIN_CONFIRM: |
| 521 | confirm = 1; |
| 522 | break; |
Ben Lindstrom | 4eb4c4e | 2002-06-21 00:04:48 +0000 | [diff] [blame] | 523 | default: |
Damien Miller | 1cfadab | 2008-06-30 00:05:21 +1000 | [diff] [blame] | 524 | error("process_add_identity: " |
| 525 | "Unknown constraint type %d", type); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 526 | free(comment); |
Damien Miller | 1cfadab | 2008-06-30 00:05:21 +1000 | [diff] [blame] | 527 | key_free(k); |
| 528 | goto send; |
Ben Lindstrom | 4eb4c4e | 2002-06-21 00:04:48 +0000 | [diff] [blame] | 529 | } |
| 530 | } |
Damien Miller | 1cfadab | 2008-06-30 00:05:21 +1000 | [diff] [blame] | 531 | success = 1; |
Damien Miller | 53d8148 | 2003-01-22 11:47:19 +1100 | [diff] [blame] | 532 | if (lifetime && !death) |
Darren Tucker | b759c9c | 2013-06-02 07:46:16 +1000 | [diff] [blame] | 533 | death = monotime() + lifetime; |
Damien Miller | 4c7728c | 2007-10-26 14:25:31 +1000 | [diff] [blame] | 534 | if ((id = lookup_identity(k, version)) == NULL) { |
Damien Miller | 7ea845e | 2010-02-12 09:21:02 +1100 | [diff] [blame] | 535 | id = xcalloc(1, sizeof(Identity)); |
Ben Lindstrom | 2b266b7 | 2002-06-21 00:08:39 +0000 | [diff] [blame] | 536 | id->key = k; |
Ben Lindstrom | 2b266b7 | 2002-06-21 00:08:39 +0000 | [diff] [blame] | 537 | TAILQ_INSERT_TAIL(&tab->idlist, id, next); |
| 538 | /* Increment the number of identities. */ |
| 539 | tab->nentries++; |
| 540 | } else { |
| 541 | key_free(k); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 542 | free(id->comment); |
Ben Lindstrom | 61d328a | 2002-06-06 21:54:57 +0000 | [diff] [blame] | 543 | } |
Damien Miller | 4c7728c | 2007-10-26 14:25:31 +1000 | [diff] [blame] | 544 | id->comment = comment; |
| 545 | id->death = death; |
| 546 | id->confirm = confirm; |
Ben Lindstrom | 2b266b7 | 2002-06-21 00:08:39 +0000 | [diff] [blame] | 547 | send: |
Ben Lindstrom | 61d328a | 2002-06-06 21:54:57 +0000 | [diff] [blame] | 548 | buffer_put_int(&e->output, 1); |
| 549 | buffer_put_char(&e->output, |
| 550 | success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE); |
| 551 | } |
| 552 | |
Ben Lindstrom | 2f71704 | 2002-06-06 21:52:03 +0000 | [diff] [blame] | 553 | /* XXX todo: encrypt sensitive data with passphrase */ |
| 554 | static void |
| 555 | process_lock_agent(SocketEntry *e, int lock) |
| 556 | { |
Ben Lindstrom | 2f71704 | 2002-06-06 21:52:03 +0000 | [diff] [blame] | 557 | int success = 0; |
Ben Lindstrom | 822b634 | 2002-06-23 21:38:49 +0000 | [diff] [blame] | 558 | char *passwd; |
Ben Lindstrom | 2f71704 | 2002-06-06 21:52:03 +0000 | [diff] [blame] | 559 | |
| 560 | passwd = buffer_get_string(&e->request, NULL); |
| 561 | if (locked && !lock && strcmp(passwd, lock_passwd) == 0) { |
| 562 | locked = 0; |
Damien Miller | a5103f4 | 2014-02-04 11:20:14 +1100 | [diff] [blame] | 563 | explicit_bzero(lock_passwd, strlen(lock_passwd)); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 564 | free(lock_passwd); |
Ben Lindstrom | 2f71704 | 2002-06-06 21:52:03 +0000 | [diff] [blame] | 565 | lock_passwd = NULL; |
| 566 | success = 1; |
| 567 | } else if (!locked && lock) { |
| 568 | locked = 1; |
| 569 | lock_passwd = xstrdup(passwd); |
| 570 | success = 1; |
| 571 | } |
Damien Miller | a5103f4 | 2014-02-04 11:20:14 +1100 | [diff] [blame] | 572 | explicit_bzero(passwd, strlen(passwd)); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 573 | free(passwd); |
Ben Lindstrom | cb72e4f | 2002-06-21 00:41:51 +0000 | [diff] [blame] | 574 | |
Ben Lindstrom | 2f71704 | 2002-06-06 21:52:03 +0000 | [diff] [blame] | 575 | buffer_put_int(&e->output, 1); |
| 576 | buffer_put_char(&e->output, |
| 577 | success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE); |
Ben Lindstrom | 2f71704 | 2002-06-06 21:52:03 +0000 | [diff] [blame] | 578 | } |
| 579 | |
| 580 | static void |
| 581 | no_identities(SocketEntry *e, u_int type) |
| 582 | { |
| 583 | Buffer msg; |
| 584 | |
| 585 | buffer_init(&msg); |
| 586 | buffer_put_char(&msg, |
| 587 | (type == SSH_AGENTC_REQUEST_RSA_IDENTITIES) ? |
| 588 | SSH_AGENT_RSA_IDENTITIES_ANSWER : SSH2_AGENT_IDENTITIES_ANSWER); |
| 589 | buffer_put_int(&msg, 0); |
| 590 | buffer_put_int(&e->output, buffer_len(&msg)); |
| 591 | buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg)); |
| 592 | buffer_free(&msg); |
| 593 | } |
Ben Lindstrom | 3f47163 | 2001-07-04 03:53:15 +0000 | [diff] [blame] | 594 | |
Damien Miller | 7ea845e | 2010-02-12 09:21:02 +1100 | [diff] [blame] | 595 | #ifdef ENABLE_PKCS11 |
Ben Lindstrom | 3f47163 | 2001-07-04 03:53:15 +0000 | [diff] [blame] | 596 | static void |
Damien Miller | 1cfadab | 2008-06-30 00:05:21 +1000 | [diff] [blame] | 597 | process_add_smartcard_key(SocketEntry *e) |
Ben Lindstrom | 3f47163 | 2001-07-04 03:53:15 +0000 | [diff] [blame] | 598 | { |
Damien Miller | 7ea845e | 2010-02-12 09:21:02 +1100 | [diff] [blame] | 599 | char *provider = NULL, *pin; |
Darren Tucker | 5511925 | 2013-06-02 07:43:59 +1000 | [diff] [blame] | 600 | int i, type, version, count = 0, success = 0, confirm = 0; |
| 601 | time_t death = 0; |
Damien Miller | 7ea845e | 2010-02-12 09:21:02 +1100 | [diff] [blame] | 602 | Key **keys = NULL, *k; |
Ben Lindstrom | 822b634 | 2002-06-23 21:38:49 +0000 | [diff] [blame] | 603 | Identity *id; |
| 604 | Idtab *tab; |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 605 | |
Damien Miller | 7ea845e | 2010-02-12 09:21:02 +1100 | [diff] [blame] | 606 | provider = buffer_get_string(&e->request, NULL); |
Ben Lindstrom | 21d1ed8 | 2002-06-06 21:48:57 +0000 | [diff] [blame] | 607 | pin = buffer_get_string(&e->request, NULL); |
Damien Miller | d94f20d | 2003-06-11 22:06:33 +1000 | [diff] [blame] | 608 | |
| 609 | while (buffer_len(&e->request)) { |
Damien Miller | 1cfadab | 2008-06-30 00:05:21 +1000 | [diff] [blame] | 610 | switch ((type = buffer_get_char(&e->request))) { |
Damien Miller | d94f20d | 2003-06-11 22:06:33 +1000 | [diff] [blame] | 611 | case SSH_AGENT_CONSTRAIN_LIFETIME: |
Darren Tucker | b759c9c | 2013-06-02 07:46:16 +1000 | [diff] [blame] | 612 | death = monotime() + buffer_get_int(&e->request); |
Damien Miller | d94f20d | 2003-06-11 22:06:33 +1000 | [diff] [blame] | 613 | break; |
| 614 | case SSH_AGENT_CONSTRAIN_CONFIRM: |
| 615 | confirm = 1; |
| 616 | break; |
| 617 | default: |
Damien Miller | 1cfadab | 2008-06-30 00:05:21 +1000 | [diff] [blame] | 618 | error("process_add_smartcard_key: " |
| 619 | "Unknown constraint type %d", type); |
Damien Miller | 1cfadab | 2008-06-30 00:05:21 +1000 | [diff] [blame] | 620 | goto send; |
Damien Miller | d94f20d | 2003-06-11 22:06:33 +1000 | [diff] [blame] | 621 | } |
| 622 | } |
| 623 | if (lifetime && !death) |
Darren Tucker | b759c9c | 2013-06-02 07:46:16 +1000 | [diff] [blame] | 624 | death = monotime() + lifetime; |
Damien Miller | d94f20d | 2003-06-11 22:06:33 +1000 | [diff] [blame] | 625 | |
Damien Miller | 7ea845e | 2010-02-12 09:21:02 +1100 | [diff] [blame] | 626 | count = pkcs11_add_provider(provider, pin, &keys); |
| 627 | for (i = 0; i < count; i++) { |
Ben Lindstrom | 0936a5b | 2002-03-26 03:17:42 +0000 | [diff] [blame] | 628 | k = keys[i]; |
| 629 | version = k->type == KEY_RSA1 ? 1 : 2; |
| 630 | tab = idtab_lookup(version); |
| 631 | if (lookup_identity(k, version) == NULL) { |
Damien Miller | 7ea845e | 2010-02-12 09:21:02 +1100 | [diff] [blame] | 632 | id = xcalloc(1, sizeof(Identity)); |
Ben Lindstrom | 0936a5b | 2002-03-26 03:17:42 +0000 | [diff] [blame] | 633 | id->key = k; |
Damien Miller | 7ea845e | 2010-02-12 09:21:02 +1100 | [diff] [blame] | 634 | id->provider = xstrdup(provider); |
| 635 | id->comment = xstrdup(provider); /* XXX */ |
Damien Miller | d94f20d | 2003-06-11 22:06:33 +1000 | [diff] [blame] | 636 | id->death = death; |
| 637 | id->confirm = confirm; |
Ben Lindstrom | 0936a5b | 2002-03-26 03:17:42 +0000 | [diff] [blame] | 638 | TAILQ_INSERT_TAIL(&tab->idlist, id, next); |
| 639 | tab->nentries++; |
| 640 | success = 1; |
| 641 | } else { |
| 642 | key_free(k); |
| 643 | } |
| 644 | keys[i] = NULL; |
Ben Lindstrom | 3f47163 | 2001-07-04 03:53:15 +0000 | [diff] [blame] | 645 | } |
Ben Lindstrom | 3f47163 | 2001-07-04 03:53:15 +0000 | [diff] [blame] | 646 | send: |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 647 | free(pin); |
| 648 | free(provider); |
| 649 | free(keys); |
Ben Lindstrom | 3f47163 | 2001-07-04 03:53:15 +0000 | [diff] [blame] | 650 | buffer_put_int(&e->output, 1); |
| 651 | buffer_put_char(&e->output, |
| 652 | success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE); |
| 653 | } |
| 654 | |
| 655 | static void |
| 656 | process_remove_smartcard_key(SocketEntry *e) |
| 657 | { |
Damien Miller | 7ea845e | 2010-02-12 09:21:02 +1100 | [diff] [blame] | 658 | char *provider = NULL, *pin = NULL; |
| 659 | int version, success = 0; |
| 660 | Identity *id, *nxt; |
Ben Lindstrom | 822b634 | 2002-06-23 21:38:49 +0000 | [diff] [blame] | 661 | Idtab *tab; |
Ben Lindstrom | 3f47163 | 2001-07-04 03:53:15 +0000 | [diff] [blame] | 662 | |
Damien Miller | 7ea845e | 2010-02-12 09:21:02 +1100 | [diff] [blame] | 663 | provider = buffer_get_string(&e->request, NULL); |
Ben Lindstrom | 21d1ed8 | 2002-06-06 21:48:57 +0000 | [diff] [blame] | 664 | pin = buffer_get_string(&e->request, NULL); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 665 | free(pin); |
Ben Lindstrom | 3f47163 | 2001-07-04 03:53:15 +0000 | [diff] [blame] | 666 | |
Damien Miller | 7ea845e | 2010-02-12 09:21:02 +1100 | [diff] [blame] | 667 | for (version = 1; version < 3; version++) { |
| 668 | tab = idtab_lookup(version); |
| 669 | for (id = TAILQ_FIRST(&tab->idlist); id; id = nxt) { |
| 670 | nxt = TAILQ_NEXT(id, next); |
Damien Miller | 0b36c83 | 2013-12-29 17:45:51 +1100 | [diff] [blame] | 671 | /* Skip file--based keys */ |
| 672 | if (id->provider == NULL) |
| 673 | continue; |
Damien Miller | 7ea845e | 2010-02-12 09:21:02 +1100 | [diff] [blame] | 674 | if (!strcmp(provider, id->provider)) { |
| 675 | TAILQ_REMOVE(&tab->idlist, id, next); |
| 676 | free_identity(id); |
| 677 | tab->nentries--; |
| 678 | } |
Ben Lindstrom | 3f47163 | 2001-07-04 03:53:15 +0000 | [diff] [blame] | 679 | } |
Ben Lindstrom | 3f47163 | 2001-07-04 03:53:15 +0000 | [diff] [blame] | 680 | } |
Damien Miller | 7ea845e | 2010-02-12 09:21:02 +1100 | [diff] [blame] | 681 | if (pkcs11_del_provider(provider) == 0) |
| 682 | success = 1; |
| 683 | else |
| 684 | error("process_remove_smartcard_key:" |
| 685 | " pkcs11_del_provider failed"); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 686 | free(provider); |
Ben Lindstrom | 3f47163 | 2001-07-04 03:53:15 +0000 | [diff] [blame] | 687 | buffer_put_int(&e->output, 1); |
| 688 | buffer_put_char(&e->output, |
| 689 | success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE); |
| 690 | } |
Damien Miller | 7ea845e | 2010-02-12 09:21:02 +1100 | [diff] [blame] | 691 | #endif /* ENABLE_PKCS11 */ |
Ben Lindstrom | 3f47163 | 2001-07-04 03:53:15 +0000 | [diff] [blame] | 692 | |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 693 | /* dispatch incoming messages */ |
| 694 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 695 | static void |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 696 | process_message(SocketEntry *e) |
| 697 | { |
Ben Lindstrom | 822b634 | 2002-06-23 21:38:49 +0000 | [diff] [blame] | 698 | u_int msg_len, type; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 699 | u_char *cp; |
Ben Lindstrom | 61d328a | 2002-06-06 21:54:57 +0000 | [diff] [blame] | 700 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 701 | if (buffer_len(&e->input) < 5) |
| 702 | return; /* Incomplete message. */ |
Damien Miller | 708d21c | 2002-01-22 23:18:15 +1100 | [diff] [blame] | 703 | cp = buffer_ptr(&e->input); |
Damien Miller | 3f94188 | 2006-03-31 23:13:02 +1100 | [diff] [blame] | 704 | msg_len = get_u32(cp); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 705 | if (msg_len > 256 * 1024) { |
Damien Miller | 58f3486 | 2002-09-04 16:31:21 +1000 | [diff] [blame] | 706 | close_socket(e); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 707 | return; |
| 708 | } |
| 709 | if (buffer_len(&e->input) < msg_len + 4) |
| 710 | return; |
Ben Lindstrom | 21d1ed8 | 2002-06-06 21:48:57 +0000 | [diff] [blame] | 711 | |
| 712 | /* move the current input to e->request */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 713 | buffer_consume(&e->input, 4); |
Ben Lindstrom | 21d1ed8 | 2002-06-06 21:48:57 +0000 | [diff] [blame] | 714 | buffer_clear(&e->request); |
| 715 | buffer_append(&e->request, buffer_ptr(&e->input), msg_len); |
| 716 | buffer_consume(&e->input, msg_len); |
| 717 | type = buffer_get_char(&e->request); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 718 | |
Ben Lindstrom | 2f71704 | 2002-06-06 21:52:03 +0000 | [diff] [blame] | 719 | /* check wheter agent is locked */ |
| 720 | if (locked && type != SSH_AGENTC_UNLOCK) { |
| 721 | buffer_clear(&e->request); |
| 722 | switch (type) { |
| 723 | case SSH_AGENTC_REQUEST_RSA_IDENTITIES: |
| 724 | case SSH2_AGENTC_REQUEST_IDENTITIES: |
| 725 | /* send empty lists */ |
| 726 | no_identities(e, type); |
| 727 | break; |
| 728 | default: |
| 729 | /* send a fail message for all other request types */ |
| 730 | buffer_put_int(&e->output, 1); |
| 731 | buffer_put_char(&e->output, SSH_AGENT_FAILURE); |
| 732 | } |
| 733 | return; |
| 734 | } |
| 735 | |
Ben Lindstrom | 3f47163 | 2001-07-04 03:53:15 +0000 | [diff] [blame] | 736 | debug("type %d", type); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 737 | switch (type) { |
Ben Lindstrom | 2f71704 | 2002-06-06 21:52:03 +0000 | [diff] [blame] | 738 | case SSH_AGENTC_LOCK: |
| 739 | case SSH_AGENTC_UNLOCK: |
| 740 | process_lock_agent(e, type == SSH_AGENTC_LOCK); |
| 741 | break; |
Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 742 | #ifdef WITH_SSH1 |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 743 | /* ssh1 */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 744 | case SSH_AGENTC_RSA_CHALLENGE: |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 745 | process_authentication_challenge1(e); |
| 746 | break; |
| 747 | case SSH_AGENTC_REQUEST_RSA_IDENTITIES: |
| 748 | process_request_identities(e, 1); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 749 | break; |
| 750 | case SSH_AGENTC_ADD_RSA_IDENTITY: |
Ben Lindstrom | 2b266b7 | 2002-06-21 00:08:39 +0000 | [diff] [blame] | 751 | case SSH_AGENTC_ADD_RSA_ID_CONSTRAINED: |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 752 | process_add_identity(e, 1); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 753 | break; |
| 754 | case SSH_AGENTC_REMOVE_RSA_IDENTITY: |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 755 | process_remove_identity(e, 1); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 756 | break; |
| 757 | case SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES: |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 758 | process_remove_all_identities(e, 1); |
| 759 | break; |
Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 760 | #endif |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 761 | /* ssh2 */ |
| 762 | case SSH2_AGENTC_SIGN_REQUEST: |
| 763 | process_sign_request2(e); |
| 764 | break; |
| 765 | case SSH2_AGENTC_REQUEST_IDENTITIES: |
| 766 | process_request_identities(e, 2); |
| 767 | break; |
| 768 | case SSH2_AGENTC_ADD_IDENTITY: |
Ben Lindstrom | 2b266b7 | 2002-06-21 00:08:39 +0000 | [diff] [blame] | 769 | case SSH2_AGENTC_ADD_ID_CONSTRAINED: |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 770 | process_add_identity(e, 2); |
| 771 | break; |
| 772 | case SSH2_AGENTC_REMOVE_IDENTITY: |
| 773 | process_remove_identity(e, 2); |
| 774 | break; |
| 775 | case SSH2_AGENTC_REMOVE_ALL_IDENTITIES: |
| 776 | process_remove_all_identities(e, 2); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 777 | break; |
Damien Miller | 7ea845e | 2010-02-12 09:21:02 +1100 | [diff] [blame] | 778 | #ifdef ENABLE_PKCS11 |
Ben Lindstrom | 3f47163 | 2001-07-04 03:53:15 +0000 | [diff] [blame] | 779 | case SSH_AGENTC_ADD_SMARTCARD_KEY: |
Damien Miller | d94f20d | 2003-06-11 22:06:33 +1000 | [diff] [blame] | 780 | case SSH_AGENTC_ADD_SMARTCARD_KEY_CONSTRAINED: |
Ben Lindstrom | 3f47163 | 2001-07-04 03:53:15 +0000 | [diff] [blame] | 781 | process_add_smartcard_key(e); |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 782 | break; |
Ben Lindstrom | 3f47163 | 2001-07-04 03:53:15 +0000 | [diff] [blame] | 783 | case SSH_AGENTC_REMOVE_SMARTCARD_KEY: |
| 784 | process_remove_smartcard_key(e); |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 785 | break; |
Damien Miller | 7ea845e | 2010-02-12 09:21:02 +1100 | [diff] [blame] | 786 | #endif /* ENABLE_PKCS11 */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 787 | default: |
| 788 | /* Unknown message. Respond with failure. */ |
| 789 | error("Unknown message %d", type); |
Ben Lindstrom | 21d1ed8 | 2002-06-06 21:48:57 +0000 | [diff] [blame] | 790 | buffer_clear(&e->request); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 791 | buffer_put_int(&e->output, 1); |
| 792 | buffer_put_char(&e->output, SSH_AGENT_FAILURE); |
| 793 | break; |
| 794 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 795 | } |
| 796 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 797 | static void |
Ben Lindstrom | 65366a8 | 2001-12-06 16:32:47 +0000 | [diff] [blame] | 798 | new_socket(sock_type type, int fd) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 799 | { |
Darren Tucker | fb16b24 | 2003-09-22 21:04:23 +1000 | [diff] [blame] | 800 | u_int i, old_alloc, new_alloc; |
Ben Lindstrom | 822b634 | 2002-06-23 21:38:49 +0000 | [diff] [blame] | 801 | |
Damien Miller | 232711f | 2004-06-15 10:35:30 +1000 | [diff] [blame] | 802 | set_nonblock(fd); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 803 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 804 | if (fd > max_fd) |
| 805 | max_fd = fd; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 806 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 807 | for (i = 0; i < sockets_alloc; i++) |
| 808 | if (sockets[i].type == AUTH_UNUSED) { |
| 809 | sockets[i].fd = fd; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 810 | buffer_init(&sockets[i].input); |
| 811 | buffer_init(&sockets[i].output); |
Ben Lindstrom | 21d1ed8 | 2002-06-06 21:48:57 +0000 | [diff] [blame] | 812 | buffer_init(&sockets[i].request); |
Darren Tucker | fb16b24 | 2003-09-22 21:04:23 +1000 | [diff] [blame] | 813 | sockets[i].type = type; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 814 | return; |
| 815 | } |
| 816 | old_alloc = sockets_alloc; |
Darren Tucker | fb16b24 | 2003-09-22 21:04:23 +1000 | [diff] [blame] | 817 | new_alloc = sockets_alloc + 10; |
Damien Miller | 3681209 | 2006-03-26 14:22:47 +1100 | [diff] [blame] | 818 | sockets = xrealloc(sockets, new_alloc, sizeof(sockets[0])); |
Darren Tucker | fb16b24 | 2003-09-22 21:04:23 +1000 | [diff] [blame] | 819 | for (i = old_alloc; i < new_alloc; i++) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 820 | sockets[i].type = AUTH_UNUSED; |
Darren Tucker | fb16b24 | 2003-09-22 21:04:23 +1000 | [diff] [blame] | 821 | sockets_alloc = new_alloc; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 822 | sockets[old_alloc].fd = fd; |
| 823 | buffer_init(&sockets[old_alloc].input); |
| 824 | buffer_init(&sockets[old_alloc].output); |
Ben Lindstrom | 21d1ed8 | 2002-06-06 21:48:57 +0000 | [diff] [blame] | 825 | buffer_init(&sockets[old_alloc].request); |
Darren Tucker | fb16b24 | 2003-09-22 21:04:23 +1000 | [diff] [blame] | 826 | sockets[old_alloc].type = type; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 827 | } |
| 828 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 829 | static int |
Darren Tucker | 2812dc9 | 2007-03-21 20:45:06 +1100 | [diff] [blame] | 830 | prepare_select(fd_set **fdrp, fd_set **fdwp, int *fdl, u_int *nallocp, |
| 831 | struct timeval **tvpp) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 832 | { |
Darren Tucker | 5511925 | 2013-06-02 07:43:59 +1000 | [diff] [blame] | 833 | u_int i, sz; |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 834 | int n = 0; |
Darren Tucker | 2812dc9 | 2007-03-21 20:45:06 +1100 | [diff] [blame] | 835 | static struct timeval tv; |
Darren Tucker | 5511925 | 2013-06-02 07:43:59 +1000 | [diff] [blame] | 836 | time_t deadline; |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 837 | |
| 838 | for (i = 0; i < sockets_alloc; i++) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 839 | switch (sockets[i].type) { |
| 840 | case AUTH_SOCKET: |
| 841 | case AUTH_CONNECTION: |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 842 | n = MAX(n, sockets[i].fd); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 843 | break; |
| 844 | case AUTH_UNUSED: |
| 845 | break; |
| 846 | default: |
| 847 | fatal("Unknown socket type %d", sockets[i].type); |
| 848 | break; |
| 849 | } |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 850 | } |
| 851 | |
| 852 | sz = howmany(n+1, NFDBITS) * sizeof(fd_mask); |
Ben Lindstrom | a3d5a4c | 2001-07-18 15:58:08 +0000 | [diff] [blame] | 853 | if (*fdrp == NULL || sz > *nallocp) { |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 854 | free(*fdrp); |
| 855 | free(*fdwp); |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 856 | *fdrp = xmalloc(sz); |
| 857 | *fdwp = xmalloc(sz); |
Ben Lindstrom | a3d5a4c | 2001-07-18 15:58:08 +0000 | [diff] [blame] | 858 | *nallocp = sz; |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 859 | } |
Ben Lindstrom | a3d5a4c | 2001-07-18 15:58:08 +0000 | [diff] [blame] | 860 | if (n < *fdl) |
| 861 | debug("XXX shrink: %d < %d", n, *fdl); |
| 862 | *fdl = n; |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 863 | memset(*fdrp, 0, sz); |
| 864 | memset(*fdwp, 0, sz); |
| 865 | |
| 866 | for (i = 0; i < sockets_alloc; i++) { |
| 867 | switch (sockets[i].type) { |
| 868 | case AUTH_SOCKET: |
| 869 | case AUTH_CONNECTION: |
| 870 | FD_SET(sockets[i].fd, *fdrp); |
| 871 | if (buffer_len(&sockets[i].output) > 0) |
| 872 | FD_SET(sockets[i].fd, *fdwp); |
| 873 | break; |
| 874 | default: |
| 875 | break; |
| 876 | } |
| 877 | } |
Darren Tucker | 2812dc9 | 2007-03-21 20:45:06 +1100 | [diff] [blame] | 878 | deadline = reaper(); |
| 879 | if (parent_alive_interval != 0) |
| 880 | deadline = (deadline == 0) ? parent_alive_interval : |
| 881 | MIN(deadline, parent_alive_interval); |
| 882 | if (deadline == 0) { |
| 883 | *tvpp = NULL; |
| 884 | } else { |
| 885 | tv.tv_sec = deadline; |
| 886 | tv.tv_usec = 0; |
| 887 | *tvpp = &tv; |
| 888 | } |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 889 | return (1); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 890 | } |
| 891 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 892 | static void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 893 | after_select(fd_set *readset, fd_set *writeset) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 894 | { |
Ben Lindstrom | 822b634 | 2002-06-23 21:38:49 +0000 | [diff] [blame] | 895 | struct sockaddr_un sunaddr; |
Damien Miller | 7684ee1 | 2000-03-17 23:40:15 +1100 | [diff] [blame] | 896 | socklen_t slen; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 897 | char buf[1024]; |
Ben Lindstrom | 822b634 | 2002-06-23 21:38:49 +0000 | [diff] [blame] | 898 | int len, sock; |
Darren Tucker | 72473c6 | 2009-10-07 09:01:03 +1100 | [diff] [blame] | 899 | u_int i, orig_alloc; |
Damien Miller | 538f181 | 2002-09-12 09:51:10 +1000 | [diff] [blame] | 900 | uid_t euid; |
| 901 | gid_t egid; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 902 | |
Darren Tucker | 72473c6 | 2009-10-07 09:01:03 +1100 | [diff] [blame] | 903 | for (i = 0, orig_alloc = sockets_alloc; i < orig_alloc; i++) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 904 | switch (sockets[i].type) { |
| 905 | case AUTH_UNUSED: |
| 906 | break; |
| 907 | case AUTH_SOCKET: |
| 908 | if (FD_ISSET(sockets[i].fd, readset)) { |
Damien Miller | 7684ee1 | 2000-03-17 23:40:15 +1100 | [diff] [blame] | 909 | slen = sizeof(sunaddr); |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 910 | sock = accept(sockets[i].fd, |
Damien Miller | 9096740 | 2006-03-26 14:07:26 +1100 | [diff] [blame] | 911 | (struct sockaddr *)&sunaddr, &slen); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 912 | if (sock < 0) { |
Damien Miller | c653b89 | 2002-02-08 22:05:41 +1100 | [diff] [blame] | 913 | error("accept from AUTH_SOCKET: %s", |
| 914 | strerror(errno)); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 915 | break; |
| 916 | } |
Damien Miller | 538f181 | 2002-09-12 09:51:10 +1000 | [diff] [blame] | 917 | if (getpeereid(sock, &euid, &egid) < 0) { |
| 918 | error("getpeereid %d failed: %s", |
| 919 | sock, strerror(errno)); |
| 920 | close(sock); |
| 921 | break; |
| 922 | } |
Damien Miller | af9de38 | 2002-10-03 11:54:35 +1000 | [diff] [blame] | 923 | if ((euid != 0) && (getuid() != euid)) { |
Damien Miller | 538f181 | 2002-09-12 09:51:10 +1000 | [diff] [blame] | 924 | error("uid mismatch: " |
Damien Miller | db30b12 | 2002-09-19 11:46:58 +1000 | [diff] [blame] | 925 | "peer euid %u != uid %u", |
| 926 | (u_int) euid, (u_int) getuid()); |
Damien Miller | 538f181 | 2002-09-12 09:51:10 +1000 | [diff] [blame] | 927 | close(sock); |
| 928 | break; |
| 929 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 930 | new_socket(AUTH_CONNECTION, sock); |
| 931 | } |
| 932 | break; |
| 933 | case AUTH_CONNECTION: |
| 934 | if (buffer_len(&sockets[i].output) > 0 && |
| 935 | FD_ISSET(sockets[i].fd, writeset)) { |
Darren Tucker | 72473c6 | 2009-10-07 09:01:03 +1100 | [diff] [blame] | 936 | len = write(sockets[i].fd, |
| 937 | buffer_ptr(&sockets[i].output), |
| 938 | buffer_len(&sockets[i].output)); |
| 939 | if (len == -1 && (errno == EAGAIN || |
| 940 | errno == EWOULDBLOCK || |
| 941 | errno == EINTR)) |
| 942 | continue; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 943 | if (len <= 0) { |
Damien Miller | 58f3486 | 2002-09-04 16:31:21 +1000 | [diff] [blame] | 944 | close_socket(&sockets[i]); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 945 | break; |
| 946 | } |
| 947 | buffer_consume(&sockets[i].output, len); |
| 948 | } |
| 949 | if (FD_ISSET(sockets[i].fd, readset)) { |
Darren Tucker | 72473c6 | 2009-10-07 09:01:03 +1100 | [diff] [blame] | 950 | len = read(sockets[i].fd, buf, sizeof(buf)); |
| 951 | if (len == -1 && (errno == EAGAIN || |
| 952 | errno == EWOULDBLOCK || |
| 953 | errno == EINTR)) |
| 954 | continue; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 955 | if (len <= 0) { |
Damien Miller | 58f3486 | 2002-09-04 16:31:21 +1000 | [diff] [blame] | 956 | close_socket(&sockets[i]); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 957 | break; |
| 958 | } |
| 959 | buffer_append(&sockets[i].input, buf, len); |
| 960 | process_message(&sockets[i]); |
| 961 | } |
| 962 | break; |
| 963 | default: |
| 964 | fatal("Unknown type %d", sockets[i].type); |
| 965 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 966 | } |
| 967 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 968 | static void |
Darren Tucker | 6fa8abd | 2003-09-22 21:10:21 +1000 | [diff] [blame] | 969 | cleanup_socket(void) |
Damien Miller | 792c511 | 1999-10-29 09:47:09 +1000 | [diff] [blame] | 970 | { |
Ben Lindstrom | 77808ab | 2001-01-26 05:10:34 +0000 | [diff] [blame] | 971 | if (socket_name[0]) |
| 972 | unlink(socket_name); |
| 973 | if (socket_dir[0]) |
| 974 | rmdir(socket_dir); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 975 | } |
| 976 | |
Darren Tucker | 3e33cec | 2003-10-02 16:12:36 +1000 | [diff] [blame] | 977 | void |
Damien Miller | 792c511 | 1999-10-29 09:47:09 +1000 | [diff] [blame] | 978 | cleanup_exit(int i) |
| 979 | { |
Darren Tucker | 6fa8abd | 2003-09-22 21:10:21 +1000 | [diff] [blame] | 980 | cleanup_socket(); |
| 981 | _exit(i); |
Damien Miller | 792c511 | 1999-10-29 09:47:09 +1000 | [diff] [blame] | 982 | } |
| 983 | |
Damien Miller | 1c13bd8 | 2006-03-26 14:28:14 +1100 | [diff] [blame] | 984 | /*ARGSUSED*/ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 985 | static void |
Ben Lindstrom | 77808ab | 2001-01-26 05:10:34 +0000 | [diff] [blame] | 986 | cleanup_handler(int sig) |
| 987 | { |
Darren Tucker | 6fa8abd | 2003-09-22 21:10:21 +1000 | [diff] [blame] | 988 | cleanup_socket(); |
Damien Miller | 7ea845e | 2010-02-12 09:21:02 +1100 | [diff] [blame] | 989 | #ifdef ENABLE_PKCS11 |
| 990 | pkcs11_terminate(); |
| 991 | #endif |
Ben Lindstrom | 77808ab | 2001-01-26 05:10:34 +0000 | [diff] [blame] | 992 | _exit(2); |
| 993 | } |
| 994 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 995 | static void |
Darren Tucker | 2812dc9 | 2007-03-21 20:45:06 +1100 | [diff] [blame] | 996 | check_parent_exists(void) |
Ben Lindstrom | 0250da0 | 2001-07-22 20:44:00 +0000 | [diff] [blame] | 997 | { |
Darren Tucker | 3e78a51 | 2011-06-03 14:14:16 +1000 | [diff] [blame] | 998 | /* |
| 999 | * If our parent has exited then getppid() will return (pid_t)1, |
| 1000 | * so testing for that should be safe. |
| 1001 | */ |
| 1002 | if (parent_pid != -1 && getppid() != parent_pid) { |
Ben Lindstrom | 0250da0 | 2001-07-22 20:44:00 +0000 | [diff] [blame] | 1003 | /* printf("Parent has died - Authentication agent exiting.\n"); */ |
Darren Tucker | 2812dc9 | 2007-03-21 20:45:06 +1100 | [diff] [blame] | 1004 | cleanup_socket(); |
| 1005 | _exit(2); |
Ben Lindstrom | 0250da0 | 2001-07-22 20:44:00 +0000 | [diff] [blame] | 1006 | } |
Ben Lindstrom | 0250da0 | 2001-07-22 20:44:00 +0000 | [diff] [blame] | 1007 | } |
| 1008 | |
| 1009 | static void |
Ben Lindstrom | 28072eb | 2001-02-10 23:13:41 +0000 | [diff] [blame] | 1010 | usage(void) |
Damien Miller | 792c511 | 1999-10-29 09:47:09 +1000 | [diff] [blame] | 1011 | { |
Damien Miller | f0858de | 2014-04-20 13:01:30 +1000 | [diff] [blame] | 1012 | fprintf(stderr, |
| 1013 | "usage: ssh-agent [-c | -s] [-d] [-a bind_address] [-t life]\n" |
| 1014 | " [command [arg ...]]\n" |
| 1015 | " ssh-agent [-c | -s] -k\n"); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1016 | exit(1); |
Damien Miller | 792c511 | 1999-10-29 09:47:09 +1000 | [diff] [blame] | 1017 | } |
| 1018 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1019 | int |
| 1020 | main(int ac, char **av) |
| 1021 | { |
Damien Miller | 6c71179 | 2003-01-24 11:36:23 +1100 | [diff] [blame] | 1022 | int c_flag = 0, d_flag = 0, k_flag = 0, s_flag = 0; |
Darren Tucker | cf0d2db | 2007-02-28 21:19:58 +1100 | [diff] [blame] | 1023 | int sock, fd, ch, result, saved_errno; |
Darren Tucker | c7a6fc4 | 2004-08-13 21:18:00 +1000 | [diff] [blame] | 1024 | u_int nalloc; |
Ben Lindstrom | 822b634 | 2002-06-23 21:38:49 +0000 | [diff] [blame] | 1025 | char *shell, *format, *pidstr, *agentsocket = NULL; |
| 1026 | fd_set *readsetp = NULL, *writesetp = NULL; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1027 | struct sockaddr_un sunaddr; |
Ben Lindstrom | 2c467a2 | 2000-12-27 04:57:41 +0000 | [diff] [blame] | 1028 | #ifdef HAVE_SETRLIMIT |
Ben Lindstrom | c72745a | 2000-12-02 19:03:54 +0000 | [diff] [blame] | 1029 | struct rlimit rlim; |
Ben Lindstrom | 2c467a2 | 2000-12-27 04:57:41 +0000 | [diff] [blame] | 1030 | #endif |
Ben Lindstrom | 3524d69 | 2001-05-03 22:59:24 +0000 | [diff] [blame] | 1031 | int prev_mask; |
Damien Miller | 615f939 | 2000-05-17 22:53:33 +1000 | [diff] [blame] | 1032 | extern int optind; |
Ben Lindstrom | 883844d | 2002-06-23 00:20:50 +0000 | [diff] [blame] | 1033 | extern char *optarg; |
Ben Lindstrom | 822b634 | 2002-06-23 21:38:49 +0000 | [diff] [blame] | 1034 | pid_t pid; |
| 1035 | char pidstrbuf[1 + 3 * sizeof pid]; |
Darren Tucker | 2812dc9 | 2007-03-21 20:45:06 +1100 | [diff] [blame] | 1036 | struct timeval *tvp = NULL; |
Darren Tucker | 9013323 | 2009-06-21 17:50:15 +1000 | [diff] [blame] | 1037 | size_t len; |
Kevin Steves | de41bc6 | 2000-12-14 00:04:08 +0000 | [diff] [blame] | 1038 | |
Darren Tucker | ce321d8 | 2005-10-03 18:11:24 +1000 | [diff] [blame] | 1039 | /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */ |
| 1040 | sanitise_stdfd(); |
| 1041 | |
Damien Miller | 6cffb9a | 2002-09-04 16:20:26 +1000 | [diff] [blame] | 1042 | /* drop */ |
| 1043 | setegid(getgid()); |
| 1044 | setgid(getgid()); |
| 1045 | |
Damien Miller | 6c4914a | 2004-03-03 11:08:59 +1100 | [diff] [blame] | 1046 | #if defined(HAVE_PRCTL) && defined(PR_SET_DUMPABLE) |
| 1047 | /* Disable ptrace on Linux without sgid bit */ |
| 1048 | prctl(PR_SET_DUMPABLE, 0); |
| 1049 | #endif |
| 1050 | |
Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 1051 | #ifdef WITH_OPENSSL |
Damien Miller | 4314c2b | 2010-09-10 11:12:09 +1000 | [diff] [blame] | 1052 | OpenSSL_add_all_algorithms(); |
Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 1053 | #endif |
Ben Lindstrom | d09fcf5 | 2001-03-29 00:29:54 +0000 | [diff] [blame] | 1054 | |
Damien Miller | 59d3d5b | 2003-08-22 09:34:41 +1000 | [diff] [blame] | 1055 | __progname = ssh_get_progname(av[0]); |
Damien Miller | 60bc517 | 2001-03-19 09:38:15 +1100 | [diff] [blame] | 1056 | seed_rng(); |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 1057 | |
Damien Miller | 53d8148 | 2003-01-22 11:47:19 +1100 | [diff] [blame] | 1058 | while ((ch = getopt(ac, av, "cdksa:t:")) != -1) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1059 | switch (ch) { |
| 1060 | case 'c': |
| 1061 | if (s_flag) |
| 1062 | usage(); |
| 1063 | c_flag++; |
| 1064 | break; |
| 1065 | case 'k': |
| 1066 | k_flag++; |
| 1067 | break; |
| 1068 | case 's': |
| 1069 | if (c_flag) |
| 1070 | usage(); |
| 1071 | s_flag++; |
| 1072 | break; |
Ben Lindstrom | d94580c | 2001-07-04 03:48:02 +0000 | [diff] [blame] | 1073 | case 'd': |
| 1074 | if (d_flag) |
| 1075 | usage(); |
| 1076 | d_flag++; |
| 1077 | break; |
Ben Lindstrom | b7788f3 | 2002-06-06 21:46:08 +0000 | [diff] [blame] | 1078 | case 'a': |
| 1079 | agentsocket = optarg; |
| 1080 | break; |
Damien Miller | 53d8148 | 2003-01-22 11:47:19 +1100 | [diff] [blame] | 1081 | case 't': |
| 1082 | if ((lifetime = convtime(optarg)) == -1) { |
| 1083 | fprintf(stderr, "Invalid lifetime\n"); |
| 1084 | usage(); |
| 1085 | } |
| 1086 | break; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1087 | default: |
| 1088 | usage(); |
| 1089 | } |
Damien Miller | 792c511 | 1999-10-29 09:47:09 +1000 | [diff] [blame] | 1090 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1091 | ac -= optind; |
| 1092 | av += optind; |
Damien Miller | 792c511 | 1999-10-29 09:47:09 +1000 | [diff] [blame] | 1093 | |
Ben Lindstrom | d94580c | 2001-07-04 03:48:02 +0000 | [diff] [blame] | 1094 | if (ac > 0 && (c_flag || k_flag || s_flag || d_flag)) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1095 | usage(); |
Damien Miller | 792c511 | 1999-10-29 09:47:09 +1000 | [diff] [blame] | 1096 | |
Ben Lindstrom | eecdf23 | 2002-04-02 21:03:51 +0000 | [diff] [blame] | 1097 | if (ac == 0 && !c_flag && !s_flag) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1098 | shell = getenv("SHELL"); |
Darren Tucker | 9013323 | 2009-06-21 17:50:15 +1000 | [diff] [blame] | 1099 | if (shell != NULL && (len = strlen(shell)) > 2 && |
| 1100 | strncmp(shell + len - 3, "csh", 3) == 0) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1101 | c_flag = 1; |
Damien Miller | 792c511 | 1999-10-29 09:47:09 +1000 | [diff] [blame] | 1102 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1103 | if (k_flag) { |
Damien Miller | 89c3fe4 | 2006-03-31 23:11:28 +1100 | [diff] [blame] | 1104 | const char *errstr = NULL; |
| 1105 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1106 | pidstr = getenv(SSH_AGENTPID_ENV_NAME); |
| 1107 | if (pidstr == NULL) { |
| 1108 | fprintf(stderr, "%s not set, cannot kill agent\n", |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 1109 | SSH_AGENTPID_ENV_NAME); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1110 | exit(1); |
| 1111 | } |
Damien Miller | 89c3fe4 | 2006-03-31 23:11:28 +1100 | [diff] [blame] | 1112 | pid = (int)strtonum(pidstr, 2, INT_MAX, &errstr); |
| 1113 | if (errstr) { |
| 1114 | fprintf(stderr, |
| 1115 | "%s=\"%s\", which is not a good PID: %s\n", |
| 1116 | SSH_AGENTPID_ENV_NAME, pidstr, errstr); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1117 | exit(1); |
| 1118 | } |
| 1119 | if (kill(pid, SIGTERM) == -1) { |
| 1120 | perror("kill"); |
| 1121 | exit(1); |
| 1122 | } |
| 1123 | format = c_flag ? "unsetenv %s;\n" : "unset %s;\n"; |
| 1124 | printf(format, SSH_AUTHSOCKET_ENV_NAME); |
| 1125 | printf(format, SSH_AGENTPID_ENV_NAME); |
Ben Lindstrom | ce0f634 | 2002-06-11 16:42:49 +0000 | [diff] [blame] | 1126 | printf("echo Agent pid %ld killed;\n", (long)pid); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1127 | exit(0); |
Damien Miller | 792c511 | 1999-10-29 09:47:09 +1000 | [diff] [blame] | 1128 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1129 | parent_pid = getpid(); |
| 1130 | |
Ben Lindstrom | b7788f3 | 2002-06-06 21:46:08 +0000 | [diff] [blame] | 1131 | if (agentsocket == NULL) { |
| 1132 | /* Create private directory for agent socket */ |
Damien Miller | 2cd6293 | 2010-12-01 11:50:35 +1100 | [diff] [blame] | 1133 | mktemp_proto(socket_dir, sizeof(socket_dir)); |
Ben Lindstrom | b7788f3 | 2002-06-06 21:46:08 +0000 | [diff] [blame] | 1134 | if (mkdtemp(socket_dir) == NULL) { |
| 1135 | perror("mkdtemp: private socket dir"); |
| 1136 | exit(1); |
| 1137 | } |
Ben Lindstrom | ce0f634 | 2002-06-11 16:42:49 +0000 | [diff] [blame] | 1138 | snprintf(socket_name, sizeof socket_name, "%s/agent.%ld", socket_dir, |
| 1139 | (long)parent_pid); |
Ben Lindstrom | b7788f3 | 2002-06-06 21:46:08 +0000 | [diff] [blame] | 1140 | } else { |
| 1141 | /* Try to use specified agent socket */ |
| 1142 | socket_dir[0] = '\0'; |
| 1143 | strlcpy(socket_name, agentsocket, sizeof socket_name); |
Damien Miller | 792c511 | 1999-10-29 09:47:09 +1000 | [diff] [blame] | 1144 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1145 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1146 | /* |
| 1147 | * Create socket early so it will exist before command gets run from |
| 1148 | * the parent. |
| 1149 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1150 | sock = socket(AF_UNIX, SOCK_STREAM, 0); |
| 1151 | if (sock < 0) { |
| 1152 | perror("socket"); |
Darren Tucker | 1dee868 | 2004-11-05 20:26:49 +1100 | [diff] [blame] | 1153 | *socket_name = '\0'; /* Don't unlink any existing file */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1154 | cleanup_exit(1); |
Damien Miller | 792c511 | 1999-10-29 09:47:09 +1000 | [diff] [blame] | 1155 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1156 | memset(&sunaddr, 0, sizeof(sunaddr)); |
| 1157 | sunaddr.sun_family = AF_UNIX; |
| 1158 | strlcpy(sunaddr.sun_path, socket_name, sizeof(sunaddr.sun_path)); |
Ben Lindstrom | 3524d69 | 2001-05-03 22:59:24 +0000 | [diff] [blame] | 1159 | prev_mask = umask(0177); |
Damien Miller | 9096740 | 2006-03-26 14:07:26 +1100 | [diff] [blame] | 1160 | if (bind(sock, (struct sockaddr *) &sunaddr, sizeof(sunaddr)) < 0) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1161 | perror("bind"); |
Darren Tucker | 1dee868 | 2004-11-05 20:26:49 +1100 | [diff] [blame] | 1162 | *socket_name = '\0'; /* Don't unlink any existing file */ |
Ben Lindstrom | 3524d69 | 2001-05-03 22:59:24 +0000 | [diff] [blame] | 1163 | umask(prev_mask); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1164 | cleanup_exit(1); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1165 | } |
Ben Lindstrom | 3524d69 | 2001-05-03 22:59:24 +0000 | [diff] [blame] | 1166 | umask(prev_mask); |
Darren Tucker | 3175eb9 | 2003-12-09 19:15:11 +1100 | [diff] [blame] | 1167 | if (listen(sock, SSH_LISTEN_BACKLOG) < 0) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1168 | perror("listen"); |
| 1169 | cleanup_exit(1); |
| 1170 | } |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 1171 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1172 | /* |
| 1173 | * Fork, and have the parent execute the command, if any, or present |
| 1174 | * the socket data. The child continues as the authentication agent. |
| 1175 | */ |
Ben Lindstrom | d94580c | 2001-07-04 03:48:02 +0000 | [diff] [blame] | 1176 | if (d_flag) { |
| 1177 | log_init(__progname, SYSLOG_LEVEL_DEBUG1, SYSLOG_FACILITY_AUTH, 1); |
| 1178 | format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n"; |
| 1179 | printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name, |
| 1180 | SSH_AUTHSOCKET_ENV_NAME); |
Ben Lindstrom | ce0f634 | 2002-06-11 16:42:49 +0000 | [diff] [blame] | 1181 | printf("echo Agent pid %ld;\n", (long)parent_pid); |
Ben Lindstrom | d94580c | 2001-07-04 03:48:02 +0000 | [diff] [blame] | 1182 | goto skip; |
| 1183 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1184 | pid = fork(); |
| 1185 | if (pid == -1) { |
| 1186 | perror("fork"); |
Damien Miller | c653b89 | 2002-02-08 22:05:41 +1100 | [diff] [blame] | 1187 | cleanup_exit(1); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1188 | } |
| 1189 | if (pid != 0) { /* Parent - execute the given command. */ |
| 1190 | close(sock); |
Ben Lindstrom | ce0f634 | 2002-06-11 16:42:49 +0000 | [diff] [blame] | 1191 | snprintf(pidstrbuf, sizeof pidstrbuf, "%ld", (long)pid); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1192 | if (ac == 0) { |
| 1193 | format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n"; |
| 1194 | printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name, |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 1195 | SSH_AUTHSOCKET_ENV_NAME); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1196 | printf(format, SSH_AGENTPID_ENV_NAME, pidstrbuf, |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 1197 | SSH_AGENTPID_ENV_NAME); |
Ben Lindstrom | ce0f634 | 2002-06-11 16:42:49 +0000 | [diff] [blame] | 1198 | printf("echo Agent pid %ld;\n", (long)pid); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1199 | exit(0); |
| 1200 | } |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 1201 | if (setenv(SSH_AUTHSOCKET_ENV_NAME, socket_name, 1) == -1 || |
| 1202 | setenv(SSH_AGENTPID_ENV_NAME, pidstrbuf, 1) == -1) { |
| 1203 | perror("setenv"); |
| 1204 | exit(1); |
| 1205 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1206 | execvp(av[0], av); |
| 1207 | perror(av[0]); |
| 1208 | exit(1); |
| 1209 | } |
Damien Miller | c653b89 | 2002-02-08 22:05:41 +1100 | [diff] [blame] | 1210 | /* child */ |
| 1211 | log_init(__progname, SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_AUTH, 0); |
Ben Lindstrom | 3fdf876 | 2001-07-22 20:40:24 +0000 | [diff] [blame] | 1212 | |
| 1213 | if (setsid() == -1) { |
Damien Miller | c653b89 | 2002-02-08 22:05:41 +1100 | [diff] [blame] | 1214 | error("setsid: %s", strerror(errno)); |
Ben Lindstrom | 3fdf876 | 2001-07-22 20:40:24 +0000 | [diff] [blame] | 1215 | cleanup_exit(1); |
| 1216 | } |
| 1217 | |
| 1218 | (void)chdir("/"); |
Damien Miller | 6c71179 | 2003-01-24 11:36:23 +1100 | [diff] [blame] | 1219 | if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) { |
| 1220 | /* XXX might close listen socket */ |
| 1221 | (void)dup2(fd, STDIN_FILENO); |
| 1222 | (void)dup2(fd, STDOUT_FILENO); |
| 1223 | (void)dup2(fd, STDERR_FILENO); |
| 1224 | if (fd > 2) |
| 1225 | close(fd); |
| 1226 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1227 | |
Ben Lindstrom | 2c467a2 | 2000-12-27 04:57:41 +0000 | [diff] [blame] | 1228 | #ifdef HAVE_SETRLIMIT |
Ben Lindstrom | c72745a | 2000-12-02 19:03:54 +0000 | [diff] [blame] | 1229 | /* deny core dumps, since memory contains unencrypted private keys */ |
| 1230 | rlim.rlim_cur = rlim.rlim_max = 0; |
| 1231 | if (setrlimit(RLIMIT_CORE, &rlim) < 0) { |
Damien Miller | c653b89 | 2002-02-08 22:05:41 +1100 | [diff] [blame] | 1232 | error("setrlimit RLIMIT_CORE: %s", strerror(errno)); |
Ben Lindstrom | c72745a | 2000-12-02 19:03:54 +0000 | [diff] [blame] | 1233 | cleanup_exit(1); |
| 1234 | } |
Ben Lindstrom | 2c467a2 | 2000-12-27 04:57:41 +0000 | [diff] [blame] | 1235 | #endif |
Ben Lindstrom | d94580c | 2001-07-04 03:48:02 +0000 | [diff] [blame] | 1236 | |
| 1237 | skip: |
Damien Miller | 7ea845e | 2010-02-12 09:21:02 +1100 | [diff] [blame] | 1238 | |
| 1239 | #ifdef ENABLE_PKCS11 |
| 1240 | pkcs11_init(0); |
| 1241 | #endif |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1242 | new_socket(AUTH_SOCKET, sock); |
Darren Tucker | 2812dc9 | 2007-03-21 20:45:06 +1100 | [diff] [blame] | 1243 | if (ac > 0) |
| 1244 | parent_alive_interval = 10; |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 1245 | idtab_init(); |
Damien Miller | 48bfa9c | 2001-07-14 12:12:55 +1000 | [diff] [blame] | 1246 | signal(SIGPIPE, SIG_IGN); |
Damien Miller | 1f0e86f | 2013-07-20 13:22:49 +1000 | [diff] [blame] | 1247 | signal(SIGINT, d_flag ? cleanup_handler : SIG_IGN); |
Ben Lindstrom | 77808ab | 2001-01-26 05:10:34 +0000 | [diff] [blame] | 1248 | signal(SIGHUP, cleanup_handler); |
| 1249 | signal(SIGTERM, cleanup_handler); |
Ben Lindstrom | a3d5a4c | 2001-07-18 15:58:08 +0000 | [diff] [blame] | 1250 | nalloc = 0; |
| 1251 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1252 | while (1) { |
Darren Tucker | 2812dc9 | 2007-03-21 20:45:06 +1100 | [diff] [blame] | 1253 | prepare_select(&readsetp, &writesetp, &max_fd, &nalloc, &tvp); |
| 1254 | result = select(max_fd + 1, readsetp, writesetp, NULL, tvp); |
Darren Tucker | cf0d2db | 2007-02-28 21:19:58 +1100 | [diff] [blame] | 1255 | saved_errno = errno; |
Darren Tucker | 2812dc9 | 2007-03-21 20:45:06 +1100 | [diff] [blame] | 1256 | if (parent_alive_interval != 0) |
| 1257 | check_parent_exists(); |
| 1258 | (void) reaper(); /* remove expired keys */ |
Darren Tucker | cf0d2db | 2007-02-28 21:19:58 +1100 | [diff] [blame] | 1259 | if (result < 0) { |
| 1260 | if (saved_errno == EINTR) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1261 | continue; |
Darren Tucker | cf0d2db | 2007-02-28 21:19:58 +1100 | [diff] [blame] | 1262 | fatal("select: %s", strerror(saved_errno)); |
| 1263 | } else if (result > 0) |
| 1264 | after_select(readsetp, writesetp); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1265 | } |
| 1266 | /* NOTREACHED */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1267 | } |