Annotation of src/usr.bin/ssh/auth1.c, Revision 1.50
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.50 ! markus 13: RCSID("$OpenBSD: auth1.c,v 1.49 2003/07/22 13:35:22 markus 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.49 markus 52: #ifdef KRB5
1.1 markus 53: case SSH_CMSG_AUTH_KERBEROS:
54: return "kerberos";
55: #endif
56: }
57: snprintf(buf, sizeof buf, "bad-auth-msg-%d", type);
58: return buf;
59: }
60:
61: /*
1.11 markus 62: * read packets, try to authenticate the user and
63: * return only if authentication is successful
1.1 markus 64: */
1.24 itojun 65: static void
1.11 markus 66: do_authloop(Authctxt *authctxt)
1.1 markus 67: {
1.5 markus 68: int authenticated = 0;
1.8 markus 69: u_int bits;
1.29 markus 70: Key *client_host_key;
1.1 markus 71: BIGNUM *n;
72: char *client_user, *password;
1.11 markus 73: char info[1024];
1.8 markus 74: u_int dlen;
75: u_int ulen;
1.1 markus 76: int type = 0;
1.11 markus 77: struct passwd *pw = authctxt->pw;
78:
79: debug("Attempting authentication for %s%.100s.",
1.27 deraadt 80: authctxt->valid ? "" : "illegal user ", authctxt->user);
1.11 markus 81:
82: /* If the user has no password, accept authentication immediately. */
83: if (options.password_authentication &&
1.49 markus 84: #ifdef KRB5
1.11 markus 85: (!options.kerberos_authentication || options.kerberos_or_local_passwd) &&
86: #endif
1.38 provos 87: PRIVSEP(auth_password(authctxt, ""))) {
1.11 markus 88: auth_log(authctxt, 1, "without authentication", "");
89: return;
90: }
1.27 deraadt 91:
1.1 markus 92: /* Indicate that authentication is needed. */
93: packet_start(SSH_SMSG_FAILURE);
94: packet_send();
95: packet_write_wait();
96:
1.11 markus 97: for (;;) {
1.5 markus 98: /* default to fail */
99: authenticated = 0;
100:
1.11 markus 101: info[0] = '\0';
1.1 markus 102:
103: /* Get a packet from the client. */
1.34 markus 104: type = packet_read();
1.1 markus 105:
106: /* Process the packet. */
107: switch (type) {
108:
1.49 markus 109: #ifdef KRB5
1.1 markus 110: case SSH_CMSG_AUTH_KERBEROS:
111: if (!options.kerberos_authentication) {
112: verbose("Kerberos authentication disabled.");
113: } else {
1.25 dugsong 114: char *kdata = packet_get_string(&dlen);
1.32 markus 115: packet_check_eom();
1.27 deraadt 116:
1.49 markus 117: if (kdata[0] != 4) { /* KRB_PROT_VERSION */
1.43 itojun 118: krb5_data tkt, reply;
1.25 dugsong 119: tkt.length = dlen;
120: tkt.data = kdata;
1.27 deraadt 121:
1.43 itojun 122: if (PRIVSEP(auth_krb5(authctxt, &tkt,
123: &client_user, &reply))) {
1.25 dugsong 124: authenticated = 1;
125: snprintf(info, sizeof(info),
126: " tktuser %.100s",
127: client_user);
1.45 deraadt 128:
1.43 itojun 129: /* Send response to client */
130: packet_start(
131: SSH_SMSG_AUTH_KERBEROS_RESPONSE);
132: packet_put_string((char *)
133: reply.data, reply.length);
134: packet_send();
135: packet_write_wait();
136:
137: if (reply.length)
138: xfree(reply.data);
1.25 dugsong 139: xfree(client_user);
140: }
1.1 markus 141: }
1.11 markus 142: xfree(kdata);
1.1 markus 143: }
144: break;
1.25 dugsong 145: case SSH_CMSG_HAVE_KERBEROS_TGT:
146: packet_send_debug("Kerberos TGT passing disabled before authentication.");
147: break;
1.49 markus 148: #endif
1.1 markus 149:
150: case SSH_CMSG_AUTH_RHOSTS_RSA:
151: if (!options.rhosts_rsa_authentication) {
152: verbose("Rhosts with RSA authentication disabled.");
153: break;
154: }
155: /*
156: * Get client user name. Note that we just have to
157: * trust the client; root on the client machine can
158: * claim to be any user.
159: */
160: client_user = packet_get_string(&ulen);
161:
162: /* Get the client host key. */
1.29 markus 163: client_host_key = key_new(KEY_RSA1);
1.1 markus 164: bits = packet_get_int();
1.33 markus 165: packet_get_bignum(client_host_key->rsa->e);
166: packet_get_bignum(client_host_key->rsa->n);
1.1 markus 167:
1.29 markus 168: if (bits != BN_num_bits(client_host_key->rsa->n))
1.5 markus 169: verbose("Warning: keysize mismatch for client_host_key: "
1.29 markus 170: "actual %d, announced %d",
1.41 deraadt 171: BN_num_bits(client_host_key->rsa->n), bits);
1.32 markus 172: packet_check_eom();
1.1 markus 173:
1.29 markus 174: authenticated = auth_rhosts_rsa(pw, client_user,
1.30 markus 175: client_host_key);
1.29 markus 176: key_free(client_host_key);
1.1 markus 177:
1.11 markus 178: snprintf(info, sizeof info, " ruser %.100s", client_user);
1.1 markus 179: xfree(client_user);
180: break;
181:
182: case SSH_CMSG_AUTH_RSA:
183: if (!options.rsa_authentication) {
184: verbose("RSA authentication disabled.");
185: break;
186: }
187: /* RSA authentication requested. */
1.29 markus 188: if ((n = BN_new()) == NULL)
189: fatal("do_authloop: BN_new failed");
1.33 markus 190: packet_get_bignum(n);
1.32 markus 191: packet_check_eom();
1.1 markus 192: authenticated = auth_rsa(pw, n);
193: BN_clear_free(n);
194: break;
195:
196: case SSH_CMSG_AUTH_PASSWORD:
197: if (!options.password_authentication) {
198: verbose("Password authentication disabled.");
199: break;
200: }
201: /*
202: * Read user password. It is in plain text, but was
203: * transmitted over the encrypted channel so it is
204: * not visible to an outside observer.
205: */
206: password = packet_get_string(&dlen);
1.32 markus 207: packet_check_eom();
1.1 markus 208:
209: /* Try authentication with the password. */
1.38 provos 210: authenticated = PRIVSEP(auth_password(authctxt, password));
1.1 markus 211:
212: memset(password, 0, strlen(password));
213: xfree(password);
214: break;
215:
216: case SSH_CMSG_AUTH_TIS:
217: debug("rcvd SSH_CMSG_AUTH_TIS");
1.23 markus 218: if (options.challenge_response_authentication == 1) {
219: char *challenge = get_challenge(authctxt);
1.11 markus 220: if (challenge != NULL) {
221: debug("sending challenge '%s'", challenge);
1.1 markus 222: packet_start(SSH_SMSG_AUTH_TIS_CHALLENGE);
1.11 markus 223: packet_put_cstring(challenge);
1.23 markus 224: xfree(challenge);
1.1 markus 225: packet_send();
226: packet_write_wait();
227: continue;
228: }
229: }
230: break;
231: case SSH_CMSG_AUTH_TIS_RESPONSE:
232: debug("rcvd SSH_CMSG_AUTH_TIS_RESPONSE");
1.23 markus 233: if (options.challenge_response_authentication == 1) {
1.1 markus 234: char *response = packet_get_string(&dlen);
1.32 markus 235: packet_check_eom();
1.11 markus 236: authenticated = verify_response(authctxt, response);
237: memset(response, 'r', dlen);
1.1 markus 238: xfree(response);
239: }
240: break;
241:
242: default:
243: /*
244: * Any unknown messages will be ignored (and failure
245: * returned) during authentication.
246: */
1.48 itojun 247: logit("Unknown message during authentication: type %d", type);
1.1 markus 248: break;
249: }
1.20 markus 250: #ifdef BSD_AUTH
251: if (authctxt->as) {
252: auth_close(authctxt->as);
253: authctxt->as = NULL;
254: }
255: #endif
1.11 markus 256: if (!authctxt->valid && authenticated)
257: fatal("INTERNAL ERROR: authenticated invalid user %s",
258: authctxt->user);
259:
260: /* Special handling for root */
1.47 markus 261: if (authenticated && authctxt->pw->pw_uid == 0 &&
1.16 markus 262: !auth_root_allowed(get_authname(type)))
1.11 markus 263: authenticated = 0;
1.1 markus 264:
1.11 markus 265: /* Log before sending the reply */
266: auth_log(authctxt, authenticated, get_authname(type), info);
1.1 markus 267:
268: if (authenticated)
269: return;
270:
1.11 markus 271: if (authctxt->failures++ > AUTH_FAIL_MAX)
272: packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
1.1 markus 273:
274: packet_start(SSH_SMSG_FAILURE);
275: packet_send();
276: packet_write_wait();
277: }
278: }
279:
280: /*
281: * Performs authentication of an incoming connection. Session key has already
282: * been exchanged and encryption is enabled.
283: */
1.37 provos 284: Authctxt *
1.26 itojun 285: do_authentication(void)
1.1 markus 286: {
1.11 markus 287: Authctxt *authctxt;
1.8 markus 288: u_int ulen;
1.40 markus 289: char *user, *style = NULL;
1.1 markus 290:
291: /* Get the name of the user that we wish to log in as. */
1.34 markus 292: packet_read_expect(SSH_CMSG_USER);
1.1 markus 293:
294: /* Get the user name. */
295: user = packet_get_string(&ulen);
1.32 markus 296: packet_check_eom();
1.1 markus 297:
1.11 markus 298: if ((style = strchr(user, ':')) != NULL)
1.25 dugsong 299: *style++ = '\0';
1.11 markus 300:
1.40 markus 301: #ifdef KRB5
1.25 dugsong 302: /* XXX - SSH.com Kerberos v5 braindeath. */
1.40 markus 303: if ((datafellows & SSH_BUG_K5USER) &&
304: options.kerberos_authentication) {
305: char *p;
306: if ((p = strchr(user, '@')) != NULL)
307: *p = '\0';
308: }
309: #endif
1.27 deraadt 310:
1.11 markus 311: authctxt = authctxt_new();
312: authctxt->user = user;
313: authctxt->style = style;
314:
1.1 markus 315: /* Verify that the user is a valid user. */
1.39 markus 316: if ((authctxt->pw = PRIVSEP(getpwnamallow(user))) != NULL)
1.11 markus 317: authctxt->valid = 1;
1.39 markus 318: else
1.11 markus 319: debug("do_authentication: illegal user %s", user);
1.17 markus 320:
1.39 markus 321: setproctitle("%s%s", authctxt->pw ? user : "unknown",
1.38 provos 322: use_privsep ? " [net]" : "");
1.1 markus 323:
324: /*
325: * If we are not running as root, the user must have the same uid as
326: * the server.
327: */
1.39 markus 328: if (!use_privsep && getuid() != 0 && authctxt->pw &&
329: authctxt->pw->pw_uid != getuid())
1.1 markus 330: packet_disconnect("Cannot change user when server not running as root.");
331:
1.11 markus 332: /*
333: * Loop until the user has been authenticated or the connection is
334: * closed, do_authloop() returns only if authentication is successful
335: */
336: do_authloop(authctxt);
1.1 markus 337:
338: /* The user has been authenticated and accepted. */
339: packet_start(SSH_SMSG_SUCCESS);
340: packet_send();
341: packet_write_wait();
342:
1.37 provos 343: return (authctxt);
1.1 markus 344: }