blob: aeda656ac8046a8f719aa5284392fd380705a8e2 [file] [log] [blame]
Adam Langleyd0592972015-03-30 14:49:51 -07001/* $OpenBSD: ssh-agent.c,v 1.199 2015/03/04 21:12:59 djm Exp $ */
Greg Hartmanbd77cf72015-02-25 13:21:06 -08002/*
3 * Author: Tatu Ylonen <ylo@cs.hut.fi>
4 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * All rights reserved
6 * The authentication agent program.
7 *
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 *
14 * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved.
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.
35 */
36
37#include "includes.h"
38
Adam Langleyd0592972015-03-30 14:49:51 -070039#include <sys/param.h> /* MIN MAX */
Greg Hartmanbd77cf72015-02-25 13:21:06 -080040#include <sys/types.h>
41#include <sys/param.h>
42#include <sys/resource.h>
43#include <sys/stat.h>
44#include <sys/socket.h>
45#ifdef HAVE_SYS_TIME_H
46# include <sys/time.h>
47#endif
48#ifdef HAVE_SYS_UN_H
49# include <sys/un.h>
50#endif
51#include "openbsd-compat/sys-queue.h"
52
Adam Langleyd0592972015-03-30 14:49:51 -070053#ifdef WITH_OPENSSL
Greg Hartmanbd77cf72015-02-25 13:21:06 -080054#include <openssl/evp.h>
Greg Hartmanbd77cf72015-02-25 13:21:06 -080055#include "openbsd-compat/openssl-compat.h"
Adam Langleyd0592972015-03-30 14:49:51 -070056#endif
Greg Hartmanbd77cf72015-02-25 13:21:06 -080057
58#include <errno.h>
59#include <fcntl.h>
Adam Langleyd0592972015-03-30 14:49:51 -070060#include <limits.h>
Greg Hartmanbd77cf72015-02-25 13:21:06 -080061#ifdef HAVE_PATHS_H
62# include <paths.h>
63#endif
64#include <signal.h>
65#include <stdarg.h>
66#include <stdio.h>
67#include <stdlib.h>
68#include <time.h>
69#include <string.h>
70#include <unistd.h>
71
Adam Langleyd0592972015-03-30 14:49:51 -070072#include "key.h" /* XXX for typedef */
73#include "buffer.h" /* XXX for typedef */
74
Greg Hartmanbd77cf72015-02-25 13:21:06 -080075#include "xmalloc.h"
76#include "ssh.h"
77#include "rsa.h"
Adam Langleyd0592972015-03-30 14:49:51 -070078#include "sshbuf.h"
79#include "sshkey.h"
Greg Hartmanbd77cf72015-02-25 13:21:06 -080080#include "authfd.h"
81#include "compat.h"
82#include "log.h"
83#include "misc.h"
Adam Langleyd0592972015-03-30 14:49:51 -070084#include "digest.h"
85#include "ssherr.h"
Greg Hartmanbd77cf72015-02-25 13:21:06 -080086
87#ifdef ENABLE_PKCS11
88#include "ssh-pkcs11.h"
89#endif
90
91#if defined(HAVE_SYS_PRCTL_H)
92#include <sys/prctl.h> /* For prctl() and PR_SET_DUMPABLE */
93#endif
94
95typedef enum {
96 AUTH_UNUSED,
97 AUTH_SOCKET,
98 AUTH_CONNECTION
99} sock_type;
100
101typedef struct {
102 int fd;
103 sock_type type;
Adam Langleyd0592972015-03-30 14:49:51 -0700104 struct sshbuf *input;
105 struct sshbuf *output;
106 struct sshbuf *request;
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800107} SocketEntry;
108
109u_int sockets_alloc = 0;
110SocketEntry *sockets = NULL;
111
112typedef struct identity {
113 TAILQ_ENTRY(identity) next;
Adam Langleyd0592972015-03-30 14:49:51 -0700114 struct sshkey *key;
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800115 char *comment;
116 char *provider;
Adam Langleyd0592972015-03-30 14:49:51 -0700117 time_t death;
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800118 u_int confirm;
119} Identity;
120
121typedef struct {
122 int nentries;
123 TAILQ_HEAD(idqueue, identity) idlist;
124} Idtab;
125
126/* private key table, one per protocol version */
127Idtab idtable[3];
128
129int max_fd = 0;
130
131/* pid of shell == parent of agent */
132pid_t parent_pid = -1;
Adam Langleyd0592972015-03-30 14:49:51 -0700133time_t parent_alive_interval = 0;
134
135/* pid of process for which cleanup_socket is applicable */
136pid_t cleanup_pid = 0;
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800137
138/* pathname and directory for AUTH_SOCKET */
Adam Langleyd0592972015-03-30 14:49:51 -0700139char socket_name[PATH_MAX];
140char socket_dir[PATH_MAX];
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800141
142/* locking */
143int locked = 0;
144char *lock_passwd = NULL;
145
146extern char *__progname;
147
Adam Langleyd0592972015-03-30 14:49:51 -0700148/* Default lifetime in seconds (0 == forever) */
149static long lifetime = 0;
150
151static int fingerprint_hash = SSH_FP_HASH_DEFAULT;
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800152
153static void
154close_socket(SocketEntry *e)
155{
156 close(e->fd);
157 e->fd = -1;
158 e->type = AUTH_UNUSED;
Adam Langleyd0592972015-03-30 14:49:51 -0700159 sshbuf_free(e->input);
160 sshbuf_free(e->output);
161 sshbuf_free(e->request);
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800162}
163
164static void
165idtab_init(void)
166{
167 int i;
168
169 for (i = 0; i <=2; i++) {
170 TAILQ_INIT(&idtable[i].idlist);
171 idtable[i].nentries = 0;
172 }
173}
174
175/* return private key table for requested protocol version */
176static Idtab *
177idtab_lookup(int version)
178{
179 if (version < 1 || version > 2)
180 fatal("internal error, bad protocol version %d", version);
181 return &idtable[version];
182}
183
184static void
185free_identity(Identity *id)
186{
Adam Langleyd0592972015-03-30 14:49:51 -0700187 sshkey_free(id->key);
188 free(id->provider);
189 free(id->comment);
190 free(id);
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800191}
192
193/* return matching private key for given public key */
194static Identity *
Adam Langleyd0592972015-03-30 14:49:51 -0700195lookup_identity(struct sshkey *key, int version)
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800196{
197 Identity *id;
198
199 Idtab *tab = idtab_lookup(version);
200 TAILQ_FOREACH(id, &tab->idlist, next) {
Adam Langleyd0592972015-03-30 14:49:51 -0700201 if (sshkey_equal(key, id->key))
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800202 return (id);
203 }
204 return (NULL);
205}
206
207/* Check confirmation of keysign request */
208static int
209confirm_key(Identity *id)
210{
211 char *p;
212 int ret = -1;
213
Adam Langleyd0592972015-03-30 14:49:51 -0700214 p = sshkey_fingerprint(id->key, fingerprint_hash, SSH_FP_DEFAULT);
215 if (p != NULL &&
216 ask_permission("Allow use of key %s?\nKey fingerprint %s.",
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800217 id->comment, p))
218 ret = 0;
Adam Langleyd0592972015-03-30 14:49:51 -0700219 free(p);
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800220
221 return (ret);
222}
223
Adam Langleyd0592972015-03-30 14:49:51 -0700224static void
225send_status(SocketEntry *e, int success)
226{
227 int r;
228
229 if ((r = sshbuf_put_u32(e->output, 1)) != 0 ||
230 (r = sshbuf_put_u8(e->output, success ?
231 SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE)) != 0)
232 fatal("%s: buffer error: %s", __func__, ssh_err(r));
233}
234
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800235/* send list of supported public keys to 'client' */
236static void
237process_request_identities(SocketEntry *e, int version)
238{
239 Idtab *tab = idtab_lookup(version);
240 Identity *id;
Adam Langleyd0592972015-03-30 14:49:51 -0700241 struct sshbuf *msg;
242 int r;
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800243
Adam Langleyd0592972015-03-30 14:49:51 -0700244 if ((msg = sshbuf_new()) == NULL)
245 fatal("%s: sshbuf_new failed", __func__);
246 if ((r = sshbuf_put_u8(msg, (version == 1) ?
247 SSH_AGENT_RSA_IDENTITIES_ANSWER :
248 SSH2_AGENT_IDENTITIES_ANSWER)) != 0 ||
249 (r = sshbuf_put_u32(msg, tab->nentries)) != 0)
250 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800251 TAILQ_FOREACH(id, &tab->idlist, next) {
252 if (id->key->type == KEY_RSA1) {
Adam Langleyd0592972015-03-30 14:49:51 -0700253#ifdef WITH_SSH1
254 if ((r = sshbuf_put_u32(msg,
255 BN_num_bits(id->key->rsa->n))) != 0 ||
256 (r = sshbuf_put_bignum1(msg,
257 id->key->rsa->e)) != 0 ||
258 (r = sshbuf_put_bignum1(msg,
259 id->key->rsa->n)) != 0)
260 fatal("%s: buffer error: %s",
261 __func__, ssh_err(r));
262#endif
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800263 } else {
264 u_char *blob;
Adam Langleyd0592972015-03-30 14:49:51 -0700265 size_t blen;
266
267 if ((r = sshkey_to_blob(id->key, &blob, &blen)) != 0) {
268 error("%s: sshkey_to_blob: %s", __func__,
269 ssh_err(r));
270 continue;
271 }
272 if ((r = sshbuf_put_string(msg, blob, blen)) != 0)
273 fatal("%s: buffer error: %s",
274 __func__, ssh_err(r));
275 free(blob);
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800276 }
Adam Langleyd0592972015-03-30 14:49:51 -0700277 if ((r = sshbuf_put_cstring(msg, id->comment)) != 0)
278 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800279 }
Adam Langleyd0592972015-03-30 14:49:51 -0700280 if ((r = sshbuf_put_stringb(e->output, msg)) != 0)
281 fatal("%s: buffer error: %s", __func__, ssh_err(r));
282 sshbuf_free(msg);
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800283}
284
Adam Langleyd0592972015-03-30 14:49:51 -0700285#ifdef WITH_SSH1
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800286/* ssh1 only */
287static void
288process_authentication_challenge1(SocketEntry *e)
289{
290 u_char buf[32], mdbuf[16], session_id[16];
291 u_int response_type;
292 BIGNUM *challenge;
293 Identity *id;
Adam Langleyd0592972015-03-30 14:49:51 -0700294 int r, len;
295 struct sshbuf *msg;
296 struct ssh_digest_ctx *md;
297 struct sshkey *key;
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800298
Adam Langleyd0592972015-03-30 14:49:51 -0700299 if ((msg = sshbuf_new()) == NULL)
300 fatal("%s: sshbuf_new failed", __func__);
301 if ((key = sshkey_new(KEY_RSA1)) == NULL)
302 fatal("%s: sshkey_new failed", __func__);
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800303 if ((challenge = BN_new()) == NULL)
Adam Langleyd0592972015-03-30 14:49:51 -0700304 fatal("%s: BN_new failed", __func__);
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800305
Adam Langleyd0592972015-03-30 14:49:51 -0700306 if ((r = sshbuf_get_u32(e->request, NULL)) != 0 || /* ignored */
307 (r = sshbuf_get_bignum1(e->request, key->rsa->e)) != 0 ||
308 (r = sshbuf_get_bignum1(e->request, key->rsa->n)) != 0 ||
309 (r = sshbuf_get_bignum1(e->request, challenge)))
310 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800311
312 /* Only protocol 1.1 is supported */
Adam Langleyd0592972015-03-30 14:49:51 -0700313 if (sshbuf_len(e->request) == 0)
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800314 goto failure;
Adam Langleyd0592972015-03-30 14:49:51 -0700315 if ((r = sshbuf_get(e->request, session_id, sizeof(session_id))) != 0 ||
316 (r = sshbuf_get_u32(e->request, &response_type)) != 0)
317 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800318 if (response_type != 1)
319 goto failure;
320
321 id = lookup_identity(key, 1);
322 if (id != NULL && (!id->confirm || confirm_key(id) == 0)) {
Adam Langleyd0592972015-03-30 14:49:51 -0700323 struct sshkey *private = id->key;
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800324 /* Decrypt the challenge using the private key. */
Adam Langleyd0592972015-03-30 14:49:51 -0700325 if ((r = rsa_private_decrypt(challenge, challenge,
326 private->rsa) != 0)) {
327 fatal("%s: rsa_public_encrypt: %s", __func__,
328 ssh_err(r));
329 goto failure; /* XXX ? */
330 }
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800331
Adam Langleyd0592972015-03-30 14:49:51 -0700332 /* The response is MD5 of decrypted challenge plus session id */
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800333 len = BN_num_bytes(challenge);
334 if (len <= 0 || len > 32) {
Adam Langleyd0592972015-03-30 14:49:51 -0700335 logit("%s: bad challenge length %d", __func__, len);
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800336 goto failure;
337 }
338 memset(buf, 0, 32);
339 BN_bn2bin(challenge, buf + 32 - len);
Adam Langleyd0592972015-03-30 14:49:51 -0700340 if ((md = ssh_digest_start(SSH_DIGEST_MD5)) == NULL ||
341 ssh_digest_update(md, buf, 32) < 0 ||
342 ssh_digest_update(md, session_id, 16) < 0 ||
343 ssh_digest_final(md, mdbuf, sizeof(mdbuf)) < 0)
344 fatal("%s: md5 failed", __func__);
345 ssh_digest_free(md);
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800346
347 /* Send the response. */
Adam Langleyd0592972015-03-30 14:49:51 -0700348 if ((r = sshbuf_put_u8(msg, SSH_AGENT_RSA_RESPONSE)) != 0 ||
349 (r = sshbuf_put(msg, mdbuf, sizeof(mdbuf))) != 0)
350 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800351 goto send;
352 }
353
Adam Langleyd0592972015-03-30 14:49:51 -0700354 failure:
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800355 /* Unknown identity or protocol error. Send failure. */
Adam Langleyd0592972015-03-30 14:49:51 -0700356 if ((r = sshbuf_put_u8(msg, SSH_AGENT_FAILURE)) != 0)
357 fatal("%s: buffer error: %s", __func__, ssh_err(r));
358 send:
359 if ((r = sshbuf_put_stringb(e->output, msg)) != 0)
360 fatal("%s: buffer error: %s", __func__, ssh_err(r));
361 sshkey_free(key);
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800362 BN_clear_free(challenge);
Adam Langleyd0592972015-03-30 14:49:51 -0700363 sshbuf_free(msg);
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800364}
Adam Langleyd0592972015-03-30 14:49:51 -0700365#endif
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800366
367/* ssh2 only */
368static void
369process_sign_request2(SocketEntry *e)
370{
371 u_char *blob, *data, *signature = NULL;
Adam Langleyd0592972015-03-30 14:49:51 -0700372 size_t blen, dlen, slen = 0;
373 u_int compat = 0, flags;
374 int r, ok = -1;
375 struct sshbuf *msg;
376 struct sshkey *key;
377 struct identity *id;
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800378
Adam Langleyd0592972015-03-30 14:49:51 -0700379 if ((msg = sshbuf_new()) == NULL)
380 fatal("%s: sshbuf_new failed", __func__);
381 if ((r = sshbuf_get_string(e->request, &blob, &blen)) != 0 ||
382 (r = sshbuf_get_string(e->request, &data, &dlen)) != 0 ||
383 (r = sshbuf_get_u32(e->request, &flags)) != 0)
384 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800385 if (flags & SSH_AGENT_OLD_SIGNATURE)
Adam Langleyd0592972015-03-30 14:49:51 -0700386 compat = SSH_BUG_SIGBLOB;
387 if ((r = sshkey_from_blob(blob, blen, &key)) != 0) {
388 error("%s: cannot parse key blob: %s", __func__, ssh_err(ok));
389 goto send;
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800390 }
Adam Langleyd0592972015-03-30 14:49:51 -0700391 if ((id = lookup_identity(key, 2)) == NULL) {
392 verbose("%s: %s key not found", __func__, sshkey_type(key));
393 goto send;
394 }
395 if (id->confirm && confirm_key(id) != 0) {
396 verbose("%s: user refused key", __func__);
397 goto send;
398 }
399 if ((r = sshkey_sign(id->key, &signature, &slen,
400 data, dlen, compat)) != 0) {
401 error("%s: sshkey_sign: %s", __func__, ssh_err(ok));
402 goto send;
403 }
404 /* Success */
405 ok = 0;
406 send:
407 sshkey_free(key);
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800408 if (ok == 0) {
Adam Langleyd0592972015-03-30 14:49:51 -0700409 if ((r = sshbuf_put_u8(msg, SSH2_AGENT_SIGN_RESPONSE)) != 0 ||
410 (r = sshbuf_put_string(msg, signature, slen)) != 0)
411 fatal("%s: buffer error: %s", __func__, ssh_err(r));
412 } else if ((r = sshbuf_put_u8(msg, SSH_AGENT_FAILURE)) != 0)
413 fatal("%s: buffer error: %s", __func__, ssh_err(r));
414
415 if ((r = sshbuf_put_stringb(e->output, msg)) != 0)
416 fatal("%s: buffer error: %s", __func__, ssh_err(r));
417
418 sshbuf_free(msg);
419 free(data);
420 free(blob);
421 free(signature);
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800422}
423
424/* shared */
425static void
426process_remove_identity(SocketEntry *e, int version)
427{
Adam Langleyd0592972015-03-30 14:49:51 -0700428 size_t blen;
429 int r, success = 0;
430 struct sshkey *key = NULL;
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800431 u_char *blob;
Adam Langleyd0592972015-03-30 14:49:51 -0700432#ifdef WITH_SSH1
433 u_int bits;
434#endif /* WITH_SSH1 */
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800435
436 switch (version) {
Adam Langleyd0592972015-03-30 14:49:51 -0700437#ifdef WITH_SSH1
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800438 case 1:
Adam Langleyd0592972015-03-30 14:49:51 -0700439 if ((key = sshkey_new(KEY_RSA1)) == NULL) {
440 error("%s: sshkey_new failed", __func__);
441 return;
442 }
443 if ((r = sshbuf_get_u32(e->request, &bits)) != 0 ||
444 (r = sshbuf_get_bignum1(e->request, key->rsa->e)) != 0 ||
445 (r = sshbuf_get_bignum1(e->request, key->rsa->n)) != 0)
446 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800447
Adam Langleyd0592972015-03-30 14:49:51 -0700448 if (bits != sshkey_size(key))
449 logit("Warning: identity keysize mismatch: "
450 "actual %u, announced %u",
451 sshkey_size(key), bits);
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800452 break;
Adam Langleyd0592972015-03-30 14:49:51 -0700453#endif /* WITH_SSH1 */
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800454 case 2:
Adam Langleyd0592972015-03-30 14:49:51 -0700455 if ((r = sshbuf_get_string(e->request, &blob, &blen)) != 0)
456 fatal("%s: buffer error: %s", __func__, ssh_err(r));
457 if ((r = sshkey_from_blob(blob, blen, &key)) != 0)
458 error("%s: sshkey_from_blob failed: %s",
459 __func__, ssh_err(r));
460 free(blob);
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800461 break;
462 }
463 if (key != NULL) {
464 Identity *id = lookup_identity(key, version);
465 if (id != NULL) {
466 /*
467 * We have this key. Free the old key. Since we
468 * don't want to leave empty slots in the middle of
469 * the array, we actually free the key there and move
470 * all the entries between the empty slot and the end
471 * of the array.
472 */
473 Idtab *tab = idtab_lookup(version);
474 if (tab->nentries < 1)
475 fatal("process_remove_identity: "
476 "internal error: tab->nentries %d",
477 tab->nentries);
478 TAILQ_REMOVE(&tab->idlist, id, next);
479 free_identity(id);
480 tab->nentries--;
481 success = 1;
482 }
Adam Langleyd0592972015-03-30 14:49:51 -0700483 sshkey_free(key);
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800484 }
Adam Langleyd0592972015-03-30 14:49:51 -0700485 send_status(e, success);
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800486}
487
488static void
489process_remove_all_identities(SocketEntry *e, int version)
490{
491 Idtab *tab = idtab_lookup(version);
492 Identity *id;
493
494 /* Loop over all identities and clear the keys. */
495 for (id = TAILQ_FIRST(&tab->idlist); id;
496 id = TAILQ_FIRST(&tab->idlist)) {
497 TAILQ_REMOVE(&tab->idlist, id, next);
498 free_identity(id);
499 }
500
501 /* Mark that there are no identities. */
502 tab->nentries = 0;
503
504 /* Send success. */
Adam Langleyd0592972015-03-30 14:49:51 -0700505 send_status(e, 1);
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800506}
507
508/* removes expired keys and returns number of seconds until the next expiry */
Adam Langleyd0592972015-03-30 14:49:51 -0700509static time_t
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800510reaper(void)
511{
Adam Langleyd0592972015-03-30 14:49:51 -0700512 time_t deadline = 0, now = monotime();
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800513 Identity *id, *nxt;
514 int version;
515 Idtab *tab;
516
517 for (version = 1; version < 3; version++) {
518 tab = idtab_lookup(version);
519 for (id = TAILQ_FIRST(&tab->idlist); id; id = nxt) {
520 nxt = TAILQ_NEXT(id, next);
521 if (id->death == 0)
522 continue;
523 if (now >= id->death) {
524 debug("expiring key '%s'", id->comment);
525 TAILQ_REMOVE(&tab->idlist, id, next);
526 free_identity(id);
527 tab->nentries--;
528 } else
529 deadline = (deadline == 0) ? id->death :
530 MIN(deadline, id->death);
531 }
532 }
533 if (deadline == 0 || deadline <= now)
534 return 0;
535 else
536 return (deadline - now);
537}
538
Adam Langleyd0592972015-03-30 14:49:51 -0700539/*
540 * XXX this and the corresponding serialisation function probably belongs
541 * in key.c
542 */
543#ifdef WITH_SSH1
544static int
545agent_decode_rsa1(struct sshbuf *m, struct sshkey **kp)
546{
547 struct sshkey *k = NULL;
548 int r = SSH_ERR_INTERNAL_ERROR;
549
550 *kp = NULL;
551 if ((k = sshkey_new_private(KEY_RSA1)) == NULL)
552 return SSH_ERR_ALLOC_FAIL;
553
554 if ((r = sshbuf_get_u32(m, NULL)) != 0 || /* ignored */
555 (r = sshbuf_get_bignum1(m, k->rsa->n)) != 0 ||
556 (r = sshbuf_get_bignum1(m, k->rsa->e)) != 0 ||
557 (r = sshbuf_get_bignum1(m, k->rsa->d)) != 0 ||
558 (r = sshbuf_get_bignum1(m, k->rsa->iqmp)) != 0 ||
559 /* SSH1 and SSL have p and q swapped */
560 (r = sshbuf_get_bignum1(m, k->rsa->q)) != 0 || /* p */
561 (r = sshbuf_get_bignum1(m, k->rsa->p)) != 0) /* q */
562 goto out;
563
564 /* Generate additional parameters */
565 if ((r = rsa_generate_additional_parameters(k->rsa)) != 0)
566 goto out;
567 /* enable blinding */
568 if (RSA_blinding_on(k->rsa, NULL) != 1) {
569 r = SSH_ERR_LIBCRYPTO_ERROR;
570 goto out;
571 }
572
573 r = 0; /* success */
574 out:
575 if (r == 0)
576 *kp = k;
577 else
578 sshkey_free(k);
579 return r;
580}
581#endif /* WITH_SSH1 */
582
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800583static void
584process_add_identity(SocketEntry *e, int version)
585{
586 Idtab *tab = idtab_lookup(version);
587 Identity *id;
Adam Langleyd0592972015-03-30 14:49:51 -0700588 int success = 0, confirm = 0;
589 u_int seconds;
590 char *comment = NULL;
591 time_t death = 0;
592 struct sshkey *k = NULL;
593 u_char ctype;
594 int r = SSH_ERR_INTERNAL_ERROR;
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800595
596 switch (version) {
Adam Langleyd0592972015-03-30 14:49:51 -0700597#ifdef WITH_SSH1
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800598 case 1:
Adam Langleyd0592972015-03-30 14:49:51 -0700599 r = agent_decode_rsa1(e->request, &k);
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800600 break;
Adam Langleyd0592972015-03-30 14:49:51 -0700601#endif /* WITH_SSH1 */
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800602 case 2:
Adam Langleyd0592972015-03-30 14:49:51 -0700603 r = sshkey_private_deserialize(e->request, &k);
604 break;
605 }
606 if (r != 0 || k == NULL ||
607 (r = sshbuf_get_cstring(e->request, &comment, NULL)) != 0) {
608 error("%s: decode private key: %s", __func__, ssh_err(r));
609 goto err;
610 }
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800611
Adam Langleyd0592972015-03-30 14:49:51 -0700612 while (sshbuf_len(e->request)) {
613 if ((r = sshbuf_get_u8(e->request, &ctype)) != 0) {
614 error("%s: buffer error: %s", __func__, ssh_err(r));
615 goto err;
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800616 }
Adam Langleyd0592972015-03-30 14:49:51 -0700617 switch (ctype) {
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800618 case SSH_AGENT_CONSTRAIN_LIFETIME:
Adam Langleyd0592972015-03-30 14:49:51 -0700619 if ((r = sshbuf_get_u32(e->request, &seconds)) != 0) {
620 error("%s: bad lifetime constraint: %s",
621 __func__, ssh_err(r));
622 goto err;
623 }
624 death = monotime() + seconds;
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800625 break;
626 case SSH_AGENT_CONSTRAIN_CONFIRM:
627 confirm = 1;
628 break;
629 default:
Adam Langleyd0592972015-03-30 14:49:51 -0700630 error("%s: Unknown constraint %d", __func__, ctype);
631 err:
632 sshbuf_reset(e->request);
633 free(comment);
634 sshkey_free(k);
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800635 goto send;
636 }
637 }
Adam Langleyd0592972015-03-30 14:49:51 -0700638
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800639 success = 1;
640 if (lifetime && !death)
Adam Langleyd0592972015-03-30 14:49:51 -0700641 death = monotime() + lifetime;
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800642 if ((id = lookup_identity(k, version)) == NULL) {
643 id = xcalloc(1, sizeof(Identity));
644 id->key = k;
645 TAILQ_INSERT_TAIL(&tab->idlist, id, next);
646 /* Increment the number of identities. */
647 tab->nentries++;
648 } else {
Adam Langleyd0592972015-03-30 14:49:51 -0700649 sshkey_free(k);
650 free(id->comment);
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800651 }
652 id->comment = comment;
653 id->death = death;
654 id->confirm = confirm;
655send:
Adam Langleyd0592972015-03-30 14:49:51 -0700656 send_status(e, success);
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800657}
658
659/* XXX todo: encrypt sensitive data with passphrase */
660static void
661process_lock_agent(SocketEntry *e, int lock)
662{
Adam Langleyd0592972015-03-30 14:49:51 -0700663 int r, success = 0;
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800664 char *passwd;
665
Adam Langleyd0592972015-03-30 14:49:51 -0700666 if ((r = sshbuf_get_cstring(e->request, &passwd, NULL)) != 0)
667 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800668 if (locked && !lock && strcmp(passwd, lock_passwd) == 0) {
669 locked = 0;
Adam Langleyd0592972015-03-30 14:49:51 -0700670 explicit_bzero(lock_passwd, strlen(lock_passwd));
671 free(lock_passwd);
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800672 lock_passwd = NULL;
673 success = 1;
674 } else if (!locked && lock) {
675 locked = 1;
676 lock_passwd = xstrdup(passwd);
677 success = 1;
678 }
Adam Langleyd0592972015-03-30 14:49:51 -0700679 explicit_bzero(passwd, strlen(passwd));
680 free(passwd);
681 send_status(e, success);
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800682}
683
684static void
685no_identities(SocketEntry *e, u_int type)
686{
Adam Langleyd0592972015-03-30 14:49:51 -0700687 struct sshbuf *msg;
688 int r;
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800689
Adam Langleyd0592972015-03-30 14:49:51 -0700690 if ((msg = sshbuf_new()) == NULL)
691 fatal("%s: sshbuf_new failed", __func__);
692 if ((r = sshbuf_put_u8(msg,
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800693 (type == SSH_AGENTC_REQUEST_RSA_IDENTITIES) ?
Adam Langleyd0592972015-03-30 14:49:51 -0700694 SSH_AGENT_RSA_IDENTITIES_ANSWER :
695 SSH2_AGENT_IDENTITIES_ANSWER)) != 0 ||
696 (r = sshbuf_put_u32(msg, 0)) != 0 ||
697 (r = sshbuf_put_stringb(e->output, msg)) != 0)
698 fatal("%s: buffer error: %s", __func__, ssh_err(r));
699 sshbuf_free(msg);
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800700}
701
702#ifdef ENABLE_PKCS11
703static void
704process_add_smartcard_key(SocketEntry *e)
705{
706 char *provider = NULL, *pin;
Adam Langleyd0592972015-03-30 14:49:51 -0700707 int r, i, version, count = 0, success = 0, confirm = 0;
708 u_int seconds;
709 time_t death = 0;
710 u_char type;
711 struct sshkey **keys = NULL, *k;
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800712 Identity *id;
713 Idtab *tab;
714
Adam Langleyd0592972015-03-30 14:49:51 -0700715 if ((r = sshbuf_get_cstring(e->request, &provider, NULL)) != 0 ||
716 (r = sshbuf_get_cstring(e->request, &pin, NULL)) != 0)
717 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800718
Adam Langleyd0592972015-03-30 14:49:51 -0700719 while (sshbuf_len(e->request)) {
720 if ((r = sshbuf_get_u8(e->request, &type)) != 0)
721 fatal("%s: buffer error: %s", __func__, ssh_err(r));
722 switch (type) {
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800723 case SSH_AGENT_CONSTRAIN_LIFETIME:
Adam Langleyd0592972015-03-30 14:49:51 -0700724 if ((r = sshbuf_get_u32(e->request, &seconds)) != 0)
725 fatal("%s: buffer error: %s",
726 __func__, ssh_err(r));
727 death = monotime() + seconds;
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800728 break;
729 case SSH_AGENT_CONSTRAIN_CONFIRM:
730 confirm = 1;
731 break;
732 default:
733 error("process_add_smartcard_key: "
734 "Unknown constraint type %d", type);
735 goto send;
736 }
737 }
738 if (lifetime && !death)
Adam Langleyd0592972015-03-30 14:49:51 -0700739 death = monotime() + lifetime;
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800740
741 count = pkcs11_add_provider(provider, pin, &keys);
742 for (i = 0; i < count; i++) {
743 k = keys[i];
744 version = k->type == KEY_RSA1 ? 1 : 2;
745 tab = idtab_lookup(version);
746 if (lookup_identity(k, version) == NULL) {
747 id = xcalloc(1, sizeof(Identity));
748 id->key = k;
749 id->provider = xstrdup(provider);
750 id->comment = xstrdup(provider); /* XXX */
751 id->death = death;
752 id->confirm = confirm;
753 TAILQ_INSERT_TAIL(&tab->idlist, id, next);
754 tab->nentries++;
755 success = 1;
756 } else {
Adam Langleyd0592972015-03-30 14:49:51 -0700757 sshkey_free(k);
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800758 }
759 keys[i] = NULL;
760 }
761send:
Adam Langleyd0592972015-03-30 14:49:51 -0700762 free(pin);
763 free(provider);
764 free(keys);
765 send_status(e, success);
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800766}
767
768static void
769process_remove_smartcard_key(SocketEntry *e)
770{
771 char *provider = NULL, *pin = NULL;
Adam Langleyd0592972015-03-30 14:49:51 -0700772 int r, version, success = 0;
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800773 Identity *id, *nxt;
774 Idtab *tab;
775
Adam Langleyd0592972015-03-30 14:49:51 -0700776 if ((r = sshbuf_get_cstring(e->request, &provider, NULL)) != 0 ||
777 (r = sshbuf_get_cstring(e->request, &pin, NULL)) != 0)
778 fatal("%s: buffer error: %s", __func__, ssh_err(r));
779 free(pin);
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800780
781 for (version = 1; version < 3; version++) {
782 tab = idtab_lookup(version);
783 for (id = TAILQ_FIRST(&tab->idlist); id; id = nxt) {
784 nxt = TAILQ_NEXT(id, next);
Adam Langleyd0592972015-03-30 14:49:51 -0700785 /* Skip file--based keys */
786 if (id->provider == NULL)
787 continue;
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800788 if (!strcmp(provider, id->provider)) {
789 TAILQ_REMOVE(&tab->idlist, id, next);
790 free_identity(id);
791 tab->nentries--;
792 }
793 }
794 }
795 if (pkcs11_del_provider(provider) == 0)
796 success = 1;
797 else
798 error("process_remove_smartcard_key:"
799 " pkcs11_del_provider failed");
Adam Langleyd0592972015-03-30 14:49:51 -0700800 free(provider);
801 send_status(e, success);
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800802}
803#endif /* ENABLE_PKCS11 */
804
805/* dispatch incoming messages */
806
807static void
808process_message(SocketEntry *e)
809{
Adam Langleyd0592972015-03-30 14:49:51 -0700810 u_int msg_len;
811 u_char type;
812 const u_char *cp;
813 int r;
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800814
Adam Langleyd0592972015-03-30 14:49:51 -0700815 if (sshbuf_len(e->input) < 5)
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800816 return; /* Incomplete message. */
Adam Langleyd0592972015-03-30 14:49:51 -0700817 cp = sshbuf_ptr(e->input);
818 msg_len = PEEK_U32(cp);
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800819 if (msg_len > 256 * 1024) {
820 close_socket(e);
821 return;
822 }
Adam Langleyd0592972015-03-30 14:49:51 -0700823 if (sshbuf_len(e->input) < msg_len + 4)
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800824 return;
825
826 /* move the current input to e->request */
Adam Langleyd0592972015-03-30 14:49:51 -0700827 sshbuf_reset(e->request);
828 if ((r = sshbuf_get_stringb(e->input, e->request)) != 0 ||
829 (r = sshbuf_get_u8(e->request, &type)) != 0)
830 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800831
832 /* check wheter agent is locked */
833 if (locked && type != SSH_AGENTC_UNLOCK) {
Adam Langleyd0592972015-03-30 14:49:51 -0700834 sshbuf_reset(e->request);
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800835 switch (type) {
836 case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
837 case SSH2_AGENTC_REQUEST_IDENTITIES:
838 /* send empty lists */
839 no_identities(e, type);
840 break;
841 default:
842 /* send a fail message for all other request types */
Adam Langleyd0592972015-03-30 14:49:51 -0700843 send_status(e, 0);
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800844 }
845 return;
846 }
847
848 debug("type %d", type);
849 switch (type) {
850 case SSH_AGENTC_LOCK:
851 case SSH_AGENTC_UNLOCK:
852 process_lock_agent(e, type == SSH_AGENTC_LOCK);
853 break;
Adam Langleyd0592972015-03-30 14:49:51 -0700854#ifdef WITH_SSH1
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800855 /* ssh1 */
856 case SSH_AGENTC_RSA_CHALLENGE:
857 process_authentication_challenge1(e);
858 break;
859 case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
860 process_request_identities(e, 1);
861 break;
862 case SSH_AGENTC_ADD_RSA_IDENTITY:
863 case SSH_AGENTC_ADD_RSA_ID_CONSTRAINED:
864 process_add_identity(e, 1);
865 break;
866 case SSH_AGENTC_REMOVE_RSA_IDENTITY:
867 process_remove_identity(e, 1);
868 break;
Adam Langleyd0592972015-03-30 14:49:51 -0700869#endif
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800870 case SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES:
Adam Langleyd0592972015-03-30 14:49:51 -0700871 process_remove_all_identities(e, 1); /* safe for !WITH_SSH1 */
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800872 break;
873 /* ssh2 */
874 case SSH2_AGENTC_SIGN_REQUEST:
875 process_sign_request2(e);
876 break;
877 case SSH2_AGENTC_REQUEST_IDENTITIES:
878 process_request_identities(e, 2);
879 break;
880 case SSH2_AGENTC_ADD_IDENTITY:
881 case SSH2_AGENTC_ADD_ID_CONSTRAINED:
882 process_add_identity(e, 2);
883 break;
884 case SSH2_AGENTC_REMOVE_IDENTITY:
885 process_remove_identity(e, 2);
886 break;
887 case SSH2_AGENTC_REMOVE_ALL_IDENTITIES:
888 process_remove_all_identities(e, 2);
889 break;
890#ifdef ENABLE_PKCS11
891 case SSH_AGENTC_ADD_SMARTCARD_KEY:
892 case SSH_AGENTC_ADD_SMARTCARD_KEY_CONSTRAINED:
893 process_add_smartcard_key(e);
894 break;
895 case SSH_AGENTC_REMOVE_SMARTCARD_KEY:
896 process_remove_smartcard_key(e);
897 break;
898#endif /* ENABLE_PKCS11 */
899 default:
900 /* Unknown message. Respond with failure. */
901 error("Unknown message %d", type);
Adam Langleyd0592972015-03-30 14:49:51 -0700902 sshbuf_reset(e->request);
903 send_status(e, 0);
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800904 break;
905 }
906}
907
908static void
909new_socket(sock_type type, int fd)
910{
911 u_int i, old_alloc, new_alloc;
912
913 set_nonblock(fd);
914
915 if (fd > max_fd)
916 max_fd = fd;
917
918 for (i = 0; i < sockets_alloc; i++)
919 if (sockets[i].type == AUTH_UNUSED) {
920 sockets[i].fd = fd;
Adam Langleyd0592972015-03-30 14:49:51 -0700921 if ((sockets[i].input = sshbuf_new()) == NULL)
922 fatal("%s: sshbuf_new failed", __func__);
923 if ((sockets[i].output = sshbuf_new()) == NULL)
924 fatal("%s: sshbuf_new failed", __func__);
925 if ((sockets[i].request = sshbuf_new()) == NULL)
926 fatal("%s: sshbuf_new failed", __func__);
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800927 sockets[i].type = type;
928 return;
929 }
930 old_alloc = sockets_alloc;
931 new_alloc = sockets_alloc + 10;
932 sockets = xrealloc(sockets, new_alloc, sizeof(sockets[0]));
933 for (i = old_alloc; i < new_alloc; i++)
934 sockets[i].type = AUTH_UNUSED;
935 sockets_alloc = new_alloc;
936 sockets[old_alloc].fd = fd;
Adam Langleyd0592972015-03-30 14:49:51 -0700937 if ((sockets[old_alloc].input = sshbuf_new()) == NULL)
938 fatal("%s: sshbuf_new failed", __func__);
939 if ((sockets[old_alloc].output = sshbuf_new()) == NULL)
940 fatal("%s: sshbuf_new failed", __func__);
941 if ((sockets[old_alloc].request = sshbuf_new()) == NULL)
942 fatal("%s: sshbuf_new failed", __func__);
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800943 sockets[old_alloc].type = type;
944}
945
946static int
947prepare_select(fd_set **fdrp, fd_set **fdwp, int *fdl, u_int *nallocp,
948 struct timeval **tvpp)
949{
Adam Langleyd0592972015-03-30 14:49:51 -0700950 u_int i, sz;
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800951 int n = 0;
952 static struct timeval tv;
Adam Langleyd0592972015-03-30 14:49:51 -0700953 time_t deadline;
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800954
955 for (i = 0; i < sockets_alloc; i++) {
956 switch (sockets[i].type) {
957 case AUTH_SOCKET:
958 case AUTH_CONNECTION:
959 n = MAX(n, sockets[i].fd);
960 break;
961 case AUTH_UNUSED:
962 break;
963 default:
964 fatal("Unknown socket type %d", sockets[i].type);
965 break;
966 }
967 }
968
969 sz = howmany(n+1, NFDBITS) * sizeof(fd_mask);
970 if (*fdrp == NULL || sz > *nallocp) {
Adam Langleyd0592972015-03-30 14:49:51 -0700971 free(*fdrp);
972 free(*fdwp);
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800973 *fdrp = xmalloc(sz);
974 *fdwp = xmalloc(sz);
975 *nallocp = sz;
976 }
977 if (n < *fdl)
978 debug("XXX shrink: %d < %d", n, *fdl);
979 *fdl = n;
980 memset(*fdrp, 0, sz);
981 memset(*fdwp, 0, sz);
982
983 for (i = 0; i < sockets_alloc; i++) {
984 switch (sockets[i].type) {
985 case AUTH_SOCKET:
986 case AUTH_CONNECTION:
987 FD_SET(sockets[i].fd, *fdrp);
Adam Langleyd0592972015-03-30 14:49:51 -0700988 if (sshbuf_len(sockets[i].output) > 0)
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800989 FD_SET(sockets[i].fd, *fdwp);
990 break;
991 default:
992 break;
993 }
994 }
995 deadline = reaper();
996 if (parent_alive_interval != 0)
997 deadline = (deadline == 0) ? parent_alive_interval :
998 MIN(deadline, parent_alive_interval);
999 if (deadline == 0) {
1000 *tvpp = NULL;
1001 } else {
1002 tv.tv_sec = deadline;
1003 tv.tv_usec = 0;
1004 *tvpp = &tv;
1005 }
1006 return (1);
1007}
1008
1009static void
1010after_select(fd_set *readset, fd_set *writeset)
1011{
1012 struct sockaddr_un sunaddr;
1013 socklen_t slen;
1014 char buf[1024];
Adam Langleyd0592972015-03-30 14:49:51 -07001015 int len, sock, r;
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001016 u_int i, orig_alloc;
1017 uid_t euid;
1018 gid_t egid;
1019
1020 for (i = 0, orig_alloc = sockets_alloc; i < orig_alloc; i++)
1021 switch (sockets[i].type) {
1022 case AUTH_UNUSED:
1023 break;
1024 case AUTH_SOCKET:
1025 if (FD_ISSET(sockets[i].fd, readset)) {
1026 slen = sizeof(sunaddr);
1027 sock = accept(sockets[i].fd,
1028 (struct sockaddr *)&sunaddr, &slen);
1029 if (sock < 0) {
1030 error("accept from AUTH_SOCKET: %s",
1031 strerror(errno));
1032 break;
1033 }
1034 if (getpeereid(sock, &euid, &egid) < 0) {
1035 error("getpeereid %d failed: %s",
1036 sock, strerror(errno));
1037 close(sock);
1038 break;
1039 }
1040 if ((euid != 0) && (getuid() != euid)) {
1041 error("uid mismatch: "
1042 "peer euid %u != uid %u",
1043 (u_int) euid, (u_int) getuid());
1044 close(sock);
1045 break;
1046 }
1047 new_socket(AUTH_CONNECTION, sock);
1048 }
1049 break;
1050 case AUTH_CONNECTION:
Adam Langleyd0592972015-03-30 14:49:51 -07001051 if (sshbuf_len(sockets[i].output) > 0 &&
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001052 FD_ISSET(sockets[i].fd, writeset)) {
1053 len = write(sockets[i].fd,
Adam Langleyd0592972015-03-30 14:49:51 -07001054 sshbuf_ptr(sockets[i].output),
1055 sshbuf_len(sockets[i].output));
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001056 if (len == -1 && (errno == EAGAIN ||
1057 errno == EWOULDBLOCK ||
1058 errno == EINTR))
1059 continue;
1060 if (len <= 0) {
1061 close_socket(&sockets[i]);
1062 break;
1063 }
Adam Langleyd0592972015-03-30 14:49:51 -07001064 if ((r = sshbuf_consume(sockets[i].output,
1065 len)) != 0)
1066 fatal("%s: buffer error: %s",
1067 __func__, ssh_err(r));
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001068 }
1069 if (FD_ISSET(sockets[i].fd, readset)) {
1070 len = read(sockets[i].fd, buf, sizeof(buf));
1071 if (len == -1 && (errno == EAGAIN ||
1072 errno == EWOULDBLOCK ||
1073 errno == EINTR))
1074 continue;
1075 if (len <= 0) {
1076 close_socket(&sockets[i]);
1077 break;
1078 }
Adam Langleyd0592972015-03-30 14:49:51 -07001079 if ((r = sshbuf_put(sockets[i].input,
1080 buf, len)) != 0)
1081 fatal("%s: buffer error: %s",
1082 __func__, ssh_err(r));
1083 explicit_bzero(buf, sizeof(buf));
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001084 process_message(&sockets[i]);
1085 }
1086 break;
1087 default:
1088 fatal("Unknown type %d", sockets[i].type);
1089 }
1090}
1091
1092static void
1093cleanup_socket(void)
1094{
Adam Langleyd0592972015-03-30 14:49:51 -07001095 if (cleanup_pid != 0 && getpid() != cleanup_pid)
1096 return;
1097 debug("%s: cleanup", __func__);
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001098 if (socket_name[0])
1099 unlink(socket_name);
1100 if (socket_dir[0])
1101 rmdir(socket_dir);
1102}
1103
1104void
1105cleanup_exit(int i)
1106{
1107 cleanup_socket();
1108 _exit(i);
1109}
1110
1111/*ARGSUSED*/
1112static void
1113cleanup_handler(int sig)
1114{
1115 cleanup_socket();
1116#ifdef ENABLE_PKCS11
1117 pkcs11_terminate();
1118#endif
1119 _exit(2);
1120}
1121
1122static void
1123check_parent_exists(void)
1124{
1125 /*
1126 * If our parent has exited then getppid() will return (pid_t)1,
1127 * so testing for that should be safe.
1128 */
1129 if (parent_pid != -1 && getppid() != parent_pid) {
1130 /* printf("Parent has died - Authentication agent exiting.\n"); */
1131 cleanup_socket();
1132 _exit(2);
1133 }
1134}
1135
1136static void
1137usage(void)
1138{
Adam Langleyd0592972015-03-30 14:49:51 -07001139 fprintf(stderr,
1140 "usage: ssh-agent [-c | -s] [-d] [-a bind_address] [-E fingerprint_hash]\n"
1141 " [-t life] [command [arg ...]]\n"
1142 " ssh-agent [-c | -s] -k\n");
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001143 exit(1);
1144}
1145
1146int
1147main(int ac, char **av)
1148{
1149 int c_flag = 0, d_flag = 0, k_flag = 0, s_flag = 0;
1150 int sock, fd, ch, result, saved_errno;
1151 u_int nalloc;
1152 char *shell, *format, *pidstr, *agentsocket = NULL;
1153 fd_set *readsetp = NULL, *writesetp = NULL;
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001154#ifdef HAVE_SETRLIMIT
1155 struct rlimit rlim;
1156#endif
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001157 extern int optind;
1158 extern char *optarg;
1159 pid_t pid;
1160 char pidstrbuf[1 + 3 * sizeof pid];
1161 struct timeval *tvp = NULL;
1162 size_t len;
Adam Langleyd0592972015-03-30 14:49:51 -07001163 mode_t prev_mask;
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001164
1165 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
1166 sanitise_stdfd();
1167
1168 /* drop */
1169 setegid(getgid());
1170 setgid(getgid());
1171
1172#if defined(HAVE_PRCTL) && defined(PR_SET_DUMPABLE)
1173 /* Disable ptrace on Linux without sgid bit */
1174 prctl(PR_SET_DUMPABLE, 0);
1175#endif
1176
Adam Langleyd0592972015-03-30 14:49:51 -07001177#ifdef WITH_OPENSSL
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001178 OpenSSL_add_all_algorithms();
Adam Langleyd0592972015-03-30 14:49:51 -07001179#endif
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001180
1181 __progname = ssh_get_progname(av[0]);
1182 seed_rng();
1183
Adam Langleyd0592972015-03-30 14:49:51 -07001184 while ((ch = getopt(ac, av, "cdksE:a:t:")) != -1) {
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001185 switch (ch) {
Adam Langleyd0592972015-03-30 14:49:51 -07001186 case 'E':
1187 fingerprint_hash = ssh_digest_alg_by_name(optarg);
1188 if (fingerprint_hash == -1)
1189 fatal("Invalid hash algorithm \"%s\"", optarg);
1190 break;
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001191 case 'c':
1192 if (s_flag)
1193 usage();
1194 c_flag++;
1195 break;
1196 case 'k':
1197 k_flag++;
1198 break;
1199 case 's':
1200 if (c_flag)
1201 usage();
1202 s_flag++;
1203 break;
1204 case 'd':
1205 if (d_flag)
1206 usage();
1207 d_flag++;
1208 break;
1209 case 'a':
1210 agentsocket = optarg;
1211 break;
1212 case 't':
1213 if ((lifetime = convtime(optarg)) == -1) {
1214 fprintf(stderr, "Invalid lifetime\n");
1215 usage();
1216 }
1217 break;
1218 default:
1219 usage();
1220 }
1221 }
1222 ac -= optind;
1223 av += optind;
1224
1225 if (ac > 0 && (c_flag || k_flag || s_flag || d_flag))
1226 usage();
1227
1228 if (ac == 0 && !c_flag && !s_flag) {
1229 shell = getenv("SHELL");
1230 if (shell != NULL && (len = strlen(shell)) > 2 &&
1231 strncmp(shell + len - 3, "csh", 3) == 0)
1232 c_flag = 1;
1233 }
1234 if (k_flag) {
1235 const char *errstr = NULL;
1236
1237 pidstr = getenv(SSH_AGENTPID_ENV_NAME);
1238 if (pidstr == NULL) {
1239 fprintf(stderr, "%s not set, cannot kill agent\n",
1240 SSH_AGENTPID_ENV_NAME);
1241 exit(1);
1242 }
1243 pid = (int)strtonum(pidstr, 2, INT_MAX, &errstr);
1244 if (errstr) {
1245 fprintf(stderr,
1246 "%s=\"%s\", which is not a good PID: %s\n",
1247 SSH_AGENTPID_ENV_NAME, pidstr, errstr);
1248 exit(1);
1249 }
1250 if (kill(pid, SIGTERM) == -1) {
1251 perror("kill");
1252 exit(1);
1253 }
1254 format = c_flag ? "unsetenv %s;\n" : "unset %s;\n";
1255 printf(format, SSH_AUTHSOCKET_ENV_NAME);
1256 printf(format, SSH_AGENTPID_ENV_NAME);
1257 printf("echo Agent pid %ld killed;\n", (long)pid);
1258 exit(0);
1259 }
1260 parent_pid = getpid();
1261
1262 if (agentsocket == NULL) {
1263 /* Create private directory for agent socket */
1264 mktemp_proto(socket_dir, sizeof(socket_dir));
1265 if (mkdtemp(socket_dir) == NULL) {
1266 perror("mkdtemp: private socket dir");
1267 exit(1);
1268 }
1269 snprintf(socket_name, sizeof socket_name, "%s/agent.%ld", socket_dir,
1270 (long)parent_pid);
1271 } else {
1272 /* Try to use specified agent socket */
1273 socket_dir[0] = '\0';
1274 strlcpy(socket_name, agentsocket, sizeof socket_name);
1275 }
1276
1277 /*
1278 * Create socket early so it will exist before command gets run from
1279 * the parent.
1280 */
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001281 prev_mask = umask(0177);
Adam Langleyd0592972015-03-30 14:49:51 -07001282 sock = unix_listener(socket_name, SSH_LISTEN_BACKLOG, 0);
1283 if (sock < 0) {
1284 /* XXX - unix_listener() calls error() not perror() */
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001285 *socket_name = '\0'; /* Don't unlink any existing file */
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001286 cleanup_exit(1);
1287 }
1288 umask(prev_mask);
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001289
1290 /*
1291 * Fork, and have the parent execute the command, if any, or present
1292 * the socket data. The child continues as the authentication agent.
1293 */
1294 if (d_flag) {
1295 log_init(__progname, SYSLOG_LEVEL_DEBUG1, SYSLOG_FACILITY_AUTH, 1);
1296 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
1297 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
1298 SSH_AUTHSOCKET_ENV_NAME);
1299 printf("echo Agent pid %ld;\n", (long)parent_pid);
1300 goto skip;
1301 }
1302 pid = fork();
1303 if (pid == -1) {
1304 perror("fork");
1305 cleanup_exit(1);
1306 }
1307 if (pid != 0) { /* Parent - execute the given command. */
1308 close(sock);
1309 snprintf(pidstrbuf, sizeof pidstrbuf, "%ld", (long)pid);
1310 if (ac == 0) {
1311 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
1312 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
1313 SSH_AUTHSOCKET_ENV_NAME);
1314 printf(format, SSH_AGENTPID_ENV_NAME, pidstrbuf,
1315 SSH_AGENTPID_ENV_NAME);
1316 printf("echo Agent pid %ld;\n", (long)pid);
1317 exit(0);
1318 }
1319 if (setenv(SSH_AUTHSOCKET_ENV_NAME, socket_name, 1) == -1 ||
1320 setenv(SSH_AGENTPID_ENV_NAME, pidstrbuf, 1) == -1) {
1321 perror("setenv");
1322 exit(1);
1323 }
1324 execvp(av[0], av);
1325 perror(av[0]);
1326 exit(1);
1327 }
1328 /* child */
1329 log_init(__progname, SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_AUTH, 0);
1330
1331 if (setsid() == -1) {
1332 error("setsid: %s", strerror(errno));
1333 cleanup_exit(1);
1334 }
1335
1336 (void)chdir("/");
1337 if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) {
1338 /* XXX might close listen socket */
1339 (void)dup2(fd, STDIN_FILENO);
1340 (void)dup2(fd, STDOUT_FILENO);
1341 (void)dup2(fd, STDERR_FILENO);
1342 if (fd > 2)
1343 close(fd);
1344 }
1345
1346#ifdef HAVE_SETRLIMIT
1347 /* deny core dumps, since memory contains unencrypted private keys */
1348 rlim.rlim_cur = rlim.rlim_max = 0;
1349 if (setrlimit(RLIMIT_CORE, &rlim) < 0) {
1350 error("setrlimit RLIMIT_CORE: %s", strerror(errno));
1351 cleanup_exit(1);
1352 }
1353#endif
1354
1355skip:
1356
Adam Langleyd0592972015-03-30 14:49:51 -07001357 cleanup_pid = getpid();
1358
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001359#ifdef ENABLE_PKCS11
1360 pkcs11_init(0);
1361#endif
1362 new_socket(AUTH_SOCKET, sock);
1363 if (ac > 0)
1364 parent_alive_interval = 10;
1365 idtab_init();
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001366 signal(SIGPIPE, SIG_IGN);
Adam Langleyd0592972015-03-30 14:49:51 -07001367 signal(SIGINT, d_flag ? cleanup_handler : SIG_IGN);
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001368 signal(SIGHUP, cleanup_handler);
1369 signal(SIGTERM, cleanup_handler);
1370 nalloc = 0;
1371
1372 while (1) {
1373 prepare_select(&readsetp, &writesetp, &max_fd, &nalloc, &tvp);
1374 result = select(max_fd + 1, readsetp, writesetp, NULL, tvp);
1375 saved_errno = errno;
1376 if (parent_alive_interval != 0)
1377 check_parent_exists();
1378 (void) reaper(); /* remove expired keys */
1379 if (result < 0) {
1380 if (saved_errno == EINTR)
1381 continue;
1382 fatal("select: %s", strerror(saved_errno));
1383 } else if (result > 0)
1384 after_select(readsetp, writesetp);
1385 }
1386 /* NOTREACHED */
1387}