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