]> andersk Git - gssapi-openssh.git/blob - openssh/auth1.c
merge of OPENSSH_3_6_1P1_SIMON_20030411
[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.47 2003/02/06 21:22:42 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 "uidswap.h"
29 #include "monitor_wrap.h"
30
31 /* import */
32 extern ServerOptions options;
33
34 #ifdef GSSAPI
35 #ifdef GSI
36 #include "globus_gss_assist.h"
37 #endif
38
39 extern Authmethod method_gssapi;
40
41 int     userauth_gssapi(Authctxt *authctxt);
42
43 void
44 auth1_gss_protocol_error(int type, u_int32_t plen, void *ctxt)
45 {
46   Authctxt *authctxt = ctxt;
47   /* Other side told us to abort, dont need to tell him */ 
48   /* maybe we can use some other method. */
49   if (type == SSH_MSG_AUTH_GSSAPI_ABORT) {
50       log("auth1: GSSAPI aborting");
51       dispatch_set(SSH_MSG_AUTH_GSSAPI_TOKEN, NULL);
52       authctxt->success = 1; /* get out of loop*/
53       return;
54   }
55
56   log("auth1: protocol error: type %d plen %d", type, plen);
57   packet_disconnect("Protocol error during GSSAPI authentication: "
58           "Unknown packet type %d", type);
59 }
60
61 #ifdef GSI
62 int
63 gsi_gridmap(char *subject_name, char **mapped_name)
64 {
65 #ifdef GLOBUS_GSI_GSS_ASSIST_MODULE
66     if (globus_module_activate(GLOBUS_GSI_GSS_ASSIST_MODULE) != 0) {
67         return 0;
68     }
69 #endif
70     return(globus_gss_assist_gridmap(subject_name, mapped_name) == 0);
71 }
72 #endif
73
74 /*
75  * SSH1 GSSAPI clients may send us a user name of the form:
76  *
77  *   (1) username:x:SSL Subject Name
78  *     or
79  *   (2) username:i:SSL Subject Name
80  *     or
81  *   (3) username
82  *
83  *  if case 1, then uname is an explicit name (ssh -l uname). Keep this
84  *  name always, rewrite the user parameter to be just uname. We'll pull
85  *  the GSSAPI idenity out and deal with (or skip it) later.
86  *  
87  *  if case 2, then uname is implicit (user didn't use the -l option), so
88  *  use the default gridmap mapping and replace uname with whatever
89  *  the gridmap maps to. If the gridmap mapping fails, drop down
90  *  to just uname
91  *  
92  *  if case 3, then leave it be.
93  *
94  *  This function may return the original pointer to the orginal string,
95  *  the original pointer to a modified string, or a completely new pointer.
96  */
97 static char *
98 ssh1_gssapi_parse_userstring(char *userstring)
99 {
100   char name_type = '\0';        /* explicit 'x' or implicit 'i' */
101   char *ssl_subject_name = NULL;
102   char *delim = NULL;
103
104   debug("Looking at username '%s' for gssapi-ssleay type name", userstring);
105   if((delim = strchr(userstring, ':')) != NULL) {
106       /* Parse and split into components */
107       ssl_subject_name = strchr(delim + 1, ':');
108
109       if (ssl_subject_name) {
110         /* Successful parse, split into components */
111         *delim = '\0';
112         name_type = *(delim + 1);
113         *ssl_subject_name = '\0';
114         ssl_subject_name++;
115
116         debug("Name parsed. type = '%c'. ssl subject name is \"%s\"",
117               name_type, ssl_subject_name);
118
119       } else {
120
121         debug("Don't understand name format. Letting it pass.");
122       } 
123   }     
124
125 #ifdef GSI
126   if(ssl_subject_name) {
127     char *gridmapped_name = NULL;
128     switch (name_type) {
129     case 'x':
130       debug("explicit name given, using %s as username", userstring);
131       break;
132
133     case 'i':
134       /* gridmap check */
135       debug("implicit name given. gridmapping '%s'", ssl_subject_name);
136
137       PRIVSEP(gsi_gridmap(ssl_subject_name, &gridmapped_name));
138       if (gridmapped_name && gridmapped_name[0] != '\0') {
139         userstring = gridmapped_name;
140         debug("I gridmapped and got %s", userstring);
141
142       } else {
143         debug("I gridmapped and got null, reverting to %s", userstring);
144       }
145       break;
146
147     default:
148       debug("Unknown name type '%c'. Ignoring.", name_type);
149       break;
150     }
151   } else {
152     debug("didn't find any :'s so I assume it's just a user name");
153   }
154 #endif /* GSI */
155
156   return userstring;
157 }
158 #endif
159
160 /*
161  * convert ssh auth msg type into description
162  */
163 static char *
164 get_authname(int type)
165 {
166         static char buf[1024];
167         switch (type) {
168         case SSH_CMSG_AUTH_PASSWORD:
169                 return "password";
170         case SSH_CMSG_AUTH_RSA:
171                 return "rsa";
172         case SSH_CMSG_AUTH_RHOSTS_RSA:
173                 return "rhosts-rsa";
174         case SSH_CMSG_AUTH_RHOSTS:
175                 return "rhosts";
176         case SSH_CMSG_AUTH_TIS:
177         case SSH_CMSG_AUTH_TIS_RESPONSE:
178                 return "challenge-response";
179 #if defined(KRB4) || defined(KRB5)
180         case SSH_CMSG_AUTH_KERBEROS:
181                 return "kerberos";
182 #endif
183 #if defined(GSSAPI)
184         case SSH_CMSG_AUTH_GSSAPI:
185                 return "gssapi";
186 #endif
187         }
188         snprintf(buf, sizeof buf, "bad-auth-msg-%d", type);
189         return buf;
190 }
191
192 /*
193  * read packets, try to authenticate the user and
194  * return only if authentication is successful
195  */
196 static void
197 do_authloop(Authctxt *authctxt)
198 {
199         int authenticated = 0;
200         u_int bits;
201         Key *client_host_key;
202         BIGNUM *n;
203         char *client_user, *password;
204         char info[1024];
205         u_int dlen;
206         u_int ulen;
207         int type = 0;
208         struct passwd *pw = authctxt->pw;
209
210         debug("Attempting authentication for %s%.100s.",
211             authctxt->valid ? "" : "illegal user ", authctxt->user);
212
213         /* If the user has no password, accept authentication immediately. */
214         if (options.password_authentication &&
215 #if defined(KRB4) || defined(KRB5)
216             (!options.kerberos_authentication || options.kerberos_or_local_passwd) &&
217 #endif
218             PRIVSEP(auth_password(authctxt, ""))) {
219                 auth_log(authctxt, 1, "without authentication", "");
220                 return;
221         }
222
223         /* Indicate that authentication is needed. */
224         packet_start(SSH_SMSG_FAILURE);
225         packet_send();
226         packet_write_wait();
227
228         client_user = NULL;
229
230         for (;;) {
231                 /* default to fail */
232                 authenticated = 0;
233
234                 info[0] = '\0';
235
236                 /* Get a packet from the client. */
237                 type = packet_read();
238
239                 /* Process the packet. */
240                 switch (type) {
241
242 #if defined(KRB4) || defined(KRB5)
243                 case SSH_CMSG_AUTH_KERBEROS:
244                         if (!options.kerberos_authentication) {
245                                 verbose("Kerberos authentication disabled.");
246                         } else {
247                                 char *kdata = packet_get_string(&dlen);
248                                 packet_check_eom();
249
250                                 if (kdata[0] == 4) { /* KRB_PROT_VERSION */
251 #ifdef KRB4
252                                         KTEXT_ST tkt, reply;
253                                         tkt.length = dlen;
254                                         if (tkt.length < MAX_KTXT_LEN)
255                                                 memcpy(tkt.dat, kdata, tkt.length);
256
257                                         if (PRIVSEP(auth_krb4(authctxt, &tkt,
258                                             &client_user, &reply))) {
259                                                 authenticated = 1;
260                                                 snprintf(info, sizeof(info),
261                                                     " tktuser %.100s",
262                                                     client_user);
263
264                                                 packet_start(
265                                                     SSH_SMSG_AUTH_KERBEROS_RESPONSE);
266                                                 packet_put_string((char *)
267                                                     reply.dat, reply.length);
268                                                 packet_send();
269                                                 packet_write_wait();
270                                         }
271 #endif /* KRB4 */
272                                 } else {
273 #ifdef KRB5
274                                         krb5_data tkt, reply;
275                                         tkt.length = dlen;
276                                         tkt.data = kdata;
277
278                                         if (PRIVSEP(auth_krb5(authctxt, &tkt,
279                                             &client_user, &reply))) {
280                                                 authenticated = 1;
281                                                 snprintf(info, sizeof(info),
282                                                     " tktuser %.100s",
283                                                     client_user);
284
285                                                 /* Send response to client */
286                                                 packet_start(
287                                                     SSH_SMSG_AUTH_KERBEROS_RESPONSE);
288                                                 packet_put_string((char *)
289                                                     reply.data, reply.length);
290                                                 packet_send();
291                                                 packet_write_wait();
292
293                                                 if (reply.length)
294                                                         xfree(reply.data);
295                                         }
296 #endif /* KRB5 */
297                                 }
298                                 xfree(kdata);
299                         }
300                         break;
301 #endif /* KRB4 || KRB5 */
302
303 #ifdef GSSAPI
304                 case SSH_CMSG_AUTH_GSSAPI:
305                         if (!options.gss_authentication) {
306                                 verbose("GSSAPI authentication disabled.");
307                                 break;
308                         }
309                         /*
310                         * GSSAPI was first added to ssh1 in ssh-1.2.27, and
311                         * was added to the SecurtCRT product. In order
312                         * to continue operating with these, we will add
313                         * the equivelent GSSAPI support to SSH1. 
314                         * Will use the gssapi routines from the ssh2 as
315                         * they are almost identical. But they use dispatch
316                         * so we need to setup the dispatch tables here 
317                         * auth1.c for use only by the gssapi code. 
318                         * Since we already have the packet, we will call
319                         * userauth_gssapi then start the dispatch loop.
320                         */
321                         if (!authctxt->valid) {
322                         packet_disconnect("Authentication rejected for invalid user");
323                         }
324                         dispatch_init(&auth1_gss_protocol_error);
325                         method_gssapi.userauth(authctxt);
326                         if (!authctxt->postponed) { /* failed before starting dispatch */
327                                 authctxt->success = 0;
328                                 authctxt->postponed = 0;
329                                 break;
330                         }
331                         dispatch_run(DISPATCH_BLOCK, &authctxt->success, authctxt);
332                         if (authctxt->postponed) { /* failed, try other methods */
333                                 authctxt->success = 0;
334                                 authctxt->postponed = 0;
335                                 break;
336                         }
337                         authenticated = 1;
338                         break;
339 #endif /* GSSAPI */
340                         
341 #if defined(AFS) || defined(KRB5)
342                         /* XXX - punt on backward compatibility here. */
343                 case SSH_CMSG_HAVE_KERBEROS_TGT:
344                         packet_send_debug("Kerberos TGT passing disabled before authentication.");
345                         break;
346 #ifdef AFS
347                 case SSH_CMSG_HAVE_AFS_TOKEN:
348                         packet_send_debug("AFS token passing disabled before authentication.");
349                         break;
350 #endif /* AFS */
351 #endif /* AFS || KRB5 */
352
353                 case SSH_CMSG_AUTH_RHOSTS:
354                         if (!options.rhosts_authentication) {
355                                 verbose("Rhosts authentication disabled.");
356                                 break;
357                         }
358                         /*
359                          * Get client user name.  Note that we just have to
360                          * trust the client; this is one reason why rhosts
361                          * authentication is insecure. (Another is
362                          * IP-spoofing on a local network.)
363                          */
364                         client_user = packet_get_string(&ulen);
365                         packet_check_eom();
366
367                         /* Try to authenticate using /etc/hosts.equiv and .rhosts. */
368                         authenticated = auth_rhosts(pw, client_user);
369
370                         snprintf(info, sizeof info, " ruser %.100s", client_user);
371                         break;
372
373                 case SSH_CMSG_AUTH_RHOSTS_RSA:
374                         if (!options.rhosts_rsa_authentication) {
375                                 verbose("Rhosts with RSA authentication disabled.");
376                                 break;
377                         }
378                         /*
379                          * Get client user name.  Note that we just have to
380                          * trust the client; root on the client machine can
381                          * claim to be any user.
382                          */
383                         client_user = packet_get_string(&ulen);
384
385                         /* Get the client host key. */
386                         client_host_key = key_new(KEY_RSA1);
387                         bits = packet_get_int();
388                         packet_get_bignum(client_host_key->rsa->e);
389                         packet_get_bignum(client_host_key->rsa->n);
390
391                         if (bits != BN_num_bits(client_host_key->rsa->n))
392                                 verbose("Warning: keysize mismatch for client_host_key: "
393                                     "actual %d, announced %d",
394                                     BN_num_bits(client_host_key->rsa->n), bits);
395                         packet_check_eom();
396
397                         authenticated = auth_rhosts_rsa(pw, client_user,
398                             client_host_key);
399                         key_free(client_host_key);
400
401                         snprintf(info, sizeof info, " ruser %.100s", client_user);
402                         break;
403
404                 case SSH_CMSG_AUTH_RSA:
405                         if (!options.rsa_authentication) {
406                                 verbose("RSA authentication disabled.");
407                                 break;
408                         }
409                         /* RSA authentication requested. */
410                         if ((n = BN_new()) == NULL)
411                                 fatal("do_authloop: BN_new failed");
412                         packet_get_bignum(n);
413                         packet_check_eom();
414                         authenticated = auth_rsa(pw, n);
415                         BN_clear_free(n);
416                         break;
417
418                 case SSH_CMSG_AUTH_PASSWORD:
419                         if (!options.password_authentication) {
420                                 verbose("Password authentication disabled.");
421                                 break;
422                         }
423                         /*
424                          * Read user password.  It is in plain text, but was
425                          * transmitted over the encrypted channel so it is
426                          * not visible to an outside observer.
427                          */
428                         password = packet_get_string(&dlen);
429                         packet_check_eom();
430
431                         /* Try authentication with the password. */
432                         authenticated = PRIVSEP(auth_password(authctxt, password));
433
434                         memset(password, 0, strlen(password));
435                         xfree(password);
436                         break;
437
438                 case SSH_CMSG_AUTH_TIS:
439                         debug("rcvd SSH_CMSG_AUTH_TIS");
440                         if (options.challenge_response_authentication == 1) {
441                                 char *challenge = get_challenge(authctxt);
442                                 if (challenge != NULL) {
443                                         debug("sending challenge '%s'", challenge);
444                                         packet_start(SSH_SMSG_AUTH_TIS_CHALLENGE);
445                                         packet_put_cstring(challenge);
446                                         xfree(challenge);
447                                         packet_send();
448                                         packet_write_wait();
449                                         continue;
450                                 }
451                         }
452                         break;
453                 case SSH_CMSG_AUTH_TIS_RESPONSE:
454                         debug("rcvd SSH_CMSG_AUTH_TIS_RESPONSE");
455                         if (options.challenge_response_authentication == 1) {
456                                 char *response = packet_get_string(&dlen);
457                                 packet_check_eom();
458                                 authenticated = verify_response(authctxt, response);
459                                 memset(response, 'r', dlen);
460                                 xfree(response);
461                         }
462                         break;
463
464                 default:
465                         /*
466                          * Any unknown messages will be ignored (and failure
467                          * returned) during authentication.
468                          */
469                         log("Unknown message during authentication: type %d", type);
470                         break;
471                 }
472 #ifdef BSD_AUTH
473                 if (authctxt->as) {
474                         auth_close(authctxt->as);
475                         authctxt->as = NULL;
476                 }
477 #endif
478                 if (!authctxt->valid && authenticated)
479                         fatal("INTERNAL ERROR: authenticated invalid user %s",
480                             authctxt->user);
481
482 #ifdef _UNICOS
483                 if (type == SSH_CMSG_AUTH_PASSWORD && !authenticated)
484                         cray_login_failure(authctxt->user, IA_UDBERR);
485                 if (authenticated && cray_access_denied(authctxt->user)) {
486                         authenticated = 0;
487                         fatal("Access denied for user %s.",authctxt->user);
488                 }
489 #endif /* _UNICOS */
490
491 #ifdef HAVE_CYGWIN
492                 if (authenticated &&
493                     !check_nt_auth(type == SSH_CMSG_AUTH_PASSWORD, pw)) {
494                         packet_disconnect("Authentication rejected for uid %d.",
495                         pw == NULL ? -1 : pw->pw_uid);
496                         authenticated = 0;
497                 }
498 #else
499                 /* Special handling for root */
500                 if (authenticated && authctxt->pw->pw_uid == 0 &&
501                     !auth_root_allowed(get_authname(type)))
502                         authenticated = 0;
503 #endif
504 #ifdef USE_PAM
505                 if (!use_privsep && authenticated && 
506                     !do_pam_account(pw->pw_name, client_user))
507                         authenticated = 0;
508 #endif
509
510                 /* Log before sending the reply */
511                 auth_log(authctxt, authenticated, get_authname(type), info);
512
513                 if (client_user != NULL) {
514                         xfree(client_user);
515                         client_user = NULL;
516                 }
517
518                 if (authenticated)
519                         return;
520
521                 if (authctxt->failures++ > AUTH_FAIL_MAX) {
522                         packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
523                 }
524
525                 packet_start(SSH_SMSG_FAILURE);
526                 packet_send();
527                 packet_write_wait();
528         }
529 }
530
531 /*
532  * Performs authentication of an incoming connection.  Session key has already
533  * been exchanged and encryption is enabled.
534  */
535 Authctxt *
536 do_authentication(void)
537 {
538         Authctxt *authctxt;
539         u_int ulen;
540         char *user, *style = NULL;
541
542         /* Get the name of the user that we wish to log in as. */
543         packet_read_expect(SSH_CMSG_USER);
544
545         /* Get the user name. */
546         user = packet_get_string(&ulen);
547         packet_check_eom();
548
549 #ifdef GSSAPI
550         /* Parse GSSAPI identity from userstring */
551         user = ssh1_gssapi_parse_userstring(user);
552 #endif /* GSSAPI */
553
554         if ((style = strchr(user, ':')) != NULL)
555                 *style++ = '\0';
556
557 #ifdef KRB5
558         /* XXX - SSH.com Kerberos v5 braindeath. */
559         if ((datafellows & SSH_BUG_K5USER) &&
560             options.kerberos_authentication) {
561                 char *p;
562                 if ((p = strchr(user, '@')) != NULL)
563                         *p = '\0';
564         }
565 #endif
566
567         authctxt = authctxt_new();
568         authctxt->user = user;
569         authctxt->style = style;
570
571         /* Verify that the user is a valid user. */
572         if ((authctxt->pw = PRIVSEP(getpwnamallow(user))) != NULL)
573                 authctxt->valid = 1;
574         else
575                 debug("do_authentication: illegal user %s", user);
576
577         setproctitle("%s%s", authctxt->pw ? user : "unknown",
578             use_privsep ? " [net]" : "");
579
580 #ifdef USE_PAM
581         PRIVSEP(start_pam(authctxt->pw == NULL ? "NOUSER" : user));
582 #endif
583
584         /*
585          * If we are not running as root, the user must have the same uid as
586          * the server. (Unless you are running Windows)
587          */
588 #ifndef HAVE_CYGWIN
589         if (!use_privsep && getuid() != 0 && authctxt->pw &&
590             authctxt->pw->pw_uid != getuid())
591                 packet_disconnect("Cannot change user when server not running as root.");
592 #endif
593
594         /*
595          * Loop until the user has been authenticated or the connection is
596          * closed, do_authloop() returns only if authentication is successful
597          */
598         do_authloop(authctxt);
599
600         /* The user has been authenticated and accepted. */
601         packet_start(SSH_SMSG_SUCCESS);
602         packet_send();
603         packet_write_wait();
604
605         return (authctxt);
606 }
This page took 0.300296 seconds and 5 git commands to generate.