]> andersk Git - openssh.git/blobdiff - sshconnect2.c
- markus@cvs.openbsd.org 2001/07/23 09:06:28
[openssh.git] / sshconnect2.c
index 2f26aa5699711438bbf3518304dc61e8b11d418e..a86d0036b70734ec7a3e491d5b89616ea4cf9a60 100644 (file)
@@ -23,7 +23,7 @@
  */
 
 #include "includes.h"
-RCSID("$OpenBSD: sshconnect2.c,v 1.65 2001/04/04 14:34:58 markus Exp $");
+RCSID("$OpenBSD: sshconnect2.c,v 1.81 2001/07/23 09:06:28 markus Exp $");
 
 #include <openssl/bn.h>
 #include <openssl/md5.h>
@@ -45,7 +45,6 @@ RCSID("$OpenBSD: sshconnect2.c,v 1.65 2001/04/04 14:34:58 markus Exp $");
 #include "key.h"
 #include "sshconnect.h"
 #include "authfile.h"
-#include "cli.h"
 #include "dh.h"
 #include "authfd.h"
 #include "log.h"
@@ -53,6 +52,7 @@ RCSID("$OpenBSD: sshconnect2.c,v 1.65 2001/04/04 14:34:58 markus Exp $");
 #include "readpass.h"
 #include "match.h"
 #include "dispatch.h"
+#include "canohost.h"
 
 /* import */
 extern char *client_version_string;
@@ -71,11 +71,11 @@ struct sockaddr *xxx_hostaddr;
 
 Kex *xxx_kex = NULL;
 
-int
-check_host_key_callback(Key *hostkey)
+static int
+verify_host_key_callback(Key *hostkey)
 {
-       check_host_key(xxx_host, xxx_hostaddr, hostkey,
-           options.user_hostfile2, options.system_hostfile2);
+       if (verify_host_key(xxx_host, xxx_hostaddr, hostkey) == -1)
+               fatal("verify_host_key failed");
        return 0;
 }
 
@@ -110,16 +110,19 @@ ssh_kex2(char *host, struct sockaddr *hostaddr)
                myproposal[PROPOSAL_MAC_ALGS_CTOS] =
                myproposal[PROPOSAL_MAC_ALGS_STOC] = options.macs;
        }
+       if (options.hostkeyalgorithms != NULL)
+               myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] =
+                   options.hostkeyalgorithms;
 
        /* start key exchange */
        kex = kex_setup(myproposal);
        kex->client_version_string=client_version_string;
        kex->server_version_string=server_version_string;
-       kex->check_host_key=&check_host_key_callback;
+       kex->verify_host_key=&verify_host_key_callback;
 
        xxx_kex = kex;
 
-       dispatch_run(DISPATCH_BLOCK, &kex->newkeys, kex);
+       dispatch_run(DISPATCH_BLOCK, &kex->done, kex);
 
        session_id2 = kex->session_id;
        session_id2_len = kex->session_id_len;
