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.18 2001/02/22 21:59:43 markus Exp $");
29 extern ServerOptions options;
30 extern char *forced_command;
32 #ifdef WITH_AIXAUTHENTICATE
33 extern char *aixloginmsg;
34 #endif /* WITH_AIXAUTHENTICATE */
37 * convert ssh auth msg type into description
40 get_authname(int type)
42 static char buf[1024];
44 case SSH_CMSG_AUTH_PASSWORD:
46 case SSH_CMSG_AUTH_RSA:
48 case SSH_CMSG_AUTH_RHOSTS_RSA:
50 case SSH_CMSG_AUTH_RHOSTS:
52 case SSH_CMSG_AUTH_TIS:
53 case SSH_CMSG_AUTH_TIS_RESPONSE:
54 return "challenge-response";
56 case SSH_CMSG_AUTH_KERBEROS:
60 snprintf(buf, sizeof buf, "bad-auth-msg-%d", type);
65 * read packets, try to authenticate the user and
66 * return only if authentication is successful
69 do_authloop(Authctxt *authctxt)
71 int authenticated = 0;
75 char *client_user, *password;
81 struct passwd *pw = authctxt->pw;
83 debug("Attempting authentication for %s%.100s.",
84 authctxt->valid ? "" : "illegal user ", authctxt->user);
86 /* If the user has no password, accept authentication immediately. */
87 if (options.password_authentication &&
89 (!options.kerberos_authentication || options.kerberos_or_local_passwd) &&
92 auth_pam_password(pw, "")) {
93 #elif defined(HAVE_OSF_SIA)
96 auth_password(authctxt, "")) {
98 auth_log(authctxt, 1, "without authentication", "");
102 /* Indicate that authentication is needed. */
103 packet_start(SSH_SMSG_FAILURE);
110 /* default to fail */
115 /* Get a packet from the client. */
116 type = packet_read(&plen);
118 /* Process the packet. */
121 case SSH_CMSG_HAVE_KERBEROS_TGT:
122 if (!options.kerberos_tgt_passing) {
123 verbose("Kerberos tgt passing disabled.");
126 /* Accept Kerberos tgt. */
127 char *tgt = packet_get_string(&dlen);
128 packet_integrity_check(plen, 4 + dlen, type);
129 if (!auth_kerberos_tgt(pw, tgt))
130 verbose("Kerberos tgt REFUSED for %.100s", authctxt->user);
135 case SSH_CMSG_HAVE_AFS_TOKEN:
136 if (!options.afs_token_passing || !k_hasafs()) {
137 verbose("AFS token passing disabled.");
140 /* Accept AFS token. */
141 char *token_string = packet_get_string(&dlen);
142 packet_integrity_check(plen, 4 + dlen, type);
143 if (!auth_afs_token(pw, token_string))
144 verbose("AFS token REFUSED for %.100s", authctxt->user);
150 case SSH_CMSG_AUTH_KERBEROS:
151 if (!options.kerberos_authentication) {
152 verbose("Kerberos authentication disabled.");
155 /* Try Kerberos v4 authentication. */
157 char *tkt_user = NULL;
158 char *kdata = packet_get_string((u_int *) &auth.length);
159 packet_integrity_check(plen, 4 + auth.length, type);
161 if (authctxt->valid) {
162 if (auth.length < MAX_KTXT_LEN)
163 memcpy(auth.dat, kdata, auth.length);
164 authenticated = auth_krb4(pw->pw_name, &auth, &tkt_user);
166 snprintf(info, sizeof info,
167 " tktuser %.100s", tkt_user);
176 case SSH_CMSG_AUTH_RHOSTS:
177 if (!options.rhosts_authentication) {
178 verbose("Rhosts authentication disabled.");
182 * Get client user name. Note that we just have to
183 * trust the client; this is one reason why rhosts
184 * authentication is insecure. (Another is
185 * IP-spoofing on a local network.)
187 client_user = packet_get_string(&ulen);
188 packet_integrity_check(plen, 4 + ulen, type);
190 /* Try to authenticate using /etc/hosts.equiv and .rhosts. */
191 authenticated = auth_rhosts(pw, client_user);
193 snprintf(info, sizeof info, " ruser %.100s", client_user);
196 case SSH_CMSG_AUTH_RHOSTS_RSA:
197 if (!options.rhosts_rsa_authentication) {
198 verbose("Rhosts with RSA authentication disabled.");
202 * Get client user name. Note that we just have to
203 * trust the client; root on the client machine can
204 * claim to be any user.
206 client_user = packet_get_string(&ulen);
208 /* Get the client host key. */
209 client_host_key = RSA_new();
210 if (client_host_key == NULL)
211 fatal("RSA_new failed");
212 client_host_key->e = BN_new();
213 client_host_key->n = BN_new();
214 if (client_host_key->e == NULL || client_host_key->n == NULL)
215 fatal("BN_new failed");
216 bits = packet_get_int();
217 packet_get_bignum(client_host_key->e, &elen);
218 packet_get_bignum(client_host_key->n, &nlen);
220 if (bits != BN_num_bits(client_host_key->n))
221 verbose("Warning: keysize mismatch for client_host_key: "
222 "actual %d, announced %d", BN_num_bits(client_host_key->n), bits);
223 packet_integrity_check(plen, (4 + ulen) + 4 + elen + nlen, type);
225 authenticated = auth_rhosts_rsa(pw, client_user, client_host_key);
226 RSA_free(client_host_key);
228 snprintf(info, sizeof info, " ruser %.100s", client_user);
231 case SSH_CMSG_AUTH_RSA:
232 if (!options.rsa_authentication) {
233 verbose("RSA authentication disabled.");
236 /* RSA authentication requested. */
238 packet_get_bignum(n, &nlen);
239 packet_integrity_check(plen, nlen, type);
240 authenticated = auth_rsa(pw, n);
244 case SSH_CMSG_AUTH_PASSWORD:
245 if (!options.password_authentication) {
246 verbose("Password authentication disabled.");
250 * Read user password. It is in plain text, but was
251 * transmitted over the encrypted channel so it is
252 * not visible to an outside observer.
254 password = packet_get_string(&dlen);
255 packet_integrity_check(plen, 4 + dlen, type);
258 /* Do PAM auth with password */
259 authenticated = auth_pam_password(pw, password);
260 #elif defined(HAVE_OSF_SIA)
261 /* Do SIA auth with password */
262 authenticated = auth_sia_password(authctxt->user,
264 #else /* !USE_PAM && !HAVE_OSF_SIA */
265 /* Try authentication with the password. */
266 authenticated = auth_password(authctxt, password);
269 memset(password, 0, strlen(password));
273 case SSH_CMSG_AUTH_TIS:
274 debug("rcvd SSH_CMSG_AUTH_TIS");
275 if (options.challenge_reponse_authentication == 1) {
276 char *challenge = get_challenge(authctxt, authctxt->style);
277 if (challenge != NULL) {
278 debug("sending challenge '%s'", challenge);
279 packet_start(SSH_SMSG_AUTH_TIS_CHALLENGE);
280 packet_put_cstring(challenge);
287 case SSH_CMSG_AUTH_TIS_RESPONSE:
288 debug("rcvd SSH_CMSG_AUTH_TIS_RESPONSE");
289 if (options.challenge_reponse_authentication == 1) {
290 char *response = packet_get_string(&dlen);
291 debug("got response '%s'", response);
292 packet_integrity_check(plen, 4 + dlen, type);
293 authenticated = verify_response(authctxt, response);
294 memset(response, 'r', dlen);
301 * Any unknown messages will be ignored (and failure
302 * returned) during authentication.
304 log("Unknown message during authentication: type %d", type);
309 auth_close(authctxt->as);
313 if (!authctxt->valid && authenticated)
314 fatal("INTERNAL ERROR: authenticated invalid user %s",
319 !check_nt_auth(type == SSH_CMSG_AUTH_PASSWORD,pw->pw_uid)) {
320 packet_disconnect("Authentication rejected for uid %d.",
325 /* Special handling for root */
326 if (authenticated && authctxt->pw->pw_uid == 0 &&
327 !auth_root_allowed(get_authname(type)))
331 if (authenticated && !do_pam_account(pw->pw_name, client_user))
335 /* Log before sending the reply */
336 auth_log(authctxt, authenticated, get_authname(type), info);
338 if (client_user != NULL) {
346 if (authctxt->failures++ > AUTH_FAIL_MAX) {
347 #ifdef WITH_AIXAUTHENTICATE
348 loginfailed(authctxt->user,
349 get_canonical_hostname(options.reverse_mapping_check),
351 #endif /* WITH_AIXAUTHENTICATE */
352 packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
355 packet_start(SSH_SMSG_FAILURE);
362 * Performs authentication of an incoming connection. Session key has already
363 * been exchanged and encryption is enabled.
372 char *user, *style = NULL;
374 /* Get the name of the user that we wish to log in as. */
375 packet_read_expect(&plen, SSH_CMSG_USER);
377 /* Get the user name. */
378 user = packet_get_string(&ulen);
379 packet_integrity_check(plen, (4 + ulen), SSH_CMSG_USER);
381 if ((style = strchr(user, ':')) != NULL)
384 authctxt = authctxt_new();
385 authctxt->user = user;
386 authctxt->style = style;
388 /* Verify that the user is a valid user. */
390 if (pw && allowed_user(pw)) {
394 debug("do_authentication: illegal user %s", user);
399 setproctitle("%s", pw ? user : "unknown");
407 * If we are not running as root, the user must have the same uid as
408 * the server. (Unless you are running Windows)
411 if (getuid() != 0 && pw && pw->pw_uid != getuid())
412 packet_disconnect("Cannot change user when server not running as root.");
416 * Loop until the user has been authenticated or the connection is
417 * closed, do_authloop() returns only if authentication is successful
419 do_authloop(authctxt);
421 /* The user has been authenticated and accepted. */
422 packet_start(SSH_SMSG_SUCCESS);
426 #ifdef WITH_AIXAUTHENTICATE
427 /* We don't have a pty yet, so just label the line as "ssh" */
428 if (loginsuccess(authctxt->user,
429 get_canonical_hostname(options.reverse_mapping_check),
430 "ssh", &aixloginmsg) < 0)
432 #endif /* WITH_AIXAUTHENTICATE */
434 xfree(authctxt->user);
437 /* Perform session preparation. */
438 do_authenticated(pw);