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