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