php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Return to Bug #60522
Patch ssh2_agent_authentication revision 2012-05-25 09:46 UTC by casper at bcx dot nl
revision 2012-05-24 20:51 UTC by casper at bcx dot nl

Patch ssh2_agent_authentication for ssh2 Bug #60522

Patch version 2012-05-24 20:51 UTC

Return to Bug #60522 | Download this patch
This patch is obsolete

Obsoleted by patches:

Patch Revisions:

Developer: casper@bcx.nl

diff --git a/ssh2-0.11.3/config.m4 b/ssh2-0.11.3/config.m4
index 462e7a2..9ec40b2 100644
--- a/ssh2-0.11.3/config.m4
+++ b/ssh2-0.11.3/config.m4
@@ -74,6 +74,15 @@ if test "$PHP_SSH2" != "no"; then
   ],[
     -L$SSH2_DIR/lib -lm 
   ])
+
+  PHP_CHECK_LIBRARY($LIBNAME,libssh2_agent_init,
+  [
+    AC_DEFINE(PHP_SSH2_AGENT_AUTH, 1, [Have libssh2 with ssh-agent support])
+  ],[
+    AC_MSG_WARN([libssh2 <= 1.2.3, ssh-agent subsystem support not enabled])
+  ],[
+    -L$SSH2_DIR/lib -lm 
+  ])
   
   PHP_SUBST(SSH2_SHARED_LIBADD)
 
diff --git a/ssh2-0.11.3/ssh2.c b/ssh2-0.11.3/ssh2.c
index 504a054..bdd4c7e 100644
--- a/ssh2-0.11.3/ssh2.c
+++ b/ssh2-0.11.3/ssh2.c
@@ -1104,6 +1104,80 @@ PHP_FUNCTION(ssh2_publickey_list)
 /* }}} */
 #endif /* PHP_SSH2_PUBLICKEY_SUBSYSTEM */
 
+#ifdef PHP_SSH2_AGENT_AUTH
+/* {{{ proto array ssh2_auth_agent(resource session, string username)
+Authenticate using the ssh agent */
+PHP_FUNCTION(ssh2_auth_agent)
+{
+	zval *zsession;
+	char *username;
+	int username_len;
+
+	LIBSSH2_SESSION *session;
+    char *userauthlist;
+	LIBSSH2_AGENT *agent = NULL;
+	int rc;
+    struct libssh2_agent_publickey *identity, *prev_identity = NULL;
+
+	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs", &zsession, &username, &username_len) == FAILURE) {
+		RETURN_FALSE;
+	}
+
+	ZEND_FETCH_RESOURCE(session, LIBSSH2_SESSION*, &zsession, -1, PHP_SSH2_SESSION_RES_NAME, le_ssh2_session);
+
+    /* check what authentication methods are available */ 
+    userauthlist = libssh2_userauth_list(session, username, username_len);
+
+    if (strstr(userauthlist, "publickey") == NULL) {
+		php_error_docref(NULL TSRMLS_CC, E_WARNING, "\"publickey\" authentication is not supported");
+		RETURN_FALSE;
+    }
+ 
+    /* Connect to the ssh-agent */ 
+    agent = libssh2_agent_init(session);
+
+    if (!agent) {
+		php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failure initializing ssh-agent support");
+		RETURN_FALSE;
+    }
+
+    if (libssh2_agent_connect(agent)) {
+		php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failure connecting to ssh-agent");
+		RETURN_FALSE;
+    }
+
+    if (libssh2_agent_list_identities(agent)) {
+		php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failure requesting identities to ssh-agent");
+		RETURN_FALSE;
+    }
+
+    while (1) {
+        rc = libssh2_agent_get_identity(agent, &identity, prev_identity);
+
+   		if (rc == 1) {
+			php_error_docref(NULL TSRMLS_CC, E_WARNING, "Couldn't continue authentication");
+			RETURN_FALSE;        
+		}
+
+        if (rc < 0) {
+			php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failure obtaining identity from ssh-agent support");
+			RETURN_FALSE;
+        }
+
+        if (!libssh2_agent_userauth(agent, username, identity)) {			
+			RETURN_TRUE;           		
+        }
+        prev_identity = identity;
+    }
+}
+/* }}} */
+
+
+
+
+#endif /* PHP_SSH2_AGENT_AUTH */
+
+
 /* ***********************
    * Module Housekeeping *
    *********************** */
@@ -1305,6 +1379,10 @@ zend_function_entry ssh2_functions[] = {
 	PHP_FE(ssh2_publickey_list,					NULL)
 #endif
 
+#ifdef PHP_SSH2_AGENT_AUTH
+	PHP_FE(ssh2_auth_agent,						NULL)
+#endif 
+
 	{NULL, NULL, NULL}
 };
 /* }}} */
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Dec 22 02:01:28 2024 UTC