xref: /freebsd/crypto/openssh/auth2.c (revision 6780ab54325a71e7e70112b11657973edde8655e)
1 /*
2  * Copyright (c) 2000 Markus Friedl.  All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
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.
12  *
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.
23  */
24 
25 #include "includes.h"
26 RCSID("$OpenBSD: auth2.c,v 1.95 2002/08/22 21:33:58 markus Exp $");
27 RCSID("$FreeBSD$");
28 
29 #include "canohost.h"
30 #include "ssh2.h"
31 #include "xmalloc.h"
32 #include "packet.h"
33 #include "log.h"
34 #include "servconf.h"
35 #include "compat.h"
36 #include "auth.h"
37 #include "dispatch.h"
38 #include "pathnames.h"
39 #include "monitor_wrap.h"
40 
41 /* import */
42 extern ServerOptions options;
43 extern u_char *session_id2;
44 extern int session_id2_len;
45 
46 Authctxt *x_authctxt = NULL;
47 
48 /* methods */
49 
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;
55 
56 Authmethod *authmethods[] = {
57 	&method_none,
58 	&method_pubkey,
59 	&method_passwd,
60 	&method_kbdint,
61 	&method_hostbased,
62 	NULL
63 };
64 
65 /* protocol */
66 
67 static void input_service_request(int, u_int32_t, void *);
68 static void input_userauth_request(int, u_int32_t, void *);
69 
70 /* helper */
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 *);
75 
76 /*
77  * loop until authctxt->success == TRUE
78  */
79 
80 Authctxt *
81 do_authentication2(void)
82 {
83 	Authctxt *authctxt = authctxt_new();
84 
85 	x_authctxt = authctxt;		/*XXX*/
86 
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;
92 	if (use_privsep)
93 		options.pam_authentication_via_kbd_int = 0;
94 
95 	dispatch_init(&dispatch_protocol_error);
96 	dispatch_set(SSH2_MSG_SERVICE_REQUEST, &input_service_request);
97 	dispatch_run(DISPATCH_BLOCK, &authctxt->success, authctxt);
98 
99 	return (authctxt);
100 }
101 
102 static void
103 input_service_request(int type, u_int32_t seq, void *ctxt)
104 {
105 	Authctxt *authctxt = ctxt;
106 	u_int len;
107 	int acceptit = 0;
108 	char *service = packet_get_string(&len);
109 	packet_check_eom();
110 
111 	if (authctxt == NULL)
112 		fatal("input_service_request: no authctxt");
113 
114 	if (strcmp(service, "ssh-userauth") == 0) {
115 		if (!authctxt->success) {
116 			acceptit = 1;
117 			/* now we can handle user-auth requests */
118 			dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &input_userauth_request);
119 		}
120 	}
121 	/* XXX all other service requests are denied */
122 
123 	if (acceptit) {
124 		packet_start(SSH2_MSG_SERVICE_ACCEPT);
125 		packet_put_cstring(service);
126 		packet_send();
127 		packet_write_wait();
128 	} else {
129 		debug("bad service request %s", service);
130 		packet_disconnect("bad service request %s", service);
131 	}
132 	xfree(service);
133 }
134 
135 static void
136 input_userauth_request(int type, u_int32_t seq, void *ctxt)
137 {
138 	Authctxt *authctxt = ctxt;
139 	Authmethod *m = NULL;
140 	char *user, *service, *method, *style = NULL;
141 	int authenticated = 0;
142 #ifdef HAVE_LOGIN_CAP
143 	login_cap_t *lc;
144 	const char *from_host, *from_ip;
145 
146         from_host = get_canonical_hostname(options.verify_reverse_mapping);
147         from_ip = get_remote_ipaddr();
148 #endif
149 
150 	if (authctxt == NULL)
151 		fatal("input_userauth_request: no authctxt");
152 
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);
158 
159 	if ((style = strchr(user, ':')) != NULL)
160 		*style++ = 0;
161 
162 	if (authctxt->attempt++ == 0) {
163 		/* setup auth context */
164 		authctxt->pw = PRIVSEP(getpwnamallow(user));
165 		if (authctxt->pw && strcmp(service, "ssh-connection")==0) {
166 			authctxt->valid = 1;
167 			debug2("input_userauth_request: setting up authctxt for %s", user);
168 #ifdef USE_PAM
169 			PRIVSEP(start_pam(authctxt->pw->pw_name));
170 #endif
171 		} else {
172 			log("input_userauth_request: illegal user %s", user);
173 #ifdef USE_PAM
174 			PRIVSEP(start_pam("NOUSER"));
175 #endif
176 		}
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;
182 		if (use_privsep)
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);
189 	}
190 
191 #ifdef HAVE_LOGIN_CAP
192         if (authctxt->pw != NULL) {
193                 lc = login_getpwclass(authctxt->pw);
194                 if (lc == NULL)
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.");
200                 }
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.");
205                 }
206                 login_close(lc);
207                 lc = NULL;
208         }
209 #endif  /* HAVE_LOGIN_CAP */
210 
211 	/* reset state */
212 	auth2_challenge_stop(authctxt);
213 	authctxt->postponed = 0;
214 
215 	/* try to authenticate user */
216 	m = authmethod_lookup(method);
217 	if (m != NULL) {
218 		debug2("input_userauth_request: try method %s", method);
219 		authenticated =	m->userauth(authctxt);
220 	}
221 	userauth_finish(authctxt, authenticated, method);
222 
223 	xfree(service);
224 	xfree(user);
225 	xfree(method);
226 }
227 
228 void
229 userauth_finish(Authctxt *authctxt, int authenticated, char *method)
230 {
231 	char *methods;
232 
233 	if (!authctxt->valid && authenticated)
234 		fatal("INTERNAL ERROR: authenticated invalid user %s",
235 		    authctxt->user);
236 
237 	/* Special handling for root */
238 	if (!use_privsep &&
239 	    authenticated && authctxt->pw->pw_uid == 0 &&
240 	    !auth_root_allowed(method))
241 		authenticated = 0;
242 
243 #ifdef USE_PAM
244 	if (!use_privsep && authenticated && authctxt->user &&
245 	    !do_pam_account(authctxt->user, NULL))
246 		authenticated = 0;
247 #endif /* USE_PAM */
248 
249 #ifdef _UNICOS
250 	if (authenticated && cray_access_denied(authctxt->user)) {
251 		authenticated = 0;
252 		fatal("Access denied for user %s.",authctxt->user);
253 	}
254 #endif /* _UNICOS */
255 
256 	/* Log before sending the reply */
257 	auth_log(authctxt, authenticated, method, " ssh2");
258 
259 	if (authctxt->postponed)
260 		return;
261 
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);
267 		packet_send();
268 		packet_write_wait();
269 		/* now we can break out */
270 		authctxt->success = 1;
271 	} else {
272 		if (authctxt->failures++ > AUTH_FAIL_MAX) {
273 			packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
274 		}
275 #ifdef _UNICOS
276 		if (strcmp(method, "password") == 0)
277 			cray_login_failure(authctxt->user, IA_UDBERR);
278 #endif /* _UNICOS */
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 */
283 		packet_send();
284 		packet_write_wait();
285 		xfree(methods);
286 	}
287 }
288 
289 /* get current user */
290 
291 struct passwd*
292 auth_get_user(void)
293 {
294 	return (x_authctxt != NULL && x_authctxt->valid) ? x_authctxt->pw : NULL;
295 }
296 
297 #define	DELIM	","
298 
299 static char *
300 authmethods_get(void)
301 {
302 	Buffer b;
303 	char *list;
304 	int i;
305 
306 	buffer_init(&b);
307 	for (i = 0; authmethods[i] != NULL; i++) {
308 		if (strcmp(authmethods[i]->name, "none") == 0)
309 			continue;
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));
316 		}
317 	}
318 	buffer_append(&b, "\0", 1);
319 	list = xstrdup(buffer_ptr(&b));
320 	buffer_free(&b);
321 	return list;
322 }
323 
324 static Authmethod *
325 authmethod_lookup(const char *name)
326 {
327 	int i;
328 
329 	if (name != NULL)
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");
337 	return NULL;
338 }
339