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.11 2001/01/18 16:59:59 markus Exp $");
32 extern ServerOptions options;
33 extern char *forced_command;
35 #ifdef WITH_AIXAUTHENTICATE
36 extern char *aixloginmsg;
37 #endif /* WITH_AIXAUTHENTICATE */
39 extern int saved_argc;
40 extern char **saved_argv;
41 #endif /* HAVE_OSF_SIA */
44 * convert ssh auth msg type into description
47 get_authname(int type)
49 static char buf[1024];
51 case SSH_CMSG_AUTH_PASSWORD:
53 case SSH_CMSG_AUTH_RSA:
55 case SSH_CMSG_AUTH_RHOSTS_RSA:
57 case SSH_CMSG_AUTH_RHOSTS:
59 case SSH_CMSG_AUTH_TIS:
60 case SSH_CMSG_AUTH_TIS_RESPONSE:
61 return "challenge-response";
63 case SSH_CMSG_AUTH_KERBEROS:
67 snprintf(buf, sizeof buf, "bad-auth-msg-%d", type);
72 * read packets, try to authenticate the user and
73 * return only if authentication is successful
76 do_authloop(Authctxt *authctxt)
78 int authenticated = 0;
82 char *client_user, *password;
88 struct passwd *pw = authctxt->pw;
90 debug("Attempting authentication for %s%.100s.",
91 authctxt->valid ? "" : "illegal user ", authctxt->user);
93 /* If the user has no password, accept authentication immediately. */
94 if (options.password_authentication &&
96 (!options.kerberos_authentication || options.kerberos_or_local_passwd) &&
99 auth_pam_password(pw, password)) {
101 auth_password(pw, "")) {
103 auth_log(authctxt, 1, "without authentication", "");
107 /* Indicate that authentication is needed. */
108 packet_start(SSH_SMSG_FAILURE);
115 /* default to fail */
120 /* Get a packet from the client. */
121 type = packet_read(&plen);
123 /* Process the packet. */
126 case SSH_CMSG_HAVE_KERBEROS_TGT:
127 if (!options.kerberos_tgt_passing) {
128 verbose("Kerberos tgt passing disabled.");
131 /* Accept Kerberos tgt. */
132 char *tgt = packet_get_string(&dlen);
133 packet_integrity_check(plen, 4 + dlen, type);
134 if (!auth_kerberos_tgt(pw, tgt))
135 verbose("Kerberos tgt REFUSED for %.100s", authctxt->user);
140 case SSH_CMSG_HAVE_AFS_TOKEN:
141 if (!options.afs_token_passing || !k_hasafs()) {
142 verbose("AFS token passing disabled.");
145 /* Accept AFS token. */
146 char *token_string = packet_get_string(&dlen);
147 packet_integrity_check(plen, 4 + dlen, type);
148 if (!auth_afs_token(pw, token_string))
149 verbose("AFS token REFUSED for %.100s", authctxt->user);
155 case SSH_CMSG_AUTH_KERBEROS:
156 if (!options.kerberos_authentication) {
157 verbose("Kerberos authentication disabled.");
160 /* Try Kerberos v4 authentication. */
162 char *tkt_user = NULL;
163 char *kdata = packet_get_string((u_int *) &auth.length);
164 packet_integrity_check(plen, 4 + auth.length, type);
166 if (authctxt->valid) {
167 if (auth.length < MAX_KTXT_LEN)
168 memcpy(auth.dat, kdata, auth.length);
169 authenticated = auth_krb4(pw->pw_name, &auth, &tkt_user);
171 snprintf(info, sizeof info,
172 " tktuser %.100s", tkt_user);
181 case SSH_CMSG_AUTH_RHOSTS:
182 if (!options.rhosts_authentication) {
183 verbose("Rhosts authentication disabled.");
187 * Get client user name. Note that we just have to
188 * trust the client; this is one reason why rhosts
189 * authentication is insecure. (Another is
190 * IP-spoofing on a local network.)
192 client_user = packet_get_string(&ulen);
193 packet_integrity_check(plen, 4 + ulen, type);
195 /* Try to authenticate using /etc/hosts.equiv and .rhosts. */
196 authenticated = auth_rhosts(pw, client_user);
198 snprintf(info, sizeof info, " ruser %.100s", client_user);
201 case SSH_CMSG_AUTH_RHOSTS_RSA:
202 if (!options.rhosts_rsa_authentication) {
203 verbose("Rhosts with RSA authentication disabled.");
207 * Get client user name. Note that we just have to
208 * trust the client; root on the client machine can
209 * claim to be any user.
211 client_user = packet_get_string(&ulen);
213 /* Get the client host key. */
214 client_host_key = RSA_new();
215 if (client_host_key == NULL)
216 fatal("RSA_new failed");
217 client_host_key->e = BN_new();
218 client_host_key->n = BN_new();
219 if (client_host_key->e == NULL || client_host_key->n == NULL)
220 fatal("BN_new failed");
221 bits = packet_get_int();
222 packet_get_bignum(client_host_key->e, &elen);
223 packet_get_bignum(client_host_key->n, &nlen);
225 if (bits != BN_num_bits(client_host_key->n))
226 verbose("Warning: keysize mismatch for client_host_key: "
227 "actual %d, announced %d", BN_num_bits(client_host_key->n), bits);
228 packet_integrity_check(plen, (4 + ulen) + 4 + elen + nlen, type);
230 authenticated = auth_rhosts_rsa(pw, client_user, client_host_key);
231 RSA_free(client_host_key);
233 snprintf(info, sizeof info, " ruser %.100s", client_user);
236 case SSH_CMSG_AUTH_RSA:
237 if (!options.rsa_authentication) {
238 verbose("RSA authentication disabled.");
241 /* RSA authentication requested. */
243 packet_get_bignum(n, &nlen);
244 packet_integrity_check(plen, nlen, type);
245 authenticated = auth_rsa(pw, n);
249 case SSH_CMSG_AUTH_PASSWORD:
250 if (!options.password_authentication) {
251 verbose("Password authentication disabled.");
255 * Read user password. It is in plain text, but was
256 * transmitted over the encrypted channel so it is
257 * not visible to an outside observer.
259 password = packet_get_string(&dlen);
260 packet_integrity_check(plen, 4 + dlen, type);
263 /* Do PAM auth with password */
264 authenticated = auth_pam_password(pw, password);
265 #elif defined(HAVE_OSF_SIA)
266 /* Do SIA auth with password */
267 if (sia_validate_user(NULL, saved_argc, saved_argv,
268 get_canonical_hostname(), pw->pw_name, NULL, 0,
269 NULL, password) == SIASUCCESS) {
272 #else /* !USE_PAM && !HAVE_OSF_SIA */
273 /* Try authentication with the password. */
274 authenticated = auth_password(pw, password);
277 memset(password, 0, strlen(password));
281 #ifdef SKEY /* ISSUE: Is this right? we don't define
282 having skey_authentication in
283 servconf.h by default so I assume
284 we need to deal with this via #ifdef
285 in some reasonable way */
286 case SSH_CMSG_AUTH_TIS:
287 debug("rcvd SSH_CMSG_AUTH_TIS");
288 if (options.skey_authentication == 1) {
289 char *challenge = get_challenge(authctxt, authctxt->style);
290 if (challenge != NULL) {
291 debug("sending challenge '%s'", challenge);
292 packet_start(SSH_SMSG_AUTH_TIS_CHALLENGE);
293 packet_put_cstring(challenge);
300 case SSH_CMSG_AUTH_TIS_RESPONSE:
301 debug("rcvd SSH_CMSG_AUTH_TIS_RESPONSE");
302 if (options.skey_authentication == 1) {
303 char *response = packet_get_string(&dlen);
304 debug("got response '%s'", response);
305 packet_integrity_check(plen, 4 + dlen, type);
306 authenticated = verify_response(authctxt, response);
307 memset(response, 'r', dlen);
311 #endif /* ISSUE: End of wrong SKEY defines */
315 * Any unknown messages will be ignored (and failure
316 * returned) during authentication.
318 log("Unknown message during authentication: type %d", type);
321 if (!authctxt->valid && authenticated)
322 fatal("INTERNAL ERROR: authenticated invalid user %s",
325 #ifdef HAVE_CYGWIN /* ISSUE: Right place? */
327 !check_nt_auth(type == SSH_CMSG_AUTH_PASSWORD,pw->pw_uid)) {
328 packet_disconnect("Authentication rejected for uid %d.",
334 /* Special handling for root */
335 if (authenticated && authctxt->pw->pw_uid == 0 && !auth_root_allowed())
338 #ifdef USE_PAM /* ISSUE: Right place? */
339 if (authenticated && !do_pam_account(pw->pw_name, client_user))
343 /* Log before sending the reply */
344 auth_log(authctxt, authenticated, get_authname(type), info);
346 if (client_user != NULL) {
354 if (authctxt->failures++ > AUTH_FAIL_MAX) {
355 #ifdef WITH_AIXAUTHENTICATE
356 loginfailed(user,get_canonical_hostname(),"ssh");
357 #endif /* WITH_AIXAUTHENTICATE */
358 packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
361 packet_start(SSH_SMSG_FAILURE);
368 * Performs authentication of an incoming connection. Session key has already
369 * been exchanged and encryption is enabled.
378 char *user, *style = NULL;
380 /* Get the name of the user that we wish to log in as. */
381 packet_read_expect(&plen, SSH_CMSG_USER);
383 /* Get the user name. */
384 user = packet_get_string(&ulen);
385 packet_integrity_check(plen, (4 + ulen), SSH_CMSG_USER);
387 if ((style = strchr(user, ':')) != NULL)
390 authctxt = authctxt_new();
391 authctxt->user = user;
392 authctxt->style = style;
394 setproctitle("%s", user);
397 /* If machine has AFS, set process authentication group. */
404 /* Verify that the user is a valid user. */
406 if (pw && allowed_user(pw)) {
410 debug("do_authentication: illegal user %s", user);
421 * If we are not running as root, the user must have the same uid as
422 * the server. (Unless you are running Windows)
425 if (getuid() != 0 && pw && pw->pw_uid != getuid())
426 packet_disconnect("Cannot change user when server not running as root.");
430 * Loop until the user has been authenticated or the connection is
431 * closed, do_authloop() returns only if authentication is successful
433 do_authloop(authctxt);
435 /* The user has been authenticated and accepted. */
436 packet_start(SSH_SMSG_SUCCESS);
440 #ifdef WITH_AIXAUTHENTICATE
441 /* We don't have a pty yet, so just label the line as "ssh" */
442 if (loginsuccess(authctxt->user,get_canonical_hostname(),"ssh",&aixloginmsg) < 0)
444 #endif /* WITH_AIXAUTHENTICATE */
446 xfree(authctxt->user);
449 /* Perform session preparation. */
450 do_authenticated(pw);