djm@openbsd.org | b52ec0b | 2019-12-13 19:09:10 +0000 | [diff] [blame] | 1 | /* $OpenBSD: ssh-agent.c,v 1.251 2019/12/13 19:09:10 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 | b52ec0b | 2019-12-13 19:09:10 +0000 | [diff] [blame] | 93 | #include "sk-api.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 | |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 285 | /* ssh2 only */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 286 | static void |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 287 | process_sign_request2(SocketEntry *e) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 288 | { |
djm@openbsd.org | f4a6a88 | 2017-04-30 23:29:10 +0000 | [diff] [blame] | 289 | const u_char *data; |
| 290 | u_char *signature = NULL; |
| 291 | size_t dlen, slen = 0; |
markus@openbsd.org | 139ca81 | 2015-01-14 13:09:09 +0000 | [diff] [blame] | 292 | u_int compat = 0, flags; |
djm@openbsd.org | 05daa21 | 2019-11-16 22:36:48 +0000 | [diff] [blame] | 293 | int r, ok = -1; |
djm@openbsd.org | b52ec0b | 2019-12-13 19:09:10 +0000 | [diff] [blame] | 294 | char *fp = NULL; |
markus@openbsd.org | 139ca81 | 2015-01-14 13:09:09 +0000 | [diff] [blame] | 295 | struct sshbuf *msg; |
djm@openbsd.org | f4a6a88 | 2017-04-30 23:29:10 +0000 | [diff] [blame] | 296 | struct sshkey *key = NULL; |
djm@openbsd.org | 0088c57 | 2015-01-14 19:33:41 +0000 | [diff] [blame] | 297 | struct identity *id; |
djm@openbsd.org | b52ec0b | 2019-12-13 19:09:10 +0000 | [diff] [blame] | 298 | struct notifier_ctx *notifier = NULL; |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 299 | |
djm@openbsd.org | 0088c57 | 2015-01-14 19:33:41 +0000 | [diff] [blame] | 300 | if ((msg = sshbuf_new()) == NULL) |
| 301 | fatal("%s: sshbuf_new failed", __func__); |
djm@openbsd.org | f4a6a88 | 2017-04-30 23:29:10 +0000 | [diff] [blame] | 302 | if ((r = sshkey_froms(e->request, &key)) != 0 || |
| 303 | (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] | 304 | (r = sshbuf_get_u32(e->request, &flags)) != 0) { |
| 305 | error("%s: couldn't parse request: %s", __func__, ssh_err(r)); |
| 306 | goto send; |
| 307 | } |
| 308 | |
djm@openbsd.org | f4a6a88 | 2017-04-30 23:29:10 +0000 | [diff] [blame] | 309 | if ((id = lookup_identity(key)) == NULL) { |
djm@openbsd.org | 0088c57 | 2015-01-14 19:33:41 +0000 | [diff] [blame] | 310 | verbose("%s: %s key not found", __func__, sshkey_type(key)); |
| 311 | goto send; |
| 312 | } |
| 313 | if (id->confirm && confirm_key(id) != 0) { |
| 314 | verbose("%s: user refused key", __func__); |
| 315 | goto send; |
| 316 | } |
djm@openbsd.org | b52ec0b | 2019-12-13 19:09:10 +0000 | [diff] [blame] | 317 | if (sshkey_is_sk(id->key) && |
| 318 | (id->key->sk_flags & SSH_SK_USER_PRESENCE_REQD)) { |
| 319 | if ((fp = sshkey_fingerprint(key, SSH_FP_HASH_DEFAULT, |
| 320 | SSH_FP_DEFAULT)) == NULL) |
| 321 | fatal("%s: fingerprint failed", __func__); |
| 322 | notifier = notify_start(0, |
| 323 | "Confirm user presence for key %s %s", |
| 324 | sshkey_type(id->key), fp); |
| 325 | } |
| 326 | if ((r = sshkey_sign(id->key, &signature, &slen, |
| 327 | data, dlen, agent_decode_alg(key, flags), |
| 328 | id->sk_provider, compat)) != 0) { |
| 329 | error("%s: sshkey_sign: %s", __func__, ssh_err(r)); |
| 330 | goto send; |
djm@openbsd.org | 0088c57 | 2015-01-14 19:33:41 +0000 | [diff] [blame] | 331 | } |
| 332 | /* Success */ |
| 333 | ok = 0; |
| 334 | send: |
djm@openbsd.org | b52ec0b | 2019-12-13 19:09:10 +0000 | [diff] [blame] | 335 | notify_complete(notifier); |
djm@openbsd.org | 0088c57 | 2015-01-14 19:33:41 +0000 | [diff] [blame] | 336 | sshkey_free(key); |
djm@openbsd.org | b52ec0b | 2019-12-13 19:09:10 +0000 | [diff] [blame] | 337 | free(fp); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 338 | if (ok == 0) { |
markus@openbsd.org | 139ca81 | 2015-01-14 13:09:09 +0000 | [diff] [blame] | 339 | if ((r = sshbuf_put_u8(msg, SSH2_AGENT_SIGN_RESPONSE)) != 0 || |
| 340 | (r = sshbuf_put_string(msg, signature, slen)) != 0) |
| 341 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
| 342 | } else if ((r = sshbuf_put_u8(msg, SSH_AGENT_FAILURE)) != 0) |
| 343 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
| 344 | |
| 345 | if ((r = sshbuf_put_stringb(e->output, msg)) != 0) |
| 346 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
| 347 | |
| 348 | sshbuf_free(msg); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 349 | free(signature); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | /* shared */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 353 | static void |
djm@openbsd.org | f4a6a88 | 2017-04-30 23:29:10 +0000 | [diff] [blame] | 354 | process_remove_identity(SocketEntry *e) |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 355 | { |
markus@openbsd.org | 139ca81 | 2015-01-14 13:09:09 +0000 | [diff] [blame] | 356 | int r, success = 0; |
| 357 | struct sshkey *key = NULL; |
djm@openbsd.org | f4a6a88 | 2017-04-30 23:29:10 +0000 | [diff] [blame] | 358 | Identity *id; |
Damien Miller | 7e8e820 | 1999-11-16 13:37:16 +1100 | [diff] [blame] | 359 | |
djm@openbsd.org | f4a6a88 | 2017-04-30 23:29:10 +0000 | [diff] [blame] | 360 | if ((r = sshkey_froms(e->request, &key)) != 0) { |
| 361 | error("%s: get key: %s", __func__, ssh_err(r)); |
| 362 | goto done; |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 363 | } |
djm@openbsd.org | f4a6a88 | 2017-04-30 23:29:10 +0000 | [diff] [blame] | 364 | if ((id = lookup_identity(key)) == NULL) { |
| 365 | debug("%s: key not found", __func__); |
| 366 | goto done; |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 367 | } |
djm@openbsd.org | f4a6a88 | 2017-04-30 23:29:10 +0000 | [diff] [blame] | 368 | /* We have this key, free it. */ |
| 369 | if (idtab->nentries < 1) |
| 370 | fatal("%s: internal error: nentries %d", |
| 371 | __func__, idtab->nentries); |
| 372 | TAILQ_REMOVE(&idtab->idlist, id, next); |
| 373 | free_identity(id); |
| 374 | idtab->nentries--; |
| 375 | sshkey_free(key); |
| 376 | success = 1; |
| 377 | done: |
markus@openbsd.org | 139ca81 | 2015-01-14 13:09:09 +0000 | [diff] [blame] | 378 | send_status(e, success); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 379 | } |
| 380 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 381 | static void |
djm@openbsd.org | f4a6a88 | 2017-04-30 23:29:10 +0000 | [diff] [blame] | 382 | process_remove_all_identities(SocketEntry *e) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 383 | { |
Damien Miller | 1a534ae | 2002-01-22 23:26:13 +1100 | [diff] [blame] | 384 | Identity *id; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 385 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 386 | /* Loop over all identities and clear the keys. */ |
djm@openbsd.org | f4a6a88 | 2017-04-30 23:29:10 +0000 | [diff] [blame] | 387 | for (id = TAILQ_FIRST(&idtab->idlist); id; |
| 388 | id = TAILQ_FIRST(&idtab->idlist)) { |
| 389 | TAILQ_REMOVE(&idtab->idlist, id, next); |
Damien Miller | 1a534ae | 2002-01-22 23:26:13 +1100 | [diff] [blame] | 390 | free_identity(id); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 391 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 392 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 393 | /* Mark that there are no identities. */ |
djm@openbsd.org | f4a6a88 | 2017-04-30 23:29:10 +0000 | [diff] [blame] | 394 | idtab->nentries = 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 395 | |
| 396 | /* Send success. */ |
markus@openbsd.org | 139ca81 | 2015-01-14 13:09:09 +0000 | [diff] [blame] | 397 | send_status(e, 1); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 398 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 399 | |
Darren Tucker | 2812dc9 | 2007-03-21 20:45:06 +1100 | [diff] [blame] | 400 | /* removes expired keys and returns number of seconds until the next expiry */ |
Darren Tucker | 5511925 | 2013-06-02 07:43:59 +1000 | [diff] [blame] | 401 | static time_t |
Ben Lindstrom | 61d328a | 2002-06-06 21:54:57 +0000 | [diff] [blame] | 402 | reaper(void) |
| 403 | { |
Darren Tucker | b759c9c | 2013-06-02 07:46:16 +1000 | [diff] [blame] | 404 | time_t deadline = 0, now = monotime(); |
Ben Lindstrom | 61d328a | 2002-06-06 21:54:57 +0000 | [diff] [blame] | 405 | Identity *id, *nxt; |
Ben Lindstrom | 61d328a | 2002-06-06 21:54:57 +0000 | [diff] [blame] | 406 | |
djm@openbsd.org | f4a6a88 | 2017-04-30 23:29:10 +0000 | [diff] [blame] | 407 | for (id = TAILQ_FIRST(&idtab->idlist); id; id = nxt) { |
| 408 | nxt = TAILQ_NEXT(id, next); |
| 409 | if (id->death == 0) |
| 410 | continue; |
| 411 | if (now >= id->death) { |
| 412 | debug("expiring key '%s'", id->comment); |
| 413 | TAILQ_REMOVE(&idtab->idlist, id, next); |
| 414 | free_identity(id); |
| 415 | idtab->nentries--; |
| 416 | } else |
| 417 | deadline = (deadline == 0) ? id->death : |
| 418 | MINIMUM(deadline, id->death); |
Ben Lindstrom | 61d328a | 2002-06-06 21:54:57 +0000 | [diff] [blame] | 419 | } |
Darren Tucker | 2812dc9 | 2007-03-21 20:45:06 +1100 | [diff] [blame] | 420 | if (deadline == 0 || deadline <= now) |
| 421 | return 0; |
| 422 | else |
| 423 | return (deadline - now); |
Ben Lindstrom | 61d328a | 2002-06-06 21:54:57 +0000 | [diff] [blame] | 424 | } |
| 425 | |
| 426 | static void |
djm@openbsd.org | f4a6a88 | 2017-04-30 23:29:10 +0000 | [diff] [blame] | 427 | process_add_identity(SocketEntry *e) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 428 | { |
Damien Miller | 4c7728c | 2007-10-26 14:25:31 +1000 | [diff] [blame] | 429 | Identity *id; |
markus@openbsd.org | 139ca81 | 2015-01-14 13:09:09 +0000 | [diff] [blame] | 430 | int success = 0, confirm = 0; |
markus@openbsd.org | 1b11ea7 | 2018-02-23 15:58:37 +0000 | [diff] [blame] | 431 | u_int seconds, maxsign; |
djm@openbsd.org | 07da39f | 2019-10-31 21:22:01 +0000 | [diff] [blame] | 432 | char *fp, *comment = NULL, *ext_name = NULL, *sk_provider = NULL; |
Darren Tucker | 5511925 | 2013-06-02 07:43:59 +1000 | [diff] [blame] | 433 | time_t death = 0; |
markus@openbsd.org | 139ca81 | 2015-01-14 13:09:09 +0000 | [diff] [blame] | 434 | struct sshkey *k = NULL; |
| 435 | u_char ctype; |
| 436 | int r = SSH_ERR_INTERNAL_ERROR; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 437 | |
djm@openbsd.org | f4a6a88 | 2017-04-30 23:29:10 +0000 | [diff] [blame] | 438 | if ((r = sshkey_private_deserialize(e->request, &k)) != 0 || |
| 439 | k == NULL || |
markus@openbsd.org | 139ca81 | 2015-01-14 13:09:09 +0000 | [diff] [blame] | 440 | (r = sshbuf_get_cstring(e->request, &comment, NULL)) != 0) { |
| 441 | error("%s: decode private key: %s", __func__, ssh_err(r)); |
| 442 | goto err; |
| 443 | } |
markus@openbsd.org | 139ca81 | 2015-01-14 13:09:09 +0000 | [diff] [blame] | 444 | while (sshbuf_len(e->request)) { |
| 445 | if ((r = sshbuf_get_u8(e->request, &ctype)) != 0) { |
| 446 | error("%s: buffer error: %s", __func__, ssh_err(r)); |
| 447 | goto err; |
| 448 | } |
| 449 | switch (ctype) { |
Ben Lindstrom | c90f8a9 | 2002-06-21 00:06:54 +0000 | [diff] [blame] | 450 | case SSH_AGENT_CONSTRAIN_LIFETIME: |
markus@openbsd.org | 139ca81 | 2015-01-14 13:09:09 +0000 | [diff] [blame] | 451 | if ((r = sshbuf_get_u32(e->request, &seconds)) != 0) { |
| 452 | error("%s: bad lifetime constraint: %s", |
| 453 | __func__, ssh_err(r)); |
| 454 | goto err; |
| 455 | } |
| 456 | death = monotime() + seconds; |
Ben Lindstrom | 4eb4c4e | 2002-06-21 00:04:48 +0000 | [diff] [blame] | 457 | break; |
Damien Miller | 6c71179 | 2003-01-24 11:36:23 +1100 | [diff] [blame] | 458 | case SSH_AGENT_CONSTRAIN_CONFIRM: |
| 459 | confirm = 1; |
| 460 | break; |
markus@openbsd.org | 1b11ea7 | 2018-02-23 15:58:37 +0000 | [diff] [blame] | 461 | case SSH_AGENT_CONSTRAIN_MAXSIGN: |
| 462 | if ((r = sshbuf_get_u32(e->request, &maxsign)) != 0) { |
| 463 | error("%s: bad maxsign constraint: %s", |
| 464 | __func__, ssh_err(r)); |
| 465 | goto err; |
| 466 | } |
| 467 | if ((r = sshkey_enable_maxsign(k, maxsign)) != 0) { |
| 468 | error("%s: cannot enable maxsign: %s", |
| 469 | __func__, ssh_err(r)); |
| 470 | goto err; |
| 471 | } |
| 472 | break; |
djm@openbsd.org | 07da39f | 2019-10-31 21:22:01 +0000 | [diff] [blame] | 473 | case SSH_AGENT_CONSTRAIN_EXTENSION: |
| 474 | if ((r = sshbuf_get_cstring(e->request, |
| 475 | &ext_name, NULL)) != 0) { |
| 476 | error("%s: cannot parse extension: %s", |
| 477 | __func__, ssh_err(r)); |
| 478 | goto err; |
| 479 | } |
| 480 | debug("%s: constraint ext %s", __func__, ext_name); |
| 481 | if (strcmp(ext_name, "sk-provider@openssh.com") == 0) { |
| 482 | if (sk_provider != NULL) { |
| 483 | error("%s already set", ext_name); |
| 484 | goto err; |
| 485 | } |
| 486 | if ((r = sshbuf_get_cstring(e->request, |
| 487 | &sk_provider, NULL)) != 0) { |
| 488 | error("%s: cannot parse %s: %s", |
| 489 | __func__, ext_name, ssh_err(r)); |
| 490 | goto err; |
| 491 | } |
| 492 | } else { |
| 493 | error("%s: unsupported constraint \"%s\"", |
| 494 | __func__, ext_name); |
| 495 | goto err; |
| 496 | } |
| 497 | free(ext_name); |
| 498 | break; |
Ben Lindstrom | 4eb4c4e | 2002-06-21 00:04:48 +0000 | [diff] [blame] | 499 | default: |
markus@openbsd.org | 139ca81 | 2015-01-14 13:09:09 +0000 | [diff] [blame] | 500 | error("%s: Unknown constraint %d", __func__, ctype); |
| 501 | err: |
djm@openbsd.org | 07da39f | 2019-10-31 21:22:01 +0000 | [diff] [blame] | 502 | free(sk_provider); |
| 503 | free(ext_name); |
markus@openbsd.org | 139ca81 | 2015-01-14 13:09:09 +0000 | [diff] [blame] | 504 | sshbuf_reset(e->request); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 505 | free(comment); |
markus@openbsd.org | 139ca81 | 2015-01-14 13:09:09 +0000 | [diff] [blame] | 506 | sshkey_free(k); |
Damien Miller | 1cfadab | 2008-06-30 00:05:21 +1000 | [diff] [blame] | 507 | goto send; |
Ben Lindstrom | 4eb4c4e | 2002-06-21 00:04:48 +0000 | [diff] [blame] | 508 | } |
| 509 | } |
djm@openbsd.org | 07da39f | 2019-10-31 21:22:01 +0000 | [diff] [blame] | 510 | if (sk_provider != NULL) { |
markus@openbsd.org | 2c55744 | 2019-11-12 19:33:08 +0000 | [diff] [blame] | 511 | if (!sshkey_is_sk(k)) { |
djm@openbsd.org | 07da39f | 2019-10-31 21:22:01 +0000 | [diff] [blame] | 512 | error("Cannot add provider: %s is not a security key", |
| 513 | sshkey_type(k)); |
| 514 | free(sk_provider); |
| 515 | goto send; |
| 516 | } |
djm@openbsd.org | ab36006 | 2019-11-15 04:12:32 +0000 | [diff] [blame] | 517 | if (strcasecmp(sk_provider, "internal") == 0) |
| 518 | debug("%s: internal provider", __func__); |
| 519 | else if (match_pattern_list(sk_provider, |
djm@openbsd.org | 07da39f | 2019-10-31 21:22:01 +0000 | [diff] [blame] | 520 | provider_whitelist, 0) != 1) { |
| 521 | error("Refusing add key: provider %s not whitelisted", |
| 522 | sk_provider); |
| 523 | free(sk_provider); |
| 524 | goto send; |
| 525 | } |
| 526 | } |
markus@openbsd.org | bf21992 | 2019-11-13 07:53:10 +0000 | [diff] [blame] | 527 | if ((r = sshkey_shield_private(k)) != 0) { |
| 528 | error("%s: shield private key: %s", __func__, ssh_err(r)); |
| 529 | goto err; |
| 530 | } |
markus@openbsd.org | 139ca81 | 2015-01-14 13:09:09 +0000 | [diff] [blame] | 531 | |
Damien Miller | 1cfadab | 2008-06-30 00:05:21 +1000 | [diff] [blame] | 532 | success = 1; |
Damien Miller | 53d8148 | 2003-01-22 11:47:19 +1100 | [diff] [blame] | 533 | if (lifetime && !death) |
Darren Tucker | b759c9c | 2013-06-02 07:46:16 +1000 | [diff] [blame] | 534 | death = monotime() + lifetime; |
djm@openbsd.org | f4a6a88 | 2017-04-30 23:29:10 +0000 | [diff] [blame] | 535 | if ((id = lookup_identity(k)) == NULL) { |
Damien Miller | 7ea845e | 2010-02-12 09:21:02 +1100 | [diff] [blame] | 536 | id = xcalloc(1, sizeof(Identity)); |
djm@openbsd.org | f4a6a88 | 2017-04-30 23:29:10 +0000 | [diff] [blame] | 537 | TAILQ_INSERT_TAIL(&idtab->idlist, id, next); |
Ben Lindstrom | 2b266b7 | 2002-06-21 00:08:39 +0000 | [diff] [blame] | 538 | /* Increment the number of identities. */ |
djm@openbsd.org | f4a6a88 | 2017-04-30 23:29:10 +0000 | [diff] [blame] | 539 | idtab->nentries++; |
Ben Lindstrom | 2b266b7 | 2002-06-21 00:08:39 +0000 | [diff] [blame] | 540 | } else { |
markus@openbsd.org | 1b11ea7 | 2018-02-23 15:58:37 +0000 | [diff] [blame] | 541 | /* key state might have been updated */ |
| 542 | sshkey_free(id->key); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 543 | free(id->comment); |
djm@openbsd.org | 07da39f | 2019-10-31 21:22:01 +0000 | [diff] [blame] | 544 | free(id->sk_provider); |
Ben Lindstrom | 61d328a | 2002-06-06 21:54:57 +0000 | [diff] [blame] | 545 | } |
markus@openbsd.org | 1b11ea7 | 2018-02-23 15:58:37 +0000 | [diff] [blame] | 546 | id->key = k; |
Damien Miller | 4c7728c | 2007-10-26 14:25:31 +1000 | [diff] [blame] | 547 | id->comment = comment; |
| 548 | id->death = death; |
| 549 | id->confirm = confirm; |
djm@openbsd.org | 07da39f | 2019-10-31 21:22:01 +0000 | [diff] [blame] | 550 | id->sk_provider = sk_provider; |
| 551 | |
| 552 | if ((fp = sshkey_fingerprint(k, SSH_FP_HASH_DEFAULT, |
| 553 | SSH_FP_DEFAULT)) == NULL) |
| 554 | fatal("%s: sshkey_fingerprint failed", __func__); |
| 555 | debug("%s: add %s %s \"%.100s\" (life: %u) (confirm: %u) " |
| 556 | "(provider: %s)", __func__, sshkey_ssh_name(k), fp, comment, |
| 557 | seconds, confirm, sk_provider == NULL ? "none" : sk_provider); |
| 558 | free(fp); |
Ben Lindstrom | 2b266b7 | 2002-06-21 00:08:39 +0000 | [diff] [blame] | 559 | send: |
markus@openbsd.org | 139ca81 | 2015-01-14 13:09:09 +0000 | [diff] [blame] | 560 | send_status(e, success); |
Ben Lindstrom | 61d328a | 2002-06-06 21:54:57 +0000 | [diff] [blame] | 561 | } |
| 562 | |
Ben Lindstrom | 2f71704 | 2002-06-06 21:52:03 +0000 | [diff] [blame] | 563 | /* XXX todo: encrypt sensitive data with passphrase */ |
| 564 | static void |
| 565 | process_lock_agent(SocketEntry *e, int lock) |
| 566 | { |
dtucker@openbsd.org | 9173d0f | 2015-05-15 05:44:21 +0000 | [diff] [blame] | 567 | int r, success = 0, delay; |
djm@openbsd.org | 1a31d02 | 2016-05-02 08:49:03 +0000 | [diff] [blame] | 568 | char *passwd; |
| 569 | u_char passwdhash[LOCK_SIZE]; |
dtucker@openbsd.org | 9173d0f | 2015-05-15 05:44:21 +0000 | [diff] [blame] | 570 | static u_int fail_count = 0; |
| 571 | size_t pwlen; |
Ben Lindstrom | 2f71704 | 2002-06-06 21:52:03 +0000 | [diff] [blame] | 572 | |
djm@openbsd.org@openbsd.org | 83a1e5d | 2017-11-15 02:10:16 +0000 | [diff] [blame] | 573 | /* |
| 574 | * This is deliberately fatal: the user has requested that we lock, |
| 575 | * but we can't parse their request properly. The only safe thing to |
| 576 | * do is abort. |
| 577 | */ |
dtucker@openbsd.org | 9173d0f | 2015-05-15 05:44:21 +0000 | [diff] [blame] | 578 | if ((r = sshbuf_get_cstring(e->request, &passwd, &pwlen)) != 0) |
markus@openbsd.org | 139ca81 | 2015-01-14 13:09:09 +0000 | [diff] [blame] | 579 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
dtucker@openbsd.org | 9173d0f | 2015-05-15 05:44:21 +0000 | [diff] [blame] | 580 | if (pwlen == 0) { |
| 581 | debug("empty password not supported"); |
| 582 | } else if (locked && !lock) { |
| 583 | if (bcrypt_pbkdf(passwd, pwlen, lock_salt, sizeof(lock_salt), |
| 584 | passwdhash, sizeof(passwdhash), LOCK_ROUNDS) < 0) |
| 585 | fatal("bcrypt_pbkdf"); |
djm@openbsd.org | 1a31d02 | 2016-05-02 08:49:03 +0000 | [diff] [blame] | 586 | if (timingsafe_bcmp(passwdhash, lock_pwhash, LOCK_SIZE) == 0) { |
dtucker@openbsd.org | 9173d0f | 2015-05-15 05:44:21 +0000 | [diff] [blame] | 587 | debug("agent unlocked"); |
| 588 | locked = 0; |
| 589 | fail_count = 0; |
djm@openbsd.org | 1a31d02 | 2016-05-02 08:49:03 +0000 | [diff] [blame] | 590 | explicit_bzero(lock_pwhash, sizeof(lock_pwhash)); |
dtucker@openbsd.org | 9173d0f | 2015-05-15 05:44:21 +0000 | [diff] [blame] | 591 | success = 1; |
| 592 | } else { |
| 593 | /* delay in 0.1s increments up to 10s */ |
| 594 | if (fail_count < 100) |
| 595 | fail_count++; |
| 596 | delay = 100000 * fail_count; |
| 597 | debug("unlock failed, delaying %0.1lf seconds", |
| 598 | (double)delay/1000000); |
| 599 | usleep(delay); |
| 600 | } |
| 601 | explicit_bzero(passwdhash, sizeof(passwdhash)); |
Ben Lindstrom | 2f71704 | 2002-06-06 21:52:03 +0000 | [diff] [blame] | 602 | } else if (!locked && lock) { |
dtucker@openbsd.org | 9173d0f | 2015-05-15 05:44:21 +0000 | [diff] [blame] | 603 | debug("agent locked"); |
Ben Lindstrom | 2f71704 | 2002-06-06 21:52:03 +0000 | [diff] [blame] | 604 | locked = 1; |
dtucker@openbsd.org | 9173d0f | 2015-05-15 05:44:21 +0000 | [diff] [blame] | 605 | arc4random_buf(lock_salt, sizeof(lock_salt)); |
| 606 | if (bcrypt_pbkdf(passwd, pwlen, lock_salt, sizeof(lock_salt), |
djm@openbsd.org | 1a31d02 | 2016-05-02 08:49:03 +0000 | [diff] [blame] | 607 | lock_pwhash, sizeof(lock_pwhash), LOCK_ROUNDS) < 0) |
dtucker@openbsd.org | 9173d0f | 2015-05-15 05:44:21 +0000 | [diff] [blame] | 608 | fatal("bcrypt_pbkdf"); |
Ben Lindstrom | 2f71704 | 2002-06-06 21:52:03 +0000 | [diff] [blame] | 609 | success = 1; |
| 610 | } |
dtucker@openbsd.org | 9173d0f | 2015-05-15 05:44:21 +0000 | [diff] [blame] | 611 | explicit_bzero(passwd, pwlen); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 612 | free(passwd); |
markus@openbsd.org | 139ca81 | 2015-01-14 13:09:09 +0000 | [diff] [blame] | 613 | send_status(e, success); |
Ben Lindstrom | 2f71704 | 2002-06-06 21:52:03 +0000 | [diff] [blame] | 614 | } |
| 615 | |
| 616 | static void |
djm@openbsd.org | f4a6a88 | 2017-04-30 23:29:10 +0000 | [diff] [blame] | 617 | no_identities(SocketEntry *e) |
Ben Lindstrom | 2f71704 | 2002-06-06 21:52:03 +0000 | [diff] [blame] | 618 | { |
markus@openbsd.org | 139ca81 | 2015-01-14 13:09:09 +0000 | [diff] [blame] | 619 | struct sshbuf *msg; |
| 620 | int r; |
Ben Lindstrom | 2f71704 | 2002-06-06 21:52:03 +0000 | [diff] [blame] | 621 | |
markus@openbsd.org | 139ca81 | 2015-01-14 13:09:09 +0000 | [diff] [blame] | 622 | if ((msg = sshbuf_new()) == NULL) |
| 623 | fatal("%s: sshbuf_new failed", __func__); |
djm@openbsd.org | f4a6a88 | 2017-04-30 23:29:10 +0000 | [diff] [blame] | 624 | if ((r = sshbuf_put_u8(msg, SSH2_AGENT_IDENTITIES_ANSWER)) != 0 || |
markus@openbsd.org | 139ca81 | 2015-01-14 13:09:09 +0000 | [diff] [blame] | 625 | (r = sshbuf_put_u32(msg, 0)) != 0 || |
| 626 | (r = sshbuf_put_stringb(e->output, msg)) != 0) |
| 627 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
| 628 | sshbuf_free(msg); |
Ben Lindstrom | 2f71704 | 2002-06-06 21:52:03 +0000 | [diff] [blame] | 629 | } |
Ben Lindstrom | 3f47163 | 2001-07-04 03:53:15 +0000 | [diff] [blame] | 630 | |
Damien Miller | 7ea845e | 2010-02-12 09:21:02 +1100 | [diff] [blame] | 631 | #ifdef ENABLE_PKCS11 |
Ben Lindstrom | 3f47163 | 2001-07-04 03:53:15 +0000 | [diff] [blame] | 632 | static void |
Damien Miller | 1cfadab | 2008-06-30 00:05:21 +1000 | [diff] [blame] | 633 | process_add_smartcard_key(SocketEntry *e) |
Ben Lindstrom | 3f47163 | 2001-07-04 03:53:15 +0000 | [diff] [blame] | 634 | { |
djm@openbsd.org@openbsd.org | 83a1e5d | 2017-11-15 02:10:16 +0000 | [diff] [blame] | 635 | char *provider = NULL, *pin = NULL, canonical_provider[PATH_MAX]; |
djm@openbsd.org | f4a6a88 | 2017-04-30 23:29:10 +0000 | [diff] [blame] | 636 | int r, i, count = 0, success = 0, confirm = 0; |
markus@openbsd.org | 139ca81 | 2015-01-14 13:09:09 +0000 | [diff] [blame] | 637 | u_int seconds; |
Darren Tucker | 5511925 | 2013-06-02 07:43:59 +1000 | [diff] [blame] | 638 | time_t death = 0; |
markus@openbsd.org | 139ca81 | 2015-01-14 13:09:09 +0000 | [diff] [blame] | 639 | u_char type; |
| 640 | struct sshkey **keys = NULL, *k; |
Ben Lindstrom | 822b634 | 2002-06-23 21:38:49 +0000 | [diff] [blame] | 641 | Identity *id; |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 642 | |
markus@openbsd.org | 139ca81 | 2015-01-14 13:09:09 +0000 | [diff] [blame] | 643 | 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] | 644 | (r = sshbuf_get_cstring(e->request, &pin, NULL)) != 0) { |
| 645 | error("%s: buffer error: %s", __func__, ssh_err(r)); |
| 646 | goto send; |
| 647 | } |
Damien Miller | d94f20d | 2003-06-11 22:06:33 +1000 | [diff] [blame] | 648 | |
markus@openbsd.org | 139ca81 | 2015-01-14 13:09:09 +0000 | [diff] [blame] | 649 | while (sshbuf_len(e->request)) { |
djm@openbsd.org@openbsd.org | 83a1e5d | 2017-11-15 02:10:16 +0000 | [diff] [blame] | 650 | if ((r = sshbuf_get_u8(e->request, &type)) != 0) { |
| 651 | error("%s: buffer error: %s", __func__, ssh_err(r)); |
| 652 | goto send; |
| 653 | } |
markus@openbsd.org | 139ca81 | 2015-01-14 13:09:09 +0000 | [diff] [blame] | 654 | switch (type) { |
Damien Miller | d94f20d | 2003-06-11 22:06:33 +1000 | [diff] [blame] | 655 | case SSH_AGENT_CONSTRAIN_LIFETIME: |
djm@openbsd.org@openbsd.org | 83a1e5d | 2017-11-15 02:10:16 +0000 | [diff] [blame] | 656 | if ((r = sshbuf_get_u32(e->request, &seconds)) != 0) { |
| 657 | error("%s: buffer error: %s", |
markus@openbsd.org | 139ca81 | 2015-01-14 13:09:09 +0000 | [diff] [blame] | 658 | __func__, ssh_err(r)); |
djm@openbsd.org@openbsd.org | 83a1e5d | 2017-11-15 02:10:16 +0000 | [diff] [blame] | 659 | goto send; |
| 660 | } |
markus@openbsd.org | 139ca81 | 2015-01-14 13:09:09 +0000 | [diff] [blame] | 661 | death = monotime() + seconds; |
Damien Miller | d94f20d | 2003-06-11 22:06:33 +1000 | [diff] [blame] | 662 | break; |
| 663 | case SSH_AGENT_CONSTRAIN_CONFIRM: |
| 664 | confirm = 1; |
| 665 | break; |
| 666 | default: |
djm@openbsd.org | f4a6a88 | 2017-04-30 23:29:10 +0000 | [diff] [blame] | 667 | error("%s: Unknown constraint type %d", __func__, type); |
Damien Miller | 1cfadab | 2008-06-30 00:05:21 +1000 | [diff] [blame] | 668 | goto send; |
Damien Miller | d94f20d | 2003-06-11 22:06:33 +1000 | [diff] [blame] | 669 | } |
| 670 | } |
djm@openbsd.org | 786d599 | 2016-11-30 03:07:37 +0000 | [diff] [blame] | 671 | if (realpath(provider, canonical_provider) == NULL) { |
| 672 | verbose("failed PKCS#11 add of \"%.100s\": realpath: %s", |
| 673 | provider, strerror(errno)); |
| 674 | goto send; |
| 675 | } |
djm@openbsd.org | 07da39f | 2019-10-31 21:22:01 +0000 | [diff] [blame] | 676 | if (match_pattern_list(canonical_provider, provider_whitelist, 0) != 1) { |
djm@openbsd.org | 786d599 | 2016-11-30 03:07:37 +0000 | [diff] [blame] | 677 | verbose("refusing PKCS#11 add of \"%.100s\": " |
| 678 | "provider not whitelisted", canonical_provider); |
| 679 | goto send; |
| 680 | } |
| 681 | debug("%s: add %.100s", __func__, canonical_provider); |
Damien Miller | d94f20d | 2003-06-11 22:06:33 +1000 | [diff] [blame] | 682 | if (lifetime && !death) |
Darren Tucker | b759c9c | 2013-06-02 07:46:16 +1000 | [diff] [blame] | 683 | death = monotime() + lifetime; |
Damien Miller | d94f20d | 2003-06-11 22:06:33 +1000 | [diff] [blame] | 684 | |
djm@openbsd.org | 786d599 | 2016-11-30 03:07:37 +0000 | [diff] [blame] | 685 | count = pkcs11_add_provider(canonical_provider, pin, &keys); |
Damien Miller | 7ea845e | 2010-02-12 09:21:02 +1100 | [diff] [blame] | 686 | for (i = 0; i < count; i++) { |
Ben Lindstrom | 0936a5b | 2002-03-26 03:17:42 +0000 | [diff] [blame] | 687 | k = keys[i]; |
djm@openbsd.org | f4a6a88 | 2017-04-30 23:29:10 +0000 | [diff] [blame] | 688 | if (lookup_identity(k) == NULL) { |
Damien Miller | 7ea845e | 2010-02-12 09:21:02 +1100 | [diff] [blame] | 689 | id = xcalloc(1, sizeof(Identity)); |
Ben Lindstrom | 0936a5b | 2002-03-26 03:17:42 +0000 | [diff] [blame] | 690 | id->key = k; |
djm@openbsd.org | 786d599 | 2016-11-30 03:07:37 +0000 | [diff] [blame] | 691 | id->provider = xstrdup(canonical_provider); |
| 692 | id->comment = xstrdup(canonical_provider); /* XXX */ |
Damien Miller | d94f20d | 2003-06-11 22:06:33 +1000 | [diff] [blame] | 693 | id->death = death; |
| 694 | id->confirm = confirm; |
djm@openbsd.org | f4a6a88 | 2017-04-30 23:29:10 +0000 | [diff] [blame] | 695 | TAILQ_INSERT_TAIL(&idtab->idlist, id, next); |
| 696 | idtab->nentries++; |
Ben Lindstrom | 0936a5b | 2002-03-26 03:17:42 +0000 | [diff] [blame] | 697 | success = 1; |
| 698 | } else { |
markus@openbsd.org | 139ca81 | 2015-01-14 13:09:09 +0000 | [diff] [blame] | 699 | sshkey_free(k); |
Ben Lindstrom | 0936a5b | 2002-03-26 03:17:42 +0000 | [diff] [blame] | 700 | } |
| 701 | keys[i] = NULL; |
Ben Lindstrom | 3f47163 | 2001-07-04 03:53:15 +0000 | [diff] [blame] | 702 | } |
Ben Lindstrom | 3f47163 | 2001-07-04 03:53:15 +0000 | [diff] [blame] | 703 | send: |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 704 | free(pin); |
| 705 | free(provider); |
| 706 | free(keys); |
markus@openbsd.org | 139ca81 | 2015-01-14 13:09:09 +0000 | [diff] [blame] | 707 | send_status(e, success); |
Ben Lindstrom | 3f47163 | 2001-07-04 03:53:15 +0000 | [diff] [blame] | 708 | } |
| 709 | |
| 710 | static void |
| 711 | process_remove_smartcard_key(SocketEntry *e) |
| 712 | { |
djm@openbsd.org | 25f8376 | 2017-03-15 02:25:09 +0000 | [diff] [blame] | 713 | char *provider = NULL, *pin = NULL, canonical_provider[PATH_MAX]; |
djm@openbsd.org | f4a6a88 | 2017-04-30 23:29:10 +0000 | [diff] [blame] | 714 | int r, success = 0; |
Damien Miller | 7ea845e | 2010-02-12 09:21:02 +1100 | [diff] [blame] | 715 | Identity *id, *nxt; |
Ben Lindstrom | 3f47163 | 2001-07-04 03:53:15 +0000 | [diff] [blame] | 716 | |
markus@openbsd.org | 139ca81 | 2015-01-14 13:09:09 +0000 | [diff] [blame] | 717 | 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] | 718 | (r = sshbuf_get_cstring(e->request, &pin, NULL)) != 0) { |
| 719 | error("%s: buffer error: %s", __func__, ssh_err(r)); |
| 720 | goto send; |
| 721 | } |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 722 | free(pin); |
Ben Lindstrom | 3f47163 | 2001-07-04 03:53:15 +0000 | [diff] [blame] | 723 | |
djm@openbsd.org | 25f8376 | 2017-03-15 02:25:09 +0000 | [diff] [blame] | 724 | if (realpath(provider, canonical_provider) == NULL) { |
| 725 | verbose("failed PKCS#11 add of \"%.100s\": realpath: %s", |
| 726 | provider, strerror(errno)); |
| 727 | goto send; |
| 728 | } |
| 729 | |
| 730 | debug("%s: remove %.100s", __func__, canonical_provider); |
djm@openbsd.org | f4a6a88 | 2017-04-30 23:29:10 +0000 | [diff] [blame] | 731 | for (id = TAILQ_FIRST(&idtab->idlist); id; id = nxt) { |
| 732 | nxt = TAILQ_NEXT(id, next); |
| 733 | /* Skip file--based keys */ |
| 734 | if (id->provider == NULL) |
| 735 | continue; |
| 736 | if (!strcmp(canonical_provider, id->provider)) { |
| 737 | TAILQ_REMOVE(&idtab->idlist, id, next); |
| 738 | free_identity(id); |
| 739 | idtab->nentries--; |
Ben Lindstrom | 3f47163 | 2001-07-04 03:53:15 +0000 | [diff] [blame] | 740 | } |
Ben Lindstrom | 3f47163 | 2001-07-04 03:53:15 +0000 | [diff] [blame] | 741 | } |
djm@openbsd.org | 25f8376 | 2017-03-15 02:25:09 +0000 | [diff] [blame] | 742 | if (pkcs11_del_provider(canonical_provider) == 0) |
Damien Miller | 7ea845e | 2010-02-12 09:21:02 +1100 | [diff] [blame] | 743 | success = 1; |
| 744 | else |
djm@openbsd.org | f4a6a88 | 2017-04-30 23:29:10 +0000 | [diff] [blame] | 745 | error("%s: pkcs11_del_provider failed", __func__); |
deraadt@openbsd.org | 1a321bf | 2017-03-15 03:52:30 +0000 | [diff] [blame] | 746 | send: |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 747 | free(provider); |
markus@openbsd.org | 139ca81 | 2015-01-14 13:09:09 +0000 | [diff] [blame] | 748 | send_status(e, success); |
Ben Lindstrom | 3f47163 | 2001-07-04 03:53:15 +0000 | [diff] [blame] | 749 | } |
Damien Miller | 7ea845e | 2010-02-12 09:21:02 +1100 | [diff] [blame] | 750 | #endif /* ENABLE_PKCS11 */ |
Ben Lindstrom | 3f47163 | 2001-07-04 03:53:15 +0000 | [diff] [blame] | 751 | |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 752 | /* dispatch incoming messages */ |
| 753 | |
djm@openbsd.org | fd0e8fa | 2017-07-19 01:15:02 +0000 | [diff] [blame] | 754 | static int |
| 755 | process_message(u_int socknum) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 756 | { |
markus@openbsd.org | 139ca81 | 2015-01-14 13:09:09 +0000 | [diff] [blame] | 757 | u_int msg_len; |
| 758 | u_char type; |
| 759 | const u_char *cp; |
| 760 | int r; |
djm@openbsd.org | fd0e8fa | 2017-07-19 01:15:02 +0000 | [diff] [blame] | 761 | SocketEntry *e; |
| 762 | |
| 763 | if (socknum >= sockets_alloc) { |
| 764 | fatal("%s: socket number %u >= allocated %u", |
| 765 | __func__, socknum, sockets_alloc); |
| 766 | } |
| 767 | e = &sockets[socknum]; |
Ben Lindstrom | 61d328a | 2002-06-06 21:54:57 +0000 | [diff] [blame] | 768 | |
markus@openbsd.org | 139ca81 | 2015-01-14 13:09:09 +0000 | [diff] [blame] | 769 | if (sshbuf_len(e->input) < 5) |
djm@openbsd.org | fd0e8fa | 2017-07-19 01:15:02 +0000 | [diff] [blame] | 770 | return 0; /* Incomplete message header. */ |
markus@openbsd.org | 139ca81 | 2015-01-14 13:09:09 +0000 | [diff] [blame] | 771 | cp = sshbuf_ptr(e->input); |
| 772 | msg_len = PEEK_U32(cp); |
djm@openbsd.org | fd0e8fa | 2017-07-19 01:15:02 +0000 | [diff] [blame] | 773 | if (msg_len > AGENT_MAX_LEN) { |
| 774 | debug("%s: socket %u (fd=%d) message too long %u > %u", |
| 775 | __func__, socknum, e->fd, msg_len, AGENT_MAX_LEN); |
| 776 | return -1; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 777 | } |
markus@openbsd.org | 139ca81 | 2015-01-14 13:09:09 +0000 | [diff] [blame] | 778 | if (sshbuf_len(e->input) < msg_len + 4) |
djm@openbsd.org | fd0e8fa | 2017-07-19 01:15:02 +0000 | [diff] [blame] | 779 | return 0; /* Incomplete message body. */ |
Ben Lindstrom | 21d1ed8 | 2002-06-06 21:48:57 +0000 | [diff] [blame] | 780 | |
| 781 | /* move the current input to e->request */ |
markus@openbsd.org | 139ca81 | 2015-01-14 13:09:09 +0000 | [diff] [blame] | 782 | sshbuf_reset(e->request); |
| 783 | if ((r = sshbuf_get_stringb(e->input, e->request)) != 0 || |
djm@openbsd.org | fd0e8fa | 2017-07-19 01:15:02 +0000 | [diff] [blame] | 784 | (r = sshbuf_get_u8(e->request, &type)) != 0) { |
| 785 | if (r == SSH_ERR_MESSAGE_INCOMPLETE || |
| 786 | r == SSH_ERR_STRING_TOO_LARGE) { |
| 787 | debug("%s: buffer error: %s", __func__, ssh_err(r)); |
| 788 | return -1; |
| 789 | } |
markus@openbsd.org | 139ca81 | 2015-01-14 13:09:09 +0000 | [diff] [blame] | 790 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
djm@openbsd.org | fd0e8fa | 2017-07-19 01:15:02 +0000 | [diff] [blame] | 791 | } |
| 792 | |
| 793 | 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] | 794 | |
djm@openbsd.org | 001aa55 | 2018-04-10 00:10:49 +0000 | [diff] [blame] | 795 | /* check whether agent is locked */ |
Ben Lindstrom | 2f71704 | 2002-06-06 21:52:03 +0000 | [diff] [blame] | 796 | if (locked && type != SSH_AGENTC_UNLOCK) { |
markus@openbsd.org | 139ca81 | 2015-01-14 13:09:09 +0000 | [diff] [blame] | 797 | sshbuf_reset(e->request); |
Ben Lindstrom | 2f71704 | 2002-06-06 21:52:03 +0000 | [diff] [blame] | 798 | switch (type) { |
Ben Lindstrom | 2f71704 | 2002-06-06 21:52:03 +0000 | [diff] [blame] | 799 | case SSH2_AGENTC_REQUEST_IDENTITIES: |
| 800 | /* send empty lists */ |
djm@openbsd.org | f4a6a88 | 2017-04-30 23:29:10 +0000 | [diff] [blame] | 801 | no_identities(e); |
Ben Lindstrom | 2f71704 | 2002-06-06 21:52:03 +0000 | [diff] [blame] | 802 | break; |
| 803 | default: |
| 804 | /* send a fail message for all other request types */ |
markus@openbsd.org | 139ca81 | 2015-01-14 13:09:09 +0000 | [diff] [blame] | 805 | send_status(e, 0); |
Ben Lindstrom | 2f71704 | 2002-06-06 21:52:03 +0000 | [diff] [blame] | 806 | } |
djm@openbsd.org | fd0e8fa | 2017-07-19 01:15:02 +0000 | [diff] [blame] | 807 | return 0; |
Ben Lindstrom | 2f71704 | 2002-06-06 21:52:03 +0000 | [diff] [blame] | 808 | } |
| 809 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 810 | switch (type) { |
Ben Lindstrom | 2f71704 | 2002-06-06 21:52:03 +0000 | [diff] [blame] | 811 | case SSH_AGENTC_LOCK: |
| 812 | case SSH_AGENTC_UNLOCK: |
| 813 | process_lock_agent(e, type == SSH_AGENTC_LOCK); |
| 814 | break; |
djm@openbsd.org | 2f04af9 | 2015-03-04 21:12:59 +0000 | [diff] [blame] | 815 | case SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES: |
djm@openbsd.org | f4a6a88 | 2017-04-30 23:29:10 +0000 | [diff] [blame] | 816 | process_remove_all_identities(e); /* safe for !WITH_SSH1 */ |
djm@openbsd.org | 2f04af9 | 2015-03-04 21:12:59 +0000 | [diff] [blame] | 817 | break; |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 818 | /* ssh2 */ |
| 819 | case SSH2_AGENTC_SIGN_REQUEST: |
| 820 | process_sign_request2(e); |
| 821 | break; |
| 822 | case SSH2_AGENTC_REQUEST_IDENTITIES: |
djm@openbsd.org | f4a6a88 | 2017-04-30 23:29:10 +0000 | [diff] [blame] | 823 | process_request_identities(e); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 824 | break; |
| 825 | case SSH2_AGENTC_ADD_IDENTITY: |
Ben Lindstrom | 2b266b7 | 2002-06-21 00:08:39 +0000 | [diff] [blame] | 826 | case SSH2_AGENTC_ADD_ID_CONSTRAINED: |
djm@openbsd.org | f4a6a88 | 2017-04-30 23:29:10 +0000 | [diff] [blame] | 827 | process_add_identity(e); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 828 | break; |
| 829 | case SSH2_AGENTC_REMOVE_IDENTITY: |
djm@openbsd.org | f4a6a88 | 2017-04-30 23:29:10 +0000 | [diff] [blame] | 830 | process_remove_identity(e); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 831 | break; |
| 832 | case SSH2_AGENTC_REMOVE_ALL_IDENTITIES: |
djm@openbsd.org | f4a6a88 | 2017-04-30 23:29:10 +0000 | [diff] [blame] | 833 | process_remove_all_identities(e); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 834 | break; |
Damien Miller | 7ea845e | 2010-02-12 09:21:02 +1100 | [diff] [blame] | 835 | #ifdef ENABLE_PKCS11 |
Ben Lindstrom | 3f47163 | 2001-07-04 03:53:15 +0000 | [diff] [blame] | 836 | case SSH_AGENTC_ADD_SMARTCARD_KEY: |
Damien Miller | d94f20d | 2003-06-11 22:06:33 +1000 | [diff] [blame] | 837 | case SSH_AGENTC_ADD_SMARTCARD_KEY_CONSTRAINED: |
Ben Lindstrom | 3f47163 | 2001-07-04 03:53:15 +0000 | [diff] [blame] | 838 | process_add_smartcard_key(e); |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 839 | break; |
Ben Lindstrom | 3f47163 | 2001-07-04 03:53:15 +0000 | [diff] [blame] | 840 | case SSH_AGENTC_REMOVE_SMARTCARD_KEY: |
| 841 | process_remove_smartcard_key(e); |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 842 | break; |
Damien Miller | 7ea845e | 2010-02-12 09:21:02 +1100 | [diff] [blame] | 843 | #endif /* ENABLE_PKCS11 */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 844 | default: |
| 845 | /* Unknown message. Respond with failure. */ |
| 846 | error("Unknown message %d", type); |
markus@openbsd.org | 139ca81 | 2015-01-14 13:09:09 +0000 | [diff] [blame] | 847 | sshbuf_reset(e->request); |
| 848 | send_status(e, 0); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 849 | break; |
| 850 | } |
djm@openbsd.org | fd0e8fa | 2017-07-19 01:15:02 +0000 | [diff] [blame] | 851 | return 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 852 | } |
| 853 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 854 | static void |
Ben Lindstrom | 65366a8 | 2001-12-06 16:32:47 +0000 | [diff] [blame] | 855 | new_socket(sock_type type, int fd) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 856 | { |
Darren Tucker | fb16b24 | 2003-09-22 21:04:23 +1000 | [diff] [blame] | 857 | u_int i, old_alloc, new_alloc; |
Ben Lindstrom | 822b634 | 2002-06-23 21:38:49 +0000 | [diff] [blame] | 858 | |
Damien Miller | 232711f | 2004-06-15 10:35:30 +1000 | [diff] [blame] | 859 | set_nonblock(fd); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 860 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 861 | if (fd > max_fd) |
| 862 | max_fd = fd; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 863 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 864 | for (i = 0; i < sockets_alloc; i++) |
| 865 | if (sockets[i].type == AUTH_UNUSED) { |
| 866 | sockets[i].fd = fd; |
markus@openbsd.org | 139ca81 | 2015-01-14 13:09:09 +0000 | [diff] [blame] | 867 | if ((sockets[i].input = sshbuf_new()) == NULL) |
| 868 | fatal("%s: sshbuf_new failed", __func__); |
| 869 | if ((sockets[i].output = sshbuf_new()) == NULL) |
| 870 | fatal("%s: sshbuf_new failed", __func__); |
| 871 | if ((sockets[i].request = sshbuf_new()) == NULL) |
| 872 | fatal("%s: sshbuf_new failed", __func__); |
Darren Tucker | fb16b24 | 2003-09-22 21:04:23 +1000 | [diff] [blame] | 873 | sockets[i].type = type; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 874 | return; |
| 875 | } |
| 876 | old_alloc = sockets_alloc; |
Darren Tucker | fb16b24 | 2003-09-22 21:04:23 +1000 | [diff] [blame] | 877 | new_alloc = sockets_alloc + 10; |
deraadt@openbsd.org | 657a5fb | 2015-04-24 01:36:00 +0000 | [diff] [blame] | 878 | sockets = xreallocarray(sockets, new_alloc, sizeof(sockets[0])); |
Darren Tucker | fb16b24 | 2003-09-22 21:04:23 +1000 | [diff] [blame] | 879 | for (i = old_alloc; i < new_alloc; i++) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 880 | sockets[i].type = AUTH_UNUSED; |
Darren Tucker | fb16b24 | 2003-09-22 21:04:23 +1000 | [diff] [blame] | 881 | sockets_alloc = new_alloc; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 882 | sockets[old_alloc].fd = fd; |
markus@openbsd.org | 139ca81 | 2015-01-14 13:09:09 +0000 | [diff] [blame] | 883 | if ((sockets[old_alloc].input = sshbuf_new()) == NULL) |
| 884 | fatal("%s: sshbuf_new failed", __func__); |
| 885 | if ((sockets[old_alloc].output = sshbuf_new()) == NULL) |
| 886 | fatal("%s: sshbuf_new failed", __func__); |
| 887 | if ((sockets[old_alloc].request = sshbuf_new()) == NULL) |
| 888 | fatal("%s: sshbuf_new failed", __func__); |
Darren Tucker | fb16b24 | 2003-09-22 21:04:23 +1000 | [diff] [blame] | 889 | sockets[old_alloc].type = type; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 890 | } |
| 891 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 892 | static int |
djm@openbsd.org | fd0e8fa | 2017-07-19 01:15:02 +0000 | [diff] [blame] | 893 | handle_socket_read(u_int socknum) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 894 | { |
djm@openbsd.org | fd0e8fa | 2017-07-19 01:15:02 +0000 | [diff] [blame] | 895 | struct sockaddr_un sunaddr; |
| 896 | socklen_t slen; |
| 897 | uid_t euid; |
| 898 | gid_t egid; |
| 899 | int fd; |
| 900 | |
| 901 | slen = sizeof(sunaddr); |
| 902 | fd = accept(sockets[socknum].fd, (struct sockaddr *)&sunaddr, &slen); |
deraadt@openbsd.org | 4d28fa7 | 2019-06-28 13:35:04 +0000 | [diff] [blame] | 903 | if (fd == -1) { |
djm@openbsd.org | fd0e8fa | 2017-07-19 01:15:02 +0000 | [diff] [blame] | 904 | error("accept from AUTH_SOCKET: %s", strerror(errno)); |
| 905 | return -1; |
| 906 | } |
deraadt@openbsd.org | 4d28fa7 | 2019-06-28 13:35:04 +0000 | [diff] [blame] | 907 | if (getpeereid(fd, &euid, &egid) == -1) { |
djm@openbsd.org | fd0e8fa | 2017-07-19 01:15:02 +0000 | [diff] [blame] | 908 | error("getpeereid %d failed: %s", fd, strerror(errno)); |
| 909 | close(fd); |
| 910 | return -1; |
| 911 | } |
| 912 | if ((euid != 0) && (getuid() != euid)) { |
| 913 | error("uid mismatch: peer euid %u != uid %u", |
| 914 | (u_int) euid, (u_int) getuid()); |
| 915 | close(fd); |
| 916 | return -1; |
| 917 | } |
| 918 | new_socket(AUTH_CONNECTION, fd); |
| 919 | return 0; |
| 920 | } |
| 921 | |
| 922 | static int |
| 923 | handle_conn_read(u_int socknum) |
| 924 | { |
djm@openbsd.org | d691588 | 2019-01-22 22:58:50 +0000 | [diff] [blame] | 925 | char buf[AGENT_RBUF_LEN]; |
djm@openbsd.org | fd0e8fa | 2017-07-19 01:15:02 +0000 | [diff] [blame] | 926 | ssize_t len; |
| 927 | int r; |
| 928 | |
| 929 | if ((len = read(sockets[socknum].fd, buf, sizeof(buf))) <= 0) { |
| 930 | if (len == -1) { |
| 931 | if (errno == EAGAIN || errno == EINTR) |
| 932 | return 0; |
| 933 | error("%s: read error on socket %u (fd %d): %s", |
| 934 | __func__, socknum, sockets[socknum].fd, |
| 935 | strerror(errno)); |
| 936 | } |
| 937 | return -1; |
| 938 | } |
| 939 | if ((r = sshbuf_put(sockets[socknum].input, buf, len)) != 0) |
| 940 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
| 941 | explicit_bzero(buf, sizeof(buf)); |
| 942 | process_message(socknum); |
| 943 | return 0; |
| 944 | } |
| 945 | |
| 946 | static int |
| 947 | handle_conn_write(u_int socknum) |
| 948 | { |
| 949 | ssize_t len; |
| 950 | int r; |
| 951 | |
| 952 | if (sshbuf_len(sockets[socknum].output) == 0) |
| 953 | return 0; /* shouldn't happen */ |
| 954 | if ((len = write(sockets[socknum].fd, |
| 955 | sshbuf_ptr(sockets[socknum].output), |
| 956 | sshbuf_len(sockets[socknum].output))) <= 0) { |
| 957 | if (len == -1) { |
| 958 | if (errno == EAGAIN || errno == EINTR) |
| 959 | return 0; |
| 960 | error("%s: read error on socket %u (fd %d): %s", |
| 961 | __func__, socknum, sockets[socknum].fd, |
| 962 | strerror(errno)); |
| 963 | } |
| 964 | return -1; |
| 965 | } |
| 966 | if ((r = sshbuf_consume(sockets[socknum].output, len)) != 0) |
| 967 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
| 968 | return 0; |
| 969 | } |
| 970 | |
| 971 | static void |
djm@openbsd.org | b2140a7 | 2018-05-11 03:38:51 +0000 | [diff] [blame] | 972 | after_poll(struct pollfd *pfd, size_t npfd, u_int maxfds) |
djm@openbsd.org | fd0e8fa | 2017-07-19 01:15:02 +0000 | [diff] [blame] | 973 | { |
| 974 | size_t i; |
djm@openbsd.org | b2140a7 | 2018-05-11 03:38:51 +0000 | [diff] [blame] | 975 | u_int socknum, activefds = npfd; |
djm@openbsd.org | fd0e8fa | 2017-07-19 01:15:02 +0000 | [diff] [blame] | 976 | |
| 977 | for (i = 0; i < npfd; i++) { |
| 978 | if (pfd[i].revents == 0) |
| 979 | continue; |
| 980 | /* Find sockets entry */ |
| 981 | for (socknum = 0; socknum < sockets_alloc; socknum++) { |
| 982 | if (sockets[socknum].type != AUTH_SOCKET && |
| 983 | sockets[socknum].type != AUTH_CONNECTION) |
| 984 | continue; |
| 985 | if (pfd[i].fd == sockets[socknum].fd) |
| 986 | break; |
| 987 | } |
| 988 | if (socknum >= sockets_alloc) { |
| 989 | error("%s: no socket for fd %d", __func__, pfd[i].fd); |
| 990 | continue; |
| 991 | } |
| 992 | /* Process events */ |
| 993 | switch (sockets[socknum].type) { |
| 994 | case AUTH_SOCKET: |
djm@openbsd.org | b2140a7 | 2018-05-11 03:38:51 +0000 | [diff] [blame] | 995 | if ((pfd[i].revents & (POLLIN|POLLERR)) == 0) |
| 996 | break; |
| 997 | if (npfd > maxfds) { |
| 998 | debug3("out of fds (active %u >= limit %u); " |
| 999 | "skipping accept", activefds, maxfds); |
| 1000 | break; |
| 1001 | } |
| 1002 | if (handle_socket_read(socknum) == 0) |
| 1003 | activefds++; |
djm@openbsd.org | fd0e8fa | 2017-07-19 01:15:02 +0000 | [diff] [blame] | 1004 | break; |
| 1005 | case AUTH_CONNECTION: |
| 1006 | if ((pfd[i].revents & (POLLIN|POLLERR)) != 0 && |
| 1007 | handle_conn_read(socknum) != 0) { |
djm@openbsd.org | b2140a7 | 2018-05-11 03:38:51 +0000 | [diff] [blame] | 1008 | goto close_sock; |
djm@openbsd.org | fd0e8fa | 2017-07-19 01:15:02 +0000 | [diff] [blame] | 1009 | } |
| 1010 | if ((pfd[i].revents & (POLLOUT|POLLHUP)) != 0 && |
djm@openbsd.org | b2140a7 | 2018-05-11 03:38:51 +0000 | [diff] [blame] | 1011 | handle_conn_write(socknum) != 0) { |
| 1012 | close_sock: |
| 1013 | if (activefds == 0) |
| 1014 | fatal("activefds == 0 at close_sock"); |
djm@openbsd.org | fd0e8fa | 2017-07-19 01:15:02 +0000 | [diff] [blame] | 1015 | close_socket(&sockets[socknum]); |
djm@openbsd.org | b2140a7 | 2018-05-11 03:38:51 +0000 | [diff] [blame] | 1016 | activefds--; |
| 1017 | break; |
| 1018 | } |
djm@openbsd.org | fd0e8fa | 2017-07-19 01:15:02 +0000 | [diff] [blame] | 1019 | break; |
| 1020 | default: |
| 1021 | break; |
| 1022 | } |
| 1023 | } |
| 1024 | } |
| 1025 | |
| 1026 | static int |
djm@openbsd.org | b2140a7 | 2018-05-11 03:38:51 +0000 | [diff] [blame] | 1027 | 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] | 1028 | { |
| 1029 | struct pollfd *pfd = *pfdp; |
| 1030 | size_t i, j, npfd = 0; |
Darren Tucker | 5511925 | 2013-06-02 07:43:59 +1000 | [diff] [blame] | 1031 | time_t deadline; |
djm@openbsd.org | d691588 | 2019-01-22 22:58:50 +0000 | [diff] [blame] | 1032 | int r; |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 1033 | |
djm@openbsd.org | fd0e8fa | 2017-07-19 01:15:02 +0000 | [diff] [blame] | 1034 | /* Count active sockets */ |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 1035 | for (i = 0; i < sockets_alloc; i++) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1036 | switch (sockets[i].type) { |
| 1037 | case AUTH_SOCKET: |
| 1038 | case AUTH_CONNECTION: |
djm@openbsd.org | fd0e8fa | 2017-07-19 01:15:02 +0000 | [diff] [blame] | 1039 | npfd++; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1040 | break; |
| 1041 | case AUTH_UNUSED: |
| 1042 | break; |
| 1043 | default: |
| 1044 | fatal("Unknown socket type %d", sockets[i].type); |
| 1045 | break; |
| 1046 | } |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 1047 | } |
djm@openbsd.org | fd0e8fa | 2017-07-19 01:15:02 +0000 | [diff] [blame] | 1048 | if (npfd != *npfdp && |
| 1049 | (pfd = recallocarray(pfd, *npfdp, npfd, sizeof(*pfd))) == NULL) |
| 1050 | fatal("%s: recallocarray failed", __func__); |
| 1051 | *pfdp = pfd; |
| 1052 | *npfdp = npfd; |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 1053 | |
djm@openbsd.org | fd0e8fa | 2017-07-19 01:15:02 +0000 | [diff] [blame] | 1054 | for (i = j = 0; i < sockets_alloc; i++) { |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 1055 | switch (sockets[i].type) { |
| 1056 | case AUTH_SOCKET: |
djm@openbsd.org | b2140a7 | 2018-05-11 03:38:51 +0000 | [diff] [blame] | 1057 | if (npfd > maxfds) { |
| 1058 | debug3("out of fds (active %zu >= limit %u); " |
| 1059 | "skipping arming listener", npfd, maxfds); |
| 1060 | break; |
| 1061 | } |
| 1062 | pfd[j].fd = sockets[i].fd; |
| 1063 | pfd[j].revents = 0; |
| 1064 | pfd[j].events = POLLIN; |
| 1065 | j++; |
| 1066 | break; |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 1067 | case AUTH_CONNECTION: |
djm@openbsd.org | fd0e8fa | 2017-07-19 01:15:02 +0000 | [diff] [blame] | 1068 | pfd[j].fd = sockets[i].fd; |
| 1069 | pfd[j].revents = 0; |
djm@openbsd.org | d691588 | 2019-01-22 22:58:50 +0000 | [diff] [blame] | 1070 | /* |
| 1071 | * Only prepare to read if we can handle a full-size |
| 1072 | * input read buffer and enqueue a max size reply.. |
| 1073 | */ |
| 1074 | if ((r = sshbuf_check_reserve(sockets[i].input, |
| 1075 | AGENT_RBUF_LEN)) == 0 && |
| 1076 | (r = sshbuf_check_reserve(sockets[i].output, |
| 1077 | AGENT_MAX_LEN)) == 0) |
| 1078 | pfd[j].events = POLLIN; |
| 1079 | else if (r != SSH_ERR_NO_BUFFER_SPACE) { |
| 1080 | fatal("%s: buffer error: %s", |
| 1081 | __func__, ssh_err(r)); |
| 1082 | } |
markus@openbsd.org | 139ca81 | 2015-01-14 13:09:09 +0000 | [diff] [blame] | 1083 | if (sshbuf_len(sockets[i].output) > 0) |
djm@openbsd.org | fd0e8fa | 2017-07-19 01:15:02 +0000 | [diff] [blame] | 1084 | pfd[j].events |= POLLOUT; |
| 1085 | j++; |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 1086 | break; |
| 1087 | default: |
| 1088 | break; |
| 1089 | } |
| 1090 | } |
Darren Tucker | 2812dc9 | 2007-03-21 20:45:06 +1100 | [diff] [blame] | 1091 | deadline = reaper(); |
| 1092 | if (parent_alive_interval != 0) |
| 1093 | deadline = (deadline == 0) ? parent_alive_interval : |
deraadt@openbsd.org | 9136ec1 | 2016-09-12 01:22:38 +0000 | [diff] [blame] | 1094 | MINIMUM(deadline, parent_alive_interval); |
Darren Tucker | 2812dc9 | 2007-03-21 20:45:06 +1100 | [diff] [blame] | 1095 | if (deadline == 0) { |
djm@openbsd.org | 9f0e44e | 2017-07-24 04:34:28 +0000 | [diff] [blame] | 1096 | *timeoutp = -1; /* INFTIM */ |
Darren Tucker | 2812dc9 | 2007-03-21 20:45:06 +1100 | [diff] [blame] | 1097 | } else { |
djm@openbsd.org | fd0e8fa | 2017-07-19 01:15:02 +0000 | [diff] [blame] | 1098 | if (deadline > INT_MAX / 1000) |
| 1099 | *timeoutp = INT_MAX / 1000; |
| 1100 | else |
| 1101 | *timeoutp = deadline * 1000; |
Darren Tucker | 2812dc9 | 2007-03-21 20:45:06 +1100 | [diff] [blame] | 1102 | } |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 1103 | return (1); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1104 | } |
| 1105 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1106 | static void |
Darren Tucker | 6fa8abd | 2003-09-22 21:10:21 +1000 | [diff] [blame] | 1107 | cleanup_socket(void) |
Damien Miller | 792c511 | 1999-10-29 09:47:09 +1000 | [diff] [blame] | 1108 | { |
Damien Miller | b1e967c | 2014-07-03 21:22:40 +1000 | [diff] [blame] | 1109 | if (cleanup_pid != 0 && getpid() != cleanup_pid) |
| 1110 | return; |
| 1111 | debug("%s: cleanup", __func__); |
Ben Lindstrom | 77808ab | 2001-01-26 05:10:34 +0000 | [diff] [blame] | 1112 | if (socket_name[0]) |
| 1113 | unlink(socket_name); |
| 1114 | if (socket_dir[0]) |
| 1115 | rmdir(socket_dir); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1116 | } |
| 1117 | |
Darren Tucker | 3e33cec | 2003-10-02 16:12:36 +1000 | [diff] [blame] | 1118 | void |
Damien Miller | 792c511 | 1999-10-29 09:47:09 +1000 | [diff] [blame] | 1119 | cleanup_exit(int i) |
| 1120 | { |
Darren Tucker | 6fa8abd | 2003-09-22 21:10:21 +1000 | [diff] [blame] | 1121 | cleanup_socket(); |
| 1122 | _exit(i); |
Damien Miller | 792c511 | 1999-10-29 09:47:09 +1000 | [diff] [blame] | 1123 | } |
| 1124 | |
Damien Miller | 1c13bd8 | 2006-03-26 14:28:14 +1100 | [diff] [blame] | 1125 | /*ARGSUSED*/ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1126 | static void |
Ben Lindstrom | 77808ab | 2001-01-26 05:10:34 +0000 | [diff] [blame] | 1127 | cleanup_handler(int sig) |
| 1128 | { |
Darren Tucker | 6fa8abd | 2003-09-22 21:10:21 +1000 | [diff] [blame] | 1129 | cleanup_socket(); |
Damien Miller | 7ea845e | 2010-02-12 09:21:02 +1100 | [diff] [blame] | 1130 | #ifdef ENABLE_PKCS11 |
| 1131 | pkcs11_terminate(); |
| 1132 | #endif |
Ben Lindstrom | 77808ab | 2001-01-26 05:10:34 +0000 | [diff] [blame] | 1133 | _exit(2); |
| 1134 | } |
| 1135 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1136 | static void |
Darren Tucker | 2812dc9 | 2007-03-21 20:45:06 +1100 | [diff] [blame] | 1137 | check_parent_exists(void) |
Ben Lindstrom | 0250da0 | 2001-07-22 20:44:00 +0000 | [diff] [blame] | 1138 | { |
Darren Tucker | 3e78a51 | 2011-06-03 14:14:16 +1000 | [diff] [blame] | 1139 | /* |
| 1140 | * If our parent has exited then getppid() will return (pid_t)1, |
| 1141 | * so testing for that should be safe. |
| 1142 | */ |
| 1143 | if (parent_pid != -1 && getppid() != parent_pid) { |
Ben Lindstrom | 0250da0 | 2001-07-22 20:44:00 +0000 | [diff] [blame] | 1144 | /* printf("Parent has died - Authentication agent exiting.\n"); */ |
Darren Tucker | 2812dc9 | 2007-03-21 20:45:06 +1100 | [diff] [blame] | 1145 | cleanup_socket(); |
| 1146 | _exit(2); |
Ben Lindstrom | 0250da0 | 2001-07-22 20:44:00 +0000 | [diff] [blame] | 1147 | } |
Ben Lindstrom | 0250da0 | 2001-07-22 20:44:00 +0000 | [diff] [blame] | 1148 | } |
| 1149 | |
| 1150 | static void |
Ben Lindstrom | 28072eb | 2001-02-10 23:13:41 +0000 | [diff] [blame] | 1151 | usage(void) |
Damien Miller | 792c511 | 1999-10-29 09:47:09 +1000 | [diff] [blame] | 1152 | { |
Damien Miller | f0858de | 2014-04-20 13:01:30 +1000 | [diff] [blame] | 1153 | fprintf(stderr, |
jmc@openbsd.org | 4402d6c | 2019-11-19 16:02:32 +0000 | [diff] [blame] | 1154 | "usage: ssh-agent [-c | -s] [-Dd] [-a bind_address] [-E fingerprint_hash]\n" |
| 1155 | " [-P provider_whitelist] [-t life] [command [arg ...]]\n" |
| 1156 | " ssh-agent [-c | -s] -k\n"); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1157 | exit(1); |
Damien Miller | 792c511 | 1999-10-29 09:47:09 +1000 | [diff] [blame] | 1158 | } |
| 1159 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1160 | int |
| 1161 | main(int ac, char **av) |
| 1162 | { |
djm@openbsd.org | 2ea9746 | 2015-04-24 05:26:44 +0000 | [diff] [blame] | 1163 | 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] | 1164 | int sock, fd, ch, result, saved_errno; |
Ben Lindstrom | 822b634 | 2002-06-23 21:38:49 +0000 | [diff] [blame] | 1165 | char *shell, *format, *pidstr, *agentsocket = NULL; |
Ben Lindstrom | 2c467a2 | 2000-12-27 04:57:41 +0000 | [diff] [blame] | 1166 | #ifdef HAVE_SETRLIMIT |
Ben Lindstrom | c72745a | 2000-12-02 19:03:54 +0000 | [diff] [blame] | 1167 | struct rlimit rlim; |
Ben Lindstrom | 2c467a2 | 2000-12-27 04:57:41 +0000 | [diff] [blame] | 1168 | #endif |
Damien Miller | 615f939 | 2000-05-17 22:53:33 +1000 | [diff] [blame] | 1169 | extern int optind; |
Ben Lindstrom | 883844d | 2002-06-23 00:20:50 +0000 | [diff] [blame] | 1170 | extern char *optarg; |
Ben Lindstrom | 822b634 | 2002-06-23 21:38:49 +0000 | [diff] [blame] | 1171 | pid_t pid; |
| 1172 | char pidstrbuf[1 + 3 * sizeof pid]; |
Darren Tucker | 9013323 | 2009-06-21 17:50:15 +1000 | [diff] [blame] | 1173 | size_t len; |
Damien Miller | ab2ec58 | 2014-07-18 15:04:47 +1000 | [diff] [blame] | 1174 | mode_t prev_mask; |
djm@openbsd.org | 9f0e44e | 2017-07-24 04:34:28 +0000 | [diff] [blame] | 1175 | int timeout = -1; /* INFTIM */ |
djm@openbsd.org | fd0e8fa | 2017-07-19 01:15:02 +0000 | [diff] [blame] | 1176 | struct pollfd *pfd = NULL; |
| 1177 | size_t npfd = 0; |
djm@openbsd.org | b2140a7 | 2018-05-11 03:38:51 +0000 | [diff] [blame] | 1178 | u_int maxfds; |
Kevin Steves | de41bc6 | 2000-12-14 00:04:08 +0000 | [diff] [blame] | 1179 | |
Darren Tucker | ce321d8 | 2005-10-03 18:11:24 +1000 | [diff] [blame] | 1180 | /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */ |
| 1181 | sanitise_stdfd(); |
| 1182 | |
Damien Miller | 6cffb9a | 2002-09-04 16:20:26 +1000 | [diff] [blame] | 1183 | /* drop */ |
| 1184 | setegid(getgid()); |
| 1185 | setgid(getgid()); |
| 1186 | |
Darren Tucker | 0fb7f59 | 2016-06-09 16:23:07 +1000 | [diff] [blame] | 1187 | platform_disable_tracing(0); /* strict=no */ |
Damien Miller | 6c4914a | 2004-03-03 11:08:59 +1100 | [diff] [blame] | 1188 | |
Darren Tucker | 7694e9d | 2019-10-28 17:05:36 +1100 | [diff] [blame] | 1189 | #ifdef RLIMIT_NOFILE |
djm@openbsd.org | b2140a7 | 2018-05-11 03:38:51 +0000 | [diff] [blame] | 1190 | if (getrlimit(RLIMIT_NOFILE, &rlim) == -1) |
| 1191 | fatal("%s: getrlimit: %s", __progname, strerror(errno)); |
Darren Tucker | 7694e9d | 2019-10-28 17:05:36 +1100 | [diff] [blame] | 1192 | #endif |
djm@openbsd.org | b2140a7 | 2018-05-11 03:38:51 +0000 | [diff] [blame] | 1193 | |
Damien Miller | 59d3d5b | 2003-08-22 09:34:41 +1000 | [diff] [blame] | 1194 | __progname = ssh_get_progname(av[0]); |
Damien Miller | 60bc517 | 2001-03-19 09:38:15 +1100 | [diff] [blame] | 1195 | seed_rng(); |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 1196 | |
djm@openbsd.org | 786d599 | 2016-11-30 03:07:37 +0000 | [diff] [blame] | 1197 | while ((ch = getopt(ac, av, "cDdksE:a:P:t:")) != -1) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1198 | switch (ch) { |
djm@openbsd.org | 56d1c83 | 2014-12-21 22:27:55 +0000 | [diff] [blame] | 1199 | case 'E': |
| 1200 | fingerprint_hash = ssh_digest_alg_by_name(optarg); |
| 1201 | if (fingerprint_hash == -1) |
| 1202 | fatal("Invalid hash algorithm \"%s\"", optarg); |
| 1203 | break; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1204 | case 'c': |
| 1205 | if (s_flag) |
| 1206 | usage(); |
| 1207 | c_flag++; |
| 1208 | break; |
| 1209 | case 'k': |
| 1210 | k_flag++; |
| 1211 | break; |
djm@openbsd.org | 786d599 | 2016-11-30 03:07:37 +0000 | [diff] [blame] | 1212 | case 'P': |
djm@openbsd.org | 07da39f | 2019-10-31 21:22:01 +0000 | [diff] [blame] | 1213 | if (provider_whitelist != NULL) |
djm@openbsd.org | 786d599 | 2016-11-30 03:07:37 +0000 | [diff] [blame] | 1214 | fatal("-P option already specified"); |
djm@openbsd.org | 07da39f | 2019-10-31 21:22:01 +0000 | [diff] [blame] | 1215 | provider_whitelist = xstrdup(optarg); |
djm@openbsd.org | 786d599 | 2016-11-30 03:07:37 +0000 | [diff] [blame] | 1216 | break; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1217 | case 's': |
| 1218 | if (c_flag) |
| 1219 | usage(); |
| 1220 | s_flag++; |
| 1221 | break; |
Ben Lindstrom | d94580c | 2001-07-04 03:48:02 +0000 | [diff] [blame] | 1222 | case 'd': |
djm@openbsd.org | 2ea9746 | 2015-04-24 05:26:44 +0000 | [diff] [blame] | 1223 | if (d_flag || D_flag) |
Ben Lindstrom | d94580c | 2001-07-04 03:48:02 +0000 | [diff] [blame] | 1224 | usage(); |
| 1225 | d_flag++; |
| 1226 | break; |
djm@openbsd.org | 2ea9746 | 2015-04-24 05:26:44 +0000 | [diff] [blame] | 1227 | case 'D': |
| 1228 | if (d_flag || D_flag) |
| 1229 | usage(); |
| 1230 | D_flag++; |
| 1231 | break; |
Ben Lindstrom | b7788f3 | 2002-06-06 21:46:08 +0000 | [diff] [blame] | 1232 | case 'a': |
| 1233 | agentsocket = optarg; |
| 1234 | break; |
Damien Miller | 53d8148 | 2003-01-22 11:47:19 +1100 | [diff] [blame] | 1235 | case 't': |
| 1236 | if ((lifetime = convtime(optarg)) == -1) { |
| 1237 | fprintf(stderr, "Invalid lifetime\n"); |
| 1238 | usage(); |
| 1239 | } |
| 1240 | break; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1241 | default: |
| 1242 | usage(); |
| 1243 | } |
Damien Miller | 792c511 | 1999-10-29 09:47:09 +1000 | [diff] [blame] | 1244 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1245 | ac -= optind; |
| 1246 | av += optind; |
Damien Miller | 792c511 | 1999-10-29 09:47:09 +1000 | [diff] [blame] | 1247 | |
djm@openbsd.org | 2ea9746 | 2015-04-24 05:26:44 +0000 | [diff] [blame] | 1248 | 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] | 1249 | usage(); |
Damien Miller | 792c511 | 1999-10-29 09:47:09 +1000 | [diff] [blame] | 1250 | |
djm@openbsd.org | 07da39f | 2019-10-31 21:22:01 +0000 | [diff] [blame] | 1251 | if (provider_whitelist == NULL) |
| 1252 | provider_whitelist = xstrdup(DEFAULT_PROVIDER_WHITELIST); |
djm@openbsd.org | 786d599 | 2016-11-30 03:07:37 +0000 | [diff] [blame] | 1253 | |
Ben Lindstrom | eecdf23 | 2002-04-02 21:03:51 +0000 | [diff] [blame] | 1254 | if (ac == 0 && !c_flag && !s_flag) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1255 | shell = getenv("SHELL"); |
Darren Tucker | 9013323 | 2009-06-21 17:50:15 +1000 | [diff] [blame] | 1256 | if (shell != NULL && (len = strlen(shell)) > 2 && |
| 1257 | strncmp(shell + len - 3, "csh", 3) == 0) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1258 | c_flag = 1; |
Damien Miller | 792c511 | 1999-10-29 09:47:09 +1000 | [diff] [blame] | 1259 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1260 | if (k_flag) { |
Damien Miller | 89c3fe4 | 2006-03-31 23:11:28 +1100 | [diff] [blame] | 1261 | const char *errstr = NULL; |
| 1262 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1263 | pidstr = getenv(SSH_AGENTPID_ENV_NAME); |
| 1264 | if (pidstr == NULL) { |
| 1265 | fprintf(stderr, "%s not set, cannot kill agent\n", |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 1266 | SSH_AGENTPID_ENV_NAME); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1267 | exit(1); |
| 1268 | } |
Damien Miller | 89c3fe4 | 2006-03-31 23:11:28 +1100 | [diff] [blame] | 1269 | pid = (int)strtonum(pidstr, 2, INT_MAX, &errstr); |
| 1270 | if (errstr) { |
| 1271 | fprintf(stderr, |
| 1272 | "%s=\"%s\", which is not a good PID: %s\n", |
| 1273 | SSH_AGENTPID_ENV_NAME, pidstr, errstr); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1274 | exit(1); |
| 1275 | } |
| 1276 | if (kill(pid, SIGTERM) == -1) { |
| 1277 | perror("kill"); |
| 1278 | exit(1); |
| 1279 | } |
| 1280 | format = c_flag ? "unsetenv %s;\n" : "unset %s;\n"; |
| 1281 | printf(format, SSH_AUTHSOCKET_ENV_NAME); |
| 1282 | printf(format, SSH_AGENTPID_ENV_NAME); |
Ben Lindstrom | ce0f634 | 2002-06-11 16:42:49 +0000 | [diff] [blame] | 1283 | printf("echo Agent pid %ld killed;\n", (long)pid); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1284 | exit(0); |
Damien Miller | 792c511 | 1999-10-29 09:47:09 +1000 | [diff] [blame] | 1285 | } |
djm@openbsd.org | b2140a7 | 2018-05-11 03:38:51 +0000 | [diff] [blame] | 1286 | |
| 1287 | /* |
| 1288 | * Minimum file descriptors: |
| 1289 | * stdio (3) + listener (1) + syslog (1 maybe) + connection (1) + |
| 1290 | * a few spare for libc / stack protectors / sanitisers, etc. |
| 1291 | */ |
| 1292 | #define SSH_AGENT_MIN_FDS (3+1+1+1+4) |
| 1293 | if (rlim.rlim_cur < SSH_AGENT_MIN_FDS) |
djm@openbsd.org | 960e7c6 | 2018-11-09 02:57:58 +0000 | [diff] [blame] | 1294 | fatal("%s: file descriptor rlimit %lld too low (minimum %u)", |
djm@openbsd.org | b2140a7 | 2018-05-11 03:38:51 +0000 | [diff] [blame] | 1295 | __progname, (long long)rlim.rlim_cur, SSH_AGENT_MIN_FDS); |
| 1296 | maxfds = rlim.rlim_cur - SSH_AGENT_MIN_FDS; |
| 1297 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1298 | parent_pid = getpid(); |
| 1299 | |
Ben Lindstrom | b7788f3 | 2002-06-06 21:46:08 +0000 | [diff] [blame] | 1300 | if (agentsocket == NULL) { |
| 1301 | /* Create private directory for agent socket */ |
Damien Miller | 2cd6293 | 2010-12-01 11:50:35 +1100 | [diff] [blame] | 1302 | mktemp_proto(socket_dir, sizeof(socket_dir)); |
Ben Lindstrom | b7788f3 | 2002-06-06 21:46:08 +0000 | [diff] [blame] | 1303 | if (mkdtemp(socket_dir) == NULL) { |
| 1304 | perror("mkdtemp: private socket dir"); |
| 1305 | exit(1); |
| 1306 | } |
Ben Lindstrom | ce0f634 | 2002-06-11 16:42:49 +0000 | [diff] [blame] | 1307 | snprintf(socket_name, sizeof socket_name, "%s/agent.%ld", socket_dir, |
| 1308 | (long)parent_pid); |
Ben Lindstrom | b7788f3 | 2002-06-06 21:46:08 +0000 | [diff] [blame] | 1309 | } else { |
| 1310 | /* Try to use specified agent socket */ |
| 1311 | socket_dir[0] = '\0'; |
| 1312 | strlcpy(socket_name, agentsocket, sizeof socket_name); |
Damien Miller | 792c511 | 1999-10-29 09:47:09 +1000 | [diff] [blame] | 1313 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1314 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1315 | /* |
| 1316 | * Create socket early so it will exist before command gets run from |
| 1317 | * the parent. |
| 1318 | */ |
Damien Miller | ab2ec58 | 2014-07-18 15:04:47 +1000 | [diff] [blame] | 1319 | prev_mask = umask(0177); |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 1320 | sock = unix_listener(socket_name, SSH_LISTEN_BACKLOG, 0); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1321 | if (sock < 0) { |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 1322 | /* XXX - unix_listener() calls error() not perror() */ |
Darren Tucker | 1dee868 | 2004-11-05 20:26:49 +1100 | [diff] [blame] | 1323 | *socket_name = '\0'; /* Don't unlink any existing file */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1324 | cleanup_exit(1); |
Damien Miller | 792c511 | 1999-10-29 09:47:09 +1000 | [diff] [blame] | 1325 | } |
Damien Miller | ab2ec58 | 2014-07-18 15:04:47 +1000 | [diff] [blame] | 1326 | umask(prev_mask); |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 1327 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1328 | /* |
| 1329 | * Fork, and have the parent execute the command, if any, or present |
| 1330 | * the socket data. The child continues as the authentication agent. |
| 1331 | */ |
djm@openbsd.org | 2ea9746 | 2015-04-24 05:26:44 +0000 | [diff] [blame] | 1332 | if (D_flag || d_flag) { |
| 1333 | log_init(__progname, |
| 1334 | d_flag ? SYSLOG_LEVEL_DEBUG3 : SYSLOG_LEVEL_INFO, |
| 1335 | SYSLOG_FACILITY_AUTH, 1); |
Ben Lindstrom | d94580c | 2001-07-04 03:48:02 +0000 | [diff] [blame] | 1336 | format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n"; |
| 1337 | printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name, |
| 1338 | SSH_AUTHSOCKET_ENV_NAME); |
Ben Lindstrom | ce0f634 | 2002-06-11 16:42:49 +0000 | [diff] [blame] | 1339 | printf("echo Agent pid %ld;\n", (long)parent_pid); |
dtucker@openbsd.org | 79394ed | 2015-12-11 02:29:03 +0000 | [diff] [blame] | 1340 | fflush(stdout); |
Ben Lindstrom | d94580c | 2001-07-04 03:48:02 +0000 | [diff] [blame] | 1341 | goto skip; |
| 1342 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1343 | pid = fork(); |
| 1344 | if (pid == -1) { |
| 1345 | perror("fork"); |
Damien Miller | c653b89 | 2002-02-08 22:05:41 +1100 | [diff] [blame] | 1346 | cleanup_exit(1); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1347 | } |
| 1348 | if (pid != 0) { /* Parent - execute the given command. */ |
| 1349 | close(sock); |
Ben Lindstrom | ce0f634 | 2002-06-11 16:42:49 +0000 | [diff] [blame] | 1350 | snprintf(pidstrbuf, sizeof pidstrbuf, "%ld", (long)pid); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1351 | if (ac == 0) { |
| 1352 | format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n"; |
| 1353 | printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name, |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 1354 | SSH_AUTHSOCKET_ENV_NAME); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1355 | printf(format, SSH_AGENTPID_ENV_NAME, pidstrbuf, |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 1356 | SSH_AGENTPID_ENV_NAME); |
Ben Lindstrom | ce0f634 | 2002-06-11 16:42:49 +0000 | [diff] [blame] | 1357 | printf("echo Agent pid %ld;\n", (long)pid); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1358 | exit(0); |
| 1359 | } |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 1360 | if (setenv(SSH_AUTHSOCKET_ENV_NAME, socket_name, 1) == -1 || |
| 1361 | setenv(SSH_AGENTPID_ENV_NAME, pidstrbuf, 1) == -1) { |
| 1362 | perror("setenv"); |
| 1363 | exit(1); |
| 1364 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1365 | execvp(av[0], av); |
| 1366 | perror(av[0]); |
| 1367 | exit(1); |
| 1368 | } |
Damien Miller | c653b89 | 2002-02-08 22:05:41 +1100 | [diff] [blame] | 1369 | /* child */ |
| 1370 | log_init(__progname, SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_AUTH, 0); |
Ben Lindstrom | 3fdf876 | 2001-07-22 20:40:24 +0000 | [diff] [blame] | 1371 | |
| 1372 | if (setsid() == -1) { |
Damien Miller | c653b89 | 2002-02-08 22:05:41 +1100 | [diff] [blame] | 1373 | error("setsid: %s", strerror(errno)); |
Ben Lindstrom | 3fdf876 | 2001-07-22 20:40:24 +0000 | [diff] [blame] | 1374 | cleanup_exit(1); |
| 1375 | } |
| 1376 | |
| 1377 | (void)chdir("/"); |
Damien Miller | 6c71179 | 2003-01-24 11:36:23 +1100 | [diff] [blame] | 1378 | if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) { |
| 1379 | /* XXX might close listen socket */ |
| 1380 | (void)dup2(fd, STDIN_FILENO); |
| 1381 | (void)dup2(fd, STDOUT_FILENO); |
| 1382 | (void)dup2(fd, STDERR_FILENO); |
| 1383 | if (fd > 2) |
| 1384 | close(fd); |
| 1385 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1386 | |
Ben Lindstrom | 2c467a2 | 2000-12-27 04:57:41 +0000 | [diff] [blame] | 1387 | #ifdef HAVE_SETRLIMIT |
Ben Lindstrom | c72745a | 2000-12-02 19:03:54 +0000 | [diff] [blame] | 1388 | /* deny core dumps, since memory contains unencrypted private keys */ |
| 1389 | rlim.rlim_cur = rlim.rlim_max = 0; |
deraadt@openbsd.org | 4d28fa7 | 2019-06-28 13:35:04 +0000 | [diff] [blame] | 1390 | if (setrlimit(RLIMIT_CORE, &rlim) == -1) { |
Damien Miller | c653b89 | 2002-02-08 22:05:41 +1100 | [diff] [blame] | 1391 | error("setrlimit RLIMIT_CORE: %s", strerror(errno)); |
Ben Lindstrom | c72745a | 2000-12-02 19:03:54 +0000 | [diff] [blame] | 1392 | cleanup_exit(1); |
| 1393 | } |
Ben Lindstrom | 2c467a2 | 2000-12-27 04:57:41 +0000 | [diff] [blame] | 1394 | #endif |
Ben Lindstrom | d94580c | 2001-07-04 03:48:02 +0000 | [diff] [blame] | 1395 | |
| 1396 | skip: |
Damien Miller | 7ea845e | 2010-02-12 09:21:02 +1100 | [diff] [blame] | 1397 | |
Damien Miller | b1e967c | 2014-07-03 21:22:40 +1000 | [diff] [blame] | 1398 | cleanup_pid = getpid(); |
| 1399 | |
Damien Miller | 7ea845e | 2010-02-12 09:21:02 +1100 | [diff] [blame] | 1400 | #ifdef ENABLE_PKCS11 |
| 1401 | pkcs11_init(0); |
| 1402 | #endif |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1403 | new_socket(AUTH_SOCKET, sock); |
Darren Tucker | 2812dc9 | 2007-03-21 20:45:06 +1100 | [diff] [blame] | 1404 | if (ac > 0) |
| 1405 | parent_alive_interval = 10; |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 1406 | idtab_init(); |
Damien Miller | 48bfa9c | 2001-07-14 12:12:55 +1000 | [diff] [blame] | 1407 | signal(SIGPIPE, SIG_IGN); |
djm@openbsd.org | 2ea9746 | 2015-04-24 05:26:44 +0000 | [diff] [blame] | 1408 | signal(SIGINT, (d_flag | D_flag) ? cleanup_handler : SIG_IGN); |
Ben Lindstrom | 77808ab | 2001-01-26 05:10:34 +0000 | [diff] [blame] | 1409 | signal(SIGHUP, cleanup_handler); |
| 1410 | signal(SIGTERM, cleanup_handler); |
Ben Lindstrom | a3d5a4c | 2001-07-18 15:58:08 +0000 | [diff] [blame] | 1411 | |
djm@openbsd.org | 786d599 | 2016-11-30 03:07:37 +0000 | [diff] [blame] | 1412 | if (pledge("stdio rpath cpath unix id proc exec", NULL) == -1) |
djm@openbsd.org | d952162 | 2015-12-01 23:29:24 +0000 | [diff] [blame] | 1413 | fatal("%s: pledge: %s", __progname, strerror(errno)); |
Damien Miller | 4626cba | 2016-01-08 14:24:56 +1100 | [diff] [blame] | 1414 | platform_pledge_agent(); |
djm@openbsd.org | d952162 | 2015-12-01 23:29:24 +0000 | [diff] [blame] | 1415 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1416 | while (1) { |
djm@openbsd.org | b2140a7 | 2018-05-11 03:38:51 +0000 | [diff] [blame] | 1417 | prepare_poll(&pfd, &npfd, &timeout, maxfds); |
djm@openbsd.org | fd0e8fa | 2017-07-19 01:15:02 +0000 | [diff] [blame] | 1418 | result = poll(pfd, npfd, timeout); |
Darren Tucker | cf0d2db | 2007-02-28 21:19:58 +1100 | [diff] [blame] | 1419 | saved_errno = errno; |
Darren Tucker | 2812dc9 | 2007-03-21 20:45:06 +1100 | [diff] [blame] | 1420 | if (parent_alive_interval != 0) |
| 1421 | check_parent_exists(); |
| 1422 | (void) reaper(); /* remove expired keys */ |
deraadt@openbsd.org | 4d28fa7 | 2019-06-28 13:35:04 +0000 | [diff] [blame] | 1423 | if (result == -1) { |
Darren Tucker | cf0d2db | 2007-02-28 21:19:58 +1100 | [diff] [blame] | 1424 | if (saved_errno == EINTR) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1425 | continue; |
djm@openbsd.org | fd0e8fa | 2017-07-19 01:15:02 +0000 | [diff] [blame] | 1426 | fatal("poll: %s", strerror(saved_errno)); |
Darren Tucker | cf0d2db | 2007-02-28 21:19:58 +1100 | [diff] [blame] | 1427 | } else if (result > 0) |
djm@openbsd.org | b2140a7 | 2018-05-11 03:38:51 +0000 | [diff] [blame] | 1428 | after_poll(pfd, npfd, maxfds); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1429 | } |
| 1430 | /* NOTREACHED */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1431 | } |