blob: 3a247f58894172e7856241543852c2188ef6d62b [file] [log] [blame]
Damien Millereba71ba2000-04-29 23:57:08 +10001/*
2 * Copyright (c) 2000 Markus Friedl. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
Damien Millereba71ba2000-04-29 23:57:08 +100012 *
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 */
Damien Millere4340be2000-09-16 13:29:08 +110024
Damien Millereba71ba2000-04-29 23:57:08 +100025#include "includes.h"
Ben Lindstrom48bd7c12001-01-09 00:35:42 +000026RCSID("$OpenBSD: auth2.c,v 1.25 2001/01/08 22:29:05 markus Exp $");
Damien Miller874d77b2000-10-14 16:23:11 +110027
28#ifdef HAVE_OSF_SIA
29# include <sia.h>
30# include <siad.h>
31#endif
Damien Millereba71ba2000-04-29 23:57:08 +100032
33#include <openssl/dsa.h>
34#include <openssl/rsa.h>
35#include <openssl/evp.h>
36
37#include "xmalloc.h"
38#include "rsa.h"
39#include "ssh.h"
40#include "pty.h"
41#include "packet.h"
42#include "buffer.h"
Damien Millereba71ba2000-04-29 23:57:08 +100043#include "servconf.h"
44#include "compat.h"
45#include "channels.h"
46#include "bufaux.h"
47#include "ssh2.h"
48#include "auth.h"
49#include "session.h"
50#include "dispatch.h"
51#include "auth.h"
52#include "key.h"
53#include "kex.h"
54
Damien Millereba71ba2000-04-29 23:57:08 +100055#include "uidswap.h"
Damien Millerf6d9e222000-06-18 14:50:44 +100056#include "auth-options.h"
Damien Millereba71ba2000-04-29 23:57:08 +100057
58/* import */
59extern ServerOptions options;
Ben Lindstrom46c16222000-12-22 01:43:59 +000060extern u_char *session_id2;
Damien Millereba71ba2000-04-29 23:57:08 +100061extern int session_id2_len;
62
Damien Miller874d77b2000-10-14 16:23:11 +110063#ifdef WITH_AIXAUTHENTICATE
64extern char *aixloginmsg;
65#endif
66#ifdef HAVE_OSF_SIA
67extern int saved_argc;
68extern char **saved_argv;
69#endif
70
71static Authctxt *x_authctxt = NULL;
72static int one = 1;
73
74typedef struct Authmethod Authmethod;
75struct Authmethod {
76 char *name;
77 int (*userauth)(Authctxt *authctxt);
78 int *enabled;
79};
80
Damien Millereba71ba2000-04-29 23:57:08 +100081/* protocol */
82
Damien Miller62cee002000-09-23 17:15:56 +110083void input_service_request(int type, int plen, void *ctxt);
84void input_userauth_request(int type, int plen, void *ctxt);
85void protocol_error(int type, int plen, void *ctxt);
Damien Millereba71ba2000-04-29 23:57:08 +100086
Damien Millereba71ba2000-04-29 23:57:08 +100087
88/* helper */
Damien Miller874d77b2000-10-14 16:23:11 +110089Authmethod *authmethod_lookup(const char *name);
90struct passwd *pwcopy(struct passwd *pw);
Damien Miller0bc1bd82000-11-13 22:57:25 +110091int user_key_allowed(struct passwd *pw, Key *key);
Damien Miller874d77b2000-10-14 16:23:11 +110092char *authmethods_get(void);
Damien Millereba71ba2000-04-29 23:57:08 +100093
Damien Miller874d77b2000-10-14 16:23:11 +110094/* auth */
Ben Lindstrom48bd7c12001-01-09 00:35:42 +000095void userauth_banner(void);
Damien Miller874d77b2000-10-14 16:23:11 +110096int userauth_none(Authctxt *authctxt);
97int userauth_passwd(Authctxt *authctxt);
98int userauth_pubkey(Authctxt *authctxt);
99int userauth_kbdint(Authctxt *authctxt);
100
101Authmethod authmethods[] = {
102 {"none",
103 userauth_none,
104 &one},
105 {"publickey",
106 userauth_pubkey,
Damien Miller0bc1bd82000-11-13 22:57:25 +1100107 &options.pubkey_authentication},
Damien Miller874d77b2000-10-14 16:23:11 +1100108 {"keyboard-interactive",
109 userauth_kbdint,
110 &options.kbd_interactive_authentication},
111 {"password",
112 userauth_passwd,
113 &options.password_authentication},
114 {NULL, NULL, NULL}
Damien Millereba71ba2000-04-29 23:57:08 +1000115};
Damien Millereba71ba2000-04-29 23:57:08 +1000116
117/*
Damien Miller874d77b2000-10-14 16:23:11 +1100118 * loop until authctxt->success == TRUE
Damien Millereba71ba2000-04-29 23:57:08 +1000119 */
120
121void
122do_authentication2()
123{
Damien Miller874d77b2000-10-14 16:23:11 +1100124 Authctxt *authctxt = xmalloc(sizeof(*authctxt));
125 memset(authctxt, 'a', sizeof(*authctxt));
126 authctxt->valid = 0;
127 authctxt->attempt = 0;
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000128 authctxt->failures = 0;
Damien Miller874d77b2000-10-14 16:23:11 +1100129 authctxt->success = 0;
130 x_authctxt = authctxt; /*XXX*/
131
Damien Miller1cead2c2000-05-01 22:55:23 +1000132#ifdef KRB4
Damien Miller874d77b2000-10-14 16:23:11 +1100133 /* turn off kerberos, not supported by SSH2 */
Damien Miller35dabd02000-05-01 21:10:33 +1000134 options.kerberos_authentication = 0;
Damien Miller1cead2c2000-05-01 22:55:23 +1000135#endif
Damien Millereba71ba2000-04-29 23:57:08 +1000136 dispatch_init(&protocol_error);
137 dispatch_set(SSH2_MSG_SERVICE_REQUEST, &input_service_request);
Damien Miller874d77b2000-10-14 16:23:11 +1100138 dispatch_run(DISPATCH_BLOCK, &authctxt->success, authctxt);
Damien Millereba71ba2000-04-29 23:57:08 +1000139 do_authenticated2();
140}
141
142void
Damien Miller62cee002000-09-23 17:15:56 +1100143protocol_error(int type, int plen, void *ctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000144{
145 log("auth: protocol error: type %d plen %d", type, plen);
146 packet_start(SSH2_MSG_UNIMPLEMENTED);
147 packet_put_int(0);
148 packet_send();
149 packet_write_wait();
150}
151
152void
Damien Miller62cee002000-09-23 17:15:56 +1100153input_service_request(int type, int plen, void *ctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000154{
Damien Miller874d77b2000-10-14 16:23:11 +1100155 Authctxt *authctxt = ctxt;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000156 u_int len;
Damien Millereba71ba2000-04-29 23:57:08 +1000157 int accept = 0;
158 char *service = packet_get_string(&len);
159 packet_done();
160
Damien Miller874d77b2000-10-14 16:23:11 +1100161 if (authctxt == NULL)
162 fatal("input_service_request: no authctxt");
163
Damien Millereba71ba2000-04-29 23:57:08 +1000164 if (strcmp(service, "ssh-userauth") == 0) {
Damien Miller874d77b2000-10-14 16:23:11 +1100165 if (!authctxt->success) {
Damien Millereba71ba2000-04-29 23:57:08 +1000166 accept = 1;
167 /* now we can handle user-auth requests */
168 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &input_userauth_request);
169 }
170 }
171 /* XXX all other service requests are denied */
172
173 if (accept) {
174 packet_start(SSH2_MSG_SERVICE_ACCEPT);
175 packet_put_cstring(service);
176 packet_send();
177 packet_write_wait();
178 } else {
179 debug("bad service request %s", service);
180 packet_disconnect("bad service request %s", service);
181 }
182 xfree(service);
183}
184
185void
Damien Miller62cee002000-09-23 17:15:56 +1100186input_userauth_request(int type, int plen, void *ctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000187{
Damien Miller874d77b2000-10-14 16:23:11 +1100188 Authctxt *authctxt = ctxt;
189 Authmethod *m = NULL;
190 char *user, *service, *method;
Damien Millereba71ba2000-04-29 23:57:08 +1000191 int authenticated = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000192
Damien Miller874d77b2000-10-14 16:23:11 +1100193 if (authctxt == NULL)
194 fatal("input_userauth_request: no authctxt");
Damien Millereba71ba2000-04-29 23:57:08 +1000195
Damien Miller874d77b2000-10-14 16:23:11 +1100196 user = packet_get_string(NULL);
197 service = packet_get_string(NULL);
198 method = packet_get_string(NULL);
199 debug("userauth-request for user %s service %s method %s", user, service, method);
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000200 debug("attempt %d failures %d", authctxt->attempt, authctxt->failures);
Damien Miller874d77b2000-10-14 16:23:11 +1100201
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000202 if (authctxt->attempt++ == 0) {
Damien Miller874d77b2000-10-14 16:23:11 +1100203 /* setup auth context */
204 struct passwd *pw = NULL;
205 setproctitle("%s", user);
206 pw = getpwnam(user);
207 if (pw && allowed_user(pw) && strcmp(service, "ssh-connection")==0) {
208 authctxt->pw = pwcopy(pw);
209 authctxt->valid = 1;
210 debug2("input_userauth_request: setting up authctxt for %s", user);
211#ifdef USE_PAM
212 start_pam(pw);
213#endif
214 } else {
215 log("input_userauth_request: illegal user %s", user);
216 }
217 authctxt->user = xstrdup(user);
218 authctxt->service = xstrdup(service);
219 } else if (authctxt->valid) {
220 if (strcmp(user, authctxt->user) != 0 ||
221 strcmp(service, authctxt->service) != 0) {
222 log("input_userauth_request: missmatch: (%s,%s)!=(%s,%s)",
223 user, service, authctxt->user, authctxt->service);
224 authctxt->valid = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000225 }
226 }
Damien Millerb70b61f2000-09-16 16:25:12 +1100227
Damien Miller874d77b2000-10-14 16:23:11 +1100228 m = authmethod_lookup(method);
229 if (m != NULL) {
230 debug2("input_userauth_request: try method %s", method);
231 authenticated = m->userauth(authctxt);
232 } else {
233 debug2("input_userauth_request: unsupported method %s", method);
234 }
235 if (!authctxt->valid && authenticated == 1) {
236 log("input_userauth_request: INTERNAL ERROR: authenticated invalid user %s service %s", user, method);
Damien Millerb70b61f2000-09-16 16:25:12 +1100237 authenticated = 0;
238 }
Damien Millerb70b61f2000-09-16 16:25:12 +1100239
Damien Miller874d77b2000-10-14 16:23:11 +1100240 /* Special handling for root */
241 if (authenticated == 1 &&
242 authctxt->valid && authctxt->pw->pw_uid == 0 && !options.permit_root_login) {
Damien Millereba71ba2000-04-29 23:57:08 +1000243 authenticated = 0;
Damien Miller874d77b2000-10-14 16:23:11 +1100244 log("ROOT LOGIN REFUSED FROM %.200s", get_canonical_hostname());
Damien Millereba71ba2000-04-29 23:57:08 +1000245 }
246
247#ifdef USE_PAM
Damien Millerd425d4d2000-10-28 21:05:57 +1100248 if (authenticated && authctxt->user && !do_pam_account(authctxt->user, NULL))
Damien Millerb70b61f2000-09-16 16:25:12 +1100249 authenticated = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000250#endif /* USE_PAM */
251
Damien Miller874d77b2000-10-14 16:23:11 +1100252 /* Log before sending the reply */
253 userauth_log(authctxt, authenticated, method);
254 userauth_reply(authctxt, authenticated);
255
256 xfree(service);
257 xfree(user);
258 xfree(method);
259}
260
Ben Lindstrom48bd7c12001-01-09 00:35:42 +0000261void
262userauth_banner(void)
263{
264 struct stat st;
265 char *banner = NULL;
266 off_t len, n;
267 int fd;
268
269 if (options.banner == NULL || (datafellows & SSH_BUG_BANNER))
270 return;
271 if ((fd = open(options.banner, O_RDONLY)) < 0) {
272 error("userauth_banner: open %s failed: %s",
273 options.banner, strerror(errno));
274 return;
275 }
276 if (fstat(fd, &st) < 0)
277 goto done;
278 len = st.st_size;
279 banner = xmalloc(len + 1);
280 if ((n = read(fd, banner, len)) < 0)
281 goto done;
282 banner[n] = '\0';
283 packet_start(SSH2_MSG_USERAUTH_BANNER);
284 packet_put_cstring(banner);
285 packet_put_cstring(""); /* language, unused */
286 packet_send();
287 debug("userauth_banner: sent");
288done:
289 if (banner)
290 xfree(banner);
291 close(fd);
292 return;
293}
Damien Miller874d77b2000-10-14 16:23:11 +1100294
295void
296userauth_log(Authctxt *authctxt, int authenticated, char *method)
297{
298 void (*authlog) (const char *fmt,...) = verbose;
299 char *user = NULL, *authmsg = NULL;
300
Damien Millere247cc42000-05-07 12:03:14 +1000301 /* Raise logging level */
302 if (authenticated == 1 ||
Damien Miller874d77b2000-10-14 16:23:11 +1100303 !authctxt->valid ||
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000304 authctxt->failures >= AUTH_FAIL_LOG ||
Damien Millere247cc42000-05-07 12:03:14 +1000305 strcmp(method, "password") == 0)
306 authlog = log;
307
Damien Millereba71ba2000-04-29 23:57:08 +1000308 if (authenticated == 1) {
309 authmsg = "Accepted";
Damien Millere247cc42000-05-07 12:03:14 +1000310 } else if (authenticated == 0) {
311 authmsg = "Failed";
312 } else {
313 authmsg = "Postponed";
314 }
Damien Millere247cc42000-05-07 12:03:14 +1000315
Damien Miller874d77b2000-10-14 16:23:11 +1100316 if (authctxt->valid) {
317 user = authctxt->pw->pw_uid == 0 ? "ROOT" : authctxt->user;
318 } else {
319 user = "NOUSER";
320 }
321
322 authlog("%s %s for %.200s from %.200s port %d ssh2",
323 authmsg,
324 method,
325 user,
326 get_remote_ipaddr(),
327 get_remote_port());
328}
329
330void
331userauth_reply(Authctxt *authctxt, int authenticated)
332{
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000333 char *methods;
Damien Millere247cc42000-05-07 12:03:14 +1000334 /* XXX todo: check if multiple auth methods are needed */
335 if (authenticated == 1) {
Damien Millerd2c208a2000-05-17 22:00:02 +1000336#ifdef WITH_AIXAUTHENTICATE
337 /* We don't have a pty yet, so just label the line as "ssh" */
Damien Millerd425d4d2000-10-28 21:05:57 +1100338 if (loginsuccess(authctxt->user?authctxt->user:"NOUSER",
339 get_canonical_hostname(), "ssh", &aixloginmsg) < 0)
Damien Millerd2c208a2000-05-17 22:00:02 +1000340 aixloginmsg = NULL;
341#endif /* WITH_AIXAUTHENTICATE */
Damien Millereba71ba2000-04-29 23:57:08 +1000342 /* turn off userauth */
343 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &protocol_error);
344 packet_start(SSH2_MSG_USERAUTH_SUCCESS);
345 packet_send();
346 packet_write_wait();
347 /* now we can break out */
Damien Miller874d77b2000-10-14 16:23:11 +1100348 authctxt->success = 1;
Damien Millereba71ba2000-04-29 23:57:08 +1000349 } else if (authenticated == 0) {
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000350 if (authctxt->failures++ >= AUTH_FAIL_MAX)
351 packet_disconnect("too many failed userauth_requests");
352 methods = authmethods_get();
Damien Millereba71ba2000-04-29 23:57:08 +1000353 packet_start(SSH2_MSG_USERAUTH_FAILURE);
Damien Miller874d77b2000-10-14 16:23:11 +1100354 packet_put_cstring(methods);
355 packet_put_char(0); /* XXX partial success, unused */
Damien Millereba71ba2000-04-29 23:57:08 +1000356 packet_send();
357 packet_write_wait();
Damien Miller874d77b2000-10-14 16:23:11 +1100358 xfree(methods);
359 } else {
360 /* do nothing, we did already send a reply */
Damien Millereba71ba2000-04-29 23:57:08 +1000361 }
Damien Millereba71ba2000-04-29 23:57:08 +1000362}
363
364int
Damien Miller874d77b2000-10-14 16:23:11 +1100365userauth_none(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000366{
Damien Miller874d77b2000-10-14 16:23:11 +1100367 /* disable method "none", only allowed one time */
368 Authmethod *m = authmethod_lookup("none");
369 if (m != NULL)
370 m->enabled = NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000371 packet_done();
Ben Lindstrom48bd7c12001-01-09 00:35:42 +0000372 userauth_banner();
Damien Millerb8c656e2000-06-28 15:22:41 +1000373
Damien Miller874d77b2000-10-14 16:23:11 +1100374 if (authctxt->valid == 0)
375 return(0);
376
377#ifdef HAVE_CYGWIN
378 if (check_nt_auth(1, authctxt->pw->pw_uid) == 0)
379 return(0);
380#endif
Damien Millereba71ba2000-04-29 23:57:08 +1000381#ifdef USE_PAM
Damien Miller874d77b2000-10-14 16:23:11 +1100382 return auth_pam_password(authctxt->pw, "");
Damien Millerb8c656e2000-06-28 15:22:41 +1000383#elif defined(HAVE_OSF_SIA)
Damien Miller874d77b2000-10-14 16:23:11 +1100384 return (sia_validate_user(NULL, saved_argc, saved_argv,
Damien Millerd425d4d2000-10-28 21:05:57 +1100385 get_canonical_hostname(), authctxt->user?authctxt->user:"NOUSER",
386 NULL, 0, NULL, "") == SIASUCCESS);
Damien Millerb8c656e2000-06-28 15:22:41 +1000387#else /* !HAVE_OSF_SIA && !USE_PAM */
Damien Miller874d77b2000-10-14 16:23:11 +1100388 return auth_password(authctxt->pw, "");
Damien Millereba71ba2000-04-29 23:57:08 +1000389#endif /* USE_PAM */
390}
Damien Miller874d77b2000-10-14 16:23:11 +1100391
Damien Millereba71ba2000-04-29 23:57:08 +1000392int
Damien Miller874d77b2000-10-14 16:23:11 +1100393userauth_passwd(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000394{
395 char *password;
396 int authenticated = 0;
397 int change;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000398 u_int len;
Damien Millereba71ba2000-04-29 23:57:08 +1000399 change = packet_get_char();
400 if (change)
401 log("password change not supported");
402 password = packet_get_string(&len);
403 packet_done();
Damien Miller874d77b2000-10-14 16:23:11 +1100404 if (authctxt->valid &&
405#ifdef HAVE_CYGWIN
406 check_nt_auth(1, authctxt->pw->pw_uid) &&
407#endif
Damien Millereba71ba2000-04-29 23:57:08 +1000408#ifdef USE_PAM
Damien Miller874d77b2000-10-14 16:23:11 +1100409 auth_pam_password(authctxt->pw, password) == 1)
Damien Millerb8c656e2000-06-28 15:22:41 +1000410#elif defined(HAVE_OSF_SIA)
411 sia_validate_user(NULL, saved_argc, saved_argv,
Damien Millerd425d4d2000-10-28 21:05:57 +1100412 get_canonical_hostname(), authctxt->user?authctxt->user:"NOUSER",
413 NULL, 0, NULL, password) == SIASUCCESS)
Damien Millerb8c656e2000-06-28 15:22:41 +1000414#else /* !USE_PAM && !HAVE_OSF_SIA */
Damien Miller874d77b2000-10-14 16:23:11 +1100415 auth_password(authctxt->pw, password) == 1)
Damien Millereba71ba2000-04-29 23:57:08 +1000416#endif /* USE_PAM */
417 authenticated = 1;
418 memset(password, 0, len);
419 xfree(password);
420 return authenticated;
421}
Damien Miller874d77b2000-10-14 16:23:11 +1100422
Damien Millereba71ba2000-04-29 23:57:08 +1000423int
Damien Miller874d77b2000-10-14 16:23:11 +1100424userauth_kbdint(Authctxt *authctxt)
425{
426 int authenticated = 0;
427 char *lang = NULL;
428 char *devs = NULL;
429
430 lang = packet_get_string(NULL);
431 devs = packet_get_string(NULL);
432 packet_done();
433
434 debug("keyboard-interactive language %s devs %s", lang, devs);
Damien Millerb8481582000-12-03 11:51:51 +1100435#ifdef USE_PAM
436 if (authenticated == 0)
437 authenticated = auth2_pam(authctxt);
438#endif
Damien Miller874d77b2000-10-14 16:23:11 +1100439#ifdef SKEY
440 /* XXX hardcoded, we should look at devs */
Damien Millerb8481582000-12-03 11:51:51 +1100441 if (authenticated == 0)
442 if (options.skey_authentication != 0)
443 authenticated = auth2_skey(authctxt);
Damien Miller874d77b2000-10-14 16:23:11 +1100444#endif
445 xfree(lang);
446 xfree(devs);
447#ifdef HAVE_CYGWIN
448 if (check_nt_auth(0, authctxt->pw->pw_uid) == 0)
449 return(0);
450#endif
451 return authenticated;
452}
453
454int
455userauth_pubkey(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000456{
457 Buffer b;
458 Key *key;
459 char *pkalg, *pkblob, *sig;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000460 u_int alen, blen, slen;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100461 int have_sig, pktype;
Damien Millereba71ba2000-04-29 23:57:08 +1000462 int authenticated = 0;
463
Damien Miller874d77b2000-10-14 16:23:11 +1100464 if (!authctxt->valid) {
465 debug2("userauth_pubkey: disabled because of invalid user");
Damien Millereba71ba2000-04-29 23:57:08 +1000466 return 0;
467 }
468 have_sig = packet_get_char();
Ben Lindstromd121f612000-12-03 17:00:47 +0000469 if (datafellows & SSH_BUG_PKAUTH) {
470 debug2("userauth_pubkey: SSH_BUG_PKAUTH");
471 /* no explicit pkalg given */
472 pkblob = packet_get_string(&blen);
473 buffer_init(&b);
474 buffer_append(&b, pkblob, blen);
475 /* so we have to extract the pkalg from the pkblob */
476 pkalg = buffer_get_string(&b, &alen);
477 buffer_free(&b);
478 } else {
479 pkalg = packet_get_string(&alen);
480 pkblob = packet_get_string(&blen);
481 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100482 pktype = key_type_from_name(pkalg);
483 if (pktype == KEY_UNSPEC) {
Ben Lindstromd121f612000-12-03 17:00:47 +0000484 /* this is perfectly legal */
485 log("userauth_pubkey: unsupported public key algorithm: %s", pkalg);
Damien Miller874d77b2000-10-14 16:23:11 +1100486 xfree(pkalg);
Ben Lindstromd121f612000-12-03 17:00:47 +0000487 xfree(pkblob);
Damien Millereba71ba2000-04-29 23:57:08 +1000488 return 0;
489 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100490 key = key_from_blob(pkblob, blen);
Damien Millereba71ba2000-04-29 23:57:08 +1000491 if (key != NULL) {
492 if (have_sig) {
493 sig = packet_get_string(&slen);
494 packet_done();
495 buffer_init(&b);
Damien Miller50a41ed2000-10-16 12:14:42 +1100496 if (datafellows & SSH_OLD_SESSIONID) {
Damien Miller6536c7d2000-06-22 21:32:31 +1000497 buffer_append(&b, session_id2, session_id2_len);
Damien Miller50a41ed2000-10-16 12:14:42 +1100498 } else {
499 buffer_put_string(&b, session_id2, session_id2_len);
Damien Miller6536c7d2000-06-22 21:32:31 +1000500 }
Damien Millerf6d9e222000-06-18 14:50:44 +1000501 /* reconstruct packet */
Damien Millereba71ba2000-04-29 23:57:08 +1000502 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
Damien Miller874d77b2000-10-14 16:23:11 +1100503 buffer_put_cstring(&b, authctxt->user);
Damien Millerf6d9e222000-06-18 14:50:44 +1000504 buffer_put_cstring(&b,
Ben Lindstromd121f612000-12-03 17:00:47 +0000505 datafellows & SSH_BUG_PKSERVICE ?
Damien Millerf6d9e222000-06-18 14:50:44 +1000506 "ssh-userauth" :
Damien Miller874d77b2000-10-14 16:23:11 +1100507 authctxt->service);
Ben Lindstromd121f612000-12-03 17:00:47 +0000508 if (datafellows & SSH_BUG_PKAUTH) {
509 buffer_put_char(&b, have_sig);
510 } else {
511 buffer_put_cstring(&b, "publickey");
512 buffer_put_char(&b, have_sig);
513 buffer_put_cstring(&b, key_ssh_name(key));
514 }
Damien Millerf6d9e222000-06-18 14:50:44 +1000515 buffer_put_string(&b, pkblob, blen);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100516#ifdef DEBUG_PK
Damien Millereba71ba2000-04-29 23:57:08 +1000517 buffer_dump(&b);
518#endif
519 /* test for correct signature */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100520 if (user_key_allowed(authctxt->pw, key) &&
521 key_verify(key, sig, slen, buffer_ptr(&b), buffer_len(&b)) == 1)
Damien Millereba71ba2000-04-29 23:57:08 +1000522 authenticated = 1;
523 buffer_clear(&b);
524 xfree(sig);
525 } else {
Damien Miller874d77b2000-10-14 16:23:11 +1100526 debug("test whether pkalg/pkblob are acceptable");
Damien Millereba71ba2000-04-29 23:57:08 +1000527 packet_done();
Damien Miller874d77b2000-10-14 16:23:11 +1100528
Damien Millereba71ba2000-04-29 23:57:08 +1000529 /* XXX fake reply and always send PK_OK ? */
Damien Millere247cc42000-05-07 12:03:14 +1000530 /*
531 * XXX this allows testing whether a user is allowed
532 * to login: if you happen to have a valid pubkey this
533 * message is sent. the message is NEVER sent at all
534 * if a user is not allowed to login. is this an
535 * issue? -markus
536 */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100537 if (user_key_allowed(authctxt->pw, key)) {
Damien Millereba71ba2000-04-29 23:57:08 +1000538 packet_start(SSH2_MSG_USERAUTH_PK_OK);
539 packet_put_string(pkalg, alen);
540 packet_put_string(pkblob, blen);
541 packet_send();
542 packet_write_wait();
543 authenticated = -1;
544 }
545 }
Damien Miller874d77b2000-10-14 16:23:11 +1100546 if (authenticated != 1)
547 auth_clear_options();
Damien Millereba71ba2000-04-29 23:57:08 +1000548 key_free(key);
549 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100550 debug2("userauth_pubkey: authenticated %d pkalg %s", authenticated, pkalg);
Damien Millereba71ba2000-04-29 23:57:08 +1000551 xfree(pkalg);
552 xfree(pkblob);
Damien Miller874d77b2000-10-14 16:23:11 +1100553#ifdef HAVE_CYGWIN
554 if (check_nt_auth(0, authctxt->pw->pw_uid) == 0)
555 return(0);
556#endif
Damien Millereba71ba2000-04-29 23:57:08 +1000557 return authenticated;
558}
559
Damien Miller874d77b2000-10-14 16:23:11 +1100560/* get current user */
Damien Millereba71ba2000-04-29 23:57:08 +1000561
562struct passwd*
563auth_get_user(void)
564{
Damien Miller874d77b2000-10-14 16:23:11 +1100565 return (x_authctxt != NULL && x_authctxt->valid) ? x_authctxt->pw : NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000566}
567
Damien Miller874d77b2000-10-14 16:23:11 +1100568#define DELIM ","
Damien Millereba71ba2000-04-29 23:57:08 +1000569
Damien Miller874d77b2000-10-14 16:23:11 +1100570char *
571authmethods_get(void)
572{
573 Authmethod *method = NULL;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000574 u_int size = 0;
Damien Miller874d77b2000-10-14 16:23:11 +1100575 char *list;
576
577 for (method = authmethods; method->name != NULL; method++) {
578 if (strcmp(method->name, "none") == 0)
579 continue;
580 if (method->enabled != NULL && *(method->enabled) != 0) {
581 if (size != 0)
582 size += strlen(DELIM);
583 size += strlen(method->name);
Damien Millereba71ba2000-04-29 23:57:08 +1000584 }
585 }
Damien Miller874d77b2000-10-14 16:23:11 +1100586 size++; /* trailing '\0' */
587 list = xmalloc(size);
588 list[0] = '\0';
589
590 for (method = authmethods; method->name != NULL; method++) {
591 if (strcmp(method->name, "none") == 0)
592 continue;
593 if (method->enabled != NULL && *(method->enabled) != 0) {
594 if (list[0] != '\0')
595 strlcat(list, DELIM, size);
596 strlcat(list, method->name, size);
597 }
598 }
599 return list;
600}
601
602Authmethod *
603authmethod_lookup(const char *name)
604{
605 Authmethod *method = NULL;
606 if (name != NULL)
607 for (method = authmethods; method->name != NULL; method++)
608 if (method->enabled != NULL &&
609 *(method->enabled) != 0 &&
610 strcmp(name, method->name) == 0)
611 return method;
612 debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
613 return NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000614}
615
616/* return 1 if user allows given key */
617int
Damien Miller0bc1bd82000-11-13 22:57:25 +1100618user_key_allowed(struct passwd *pw, Key *key)
Damien Millereba71ba2000-04-29 23:57:08 +1000619{
620 char line[8192], file[1024];
621 int found_key = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000622 FILE *f;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000623 u_long linenum = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000624 struct stat st;
625 Key *found;
626
Damien Miller874d77b2000-10-14 16:23:11 +1100627 if (pw == NULL)
628 return 0;
629
Damien Millereba71ba2000-04-29 23:57:08 +1000630 /* Temporarily use the user's uid. */
631 temporarily_use_uid(pw->pw_uid);
632
633 /* The authorized keys. */
634 snprintf(file, sizeof file, "%.500s/%.100s", pw->pw_dir,
635 SSH_USER_PERMITTED_KEYS2);
636
637 /* Fail quietly if file does not exist */
638 if (stat(file, &st) < 0) {
639 /* Restore the privileged uid. */
640 restore_uid();
641 return 0;
642 }
643 /* Open the file containing the authorized keys. */
644 f = fopen(file, "r");
645 if (!f) {
646 /* Restore the privileged uid. */
647 restore_uid();
648 return 0;
649 }
650 if (options.strict_modes) {
651 int fail = 0;
652 char buf[1024];
653 /* Check open file in order to avoid open/stat races */
654 if (fstat(fileno(f), &st) < 0 ||
655 (st.st_uid != 0 && st.st_uid != pw->pw_uid) ||
656 (st.st_mode & 022) != 0) {
Damien Millerd3444942000-09-30 14:20:03 +1100657 snprintf(buf, sizeof buf,
658 "%s authentication refused for %.100s: "
659 "bad ownership or modes for '%s'.",
660 key_type(key), pw->pw_name, file);
Damien Millereba71ba2000-04-29 23:57:08 +1000661 fail = 1;
662 } else {
663 /* Check path to SSH_USER_PERMITTED_KEYS */
664 int i;
665 static const char *check[] = {
666 "", SSH_USER_DIR, NULL
667 };
668 for (i = 0; check[i]; i++) {
669 snprintf(line, sizeof line, "%.500s/%.100s",
670 pw->pw_dir, check[i]);
671 if (stat(line, &st) < 0 ||
672 (st.st_uid != 0 && st.st_uid != pw->pw_uid) ||
673 (st.st_mode & 022) != 0) {
674 snprintf(buf, sizeof buf,
Damien Millerd3444942000-09-30 14:20:03 +1100675 "%s authentication refused for %.100s: "
Damien Millereba71ba2000-04-29 23:57:08 +1000676 "bad ownership or modes for '%s'.",
Damien Millerd3444942000-09-30 14:20:03 +1100677 key_type(key), pw->pw_name, line);
Damien Millereba71ba2000-04-29 23:57:08 +1000678 fail = 1;
679 break;
680 }
681 }
682 }
683 if (fail) {
Damien Millereba71ba2000-04-29 23:57:08 +1000684 fclose(f);
Damien Miller37023962000-07-11 17:31:38 +1000685 log("%s",buf);
Damien Millereba71ba2000-04-29 23:57:08 +1000686 restore_uid();
687 return 0;
688 }
689 }
690 found_key = 0;
Damien Millerd3444942000-09-30 14:20:03 +1100691 found = key_new(key->type);
Damien Millereba71ba2000-04-29 23:57:08 +1000692
693 while (fgets(line, sizeof(line), f)) {
Damien Millerf6d9e222000-06-18 14:50:44 +1000694 char *cp, *options = NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000695 linenum++;
696 /* Skip leading whitespace, empty and comment lines. */
697 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
698 ;
699 if (!*cp || *cp == '\n' || *cp == '#')
700 continue;
Damien Millerf6d9e222000-06-18 14:50:44 +1000701
Damien Miller0bc1bd82000-11-13 22:57:25 +1100702 if (key_read(found, &cp) == -1) {
Damien Millerf6d9e222000-06-18 14:50:44 +1000703 /* no key? check if there are options for this key */
704 int quoted = 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100705 debug2("user_key_allowed: check options: '%s'", cp);
Damien Millerf6d9e222000-06-18 14:50:44 +1000706 options = cp;
707 for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
708 if (*cp == '\\' && cp[1] == '"')
709 cp++; /* Skip both */
710 else if (*cp == '"')
711 quoted = !quoted;
712 }
713 /* Skip remaining whitespace. */
714 for (; *cp == ' ' || *cp == '\t'; cp++)
715 ;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100716 if (key_read(found, &cp) == -1) {
717 debug2("user_key_allowed: advance: '%s'", cp);
Damien Millerf6d9e222000-06-18 14:50:44 +1000718 /* still no key? advance to next line*/
719 continue;
720 }
721 }
722 if (key_equal(found, key) &&
723 auth_parse_options(pw, options, linenum) == 1) {
Damien Millereba71ba2000-04-29 23:57:08 +1000724 found_key = 1;
725 debug("matching key found: file %s, line %ld",
726 file, linenum);
727 break;
728 }
729 }
730 restore_uid();
731 fclose(f);
732 key_free(found);
733 return found_key;
734}
Damien Miller874d77b2000-10-14 16:23:11 +1100735
736struct passwd *
737pwcopy(struct passwd *pw)
738{
739 struct passwd *copy = xmalloc(sizeof(*copy));
740 memset(copy, 0, sizeof(*copy));
741 copy->pw_name = xstrdup(pw->pw_name);
742 copy->pw_passwd = xstrdup(pw->pw_passwd);
743 copy->pw_uid = pw->pw_uid;
744 copy->pw_gid = pw->pw_gid;
745#ifdef HAVE_PW_CLASS_IN_PASSWD
746 copy->pw_class = xstrdup(pw->pw_class);
747#endif
748 copy->pw_dir = xstrdup(pw->pw_dir);
749 copy->pw_shell = xstrdup(pw->pw_shell);
750 return copy;
751}