Improve ssh_get_knownhost_line.

git-svn-id: svn+ssh://svn.berlios.de/svnroot/repos/libssh/trunk@631 7dcaeef0-15fb-0310-b436-a5af3365683c
This commit is contained in:
Andreas Schneider
2009-04-27 17:52:09 +00:00
parent b1e62ad22b
commit a19aebba18

View File

@@ -1021,6 +1021,7 @@ static void tokens_free(char **tokens) {
*/ */
SAFE_FREE(tokens); SAFE_FREE(tokens);
} }
/** \brief returns one line of known host file /** \brief returns one line of known host file
* will return a token array containing (host|ip) keytype key * will return a token array containing (host|ip) keytype key
* \param file pointer to the known host file. Could be pointing to NULL at start * \param file pointer to the known host file. Could be pointing to NULL at start
@@ -1030,40 +1031,52 @@ static void tokens_free(char **tokens) {
* \returns NULL if no match was found or the file was not found * \returns NULL if no match was found or the file was not found
* \returns found_type type of key (ie "dsa","ssh-rsa1"). Don't free that value. * \returns found_type type of key (ie "dsa","ssh-rsa1"). Don't free that value.
*/ */
static char **ssh_get_knownhost_line(SSH_SESSION *session, FILE **file, static char **ssh_get_knownhost_line(SSH_SESSION *session, FILE **file,
const char *filename, const char **found_type) { const char *filename, const char **found_type) {
char buffer[4096]; char buffer[4096] = {0};
char *ptr; char *ptr;
char **tokens; char **tokens;
enter_function(); enter_function();
if(!*file){
*file=fopen(filename,"r"); if(*file == NULL){
if(!file){ *file = fopen(filename,"r");
if (*file == NULL) {
leave_function(); leave_function();
return NULL; return NULL;
} }
} }
while(fgets(buffer,sizeof(buffer),*file)){
ptr=strchr(buffer,'\n'); while (fgets(buffer, sizeof(buffer), *file)) {
if(ptr) *ptr=0; ptr = strchr(buffer, '\n');
if((ptr=strchr(buffer,'\r'))) *ptr=0; if (ptr) {
if(!buffer[0] || buffer[0]=='#') }
ptr = strchr(buffer,'\r');
if (ptr) {
*ptr = '\0';
}
if (!buffer[0] || buffer[0] == '#') {
continue; /* skip empty lines */ continue; /* skip empty lines */
tokens=space_tokenize(buffer); }
tokens = space_tokenize(buffer);
if (tokens == NULL) { if (tokens == NULL) {
fclose(*file); fclose(*file);
*file = NULL; *file = NULL;
leave_function(); leave_function();
return NULL; return NULL;
} }
if(!tokens[0] || !tokens[1] || !tokens[2]){
if(!tokens[0] || !tokens[1] || !tokens[2]) {
/* it should have at least 3 tokens */ /* it should have at least 3 tokens */
tokens_free(tokens); tokens_free(tokens);
continue; continue;
} }
*found_type = tokens[1]; *found_type = tokens[1];
if(tokens[3]){ if (tokens[3]) {
/* openssh rsa1 format has 4 tokens on the line. Recognize it /* openssh rsa1 format has 4 tokens on the line. Recognize it
by the fact that everything is all digits */ by the fact that everything is all digits */
if (tokens[4]) { if (tokens[4]) {
@@ -1082,8 +1095,10 @@ static char **ssh_get_knownhost_line(SSH_SESSION *session, FILE **file,
leave_function(); leave_function();
return tokens; return tokens;
} }
fclose(*file); fclose(*file);
*file=NULL; *file = NULL;
/* we did not find anything, end of file*/ /* we did not find anything, end of file*/
leave_function(); leave_function();
return NULL; return NULL;