]> andersk Git - openssh.git/blob - auth1.c
- (dtucker) OpenBSD CVS Sync
[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.49 2003/07/22 13:35:22 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:
164                         if (!options.rhosts_authentication) {
165                                 verbose("Rhosts authentication disabled.");
166                                 break;
167                         }
168                         /*
169                          * Get client user name.  Note that we just have to
170                          * trust the client; this is one reason why rhosts
171                          * authentication is insecure. (Another is
172                          * IP-spoofing on a local network.)
173                          */
174                         client_user = packet_get_string(&ulen);
175                         packet_check_eom();
176
177                         /* Try to authenticate using /etc/hosts.equiv and .rhosts. */
178                         authenticated = auth_rhosts(pw, client_user);
179
180                         snprintf(info, sizeof info, " ruser %.100s", client_user);
181                         break;
182
183                 case SSH_CMSG_AUTH_RHOSTS_RSA:
184                         if (!options.rhosts_rsa_authentication) {
185                                 verbose("Rhosts with RSA authentication disabled.");
186                                 break;
187                         }
188                         /*
189                          * Get client user name.  Note that we just have to
190                          * trust the client; root on the client machine can
191                          * claim to be any user.
192                          */
193                         client_user = packet_get_string(&ulen);
194
195                         /* Get the client host key. */
196                         client_host_key = key_new(KEY_RSA1);
197                         bits = packet_get_int();
198                         packet_get_bignum(client_host_key->rsa->e);
199                         packet_get_bignum(client_host_key->rsa->n);
200
201                         if (bits != BN_num_bits(client_host_key->rsa->n))
202                                 verbose("Warning: keysize mismatch for client_host_key: "
203                                     "actual %d, announced %d",
204                                     BN_num_bits(client_host_key->rsa->n), bits);
205                         packet_check_eom();
206
207                         authenticated = auth_rhosts_rsa(pw, client_user,
208                             client_host_key);
209                         key_free(client_host_key);
210
211                         snprintf(info, sizeof info, " ruser %.100s", client_user);
212                         break;
213
214                 case SSH_CMSG_AUTH_RSA:
215                         if (!options.rsa_authentication) {
216                                 verbose("RSA authentication disabled.");
217                                 break;
218                         }
219                         /* RSA authentication requested. */
220                         if ((n = BN_new()) == NULL)
221                                 fatal("do_authloop: BN_new failed");
222                         packet_get_bignum(n);
223                         packet_check_eom();
224                         authenticated = auth_rsa(pw, n);
225                         BN_clear_free(n);
226                         break;
227
228                 case SSH_CMSG_AUTH_PASSWORD:
229                         if (!options.password_authentication) {
230                                 verbose("Password authentication disabled.");
231                                 break;
232                         }
233                         /*
234                          * Read user password.  It is in plain text, but was
235                          * transmitted over the encrypted channel so it is
236                          * not visible to an outside observer.
237                          */
238                         password = packet_get_string(&dlen);
239                         packet_check_eom();
240
241                         /* Try authentication with the password. */
242                         authenticated = PRIVSEP(auth_password(authctxt, password));
243
244                         memset(password, 0, strlen(password));
245                         xfree(password);
246                         break;
247
248                 case SSH_CMSG_AUTH_TIS:
249                         debug("rcvd SSH_CMSG_AUTH_TIS");
250                         if (options.challenge_response_authentication == 1) {
251                                 char *challenge = get_challenge(authctxt);
252                                 if (challenge != NULL) {
253                                         debug("sending challenge '%s'", challenge);
254                                         packet_start(SSH_SMSG_AUTH_TIS_CHALLENGE);
255                                         packet_put_cstring(challenge);
256                                         xfree(challenge);
257                                         packet_send();
258                                         packet_write_wait();
259                                         continue;
260                                 }
261                         }
262                         break;
263                 case SSH_CMSG_AUTH_TIS_RESPONSE:
264                         debug("rcvd SSH_CMSG_AUTH_TIS_RESPONSE");
265                         if (options.challenge_response_authentication == 1) {
266                                 char *response = packet_get_string(&dlen);
267                                 packet_check_eom();
268                                 authenticated = verify_response(authctxt, response);
269                                 memset(response, 'r', dlen);
270                                 xfree(response);
271                         }
272                         break;
273
274                 default:
275                         /*
276                          * Any unknown messages will be ignored (and failure
277                          * returned) during authentication.
278                          */
279                         logit("Unknown message during authentication: type %d", type);
280                         break;
281                 }
282 #ifdef BSD_AUTH
283                 if (authctxt->as) {
284                         auth_close(authctxt->as);
285                         authctxt->as = NULL;
286                 }
287 #endif
288                 if (!authctxt->valid && authenticated)
289                         fatal("INTERNAL ERROR: authenticated invalid user %s",
290                             authctxt->user);
291
292 #ifdef _UNICOS
293                 if (authenticated && cray_access_denied(authctxt->user)) {
294                         authenticated = 0;
295                         fatal("Access denied for user %s.",authctxt->user);
296                 }
297 #endif /* _UNICOS */
298
299 #ifdef HAVE_CYGWIN
300                 if (authenticated &&
301                     !check_nt_auth(type == SSH_CMSG_AUTH_PASSWORD, pw)) {
302                         packet_disconnect("Authentication rejected for uid %d.",
303                         pw == NULL ? -1 : pw->pw_uid);
304                         authenticated = 0;
305                 }
306 #else
307                 /* Special handling for root */
308                 if (authenticated && authctxt->pw->pw_uid == 0 &&
309                     !auth_root_allowed(get_authname(type)))
310                         authenticated = 0;
311 #endif
312
313                 /* Log before sending the reply */
314                 auth_log(authctxt, authenticated, get_authname(type), info);
315
316                 if (client_user != NULL) {
317                         xfree(client_user);
318                         client_user = NULL;
319                 }
320
321                 if (authenticated)
322                         return;
323
324                 if (authctxt->failures++ > AUTH_FAIL_MAX)
325                         packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
326
327                 packet_start(SSH_SMSG_FAILURE);
328                 packet_send();
329                 packet_write_wait();
330         }
331 }
332
333 /*
334  * Performs authentication of an incoming connection.  Session key has already
335  * been exchanged and encryption is enabled.
336  */
337 Authctxt *
338 do_authentication(void)
339 {
340         Authctxt *authctxt;
341         u_int ulen;
342         char *user, *style = NULL;
343
344         /* Get the name of the user that we wish to log in as. */
345         packet_read_expect(SSH_CMSG_USER);
346
347         /* Get the user name. */
348         user = packet_get_string(&ulen);
349         packet_check_eom();
350
351         if ((style = strchr(user, ':')) != NULL)
352                 *style++ = '\0';
353
354 #ifdef KRB5
355         /* XXX - SSH.com Kerberos v5 braindeath. */
356         if ((datafellows & SSH_BUG_K5USER) &&
357             options.kerberos_authentication) {
358                 char *p;
359                 if ((p = strchr(user, '@')) != NULL)
360                         *p = '\0';
361         }
362 #endif
363
364         authctxt = authctxt_new();
365         authctxt->user = user;
366         authctxt->style = style;
367
368         /* Verify that the user is a valid user. */
369         if ((authctxt->pw = PRIVSEP(getpwnamallow(user))) != NULL)
370                 authctxt->valid = 1;
371         else
372                 debug("do_authentication: illegal user %s", user);
373
374         setproctitle("%s%s", authctxt->pw ? user : "unknown",
375             use_privsep ? " [net]" : "");
376
377 #ifdef USE_PAM
378         if (options.use_pam)
379                 PRIVSEP(start_pam(user));
380 #endif
381
382         /*
383          * If we are not running as root, the user must have the same uid as
384          * the server. (Unless you are running Windows)
385          */
386 #ifndef HAVE_CYGWIN
387         if (!use_privsep && getuid() != 0 && authctxt->pw &&
388             authctxt->pw->pw_uid != getuid())
389                 packet_disconnect("Cannot change user when server not running as root.");
390 #endif
391
392         /*
393          * Loop until the user has been authenticated or the connection is
394          * closed, do_authloop() returns only if authentication is successful
395          */
396         do_authloop(authctxt);
397
398         /* The user has been authenticated and accepted. */
399         packet_start(SSH_SMSG_SUCCESS);
400         packet_send();
401         packet_write_wait();
402
403         return (authctxt);
404 }
This page took 0.076715 seconds and 5 git commands to generate.