]> andersk Git - openssh.git/blame - auth2.c
- stevesk@cvs.openbsd.org 2007/04/14 22:01:58
[openssh.git] / auth2.c
CommitLineData
2e5c9c30 1/* $OpenBSD: auth2.c,v 1.115 2007/04/14 22:01:58 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>
31652869 31#include <stdarg.h>
00146caa 32#include <string.h>
b1842393 33
a306f2dd 34#include "xmalloc.h"
31652869 35#include "ssh2.h"
a306f2dd 36#include "packet.h"
42f11eb2 37#include "log.h"
31652869 38#include "buffer.h"
a306f2dd 39#include "servconf.h"
40#include "compat.h"
31652869 41#include "key.h"
42#include "hostfile.h"
a306f2dd 43#include "auth.h"
a306f2dd 44#include "dispatch.h"
42f11eb2 45#include "pathnames.h"
ba6dd90e 46#include "buffer.h"
a306f2dd 47
7364bd04 48#ifdef GSSAPI
49#include "ssh-gss.h"
50#endif
31652869 51#include "monitor_wrap.h"
7364bd04 52
a306f2dd 53/* import */
54extern ServerOptions options;
1e3b8b07 55extern u_char *session_id2;
a27002e5 56extern u_int session_id2_len;
ba6dd90e 57extern Buffer loginmsg;
a306f2dd 58
01c24737 59/* methods */
60
61extern Authmethod method_none;
62extern Authmethod method_pubkey;
63extern Authmethod method_passwd;
64extern Authmethod method_kbdint;
65extern Authmethod method_hostbased;
7364bd04 66#ifdef GSSAPI
67extern Authmethod method_gssapi;
68#endif
01c24737 69
70Authmethod *authmethods[] = {
71 &method_none,
72 &method_pubkey,
7364bd04 73#ifdef GSSAPI
74 &method_gssapi,
75#endif
01c24737 76 &method_passwd,
77 &method_kbdint,
78 &method_hostbased,
79 NULL
94ec8c6b 80};
81
a306f2dd 82/* protocol */
83
7819b5c3 84static void input_service_request(int, u_int32_t, void *);
85static void input_userauth_request(int, u_int32_t, void *);
a306f2dd 86
a306f2dd 87/* helper */
396c147e 88static Authmethod *authmethod_lookup(const char *);
ffb215be 89static char *authmethods_get(void);
1853d1ef 90int user_key_allowed(struct passwd *, Key *);
a306f2dd 91
a306f2dd 92/*
94ec8c6b 93 * loop until authctxt->success == TRUE
a306f2dd 94 */
95
2362db19 96void
97do_authentication2(Authctxt *authctxt)
a306f2dd 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
ffb215be 284static char *
94ec8c6b 285authmethods_get(void)
a306f2dd 286{
3469eac4 287 Buffer b;
94ec8c6b 288 char *list;
01c24737 289 int i;
94ec8c6b 290
3469eac4 291 buffer_init(&b);
01c24737 292 for (i = 0; authmethods[i] != NULL; i++) {
293 if (strcmp(authmethods[i]->name, "none") == 0)
94ec8c6b 294 continue;
01c24737 295 if (authmethods[i]->enabled != NULL &&
296 *(authmethods[i]->enabled) != 0) {
3469eac4 297 if (buffer_len(&b) > 0)
298 buffer_append(&b, ",", 1);
01c24737 299 buffer_append(&b, authmethods[i]->name,
300 strlen(authmethods[i]->name));
a306f2dd 301 }
302 }
3469eac4 303 buffer_append(&b, "\0", 1);
304 list = xstrdup(buffer_ptr(&b));
305 buffer_free(&b);
94ec8c6b 306 return list;
307}
308
396c147e 309static Authmethod *
94ec8c6b 310authmethod_lookup(const char *name)
311{
01c24737 312 int i;
313
94ec8c6b 314 if (name != NULL)
01c24737 315 for (i = 0; authmethods[i] != NULL; i++)
316 if (authmethods[i]->enabled != NULL &&
317 *(authmethods[i]->enabled) != 0 &&
318 strcmp(name, authmethods[i]->name) == 0)
319 return authmethods[i];
320 debug2("Unrecognized authentication method name: %s",
321 name ? name : "NULL");
94ec8c6b 322 return NULL;
a306f2dd 323}
This page took 0.238758 seconds and 5 git commands to generate.