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.102 2003/08/26 09:58:43 markus Exp $");
38 #include "pathnames.h"
39 #include "monitor_wrap.h"
46 extern ServerOptions options;
47 extern u_char *session_id2;
48 extern u_int session_id2_len;
50 Authctxt *x_authctxt = NULL;
54 extern Authmethod method_none;
55 extern Authmethod method_pubkey;
56 extern Authmethod method_passwd;
57 extern Authmethod method_kbdint;
58 extern Authmethod method_hostbased;
60 extern Authmethod method_gssapi;
63 Authmethod *authmethods[] = {
77 static void input_service_request(int, u_int32_t, void *);
78 static void input_userauth_request(int, u_int32_t, void *);
81 static Authmethod *authmethod_lookup(const char *);
82 static char *authmethods_get(void);
83 int user_key_allowed(struct passwd *, Key *);
84 int hostbased_key_allowed(struct passwd *, const char *, char *, Key *);
87 * loop until authctxt->success == TRUE
91 do_authentication2(void)
93 Authctxt *authctxt = authctxt_new();
95 x_authctxt = authctxt; /*XXX*/
97 /* challenge-response is implemented via keyboard interactive */
98 if (options.challenge_response_authentication)
99 options.kbd_interactive_authentication = 1;
101 dispatch_init(&dispatch_protocol_error);
102 dispatch_set(SSH2_MSG_SERVICE_REQUEST, &input_service_request);
103 dispatch_run(DISPATCH_BLOCK, &authctxt->success, authctxt);
109 input_service_request(int type, u_int32_t seq, void *ctxt)
111 Authctxt *authctxt = ctxt;
114 char *service = packet_get_string(&len);
117 if (authctxt == NULL)
118 fatal("input_service_request: no authctxt");
120 if (strcmp(service, "ssh-userauth") == 0) {
121 if (!authctxt->success) {
123 /* now we can handle user-auth requests */
124 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &input_userauth_request);
127 /* XXX all other service requests are denied */
130 packet_start(SSH2_MSG_SERVICE_ACCEPT);
131 packet_put_cstring(service);
135 debug("bad service request %s", service);
136 packet_disconnect("bad service request %s", service);
142 input_userauth_request(int type, u_int32_t seq, void *ctxt)
144 Authctxt *authctxt = ctxt;
145 Authmethod *m = NULL;
146 char *user, *service, *method, *style = NULL;
147 int authenticated = 0;
148 #ifdef HAVE_LOGIN_CAP
150 const char *from_host, *from_ip;
152 from_host = get_canonical_hostname(options.use_dns);
153 from_ip = get_remote_ipaddr();
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);
162 debug("userauth-request for user %s service %s method %s", user, service, method);
163 debug("attempt %d failures %d", authctxt->attempt, authctxt->failures);
165 if ((style = strchr(user, ':')) != NULL)
168 if (authctxt->attempt++ == 0) {
169 /* setup auth context */
170 authctxt->pw = PRIVSEP(getpwnamallow(user));
171 if (authctxt->pw && strcmp(service, "ssh-connection")==0) {
173 debug2("input_userauth_request: setting up authctxt for %s", user);
176 PRIVSEP(start_pam(authctxt->pw->pw_name));
179 logit("input_userauth_request: illegal user %s", user);
180 authctxt->pw = fakepw();
183 PRIVSEP(start_pam(user));
186 setproctitle("%s%s", authctxt->pw ? user : "unknown",
187 use_privsep ? " [net]" : "");
188 authctxt->user = xstrdup(user);
189 authctxt->service = xstrdup(service);
190 authctxt->style = style ? xstrdup(style) : NULL;
192 mm_inform_authserv(service, style);
193 } else if (strcmp(user, authctxt->user) != 0 ||
194 strcmp(service, authctxt->service) != 0) {
195 packet_disconnect("Change of username or service not allowed: "
196 "(%s,%s) -> (%s,%s)",
197 authctxt->user, authctxt->service, user, service);
200 #ifdef HAVE_LOGIN_CAP
201 if (authctxt->pw != NULL) {
202 lc = login_getpwclass(authctxt->pw);
204 lc = login_getclassbyname(NULL, authctxt->pw);
205 if (!auth_hostok(lc, from_host, from_ip)) {
206 logit("Denied connection for %.200s from %.200s [%.200s].",
207 authctxt->pw->pw_name, from_host, from_ip);
208 packet_disconnect("Sorry, you are not allowed to connect.");
210 if (!auth_timeok(lc, time(NULL))) {
211 logit("LOGIN %.200s REFUSED (TIME) FROM %.200s",
212 authctxt->pw->pw_name, from_host);
213 packet_disconnect("Logins not available right now.");
218 #endif /* HAVE_LOGIN_CAP */
221 auth2_challenge_stop(authctxt);
224 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
225 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, NULL);
228 authctxt->postponed = 0;
230 /* try to authenticate user */
231 m = authmethod_lookup(method);
233 debug2("input_userauth_request: try method %s", method);
234 authenticated = m->userauth(authctxt);
236 userauth_finish(authctxt, authenticated, method);
244 userauth_finish(Authctxt *authctxt, int authenticated, char *method)
248 if (!authctxt->valid && authenticated)
249 fatal("INTERNAL ERROR: authenticated invalid user %s",
252 /* Special handling for root */
253 if (authenticated && authctxt->pw->pw_uid == 0 &&
254 !auth_root_allowed(method))
258 if (options.use_pam && authenticated && !PRIVSEP(do_pam_account()))
263 if (authenticated && cray_access_denied(authctxt->user)) {
265 fatal("Access denied for user %s.",authctxt->user);
269 /* Log before sending the reply */
270 auth_log(authctxt, authenticated, method, " ssh2");
272 if (authctxt->postponed)
275 /* XXX todo: check if multiple auth methods are needed */
276 if (authenticated == 1) {
277 /* turn off userauth */
278 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &dispatch_protocol_ignore);
279 packet_start(SSH2_MSG_USERAUTH_SUCCESS);
282 /* now we can break out */
283 authctxt->success = 1;
285 if (authctxt->failures++ > AUTH_FAIL_MAX)
286 packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
287 methods = authmethods_get();
288 packet_start(SSH2_MSG_USERAUTH_FAILURE);
289 packet_put_cstring(methods);
290 packet_put_char(0); /* XXX partial success, unused */
297 /* get current user */
302 return (x_authctxt != NULL && x_authctxt->valid) ? x_authctxt->pw : NULL;
308 authmethods_get(void)
315 for (i = 0; authmethods[i] != NULL; i++) {
316 if (strcmp(authmethods[i]->name, "none") == 0)
318 if (authmethods[i]->enabled != NULL &&
319 *(authmethods[i]->enabled) != 0) {
320 if (buffer_len(&b) > 0)
321 buffer_append(&b, ",", 1);
322 buffer_append(&b, authmethods[i]->name,
323 strlen(authmethods[i]->name));
326 buffer_append(&b, "\0", 1);
327 list = xstrdup(buffer_ptr(&b));
333 authmethod_lookup(const char *name)
338 for (i = 0; authmethods[i] != NULL; i++)
339 if (authmethods[i]->enabled != NULL &&
340 *(authmethods[i]->enabled) != 0 &&
341 strcmp(name, authmethods[i]->name) == 0)
342 return authmethods[i];
343 debug2("Unrecognized authentication method name: %s",
344 name ? name : "NULL");