1 /* $OpenBSD: auth2.c,v 1.116 2007/09/29 00:25:51 dtucker 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"
52 #include "monitor_wrap.h"
55 extern ServerOptions options;
56 extern u_char *session_id2;
57 extern u_int session_id2_len;
58 extern Buffer loginmsg;
62 extern Authmethod method_none;
63 extern Authmethod method_pubkey;
64 extern Authmethod method_passwd;
65 extern Authmethod method_kbdint;
66 extern Authmethod method_hostbased;
68 extern Authmethod method_external;
69 extern Authmethod method_gsskeyex;
70 extern Authmethod method_gssapi;
71 extern Authmethod method_gssapi_compat;
74 static int log_flag = 0;
77 Authmethod *authmethods[] = {
84 &method_gssapi_compat,
94 static void input_service_request(int, u_int32_t, void *);
95 static void input_userauth_request(int, u_int32_t, void *);
98 static Authmethod *authmethod_lookup(const char *);
99 static char *authmethods_get(void);
102 * loop until authctxt->success == TRUE
106 do_authentication2(Authctxt *authctxt)
108 dispatch_init(&dispatch_protocol_error);
109 dispatch_set(SSH2_MSG_SERVICE_REQUEST, &input_service_request);
110 dispatch_run(DISPATCH_BLOCK, &authctxt->success, authctxt);
115 input_service_request(int type, u_int32_t seq, void *ctxt)
117 Authctxt *authctxt = ctxt;
120 char *service = packet_get_string(&len);
123 if (authctxt == NULL)
124 fatal("input_service_request: no authctxt");
126 if (strcmp(service, "ssh-userauth") == 0) {
127 if (!authctxt->success) {
129 /* now we can handle user-auth requests */
130 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &input_userauth_request);
133 /* XXX all other service requests are denied */
136 packet_start(SSH2_MSG_SERVICE_ACCEPT);
137 packet_put_cstring(service);
141 debug("bad service request %s", service);
142 packet_disconnect("bad service request %s", service);
149 input_userauth_request(int type, u_int32_t seq, void *ctxt)
151 Authctxt *authctxt = ctxt;
152 Authmethod *m = NULL;
153 char *user, *service, *method, *style = NULL;
154 int authenticated = 0;
156 if (authctxt == NULL)
157 fatal("input_userauth_request: no authctxt");
159 user = packet_get_string(NULL);
160 service = packet_get_string(NULL);
161 method = packet_get_string(NULL);
164 if (user[0] == '\0') {
165 debug("received empty username for %s", method);
166 if (strcmp(method, "external-keyx") == 0 ||
167 strcmp(method, "gssapi-keyex") == 0) {
169 PRIVSEP(ssh_gssapi_localname(&lname));
170 if (lname && lname[0] != '\0') {
173 debug("set username to %s from gssapi context", user);
175 debug("failed to set username from gssapi context");
176 packet_send_debug("failed to set username from gssapi context");
182 debug("userauth-request for user %s service %s method %s",
183 user[0] ? user : "<implicit>", service, method);
185 logit("SSH: Server;Ltype: Authname;Remote: %s-%d;Name: %s",
186 get_remote_ipaddr(), get_remote_port(), user);
189 debug("attempt %d failures %d", authctxt->attempt, authctxt->failures);
191 if ((style = strchr(user, ':')) != NULL)
194 /* If first time or username changed or empty username,
195 setup/reset authentication context. */
196 if ((authctxt->attempt++ == 0) ||
197 (strcmp(user, authctxt->user) != 0) ||
198 (strcmp(user, "") == 0)) {
199 if (authctxt->user) {
200 xfree(authctxt->user);
201 authctxt->user = NULL;
204 authctxt->user = xstrdup(user);
205 if (strcmp(service, "ssh-connection") != 0) {
206 packet_disconnect("Unsupported service %s", service);
209 /* If we're going to set the username based on the
210 GSSAPI context later, then wait until then to
211 verify it. Just put in placeholders for now. */
212 if ((strcmp(user, "") == 0) &&
213 ((strcmp(method, "gssapi") == 0) ||
214 (strcmp(method, "gssapi-with-mic") == 0))) {
215 authctxt->pw = fakepw();
218 authctxt->pw = PRIVSEP(getpwnamallow(user));
221 debug2("input_userauth_request: setting up authctxt for %s", user);
223 logit("input_userauth_request: invalid user %s", user);
224 authctxt->pw = fakepw();
225 #ifdef SSH_AUDIT_EVENTS
226 PRIVSEP(audit_event(SSH_INVALID_USER));
230 } /* endif for setting username based on GSSAPI context */
234 PRIVSEP(start_pam(authctxt));
236 setproctitle("%s%s", authctxt->valid ? user : "unknown",
237 use_privsep ? " [net]" : "");
238 if (authctxt->attempt == 1) {
239 authctxt->service = xstrdup(service);
240 authctxt->style = style ? xstrdup(style) : NULL;
242 mm_inform_authserv(service, style);
245 if (strcmp(service, authctxt->service) != 0) {
246 packet_disconnect("Change of service not allowed: "
247 "(%s,%s) -> (%s,%s)",
248 authctxt->user, authctxt->service, user, service);
251 auth2_challenge_stop(authctxt);
254 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
255 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, NULL);
258 authctxt->postponed = 0;
259 authctxt->server_caused_failure = 0;
261 /* try to authenticate user */
262 m = authmethod_lookup(method);
264 debug2("input_userauth_request: try method %s", method);
265 authenticated = m->userauth(authctxt);
267 userauth_finish(authctxt, authenticated, method);
275 userauth_finish(Authctxt *authctxt, int authenticated, char *method)
279 if (!authctxt->valid && authenticated)
280 fatal("INTERNAL ERROR: authenticated invalid user %s",
283 /* Special handling for root */
284 if (authenticated && authctxt->pw->pw_uid == 0 &&
285 !auth_root_allowed(method)) {
287 #ifdef SSH_AUDIT_EVENTS
288 PRIVSEP(audit_event(SSH_LOGIN_ROOT_DENIED));
293 if (options.use_pam && authenticated) {
294 if (!PRIVSEP(do_pam_account())) {
295 /* if PAM returned a message, send it to the user */
296 if (buffer_len(&loginmsg) > 0) {
297 buffer_append(&loginmsg, "\0", 1);
298 userauth_send_banner(buffer_ptr(&loginmsg));
301 fatal("Access denied for user %s by PAM account "
302 "configuration", authctxt->user);
308 if (authenticated && cray_access_denied(authctxt->user)) {
310 fatal("Access denied for user %s.",authctxt->user);
314 /* Log before sending the reply */
315 auth_log(authctxt, authenticated, method, " ssh2");
317 if (authctxt->postponed)
320 /* XXX todo: check if multiple auth methods are needed */
321 if (authenticated == 1) {
322 /* turn off userauth */
323 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &dispatch_protocol_ignore);
324 packet_start(SSH2_MSG_USERAUTH_SUCCESS);
327 /* now we can break out */
328 authctxt->success = 1;
330 /* Dont count server configuration issues against the client */
331 if (!authctxt->server_caused_failure &&
332 authctxt->failures++ > options.max_authtries) {
333 #ifdef SSH_AUDIT_EVENTS
334 PRIVSEP(audit_event(SSH_LOGIN_EXCEED_MAXTRIES));
336 packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
338 methods = authmethods_get();
339 packet_start(SSH2_MSG_USERAUTH_FAILURE);
340 packet_put_cstring(methods);
341 packet_put_char(0); /* XXX partial success, unused */
349 authmethods_get(void)
356 for (i = 0; authmethods[i] != NULL; i++) {
357 if (strcmp(authmethods[i]->name, "none") == 0)
359 if (authmethods[i]->enabled != NULL &&
360 *(authmethods[i]->enabled) != 0) {
361 if (buffer_len(&b) > 0)
362 buffer_append(&b, ",", 1);
363 buffer_append(&b, authmethods[i]->name,
364 strlen(authmethods[i]->name));
367 buffer_append(&b, "\0", 1);
368 list = xstrdup(buffer_ptr(&b));
374 authmethod_lookup(const char *name)
379 for (i = 0; authmethods[i] != NULL; i++)
380 if (authmethods[i]->enabled != NULL &&
381 *(authmethods[i]->enabled) != 0 &&
382 strcmp(name, authmethods[i]->name) == 0)
383 return authmethods[i];
384 debug2("Unrecognized authentication method name: %s",
385 name ? name : "NULL");