]> andersk Git - openssh.git/blame - auth2.c
- (tim) [buildpkg.sh.in] Some systems have really limited nawk (OpenServer).
[openssh.git] / auth2.c
CommitLineData
31652869 1/* $OpenBSD: auth2.c,v 1.113 2006/08/03 03:34:41 deraadt 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{
aa8003d6 99 /* challenge-response is implemented via keyboard interactive */
5ba55ada 100 if (options.challenge_response_authentication)
d464095c 101 options.kbd_interactive_authentication = 1;
102
6367063f 103 dispatch_init(&dispatch_protocol_error);
a306f2dd 104 dispatch_set(SSH2_MSG_SERVICE_REQUEST, &input_service_request);
94ec8c6b 105 dispatch_run(DISPATCH_BLOCK, &authctxt->success, authctxt);
a306f2dd 106}
107
fe8c3af1 108/*ARGSUSED*/
396c147e 109static void
7819b5c3 110input_service_request(int type, u_int32_t seq, void *ctxt)
a306f2dd 111{
94ec8c6b 112 Authctxt *authctxt = ctxt;
1e3b8b07 113 u_int len;
7fdc56c5 114 int acceptit = 0;
a306f2dd 115 char *service = packet_get_string(&len);
95500969 116 packet_check_eom();
a306f2dd 117
94ec8c6b 118 if (authctxt == NULL)
119 fatal("input_service_request: no authctxt");
120
a306f2dd 121 if (strcmp(service, "ssh-userauth") == 0) {
94ec8c6b 122 if (!authctxt->success) {
7fdc56c5 123 acceptit = 1;
a306f2dd 124 /* now we can handle user-auth requests */
125 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &input_userauth_request);
126 }
127 }
128 /* XXX all other service requests are denied */
129
7fdc56c5 130 if (acceptit) {
a306f2dd 131 packet_start(SSH2_MSG_SERVICE_ACCEPT);
132 packet_put_cstring(service);
133 packet_send();
134 packet_write_wait();
135 } else {
136 debug("bad service request %s", service);
137 packet_disconnect("bad service request %s", service);
138 }
139 xfree(service);
140}
141
fe8c3af1 142/*ARGSUSED*/
396c147e 143static void
7819b5c3 144input_userauth_request(int type, u_int32_t seq, void *ctxt)
a306f2dd 145{
94ec8c6b 146 Authctxt *authctxt = ctxt;
147 Authmethod *m = NULL;
59c97189 148 char *user, *service, *method, *style = NULL;
a306f2dd 149 int authenticated = 0;
a306f2dd 150
94ec8c6b 151 if (authctxt == NULL)
152 fatal("input_userauth_request: no authctxt");
a306f2dd 153
94ec8c6b 154 user = packet_get_string(NULL);
155 service = packet_get_string(NULL);
156 method = packet_get_string(NULL);
157 debug("userauth-request for user %s service %s method %s", user, service, method);
8abcdba4 158 debug("attempt %d failures %d", authctxt->attempt, authctxt->failures);
94ec8c6b 159
59c97189 160 if ((style = strchr(user, ':')) != NULL)
161 *style++ = 0;
162
2b87da3b 163 if (authctxt->attempt++ == 0) {
63284fbb 164 /* setup auth context */
f7ed12f1 165 authctxt->pw = PRIVSEP(getpwnamallow(user));
529d73ab 166 authctxt->user = xstrdup(user);
f7ed12f1 167 if (authctxt->pw && strcmp(service, "ssh-connection")==0) {
94ec8c6b 168 authctxt->valid = 1;
169 debug2("input_userauth_request: setting up authctxt for %s", user);
94ec8c6b 170 } else {
d77347cc 171 logit("input_userauth_request: invalid user %s", user);
96d0bf74 172 authctxt->pw = fakepw();
6039eeef 173#ifdef SSH_AUDIT_EVENTS
174 PRIVSEP(audit_event(SSH_INVALID_USER));
04fc7a67 175#endif
94ec8c6b 176 }
b6c37221 177#ifdef USE_PAM
178 if (options.use_pam)
179 PRIVSEP(start_pam(authctxt));
180#endif
bd5c0694 181 setproctitle("%s%s", authctxt->valid ? user : "unknown",
1853d1ef 182 use_privsep ? " [net]" : "");
94ec8c6b 183 authctxt->service = xstrdup(service);
2a6a054e 184 authctxt->style = style ? xstrdup(style) : NULL;
1853d1ef 185 if (use_privsep)
186 mm_inform_authserv(service, style);
2a6a054e 187 } else if (strcmp(user, authctxt->user) != 0 ||
188 strcmp(service, authctxt->service) != 0) {
189 packet_disconnect("Change of username or service not allowed: "
190 "(%s,%s) -> (%s,%s)",
191 authctxt->user, authctxt->service, user, service);
a306f2dd 192 }
59c97189 193 /* reset state */
2f293d43 194 auth2_challenge_stop(authctxt);
7364bd04 195
196#ifdef GSSAPI
197 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
198 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, NULL);
199#endif
200
59c97189 201 authctxt->postponed = 0;
9cd45ea4 202
59c97189 203 /* try to authenticate user */
94ec8c6b 204 m = authmethod_lookup(method);
205 if (m != NULL) {
206 debug2("input_userauth_request: try method %s", method);
207 authenticated = m->userauth(authctxt);
9cd45ea4 208 }
d9cd3575 209 userauth_finish(authctxt, authenticated, method);
210
211 xfree(service);
212 xfree(user);
213 xfree(method);
214}
215
216void
217userauth_finish(Authctxt *authctxt, int authenticated, char *method)
218{
4e81159c 219 char *methods;
220
59c97189 221 if (!authctxt->valid && authenticated)
222 fatal("INTERNAL ERROR: authenticated invalid user %s",
223 authctxt->user);
9cd45ea4 224
94ec8c6b 225 /* Special handling for root */
5d47f1d4 226 if (authenticated && authctxt->pw->pw_uid == 0 &&
c00e4d75 227 !auth_root_allowed(method)) {
a306f2dd 228 authenticated = 0;
6039eeef 229#ifdef SSH_AUDIT_EVENTS
230 PRIVSEP(audit_event(SSH_LOGIN_ROOT_DENIED));
c00e4d75 231#endif
232 }
a306f2dd 233
5b9e2464 234#ifdef USE_PAM
ba6dd90e 235 if (options.use_pam && authenticated) {
236 if (!PRIVSEP(do_pam_account())) {
ba6dd90e 237 /* if PAM returned a message, send it to the user */
238 if (buffer_len(&loginmsg) > 0) {
239 buffer_append(&loginmsg, "\0", 1);
240 userauth_send_banner(buffer_ptr(&loginmsg));
9aab0af7 241 packet_write_wait();
ba6dd90e 242 }
9aab0af7 243 fatal("Access denied for user %s by PAM account "
98c044d0 244 "configuration", authctxt->user);
ba6dd90e 245 }
246 }
5b9e2464 247#endif
248
ef51930f 249#ifdef _UNICOS
250 if (authenticated && cray_access_denied(authctxt->user)) {
251 authenticated = 0;
252 fatal("Access denied for user %s.",authctxt->user);
253 }
254#endif /* _UNICOS */
255
94ec8c6b 256 /* Log before sending the reply */
59c97189 257 auth_log(authctxt, authenticated, method, " ssh2");
258
4e81159c 259 if (authctxt->postponed)
260 return;
261
262 /* XXX todo: check if multiple auth methods are needed */
263 if (authenticated == 1) {
264 /* turn off userauth */
6367063f 265 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &dispatch_protocol_ignore);
4e81159c 266 packet_start(SSH2_MSG_USERAUTH_SUCCESS);
267 packet_send();
268 packet_write_wait();
269 /* now we can break out */
270 authctxt->success = 1;
271 } else {
c00e4d75 272 if (authctxt->failures++ > options.max_authtries) {
6039eeef 273#ifdef SSH_AUDIT_EVENTS
274 PRIVSEP(audit_event(SSH_LOGIN_EXCEED_MAXTRIES));
c00e4d75 275#endif
4e81159c 276 packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
c00e4d75 277 }
4e81159c 278 methods = authmethods_get();
279 packet_start(SSH2_MSG_USERAUTH_FAILURE);
280 packet_put_cstring(methods);
281 packet_put_char(0); /* XXX partial success, unused */
282 packet_send();
283 packet_write_wait();
284 xfree(methods);
285 }
94ec8c6b 286}
287
94ec8c6b 288#define DELIM ","
289
ffb215be 290static char *
94ec8c6b 291authmethods_get(void)
a306f2dd 292{
3469eac4 293 Buffer b;
94ec8c6b 294 char *list;
01c24737 295 int i;
94ec8c6b 296
3469eac4 297 buffer_init(&b);
01c24737 298 for (i = 0; authmethods[i] != NULL; i++) {
299 if (strcmp(authmethods[i]->name, "none") == 0)
94ec8c6b 300 continue;
01c24737 301 if (authmethods[i]->enabled != NULL &&
302 *(authmethods[i]->enabled) != 0) {
3469eac4 303 if (buffer_len(&b) > 0)
304 buffer_append(&b, ",", 1);
01c24737 305 buffer_append(&b, authmethods[i]->name,
306 strlen(authmethods[i]->name));
a306f2dd 307 }
308 }
3469eac4 309 buffer_append(&b, "\0", 1);
310 list = xstrdup(buffer_ptr(&b));
311 buffer_free(&b);
94ec8c6b 312 return list;
313}
314
396c147e 315static Authmethod *
94ec8c6b 316authmethod_lookup(const char *name)
317{
01c24737 318 int i;
319
94ec8c6b 320 if (name != NULL)
01c24737 321 for (i = 0; authmethods[i] != NULL; i++)
322 if (authmethods[i]->enabled != NULL &&
323 *(authmethods[i]->enabled) != 0 &&
324 strcmp(name, authmethods[i]->name) == 0)
325 return authmethods[i];
326 debug2("Unrecognized authentication method name: %s",
327 name ? name : "NULL");
94ec8c6b 328 return NULL;
a306f2dd 329}
This page took 0.353623 seconds and 5 git commands to generate.