X-Git-Url: http://andersk.mit.edu/gitweb/openssh.git/blobdiff_plain/528afafa5c8fc71666c64244055cd89835524a0e..dc11a83afe9daca9d29ef753205ad8479550b750:/authfile.c?ds=sidebyside diff --git a/authfile.c b/authfile.c index 1c006c43..735c6478 100644 --- a/authfile.c +++ b/authfile.c @@ -1,3 +1,4 @@ +/* $OpenBSD: authfile.c,v 1.76 2006/08/03 03:34:41 deraadt Exp $ */ /* * Author: Tatu Ylonen * Copyright (c) 1995 Tatu Ylonen , Espoo, Finland @@ -36,21 +37,34 @@ */ #include "includes.h" -RCSID("$OpenBSD: authfile.c,v 1.58 2004/08/23 11:48:09 djm Exp $"); + +#include +#include +#include +#include #include #include #include -#include "cipher.h" +#include +#include +#include +#include +#include +#include +#include + #include "xmalloc.h" +#include "cipher.h" #include "buffer.h" -#include "bufaux.h" #include "key.h" #include "ssh.h" #include "log.h" #include "authfile.h" #include "rsa.h" +#include "misc.h" +#include "atomicio.h" /* Version identification string for SSH v1 identity files. */ static const char authfile_id_string[] = @@ -146,8 +160,8 @@ key_save_private_rsa1(Key *key, const char *filename, const char *passphrase, buffer_free(&encrypted); return 0; } - if (write(fd, buffer_ptr(&encrypted), buffer_len(&encrypted)) != - buffer_len(&encrypted)) { + if (atomicio(vwrite, fd, buffer_ptr(&encrypted), + buffer_len(&encrypted)) != buffer_len(&encrypted)) { error("write to key file %s failed: %s", filename, strerror(errno)); buffer_free(&encrypted); @@ -182,7 +196,7 @@ key_save_private_pem(Key *key, const char *filename, const char *_passphrase, return 0; } fp = fdopen(fd, "w"); - if (fp == NULL ) { + if (fp == NULL) { error("fdopen %s failed: %s.", filename, strerror(errno)); close(fd); return 0; @@ -209,12 +223,10 @@ key_save_private(Key *key, const char *filename, const char *passphrase, case KEY_RSA1: return key_save_private_rsa1(key, filename, passphrase, comment); - break; case KEY_DSA: case KEY_RSA: return key_save_private_pem(key, filename, passphrase, comment); - break; default: break; } @@ -235,7 +247,7 @@ key_load_public_rsa1(int fd, const char *filename, char **commentp) Key *pub; struct stat st; char *cp; - int i; + u_int i; size_t len; if (fstat(fd, &st) < 0) { @@ -252,7 +264,7 @@ key_load_public_rsa1(int fd, const char *filename, char **commentp) buffer_init(&buffer); cp = buffer_append_space(&buffer, len); - if (read(fd, cp, (size_t) len) != (size_t) len) { + if (atomicio(read, fd, cp, len) != len) { debug("Read from key file %.200s failed: %.100s", filename, strerror(errno)); buffer_free(&buffer); @@ -321,7 +333,8 @@ static Key * key_load_private_rsa1(int fd, const char *filename, const char *passphrase, char **commentp) { - int i, check1, check2, cipher_type; + u_int i; + int check1, check2, cipher_type; size_t len; Buffer buffer, decrypted; u_char *cp; @@ -346,7 +359,7 @@ key_load_private_rsa1(int fd, const char *filename, const char *passphrase, buffer_init(&buffer); cp = buffer_append_space(&buffer, len); - if (read(fd, cp, (size_t) len) != (size_t) len) { + if (atomicio(read, fd, cp, len) != len) { debug("Read from key file %.200s failed: %.100s", filename, strerror(errno)); buffer_free(&buffer); @@ -504,7 +517,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; @@ -534,7 +547,7 @@ key_perm_ok(int fd, const char *filename) Key * key_load_private_type(int type, const char *filename, const char *passphrase, - char **commentp) + char **commentp, int *perm_ok) { int fd; @@ -542,22 +555,24 @@ key_load_private_type(int type, const char *filename, const char *passphrase, if (fd < 0) return NULL; if (!key_perm_ok(fd, filename)) { + if (perm_ok != NULL) + *perm_ok = 0; error("bad permissions: ignore key: %s", filename); close(fd); return NULL; } + if (perm_ok != NULL) + *perm_ok = 1; switch (type) { case KEY_RSA1: return key_load_private_rsa1(fd, filename, passphrase, commentp); /* closes fd */ - break; case KEY_DSA: case KEY_RSA: case KEY_UNSPEC: return key_load_private_pem(fd, type, passphrase, commentp); /* closes fd */ - break; default: close(fd); break; @@ -601,13 +616,14 @@ static int key_try_load_public(Key *k, const char *filename, char **commentp) { FILE *f; - char line[4096]; + char line[SSH_MAX_PUBKEY_BYTES]; char *cp; + u_long linenum = 0; f = fopen(filename, "r"); if (f != NULL) { - while (fgets(line, sizeof(line), f)) { - line[sizeof(line)-1] = '\0'; + while (read_keyfile_line(f, filename, line, sizeof(line), + &linenum) != -1) { cp = line; switch (*cp) { case '#':