~ubuntu-branches/ubuntu/trusty/libssh/trusty

« back to all changes in this revision

Viewing changes to examples/knownhosts.c

  • Committer: Bazaar Package Importer
  • Author(s): Laurent Bigonville
  • Date: 2009-12-12 14:29:12 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20091212142912-ha5g2iibt6nfnjq8
Tags: 0.4.0-1
* New upstream release.
  - Bump soname
  - Adjust .symbols file
* Readd static library in -dev package
* Let dh_lintian install override file
* debian/README.Debian: Update file
* debian/rules: Add list-missing rule

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * knownhosts.c
 
3
 * This file contains an example of how verify the identity of a
 
4
 * SSH server using libssh
 
5
 */
 
6
 
 
7
/*
 
8
Copyright 2003-2009 Aris Adamantiadis
 
9
 
 
10
This file is part of the SSH Library
 
11
 
 
12
You are free to copy this file, modify it in any way, consider it being public
 
13
domain. This does not apply to the rest of the library though, but it is
 
14
allowed to cut-and-paste working code from this file to any license of
 
15
program.
 
16
The goal is to show the API in action. It's not a reference on how terminal
 
17
clients must be made or how a client should react.
 
18
 */
 
19
 
 
20
#include <errno.h>
 
21
#include <stdio.h>
 
22
#include <stdlib.h>
 
23
#include <unistd.h>
 
24
#include <string.h>
 
25
 
 
26
#include <libssh/libssh.h>
 
27
#include "examples_common.h"
 
28
 
 
29
int verify_knownhost(ssh_session session){
 
30
  char *hexa;
 
31
  int state;
 
32
  char buf[10];
 
33
  unsigned char *hash = NULL;
 
34
  int hlen;
 
35
 
 
36
  state=ssh_is_server_known(session);
 
37
 
 
38
  hlen = ssh_get_pubkey_hash(session, &hash);
 
39
  if (hlen < 0) {
 
40
    return -1;
 
41
  }
 
42
  switch(state){
 
43
    case SSH_SERVER_KNOWN_OK:
 
44
      break; /* ok */
 
45
    case SSH_SERVER_KNOWN_CHANGED:
 
46
      fprintf(stderr,"Host key for server changed : server's one is now :\n");
 
47
      ssh_print_hexa("Public key hash",hash, hlen);
 
48
      free(hash);
 
49
      fprintf(stderr,"For security reason, connection will be stopped\n");
 
50
      return -1;
 
51
    case SSH_SERVER_FOUND_OTHER:
 
52
      fprintf(stderr,"The host key for this server was not found but an other type of key exists.\n");
 
53
      fprintf(stderr,"An attacker might change the default server key to confuse your client"
 
54
          "into thinking the key does not exist\n"
 
55
          "We advise you to rerun the client with -d or -r for more safety.\n");
 
56
      return -1;
 
57
    case SSH_SERVER_FILE_NOT_FOUND:
 
58
      fprintf(stderr,"Could not find known host file. If you accept the host key here,\n");
 
59
      fprintf(stderr,"the file will be automatically created.\n");
 
60
      /* fallback to SSH_SERVER_NOT_KNOWN behavior */
 
61
    case SSH_SERVER_NOT_KNOWN:
 
62
      hexa = ssh_get_hexa(hash, hlen);
 
63
      fprintf(stderr,"The server is unknown. Do you trust the host key ?\n");
 
64
      fprintf(stderr, "Public key hash: %s\n", hexa);
 
65
      free(hexa);
 
66
      fgets(buf,sizeof(buf),stdin);
 
67
      if(strncasecmp(buf,"yes",3)!=0){
 
68
        return -1;
 
69
      }
 
70
      fprintf(stderr,"This new key will be written on disk for further usage. do you agree ?\n");
 
71
      fgets(buf,sizeof(buf),stdin);
 
72
      if(strncasecmp(buf,"yes",3)==0){
 
73
        if (ssh_write_knownhost(session) < 0) {
 
74
          free(hash);
 
75
          fprintf(stderr, "error %s\n", strerror(errno));
 
76
          return -1;
 
77
        }
 
78
      }
 
79
 
 
80
      break;
 
81
    case SSH_SERVER_ERROR:
 
82
      free(hash);
 
83
      fprintf(stderr,"%s",ssh_get_error(session));
 
84
      return -1;
 
85
  }
 
86
  free(hash);
 
87
  return 0;
 
88
}