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