]> andersk Git - openssh.git/blame - auth2.c
- stevesk@cvs.openbsd.org 2006/08/01 23:36:12
[openssh.git] / auth2.c
CommitLineData
00146caa 1/* $OpenBSD: auth2.c,v 1.112 2006/07/22 20:48:22 stevesk Exp $ */
a306f2dd 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.
a306f2dd 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 */
bcbf86ec 25
a306f2dd 26#include "includes.h"
a306f2dd 27
b1842393 28#include <sys/types.h>
29
30#include <pwd.h>
00146caa 31#include <string.h>
b1842393 32
42f11eb2 33#include "ssh2.h"
a306f2dd 34#include "xmalloc.h"
a306f2dd 35#include "packet.h"
42f11eb2 36#include "log.h"
a306f2dd 37#include "servconf.h"
38#include "compat.h"
a306f2dd 39#include "auth.h"
a306f2dd 40#include "dispatch.h"
42f11eb2 41#include "pathnames.h"
1853d1ef 42#include "monitor_wrap.h"
ba6dd90e 43#include "buffer.h"
a306f2dd 44
7364bd04 45#ifdef GSSAPI
46#include "ssh-gss.h"
47#endif
48
a306f2dd 49/* import */
50extern ServerOptions options;
1e3b8b07 51extern u_char *session_id2;
a27002e5 52extern u_int session_id2_len;
ba6dd90e 53extern Buffer loginmsg;
a306f2dd 54
01c24737 55/* methods */
56
57extern Authmethod method_none;
58extern Authmethod method_pubkey;
59extern Authmethod method_passwd;
60extern Authmethod method_kbdint;
61extern Authmethod method_hostbased;
7364bd04 62#ifdef GSSAPI
63extern Authmethod method_gssapi;
64#endif
01c24737 65
66Authmethod *authmethods[] = {
67 &method_none,
68 &method_pubkey,
7364bd04 69#ifdef GSSAPI
70 &method_gssapi,
71#endif
01c24737 72 &method_passwd,
73 &method_kbdint,
74 &method_hostbased,
75 NULL
94ec8c6b 76};
77
a306f2dd 78/* protocol */
79
7819b5c3 80static void input_service_request(int, u_int32_t, void *);
81static void input_userauth_request(int, u_int32_t, void *);
a306f2dd 82
a306f2dd 83/* helper */
396c147e 84static Authmethod *authmethod_lookup(const char *);
ffb215be 85static char *authmethods_get(void);
1853d1ef 86int user_key_allowed(struct passwd *, Key *);
a306f2dd 87
a306f2dd 88/*
94ec8c6b 89 * loop until authctxt->success == TRUE
a306f2dd 90 */
91
2362db19 92void
93do_authentication2(Authctxt *authctxt)
a306f2dd 94{
aa8003d6 95 /* challenge-response is implemented via keyboard interactive */
5ba55ada 96 if (options.challenge_response_authentication)
d464095c 97 options.kbd_interactive_authentication = 1;
98
6367063f 99 dispatch_init(&dispatch_protocol_error);
a306f2dd 100 dispatch_set(SSH2_MSG_SERVICE_REQUEST, &input_service_request);
94ec8c6b 101 dispatch_run(DISPATCH_BLOCK, &authctxt->success, authctxt);
a306f2dd 102}
103
fe8c3af1 104/*ARGSUSED*/
396c147e 105static void
7819b5c3 106input_service_request(int type, u_int32_t seq, void *ctxt)
a306f2dd 107{
94ec8c6b 108 Authctxt *authctxt = ctxt;
1e3b8b07 109 u_int len;
7fdc56c5 110 int acceptit = 0;
a306f2dd 111 char *service = packet_get_string(&len);
95500969 112 packet_check_eom();
a306f2dd 113
94ec8c6b 114 if (authctxt == NULL)
115 fatal("input_service_request: no authctxt");
116
a306f2dd 117 if (strcmp(service, "ssh-userauth") == 0) {
94ec8c6b 118 if (!authctxt->success) {
7fdc56c5 119 acceptit = 1;
a306f2dd 120 /* now we can handle user-auth requests */
121 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &input_userauth_request);
122 }
123 }
124 /* XXX all other service requests are denied */
125
7fdc56c5 126 if (acceptit) {
a306f2dd 127 packet_start(SSH2_MSG_SERVICE_ACCEPT);
128 packet_put_cstring(service);
129 packet_send();
130 packet_write_wait();
131 } else {
132 debug("bad service request %s", service);
133 packet_disconnect("bad service request %s", service);
134 }
135 xfree(service);
136}
137
fe8c3af1 138/*ARGSUSED*/
396c147e 139static void
7819b5c3 140input_userauth_request(int type, u_int32_t seq, void *ctxt)
a306f2dd 141{
94ec8c6b 142 Authctxt *authctxt = ctxt;
143 Authmethod *m = NULL;
59c97189 144 char *user, *service, *method, *style = NULL;
a306f2dd 145 int authenticated = 0;
a306f2dd 146
94ec8c6b 147 if (authctxt == NULL)
148 fatal("input_userauth_request: no authctxt");
a306f2dd 149
94ec8c6b 150 user = packet_get_string(NULL);
151 service = packet_get_string(NULL);
152 method = packet_get_string(NULL);
153 debug("userauth-request for user %s service %s method %s", user, service, method);
8abcdba4 154 debug("attempt %d failures %d", authctxt->attempt, authctxt->failures);
94ec8c6b 155
59c97189 156 if ((style = strchr(user, ':')) != NULL)
157 *style++ = 0;
158
2b87da3b 159 if (authctxt->attempt++ == 0) {
63284fbb 160 /* setup auth context */
f7ed12f1 161 authctxt->pw = PRIVSEP(getpwnamallow(user));
529d73ab 162 authctxt->user = xstrdup(user);
f7ed12f1 163 if (authctxt->pw && strcmp(service, "ssh-connection")==0) {
94ec8c6b 164 authctxt->valid = 1;
165 debug2("input_userauth_request: setting up authctxt for %s", user);
94ec8c6b 166 } else {
d77347cc 167 logit("input_userauth_request: invalid user %s", user);
96d0bf74 168 authctxt->pw = fakepw();
6039eeef 169#ifdef SSH_AUDIT_EVENTS
170 PRIVSEP(audit_event(SSH_INVALID_USER));
04fc7a67 171#endif
94ec8c6b 172 }
b6c37221 173#ifdef USE_PAM
174 if (options.use_pam)
175 PRIVSEP(start_pam(authctxt));
176#endif
bd5c0694 177 setproctitle("%s%s", authctxt->valid ? user : "unknown",
1853d1ef 178 use_privsep ? " [net]" : "");
94ec8c6b 179 authctxt->service = xstrdup(service);
2a6a054e 180 authctxt->style = style ? xstrdup(style) : NULL;
1853d1ef 181 if (use_privsep)
182 mm_inform_authserv(service, style);
2a6a054e 183 } else if (strcmp(user, authctxt->user) != 0 ||
184 strcmp(service, authctxt->service) != 0) {
185 packet_disconnect("Change of username or service not allowed: "
186 "(%s,%s) -> (%s,%s)",
187 authctxt->user, authctxt->service, user, service);
a306f2dd 188 }
59c97189 189 /* reset state */
2f293d43 190 auth2_challenge_stop(authctxt);
7364bd04 191
192#ifdef GSSAPI
193 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
194 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, NULL);
195#endif
196
59c97189 197 authctxt->postponed = 0;
9cd45ea4 198
59c97189 199 /* try to authenticate user */
94ec8c6b 200 m = authmethod_lookup(method);
201 if (m != NULL) {
202 debug2("input_userauth_request: try method %s", method);
203 authenticated = m->userauth(authctxt);
9cd45ea4 204 }
d9cd3575 205 userauth_finish(authctxt, authenticated, method);
206
207 xfree(service);
208 xfree(user);
209 xfree(method);
210}
211
212void
213userauth_finish(Authctxt *authctxt, int authenticated, char *method)
214{
4e81159c 215 char *methods;
216
59c97189 217 if (!authctxt->valid && authenticated)
218 fatal("INTERNAL ERROR: authenticated invalid user %s",
219 authctxt->user);
9cd45ea4 220
94ec8c6b 221 /* Special handling for root */
5d47f1d4 222 if (authenticated && authctxt->pw->pw_uid == 0 &&
c00e4d75 223 !auth_root_allowed(method)) {
a306f2dd 224 authenticated = 0;
6039eeef 225#ifdef SSH_AUDIT_EVENTS
226 PRIVSEP(audit_event(SSH_LOGIN_ROOT_DENIED));
c00e4d75 227#endif
228 }
a306f2dd 229
5b9e2464 230#ifdef USE_PAM
ba6dd90e 231 if (options.use_pam && authenticated) {
232 if (!PRIVSEP(do_pam_account())) {
ba6dd90e 233 /* if PAM returned a message, send it to the user */
234 if (buffer_len(&loginmsg) > 0) {
235 buffer_append(&loginmsg, "\0", 1);
236 userauth_send_banner(buffer_ptr(&loginmsg));
9aab0af7 237 packet_write_wait();
ba6dd90e 238 }
9aab0af7 239 fatal("Access denied for user %s by PAM account "
98c044d0 240 "configuration", authctxt->user);
ba6dd90e 241 }
242 }
5b9e2464 243#endif
244
ef51930f 245#ifdef _UNICOS
246 if (authenticated && cray_access_denied(authctxt->user)) {
247 authenticated = 0;
248 fatal("Access denied for user %s.",authctxt->user);
249 }
250#endif /* _UNICOS */
251
94ec8c6b 252 /* Log before sending the reply */
59c97189 253 auth_log(authctxt, authenticated, method, " ssh2");
254
4e81159c 255 if (authctxt->postponed)
256 return;
257
258 /* XXX todo: check if multiple auth methods are needed */
259 if (authenticated == 1) {
260 /* turn off userauth */
6367063f 261 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &dispatch_protocol_ignore);
4e81159c 262 packet_start(SSH2_MSG_USERAUTH_SUCCESS);
263 packet_send();
264 packet_write_wait();
265 /* now we can break out */
266 authctxt->success = 1;
267 } else {
c00e4d75 268 if (authctxt->failures++ > options.max_authtries) {
6039eeef 269#ifdef SSH_AUDIT_EVENTS
270 PRIVSEP(audit_event(SSH_LOGIN_EXCEED_MAXTRIES));
c00e4d75 271#endif
4e81159c 272 packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
c00e4d75 273 }
4e81159c 274 methods = authmethods_get();
275 packet_start(SSH2_MSG_USERAUTH_FAILURE);
276 packet_put_cstring(methods);
277 packet_put_char(0); /* XXX partial success, unused */
278 packet_send();
279 packet_write_wait();
280 xfree(methods);
281 }
94ec8c6b 282}
283
94ec8c6b 284#define DELIM ","
285
ffb215be 286static char *
94ec8c6b 287authmethods_get(void)
a306f2dd 288{
3469eac4 289 Buffer b;
94ec8c6b 290 char *list;
01c24737 291 int i;
94ec8c6b 292
3469eac4 293 buffer_init(&b);
01c24737 294 for (i = 0; authmethods[i] != NULL; i++) {
295 if (strcmp(authmethods[i]->name, "none") == 0)
94ec8c6b 296 continue;
01c24737 297 if (authmethods[i]->enabled != NULL &&
298 *(authmethods[i]->enabled) != 0) {
3469eac4 299 if (buffer_len(&b) > 0)
300 buffer_append(&b, ",", 1);
01c24737 301 buffer_append(&b, authmethods[i]->name,
302 strlen(authmethods[i]->name));
a306f2dd 303 }
304 }
3469eac4 305 buffer_append(&b, "\0", 1);
306 list = xstrdup(buffer_ptr(&b));
307 buffer_free(&b);
94ec8c6b 308 return list;
309}
310
396c147e 311static Authmethod *
94ec8c6b 312authmethod_lookup(const char *name)
313{
01c24737 314 int i;
315
94ec8c6b 316 if (name != NULL)
01c24737 317 for (i = 0; authmethods[i] != NULL; i++)
318 if (authmethods[i]->enabled != NULL &&
319 *(authmethods[i]->enabled) != 0 &&
320 strcmp(name, authmethods[i]->name) == 0)
321 return authmethods[i];
322 debug2("Unrecognized authentication method name: %s",
323 name ? name : "NULL");
94ec8c6b 324 return NULL;
a306f2dd 325}
This page took 0.425524 seconds and 5 git commands to generate.