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