1 /* $OpenBSD: auth2.c,v 1.115 2007/04/14 22:01:58 stevesk Exp $ */
3 * Copyright (c) 2000 Markus Friedl. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
28 #include <sys/types.h>
45 #include "pathnames.h"
51 #include "monitor_wrap.h"
54 extern ServerOptions options;
55 extern u_char *session_id2;
56 extern u_int session_id2_len;
57 extern Buffer loginmsg;
61 extern Authmethod method_none;
62 extern Authmethod method_pubkey;
63 extern Authmethod method_passwd;
64 extern Authmethod method_kbdint;
65 extern Authmethod method_hostbased;
67 extern Authmethod method_external;
68 extern Authmethod method_gsskeyex;
69 extern Authmethod method_gssapi;
70 extern Authmethod method_gssapi_compat;
73 Authmethod *authmethods[] = {
80 &method_gssapi_compat,
90 static void input_service_request(int, u_int32_t, void *);
91 static void input_userauth_request(int, u_int32_t, void *);
94 static Authmethod *authmethod_lookup(const char *);
95 static char *authmethods_get(void);
96 int user_key_allowed(struct passwd *, Key *);
99 * loop until authctxt->success == TRUE
103 do_authentication2(Authctxt *authctxt)
105 dispatch_init(&dispatch_protocol_error);
106 dispatch_set(SSH2_MSG_SERVICE_REQUEST, &input_service_request);
107 dispatch_run(DISPATCH_BLOCK, &authctxt->success, authctxt);
112 input_service_request(int type, u_int32_t seq, void *ctxt)
114 Authctxt *authctxt = ctxt;
117 char *service = packet_get_string(&len);
120 if (authctxt == NULL)
121 fatal("input_service_request: no authctxt");
123 if (strcmp(service, "ssh-userauth") == 0) {
124 if (!authctxt->success) {
126 /* now we can handle user-auth requests */
127 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &input_userauth_request);
130 /* XXX all other service requests are denied */
133 packet_start(SSH2_MSG_SERVICE_ACCEPT);
134 packet_put_cstring(service);
138 debug("bad service request %s", service);
139 packet_disconnect("bad service request %s", service);
146 input_userauth_request(int type, u_int32_t seq, void *ctxt)
148 Authctxt *authctxt = ctxt;
149 Authmethod *m = NULL;
150 char *user, *service, *method, *style = NULL;
151 int authenticated = 0;
153 if (authctxt == NULL)
154 fatal("input_userauth_request: no authctxt");
156 user = packet_get_string(NULL);
157 service = packet_get_string(NULL);
158 method = packet_get_string(NULL);
161 if (user[0] == '\0') {
162 debug("received empty username for %s", method);
163 if (strcmp(method, "external-keyx") == 0 ||
164 strcmp(method, "gssapi-keyex") == 0) {
166 PRIVSEP(ssh_gssapi_localname(&lname));
167 if (lname && lname[0] != '\0') {
170 debug("set username to %s from gssapi context", user);
172 debug("failed to set username from gssapi context");
173 packet_send_debug("failed to set username from gssapi context");
179 debug("userauth-request for user %s service %s method %s",
180 user[0] ? user : "<implicit>", service, method);
181 debug("attempt %d failures %d", authctxt->attempt, authctxt->failures);
183 if ((style = strchr(user, ':')) != NULL)
186 /* If first time or username changed or empty username,
187 setup/reset authentication context. */
188 if ((authctxt->attempt++ == 0) ||
189 (strcmp(user, authctxt->user) != 0) ||
190 (strcmp(user, "") == 0)) {
191 if (authctxt->user) {
192 xfree(authctxt->user);
193 authctxt->user = NULL;
196 authctxt->user = xstrdup(user);
197 if (strcmp(service, "ssh-connection") != 0) {
198 packet_disconnect("Unsupported service %s", service);
201 /* If we're going to set the username based on the
202 GSSAPI context later, then wait until then to
203 verify it. Just put in placeholders for now. */
204 if ((strcmp(user, "") == 0) &&
205 ((strcmp(method, "gssapi") == 0) ||
206 (strcmp(method, "gssapi-with-mic") == 0))) {
207 authctxt->pw = fakepw();
210 authctxt->pw = PRIVSEP(getpwnamallow(user));
213 debug2("input_userauth_request: setting up authctxt for %s", user);
215 logit("input_userauth_request: invalid user %s", user);
216 authctxt->pw = fakepw();
217 #ifdef SSH_AUDIT_EVENTS
218 PRIVSEP(audit_event(SSH_INVALID_USER));
222 } /* endif for setting username based on GSSAPI context */
226 PRIVSEP(start_pam(authctxt));
228 setproctitle("%s%s", authctxt->valid ? user : "unknown",
229 use_privsep ? " [net]" : "");
230 if (authctxt->attempt == 1) {
231 authctxt->service = xstrdup(service);
232 authctxt->style = style ? xstrdup(style) : NULL;
234 mm_inform_authserv(service, style);
237 if (strcmp(service, authctxt->service) != 0) {
238 packet_disconnect("Change of service not allowed: "
239 "(%s,%s) -> (%s,%s)",
240 authctxt->user, authctxt->service, user, service);
243 auth2_challenge_stop(authctxt);
246 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
247 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, NULL);
250 authctxt->postponed = 0;
251 authctxt->server_caused_failure = 0;
253 /* try to authenticate user */
254 m = authmethod_lookup(method);
256 debug2("input_userauth_request: try method %s", method);
257 authenticated = m->userauth(authctxt);
259 userauth_finish(authctxt, authenticated, method);
267 userauth_finish(Authctxt *authctxt, int authenticated, char *method)
271 if (!authctxt->valid && authenticated)
272 fatal("INTERNAL ERROR: authenticated invalid user %s",
275 /* Special handling for root */
276 if (authenticated && authctxt->pw->pw_uid == 0 &&
277 !auth_root_allowed(method)) {
279 #ifdef SSH_AUDIT_EVENTS
280 PRIVSEP(audit_event(SSH_LOGIN_ROOT_DENIED));
285 if (options.use_pam && authenticated) {
286 if (!PRIVSEP(do_pam_account())) {
287 /* if PAM returned a message, send it to the user */
288 if (buffer_len(&loginmsg) > 0) {
289 buffer_append(&loginmsg, "\0", 1);
290 userauth_send_banner(buffer_ptr(&loginmsg));
293 fatal("Access denied for user %s by PAM account "
294 "configuration", authctxt->user);
300 if (authenticated && cray_access_denied(authctxt->user)) {
302 fatal("Access denied for user %s.",authctxt->user);
306 /* Log before sending the reply */
307 auth_log(authctxt, authenticated, method, " ssh2");
309 if (authctxt->postponed)
312 /* XXX todo: check if multiple auth methods are needed */
313 if (authenticated == 1) {
314 /* turn off userauth */
315 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &dispatch_protocol_ignore);
316 packet_start(SSH2_MSG_USERAUTH_SUCCESS);
319 /* now we can break out */
320 authctxt->success = 1;
322 /* Dont count server configuration issues against the client */
323 if (!authctxt->server_caused_failure &&
324 authctxt->failures++ > options.max_authtries) {
325 #ifdef SSH_AUDIT_EVENTS
326 PRIVSEP(audit_event(SSH_LOGIN_EXCEED_MAXTRIES));
328 packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
330 methods = authmethods_get();
331 packet_start(SSH2_MSG_USERAUTH_FAILURE);
332 packet_put_cstring(methods);
333 packet_put_char(0); /* XXX partial success, unused */
341 authmethods_get(void)
348 for (i = 0; authmethods[i] != NULL; i++) {
349 if (strcmp(authmethods[i]->name, "none") == 0)
351 if (authmethods[i]->enabled != NULL &&
352 *(authmethods[i]->enabled) != 0) {
353 if (buffer_len(&b) > 0)
354 buffer_append(&b, ",", 1);
355 buffer_append(&b, authmethods[i]->name,
356 strlen(authmethods[i]->name));
359 buffer_append(&b, "\0", 1);
360 list = xstrdup(buffer_ptr(&b));
366 authmethod_lookup(const char *name)
371 for (i = 0; authmethods[i] != NULL; i++)
372 if (authmethods[i]->enabled != NULL &&
373 *(authmethods[i]->enabled) != 0 &&
374 strcmp(name, authmethods[i]->name) == 0)
375 return authmethods[i];
376 debug2("Unrecognized authentication method name: %s",
377 name ? name : "NULL");