djm@openbsd.org | 9f0e44e | 2017-07-24 04:34:28 +0000 | [diff] [blame] | 1 | /* $OpenBSD: ssh-agent.c,v 1.224 2017/07/24 04:34:28 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> |
deraadt@openbsd.org | 2ae4f33 | 2015-01-16 06:40:12 +0000 | [diff] [blame] | 59 | #include <limits.h> |
Damien Miller | 03e2003 | 2006-03-15 11:16:59 +1100 | [diff] [blame] | 60 | #ifdef HAVE_PATHS_H |
Damien Miller | a9263d0 | 2006-03-15 11:18:26 +1100 | [diff] [blame] | 61 | # include <paths.h> |
Damien Miller | 03e2003 | 2006-03-15 11:16:59 +1100 | [diff] [blame] | 62 | #endif |
djm@openbsd.org | fd0e8fa | 2017-07-19 01:15:02 +0000 | [diff] [blame] | 63 | #ifdef HAVE_POLL_H |
| 64 | # include <poll.h> |
| 65 | #endif |
Damien Miller | 6ff3cad | 2006-03-15 11:52:09 +1100 | [diff] [blame] | 66 | #include <signal.h> |
Damien Miller | ded319c | 2006-09-01 15:38:36 +1000 | [diff] [blame] | 67 | #include <stdarg.h> |
Damien Miller | a7a73ee | 2006-08-05 11:37:59 +1000 | [diff] [blame] | 68 | #include <stdio.h> |
Damien Miller | e7a1e5c | 2006-08-05 11:34:19 +1000 | [diff] [blame] | 69 | #include <stdlib.h> |
Damien Miller | 5598b4f | 2006-07-24 14:09:40 +1000 | [diff] [blame] | 70 | #include <time.h> |
Damien Miller | e3476ed | 2006-07-24 14:13:33 +1000 | [diff] [blame] | 71 | #include <string.h> |
Damien Miller | e6b3b61 | 2006-07-24 14:01:23 +1000 | [diff] [blame] | 72 | #include <unistd.h> |
Damien Miller | e97201f | 2015-05-21 17:55:15 +1000 | [diff] [blame] | 73 | #ifdef HAVE_UTIL_H |
| 74 | # include <util.h> |
| 75 | #endif |
Damien Miller | 6ff3cad | 2006-03-15 11:52:09 +1100 | [diff] [blame] | 76 | |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 77 | #include "xmalloc.h" |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 78 | #include "ssh.h" |
markus@openbsd.org | 139ca81 | 2015-01-14 13:09:09 +0000 | [diff] [blame] | 79 | #include "sshbuf.h" |
| 80 | #include "sshkey.h" |
Damien Miller | 994cf14 | 2000-07-21 10:19:44 +1000 | [diff] [blame] | 81 | #include "authfd.h" |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 82 | #include "compat.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 83 | #include "log.h" |
Damien Miller | 6c71179 | 2003-01-24 11:36:23 +1100 | [diff] [blame] | 84 | #include "misc.h" |
Damien Miller | 4a1c7aa | 2014-02-04 11:03:36 +1100 | [diff] [blame] | 85 | #include "digest.h" |
markus@openbsd.org | 139ca81 | 2015-01-14 13:09:09 +0000 | [diff] [blame] | 86 | #include "ssherr.h" |
djm@openbsd.org | 786d599 | 2016-11-30 03:07:37 +0000 | [diff] [blame] | 87 | #include "match.h" |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 88 | |
Damien Miller | 7ea845e | 2010-02-12 09:21:02 +1100 | [diff] [blame] | 89 | #ifdef ENABLE_PKCS11 |
| 90 | #include "ssh-pkcs11.h" |
Ben Lindstrom | bcc1808 | 2001-08-06 21:59:25 +0000 | [diff] [blame] | 91 | #endif |
Ben Lindstrom | 3f47163 | 2001-07-04 03:53:15 +0000 | [diff] [blame] | 92 | |
djm@openbsd.org | 786d599 | 2016-11-30 03:07:37 +0000 | [diff] [blame] | 93 | #ifndef DEFAULT_PKCS11_WHITELIST |
djm@openbsd.org | b108ce9 | 2017-01-04 02:21:43 +0000 | [diff] [blame] | 94 | # define DEFAULT_PKCS11_WHITELIST "/usr/lib*/*,/usr/local/lib*/*" |
djm@openbsd.org | 786d599 | 2016-11-30 03:07:37 +0000 | [diff] [blame] | 95 | #endif |
| 96 | |
djm@openbsd.org | fd0e8fa | 2017-07-19 01:15:02 +0000 | [diff] [blame] | 97 | /* Maximum accepted message length */ |
| 98 | #define AGENT_MAX_LEN (256*1024) |
| 99 | |
Ben Lindstrom | 65366a8 | 2001-12-06 16:32:47 +0000 | [diff] [blame] | 100 | typedef enum { |
| 101 | AUTH_UNUSED, |
| 102 | AUTH_SOCKET, |
| 103 | AUTH_CONNECTION |
| 104 | } sock_type; |
| 105 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 106 | typedef struct { |
| 107 | int fd; |
Ben Lindstrom | 65366a8 | 2001-12-06 16:32:47 +0000 | [diff] [blame] | 108 | sock_type type; |
markus@openbsd.org | 139ca81 | 2015-01-14 13:09:09 +0000 | [diff] [blame] | 109 | struct sshbuf *input; |
| 110 | struct sshbuf *output; |
| 111 | struct sshbuf *request; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 112 | } SocketEntry; |
| 113 | |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 114 | u_int sockets_alloc = 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 115 | SocketEntry *sockets = NULL; |
| 116 | |
Damien Miller | 1a534ae | 2002-01-22 23:26:13 +1100 | [diff] [blame] | 117 | typedef struct identity { |
| 118 | TAILQ_ENTRY(identity) next; |
markus@openbsd.org | 139ca81 | 2015-01-14 13:09:09 +0000 | [diff] [blame] | 119 | struct sshkey *key; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 120 | char *comment; |
Damien Miller | 7ea845e | 2010-02-12 09:21:02 +1100 | [diff] [blame] | 121 | char *provider; |
Darren Tucker | 5511925 | 2013-06-02 07:43:59 +1000 | [diff] [blame] | 122 | time_t death; |
Damien Miller | 6c71179 | 2003-01-24 11:36:23 +1100 | [diff] [blame] | 123 | u_int confirm; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 124 | } Identity; |
| 125 | |
djm@openbsd.org | f4a6a88 | 2017-04-30 23:29:10 +0000 | [diff] [blame] | 126 | struct idtable { |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 127 | int nentries; |
Damien Miller | 1a534ae | 2002-01-22 23:26:13 +1100 | [diff] [blame] | 128 | TAILQ_HEAD(idqueue, identity) idlist; |
djm@openbsd.org | f4a6a88 | 2017-04-30 23:29:10 +0000 | [diff] [blame] | 129 | }; |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 130 | |
djm@openbsd.org | f4a6a88 | 2017-04-30 23:29:10 +0000 | [diff] [blame] | 131 | /* private key table */ |
| 132 | struct idtable *idtab; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 133 | |
| 134 | int max_fd = 0; |
| 135 | |
| 136 | /* pid of shell == parent of agent */ |
Damien Miller | 166fca8 | 2000-04-20 07:42:21 +1000 | [diff] [blame] | 137 | pid_t parent_pid = -1; |
Darren Tucker | 073f795 | 2013-06-02 23:47:11 +1000 | [diff] [blame] | 138 | time_t parent_alive_interval = 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 139 | |
Damien Miller | b1e967c | 2014-07-03 21:22:40 +1000 | [diff] [blame] | 140 | /* pid of process for which cleanup_socket is applicable */ |
| 141 | pid_t cleanup_pid = 0; |
| 142 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 143 | /* pathname and directory for AUTH_SOCKET */ |
deraadt@openbsd.org | 2ae4f33 | 2015-01-16 06:40:12 +0000 | [diff] [blame] | 144 | char socket_name[PATH_MAX]; |
| 145 | char socket_dir[PATH_MAX]; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 146 | |
djm@openbsd.org | 786d599 | 2016-11-30 03:07:37 +0000 | [diff] [blame] | 147 | /* PKCS#11 path whitelist */ |
| 148 | static char *pkcs11_whitelist; |
| 149 | |
Ben Lindstrom | 2f71704 | 2002-06-06 21:52:03 +0000 | [diff] [blame] | 150 | /* locking */ |
dtucker@openbsd.org | 9173d0f | 2015-05-15 05:44:21 +0000 | [diff] [blame] | 151 | #define LOCK_SIZE 32 |
| 152 | #define LOCK_SALT_SIZE 16 |
| 153 | #define LOCK_ROUNDS 1 |
Ben Lindstrom | 2f71704 | 2002-06-06 21:52:03 +0000 | [diff] [blame] | 154 | int locked = 0; |
djm@openbsd.org | 1a31d02 | 2016-05-02 08:49:03 +0000 | [diff] [blame] | 155 | u_char lock_pwhash[LOCK_SIZE]; |
| 156 | u_char lock_salt[LOCK_SALT_SIZE]; |
Ben Lindstrom | 2f71704 | 2002-06-06 21:52:03 +0000 | [diff] [blame] | 157 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 158 | extern char *__progname; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 159 | |
Darren Tucker | 5511925 | 2013-06-02 07:43:59 +1000 | [diff] [blame] | 160 | /* Default lifetime in seconds (0 == forever) */ |
| 161 | static long lifetime = 0; |
Damien Miller | 53d8148 | 2003-01-22 11:47:19 +1100 | [diff] [blame] | 162 | |
djm@openbsd.org | 56d1c83 | 2014-12-21 22:27:55 +0000 | [diff] [blame] | 163 | static int fingerprint_hash = SSH_FP_HASH_DEFAULT; |
| 164 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 165 | static void |
Damien Miller | 58f3486 | 2002-09-04 16:31:21 +1000 | [diff] [blame] | 166 | close_socket(SocketEntry *e) |
| 167 | { |
Damien Miller | 58f3486 | 2002-09-04 16:31:21 +1000 | [diff] [blame] | 168 | close(e->fd); |
| 169 | e->fd = -1; |
| 170 | e->type = AUTH_UNUSED; |
markus@openbsd.org | 139ca81 | 2015-01-14 13:09:09 +0000 | [diff] [blame] | 171 | sshbuf_free(e->input); |
| 172 | sshbuf_free(e->output); |
| 173 | sshbuf_free(e->request); |
Damien Miller | 58f3486 | 2002-09-04 16:31:21 +1000 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | static void |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 177 | idtab_init(void) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 178 | { |
djm@openbsd.org | f4a6a88 | 2017-04-30 23:29:10 +0000 | [diff] [blame] | 179 | idtab = xcalloc(1, sizeof(*idtab)); |
| 180 | TAILQ_INIT(&idtab->idlist); |
| 181 | idtab->nentries = 0; |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 182 | } |
| 183 | |
Ben Lindstrom | 61d328a | 2002-06-06 21:54:57 +0000 | [diff] [blame] | 184 | static void |
| 185 | free_identity(Identity *id) |
| 186 | { |
markus@openbsd.org | 139ca81 | 2015-01-14 13:09:09 +0000 | [diff] [blame] | 187 | sshkey_free(id->key); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 188 | free(id->provider); |
| 189 | free(id->comment); |
| 190 | free(id); |
Ben Lindstrom | 61d328a | 2002-06-06 21:54:57 +0000 | [diff] [blame] | 191 | } |
| 192 | |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 193 | /* return matching private key for given public key */ |
Damien Miller | 1a534ae | 2002-01-22 23:26:13 +1100 | [diff] [blame] | 194 | static Identity * |
djm@openbsd.org | f4a6a88 | 2017-04-30 23:29:10 +0000 | [diff] [blame] | 195 | lookup_identity(struct sshkey *key) |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 196 | { |
Damien Miller | 1a534ae | 2002-01-22 23:26:13 +1100 | [diff] [blame] | 197 | Identity *id; |
| 198 | |
djm@openbsd.org | f4a6a88 | 2017-04-30 23:29:10 +0000 | [diff] [blame] | 199 | TAILQ_FOREACH(id, &idtab->idlist, next) { |
markus@openbsd.org | 139ca81 | 2015-01-14 13:09:09 +0000 | [diff] [blame] | 200 | if (sshkey_equal(key, id->key)) |
Damien Miller | 1a534ae | 2002-01-22 23:26:13 +1100 | [diff] [blame] | 201 | return (id); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 202 | } |
Damien Miller | 1a534ae | 2002-01-22 23:26:13 +1100 | [diff] [blame] | 203 | return (NULL); |
| 204 | } |
| 205 | |
Damien Miller | 6c71179 | 2003-01-24 11:36:23 +1100 | [diff] [blame] | 206 | /* Check confirmation of keysign request */ |
| 207 | static int |
| 208 | confirm_key(Identity *id) |
| 209 | { |
Darren Tucker | ce327b6 | 2004-11-05 20:38:03 +1100 | [diff] [blame] | 210 | char *p; |
Damien Miller | 6c71179 | 2003-01-24 11:36:23 +1100 | [diff] [blame] | 211 | int ret = -1; |
| 212 | |
markus@openbsd.org | 139ca81 | 2015-01-14 13:09:09 +0000 | [diff] [blame] | 213 | p = sshkey_fingerprint(id->key, fingerprint_hash, SSH_FP_DEFAULT); |
djm@openbsd.org | 9ce86c9 | 2015-01-28 22:36:00 +0000 | [diff] [blame] | 214 | if (p != NULL && |
| 215 | ask_permission("Allow use of key %s?\nKey fingerprint %s.", |
Darren Tucker | ce327b6 | 2004-11-05 20:38:03 +1100 | [diff] [blame] | 216 | id->comment, p)) |
| 217 | ret = 0; |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 218 | free(p); |
Darren Tucker | ce327b6 | 2004-11-05 20:38:03 +1100 | [diff] [blame] | 219 | |
Damien Miller | 6c71179 | 2003-01-24 11:36:23 +1100 | [diff] [blame] | 220 | return (ret); |
| 221 | } |
| 222 | |
markus@openbsd.org | 139ca81 | 2015-01-14 13:09:09 +0000 | [diff] [blame] | 223 | static void |
| 224 | send_status(SocketEntry *e, int success) |
| 225 | { |
| 226 | int r; |
| 227 | |
| 228 | if ((r = sshbuf_put_u32(e->output, 1)) != 0 || |
| 229 | (r = sshbuf_put_u8(e->output, success ? |
| 230 | SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE)) != 0) |
| 231 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
| 232 | } |
| 233 | |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 234 | /* send list of supported public keys to 'client' */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 235 | static void |
djm@openbsd.org | f4a6a88 | 2017-04-30 23:29:10 +0000 | [diff] [blame] | 236 | process_request_identities(SocketEntry *e) |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 237 | { |
Damien Miller | 1a534ae | 2002-01-22 23:26:13 +1100 | [diff] [blame] | 238 | Identity *id; |
markus@openbsd.org | 139ca81 | 2015-01-14 13:09:09 +0000 | [diff] [blame] | 239 | struct sshbuf *msg; |
| 240 | int r; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 241 | |
markus@openbsd.org | 139ca81 | 2015-01-14 13:09:09 +0000 | [diff] [blame] | 242 | if ((msg = sshbuf_new()) == NULL) |
| 243 | fatal("%s: sshbuf_new failed", __func__); |
djm@openbsd.org | f4a6a88 | 2017-04-30 23:29:10 +0000 | [diff] [blame] | 244 | if ((r = sshbuf_put_u8(msg, SSH2_AGENT_IDENTITIES_ANSWER)) != 0 || |
| 245 | (r = sshbuf_put_u32(msg, idtab->nentries)) != 0) |
markus@openbsd.org | 139ca81 | 2015-01-14 13:09:09 +0000 | [diff] [blame] | 246 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
djm@openbsd.org | f4a6a88 | 2017-04-30 23:29:10 +0000 | [diff] [blame] | 247 | TAILQ_FOREACH(id, &idtab->idlist, next) { |
| 248 | if ((r = sshkey_puts(id->key, msg)) != 0 || |
| 249 | (r = sshbuf_put_cstring(msg, id->comment)) != 0) { |
| 250 | error("%s: put key/comment: %s", __func__, |
djm@openbsd.org | 873d3e7 | 2017-04-30 23:18:44 +0000 | [diff] [blame] | 251 | ssh_err(r)); |
| 252 | continue; |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 253 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 254 | } |
markus@openbsd.org | 139ca81 | 2015-01-14 13:09:09 +0000 | [diff] [blame] | 255 | if ((r = sshbuf_put_stringb(e->output, msg)) != 0) |
| 256 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
| 257 | sshbuf_free(msg); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 258 | } |
| 259 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 260 | |
markus@openbsd.org | 76c9fbb | 2015-12-04 16:41:28 +0000 | [diff] [blame] | 261 | static char * |
| 262 | agent_decode_alg(struct sshkey *key, u_int flags) |
| 263 | { |
| 264 | if (key->type == KEY_RSA) { |
| 265 | if (flags & SSH_AGENT_RSA_SHA2_256) |
| 266 | return "rsa-sha2-256"; |
| 267 | else if (flags & SSH_AGENT_RSA_SHA2_512) |
| 268 | return "rsa-sha2-512"; |
| 269 | } |
| 270 | return NULL; |
| 271 | } |
| 272 | |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 273 | /* ssh2 only */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 274 | static void |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 275 | process_sign_request2(SocketEntry *e) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 276 | { |
djm@openbsd.org | f4a6a88 | 2017-04-30 23:29:10 +0000 | [diff] [blame] | 277 | const u_char *data; |
| 278 | u_char *signature = NULL; |
| 279 | size_t dlen, slen = 0; |
markus@openbsd.org | 139ca81 | 2015-01-14 13:09:09 +0000 | [diff] [blame] | 280 | u_int compat = 0, flags; |
| 281 | int r, ok = -1; |
| 282 | struct sshbuf *msg; |
djm@openbsd.org | f4a6a88 | 2017-04-30 23:29:10 +0000 | [diff] [blame] | 283 | struct sshkey *key = NULL; |
djm@openbsd.org | 0088c57 | 2015-01-14 19:33:41 +0000 | [diff] [blame] | 284 | struct identity *id; |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 285 | |
djm@openbsd.org | 0088c57 | 2015-01-14 19:33:41 +0000 | [diff] [blame] | 286 | if ((msg = sshbuf_new()) == NULL) |
| 287 | fatal("%s: sshbuf_new failed", __func__); |
djm@openbsd.org | f4a6a88 | 2017-04-30 23:29:10 +0000 | [diff] [blame] | 288 | if ((r = sshkey_froms(e->request, &key)) != 0 || |
| 289 | (r = sshbuf_get_string_direct(e->request, &data, &dlen)) != 0 || |
markus@openbsd.org | 139ca81 | 2015-01-14 13:09:09 +0000 | [diff] [blame] | 290 | (r = sshbuf_get_u32(e->request, &flags)) != 0) |
| 291 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 292 | if (flags & SSH_AGENT_OLD_SIGNATURE) |
markus@openbsd.org | 139ca81 | 2015-01-14 13:09:09 +0000 | [diff] [blame] | 293 | compat = SSH_BUG_SIGBLOB; |
djm@openbsd.org | f4a6a88 | 2017-04-30 23:29:10 +0000 | [diff] [blame] | 294 | if ((id = lookup_identity(key)) == NULL) { |
djm@openbsd.org | 0088c57 | 2015-01-14 19:33:41 +0000 | [diff] [blame] | 295 | verbose("%s: %s key not found", __func__, sshkey_type(key)); |
| 296 | goto send; |
| 297 | } |
| 298 | if (id->confirm && confirm_key(id) != 0) { |
| 299 | verbose("%s: user refused key", __func__); |
| 300 | goto send; |
| 301 | } |
| 302 | if ((r = sshkey_sign(id->key, &signature, &slen, |
markus@openbsd.org | 76c9fbb | 2015-12-04 16:41:28 +0000 | [diff] [blame] | 303 | data, dlen, agent_decode_alg(key, flags), compat)) != 0) { |
djm@openbsd.org | 39736be | 2015-12-11 02:20:28 +0000 | [diff] [blame] | 304 | error("%s: sshkey_sign: %s", __func__, ssh_err(r)); |
djm@openbsd.org | 0088c57 | 2015-01-14 19:33:41 +0000 | [diff] [blame] | 305 | goto send; |
| 306 | } |
| 307 | /* Success */ |
| 308 | ok = 0; |
| 309 | send: |
| 310 | sshkey_free(key); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 311 | if (ok == 0) { |
markus@openbsd.org | 139ca81 | 2015-01-14 13:09:09 +0000 | [diff] [blame] | 312 | if ((r = sshbuf_put_u8(msg, SSH2_AGENT_SIGN_RESPONSE)) != 0 || |
| 313 | (r = sshbuf_put_string(msg, signature, slen)) != 0) |
| 314 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
| 315 | } else if ((r = sshbuf_put_u8(msg, SSH_AGENT_FAILURE)) != 0) |
| 316 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
| 317 | |
| 318 | if ((r = sshbuf_put_stringb(e->output, msg)) != 0) |
| 319 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
| 320 | |
| 321 | sshbuf_free(msg); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 322 | free(signature); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | /* shared */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 326 | static void |
djm@openbsd.org | f4a6a88 | 2017-04-30 23:29:10 +0000 | [diff] [blame] | 327 | process_remove_identity(SocketEntry *e) |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 328 | { |
markus@openbsd.org | 139ca81 | 2015-01-14 13:09:09 +0000 | [diff] [blame] | 329 | int r, success = 0; |
| 330 | struct sshkey *key = NULL; |
djm@openbsd.org | f4a6a88 | 2017-04-30 23:29:10 +0000 | [diff] [blame] | 331 | Identity *id; |
Damien Miller | 7e8e820 | 1999-11-16 13:37:16 +1100 | [diff] [blame] | 332 | |
djm@openbsd.org | f4a6a88 | 2017-04-30 23:29:10 +0000 | [diff] [blame] | 333 | if ((r = sshkey_froms(e->request, &key)) != 0) { |
| 334 | error("%s: get key: %s", __func__, ssh_err(r)); |
| 335 | goto done; |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 336 | } |
djm@openbsd.org | f4a6a88 | 2017-04-30 23:29:10 +0000 | [diff] [blame] | 337 | if ((id = lookup_identity(key)) == NULL) { |
| 338 | debug("%s: key not found", __func__); |
| 339 | goto done; |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 340 | } |
djm@openbsd.org | f4a6a88 | 2017-04-30 23:29:10 +0000 | [diff] [blame] | 341 | /* We have this key, free it. */ |
| 342 | if (idtab->nentries < 1) |
| 343 | fatal("%s: internal error: nentries %d", |
| 344 | __func__, idtab->nentries); |
| 345 | TAILQ_REMOVE(&idtab->idlist, id, next); |
| 346 | free_identity(id); |
| 347 | idtab->nentries--; |
| 348 | sshkey_free(key); |
| 349 | success = 1; |
| 350 | done: |
markus@openbsd.org | 139ca81 | 2015-01-14 13:09:09 +0000 | [diff] [blame] | 351 | send_status(e, success); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 352 | } |
| 353 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 354 | static void |
djm@openbsd.org | f4a6a88 | 2017-04-30 23:29:10 +0000 | [diff] [blame] | 355 | process_remove_all_identities(SocketEntry *e) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 356 | { |
Damien Miller | 1a534ae | 2002-01-22 23:26:13 +1100 | [diff] [blame] | 357 | Identity *id; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 358 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 359 | /* Loop over all identities and clear the keys. */ |
djm@openbsd.org | f4a6a88 | 2017-04-30 23:29:10 +0000 | [diff] [blame] | 360 | for (id = TAILQ_FIRST(&idtab->idlist); id; |
| 361 | id = TAILQ_FIRST(&idtab->idlist)) { |
| 362 | TAILQ_REMOVE(&idtab->idlist, id, next); |
Damien Miller | 1a534ae | 2002-01-22 23:26:13 +1100 | [diff] [blame] | 363 | free_identity(id); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 364 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 365 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 366 | /* Mark that there are no identities. */ |
djm@openbsd.org | f4a6a88 | 2017-04-30 23:29:10 +0000 | [diff] [blame] | 367 | idtab->nentries = 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 368 | |
| 369 | /* Send success. */ |
markus@openbsd.org | 139ca81 | 2015-01-14 13:09:09 +0000 | [diff] [blame] | 370 | send_status(e, 1); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 371 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 372 | |
Darren Tucker | 2812dc9 | 2007-03-21 20:45:06 +1100 | [diff] [blame] | 373 | /* removes expired keys and returns number of seconds until the next expiry */ |
Darren Tucker | 5511925 | 2013-06-02 07:43:59 +1000 | [diff] [blame] | 374 | static time_t |
Ben Lindstrom | 61d328a | 2002-06-06 21:54:57 +0000 | [diff] [blame] | 375 | reaper(void) |
| 376 | { |
Darren Tucker | b759c9c | 2013-06-02 07:46:16 +1000 | [diff] [blame] | 377 | time_t deadline = 0, now = monotime(); |
Ben Lindstrom | 61d328a | 2002-06-06 21:54:57 +0000 | [diff] [blame] | 378 | Identity *id, *nxt; |
Ben Lindstrom | 61d328a | 2002-06-06 21:54:57 +0000 | [diff] [blame] | 379 | |
djm@openbsd.org | f4a6a88 | 2017-04-30 23:29:10 +0000 | [diff] [blame] | 380 | for (id = TAILQ_FIRST(&idtab->idlist); id; id = nxt) { |
| 381 | nxt = TAILQ_NEXT(id, next); |
| 382 | if (id->death == 0) |
| 383 | continue; |
| 384 | if (now >= id->death) { |
| 385 | debug("expiring key '%s'", id->comment); |
| 386 | TAILQ_REMOVE(&idtab->idlist, id, next); |
| 387 | free_identity(id); |
| 388 | idtab->nentries--; |
| 389 | } else |
| 390 | deadline = (deadline == 0) ? id->death : |
| 391 | MINIMUM(deadline, id->death); |
Ben Lindstrom | 61d328a | 2002-06-06 21:54:57 +0000 | [diff] [blame] | 392 | } |
Darren Tucker | 2812dc9 | 2007-03-21 20:45:06 +1100 | [diff] [blame] | 393 | if (deadline == 0 || deadline <= now) |
| 394 | return 0; |
| 395 | else |
| 396 | return (deadline - now); |
Ben Lindstrom | 61d328a | 2002-06-06 21:54:57 +0000 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | static void |
djm@openbsd.org | f4a6a88 | 2017-04-30 23:29:10 +0000 | [diff] [blame] | 400 | process_add_identity(SocketEntry *e) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 401 | { |
Damien Miller | 4c7728c | 2007-10-26 14:25:31 +1000 | [diff] [blame] | 402 | Identity *id; |
markus@openbsd.org | 139ca81 | 2015-01-14 13:09:09 +0000 | [diff] [blame] | 403 | int success = 0, confirm = 0; |
| 404 | u_int seconds; |
| 405 | char *comment = NULL; |
Darren Tucker | 5511925 | 2013-06-02 07:43:59 +1000 | [diff] [blame] | 406 | time_t death = 0; |
markus@openbsd.org | 139ca81 | 2015-01-14 13:09:09 +0000 | [diff] [blame] | 407 | struct sshkey *k = NULL; |
| 408 | u_char ctype; |
| 409 | int r = SSH_ERR_INTERNAL_ERROR; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 410 | |
djm@openbsd.org | f4a6a88 | 2017-04-30 23:29:10 +0000 | [diff] [blame] | 411 | if ((r = sshkey_private_deserialize(e->request, &k)) != 0 || |
| 412 | k == NULL || |
markus@openbsd.org | 139ca81 | 2015-01-14 13:09:09 +0000 | [diff] [blame] | 413 | (r = sshbuf_get_cstring(e->request, &comment, NULL)) != 0) { |
| 414 | error("%s: decode private key: %s", __func__, ssh_err(r)); |
| 415 | goto err; |
| 416 | } |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 417 | |
markus@openbsd.org | 139ca81 | 2015-01-14 13:09:09 +0000 | [diff] [blame] | 418 | while (sshbuf_len(e->request)) { |
| 419 | if ((r = sshbuf_get_u8(e->request, &ctype)) != 0) { |
| 420 | error("%s: buffer error: %s", __func__, ssh_err(r)); |
| 421 | goto err; |
| 422 | } |
| 423 | switch (ctype) { |
Ben Lindstrom | c90f8a9 | 2002-06-21 00:06:54 +0000 | [diff] [blame] | 424 | case SSH_AGENT_CONSTRAIN_LIFETIME: |
markus@openbsd.org | 139ca81 | 2015-01-14 13:09:09 +0000 | [diff] [blame] | 425 | if ((r = sshbuf_get_u32(e->request, &seconds)) != 0) { |
| 426 | error("%s: bad lifetime constraint: %s", |
| 427 | __func__, ssh_err(r)); |
| 428 | goto err; |
| 429 | } |
| 430 | death = monotime() + seconds; |
Ben Lindstrom | 4eb4c4e | 2002-06-21 00:04:48 +0000 | [diff] [blame] | 431 | break; |
Damien Miller | 6c71179 | 2003-01-24 11:36:23 +1100 | [diff] [blame] | 432 | case SSH_AGENT_CONSTRAIN_CONFIRM: |
| 433 | confirm = 1; |
| 434 | break; |
Ben Lindstrom | 4eb4c4e | 2002-06-21 00:04:48 +0000 | [diff] [blame] | 435 | default: |
markus@openbsd.org | 139ca81 | 2015-01-14 13:09:09 +0000 | [diff] [blame] | 436 | error("%s: Unknown constraint %d", __func__, ctype); |
| 437 | err: |
| 438 | sshbuf_reset(e->request); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 439 | free(comment); |
markus@openbsd.org | 139ca81 | 2015-01-14 13:09:09 +0000 | [diff] [blame] | 440 | sshkey_free(k); |
Damien Miller | 1cfadab | 2008-06-30 00:05:21 +1000 | [diff] [blame] | 441 | goto send; |
Ben Lindstrom | 4eb4c4e | 2002-06-21 00:04:48 +0000 | [diff] [blame] | 442 | } |
| 443 | } |
markus@openbsd.org | 139ca81 | 2015-01-14 13:09:09 +0000 | [diff] [blame] | 444 | |
Damien Miller | 1cfadab | 2008-06-30 00:05:21 +1000 | [diff] [blame] | 445 | success = 1; |
Damien Miller | 53d8148 | 2003-01-22 11:47:19 +1100 | [diff] [blame] | 446 | if (lifetime && !death) |
Darren Tucker | b759c9c | 2013-06-02 07:46:16 +1000 | [diff] [blame] | 447 | death = monotime() + lifetime; |
djm@openbsd.org | f4a6a88 | 2017-04-30 23:29:10 +0000 | [diff] [blame] | 448 | if ((id = lookup_identity(k)) == NULL) { |
Damien Miller | 7ea845e | 2010-02-12 09:21:02 +1100 | [diff] [blame] | 449 | id = xcalloc(1, sizeof(Identity)); |
Ben Lindstrom | 2b266b7 | 2002-06-21 00:08:39 +0000 | [diff] [blame] | 450 | id->key = k; |
djm@openbsd.org | f4a6a88 | 2017-04-30 23:29:10 +0000 | [diff] [blame] | 451 | TAILQ_INSERT_TAIL(&idtab->idlist, id, next); |
Ben Lindstrom | 2b266b7 | 2002-06-21 00:08:39 +0000 | [diff] [blame] | 452 | /* Increment the number of identities. */ |
djm@openbsd.org | f4a6a88 | 2017-04-30 23:29:10 +0000 | [diff] [blame] | 453 | idtab->nentries++; |
Ben Lindstrom | 2b266b7 | 2002-06-21 00:08:39 +0000 | [diff] [blame] | 454 | } else { |
markus@openbsd.org | 139ca81 | 2015-01-14 13:09:09 +0000 | [diff] [blame] | 455 | sshkey_free(k); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 456 | free(id->comment); |
Ben Lindstrom | 61d328a | 2002-06-06 21:54:57 +0000 | [diff] [blame] | 457 | } |
Damien Miller | 4c7728c | 2007-10-26 14:25:31 +1000 | [diff] [blame] | 458 | id->comment = comment; |
| 459 | id->death = death; |
| 460 | id->confirm = confirm; |
Ben Lindstrom | 2b266b7 | 2002-06-21 00:08:39 +0000 | [diff] [blame] | 461 | send: |
markus@openbsd.org | 139ca81 | 2015-01-14 13:09:09 +0000 | [diff] [blame] | 462 | send_status(e, success); |
Ben Lindstrom | 61d328a | 2002-06-06 21:54:57 +0000 | [diff] [blame] | 463 | } |
| 464 | |
Ben Lindstrom | 2f71704 | 2002-06-06 21:52:03 +0000 | [diff] [blame] | 465 | /* XXX todo: encrypt sensitive data with passphrase */ |
| 466 | static void |
| 467 | process_lock_agent(SocketEntry *e, int lock) |
| 468 | { |
dtucker@openbsd.org | 9173d0f | 2015-05-15 05:44:21 +0000 | [diff] [blame] | 469 | int r, success = 0, delay; |
djm@openbsd.org | 1a31d02 | 2016-05-02 08:49:03 +0000 | [diff] [blame] | 470 | char *passwd; |
| 471 | u_char passwdhash[LOCK_SIZE]; |
dtucker@openbsd.org | 9173d0f | 2015-05-15 05:44:21 +0000 | [diff] [blame] | 472 | static u_int fail_count = 0; |
| 473 | size_t pwlen; |
Ben Lindstrom | 2f71704 | 2002-06-06 21:52:03 +0000 | [diff] [blame] | 474 | |
dtucker@openbsd.org | 9173d0f | 2015-05-15 05:44:21 +0000 | [diff] [blame] | 475 | if ((r = sshbuf_get_cstring(e->request, &passwd, &pwlen)) != 0) |
markus@openbsd.org | 139ca81 | 2015-01-14 13:09:09 +0000 | [diff] [blame] | 476 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
dtucker@openbsd.org | 9173d0f | 2015-05-15 05:44:21 +0000 | [diff] [blame] | 477 | if (pwlen == 0) { |
| 478 | debug("empty password not supported"); |
| 479 | } else if (locked && !lock) { |
| 480 | if (bcrypt_pbkdf(passwd, pwlen, lock_salt, sizeof(lock_salt), |
| 481 | passwdhash, sizeof(passwdhash), LOCK_ROUNDS) < 0) |
| 482 | fatal("bcrypt_pbkdf"); |
djm@openbsd.org | 1a31d02 | 2016-05-02 08:49:03 +0000 | [diff] [blame] | 483 | if (timingsafe_bcmp(passwdhash, lock_pwhash, LOCK_SIZE) == 0) { |
dtucker@openbsd.org | 9173d0f | 2015-05-15 05:44:21 +0000 | [diff] [blame] | 484 | debug("agent unlocked"); |
| 485 | locked = 0; |
| 486 | fail_count = 0; |
djm@openbsd.org | 1a31d02 | 2016-05-02 08:49:03 +0000 | [diff] [blame] | 487 | explicit_bzero(lock_pwhash, sizeof(lock_pwhash)); |
dtucker@openbsd.org | 9173d0f | 2015-05-15 05:44:21 +0000 | [diff] [blame] | 488 | success = 1; |
| 489 | } else { |
| 490 | /* delay in 0.1s increments up to 10s */ |
| 491 | if (fail_count < 100) |
| 492 | fail_count++; |
| 493 | delay = 100000 * fail_count; |
| 494 | debug("unlock failed, delaying %0.1lf seconds", |
| 495 | (double)delay/1000000); |
| 496 | usleep(delay); |
| 497 | } |
| 498 | explicit_bzero(passwdhash, sizeof(passwdhash)); |
Ben Lindstrom | 2f71704 | 2002-06-06 21:52:03 +0000 | [diff] [blame] | 499 | } else if (!locked && lock) { |
dtucker@openbsd.org | 9173d0f | 2015-05-15 05:44:21 +0000 | [diff] [blame] | 500 | debug("agent locked"); |
Ben Lindstrom | 2f71704 | 2002-06-06 21:52:03 +0000 | [diff] [blame] | 501 | locked = 1; |
dtucker@openbsd.org | 9173d0f | 2015-05-15 05:44:21 +0000 | [diff] [blame] | 502 | arc4random_buf(lock_salt, sizeof(lock_salt)); |
| 503 | if (bcrypt_pbkdf(passwd, pwlen, lock_salt, sizeof(lock_salt), |
djm@openbsd.org | 1a31d02 | 2016-05-02 08:49:03 +0000 | [diff] [blame] | 504 | lock_pwhash, sizeof(lock_pwhash), LOCK_ROUNDS) < 0) |
dtucker@openbsd.org | 9173d0f | 2015-05-15 05:44:21 +0000 | [diff] [blame] | 505 | fatal("bcrypt_pbkdf"); |
Ben Lindstrom | 2f71704 | 2002-06-06 21:52:03 +0000 | [diff] [blame] | 506 | success = 1; |
| 507 | } |
dtucker@openbsd.org | 9173d0f | 2015-05-15 05:44:21 +0000 | [diff] [blame] | 508 | explicit_bzero(passwd, pwlen); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 509 | free(passwd); |
markus@openbsd.org | 139ca81 | 2015-01-14 13:09:09 +0000 | [diff] [blame] | 510 | send_status(e, success); |
Ben Lindstrom | 2f71704 | 2002-06-06 21:52:03 +0000 | [diff] [blame] | 511 | } |
| 512 | |
| 513 | static void |
djm@openbsd.org | f4a6a88 | 2017-04-30 23:29:10 +0000 | [diff] [blame] | 514 | no_identities(SocketEntry *e) |
Ben Lindstrom | 2f71704 | 2002-06-06 21:52:03 +0000 | [diff] [blame] | 515 | { |
markus@openbsd.org | 139ca81 | 2015-01-14 13:09:09 +0000 | [diff] [blame] | 516 | struct sshbuf *msg; |
| 517 | int r; |
Ben Lindstrom | 2f71704 | 2002-06-06 21:52:03 +0000 | [diff] [blame] | 518 | |
markus@openbsd.org | 139ca81 | 2015-01-14 13:09:09 +0000 | [diff] [blame] | 519 | if ((msg = sshbuf_new()) == NULL) |
| 520 | fatal("%s: sshbuf_new failed", __func__); |
djm@openbsd.org | f4a6a88 | 2017-04-30 23:29:10 +0000 | [diff] [blame] | 521 | if ((r = sshbuf_put_u8(msg, SSH2_AGENT_IDENTITIES_ANSWER)) != 0 || |
markus@openbsd.org | 139ca81 | 2015-01-14 13:09:09 +0000 | [diff] [blame] | 522 | (r = sshbuf_put_u32(msg, 0)) != 0 || |
| 523 | (r = sshbuf_put_stringb(e->output, msg)) != 0) |
| 524 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
| 525 | sshbuf_free(msg); |
Ben Lindstrom | 2f71704 | 2002-06-06 21:52:03 +0000 | [diff] [blame] | 526 | } |
Ben Lindstrom | 3f47163 | 2001-07-04 03:53:15 +0000 | [diff] [blame] | 527 | |
Damien Miller | 7ea845e | 2010-02-12 09:21:02 +1100 | [diff] [blame] | 528 | #ifdef ENABLE_PKCS11 |
Ben Lindstrom | 3f47163 | 2001-07-04 03:53:15 +0000 | [diff] [blame] | 529 | static void |
Damien Miller | 1cfadab | 2008-06-30 00:05:21 +1000 | [diff] [blame] | 530 | process_add_smartcard_key(SocketEntry *e) |
Ben Lindstrom | 3f47163 | 2001-07-04 03:53:15 +0000 | [diff] [blame] | 531 | { |
djm@openbsd.org | 786d599 | 2016-11-30 03:07:37 +0000 | [diff] [blame] | 532 | char *provider = NULL, *pin, canonical_provider[PATH_MAX]; |
djm@openbsd.org | f4a6a88 | 2017-04-30 23:29:10 +0000 | [diff] [blame] | 533 | int r, i, count = 0, success = 0, confirm = 0; |
markus@openbsd.org | 139ca81 | 2015-01-14 13:09:09 +0000 | [diff] [blame] | 534 | u_int seconds; |
Darren Tucker | 5511925 | 2013-06-02 07:43:59 +1000 | [diff] [blame] | 535 | time_t death = 0; |
markus@openbsd.org | 139ca81 | 2015-01-14 13:09:09 +0000 | [diff] [blame] | 536 | u_char type; |
| 537 | struct sshkey **keys = NULL, *k; |
Ben Lindstrom | 822b634 | 2002-06-23 21:38:49 +0000 | [diff] [blame] | 538 | Identity *id; |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 539 | |
markus@openbsd.org | 139ca81 | 2015-01-14 13:09:09 +0000 | [diff] [blame] | 540 | if ((r = sshbuf_get_cstring(e->request, &provider, NULL)) != 0 || |
| 541 | (r = sshbuf_get_cstring(e->request, &pin, NULL)) != 0) |
| 542 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
Damien Miller | d94f20d | 2003-06-11 22:06:33 +1000 | [diff] [blame] | 543 | |
markus@openbsd.org | 139ca81 | 2015-01-14 13:09:09 +0000 | [diff] [blame] | 544 | while (sshbuf_len(e->request)) { |
| 545 | if ((r = sshbuf_get_u8(e->request, &type)) != 0) |
| 546 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
| 547 | switch (type) { |
Damien Miller | d94f20d | 2003-06-11 22:06:33 +1000 | [diff] [blame] | 548 | case SSH_AGENT_CONSTRAIN_LIFETIME: |
markus@openbsd.org | 139ca81 | 2015-01-14 13:09:09 +0000 | [diff] [blame] | 549 | if ((r = sshbuf_get_u32(e->request, &seconds)) != 0) |
| 550 | fatal("%s: buffer error: %s", |
| 551 | __func__, ssh_err(r)); |
| 552 | death = monotime() + seconds; |
Damien Miller | d94f20d | 2003-06-11 22:06:33 +1000 | [diff] [blame] | 553 | break; |
| 554 | case SSH_AGENT_CONSTRAIN_CONFIRM: |
| 555 | confirm = 1; |
| 556 | break; |
| 557 | default: |
djm@openbsd.org | f4a6a88 | 2017-04-30 23:29:10 +0000 | [diff] [blame] | 558 | error("%s: Unknown constraint type %d", __func__, type); |
Damien Miller | 1cfadab | 2008-06-30 00:05:21 +1000 | [diff] [blame] | 559 | goto send; |
Damien Miller | d94f20d | 2003-06-11 22:06:33 +1000 | [diff] [blame] | 560 | } |
| 561 | } |
djm@openbsd.org | 786d599 | 2016-11-30 03:07:37 +0000 | [diff] [blame] | 562 | if (realpath(provider, canonical_provider) == NULL) { |
| 563 | verbose("failed PKCS#11 add of \"%.100s\": realpath: %s", |
| 564 | provider, strerror(errno)); |
| 565 | goto send; |
| 566 | } |
| 567 | if (match_pattern_list(canonical_provider, pkcs11_whitelist, 0) != 1) { |
| 568 | verbose("refusing PKCS#11 add of \"%.100s\": " |
| 569 | "provider not whitelisted", canonical_provider); |
| 570 | goto send; |
| 571 | } |
| 572 | debug("%s: add %.100s", __func__, canonical_provider); |
Damien Miller | d94f20d | 2003-06-11 22:06:33 +1000 | [diff] [blame] | 573 | if (lifetime && !death) |
Darren Tucker | b759c9c | 2013-06-02 07:46:16 +1000 | [diff] [blame] | 574 | death = monotime() + lifetime; |
Damien Miller | d94f20d | 2003-06-11 22:06:33 +1000 | [diff] [blame] | 575 | |
djm@openbsd.org | 786d599 | 2016-11-30 03:07:37 +0000 | [diff] [blame] | 576 | count = pkcs11_add_provider(canonical_provider, pin, &keys); |
Damien Miller | 7ea845e | 2010-02-12 09:21:02 +1100 | [diff] [blame] | 577 | for (i = 0; i < count; i++) { |
Ben Lindstrom | 0936a5b | 2002-03-26 03:17:42 +0000 | [diff] [blame] | 578 | k = keys[i]; |
djm@openbsd.org | f4a6a88 | 2017-04-30 23:29:10 +0000 | [diff] [blame] | 579 | if (lookup_identity(k) == NULL) { |
Damien Miller | 7ea845e | 2010-02-12 09:21:02 +1100 | [diff] [blame] | 580 | id = xcalloc(1, sizeof(Identity)); |
Ben Lindstrom | 0936a5b | 2002-03-26 03:17:42 +0000 | [diff] [blame] | 581 | id->key = k; |
djm@openbsd.org | 786d599 | 2016-11-30 03:07:37 +0000 | [diff] [blame] | 582 | id->provider = xstrdup(canonical_provider); |
| 583 | id->comment = xstrdup(canonical_provider); /* XXX */ |
Damien Miller | d94f20d | 2003-06-11 22:06:33 +1000 | [diff] [blame] | 584 | id->death = death; |
| 585 | id->confirm = confirm; |
djm@openbsd.org | f4a6a88 | 2017-04-30 23:29:10 +0000 | [diff] [blame] | 586 | TAILQ_INSERT_TAIL(&idtab->idlist, id, next); |
| 587 | idtab->nentries++; |
Ben Lindstrom | 0936a5b | 2002-03-26 03:17:42 +0000 | [diff] [blame] | 588 | success = 1; |
| 589 | } else { |
markus@openbsd.org | 139ca81 | 2015-01-14 13:09:09 +0000 | [diff] [blame] | 590 | sshkey_free(k); |
Ben Lindstrom | 0936a5b | 2002-03-26 03:17:42 +0000 | [diff] [blame] | 591 | } |
| 592 | keys[i] = NULL; |
Ben Lindstrom | 3f47163 | 2001-07-04 03:53:15 +0000 | [diff] [blame] | 593 | } |
Ben Lindstrom | 3f47163 | 2001-07-04 03:53:15 +0000 | [diff] [blame] | 594 | send: |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 595 | free(pin); |
| 596 | free(provider); |
| 597 | free(keys); |
markus@openbsd.org | 139ca81 | 2015-01-14 13:09:09 +0000 | [diff] [blame] | 598 | send_status(e, success); |
Ben Lindstrom | 3f47163 | 2001-07-04 03:53:15 +0000 | [diff] [blame] | 599 | } |
| 600 | |
| 601 | static void |
| 602 | process_remove_smartcard_key(SocketEntry *e) |
| 603 | { |
djm@openbsd.org | 25f8376 | 2017-03-15 02:25:09 +0000 | [diff] [blame] | 604 | char *provider = NULL, *pin = NULL, canonical_provider[PATH_MAX]; |
djm@openbsd.org | f4a6a88 | 2017-04-30 23:29:10 +0000 | [diff] [blame] | 605 | int r, success = 0; |
Damien Miller | 7ea845e | 2010-02-12 09:21:02 +1100 | [diff] [blame] | 606 | Identity *id, *nxt; |
Ben Lindstrom | 3f47163 | 2001-07-04 03:53:15 +0000 | [diff] [blame] | 607 | |
markus@openbsd.org | 139ca81 | 2015-01-14 13:09:09 +0000 | [diff] [blame] | 608 | if ((r = sshbuf_get_cstring(e->request, &provider, NULL)) != 0 || |
| 609 | (r = sshbuf_get_cstring(e->request, &pin, NULL)) != 0) |
| 610 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 611 | free(pin); |
Ben Lindstrom | 3f47163 | 2001-07-04 03:53:15 +0000 | [diff] [blame] | 612 | |
djm@openbsd.org | 25f8376 | 2017-03-15 02:25:09 +0000 | [diff] [blame] | 613 | if (realpath(provider, canonical_provider) == NULL) { |
| 614 | verbose("failed PKCS#11 add of \"%.100s\": realpath: %s", |
| 615 | provider, strerror(errno)); |
| 616 | goto send; |
| 617 | } |
| 618 | |
| 619 | debug("%s: remove %.100s", __func__, canonical_provider); |
djm@openbsd.org | f4a6a88 | 2017-04-30 23:29:10 +0000 | [diff] [blame] | 620 | for (id = TAILQ_FIRST(&idtab->idlist); id; id = nxt) { |
| 621 | nxt = TAILQ_NEXT(id, next); |
| 622 | /* Skip file--based keys */ |
| 623 | if (id->provider == NULL) |
| 624 | continue; |
| 625 | if (!strcmp(canonical_provider, id->provider)) { |
| 626 | TAILQ_REMOVE(&idtab->idlist, id, next); |
| 627 | free_identity(id); |
| 628 | idtab->nentries--; |
Ben Lindstrom | 3f47163 | 2001-07-04 03:53:15 +0000 | [diff] [blame] | 629 | } |
Ben Lindstrom | 3f47163 | 2001-07-04 03:53:15 +0000 | [diff] [blame] | 630 | } |
djm@openbsd.org | 25f8376 | 2017-03-15 02:25:09 +0000 | [diff] [blame] | 631 | if (pkcs11_del_provider(canonical_provider) == 0) |
Damien Miller | 7ea845e | 2010-02-12 09:21:02 +1100 | [diff] [blame] | 632 | success = 1; |
| 633 | else |
djm@openbsd.org | f4a6a88 | 2017-04-30 23:29:10 +0000 | [diff] [blame] | 634 | error("%s: pkcs11_del_provider failed", __func__); |
deraadt@openbsd.org | 1a321bf | 2017-03-15 03:52:30 +0000 | [diff] [blame] | 635 | send: |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 636 | free(provider); |
markus@openbsd.org | 139ca81 | 2015-01-14 13:09:09 +0000 | [diff] [blame] | 637 | send_status(e, success); |
Ben Lindstrom | 3f47163 | 2001-07-04 03:53:15 +0000 | [diff] [blame] | 638 | } |
Damien Miller | 7ea845e | 2010-02-12 09:21:02 +1100 | [diff] [blame] | 639 | #endif /* ENABLE_PKCS11 */ |
Ben Lindstrom | 3f47163 | 2001-07-04 03:53:15 +0000 | [diff] [blame] | 640 | |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 641 | /* dispatch incoming messages */ |
| 642 | |
djm@openbsd.org | fd0e8fa | 2017-07-19 01:15:02 +0000 | [diff] [blame] | 643 | static int |
| 644 | process_message(u_int socknum) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 645 | { |
markus@openbsd.org | 139ca81 | 2015-01-14 13:09:09 +0000 | [diff] [blame] | 646 | u_int msg_len; |
| 647 | u_char type; |
| 648 | const u_char *cp; |
| 649 | int r; |
djm@openbsd.org | fd0e8fa | 2017-07-19 01:15:02 +0000 | [diff] [blame] | 650 | SocketEntry *e; |
| 651 | |
| 652 | if (socknum >= sockets_alloc) { |
| 653 | fatal("%s: socket number %u >= allocated %u", |
| 654 | __func__, socknum, sockets_alloc); |
| 655 | } |
| 656 | e = &sockets[socknum]; |
Ben Lindstrom | 61d328a | 2002-06-06 21:54:57 +0000 | [diff] [blame] | 657 | |
markus@openbsd.org | 139ca81 | 2015-01-14 13:09:09 +0000 | [diff] [blame] | 658 | if (sshbuf_len(e->input) < 5) |
djm@openbsd.org | fd0e8fa | 2017-07-19 01:15:02 +0000 | [diff] [blame] | 659 | return 0; /* Incomplete message header. */ |
markus@openbsd.org | 139ca81 | 2015-01-14 13:09:09 +0000 | [diff] [blame] | 660 | cp = sshbuf_ptr(e->input); |
| 661 | msg_len = PEEK_U32(cp); |
djm@openbsd.org | fd0e8fa | 2017-07-19 01:15:02 +0000 | [diff] [blame] | 662 | if (msg_len > AGENT_MAX_LEN) { |
| 663 | debug("%s: socket %u (fd=%d) message too long %u > %u", |
| 664 | __func__, socknum, e->fd, msg_len, AGENT_MAX_LEN); |
| 665 | return -1; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 666 | } |
markus@openbsd.org | 139ca81 | 2015-01-14 13:09:09 +0000 | [diff] [blame] | 667 | if (sshbuf_len(e->input) < msg_len + 4) |
djm@openbsd.org | fd0e8fa | 2017-07-19 01:15:02 +0000 | [diff] [blame] | 668 | return 0; /* Incomplete message body. */ |
Ben Lindstrom | 21d1ed8 | 2002-06-06 21:48:57 +0000 | [diff] [blame] | 669 | |
| 670 | /* move the current input to e->request */ |
markus@openbsd.org | 139ca81 | 2015-01-14 13:09:09 +0000 | [diff] [blame] | 671 | sshbuf_reset(e->request); |
| 672 | if ((r = sshbuf_get_stringb(e->input, e->request)) != 0 || |
djm@openbsd.org | fd0e8fa | 2017-07-19 01:15:02 +0000 | [diff] [blame] | 673 | (r = sshbuf_get_u8(e->request, &type)) != 0) { |
| 674 | if (r == SSH_ERR_MESSAGE_INCOMPLETE || |
| 675 | r == SSH_ERR_STRING_TOO_LARGE) { |
| 676 | debug("%s: buffer error: %s", __func__, ssh_err(r)); |
| 677 | return -1; |
| 678 | } |
markus@openbsd.org | 139ca81 | 2015-01-14 13:09:09 +0000 | [diff] [blame] | 679 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
djm@openbsd.org | fd0e8fa | 2017-07-19 01:15:02 +0000 | [diff] [blame] | 680 | } |
| 681 | |
| 682 | debug("%s: socket %u (fd=%d) type %d", __func__, socknum, e->fd, type); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 683 | |
Ben Lindstrom | 2f71704 | 2002-06-06 21:52:03 +0000 | [diff] [blame] | 684 | /* check wheter agent is locked */ |
| 685 | if (locked && type != SSH_AGENTC_UNLOCK) { |
markus@openbsd.org | 139ca81 | 2015-01-14 13:09:09 +0000 | [diff] [blame] | 686 | sshbuf_reset(e->request); |
Ben Lindstrom | 2f71704 | 2002-06-06 21:52:03 +0000 | [diff] [blame] | 687 | switch (type) { |
Ben Lindstrom | 2f71704 | 2002-06-06 21:52:03 +0000 | [diff] [blame] | 688 | case SSH2_AGENTC_REQUEST_IDENTITIES: |
| 689 | /* send empty lists */ |
djm@openbsd.org | f4a6a88 | 2017-04-30 23:29:10 +0000 | [diff] [blame] | 690 | no_identities(e); |
Ben Lindstrom | 2f71704 | 2002-06-06 21:52:03 +0000 | [diff] [blame] | 691 | break; |
| 692 | default: |
| 693 | /* send a fail message for all other request types */ |
markus@openbsd.org | 139ca81 | 2015-01-14 13:09:09 +0000 | [diff] [blame] | 694 | send_status(e, 0); |
Ben Lindstrom | 2f71704 | 2002-06-06 21:52:03 +0000 | [diff] [blame] | 695 | } |
djm@openbsd.org | fd0e8fa | 2017-07-19 01:15:02 +0000 | [diff] [blame] | 696 | return 0; |
Ben Lindstrom | 2f71704 | 2002-06-06 21:52:03 +0000 | [diff] [blame] | 697 | } |
| 698 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 699 | switch (type) { |
Ben Lindstrom | 2f71704 | 2002-06-06 21:52:03 +0000 | [diff] [blame] | 700 | case SSH_AGENTC_LOCK: |
| 701 | case SSH_AGENTC_UNLOCK: |
| 702 | process_lock_agent(e, type == SSH_AGENTC_LOCK); |
| 703 | break; |
djm@openbsd.org | 2f04af9 | 2015-03-04 21:12:59 +0000 | [diff] [blame] | 704 | case SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES: |
djm@openbsd.org | f4a6a88 | 2017-04-30 23:29:10 +0000 | [diff] [blame] | 705 | process_remove_all_identities(e); /* safe for !WITH_SSH1 */ |
djm@openbsd.org | 2f04af9 | 2015-03-04 21:12:59 +0000 | [diff] [blame] | 706 | break; |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 707 | /* ssh2 */ |
| 708 | case SSH2_AGENTC_SIGN_REQUEST: |
| 709 | process_sign_request2(e); |
| 710 | break; |
| 711 | case SSH2_AGENTC_REQUEST_IDENTITIES: |
djm@openbsd.org | f4a6a88 | 2017-04-30 23:29:10 +0000 | [diff] [blame] | 712 | process_request_identities(e); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 713 | break; |
| 714 | case SSH2_AGENTC_ADD_IDENTITY: |
Ben Lindstrom | 2b266b7 | 2002-06-21 00:08:39 +0000 | [diff] [blame] | 715 | case SSH2_AGENTC_ADD_ID_CONSTRAINED: |
djm@openbsd.org | f4a6a88 | 2017-04-30 23:29:10 +0000 | [diff] [blame] | 716 | process_add_identity(e); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 717 | break; |
| 718 | case SSH2_AGENTC_REMOVE_IDENTITY: |
djm@openbsd.org | f4a6a88 | 2017-04-30 23:29:10 +0000 | [diff] [blame] | 719 | process_remove_identity(e); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 720 | break; |
| 721 | case SSH2_AGENTC_REMOVE_ALL_IDENTITIES: |
djm@openbsd.org | f4a6a88 | 2017-04-30 23:29:10 +0000 | [diff] [blame] | 722 | process_remove_all_identities(e); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 723 | break; |
Damien Miller | 7ea845e | 2010-02-12 09:21:02 +1100 | [diff] [blame] | 724 | #ifdef ENABLE_PKCS11 |
Ben Lindstrom | 3f47163 | 2001-07-04 03:53:15 +0000 | [diff] [blame] | 725 | case SSH_AGENTC_ADD_SMARTCARD_KEY: |
Damien Miller | d94f20d | 2003-06-11 22:06:33 +1000 | [diff] [blame] | 726 | case SSH_AGENTC_ADD_SMARTCARD_KEY_CONSTRAINED: |
Ben Lindstrom | 3f47163 | 2001-07-04 03:53:15 +0000 | [diff] [blame] | 727 | process_add_smartcard_key(e); |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 728 | break; |
Ben Lindstrom | 3f47163 | 2001-07-04 03:53:15 +0000 | [diff] [blame] | 729 | case SSH_AGENTC_REMOVE_SMARTCARD_KEY: |
| 730 | process_remove_smartcard_key(e); |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 731 | break; |
Damien Miller | 7ea845e | 2010-02-12 09:21:02 +1100 | [diff] [blame] | 732 | #endif /* ENABLE_PKCS11 */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 733 | default: |
| 734 | /* Unknown message. Respond with failure. */ |
| 735 | error("Unknown message %d", type); |
markus@openbsd.org | 139ca81 | 2015-01-14 13:09:09 +0000 | [diff] [blame] | 736 | sshbuf_reset(e->request); |
| 737 | send_status(e, 0); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 738 | break; |
| 739 | } |
djm@openbsd.org | fd0e8fa | 2017-07-19 01:15:02 +0000 | [diff] [blame] | 740 | return 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 741 | } |
| 742 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 743 | static void |
Ben Lindstrom | 65366a8 | 2001-12-06 16:32:47 +0000 | [diff] [blame] | 744 | new_socket(sock_type type, int fd) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 745 | { |
Darren Tucker | fb16b24 | 2003-09-22 21:04:23 +1000 | [diff] [blame] | 746 | u_int i, old_alloc, new_alloc; |
Ben Lindstrom | 822b634 | 2002-06-23 21:38:49 +0000 | [diff] [blame] | 747 | |
Damien Miller | 232711f | 2004-06-15 10:35:30 +1000 | [diff] [blame] | 748 | set_nonblock(fd); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 749 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 750 | if (fd > max_fd) |
| 751 | max_fd = fd; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 752 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 753 | for (i = 0; i < sockets_alloc; i++) |
| 754 | if (sockets[i].type == AUTH_UNUSED) { |
| 755 | sockets[i].fd = fd; |
markus@openbsd.org | 139ca81 | 2015-01-14 13:09:09 +0000 | [diff] [blame] | 756 | if ((sockets[i].input = sshbuf_new()) == NULL) |
| 757 | fatal("%s: sshbuf_new failed", __func__); |
| 758 | if ((sockets[i].output = sshbuf_new()) == NULL) |
| 759 | fatal("%s: sshbuf_new failed", __func__); |
| 760 | if ((sockets[i].request = sshbuf_new()) == NULL) |
| 761 | fatal("%s: sshbuf_new failed", __func__); |
Darren Tucker | fb16b24 | 2003-09-22 21:04:23 +1000 | [diff] [blame] | 762 | sockets[i].type = type; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 763 | return; |
| 764 | } |
| 765 | old_alloc = sockets_alloc; |
Darren Tucker | fb16b24 | 2003-09-22 21:04:23 +1000 | [diff] [blame] | 766 | new_alloc = sockets_alloc + 10; |
deraadt@openbsd.org | 657a5fb | 2015-04-24 01:36:00 +0000 | [diff] [blame] | 767 | sockets = xreallocarray(sockets, new_alloc, sizeof(sockets[0])); |
Darren Tucker | fb16b24 | 2003-09-22 21:04:23 +1000 | [diff] [blame] | 768 | for (i = old_alloc; i < new_alloc; i++) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 769 | sockets[i].type = AUTH_UNUSED; |
Darren Tucker | fb16b24 | 2003-09-22 21:04:23 +1000 | [diff] [blame] | 770 | sockets_alloc = new_alloc; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 771 | sockets[old_alloc].fd = fd; |
markus@openbsd.org | 139ca81 | 2015-01-14 13:09:09 +0000 | [diff] [blame] | 772 | if ((sockets[old_alloc].input = sshbuf_new()) == NULL) |
| 773 | fatal("%s: sshbuf_new failed", __func__); |
| 774 | if ((sockets[old_alloc].output = sshbuf_new()) == NULL) |
| 775 | fatal("%s: sshbuf_new failed", __func__); |
| 776 | if ((sockets[old_alloc].request = sshbuf_new()) == NULL) |
| 777 | fatal("%s: sshbuf_new failed", __func__); |
Darren Tucker | fb16b24 | 2003-09-22 21:04:23 +1000 | [diff] [blame] | 778 | sockets[old_alloc].type = type; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 779 | } |
| 780 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 781 | static int |
djm@openbsd.org | fd0e8fa | 2017-07-19 01:15:02 +0000 | [diff] [blame] | 782 | handle_socket_read(u_int socknum) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 783 | { |
djm@openbsd.org | fd0e8fa | 2017-07-19 01:15:02 +0000 | [diff] [blame] | 784 | struct sockaddr_un sunaddr; |
| 785 | socklen_t slen; |
| 786 | uid_t euid; |
| 787 | gid_t egid; |
| 788 | int fd; |
| 789 | |
| 790 | slen = sizeof(sunaddr); |
| 791 | fd = accept(sockets[socknum].fd, (struct sockaddr *)&sunaddr, &slen); |
| 792 | if (fd < 0) { |
| 793 | error("accept from AUTH_SOCKET: %s", strerror(errno)); |
| 794 | return -1; |
| 795 | } |
| 796 | if (getpeereid(fd, &euid, &egid) < 0) { |
| 797 | error("getpeereid %d failed: %s", fd, strerror(errno)); |
| 798 | close(fd); |
| 799 | return -1; |
| 800 | } |
| 801 | if ((euid != 0) && (getuid() != euid)) { |
| 802 | error("uid mismatch: peer euid %u != uid %u", |
| 803 | (u_int) euid, (u_int) getuid()); |
| 804 | close(fd); |
| 805 | return -1; |
| 806 | } |
| 807 | new_socket(AUTH_CONNECTION, fd); |
| 808 | return 0; |
| 809 | } |
| 810 | |
| 811 | static int |
| 812 | handle_conn_read(u_int socknum) |
| 813 | { |
| 814 | char buf[1024]; |
| 815 | ssize_t len; |
| 816 | int r; |
| 817 | |
| 818 | if ((len = read(sockets[socknum].fd, buf, sizeof(buf))) <= 0) { |
| 819 | if (len == -1) { |
| 820 | if (errno == EAGAIN || errno == EINTR) |
| 821 | return 0; |
| 822 | error("%s: read error on socket %u (fd %d): %s", |
| 823 | __func__, socknum, sockets[socknum].fd, |
| 824 | strerror(errno)); |
| 825 | } |
| 826 | return -1; |
| 827 | } |
| 828 | if ((r = sshbuf_put(sockets[socknum].input, buf, len)) != 0) |
| 829 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
| 830 | explicit_bzero(buf, sizeof(buf)); |
| 831 | process_message(socknum); |
| 832 | return 0; |
| 833 | } |
| 834 | |
| 835 | static int |
| 836 | handle_conn_write(u_int socknum) |
| 837 | { |
| 838 | ssize_t len; |
| 839 | int r; |
| 840 | |
| 841 | if (sshbuf_len(sockets[socknum].output) == 0) |
| 842 | return 0; /* shouldn't happen */ |
| 843 | if ((len = write(sockets[socknum].fd, |
| 844 | sshbuf_ptr(sockets[socknum].output), |
| 845 | sshbuf_len(sockets[socknum].output))) <= 0) { |
| 846 | if (len == -1) { |
| 847 | if (errno == EAGAIN || errno == EINTR) |
| 848 | return 0; |
| 849 | error("%s: read error on socket %u (fd %d): %s", |
| 850 | __func__, socknum, sockets[socknum].fd, |
| 851 | strerror(errno)); |
| 852 | } |
| 853 | return -1; |
| 854 | } |
| 855 | if ((r = sshbuf_consume(sockets[socknum].output, len)) != 0) |
| 856 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
| 857 | return 0; |
| 858 | } |
| 859 | |
| 860 | static void |
| 861 | after_poll(struct pollfd *pfd, size_t npfd) |
| 862 | { |
| 863 | size_t i; |
| 864 | u_int socknum; |
| 865 | |
| 866 | for (i = 0; i < npfd; i++) { |
| 867 | if (pfd[i].revents == 0) |
| 868 | continue; |
| 869 | /* Find sockets entry */ |
| 870 | for (socknum = 0; socknum < sockets_alloc; socknum++) { |
| 871 | if (sockets[socknum].type != AUTH_SOCKET && |
| 872 | sockets[socknum].type != AUTH_CONNECTION) |
| 873 | continue; |
| 874 | if (pfd[i].fd == sockets[socknum].fd) |
| 875 | break; |
| 876 | } |
| 877 | if (socknum >= sockets_alloc) { |
| 878 | error("%s: no socket for fd %d", __func__, pfd[i].fd); |
| 879 | continue; |
| 880 | } |
| 881 | /* Process events */ |
| 882 | switch (sockets[socknum].type) { |
| 883 | case AUTH_SOCKET: |
| 884 | if ((pfd[i].revents & (POLLIN|POLLERR)) != 0 && |
| 885 | handle_socket_read(socknum) != 0) |
| 886 | close_socket(&sockets[socknum]); |
| 887 | break; |
| 888 | case AUTH_CONNECTION: |
| 889 | if ((pfd[i].revents & (POLLIN|POLLERR)) != 0 && |
| 890 | handle_conn_read(socknum) != 0) { |
| 891 | close_socket(&sockets[socknum]); |
| 892 | break; |
| 893 | } |
| 894 | if ((pfd[i].revents & (POLLOUT|POLLHUP)) != 0 && |
| 895 | handle_conn_write(socknum) != 0) |
| 896 | close_socket(&sockets[socknum]); |
| 897 | break; |
| 898 | default: |
| 899 | break; |
| 900 | } |
| 901 | } |
| 902 | } |
| 903 | |
| 904 | static int |
| 905 | prepare_poll(struct pollfd **pfdp, size_t *npfdp, int *timeoutp) |
| 906 | { |
| 907 | struct pollfd *pfd = *pfdp; |
| 908 | size_t i, j, npfd = 0; |
Darren Tucker | 5511925 | 2013-06-02 07:43:59 +1000 | [diff] [blame] | 909 | time_t deadline; |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 910 | |
djm@openbsd.org | fd0e8fa | 2017-07-19 01:15:02 +0000 | [diff] [blame] | 911 | /* Count active sockets */ |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 912 | for (i = 0; i < sockets_alloc; i++) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 913 | switch (sockets[i].type) { |
| 914 | case AUTH_SOCKET: |
| 915 | case AUTH_CONNECTION: |
djm@openbsd.org | fd0e8fa | 2017-07-19 01:15:02 +0000 | [diff] [blame] | 916 | npfd++; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 917 | break; |
| 918 | case AUTH_UNUSED: |
| 919 | break; |
| 920 | default: |
| 921 | fatal("Unknown socket type %d", sockets[i].type); |
| 922 | break; |
| 923 | } |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 924 | } |
djm@openbsd.org | fd0e8fa | 2017-07-19 01:15:02 +0000 | [diff] [blame] | 925 | if (npfd != *npfdp && |
| 926 | (pfd = recallocarray(pfd, *npfdp, npfd, sizeof(*pfd))) == NULL) |
| 927 | fatal("%s: recallocarray failed", __func__); |
| 928 | *pfdp = pfd; |
| 929 | *npfdp = npfd; |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 930 | |
djm@openbsd.org | fd0e8fa | 2017-07-19 01:15:02 +0000 | [diff] [blame] | 931 | for (i = j = 0; i < sockets_alloc; i++) { |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 932 | switch (sockets[i].type) { |
| 933 | case AUTH_SOCKET: |
| 934 | case AUTH_CONNECTION: |
djm@openbsd.org | fd0e8fa | 2017-07-19 01:15:02 +0000 | [diff] [blame] | 935 | pfd[j].fd = sockets[i].fd; |
| 936 | pfd[j].revents = 0; |
| 937 | /* XXX backoff when input buffer full */ |
| 938 | pfd[j].events = POLLIN; |
markus@openbsd.org | 139ca81 | 2015-01-14 13:09:09 +0000 | [diff] [blame] | 939 | if (sshbuf_len(sockets[i].output) > 0) |
djm@openbsd.org | fd0e8fa | 2017-07-19 01:15:02 +0000 | [diff] [blame] | 940 | pfd[j].events |= POLLOUT; |
| 941 | j++; |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 942 | break; |
| 943 | default: |
| 944 | break; |
| 945 | } |
| 946 | } |
Darren Tucker | 2812dc9 | 2007-03-21 20:45:06 +1100 | [diff] [blame] | 947 | deadline = reaper(); |
| 948 | if (parent_alive_interval != 0) |
| 949 | deadline = (deadline == 0) ? parent_alive_interval : |
deraadt@openbsd.org | 9136ec1 | 2016-09-12 01:22:38 +0000 | [diff] [blame] | 950 | MINIMUM(deadline, parent_alive_interval); |
Darren Tucker | 2812dc9 | 2007-03-21 20:45:06 +1100 | [diff] [blame] | 951 | if (deadline == 0) { |
djm@openbsd.org | 9f0e44e | 2017-07-24 04:34:28 +0000 | [diff] [blame] | 952 | *timeoutp = -1; /* INFTIM */ |
Darren Tucker | 2812dc9 | 2007-03-21 20:45:06 +1100 | [diff] [blame] | 953 | } else { |
djm@openbsd.org | fd0e8fa | 2017-07-19 01:15:02 +0000 | [diff] [blame] | 954 | if (deadline > INT_MAX / 1000) |
| 955 | *timeoutp = INT_MAX / 1000; |
| 956 | else |
| 957 | *timeoutp = deadline * 1000; |
Darren Tucker | 2812dc9 | 2007-03-21 20:45:06 +1100 | [diff] [blame] | 958 | } |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 959 | return (1); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 960 | } |
| 961 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 962 | static void |
Darren Tucker | 6fa8abd | 2003-09-22 21:10:21 +1000 | [diff] [blame] | 963 | cleanup_socket(void) |
Damien Miller | 792c511 | 1999-10-29 09:47:09 +1000 | [diff] [blame] | 964 | { |
Damien Miller | b1e967c | 2014-07-03 21:22:40 +1000 | [diff] [blame] | 965 | if (cleanup_pid != 0 && getpid() != cleanup_pid) |
| 966 | return; |
| 967 | debug("%s: cleanup", __func__); |
Ben Lindstrom | 77808ab | 2001-01-26 05:10:34 +0000 | [diff] [blame] | 968 | if (socket_name[0]) |
| 969 | unlink(socket_name); |
| 970 | if (socket_dir[0]) |
| 971 | rmdir(socket_dir); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 972 | } |
| 973 | |
Darren Tucker | 3e33cec | 2003-10-02 16:12:36 +1000 | [diff] [blame] | 974 | void |
Damien Miller | 792c511 | 1999-10-29 09:47:09 +1000 | [diff] [blame] | 975 | cleanup_exit(int i) |
| 976 | { |
Darren Tucker | 6fa8abd | 2003-09-22 21:10:21 +1000 | [diff] [blame] | 977 | cleanup_socket(); |
| 978 | _exit(i); |
Damien Miller | 792c511 | 1999-10-29 09:47:09 +1000 | [diff] [blame] | 979 | } |
| 980 | |
Damien Miller | 1c13bd8 | 2006-03-26 14:28:14 +1100 | [diff] [blame] | 981 | /*ARGSUSED*/ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 982 | static void |
Ben Lindstrom | 77808ab | 2001-01-26 05:10:34 +0000 | [diff] [blame] | 983 | cleanup_handler(int sig) |
| 984 | { |
Darren Tucker | 6fa8abd | 2003-09-22 21:10:21 +1000 | [diff] [blame] | 985 | cleanup_socket(); |
Damien Miller | 7ea845e | 2010-02-12 09:21:02 +1100 | [diff] [blame] | 986 | #ifdef ENABLE_PKCS11 |
| 987 | pkcs11_terminate(); |
| 988 | #endif |
Ben Lindstrom | 77808ab | 2001-01-26 05:10:34 +0000 | [diff] [blame] | 989 | _exit(2); |
| 990 | } |
| 991 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 992 | static void |
Darren Tucker | 2812dc9 | 2007-03-21 20:45:06 +1100 | [diff] [blame] | 993 | check_parent_exists(void) |
Ben Lindstrom | 0250da0 | 2001-07-22 20:44:00 +0000 | [diff] [blame] | 994 | { |
Darren Tucker | 3e78a51 | 2011-06-03 14:14:16 +1000 | [diff] [blame] | 995 | /* |
| 996 | * If our parent has exited then getppid() will return (pid_t)1, |
| 997 | * so testing for that should be safe. |
| 998 | */ |
| 999 | if (parent_pid != -1 && getppid() != parent_pid) { |
Ben Lindstrom | 0250da0 | 2001-07-22 20:44:00 +0000 | [diff] [blame] | 1000 | /* printf("Parent has died - Authentication agent exiting.\n"); */ |
Darren Tucker | 2812dc9 | 2007-03-21 20:45:06 +1100 | [diff] [blame] | 1001 | cleanup_socket(); |
| 1002 | _exit(2); |
Ben Lindstrom | 0250da0 | 2001-07-22 20:44:00 +0000 | [diff] [blame] | 1003 | } |
Ben Lindstrom | 0250da0 | 2001-07-22 20:44:00 +0000 | [diff] [blame] | 1004 | } |
| 1005 | |
| 1006 | static void |
Ben Lindstrom | 28072eb | 2001-02-10 23:13:41 +0000 | [diff] [blame] | 1007 | usage(void) |
Damien Miller | 792c511 | 1999-10-29 09:47:09 +1000 | [diff] [blame] | 1008 | { |
Damien Miller | f0858de | 2014-04-20 13:01:30 +1000 | [diff] [blame] | 1009 | fprintf(stderr, |
jmc@openbsd.org | b7ca276 | 2015-04-24 06:26:49 +0000 | [diff] [blame] | 1010 | "usage: ssh-agent [-c | -s] [-Dd] [-a bind_address] [-E fingerprint_hash]\n" |
djm@openbsd.org | 786d599 | 2016-11-30 03:07:37 +0000 | [diff] [blame] | 1011 | " [-P pkcs11_whitelist] [-t life] [command [arg ...]]\n" |
Damien Miller | f0858de | 2014-04-20 13:01:30 +1000 | [diff] [blame] | 1012 | " ssh-agent [-c | -s] -k\n"); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1013 | exit(1); |
Damien Miller | 792c511 | 1999-10-29 09:47:09 +1000 | [diff] [blame] | 1014 | } |
| 1015 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1016 | int |
| 1017 | main(int ac, char **av) |
| 1018 | { |
djm@openbsd.org | 2ea9746 | 2015-04-24 05:26:44 +0000 | [diff] [blame] | 1019 | int c_flag = 0, d_flag = 0, D_flag = 0, k_flag = 0, s_flag = 0; |
Darren Tucker | cf0d2db | 2007-02-28 21:19:58 +1100 | [diff] [blame] | 1020 | int sock, fd, ch, result, saved_errno; |
Ben Lindstrom | 822b634 | 2002-06-23 21:38:49 +0000 | [diff] [blame] | 1021 | char *shell, *format, *pidstr, *agentsocket = NULL; |
Ben Lindstrom | 2c467a2 | 2000-12-27 04:57:41 +0000 | [diff] [blame] | 1022 | #ifdef HAVE_SETRLIMIT |
Ben Lindstrom | c72745a | 2000-12-02 19:03:54 +0000 | [diff] [blame] | 1023 | struct rlimit rlim; |
Ben Lindstrom | 2c467a2 | 2000-12-27 04:57:41 +0000 | [diff] [blame] | 1024 | #endif |
Damien Miller | 615f939 | 2000-05-17 22:53:33 +1000 | [diff] [blame] | 1025 | extern int optind; |
Ben Lindstrom | 883844d | 2002-06-23 00:20:50 +0000 | [diff] [blame] | 1026 | extern char *optarg; |
Ben Lindstrom | 822b634 | 2002-06-23 21:38:49 +0000 | [diff] [blame] | 1027 | pid_t pid; |
| 1028 | char pidstrbuf[1 + 3 * sizeof pid]; |
Darren Tucker | 9013323 | 2009-06-21 17:50:15 +1000 | [diff] [blame] | 1029 | size_t len; |
Damien Miller | ab2ec58 | 2014-07-18 15:04:47 +1000 | [diff] [blame] | 1030 | mode_t prev_mask; |
djm@openbsd.org | 9f0e44e | 2017-07-24 04:34:28 +0000 | [diff] [blame] | 1031 | int timeout = -1; /* INFTIM */ |
djm@openbsd.org | fd0e8fa | 2017-07-19 01:15:02 +0000 | [diff] [blame] | 1032 | struct pollfd *pfd = NULL; |
| 1033 | size_t npfd = 0; |
Kevin Steves | de41bc6 | 2000-12-14 00:04:08 +0000 | [diff] [blame] | 1034 | |
dtucker@openbsd.org | ffb1e7e | 2016-02-15 09:47:49 +0000 | [diff] [blame] | 1035 | ssh_malloc_init(); /* must be called before any mallocs */ |
Darren Tucker | ce321d8 | 2005-10-03 18:11:24 +1000 | [diff] [blame] | 1036 | /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */ |
| 1037 | sanitise_stdfd(); |
| 1038 | |
Damien Miller | 6cffb9a | 2002-09-04 16:20:26 +1000 | [diff] [blame] | 1039 | /* drop */ |
| 1040 | setegid(getgid()); |
| 1041 | setgid(getgid()); |
| 1042 | |
Darren Tucker | 0fb7f59 | 2016-06-09 16:23:07 +1000 | [diff] [blame] | 1043 | platform_disable_tracing(0); /* strict=no */ |
Damien Miller | 6c4914a | 2004-03-03 11:08:59 +1100 | [diff] [blame] | 1044 | |
Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 1045 | #ifdef WITH_OPENSSL |
Damien Miller | 4314c2b | 2010-09-10 11:12:09 +1000 | [diff] [blame] | 1046 | OpenSSL_add_all_algorithms(); |
Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 1047 | #endif |
Ben Lindstrom | d09fcf5 | 2001-03-29 00:29:54 +0000 | [diff] [blame] | 1048 | |
Damien Miller | 59d3d5b | 2003-08-22 09:34:41 +1000 | [diff] [blame] | 1049 | __progname = ssh_get_progname(av[0]); |
Damien Miller | 60bc517 | 2001-03-19 09:38:15 +1100 | [diff] [blame] | 1050 | seed_rng(); |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 1051 | |
djm@openbsd.org | 786d599 | 2016-11-30 03:07:37 +0000 | [diff] [blame] | 1052 | while ((ch = getopt(ac, av, "cDdksE:a:P:t:")) != -1) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1053 | switch (ch) { |
djm@openbsd.org | 56d1c83 | 2014-12-21 22:27:55 +0000 | [diff] [blame] | 1054 | case 'E': |
| 1055 | fingerprint_hash = ssh_digest_alg_by_name(optarg); |
| 1056 | if (fingerprint_hash == -1) |
| 1057 | fatal("Invalid hash algorithm \"%s\"", optarg); |
| 1058 | break; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1059 | case 'c': |
| 1060 | if (s_flag) |
| 1061 | usage(); |
| 1062 | c_flag++; |
| 1063 | break; |
| 1064 | case 'k': |
| 1065 | k_flag++; |
| 1066 | break; |
djm@openbsd.org | 786d599 | 2016-11-30 03:07:37 +0000 | [diff] [blame] | 1067 | case 'P': |
| 1068 | if (pkcs11_whitelist != NULL) |
| 1069 | fatal("-P option already specified"); |
| 1070 | pkcs11_whitelist = xstrdup(optarg); |
| 1071 | break; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1072 | case 's': |
| 1073 | if (c_flag) |
| 1074 | usage(); |
| 1075 | s_flag++; |
| 1076 | break; |
Ben Lindstrom | d94580c | 2001-07-04 03:48:02 +0000 | [diff] [blame] | 1077 | case 'd': |
djm@openbsd.org | 2ea9746 | 2015-04-24 05:26:44 +0000 | [diff] [blame] | 1078 | if (d_flag || D_flag) |
Ben Lindstrom | d94580c | 2001-07-04 03:48:02 +0000 | [diff] [blame] | 1079 | usage(); |
| 1080 | d_flag++; |
| 1081 | break; |
djm@openbsd.org | 2ea9746 | 2015-04-24 05:26:44 +0000 | [diff] [blame] | 1082 | case 'D': |
| 1083 | if (d_flag || D_flag) |
| 1084 | usage(); |
| 1085 | D_flag++; |
| 1086 | break; |
Ben Lindstrom | b7788f3 | 2002-06-06 21:46:08 +0000 | [diff] [blame] | 1087 | case 'a': |
| 1088 | agentsocket = optarg; |
| 1089 | break; |
Damien Miller | 53d8148 | 2003-01-22 11:47:19 +1100 | [diff] [blame] | 1090 | case 't': |
| 1091 | if ((lifetime = convtime(optarg)) == -1) { |
| 1092 | fprintf(stderr, "Invalid lifetime\n"); |
| 1093 | usage(); |
| 1094 | } |
| 1095 | break; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1096 | default: |
| 1097 | usage(); |
| 1098 | } |
Damien Miller | 792c511 | 1999-10-29 09:47:09 +1000 | [diff] [blame] | 1099 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1100 | ac -= optind; |
| 1101 | av += optind; |
Damien Miller | 792c511 | 1999-10-29 09:47:09 +1000 | [diff] [blame] | 1102 | |
djm@openbsd.org | 2ea9746 | 2015-04-24 05:26:44 +0000 | [diff] [blame] | 1103 | if (ac > 0 && (c_flag || k_flag || s_flag || d_flag || D_flag)) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1104 | usage(); |
Damien Miller | 792c511 | 1999-10-29 09:47:09 +1000 | [diff] [blame] | 1105 | |
djm@openbsd.org | 786d599 | 2016-11-30 03:07:37 +0000 | [diff] [blame] | 1106 | if (pkcs11_whitelist == NULL) |
| 1107 | pkcs11_whitelist = xstrdup(DEFAULT_PKCS11_WHITELIST); |
| 1108 | |
Ben Lindstrom | eecdf23 | 2002-04-02 21:03:51 +0000 | [diff] [blame] | 1109 | if (ac == 0 && !c_flag && !s_flag) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1110 | shell = getenv("SHELL"); |
Darren Tucker | 9013323 | 2009-06-21 17:50:15 +1000 | [diff] [blame] | 1111 | if (shell != NULL && (len = strlen(shell)) > 2 && |
| 1112 | strncmp(shell + len - 3, "csh", 3) == 0) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1113 | c_flag = 1; |
Damien Miller | 792c511 | 1999-10-29 09:47:09 +1000 | [diff] [blame] | 1114 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1115 | if (k_flag) { |
Damien Miller | 89c3fe4 | 2006-03-31 23:11:28 +1100 | [diff] [blame] | 1116 | const char *errstr = NULL; |
| 1117 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1118 | pidstr = getenv(SSH_AGENTPID_ENV_NAME); |
| 1119 | if (pidstr == NULL) { |
| 1120 | fprintf(stderr, "%s not set, cannot kill agent\n", |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 1121 | SSH_AGENTPID_ENV_NAME); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1122 | exit(1); |
| 1123 | } |
Damien Miller | 89c3fe4 | 2006-03-31 23:11:28 +1100 | [diff] [blame] | 1124 | pid = (int)strtonum(pidstr, 2, INT_MAX, &errstr); |
| 1125 | if (errstr) { |
| 1126 | fprintf(stderr, |
| 1127 | "%s=\"%s\", which is not a good PID: %s\n", |
| 1128 | SSH_AGENTPID_ENV_NAME, pidstr, errstr); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1129 | exit(1); |
| 1130 | } |
| 1131 | if (kill(pid, SIGTERM) == -1) { |
| 1132 | perror("kill"); |
| 1133 | exit(1); |
| 1134 | } |
| 1135 | format = c_flag ? "unsetenv %s;\n" : "unset %s;\n"; |
| 1136 | printf(format, SSH_AUTHSOCKET_ENV_NAME); |
| 1137 | printf(format, SSH_AGENTPID_ENV_NAME); |
Ben Lindstrom | ce0f634 | 2002-06-11 16:42:49 +0000 | [diff] [blame] | 1138 | printf("echo Agent pid %ld killed;\n", (long)pid); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1139 | exit(0); |
Damien Miller | 792c511 | 1999-10-29 09:47:09 +1000 | [diff] [blame] | 1140 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1141 | parent_pid = getpid(); |
| 1142 | |
Ben Lindstrom | b7788f3 | 2002-06-06 21:46:08 +0000 | [diff] [blame] | 1143 | if (agentsocket == NULL) { |
| 1144 | /* Create private directory for agent socket */ |
Damien Miller | 2cd6293 | 2010-12-01 11:50:35 +1100 | [diff] [blame] | 1145 | mktemp_proto(socket_dir, sizeof(socket_dir)); |
Ben Lindstrom | b7788f3 | 2002-06-06 21:46:08 +0000 | [diff] [blame] | 1146 | if (mkdtemp(socket_dir) == NULL) { |
| 1147 | perror("mkdtemp: private socket dir"); |
| 1148 | exit(1); |
| 1149 | } |
Ben Lindstrom | ce0f634 | 2002-06-11 16:42:49 +0000 | [diff] [blame] | 1150 | snprintf(socket_name, sizeof socket_name, "%s/agent.%ld", socket_dir, |
| 1151 | (long)parent_pid); |
Ben Lindstrom | b7788f3 | 2002-06-06 21:46:08 +0000 | [diff] [blame] | 1152 | } else { |
| 1153 | /* Try to use specified agent socket */ |
| 1154 | socket_dir[0] = '\0'; |
| 1155 | strlcpy(socket_name, agentsocket, sizeof socket_name); |
Damien Miller | 792c511 | 1999-10-29 09:47:09 +1000 | [diff] [blame] | 1156 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1157 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1158 | /* |
| 1159 | * Create socket early so it will exist before command gets run from |
| 1160 | * the parent. |
| 1161 | */ |
Damien Miller | ab2ec58 | 2014-07-18 15:04:47 +1000 | [diff] [blame] | 1162 | prev_mask = umask(0177); |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 1163 | sock = unix_listener(socket_name, SSH_LISTEN_BACKLOG, 0); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1164 | if (sock < 0) { |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 1165 | /* XXX - unix_listener() calls error() not perror() */ |
Darren Tucker | 1dee868 | 2004-11-05 20:26:49 +1100 | [diff] [blame] | 1166 | *socket_name = '\0'; /* Don't unlink any existing file */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1167 | cleanup_exit(1); |
Damien Miller | 792c511 | 1999-10-29 09:47:09 +1000 | [diff] [blame] | 1168 | } |
Damien Miller | ab2ec58 | 2014-07-18 15:04:47 +1000 | [diff] [blame] | 1169 | umask(prev_mask); |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 1170 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1171 | /* |
| 1172 | * Fork, and have the parent execute the command, if any, or present |
| 1173 | * the socket data. The child continues as the authentication agent. |
| 1174 | */ |
djm@openbsd.org | 2ea9746 | 2015-04-24 05:26:44 +0000 | [diff] [blame] | 1175 | if (D_flag || d_flag) { |
| 1176 | log_init(__progname, |
| 1177 | d_flag ? SYSLOG_LEVEL_DEBUG3 : SYSLOG_LEVEL_INFO, |
| 1178 | SYSLOG_FACILITY_AUTH, 1); |
Ben Lindstrom | d94580c | 2001-07-04 03:48:02 +0000 | [diff] [blame] | 1179 | format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n"; |
| 1180 | printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name, |
| 1181 | SSH_AUTHSOCKET_ENV_NAME); |
Ben Lindstrom | ce0f634 | 2002-06-11 16:42:49 +0000 | [diff] [blame] | 1182 | printf("echo Agent pid %ld;\n", (long)parent_pid); |
dtucker@openbsd.org | 79394ed | 2015-12-11 02:29:03 +0000 | [diff] [blame] | 1183 | fflush(stdout); |
Ben Lindstrom | d94580c | 2001-07-04 03:48:02 +0000 | [diff] [blame] | 1184 | goto skip; |
| 1185 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1186 | pid = fork(); |
| 1187 | if (pid == -1) { |
| 1188 | perror("fork"); |
Damien Miller | c653b89 | 2002-02-08 22:05:41 +1100 | [diff] [blame] | 1189 | cleanup_exit(1); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1190 | } |
| 1191 | if (pid != 0) { /* Parent - execute the given command. */ |
| 1192 | close(sock); |
Ben Lindstrom | ce0f634 | 2002-06-11 16:42:49 +0000 | [diff] [blame] | 1193 | snprintf(pidstrbuf, sizeof pidstrbuf, "%ld", (long)pid); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1194 | if (ac == 0) { |
| 1195 | format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n"; |
| 1196 | printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name, |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 1197 | SSH_AUTHSOCKET_ENV_NAME); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1198 | printf(format, SSH_AGENTPID_ENV_NAME, pidstrbuf, |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 1199 | SSH_AGENTPID_ENV_NAME); |
Ben Lindstrom | ce0f634 | 2002-06-11 16:42:49 +0000 | [diff] [blame] | 1200 | printf("echo Agent pid %ld;\n", (long)pid); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1201 | exit(0); |
| 1202 | } |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 1203 | if (setenv(SSH_AUTHSOCKET_ENV_NAME, socket_name, 1) == -1 || |
| 1204 | setenv(SSH_AGENTPID_ENV_NAME, pidstrbuf, 1) == -1) { |
| 1205 | perror("setenv"); |
| 1206 | exit(1); |
| 1207 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1208 | execvp(av[0], av); |
| 1209 | perror(av[0]); |
| 1210 | exit(1); |
| 1211 | } |
Damien Miller | c653b89 | 2002-02-08 22:05:41 +1100 | [diff] [blame] | 1212 | /* child */ |
| 1213 | log_init(__progname, SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_AUTH, 0); |
Ben Lindstrom | 3fdf876 | 2001-07-22 20:40:24 +0000 | [diff] [blame] | 1214 | |
| 1215 | if (setsid() == -1) { |
Damien Miller | c653b89 | 2002-02-08 22:05:41 +1100 | [diff] [blame] | 1216 | error("setsid: %s", strerror(errno)); |
Ben Lindstrom | 3fdf876 | 2001-07-22 20:40:24 +0000 | [diff] [blame] | 1217 | cleanup_exit(1); |
| 1218 | } |
| 1219 | |
| 1220 | (void)chdir("/"); |
Damien Miller | 6c71179 | 2003-01-24 11:36:23 +1100 | [diff] [blame] | 1221 | if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) { |
| 1222 | /* XXX might close listen socket */ |
| 1223 | (void)dup2(fd, STDIN_FILENO); |
| 1224 | (void)dup2(fd, STDOUT_FILENO); |
| 1225 | (void)dup2(fd, STDERR_FILENO); |
| 1226 | if (fd > 2) |
| 1227 | close(fd); |
| 1228 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1229 | |
Ben Lindstrom | 2c467a2 | 2000-12-27 04:57:41 +0000 | [diff] [blame] | 1230 | #ifdef HAVE_SETRLIMIT |
Ben Lindstrom | c72745a | 2000-12-02 19:03:54 +0000 | [diff] [blame] | 1231 | /* deny core dumps, since memory contains unencrypted private keys */ |
| 1232 | rlim.rlim_cur = rlim.rlim_max = 0; |
| 1233 | if (setrlimit(RLIMIT_CORE, &rlim) < 0) { |
Damien Miller | c653b89 | 2002-02-08 22:05:41 +1100 | [diff] [blame] | 1234 | error("setrlimit RLIMIT_CORE: %s", strerror(errno)); |
Ben Lindstrom | c72745a | 2000-12-02 19:03:54 +0000 | [diff] [blame] | 1235 | cleanup_exit(1); |
| 1236 | } |
Ben Lindstrom | 2c467a2 | 2000-12-27 04:57:41 +0000 | [diff] [blame] | 1237 | #endif |
Ben Lindstrom | d94580c | 2001-07-04 03:48:02 +0000 | [diff] [blame] | 1238 | |
| 1239 | skip: |
Damien Miller | 7ea845e | 2010-02-12 09:21:02 +1100 | [diff] [blame] | 1240 | |
Damien Miller | b1e967c | 2014-07-03 21:22:40 +1000 | [diff] [blame] | 1241 | cleanup_pid = getpid(); |
| 1242 | |
Damien Miller | 7ea845e | 2010-02-12 09:21:02 +1100 | [diff] [blame] | 1243 | #ifdef ENABLE_PKCS11 |
| 1244 | pkcs11_init(0); |
| 1245 | #endif |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1246 | new_socket(AUTH_SOCKET, sock); |
Darren Tucker | 2812dc9 | 2007-03-21 20:45:06 +1100 | [diff] [blame] | 1247 | if (ac > 0) |
| 1248 | parent_alive_interval = 10; |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 1249 | idtab_init(); |
Damien Miller | 48bfa9c | 2001-07-14 12:12:55 +1000 | [diff] [blame] | 1250 | signal(SIGPIPE, SIG_IGN); |
djm@openbsd.org | 2ea9746 | 2015-04-24 05:26:44 +0000 | [diff] [blame] | 1251 | signal(SIGINT, (d_flag | D_flag) ? cleanup_handler : SIG_IGN); |
Ben Lindstrom | 77808ab | 2001-01-26 05:10:34 +0000 | [diff] [blame] | 1252 | signal(SIGHUP, cleanup_handler); |
| 1253 | signal(SIGTERM, cleanup_handler); |
Ben Lindstrom | a3d5a4c | 2001-07-18 15:58:08 +0000 | [diff] [blame] | 1254 | |
djm@openbsd.org | 786d599 | 2016-11-30 03:07:37 +0000 | [diff] [blame] | 1255 | if (pledge("stdio rpath cpath unix id proc exec", NULL) == -1) |
djm@openbsd.org | d952162 | 2015-12-01 23:29:24 +0000 | [diff] [blame] | 1256 | fatal("%s: pledge: %s", __progname, strerror(errno)); |
Damien Miller | 4626cba | 2016-01-08 14:24:56 +1100 | [diff] [blame] | 1257 | platform_pledge_agent(); |
djm@openbsd.org | d952162 | 2015-12-01 23:29:24 +0000 | [diff] [blame] | 1258 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1259 | while (1) { |
djm@openbsd.org | fd0e8fa | 2017-07-19 01:15:02 +0000 | [diff] [blame] | 1260 | prepare_poll(&pfd, &npfd, &timeout); |
| 1261 | result = poll(pfd, npfd, timeout); |
Darren Tucker | cf0d2db | 2007-02-28 21:19:58 +1100 | [diff] [blame] | 1262 | saved_errno = errno; |
Darren Tucker | 2812dc9 | 2007-03-21 20:45:06 +1100 | [diff] [blame] | 1263 | if (parent_alive_interval != 0) |
| 1264 | check_parent_exists(); |
| 1265 | (void) reaper(); /* remove expired keys */ |
Darren Tucker | cf0d2db | 2007-02-28 21:19:58 +1100 | [diff] [blame] | 1266 | if (result < 0) { |
| 1267 | if (saved_errno == EINTR) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1268 | continue; |
djm@openbsd.org | fd0e8fa | 2017-07-19 01:15:02 +0000 | [diff] [blame] | 1269 | fatal("poll: %s", strerror(saved_errno)); |
Darren Tucker | cf0d2db | 2007-02-28 21:19:58 +1100 | [diff] [blame] | 1270 | } else if (result > 0) |
djm@openbsd.org | fd0e8fa | 2017-07-19 01:15:02 +0000 | [diff] [blame] | 1271 | after_poll(pfd, npfd); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1272 | } |
| 1273 | /* NOTREACHED */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1274 | } |