auth2.c revision 4a2e944d74dafc80c85d74c8b11430bbecd98824
/*
* Copyright (c) 2000 Markus Friedl. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
*/
#include "includes.h"
#include "ssh2.h"
#include "xmalloc.h"
#include "packet.h"
#include "log.h"
#include "servconf.h"
#include "compat.h"
#include "misc.h"
#include "auth.h"
#include "dispatch.h"
#include "sshlogin.h"
#include "pathnames.h"
#ifdef HAVE_BSM
#include "bsmaudit.h"
extern adt_session_data_t *ah;
#endif /* HAVE_BSM */
#ifdef GSSAPI
#include "ssh-gss.h"
#endif
/* import */
extern ServerOptions options;
extern u_char *session_id2;
extern int session_id2_len;
/* methods */
extern Authmethod method_none;
extern Authmethod method_pubkey;
extern Authmethod method_passwd;
extern Authmethod method_kbdint;
extern Authmethod method_hostbased;
extern Authmethod method_external;
extern Authmethod method_gssapi;
static Authmethod *authmethods[] = {
#ifdef GSSAPI
#endif
};
/* protocol */
static void input_service_request(int, u_int32_t, void *);
static void input_userauth_request(int, u_int32_t, void *);
/* helper */
static Authmethod *authmethod_lookup(const char *);
static char *authmethods_get(void);
Authmethod *method);
/*static char *authmethods_get_kbdint(void);*/
static void userauth_reset_methods(void);
/*
* loop until authctxt->success == TRUE
*/
Authctxt *
do_authentication2(void)
{
#ifdef HAVE_BSM
#endif /* HAVE_BSM */
return (authctxt);
}
static void
{
int acceptit = 0;
fatal("input_service_request: no authctxt");
acceptit = 1;
/* now we can handle user-auth requests */
}
}
/* XXX all other service requests are denied */
if (acceptit) {
packet_send();
} else {
}
}
static void
{
Authmethod *m = NULL;
int valid_attempt;
fatal("input_userauth_request: no authctxt");
debug("attempt %d initial attempt %d failures %d initial failures %d",
m = authmethod_lookup(method);
*style++ = 0;
if (m != NULL && m->is_initial)
authctxt->init_attempt++;
valid_attempt = 0;
} else {
valid_attempt = 1;
}
/* setup auth context */
/* May want to abstract SSHv2 services someday */
/* enforced in userauth_finish() below */
if (valid_attempt) {
}
} else {
}
} else {
char *abandoned;
/*
* Check for abandoned [multi-round-trip] userauths
* methods (e.g., kbdint). Userauth method abandonment
* should be treated as userauth method failure and
* counted against max_auth_tries.
*/
/* userauth_finish() will now packet_disconnect() */
/* NOTREACHED */
}
/* Handle user|service changes, possibly packet_disconnect() */
}
/* run userauth method, try to authenticate user */
if (m != NULL && userauth_method_can_run(m)) {
m->postponed = 0;
m->abandoned = 0;
m->authenticated = 0;
if (!m->is_initial ||
if (authctxt->unwind_dispatch_loop) {
/*
* Method ran nested dispatch loop but was
* abandoned. Cleanup and return without doing
* anything else; we're just unwinding the stack.
*/
authctxt->unwind_dispatch_loop = 0;
goto done;
}
if (m->postponed)
goto done; /* multi-round trip userauth not finished */
if (m->abandoned) {
/* multi-round trip userauth abandoned, log failure */
goto done;
}
}
done:
}
void
{
int authenticated, partial;
/* unknown method handling -- must elicit userauth failure msg */
authenticated = 0;
partial = 0;
goto done_checking;
}
#ifndef USE_PAM
/* Special handling for root (done elsewhere for PAM) */
#endif /* USE_PAM */
#ifdef _UNICOS
}
#endif /* _UNICOS */
#ifdef USE_PAM
/*
* If the userauth method failed to complete PAM work then force
* partial failure.
*/
partial = 1;
#endif /* USE_PAM */
/*
* To properly support invalid userauth method names we set
* authenticated=0, partial=0 above and know that
* authctxt->method == NULL.
*
* No unguarded reference to authctxt->method allowed from here.
* Checking authenticated != 0 is a valid guard; authctxt->method
* MUST NOT be NULL if authenticated.
*/
/*
* We get here if the PreUserauthHook fails but the
* user is otherwise valid.
* An error in the PAM handling could also get us here
* but we need not panic, just treat as a failure.
*/
authenticated = 0;
log("Ignoring authenticated invalid user %s",
}
/* Log before sending the reply */
if (authenticated && !partial) {
/* turn off userauth */
packet_send();
/* now we can break out */
} else {
char *methods;
#ifdef USE_PAM
/*
* Keep track of last PAM error (or PERM_DENIED) for BSM
* login failure auditing, which may run after the PAM
* state has been cleaned up.
*/
#endif /* USE_PAM */
#ifdef HAVE_BSM
authctxt);
#endif /* HAVE_BSM */
}
#ifdef _UNICOS
#endif /* _UNICOS */
/*
* If (partial) then authmethods_get() will return only
* required methods, likely only "keyboard-interactive;"
* (methods == NULL) implies failure, even if (partial == 1)
*/
methods = authmethods_get();
if (methods)
packet_send();
}
}
/* get current user */
struct passwd*
auth_get_user(void)
{
}
#define DELIM ","
#if 0
static char *
authmethods_get_kbdint(void)
{
Buffer b;
int i;
for (i = 0; authmethods[i] != NULL; i++) {
continue;
}
return NULL;
}
#endif
void
{
/*
* NOTE:
*
* SSHv2 services should be abstracted and service changes during
* userauth should be supported as per the userauth draft. In the PAM
* case, support for multiple SSHv2 services means that we have to
* format the PAM service name according to the SSHv2 service *and* the
* SSHv2 userauth being attempted ("passwd", "kbdint" and "other").
*
* We'll cross that bridge when we come to it. For now disallow service
* changes during userauth if using PAM, but allow username changes.
*/
/* authctxt->service must == ssh-connection here */
packet_disconnect("Change of service not "
"allowed: %s and %s",
}
return;
/* All good; update authctxt */
/* Forget method state; abandon postponed userauths */
}
int
{
int i;
int required = 0;
int sufficient = 0;
/*
* v1 does not set authctxt->method
* partial userauth failure is a v2 concept
*/
return 0;
for (i = 0; authmethods[i] != NULL; i++) {
if (authmethods[i]->required)
required++;
if (authmethods[i]->sufficient)
sufficient++;
}
if (required == 0 && sufficient == 0)
return 1;
}
int
{
return 0;
return 1;
}
static
void
userauth_reset_methods(void)
{
int i;
for (i = 0; authmethods[i] != NULL; i++) {
/* note: counters not reset */
authmethods[i]->required = 0;
authmethods[i]->sufficient = 0;
authmethods[i]->authenticated = 0;
authmethods[i]->not_again = 0;
authmethods[i]->postponed = 0;
authmethods[i]->abandoned = 0;
}
}
void
userauth_force_kbdint(void)
{
int i;
for (i = 0; authmethods[i] != NULL; i++) {
authmethods[i]->required = 0;
authmethods[i]->sufficient = 0;
}
}
/*
* Check to see if a previously run multi-round trip userauth method has
* been abandoned and call its cleanup function.
*
* Abandoned userauth method invocations are counted as userauth failures.
*/
static
char *
{
int i;
/* optimization: check current method first */
else
if (method->is_initial)
/*
* Since we check for abandonment whenever a userauth is
* requested we know only one method could have been
* in postponed state, so we can return now.
*/
}
for (i = 0; authmethods[i] != NULL; i++) {
if (!authmethods[i]->postponed)
continue;
/* some method was postponed and a diff one is being started */
if (method != authmethods[i]) {
authmethods[i]->postponed = 0;
if (authmethods[i]->abandon)
authmethods[i]);
else
authmethods[i]->abandons++;
if (authmethods[i]->is_initial)
}
}
return NULL;
}
static char *
authmethods_get(void)
{
Buffer b;
char *list;
int i;
int sufficient = 0;
int required = 0;
int authenticated = 0;
int partial = 0;
/*
* If at least one method succeeded partially then at least one
* authmethod will be required and only required methods should
* continue.
*/
for (i = 0; authmethods[i] != NULL; i++) {
if (authmethods[i]->authenticated)
if (authmethods[i]->required)
required++;
if (authmethods[i]->sufficient)
sufficient++;
}
buffer_init(&b);
for (i = 0; authmethods[i] != NULL; i++) {
continue;
continue;
continue;
if (authmethods[i]->not_again)
continue;
if (authmethods[i]->required) {
if (buffer_len(&b) > 0)
continue;
}
/*
* A method can be enabled (marked sufficient)
* dynamically provided that at least one other method
* has succeeded partially.
*/
*(authmethods[i]->enabled) != 0)) {
if (buffer_len(&b) > 0)
}
}
buffer_free(&b);
return list;
}
static Authmethod *
authmethod_lookup(const char *name)
{
int i;
/*
* Method must be sufficient, required or enabled and must not
* be marked as not able to run again
*/
for (i = 0; authmethods[i] != NULL; i++)
if (((authmethods[i]->sufficient ||
authmethods[i]->required) ||
*(authmethods[i]->enabled) != 0)) &&
!authmethods[i]->not_again &&
return authmethods[i];
debug2("Unrecognized authentication method name: %s",
return NULL;
}
static void
{
if (!method)
fatal("Internal error in authmethod_count_attempt()");
return;
else if (method->authenticated)
else
return;
}