]> andersk Git - openssh.git/blobdiff - auth2.c
- itojun@cvs.openbsd.org 2002/05/13 02:37:39
[openssh.git] / auth2.c
diff --git a/auth2.c b/auth2.c
index 1ad905f18ef4bc4b6f4efbc65bb02617e5ce4451..6bcc5652787fbac9e67e5205bee4291bb3331d3a 100644 (file)
--- a/auth2.c
+++ b/auth2.c
@@ -23,7 +23,7 @@
  */
 
 #include "includes.h"
-RCSID("$OpenBSD: auth2.c,v 1.79 2001/12/28 12:14:27 markus Exp $");
+RCSID("$OpenBSD: auth2.c,v 1.91 2002/05/13 02:37:39 itojun Exp $");
 
 #include <openssl/evp.h>
 
@@ -47,17 +47,18 @@ RCSID("$OpenBSD: auth2.c,v 1.79 2001/12/28 12:14:27 markus Exp $");
 #include "pathnames.h"
 #include "uidswap.h"
 #include "auth-options.h"
-#include "misc.h"
 #include "hostfile.h"
 #include "canohost.h"
 #include "match.h"
+#include "monitor_wrap.h"
+#include "atomicio.h"
 
 /* import */
 extern ServerOptions options;
 extern u_char *session_id2;
 extern int session_id2_len;
 
-static Authctxt        *x_authctxt = NULL;
+Authctxt *x_authctxt = NULL;
 static int one = 1;
 
 typedef struct Authmethod Authmethod;
