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