]> andersk Git - gssapi-openssh.git/blob - openssh/auth1.c
rather than use a different GSSAPI auth function (auth_gssapi in augh-gssapi.c)
[gssapi-openssh.git] / 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.35 2002/02/03 17:53:25 markus Exp $");
14
15 #include "xmalloc.h"
16 #include "rsa.h"
17 #include "ssh1.h"
18 #include "dispatch.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 "misc.h"
29 #include "uidswap.h"
30
31 #ifdef GSSAPI
32 int     userauth_gssapi(Authctxt *authctxt);
33 char *  gssapi_parse_userstring(char *userstring);
34 #endif
35
36 /* import */
37 extern ServerOptions options;
38
39 #ifdef GSSAPI
40 void
41 auth1_gss_protocol_error(int type, u_int32_t plen, void *ctxt)
42 {
43   Authctxt *authctxt = ctxt;
44   /* Other side told us to abort, dont need to tell him */ 
45   /* maybe we can us some other method. */
46   if (type == SSH_MSG_AUTH_GSSAPI_ABORT) {
47       log("auth1: GSSAPI aborting");
48       dispatch_set(SSH_MSG_AUTH_GSSAPI_TOKEN, NULL);
49       authctxt->success = 1; /* get out of loop*/
50       return;
51   }
52
53   log("auth1: protocol error: type %d plen %d", type, plen);
54   packet_disconnect("Protocol error during GSSAPI authentication: "
55           "Unknown packet type %d", type);
56 }
57 #endif
58
59 /*
60  * convert ssh auth msg type into description
61  */
62 static char *
63 get_authname(int type)
64 {
65         static char buf[1024];
66         switch (type) {
67         case SSH_CMSG_AUTH_PASSWORD:
68                 return "password";
69         case SSH_CMSG_AUTH_RSA:
70                 return "rsa";
71         case SSH_CMSG_AUTH_RHOSTS_RSA:
72                 return "rhosts-rsa";
73         case SSH_CMSG_AUTH_RHOSTS:
74                 return "rhosts";
75         case SSH_CMSG_AUTH_TIS:
76         case SSH_CMSG_AUTH_TIS_RESPONSE:
77                 return "challenge-response";
78 #if defined(KRB4) || defined(KRB5)
79         case SSH_CMSG_AUTH_KERBEROS:
80                 return "kerberos";
81 #endif
82 #if defined(GSSAPI)
83         case SSH_CMSG_AUTH_GSSAPI:
84                 return "gssapi";
85 #endif
86         }
87         snprintf(buf, sizeof buf, "bad-auth-msg-%d", type);
88         return buf;
89 }
90
91 /*
92  * read packets, try to authenticate the user and
93  * return only if authentication is successful
94  */
95 static void
96 do_authloop(Authctxt *authctxt)
97 {
98         int authenticated = 0;
99         u_int bits;
100         Key *client_host_key;
101         BIGNUM *n;
102         char *client_user, *password;
103         char info[1024];
104         u_int dlen;
105         u_int ulen;
106         int type = 0;
107         struct passwd *pw = authctxt->pw;
108
109         debug("Attempting authentication for %s%.100s.",
110             authctxt->valid ? "" : "illegal user ", authctxt->user);
111
112         /* If the user has no password, accept authentication immediately. */
113         if (options.password_authentication &&
114 #if defined(KRB4) || defined(KRB5)
115             (!options.kerberos_authentication || options.kerberos_or_local_passwd) &&
116 #endif
117 #ifdef USE_PAM
118             auth_pam_password(pw, "")) {
119 #elif defined(HAVE_OSF_SIA)
120             0) {
121 #else
122             auth_password(authctxt, "")) {
123 #endif
124                 auth_log(authctxt, 1, "without authentication", "");
125                 return;
126         }
127
128         /* Indicate that authentication is needed. */
129         packet_start(SSH_SMSG_FAILURE);
130         packet_send();
131         packet_write_wait();
132
133         client_user = NULL;
134
135         for (;;) {
136                 /* default to fail */
137                 authenticated = 0;
138
139                 info[0] = '\0';
140
141                 /* Get a packet from the client. */
142                 type = packet_read();
143
144                 /* Process the packet. */
145                 switch (type) {
146
147 #if defined(KRB4) || defined(KRB5)
148                 case SSH_CMSG_AUTH_KERBEROS:
149                         if (!options.kerberos_authentication) {
150                                 verbose("Kerberos authentication disabled.");
151                         } else {
152                                 char *kdata = packet_get_string(&dlen);
153                                 packet_check_eom();
154
155                                 if (kdata[0] == 4) { /* KRB_PROT_VERSION */
156 #ifdef KRB4
157                                         KTEXT_ST tkt;
158
159                                         tkt.length = dlen;
160                                         if (tkt.length < MAX_KTXT_LEN)
161                                                 memcpy(tkt.dat, kdata, tkt.length);
162
163                                         if (auth_krb4(authctxt, &tkt, &client_user)) {
164                                                 authenticated = 1;
165                                                 snprintf(info, sizeof(info),
166                                                     " tktuser %.100s",
167                                                     client_user);
168                                         }
169 #endif /* KRB4 */
170                                 } else {
171 #ifdef KRB5
172                                         krb5_data tkt;
173                                         tkt.length = dlen;
174                                         tkt.data = kdata;
175
176                                         if (auth_krb5(authctxt, &tkt, &client_user)) {
177                                                 authenticated = 1;
178                                                 snprintf(info, sizeof(info),
179                                                     " tktuser %.100s",
180                                                     client_user);
181                                         }
182 #endif /* KRB5 */
183                                 }
184                                 xfree(kdata);
185                         }
186                         break;
187 #endif /* KRB4 || KRB5 */
188
189 #ifdef GSSAPI
190                 case SSH_CMSG_AUTH_GSSAPI:
191                         if (!options.gss_authentication) {
192                                 verbose("GSSAPI authentication disabled.");
193                                 break;
194                         }
195                         /*
196                         * GSSAPI was first added to ssh1 in ssh-1.2.27, and
197                         * was added to the SecurtCRT product. In order
198                         * to continue operating with these, we will add
199                         * the equivelent GSSAPI support to SSH1. 
200                         * Will use the gssapi routines from the ssh2 as
201                         * they are almost identical. But they use dispatch
202                         * so we need to setup the dispatch tables here 
203                         * auth1.c for use only by the gssapi code. 
204                         * Since we already have the packet, we will call
205                         * userauth_gssapi then start the dispatch loop.
206                         */
207                         if (!authctxt->valid) {
208                         packet_disconnect("Authentication rejected for invalid user");
209                         }
210                         dispatch_init(&auth1_gss_protocol_error);
211                         userauth_gssapi(authctxt);
212                         dispatch_run(DISPATCH_BLOCK, &authctxt->success, authctxt);
213                         if (authctxt->postponed) { /* failed, try other methods */
214                                 authctxt->success = 0;
215                                 authctxt->postponed = 0;
216                                 break;
217                         }
218                         do_authenticated(authctxt);
219                         /* will only return if authenticated */
220                         authenticated = 1;
221                         break;
222 #endif /* GSSAPI */
223                         
224 #if defined(AFS) || defined(KRB5)
225                         /* XXX - punt on backward compatibility here. */
226                 case SSH_CMSG_HAVE_KERBEROS_TGT:
227                         packet_send_debug("Kerberos TGT passing disabled before authentication.");
228                         break;
229 #ifdef AFS
230                 case SSH_CMSG_HAVE_AFS_TOKEN:
231                         packet_send_debug("AFS token passing disabled before authentication.");
232                         break;
233 #endif /* AFS */
234 #endif /* AFS || KRB5 */
235
236                 case SSH_CMSG_AUTH_RHOSTS:
237                         if (!options.rhosts_authentication) {
238                                 verbose("Rhosts authentication disabled.");
239                                 break;
240                         }
241                         /*
242                          * Get client user name.  Note that we just have to
243                          * trust the client; this is one reason why rhosts
244                          * authentication is insecure. (Another is
245                          * IP-spoofing on a local network.)
246                          */
247                         client_user = packet_get_string(&ulen);
248                         packet_check_eom();
249
250                         /* Try to authenticate using /etc/hosts.equiv and .rhosts. */
251                         authenticated = auth_rhosts(pw, client_user);
252
253                         snprintf(info, sizeof info, " ruser %.100s", client_user);
254                         break;
255
256                 case SSH_CMSG_AUTH_RHOSTS_RSA:
257                         if (!options.rhosts_rsa_authentication) {
258                                 verbose("Rhosts with RSA authentication disabled.");
259                                 break;
260                         }
261                         /*
262                          * Get client user name.  Note that we just have to
263                          * trust the client; root on the client machine can
264                          * claim to be any user.
265                          */
266                         client_user = packet_get_string(&ulen);
267
268                         /* Get the client host key. */
269                         client_host_key = key_new(KEY_RSA1);
270                         bits = packet_get_int();
271                         packet_get_bignum(client_host_key->rsa->e);
272                         packet_get_bignum(client_host_key->rsa->n);
273
274                         if (bits != BN_num_bits(client_host_key->rsa->n))
275                                 verbose("Warning: keysize mismatch for client_host_key: "
276                                     "actual %d, announced %d",
277                                      BN_num_bits(client_host_key->rsa->n), bits);
278                         packet_check_eom();
279
280                         authenticated = auth_rhosts_rsa(pw, client_user,
281                             client_host_key);
282                         key_free(client_host_key);
283
284                         snprintf(info, sizeof info, " ruser %.100s", client_user);
285                         break;
286
287                 case SSH_CMSG_AUTH_RSA:
288                         if (!options.rsa_authentication) {
289                                 verbose("RSA authentication disabled.");
290                                 break;
291                         }
292                         /* RSA authentication requested. */
293                         if ((n = BN_new()) == NULL)
294                                 fatal("do_authloop: BN_new failed");
295                         packet_get_bignum(n);
296                         packet_check_eom();
297                         authenticated = auth_rsa(pw, n);
298                         BN_clear_free(n);
299                         break;
300
301                 case SSH_CMSG_AUTH_PASSWORD:
302                         if (!options.password_authentication) {
303                                 verbose("Password authentication disabled.");
304                                 break;
305                         }
306                         /*
307                          * Read user password.  It is in plain text, but was
308                          * transmitted over the encrypted channel so it is
309                          * not visible to an outside observer.
310                          */
311                         password = packet_get_string(&dlen);
312                         packet_check_eom();
313
314 #ifdef USE_PAM
315                         /* Do PAM auth with password */
316                         authenticated = auth_pam_password(pw, password);
317 #elif defined(HAVE_OSF_SIA)
318                         /* Do SIA auth with password */
319                         authenticated = auth_sia_password(authctxt->user, 
320                             password);
321 #else /* !USE_PAM && !HAVE_OSF_SIA */
322                         /* Try authentication with the password. */
323                         authenticated = auth_password(authctxt, password);
324 #endif /* USE_PAM */
325
326                         memset(password, 0, strlen(password));
327                         xfree(password);
328                         break;
329
330                 case SSH_CMSG_AUTH_TIS:
331                         debug("rcvd SSH_CMSG_AUTH_TIS");
332                         if (options.challenge_response_authentication == 1) {
333                                 char *challenge = get_challenge(authctxt);
334                                 if (challenge != NULL) {
335                                         debug("sending challenge '%s'", challenge);
336                                         packet_start(SSH_SMSG_AUTH_TIS_CHALLENGE);
337                                         packet_put_cstring(challenge);
338                                         xfree(challenge);
339                                         packet_send();
340                                         packet_write_wait();
341                                         continue;
342                                 }
343                         }
344                         break;
345                 case SSH_CMSG_AUTH_TIS_RESPONSE:
346                         debug("rcvd SSH_CMSG_AUTH_TIS_RESPONSE");
347                         if (options.challenge_response_authentication == 1) {
348                                 char *response = packet_get_string(&dlen);
349                                 debug("got response '%s'", response);
350                                 packet_check_eom();
351                                 authenticated = verify_response(authctxt, response);
352                                 memset(response, 'r', dlen);
353                                 xfree(response);
354                         }
355                         break;
356
357                 default:
358                         /*
359                          * Any unknown messages will be ignored (and failure
360                          * returned) during authentication.
361                          */
362                         log("Unknown message during authentication: type %d", type);
363                         break;
364                 }
365 #ifdef BSD_AUTH
366                 if (authctxt->as) {
367                         auth_close(authctxt->as);
368                         authctxt->as = NULL;
369                 }
370 #endif
371                 if (!authctxt->valid && authenticated)
372                         fatal("INTERNAL ERROR: authenticated invalid user %s",
373                             authctxt->user);
374
375 #ifdef HAVE_CYGWIN
376                 if (authenticated &&
377                     !check_nt_auth(type == SSH_CMSG_AUTH_PASSWORD, pw)) {
378                         packet_disconnect("Authentication rejected for uid %d.",
379                         pw == NULL ? -1 : pw->pw_uid);
380                         authenticated = 0;
381                 }
382 #else
383                 /* Special handling for root */
384                 if (authenticated && authctxt->pw->pw_uid == 0 &&
385                     !auth_root_allowed(get_authname(type)))
386                         authenticated = 0;
387 #endif
388 #ifdef USE_PAM
389                 if (authenticated && !do_pam_account(pw->pw_name, client_user))
390                         authenticated = 0;
391 #endif
392
393                 /* Log before sending the reply */
394                 auth_log(authctxt, authenticated, get_authname(type), info);
395
396                 if (client_user != NULL) {
397                         xfree(client_user);
398                         client_user = NULL;
399                 }
400
401                 if (authenticated)
402                         return;
403
404                 if (authctxt->failures++ > AUTH_FAIL_MAX) {
405 #ifdef WITH_AIXAUTHENTICATE
406                         loginfailed(authctxt->user,
407                             get_canonical_hostname(options.verify_reverse_mapping),
408                             "ssh");
409 #endif /* WITH_AIXAUTHENTICATE */
410                         packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
411                 }
412
413                 packet_start(SSH_SMSG_FAILURE);
414                 packet_send();
415                 packet_write_wait();
416         }
417 }
418
419 /*
420  * Performs authentication of an incoming connection.  Session key has already
421  * been exchanged and encryption is enabled.
422  */
423 void
424 do_authentication(void)
425 {
426         Authctxt *authctxt;
427         struct passwd *pw;
428         u_int ulen;
429         char *p, *user, *style = NULL;
430
431         /* Get the name of the user that we wish to log in as. */
432         packet_read_expect(SSH_CMSG_USER);
433
434         /* Get the user name. */
435         user = packet_get_string(&ulen);
436         packet_check_eom();
437
438 /*modified by binhe*/
439 #ifndef GSSAPI
440   /* GSSAPI clients may legitimately send long names (see below) */
441
442   if (strlen(user) > 255)
443     do_authentication_fail_loop();
444
445 #else /* GSSAPI */
446
447   /* Parse GSSAPI identity from userstring */
448   user = gssapi_parse_userstring(user);
449
450 #endif /* GSSAPI */
451 /*end of modification*/
452
453         if ((style = strchr(user, ':')) != NULL)
454                 *style++ = '\0';
455
456         /* XXX - SSH.com Kerberos v5 braindeath. */
457         if ((p = strchr(user, '@')) != NULL)
458                 *p = '\0';
459
460         authctxt = authctxt_new();
461         authctxt->user = user;
462         authctxt->style = style;
463
464         /* Verify that the user is a valid user. */
465         pw = getpwnam(user);
466         if (pw && allowed_user(pw)) {
467                 authctxt->valid = 1;
468                 pw = pwcopy(pw);
469         } else {
470                 debug("do_authentication: illegal user %s", user);
471                 pw = NULL;
472         }
473         authctxt->pw = pw;
474
475         setproctitle("%s", pw ? user : "unknown");
476
477 #ifdef USE_PAM
478         start_pam(pw == NULL ? "NOUSER" : user);
479 #endif
480
481         /*
482          * If we are not running as root, the user must have the same uid as
483          * the server. (Unless you are running Windows)
484          */
485 #ifndef HAVE_CYGWIN
486         if (getuid() != 0 && pw && pw->pw_uid != getuid())
487                 packet_disconnect("Cannot change user when server not running as root.");
488 #endif
489
490         /*
491          * Loop until the user has been authenticated or the connection is
492          * closed, do_authloop() returns only if authentication is successful
493          */
494         do_authloop(authctxt);
495
496         /* The user has been authenticated and accepted. */
497         packet_start(SSH_SMSG_SUCCESS);
498         packet_send();
499         packet_write_wait();
500
501         /* Perform session preparation. */
502         do_authenticated(authctxt);
503 }
This page took 0.559855 seconds and 5 git commands to generate.