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