1 /* $OpenBSD: auth2.c,v 1.119 2008/07/04 23:30:16 djm 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>
50 #include "pathnames.h"
57 #include "monitor_wrap.h"
60 extern ServerOptions options;
61 extern u_char *session_id2;
62 extern u_int session_id2_len;
63 extern Buffer loginmsg;
67 extern Authmethod method_none;
68 extern Authmethod method_pubkey;
69 extern Authmethod method_passwd;
70 extern Authmethod method_kbdint;
71 extern Authmethod method_hostbased;
73 extern Authmethod method_gssapi;
76 static int log_flag = 0;
79 Authmethod *authmethods[] = {
93 static void input_service_request(int, u_int32_t, void *);
94 static void input_userauth_request(int, u_int32_t, void *);
97 static Authmethod *authmethod_lookup(const char *);
98 static char *authmethods_get(void);
101 auth2_read_banner(void)
108 if ((fd = open(options.banner, O_RDONLY)) == -1)
110 if (fstat(fd, &st) == -1) {
114 if (st.st_size > 1*1024*1024) {
119 len = (size_t)st.st_size; /* truncate */
120 banner = xmalloc(len + 1);
121 n = atomicio(read, fd, banner, len);
134 userauth_send_banner(const char *msg)
136 if (datafellows & SSH_BUG_BANNER)
139 packet_start(SSH2_MSG_USERAUTH_BANNER);
140 packet_put_cstring(msg);
141 packet_put_cstring(""); /* language, unused */
143 debug("%s: sent", __func__);
147 userauth_banner(void)
151 if (options.banner == NULL ||
152 strcasecmp(options.banner, "none") == 0 ||
153 (datafellows & SSH_BUG_BANNER) != 0)
156 if ((banner = PRIVSEP(auth2_read_banner())) == NULL)
158 userauth_send_banner(banner);
166 * loop until authctxt->success == TRUE
169 do_authentication2(Authctxt *authctxt)
171 dispatch_init(&dispatch_protocol_error);
172 dispatch_set(SSH2_MSG_SERVICE_REQUEST, &input_service_request);
173 dispatch_run(DISPATCH_BLOCK, &authctxt->success, authctxt);
178 input_service_request(int type, u_int32_t seq, void *ctxt)
180 Authctxt *authctxt = ctxt;
183 char *service = packet_get_string(&len);
186 if (authctxt == NULL)
187 fatal("input_service_request: no authctxt");
189 if (strcmp(service, "ssh-userauth") == 0) {
190 if (!authctxt->success) {
192 /* now we can handle user-auth requests */
193 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &input_userauth_request);
196 /* XXX all other service requests are denied */
199 packet_start(SSH2_MSG_SERVICE_ACCEPT);
200 packet_put_cstring(service);
204 debug("bad service request %s", service);
205 packet_disconnect("bad service request %s", service);
212 input_userauth_request(int type, u_int32_t seq, void *ctxt)
214 Authctxt *authctxt = ctxt;
215 Authmethod *m = NULL;
216 char *user, *service, *method, *style = NULL;
217 int authenticated = 0;
219 if (authctxt == NULL)
220 fatal("input_userauth_request: no authctxt");
222 user = packet_get_string(NULL);
223 service = packet_get_string(NULL);
224 method = packet_get_string(NULL);
225 debug("userauth-request for user %s service %s method %s", user, service, method);
227 logit("SSH: Server;Ltype: Authname;Remote: %s-%d;Name: %s",
228 get_remote_ipaddr(), get_remote_port(), user);
231 debug("attempt %d failures %d", authctxt->attempt, authctxt->failures);
233 if ((style = strchr(user, ':')) != NULL)
236 if (authctxt->attempt++ == 0) {
237 /* setup auth context */
238 authctxt->pw = PRIVSEP(getpwnamallow(user));
239 authctxt->user = xstrdup(user);
240 if (authctxt->pw && strcmp(service, "ssh-connection")==0) {
242 debug2("input_userauth_request: setting up authctxt for %s", user);
244 logit("input_userauth_request: invalid user %s", user);
245 authctxt->pw = fakepw();
246 #ifdef SSH_AUDIT_EVENTS
247 PRIVSEP(audit_event(SSH_INVALID_USER));
252 PRIVSEP(start_pam(authctxt));
254 setproctitle("%s%s", authctxt->valid ? user : "unknown",
255 use_privsep ? " [net]" : "");
256 authctxt->service = xstrdup(service);
257 authctxt->style = style ? xstrdup(style) : NULL;
259 mm_inform_authserv(service, style);
261 } else if (strcmp(user, authctxt->user) != 0 ||
262 strcmp(service, authctxt->service) != 0) {
263 packet_disconnect("Change of username or service not allowed: "
264 "(%s,%s) -> (%s,%s)",
265 authctxt->user, authctxt->service, user, service);
268 auth2_challenge_stop(authctxt);
271 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
272 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, NULL);
275 authctxt->postponed = 0;
277 /* try to authenticate user */
278 m = authmethod_lookup(method);
279 if (m != NULL && authctxt->failures < options.max_authtries) {
280 debug2("input_userauth_request: try method %s", method);
281 authenticated = m->userauth(authctxt);
283 userauth_finish(authctxt, authenticated, method);
291 userauth_finish(Authctxt *authctxt, int authenticated, char *method)
295 if (!authctxt->valid && authenticated)
296 fatal("INTERNAL ERROR: authenticated invalid user %s",
299 /* Special handling for root */
300 if (authenticated && authctxt->pw->pw_uid == 0 &&
301 !auth_root_allowed(method)) {
303 #ifdef SSH_AUDIT_EVENTS
304 PRIVSEP(audit_event(SSH_LOGIN_ROOT_DENIED));
309 if (options.use_pam && authenticated) {
310 if (!PRIVSEP(do_pam_account())) {
311 /* if PAM returned a message, send it to the user */
312 if (buffer_len(&loginmsg) > 0) {
313 buffer_append(&loginmsg, "\0", 1);
314 userauth_send_banner(buffer_ptr(&loginmsg));
317 fatal("Access denied for user %s by PAM account "
318 "configuration", authctxt->user);
324 if (authenticated && cray_access_denied(authctxt->user)) {
326 fatal("Access denied for user %s.",authctxt->user);
330 /* Log before sending the reply */
331 auth_log(authctxt, authenticated, method, " ssh2");
333 if (authctxt->postponed)
336 /* XXX todo: check if multiple auth methods are needed */
337 if (authenticated == 1) {
338 /* turn off userauth */
339 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &dispatch_protocol_ignore);
340 packet_start(SSH2_MSG_USERAUTH_SUCCESS);
343 /* now we can break out */
344 authctxt->success = 1;
347 /* Allow initial try of "none" auth without failure penalty */
348 if (authctxt->attempt > 1 || strcmp(method, "none") != 0)
349 authctxt->failures++;
350 if (authctxt->failures >= options.max_authtries) {
351 #ifdef SSH_AUDIT_EVENTS
352 PRIVSEP(audit_event(SSH_LOGIN_EXCEED_MAXTRIES));
354 packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
356 methods = authmethods_get();
357 packet_start(SSH2_MSG_USERAUTH_FAILURE);
358 packet_put_cstring(methods);
359 packet_put_char(0); /* XXX partial success, unused */
367 authmethods_get(void)
374 for (i = 0; authmethods[i] != NULL; i++) {
375 if (strcmp(authmethods[i]->name, "none") == 0)
377 if (authmethods[i]->enabled != NULL &&
378 *(authmethods[i]->enabled) != 0) {
379 if (buffer_len(&b) > 0)
380 buffer_append(&b, ",", 1);
381 buffer_append(&b, authmethods[i]->name,
382 strlen(authmethods[i]->name));
385 buffer_append(&b, "\0", 1);
386 list = xstrdup(buffer_ptr(&b));
392 authmethod_lookup(const char *name)
397 for (i = 0; authmethods[i] != NULL; i++)
398 if (authmethods[i]->enabled != NULL &&
399 *(authmethods[i]->enabled) != 0 &&
400 strcmp(name, authmethods[i]->name) == 0)
401 return authmethods[i];
402 debug2("Unrecognized authentication method name: %s",
403 name ? name : "NULL");