Annotation of src/usr.bin/ssh/auth1.c, Revision 1.58
1.1 markus 1: /*
2: * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
3: * All rights reserved
1.4 deraadt 4: *
5: * As far as I am concerned, the code I have written for this software
6: * can be used freely for any purpose. Any derived versions of this
7: * software must be clearly marked as such, and if the derived work is
8: * incompatible with the protocol description in the RFC file, it must be
9: * called by a name other than "ssh" or "Secure Shell".
1.1 markus 10: */
11:
12: #include "includes.h"
1.58 ! djm 13: RCSID("$OpenBSD: auth1.c,v 1.57 2004/05/23 23:59:53 dtucker Exp $");
1.1 markus 14:
15: #include "xmalloc.h"
16: #include "rsa.h"
1.12 markus 17: #include "ssh1.h"
1.1 markus 18: #include "packet.h"
19: #include "buffer.h"
1.13 markus 20: #include "log.h"
1.1 markus 21: #include "servconf.h"
22: #include "compat.h"
23: #include "auth.h"
1.35 markus 24: #include "channels.h"
1.1 markus 25: #include "session.h"
1.25 dugsong 26: #include "uidswap.h"
1.38 provos 27: #include "monitor_wrap.h"
1.1 markus 28:
29: /* import */
30: extern ServerOptions options;
31:
32: /*
33: * convert ssh auth msg type into description
34: */
1.24 itojun 35: static char *
1.1 markus 36: get_authname(int type)
37: {
38: static char buf[1024];
39: switch (type) {
40: case SSH_CMSG_AUTH_PASSWORD:
41: return "password";
42: case SSH_CMSG_AUTH_RSA:
43: return "rsa";
44: case SSH_CMSG_AUTH_RHOSTS_RSA:
45: return "rhosts-rsa";
46: case SSH_CMSG_AUTH_RHOSTS:
47: return "rhosts";
1.11 markus 48: case SSH_CMSG_AUTH_TIS:
49: case SSH_CMSG_AUTH_TIS_RESPONSE:
50: return "challenge-response";
1.1 markus 51: }
52: snprintf(buf, sizeof buf, "bad-auth-msg-%d", type);
53: return buf;
54: }
55:
56: /*
1.11 markus 57: * read packets, try to authenticate the user and
58: * return only if authentication is successful
1.1 markus 59: */
1.24 itojun 60: static void
1.11 markus 61: do_authloop(Authctxt *authctxt)
1.1 markus 62: {
1.5 markus 63: int authenticated = 0;
1.8 markus 64: u_int bits;
1.29 markus 65: Key *client_host_key;
1.1 markus 66: BIGNUM *n;
67: char *client_user, *password;
1.11 markus 68: char info[1024];
1.8 markus 69: u_int dlen;
70: u_int ulen;
1.1 markus 71: int type = 0;
1.11 markus 72:
73: debug("Attempting authentication for %s%.100s.",
1.27 deraadt 74: authctxt->valid ? "" : "illegal user ", authctxt->user);
1.11 markus 75:
76: /* If the user has no password, accept authentication immediately. */
77: if (options.password_authentication &&
1.49 markus 78: #ifdef KRB5
1.11 markus 79: (!options.kerberos_authentication || options.kerberos_or_local_passwd) &&
80: #endif
1.38 provos 81: PRIVSEP(auth_password(authctxt, ""))) {
1.11 markus 82: auth_log(authctxt, 1, "without authentication", "");
83: return;
84: }
1.27 deraadt 85:
1.1 markus 86: /* Indicate that authentication is needed. */
87: packet_start(SSH_SMSG_FAILURE);
88: packet_send();
89: packet_write_wait();
90:
1.11 markus 91: for (;;) {
1.5 markus 92: /* default to fail */
93: authenticated = 0;
94:
1.11 markus 95: info[0] = '\0';
1.1 markus 96:
97: /* Get a packet from the client. */
1.34 markus 98: type = packet_read();
1.1 markus 99:
100: /* Process the packet. */
101: switch (type) {
102: case SSH_CMSG_AUTH_RHOSTS_RSA:
103: if (!options.rhosts_rsa_authentication) {
104: verbose("Rhosts with RSA authentication disabled.");
105: break;
106: }
107: /*
108: * Get client user name. Note that we just have to
109: * trust the client; root on the client machine can
110: * claim to be any user.
111: */
112: client_user = packet_get_string(&ulen);
113:
114: /* Get the client host key. */
1.29 markus 115: client_host_key = key_new(KEY_RSA1);
1.1 markus 116: bits = packet_get_int();
1.33 markus 117: packet_get_bignum(client_host_key->rsa->e);
118: packet_get_bignum(client_host_key->rsa->n);
1.1 markus 119:
1.29 markus 120: if (bits != BN_num_bits(client_host_key->rsa->n))
1.5 markus 121: verbose("Warning: keysize mismatch for client_host_key: "
1.29 markus 122: "actual %d, announced %d",
1.41 deraadt 123: BN_num_bits(client_host_key->rsa->n), bits);
1.32 markus 124: packet_check_eom();
1.1 markus 125:
1.54 djm 126: authenticated = auth_rhosts_rsa(authctxt, client_user,
1.30 markus 127: client_host_key);
1.29 markus 128: key_free(client_host_key);
1.1 markus 129:
1.11 markus 130: snprintf(info, sizeof info, " ruser %.100s", client_user);
1.1 markus 131: xfree(client_user);
132: break;
133:
134: case SSH_CMSG_AUTH_RSA:
135: if (!options.rsa_authentication) {
136: verbose("RSA authentication disabled.");
137: break;
138: }
139: /* RSA authentication requested. */
1.29 markus 140: if ((n = BN_new()) == NULL)
141: fatal("do_authloop: BN_new failed");
1.33 markus 142: packet_get_bignum(n);
1.32 markus 143: packet_check_eom();
1.54 djm 144: authenticated = auth_rsa(authctxt, n);
1.1 markus 145: BN_clear_free(n);
146: break;
147:
148: case SSH_CMSG_AUTH_PASSWORD:
149: if (!options.password_authentication) {
150: verbose("Password authentication disabled.");
151: break;
152: }
153: /*
154: * Read user password. It is in plain text, but was
155: * transmitted over the encrypted channel so it is
156: * not visible to an outside observer.
157: */
158: password = packet_get_string(&dlen);
1.32 markus 159: packet_check_eom();
1.1 markus 160:
161: /* Try authentication with the password. */
1.38 provos 162: authenticated = PRIVSEP(auth_password(authctxt, password));
1.1 markus 163:
164: memset(password, 0, strlen(password));
165: xfree(password);
166: break;
167:
168: case SSH_CMSG_AUTH_TIS:
169: debug("rcvd SSH_CMSG_AUTH_TIS");
1.23 markus 170: if (options.challenge_response_authentication == 1) {
171: char *challenge = get_challenge(authctxt);
1.11 markus 172: if (challenge != NULL) {
173: debug("sending challenge '%s'", challenge);
1.1 markus 174: packet_start(SSH_SMSG_AUTH_TIS_CHALLENGE);
1.11 markus 175: packet_put_cstring(challenge);
1.23 markus 176: xfree(challenge);
1.1 markus 177: packet_send();
178: packet_write_wait();
179: continue;
180: }
181: }
182: break;
183: case SSH_CMSG_AUTH_TIS_RESPONSE:
184: debug("rcvd SSH_CMSG_AUTH_TIS_RESPONSE");
1.23 markus 185: if (options.challenge_response_authentication == 1) {
1.1 markus 186: char *response = packet_get_string(&dlen);
1.32 markus 187: packet_check_eom();
1.11 markus 188: authenticated = verify_response(authctxt, response);
189: memset(response, 'r', dlen);
1.1 markus 190: xfree(response);
191: }
192: break;
193:
194: default:
195: /*
196: * Any unknown messages will be ignored (and failure
197: * returned) during authentication.
198: */
1.48 itojun 199: logit("Unknown message during authentication: type %d", type);
1.1 markus 200: break;
201: }
1.20 markus 202: #ifdef BSD_AUTH
203: if (authctxt->as) {
204: auth_close(authctxt->as);
205: authctxt->as = NULL;
206: }
207: #endif
1.11 markus 208: if (!authctxt->valid && authenticated)
209: fatal("INTERNAL ERROR: authenticated invalid user %s",
210: authctxt->user);
211:
212: /* Special handling for root */
1.47 markus 213: if (authenticated && authctxt->pw->pw_uid == 0 &&
1.16 markus 214: !auth_root_allowed(get_authname(type)))
1.11 markus 215: authenticated = 0;
1.1 markus 216:
1.11 markus 217: /* Log before sending the reply */
218: auth_log(authctxt, authenticated, get_authname(type), info);
1.1 markus 219:
220: if (authenticated)
221: return;
222:
1.57 dtucker 223: if (authctxt->failures++ > options.max_authtries)
1.11 markus 224: packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
1.1 markus 225:
226: packet_start(SSH_SMSG_FAILURE);
227: packet_send();
228: packet_write_wait();
229: }
230: }
231:
232: /*
233: * Performs authentication of an incoming connection. Session key has already
234: * been exchanged and encryption is enabled.
235: */
1.53 markus 236: void
237: do_authentication(Authctxt *authctxt)
1.1 markus 238: {
1.8 markus 239: u_int ulen;
1.40 markus 240: char *user, *style = NULL;
1.1 markus 241:
242: /* Get the name of the user that we wish to log in as. */
1.34 markus 243: packet_read_expect(SSH_CMSG_USER);
1.1 markus 244:
245: /* Get the user name. */
246: user = packet_get_string(&ulen);
1.32 markus 247: packet_check_eom();
1.1 markus 248:
1.11 markus 249: if ((style = strchr(user, ':')) != NULL)
1.25 dugsong 250: *style++ = '\0';
1.27 deraadt 251:
1.11 markus 252: authctxt->user = user;
253: authctxt->style = style;
254:
1.1 markus 255: /* Verify that the user is a valid user. */
1.39 markus 256: if ((authctxt->pw = PRIVSEP(getpwnamallow(user))) != NULL)
1.11 markus 257: authctxt->valid = 1;
1.51 markus 258: else {
1.11 markus 259: debug("do_authentication: illegal user %s", user);
1.51 markus 260: authctxt->pw = fakepw();
261: }
1.17 markus 262:
1.58 ! djm 263: setproctitle("%s%s", authctxt->valid ? user : "unknown",
1.38 provos 264: use_privsep ? " [net]" : "");
1.1 markus 265:
266: /*
267: * If we are not running as root, the user must have the same uid as
268: * the server.
269: */
1.39 markus 270: if (!use_privsep && getuid() != 0 && authctxt->pw &&
271: authctxt->pw->pw_uid != getuid())
1.1 markus 272: packet_disconnect("Cannot change user when server not running as root.");
273:
1.11 markus 274: /*
275: * Loop until the user has been authenticated or the connection is
276: * closed, do_authloop() returns only if authentication is successful
277: */
278: do_authloop(authctxt);
1.1 markus 279:
280: /* The user has been authenticated and accepted. */
281: packet_start(SSH_SMSG_SUCCESS);
282: packet_send();
283: packet_write_wait();
284: }