cvs: pecl /pam CREDITS README config.m4 package.xml pam.c php_pam.h /pam/tests 001.phpt 002.phpt 003.phpt 004.phpt pam.inc
| From: | Mikael Johansson | Date: | Mon, 13 Nov 2006 07:47:40 +0000 |
| Subject: | cvs: pecl /pam CREDITS README config.m4 package.xml pam.c php_pam.h /pam/tests 001.phpt 002.phpt 003.phpt 004.phpt pam.inc | ||
| Groups: | php.pecl.cvs | ||
| Request: | Send a blank email to pecl-cvs+get-6628@lists.php.net to get a copy of this message | ||
mikl Mon Nov 13 07:47:40 2006 UTC
Added files:
/pecl/pam CREDITS README config.m4 package.xml pam.c php_pam.h
/pecl/pam/tests 001.phpt 002.phpt 003.phpt 004.phpt pam.inc
Log:
Initial PECL import
http://cvs.php.net/viewvc.cgi/pecl/pam/config.m4?view=markup&rev=1.1 Index: pecl/pam/config.m4 +++ pecl/pam/config.m4 dnl $Id: config.m4,v 1.1 2006/11/13 07:47:40 mikl Exp $ dnl config.m4 for extension pam dnl vim:se ts=2 sw=2 et: PHP_ARG_WITH(pam, for PAM support, [ --with-pam[=DIR] Include PAM support. DIR is the libpam install prefix]) if test -z "$PHP_DEBUG"; then AC_ARG_ENABLE(debug, [ --enable-debug compile with debugging symbols],[ PHP_DEBUG=$enableval ],[ PHP_DEBUG=no ]) fi if test "$PHP_PAM" != "no"; then AC_DEFINE(HAVE_PAM, 1, [ ]) AC_CHECK_HEADERS(security/pam_appl.h) for i in $PHP_PAM /usr/local /usr /; do if test -f $i/lib/libpam.$SHLIB_SUFFIX_NAME; then PAM_DIR=$i fi done if test -z "$PAM_DIR"; then AC_MSG_ERROR([install the the PAM library or use --with-pam=DIR to specify the location.]) fi PHP_CHECK_LIBRARY(pam, pam_start,,[ AC_MSG_ERROR([failed to find and link against libpam]) ],-L$PAM_DIR/lib) PHP_ADD_LIBRARY_WITH_PATH(pam, $PAM_DIR/lib, PAM_SHARED_LIBADD) PHP_ADD_INCLUDE($PAM_DIR/include) PHP_SUBST(PAM_SHARED_LIBADD) if test "$PHP_DEBUG" = "yes"; then CFLAGS="$CFLAGS -Wall" fi PHP_NEW_EXTENSION(pam, pam.c, $ext_shared) fi http://cvs.php.net/viewvc.cgi/pecl/pam/package.xml?view=markup&rev=1.1 Index: pecl/pam/package.xml +++ pecl/pam/package.xml <?xml version="1.0" encoding="ISO-8859-1" ?> <!DOCTYPE package SYSTEM "http://pear.php.net/dtd/package-1.0"> <package version="1.0"> <name>pam</name> <summary>PAM integration</summary> <description>This extension provides PAM (Pluggable Authentication Modules) integration</description> <maintainers> <maintainer> <user>mikl</user> <name>Mikael Johansson</name> <email>mikael AT synd DOT info</email> <role>lead</role> </maintainer> </maintainers> <release> <version>1.0.0</version> <date>2006-11-13</date> <license>PHP</license> <state>stable</state> <notes>Initial PECL release</notes> <deps> <dep type="php" rel="ge" version="4.3.0" optional="no"/> </deps> </release> <filelist> <file role="doc" name="CREDITS" /> <file role="doc" name="README" /> <file role="src" name="config.m4" /> <file role="src" name="php_pam.h" /> <file role="src" name="pam.c" /> </filelist> <changelog> </changelog> </package> http://cvs.php.net/viewvc.cgi/pecl/pam/pam.c?view=markup&rev=1.1 Index: pecl/pam/pam.c +++ pecl/pam/pam.c /* +----------------------------------------------------------------------+ | PHP version 4.0 | +----------------------------------------------------------------------+ | Copyright (c) 1997, 1998, 1999, 2000 The PHP Group | +----------------------------------------------------------------------+ | This source file is subject to version 2.02 of the PHP license, | | that is bundled with this package in the file LICENSE, and is | | available at through the world-wide-web at | | http://www.php.net/license/2_02.txt. | | If you did not receive a copy of the PHP license and are unable to | | obtain it through the world-wide-web, please send a note to | | license@php.net so we can mail you a copy immediately. | +----------------------------------------------------------------------+ | Authors: Mikael Johansson <mikael AT synd DOT info> | | Chad Cunningham | +----------------------------------------------------------------------+ */ /* $Id: pam.c,v 1.1 2006/11/13 07:47:40 mikl Exp $ */ #ifdef HAVE_CONFIG_H #include "config.h" #endif #include "php.h" #include "php_ini.h" #include "ext/standard/info.h" #include "php_pam.h" #include <security/pam_appl.h> ZEND_DECLARE_MODULE_GLOBALS(pam) /* {{{ pam_functions[] */ zend_function_entry pam_functions[] = { PHP_FE(pam_auth, NULL) PHP_FE(pam_chpass, NULL) {NULL, NULL, NULL} }; /* }}} */ /* {{{ pam_module_entry */ zend_module_entry pam_module_entry = { #if ZEND_MODULE_API_NO >= 20010901 STANDARD_MODULE_HEADER, #endif "pam", pam_functions, PHP_MINIT(pam), PHP_MSHUTDOWN(pam), NULL, /* Replace with NULL if there's nothing to do at request start */ NULL, /* Replace with NULL if there's nothing to do at request end */ PHP_MINFO(pam), #if ZEND_MODULE_API_NO >= 20010901 NO_VERSION_YET, /* Replace with version number for your extension */ #endif STANDARD_MODULE_PROPERTIES }; /* }}} */ #ifdef COMPILE_DL_PAM ZEND_GET_MODULE(pam) #endif /* {{{ PHP_INI */ PHP_INI_BEGIN() STD_PHP_INI_ENTRY("pam.servicename", "php", PHP_INI_ALL, OnUpdateString, servicename, zend_pam_globals, pam_globals) PHP_INI_END() /* }}} */ /* {{{ php_pam_init_globals */ static void php_pam_init_globals(zend_pam_globals *pam_globals) { pam_globals->servicename = NULL; } /* }}} */ /* {{{ PHP_MINIT_FUNCTION */ PHP_MINIT_FUNCTION(pam) { ZEND_INIT_MODULE_GLOBALS(pam, php_pam_init_globals, NULL); REGISTER_INI_ENTRIES(); return SUCCESS; } /* }}} */ /* {{{ PHP_MSHUTDOWN_FUNCTION */ PHP_MSHUTDOWN_FUNCTION(pam) { UNREGISTER_INI_ENTRIES(); return SUCCESS; } /* }}} */ /* {{{ PHP_MINFO_FUNCTION */ PHP_MINFO_FUNCTION(pam) { php_info_print_table_start(); php_info_print_table_header(2, "PAM support", "enabled"); php_info_print_table_end(); DISPLAY_INI_ENTRIES(); } /* }}} */ /* * auth_pam_talker: supply authentication information to PAM when asked * * Assumptions: * A password is asked for by requesting input without echoing * A username is asked for by requesting input _with_ echoing * */ static int auth_pam_talker(int num_msg, const struct pam_message ** msg, struct pam_response ** resp, void *appdata_ptr) { unsigned short i = 0; pam_auth_t *userinfo = (pam_auth_t *) appdata_ptr; struct pam_response *response = 0; /* parameter sanity checking */ if (!resp || !msg || !userinfo) return PAM_CONV_ERR; /* allocate memory to store response */ response = malloc(num_msg * sizeof(struct pam_response)); if (!response) return PAM_CONV_ERR; /* copy values */ for (i = 0; i < num_msg; i++) { /* initialize to safe values */ response[i].resp_retcode = 0; response[i].resp = 0; /* select response based on requested output style */ switch (msg[i]->msg_style) { case PAM_PROMPT_ECHO_ON: /* on memory allocation failure, auth fails */ response[i].resp = strdup(userinfo->name); break; case PAM_PROMPT_ECHO_OFF: response[i].resp = strdup(userinfo->pw); break; default: if (response) free(response); return PAM_CONV_ERR; } } /* everything okay, set PAM response values */ *resp = response; return PAM_SUCCESS; } /* * chpass_pam_talker: supply authentication information to PAM when asked * * Assumptions: * A password is asked for by requesting input without echoing * A username is asked for by requesting input _with_ echoing * */ static int chpass_pam_talker(int num_msg, const struct pam_message ** msg, struct pam_response ** resp, void *appdata_ptr) { unsigned short i = 0; pam_chpass_t *userinfo = (pam_chpass_t *) appdata_ptr; struct pam_response *response = 0; /* parameter sanity checking */ if (!resp || !msg || !userinfo) return PAM_CONV_ERR; /* allocate memory to store response */ response = malloc(num_msg * sizeof(struct pam_response)); if (!response) return PAM_CONV_ERR; /* copy values */ for (i = 0; i < num_msg; i++) { /* initialize to safe values */ response[i].resp_retcode = 0; response[i].resp = 0; /* select response based on requested output style */ switch (msg[i]->msg_style) { case PAM_PROMPT_ECHO_ON: /* on memory allocation failure, auth fails */ response[i].resp = strdup(userinfo->name); break; case PAM_PROMPT_ECHO_OFF: if (userinfo->count++) { response[i].resp = strdup(userinfo->newpw); } else { response[i].resp = strdup(userinfo->oldpw); } break; default: if (response) free(response); return PAM_CONV_ERR; } } /* everything okay, set PAM response values */ *resp = response; return PAM_SUCCESS; } /* {{{ proto bool pam_auth( string host, string password [, string &status ]) Authenticates a user and returns TRUE on success, FALSE on failure */ PHP_FUNCTION(pam_auth) { char *username, *password; int username_len, password_len; zval *status = NULL; pam_auth_t userinfo = {NULL, NULL}; struct pam_conv conv_info = {&auth_pam_talker, (void *) &userinfo}; pam_handle_t *pamh = NULL; int result; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|z", &username, &username_len, &password, &password_len, &status) == FAILURE) { return; } userinfo.name = username; userinfo.pw = password; if ((result = pam_start(PAM_G(servicename), userinfo.name, &conv_info, &pamh)) != PAM_SUCCESS || (result = pam_authenticate(pamh, PAM_DISALLOW_NULL_AUTHTOK)) != PAM_SUCCESS || (result = pam_acct_mgmt(pamh, PAM_DISALLOW_NULL_AUTHTOK)) != PAM_SUCCESS) { if (status) { zval_dtor(status); ZVAL_STRING(status, (char *) pam_strerror(pamh, result), 1); } pam_end(pamh, result); RETURN_FALSE; } pam_end(pamh, PAM_SUCCESS); RETURN_TRUE; } /* }}} */ /* {{{ proto bool pam_auth( string host, string password [, string &status ]) Changes a users password and returns TRUE on success, FALSE on failure */ PHP_FUNCTION(pam_chpass) { char *username, *oldpass, *newpass; int username_len, oldpass_len, newpass_len; zval *status = NULL; pam_chpass_t userinfo = {NULL, NULL, NULL, 0}; struct pam_conv conv_info = {&chpass_pam_talker, (void *) &userinfo}; pam_handle_t *pamh = NULL; int result; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sss|z", &username, &username_len, &oldpass, &oldpass_len, &newpass, &newpass_len, &status) == FAILURE) { return; } userinfo.name = username; userinfo.oldpw = oldpass; userinfo.newpw = newpass; if ((result = pam_start(PAM_G(servicename), userinfo.name, &conv_info, &pamh)) != PAM_SUCCESS || (result = pam_authenticate(pamh, PAM_DISALLOW_NULL_AUTHTOK)) != PAM_SUCCESS || (result = pam_chauthtok(pamh, PAM_DISALLOW_NULL_AUTHTOK)) != PAM_SUCCESS) { if (status) { zval_dtor(status); ZVAL_STRING(status, (char *) pam_strerror(pamh, result), 1); } pam_end(pamh, PAM_SUCCESS); RETURN_FALSE; } pam_end(pamh, PAM_SUCCESS); RETURN_TRUE; } /* }}} */ /* * Local variables: * tab-width: 4 * c-basic-offset: 4 * End: * vim600: noet sw=4 ts=4 fdm=marker * vim<600: noet sw=4 ts=4 */ http://cvs.php.net/viewvc.cgi/pecl/pam/php_pam.h?view=markup&rev=1.1 Index: pecl/pam/php_pam.h +++ pecl/pam/php_pam.h /* +----------------------------------------------------------------------+ | PHP version 4.0 | +----------------------------------------------------------------------+ | Copyright (c) 1997, 1998, 1999, 2000 The PHP Group | +----------------------------------------------------------------------+ | This source file is subject to version 2.02 of the PHP license, | | that is bundled with this package in the file LICENSE, and is | | available at through the world-wide-web at | | http://www.php.net/license/2_02.txt. | | If you did not receive a copy of the PHP license and are unable to | | obtain it through the world-wide-web, please send a note to | | license@php.net so we can mail you a copy immediately. | +----------------------------------------------------------------------+ | Authors: Mikael Johansson <mikael AT synd DOT info> | | Chad Cunningham | +----------------------------------------------------------------------+ */ /* $Id: php_pam.h,v 1.1 2006/11/13 07:47:40 mikl Exp $ */ #ifndef PHP_PAM_H #define PHP_PAM_H extern zend_module_entry pam_module_entry; #define phpext_pam_ptr &pam_module_entry #ifdef PHP_WIN32 #define PHP_PAM_API __declspec(dllexport) #else #define PHP_PAM_API #endif #ifdef ZTS #include "TSRM.h" #endif PHP_MINIT_FUNCTION(pam); PHP_MSHUTDOWN_FUNCTION(pam); PHP_MINFO_FUNCTION(pam); PHP_FUNCTION(pam_auth); PHP_FUNCTION(pam_chpass); ZEND_BEGIN_MODULE_GLOBALS(pam) char *servicename; ZEND_END_MODULE_GLOBALS(pam) typedef struct { char *name, *pw; } pam_auth_t; typedef struct { char *name, *oldpw, *newpw; int count; } pam_chpass_t; #ifdef ZTS #define PAM_G(v) TSRMG(pam_globals_id, zend_pam_globals *, v) #else #define PAM_G(v) (pam_globals.v) #endif #endif /* PHP_PAM_H */ /* * Local variables: * tab-width: 4 * c-basic-offset: 4 * End: * vim600: noet sw=4 ts=4 fdm=marker * vim<600: noet sw=4 ts=4 */ http://cvs.php.net/viewvc.cgi/pecl/pam/tests/001.phpt?view=markup&rev=1.1 Index: pecl/pam/tests/001.phpt +++ pecl/pam/tests/001.phpt --TEST-- pam_auth() with invalid user --SKIPIF-- <?php include 'pam.inc'; ?> --FILE-- <?php include 'pam.inc'; // Test auth $result = pam_auth(md5(uniqid('')), md5(uniqid(''))); var_dump($result); // Test error message $status = null; $result = pam_auth(md5(uniqid('')), md5(uniqid('')), $status); var_dump($result); var_dump($status); ?> --EXPECTF-- bool(false) bool(false) string(%d) "%s" http://cvs.php.net/viewvc.cgi/pecl/pam/tests/002.phpt?view=markup&rev=1.1 Index: pecl/pam/tests/002.phpt +++ pecl/pam/tests/002.phpt --TEST-- pam_auth() with valid user --SKIPIF-- <?php include 'pam.inc'; if (!isset($username, $password)) print 'skip'; ?> --FILE-- <?php include 'pam.inc'; // Test auth $result = pam_auth($username, $password); var_dump($result); // Test auth with untouched status $status = null; $result = pam_auth($username, $password, $status); var_dump($result); var_dump($status); // Test error message $status = null; $result = pam_auth($username, md5(uniqid('')), $status); var_dump($result); var_dump($status); ?> --EXPECTF-- bool(true) bool(true) NULL bool(false) string(%d) "%s" http://cvs.php.net/viewvc.cgi/pecl/pam/tests/003.phpt?view=markup&rev=1.1 Index: pecl/pam/tests/003.phpt +++ pecl/pam/tests/003.phpt --TEST-- pam_chpass() --SKIPIF-- <?php include 'pam.inc'; if (!isset($username, $password)) print 'skip'; ?> --FILE-- <?php include 'pam.inc'; // Assert auth with original password $result = pam_auth($username, $password); var_dump($result); // Test change password with invalid original password $newpass = substr(md5(uniqid('')), 0, 8); $status = null; $result = pam_chpass($username, substr(md5(uniqid('')), 0, 8), $newpass, $status); var_dump($result); var_dump($status); // Test change password $result = pam_chpass($username, $password, $newpass); var_dump($result); // Fail auth with old password $result = pam_auth($username, $password); var_dump($result); // Succeed auth with new password $result = pam_auth($username, $newpass); var_dump($result); // Revert to old password $result = pam_chpass($username, $newpass, $password); var_dump($result); ?> --EXPECTF-- bool(true) bool(false) string(%d) "%s" bool(true) bool(false) bool(true) bool(true) http://cvs.php.net/viewvc.cgi/pecl/pam/tests/004.phpt?view=markup&rev=1.1 Index: pecl/pam/tests/004.phpt +++ pecl/pam/tests/004.phpt --TEST-- ini_test('pam.servicename', ..) --SKIPIF-- <?php include 'pam.inc'; if (!isset($username, $password)) print 'skip'; ?> --FILE-- <?php include 'pam.inc'; // Test auth $result = pam_auth($username, $password); var_dump($result); // Set servicename to invalid value $result = ini_set('pam.servicename', md5(uniqid(''))); var_dump($result); // Test auth with untouched status $result = pam_auth($username, $password); var_dump($result); // Test error message $status = null; $result = pam_auth($username, $password, $status); var_dump($result); var_dump($status); ?> --EXPECTF-- bool(true) string(%d) "%s" bool(false) bool(false) string(%d) "%s" http://cvs.php.net/viewvc.cgi/pecl/pam/tests/pam.inc?view=markup&rev=1.1 Index: pecl/pam/tests/pam.inc +++ pecl/pam/tests/pam.inc <? if (!extension_loaded("pam")) { print "skip"; return; } // Change these to a valid username and password //$username = 'nobody'; //$password = 'secret';
http://cvs.php.net/viewvc.cgi/pecl/pam/config.m4?view=markup&rev=1.1 Index: pecl/pam/config.m4 +++ pecl/pam/config.m4 dnl $Id: config.m4,v 1.1 2006/11/13 07:47:40 mikl Exp $ dnl config.m4 for extension pam dnl vim:se ts=2 sw=2 et: PHP_ARG_WITH(pam, for PAM support, [ --with-pam[=DIR] Include PAM support. DIR is the libpam install prefix]) if test -z "$PHP_DEBUG"; then AC_ARG_ENABLE(debug, [ --enable-debug compile with debugging symbols],[ PHP_DEBUG=$enableval ],[ PHP_DEBUG=no ]) fi if test "$PHP_PAM" != "no"; then AC_DEFINE(HAVE_PAM, 1, [ ]) AC_CHECK_HEADERS(security/pam_appl.h) for i in $PHP_PAM /usr/local /usr /; do if test -f $i/lib/libpam.$SHLIB_SUFFIX_NAME; then PAM_DIR=$i fi done if test -z "$PAM_DIR"; then AC_MSG_ERROR([install the the PAM library or use --with-pam=DIR to specify the location.]) fi PHP_CHECK_LIBRARY(pam, pam_start,,[ AC_MSG_ERROR([failed to find and link against libpam]) ],-L$PAM_DIR/lib) PHP_ADD_LIBRARY_WITH_PATH(pam, $PAM_DIR/lib, PAM_SHARED_LIBADD) PHP_ADD_INCLUDE($PAM_DIR/include) PHP_SUBST(PAM_SHARED_LIBADD) if test "$PHP_DEBUG" = "yes"; then CFLAGS="$CFLAGS -Wall" fi PHP_NEW_EXTENSION(pam, pam.c, $ext_shared) fi http://cvs.php.net/viewvc.cgi/pecl/pam/package.xml?view=markup&rev=1.1 Index: pecl/pam/package.xml +++ pecl/pam/package.xml <?xml version="1.0" encoding="ISO-8859-1" ?> <!DOCTYPE package SYSTEM "http://pear.php.net/dtd/package-1.0"> <package version="1.0"> <name>pam</name> <summary>PAM integration</summary> <description>This extension provides PAM (Pluggable Authentication Modules) integration</description> <maintainers> <maintainer> <user>mikl</user> <name>Mikael Johansson</name> <email>mikael AT synd DOT info</email> <role>lead</role> </maintainer> </maintainers> <release> <version>1.0.0</version> <date>2006-11-13</date> <license>PHP</license> <state>stable</state> <notes>Initial PECL release</notes> <deps> <dep type="php" rel="ge" version="4.3.0" optional="no"/> </deps> </release> <filelist> <file role="doc" name="CREDITS" /> <file role="doc" name="README" /> <file role="src" name="config.m4" /> <file role="src" name="php_pam.h" /> <file role="src" name="pam.c" /> </filelist> <changelog> </changelog> </package> http://cvs.php.net/viewvc.cgi/pecl/pam/pam.c?view=markup&rev=1.1 Index: pecl/pam/pam.c +++ pecl/pam/pam.c /* +----------------------------------------------------------------------+ | PHP version 4.0 | +----------------------------------------------------------------------+ | Copyright (c) 1997, 1998, 1999, 2000 The PHP Group | +----------------------------------------------------------------------+ | This source file is subject to version 2.02 of the PHP license, | | that is bundled with this package in the file LICENSE, and is | | available at through the world-wide-web at | | http://www.php.net/license/2_02.txt. | | If you did not receive a copy of the PHP license and are unable to | | obtain it through the world-wide-web, please send a note to | | license@php.net so we can mail you a copy immediately. | +----------------------------------------------------------------------+ | Authors: Mikael Johansson <mikael AT synd DOT info> | | Chad Cunningham | +----------------------------------------------------------------------+ */ /* $Id: pam.c,v 1.1 2006/11/13 07:47:40 mikl Exp $ */ #ifdef HAVE_CONFIG_H #include "config.h" #endif #include "php.h" #include "php_ini.h" #include "ext/standard/info.h" #include "php_pam.h" #include <security/pam_appl.h> ZEND_DECLARE_MODULE_GLOBALS(pam) /* {{{ pam_functions[] */ zend_function_entry pam_functions[] = { PHP_FE(pam_auth, NULL) PHP_FE(pam_chpass, NULL) {NULL, NULL, NULL} }; /* }}} */ /* {{{ pam_module_entry */ zend_module_entry pam_module_entry = { #if ZEND_MODULE_API_NO >= 20010901 STANDARD_MODULE_HEADER, #endif "pam", pam_functions, PHP_MINIT(pam), PHP_MSHUTDOWN(pam), NULL, /* Replace with NULL if there's nothing to do at request start */ NULL, /* Replace with NULL if there's nothing to do at request end */ PHP_MINFO(pam), #if ZEND_MODULE_API_NO >= 20010901 NO_VERSION_YET, /* Replace with version number for your extension */ #endif STANDARD_MODULE_PROPERTIES }; /* }}} */ #ifdef COMPILE_DL_PAM ZEND_GET_MODULE(pam) #endif /* {{{ PHP_INI */ PHP_INI_BEGIN() STD_PHP_INI_ENTRY("pam.servicename", "php", PHP_INI_ALL, OnUpdateString, servicename, zend_pam_globals, pam_globals) PHP_INI_END() /* }}} */ /* {{{ php_pam_init_globals */ static void php_pam_init_globals(zend_pam_globals *pam_globals) { pam_globals->servicename = NULL; } /* }}} */ /* {{{ PHP_MINIT_FUNCTION */ PHP_MINIT_FUNCTION(pam) { ZEND_INIT_MODULE_GLOBALS(pam, php_pam_init_globals, NULL); REGISTER_INI_ENTRIES(); return SUCCESS; } /* }}} */ /* {{{ PHP_MSHUTDOWN_FUNCTION */ PHP_MSHUTDOWN_FUNCTION(pam) { UNREGISTER_INI_ENTRIES(); return SUCCESS; } /* }}} */ /* {{{ PHP_MINFO_FUNCTION */ PHP_MINFO_FUNCTION(pam) { php_info_print_table_start(); php_info_print_table_header(2, "PAM support", "enabled"); php_info_print_table_end(); DISPLAY_INI_ENTRIES(); } /* }}} */ /* * auth_pam_talker: supply authentication information to PAM when asked * * Assumptions: * A password is asked for by requesting input without echoing * A username is asked for by requesting input _with_ echoing * */ static int auth_pam_talker(int num_msg, const struct pam_message ** msg, struct pam_response ** resp, void *appdata_ptr) { unsigned short i = 0; pam_auth_t *userinfo = (pam_auth_t *) appdata_ptr; struct pam_response *response = 0; /* parameter sanity checking */ if (!resp || !msg || !userinfo) return PAM_CONV_ERR; /* allocate memory to store response */ response = malloc(num_msg * sizeof(struct pam_response)); if (!response) return PAM_CONV_ERR; /* copy values */ for (i = 0; i < num_msg; i++) { /* initialize to safe values */ response[i].resp_retcode = 0; response[i].resp = 0; /* select response based on requested output style */ switch (msg[i]->msg_style) { case PAM_PROMPT_ECHO_ON: /* on memory allocation failure, auth fails */ response[i].resp = strdup(userinfo->name); break; case PAM_PROMPT_ECHO_OFF: response[i].resp = strdup(userinfo->pw); break; default: if (response) free(response); return PAM_CONV_ERR; } } /* everything okay, set PAM response values */ *resp = response; return PAM_SUCCESS; } /* * chpass_pam_talker: supply authentication information to PAM when asked * * Assumptions: * A password is asked for by requesting input without echoing * A username is asked for by requesting input _with_ echoing * */ static int chpass_pam_talker(int num_msg, const struct pam_message ** msg, struct pam_response ** resp, void *appdata_ptr) { unsigned short i = 0; pam_chpass_t *userinfo = (pam_chpass_t *) appdata_ptr; struct pam_response *response = 0; /* parameter sanity checking */ if (!resp || !msg || !userinfo) return PAM_CONV_ERR; /* allocate memory to store response */ response = malloc(num_msg * sizeof(struct pam_response)); if (!response) return PAM_CONV_ERR; /* copy values */ for (i = 0; i < num_msg; i++) { /* initialize to safe values */ response[i].resp_retcode = 0; response[i].resp = 0; /* select response based on requested output style */ switch (msg[i]->msg_style) { case PAM_PROMPT_ECHO_ON: /* on memory allocation failure, auth fails */ response[i].resp = strdup(userinfo->name); break; case PAM_PROMPT_ECHO_OFF: if (userinfo->count++) { response[i].resp = strdup(userinfo->newpw); } else { response[i].resp = strdup(userinfo->oldpw); } break; default: if (response) free(response); return PAM_CONV_ERR; } } /* everything okay, set PAM response values */ *resp = response; return PAM_SUCCESS; } /* {{{ proto bool pam_auth( string host, string password [, string &status ]) Authenticates a user and returns TRUE on success, FALSE on failure */ PHP_FUNCTION(pam_auth) { char *username, *password; int username_len, password_len; zval *status = NULL; pam_auth_t userinfo = {NULL, NULL}; struct pam_conv conv_info = {&auth_pam_talker, (void *) &userinfo}; pam_handle_t *pamh = NULL; int result; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|z", &username, &username_len, &password, &password_len, &status) == FAILURE) { return; } userinfo.name = username; userinfo.pw = password; if ((result = pam_start(PAM_G(servicename), userinfo.name, &conv_info, &pamh)) != PAM_SUCCESS || (result = pam_authenticate(pamh, PAM_DISALLOW_NULL_AUTHTOK)) != PAM_SUCCESS || (result = pam_acct_mgmt(pamh, PAM_DISALLOW_NULL_AUTHTOK)) != PAM_SUCCESS) { if (status) { zval_dtor(status); ZVAL_STRING(status, (char *) pam_strerror(pamh, result), 1); } pam_end(pamh, result); RETURN_FALSE; } pam_end(pamh, PAM_SUCCESS); RETURN_TRUE; } /* }}} */ /* {{{ proto bool pam_auth( string host, string password [, string &status ]) Changes a users password and returns TRUE on success, FALSE on failure */ PHP_FUNCTION(pam_chpass) { char *username, *oldpass, *newpass; int username_len, oldpass_len, newpass_len; zval *status = NULL; pam_chpass_t userinfo = {NULL, NULL, NULL, 0}; struct pam_conv conv_info = {&chpass_pam_talker, (void *) &userinfo}; pam_handle_t *pamh = NULL; int result; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sss|z", &username, &username_len, &oldpass, &oldpass_len, &newpass, &newpass_len, &status) == FAILURE) { return; } userinfo.name = username; userinfo.oldpw = oldpass; userinfo.newpw = newpass; if ((result = pam_start(PAM_G(servicename), userinfo.name, &conv_info, &pamh)) != PAM_SUCCESS || (result = pam_authenticate(pamh, PAM_DISALLOW_NULL_AUTHTOK)) != PAM_SUCCESS || (result = pam_chauthtok(pamh, PAM_DISALLOW_NULL_AUTHTOK)) != PAM_SUCCESS) { if (status) { zval_dtor(status); ZVAL_STRING(status, (char *) pam_strerror(pamh, result), 1); } pam_end(pamh, PAM_SUCCESS); RETURN_FALSE; } pam_end(pamh, PAM_SUCCESS); RETURN_TRUE; } /* }}} */ /* * Local variables: * tab-width: 4 * c-basic-offset: 4 * End: * vim600: noet sw=4 ts=4 fdm=marker * vim<600: noet sw=4 ts=4 */ http://cvs.php.net/viewvc.cgi/pecl/pam/php_pam.h?view=markup&rev=1.1 Index: pecl/pam/php_pam.h +++ pecl/pam/php_pam.h /* +----------------------------------------------------------------------+ | PHP version 4.0 | +----------------------------------------------------------------------+ | Copyright (c) 1997, 1998, 1999, 2000 The PHP Group | +----------------------------------------------------------------------+ | This source file is subject to version 2.02 of the PHP license, | | that is bundled with this package in the file LICENSE, and is | | available at through the world-wide-web at | | http://www.php.net/license/2_02.txt. | | If you did not receive a copy of the PHP license and are unable to | | obtain it through the world-wide-web, please send a note to | | license@php.net so we can mail you a copy immediately. | +----------------------------------------------------------------------+ | Authors: Mikael Johansson <mikael AT synd DOT info> | | Chad Cunningham | +----------------------------------------------------------------------+ */ /* $Id: php_pam.h,v 1.1 2006/11/13 07:47:40 mikl Exp $ */ #ifndef PHP_PAM_H #define PHP_PAM_H extern zend_module_entry pam_module_entry; #define phpext_pam_ptr &pam_module_entry #ifdef PHP_WIN32 #define PHP_PAM_API __declspec(dllexport) #else #define PHP_PAM_API #endif #ifdef ZTS #include "TSRM.h" #endif PHP_MINIT_FUNCTION(pam); PHP_MSHUTDOWN_FUNCTION(pam); PHP_MINFO_FUNCTION(pam); PHP_FUNCTION(pam_auth); PHP_FUNCTION(pam_chpass); ZEND_BEGIN_MODULE_GLOBALS(pam) char *servicename; ZEND_END_MODULE_GLOBALS(pam) typedef struct { char *name, *pw; } pam_auth_t; typedef struct { char *name, *oldpw, *newpw; int count; } pam_chpass_t; #ifdef ZTS #define PAM_G(v) TSRMG(pam_globals_id, zend_pam_globals *, v) #else #define PAM_G(v) (pam_globals.v) #endif #endif /* PHP_PAM_H */ /* * Local variables: * tab-width: 4 * c-basic-offset: 4 * End: * vim600: noet sw=4 ts=4 fdm=marker * vim<600: noet sw=4 ts=4 */ http://cvs.php.net/viewvc.cgi/pecl/pam/tests/001.phpt?view=markup&rev=1.1 Index: pecl/pam/tests/001.phpt +++ pecl/pam/tests/001.phpt --TEST-- pam_auth() with invalid user --SKIPIF-- <?php include 'pam.inc'; ?> --FILE-- <?php include 'pam.inc'; // Test auth $result = pam_auth(md5(uniqid('')), md5(uniqid(''))); var_dump($result); // Test error message $status = null; $result = pam_auth(md5(uniqid('')), md5(uniqid('')), $status); var_dump($result); var_dump($status); ?> --EXPECTF-- bool(false) bool(false) string(%d) "%s" http://cvs.php.net/viewvc.cgi/pecl/pam/tests/002.phpt?view=markup&rev=1.1 Index: pecl/pam/tests/002.phpt +++ pecl/pam/tests/002.phpt --TEST-- pam_auth() with valid user --SKIPIF-- <?php include 'pam.inc'; if (!isset($username, $password)) print 'skip'; ?> --FILE-- <?php include 'pam.inc'; // Test auth $result = pam_auth($username, $password); var_dump($result); // Test auth with untouched status $status = null; $result = pam_auth($username, $password, $status); var_dump($result); var_dump($status); // Test error message $status = null; $result = pam_auth($username, md5(uniqid('')), $status); var_dump($result); var_dump($status); ?> --EXPECTF-- bool(true) bool(true) NULL bool(false) string(%d) "%s" http://cvs.php.net/viewvc.cgi/pecl/pam/tests/003.phpt?view=markup&rev=1.1 Index: pecl/pam/tests/003.phpt +++ pecl/pam/tests/003.phpt --TEST-- pam_chpass() --SKIPIF-- <?php include 'pam.inc'; if (!isset($username, $password)) print 'skip'; ?> --FILE-- <?php include 'pam.inc'; // Assert auth with original password $result = pam_auth($username, $password); var_dump($result); // Test change password with invalid original password $newpass = substr(md5(uniqid('')), 0, 8); $status = null; $result = pam_chpass($username, substr(md5(uniqid('')), 0, 8), $newpass, $status); var_dump($result); var_dump($status); // Test change password $result = pam_chpass($username, $password, $newpass); var_dump($result); // Fail auth with old password $result = pam_auth($username, $password); var_dump($result); // Succeed auth with new password $result = pam_auth($username, $newpass); var_dump($result); // Revert to old password $result = pam_chpass($username, $newpass, $password); var_dump($result); ?> --EXPECTF-- bool(true) bool(false) string(%d) "%s" bool(true) bool(false) bool(true) bool(true) http://cvs.php.net/viewvc.cgi/pecl/pam/tests/004.phpt?view=markup&rev=1.1 Index: pecl/pam/tests/004.phpt +++ pecl/pam/tests/004.phpt --TEST-- ini_test('pam.servicename', ..) --SKIPIF-- <?php include 'pam.inc'; if (!isset($username, $password)) print 'skip'; ?> --FILE-- <?php include 'pam.inc'; // Test auth $result = pam_auth($username, $password); var_dump($result); // Set servicename to invalid value $result = ini_set('pam.servicename', md5(uniqid(''))); var_dump($result); // Test auth with untouched status $result = pam_auth($username, $password); var_dump($result); // Test error message $status = null; $result = pam_auth($username, $password, $status); var_dump($result); var_dump($status); ?> --EXPECTF-- bool(true) string(%d) "%s" bool(false) bool(false) string(%d) "%s" http://cvs.php.net/viewvc.cgi/pecl/pam/tests/pam.inc?view=markup&rev=1.1 Index: pecl/pam/tests/pam.inc +++ pecl/pam/tests/pam.inc <? if (!extension_loaded("pam")) { print "skip"; return; } // Change these to a valid username and password //$username = 'nobody'; //$password = 'secret';