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.95 2002/08/22 21:33:58 markus Exp $");
27 RCSID("$FreeBSD: src/crypto/openssh/auth2.c,v 1.2.2.9 2003/02/03 17:31:06 des Exp $");
38 #include "pathnames.h"
39 #include "monitor_wrap.h"
42 extern ServerOptions options;
43 extern u_char *session_id2;
44 extern int session_id2_len;
46 Authctxt *x_authctxt = NULL;
50 extern Authmethod method_none;
51 extern Authmethod method_pubkey;
52 extern Authmethod method_passwd;
53 extern Authmethod method_kbdint;
54 extern Authmethod method_hostbased;
56 Authmethod *authmethods[] = {
67 static void input_service_request(int, u_int32_t, void *);
68 static void input_userauth_request(int, u_int32_t, void *);
71 static Authmethod *authmethod_lookup(const char *);
72 static char *authmethods_get(void);
73 int user_key_allowed(struct passwd *, Key *);
74 int hostbased_key_allowed(struct passwd *, const char *, char *, Key *);
77 * loop until authctxt->success == TRUE
81 do_authentication2(void)
83 Authctxt *authctxt = authctxt_new();
85 x_authctxt = authctxt; /*XXX*/
87 /* challenge-response is implemented via keyboard interactive */
88 if (options.challenge_response_authentication)
89 options.kbd_interactive_authentication = 1;
90 if (options.pam_authentication_via_kbd_int)
91 options.kbd_interactive_authentication = 1;
93 options.pam_authentication_via_kbd_int = 0;
95 dispatch_init(&dispatch_protocol_error);
96 dispatch_set(SSH2_MSG_SERVICE_REQUEST, &input_service_request);
97 dispatch_run(DISPATCH_BLOCK, &authctxt->success, authctxt);
103 input_service_request(int type, u_int32_t seq, void *ctxt)
105 Authctxt *authctxt = ctxt;
108 char *service = packet_get_string(&len);
111 if (authctxt == NULL)
112 fatal("input_service_request: no authctxt");
114 if (strcmp(service, "ssh-userauth") == 0) {
115 if (!authctxt->success) {
117 /* now we can handle user-auth requests */
118 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &input_userauth_request);
121 /* XXX all other service requests are denied */
124 packet_start(SSH2_MSG_SERVICE_ACCEPT);
125 packet_put_cstring(service);
129 debug("bad service request %s", service);
130 packet_disconnect("bad service request %s", service);
136 input_userauth_request(int type, u_int32_t seq, void *ctxt)
138 Authctxt *authctxt = ctxt;
139 Authmethod *m = NULL;
140 char *user, *service, *method, *style = NULL;
141 int authenticated = 0;
142 #ifdef HAVE_LOGIN_CAP
144 const char *from_host, *from_ip;
146 from_host = get_canonical_hostname(options.verify_reverse_mapping);
147 from_ip = get_remote_ipaddr();
150 if (authctxt == NULL)
151 fatal("input_userauth_request: no authctxt");
153 user = packet_get_string(NULL);
154 service = packet_get_string(NULL);
155 method = packet_get_string(NULL);
156 debug("userauth-request for user %s service %s method %s", user, service, method);
157 debug("attempt %d failures %d", authctxt->attempt, authctxt->failures);
159 if ((style = strchr(user, ':')) != NULL)
162 if (authctxt->attempt++ == 0) {
163 /* setup auth context */
164 authctxt->pw = PRIVSEP(getpwnamallow(user));
165 if (authctxt->pw && strcmp(service, "ssh-connection")==0) {
167 debug2("input_userauth_request: setting up authctxt for %s", user);
169 PRIVSEP(start_pam(authctxt->pw->pw_name));
172 log("input_userauth_request: illegal user %s", user);
174 PRIVSEP(start_pam("NOUSER"));
177 setproctitle("%s%s", authctxt->pw ? user : "unknown",
178 use_privsep ? " [net]" : "");
179 authctxt->user = xstrdup(user);
180 authctxt->service = xstrdup(service);
181 authctxt->style = style ? xstrdup(style) : NULL;
183 mm_inform_authserv(service, style);
184 } else if (strcmp(user, authctxt->user) != 0 ||
185 strcmp(service, authctxt->service) != 0) {
186 packet_disconnect("Change of username or service not allowed: "
187 "(%s,%s) -> (%s,%s)",
188 authctxt->user, authctxt->service, user, service);
191 #ifdef HAVE_LOGIN_CAP
192 if (authctxt->pw != NULL) {
193 lc = login_getpwclass(authctxt->pw);
195 lc = login_getclassbyname(NULL, authctxt->pw);
196 if (!auth_hostok(lc, from_host, from_ip)) {
197 log("Denied connection for %.200s from %.200s [%.200s].",
198 authctxt->pw->pw_name, from_host, from_ip);
199 packet_disconnect("Sorry, you are not allowed to connect.");
201 if (!auth_timeok(lc, time(NULL))) {
202 log("LOGIN %.200s REFUSED (TIME) FROM %.200s",
203 authctxt->pw->pw_name, from_host);
204 packet_disconnect("Logins not available right now.");
209 #endif /* HAVE_LOGIN_CAP */
212 auth2_challenge_stop(authctxt);
213 authctxt->postponed = 0;
215 /* try to authenticate user */
216 m = authmethod_lookup(method);
218 debug2("input_userauth_request: try method %s", method);
219 authenticated = m->userauth(authctxt);
221 userauth_finish(authctxt, authenticated, method);
229 userauth_finish(Authctxt *authctxt, int authenticated, char *method)
233 if (!authctxt->valid && authenticated)
234 fatal("INTERNAL ERROR: authenticated invalid user %s",
237 /* Special handling for root */
239 authenticated && authctxt->pw->pw_uid == 0 &&
240 !auth_root_allowed(method))
244 if (!use_privsep && authenticated && authctxt->user &&
245 !do_pam_account(authctxt->user, NULL))
250 if (authenticated && cray_access_denied(authctxt->user)) {
252 fatal("Access denied for user %s.",authctxt->user);
256 /* Log before sending the reply */
257 auth_log(authctxt, authenticated, method, " ssh2");
259 if (authctxt->postponed)
262 /* XXX todo: check if multiple auth methods are needed */
263 if (authenticated == 1) {
264 /* turn off userauth */
265 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &dispatch_protocol_ignore);
266 packet_start(SSH2_MSG_USERAUTH_SUCCESS);
269 /* now we can break out */
270 authctxt->success = 1;
272 if (authctxt->failures++ > AUTH_FAIL_MAX) {
273 packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
276 if (strcmp(method, "password") == 0)
277 cray_login_failure(authctxt->user, IA_UDBERR);
279 methods = authmethods_get();
280 packet_start(SSH2_MSG_USERAUTH_FAILURE);
281 packet_put_cstring(methods);
282 packet_put_char(0); /* XXX partial success, unused */
289 /* get current user */
294 return (x_authctxt != NULL && x_authctxt->valid) ? x_authctxt->pw : NULL;
300 authmethods_get(void)
307 for (i = 0; authmethods[i] != NULL; i++) {
308 if (strcmp(authmethods[i]->name, "none") == 0)
310 if (authmethods[i]->enabled != NULL &&
311 *(authmethods[i]->enabled) != 0) {
312 if (buffer_len(&b) > 0)
313 buffer_append(&b, ",", 1);
314 buffer_append(&b, authmethods[i]->name,
315 strlen(authmethods[i]->name));
318 buffer_append(&b, "\0", 1);
319 list = xstrdup(buffer_ptr(&b));
325 authmethod_lookup(const char *name)
330 for (i = 0; authmethods[i] != NULL; i++)
331 if (authmethods[i]->enabled != NULL &&
332 *(authmethods[i]->enabled) != 0 &&
333 strcmp(name, authmethods[i]->name) == 0)
334 return authmethods[i];
335 debug2("Unrecognized authentication method name: %s",
336 name ? name : "NULL");