1 /* $OpenBSD: auth2.c,v 1.121 2009/06/22 05:39:28 dtucker Exp $ */
3 * Copyright (c) 2000 Markus Friedl. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
28 #include <sys/types.h>
50 #include "pathnames.h"
57 #include "monitor_wrap.h"
60 extern ServerOptions options;
61 extern u_char *session_id2;
62 extern u_int session_id2_len;
63 extern Buffer loginmsg;
67 extern Authmethod method_none;
68 extern Authmethod method_pubkey;
69 extern Authmethod method_passwd;
70 extern Authmethod method_kbdint;
71 extern Authmethod method_hostbased;
73 extern Authmethod method_external;
74 extern Authmethod method_gsskeyex;
75 extern Authmethod method_gssapi;
76 extern Authmethod method_gssapi_compat;
79 extern Authmethod method_jpake;
82 static int log_flag = 0;
85 Authmethod *authmethods[] = {
92 &method_gssapi_compat,
105 static void input_service_request(int, u_int32_t, void *);
106 static void input_userauth_request(int, u_int32_t, void *);
109 static Authmethod *authmethod_lookup(const char *);
110 static char *authmethods_get(void);
113 auth2_read_banner(void)
120 if ((fd = open(options.banner, O_RDONLY)) == -1)
122 if (fstat(fd, &st) == -1) {
126 if (st.st_size > 1*1024*1024) {
131 len = (size_t)st.st_size; /* truncate */
132 banner = xmalloc(len + 1);
133 n = atomicio(read, fd, banner, len);
146 userauth_send_banner(const char *msg)
148 if (datafellows & SSH_BUG_BANNER)
151 packet_start(SSH2_MSG_USERAUTH_BANNER);
152 packet_put_cstring(msg);
153 packet_put_cstring(""); /* language, unused */
155 debug("%s: sent", __func__);
159 userauth_banner(void)
163 if (options.banner == NULL ||
164 strcasecmp(options.banner, "none") == 0 ||
165 (datafellows & SSH_BUG_BANNER) != 0)
168 if ((banner = PRIVSEP(auth2_read_banner())) == NULL)
170 userauth_send_banner(banner);
178 * loop until authctxt->success == TRUE
181 do_authentication2(Authctxt *authctxt)
183 dispatch_init(&dispatch_protocol_error);
184 dispatch_set(SSH2_MSG_SERVICE_REQUEST, &input_service_request);
185 dispatch_run(DISPATCH_BLOCK, &authctxt->success, authctxt);
190 input_service_request(int type, u_int32_t seq, void *ctxt)
192 Authctxt *authctxt = ctxt;
195 char *service = packet_get_string(&len);
198 if (authctxt == NULL)
199 fatal("input_service_request: no authctxt");
201 if (strcmp(service, "ssh-userauth") == 0) {
202 if (!authctxt->success) {
204 /* now we can handle user-auth requests */
205 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &input_userauth_request);
208 /* XXX all other service requests are denied */
211 packet_start(SSH2_MSG_SERVICE_ACCEPT);
212 packet_put_cstring(service);
216 debug("bad service request %s", service);
217 packet_disconnect("bad service request %s", service);
224 input_userauth_request(int type, u_int32_t seq, void *ctxt)
226 Authctxt *authctxt = ctxt;
227 Authmethod *m = NULL;
228 char *user, *service, *method, *style = NULL;
229 int authenticated = 0;
231 if (authctxt == NULL)
232 fatal("input_userauth_request: no authctxt");
234 user = packet_get_string(NULL);
235 service = packet_get_string(NULL);
236 method = packet_get_string(NULL);
239 if (user[0] == '\0') {
240 debug("received empty username for %s", method);
241 if (strcmp(method, "external-keyx") == 0 ||
242 strcmp(method, "gssapi-keyex") == 0) {
244 PRIVSEP(ssh_gssapi_localname(&lname));
245 if (lname && lname[0] != '\0') {
248 debug("set username to %s from gssapi context", user);
250 debug("failed to set username from gssapi context");
251 packet_send_debug("failed to set username from gssapi context");
257 debug("userauth-request for user %s service %s method %s",
258 user[0] ? user : "<implicit>", service, method);
260 logit("SSH: Server;Ltype: Authname;Remote: %s-%d;Name: %s",
261 get_remote_ipaddr(), get_remote_port(),
262 user[0] ? user : "<implicit>");
265 debug("attempt %d failures %d", authctxt->attempt, authctxt->failures);
267 if ((style = strchr(user, ':')) != NULL)
270 /* If first time or username changed or empty username,
271 setup/reset authentication context. */
272 if ((authctxt->attempt++ == 0) ||
273 (strcmp(user, authctxt->user) != 0) ||
274 (strcmp(user, "") == 0)) {
275 if (authctxt->user) {
276 xfree(authctxt->user);
277 authctxt->user = NULL;
280 authctxt->user = xstrdup(user);
281 if (strcmp(service, "ssh-connection") != 0) {
282 packet_disconnect("Unsupported service %s", service);
285 /* If we're going to set the username based on the
286 GSSAPI context later, then wait until then to
287 verify it. Just put in placeholders for now. */
288 if ((strcmp(user, "") == 0) &&
289 ((strcmp(method, "gssapi") == 0) ||
290 (strcmp(method, "gssapi-with-mic") == 0))) {
291 authctxt->pw = fakepw();
294 authctxt->pw = PRIVSEP(getpwnamallow(user));
297 debug2("input_userauth_request: setting up authctxt for %s", user);
299 logit("input_userauth_request: invalid user %s", user);
300 authctxt->pw = fakepw();
301 #ifdef SSH_AUDIT_EVENTS
302 PRIVSEP(audit_event(SSH_INVALID_USER));
306 } /* endif for setting username based on GSSAPI context */
310 PRIVSEP(start_pam(authctxt));
312 setproctitle("%s%s", authctxt->valid ? user : "unknown",
313 use_privsep ? " [net]" : "");
314 if (authctxt->attempt == 1) {
315 authctxt->service = xstrdup(service);
316 authctxt->style = style ? xstrdup(style) : NULL;
318 mm_inform_authserv(service, style);
322 if (strcmp(service, authctxt->service) != 0) {
323 packet_disconnect("Change of service not allowed: "
324 "(%s,%s) -> (%s,%s)",
325 authctxt->user, authctxt->service, user, service);
328 auth2_challenge_stop(authctxt);
330 auth2_jpake_stop(authctxt);
334 /* XXX move to auth2_gssapi_stop() */
335 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
336 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, NULL);
339 authctxt->postponed = 0;
340 authctxt->server_caused_failure = 0;
342 /* try to authenticate user */
343 m = authmethod_lookup(method);
344 if (m != NULL && authctxt->failures < options.max_authtries) {
345 debug2("input_userauth_request: try method %s", method);
346 authenticated = m->userauth(authctxt);
348 userauth_finish(authctxt, authenticated, method);
356 userauth_finish(Authctxt *authctxt, int authenticated, char *method)
360 if (!authctxt->valid && authenticated)
361 fatal("INTERNAL ERROR: authenticated invalid user %s",
364 /* Special handling for root */
365 if (authenticated && authctxt->pw->pw_uid == 0 &&
366 !auth_root_allowed(method)) {
368 #ifdef SSH_AUDIT_EVENTS
369 PRIVSEP(audit_event(SSH_LOGIN_ROOT_DENIED));
374 if (options.use_pam && authenticated) {
375 if (!PRIVSEP(do_pam_account())) {
376 /* if PAM returned a message, send it to the user */
377 if (buffer_len(&loginmsg) > 0) {
378 buffer_append(&loginmsg, "\0", 1);
379 userauth_send_banner(buffer_ptr(&loginmsg));
382 fatal("Access denied for user %s by PAM account "
383 "configuration", authctxt->user);
389 if (authenticated && cray_access_denied(authctxt->user)) {
391 fatal("Access denied for user %s.",authctxt->user);
395 /* Log before sending the reply */
396 auth_log(authctxt, authenticated, method, " ssh2");
398 if (authctxt->postponed)
401 /* XXX todo: check if multiple auth methods are needed */
402 if (authenticated == 1) {
403 /* turn off userauth */
404 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &dispatch_protocol_ignore);
405 packet_start(SSH2_MSG_USERAUTH_SUCCESS);
408 /* now we can break out */
409 authctxt->success = 1;
411 /* Dont count server configuration issues against the client */
412 /* Allow initial try of "none" auth without failure penalty */
413 if (!authctxt->server_caused_failure &&
414 (authctxt->attempt > 1 || strcmp(method, "none") != 0))
415 authctxt->failures++;
416 if (authctxt->failures >= options.max_authtries) {
417 #ifdef SSH_AUDIT_EVENTS
418 PRIVSEP(audit_event(SSH_LOGIN_EXCEED_MAXTRIES));
420 packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
422 methods = authmethods_get();
423 packet_start(SSH2_MSG_USERAUTH_FAILURE);
424 packet_put_cstring(methods);
425 packet_put_char(0); /* XXX partial success, unused */
433 authmethods_get(void)
440 for (i = 0; authmethods[i] != NULL; i++) {
441 if (strcmp(authmethods[i]->name, "none") == 0)
443 if (authmethods[i]->enabled != NULL &&
444 *(authmethods[i]->enabled) != 0) {
445 if (buffer_len(&b) > 0)
446 buffer_append(&b, ",", 1);
447 buffer_append(&b, authmethods[i]->name,
448 strlen(authmethods[i]->name));
451 buffer_append(&b, "\0", 1);
452 list = xstrdup(buffer_ptr(&b));
458 authmethod_lookup(const char *name)
463 for (i = 0; authmethods[i] != NULL; i++)
464 if (authmethods[i]->enabled != NULL &&
465 *(authmethods[i]->enabled) != 0 &&
466 strcmp(name, authmethods[i]->name) == 0)
467 return authmethods[i];
468 debug2("Unrecognized authentication method name: %s",
469 name ? name : "NULL");