]> andersk Git - gssapi-openssh.git/blame - openssh/auth2.c
OPENSSH_4_5P1_20070218 merged to GPT branch
[gssapi-openssh.git] / openssh / auth2.c
CommitLineData
2e437378 1/* $OpenBSD: auth2.c,v 1.113 2006/08/03 03:34:41 deraadt Exp $ */
3c0ef626 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.
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 */
25
26#include "includes.h"
3c0ef626 27
2e437378 28#include <sys/types.h>
29
30#include <pwd.h>
31#include <stdarg.h>
32#include <string.h>
33
3c0ef626 34#include "xmalloc.h"
2e437378 35#include "ssh2.h"
3c0ef626 36#include "packet.h"
3c0ef626 37#include "log.h"
2e437378 38#include "buffer.h"
3c0ef626 39#include "servconf.h"
416fd2a8 40#include "compat.h"
2e437378 41#include "key.h"
42#include "hostfile.h"
3c0ef626 43#include "auth.h"
3c0ef626 44#include "dispatch.h"
3c0ef626 45#include "pathnames.h"
34fee935 46#include "buffer.h"
3c0ef626 47
5598e598 48#ifdef GSSAPI
49#include "ssh-gss.h"
50#endif
2e437378 51#include "monitor_wrap.h"
5598e598 52
3c0ef626 53/* import */
54extern ServerOptions options;
55extern u_char *session_id2;
70791e56 56extern u_int session_id2_len;
34fee935 57extern Buffer loginmsg;
3c0ef626 58
ff2d7a98 59/* methods */
60
61extern Authmethod method_none;
ff2d7a98 62extern Authmethod method_pubkey;
63extern Authmethod method_passwd;
64extern Authmethod method_kbdint;
65extern Authmethod method_hostbased;
88928908 66#ifdef GSSAPI
67extern Authmethod method_external;
34fee935 68extern Authmethod method_gsskeyex;
88928908 69extern Authmethod method_gssapi;
416fd2a8 70extern Authmethod method_gssapi_compat;
88928908 71#endif
ff2d7a98 72
73Authmethod *authmethods[] = {
74 &method_none,
ff2d7a98 75 &method_pubkey,
70791e56 76#ifdef GSSAPI
34fee935 77 &method_gsskeyex,
416fd2a8 78 &method_external,
70791e56 79 &method_gssapi,
416fd2a8 80 &method_gssapi_compat,
70791e56 81#endif
ff2d7a98 82 &method_passwd,
83 &method_kbdint,
84 &method_hostbased,
85 NULL
3c0ef626 86};
87
88/* protocol */
89
1e608e42 90static void input_service_request(int, u_int32_t, void *);
91static void input_userauth_request(int, u_int32_t, void *);
3c0ef626 92
93/* helper */
94static Authmethod *authmethod_lookup(const char *);
95static char *authmethods_get(void);
2980ea68 96int user_key_allowed(struct passwd *, Key *);
3c0ef626 97
3c0ef626 98/*
99 * loop until authctxt->success == TRUE
100 */
101
416fd2a8 102void
103do_authentication2(Authctxt *authctxt)
3c0ef626 104{
3c0ef626 105 /* challenge-response is implemented via keyboard interactive */
106 if (options.challenge_response_authentication)
107 options.kbd_interactive_authentication = 1;
3c0ef626 108
1e608e42 109 dispatch_init(&dispatch_protocol_error);
3c0ef626 110 dispatch_set(SSH2_MSG_SERVICE_REQUEST, &input_service_request);
111 dispatch_run(DISPATCH_BLOCK, &authctxt->success, authctxt);
3c0ef626 112}
113
2e437378 114/*ARGSUSED*/
3c0ef626 115static void
1e608e42 116input_service_request(int type, u_int32_t seq, void *ctxt)
3c0ef626 117{
118 Authctxt *authctxt = ctxt;
119 u_int len;
e54b3d7c 120 int acceptit = 0;
3c0ef626 121 char *service = packet_get_string(&len);
1e608e42 122 packet_check_eom();
3c0ef626 123
124 if (authctxt == NULL)
125 fatal("input_service_request: no authctxt");
126
127 if (strcmp(service, "ssh-userauth") == 0) {
128 if (!authctxt->success) {
e54b3d7c 129 acceptit = 1;
3c0ef626 130 /* now we can handle user-auth requests */
131 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &input_userauth_request);
132 }
133 }
134 /* XXX all other service requests are denied */
135
e54b3d7c 136 if (acceptit) {
3c0ef626 137 packet_start(SSH2_MSG_SERVICE_ACCEPT);
138 packet_put_cstring(service);
139 packet_send();
140 packet_write_wait();
141 } else {
142 debug("bad service request %s", service);
143 packet_disconnect("bad service request %s", service);
144 }
145 xfree(service);
146}
147
2e437378 148/*ARGSUSED*/
3c0ef626 149static void
1e608e42 150input_userauth_request(int type, u_int32_t seq, void *ctxt)
3c0ef626 151{
152 Authctxt *authctxt = ctxt;
153 Authmethod *m = NULL;
154 char *user, *service, *method, *style = NULL;
155 int authenticated = 0;
156
157 if (authctxt == NULL)
158 fatal("input_userauth_request: no authctxt");
159
160 user = packet_get_string(NULL);
161 service = packet_get_string(NULL);
162 method = packet_get_string(NULL);
2980ea68 163
164#ifdef GSSAPI
1b56ff3d 165 if (user[0] == '\0') {
c2397a66 166 debug("received empty username for %s", method);
34fee935 167 if (strcmp(method, "external-keyx") == 0 ||
168 strcmp(method, "gssapi-keyex") == 0) {
c2397a66 169 char *lname = NULL;
170 PRIVSEP(ssh_gssapi_localname(&lname));
171 if (lname && lname[0] != '\0') {
172 xfree(user);
173 user = lname;
174 debug("set username to %s from gssapi context", user);
57877bbc 175 } else {
c2397a66 176 debug("failed to set username from gssapi context");
e00da40d 177 packet_send_debug("failed to set username from gssapi context");
c2397a66 178 }
ff2d7a98 179 }
180 }
181#endif
2980ea68 182
c2397a66 183 debug("userauth-request for user %s service %s method %s",
1b56ff3d 184 user[0] ? user : "<implicit>", service, method);
3c0ef626 185 debug("attempt %d failures %d", authctxt->attempt, authctxt->failures);
186
187 if ((style = strchr(user, ':')) != NULL)
188 *style++ = 0;
189
d370c64b 190 /* If first time or username changed or empty username,
416fd2a8 191 setup/reset authentication context. */
192 if ((authctxt->attempt++ == 0) ||
193 (strcmp(user, authctxt->user) != 0) ||
194 (strcmp(user, "") == 0)) {
ff2d7a98 195 if (authctxt->user) {
196 xfree(authctxt->user);
197 authctxt->user = NULL;
198 }
34fee935 199 authctxt->valid = 0;
d370c64b 200 authctxt->user = xstrdup(user);
201 if (strcmp(service, "ssh-connection") != 0) {
202 packet_disconnect("Unsupported service %s", service);
203 }
c2397a66 204#ifdef GSSAPI
57877bbc 205 /* If we're going to set the username based on the
206 GSSAPI context later, then wait until then to
1b56ff3d 207 verify it. Just put in placeholders for now. */
208 if ((strcmp(user, "") == 0) &&
209 ((strcmp(method, "gssapi") == 0) ||
210 (strcmp(method, "gssapi-with-mic") == 0))) {
211 authctxt->pw = fakepw();
1b56ff3d 212 } else {
c2397a66 213#endif
2980ea68 214 authctxt->pw = PRIVSEP(getpwnamallow(user));
d370c64b 215 if (authctxt->pw) {
3c0ef626 216 authctxt->valid = 1;
217 debug2("input_userauth_request: setting up authctxt for %s", user);
3c0ef626 218 } else {
1b56ff3d 219 logit("input_userauth_request: invalid user %s", user);
70791e56 220 authctxt->pw = fakepw();
34fee935 221#ifdef SSH_AUDIT_EVENTS
222 PRIVSEP(audit_event(SSH_INVALID_USER));
3c0ef626 223#endif
224 }
c2397a66 225#ifdef GSSAPI
57877bbc 226 } /* endif for setting username based on GSSAPI context */
e00da40d 227#endif
228#ifdef USE_PAM
229 if (options.use_pam)
230 PRIVSEP(start_pam(authctxt));
c2397a66 231#endif
1b56ff3d 232 setproctitle("%s%s", authctxt->valid ? user : "unknown",
2980ea68 233 use_privsep ? " [net]" : "");
34fee935 234 if (authctxt->attempt == 1) {
d370c64b 235 authctxt->service = xstrdup(service);
236 authctxt->style = style ? xstrdup(style) : NULL;
237 if (use_privsep)
238 mm_inform_authserv(service, style);
239 }
1b56ff3d 240 }
241 if (strcmp(service, authctxt->service) != 0) {
88928908 242 packet_disconnect("Change of service not allowed: "
243 "(%s,%s) -> (%s,%s)",
244 authctxt->user, authctxt->service, user, service);
3c0ef626 245 }
246 /* reset state */
1e608e42 247 auth2_challenge_stop(authctxt);
5598e598 248
249#ifdef GSSAPI
1e608e42 250 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
251 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, NULL);
5598e598 252#endif
253
3c0ef626 254 authctxt->postponed = 0;
34fee935 255 authctxt->server_caused_failure = 0;
3c0ef626 256
257 /* try to authenticate user */
258 m = authmethod_lookup(method);
259 if (m != NULL) {
260 debug2("input_userauth_request: try method %s", method);
261 authenticated = m->userauth(authctxt);
262 }
263 userauth_finish(authctxt, authenticated, method);
264
265 xfree(service);
266 xfree(user);
267 xfree(method);
268}
269
270void
271userauth_finish(Authctxt *authctxt, int authenticated, char *method)
272{
273 char *methods;
274
275 if (!authctxt->valid && authenticated)
276 fatal("INTERNAL ERROR: authenticated invalid user %s",
277 authctxt->user);
278
279 /* Special handling for root */
1c14df9e 280 if (authenticated && authctxt->pw->pw_uid == 0 &&
34fee935 281 !auth_root_allowed(method)) {
3c0ef626 282 authenticated = 0;
34fee935 283#ifdef SSH_AUDIT_EVENTS
284 PRIVSEP(audit_event(SSH_LOGIN_ROOT_DENIED));
285#endif
286 }
3c0ef626 287
288#ifdef USE_PAM
34fee935 289 if (options.use_pam && authenticated) {
290 if (!PRIVSEP(do_pam_account())) {
291 /* if PAM returned a message, send it to the user */
292 if (buffer_len(&loginmsg) > 0) {
293 buffer_append(&loginmsg, "\0", 1);
294 userauth_send_banner(buffer_ptr(&loginmsg));
295 packet_write_wait();
296 }
297 fatal("Access denied for user %s by PAM account "
298 "configuration", authctxt->user);
299 }
300 }
70791e56 301#endif
3c0ef626 302
e54b3d7c 303#ifdef _UNICOS
304 if (authenticated && cray_access_denied(authctxt->user)) {
305 authenticated = 0;
306 fatal("Access denied for user %s.",authctxt->user);
307 }
308#endif /* _UNICOS */
309
3c0ef626 310 /* Log before sending the reply */
311 auth_log(authctxt, authenticated, method, " ssh2");
312
313 if (authctxt->postponed)
314 return;
315
316 /* XXX todo: check if multiple auth methods are needed */
317 if (authenticated == 1) {
318 /* turn off userauth */
1e608e42 319 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &dispatch_protocol_ignore);
3c0ef626 320 packet_start(SSH2_MSG_USERAUTH_SUCCESS);
321 packet_send();
322 packet_write_wait();
323 /* now we can break out */
324 authctxt->success = 1;
325 } else {
34fee935 326 /* Dont count server configuration issues against the client */
327 if (!authctxt->server_caused_failure &&
328 authctxt->failures++ > options.max_authtries) {
329#ifdef SSH_AUDIT_EVENTS
330 PRIVSEP(audit_event(SSH_LOGIN_EXCEED_MAXTRIES));
331#endif
3c0ef626 332 packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
34fee935 333 }
3c0ef626 334 methods = authmethods_get();
335 packet_start(SSH2_MSG_USERAUTH_FAILURE);
336 packet_put_cstring(methods);
337 packet_put_char(0); /* XXX partial success, unused */
338 packet_send();
339 packet_write_wait();
340 xfree(methods);
341 }
342}
343
3c0ef626 344#define DELIM ","
345
346static char *
347authmethods_get(void)
348{
1e608e42 349 Buffer b;
3c0ef626 350 char *list;
ff2d7a98 351 int i;
3c0ef626 352
1e608e42 353 buffer_init(&b);
ff2d7a98 354 for (i = 0; authmethods[i] != NULL; i++) {
355 if (strcmp(authmethods[i]->name, "none") == 0)
3c0ef626 356 continue;
ff2d7a98 357 if (authmethods[i]->enabled != NULL &&
358 *(authmethods[i]->enabled) != 0) {
1e608e42 359 if (buffer_len(&b) > 0)
360 buffer_append(&b, ",", 1);
ff2d7a98 361 buffer_append(&b, authmethods[i]->name,
362 strlen(authmethods[i]->name));
3c0ef626 363 }
364 }
1e608e42 365 buffer_append(&b, "\0", 1);
366 list = xstrdup(buffer_ptr(&b));
367 buffer_free(&b);
3c0ef626 368 return list;
369}
370
371static Authmethod *
372authmethod_lookup(const char *name)
373{
ff2d7a98 374 int i;
375
3c0ef626 376 if (name != NULL)
ff2d7a98 377 for (i = 0; authmethods[i] != NULL; i++)
378 if (authmethods[i]->enabled != NULL &&
379 *(authmethods[i]->enabled) != 0 &&
380 strcmp(name, authmethods[i]->name) == 0)
381 return authmethods[i];
382 debug2("Unrecognized authentication method name: %s",
383 name ? name : "NULL");
3c0ef626 384 return NULL;
385}
This page took 0.213571 seconds and 5 git commands to generate.