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