Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1 | /* |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2 | * Author: Tatu Ylonen <ylo@cs.hut.fi> |
| 3 | * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland |
| 4 | * All rights reserved |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 5 | * The authentication agent program. |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 6 | * |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 7 | * As far as I am concerned, the code I have written for this software |
| 8 | * can be used freely for any purpose. Any derived versions of this |
| 9 | * software must be clearly marked as such, and if the derived work is |
| 10 | * incompatible with the protocol description in the RFC file, it must be |
| 11 | * called by a name other than "ssh" or "Secure Shell". |
| 12 | * |
Ben Lindstrom | 4469723 | 2001-07-04 03:32:30 +0000 | [diff] [blame] | 13 | * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved. |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 14 | * |
| 15 | * Redistribution and use in source and binary forms, with or without |
| 16 | * modification, are permitted provided that the following conditions |
| 17 | * are met: |
| 18 | * 1. Redistributions of source code must retain the above copyright |
| 19 | * notice, this list of conditions and the following disclaimer. |
| 20 | * 2. Redistributions in binary form must reproduce the above copyright |
| 21 | * notice, this list of conditions and the following disclaimer in the |
| 22 | * documentation and/or other materials provided with the distribution. |
| 23 | * |
| 24 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 25 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 26 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 27 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 28 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 29 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 30 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 31 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 32 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 33 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 34 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 35 | |
| 36 | #include "includes.h" |
Damien Miller | ec52d7c | 2002-01-22 23:52:17 +1100 | [diff] [blame] | 37 | #include "openbsd-compat/fake-queue.h" |
Ben Lindstrom | 494709d | 2002-06-11 15:42:53 +0000 | [diff] [blame] | 38 | RCSID("$OpenBSD: ssh-agent.c,v 1.90 2002/06/09 13:32:01 markus Exp $"); |
Damien Miller | ec52d7c | 2002-01-22 23:52:17 +1100 | [diff] [blame] | 39 | |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 40 | #include <openssl/evp.h> |
| 41 | #include <openssl/md5.h> |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 42 | |
| 43 | #include "ssh.h" |
| 44 | #include "rsa.h" |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 45 | #include "buffer.h" |
| 46 | #include "bufaux.h" |
| 47 | #include "xmalloc.h" |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 48 | #include "getput.h" |
Damien Miller | 994cf14 | 2000-07-21 10:19:44 +1000 | [diff] [blame] | 49 | #include "key.h" |
| 50 | #include "authfd.h" |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 51 | #include "compat.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 52 | #include "log.h" |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 53 | |
Ben Lindstrom | 3f47163 | 2001-07-04 03:53:15 +0000 | [diff] [blame] | 54 | #ifdef SMARTCARD |
Ben Lindstrom | 3f47163 | 2001-07-04 03:53:15 +0000 | [diff] [blame] | 55 | #include "scard.h" |
Ben Lindstrom | bcc1808 | 2001-08-06 21:59:25 +0000 | [diff] [blame] | 56 | #endif |
Ben Lindstrom | 3f47163 | 2001-07-04 03:53:15 +0000 | [diff] [blame] | 57 | |
Ben Lindstrom | 65366a8 | 2001-12-06 16:32:47 +0000 | [diff] [blame] | 58 | typedef enum { |
| 59 | AUTH_UNUSED, |
| 60 | AUTH_SOCKET, |
| 61 | AUTH_CONNECTION |
| 62 | } sock_type; |
| 63 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 64 | typedef struct { |
| 65 | int fd; |
Ben Lindstrom | 65366a8 | 2001-12-06 16:32:47 +0000 | [diff] [blame] | 66 | sock_type type; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 67 | Buffer input; |
| 68 | Buffer output; |
Ben Lindstrom | 21d1ed8 | 2002-06-06 21:48:57 +0000 | [diff] [blame] | 69 | Buffer request; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 70 | } SocketEntry; |
| 71 | |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 72 | u_int sockets_alloc = 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 73 | SocketEntry *sockets = NULL; |
| 74 | |
Damien Miller | 1a534ae | 2002-01-22 23:26:13 +1100 | [diff] [blame] | 75 | typedef struct identity { |
| 76 | TAILQ_ENTRY(identity) next; |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 77 | Key *key; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 78 | char *comment; |
Ben Lindstrom | 61d328a | 2002-06-06 21:54:57 +0000 | [diff] [blame] | 79 | u_int death; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 80 | } Identity; |
| 81 | |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 82 | typedef struct { |
| 83 | int nentries; |
Damien Miller | 1a534ae | 2002-01-22 23:26:13 +1100 | [diff] [blame] | 84 | TAILQ_HEAD(idqueue, identity) idlist; |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 85 | } Idtab; |
| 86 | |
| 87 | /* private key table, one per protocol version */ |
| 88 | Idtab idtable[3]; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 89 | |
| 90 | int max_fd = 0; |
| 91 | |
| 92 | /* pid of shell == parent of agent */ |
Damien Miller | 166fca8 | 2000-04-20 07:42:21 +1000 | [diff] [blame] | 93 | pid_t parent_pid = -1; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 94 | |
| 95 | /* pathname and directory for AUTH_SOCKET */ |
| 96 | char socket_name[1024]; |
| 97 | char socket_dir[1024]; |
| 98 | |
Ben Lindstrom | 2f71704 | 2002-06-06 21:52:03 +0000 | [diff] [blame] | 99 | /* locking */ |
| 100 | int locked = 0; |
| 101 | char *lock_passwd = NULL; |
| 102 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 103 | #ifdef HAVE___PROGNAME |
| 104 | extern char *__progname; |
Ben Lindstrom | 49a79c0 | 2000-11-17 03:47:20 +0000 | [diff] [blame] | 105 | #else |
| 106 | char *__progname; |
| 107 | #endif |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 108 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 109 | static void |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 110 | idtab_init(void) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 111 | { |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 112 | int i; |
Ben Lindstrom | 1c37c6a | 2001-12-06 18:00:18 +0000 | [diff] [blame] | 113 | for (i = 0; i <=2; i++) { |
Damien Miller | 1a534ae | 2002-01-22 23:26:13 +1100 | [diff] [blame] | 114 | TAILQ_INIT(&idtable[i].idlist); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 115 | idtable[i].nentries = 0; |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | /* return private key table for requested protocol version */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 120 | static Idtab * |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 121 | idtab_lookup(int version) |
| 122 | { |
| 123 | if (version < 1 || version > 2) |
| 124 | fatal("internal error, bad protocol version %d", version); |
| 125 | return &idtable[version]; |
| 126 | } |
| 127 | |
Ben Lindstrom | 61d328a | 2002-06-06 21:54:57 +0000 | [diff] [blame] | 128 | static void |
| 129 | free_identity(Identity *id) |
| 130 | { |
| 131 | key_free(id->key); |
| 132 | xfree(id->comment); |
| 133 | xfree(id); |
| 134 | } |
| 135 | |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 136 | /* return matching private key for given public key */ |
Damien Miller | 1a534ae | 2002-01-22 23:26:13 +1100 | [diff] [blame] | 137 | static Identity * |
| 138 | lookup_identity(Key *key, int version) |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 139 | { |
Damien Miller | 1a534ae | 2002-01-22 23:26:13 +1100 | [diff] [blame] | 140 | Identity *id; |
| 141 | |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 142 | Idtab *tab = idtab_lookup(version); |
Damien Miller | 1a534ae | 2002-01-22 23:26:13 +1100 | [diff] [blame] | 143 | TAILQ_FOREACH(id, &tab->idlist, next) { |
| 144 | if (key_equal(key, id->key)) |
| 145 | return (id); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 146 | } |
Damien Miller | 1a534ae | 2002-01-22 23:26:13 +1100 | [diff] [blame] | 147 | return (NULL); |
| 148 | } |
| 149 | |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 150 | /* send list of supported public keys to 'client' */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 151 | static void |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 152 | process_request_identities(SocketEntry *e, int version) |
| 153 | { |
| 154 | Idtab *tab = idtab_lookup(version); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 155 | Buffer msg; |
Damien Miller | 1a534ae | 2002-01-22 23:26:13 +1100 | [diff] [blame] | 156 | Identity *id; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 157 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 158 | buffer_init(&msg); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 159 | buffer_put_char(&msg, (version == 1) ? |
| 160 | SSH_AGENT_RSA_IDENTITIES_ANSWER : SSH2_AGENT_IDENTITIES_ANSWER); |
| 161 | buffer_put_int(&msg, tab->nentries); |
Damien Miller | 1a534ae | 2002-01-22 23:26:13 +1100 | [diff] [blame] | 162 | TAILQ_FOREACH(id, &tab->idlist, next) { |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 163 | if (id->key->type == KEY_RSA1) { |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 164 | buffer_put_int(&msg, BN_num_bits(id->key->rsa->n)); |
| 165 | buffer_put_bignum(&msg, id->key->rsa->e); |
| 166 | buffer_put_bignum(&msg, id->key->rsa->n); |
| 167 | } else { |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 168 | u_char *blob; |
| 169 | u_int blen; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 170 | key_to_blob(id->key, &blob, &blen); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 171 | buffer_put_string(&msg, blob, blen); |
| 172 | xfree(blob); |
| 173 | } |
| 174 | buffer_put_cstring(&msg, id->comment); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 175 | } |
| 176 | buffer_put_int(&e->output, buffer_len(&msg)); |
| 177 | buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg)); |
| 178 | buffer_free(&msg); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 179 | } |
| 180 | |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 181 | /* ssh1 only */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 182 | static void |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 183 | process_authentication_challenge1(SocketEntry *e) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 184 | { |
Damien Miller | 1a534ae | 2002-01-22 23:26:13 +1100 | [diff] [blame] | 185 | Identity *id; |
| 186 | Key *key; |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 187 | BIGNUM *challenge; |
| 188 | int i, len; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 189 | Buffer msg; |
| 190 | MD5_CTX md; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 191 | u_char buf[32], mdbuf[16], session_id[16]; |
| 192 | u_int response_type; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 193 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 194 | buffer_init(&msg); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 195 | key = key_new(KEY_RSA1); |
Damien Miller | da75516 | 2002-01-22 23:09:22 +1100 | [diff] [blame] | 196 | if ((challenge = BN_new()) == NULL) |
| 197 | fatal("process_authentication_challenge1: BN_new failed"); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 198 | |
Ben Lindstrom | 21d1ed8 | 2002-06-06 21:48:57 +0000 | [diff] [blame] | 199 | buffer_get_int(&e->request); /* ignored */ |
| 200 | buffer_get_bignum(&e->request, key->rsa->e); |
| 201 | buffer_get_bignum(&e->request, key->rsa->n); |
| 202 | buffer_get_bignum(&e->request, challenge); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 203 | |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 204 | /* Only protocol 1.1 is supported */ |
Ben Lindstrom | 21d1ed8 | 2002-06-06 21:48:57 +0000 | [diff] [blame] | 205 | if (buffer_len(&e->request) == 0) |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 206 | goto failure; |
Ben Lindstrom | 21d1ed8 | 2002-06-06 21:48:57 +0000 | [diff] [blame] | 207 | buffer_get(&e->request, session_id, 16); |
| 208 | response_type = buffer_get_int(&e->request); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 209 | if (response_type != 1) |
| 210 | goto failure; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 211 | |
Damien Miller | 1a534ae | 2002-01-22 23:26:13 +1100 | [diff] [blame] | 212 | id = lookup_identity(key, 1); |
| 213 | if (id != NULL) { |
| 214 | Key *private = id->key; |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 215 | /* Decrypt the challenge using the private key. */ |
Damien Miller | 7650bc6 | 2001-01-30 09:27:26 +1100 | [diff] [blame] | 216 | if (rsa_private_decrypt(challenge, challenge, private->rsa) <= 0) |
| 217 | goto failure; |
Damien Miller | fd7c911 | 1999-11-08 16:15:55 +1100 | [diff] [blame] | 218 | |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 219 | /* The response is MD5 of decrypted challenge plus session id. */ |
| 220 | len = BN_num_bytes(challenge); |
| 221 | if (len <= 0 || len > 32) { |
| 222 | log("process_authentication_challenge: bad challenge length %d", len); |
| 223 | goto failure; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 224 | } |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 225 | memset(buf, 0, 32); |
| 226 | BN_bn2bin(challenge, buf + 32 - len); |
| 227 | MD5_Init(&md); |
| 228 | MD5_Update(&md, buf, 32); |
| 229 | MD5_Update(&md, session_id, 16); |
| 230 | MD5_Final(mdbuf, &md); |
| 231 | |
| 232 | /* Send the response. */ |
| 233 | buffer_put_char(&msg, SSH_AGENT_RSA_RESPONSE); |
| 234 | for (i = 0; i < 16; i++) |
| 235 | buffer_put_char(&msg, mdbuf[i]); |
| 236 | goto send; |
| 237 | } |
| 238 | |
| 239 | failure: |
| 240 | /* Unknown identity or protocol error. Send failure. */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 241 | buffer_put_char(&msg, SSH_AGENT_FAILURE); |
| 242 | send: |
| 243 | buffer_put_int(&e->output, buffer_len(&msg)); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 244 | buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg)); |
| 245 | key_free(key); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 246 | BN_clear_free(challenge); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 247 | buffer_free(&msg); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 248 | } |
| 249 | |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 250 | /* ssh2 only */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 251 | static void |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 252 | process_sign_request2(SocketEntry *e) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 253 | { |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 254 | extern int datafellows; |
Damien Miller | 1a534ae | 2002-01-22 23:26:13 +1100 | [diff] [blame] | 255 | Key *key; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 256 | u_char *blob, *data, *signature = NULL; |
| 257 | u_int blen, dlen, slen = 0; |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 258 | int flags; |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 259 | Buffer msg; |
| 260 | int ok = -1; |
| 261 | |
| 262 | datafellows = 0; |
Kevin Steves | fa72dda | 2000-12-15 18:39:12 +0000 | [diff] [blame] | 263 | |
Ben Lindstrom | 21d1ed8 | 2002-06-06 21:48:57 +0000 | [diff] [blame] | 264 | blob = buffer_get_string(&e->request, &blen); |
| 265 | data = buffer_get_string(&e->request, &dlen); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 266 | |
Ben Lindstrom | 21d1ed8 | 2002-06-06 21:48:57 +0000 | [diff] [blame] | 267 | flags = buffer_get_int(&e->request); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 268 | if (flags & SSH_AGENT_OLD_SIGNATURE) |
| 269 | datafellows = SSH_BUG_SIGBLOB; |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 270 | |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 271 | key = key_from_blob(blob, blen); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 272 | if (key != NULL) { |
Damien Miller | 1a534ae | 2002-01-22 23:26:13 +1100 | [diff] [blame] | 273 | Identity *id = lookup_identity(key, 2); |
| 274 | if (id != NULL) |
| 275 | ok = key_sign(id->key, &signature, &slen, data, dlen); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 276 | } |
| 277 | key_free(key); |
| 278 | buffer_init(&msg); |
| 279 | if (ok == 0) { |
| 280 | buffer_put_char(&msg, SSH2_AGENT_SIGN_RESPONSE); |
| 281 | buffer_put_string(&msg, signature, slen); |
| 282 | } else { |
| 283 | buffer_put_char(&msg, SSH_AGENT_FAILURE); |
| 284 | } |
| 285 | buffer_put_int(&e->output, buffer_len(&msg)); |
| 286 | buffer_append(&e->output, buffer_ptr(&msg), |
| 287 | buffer_len(&msg)); |
| 288 | buffer_free(&msg); |
| 289 | xfree(data); |
| 290 | xfree(blob); |
| 291 | if (signature != NULL) |
| 292 | xfree(signature); |
| 293 | } |
| 294 | |
| 295 | /* shared */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 296 | static void |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 297 | process_remove_identity(SocketEntry *e, int version) |
| 298 | { |
Damien Miller | 1a534ae | 2002-01-22 23:26:13 +1100 | [diff] [blame] | 299 | Key *key = NULL; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 300 | u_char *blob; |
| 301 | u_int blen; |
| 302 | u_int bits; |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 303 | int success = 0; |
Damien Miller | 7e8e820 | 1999-11-16 13:37:16 +1100 | [diff] [blame] | 304 | |
Ben Lindstrom | 1c37c6a | 2001-12-06 18:00:18 +0000 | [diff] [blame] | 305 | switch (version) { |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 306 | case 1: |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 307 | key = key_new(KEY_RSA1); |
Ben Lindstrom | 21d1ed8 | 2002-06-06 21:48:57 +0000 | [diff] [blame] | 308 | bits = buffer_get_int(&e->request); |
| 309 | buffer_get_bignum(&e->request, key->rsa->e); |
| 310 | buffer_get_bignum(&e->request, key->rsa->n); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 311 | |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 312 | if (bits != key_size(key)) |
| 313 | log("Warning: identity keysize mismatch: actual %d, announced %d", |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 314 | key_size(key), bits); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 315 | break; |
| 316 | case 2: |
Ben Lindstrom | 21d1ed8 | 2002-06-06 21:48:57 +0000 | [diff] [blame] | 317 | blob = buffer_get_string(&e->request, &blen); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 318 | key = key_from_blob(blob, blen); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 319 | xfree(blob); |
| 320 | break; |
| 321 | } |
| 322 | if (key != NULL) { |
Damien Miller | 1a534ae | 2002-01-22 23:26:13 +1100 | [diff] [blame] | 323 | Identity *id = lookup_identity(key, version); |
| 324 | if (id != NULL) { |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 325 | /* |
| 326 | * We have this key. Free the old key. Since we |
| 327 | * don\'t want to leave empty slots in the middle of |
Ben Lindstrom | 1492029 | 2000-11-21 21:24:55 +0000 | [diff] [blame] | 328 | * the array, we actually free the key there and move |
| 329 | * all the entries between the empty slot and the end |
| 330 | * of the array. |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 331 | */ |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 332 | Idtab *tab = idtab_lookup(version); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 333 | if (tab->nentries < 1) |
| 334 | fatal("process_remove_identity: " |
| 335 | "internal error: tab->nentries %d", |
| 336 | tab->nentries); |
Damien Miller | 1a534ae | 2002-01-22 23:26:13 +1100 | [diff] [blame] | 337 | TAILQ_REMOVE(&tab->idlist, id, next); |
| 338 | free_identity(id); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 339 | tab->nentries--; |
| 340 | success = 1; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 341 | } |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 342 | key_free(key); |
| 343 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 344 | buffer_put_int(&e->output, 1); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 345 | buffer_put_char(&e->output, |
| 346 | success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 347 | } |
| 348 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 349 | static void |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 350 | process_remove_all_identities(SocketEntry *e, int version) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 351 | { |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 352 | Idtab *tab = idtab_lookup(version); |
Damien Miller | 1a534ae | 2002-01-22 23:26:13 +1100 | [diff] [blame] | 353 | Identity *id; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 354 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 355 | /* Loop over all identities and clear the keys. */ |
Damien Miller | 1a534ae | 2002-01-22 23:26:13 +1100 | [diff] [blame] | 356 | for (id = TAILQ_FIRST(&tab->idlist); id; |
| 357 | id = TAILQ_FIRST(&tab->idlist)) { |
| 358 | TAILQ_REMOVE(&tab->idlist, id, next); |
| 359 | free_identity(id); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 360 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 361 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 362 | /* Mark that there are no identities. */ |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 363 | tab->nentries = 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 364 | |
| 365 | /* Send success. */ |
| 366 | buffer_put_int(&e->output, 1); |
| 367 | buffer_put_char(&e->output, SSH_AGENT_SUCCESS); |
| 368 | return; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 369 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 370 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 371 | static void |
Ben Lindstrom | 61d328a | 2002-06-06 21:54:57 +0000 | [diff] [blame] | 372 | reaper(void) |
| 373 | { |
| 374 | Idtab *tab; |
| 375 | Identity *id, *nxt; |
| 376 | int version; |
| 377 | u_int now = time(NULL); |
| 378 | |
| 379 | for (version = 1; version < 3; version++) { |
| 380 | tab = idtab_lookup(version); |
| 381 | for (id = TAILQ_FIRST(&tab->idlist); id; id = nxt) { |
| 382 | nxt = TAILQ_NEXT(id, next); |
| 383 | if (id->death != 0 && now >= id->death) { |
| 384 | TAILQ_REMOVE(&tab->idlist, id, next); |
| 385 | free_identity(id); |
| 386 | tab->nentries--; |
| 387 | } |
| 388 | } |
| 389 | } |
| 390 | } |
| 391 | |
| 392 | static void |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 393 | process_add_identity(SocketEntry *e, int version) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 394 | { |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 395 | Key *k = NULL; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 396 | char *type_name; |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 397 | char *comment; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 398 | int type, success = 0; |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 399 | Idtab *tab = idtab_lookup(version); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 400 | |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 401 | switch (version) { |
| 402 | case 1: |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 403 | k = key_new_private(KEY_RSA1); |
Ben Lindstrom | 21d1ed8 | 2002-06-06 21:48:57 +0000 | [diff] [blame] | 404 | buffer_get_int(&e->request); /* ignored */ |
| 405 | buffer_get_bignum(&e->request, k->rsa->n); |
| 406 | buffer_get_bignum(&e->request, k->rsa->e); |
| 407 | buffer_get_bignum(&e->request, k->rsa->d); |
| 408 | buffer_get_bignum(&e->request, k->rsa->iqmp); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 409 | |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 410 | /* SSH and SSL have p and q swapped */ |
Ben Lindstrom | 21d1ed8 | 2002-06-06 21:48:57 +0000 | [diff] [blame] | 411 | buffer_get_bignum(&e->request, k->rsa->q); /* p */ |
| 412 | buffer_get_bignum(&e->request, k->rsa->p); /* q */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 413 | |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 414 | /* Generate additional parameters */ |
Ben Lindstrom | f7297dd | 2001-07-04 05:02:23 +0000 | [diff] [blame] | 415 | rsa_generate_additional_parameters(k->rsa); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 416 | break; |
| 417 | case 2: |
Ben Lindstrom | 21d1ed8 | 2002-06-06 21:48:57 +0000 | [diff] [blame] | 418 | type_name = buffer_get_string(&e->request, NULL); |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 419 | type = key_type_from_name(type_name); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 420 | xfree(type_name); |
Ben Lindstrom | 1c37c6a | 2001-12-06 18:00:18 +0000 | [diff] [blame] | 421 | switch (type) { |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 422 | case KEY_DSA: |
| 423 | k = key_new_private(type); |
Ben Lindstrom | 21d1ed8 | 2002-06-06 21:48:57 +0000 | [diff] [blame] | 424 | buffer_get_bignum2(&e->request, k->dsa->p); |
| 425 | buffer_get_bignum2(&e->request, k->dsa->q); |
| 426 | buffer_get_bignum2(&e->request, k->dsa->g); |
| 427 | buffer_get_bignum2(&e->request, k->dsa->pub_key); |
| 428 | buffer_get_bignum2(&e->request, k->dsa->priv_key); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 429 | break; |
| 430 | case KEY_RSA: |
| 431 | k = key_new_private(type); |
Ben Lindstrom | 21d1ed8 | 2002-06-06 21:48:57 +0000 | [diff] [blame] | 432 | buffer_get_bignum2(&e->request, k->rsa->n); |
| 433 | buffer_get_bignum2(&e->request, k->rsa->e); |
| 434 | buffer_get_bignum2(&e->request, k->rsa->d); |
| 435 | buffer_get_bignum2(&e->request, k->rsa->iqmp); |
| 436 | buffer_get_bignum2(&e->request, k->rsa->p); |
| 437 | buffer_get_bignum2(&e->request, k->rsa->q); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 438 | |
| 439 | /* Generate additional parameters */ |
Ben Lindstrom | f7297dd | 2001-07-04 05:02:23 +0000 | [diff] [blame] | 440 | rsa_generate_additional_parameters(k->rsa); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 441 | break; |
| 442 | default: |
Ben Lindstrom | 21d1ed8 | 2002-06-06 21:48:57 +0000 | [diff] [blame] | 443 | buffer_clear(&e->request); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 444 | goto send; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 445 | } |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 446 | break; |
| 447 | } |
Ben Lindstrom | 21d1ed8 | 2002-06-06 21:48:57 +0000 | [diff] [blame] | 448 | comment = buffer_get_string(&e->request, NULL); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 449 | if (k == NULL) { |
| 450 | xfree(comment); |
| 451 | goto send; |
| 452 | } |
| 453 | success = 1; |
Damien Miller | 1a534ae | 2002-01-22 23:26:13 +1100 | [diff] [blame] | 454 | if (lookup_identity(k, version) == NULL) { |
| 455 | Identity *id = xmalloc(sizeof(Identity)); |
| 456 | id->key = k; |
| 457 | id->comment = comment; |
Ben Lindstrom | 61d328a | 2002-06-06 21:54:57 +0000 | [diff] [blame] | 458 | id->death = 0; |
Damien Miller | 1a534ae | 2002-01-22 23:26:13 +1100 | [diff] [blame] | 459 | TAILQ_INSERT_TAIL(&tab->idlist, id, next); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 460 | /* Increment the number of identities. */ |
| 461 | tab->nentries++; |
| 462 | } else { |
| 463 | key_free(k); |
| 464 | xfree(comment); |
| 465 | } |
| 466 | send: |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 467 | buffer_put_int(&e->output, 1); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 468 | buffer_put_char(&e->output, |
| 469 | success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 470 | } |
| 471 | |
Ben Lindstrom | 61d328a | 2002-06-06 21:54:57 +0000 | [diff] [blame] | 472 | static void |
| 473 | process_lifetime_identity(SocketEntry *e, int version) |
| 474 | { |
| 475 | Key *key = NULL; |
| 476 | u_char *blob; |
| 477 | u_int blen, bits, death; |
| 478 | int success = 0; |
| 479 | |
| 480 | death = time(NULL) + buffer_get_int(&e->request); |
| 481 | |
| 482 | switch (version) { |
| 483 | case 1: |
| 484 | key = key_new(KEY_RSA1); |
| 485 | bits = buffer_get_int(&e->request); |
| 486 | buffer_get_bignum(&e->request, key->rsa->e); |
| 487 | buffer_get_bignum(&e->request, key->rsa->n); |
| 488 | |
| 489 | break; |
| 490 | case 2: |
| 491 | blob = buffer_get_string(&e->request, &blen); |
| 492 | key = key_from_blob(blob, blen); |
| 493 | xfree(blob); |
| 494 | break; |
| 495 | } |
| 496 | if (key != NULL) { |
| 497 | Identity *id = lookup_identity(key, version); |
| 498 | if (id != NULL && id->death == 0) { |
| 499 | id->death = death; |
| 500 | success = 1; |
| 501 | } |
| 502 | key_free(key); |
| 503 | } |
| 504 | buffer_put_int(&e->output, 1); |
| 505 | buffer_put_char(&e->output, |
| 506 | success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE); |
| 507 | } |
| 508 | |
Ben Lindstrom | 2f71704 | 2002-06-06 21:52:03 +0000 | [diff] [blame] | 509 | /* XXX todo: encrypt sensitive data with passphrase */ |
| 510 | static void |
| 511 | process_lock_agent(SocketEntry *e, int lock) |
| 512 | { |
| 513 | char *passwd; |
| 514 | int success = 0; |
| 515 | |
| 516 | passwd = buffer_get_string(&e->request, NULL); |
| 517 | if (locked && !lock && strcmp(passwd, lock_passwd) == 0) { |
| 518 | locked = 0; |
| 519 | memset(lock_passwd, 0, strlen(lock_passwd)); |
| 520 | xfree(lock_passwd); |
| 521 | lock_passwd = NULL; |
| 522 | success = 1; |
| 523 | } else if (!locked && lock) { |
| 524 | locked = 1; |
| 525 | lock_passwd = xstrdup(passwd); |
| 526 | success = 1; |
| 527 | } |
| 528 | memset(passwd, 0, strlen(passwd)); |
| 529 | xfree(passwd); |
| 530 | |
| 531 | buffer_put_int(&e->output, 1); |
| 532 | buffer_put_char(&e->output, |
| 533 | success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE); |
| 534 | return; |
| 535 | } |
| 536 | |
| 537 | static void |
| 538 | no_identities(SocketEntry *e, u_int type) |
| 539 | { |
| 540 | Buffer msg; |
| 541 | |
| 542 | buffer_init(&msg); |
| 543 | buffer_put_char(&msg, |
| 544 | (type == SSH_AGENTC_REQUEST_RSA_IDENTITIES) ? |
| 545 | SSH_AGENT_RSA_IDENTITIES_ANSWER : SSH2_AGENT_IDENTITIES_ANSWER); |
| 546 | buffer_put_int(&msg, 0); |
| 547 | buffer_put_int(&e->output, buffer_len(&msg)); |
| 548 | buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg)); |
| 549 | buffer_free(&msg); |
| 550 | } |
Ben Lindstrom | 3f47163 | 2001-07-04 03:53:15 +0000 | [diff] [blame] | 551 | |
| 552 | #ifdef SMARTCARD |
| 553 | static void |
| 554 | process_add_smartcard_key (SocketEntry *e) |
| 555 | { |
Ben Lindstrom | 0936a5b | 2002-03-26 03:17:42 +0000 | [diff] [blame] | 556 | Identity *id; |
Ben Lindstrom | 3f47163 | 2001-07-04 03:53:15 +0000 | [diff] [blame] | 557 | Idtab *tab; |
Ben Lindstrom | 0936a5b | 2002-03-26 03:17:42 +0000 | [diff] [blame] | 558 | Key **keys, *k; |
Ben Lindstrom | ba72d30 | 2002-03-22 03:51:06 +0000 | [diff] [blame] | 559 | char *sc_reader_id = NULL, *pin; |
Ben Lindstrom | 0936a5b | 2002-03-26 03:17:42 +0000 | [diff] [blame] | 560 | int i, version, success = 0; |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 561 | |
Ben Lindstrom | 21d1ed8 | 2002-06-06 21:48:57 +0000 | [diff] [blame] | 562 | sc_reader_id = buffer_get_string(&e->request, NULL); |
| 563 | pin = buffer_get_string(&e->request, NULL); |
Ben Lindstrom | 0936a5b | 2002-03-26 03:17:42 +0000 | [diff] [blame] | 564 | keys = sc_get_keys(sc_reader_id, pin); |
Ben Lindstrom | f7db3bb | 2001-08-06 21:35:51 +0000 | [diff] [blame] | 565 | xfree(sc_reader_id); |
Ben Lindstrom | ba72d30 | 2002-03-22 03:51:06 +0000 | [diff] [blame] | 566 | xfree(pin); |
Ben Lindstrom | 3f47163 | 2001-07-04 03:53:15 +0000 | [diff] [blame] | 567 | |
Ben Lindstrom | 0936a5b | 2002-03-26 03:17:42 +0000 | [diff] [blame] | 568 | if (keys == NULL || keys[0] == NULL) { |
| 569 | error("sc_get_keys failed"); |
Ben Lindstrom | 3f47163 | 2001-07-04 03:53:15 +0000 | [diff] [blame] | 570 | goto send; |
| 571 | } |
Ben Lindstrom | 0936a5b | 2002-03-26 03:17:42 +0000 | [diff] [blame] | 572 | for (i = 0; keys[i] != NULL; i++) { |
| 573 | k = keys[i]; |
| 574 | version = k->type == KEY_RSA1 ? 1 : 2; |
| 575 | tab = idtab_lookup(version); |
| 576 | if (lookup_identity(k, version) == NULL) { |
| 577 | id = xmalloc(sizeof(Identity)); |
| 578 | id->key = k; |
| 579 | id->comment = xstrdup("smartcard key"); |
Ben Lindstrom | 61d328a | 2002-06-06 21:54:57 +0000 | [diff] [blame] | 580 | id->death = 0; |
Ben Lindstrom | 0936a5b | 2002-03-26 03:17:42 +0000 | [diff] [blame] | 581 | TAILQ_INSERT_TAIL(&tab->idlist, id, next); |
| 582 | tab->nentries++; |
| 583 | success = 1; |
| 584 | } else { |
| 585 | key_free(k); |
| 586 | } |
| 587 | keys[i] = NULL; |
Ben Lindstrom | 3f47163 | 2001-07-04 03:53:15 +0000 | [diff] [blame] | 588 | } |
Ben Lindstrom | 0936a5b | 2002-03-26 03:17:42 +0000 | [diff] [blame] | 589 | xfree(keys); |
Ben Lindstrom | 3f47163 | 2001-07-04 03:53:15 +0000 | [diff] [blame] | 590 | send: |
| 591 | buffer_put_int(&e->output, 1); |
| 592 | buffer_put_char(&e->output, |
| 593 | success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE); |
| 594 | } |
| 595 | |
| 596 | static void |
| 597 | process_remove_smartcard_key(SocketEntry *e) |
| 598 | { |
Ben Lindstrom | 0936a5b | 2002-03-26 03:17:42 +0000 | [diff] [blame] | 599 | Identity *id; |
| 600 | Idtab *tab; |
| 601 | Key **keys, *k = NULL; |
Ben Lindstrom | ba72d30 | 2002-03-22 03:51:06 +0000 | [diff] [blame] | 602 | char *sc_reader_id = NULL, *pin; |
Ben Lindstrom | 0936a5b | 2002-03-26 03:17:42 +0000 | [diff] [blame] | 603 | int i, version, success = 0; |
Ben Lindstrom | 3f47163 | 2001-07-04 03:53:15 +0000 | [diff] [blame] | 604 | |
Ben Lindstrom | 21d1ed8 | 2002-06-06 21:48:57 +0000 | [diff] [blame] | 605 | sc_reader_id = buffer_get_string(&e->request, NULL); |
| 606 | pin = buffer_get_string(&e->request, NULL); |
Ben Lindstrom | 0936a5b | 2002-03-26 03:17:42 +0000 | [diff] [blame] | 607 | keys = sc_get_keys(sc_reader_id, pin); |
Ben Lindstrom | f7db3bb | 2001-08-06 21:35:51 +0000 | [diff] [blame] | 608 | xfree(sc_reader_id); |
Ben Lindstrom | ba72d30 | 2002-03-22 03:51:06 +0000 | [diff] [blame] | 609 | xfree(pin); |
Ben Lindstrom | 3f47163 | 2001-07-04 03:53:15 +0000 | [diff] [blame] | 610 | |
Ben Lindstrom | 0936a5b | 2002-03-26 03:17:42 +0000 | [diff] [blame] | 611 | if (keys == NULL || keys[0] == NULL) { |
| 612 | error("sc_get_keys failed"); |
| 613 | goto send; |
| 614 | } |
| 615 | for (i = 0; keys[i] != NULL; i++) { |
| 616 | k = keys[i]; |
| 617 | version = k->type == KEY_RSA1 ? 1 : 2; |
| 618 | if ((id = lookup_identity(k, version)) != NULL) { |
| 619 | tab = idtab_lookup(version); |
Ben Lindstrom | 5a6abda | 2002-06-09 19:41:48 +0000 | [diff] [blame] | 620 | TAILQ_REMOVE(&tab->idlist, id, next); |
Ben Lindstrom | 3f47163 | 2001-07-04 03:53:15 +0000 | [diff] [blame] | 621 | tab->nentries--; |
Damien Miller | 1a534ae | 2002-01-22 23:26:13 +1100 | [diff] [blame] | 622 | free_identity(id); |
Ben Lindstrom | 3f47163 | 2001-07-04 03:53:15 +0000 | [diff] [blame] | 623 | success = 1; |
| 624 | } |
| 625 | key_free(k); |
Ben Lindstrom | 0936a5b | 2002-03-26 03:17:42 +0000 | [diff] [blame] | 626 | keys[i] = NULL; |
Ben Lindstrom | 3f47163 | 2001-07-04 03:53:15 +0000 | [diff] [blame] | 627 | } |
Ben Lindstrom | 0936a5b | 2002-03-26 03:17:42 +0000 | [diff] [blame] | 628 | xfree(keys); |
| 629 | send: |
Ben Lindstrom | 3f47163 | 2001-07-04 03:53:15 +0000 | [diff] [blame] | 630 | buffer_put_int(&e->output, 1); |
| 631 | buffer_put_char(&e->output, |
| 632 | success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE); |
| 633 | } |
Ben Lindstrom | ffce147 | 2001-08-06 21:57:31 +0000 | [diff] [blame] | 634 | #endif /* SMARTCARD */ |
Ben Lindstrom | 3f47163 | 2001-07-04 03:53:15 +0000 | [diff] [blame] | 635 | |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 636 | /* dispatch incoming messages */ |
| 637 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 638 | static void |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 639 | process_message(SocketEntry *e) |
| 640 | { |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 641 | u_int msg_len; |
| 642 | u_int type; |
| 643 | u_char *cp; |
Ben Lindstrom | 61d328a | 2002-06-06 21:54:57 +0000 | [diff] [blame] | 644 | |
| 645 | /* kill dead keys */ |
| 646 | reaper(); |
| 647 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 648 | if (buffer_len(&e->input) < 5) |
| 649 | return; /* Incomplete message. */ |
Damien Miller | 708d21c | 2002-01-22 23:18:15 +1100 | [diff] [blame] | 650 | cp = buffer_ptr(&e->input); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 651 | msg_len = GET_32BIT(cp); |
| 652 | if (msg_len > 256 * 1024) { |
| 653 | shutdown(e->fd, SHUT_RDWR); |
| 654 | close(e->fd); |
| 655 | e->type = AUTH_UNUSED; |
Ben Lindstrom | 21d1ed8 | 2002-06-06 21:48:57 +0000 | [diff] [blame] | 656 | buffer_free(&e->input); |
| 657 | buffer_free(&e->output); |
| 658 | buffer_free(&e->request); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 659 | return; |
| 660 | } |
| 661 | if (buffer_len(&e->input) < msg_len + 4) |
| 662 | return; |
Ben Lindstrom | 21d1ed8 | 2002-06-06 21:48:57 +0000 | [diff] [blame] | 663 | |
| 664 | /* move the current input to e->request */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 665 | buffer_consume(&e->input, 4); |
Ben Lindstrom | 21d1ed8 | 2002-06-06 21:48:57 +0000 | [diff] [blame] | 666 | buffer_clear(&e->request); |
| 667 | buffer_append(&e->request, buffer_ptr(&e->input), msg_len); |
| 668 | buffer_consume(&e->input, msg_len); |
| 669 | type = buffer_get_char(&e->request); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 670 | |
Ben Lindstrom | 2f71704 | 2002-06-06 21:52:03 +0000 | [diff] [blame] | 671 | /* check wheter agent is locked */ |
| 672 | if (locked && type != SSH_AGENTC_UNLOCK) { |
| 673 | buffer_clear(&e->request); |
| 674 | switch (type) { |
| 675 | case SSH_AGENTC_REQUEST_RSA_IDENTITIES: |
| 676 | case SSH2_AGENTC_REQUEST_IDENTITIES: |
| 677 | /* send empty lists */ |
| 678 | no_identities(e, type); |
| 679 | break; |
| 680 | default: |
| 681 | /* send a fail message for all other request types */ |
| 682 | buffer_put_int(&e->output, 1); |
| 683 | buffer_put_char(&e->output, SSH_AGENT_FAILURE); |
| 684 | } |
| 685 | return; |
| 686 | } |
| 687 | |
Ben Lindstrom | 3f47163 | 2001-07-04 03:53:15 +0000 | [diff] [blame] | 688 | debug("type %d", type); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 689 | switch (type) { |
Ben Lindstrom | 2f71704 | 2002-06-06 21:52:03 +0000 | [diff] [blame] | 690 | case SSH_AGENTC_LOCK: |
| 691 | case SSH_AGENTC_UNLOCK: |
| 692 | process_lock_agent(e, type == SSH_AGENTC_LOCK); |
| 693 | break; |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 694 | /* ssh1 */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 695 | case SSH_AGENTC_RSA_CHALLENGE: |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 696 | process_authentication_challenge1(e); |
| 697 | break; |
| 698 | case SSH_AGENTC_REQUEST_RSA_IDENTITIES: |
| 699 | process_request_identities(e, 1); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 700 | break; |
| 701 | case SSH_AGENTC_ADD_RSA_IDENTITY: |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 702 | process_add_identity(e, 1); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 703 | break; |
| 704 | case SSH_AGENTC_REMOVE_RSA_IDENTITY: |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 705 | process_remove_identity(e, 1); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 706 | break; |
| 707 | case SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES: |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 708 | process_remove_all_identities(e, 1); |
| 709 | break; |
Ben Lindstrom | 61d328a | 2002-06-06 21:54:57 +0000 | [diff] [blame] | 710 | case SSH_AGENTC_LIFETIME_IDENTITY1: |
| 711 | process_lifetime_identity(e, 1); |
| 712 | break; |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 713 | /* ssh2 */ |
| 714 | case SSH2_AGENTC_SIGN_REQUEST: |
| 715 | process_sign_request2(e); |
| 716 | break; |
| 717 | case SSH2_AGENTC_REQUEST_IDENTITIES: |
| 718 | process_request_identities(e, 2); |
| 719 | break; |
| 720 | case SSH2_AGENTC_ADD_IDENTITY: |
| 721 | process_add_identity(e, 2); |
| 722 | break; |
| 723 | case SSH2_AGENTC_REMOVE_IDENTITY: |
| 724 | process_remove_identity(e, 2); |
| 725 | break; |
| 726 | case SSH2_AGENTC_REMOVE_ALL_IDENTITIES: |
| 727 | process_remove_all_identities(e, 2); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 728 | break; |
Ben Lindstrom | 61d328a | 2002-06-06 21:54:57 +0000 | [diff] [blame] | 729 | case SSH_AGENTC_LIFETIME_IDENTITY: |
| 730 | process_lifetime_identity(e, 2); |
| 731 | break; |
Ben Lindstrom | 3f47163 | 2001-07-04 03:53:15 +0000 | [diff] [blame] | 732 | #ifdef SMARTCARD |
| 733 | case SSH_AGENTC_ADD_SMARTCARD_KEY: |
| 734 | process_add_smartcard_key(e); |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 735 | break; |
Ben Lindstrom | 3f47163 | 2001-07-04 03:53:15 +0000 | [diff] [blame] | 736 | case SSH_AGENTC_REMOVE_SMARTCARD_KEY: |
| 737 | process_remove_smartcard_key(e); |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 738 | break; |
Ben Lindstrom | ffce147 | 2001-08-06 21:57:31 +0000 | [diff] [blame] | 739 | #endif /* SMARTCARD */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 740 | default: |
| 741 | /* Unknown message. Respond with failure. */ |
| 742 | error("Unknown message %d", type); |
Ben Lindstrom | 21d1ed8 | 2002-06-06 21:48:57 +0000 | [diff] [blame] | 743 | buffer_clear(&e->request); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 744 | buffer_put_int(&e->output, 1); |
| 745 | buffer_put_char(&e->output, SSH_AGENT_FAILURE); |
| 746 | break; |
| 747 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 748 | } |
| 749 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 750 | static void |
Ben Lindstrom | 65366a8 | 2001-12-06 16:32:47 +0000 | [diff] [blame] | 751 | new_socket(sock_type type, int fd) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 752 | { |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 753 | u_int i, old_alloc; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 754 | if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0) |
| 755 | error("fcntl O_NONBLOCK: %s", strerror(errno)); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 756 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 757 | if (fd > max_fd) |
| 758 | max_fd = fd; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 759 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 760 | for (i = 0; i < sockets_alloc; i++) |
| 761 | if (sockets[i].type == AUTH_UNUSED) { |
| 762 | sockets[i].fd = fd; |
| 763 | sockets[i].type = type; |
| 764 | buffer_init(&sockets[i].input); |
| 765 | buffer_init(&sockets[i].output); |
Ben Lindstrom | 21d1ed8 | 2002-06-06 21:48:57 +0000 | [diff] [blame] | 766 | buffer_init(&sockets[i].request); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 767 | return; |
| 768 | } |
| 769 | old_alloc = sockets_alloc; |
| 770 | sockets_alloc += 10; |
| 771 | if (sockets) |
| 772 | sockets = xrealloc(sockets, sockets_alloc * sizeof(sockets[0])); |
| 773 | else |
| 774 | sockets = xmalloc(sockets_alloc * sizeof(sockets[0])); |
| 775 | for (i = old_alloc; i < sockets_alloc; i++) |
| 776 | sockets[i].type = AUTH_UNUSED; |
| 777 | sockets[old_alloc].type = type; |
| 778 | sockets[old_alloc].fd = fd; |
| 779 | buffer_init(&sockets[old_alloc].input); |
| 780 | buffer_init(&sockets[old_alloc].output); |
Ben Lindstrom | 21d1ed8 | 2002-06-06 21:48:57 +0000 | [diff] [blame] | 781 | buffer_init(&sockets[old_alloc].request); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 782 | } |
| 783 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 784 | static int |
Ben Lindstrom | a3d5a4c | 2001-07-18 15:58:08 +0000 | [diff] [blame] | 785 | prepare_select(fd_set **fdrp, fd_set **fdwp, int *fdl, int *nallocp) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 786 | { |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 787 | u_int i, sz; |
| 788 | int n = 0; |
| 789 | |
| 790 | for (i = 0; i < sockets_alloc; i++) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 791 | switch (sockets[i].type) { |
| 792 | case AUTH_SOCKET: |
| 793 | case AUTH_CONNECTION: |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 794 | n = MAX(n, sockets[i].fd); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 795 | break; |
| 796 | case AUTH_UNUSED: |
| 797 | break; |
| 798 | default: |
| 799 | fatal("Unknown socket type %d", sockets[i].type); |
| 800 | break; |
| 801 | } |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 802 | } |
| 803 | |
| 804 | sz = howmany(n+1, NFDBITS) * sizeof(fd_mask); |
Ben Lindstrom | a3d5a4c | 2001-07-18 15:58:08 +0000 | [diff] [blame] | 805 | if (*fdrp == NULL || sz > *nallocp) { |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 806 | if (*fdrp) |
Ben Lindstrom | 86ebcb6 | 2001-04-04 01:53:20 +0000 | [diff] [blame] | 807 | xfree(*fdrp); |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 808 | if (*fdwp) |
Ben Lindstrom | 86ebcb6 | 2001-04-04 01:53:20 +0000 | [diff] [blame] | 809 | xfree(*fdwp); |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 810 | *fdrp = xmalloc(sz); |
| 811 | *fdwp = xmalloc(sz); |
Ben Lindstrom | a3d5a4c | 2001-07-18 15:58:08 +0000 | [diff] [blame] | 812 | *nallocp = sz; |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 813 | } |
Ben Lindstrom | a3d5a4c | 2001-07-18 15:58:08 +0000 | [diff] [blame] | 814 | if (n < *fdl) |
| 815 | debug("XXX shrink: %d < %d", n, *fdl); |
| 816 | *fdl = n; |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 817 | memset(*fdrp, 0, sz); |
| 818 | memset(*fdwp, 0, sz); |
| 819 | |
| 820 | for (i = 0; i < sockets_alloc; i++) { |
| 821 | switch (sockets[i].type) { |
| 822 | case AUTH_SOCKET: |
| 823 | case AUTH_CONNECTION: |
| 824 | FD_SET(sockets[i].fd, *fdrp); |
| 825 | if (buffer_len(&sockets[i].output) > 0) |
| 826 | FD_SET(sockets[i].fd, *fdwp); |
| 827 | break; |
| 828 | default: |
| 829 | break; |
| 830 | } |
| 831 | } |
| 832 | return (1); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 833 | } |
| 834 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 835 | static void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 836 | after_select(fd_set *readset, fd_set *writeset) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 837 | { |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 838 | u_int i; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 839 | int len, sock; |
Damien Miller | 7684ee1 | 2000-03-17 23:40:15 +1100 | [diff] [blame] | 840 | socklen_t slen; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 841 | char buf[1024]; |
| 842 | struct sockaddr_un sunaddr; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 843 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 844 | for (i = 0; i < sockets_alloc; i++) |
| 845 | switch (sockets[i].type) { |
| 846 | case AUTH_UNUSED: |
| 847 | break; |
| 848 | case AUTH_SOCKET: |
| 849 | if (FD_ISSET(sockets[i].fd, readset)) { |
Damien Miller | 7684ee1 | 2000-03-17 23:40:15 +1100 | [diff] [blame] | 850 | slen = sizeof(sunaddr); |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 851 | sock = accept(sockets[i].fd, |
| 852 | (struct sockaddr *) &sunaddr, &slen); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 853 | if (sock < 0) { |
Damien Miller | c653b89 | 2002-02-08 22:05:41 +1100 | [diff] [blame] | 854 | error("accept from AUTH_SOCKET: %s", |
| 855 | strerror(errno)); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 856 | break; |
| 857 | } |
| 858 | new_socket(AUTH_CONNECTION, sock); |
| 859 | } |
| 860 | break; |
| 861 | case AUTH_CONNECTION: |
| 862 | if (buffer_len(&sockets[i].output) > 0 && |
| 863 | FD_ISSET(sockets[i].fd, writeset)) { |
Ben Lindstrom | b3144e5 | 2001-03-06 03:31:34 +0000 | [diff] [blame] | 864 | do { |
| 865 | len = write(sockets[i].fd, |
| 866 | buffer_ptr(&sockets[i].output), |
| 867 | buffer_len(&sockets[i].output)); |
| 868 | if (len == -1 && (errno == EAGAIN || |
| 869 | errno == EINTR)) |
| 870 | continue; |
| 871 | break; |
| 872 | } while (1); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 873 | if (len <= 0) { |
| 874 | shutdown(sockets[i].fd, SHUT_RDWR); |
| 875 | close(sockets[i].fd); |
| 876 | sockets[i].type = AUTH_UNUSED; |
Damien Miller | a552faf | 2000-04-21 15:55:20 +1000 | [diff] [blame] | 877 | buffer_free(&sockets[i].input); |
| 878 | buffer_free(&sockets[i].output); |
Ben Lindstrom | 21d1ed8 | 2002-06-06 21:48:57 +0000 | [diff] [blame] | 879 | buffer_free(&sockets[i].request); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 880 | break; |
| 881 | } |
| 882 | buffer_consume(&sockets[i].output, len); |
| 883 | } |
| 884 | if (FD_ISSET(sockets[i].fd, readset)) { |
Ben Lindstrom | b3144e5 | 2001-03-06 03:31:34 +0000 | [diff] [blame] | 885 | do { |
| 886 | len = read(sockets[i].fd, buf, sizeof(buf)); |
| 887 | if (len == -1 && (errno == EAGAIN || |
| 888 | errno == EINTR)) |
| 889 | continue; |
| 890 | break; |
| 891 | } while (1); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 892 | if (len <= 0) { |
| 893 | shutdown(sockets[i].fd, SHUT_RDWR); |
| 894 | close(sockets[i].fd); |
| 895 | sockets[i].type = AUTH_UNUSED; |
Damien Miller | a552faf | 2000-04-21 15:55:20 +1000 | [diff] [blame] | 896 | buffer_free(&sockets[i].input); |
| 897 | buffer_free(&sockets[i].output); |
Ben Lindstrom | 21d1ed8 | 2002-06-06 21:48:57 +0000 | [diff] [blame] | 898 | buffer_free(&sockets[i].request); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 899 | break; |
| 900 | } |
| 901 | buffer_append(&sockets[i].input, buf, len); |
| 902 | process_message(&sockets[i]); |
| 903 | } |
| 904 | break; |
| 905 | default: |
| 906 | fatal("Unknown type %d", sockets[i].type); |
| 907 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 908 | } |
| 909 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 910 | static void |
Damien Miller | c653b89 | 2002-02-08 22:05:41 +1100 | [diff] [blame] | 911 | cleanup_socket(void *p) |
Damien Miller | 792c511 | 1999-10-29 09:47:09 +1000 | [diff] [blame] | 912 | { |
Ben Lindstrom | 77808ab | 2001-01-26 05:10:34 +0000 | [diff] [blame] | 913 | if (socket_name[0]) |
| 914 | unlink(socket_name); |
| 915 | if (socket_dir[0]) |
| 916 | rmdir(socket_dir); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 917 | } |
| 918 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 919 | static void |
Damien Miller | 792c511 | 1999-10-29 09:47:09 +1000 | [diff] [blame] | 920 | cleanup_exit(int i) |
| 921 | { |
Damien Miller | c653b89 | 2002-02-08 22:05:41 +1100 | [diff] [blame] | 922 | cleanup_socket(NULL); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 923 | exit(i); |
Damien Miller | 792c511 | 1999-10-29 09:47:09 +1000 | [diff] [blame] | 924 | } |
| 925 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 926 | static void |
Ben Lindstrom | 77808ab | 2001-01-26 05:10:34 +0000 | [diff] [blame] | 927 | cleanup_handler(int sig) |
| 928 | { |
Damien Miller | c653b89 | 2002-02-08 22:05:41 +1100 | [diff] [blame] | 929 | cleanup_socket(NULL); |
Ben Lindstrom | 77808ab | 2001-01-26 05:10:34 +0000 | [diff] [blame] | 930 | _exit(2); |
| 931 | } |
| 932 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 933 | static void |
Ben Lindstrom | 0250da0 | 2001-07-22 20:44:00 +0000 | [diff] [blame] | 934 | check_parent_exists(int sig) |
| 935 | { |
| 936 | int save_errno = errno; |
| 937 | |
| 938 | if (parent_pid != -1 && kill(parent_pid, 0) < 0) { |
| 939 | /* printf("Parent has died - Authentication agent exiting.\n"); */ |
| 940 | cleanup_handler(sig); /* safe */ |
| 941 | } |
| 942 | signal(SIGALRM, check_parent_exists); |
| 943 | alarm(10); |
| 944 | errno = save_errno; |
| 945 | } |
| 946 | |
| 947 | static void |
Ben Lindstrom | 28072eb | 2001-02-10 23:13:41 +0000 | [diff] [blame] | 948 | usage(void) |
Damien Miller | 792c511 | 1999-10-29 09:47:09 +1000 | [diff] [blame] | 949 | { |
Ben Lindstrom | ddfb1e3 | 2001-08-06 22:06:35 +0000 | [diff] [blame] | 950 | fprintf(stderr, "Usage: %s [options] [command [args ...]]\n", |
Kevin Steves | 2926586 | 2001-01-24 14:06:28 +0000 | [diff] [blame] | 951 | __progname); |
Ben Lindstrom | ddfb1e3 | 2001-08-06 22:06:35 +0000 | [diff] [blame] | 952 | fprintf(stderr, "Options:\n"); |
| 953 | fprintf(stderr, " -c Generate C-shell commands on stdout.\n"); |
| 954 | fprintf(stderr, " -s Generate Bourne shell commands on stdout.\n"); |
| 955 | fprintf(stderr, " -k Kill the current agent.\n"); |
| 956 | fprintf(stderr, " -d Debug mode.\n"); |
Ben Lindstrom | b7788f3 | 2002-06-06 21:46:08 +0000 | [diff] [blame] | 957 | fprintf(stderr, " -a socket Bind agent socket to given name.\n"); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 958 | exit(1); |
Damien Miller | 792c511 | 1999-10-29 09:47:09 +1000 | [diff] [blame] | 959 | } |
| 960 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 961 | int |
| 962 | main(int ac, char **av) |
| 963 | { |
Ben Lindstrom | a3d5a4c | 2001-07-18 15:58:08 +0000 | [diff] [blame] | 964 | int sock, c_flag = 0, d_flag = 0, k_flag = 0, s_flag = 0, ch, nalloc; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 965 | struct sockaddr_un sunaddr; |
Ben Lindstrom | 2c467a2 | 2000-12-27 04:57:41 +0000 | [diff] [blame] | 966 | #ifdef HAVE_SETRLIMIT |
Ben Lindstrom | c72745a | 2000-12-02 19:03:54 +0000 | [diff] [blame] | 967 | struct rlimit rlim; |
Ben Lindstrom | 2c467a2 | 2000-12-27 04:57:41 +0000 | [diff] [blame] | 968 | #endif |
Ben Lindstrom | 3524d69 | 2001-05-03 22:59:24 +0000 | [diff] [blame] | 969 | #ifdef HAVE_CYGWIN |
| 970 | int prev_mask; |
| 971 | #endif |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 972 | pid_t pid; |
| 973 | char *shell, *format, *pidstr, pidstrbuf[1 + 3 * sizeof pid]; |
Ben Lindstrom | b7788f3 | 2002-06-06 21:46:08 +0000 | [diff] [blame] | 974 | char *agentsocket = NULL; |
Damien Miller | 615f939 | 2000-05-17 22:53:33 +1000 | [diff] [blame] | 975 | extern int optind; |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 976 | fd_set *readsetp = NULL, *writesetp = NULL; |
Kevin Steves | de41bc6 | 2000-12-14 00:04:08 +0000 | [diff] [blame] | 977 | |
Ben Lindstrom | d09fcf5 | 2001-03-29 00:29:54 +0000 | [diff] [blame] | 978 | SSLeay_add_all_algorithms(); |
| 979 | |
Ben Lindstrom | 49a79c0 | 2000-11-17 03:47:20 +0000 | [diff] [blame] | 980 | __progname = get_progname(av[0]); |
Damien Miller | f9b625c | 2000-07-09 22:42:32 +1000 | [diff] [blame] | 981 | init_rng(); |
Damien Miller | 60bc517 | 2001-03-19 09:38:15 +1100 | [diff] [blame] | 982 | seed_rng(); |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 983 | |
Damien Miller | d0cff3e | 2000-04-20 23:12:58 +1000 | [diff] [blame] | 984 | #ifdef __GNU_LIBRARY__ |
Ben Lindstrom | b7788f3 | 2002-06-06 21:46:08 +0000 | [diff] [blame] | 985 | while ((ch = getopt(ac, av, "+cdksa:")) != -1) { |
Damien Miller | d0cff3e | 2000-04-20 23:12:58 +1000 | [diff] [blame] | 986 | #else /* __GNU_LIBRARY__ */ |
Ben Lindstrom | b7788f3 | 2002-06-06 21:46:08 +0000 | [diff] [blame] | 987 | while ((ch = getopt(ac, av, "cdksa:")) != -1) { |
Damien Miller | d0cff3e | 2000-04-20 23:12:58 +1000 | [diff] [blame] | 988 | #endif /* __GNU_LIBRARY__ */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 989 | switch (ch) { |
| 990 | case 'c': |
| 991 | if (s_flag) |
| 992 | usage(); |
| 993 | c_flag++; |
| 994 | break; |
| 995 | case 'k': |
| 996 | k_flag++; |
| 997 | break; |
| 998 | case 's': |
| 999 | if (c_flag) |
| 1000 | usage(); |
| 1001 | s_flag++; |
| 1002 | break; |
Ben Lindstrom | d94580c | 2001-07-04 03:48:02 +0000 | [diff] [blame] | 1003 | case 'd': |
| 1004 | if (d_flag) |
| 1005 | usage(); |
| 1006 | d_flag++; |
| 1007 | break; |
Ben Lindstrom | b7788f3 | 2002-06-06 21:46:08 +0000 | [diff] [blame] | 1008 | case 'a': |
| 1009 | agentsocket = optarg; |
| 1010 | break; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1011 | default: |
| 1012 | usage(); |
| 1013 | } |
Damien Miller | 792c511 | 1999-10-29 09:47:09 +1000 | [diff] [blame] | 1014 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1015 | ac -= optind; |
| 1016 | av += optind; |
Damien Miller | 792c511 | 1999-10-29 09:47:09 +1000 | [diff] [blame] | 1017 | |
Ben Lindstrom | d94580c | 2001-07-04 03:48:02 +0000 | [diff] [blame] | 1018 | if (ac > 0 && (c_flag || k_flag || s_flag || d_flag)) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1019 | usage(); |
Damien Miller | 792c511 | 1999-10-29 09:47:09 +1000 | [diff] [blame] | 1020 | |
Ben Lindstrom | eecdf23 | 2002-04-02 21:03:51 +0000 | [diff] [blame] | 1021 | if (ac == 0 && !c_flag && !s_flag) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1022 | shell = getenv("SHELL"); |
| 1023 | if (shell != NULL && strncmp(shell + strlen(shell) - 3, "csh", 3) == 0) |
| 1024 | c_flag = 1; |
Damien Miller | 792c511 | 1999-10-29 09:47:09 +1000 | [diff] [blame] | 1025 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1026 | if (k_flag) { |
| 1027 | pidstr = getenv(SSH_AGENTPID_ENV_NAME); |
| 1028 | if (pidstr == NULL) { |
| 1029 | fprintf(stderr, "%s not set, cannot kill agent\n", |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 1030 | SSH_AGENTPID_ENV_NAME); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1031 | exit(1); |
| 1032 | } |
| 1033 | pid = atoi(pidstr); |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 1034 | if (pid < 1) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1035 | fprintf(stderr, "%s=\"%s\", which is not a good PID\n", |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 1036 | SSH_AGENTPID_ENV_NAME, pidstr); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1037 | exit(1); |
| 1038 | } |
| 1039 | if (kill(pid, SIGTERM) == -1) { |
| 1040 | perror("kill"); |
| 1041 | exit(1); |
| 1042 | } |
| 1043 | format = c_flag ? "unsetenv %s;\n" : "unset %s;\n"; |
| 1044 | printf(format, SSH_AUTHSOCKET_ENV_NAME); |
| 1045 | printf(format, SSH_AGENTPID_ENV_NAME); |
| 1046 | printf("echo Agent pid %d killed;\n", pid); |
| 1047 | exit(0); |
Damien Miller | 792c511 | 1999-10-29 09:47:09 +1000 | [diff] [blame] | 1048 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1049 | parent_pid = getpid(); |
| 1050 | |
Ben Lindstrom | b7788f3 | 2002-06-06 21:46:08 +0000 | [diff] [blame] | 1051 | if (agentsocket == NULL) { |
| 1052 | /* Create private directory for agent socket */ |
| 1053 | strlcpy(socket_dir, "/tmp/ssh-XXXXXXXX", sizeof socket_dir); |
| 1054 | if (mkdtemp(socket_dir) == NULL) { |
| 1055 | perror("mkdtemp: private socket dir"); |
| 1056 | exit(1); |
| 1057 | } |
| 1058 | snprintf(socket_name, sizeof socket_name, "%s/agent.%d", socket_dir, |
| 1059 | parent_pid); |
| 1060 | } else { |
| 1061 | /* Try to use specified agent socket */ |
| 1062 | socket_dir[0] = '\0'; |
| 1063 | strlcpy(socket_name, agentsocket, sizeof socket_name); |
Damien Miller | 792c511 | 1999-10-29 09:47:09 +1000 | [diff] [blame] | 1064 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1065 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1066 | /* |
| 1067 | * Create socket early so it will exist before command gets run from |
| 1068 | * the parent. |
| 1069 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1070 | sock = socket(AF_UNIX, SOCK_STREAM, 0); |
| 1071 | if (sock < 0) { |
| 1072 | perror("socket"); |
| 1073 | cleanup_exit(1); |
Damien Miller | 792c511 | 1999-10-29 09:47:09 +1000 | [diff] [blame] | 1074 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1075 | memset(&sunaddr, 0, sizeof(sunaddr)); |
| 1076 | sunaddr.sun_family = AF_UNIX; |
| 1077 | strlcpy(sunaddr.sun_path, socket_name, sizeof(sunaddr.sun_path)); |
Ben Lindstrom | 3524d69 | 2001-05-03 22:59:24 +0000 | [diff] [blame] | 1078 | #ifdef HAVE_CYGWIN |
| 1079 | prev_mask = umask(0177); |
| 1080 | #endif |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1081 | if (bind(sock, (struct sockaddr *) & sunaddr, sizeof(sunaddr)) < 0) { |
| 1082 | perror("bind"); |
Ben Lindstrom | 3524d69 | 2001-05-03 22:59:24 +0000 | [diff] [blame] | 1083 | #ifdef HAVE_CYGWIN |
| 1084 | umask(prev_mask); |
| 1085 | #endif |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1086 | cleanup_exit(1); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1087 | } |
Ben Lindstrom | 3524d69 | 2001-05-03 22:59:24 +0000 | [diff] [blame] | 1088 | #ifdef HAVE_CYGWIN |
| 1089 | umask(prev_mask); |
| 1090 | #endif |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1091 | if (listen(sock, 5) < 0) { |
| 1092 | perror("listen"); |
| 1093 | cleanup_exit(1); |
| 1094 | } |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 1095 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1096 | /* |
| 1097 | * Fork, and have the parent execute the command, if any, or present |
| 1098 | * the socket data. The child continues as the authentication agent. |
| 1099 | */ |
Ben Lindstrom | d94580c | 2001-07-04 03:48:02 +0000 | [diff] [blame] | 1100 | if (d_flag) { |
| 1101 | log_init(__progname, SYSLOG_LEVEL_DEBUG1, SYSLOG_FACILITY_AUTH, 1); |
| 1102 | format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n"; |
| 1103 | printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name, |
| 1104 | SSH_AUTHSOCKET_ENV_NAME); |
| 1105 | printf("echo Agent pid %d;\n", parent_pid); |
| 1106 | goto skip; |
| 1107 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1108 | pid = fork(); |
| 1109 | if (pid == -1) { |
| 1110 | perror("fork"); |
Damien Miller | c653b89 | 2002-02-08 22:05:41 +1100 | [diff] [blame] | 1111 | cleanup_exit(1); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1112 | } |
| 1113 | if (pid != 0) { /* Parent - execute the given command. */ |
| 1114 | close(sock); |
| 1115 | snprintf(pidstrbuf, sizeof pidstrbuf, "%d", pid); |
| 1116 | if (ac == 0) { |
| 1117 | format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n"; |
| 1118 | printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name, |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 1119 | SSH_AUTHSOCKET_ENV_NAME); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1120 | printf(format, SSH_AGENTPID_ENV_NAME, pidstrbuf, |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 1121 | SSH_AGENTPID_ENV_NAME); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1122 | printf("echo Agent pid %d;\n", pid); |
| 1123 | exit(0); |
| 1124 | } |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 1125 | if (setenv(SSH_AUTHSOCKET_ENV_NAME, socket_name, 1) == -1 || |
| 1126 | setenv(SSH_AGENTPID_ENV_NAME, pidstrbuf, 1) == -1) { |
| 1127 | perror("setenv"); |
| 1128 | exit(1); |
| 1129 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1130 | execvp(av[0], av); |
| 1131 | perror(av[0]); |
| 1132 | exit(1); |
| 1133 | } |
Damien Miller | c653b89 | 2002-02-08 22:05:41 +1100 | [diff] [blame] | 1134 | /* child */ |
| 1135 | log_init(__progname, SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_AUTH, 0); |
Ben Lindstrom | 3fdf876 | 2001-07-22 20:40:24 +0000 | [diff] [blame] | 1136 | |
| 1137 | if (setsid() == -1) { |
Damien Miller | c653b89 | 2002-02-08 22:05:41 +1100 | [diff] [blame] | 1138 | error("setsid: %s", strerror(errno)); |
Ben Lindstrom | 3fdf876 | 2001-07-22 20:40:24 +0000 | [diff] [blame] | 1139 | cleanup_exit(1); |
| 1140 | } |
| 1141 | |
| 1142 | (void)chdir("/"); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1143 | close(0); |
| 1144 | close(1); |
| 1145 | close(2); |
| 1146 | |
Ben Lindstrom | 2c467a2 | 2000-12-27 04:57:41 +0000 | [diff] [blame] | 1147 | #ifdef HAVE_SETRLIMIT |
Ben Lindstrom | c72745a | 2000-12-02 19:03:54 +0000 | [diff] [blame] | 1148 | /* deny core dumps, since memory contains unencrypted private keys */ |
| 1149 | rlim.rlim_cur = rlim.rlim_max = 0; |
| 1150 | if (setrlimit(RLIMIT_CORE, &rlim) < 0) { |
Damien Miller | c653b89 | 2002-02-08 22:05:41 +1100 | [diff] [blame] | 1151 | error("setrlimit RLIMIT_CORE: %s", strerror(errno)); |
Ben Lindstrom | c72745a | 2000-12-02 19:03:54 +0000 | [diff] [blame] | 1152 | cleanup_exit(1); |
| 1153 | } |
Ben Lindstrom | 2c467a2 | 2000-12-27 04:57:41 +0000 | [diff] [blame] | 1154 | #endif |
Ben Lindstrom | d94580c | 2001-07-04 03:48:02 +0000 | [diff] [blame] | 1155 | |
| 1156 | skip: |
Damien Miller | c653b89 | 2002-02-08 22:05:41 +1100 | [diff] [blame] | 1157 | fatal_add_cleanup(cleanup_socket, NULL); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1158 | new_socket(AUTH_SOCKET, sock); |
| 1159 | if (ac > 0) { |
| 1160 | signal(SIGALRM, check_parent_exists); |
| 1161 | alarm(10); |
| 1162 | } |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 1163 | idtab_init(); |
Damien Miller | 48bfa9c | 2001-07-14 12:12:55 +1000 | [diff] [blame] | 1164 | if (!d_flag) |
Ben Lindstrom | d94580c | 2001-07-04 03:48:02 +0000 | [diff] [blame] | 1165 | signal(SIGINT, SIG_IGN); |
Damien Miller | 48bfa9c | 2001-07-14 12:12:55 +1000 | [diff] [blame] | 1166 | signal(SIGPIPE, SIG_IGN); |
Ben Lindstrom | 77808ab | 2001-01-26 05:10:34 +0000 | [diff] [blame] | 1167 | signal(SIGHUP, cleanup_handler); |
| 1168 | signal(SIGTERM, cleanup_handler); |
Ben Lindstrom | a3d5a4c | 2001-07-18 15:58:08 +0000 | [diff] [blame] | 1169 | nalloc = 0; |
| 1170 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1171 | while (1) { |
Ben Lindstrom | a3d5a4c | 2001-07-18 15:58:08 +0000 | [diff] [blame] | 1172 | prepare_select(&readsetp, &writesetp, &max_fd, &nalloc); |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 1173 | if (select(max_fd + 1, readsetp, writesetp, NULL, NULL) < 0) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1174 | if (errno == EINTR) |
| 1175 | continue; |
Damien Miller | c653b89 | 2002-02-08 22:05:41 +1100 | [diff] [blame] | 1176 | fatal("select: %s", strerror(errno)); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1177 | } |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 1178 | after_select(readsetp, writesetp); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1179 | } |
| 1180 | /* NOTREACHED */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1181 | } |