2 * Copyright (c) 2000 Markus Friedl. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
26 RCSID("$OpenBSD: auth2.c,v 1.107 2004/07/28 09:40:29 markus Exp $");
36 #include "pathnames.h"
37 #include "monitor_wrap.h"
45 extern ServerOptions options;
46 extern u_char *session_id2;
47 extern u_int session_id2_len;
48 extern Buffer loginmsg;
52 extern Authmethod method_none;
53 extern Authmethod method_pubkey;
54 extern Authmethod method_passwd;
55 extern Authmethod method_kbdint;
56 extern Authmethod method_hostbased;
58 extern Authmethod method_external;
59 extern Authmethod method_gsskeyex;
60 extern Authmethod method_gssapi;
61 extern Authmethod method_gssapi_compat;
64 Authmethod *authmethods[] = {
71 &method_gssapi_compat,
81 static void input_service_request(int, u_int32_t, void *);
82 static void input_userauth_request(int, u_int32_t, void *);
85 static Authmethod *authmethod_lookup(const char *);
86 static char *authmethods_get(void);
87 int user_key_allowed(struct passwd *, Key *);
90 * loop until authctxt->success == TRUE
94 do_authentication2(Authctxt *authctxt)
96 /* challenge-response is implemented via keyboard interactive */
97 if (options.challenge_response_authentication)
98 options.kbd_interactive_authentication = 1;
100 dispatch_init(&dispatch_protocol_error);
101 dispatch_set(SSH2_MSG_SERVICE_REQUEST, &input_service_request);
102 dispatch_run(DISPATCH_BLOCK, &authctxt->success, authctxt);
106 input_service_request(int type, u_int32_t seq, void *ctxt)
108 Authctxt *authctxt = ctxt;
111 char *service = packet_get_string(&len);
114 if (authctxt == NULL)
115 fatal("input_service_request: no authctxt");
117 if (strcmp(service, "ssh-userauth") == 0) {
118 if (!authctxt->success) {
120 /* now we can handle user-auth requests */
121 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &input_userauth_request);
124 /* XXX all other service requests are denied */
127 packet_start(SSH2_MSG_SERVICE_ACCEPT);
128 packet_put_cstring(service);
132 debug("bad service request %s", service);
133 packet_disconnect("bad service request %s", service);
139 input_userauth_request(int type, u_int32_t seq, void *ctxt)
141 Authctxt *authctxt = ctxt;
142 Authmethod *m = NULL;
143 char *user, *service, *method, *style = NULL;
144 int authenticated = 0;
146 if (authctxt == NULL)
147 fatal("input_userauth_request: no authctxt");
149 user = packet_get_string(NULL);
150 service = packet_get_string(NULL);
151 method = packet_get_string(NULL);
154 if (user[0] == '\0') {
155 debug("received empty username for %s", method);
156 if (strcmp(method, "external-keyx") == 0 ||
157 strcmp(method, "gssapi-keyex") == 0) {
159 PRIVSEP(ssh_gssapi_localname(&lname));
160 if (lname && lname[0] != '\0') {
163 debug("set username to %s from gssapi context", user);
165 debug("failed to set username from gssapi context");
166 packet_send_debug("failed to set username from gssapi context");
172 debug("userauth-request for user %s service %s method %s",
173 user[0] ? user : "<implicit>", service, method);
174 debug("attempt %d failures %d", authctxt->attempt, authctxt->failures);
176 if ((style = strchr(user, ':')) != NULL)
179 /* If first time or username changed or implicit username,
180 setup/reset authentication context. */
181 if ((authctxt->attempt++ == 0) ||
182 (strcmp(user, authctxt->user) != 0) ||
183 (strcmp(user, "") == 0)) {
184 if (authctxt->user) {
185 xfree(authctxt->user);
186 authctxt->user = NULL;
190 /* If we're going to set the username based on the
191 GSSAPI context later, then wait until then to
192 verify it. Just put in placeholders for now. */
193 if ((strcmp(user, "") == 0) &&
194 ((strcmp(method, "gssapi") == 0) ||
195 (strcmp(method, "gssapi-with-mic") == 0))) {
196 authctxt->pw = fakepw();
197 authctxt->user = xstrdup(user);
200 authctxt->pw = PRIVSEP(getpwnamallow(user));
201 authctxt->user = xstrdup(user);
202 if (authctxt->pw && strcmp(service, "ssh-connection")==0) {
204 debug2("input_userauth_request: setting up authctxt for %s", user);
206 logit("input_userauth_request: invalid user %s", user);
207 authctxt->pw = fakepw();
208 #ifdef SSH_AUDIT_EVENTS
209 PRIVSEP(audit_event(SSH_INVALID_USER));
213 } /* endif for setting username based on GSSAPI context */
217 PRIVSEP(start_pam(authctxt));
219 setproctitle("%s%s", authctxt->valid ? user : "unknown",
220 use_privsep ? " [net]" : "");
222 if (authctxt->attempt == 1) {
224 authctxt->service = xstrdup(service);
225 authctxt->style = style ? xstrdup(style) : NULL;
227 mm_inform_authserv(service, style);
229 } /* if (authctxt->attempt == 1) */
232 if (strcmp(service, authctxt->service) != 0) {
233 packet_disconnect("Change of service not allowed: "
234 "(%s,%s) -> (%s,%s)",
235 authctxt->user, authctxt->service, user, service);
238 auth2_challenge_stop(authctxt);
241 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
242 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, NULL);
245 authctxt->postponed = 0;
246 authctxt->server_caused_failure = 0;
248 /* try to authenticate user */
249 m = authmethod_lookup(method);
251 debug2("input_userauth_request: try method %s", method);
252 authenticated = m->userauth(authctxt);
254 userauth_finish(authctxt, authenticated, method);
262 userauth_finish(Authctxt *authctxt, int authenticated, char *method)
266 if (!authctxt->valid && authenticated)
267 fatal("INTERNAL ERROR: authenticated invalid user %s",
270 /* Special handling for root */
271 if (authenticated && authctxt->pw->pw_uid == 0 &&
272 !auth_root_allowed(method)) {
274 #ifdef SSH_AUDIT_EVENTS
275 PRIVSEP(audit_event(SSH_LOGIN_ROOT_DENIED));
280 if (options.use_pam && authenticated) {
281 if (!PRIVSEP(do_pam_account())) {
282 /* if PAM returned a message, send it to the user */
283 if (buffer_len(&loginmsg) > 0) {
284 buffer_append(&loginmsg, "\0", 1);
285 userauth_send_banner(buffer_ptr(&loginmsg));
288 fatal("Access denied for user %s by PAM account "
289 "configuration", authctxt->user);
295 if (authenticated && cray_access_denied(authctxt->user)) {
297 fatal("Access denied for user %s.",authctxt->user);
301 /* Log before sending the reply */
302 auth_log(authctxt, authenticated, method, " ssh2");
304 if (authctxt->postponed)
307 /* XXX todo: check if multiple auth methods are needed */
308 if (authenticated == 1) {
309 /* turn off userauth */
310 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &dispatch_protocol_ignore);
311 packet_start(SSH2_MSG_USERAUTH_SUCCESS);
314 /* now we can break out */
315 authctxt->success = 1;
317 /* Dont count server configuration issues against the client */
318 if (!authctxt->server_caused_failure &&
319 authctxt->failures++ > options.max_authtries) {
320 #ifdef SSH_AUDIT_EVENTS
321 PRIVSEP(audit_event(SSH_LOGIN_EXCEED_MAXTRIES));
323 packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
325 methods = authmethods_get();
326 packet_start(SSH2_MSG_USERAUTH_FAILURE);
327 packet_put_cstring(methods);
328 packet_put_char(0); /* XXX partial success, unused */
338 authmethods_get(void)
345 for (i = 0; authmethods[i] != NULL; i++) {
346 if (strcmp(authmethods[i]->name, "none") == 0)
348 if (authmethods[i]->enabled != NULL &&
349 *(authmethods[i]->enabled) != 0) {
350 if (buffer_len(&b) > 0)
351 buffer_append(&b, ",", 1);
352 buffer_append(&b, authmethods[i]->name,
353 strlen(authmethods[i]->name));
356 buffer_append(&b, "\0", 1);
357 list = xstrdup(buffer_ptr(&b));
363 authmethod_lookup(const char *name)
368 for (i = 0; authmethods[i] != NULL; i++)
369 if (authmethods[i]->enabled != NULL &&
370 *(authmethods[i]->enabled) != 0 &&
371 strcmp(name, authmethods[i]->name) == 0)
372 return authmethods[i];
373 debug2("Unrecognized authentication method name: %s",
374 name ? name : "NULL");