]> andersk Git - gssapi-openssh.git/blame - openssh/auth2.c
o Bump version to 2.7.
[gssapi-openssh.git] / openssh / auth2.c
CommitLineData
3c0ef626 1/*
2 * Copyright (c) 2000 Markus Friedl. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 */
24
25#include "includes.h"
bfe49944 26RCSID("$OpenBSD: auth2.c,v 1.96 2003/02/06 21:22:43 markus Exp $");
3c0ef626 27
28#include "ssh2.h"
a2d82e42 29#include "ssh1.h"
3c0ef626 30#include "xmalloc.h"
3c0ef626 31#include "packet.h"
3c0ef626 32#include "log.h"
33#include "servconf.h"
34#include "compat.h"
3c0ef626 35#include "auth.h"
3c0ef626 36#include "dispatch.h"
3c0ef626 37#include "pathnames.h"
510132b6 38#include "monitor_wrap.h"
3c0ef626 39
5598e598 40#ifdef GSSAPI
41#include "ssh-gss.h"
42#endif
43
3c0ef626 44/* import */
45extern ServerOptions options;
46extern u_char *session_id2;
47extern int session_id2_len;
48
510132b6 49Authctxt *x_authctxt = NULL;
3c0ef626 50
44a053a3 51/* methods */
52
53extern Authmethod method_none;
44a053a3 54extern Authmethod method_pubkey;
55extern Authmethod method_passwd;
56extern Authmethod method_kbdint;
57extern Authmethod method_hostbased;
db870295 58#ifdef GSSAPI
59extern Authmethod method_external;
60extern Authmethod method_gssapi;
61#endif
44a053a3 62
63Authmethod *authmethods[] = {
64 &method_none,
65#ifdef GSSAPI
66 &method_external,
67 &method_gssapi,
68#endif
69 &method_pubkey,
70 &method_passwd,
71 &method_kbdint,
72 &method_hostbased,
73 NULL
3c0ef626 74};
75
76/* protocol */
77
1e608e42 78static void input_service_request(int, u_int32_t, void *);
79static void input_userauth_request(int, u_int32_t, void *);
3c0ef626 80
81/* helper */
82static Authmethod *authmethod_lookup(const char *);
83static char *authmethods_get(void);
510132b6 84int user_key_allowed(struct passwd *, Key *);
85int hostbased_key_allowed(struct passwd *, const char *, char *, Key *);
3c0ef626 86
3c0ef626 87/*
88 * loop until authctxt->success == TRUE
89 */
90
510132b6 91Authctxt *
1e608e42 92do_authentication2(void)
3c0ef626 93{
94 Authctxt *authctxt = authctxt_new();
95
96 x_authctxt = authctxt; /*XXX*/
97
98 /* challenge-response is implemented via keyboard interactive */
99 if (options.challenge_response_authentication)
100 options.kbd_interactive_authentication = 1;
101 if (options.pam_authentication_via_kbd_int)
102 options.kbd_interactive_authentication = 1;
510132b6 103 if (use_privsep)
104 options.pam_authentication_via_kbd_int = 0;
3c0ef626 105
1e608e42 106 dispatch_init(&dispatch_protocol_error);
3c0ef626 107 dispatch_set(SSH2_MSG_SERVICE_REQUEST, &input_service_request);
108 dispatch_run(DISPATCH_BLOCK, &authctxt->success, authctxt);
510132b6 109
110 return (authctxt);
3c0ef626 111}
112
113static void
1e608e42 114input_service_request(int type, u_int32_t seq, void *ctxt)
3c0ef626 115{
116 Authctxt *authctxt = ctxt;
117 u_int len;
d03f4262 118 int acceptit = 0;
3c0ef626 119 char *service = packet_get_string(&len);
1e608e42 120 packet_check_eom();
3c0ef626 121
122 if (authctxt == NULL)
123 fatal("input_service_request: no authctxt");
124
125 if (strcmp(service, "ssh-userauth") == 0) {
126 if (!authctxt->success) {
d03f4262 127 acceptit = 1;
3c0ef626 128 /* now we can handle user-auth requests */
129 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &input_userauth_request);
130 }
131 }
132 /* XXX all other service requests are denied */
133
d03f4262 134 if (acceptit) {
3c0ef626 135 packet_start(SSH2_MSG_SERVICE_ACCEPT);
136 packet_put_cstring(service);
137 packet_send();
138 packet_write_wait();
139 } else {
140 debug("bad service request %s", service);
141 packet_disconnect("bad service request %s", service);
142 }
143 xfree(service);
144}
145
146static void
1e608e42 147input_userauth_request(int type, u_int32_t seq, void *ctxt)
3c0ef626 148{
149 Authctxt *authctxt = ctxt;
150 Authmethod *m = NULL;
151 char *user, *service, *method, *style = NULL;
152 int authenticated = 0;
153
154 if (authctxt == NULL)
155 fatal("input_userauth_request: no authctxt");
156
157 user = packet_get_string(NULL);
158 service = packet_get_string(NULL);
159 method = packet_get_string(NULL);
73a68d83 160
305d07c0 161#ifdef GSSAPI
b59afbfe 162 if (strcmp(user, "") == 0) {
b8e8844a 163 debug("received empty username for %s", method);
164 if (strcmp(method, "external-keyx") == 0) {
165 char *lname = NULL;
166 PRIVSEP(ssh_gssapi_localname(&lname));
167 if (lname && lname[0] != '\0') {
168 xfree(user);
169 user = lname;
170 debug("set username to %s from gssapi context", user);
171 } else if (authctxt->valid) {
172 debug("failed to set username from gssapi context");
173 }
b59afbfe 174 }
175 }
176#endif
73a68d83 177
b8e8844a 178 debug("userauth-request for user %s service %s method %s",
179 (user && user[0]) ? user : "<implicit>", service, method);
3c0ef626 180 debug("attempt %d failures %d", authctxt->attempt, authctxt->failures);
181
182 if ((style = strchr(user, ':')) != NULL)
183 *style++ = 0;
184
b8e8844a 185 authctxt->attempt++;
b59afbfe 186 if (!authctxt->user ||
187 strcmp(user, authctxt->user) != 0) {
3c0ef626 188 /* setup auth context */
b59afbfe 189 if (authctxt->user) {
190 xfree(authctxt->user);
191 authctxt->user = NULL;
192 }
193 if (authctxt->service) {
194 xfree(authctxt->service);
195 authctxt->service = NULL;
196 }
197 if (authctxt->style) {
198 xfree(authctxt->style);
199 authctxt->style = NULL;
200 }
b8e8844a 201#ifdef GSSAPI
202 /* We'll verify the username after we set it from the
203 GSSAPI context. */
204 if ((strcmp(user, "") == 0) &&
205 ((strcmp(method, "gssapi") == 0) ||
206 (strcmp(method, "external-keyx") == 0))) {
207 authctxt->pw = NULL;
208 authctxt->valid = 1;
209 } else {
210#endif
510132b6 211 authctxt->pw = PRIVSEP(getpwnamallow(user));
212 if (authctxt->pw && strcmp(service, "ssh-connection")==0) {
3c0ef626 213 authctxt->valid = 1;
214 debug2("input_userauth_request: setting up authctxt for %s", user);
215#ifdef USE_PAM
510132b6 216 PRIVSEP(start_pam(authctxt->pw->pw_name));
3c0ef626 217#endif
218 } else {
e964b42b 219 authctxt->valid = 0;
3c0ef626 220 log("input_userauth_request: illegal user %s", user);
221#ifdef USE_PAM
510132b6 222 PRIVSEP(start_pam("NOUSER"));
3c0ef626 223#endif
224 }
b8e8844a 225#ifdef GSSAPI
226 }
227#endif
510132b6 228 setproctitle("%s%s", authctxt->pw ? user : "unknown",
229 use_privsep ? " [net]" : "");
3c0ef626 230 authctxt->user = xstrdup(user);
231 authctxt->service = xstrdup(service);
232 authctxt->style = style ? xstrdup(style) : NULL;
b8e8844a 233 if (use_privsep && (authctxt->attempt == 1))
510132b6 234 mm_inform_authserv(service, style);
67b84e65 235 } else if (strcmp(service, authctxt->service) != 0) {
236 packet_disconnect("Change of service not allowed: "
237 "(%s,%s) -> (%s,%s)",
238 authctxt->user, authctxt->service, user, service);
3c0ef626 239 }
240 /* reset state */
1e608e42 241 auth2_challenge_stop(authctxt);
5598e598 242
243#ifdef GSSAPI
1e608e42 244 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
245 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, NULL);
5598e598 246#endif
247
3c0ef626 248 authctxt->postponed = 0;
3c0ef626 249
250 /* try to authenticate user */
251 m = authmethod_lookup(method);
252 if (m != NULL) {
253 debug2("input_userauth_request: try method %s", method);
254 authenticated = m->userauth(authctxt);
255 }
256 userauth_finish(authctxt, authenticated, method);
257
258 xfree(service);
259 xfree(user);
260 xfree(method);
261}
262
263void
264userauth_finish(Authctxt *authctxt, int authenticated, char *method)
265{
266 char *methods;
267
268 if (!authctxt->valid && authenticated)
269 fatal("INTERNAL ERROR: authenticated invalid user %s",
270 authctxt->user);
271
272 /* Special handling for root */
bfe49944 273 if (authenticated && authctxt->pw->pw_uid == 0 &&
3c0ef626 274 !auth_root_allowed(method))
275 authenticated = 0;
276
277#ifdef USE_PAM
510132b6 278 if (!use_privsep && authenticated && authctxt->user &&
279 !do_pam_account(authctxt->user, NULL))
3c0ef626 280 authenticated = 0;
281#endif /* USE_PAM */
282
d03f4262 283#ifdef _UNICOS
284 if (authenticated && cray_access_denied(authctxt->user)) {
285 authenticated = 0;
286 fatal("Access denied for user %s.",authctxt->user);
287 }
288#endif /* _UNICOS */
289
3c0ef626 290 /* Log before sending the reply */
a2d82e42 291 if (!compat20)
292 auth_log(authctxt, authenticated, method, " ssh1");
293 else
3c0ef626 294 auth_log(authctxt, authenticated, method, " ssh2");
295
296 if (authctxt->postponed)
297 return;
298
299 /* XXX todo: check if multiple auth methods are needed */
300 if (authenticated == 1) {
301 /* turn off userauth */
1e608e42 302 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &dispatch_protocol_ignore);
b59afbfe 303 if (compat20) {
3c0ef626 304 packet_start(SSH2_MSG_USERAUTH_SUCCESS);
305 packet_send();
306 packet_write_wait();
b59afbfe 307 }
3c0ef626 308 /* now we can break out */
309 authctxt->success = 1;
310 } else {
311 if (authctxt->failures++ > AUTH_FAIL_MAX) {
3c0ef626 312 packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
313 }
411da244 314 if (!compat20) {
315 /*
316 * Break out of the dispatch loop now and go back to
317 * SSH1 code. We need to set the 'success' flag to
318 * break out of the loop. Set the 'postponed' flag to
319 * tell the SSH1 code that authentication failed. The
320 * SSH1 code will handle sending SSH_SMSG_FAILURE.
321 */
322 authctxt->success = authctxt->postponed = 1;
323 } else {
d03f4262 324#ifdef _UNICOS
325 if (strcmp(method, "password") == 0)
326 cray_login_failure(authctxt->user, IA_UDBERR);
327#endif /* _UNICOS */
3c0ef626 328 methods = authmethods_get();
329 packet_start(SSH2_MSG_USERAUTH_FAILURE);
330 packet_put_cstring(methods);
331 packet_put_char(0); /* XXX partial success, unused */
332 packet_send();
333 packet_write_wait();
334 xfree(methods);
411da244 335 }
3c0ef626 336 }
337}
338
3c0ef626 339/* get current user */
340
341struct passwd*
342auth_get_user(void)
343{
344 return (x_authctxt != NULL && x_authctxt->valid) ? x_authctxt->pw : NULL;
345}
346
347#define DELIM ","
348
349static char *
350authmethods_get(void)
351{
1e608e42 352 Buffer b;
3c0ef626 353 char *list;
44a053a3 354 int i;
3c0ef626 355
1e608e42 356 buffer_init(&b);
44a053a3 357 for (i = 0; authmethods[i] != NULL; i++) {
358 if (strcmp(authmethods[i]->name, "none") == 0)
3c0ef626 359 continue;
44a053a3 360 if (authmethods[i]->enabled != NULL &&
361 *(authmethods[i]->enabled) != 0) {
1e608e42 362 if (buffer_len(&b) > 0)
363 buffer_append(&b, ",", 1);
44a053a3 364 buffer_append(&b, authmethods[i]->name,
365 strlen(authmethods[i]->name));
3c0ef626 366 }
367 }
1e608e42 368 buffer_append(&b, "\0", 1);
369 list = xstrdup(buffer_ptr(&b));
370 buffer_free(&b);
3c0ef626 371 return list;
372}
373
374static Authmethod *
375authmethod_lookup(const char *name)
376{
44a053a3 377 int i;
378
3c0ef626 379 if (name != NULL)
44a053a3 380 for (i = 0; authmethods[i] != NULL; i++)
381 if (authmethods[i]->enabled != NULL &&
382 *(authmethods[i]->enabled) != 0 &&
383 strcmp(name, authmethods[i]->name) == 0)
384 return authmethods[i];
385 debug2("Unrecognized authentication method name: %s",
386 name ? name : "NULL");
3c0ef626 387 return NULL;
388}
This page took 0.308365 seconds and 5 git commands to generate.