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