Additional ssh patches relating to the same fatal() cleanup issue. They
[dragonfly.git] / crypto / openssh / auth1.c
1 /*
2  * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
3  *                    All rights reserved
4  *
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".
10  */
11
12 #include "includes.h"
13 RCSID("$OpenBSD: auth1.c,v 1.44 2002/09/26 11:38:43 markus Exp $");
14 RCSID("$FreeBSD: src/crypto/openssh/auth1.c,v 1.3.2.10 2003/04/07 09:56:46 des Exp $");
15 RCSID("$DragonFly: src/crypto/openssh/Attic/auth1.c,v 1.2 2003/06/17 04:24:36 dillon Exp $");
16
17 #include "xmalloc.h"
18 #include "rsa.h"
19 #include "ssh1.h"
20 #include "packet.h"
21 #include "buffer.h"
22 #include "mpaux.h"
23 #include "log.h"
24 #include "servconf.h"
25 #include "compat.h"
26 #include "auth.h"
27 #include "channels.h"
28 #include "session.h"
29 #include "uidswap.h"
30 #include "monitor_wrap.h"
31
32 /* import */
33 extern ServerOptions options;
34
35 /*
36  * convert ssh auth msg type into description
37  */
38 static char *
39 get_authname(int type)
40 {
41         static char buf[1024];
42         switch (type) {
43         case SSH_CMSG_AUTH_PASSWORD:
44                 return "password";
45         case SSH_CMSG_AUTH_RSA:
46                 return "rsa";
47         case SSH_CMSG_AUTH_RHOSTS_RSA:
48                 return "rhosts-rsa";
49         case SSH_CMSG_AUTH_RHOSTS:
50                 return "rhosts";
51         case SSH_CMSG_AUTH_TIS:
52         case SSH_CMSG_AUTH_TIS_RESPONSE:
53                 return "challenge-response";
54 #if defined(KRB4) || defined(KRB5)
55         case SSH_CMSG_AUTH_KERBEROS:
56                 return "kerberos";
57 #endif
58         }
59         snprintf(buf, sizeof buf, "bad-auth-msg-%d", type);
60         return buf;
61 }
62
63 /*
64  * read packets, try to authenticate the user and
65  * return only if authentication is successful
66  */
67 static void
68 do_authloop(Authctxt *authctxt)
69 {
70         int authenticated = 0;
71         u_int bits;
72         Key *client_host_key;
73         BIGNUM *n;
74         char *client_user, *password;
75         char info[1024];
76         u_int dlen;
77         u_int ulen;
78         int prev, type = 0;
79         struct passwd *pw = authctxt->pw;
80
81         debug("Attempting authentication for %s%.100s.",
82             authctxt->valid ? "" : "illegal user ", authctxt->user);
83
84         /* If the user has no password, accept authentication immediately. */
85         if (options.password_authentication &&
86 #if defined(KRB4) || defined(KRB5)
87             (!options.kerberos_authentication || options.kerberos_or_local_passwd) &&
88 #endif
89             PRIVSEP(auth_password(authctxt, ""))) {
90                 auth_log(authctxt, 1, "without authentication", "");
91                 return;
92         }
93
94         /* Indicate that authentication is needed. */
95         packet_start(SSH_SMSG_FAILURE);
96         packet_send();
97         packet_write_wait();
98
99         client_user = NULL;
100
101         for (;;) {
102                 /* default to fail */
103                 authenticated = 0;
104
105                 info[0] = '\0';
106
107                 /* Get a packet from the client. */
108                 prev = type;
109                 type = packet_read();
110
111                 /*
112                  * If we started challenge-response authentication but the
113                  * next packet is not a response to our challenge, release
114                  * the resources allocated by get_challenge() (which would
115                  * normally have been released by verify_response() had we
116                  * received such a response)
117                  */
118                 if (prev == SSH_CMSG_AUTH_TIS &&
119                     type != SSH_CMSG_AUTH_TIS_RESPONSE)
120                         abandon_challenge_response(authctxt);
121
122                 /* Process the packet. */
123                 switch (type) {
124
125 #if defined(KRB4) || defined(KRB5)
126                 case SSH_CMSG_AUTH_KERBEROS:
127                         if (!options.kerberos_authentication) {
128                                 verbose("Kerberos authentication disabled.");
129                         } else {
130                                 char *kdata = packet_get_string(&dlen);
131                                 packet_check_eom();
132
133                                 if (kdata[0] == 4) { /* KRB_PROT_VERSION */
134 #ifdef KRB4
135                                         KTEXT_ST tkt, reply;
136                                         tkt.length = dlen;
137                                         if (tkt.length < MAX_KTXT_LEN)
138                                                 memcpy(tkt.dat, kdata, tkt.length);
139
140                                         if (PRIVSEP(auth_krb4(authctxt, &tkt,
141                                             &client_user, &reply))) {
142                                                 authenticated = 1;
143                                                 snprintf(info, sizeof(info),
144                                                     " tktuser %.100s",
145                                                     client_user);
146
147                                                 packet_start(
148                                                     SSH_SMSG_AUTH_KERBEROS_RESPONSE);
149                                                 packet_put_string((char *)
150                                                     reply.dat, reply.length);
151                                                 packet_send();
152                                                 packet_write_wait();
153                                         }
154 #endif /* KRB4 */
155                                 } else {
156 #ifdef KRB5
157                                         krb5_data tkt, reply;
158                                         tkt.length = dlen;
159                                         tkt.data = kdata;
160
161                                         if (PRIVSEP(auth_krb5(authctxt, &tkt,
162                                             &client_user, &reply))) {
163                                                 authenticated = 1;
164                                                 snprintf(info, sizeof(info),
165                                                     " tktuser %.100s",
166                                                     client_user);
167  
168                                                 /* Send response to client */
169                                                 packet_start(
170                                                     SSH_SMSG_AUTH_KERBEROS_RESPONSE);
171                                                 packet_put_string((char *)
172                                                     reply.data, reply.length);
173                                                 packet_send();
174                                                 packet_write_wait();
175
176                                                 if (reply.length)
177                                                         xfree(reply.data);
178                                         }
179 #endif /* KRB5 */
180                                 }
181                                 xfree(kdata);
182                         }
183                         break;
184 #endif /* KRB4 || KRB5 */
185
186 #if defined(AFS) || defined(KRB5)
187                         /* XXX - punt on backward compatibility here. */
188                 case SSH_CMSG_HAVE_KERBEROS_TGT:
189                         packet_send_debug("Kerberos TGT passing disabled before authentication.");
190                         break;
191 #ifdef AFS
192                 case SSH_CMSG_HAVE_AFS_TOKEN:
193                         packet_send_debug("AFS token passing disabled before authentication.");
194                         break;
195 #endif /* AFS */
196 #endif /* AFS || KRB5 */
197
198                 case SSH_CMSG_AUTH_RHOSTS:
199                         if (!options.rhosts_authentication) {
200                                 verbose("Rhosts authentication disabled.");
201                                 break;
202                         }
203                         /*
204                          * Get client user name.  Note that we just have to
205                          * trust the client; this is one reason why rhosts
206                          * authentication is insecure. (Another is
207                          * IP-spoofing on a local network.)
208                          */
209                         client_user = packet_get_string(&ulen);
210                         packet_check_eom();
211
212                         /* Try to authenticate using /etc/hosts.equiv and .rhosts. */
213                         authenticated = auth_rhosts(pw, client_user);
214
215                         snprintf(info, sizeof info, " ruser %.100s", client_user);
216                         break;
217
218                 case SSH_CMSG_AUTH_RHOSTS_RSA:
219                         if (!options.rhosts_rsa_authentication) {
220                                 verbose("Rhosts with RSA authentication disabled.");
221                                 break;
222                         }
223                         /*
224                          * Get client user name.  Note that we just have to
225                          * trust the client; root on the client machine can
226                          * claim to be any user.
227                          */
228                         client_user = packet_get_string(&ulen);
229
230                         /* Get the client host key. */
231                         client_host_key = key_new(KEY_RSA1);
232                         bits = packet_get_int();
233                         packet_get_bignum(client_host_key->rsa->e);
234                         packet_get_bignum(client_host_key->rsa->n);
235
236                         if (bits != BN_num_bits(client_host_key->rsa->n))
237                                 verbose("Warning: keysize mismatch for client_host_key: "
238                                     "actual %d, announced %d",
239                                     BN_num_bits(client_host_key->rsa->n), bits);
240                         packet_check_eom();
241
242                         authenticated = auth_rhosts_rsa(pw, client_user,
243                             client_host_key);
244                         key_free(client_host_key);
245
246                         snprintf(info, sizeof info, " ruser %.100s", client_user);
247                         break;
248
249                 case SSH_CMSG_AUTH_RSA:
250                         if (!options.rsa_authentication) {
251                                 verbose("RSA authentication disabled.");
252                                 break;
253                         }
254                         /* RSA authentication requested. */
255                         if ((n = BN_new()) == NULL)
256                                 fatal("do_authloop: BN_new failed");
257                         packet_get_bignum(n);
258                         packet_check_eom();
259                         authenticated = auth_rsa(pw, n);
260                         BN_clear_free(n);
261                         break;
262
263                 case SSH_CMSG_AUTH_PASSWORD:
264                         if (!options.password_authentication) {
265                                 verbose("Password authentication disabled.");
266                                 break;
267                         }
268                         /*
269                          * Read user password.  It is in plain text, but was
270                          * transmitted over the encrypted channel so it is
271                          * not visible to an outside observer.
272                          */
273                         password = packet_get_string(&dlen);
274                         packet_check_eom();
275
276                         /* Try authentication with the password. */
277                         authenticated = PRIVSEP(auth_password(authctxt, password));
278
279                         memset(password, 0, strlen(password));
280                         xfree(password);
281                         break;
282
283                 case SSH_CMSG_AUTH_TIS:
284                         debug("rcvd SSH_CMSG_AUTH_TIS");
285                         if (options.challenge_response_authentication == 1) {
286                                 char *challenge = get_challenge(authctxt);
287                                 if (challenge != NULL) {
288                                         debug("sending challenge '%s'", challenge);
289                                         packet_start(SSH_SMSG_AUTH_TIS_CHALLENGE);
290                                         packet_put_cstring(challenge);
291                                         xfree(challenge);
292                                         packet_send();
293                                         packet_write_wait();
294                                         continue;
295                                 }
296                         }
297                         break;
298                 case SSH_CMSG_AUTH_TIS_RESPONSE:
299                         debug("rcvd SSH_CMSG_AUTH_TIS_RESPONSE");
300                         if (options.challenge_response_authentication == 1) {
301                                 char *response = packet_get_string(&dlen);
302                                 debug("got response '%s'", response);
303                                 packet_check_eom();
304                                 authenticated = verify_response(authctxt, response);
305                                 memset(response, 'r', dlen);
306                                 xfree(response);
307                         }
308                         break;
309
310                 default:
311                         /*
312                          * Any unknown messages will be ignored (and failure
313                          * returned) during authentication.
314                          */
315                         log("Unknown message during authentication: type %d", type);
316                         break;
317                 }
318 #ifdef BSD_AUTH
319                 if (authctxt->as) {
320                         auth_close(authctxt->as);
321                         authctxt->as = NULL;
322                 }
323 #endif
324                 if (!authctxt->valid && authenticated)
325                         fatal("INTERNAL ERROR: authenticated invalid user %s",
326                             authctxt->user);
327
328 #ifdef _UNICOS
329                 if (type == SSH_CMSG_AUTH_PASSWORD && !authenticated)
330                         cray_login_failure(authctxt->user, IA_UDBERR);
331                 if (authenticated && cray_access_denied(authctxt->user)) {
332                         authenticated = 0;
333                         fatal("Access denied for user %s.",authctxt->user);
334                 }
335 #endif /* _UNICOS */
336
337 #ifdef HAVE_CYGWIN
338                 if (authenticated &&
339                     !check_nt_auth(type == SSH_CMSG_AUTH_PASSWORD, pw)) {
340                         packet_disconnect("Authentication rejected for uid %d.",
341                         pw == NULL ? -1 : pw->pw_uid);
342                         authenticated = 0;
343                 }
344 #else
345                 /* Special handling for root */
346                 if (!use_privsep &&
347                     authenticated && authctxt->pw->pw_uid == 0 &&
348                     !auth_root_allowed(get_authname(type)))
349                         authenticated = 0;
350 #endif
351 #ifdef USE_PAM
352                 if (!use_privsep && authenticated && 
353                     !do_pam_account(pw->pw_name, client_user))
354                         authenticated = 0;
355 #endif
356
357                 /* Log before sending the reply */
358                 auth_log(authctxt, authenticated, get_authname(type), info);
359
360                 if (client_user != NULL) {
361                         xfree(client_user);
362                         client_user = NULL;
363                 }
364
365                 if (authenticated)
366                         return;
367
368                 if (authctxt->failures++ > AUTH_FAIL_MAX) {
369                         packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
370                 }
371
372                 packet_start(SSH_SMSG_FAILURE);
373                 packet_send();
374                 packet_write_wait();
375         }
376 }
377
378 /*
379  * Performs authentication of an incoming connection.  Session key has already
380  * been exchanged and encryption is enabled.
381  */
382 Authctxt *
383 do_authentication(void)
384 {
385         Authctxt *authctxt;
386         u_int ulen;
387         char *user, *style = NULL;
388
389         /* Get the name of the user that we wish to log in as. */
390         packet_read_expect(SSH_CMSG_USER);
391
392         /* Get the user name. */
393         user = packet_get_string(&ulen);
394         packet_check_eom();
395
396         if ((style = strchr(user, ':')) != NULL)
397                 *style++ = '\0';
398
399 #ifdef KRB5
400         /* XXX - SSH.com Kerberos v5 braindeath. */
401         if ((datafellows & SSH_BUG_K5USER) &&
402             options.kerberos_authentication) {
403                 char *p;
404                 if ((p = strchr(user, '@')) != NULL)
405                         *p = '\0';
406         }
407 #endif
408
409         authctxt = authctxt_new();
410         authctxt->user = user;
411         authctxt->style = style;
412
413         /* Verify that the user is a valid user. */
414         if ((authctxt->pw = PRIVSEP(getpwnamallow(user))) != NULL)
415                 authctxt->valid = 1;
416         else
417                 debug("do_authentication: illegal user %s", user);
418
419         setproctitle("%s%s", authctxt->pw ? user : "unknown",
420             use_privsep ? " [net]" : "");
421
422 #ifdef USE_PAM
423         PRIVSEP(start_pam(authctxt->pw == NULL ? "NOUSER" : user));
424 #endif
425
426         /*
427          * If we are not running as root, the user must have the same uid as
428          * the server. (Unless you are running Windows)
429          */
430 #ifndef HAVE_CYGWIN
431         if (!use_privsep && getuid() != 0 && authctxt->pw &&
432             authctxt->pw->pw_uid != getuid())
433                 packet_disconnect("Cannot change user when server not running as root.");
434 #endif
435
436         /*
437          * Loop until the user has been authenticated or the connection is
438          * closed, do_authloop() returns only if authentication is successful
439          */
440         do_authloop(authctxt);
441
442         /* The user has been authenticated and accepted. */
443         packet_start(SSH_SMSG_SUCCESS);
444         packet_send();
445         packet_write_wait();
446
447         return (authctxt);
448 }