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