]> andersk Git - openssh.git/blob - auth1.c
- dtucker@cvs.openbsd.org 2006/08/01 11:34:36
[openssh.git] / auth1.c
1 /* $OpenBSD: auth1.c,v 1.68 2006/07/22 20:48:22 stevesk Exp $ */
2 /*
3  * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4  *                    All rights reserved
5  *
6  * As far as I am concerned, the code I have written for this software
7  * can be used freely for any purpose.  Any derived versions of this
8  * software must be clearly marked as such, and if the derived work is
9  * incompatible with the protocol description in the RFC file, it must be
10  * called by a name other than "ssh" or "Secure Shell".
11  */
12
13 #include "includes.h"
14
15 #include <sys/types.h>
16
17 #include <string.h>
18 #include <unistd.h>
19
20 #include "xmalloc.h"
21 #include "rsa.h"
22 #include "ssh1.h"
23 #include "packet.h"
24 #include "buffer.h"
25 #include "log.h"
26 #include "servconf.h"
27 #include "compat.h"
28 #include "auth.h"
29 #include "channels.h"
30 #include "session.h"
31 #include "uidswap.h"
32 #include "monitor_wrap.h"
33 #include "buffer.h"
34
35 /* import */
36 extern ServerOptions options;
37 extern Buffer loginmsg;
38
39 static int auth1_process_password(Authctxt *, char *, size_t);
40 static int auth1_process_rsa(Authctxt *, char *, size_t);
41 static int auth1_process_rhosts_rsa(Authctxt *, char *, size_t);
42 static int auth1_process_tis_challenge(Authctxt *, char *, size_t);
43 static int auth1_process_tis_response(Authctxt *, char *, size_t);
44
45 static char *client_user = NULL;    /* Used to fill in remote user for PAM */
46
47 struct AuthMethod1 {
48         int type;
49         char *name;
50         int *enabled;
51         int (*method)(Authctxt *, char *, size_t);
52 };
53
54 const struct AuthMethod1 auth1_methods[] = {
55         {
56                 SSH_CMSG_AUTH_PASSWORD, "password",
57                 &options.password_authentication, auth1_process_password
58         },
59         {
60                 SSH_CMSG_AUTH_RSA, "rsa",
61                 &options.rsa_authentication, auth1_process_rsa
62         },
63         {
64                 SSH_CMSG_AUTH_RHOSTS_RSA, "rhosts-rsa",
65                 &options.rhosts_rsa_authentication, auth1_process_rhosts_rsa
66         },
67         {
68                 SSH_CMSG_AUTH_TIS, "challenge-response",
69                 &options.challenge_response_authentication,
70                 auth1_process_tis_challenge
71         },
72         {
73                 SSH_CMSG_AUTH_TIS_RESPONSE, "challenge-response",
74                 &options.challenge_response_authentication,
75                 auth1_process_tis_response
76         },
77         { -1, NULL, NULL, NULL}
78 };
79
80 static const struct AuthMethod1
81 *lookup_authmethod1(int type)
82 {
83         int i;
84
85         for (i = 0; auth1_methods[i].name != NULL; i++)
86                 if (auth1_methods[i].type == type)
87                         return (&(auth1_methods[i]));
88
89         return (NULL);
90 }
91
92 static char *
93 get_authname(int type)
94 {
95         const struct AuthMethod1 *a;
96         static char buf[64];
97
98         if ((a = lookup_authmethod1(type)) != NULL)
99                 return (a->name);
100         snprintf(buf, sizeof(buf), "bad-auth-msg-%d", type);
101         return (buf);
102 }
103
104 /*ARGSUSED*/
105 static int
106 auth1_process_password(Authctxt *authctxt, char *info, size_t infolen)
107 {
108         int authenticated = 0;
109         char *password;
110         u_int dlen;
111
112         /*
113          * Read user password.  It is in plain text, but was
114          * transmitted over the encrypted channel so it is
115          * not visible to an outside observer.
116          */
117         password = packet_get_string(&dlen);
118         packet_check_eom();
119
120         /* Try authentication with the password. */
121         authenticated = PRIVSEP(auth_password(authctxt, password));
122
123         memset(password, 0, dlen);
124         xfree(password);
125
126         return (authenticated);
127 }
128
129 /*ARGSUSED*/
130 static int
131 auth1_process_rsa(Authctxt *authctxt, char *info, size_t infolen)
132 {
133         int authenticated = 0;
134         BIGNUM *n;
135
136         /* RSA authentication requested. */
137         if ((n = BN_new()) == NULL)
138                 fatal("do_authloop: BN_new failed");
139         packet_get_bignum(n);
140         packet_check_eom();
141         authenticated = auth_rsa(authctxt, n);
142         BN_clear_free(n);
143
144         return (authenticated);
145 }
146
147 /*ARGSUSED*/
148 static int
149 auth1_process_rhosts_rsa(Authctxt *authctxt, char *info, size_t infolen)
150 {
151         int keybits, authenticated = 0;
152         u_int bits;
153         Key *client_host_key;
154         u_int ulen;
155
156         /*
157          * Get client user name.  Note that we just have to
158          * trust the client; root on the client machine can
159          * claim to be any user.
160          */
161         client_user = packet_get_string(&ulen);
162
163         /* Get the client host key. */
164         client_host_key = key_new(KEY_RSA1);
165         bits = packet_get_int();
166         packet_get_bignum(client_host_key->rsa->e);
167         packet_get_bignum(client_host_key->rsa->n);
168
169         keybits = BN_num_bits(client_host_key->rsa->n);
170         if (keybits < 0 || bits != (u_int)keybits) {
171                 verbose("Warning: keysize mismatch for client_host_key: "
172                     "actual %d, announced %d",
173                     BN_num_bits(client_host_key->rsa->n), bits);
174         }
175         packet_check_eom();
176
177         authenticated = auth_rhosts_rsa(authctxt, client_user,
178             client_host_key);
179         key_free(client_host_key);
180
181         snprintf(info, infolen, " ruser %.100s", client_user);
182
183         return (authenticated);
184 }
185
186 /*ARGSUSED*/
187 static int
188 auth1_process_tis_challenge(Authctxt *authctxt, char *info, size_t infolen)
189 {
190         char *challenge;
191
192         if ((challenge = get_challenge(authctxt)) == NULL)
193                 return (0);
194
195         debug("sending challenge '%s'", challenge);
196         packet_start(SSH_SMSG_AUTH_TIS_CHALLENGE);
197         packet_put_cstring(challenge);
198         xfree(challenge);
199         packet_send();
200         packet_write_wait();
201
202         return (-1);
203 }
204
205 /*ARGSUSED*/
206 static int
207 auth1_process_tis_response(Authctxt *authctxt, char *info, size_t infolen)
208 {
209         int authenticated = 0;
210         char *response;
211         u_int dlen;
212
213         response = packet_get_string(&dlen);
214         packet_check_eom();
215         authenticated = verify_response(authctxt, response);
216         memset(response, 'r', dlen);
217         xfree(response);
218
219         return (authenticated);
220 }
221
222 /*
223  * read packets, try to authenticate the user and
224  * return only if authentication is successful
225  */
226 static void
227 do_authloop(Authctxt *authctxt)
228 {
229         int authenticated = 0;
230         char info[1024];
231         int prev = 0, type = 0;
232         const struct AuthMethod1 *meth;
233
234         debug("Attempting authentication for %s%.100s.",
235             authctxt->valid ? "" : "invalid user ", authctxt->user);
236
237         /* If the user has no password, accept authentication immediately. */
238         if (options.password_authentication &&
239 #ifdef KRB5
240             (!options.kerberos_authentication || options.kerberos_or_local_passwd) &&
241 #endif
242             PRIVSEP(auth_password(authctxt, ""))) {
243 #ifdef USE_PAM
244                 if (options.use_pam && (PRIVSEP(do_pam_account())))
245 #endif
246                 {
247                         auth_log(authctxt, 1, "without authentication", "");
248                         return;
249                 }
250         }
251
252         /* Indicate that authentication is needed. */
253         packet_start(SSH_SMSG_FAILURE);
254         packet_send();
255         packet_write_wait();
256
257         for (;;) {
258                 /* default to fail */
259                 authenticated = 0;
260
261                 info[0] = '\0';
262
263                 /* Get a packet from the client. */
264                 prev = type;
265                 type = packet_read();
266
267                 /*
268                  * If we started challenge-response authentication but the
269                  * next packet is not a response to our challenge, release
270                  * the resources allocated by get_challenge() (which would
271                  * normally have been released by verify_response() had we
272                  * received such a response)
273                  */
274                 if (prev == SSH_CMSG_AUTH_TIS &&
275                     type != SSH_CMSG_AUTH_TIS_RESPONSE)
276                         abandon_challenge_response(authctxt);
277
278                 if ((meth = lookup_authmethod1(type)) == NULL) {
279                         logit("Unknown message during authentication: "
280                             "type %d", type);
281                         goto skip;
282                 }
283
284                 if (!*(meth->enabled)) {
285                         verbose("%s authentication disabled.", meth->name);
286                         goto skip;
287                 }
288
289                 authenticated = meth->method(authctxt, info, sizeof(info));
290                 if (authenticated == -1)
291                         continue; /* "postponed" */
292
293 #ifdef BSD_AUTH
294                 if (authctxt->as) {
295                         auth_close(authctxt->as);
296                         authctxt->as = NULL;
297                 }
298 #endif
299                 if (!authctxt->valid && authenticated)
300                         fatal("INTERNAL ERROR: authenticated invalid user %s",
301                             authctxt->user);
302
303 #ifdef _UNICOS
304                 if (authenticated && cray_access_denied(authctxt->user)) {
305                         authenticated = 0;
306                         fatal("Access denied for user %s.",authctxt->user);
307                 }
308 #endif /* _UNICOS */
309
310 #ifdef HAVE_CYGWIN
311                 if (authenticated &&
312                     !check_nt_auth(type == SSH_CMSG_AUTH_PASSWORD,
313                     authctxt->pw)) {
314                         packet_disconnect("Authentication rejected for uid %d.",
315                             authctxt->pw == NULL ? -1 : authctxt->pw->pw_uid);
316                         authenticated = 0;
317                 }
318 #else
319                 /* Special handling for root */
320                 if (authenticated && authctxt->pw->pw_uid == 0 &&
321                     !auth_root_allowed(meth->name)) {
322                         authenticated = 0;
323 # ifdef SSH_AUDIT_EVENTS
324                         PRIVSEP(audit_event(SSH_LOGIN_ROOT_DENIED));
325 # endif
326                 }
327 #endif
328
329 #ifdef USE_PAM
330                 if (options.use_pam && authenticated &&
331                     !PRIVSEP(do_pam_account())) {
332                         char *msg;
333                         size_t len;
334
335                         error("Access denied for user %s by PAM account "
336                             "configuration", authctxt->user);
337                         len = buffer_len(&loginmsg);
338                         buffer_append(&loginmsg, "\0", 1);
339                         msg = buffer_ptr(&loginmsg);
340                         /* strip trailing newlines */
341                         if (len > 0)
342                                 while (len > 0 && msg[--len] == '\n')
343                                         msg[len] = '\0';
344                         else
345                                 msg = "Access denied.";
346                         packet_disconnect(msg);
347                 }
348 #endif
349
350  skip:
351                 /* Log before sending the reply */
352                 auth_log(authctxt, authenticated, get_authname(type), info);
353
354                 if (client_user != NULL) {
355                         xfree(client_user);
356                         client_user = NULL;
357                 }
358
359                 if (authenticated)
360                         return;
361
362                 if (authctxt->failures++ > options.max_authtries) {
363 #ifdef SSH_AUDIT_EVENTS
364                         PRIVSEP(audit_event(SSH_LOGIN_EXCEED_MAXTRIES));
365 #endif
366                         packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
367                 }
368
369                 packet_start(SSH_SMSG_FAILURE);
370                 packet_send();
371                 packet_write_wait();
372         }
373 }
374
375 /*
376  * Performs authentication of an incoming connection.  Session key has already
377  * been exchanged and encryption is enabled.
378  */
379 void
380 do_authentication(Authctxt *authctxt)
381 {
382         u_int ulen;
383         char *user, *style = NULL;
384
385         /* Get the name of the user that we wish to log in as. */
386         packet_read_expect(SSH_CMSG_USER);
387
388         /* Get the user name. */
389         user = packet_get_string(&ulen);
390         packet_check_eom();
391
392         if ((style = strchr(user, ':')) != NULL)
393                 *style++ = '\0';
394
395         authctxt->user = user;
396         authctxt->style = style;
397
398         /* Verify that the user is a valid user. */
399         if ((authctxt->pw = PRIVSEP(getpwnamallow(user))) != NULL)
400                 authctxt->valid = 1;
401         else {
402                 debug("do_authentication: invalid user %s", user);
403                 authctxt->pw = fakepw();
404         }
405
406         setproctitle("%s%s", authctxt->valid ? user : "unknown",
407             use_privsep ? " [net]" : "");
408
409 #ifdef USE_PAM
410         if (options.use_pam)
411                 PRIVSEP(start_pam(authctxt));
412 #endif
413
414         /*
415          * If we are not running as root, the user must have the same uid as
416          * the server.
417          */
418 #ifndef HAVE_CYGWIN
419         if (!use_privsep && getuid() != 0 && authctxt->pw &&
420             authctxt->pw->pw_uid != getuid())
421                 packet_disconnect("Cannot change user when server not running as root.");
422 #endif
423
424         /*
425          * Loop until the user has been authenticated or the connection is
426          * closed, do_authloop() returns only if authentication is successful
427          */
428         do_authloop(authctxt);
429
430         /* The user has been authenticated and accepted. */
431         packet_start(SSH_SMSG_SUCCESS);
432         packet_send();
433         packet_write_wait();
434 }
This page took 1.98854 seconds and 5 git commands to generate.