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