From 8275fade44b56aed722ea91bc4586f48babece80 Mon Sep 17 00:00:00 2001 From: Damien Miller Date: Wed, 15 Mar 2006 12:06:23 +1100 Subject: [PATCH] - dtucker@cvs.openbsd.org 2006/03/13 10:26:52 [authfile.c authfile.h ssh-add.c] Make ssh-add check file permissions before attempting to load private key files multiple times; it will fail anyway and this prevents confusing multiple prompts and warnings. mindrot #1138, ok djm@ --- ChangeLog | 7 ++++++- authfile.c | 4 ++-- authfile.h | 3 ++- ssh-add.c | 17 +++++++++++++---- 4 files changed, 23 insertions(+), 8 deletions(-) diff --git a/ChangeLog b/ChangeLog index c72eeed41..d41b4cb46 100644 --- a/ChangeLog +++ b/ChangeLog @@ -224,6 +224,11 @@ [misc.c ssh_config.5 sshd_config.5] Allow config directives to contain whitespace by surrounding them by double quotes. mindrot #482, man page help from jmc@, ok djm@ + - dtucker@cvs.openbsd.org 2006/03/13 10:26:52 + [authfile.c authfile.h ssh-add.c] + Make ssh-add check file permissions before attempting to load private + key files multiple times; it will fail anyway and this prevents confusing + multiple prompts and warnings. mindrot #1138, ok djm@ 20060313 - (dtucker) [configure.ac] Bug #1171: Don't use printf("%lld", longlong) @@ -4125,4 +4130,4 @@ - (djm) Trim deprecated options from INSTALL. Mention UsePAM - (djm) Fix quote handling in sftp; Patch from admorten AT umich.edu -$Id: ChangeLog,v 1.4203 2006/03/15 01:05:59 djm Exp $ +$Id: ChangeLog,v 1.4204 2006/03/15 01:06:23 djm Exp $ diff --git a/authfile.c b/authfile.c index f97cf1820..0656262d0 100644 --- a/authfile.c +++ b/authfile.c @@ -36,7 +36,7 @@ */ #include "includes.h" -RCSID("$OpenBSD: authfile.c,v 1.62 2006/02/20 17:19:54 stevesk Exp $"); +RCSID("$OpenBSD: authfile.c,v 1.63 2006/03/13 10:26:52 dtucker Exp $"); #include #include @@ -510,7 +510,7 @@ key_load_private_pem(int fd, int type, const char *passphrase, return prv; } -static int +int key_perm_ok(int fd, const char *filename) { struct stat st; diff --git a/authfile.h b/authfile.h index 7f92701ec..a16caa7a8 100644 --- a/authfile.h +++ b/authfile.h @@ -1,4 +1,4 @@ -/* $OpenBSD: authfile.h,v 1.10 2002/05/23 19:24:30 markus Exp $ */ +/* $OpenBSD: authfile.h,v 1.11 2006/03/13 10:26:52 dtucker Exp $ */ /* * Author: Tatu Ylonen @@ -21,5 +21,6 @@ Key *key_load_public_type(int, const char *, char **); Key *key_load_private(const char *, const char *, char **); Key *key_load_private_type(int, const char *, const char *, char **); Key *key_load_private_pem(int, int, const char *, char **); +int key_perm_ok(int, const char *); #endif diff --git a/ssh-add.c b/ssh-add.c index 8bfc401e8..59933012d 100644 --- a/ssh-add.c +++ b/ssh-add.c @@ -35,7 +35,7 @@ */ #include "includes.h" -RCSID("$OpenBSD: ssh-add.c,v 1.75 2006/02/20 17:19:54 stevesk Exp $"); +RCSID("$OpenBSD: ssh-add.c,v 1.76 2006/03/13 10:26:52 dtucker Exp $"); #include #include @@ -127,16 +127,25 @@ delete_all(AuthenticationConnection *ac) static int add_file(AuthenticationConnection *ac, const char *filename) { - struct stat st; Key *private; char *comment = NULL; char msg[1024]; - int ret = -1; + int fd, perms_ok, ret = -1; - if (stat(filename, &st) < 0) { + if ((fd = open(filename, 0)) < 0) { perror(filename); return -1; } + + /* + * Since we'll try to load a keyfile multiple times, permission errors + * will occur multiple times, so check perms first and bail if wrong. + */ + perms_ok = key_perm_ok(fd, filename); + close(fd); + if (!perms_ok) + return -1; + /* At first, try empty passphrase */ private = key_load_private(filename, "", &comment); if (comment == NULL)