rcolebaugh / rpms / openssh

Forked from rpms/openssh 2 years ago
Clone

Blame SOURCES/pam_ssh_agent_auth-0.10.3-agent_structure.patch

1d31ef
diff -up openssh-7.4p1/pam_ssh_agent_auth-0.10.3/get_command_line.c.psaa-agent openssh-7.4p1/pam_ssh_agent_auth-0.10.3/get_command_line.c
1d31ef
--- openssh-7.4p1/pam_ssh_agent_auth-0.10.3/get_command_line.c.psaa-agent	2017-02-14 10:19:16.466070259 +0100
1d31ef
+++ openssh-7.4p1/pam_ssh_agent_auth-0.10.3/get_command_line.c	2017-02-14 10:26:10.062866980 +0100
1d31ef
@@ -65,8 +65,8 @@ proc_pid_cmdline(char *** inargv)
1d31ef
                 case EOF:
1d31ef
                 case '\0':
1d31ef
                     if (len > 0) { 
1d31ef
-                        argv = pamsshagentauth_xrealloc(argv, count + 1, sizeof(*argv));
1d31ef
-                        argv[count] = pamsshagentauth_xcalloc(len + 1, sizeof(*argv[count]));
1d31ef
+                        argv = xrealloc(argv, count + 1, sizeof(*argv));
1d31ef
+                        argv[count] = xcalloc(len + 1, sizeof(*argv[count]));
1d31ef
                         strncpy(argv[count++], argbuf, len);
1d31ef
                         memset(argbuf, '\0', MAX_LEN_PER_CMDLINE_ARG + 1);
1d31ef
                         len = 0;
1d31ef
diff -up openssh-7.4p1/pam_ssh_agent_auth-0.10.3/identity.h.psaa-agent openssh-7.4p1/pam_ssh_agent_auth-0.10.3/identity.h
1d31ef
--- openssh-7.4p1/pam_ssh_agent_auth-0.10.3/identity.h.psaa-agent	2016-11-13 04:24:32.000000000 +0100
1d31ef
+++ openssh-7.4p1/pam_ssh_agent_auth-0.10.3/identity.h	2017-02-14 10:19:12.225071868 +0100
1d31ef
@@ -38,6 +38,12 @@
1d31ef
 typedef struct identity Identity;
1d31ef
 typedef struct idlist Idlist;
1d31ef
 
1d31ef
+typedef struct {
1d31ef
+       int     fd;
1d31ef
+       Buffer  identities;
1d31ef
+       int     howmany;
1d31ef
+}      AuthenticationConnection;
1d31ef
+
1d31ef
 struct identity {
1d31ef
     TAILQ_ENTRY(identity) next;
1d31ef
     AuthenticationConnection *ac;   /* set if agent supports key */
1d31ef
diff -up openssh-7.4p1/pam_ssh_agent_auth-0.10.3/iterate_ssh_agent_keys.c.psaa-agent openssh-7.4p1/pam_ssh_agent_auth-0.10.3/iterate_ssh_agent_keys.c
1d31ef
--- openssh-7.4p1/pam_ssh_agent_auth-0.10.3/iterate_ssh_agent_keys.c.psaa-agent	2017-02-14 10:19:12.224071868 +0100
1d31ef
+++ openssh-7.4p1/pam_ssh_agent_auth-0.10.3/iterate_ssh_agent_keys.c	2017-02-14 10:19:12.226071867 +0100
1d31ef
@@ -52,12 +52,15 @@
1d31ef
 #include <openssl/evp.h>
1d31ef
 #include "ssh2.h"
1d31ef
 #include "misc.h"
1d31ef
+#include "ssherr.h"
1d31ef
 
1d31ef
 #include "userauth_pubkey_from_id.h"
1d31ef
 #include "identity.h"
1d31ef
 #include "get_command_line.h"
1d31ef
 extern char **environ;
1d31ef
 
1d31ef
+#define PAM_SSH_AGENT_AUTH_REQUESTv1 101
1d31ef
+
1d31ef
 static char *
1d31ef
 log_action(char ** action, size_t count)
1d31ef
 {
1d31ef
@@ -67,7 +70,7 @@ log_action(char ** action, size_t count)
1d31ef
     if (count == 0)
1d31ef
         return NULL;
1d31ef
    
1d31ef
-    buf = pamsshagentauth_xcalloc((count * MAX_LEN_PER_CMDLINE_ARG) + (count * 3), sizeof(*buf));
1d31ef
+    buf = xcalloc((count * MAX_LEN_PER_CMDLINE_ARG) + (count * 3), sizeof(*buf));
1d31ef
     for (i = 0; i < count; i++) {
1d31ef
         strcat(buf, (i > 0) ? " '" : "'");
1d31ef
         strncat(buf, action[i], MAX_LEN_PER_CMDLINE_ARG);
1d31ef
@@ -80,12 +83,12 @@ void
1d31ef
 agent_action(Buffer *buf, char ** action, size_t count)
1d31ef
 {
1d31ef
     size_t i;
1d31ef
-    pamsshagentauth_buffer_init(buf);
1d31ef
+    buffer_init(buf);
1d31ef
 
1d31ef
-    pamsshagentauth_buffer_put_int(buf, count);
1d31ef
+    buffer_put_int(buf, count);
1d31ef
 
1d31ef
     for (i = 0; i < count; i++) {
1d31ef
-        pamsshagentauth_buffer_put_cstring(buf, action[i]);
1d31ef
+        buffer_put_cstring(buf, action[i]);
1d31ef
     }
1d31ef
 }
1d31ef
 
1d31ef
@@ -109,17 +112,17 @@ pamsshagentauth_session_id2_gen(Buffer *
1d31ef
     char * retc;
1d31ef
     int32_t reti;
1d31ef
 
1d31ef
-    rnd = pamsshagentauth_arc4random();
1d31ef
+    rnd = arc4random();
1d31ef
     cookie_len = ((uint8_t) rnd);
1d31ef
     while (cookie_len < 16) { 
1d31ef
         cookie_len += 16;                                          /* Add 16 bytes to the size to ensure that while the length is random, the length is always reasonable; ticket #18 */
1d31ef
     }
1d31ef
 
1d31ef
-    cookie = pamsshagentauth_xcalloc(1,cookie_len);
1d31ef
+    cookie = xcalloc(1,cookie_len);
1d31ef
 
1d31ef
     for (i = 0; i < cookie_len; i++) {
1d31ef
         if (i % 4 == 0) {
1d31ef
-            rnd = pamsshagentauth_arc4random();
1d31ef
+            rnd = arc4random();
1d31ef
         }
1d31ef
         cookie[i] = (char) rnd;
1d31ef
         rnd >>= 8;
1d31ef
@@ -134,7 +137,7 @@ pamsshagentauth_session_id2_gen(Buffer *
1d31ef
     }
1d31ef
     else {
1d31ef
         action_logbuf = "unknown on this platform";
1d31ef
-        pamsshagentauth_buffer_init(&action_agentbuf); /* stays empty, means unavailable */
1d31ef
+        buffer_init(&action_agentbuf); /* stays empty, means unavailable */
1d31ef
     }
1d31ef
     
1d31ef
     /*
1d31ef
@@ -151,35 +154,35 @@ pamsshagentauth_session_id2_gen(Buffer *
1d31ef
     retc = getcwd(pwd, sizeof(pwd) - 1);
1d31ef
     time(&ts);
1d31ef
 
1d31ef
-    pamsshagentauth_buffer_init(session_id2);
1d31ef
+    buffer_init(session_id2);
1d31ef
 
1d31ef
-    pamsshagentauth_buffer_put_int(session_id2, PAM_SSH_AGENT_AUTH_REQUESTv1);
1d31ef
-    /* pamsshagentauth_debug3("cookie: %s", pamsshagentauth_tohex(cookie, cookie_len)); */
1d31ef
-    pamsshagentauth_buffer_put_string(session_id2, cookie, cookie_len);
1d31ef
-    /* pamsshagentauth_debug3("user: %s", user); */
1d31ef
-    pamsshagentauth_buffer_put_cstring(session_id2, user);
1d31ef
-    /* pamsshagentauth_debug3("ruser: %s", ruser); */
1d31ef
-    pamsshagentauth_buffer_put_cstring(session_id2, ruser);
1d31ef
-    /* pamsshagentauth_debug3("servicename: %s", servicename); */
1d31ef
-    pamsshagentauth_buffer_put_cstring(session_id2, servicename);
1d31ef
-    /* pamsshagentauth_debug3("pwd: %s", pwd); */
1d31ef
+    buffer_put_int(session_id2, PAM_SSH_AGENT_AUTH_REQUESTv1);
1d31ef
+    /* debug3("cookie: %s", tohex(cookie, cookie_len)); */
1d31ef
+    buffer_put_string(session_id2, cookie, cookie_len);
1d31ef
+    /* debug3("user: %s", user); */
1d31ef
+    buffer_put_cstring(session_id2, user);
1d31ef
+    /* debug3("ruser: %s", ruser); */
1d31ef
+    buffer_put_cstring(session_id2, ruser);
1d31ef
+    /* debug3("servicename: %s", servicename); */
1d31ef
+    buffer_put_cstring(session_id2, servicename);
1d31ef
+    /* debug3("pwd: %s", pwd); */
1d31ef
     if(retc)
1d31ef
-        pamsshagentauth_buffer_put_cstring(session_id2, pwd);
1d31ef
+        buffer_put_cstring(session_id2, pwd);
1d31ef
     else
1d31ef
-        pamsshagentauth_buffer_put_cstring(session_id2, "");
1d31ef
-    /* pamsshagentauth_debug3("action: %s", action_logbuf); */
1d31ef
-    pamsshagentauth_buffer_put_string(session_id2, action_agentbuf.buf + action_agentbuf.offset, action_agentbuf.end - action_agentbuf.offset);
1d31ef
+        buffer_put_cstring(session_id2, "");
1d31ef
+    /* debug3("action: %s", action_logbuf); */
1d31ef
+    buffer_put_string(session_id2, sshbuf_ptr(&action_agentbuf), sshbuf_len(&action_agentbuf));
1d31ef
     if (free_logbuf) { 
1d31ef
         free(action_logbuf);
1d31ef
-        pamsshagentauth_buffer_free(&action_agentbuf);
1d31ef
+        buffer_free(&action_agentbuf);
1d31ef
     }
1d31ef
-    /* pamsshagentauth_debug3("hostname: %s", hostname); */
1d31ef
+    /* debug3("hostname: %s", hostname); */
1d31ef
     if(reti >= 0)
1d31ef
-        pamsshagentauth_buffer_put_cstring(session_id2, hostname);
1d31ef
+        buffer_put_cstring(session_id2, hostname);
1d31ef
     else
1d31ef
-        pamsshagentauth_buffer_put_cstring(session_id2, "");
1d31ef
-    /* pamsshagentauth_debug3("ts: %ld", ts); */
1d31ef
-    pamsshagentauth_buffer_put_int64(session_id2, (uint64_t) ts);
1d31ef
+        buffer_put_cstring(session_id2, "");
1d31ef
+    /* debug3("ts: %ld", ts); */
1d31ef
+    buffer_put_int64(session_id2, (uint64_t) ts);
1d31ef
 
1d31ef
     free(cookie);
1d31ef
     return;
1d31ef
@@ -288,39 +291,47 @@ pamsshagentauth_find_authorized_keys(con
1d31ef
 {
1d31ef
     Buffer session_id2 = { 0 };
1d31ef
     Identity *id;
1d31ef
-    Key *key;
1d31ef
     AuthenticationConnection *ac;
1d31ef
-    char *comment;
1d31ef
     uint8_t retval = 0;
1d31ef
     uid_t uid = getpwnam(ruser)->pw_uid;
1d31ef
+    struct ssh_identitylist *idlist;
1d31ef
+    int r;
1d31ef
+    unsigned int i;
1d31ef
 
1d31ef
     OpenSSL_add_all_digests();
1d31ef
     pamsshagentauth_session_id2_gen(&session_id2, user, ruser, servicename);
1d31ef
 
1d31ef
     if ((ac = ssh_get_authentication_connection_for_uid(uid))) {
1d31ef
-        pamsshagentauth_verbose("Contacted ssh-agent of user %s (%u)", ruser, uid);
1d31ef
-        for (key = ssh_get_first_identity(ac, &comment, 2); key != NULL; key = ssh_get_next_identity(ac, &comment, 2)) 
1d31ef
+        verbose("Contacted ssh-agent of user %s (%u)", ruser, uid);
1d31ef
+		if ((r = ssh_fetch_identitylist(ac->fd, 2,
1d31ef
+		    &idlist)) != 0) {
1d31ef
+			if (r != SSH_ERR_AGENT_NO_IDENTITIES)
1d31ef
+				fprintf(stderr, "error fetching identities for "
1d31ef
+				    "protocol %d: %s\n", 2, ssh_err(r));
1d31ef
+		} else {
1d31ef
+		for (i = 0; i < idlist->nkeys; i++)
1d31ef
         {
1d31ef
-            if(key != NULL) {
1d31ef
-                id = pamsshagentauth_xcalloc(1, sizeof(*id));
1d31ef
-                id->key = key;
1d31ef
-                id->filename = comment;
1d31ef
+            if(idlist->keys[i] != NULL) {
1d31ef
+                id = xcalloc(1, sizeof(*id));
1d31ef
+                id->key = idlist->keys[i];
1d31ef
+                id->filename = idlist->comments[i];
1d31ef
                 id->ac = ac;
1d31ef
                 if(userauth_pubkey_from_id(ruser, id, &session_id2)) {
1d31ef
                     retval = 1;
1d31ef
                 }
1d31ef
-                free(id->filename);
1d31ef
-                pamsshagentauth_key_free(id->key);
1d31ef
                 free(id);
1d31ef
                 if(retval == 1)
1d31ef
                     break;
1d31ef
             }
1d31ef
         }
1d31ef
-        pamsshagentauth_buffer_free(&session_id2);
1d31ef
-        ssh_close_authentication_connection(ac);
1d31ef
+        buffer_free(&session_id2);
1d31ef
+        ssh_free_identitylist(idlist);
1d31ef
+        ssh_close_authentication_socket(ac->fd);
1d31ef
+        free(ac);
1d31ef
+        }
1d31ef
     }
1d31ef
     else {
1d31ef
-        pamsshagentauth_verbose("No ssh-agent could be contacted");
1d31ef
+        verbose("No ssh-agent could be contacted");
1d31ef
     }
1d31ef
     /* pamsshagentauth_xfree(session_id2); */
1d31ef
     EVP_cleanup();
1d31ef
diff -up openssh-7.4p1/pam_ssh_agent_auth-0.10.3/pam_ssh_agent_auth.c.psaa-agent openssh-7.4p1/pam_ssh_agent_auth-0.10.3/pam_ssh_agent_auth.c
1d31ef
--- openssh-7.4p1/pam_ssh_agent_auth-0.10.3/pam_ssh_agent_auth.c.psaa-agent	2017-02-14 10:19:12.223071868 +0100
1d31ef
+++ openssh-7.4p1/pam_ssh_agent_auth-0.10.3/pam_ssh_agent_auth.c	2017-02-14 10:19:12.226071867 +0100
1d31ef
@@ -104,7 +104,7 @@ pam_sm_authenticate(pam_handle_t * pamh,
1d31ef
  * a patch 8-)
1d31ef
  */
1d31ef
 #if ! HAVE___PROGNAME || HAVE_BUNDLE
1d31ef
-    __progname = pamsshagentauth_xstrdup(servicename);
1d31ef
+    __progname = xstrdup(servicename);
1d31ef
 #endif
1d31ef
 
1d31ef
     for(i = argc, argv_ptr = (char **) argv; i > 0; ++argv_ptr, i--) {
1d31ef
@@ -130,11 +130,11 @@ pam_sm_authenticate(pam_handle_t * pamh,
1d31ef
 #endif
1d31ef
     }
1d31ef
 
1d31ef
-    pamsshagentauth_log_init(__progname, log_lvl, facility, getenv("PAM_SSH_AGENT_AUTH_DEBUG") ? 1 : 0);
1d31ef
+    log_init(__progname, log_lvl, facility, getenv("PAM_SSH_AGENT_AUTH_DEBUG") ? 1 : 0);
1d31ef
     pam_get_item(pamh, PAM_USER, (void *) &user);
1d31ef
     pam_get_item(pamh, PAM_RUSER, (void *) &ruser_ptr);
1d31ef
 
1d31ef
-    pamsshagentauth_verbose("Beginning pam_ssh_agent_auth for user %s", user);
1d31ef
+    verbose("Beginning pam_ssh_agent_auth for user %s", user);
1d31ef
 
1d31ef
     if(ruser_ptr) {
1d31ef
         strncpy(ruser, ruser_ptr, sizeof(ruser) - 1);
1d31ef
@@ -149,12 +149,12 @@ pam_sm_authenticate(pam_handle_t * pamh,
1d31ef
 #ifdef ENABLE_SUDO_HACK
1d31ef
         if( (strlen(sudo_service_name) > 0) && strncasecmp(servicename, sudo_service_name, sizeof(sudo_service_name) - 1) == 0 && getenv("SUDO_USER") ) {
1d31ef
             strncpy(ruser, getenv("SUDO_USER"), sizeof(ruser) - 1 );
1d31ef
-            pamsshagentauth_verbose( "Using environment variable SUDO_USER (%s)", ruser );
1d31ef
+            verbose( "Using environment variable SUDO_USER (%s)", ruser );
1d31ef
         } else
1d31ef
 #endif
1d31ef
         {
1d31ef
             if( ! getpwuid(getuid()) ) {
1d31ef
-                pamsshagentauth_verbose("Unable to getpwuid(getuid())");
1d31ef
+                verbose("Unable to getpwuid(getuid())");
1d31ef
                 goto cleanexit;
1d31ef
             }
1d31ef
             strncpy(ruser, getpwuid(getuid())->pw_name, sizeof(ruser) - 1);
1d31ef
@@ -163,11 +163,11 @@ pam_sm_authenticate(pam_handle_t * pamh,
1d31ef
 
1d31ef
     /* Might as well explicitely confirm the user exists here */
1d31ef
     if(! getpwnam(ruser) ) {
1d31ef
-        pamsshagentauth_verbose("getpwnam(%s) failed, bailing out", ruser);
1d31ef
+        verbose("getpwnam(%s) failed, bailing out", ruser);
1d31ef
         goto cleanexit;
1d31ef
     }
1d31ef
     if( ! getpwnam(user) ) {
1d31ef
-        pamsshagentauth_verbose("getpwnam(%s) failed, bailing out", user);
1d31ef
+        verbose("getpwnam(%s) failed, bailing out", user);
1d31ef
         goto cleanexit;
1d31ef
     }
1d31ef
 
1d31ef
@@ -177,8 +177,8 @@ pam_sm_authenticate(pam_handle_t * pamh,
1d31ef
          */
1d31ef
         parse_authorized_key_file(user, authorized_keys_file_input);
1d31ef
     } else {
1d31ef
-        pamsshagentauth_verbose("Using default file=/etc/security/authorized_keys");
1d31ef
-        authorized_keys_file = pamsshagentauth_xstrdup("/etc/security/authorized_keys");
1d31ef
+        verbose("Using default file=/etc/security/authorized_keys");
1d31ef
+        authorized_keys_file = xstrdup("/etc/security/authorized_keys");
1d31ef
     }
1d31ef
 
1d31ef
     /*
1d31ef
@@ -187,19 +187,19 @@ pam_sm_authenticate(pam_handle_t * pamh,
1d31ef
      */
1d31ef
 
1d31ef
     if(user && strlen(ruser) > 0) {
1d31ef
-        pamsshagentauth_verbose("Attempting authentication: `%s' as `%s' using %s", ruser, user, authorized_keys_file);
1d31ef
+        verbose("Attempting authentication: `%s' as `%s' using %s", ruser, user, authorized_keys_file);
1d31ef
 
1d31ef
         /*
1d31ef
          * this pw_uid is used to validate the SSH_AUTH_SOCK, and so must be the uid of the ruser invoking the program, not the target-user
1d31ef
          */
1d31ef
         if(pamsshagentauth_find_authorized_keys(user, ruser, servicename)) { /* getpwnam(ruser)->pw_uid)) { */
1d31ef
-            pamsshagentauth_logit("Authenticated: `%s' as `%s' using %s", ruser, user, authorized_keys_file);
1d31ef
+            logit("Authenticated: `%s' as `%s' using %s", ruser, user, authorized_keys_file);
1d31ef
             retval = PAM_SUCCESS;
1d31ef
         } else {
1d31ef
-            pamsshagentauth_logit("Failed Authentication: `%s' as `%s' using %s", ruser, user, authorized_keys_file);
1d31ef
+            logit("Failed Authentication: `%s' as `%s' using %s", ruser, user, authorized_keys_file);
1d31ef
         }
1d31ef
     } else {
1d31ef
-        pamsshagentauth_logit("No %s specified, cannot continue with this form of authentication", (user) ? "ruser" : "user" );
1d31ef
+        logit("No %s specified, cannot continue with this form of authentication", (user) ? "ruser" : "user" );
1d31ef
     }
1d31ef
 
1d31ef
 cleanexit:
1d31ef
diff -up openssh-7.4p1/pam_ssh_agent_auth-0.10.3/pam_user_authorized_keys.c.psaa-agent openssh-7.4p1/pam_ssh_agent_auth-0.10.3/pam_user_authorized_keys.c
1d31ef
--- openssh-7.4p1/pam_ssh_agent_auth-0.10.3/pam_user_authorized_keys.c.psaa-agent	2016-11-13 04:24:32.000000000 +0100
1d31ef
+++ openssh-7.4p1/pam_ssh_agent_auth-0.10.3/pam_user_authorized_keys.c	2017-02-14 10:19:12.226071867 +0100
1d31ef
@@ -117,12 +117,12 @@ parse_authorized_key_file(const char *us
1d31ef
         } else {
1d31ef
             slash_ptr = strchr(auth_keys_file_buf, '/');
1d31ef
             if(!slash_ptr)
1d31ef
-                pamsshagentauth_fatal
1d31ef
+                fatal
1d31ef
                     ("cannot expand tilde in path without a `/'");
1d31ef
 
1d31ef
             owner_uname_len = slash_ptr - auth_keys_file_buf - 1;
1d31ef
             if(owner_uname_len > (sizeof(owner_uname) - 1))
1d31ef
-                pamsshagentauth_fatal("Username too long");
1d31ef
+                fatal("Username too long");
1d31ef
 
1d31ef
             strncat(owner_uname, auth_keys_file_buf + 1, owner_uname_len);
1d31ef
             if(!authorized_keys_file_allowed_owner_uid)
1d31ef
@@ -130,11 +130,11 @@ parse_authorized_key_file(const char *us
1d31ef
                     getpwnam(owner_uname)->pw_uid;
1d31ef
         }
1d31ef
         authorized_keys_file =
1d31ef
-            pamsshagentauth_tilde_expand_filename(auth_keys_file_buf,
1d31ef
+            tilde_expand_filename(auth_keys_file_buf,
1d31ef
                                                   authorized_keys_file_allowed_owner_uid);
1d31ef
         strncpy(auth_keys_file_buf, authorized_keys_file,
1d31ef
                 sizeof(auth_keys_file_buf) - 1);
1d31ef
-        pamsshagentauth_xfree(authorized_keys_file)        /* when we
1d31ef
+        free(authorized_keys_file)        /* when we
1d31ef
                                                               percent_expand
1d31ef
                                                               later, we'd step
1d31ef
                                                               on this, so free
1d31ef
@@ -150,7 +150,7 @@ parse_authorized_key_file(const char *us
1d31ef
     strncat(hostname, fqdn, strcspn(fqdn, "."));
1d31ef
 #endif
1d31ef
     authorized_keys_file =
1d31ef
-        pamsshagentauth_percent_expand(auth_keys_file_buf, "h",
1d31ef
+        percent_expand(auth_keys_file_buf, "h",
1d31ef
                                        getpwnam(user)->pw_dir, "H", hostname,
1d31ef
                                        "f", fqdn, "u", user, NULL);
1d31ef
 }
1d31ef
diff -up openssh-7.4p1/pam_ssh_agent_auth-0.10.3/pam_user_key_allowed2.c.psaa-agent openssh-7.4p1/pam_ssh_agent_auth-0.10.3/pam_user_key_allowed2.c
1d31ef
--- openssh-7.4p1/pam_ssh_agent_auth-0.10.3/pam_user_key_allowed2.c.psaa-agent	2017-02-14 10:19:12.224071868 +0100
1d31ef
+++ openssh-7.4p1/pam_ssh_agent_auth-0.10.3/pam_user_key_allowed2.c	2017-02-14 10:19:12.226071867 +0100
1d31ef
@@ -35,6 +35,7 @@
1d31ef
 #include <sys/stat.h>
1d31ef
 #include <sys/wait.h>
1d31ef
 #include <fcntl.h>
1d31ef
+#include <unistd.h>
1d31ef
 
1d31ef
 #include <pwd.h>
1d31ef
 #include <stdio.h>
1d31ef
@@ -53,6 +54,7 @@
1d31ef
 #include "misc.h"
1d31ef
 #include "secure_filename.h"
1d31ef
 #include "uidswap.h"
1d31ef
+#include "digest.h"
1d31ef
 
1d31ef
 #include "identity.h"
1d31ef
 
1d31ef
@@ -68,7 +70,7 @@ pamsshagentauth_check_authkeys_file(FILE
1d31ef
     char *fp;
1d31ef
 
1d31ef
     found_key = 0;
1d31ef
-    found = pamsshagentauth_key_new(key->type);
1d31ef
+    found = key_new(key->type);
1d31ef
 
1d31ef
     while(read_keyfile_line(f, file, line, sizeof(line), &linenum) != -1) {
1d31ef
         char *cp = NULL; /* *key_options = NULL; */
1d31ef
@@ -78,11 +80,11 @@ pamsshagentauth_check_authkeys_file(FILE
1d31ef
         if(!*cp || *cp == '\n' || *cp == '#')
1d31ef
             continue;
1d31ef
 
1d31ef
-        if(pamsshagentauth_key_read(found, &cp) != 1) {
1d31ef
+        if(key_read(found, &cp) != 1) {
1d31ef
             /* no key? check if there are options for this key */
1d31ef
             int quoted = 0;
1d31ef
 
1d31ef
-            pamsshagentauth_verbose("user_key_allowed: check options: '%s'", cp);
1d31ef
+            verbose("user_key_allowed: check options: '%s'", cp);
1d31ef
             /* key_options = cp; */
1d31ef
             for(; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
1d31ef
                 if(*cp == '\\' && cp[1] == '"')
1d31ef
@@ -92,26 +94,26 @@ pamsshagentauth_check_authkeys_file(FILE
1d31ef
             }
1d31ef
             /* Skip remaining whitespace. */
1d31ef
             for(; *cp == ' ' || *cp == '\t'; cp++);
1d31ef
-            if(pamsshagentauth_key_read(found, &cp) != 1) {
1d31ef
-                pamsshagentauth_verbose("user_key_allowed: advance: '%s'", cp);
1d31ef
+            if(key_read(found, &cp) != 1) {
1d31ef
+                verbose("user_key_allowed: advance: '%s'", cp);
1d31ef
                 /* still no key? advance to next line */
1d31ef
                 continue;
1d31ef
             }
1d31ef
         }
1d31ef
-        if(pamsshagentauth_key_equal(found, key)) {
1d31ef
+        if(key_equal(found, key)) {
1d31ef
             found_key = 1;
1d31ef
-            pamsshagentauth_logit("matching key found: file/command %s, line %lu", file,
1d31ef
+            logit("matching key found: file/command %s, line %lu", file,
1d31ef
                                   linenum);
1d31ef
-            fp = pamsshagentauth_key_fingerprint(found, SSH_FP_MD5, SSH_FP_HEX);
1d31ef
-            pamsshagentauth_logit("Found matching %s key: %s",
1d31ef
-                                  pamsshagentauth_key_type(found), fp);
1d31ef
+            fp = key_fingerprint(found, SSH_FP_MD5, SSH_FP_HEX);
1d31ef
+            logit("Found matching %s key: %s",
1d31ef
+                                  key_type(found), fp);
1d31ef
             free(fp);
1d31ef
             break;
1d31ef
         }
1d31ef
     }
1d31ef
-    pamsshagentauth_key_free(found);
1d31ef
+    key_free(found);
1d31ef
     if(!found_key)
1d31ef
-        pamsshagentauth_verbose("key not found");
1d31ef
+        verbose("key not found");
1d31ef
     return found_key;
1d31ef
 }
1d31ef
 
1d31ef
@@ -128,11 +130,11 @@ pamsshagentauth_user_key_allowed2(struct
1d31ef
     char buf[SSH_MAX_PUBKEY_BYTES];
1d31ef
 
1d31ef
     /* Temporarily use the user's uid. */
1d31ef
-    pamsshagentauth_verbose("trying public key file %s", file);
1d31ef
+    verbose("trying public key file %s", file);
1d31ef
 
1d31ef
     /* Fail not so quietly if file does not exist */
1d31ef
     if(stat(file, &st) < 0) {
1d31ef
-        pamsshagentauth_verbose("File not found: %s", file);
1d31ef
+        verbose("File not found: %s", file);
1d31ef
         return 0;
1d31ef
     }
1d31ef
 
1d31ef
@@ -144,7 +146,7 @@ pamsshagentauth_user_key_allowed2(struct
1d31ef
 
1d31ef
     if(pamsshagentauth_secure_filename(f, file, pw, buf, sizeof(buf)) != 0) {
1d31ef
         fclose(f);
1d31ef
-        pamsshagentauth_logit("Authentication refused: %s", buf);
1d31ef
+        logit("Authentication refused: %s", buf);
1d31ef
         return 0;
1d31ef
     }
1d31ef
 
1d31ef
@@ -187,44 +189,44 @@ pamsshagentauth_user_key_command_allowed
1d31ef
     else {
1d31ef
         pw = getpwnam(authorized_keys_command_user);
1d31ef
         if(pw == NULL) {
1d31ef
-            pamsshagentauth_logerror("authorized_keys_command_user \"%s\" not found: %s",
1d31ef
+            error("authorized_keys_command_user \"%s\" not found: %s",
1d31ef
                  authorized_keys_command_user, strerror(errno));
1d31ef
             return 0;
1d31ef
         }
1d31ef
     }
1d31ef
 
1d31ef
-    pamsshagentauth_temporarily_use_uid(pw);
1d31ef
+    temporarily_use_uid(pw);
1d31ef
 
1d31ef
     if(stat(authorized_keys_command, &st) < 0) {
1d31ef
-        pamsshagentauth_logerror
1d31ef
+        error
1d31ef
             ("Could not stat AuthorizedKeysCommand \"%s\": %s",
1d31ef
              authorized_keys_command, strerror(errno));
1d31ef
         goto out;
1d31ef
     }
1d31ef
     if(pamsshagentauth_auth_secure_path
1d31ef
        (authorized_keys_command, &st, NULL, 0, errmsg, sizeof(errmsg)) != 0) {
1d31ef
-        pamsshagentauth_logerror("Unsafe AuthorizedKeysCommand: %s", errmsg);
1d31ef
+        error("Unsafe AuthorizedKeysCommand: %s", errmsg);
1d31ef
         goto out;
1d31ef
     }
1d31ef
 
1d31ef
     /* open the pipe and read the keys */
1d31ef
     if(pipe(p) != 0) {
1d31ef
-        pamsshagentauth_logerror("%s: pipe: %s", __func__, strerror(errno));
1d31ef
+        error("%s: pipe: %s", __func__, strerror(errno));
1d31ef
         goto out;
1d31ef
     }
1d31ef
 
1d31ef
-    pamsshagentauth_debug("Running AuthorizedKeysCommand: \"%s\" as \"%s\" with argument: \"%s\"",
1d31ef
+    debug("Running AuthorizedKeysCommand: \"%s\" as \"%s\" with argument: \"%s\"",
1d31ef
                           authorized_keys_command, pw->pw_name, username);
1d31ef
 
1d31ef
     /* 
1d31ef
      * Don't want to call this in the child, where it can fatal() and
1d31ef
      * run cleanup_exit() code.
1d31ef
      */
1d31ef
-    pamsshagentauth_restore_uid();
1d31ef
+    restore_uid();
1d31ef
 
1d31ef
     switch ((pid = fork())) {
1d31ef
     case -1:                                              /* error */
1d31ef
-        pamsshagentauth_logerror("%s: fork: %s", __func__, strerror(errno));
1d31ef
+        error("%s: fork: %s", __func__, strerror(errno));
1d31ef
         close(p[0]);
1d31ef
         close(p[1]);
1d31ef
         return 0;
1d31ef
@@ -234,13 +236,13 @@ pamsshagentauth_user_key_command_allowed
1d31ef
 
1d31ef
         /* do this before the setresuid so thta they can be logged */
1d31ef
         if((devnull = open(_PATH_DEVNULL, O_RDWR)) == -1) {
1d31ef
-            pamsshagentauth_logerror("%s: open %s: %s", __func__, _PATH_DEVNULL,
1d31ef
+            error("%s: open %s: %s", __func__, _PATH_DEVNULL,
1d31ef
                                      strerror(errno));
1d31ef
             _exit(1);
1d31ef
         }
1d31ef
         if(dup2(devnull, STDIN_FILENO) == -1 || dup2(p[1], STDOUT_FILENO) == -1
1d31ef
            || dup2(devnull, STDERR_FILENO) == -1) {
1d31ef
-            pamsshagentauth_logerror("%s: dup2: %s", __func__, strerror(errno));
1d31ef
+            error("%s: dup2: %s", __func__, strerror(errno));
1d31ef
             _exit(1);
1d31ef
         }
1d31ef
 #if defined(HAVE_SETRESGID) && !defined(BROKEN_SETRESGID)
1d31ef
@@ -248,7 +250,7 @@ pamsshagentauth_user_key_command_allowed
1d31ef
 #else
1d31ef
         if (setgid(pw->pw_gid) != 0 || setegid(pw->pw_gid) != 0) {
1d31ef
 #endif
1d31ef
-            pamsshagentauth_logerror("setresgid %u: %s", (u_int) pw->pw_gid,
1d31ef
+            error("setresgid %u: %s", (u_int) pw->pw_gid,
1d31ef
                                      strerror(errno));
1d31ef
             _exit(1);
1d31ef
         }
1d31ef
@@ -258,7 +260,7 @@ pamsshagentauth_user_key_command_allowed
1d31ef
 #else
1d31ef
         if (setuid(pw->pw_uid) != 0 || seteuid(pw->pw_uid) != 0) {
1d31ef
 #endif
1d31ef
-            pamsshagentauth_logerror("setresuid %u: %s", (u_int) pw->pw_uid,
1d31ef
+            error("setresuid %u: %s", (u_int) pw->pw_uid,
1d31ef
                                      strerror(errno));
1d31ef
             _exit(1);
1d31ef
         }
1d31ef
@@ -270,18 +272,18 @@ pamsshagentauth_user_key_command_allowed
1d31ef
 
1d31ef
         /* pretty sure this will barf because we are now suid, but since we
1d31ef
            should't reach this anyway, I'll leave it here */
1d31ef
-        pamsshagentauth_logerror("AuthorizedKeysCommand %s exec failed: %s",
1d31ef
+        error("AuthorizedKeysCommand %s exec failed: %s",
1d31ef
                                  authorized_keys_command, strerror(errno));
1d31ef
         _exit(127);
1d31ef
     default:                                              /* parent */
1d31ef
         break;
1d31ef
     }
1d31ef
 
1d31ef
-    pamsshagentauth_temporarily_use_uid(pw);
1d31ef
+    temporarily_use_uid(pw);
1d31ef
 
1d31ef
     close(p[1]);
1d31ef
     if((f = fdopen(p[0], "r")) == NULL) {
1d31ef
-        pamsshagentauth_logerror("%s: fdopen: %s", __func__, strerror(errno));
1d31ef
+        error("%s: fdopen: %s", __func__, strerror(errno));
1d31ef
         close(p[0]);
1d31ef
         /* Don't leave zombie child */
1d31ef
         while(waitpid(pid, NULL, 0) == -1 && errno == EINTR);
1d31ef
@@ -292,22 +294,22 @@ pamsshagentauth_user_key_command_allowed
1d31ef
 
1d31ef
     while(waitpid(pid, &status, 0) == -1) {
1d31ef
         if(errno != EINTR) {
1d31ef
-            pamsshagentauth_logerror("%s: waitpid: %s", __func__,
1d31ef
+            error("%s: waitpid: %s", __func__,
1d31ef
                                      strerror(errno));
1d31ef
             goto out;
1d31ef
         }
1d31ef
     }
1d31ef
     if(WIFSIGNALED(status)) {
1d31ef
-        pamsshagentauth_logerror("AuthorizedKeysCommand %s exited on signal %d",
1d31ef
+        error("AuthorizedKeysCommand %s exited on signal %d",
1d31ef
                                  authorized_keys_command, WTERMSIG(status));
1d31ef
         goto out;
1d31ef
     } else if(WEXITSTATUS(status) != 0) {
1d31ef
-        pamsshagentauth_logerror("AuthorizedKeysCommand %s returned status %d",
1d31ef
+        error("AuthorizedKeysCommand %s returned status %d",
1d31ef
                                  authorized_keys_command, WEXITSTATUS(status));
1d31ef
         goto out;
1d31ef
     }
1d31ef
     found_key = ok;
1d31ef
   out:
1d31ef
-    pamsshagentauth_restore_uid();
1d31ef
+    restore_uid();
1d31ef
     return found_key;
1d31ef
 }
1d31ef
diff -up openssh-7.4p1/pam_ssh_agent_auth-0.10.3/secure_filename.c.psaa-agent openssh-7.4p1/pam_ssh_agent_auth-0.10.3/secure_filename.c
1d31ef
--- openssh-7.4p1/pam_ssh_agent_auth-0.10.3/secure_filename.c.psaa-agent	2016-11-13 04:24:32.000000000 +0100
1d31ef
+++ openssh-7.4p1/pam_ssh_agent_auth-0.10.3/secure_filename.c	2017-02-14 10:19:12.226071867 +0100
1d31ef
@@ -80,7 +80,7 @@ pamsshagentauth_auth_secure_path(const c
1d31ef
 	int comparehome = 0;
1d31ef
 	struct stat st;
1d31ef
 
1d31ef
-    pamsshagentauth_verbose("auth_secure_filename: checking for uid: %u", uid);
1d31ef
+    verbose("auth_secure_filename: checking for uid: %u", uid);
1d31ef
 
1d31ef
 	if (realpath(name, buf) == NULL) {
1d31ef
 		snprintf(err, errlen, "realpath %s failed: %s", name,
1d31ef
@@ -115,9 +115,9 @@ pamsshagentauth_auth_secure_path(const c
1d31ef
 			snprintf(err, errlen, "dirname() failed");
1d31ef
 			return -1;
1d31ef
 		}
1d31ef
-		pamsshagentauth_strlcpy(buf, cp, sizeof(buf));
1d31ef
+		strlcpy(buf, cp, sizeof(buf));
1d31ef
 
1d31ef
-		pamsshagentauth_verbose("secure_filename: checking '%s'", buf);
1d31ef
+		verbose("secure_filename: checking '%s'", buf);
1d31ef
 		if (stat(buf, &st) < 0 ||
1d31ef
 		    (st.st_uid != 0 && st.st_uid != uid) ||
1d31ef
 		    (st.st_mode & 022) != 0) {
1d31ef
@@ -128,7 +128,7 @@ pamsshagentauth_auth_secure_path(const c
1d31ef
 
1d31ef
 		/* If are passed the homedir then we can stop */
1d31ef
 		if (comparehome && strcmp(homedir, buf) == 0) {
1d31ef
-			pamsshagentauth_verbose("secure_filename: terminating check at '%s'",
1d31ef
+			verbose("secure_filename: terminating check at '%s'",
1d31ef
 			    buf);
1d31ef
 			break;
1d31ef
 		}
1d31ef
diff -up openssh-7.4p1/pam_ssh_agent_auth-0.10.3/userauth_pubkey_from_id.c.psaa-agent openssh-7.4p1/pam_ssh_agent_auth-0.10.3/userauth_pubkey_from_id.c
1d31ef
--- openssh-7.4p1/pam_ssh_agent_auth-0.10.3/userauth_pubkey_from_id.c.psaa-agent	2017-02-14 10:19:12.224071868 +0100
1d31ef
+++ openssh-7.4p1/pam_ssh_agent_auth-0.10.3/userauth_pubkey_from_id.c	2017-02-14 10:19:12.226071867 +0100
1d31ef
@@ -48,6 +48,8 @@
1d31ef
 #include "identity.h"
1d31ef
 #include "pam_user_authorized_keys.h"
1d31ef
 
1d31ef
+#define SSH2_MSG_USERAUTH_TRUST_REQUEST          54
1d31ef
+
1d31ef
 /* extern u_char  *session_id2;
1d31ef
 extern uint8_t  session_id_len;
1d31ef
  */
9a9eba
@@ -58,40 +60,41 @@ userauth_pubkey_from_id(const char *ruse
1d31ef
     Buffer          b = { 0 };
1d31ef
     char           *pkalg = NULL;
1d31ef
     u_char         *pkblob = NULL, *sig = NULL;
1d31ef
-    u_int           blen = 0, slen = 0;
1d31ef
+    u_int           blen = 0;
1d31ef
+    size_t          slen = 0;
1d31ef
     int             authenticated = 0;
1d31ef
 
1d31ef
     pkalg = (char *) key_ssh_name(id->key);
9a9eba
 
9a9eba
+    /* construct packet to sign and test */
9a9eba
+    buffer_init(&b);
9a9eba
+
9a9eba
     /* first test if this key is even allowed */
1d31ef
     if(! pam_user_key_allowed(ruser, id->key))
1d31ef
         goto user_auth_clean_exit;
1d31ef
 
1d31ef
-    if(pamsshagentauth_key_to_blob(id->key, &pkblob, &blen) == 0)
1d31ef
+    if(key_to_blob(id->key, &pkblob, &blen) == 0)
1d31ef
         goto user_auth_clean_exit;
1d31ef
 
9a9eba
-    /* construct packet to sign and test */
1d31ef
-    pamsshagentauth_buffer_init(&b);
9a9eba
-
1d31ef
-    pamsshagentauth_buffer_put_string(&b, session_id2->buf + session_id2->offset, session_id2->end - session_id2->offset);
1d31ef
-    pamsshagentauth_buffer_put_char(&b, SSH2_MSG_USERAUTH_TRUST_REQUEST); 
1d31ef
-    pamsshagentauth_buffer_put_cstring(&b, ruser);
1d31ef
-    pamsshagentauth_buffer_put_cstring(&b, "pam_ssh_agent_auth");
1d31ef
-    pamsshagentauth_buffer_put_cstring(&b, "publickey");
1d31ef
-    pamsshagentauth_buffer_put_char(&b, 1);
1d31ef
-    pamsshagentauth_buffer_put_cstring(&b, pkalg);
1d31ef
-    pamsshagentauth_buffer_put_string(&b, pkblob, blen);
1d31ef
+    buffer_put_string(&b, sshbuf_ptr(session_id2), sshbuf_len(session_id2));
1d31ef
+    buffer_put_char(&b, SSH2_MSG_USERAUTH_TRUST_REQUEST); 
1d31ef
+    buffer_put_cstring(&b, ruser);
1d31ef
+    buffer_put_cstring(&b, "pam_ssh_agent_auth");
1d31ef
+    buffer_put_cstring(&b, "publickey");
1d31ef
+    buffer_put_char(&b, 1);
1d31ef
+    buffer_put_cstring(&b, pkalg);
1d31ef
+    buffer_put_string(&b, pkblob, blen);
1d31ef
 
1d31ef
-    if(ssh_agent_sign(id->ac, id->key, &sig, &slen, pamsshagentauth_buffer_ptr(&b), pamsshagentauth_buffer_len(&b)) != 0)
1d31ef
+    if(ssh_agent_sign(id->ac->fd, id->key, &sig, &slen, buffer_ptr(&b), buffer_len(&b), NULL, 0) != 0)
1d31ef
         goto user_auth_clean_exit;
1d31ef
 
1d31ef
     /* test for correct signature */
1d31ef
-    if(pamsshagentauth_key_verify(id->key, sig, slen, pamsshagentauth_buffer_ptr(&b), pamsshagentauth_buffer_len(&b)) == 1)
1d31ef
+    if(key_verify(id->key, sig, slen, buffer_ptr(&b), buffer_len(&b)) == 1)
1d31ef
         authenticated = 1;
1d31ef
 
1d31ef
   user_auth_clean_exit:
1d31ef
     /* if(&b != NULL) */
1d31ef
-    pamsshagentauth_buffer_free(&b);
1d31ef
+    buffer_free(&b);
1d31ef
     if(sig != NULL)
1d31ef
         free(sig);
1d31ef
     if(pkblob != NULL)