Browse Source

PHP MysqlND module

isundil 5 years ago
parent
commit
f44932cfc0
3 changed files with 132 additions and 0 deletions
  1. 26 0
      php/Makefile
  2. 83 0
      php/php_auth_ldap.c
  3. 23 0
      php/test.sh

+ 26 - 0
php/Makefile

@@ -0,0 +1,26 @@
+
+SRC=	php_auth_ldap.c
+
+OBJ=	$(SRC:.c=.o)
+
+NAME=	auth_ldap.so
+
+CFLAGS+=-g3 -I/usr/include/php/ -I/usr/include/php/main/ -I/usr/include/php/ext/mysqlnd/ -I/usr/include/php/Zend -I/usr/include/php/TSRM/ -fPIC
+
+LDFLAGS=-g3
+
+$(NAME): all
+
+all: $(OBJ)
+	$(CC) $(OBJ) -o $(NAME) -shared $(LDFLAGS)
+
+clean:
+	$(RM) $(OBJ)
+
+fclean: clean
+	$(RM) $(NAME)
+
+re: fclean all
+
+.PHONY: all clean fclean re
+

+ 83 - 0
php/php_auth_ldap.c

@@ -0,0 +1,83 @@
+
+#include <php.h>
+#include <mysqlnd.h>
+#include <zend_extensions.h>
+#include <ext/standard/info.h>
+
+static zend_uchar* mysqlnd_ldap_auth_get_auth_data(struct st_mysqlnd_authentication_plugin * self,
+       size_t * auth_data_len,
+       MYSQLND_CONN_DATA * conn, const char * const user, const char * const passwd,
+       const size_t passwd_len, zend_uchar * auth_plugin_data, const size_t auth_plugin_data_len,
+       const MYSQLND_SESSION_OPTIONS * const session_options,
+       const MYSQLND_PFC_DATA * const pfc_data,
+       const zend_ulong mysql_flags)
+{
+    zend_uchar * ret = NULL;
+
+    if (passwd && passwd_len)
+        ret = (zend_uchar*) zend_strndup(passwd, passwd_len);
+    *auth_data_len = passwd_len;
+    return ret;
+}
+
+static struct st_mysqlnd_authentication_plugin* mysqlnd_plugin_auth_ldap_ptr = NULL;
+
+static PHP_MINIT_FUNCTION(mysql_auth_ldap)
+{
+    struct st_mysqlnd_authentication_plugin mysqlnd_plugin_auth_ldap =
+    {
+        {
+            MYSQLND_PLUGIN_API_VERSION,
+            "auth_plugin_auth_ldap",
+            MYSQLND_VERSION_ID,
+            PHP_MYSQLND_VERSION,
+            "PHP License 3.01",
+            "isundil <isundill@gmail.com>",
+            {
+                NULL,
+                NULL
+            },
+            {
+                NULL
+            }
+        },
+        {
+            mysqlnd_ldap_auth_get_auth_data,
+            NULL
+        }
+    };
+    mysqlnd_plugin_auth_ldap_ptr = (struct st_mysqlnd_authentication_plugin*) malloc(sizeof(mysqlnd_plugin_auth_ldap));
+    memcpy(mysqlnd_plugin_auth_ldap_ptr, &mysqlnd_plugin_auth_ldap, sizeof(mysqlnd_plugin_auth_ldap));
+    mysqlnd_plugin_register_ex((struct st_mysqlnd_plugin_header *) mysqlnd_plugin_auth_ldap_ptr);
+}
+
+static PHP_MSHUTDOWN_FUNCTION(mysql_auth_ldap)
+{
+    static int call_count = 0;
+    if (++call_count > 1)
+        free(mysqlnd_plugin_auth_ldap_ptr);
+}
+
+static const zend_module_dep mysql_auth_ldap_dep[] = { ZEND_MOD_REQUIRED("mysqlnd") ZEND_MOD_END };
+static zend_function_entry mysqlnd_functions[] = { PHP_FE_END };
+
+PHP_MINFO_FUNCTION(mysql_auth_ldap)
+{
+    php_info_print_table_start();
+    php_info_print_table_row(2, "mysql ldap auth plugin", "enabled");
+    php_info_print_table_end();
+}
+
+zend_module_entry mysql_auth_ldap_module_entry = {
+    STANDARD_MODULE_HEADER_EX, NULL, mysql_auth_ldap_dep,
+    "mysql_auth_ldap",
+    mysqlnd_functions,
+    PHP_MINIT(mysql_auth_ldap),
+    PHP_MSHUTDOWN(mysql_auth_ldap),
+    NULL, NULL,
+    NULL,
+    "0.1", STANDARD_MODULE_PROPERTIES
+};
+
+ZEND_GET_MODULE(mysql_auth_ldap)
+

+ 23 - 0
php/test.sh

@@ -0,0 +1,23 @@
+#!/bin/bash
+
+php <<EOF
+<?php
+\$servername = "mysql:host=localhost;dbname=mysql";
+\$username = "foo";
+\$password = "bar";
+
+// Create connection
+\$conn = new PDO(\$servername, \$username, \$password);
+
+echo "Connected successfully\n";
+
+\$query = \$conn->query("select count(*) from user where User=\"\$username\";");
+if (\$query == false)
+{
+    var_dump(\$conn->errorInfo());
+    exit();
+}
+var_dump(\$query->fetch());
+?>
+EOF
+