xref: /dragonfly/crypto/openssh/auth2.c (revision 9f3fc534)
1 /* $OpenBSD: auth2.c,v 1.120 2008/11/04 08:22:12 djm Exp $ */
2 /*
3  * Copyright (c) 2000 Markus Friedl.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
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.
24  */
25 
26 #include "includes.h"
27 
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <sys/uio.h>
31 
32 #include <fcntl.h>
33 #include <pwd.h>
34 #include <stdarg.h>
35 #include <string.h>
36 #include <unistd.h>
37 
38 #include "xmalloc.h"
39 #include "canohost.h"
40 #include "atomicio.h"
41 #include "ssh2.h"
42 #include "packet.h"
43 #include "log.h"
44 #include "buffer.h"
45 #include "servconf.h"
46 #include "compat.h"
47 #include "key.h"
48 #include "hostfile.h"
49 #include "auth.h"
50 #include "dispatch.h"
51 #include "pathnames.h"
52 #include "buffer.h"
53 
54 #ifdef GSSAPI
55 #include "ssh-gss.h"
56 #endif
57 #include "monitor_wrap.h"
58 
59 /* import */
60 extern ServerOptions options;
61 extern u_char *session_id2;
62 extern u_int session_id2_len;
63 extern Buffer loginmsg;
64 
65 /* methods */
66 
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;
72 #ifdef GSSAPI
73 extern Authmethod method_gssapi;
74 #endif
75 #ifdef JPAKE
76 extern Authmethod method_jpake;
77 #endif
78 
79 Authmethod *authmethods[] = {
80 	&method_none,
81 	&method_pubkey,
82 #ifdef GSSAPI
83 	&method_gssapi,
84 #endif
85 #ifdef JPAKE
86 	&method_jpake,
87 #endif
88 	&method_passwd,
89 	&method_kbdint,
90 	&method_hostbased,
91 	NULL
92 };
93 
94 /* protocol */
95 
96 static void input_service_request(int, u_int32_t, void *);
97 static void input_userauth_request(int, u_int32_t, void *);
98 
99 /* helper */
100 static Authmethod *authmethod_lookup(const char *);
101 static char *authmethods_get(void);
102 
103 char *
104 auth2_read_banner(void)
105 {
106 	struct stat st;
107 	char *banner = NULL;
108 	size_t len, n;
109 	int fd;
110 
111 	if ((fd = open(options.banner, O_RDONLY)) == -1)
112 		return (NULL);
113 	if (fstat(fd, &st) == -1) {
114 		close(fd);
115 		return (NULL);
116 	}
117 	if (st.st_size > 1*1024*1024) {
118 		close(fd);
119 		return (NULL);
120 	}
121 
122 	len = (size_t)st.st_size;		/* truncate */
123 	banner = xmalloc(len + 1);
124 	n = atomicio(read, fd, banner, len);
125 	close(fd);
126 
127 	if (n != len) {
128 		xfree(banner);
129 		return (NULL);
130 	}
131 	banner[n] = '\0';
132 
133 	return (banner);
134 }
135 
136 void
137 userauth_send_banner(const char *msg)
138 {
139 	if (datafellows & SSH_BUG_BANNER)
140 		return;
141 
142 	packet_start(SSH2_MSG_USERAUTH_BANNER);
143 	packet_put_cstring(msg);
144 	packet_put_cstring("");		/* language, unused */
145 	packet_send();
146 	debug("%s: sent", __func__);
147 }
148 
149 static void
150 userauth_banner(void)
151 {
152 	char *banner = NULL;
153 
154 	if (options.banner == NULL ||
155 	    strcasecmp(options.banner, "none") == 0 ||
156 	    (datafellows & SSH_BUG_BANNER) != 0)
157 		return;
158 
159 	if ((banner = PRIVSEP(auth2_read_banner())) == NULL)
160 		goto done;
161 	userauth_send_banner(banner);
162 
163 done:
164 	if (banner)
165 		xfree(banner);
166 }
167 
168 /*
169  * loop until authctxt->success == TRUE
170  */
171 void
172 do_authentication2(Authctxt *authctxt)
173 {
174 	dispatch_init(&dispatch_protocol_error);
175 	dispatch_set(SSH2_MSG_SERVICE_REQUEST, &input_service_request);
176 	dispatch_run(DISPATCH_BLOCK, &authctxt->success, authctxt);
177 }
178 
179 /*ARGSUSED*/
180 static void
181 input_service_request(int type, u_int32_t seq, void *ctxt)
182 {
183 	Authctxt *authctxt = ctxt;
184 	u_int len;
185 	int acceptit = 0;
186 	char *service = packet_get_string(&len);
187 	packet_check_eom();
188 
189 	if (authctxt == NULL)
190 		fatal("input_service_request: no authctxt");
191 
192 	if (strcmp(service, "ssh-userauth") == 0) {
193 		if (!authctxt->success) {
194 			acceptit = 1;
195 			/* now we can handle user-auth requests */
196 			dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &input_userauth_request);
197 		}
198 	}
199 	/* XXX all other service requests are denied */
200 
201 	if (acceptit) {
202 		packet_start(SSH2_MSG_SERVICE_ACCEPT);
203 		packet_put_cstring(service);
204 		packet_send();
205 		packet_write_wait();
206 	} else {
207 		debug("bad service request %s", service);
208 		packet_disconnect("bad service request %s", service);
209 	}
210 	xfree(service);
211 }
212 
213 /*ARGSUSED*/
214 static void
215 input_userauth_request(int type, u_int32_t seq, void *ctxt)
216 {
217 	Authctxt *authctxt = ctxt;
218 	Authmethod *m = NULL;
219 	char *user, *service, *method, *style = NULL;
220 	int authenticated = 0;
221 #ifdef HAVE_LOGIN_CAP
222 	login_cap_t *lc;
223 	const char *from_host, *from_ip;
224 
225         from_host = get_canonical_hostname(options.use_dns);
226         from_ip = get_remote_ipaddr();
227 #endif
228 
229 	if (authctxt == NULL)
230 		fatal("input_userauth_request: no authctxt");
231 
232 	user = packet_get_string(NULL);
233 	service = packet_get_string(NULL);
234 	method = packet_get_string(NULL);
235 	debug("userauth-request for user %s service %s method %s", user, service, method);
236 	debug("attempt %d failures %d", authctxt->attempt, authctxt->failures);
237 
238 	if ((style = strchr(user, ':')) != NULL)
239 		*style++ = 0;
240 
241 	if (authctxt->attempt++ == 0) {
242 		/* setup auth context */
243 		authctxt->pw = PRIVSEP(getpwnamallow(user));
244 		authctxt->user = xstrdup(user);
245 		if (authctxt->pw && strcmp(service, "ssh-connection")==0) {
246 			authctxt->valid = 1;
247 			debug2("input_userauth_request: setting up authctxt for %s", user);
248 		} else {
249 			logit("input_userauth_request: invalid user %s", user);
250 			authctxt->pw = fakepw();
251 #ifdef SSH_AUDIT_EVENTS
252 			PRIVSEP(audit_event(SSH_INVALID_USER));
253 #endif
254 		}
255 #ifdef USE_PAM
256 		if (options.use_pam)
257 			PRIVSEP(start_pam(authctxt));
258 #endif
259 		setproctitle("%s%s", authctxt->valid ? user : "unknown",
260 		    use_privsep ? " [net]" : "");
261 		authctxt->service = xstrdup(service);
262 		authctxt->style = style ? xstrdup(style) : NULL;
263 		if (use_privsep)
264 			mm_inform_authserv(service, style);
265 		userauth_banner();
266 	} else if (strcmp(user, authctxt->user) != 0 ||
267 	    strcmp(service, authctxt->service) != 0) {
268 		packet_disconnect("Change of username or service not allowed: "
269 		    "(%s,%s) -> (%s,%s)",
270 		    authctxt->user, authctxt->service, user, service);
271 	}
272 
273 #ifdef HAVE_LOGIN_CAP
274         if (authctxt->pw != NULL) {
275                 lc = login_getpwclass(authctxt->pw);
276                 if (lc == NULL)
277                         lc = login_getclassbyname(NULL, authctxt->pw);
278                 if (!auth_hostok(lc, from_host, from_ip)) {
279                         logit("Denied connection for %.200s from %.200s [%.200s].",
280                             authctxt->pw->pw_name, from_host, from_ip);
281                         packet_disconnect("Sorry, you are not allowed to connect.");
282                 }
283                 if (!auth_timeok(lc, time(NULL))) {
284                         logit("LOGIN %.200s REFUSED (TIME) FROM %.200s",
285                             authctxt->pw->pw_name, from_host);
286                         packet_disconnect("Logins not available right now.");
287                 }
288                 login_close(lc);
289                 lc = NULL;
290         }
291 #endif  /* HAVE_LOGIN_CAP */
292 
293 	/* reset state */
294 	auth2_challenge_stop(authctxt);
295 #ifdef JPAKE
296 	auth2_jpake_stop(authctxt);
297 #endif
298 
299 #ifdef GSSAPI
300 	/* XXX move to auth2_gssapi_stop() */
301 	dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
302 	dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, NULL);
303 #endif
304 
305 	authctxt->postponed = 0;
306 
307 	/* try to authenticate user */
308 	m = authmethod_lookup(method);
309 	if (m != NULL && authctxt->failures < options.max_authtries) {
310 		debug2("input_userauth_request: try method %s", method);
311 		authenticated =	m->userauth(authctxt);
312 	}
313 	userauth_finish(authctxt, authenticated, method);
314 
315 	xfree(service);
316 	xfree(user);
317 	xfree(method);
318 }
319 
320 void
321 userauth_finish(Authctxt *authctxt, int authenticated, char *method)
322 {
323 	char *methods;
324 
325 	if (!authctxt->valid && authenticated)
326 		fatal("INTERNAL ERROR: authenticated invalid user %s",
327 		    authctxt->user);
328 
329 	/* Special handling for root */
330 	if (authenticated && authctxt->pw->pw_uid == 0 &&
331 	    !auth_root_allowed(method)) {
332 		authenticated = 0;
333 #ifdef SSH_AUDIT_EVENTS
334 		PRIVSEP(audit_event(SSH_LOGIN_ROOT_DENIED));
335 #endif
336 	}
337 
338 #ifdef USE_PAM
339 	if (options.use_pam && authenticated) {
340 		if (!PRIVSEP(do_pam_account())) {
341 			/* if PAM returned a message, send it to the user */
342 			if (buffer_len(&loginmsg) > 0) {
343 				buffer_append(&loginmsg, "\0", 1);
344 				userauth_send_banner(buffer_ptr(&loginmsg));
345 				packet_write_wait();
346 			}
347 			fatal("Access denied for user %s by PAM account "
348 			    "configuration", authctxt->user);
349 		}
350 	}
351 #endif
352 
353 #ifdef _UNICOS
354 	if (authenticated && cray_access_denied(authctxt->user)) {
355 		authenticated = 0;
356 		fatal("Access denied for user %s.",authctxt->user);
357 	}
358 #endif /* _UNICOS */
359 
360 	/* Log before sending the reply */
361 	auth_log(authctxt, authenticated, method, " ssh2");
362 
363 	if (authctxt->postponed)
364 		return;
365 
366 	/* XXX todo: check if multiple auth methods are needed */
367 	if (authenticated == 1) {
368 		/* turn off userauth */
369 		dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &dispatch_protocol_ignore);
370 		packet_start(SSH2_MSG_USERAUTH_SUCCESS);
371 		packet_send();
372 		packet_write_wait();
373 		/* now we can break out */
374 		authctxt->success = 1;
375 	} else {
376 
377 		/* Allow initial try of "none" auth without failure penalty */
378 		if (authctxt->attempt > 1 || strcmp(method, "none") != 0)
379 			authctxt->failures++;
380 		if (authctxt->failures >= options.max_authtries) {
381 #ifdef SSH_AUDIT_EVENTS
382 			PRIVSEP(audit_event(SSH_LOGIN_EXCEED_MAXTRIES));
383 #endif
384 			packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
385 		}
386 		methods = authmethods_get();
387 		packet_start(SSH2_MSG_USERAUTH_FAILURE);
388 		packet_put_cstring(methods);
389 		packet_put_char(0);	/* XXX partial success, unused */
390 		packet_send();
391 		packet_write_wait();
392 		xfree(methods);
393 	}
394 }
395 
396 static char *
397 authmethods_get(void)
398 {
399 	Buffer b;
400 	char *list;
401 	int i;
402 
403 	buffer_init(&b);
404 	for (i = 0; authmethods[i] != NULL; i++) {
405 		if (strcmp(authmethods[i]->name, "none") == 0)
406 			continue;
407 		if (authmethods[i]->enabled != NULL &&
408 		    *(authmethods[i]->enabled) != 0) {
409 			if (buffer_len(&b) > 0)
410 				buffer_append(&b, ",", 1);
411 			buffer_append(&b, authmethods[i]->name,
412 			    strlen(authmethods[i]->name));
413 		}
414 	}
415 	buffer_append(&b, "\0", 1);
416 	list = xstrdup(buffer_ptr(&b));
417 	buffer_free(&b);
418 	return list;
419 }
420 
421 static Authmethod *
422 authmethod_lookup(const char *name)
423 {
424 	int i;
425 
426 	if (name != NULL)
427 		for (i = 0; authmethods[i] != NULL; i++)
428 			if (authmethods[i]->enabled != NULL &&
429 			    *(authmethods[i]->enabled) != 0 &&
430 			    strcmp(name, authmethods[i]->name) == 0)
431 				return authmethods[i];
432 	debug2("Unrecognized authentication method name: %s",
433 	    name ? name : "NULL");
434 	return NULL;
435 }
436 
437