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