Main Page | Namespace List | Class List | Directories | File List | Class Members | File Members

auth2-hostbased.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2000 Markus Friedl.  All rights reserved.
00003  *
00004  * Redistribution and use in source and binary forms, with or without
00005  * modification, are permitted provided that the following conditions
00006  * are met:
00007  * 1. Redistributions of source code must retain the above copyright
00008  *    notice, this list of conditions and the following disclaimer.
00009  * 2. Redistributions in binary form must reproduce the above copyright
00010  *    notice, this list of conditions and the following disclaimer in the
00011  *    documentation and/or other materials provided with the distribution.
00012  *
00013  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
00014  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00015  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
00016  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
00017  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
00018  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00019  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00020  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00021  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
00022  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00023  */
00024 
00025 #include "includes.h"
00026 RCSID("$OpenBSD: auth2-hostbased.c,v 1.6 2004/01/19 21:25:15 markus Exp $");
00027 
00028 #include "ssh2.h"
00029 #include "xmalloc.h"
00030 #include "packet.h"
00031 #include "buffer.h"
00032 #include "log.h"
00033 #include "servconf.h"
00034 #include "compat.h"
00035 #include "bufaux.h"
00036 #include "auth.h"
00037 #include "key.h"
00038 #include "canohost.h"
00039 #include "monitor_wrap.h"
00040 #include "pathnames.h"
00041 
00042 /* import */
00043 extern ServerOptions options;
00044 extern u_char *session_id2;
00045 extern u_int session_id2_len;
00046 
00047 static int
00048 userauth_hostbased(Authctxt *authctxt)
00049 {
00050         Buffer b;
00051         Key *key = NULL;
00052         char *pkalg, *cuser, *chost, *service;
00053         u_char *pkblob, *sig;
00054         u_int alen, blen, slen;
00055         int pktype;
00056         int authenticated = 0;
00057 
00058         if (!authctxt->valid) {
00059                 debug2("userauth_hostbased: disabled because of invalid user");
00060                 return 0;
00061         }
00062         pkalg = packet_get_string(&alen);
00063         pkblob = packet_get_string(&blen);
00064         chost = packet_get_string(NULL);
00065         cuser = packet_get_string(NULL);
00066         sig = packet_get_string(&slen);
00067 
00068         debug("userauth_hostbased: cuser %s chost %s pkalg %s slen %d",
00069             cuser, chost, pkalg, slen);
00070 #ifdef DEBUG_PK
00071         debug("signature:");
00072         buffer_init(&b);
00073         buffer_append(&b, sig, slen);
00074         buffer_dump(&b);
00075         buffer_free(&b);
00076 #endif
00077         pktype = key_type_from_name(pkalg);
00078         if (pktype == KEY_UNSPEC) {
00079                 /* this is perfectly legal */
00080                 logit("userauth_hostbased: unsupported "
00081                     "public key algorithm: %s", pkalg);
00082                 goto done;
00083         }
00084         key = key_from_blob(pkblob, blen);
00085         if (key == NULL) {
00086                 error("userauth_hostbased: cannot decode key: %s", pkalg);
00087                 goto done;
00088         }
00089         if (key->type != pktype) {
00090                 error("userauth_hostbased: type mismatch for decoded key "
00091                     "(received %d, expected %d)", key->type, pktype);
00092                 goto done;
00093         }
00094         service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" :
00095             authctxt->service;
00096         buffer_init(&b);
00097         buffer_put_string(&b, session_id2, session_id2_len);
00098         /* reconstruct packet */
00099         buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
00100         buffer_put_cstring(&b, authctxt->user);
00101         buffer_put_cstring(&b, service);
00102         buffer_put_cstring(&b, "hostbased");
00103         buffer_put_string(&b, pkalg, alen);
00104         buffer_put_string(&b, pkblob, blen);
00105         buffer_put_cstring(&b, chost);
00106         buffer_put_cstring(&b, cuser);
00107 #ifdef DEBUG_PK
00108         buffer_dump(&b);
00109 #endif
00110         /* test for allowed key and correct signature */
00111         authenticated = 0;
00112         if (PRIVSEP(hostbased_key_allowed(authctxt->pw, cuser, chost, key)) &&
00113             PRIVSEP(key_verify(key, sig, slen, buffer_ptr(&b),
00114                         buffer_len(&b))) == 1)
00115                 authenticated = 1;
00116 
00117         buffer_free(&b);
00118 done:
00119         debug2("userauth_hostbased: authenticated %d", authenticated);
00120         if (key != NULL)
00121                 key_free(key);
00122         xfree(pkalg);
00123         xfree(pkblob);
00124         xfree(cuser);
00125         xfree(chost);
00126         xfree(sig);
00127         return authenticated;
00128 }
00129 
00130 /* return 1 if given hostkey is allowed */
00131 int
00132 hostbased_key_allowed(struct passwd *pw, const char *cuser, char *chost,
00133     Key *key)
00134 {
00135         const char *resolvedname, *ipaddr, *lookup;
00136         HostStatus host_status;
00137         int len;
00138 
00139         resolvedname = get_canonical_hostname(options.use_dns);
00140         ipaddr = get_remote_ipaddr();
00141 
00142         debug2("userauth_hostbased: chost %s resolvedname %s ipaddr %s",
00143             chost, resolvedname, ipaddr);
00144 
00145         if (options.hostbased_uses_name_from_packet_only) {
00146                 if (auth_rhosts2(pw, cuser, chost, chost) == 0)
00147                         return 0;
00148                 lookup = chost;
00149         } else {
00150                 if (((len = strlen(chost)) > 0) && chost[len - 1] == '.') {
00151                         debug2("stripping trailing dot from chost %s", chost);
00152                         chost[len - 1] = '\0';
00153                 }
00154                 if (strcasecmp(resolvedname, chost) != 0)
00155                         logit("userauth_hostbased mismatch: "
00156                             "client sends %s, but we resolve %s to %s",
00157                             chost, ipaddr, resolvedname);
00158                 if (auth_rhosts2(pw, cuser, resolvedname, ipaddr) == 0)
00159                         return 0;
00160                 lookup = resolvedname;
00161         }
00162         debug2("userauth_hostbased: access allowed by auth_rhosts2");
00163 
00164         host_status = check_key_in_hostfiles(pw, key, lookup,
00165             _PATH_SSH_SYSTEM_HOSTFILE,
00166             options.ignore_user_known_hosts ? NULL : _PATH_SSH_USER_HOSTFILE);
00167 
00168         /* backward compat if no key has been found. */
00169         if (host_status == HOST_NEW)
00170                 host_status = check_key_in_hostfiles(pw, key, lookup,
00171                     _PATH_SSH_SYSTEM_HOSTFILE2,
00172                     options.ignore_user_known_hosts ? NULL :
00173                     _PATH_SSH_USER_HOSTFILE2);
00174 
00175         return (host_status == HOST_OK);
00176 }
00177 
00178 Authmethod method_hostbased = {
00179         "hostbased",
00180         userauth_hostbased,
00181         &options.hostbased_authentication
00182 };

© sourcejam.com 2005-2008