@@ -69,15 +70,14 @@ struct Authmethod {
 
 /* protocol */
 
-static void input_service_request(int, int, u_int32_t, void *);
-static void input_userauth_request(int, int, u_int32_t, void *);
-static void protocol_error(int, int, u_int32_t, void *);
+static void input_service_request(int, u_int32_t, void *);
+static void input_userauth_request(int, u_int32_t, void *);
 
 /* helper */
 static Authmethod *authmethod_lookup(const char *);
 static char *authmethods_get(void);
-static int user_key_allowed(struct passwd *, Key *);
-static int hostbased_key_allowed(struct passwd *, const char *, char *, Key *);
+int user_key_allowed(struct passwd *, Key *);
+int hostbased_key_allowed(struct passwd *, const char *, char *, Key *);
 
 /* auth */
 static void userauth_banner(void);
@@ -110,7 +110,7 @@ Authmethod authmethods[] = {
  * loop until authctxt->success == TRUE
  */
 
-void
+Authctxt *
 do_authentication2(void)
 {
        Authctxt *authctxt = authctxt_new();
@@ -122,25 +122,18 @@ do_authentication2(void)
                options.kbd_interactive_authentication = 1;
        if (options.pam_authentication_via_kbd_int)
                options.kbd_interactive_authentication = 1;
+       if (use_privsep)
+               options.pam_authentication_via_kbd_int = 0;
 
-       dispatch_init(&protocol_error);
+       dispatch_init(&dispatch_protocol_error);
        dispatch_set(SSH2_MSG_SERVICE_REQUEST, &input_service_request);
        dispatch_run(DISPATCH_BLOCK, &authctxt->success, authctxt);
-       do_authenticated(authctxt);
-}
 
-static void
-protocol_error(int type, int plen, u_int32_t seq, void *ctxt)
-{
-       log("auth: protocol error: type %d plen %d", type, plen);
-       packet_start(SSH2_MSG_UNIMPLEMENTED);
-       packet_put_int(seq);
-       packet_send();
-       packet_write_wait();
+       return (authctxt);
 }
 
 static void
-input_service_request(int type, int plen, u_int32_t seq, void *ctxt)
+input_service_request(int type, u_int32_t seq, void *ctxt)
 {
        Authctxt *authctxt = ctxt;
        u_int len;
@@ -173,7 +166,7 @@ input_service_request(int type, int plen, u_int32_t seq, void *ctxt)
 }
 
 static void
-input_userauth_request(int type, int plen, u_int32_t seq, void *ctxt)
+input_userauth_request(int type, u_int32_t seq, void *ctxt)
 {
        Authctxt *authctxt = ctxt;
        Authmethod *m = NULL;
@@ -194,25 +187,26 @@ input_userauth_request(int type, int plen, u_int32_t seq, void *ctxt)
 
        if (authctxt->attempt++ == 0) {
                /* setup auth context */
-               struct passwd *pw = NULL;
-               pw = getpwnam(user);
-               if (pw && allowed_user(pw) && strcmp(service, "ssh-connection")==0) {
-                       authctxt->pw = pwcopy(pw);
+               authctxt->pw = PRIVSEP(getpwnamallow(user));
+               if (authctxt->pw && strcmp(service, "ssh-connection")==0) {
                        authctxt->valid = 1;
                        debug2("input_userauth_request: setting up authctxt for %s", user);
 #ifdef USE_PAM
-                       start_pam(pw->pw_name);
+                       PRIVSEP(start_pam(authctxt->pw->pw_name));
 #endif
                } else {
                        log("input_userauth_request: illegal user %s", user);
 #ifdef USE_PAM
-                       start_pam("NOUSER");
+                       PRIVSEP(start_pam("NOUSER"));
 #endif
                }
-               setproctitle("%s", pw ? user : "unknown");
+               setproctitle("%s%s", authctxt->pw ? user : "unknown",
+                   use_privsep ? " [net]" : "");
                authctxt->user = xstrdup(user);
                authctxt->service = xstrdup(service);
                authctxt->style = style ? xstrdup(style) : NULL;
+               if (use_privsep)
+                       mm_inform_authserv(service, style);
        } else if (strcmp(user, authctxt->user) != 0 ||
            strcmp(service, authctxt->service) != 0) {
                packet_disconnect("Change of username or service not allowed: "
@@ -251,8 +245,8 @@ userauth_finish(Authctxt *authctxt, int authenticated, char *method)
                authenticated = 0;
 
 #ifdef USE_PAM
-       if (authenticated && authctxt->user && !do_pam_account(authctxt->user,
-           NULL))
+       if (!use_privsep && authenticated && authctxt->user && 
+           !do_pam_account(authctxt->user, NULL))
                authenticated = 0;
 #endif /* USE_PAM */
 
@@ -265,7 +259,7 @@ userauth_finish(Authctxt *authctxt, int authenticated, char *method)
        /* XXX todo: check if multiple auth methods are needed */
        if (authenticated == 1) {
                /* turn off userauth */
-               dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &protocol_error);
+               dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &dispatch_protocol_ignore);
                packet_start(SSH2_MSG_USERAUTH_SUCCESS);
                packet_send();
                packet_write_wait();
@@ -275,7 +269,7 @@ userauth_finish(Authctxt *authctxt, int authenticated, char *method)
                if (authctxt->failures++ > AUTH_FAIL_MAX) {
 #ifdef WITH_AIXAUTHENTICATE
                        loginfailed(authctxt->user,
-                           get_canonical_hostname(options.reverse_mapping_check),
+                           get_canonical_hostname(options.verify_reverse_mapping),
                            "ssh");
 #endif /* WITH_AIXAUTHENTICATE */
                        packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
@@ -290,25 +284,45 @@ userauth_finish(Authctxt *authctxt, int authenticated, char *method)
        }
 }
 
-static void
-userauth_banner(void)
+char *
+auth2_read_banner(void)
 {
        struct stat st;
        char *banner = NULL;
        off_t len, n;
        int fd;
 
-       if (options.banner == NULL || (datafellows & SSH_BUG_BANNER))
-               return;
-       if ((fd = open(options.banner, O_RDONLY)) < 0)
-               return;
-       if (fstat(fd, &st) < 0)
-               goto done;
+       if ((fd = open(options.banner, O_RDONLY)) == -1)
+               return (NULL);
+       if (fstat(fd, &st) == -1) {
+               close(fd);
+               return (NULL);
+       }
        len = st.st_size;
        banner = xmalloc(len + 1);
-       if ((n = read(fd, banner, len)) < 0)
-               goto done;
+       n = atomicio(read, fd, banner, len);
+       close(fd);
+
+       if (n != len) {
+               free(banner);
+               return (NULL);
+       }
        banner[n] = '\0';
+       
+       return (banner);
+}
+
+static void
+userauth_banner(void)
+{
+       char *banner = NULL;
+
+       if (options.banner == NULL || (datafellows & SSH_BUG_BANNER))
+               return;
+
+       if ((banner = PRIVSEP(auth2_read_banner())) == NULL)
+               goto done;
+
        packet_start(SSH2_MSG_USERAUTH_BANNER);
        packet_put_cstring(banner);
        packet_put_cstring("");         /* language, unused */
@@ -317,7 +331,6 @@ userauth_banner(void)
 done:
        if (banner)
                xfree(banner);
-       close(fd);
        return;
 }
 
@@ -338,13 +351,7 @@ userauth_none(Authctxt *authctxt)
        if (check_nt_auth(1, authctxt->pw) == 0)
                return(0);
 #endif
-#ifdef USE_PAM
-       return auth_pam_password(authctxt->pw, "");
-#elif defined(HAVE_OSF_SIA)
-       return 0;
-#else /* !HAVE_OSF_SIA && !USE_PAM */
-       return auth_password(authctxt, "");
-#endif /* USE_PAM */
+       return PRIVSEP(auth_password(authctxt, ""));
 }
 
 static int
@@ -363,13 +370,7 @@ userauth_passwd(Authctxt *authctxt)
 #ifdef HAVE_CYGWIN
            check_nt_auth(1, authctxt->pw) &&
 #endif
-#ifdef USE_PAM
-           auth_pam_password(authctxt->pw, password) == 1)
-#elif defined(HAVE_OSF_SIA)
-           auth_sia_password(authctxt->user, password) == 1)
-#else /* !USE_PAM && !HAVE_OSF_SIA */
-           auth_password(authctxt, password) == 1)
-#endif /* USE_PAM */
+           PRIVSEP(auth_password(authctxt, password)) == 1)
                authenticated = 1;
        memset(password, 0, len);
        xfree(password);
