1 /* $OpenBSD: auth2-hostbased.c,v 1.12 2008/07/17 08:51:07 djm Exp $ */
2 /*
3 * Copyright (c) 2000 Markus Friedl. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26
27 #include <sys/types.h>
28
29 #include <pwd.h>
30 #include <string.h>
31 #include <stdarg.h>
32
33 #include "xmalloc.h"
34 #include "ssh2.h"
35 #include "packet.h"
36 #include "buffer.h"
37 #include "log.h"
38 #include "servconf.h"
39 #include "compat.h"
40 #include "key.h"
41 #include "hostfile.h"
42 #include "auth.h"
43 #include "canohost.h"
44 #include "monitor_wrap.h"
45 #include "pathnames.h"
46
47 __RCSID("$MirOS: src/usr.bin/ssh/auth2-hostbased.c,v 1.7 2011/01/15 21:52:40 tg Exp $");
48
49 /* import */
50 extern ServerOptions options;
51 extern u_char *session_id2;
52 extern u_int session_id2_len;
53
54 static int
userauth_hostbased(Authctxt * authctxt)55 userauth_hostbased(Authctxt *authctxt)
56 {
57 Buffer b;
58 Key *key = NULL;
59 char *pkalg, *cuser, *chost, *service;
60 u_char *pkblob, *sig;
61 u_int alen, blen, slen;
62 int pktype;
63 int authenticated = 0;
64
65 if (!authctxt->valid) {
66 debug2("userauth_hostbased: disabled because of invalid user");
67 return 0;
68 }
69 pkalg = packet_get_string(&alen);
70 pkblob = packet_get_string(&blen);
71 chost = packet_get_string(NULL);
72 cuser = packet_get_string(NULL);
73 sig = packet_get_string(&slen);
74
75 debug("userauth_hostbased: cuser %s chost %s pkalg %s slen %d",
76 cuser, chost, pkalg, slen);
77 #ifdef DEBUG_PK
78 debug("signature:");
79 buffer_init(&b);
80 buffer_append(&b, sig, slen);
81 buffer_dump(&b);
82 buffer_free(&b);
83 #endif
84 pktype = key_type_from_name(pkalg);
85 if (pktype == KEY_UNSPEC) {
86 /* this is perfectly legal */
87 logit("userauth_hostbased: unsupported "
88 "public key algorithm: %s", pkalg);
89 goto done;
90 }
91 key = key_from_blob(pkblob, blen);
92 if (key == NULL) {
93 error("userauth_hostbased: cannot decode key: %s", pkalg);
94 goto done;
95 }
96 if (key->type != pktype) {
97 error("userauth_hostbased: type mismatch for decoded key "
98 "(received %d, expected %d)", key->type, pktype);
99 goto done;
100 }
101 service = datafellows & SSH_BUG_HBSERVICE ? (char *)"ssh-userauth" :
102 authctxt->service;
103 buffer_init(&b);
104 buffer_put_string(&b, session_id2, session_id2_len);
105 /* reconstruct packet */
106 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
107 buffer_put_cstring(&b, authctxt->user);
108 buffer_put_cstring(&b, service);
109 buffer_put_cstring(&b, "hostbased");
110 buffer_put_string(&b, pkalg, alen);
111 buffer_put_string(&b, pkblob, blen);
112 buffer_put_cstring(&b, chost);
113 buffer_put_cstring(&b, cuser);
114 #ifdef DEBUG_PK
115 buffer_dump(&b);
116 #endif
117 /* test for allowed key and correct signature */
118 authenticated = 0;
119 if (PRIVSEP(hostbased_key_allowed(authctxt->pw, cuser, chost, key)) &&
120 PRIVSEP(key_verify(key, sig, slen, buffer_ptr(&b),
121 buffer_len(&b))) == 1)
122 authenticated = 1;
123
124 buffer_free(&b);
125 done:
126 debug2("userauth_hostbased: authenticated %d", authenticated);
127 if (key != NULL)
128 key_free(key);
129 xfree(pkalg);
130 xfree(pkblob);
131 xfree(cuser);
132 xfree(chost);
133 xfree(sig);
134 return authenticated;
135 }
136
137 /* return 1 if given hostkey is allowed */
138 int
hostbased_key_allowed(struct passwd * pw,const char * cuser,char * chost,Key * key)139 hostbased_key_allowed(struct passwd *pw, const char *cuser, char *chost,
140 Key *key)
141 {
142 const char *resolvedname, *ipaddr, *lookup;
143 HostStatus host_status;
144 int len;
145
146 resolvedname = get_canonical_hostname(options.use_dns);
147 ipaddr = get_remote_ipaddr();
148
149 debug2("userauth_hostbased: chost %s resolvedname %s ipaddr %s",
150 chost, resolvedname, ipaddr);
151
152 if (((len = strlen(chost)) > 0) && chost[len - 1] == '.') {
153 debug2("stripping trailing dot from chost %s", chost);
154 chost[len - 1] = '\0';
155 }
156
157 if (options.hostbased_uses_name_from_packet_only) {
158 if (auth_rhosts2(pw, cuser, chost, chost) == 0)
159 return 0;
160 lookup = chost;
161 } else {
162 if (strcasecmp(resolvedname, chost) != 0)
163 logit("userauth_hostbased mismatch: "
164 "client sends %s, but we resolve %s to %s",
165 chost, ipaddr, resolvedname);
166 if (auth_rhosts2(pw, cuser, resolvedname, ipaddr) == 0)
167 return 0;
168 lookup = resolvedname;
169 }
170 debug2("userauth_hostbased: access allowed by auth_rhosts2");
171
172 host_status = check_key_in_hostfiles(pw, key, lookup,
173 _PATH_SSH_SYSTEM_HOSTFILE,
174 options.ignore_user_known_hosts ? NULL :
175 #ifdef _PATH_SSH_ROOT_HOSTFILE
176 (!pw->pw_dir || !pw->pw_dir[0] || (pw->pw_dir[0] == '/' &&
177 !pw->pw_dir[1])) ? _PATH_SSH_ROOT_HOSTFILE :
178 #endif
179 _PATH_SSH_USER_HOSTFILE);
180
181 /* backward compat if no key has been found. */
182 if (host_status == HOST_NEW)
183 host_status = check_key_in_hostfiles(pw, key, lookup,
184 _PATH_SSH_SYSTEM_HOSTFILE2,
185 options.ignore_user_known_hosts ? NULL :
186 _PATH_SSH_USER_HOSTFILE2);
187
188 return (host_status == HOST_OK);
189 }
190
191 Authmethod method_hostbased = {
192 "hostbased",
193 userauth_hostbased,
194 &options.hostbased_authentication
195 };
196