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