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