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