@@ -408,8 +409,9 @@ static int
 userauth_pubkey(Authctxt *authctxt)
 {
        Buffer b;
-       Key *key;
-       char *pkalg, *pkblob, *sig;
+       Key *key = NULL;
+       char *pkalg;
+       u_char *pkblob, *sig;
        u_int alen, blen, slen;
        int have_sig, pktype;
        int authenticated = 0;
@@ -435,72 +437,82 @@ userauth_pubkey(Authctxt *authctxt)
        pktype = key_type_from_name(pkalg);
        if (pktype == KEY_UNSPEC) {
                /* this is perfectly legal */
-               log("userauth_pubkey: unsupported public key algorithm: %s", pkalg);
-               xfree(pkalg);
-               xfree(pkblob);
-               return 0;
+               log("userauth_pubkey: unsupported public key algorithm: %s",
+                   pkalg);
+               goto done;
        }
        key = key_from_blob(pkblob, blen);
-       if (key != NULL) {
-               if (have_sig) {
-                       sig = packet_get_string(&slen);
-                       packet_check_eom();
-                       buffer_init(&b);
-                       if (datafellows & SSH_OLD_SESSIONID) {
-                               buffer_append(&b, session_id2, session_id2_len);
-                       } else {
-                               buffer_put_string(&b, session_id2, session_id2_len);
-                       }
-                       /* reconstruct packet */
-                       buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
-                       buffer_put_cstring(&b, authctxt->user);
-                       buffer_put_cstring(&b,
-                           datafellows & SSH_BUG_PKSERVICE ?
-                           "ssh-userauth" :
-                           authctxt->service);
-                       if (datafellows & SSH_BUG_PKAUTH) {
-                               buffer_put_char(&b, have_sig);
-                       } else {
-                               buffer_put_cstring(&b, "publickey");
-                               buffer_put_char(&b, have_sig);
-                               buffer_put_cstring(&b, pkalg);
-                       }
-                       buffer_put_string(&b, pkblob, blen);
+       if (key == NULL) {
+               error("userauth_pubkey: cannot decode key: %s", pkalg);
+               goto done;
+       }
+       if (key->type != pktype) {
+               error("userauth_pubkey: type mismatch for decoded key "
+                   "(received %d, expected %d)", key->type, pktype);
+               goto done;
+       }
+       if (have_sig) {
+               sig = packet_get_string(&slen);
+               packet_check_eom();
+               buffer_init(&b);
+               if (datafellows & SSH_OLD_SESSIONID) {
+                       buffer_append(&b, session_id2, session_id2_len);
+               } else {
+                       buffer_put_string(&b, session_id2, session_id2_len);
+               }
+               /* reconstruct packet */
+               buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
+               buffer_put_cstring(&b, authctxt->user);
+               buffer_put_cstring(&b,
+                   datafellows & SSH_BUG_PKSERVICE ?
+                   "ssh-userauth" :
+                   authctxt->service);
+               if (datafellows & SSH_BUG_PKAUTH) {
+                       buffer_put_char(&b, have_sig);
+               } else {
+                       buffer_put_cstring(&b, "publickey");
+                       buffer_put_char(&b, have_sig);
+                       buffer_put_cstring(&b, pkalg);
+               }
+               buffer_put_string(&b, pkblob, blen);
 #ifdef DEBUG_PK
-                       buffer_dump(&b);
+               buffer_dump(&b);
 #endif
-                       /* test for correct signature */
-                       if (user_key_allowed(authctxt->pw, key) &&
-                           key_verify(key, sig, slen, buffer_ptr(&b), buffer_len(&b)) == 1)
-                               authenticated = 1;
-                       buffer_clear(&b);
-                       xfree(sig);
-               } else {
-                       debug("test whether pkalg/pkblob are acceptable");
-                       packet_check_eom();
-
-                       /* XXX fake reply and always send PK_OK ? */
-                       /*
-                        * XXX this allows testing whether a user is allowed
-                        * to login: if you happen to have a valid pubkey this
-                        * message is sent. the message is NEVER sent at all
-                        * if a user is not allowed to login. is this an
-                        * issue? -markus
-                        */
-                       if (user_key_allowed(authctxt->pw, key)) {
-                               packet_start(SSH2_MSG_USERAUTH_PK_OK);
-                               packet_put_string(pkalg, alen);
-                               packet_put_string(pkblob, blen);
-                               packet_send();
-                               packet_write_wait();
-                               authctxt->postponed = 1;
-                       }
+               /* test for correct signature */
+               authenticated = 0;
+               if (PRIVSEP(user_key_allowed(authctxt->pw, key)) &&
+                   PRIVSEP(key_verify(key, sig, slen, buffer_ptr(&b),
+                               buffer_len(&b))) == 1)
+                       authenticated = 1;
+               buffer_clear(&b);
+               xfree(sig);
+       } else {
+               debug("test whether pkalg/pkblob are acceptable");
+               packet_check_eom();
+
+               /* XXX fake reply and always send PK_OK ? */
+               /*
+                * XXX this allows testing whether a user is allowed
+                * to login: if you happen to have a valid pubkey this
+                * message is sent. the message is NEVER sent at all
+                * if a user is not allowed to login. is this an
+                * issue? -markus
+                */
+               if (PRIVSEP(user_key_allowed(authctxt->pw, key))) {
+                       packet_start(SSH2_MSG_USERAUTH_PK_OK);
+                       packet_put_string(pkalg, alen);
+                       packet_put_string(pkblob, blen);
+                       packet_send();
+                       packet_write_wait();
+                       authctxt->postponed = 1;
                }
-               if (authenticated != 1)
-                       auth_clear_options();
-               key_free(key);
        }
+       if (authenticated != 1)
+               auth_clear_options();
+done:
        debug2("userauth_pubkey: authenticated %d pkalg %s", authenticated, pkalg);
+       if (key != NULL)
+               key_free(key);
        xfree(pkalg);
        xfree(pkblob);
 #ifdef HAVE_CYGWIN
@@ -514,8 +526,9 @@ static int
 userauth_hostbased(Authctxt *authctxt)
 {
        Buffer b;
-       Key *key;
-       char *pkalg, *pkblob, *sig, *cuser, *chost, *service;
+       Key *key = NULL;
+       char *pkalg, *cuser, *chost, *service;
+       u_char *pkblob, *sig;
        u_int alen, blen, slen;
        int pktype;
        int authenticated = 0;
@@ -548,7 +561,12 @@ userauth_hostbased(Authctxt *authctxt)
        }
        key = key_from_blob(pkblob, blen);
        if (key == NULL) {
-               debug("userauth_hostbased: cannot decode key: %s", pkalg);
+               error("userauth_hostbased: cannot decode key: %s", pkalg);
+               goto done;
+       }
+       if (key->type != pktype) {
+               error("userauth_hostbased: type mismatch for decoded key "
+                   "(received %d, expected %d)", key->type, pktype);
                goto done;
        }
        service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" :
@@ -568,15 +586,17 @@ userauth_hostbased(Authctxt *authctxt)
        buffer_dump(&b);
 #endif
        /* test for allowed key and correct signature */
-       if (hostbased_key_allowed(authctxt->pw, cuser, chost, key) &&
-           key_verify(key, sig, slen, buffer_ptr(&b), buffer_len(&b)) == 1)
+       authenticated = 0;
+       if (PRIVSEP(hostbased_key_allowed(authctxt->pw, cuser, chost, key)) &&
+           PRIVSEP(key_verify(key, sig, slen, buffer_ptr(&b),
+                       buffer_len(&b))) == 1)
                authenticated = 1;
 
        buffer_clear(&b);
-       key_free(key);
-
 done:
        debug2("userauth_hostbased: authenticated %d", authenticated);
+       if (key != NULL)
+               key_free(key);
        xfree(pkalg);
        xfree(pkblob);
        xfree(cuser);
@@ -599,31 +619,22 @@ static char *
 authmethods_get(void)
 {
        Authmethod *method = NULL;
-       u_int size = 0;
+       Buffer b;
        char *list;
 
+       buffer_init(&b);
        for (method = authmethods; method->name != NULL; method++) {
                if (strcmp(method->name, "none") == 0)
                        continue;
                if (method->enabled != NULL && *(method->enabled) != 0) {
-                       if (size != 0)
-                               size += strlen(DELIM);
-                       size += strlen(method->name);
-               }
-       }
-       size++;                 /* trailing '\0' */
-       list = xmalloc(size);
-       list[0] = '\0';
-
-       for (method = authmethods; method->name != NULL; method++) {
-               if (strcmp(method->name, "none") == 0)
-                       continue;
-               if (method->enabled != NULL && *(method->enabled) != 0) {
-                       if (list[0] != '\0')
-                               strlcat(list, DELIM, size);
-                       strlcat(list, method->name, size);
+                       if (buffer_len(&b) > 0)
+                               buffer_append(&b, ",", 1);
+                       buffer_append(&b, method->name, strlen(method->name));
                }
        }
+       buffer_append(&b, "\0", 1);
+       list = xstrdup(buffer_ptr(&b));
+       buffer_free(&b);
        return list;
 }
 
@@ -735,7 +746,7 @@ user_key_allowed2(struct passwd *pw, Key *key, char *file)
 }
 
 /* check whether given key is in .ssh/authorized_keys* */
-static int
+int
 user_key_allowed(struct passwd *pw, Key *key)
 {
        int success;
@@ -755,7 +766,7 @@ user_key_allowed(struct passwd *pw, Key *key)
 }
 
 /* return 1 if given hostkey is allowed */
-static int
+int
 hostbased_key_allowed(struct passwd *pw, const char *cuser, char *chost,
     Key *key)
 {
@@ -763,7 +774,7 @@ hostbased_key_allowed(struct passwd *pw, const char *cuser, char *chost,
        HostStatus host_status;
        int len;
 
-       resolvedname = get_canonical_hostname(options.reverse_mapping_check);
+       resolvedname = get_canonical_hostname(options.verify_reverse_mapping);
        ipaddr = get_remote_ipaddr();
 
        debug2("userauth_hostbased: chost %s resolvedname %s ipaddr %s",
@@ -801,4 +812,3 @@ hostbased_key_allowed(struct passwd *pw, const char *cuser, char *chost,
 
        return (host_status == HOST_OK);
 }
-
This page took 0.05112 seconds and 4 git commands to generate.