utilities - Temporary map out libevtr, ktrdump, and evtranalyze from x86_64
[dragonfly.git] / crypto / openssh / auth2.c
blobf76f8831484dc07a84f09d054a8544fdfb93b57e
1 /* $OpenBSD: auth2.c,v 1.121 2009/06/22 05:39:28 dtucker Exp $ */
2 /*
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
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.
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.
26 #include "includes.h"
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <sys/uio.h>
32 #include <fcntl.h>
33 #include <pwd.h>
34 #include <stdarg.h>
35 #include <string.h>
36 #include <unistd.h>
38 #include "atomicio.h"
39 #include "xmalloc.h"
40 #include "ssh2.h"
41 #include "packet.h"
42 #include "log.h"
43 #include "buffer.h"
44 #include "servconf.h"
45 #include "compat.h"
46 #include "key.h"
47 #include "hostfile.h"
48 #include "auth.h"
49 #include "dispatch.h"
50 #include "pathnames.h"
51 #include "buffer.h"
52 #include "canohost.h"
54 #ifdef GSSAPI
55 #include "ssh-gss.h"
56 #endif
57 #include "monitor_wrap.h"
59 /* import */
60 extern ServerOptions options;
61 extern u_char *session_id2;
62 extern u_int session_id2_len;
63 extern Buffer loginmsg;
65 /* methods */
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
79 static int log_flag = 0;
82 Authmethod *authmethods[] = {
83 &method_none,
84 &method_pubkey,
85 #ifdef GSSAPI
86 &method_gssapi,
87 #endif
88 #ifdef JPAKE
89 &method_jpake,
90 #endif
91 &method_passwd,
92 &method_kbdint,
93 &method_hostbased,
94 NULL
97 /* protocol */
99 static void input_service_request(int, u_int32_t, void *);
100 static void input_userauth_request(int, u_int32_t, void *);
102 /* helper */
103 static Authmethod *authmethod_lookup(const char *);
104 static char *authmethods_get(void);
106 char *
107 auth2_read_banner(void)
109 struct stat st;
110 char *banner = NULL;
111 size_t len, n;
112 int fd;
114 if ((fd = open(options.banner, O_RDONLY)) == -1)
115 return (NULL);
116 if (fstat(fd, &st) == -1) {
117 close(fd);
118 return (NULL);
120 if (st.st_size > 1*1024*1024) {
121 close(fd);
122 return (NULL);
125 len = (size_t)st.st_size; /* truncate */
126 banner = xmalloc(len + 1);
127 n = atomicio(read, fd, banner, len);
128 close(fd);
130 if (n != len) {
131 xfree(banner);
132 return (NULL);
134 banner[n] = '\0';
136 return (banner);
139 void
140 userauth_send_banner(const char *msg)
142 if (datafellows & SSH_BUG_BANNER)
143 return;
145 packet_start(SSH2_MSG_USERAUTH_BANNER);
146 packet_put_cstring(msg);
147 packet_put_cstring(""); /* language, unused */
148 packet_send();
149 debug("%s: sent", __func__);
152 static void
153 userauth_banner(void)
155 char *banner = NULL;
157 if (options.banner == NULL ||
158 strcasecmp(options.banner, "none") == 0 ||
159 (datafellows & SSH_BUG_BANNER) != 0)
160 return;
162 if ((banner = PRIVSEP(auth2_read_banner())) == NULL)
163 goto done;
164 userauth_send_banner(banner);
166 done:
167 if (banner)
168 xfree(banner);
172 * loop until authctxt->success == TRUE
174 void
175 do_authentication2(Authctxt *authctxt)
177 dispatch_init(&dispatch_protocol_error);
178 dispatch_set(SSH2_MSG_SERVICE_REQUEST, &input_service_request);
179 dispatch_run(DISPATCH_BLOCK, &authctxt->success, authctxt);
182 /*ARGSUSED*/
183 static void
184 input_service_request(int type, u_int32_t seq, void *ctxt)
186 Authctxt *authctxt = ctxt;
187 u_int len;
188 int acceptit = 0;
189 char *service = packet_get_string(&len);
190 packet_check_eom();
192 if (authctxt == NULL)
193 fatal("input_service_request: no authctxt");
195 if (strcmp(service, "ssh-userauth") == 0) {
196 if (!authctxt->success) {
197 acceptit = 1;
198 /* now we can handle user-auth requests */
199 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &input_userauth_request);
202 /* XXX all other service requests are denied */
204 if (acceptit) {
205 packet_start(SSH2_MSG_SERVICE_ACCEPT);
206 packet_put_cstring(service);
207 packet_send();
208 packet_write_wait();
209 } else {
210 debug("bad service request %s", service);
211 packet_disconnect("bad service request %s", service);
213 xfree(service);
216 /*ARGSUSED*/
217 static void
218 input_userauth_request(int type, u_int32_t seq, void *ctxt)
220 Authctxt *authctxt = ctxt;
221 Authmethod *m = NULL;
222 char *user, *service, *method, *style = NULL;
223 int authenticated = 0;
224 #ifdef HAVE_LOGIN_CAP
225 login_cap_t *lc;
226 const char *from_host, *from_ip;
228 from_host = get_canonical_hostname(options.use_dns);
229 from_ip = get_remote_ipaddr();
230 #endif
232 if (authctxt == NULL)
233 fatal("input_userauth_request: no authctxt");
235 user = packet_get_string(NULL);
236 service = packet_get_string(NULL);
237 method = packet_get_string(NULL);
238 debug("userauth-request for user %s service %s method %s", user, service, method);
239 if (!log_flag) {
240 logit("SSH: Server;Ltype: Authname;Remote: %s-%d;Name: %s",
241 get_remote_ipaddr(), get_remote_port(), user);
242 log_flag = 1;
244 debug("attempt %d failures %d", authctxt->attempt, authctxt->failures);
246 if ((style = strchr(user, ':')) != NULL)
247 *style++ = 0;
249 if (authctxt->attempt++ == 0) {
250 /* setup auth context */
251 authctxt->pw = PRIVSEP(getpwnamallow(user));
252 authctxt->user = xstrdup(user);
253 if (authctxt->pw && strcmp(service, "ssh-connection")==0) {
254 authctxt->valid = 1;
255 debug2("input_userauth_request: setting up authctxt for %s", user);
256 } else {
257 logit("input_userauth_request: invalid user %s", user);
258 authctxt->pw = fakepw();
259 #ifdef SSH_AUDIT_EVENTS
260 PRIVSEP(audit_event(SSH_INVALID_USER));
261 #endif
263 #ifdef USE_PAM
264 if (options.use_pam)
265 PRIVSEP(start_pam(authctxt));
266 #endif
267 setproctitle("%s%s", authctxt->valid ? user : "unknown",
268 use_privsep ? " [net]" : "");
269 authctxt->service = xstrdup(service);
270 authctxt->style = style ? xstrdup(style) : NULL;
271 if (use_privsep)
272 mm_inform_authserv(service, style);
273 userauth_banner();
274 } else if (strcmp(user, authctxt->user) != 0 ||
275 strcmp(service, authctxt->service) != 0) {
276 packet_disconnect("Change of username or service not allowed: "
277 "(%s,%s) -> (%s,%s)",
278 authctxt->user, authctxt->service, user, service);
281 #ifdef HAVE_LOGIN_CAP
282 if (authctxt->pw != NULL) {
283 lc = login_getpwclass(authctxt->pw);
284 if (lc == NULL)
285 lc = login_getclassbyname(NULL, authctxt->pw);
286 if (!auth_hostok(lc, from_host, from_ip)) {
287 logit("Denied connection for %.200s from %.200s [%.200s].",
288 authctxt->pw->pw_name, from_host, from_ip);
289 packet_disconnect("Sorry, you are not allowed to connect.");
291 if (!auth_timeok(lc, time(NULL))) {
292 logit("LOGIN %.200s REFUSED (TIME) FROM %.200s",
293 authctxt->pw->pw_name, from_host);
294 packet_disconnect("Logins not available right now.");
296 login_close(lc);
297 lc = NULL;
299 #endif /* HAVE_LOGIN_CAP */
301 /* reset state */
302 auth2_challenge_stop(authctxt);
303 #ifdef JPAKE
304 auth2_jpake_stop(authctxt);
305 #endif
307 #ifdef GSSAPI
308 /* XXX move to auth2_gssapi_stop() */
309 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
310 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, NULL);
311 #endif
313 authctxt->postponed = 0;
315 /* try to authenticate user */
316 m = authmethod_lookup(method);
317 if (m != NULL && authctxt->failures < options.max_authtries) {
318 debug2("input_userauth_request: try method %s", method);
319 authenticated = m->userauth(authctxt);
321 userauth_finish(authctxt, authenticated, method);
323 xfree(service);
324 xfree(user);
325 xfree(method);
328 void
329 userauth_finish(Authctxt *authctxt, int authenticated, char *method)
331 char *methods;
333 if (!authctxt->valid && authenticated)
334 fatal("INTERNAL ERROR: authenticated invalid user %s",
335 authctxt->user);
337 /* Special handling for root */
338 if (authenticated && authctxt->pw->pw_uid == 0 &&
339 !auth_root_allowed(method)) {
340 authenticated = 0;
341 #ifdef SSH_AUDIT_EVENTS
342 PRIVSEP(audit_event(SSH_LOGIN_ROOT_DENIED));
343 #endif
346 #ifdef USE_PAM
347 if (options.use_pam && authenticated) {
348 if (!PRIVSEP(do_pam_account())) {
349 /* if PAM returned a message, send it to the user */
350 if (buffer_len(&loginmsg) > 0) {
351 buffer_append(&loginmsg, "\0", 1);
352 userauth_send_banner(buffer_ptr(&loginmsg));
353 packet_write_wait();
355 fatal("Access denied for user %s by PAM account "
356 "configuration", authctxt->user);
359 #endif
361 #ifdef _UNICOS
362 if (authenticated && cray_access_denied(authctxt->user)) {
363 authenticated = 0;
364 fatal("Access denied for user %s.",authctxt->user);
366 #endif /* _UNICOS */
368 /* Log before sending the reply */
369 auth_log(authctxt, authenticated, method, " ssh2");
371 if (authctxt->postponed)
372 return;
374 /* XXX todo: check if multiple auth methods are needed */
375 if (authenticated == 1) {
376 /* turn off userauth */
377 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &dispatch_protocol_ignore);
378 packet_start(SSH2_MSG_USERAUTH_SUCCESS);
379 packet_send();
380 packet_write_wait();
381 /* now we can break out */
382 authctxt->success = 1;
383 } else {
385 /* Allow initial try of "none" auth without failure penalty */
386 if (authctxt->attempt > 1 || strcmp(method, "none") != 0)
387 authctxt->failures++;
388 if (authctxt->failures >= options.max_authtries) {
389 #ifdef SSH_AUDIT_EVENTS
390 PRIVSEP(audit_event(SSH_LOGIN_EXCEED_MAXTRIES));
391 #endif
392 packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
394 methods = authmethods_get();
395 packet_start(SSH2_MSG_USERAUTH_FAILURE);
396 packet_put_cstring(methods);
397 packet_put_char(0); /* XXX partial success, unused */
398 packet_send();
399 packet_write_wait();
400 xfree(methods);
404 static char *
405 authmethods_get(void)
407 Buffer b;
408 char *list;
409 int i;
411 buffer_init(&b);
412 for (i = 0; authmethods[i] != NULL; i++) {
413 if (strcmp(authmethods[i]->name, "none") == 0)
414 continue;
415 if (authmethods[i]->enabled != NULL &&
416 *(authmethods[i]->enabled) != 0) {
417 if (buffer_len(&b) > 0)
418 buffer_append(&b, ",", 1);
419 buffer_append(&b, authmethods[i]->name,
420 strlen(authmethods[i]->name));
423 buffer_append(&b, "\0", 1);
424 list = xstrdup(buffer_ptr(&b));
425 buffer_free(&b);
426 return list;
429 static Authmethod *
430 authmethod_lookup(const char *name)
432 int i;
434 if (name != NULL)
435 for (i = 0; authmethods[i] != NULL; i++)
436 if (authmethods[i]->enabled != NULL &&
437 *(authmethods[i]->enabled) != 0 &&
438 strcmp(name, authmethods[i]->name) == 0)
439 return authmethods[i];
440 debug2("Unrecognized authentication method name: %s",
441 name ? name : "NULL");
442 return NULL;