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