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