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