2 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
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".
13 RCSID("$OpenBSD: auth1.c,v 1.14 2001/01/22 23:06:39 markus Exp $");
33 extern ServerOptions options;
34 extern char *forced_command;
36 #ifdef WITH_AIXAUTHENTICATE
37 extern char *aixloginmsg;
38 #endif /* WITH_AIXAUTHENTICATE */
40 extern int saved_argc;
41 extern char **saved_argv;
42 #endif /* HAVE_OSF_SIA */
45 * convert ssh auth msg type into description
48 get_authname(int type)
50 static char buf[1024];
52 case SSH_CMSG_AUTH_PASSWORD:
54 case SSH_CMSG_AUTH_RSA:
56 case SSH_CMSG_AUTH_RHOSTS_RSA:
58 case SSH_CMSG_AUTH_RHOSTS:
60 case SSH_CMSG_AUTH_TIS:
61 case SSH_CMSG_AUTH_TIS_RESPONSE:
62 return "challenge-response";
64 case SSH_CMSG_AUTH_KERBEROS:
68 snprintf(buf, sizeof buf, "bad-auth-msg-%d", type);
73 * read packets, try to authenticate the user and
74 * return only if authentication is successful
77 do_authloop(Authctxt *authctxt)
79 int authenticated = 0;
83 char *client_user, *password;
89 struct passwd *pw = authctxt->pw;
91 debug("Attempting authentication for %s%.100s.",
92 authctxt->valid ? "" : "illegal user ", authctxt->user);
94 /* If the user has no password, accept authentication immediately. */
95 if (options.password_authentication &&
97 (!options.kerberos_authentication || options.kerberos_or_local_passwd) &&
100 auth_pam_password(pw, password)) {
102 auth_password(pw, "")) {
104 auth_log(authctxt, 1, "without authentication", "");
108 /* Indicate that authentication is needed. */
109 packet_start(SSH_SMSG_FAILURE);
116 /* default to fail */
121 /* Get a packet from the client. */
122 type = packet_read(&plen);
124 /* Process the packet. */
127 case SSH_CMSG_HAVE_KERBEROS_TGT:
128 if (!options.kerberos_tgt_passing) {
129 verbose("Kerberos tgt passing disabled.");
132 /* Accept Kerberos tgt. */
133 char *tgt = packet_get_string(&dlen);
134 packet_integrity_check(plen, 4 + dlen, type);
135 if (!auth_kerberos_tgt(pw, tgt))
136 verbose("Kerberos tgt REFUSED for %.100s", authctxt->user);
141 case SSH_CMSG_HAVE_AFS_TOKEN:
142 if (!options.afs_token_passing || !k_hasafs()) {
143 verbose("AFS token passing disabled.");
146 /* Accept AFS token. */
147 char *token_string = packet_get_string(&dlen);
148 packet_integrity_check(plen, 4 + dlen, type);
149 if (!auth_afs_token(pw, token_string))
150 verbose("AFS token REFUSED for %.100s", authctxt->user);
156 case SSH_CMSG_AUTH_KERBEROS:
157 if (!options.kerberos_authentication) {
158 verbose("Kerberos authentication disabled.");
161 /* Try Kerberos v4 authentication. */
163 char *tkt_user = NULL;
164 char *kdata = packet_get_string((u_int *) &auth.length);
165 packet_integrity_check(plen, 4 + auth.length, type);
167 if (authctxt->valid) {
168 if (auth.length < MAX_KTXT_LEN)
169 memcpy(auth.dat, kdata, auth.length);
170 authenticated = auth_krb4(pw->pw_name, &auth, &tkt_user);
172 snprintf(info, sizeof info,
173 " tktuser %.100s", tkt_user);
182 case SSH_CMSG_AUTH_RHOSTS:
183 if (!options.rhosts_authentication) {
184 verbose("Rhosts authentication disabled.");
188 * Get client user name. Note that we just have to
189 * trust the client; this is one reason why rhosts
190 * authentication is insecure. (Another is
191 * IP-spoofing on a local network.)
193 client_user = packet_get_string(&ulen);
194 packet_integrity_check(plen, 4 + ulen, type);
196 /* Try to authenticate using /etc/hosts.equiv and .rhosts. */
197 authenticated = auth_rhosts(pw, client_user);
199 snprintf(info, sizeof info, " ruser %.100s", client_user);
202 case SSH_CMSG_AUTH_RHOSTS_RSA:
203 if (!options.rhosts_rsa_authentication) {
204 verbose("Rhosts with RSA authentication disabled.");
208 * Get client user name. Note that we just have to
209 * trust the client; root on the client machine can
210 * claim to be any user.
212 client_user = packet_get_string(&ulen);
214 /* Get the client host key. */
215 client_host_key = RSA_new();
216 if (client_host_key == NULL)
217 fatal("RSA_new failed");
218 client_host_key->e = BN_new();
219 client_host_key->n = BN_new();
220 if (client_host_key->e == NULL || client_host_key->n == NULL)
221 fatal("BN_new failed");
222 bits = packet_get_int();
223 packet_get_bignum(client_host_key->e, &elen);
224 packet_get_bignum(client_host_key->n, &nlen);
226 if (bits != BN_num_bits(client_host_key->n))
227 verbose("Warning: keysize mismatch for client_host_key: "
228 "actual %d, announced %d", BN_num_bits(client_host_key->n), bits);
229 packet_integrity_check(plen, (4 + ulen) + 4 + elen + nlen, type);
231 authenticated = auth_rhosts_rsa(pw, client_user, client_host_key);
232 RSA_free(client_host_key);
234 snprintf(info, sizeof info, " ruser %.100s", client_user);
237 case SSH_CMSG_AUTH_RSA:
238 if (!options.rsa_authentication) {
239 verbose("RSA authentication disabled.");
242 /* RSA authentication requested. */
244 packet_get_bignum(n, &nlen);
245 packet_integrity_check(plen, nlen, type);
246 authenticated = auth_rsa(pw, n);
250 case SSH_CMSG_AUTH_PASSWORD:
251 if (!options.password_authentication) {
252 verbose("Password authentication disabled.");
256 * Read user password. It is in plain text, but was
257 * transmitted over the encrypted channel so it is
258 * not visible to an outside observer.
260 password = packet_get_string(&dlen);
261 packet_integrity_check(plen, 4 + dlen, type);
264 /* Do PAM auth with password */
265 authenticated = auth_pam_password(pw, password);
266 #elif defined(HAVE_OSF_SIA)
267 /* Do SIA auth with password */
268 if (sia_validate_user(NULL, saved_argc, saved_argv,
269 get_canonical_hostname(options.reverse_mapping_check),
270 pw->pw_name, NULL, 0, NULL, password) == SIASUCCESS) {
273 #else /* !USE_PAM && !HAVE_OSF_SIA */
274 /* Try authentication with the password. */
275 authenticated = auth_password(pw, password);
278 memset(password, 0, strlen(password));
282 case SSH_CMSG_AUTH_TIS:
283 debug("rcvd SSH_CMSG_AUTH_TIS");
284 if (options.challenge_reponse_authentication == 1) {
285 char *challenge = get_challenge(authctxt, authctxt->style);
286 if (challenge != NULL) {
287 debug("sending challenge '%s'", challenge);
288 packet_start(SSH_SMSG_AUTH_TIS_CHALLENGE);
289 packet_put_cstring(challenge);
296 case SSH_CMSG_AUTH_TIS_RESPONSE:
297 debug("rcvd SSH_CMSG_AUTH_TIS_RESPONSE");
298 if (options.challenge_reponse_authentication == 1) {
299 char *response = packet_get_string(&dlen);
300 debug("got response '%s'", response);
301 packet_integrity_check(plen, 4 + dlen, type);
302 authenticated = verify_response(authctxt, response);
303 memset(response, 'r', dlen);
310 * Any unknown messages will be ignored (and failure
311 * returned) during authentication.
313 log("Unknown message during authentication: type %d", type);
316 if (!authctxt->valid && authenticated)
317 fatal("INTERNAL ERROR: authenticated invalid user %s",
322 !check_nt_auth(type == SSH_CMSG_AUTH_PASSWORD,pw->pw_uid)) {
323 packet_disconnect("Authentication rejected for uid %d.",
328 /* Special handling for root */
329 if (authenticated && authctxt->pw->pw_uid == 0 && !auth_root_allowed())
333 if (authenticated && !do_pam_account(pw->pw_name, client_user))
337 /* Log before sending the reply */
338 auth_log(authctxt, authenticated, get_authname(type), info);
340 if (client_user != NULL) {
348 if (authctxt->failures++ > AUTH_FAIL_MAX) {
349 #ifdef WITH_AIXAUTHENTICATE
350 loginfailed(authctxt->user,
351 get_canonical_hostname(options.reverse_mapping_check),
353 #endif /* WITH_AIXAUTHENTICATE */
354 packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
357 packet_start(SSH_SMSG_FAILURE);
364 * Performs authentication of an incoming connection. Session key has already
365 * been exchanged and encryption is enabled.
374 char *user, *style = NULL;
376 /* Get the name of the user that we wish to log in as. */
377 packet_read_expect(&plen, SSH_CMSG_USER);
379 /* Get the user name. */
380 user = packet_get_string(&ulen);
381 packet_integrity_check(plen, (4 + ulen), SSH_CMSG_USER);
383 if ((style = strchr(user, ':')) != NULL)
386 authctxt = authctxt_new();
387 authctxt->user = user;
388 authctxt->style = style;
390 setproctitle("%s", user);
393 /* If machine has AFS, set process authentication group. */
400 /* Verify that the user is a valid user. */
402 if (pw && allowed_user(pw)) {
406 debug("do_authentication: illegal user %s", user);
417 * If we are not running as root, the user must have the same uid as
418 * the server. (Unless you are running Windows)
421 if (getuid() != 0 && pw && pw->pw_uid != getuid())
422 packet_disconnect("Cannot change user when server not running as root.");
426 * Loop until the user has been authenticated or the connection is
427 * closed, do_authloop() returns only if authentication is successful
429 do_authloop(authctxt);
431 /* The user has been authenticated and accepted. */
432 packet_start(SSH_SMSG_SUCCESS);
436 #ifdef WITH_AIXAUTHENTICATE
437 /* We don't have a pty yet, so just label the line as "ssh" */
438 if (loginsuccess(authctxt->user,
439 get_canonical_hostname(options.reverse_mapping_check),
440 "ssh", &aixloginmsg) < 0)
442 #endif /* WITH_AIXAUTHENTICATE */
444 xfree(authctxt->user);
447 /* Perform session preparation. */
448 do_authenticated(pw);