Skip to content

Implemented posix_getspnam() function #72

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ext/posix/config.m4
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ if test "$PHP_POSIX" = "yes"; then

AC_CHECK_HEADERS(sys/mkdev.h)

AC_CHECK_FUNCS(seteuid setegid setsid getsid setpgid getpgid ctermid mkfifo mknod getrlimit getlogin getgroups makedev initgroups getpwuid_r getgrgid_r)
AC_CHECK_FUNCS(seteuid setegid setsid getsid setpgid getpgid ctermid mkfifo mknod getrlimit getlogin getgroups makedev initgroups getpwuid_r getgrgid_r getspnam getspnam_r)

AC_MSG_CHECKING([for working ttyname_r() implementation])
AC_TRY_RUN([
Expand Down
3 changes: 3 additions & 0 deletions ext/posix/php_posix.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@ PHP_FUNCTION(posix_getgrnam);
PHP_FUNCTION(posix_getgrgid);
PHP_FUNCTION(posix_getpwnam);
PHP_FUNCTION(posix_getpwuid);
#ifdef HAVE_GETSPNAM
PHP_FUNCTION(posix_getspnam);
#endif

#ifdef HAVE_GETRLIMIT
PHP_FUNCTION(posix_getrlimit);
Expand Down
85 changes: 85 additions & 0 deletions ext/posix/posix.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@
#include <errno.h>
#include <grp.h>
#include <pwd.h>
#ifdef HAVE_GETSPNAM
# include <shadow.h>
#endif
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't there be a test for HAVE_SHADOW_H here instead? (or as well?)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AC_CHECK_FUNCS() should take care of that. You either have it and it's in shadow.h, or you don't. I haven't seen it done in a different way. It's not POSIX, so we might run into other implementations.

#if HAVE_SYS_MKDEV_H
# include <sys/mkdev.h>
#endif
Expand Down Expand Up @@ -190,6 +193,12 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_posix_getpwnam, 0, 0, 1)
ZEND_ARG_INFO(0, username)
ZEND_END_ARG_INFO()

#ifdef HAVE_GETSPNAM
ZEND_BEGIN_ARG_INFO_EX(arginfo_posix_getspnam, 0, 0, 1)
ZEND_ARG_INFO(0, username)
ZEND_END_ARG_INFO()
#endif

ZEND_BEGIN_ARG_INFO_EX(arginfo_posix_getpwuid, 0, 0, 1)
ZEND_ARG_INFO(0, uid)
ZEND_END_ARG_INFO()
Expand Down Expand Up @@ -288,6 +297,9 @@ const zend_function_entry posix_functions[] = {
PHP_FE(posix_getgrnam, arginfo_posix_getgrnam)
PHP_FE(posix_getgrgid, arginfo_posix_getgrgid)
PHP_FE(posix_getpwnam, arginfo_posix_getpwnam)
#ifdef HAVE_GETSPNAM
PHP_FE(posix_getspnam, arginfo_posix_getspnam)
#endif
PHP_FE(posix_getpwuid, arginfo_posix_getpwuid)

#ifdef HAVE_GETRLIMIT
Expand Down Expand Up @@ -1120,6 +1132,28 @@ int php_posix_passwd_to_array(struct passwd *pw, zval *return_value) /* {{{ */
}
/* }}} */

#ifdef HAVE_GETSPNAM
static int php_posix_spwd_to_array(struct spwd *spw, zval *return_value) /* {{{ */
{
if (NULL == spw)
return 0;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use { and } with if statements.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a direct copy from php_posix_passwd_to_array(). Should I fix both ?

if (NULL == return_value || Z_TYPE_P(return_value) != IS_ARRAY)
return 0;

add_assoc_string(return_value, "namp", spw->sp_namp, 1);
add_assoc_string(return_value, "pwdp", spw->sp_pwdp, 1);
add_assoc_long (return_value, "lstchg", spw->sp_lstchg);
add_assoc_long (return_value, "min", spw->sp_min);
add_assoc_long (return_value, "max", spw->sp_max);
add_assoc_long (return_value, "warn", spw->sp_warn);
add_assoc_long (return_value, "inact", spw->sp_inact);
add_assoc_long (return_value, "expire", spw->sp_expire);
add_assoc_long (return_value, "flag", spw->sp_flag);
return 1;
}
#endif
/* }}} */

/* {{{ proto array posix_getpwnam(string groupname)
User database access (POSIX.1, 9.2.2) */
PHP_FUNCTION(posix_getpwnam)
Expand Down Expand Up @@ -1169,6 +1203,57 @@ PHP_FUNCTION(posix_getpwnam)
}
/* }}} */

/* {{{ proto array posix_getspnam(string username) */
#ifdef HAVE_GETSPNAM
PHP_FUNCTION(posix_getspnam)
{
struct spwd *spw;
char *name;
int name_len;
#if defined(ZTS) && defined(HAVE_GETSPNAM_R)
struct spwd spwbuf;
long buflen;
char *buf;
#endif

if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &name, &name_len) == FAILURE) {
RETURN_FALSE;
}

#if defined(ZTS) && defined(HAVE_GETSPNAM_R)
/* No POSIX function to find out. Use a safe value here */
buflen = sysconf(sizeof(*spwd) * 2);
if (buflen < 1) {
RETURN_FALSE;
}
buf = emalloc(buflen);
pw = &spwbuf;

if (getspnam_r(name, spw, buf, buflen, &spw) || spw == NULL) {
efree(buf);
POSIX_G(last_error) = errno;
RETURN_FALSE;
}
#else
if (NULL == (spw = getspnam(name))) {
POSIX_G(last_error) = errno;
RETURN_FALSE;
}
#endif
array_init(return_value);

if (!php_posix_spwd_to_array(spw, return_value)) {
zval_dtor(return_value);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in case you have ZTS and GETSPANAM_R so

#if defined(ZTS) && defined(HAVE_GETSPNAM_R)

get's execute you have buf allocated. In case php_posix_spwd_to_array fails you are not
freeing buf anymore.

php_error_docref(NULL TSRMLS_CC, E_WARNING, "unable to convert spwd struct to array");
RETVAL_FALSE;
}
#if defined(ZTS) && defined(HAVE_GETPWNAM_R)
efree(buf);
#endif
}
#endif
/* }}} */

/* {{{ proto array posix_getpwuid(long uid)
User database access (POSIX.1, 9.2.2) */
PHP_FUNCTION(posix_getpwuid)
Expand Down