]> andersk Git - openssh.git/blame - auth2.c
- (djm) Automatically generate host key during "make install". Suggested
[openssh.git] / auth2.c
CommitLineData
a306f2dd 1/*
2 * Copyright (c) 2000 Markus Friedl. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * 3. All advertising materials mentioning features or use of this software
13 * must display the following acknowledgement:
14 * This product includes software developed by Markus Friedl.
15 * 4. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29#include "includes.h"
38c295d6 30RCSID("$OpenBSD: auth2.c,v 1.10 2000/06/18 04:05:02 markus Exp $");
a306f2dd 31
32#include <openssl/dsa.h>
33#include <openssl/rsa.h>
34#include <openssl/evp.h>
35
36#include "xmalloc.h"
37#include "rsa.h"
38#include "ssh.h"
39#include "pty.h"
40#include "packet.h"
41#include "buffer.h"
42#include "cipher.h"
43#include "servconf.h"
44#include "compat.h"
45#include "channels.h"
46#include "bufaux.h"
47#include "ssh2.h"
48#include "auth.h"
49#include "session.h"
50#include "dispatch.h"
51#include "auth.h"
52#include "key.h"
53#include "kex.h"
54
55#include "dsa.h"
56#include "uidswap.h"
38c295d6 57#include "auth-options.h"
a306f2dd 58
59/* import */
60extern ServerOptions options;
61extern unsigned char *session_id2;
62extern int session_id2_len;
63
64/* protocol */
65
66void input_service_request(int type, int plen);
67void input_userauth_request(int type, int plen);
68void protocol_error(int type, int plen);
69
70/* auth */
71int ssh2_auth_none(struct passwd *pw);
72int ssh2_auth_password(struct passwd *pw);
38c295d6 73int ssh2_auth_pubkey(struct passwd *pw, char *service);
a306f2dd 74
75/* helper */
76struct passwd* auth_set_user(char *u, char *s);
77int user_dsa_key_allowed(struct passwd *pw, Key *key);
78
79typedef struct Authctxt Authctxt;
80struct Authctxt {
81 char *user;
82 char *service;
83 struct passwd pw;
84 int valid;
85};
86static Authctxt *authctxt = NULL;
87static int userauth_success = 0;
88
89/*
90 * loop until userauth_success == TRUE
91 */
92
93void
94do_authentication2()
95{
3189621b 96 /* turn off skey/kerberos, not supported by SSH2 */
8cb940db 97#ifdef SKEY
3189621b 98 options.skey_authentication = 0;
8cb940db 99#endif
100#ifdef KRB4
3189621b 101 options.kerberos_authentication = 0;
8cb940db 102#endif
3189621b 103
a306f2dd 104 dispatch_init(&protocol_error);
105 dispatch_set(SSH2_MSG_SERVICE_REQUEST, &input_service_request);
106 dispatch_run(DISPATCH_BLOCK, &userauth_success);
107 do_authenticated2();
108}
109
110void
111protocol_error(int type, int plen)
112{
113 log("auth: protocol error: type %d plen %d", type, plen);
114 packet_start(SSH2_MSG_UNIMPLEMENTED);
115 packet_put_int(0);
116 packet_send();
117 packet_write_wait();
118}
119
120void
121input_service_request(int type, int plen)
122{
123 unsigned int len;
124 int accept = 0;
125 char *service = packet_get_string(&len);
126 packet_done();
127
128 if (strcmp(service, "ssh-userauth") == 0) {
129 if (!userauth_success) {
130 accept = 1;
131 /* now we can handle user-auth requests */
132 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &input_userauth_request);
133 }
134 }
135 /* XXX all other service requests are denied */
136
137 if (accept) {
138 packet_start(SSH2_MSG_SERVICE_ACCEPT);
139 packet_put_cstring(service);
140 packet_send();
141 packet_write_wait();
142 } else {
143 debug("bad service request %s", service);
144 packet_disconnect("bad service request %s", service);
145 }
146 xfree(service);
147}
148
149void
150input_userauth_request(int type, int plen)
151{
152 static void (*authlog) (const char *fmt,...) = verbose;
153 static int attempt = 0;
38c295d6 154 unsigned int len;
a306f2dd 155 int authenticated = 0;
38c295d6 156 char *user, *service, *method, *authmsg = NULL;
a306f2dd 157 struct passwd *pw;
c1ef8333 158#ifdef WITH_AIXAUTHENTICATE
159 extern char *aixloginmsg;
160#endif /* WITH_AIXAUTHENTICATE */
a306f2dd 161
a306f2dd 162 user = packet_get_string(&len);
163 service = packet_get_string(&len);
164 method = packet_get_string(&len);
c1ef8333 165 if (++attempt == AUTH_FAIL_MAX) {
166#ifdef WITH_AIXAUTHENTICATE
167 loginfailed(user,get_canonical_hostname(),"ssh");
168#endif /* WITH_AIXAUTHENTICATE */
169 packet_disconnect("too many failed userauth_requests");
170 }
a306f2dd 171 debug("userauth-request for user %s service %s method %s", user, service, method);
172
173 /* XXX we only allow the ssh-connection service */
174 pw = auth_set_user(user, service);
175 if (pw && strcmp(service, "ssh-connection")==0) {
176 if (strcmp(method, "none") == 0) {
177 authenticated = ssh2_auth_none(pw);
178 } else if (strcmp(method, "password") == 0) {
179 authenticated = ssh2_auth_password(pw);
180 } else if (strcmp(method, "publickey") == 0) {
38c295d6 181 authenticated = ssh2_auth_pubkey(pw, service);
a306f2dd 182 }
183 }
184 if (authenticated && pw && pw->pw_uid == 0 && !options.permit_root_login) {
185 authenticated = 0;
186 log("ROOT LOGIN REFUSED FROM %.200s",
187 get_canonical_hostname());
188 }
189
190#ifdef USE_PAM
191 if (authenticated && !do_pam_account(pw->pw_name, NULL))
192 authenticated = 0;
193#endif /* USE_PAM */
194
1d1ffb87 195 /* Raise logging level */
196 if (authenticated == 1 ||
197 attempt == AUTH_FAIL_LOG ||
198 strcmp(method, "password") == 0)
199 authlog = log;
200
201 /* Log before sending the reply */
a306f2dd 202 if (authenticated == 1) {
203 authmsg = "Accepted";
1d1ffb87 204 } else if (authenticated == 0) {
205 authmsg = "Failed";
206 } else {
207 authmsg = "Postponed";
208 }
209 authlog("%s %s for %.200s from %.200s port %d ssh2",
210 authmsg,
211 method,
212 pw && pw->pw_uid == 0 ? "ROOT" : user,
213 get_remote_ipaddr(),
214 get_remote_port());
215
216 /* XXX todo: check if multiple auth methods are needed */
217 if (authenticated == 1) {
c1ef8333 218#ifdef WITH_AIXAUTHENTICATE
219 /* We don't have a pty yet, so just label the line as "ssh" */
220 if (loginsuccess(user,get_canonical_hostname(),"ssh",
221 &aixloginmsg) < 0)
222 aixloginmsg = NULL;
223#endif /* WITH_AIXAUTHENTICATE */
a306f2dd 224 /* turn off userauth */
225 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &protocol_error);
226 packet_start(SSH2_MSG_USERAUTH_SUCCESS);
227 packet_send();
228 packet_write_wait();
229 /* now we can break out */
230 userauth_success = 1;
231 } else if (authenticated == 0) {
a306f2dd 232 packet_start(SSH2_MSG_USERAUTH_FAILURE);
233 packet_put_cstring("publickey,password"); /* XXX dynamic */
234 packet_put_char(0); /* XXX partial success, unused */
235 packet_send();
236 packet_write_wait();
a306f2dd 237 }
a306f2dd 238
239 xfree(service);
240 xfree(user);
241 xfree(method);
242}
243
244int
245ssh2_auth_none(struct passwd *pw)
246{
247 packet_done();
248#ifdef USE_PAM
249 return auth_pam_password(pw, "");
250#else /* USE_PAM */
251 return auth_password(pw, "");
252#endif /* USE_PAM */
253}
254int
255ssh2_auth_password(struct passwd *pw)
256{
257 char *password;
258 int authenticated = 0;
259 int change;
260 unsigned int len;
261 change = packet_get_char();
262 if (change)
263 log("password change not supported");
264 password = packet_get_string(&len);
265 packet_done();
266 if (options.password_authentication &&
267#ifdef USE_PAM
268 auth_pam_password(pw, password) == 1)
269#else /* USE_PAM */
270 auth_password(pw, password) == 1)
271#endif /* USE_PAM */
272 authenticated = 1;
273 memset(password, 0, len);
274 xfree(password);
275 return authenticated;
276}
277int
38c295d6 278ssh2_auth_pubkey(struct passwd *pw, char *service)
a306f2dd 279{
280 Buffer b;
281 Key *key;
282 char *pkalg, *pkblob, *sig;
283 unsigned int alen, blen, slen;
284 int have_sig;
285 int authenticated = 0;
286
1d1ffb87 287 if (options.dsa_authentication == 0) {
a306f2dd 288 debug("pubkey auth disabled");
289 return 0;
290 }
291 have_sig = packet_get_char();
292 pkalg = packet_get_string(&alen);
293 if (strcmp(pkalg, KEX_DSS) != 0) {
294 xfree(pkalg);
295 log("bad pkalg %s", pkalg); /*XXX*/
296 return 0;
297 }
298 pkblob = packet_get_string(&blen);
299 key = dsa_key_from_blob(pkblob, blen);
300 if (key != NULL) {
301 if (have_sig) {
302 sig = packet_get_string(&slen);
303 packet_done();
304 buffer_init(&b);
305 buffer_append(&b, session_id2, session_id2_len);
38c295d6 306
307 /* reconstruct packet */
a306f2dd 308 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
38c295d6 309 buffer_put_cstring(&b, pw->pw_name);
310 buffer_put_cstring(&b,
311 datafellows & SSH_BUG_PUBKEYAUTH ?
312 "ssh-userauth" :
313 service);
314 buffer_put_cstring(&b, "publickey");
315 buffer_put_char(&b, have_sig);
316 buffer_put_cstring(&b, KEX_DSS);
317 buffer_put_string(&b, pkblob, blen);
a306f2dd 318#ifdef DEBUG_DSS
319 buffer_dump(&b);
320#endif
321 /* test for correct signature */
322 if (user_dsa_key_allowed(pw, key) &&
323 dsa_verify(key, sig, slen, buffer_ptr(&b), buffer_len(&b)) == 1)
324 authenticated = 1;
325 buffer_clear(&b);
326 xfree(sig);
327 } else {
328 packet_done();
329 debug("test key...");
330 /* test whether pkalg/pkblob are acceptable */
331 /* XXX fake reply and always send PK_OK ? */
1d1ffb87 332 /*
333 * XXX this allows testing whether a user is allowed
334 * to login: if you happen to have a valid pubkey this
335 * message is sent. the message is NEVER sent at all
336 * if a user is not allowed to login. is this an
337 * issue? -markus
338 */
a306f2dd 339 if (user_dsa_key_allowed(pw, key)) {
340 packet_start(SSH2_MSG_USERAUTH_PK_OK);
341 packet_put_string(pkalg, alen);
342 packet_put_string(pkblob, blen);
343 packet_send();
344 packet_write_wait();
345 authenticated = -1;
346 }
347 }
348 key_free(key);
349 }
350 xfree(pkalg);
351 xfree(pkblob);
352 return authenticated;
353}
354
355/* set and get current user */
356
357struct passwd*
358auth_get_user(void)
359{
360 return (authctxt != NULL && authctxt->valid) ? &authctxt->pw : NULL;
361}
362
363struct passwd*
364auth_set_user(char *u, char *s)
365{
366 struct passwd *pw, *copy;
367
368 if (authctxt == NULL) {
369 authctxt = xmalloc(sizeof(*authctxt));
370 authctxt->valid = 0;
371 authctxt->user = xstrdup(u);
372 authctxt->service = xstrdup(s);
373 setproctitle("%s", u);
374 pw = getpwnam(u);
375 if (!pw || !allowed_user(pw)) {
376 log("auth_set_user: illegal user %s", u);
377 return NULL;
378 }
379#ifdef USE_PAM
380 start_pam(pw);
381#endif
382 copy = &authctxt->pw;
383 memset(copy, 0, sizeof(*copy));
384 copy->pw_name = xstrdup(pw->pw_name);
385 copy->pw_passwd = xstrdup(pw->pw_passwd);
386 copy->pw_uid = pw->pw_uid;
387 copy->pw_gid = pw->pw_gid;
388 copy->pw_dir = xstrdup(pw->pw_dir);
389 copy->pw_shell = xstrdup(pw->pw_shell);
390 authctxt->valid = 1;
391 } else {
392 if (strcmp(u, authctxt->user) != 0 ||
393 strcmp(s, authctxt->service) != 0) {
394 log("auth_set_user: missmatch: (%s,%s)!=(%s,%s)",
395 u, s, authctxt->user, authctxt->service);
396 return NULL;
397 }
398 }
399 return auth_get_user();
400}
401
402/* return 1 if user allows given key */
403int
404user_dsa_key_allowed(struct passwd *pw, Key *key)
405{
406 char line[8192], file[1024];
407 int found_key = 0;
408 unsigned int bits = -1;
409 FILE *f;
410 unsigned long linenum = 0;
411 struct stat st;
412 Key *found;
413
414 /* Temporarily use the user's uid. */
415 temporarily_use_uid(pw->pw_uid);
416
417 /* The authorized keys. */
418 snprintf(file, sizeof file, "%.500s/%.100s", pw->pw_dir,
419 SSH_USER_PERMITTED_KEYS2);
420
421 /* Fail quietly if file does not exist */
422 if (stat(file, &st) < 0) {
423 /* Restore the privileged uid. */
424 restore_uid();
425 return 0;
426 }
427 /* Open the file containing the authorized keys. */
428 f = fopen(file, "r");
429 if (!f) {
430 /* Restore the privileged uid. */
431 restore_uid();
432 return 0;
433 }
434 if (options.strict_modes) {
435 int fail = 0;
436 char buf[1024];
437 /* Check open file in order to avoid open/stat races */
438 if (fstat(fileno(f), &st) < 0 ||
439 (st.st_uid != 0 && st.st_uid != pw->pw_uid) ||
440 (st.st_mode & 022) != 0) {
441 snprintf(buf, sizeof buf, "DSA authentication refused for %.100s: "
442 "bad ownership or modes for '%s'.", pw->pw_name, file);
443 fail = 1;
444 } else {
445 /* Check path to SSH_USER_PERMITTED_KEYS */
446 int i;
447 static const char *check[] = {
448 "", SSH_USER_DIR, NULL
449 };
450 for (i = 0; check[i]; i++) {
451 snprintf(line, sizeof line, "%.500s/%.100s",
452 pw->pw_dir, check[i]);
453 if (stat(line, &st) < 0 ||
454 (st.st_uid != 0 && st.st_uid != pw->pw_uid) ||
455 (st.st_mode & 022) != 0) {
456 snprintf(buf, sizeof buf,
457 "DSA authentication refused for %.100s: "
458 "bad ownership or modes for '%s'.",
459 pw->pw_name, line);
460 fail = 1;
461 break;
462 }
463 }
464 }
465 if (fail) {
466 log(buf);
467 fclose(f);
468 restore_uid();
469 return 0;
470 }
471 }
472 found_key = 0;
473 found = key_new(KEY_DSA);
474
475 while (fgets(line, sizeof(line), f)) {
38c295d6 476 char *cp, *options = NULL;
a306f2dd 477 linenum++;
478 /* Skip leading whitespace, empty and comment lines. */
479 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
480 ;
481 if (!*cp || *cp == '\n' || *cp == '#')
482 continue;
38c295d6 483
a306f2dd 484 bits = key_read(found, &cp);
38c295d6 485 if (bits == 0) {
486 /* no key? check if there are options for this key */
487 int quoted = 0;
488 options = cp;
489 for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
490 if (*cp == '\\' && cp[1] == '"')
491 cp++; /* Skip both */
492 else if (*cp == '"')
493 quoted = !quoted;
494 }
495 /* Skip remaining whitespace. */
496 for (; *cp == ' ' || *cp == '\t'; cp++)
497 ;
498 bits = key_read(found, &cp);
499 if (bits == 0) {
500 /* still no key? advance to next line*/
501 continue;
502 }
503 }
504 if (key_equal(found, key) &&
505 auth_parse_options(pw, options, linenum) == 1) {
a306f2dd 506 found_key = 1;
507 debug("matching key found: file %s, line %ld",
508 file, linenum);
509 break;
510 }
511 }
512 restore_uid();
513 fclose(f);
514 key_free(found);
515 return found_key;
516}
This page took 0.350201 seconds and 5 git commands to generate.