No empty .Rs/.Re
[netbsd-mini2440.git] / crypto / external / bsd / openssh / dist / auth1.c
blobd9b1fdd9be6db1a56493c5d9be5475fda9de02af
1 /* $NetBSD$ */
2 /* $OpenBSD: auth1.c,v 1.73 2008/07/04 23:30:16 djm Exp $ */
3 /*
4 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * All rights reserved
7 * As far as I am concerned, the code I have written for this software
8 * can be used freely for any purpose. Any derived versions of this
9 * software must be clearly marked as such, and if the derived work is
10 * incompatible with the protocol description in the RFC file, it must be
11 * called by a name other than "ssh" or "Secure Shell".
14 #include "includes.h"
15 __RCSID("$NetBSD: auth1.c,v 1.33 2009/02/16 20:53:54 christos Exp $");
16 #include <sys/types.h>
17 #include <sys/queue.h>
19 #include <stdio.h>
20 #include <string.h>
21 #include <unistd.h>
22 #include <pwd.h>
24 #include "xmalloc.h"
25 #include "rsa.h"
26 #include "ssh1.h"
27 #include "packet.h"
28 #include "buffer.h"
29 #include "log.h"
30 #include "servconf.h"
31 #include "compat.h"
32 #include "key.h"
33 #include "hostfile.h"
34 #include "auth.h"
35 #include "channels.h"
36 #include "session.h"
37 #include "uidswap.h"
38 #ifdef GSSAPI
39 #include "ssh-gss.h"
40 #endif
41 #include "monitor_wrap.h"
42 #include "buffer.h"
44 /* import */
45 extern ServerOptions options;
46 extern Buffer loginmsg;
48 static int auth1_process_password(Authctxt *, char *, size_t);
49 static int auth1_process_rsa(Authctxt *, char *, size_t);
50 static int auth1_process_rhosts_rsa(Authctxt *, char *, size_t);
51 static int auth1_process_tis_challenge(Authctxt *, char *, size_t);
52 static int auth1_process_tis_response(Authctxt *, char *, size_t);
53 #if defined(KRB4) || defined(KRB5)
54 static int auth1_process_kerberos(Authctxt *, char *, size_t);
55 #endif
57 struct AuthMethod1 {
58 int type;
59 char *name;
60 int *enabled;
61 int (*method)(Authctxt *, char *, size_t);
64 const struct AuthMethod1 auth1_methods[] = {
66 SSH_CMSG_AUTH_PASSWORD, "password",
67 &options.password_authentication, auth1_process_password
70 SSH_CMSG_AUTH_RSA, "rsa",
71 &options.rsa_authentication, auth1_process_rsa
74 SSH_CMSG_AUTH_RHOSTS_RSA, "rhosts-rsa",
75 &options.rhosts_rsa_authentication, auth1_process_rhosts_rsa
78 SSH_CMSG_AUTH_TIS, "challenge-response",
79 &options.challenge_response_authentication,
80 auth1_process_tis_challenge
83 SSH_CMSG_AUTH_TIS_RESPONSE, "challenge-response",
84 &options.challenge_response_authentication,
85 auth1_process_tis_response
87 #if defined(KRB4) || defined(KRB5)
89 SSH_CMSG_AUTH_KERBEROS, "kerberos",
90 &options.kerberos_authentication,
91 auth1_process_kerberos
93 #endif /* KRB4 || KRB5 */
94 { -1, NULL, NULL, NULL}
97 static const struct AuthMethod1
98 *lookup_authmethod1(int type)
100 int i;
102 for (i = 0; auth1_methods[i].name != NULL; i++)
103 if (auth1_methods[i].type == type)
104 return (&(auth1_methods[i]));
106 return (NULL);
109 static char *
110 get_authname(int type)
112 const struct AuthMethod1 *a;
113 static char buf[64];
115 if ((a = lookup_authmethod1(type)) != NULL)
116 return (a->name);
117 snprintf(buf, sizeof(buf), "bad-auth-msg-%d", type);
118 return (buf);
121 /*ARGSUSED*/
122 static int
123 auth1_process_password(Authctxt *authctxt, char *info, size_t infolen)
125 int authenticated = 0;
126 char *password;
127 u_int dlen;
130 * Read user password. It is in plain text, but was
131 * transmitted over the encrypted channel so it is
132 * not visible to an outside observer.
134 password = packet_get_string(&dlen);
135 packet_check_eom();
137 /* Try authentication with the password. */
138 authenticated = PRIVSEP(auth_password(authctxt, password));
140 memset(password, 0, dlen);
141 xfree(password);
143 return (authenticated);
146 #if defined(KRB4) || defined(KRB5)
147 static int
148 auth1_process_kerberos(Authctxt *authctxt, char *info, size_t infolen)
150 int authenticated = 0;
151 u_int dlen;
152 char *client_user;
153 char *kdata = packet_get_string(&dlen);
154 packet_check_eom();
156 if (kdata[0] == 4) { /* KRB_PROT_VERSION */
157 #ifdef KRB4
158 KTEXT_ST tkt, reply;
159 tkt.length = dlen;
160 if (tkt.length < MAX_KTXT_LEN)
161 memcpy(tkt.dat, kdata, tkt.length);
163 if (PRIVSEP(auth_krb4(authctxt, &tkt, &client_user, &reply))) {
164 authenticated = 1;
165 snprintf(info, sizeof(info), " tktuser %.100s",
166 client_user);
168 packet_start(SSH_SMSG_AUTH_KERBEROS_RESPONSE);
169 packet_put_string((char *)
170 reply.dat, reply.length);
171 packet_send();
172 packet_write_wait();
174 xfree(client_user);
176 #endif /* KRB4 */
177 } else {
178 #ifdef KRB5
179 krb5_data tkt, reply;
180 tkt.length = dlen;
181 tkt.data = kdata;
183 if (PRIVSEP(auth_krb5(authctxt, &tkt, &client_user, &reply))) {
184 authenticated = 1;
185 snprintf(info, sizeof(info), " tktuser %.100s",
186 client_user);
188 /* Send response to client */
189 packet_start(SSH_SMSG_AUTH_KERBEROS_RESPONSE);
190 packet_put_string((char *)reply.data, reply.length);
191 packet_send();
192 packet_write_wait();
194 if (reply.length)
195 xfree(reply.data);
196 xfree(client_user);
198 #endif /* KRB5 */
200 xfree(kdata);
201 return authenticated;
203 #endif /* KRB4 || KRB5 */
205 /*ARGSUSED*/
206 static int
207 auth1_process_rhosts_rsa(Authctxt *authctxt, char *info, size_t infolen)
209 int keybits, authenticated = 0;
210 u_int bits;
211 char *client_user;
212 Key *client_host_key;
213 u_int ulen;
216 * Get client user name. Note that we just have to
217 * trust the client; root on the client machine can
218 * claim to be any user.
220 client_user = packet_get_string(&ulen);
222 /* Get the client host key. */
223 client_host_key = key_new(KEY_RSA1);
224 bits = packet_get_int();
225 packet_get_bignum(client_host_key->rsa->e);
226 packet_get_bignum(client_host_key->rsa->n);
228 keybits = BN_num_bits(client_host_key->rsa->n);
229 if (keybits < 0 || bits != (u_int)keybits) {
230 verbose("Warning: keysize mismatch for client_host_key: "
231 "actual %d, announced %d",
232 BN_num_bits(client_host_key->rsa->n), bits);
234 packet_check_eom();
236 authenticated = auth_rhosts_rsa(authctxt, client_user,
237 client_host_key);
238 key_free(client_host_key);
240 snprintf(info, infolen, " ruser %.100s", client_user);
241 xfree(client_user);
243 return (authenticated);
246 /*ARGSUSED*/
247 static int
248 auth1_process_rsa(Authctxt *authctxt, char *info, size_t infolen)
250 int authenticated = 0;
251 BIGNUM *n;
253 /* RSA authentication requested. */
254 if ((n = BN_new()) == NULL)
255 fatal("do_authloop: BN_new failed");
256 packet_get_bignum(n);
257 packet_check_eom();
258 authenticated = auth_rsa(authctxt, n);
259 BN_clear_free(n);
261 return (authenticated);
264 /*ARGSUSED*/
265 static int
266 auth1_process_tis_challenge(Authctxt *authctxt, char *info, size_t infolen)
268 char *challenge;
270 if ((challenge = get_challenge(authctxt)) == NULL)
271 return (0);
273 debug("sending challenge '%s'", challenge);
274 packet_start(SSH_SMSG_AUTH_TIS_CHALLENGE);
275 packet_put_cstring(challenge);
276 xfree(challenge);
277 packet_send();
278 packet_write_wait();
280 return (-1);
283 /*ARGSUSED*/
284 static int
285 auth1_process_tis_response(Authctxt *authctxt, char *info, size_t infolen)
287 int authenticated = 0;
288 char *response;
289 u_int dlen;
291 response = packet_get_string(&dlen);
292 packet_check_eom();
293 authenticated = verify_response(authctxt, response);
294 memset(response, 'r', dlen);
295 xfree(response);
297 return (authenticated);
301 * read packets, try to authenticate the user and
302 * return only if authentication is successful
304 static void
305 do_authloop(Authctxt *authctxt)
307 int authenticated = 0;
308 char info[1024];
309 int type = 0;
310 const struct AuthMethod1 *meth;
312 debug("Attempting authentication for %s%.100s.",
313 authctxt->valid ? "" : "invalid user ", authctxt->user);
315 /* If the user has no password, accept authentication immediately. */
316 if (options.password_authentication &&
317 #if defined(KRB4) || defined(KRB5)
318 (!options.kerberos_authentication || options.kerberos_or_local_passwd) &&
319 #endif
320 PRIVSEP(auth_password(authctxt, ""))) {
321 #ifdef USE_PAM
322 if (options.use_pam && PRIVSEP(do_pam_account()))
323 #endif
325 auth_log(authctxt, 1, "without authentication", "");
326 return;
328 return;
331 /* Indicate that authentication is needed. */
332 packet_start(SSH_SMSG_FAILURE);
333 packet_send();
334 packet_write_wait();
336 for (;;) {
337 /* default to fail */
338 authenticated = 0;
340 info[0] = '\0';
342 /* Get a packet from the client. */
343 type = packet_read();
344 if (authctxt->failures >= options.max_authtries)
345 goto skip;
346 if ((meth = lookup_authmethod1(type)) == NULL) {
347 logit("Unknown message during authentication: "
348 "type %d", type);
349 goto skip;
352 if (!*(meth->enabled)) {
353 verbose("%s authentication disabled.", meth->name);
354 goto skip;
357 authenticated = meth->method(authctxt, info, sizeof(info));
358 if (authenticated == -1)
359 continue; /* "postponed" */
361 #ifdef BSD_AUTH
362 if (authctxt->as) {
363 auth_close(authctxt->as);
364 authctxt->as = NULL;
366 #endif
367 if (!authctxt->valid && authenticated)
368 fatal("INTERNAL ERROR: authenticated invalid user %s",
369 authctxt->user);
371 /* Special handling for root */
372 if (authenticated && authctxt->pw->pw_uid == 0 &&
373 !auth_root_allowed(meth->name))
374 authenticated = 0;
376 #ifdef USE_PAM
377 if (options.use_pam && authenticated &&
378 !PRIVSEP(do_pam_account())) {
379 char *msg;
380 size_t len;
382 error("Access denied for user %s by PAM account "
383 "configuration", authctxt->user);
384 len = buffer_len(&loginmsg);
385 buffer_append(&loginmsg, "\0", 1);
386 msg = buffer_ptr(&loginmsg);
387 /* strip trailing newlines */
388 if (len > 0)
389 while (len > 0 && msg[--len] == '\n')
390 msg[len] = '\0';
391 else
392 msg = "Access denied.";
393 packet_disconnect(msg);
395 #endif
397 skip:
398 /* Log before sending the reply */
399 auth_log(authctxt, authenticated, get_authname(type), info);
401 if (authenticated)
402 return;
404 if (++authctxt->failures >= options.max_authtries)
405 packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
407 packet_start(SSH_SMSG_FAILURE);
408 packet_send();
409 packet_write_wait();
414 * Performs authentication of an incoming connection. Session key has already
415 * been exchanged and encryption is enabled.
417 void
418 do_authentication(Authctxt *authctxt)
420 u_int ulen;
421 char *user, *style = NULL;
423 /* Get the name of the user that we wish to log in as. */
424 packet_read_expect(SSH_CMSG_USER);
426 /* Get the user name. */
427 user = packet_get_string(&ulen);
428 packet_check_eom();
430 if ((style = strchr(user, ':')) != NULL)
431 *style++ = '\0';
433 #ifdef KRB5
434 /* XXX - SSH.com Kerberos v5 braindeath. */
435 if ((datafellows & SSH_BUG_K5USER) &&
436 options.kerberos_authentication) {
437 char *p;
438 if ((p = strchr(user, '@')) != NULL)
439 *p = '\0';
441 #endif
443 authctxt->user = user;
444 authctxt->style = style;
446 /* Verify that the user is a valid user. */
447 if ((authctxt->pw = PRIVSEP(getpwnamallow(user))) != NULL)
448 authctxt->valid = 1;
449 else {
450 debug("do_authentication: invalid user %s", user);
451 authctxt->pw = fakepw();
454 setproctitle("%s%s", authctxt->valid ? user : "unknown",
455 use_privsep ? " [net]" : "");
457 #ifdef USE_PAM
458 if (options.use_pam)
459 PRIVSEP(start_pam(authctxt));
460 #endif
463 * If we are not running as root, the user must have the same uid as
464 * the server.
466 if (!use_privsep && getuid() != 0 && authctxt->pw &&
467 authctxt->pw->pw_uid != getuid())
468 packet_disconnect("Cannot change user when server not running as root.");
471 * Loop until the user has been authenticated or the connection is
472 * closed, do_authloop() returns only if authentication is successful
474 do_authloop(authctxt);
476 /* The user has been authenticated and accepted. */
477 packet_start(SSH_SMSG_SUCCESS);
478 packet_send();
479 packet_write_wait();