@@ -147,15 +150,20 @@ typedef int sign_cb_fn(
 
 struct Authctxt {
        const char *server_user;
+       const char *local_user;
        const char *host;
        const char *service;
-       AuthenticationConnection *agent;
        Authmethod *method;
        int success;
        char *authlist;
+       /* pubkey */
        Key *last_key;
        sign_cb_fn *last_key_sign;
        int last_key_hint;
+       AuthenticationConnection *agent;
+       /* hostbased */
+       Key **keys;
+       int nkeys;
 };
 struct Authmethod {
        char    *name;          /* string to compare against server's list */
@@ -175,31 +183,34 @@ int       userauth_none(Authctxt *authctxt);
 int    userauth_pubkey(Authctxt *authctxt);
 int    userauth_passwd(Authctxt *authctxt);
 int    userauth_kbdint(Authctxt *authctxt);
+int    userauth_hostbased(Authctxt *authctxt);
 
 void   userauth(Authctxt *authctxt, char *authlist);
 
-int
-sign_and_send_pubkey(Authctxt *authctxt, Key *k,
-    sign_cb_fn *sign_callback);
-void   clear_auth_state(Authctxt *authctxt);
+static int sign_and_send_pubkey(Authctxt *, Key *, sign_cb_fn *);
+static void clear_auth_state(Authctxt *);
 
-Authmethod *authmethod_get(char *authlist);
-Authmethod *authmethod_lookup(const char *name);
-char *authmethods_get(void);
+static Authmethod *authmethod_get(char *authlist);
+static Authmethod *authmethod_lookup(const char *name);
+static char *authmethods_get(void);
 
 Authmethod authmethods[] = {
+       {"hostbased",
+               userauth_hostbased,
+               &options.hostbased_authentication,
+               NULL},
        {"publickey",
                userauth_pubkey,
                &options.pubkey_authentication,
                NULL},
-       {"password",
-               userauth_passwd,
-               &options.password_authentication,
-               &options.batch_mode},
        {"keyboard-interactive",
                userauth_kbdint,
                &options.kbd_interactive_authentication,
                &options.batch_mode},
+       {"password",
+               userauth_passwd,
+               &options.password_authentication,
+               &options.batch_mode},
        {"none",
                userauth_none,
                NULL,
@@ -208,13 +219,14 @@ Authmethod authmethods[] = {
 };
 
 void
-ssh_userauth2(const char *server_user, char *host)
+ssh_userauth2(const char *local_user, const char *server_user, char *host,
+    Key **keys, int nkeys)
 {
        Authctxt authctxt;
        int type;
        int plen;
 
-       if (options.challenge_reponse_authentication)
+       if (options.challenge_response_authentication)
                options.kbd_interactive_authentication = 1;
 
        debug("send SSH2_MSG_SERVICE_REQUEST");
@@ -242,11 +254,14 @@ ssh_userauth2(const char *server_user, char *host)
        /* setup authentication context */
        authctxt.agent = ssh_get_authentication_connection();
        authctxt.server_user = server_user;
+       authctxt.local_user = local_user;
        authctxt.host = host;
        authctxt.service = "ssh-connection";            /* service name */
        authctxt.success = 0;
        authctxt.method = authmethod_lookup("none");
        authctxt.authlist = NULL;
+       authctxt.keys = keys;
+       authctxt.nkeys = nkeys;
        if (authctxt.method == NULL)
                fatal("ssh_userauth2: internal error: cannot send userauth none request");
 
@@ -343,7 +358,7 @@ input_userauth_pk_ok(int type, int plen, void *ctxt)
        Authctxt *authctxt = ctxt;
        Key *key = NULL;
        Buffer b;
-       int alen, blen, pktype, sent = 0;
+       int alen, blen, sent = 0;
        char *pkalg, *pkblob, *fp;
 
        if (authctxt == NULL)
@@ -371,7 +386,7 @@ input_userauth_pk_ok(int type, int plen, void *ctxt)
                        debug("no last key or no sign cb");
                        break;
                }
-               if ((pktype = key_type_from_name(pkalg)) == KEY_UNSPEC) {
+               if (key_type_from_name(pkalg) == KEY_UNSPEC) {
                        debug("unknown pkalg %s", pkalg);
                        break;
                }
@@ -446,7 +461,7 @@ userauth_passwd(Authctxt *authctxt)
        return 1;
 }
 
-void
+static void
 clear_auth_state(Authctxt *authctxt)
 {
        /* XXX clear authentication state */
@@ -459,7 +474,7 @@ clear_auth_state(Authctxt *authctxt)
        authctxt->last_key_sign = NULL;
 }
 
-int
+static int
 sign_and_send_pubkey(Authctxt *authctxt, Key *k, sign_cb_fn *sign_callback)
 {
        Buffer b;
@@ -544,7 +559,7 @@ sign_and_send_pubkey(Authctxt *authctxt, Key *k, sign_cb_fn *sign_callback)
        return 1;
 }
 
-int
+static int
 send_pubkey_test(Authctxt *authctxt, Key *k, sign_cb_fn *sign_callback,
     int hint)
 {
@@ -577,7 +592,7 @@ send_pubkey_test(Authctxt *authctxt, Key *k, sign_cb_fn *sign_callback,
        return 1;
 }
 
-Key *
+static Key *
 load_identity_file(char *filename)
 {
        Key *private;
@@ -615,7 +630,7 @@ load_identity_file(char *filename)
        return private;
 }
 
-int
+static int
 identity_sign_cb(Authctxt *authctxt, Key *key, u_char **sigp, int *lenp,
     u_char *data, int datalen)
 {
@@ -625,6 +640,11 @@ identity_sign_cb(Authctxt *authctxt, Key *key, u_char **sigp, int *lenp,
        idx = authctxt->last_key_hint;
        if (idx < 0)
                return -1;
+
+       /* private key is stored in external hardware */
+       if (options.identity_keys[idx]->flags & KEY_FLAG_EXT) 
+               return key_sign(options.identity_keys[idx], sigp, lenp, data, datalen);
+
        private = load_identity_file(options.identity_files[idx]);
        if (private == NULL)
                return -1;
@@ -633,19 +653,21 @@ identity_sign_cb(Authctxt *authctxt, Key *key, u_char **sigp, int *lenp,
        return ret;
 }
 
-int agent_sign_cb(Authctxt *authctxt, Key *key, u_char **sigp, int *lenp,
+static int
+agent_sign_cb(Authctxt *authctxt, Key *key, u_char **sigp, int *lenp,
     u_char *data, int datalen)
 {
        return ssh_agent_sign(authctxt->agent, key, sigp, lenp, data, datalen);
 }
 
-int key_sign_cb(Authctxt *authctxt, Key *key, u_char **sigp, int *lenp,
+static int
+key_sign_cb(Authctxt *authctxt, Key *key, u_char **sigp, int *lenp,
     u_char *data, int datalen)
 {
-        return key_sign(key, sigp, lenp, data, datalen);
+       return key_sign(key, sigp, lenp, data, datalen);
 }
 
-int
+static int
 userauth_pubkey_agent(Authctxt *authctxt)
 {
        static int called = 0;
@@ -752,9 +774,9 @@ input_userauth_info_req(int type, int plen, void *ctxt)
        inst = packet_get_string(NULL);
        lang = packet_get_string(NULL);
        if (strlen(name) > 0)
-               cli_mesg(name);
+               log("%s", name);
        if (strlen(inst) > 0)
-               cli_mesg(inst);
+               log("%s", inst);
        xfree(name);
        xfree(inst);
        xfree(lang);
@@ -769,11 +791,12 @@ input_userauth_info_req(int type, int plen, void *ctxt)
        packet_start(SSH2_MSG_USERAUTH_INFO_RESPONSE);
        packet_put_int(num_prompts);
 
+       debug2("input_userauth_info_req: num_prompts %d", num_prompts);
        for (i = 0; i < num_prompts; i++) {
                prompt = packet_get_string(NULL);
                echo = packet_get_char();
 
-               response = cli_prompt(prompt, echo);
+               response = read_passphrase(prompt, echo ? RP_ECHO : 0);
 
                packet_put_cstring(response);
                memset(response, 0, strlen(response));
@@ -786,13 +809,102 @@ input_userauth_info_req(int type, int plen, void *ctxt)
        packet_send();
 }
 
+/*
+ * this will be move to an external program (ssh-keysign) ASAP. ssh-keysign
+ * will be setuid-root and the sbit can be removed from /usr/bin/ssh.
+ */
+int
+userauth_hostbased(Authctxt *authctxt)
+{
+       Key *private = NULL;
+       Buffer b;
+       u_char *signature, *blob;
+       char *chost, *pkalg, *p;
+       const char *service;
+       u_int blen, slen;
+       int ok, i, len, found = 0;
+
+       p = get_local_name(packet_get_connection_in());
+       if (p == NULL) {
+               error("userauth_hostbased: cannot get local ipaddr/name");
+               return 0;
+       }
+       len = strlen(p) + 2;
+       chost = xmalloc(len);
+       strlcpy(chost, p, len);
+       strlcat(chost, ".", len);
+       debug2("userauth_hostbased: chost %s", chost);
+       /* check for a useful key */
+       for (i = 0; i < authctxt->nkeys; i++) {
+               private = authctxt->keys[i];
+               if (private && private->type != KEY_RSA1) {
+                       found = 1;
+                       /* we take and free the key */
+                       authctxt->keys[i] = NULL;
+                       break;
+               }
+       }
+       if (!found) {
+               xfree(chost);
+               return 0;
+       }
+       if (key_to_blob(private, &blob, &blen) == 0) {
+               key_free(private);
+               xfree(chost);
+               return 0;
+       }
+       service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" :
+           authctxt->service;
+       pkalg = xstrdup(key_ssh_name(private));
+       buffer_init(&b);
+       /* construct data */
+       buffer_put_string(&b, session_id2, session_id2_len);
+       buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
+       buffer_put_cstring(&b, authctxt->server_user);
+       buffer_put_cstring(&b, service);
+       buffer_put_cstring(&b, authctxt->method->name);
+       buffer_put_cstring(&b, pkalg);
+       buffer_put_string(&b, blob, blen);
+       buffer_put_cstring(&b, chost);
+       buffer_put_cstring(&b, authctxt->local_user);
+#ifdef DEBUG_PK
+       buffer_dump(&b);
+#endif
+       debug2("xxx: chost %s", chost);
+       ok = key_sign(private, &signature, &slen, buffer_ptr(&b), buffer_len(&b));
+       key_free(private);
+       buffer_free(&b);
+       if (ok != 0) {
+               error("key_sign failed");
+               xfree(chost);
+               xfree(pkalg);
+               return 0;
+       }
+       packet_start(SSH2_MSG_USERAUTH_REQUEST);
+       packet_put_cstring(authctxt->server_user);
+       packet_put_cstring(authctxt->service);
+       packet_put_cstring(authctxt->method->name);
+       packet_put_cstring(pkalg);
+       packet_put_string(blob, blen);
+       packet_put_cstring(chost);
+       packet_put_cstring(authctxt->local_user);
+       packet_put_string(signature, slen);
+       memset(signature, 's', slen);
+       xfree(signature);
+       xfree(chost);
+       xfree(pkalg);
+
+       packet_send();
+       return 1;
+}
+
 /* find auth method */
 
 /*
  * given auth method name, if configurable options permit this method fill
  * in auth_ident field and return true, otherwise return false.
  */
-int
+static int
 authmethod_is_enabled(Authmethod *method)
 {
        if (method == NULL)
@@ -806,7 +918,7 @@ authmethod_is_enabled(Authmethod *method)
        return 1;
 }
 
-Authmethod *
+static Authmethod *
 authmethod_lookup(const char *name)
 {
        Authmethod *method = NULL;
@@ -825,9 +937,9 @@ static char *preferred = NULL;
 /*
  * Given the authentication method list sent by the server, return the
  * next method we should try.  If the server initially sends a nil list,
- * use a built-in default list. 
+ * use a built-in default list.
  */
-Authmethod *
+static Authmethod *
 authmethod_get(char *authlist)
 {
 
@@ -869,7 +981,8 @@ authmethod_get(char *authlist)
 
 
 #define        DELIM   ","
-char *
+
+static char *
 authmethods_get(void)
 {
        Authmethod *method = NULL;
This page took 0.052523 seconds and 4 git commands to generate.