Annotation of src/usr.bin/ssh/auth1.c, Revision 1.78
1.78 ! djm 1: /* $OpenBSD: auth1.c,v 1.77 2012/12/02 20:34:09 djm Exp $ */
1.1 markus 2: /*
3: * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4: * All rights reserved
1.4 deraadt 5: *
6: * As far as I am concerned, the code I have written for this software
7: * can be used freely for any purpose. Any derived versions of this
8: * software must be clearly marked as such, and if the derived work is
9: * incompatible with the protocol description in the RFC file, it must be
10: * called by a name other than "ssh" or "Secure Shell".
1.1 markus 11: */
12:
1.67 stevesk 13: #include <sys/types.h>
1.72 djm 14: #include <sys/queue.h>
1.67 stevesk 15:
1.69 stevesk 16: #include <stdio.h>
1.68 stevesk 17: #include <string.h>
1.67 stevesk 18: #include <unistd.h>
1.70 deraadt 19: #include <pwd.h>
1.1 markus 20:
21: #include "xmalloc.h"
22: #include "rsa.h"
1.12 markus 23: #include "ssh1.h"
1.1 markus 24: #include "packet.h"
25: #include "buffer.h"
1.13 markus 26: #include "log.h"
1.1 markus 27: #include "servconf.h"
28: #include "compat.h"
1.70 deraadt 29: #include "key.h"
30: #include "hostfile.h"
1.1 markus 31: #include "auth.h"
1.35 markus 32: #include "channels.h"
1.1 markus 33: #include "session.h"
1.25 dugsong 34: #include "uidswap.h"
1.70 deraadt 35: #ifdef GSSAPI
36: #include "ssh-gss.h"
37: #endif
1.38 provos 38: #include "monitor_wrap.h"
1.1 markus 39:
40: /* import */
41: extern ServerOptions options;
42:
1.60 djm 43: static int auth1_process_password(Authctxt *, char *, size_t);
44: static int auth1_process_rsa(Authctxt *, char *, size_t);
45: static int auth1_process_rhosts_rsa(Authctxt *, char *, size_t);
46: static int auth1_process_tis_challenge(Authctxt *, char *, size_t);
47: static int auth1_process_tis_response(Authctxt *, char *, size_t);
48:
49: struct AuthMethod1 {
50: int type;
51: char *name;
52: int *enabled;
53: int (*method)(Authctxt *, char *, size_t);
54: };
55:
56: const struct AuthMethod1 auth1_methods[] = {
57: {
58: SSH_CMSG_AUTH_PASSWORD, "password",
59: &options.password_authentication, auth1_process_password
60: },
61: {
62: SSH_CMSG_AUTH_RSA, "rsa",
63: &options.rsa_authentication, auth1_process_rsa
64: },
65: {
66: SSH_CMSG_AUTH_RHOSTS_RSA, "rhosts-rsa",
67: &options.rhosts_rsa_authentication, auth1_process_rhosts_rsa
68: },
69: {
70: SSH_CMSG_AUTH_TIS, "challenge-response",
71: &options.challenge_response_authentication,
72: auth1_process_tis_challenge
73: },
74: {
75: SSH_CMSG_AUTH_TIS_RESPONSE, "challenge-response",
76: &options.challenge_response_authentication,
77: auth1_process_tis_response
78: },
79: { -1, NULL, NULL, NULL}
80: };
81:
82: static const struct AuthMethod1
83: *lookup_authmethod1(int type)
84: {
85: int i;
86:
1.64 deraadt 87: for (i = 0; auth1_methods[i].name != NULL; i++)
1.60 djm 88: if (auth1_methods[i].type == type)
89: return (&(auth1_methods[i]));
90:
91: return (NULL);
92: }
93:
1.24 itojun 94: static char *
1.1 markus 95: get_authname(int type)
96: {
1.60 djm 97: const struct AuthMethod1 *a;
98: static char buf[64];
99:
100: if ((a = lookup_authmethod1(type)) != NULL)
101: return (a->name);
102: snprintf(buf, sizeof(buf), "bad-auth-msg-%d", type);
103: return (buf);
104: }
105:
1.65 deraadt 106: /*ARGSUSED*/
1.60 djm 107: static int
108: auth1_process_password(Authctxt *authctxt, char *info, size_t infolen)
109: {
110: int authenticated = 0;
111: char *password;
112: u_int dlen;
113:
114: /*
115: * Read user password. It is in plain text, but was
116: * transmitted over the encrypted channel so it is
117: * not visible to an outside observer.
118: */
119: password = packet_get_string(&dlen);
120: packet_check_eom();
121:
122: /* Try authentication with the password. */
123: authenticated = PRIVSEP(auth_password(authctxt, password));
124:
125: memset(password, 0, dlen);
1.78 ! djm 126: free(password);
1.60 djm 127:
128: return (authenticated);
129: }
130:
1.65 deraadt 131: /*ARGSUSED*/
1.60 djm 132: static int
133: auth1_process_rsa(Authctxt *authctxt, char *info, size_t infolen)
134: {
135: int authenticated = 0;
136: BIGNUM *n;
137:
138: /* RSA authentication requested. */
139: if ((n = BN_new()) == NULL)
140: fatal("do_authloop: BN_new failed");
141: packet_get_bignum(n);
142: packet_check_eom();
143: authenticated = auth_rsa(authctxt, n);
144: BN_clear_free(n);
145:
146: return (authenticated);
147: }
148:
1.65 deraadt 149: /*ARGSUSED*/
1.60 djm 150: static int
151: auth1_process_rhosts_rsa(Authctxt *authctxt, char *info, size_t infolen)
152: {
1.61 djm 153: int keybits, authenticated = 0;
1.60 djm 154: u_int bits;
155: char *client_user;
156: Key *client_host_key;
157: u_int ulen;
158:
159: /*
160: * Get client user name. Note that we just have to
161: * trust the client; root on the client machine can
162: * claim to be any user.
163: */
1.75 djm 164: client_user = packet_get_cstring(&ulen);
1.60 djm 165:
166: /* Get the client host key. */
167: client_host_key = key_new(KEY_RSA1);
168: bits = packet_get_int();
169: packet_get_bignum(client_host_key->rsa->e);
170: packet_get_bignum(client_host_key->rsa->n);
171:
1.61 djm 172: keybits = BN_num_bits(client_host_key->rsa->n);
173: if (keybits < 0 || bits != (u_int)keybits) {
1.60 djm 174: verbose("Warning: keysize mismatch for client_host_key: "
175: "actual %d, announced %d",
176: BN_num_bits(client_host_key->rsa->n), bits);
1.1 markus 177: }
1.60 djm 178: packet_check_eom();
179:
180: authenticated = auth_rhosts_rsa(authctxt, client_user,
181: client_host_key);
182: key_free(client_host_key);
183:
184: snprintf(info, infolen, " ruser %.100s", client_user);
1.78 ! djm 185: free(client_user);
1.62 djm 186:
1.60 djm 187: return (authenticated);
188: }
189:
1.65 deraadt 190: /*ARGSUSED*/
1.60 djm 191: static int
192: auth1_process_tis_challenge(Authctxt *authctxt, char *info, size_t infolen)
193: {
194: char *challenge;
1.62 djm 195:
1.60 djm 196: if ((challenge = get_challenge(authctxt)) == NULL)
197: return (0);
198:
199: debug("sending challenge '%s'", challenge);
200: packet_start(SSH_SMSG_AUTH_TIS_CHALLENGE);
201: packet_put_cstring(challenge);
1.78 ! djm 202: free(challenge);
1.60 djm 203: packet_send();
204: packet_write_wait();
205:
206: return (-1);
207: }
208:
1.65 deraadt 209: /*ARGSUSED*/
1.60 djm 210: static int
211: auth1_process_tis_response(Authctxt *authctxt, char *info, size_t infolen)
212: {
213: int authenticated = 0;
214: char *response;
215: u_int dlen;
216:
217: response = packet_get_string(&dlen);
218: packet_check_eom();
219: authenticated = verify_response(authctxt, response);
220: memset(response, 'r', dlen);
1.78 ! djm 221: free(response);
1.60 djm 222:
223: return (authenticated);
1.1 markus 224: }
225:
226: /*
1.11 markus 227: * read packets, try to authenticate the user and
228: * return only if authentication is successful
1.1 markus 229: */
1.24 itojun 230: static void
1.11 markus 231: do_authloop(Authctxt *authctxt)
1.1 markus 232: {
1.5 markus 233: int authenticated = 0;
1.11 markus 234: char info[1024];
1.1 markus 235: int type = 0;
1.60 djm 236: const struct AuthMethod1 *meth;
1.11 markus 237:
238: debug("Attempting authentication for %s%.100s.",
1.59 markus 239: authctxt->valid ? "" : "invalid user ", authctxt->user);
1.11 markus 240:
241: /* If the user has no password, accept authentication immediately. */
1.74 djm 242: if (options.permit_empty_passwd && options.password_authentication &&
1.49 markus 243: #ifdef KRB5
1.11 markus 244: (!options.kerberos_authentication || options.kerberos_or_local_passwd) &&
245: #endif
1.38 provos 246: PRIVSEP(auth_password(authctxt, ""))) {
1.77 djm 247: auth_log(authctxt, 1, 0, "without authentication", NULL, "");
1.11 markus 248: return;
249: }
1.27 deraadt 250:
1.1 markus 251: /* Indicate that authentication is needed. */
252: packet_start(SSH_SMSG_FAILURE);
253: packet_send();
254: packet_write_wait();
255:
1.11 markus 256: for (;;) {
1.5 markus 257: /* default to fail */
258: authenticated = 0;
259:
1.11 markus 260: info[0] = '\0';
1.1 markus 261:
262: /* Get a packet from the client. */
1.34 markus 263: type = packet_read();
1.73 djm 264: if (authctxt->failures >= options.max_authtries)
265: goto skip;
1.60 djm 266: if ((meth = lookup_authmethod1(type)) == NULL) {
267: logit("Unknown message during authentication: "
268: "type %d", type);
269: goto skip;
270: }
1.1 markus 271:
1.60 djm 272: if (!*(meth->enabled)) {
273: verbose("%s authentication disabled.", meth->name);
274: goto skip;
1.1 markus 275: }
1.60 djm 276:
277: authenticated = meth->method(authctxt, info, sizeof(info));
278: if (authenticated == -1)
279: continue; /* "postponed" */
280:
1.20 markus 281: if (authctxt->as) {
282: auth_close(authctxt->as);
283: authctxt->as = NULL;
284: }
1.11 markus 285: if (!authctxt->valid && authenticated)
286: fatal("INTERNAL ERROR: authenticated invalid user %s",
287: authctxt->user);
288:
289: /* Special handling for root */
1.47 markus 290: if (authenticated && authctxt->pw->pw_uid == 0 &&
1.60 djm 291: !auth_root_allowed(meth->name))
1.11 markus 292: authenticated = 0;
1.1 markus 293:
1.60 djm 294: skip:
1.11 markus 295: /* Log before sending the reply */
1.77 djm 296: auth_log(authctxt, authenticated, 0, get_authname(type),
297: NULL, info);
1.1 markus 298:
299: if (authenticated)
300: return;
301:
1.73 djm 302: if (++authctxt->failures >= options.max_authtries)
1.11 markus 303: packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
1.1 markus 304:
305: packet_start(SSH_SMSG_FAILURE);
306: packet_send();
307: packet_write_wait();
308: }
309: }
310:
311: /*
312: * Performs authentication of an incoming connection. Session key has already
313: * been exchanged and encryption is enabled.
314: */
1.53 markus 315: void
316: do_authentication(Authctxt *authctxt)
1.1 markus 317: {
1.8 markus 318: u_int ulen;
1.40 markus 319: char *user, *style = NULL;
1.1 markus 320:
321: /* Get the name of the user that we wish to log in as. */
1.34 markus 322: packet_read_expect(SSH_CMSG_USER);
1.1 markus 323:
324: /* Get the user name. */
1.75 djm 325: user = packet_get_cstring(&ulen);
1.32 markus 326: packet_check_eom();
1.1 markus 327:
1.11 markus 328: if ((style = strchr(user, ':')) != NULL)
1.25 dugsong 329: *style++ = '\0';
1.27 deraadt 330:
1.11 markus 331: authctxt->user = user;
332: authctxt->style = style;
333:
1.1 markus 334: /* Verify that the user is a valid user. */
1.39 markus 335: if ((authctxt->pw = PRIVSEP(getpwnamallow(user))) != NULL)
1.11 markus 336: authctxt->valid = 1;
1.51 markus 337: else {
1.59 markus 338: debug("do_authentication: invalid user %s", user);
1.51 markus 339: authctxt->pw = fakepw();
340: }
1.76 djm 341:
342: /* Configuration may have changed as a result of Match */
343: if (options.num_auth_methods != 0)
344: fatal("AuthenticationMethods is not supported with SSH "
345: "protocol 1");
1.17 markus 346:
1.58 djm 347: setproctitle("%s%s", authctxt->valid ? user : "unknown",
1.38 provos 348: use_privsep ? " [net]" : "");
1.1 markus 349:
350: /*
351: * If we are not running as root, the user must have the same uid as
352: * the server.
353: */
1.39 markus 354: if (!use_privsep && getuid() != 0 && authctxt->pw &&
355: authctxt->pw->pw_uid != getuid())
1.1 markus 356: packet_disconnect("Cannot change user when server not running as root.");
357:
1.11 markus 358: /*
359: * Loop until the user has been authenticated or the connection is
360: * closed, do_authloop() returns only if authentication is successful
361: */
362: do_authloop(authctxt);
1.1 markus 363:
364: /* The user has been authenticated and accepted. */
365: packet_start(SSH_SMSG_SUCCESS);
366: packet_send();
367: packet_write_wait();
368: }