Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Implemented posix_getspnam() function #72

Closed
wants to merge 2 commits into from

6 participants

@igmar

Added posix_getspnam() function to the posix extension.

@derickr derickr commented on the diff
ext/posix/posix.c
@@ -49,6 +49,9 @@
#include <errno.h>
#include <grp.h>
#include <pwd.h>
+#ifdef HAVE_GETSPNAM
+# include <shadow.h>
+#endif
@derickr Owner
derickr added a note

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

@igmar
igmar added a note

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
ext/posix/posix.c
@@ -1120,6 +1132,28 @@ int php_posix_passwd_to_array(struct passwd *pw, zval *return_value) /* {{{ */
}
/* }}} */
+#ifdef HAVE_GETSPNAM
+int php_posix_spwd_to_array(struct spwd *spw, zval *return_value) /* {{{ */
+{
+ if (NULL == spw)
+ return 0;
@derickr Owner
derickr added a note

Please use { and } with if statements.

@igmar
igmar added a note

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
ext/posix/posix.c
@@ -1120,6 +1132,28 @@ int php_posix_passwd_to_array(struct passwd *pw, zval *return_value) /* {{{ */
}
/* }}} */
+#ifdef HAVE_GETSPNAM
+int php_posix_spwd_to_array(struct spwd *spw, zval *return_value) /* {{{ */
@laruence Owner
laruence added a note

declare it with static if you don't want intentionally expose it.

@igmar
igmar added a note

Just commited 6ce843f

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@dsp
Owner
dsp commented

getspwnam is not part of the POSIX.1, POSIX.1b or POSIX.1c standard.

From the Linux Manpages:

CONFORMING TO
The shadow password database and its associated API are not specified in POSIX.1-2001. However, many other systems provide a similar API.

I'm not sure if this should belong to ext/posix, but then on the other hand there is no suitable other extension to add this function too.

@igmar

The reason I've put it in ext/posix because similar functions are also in that extension. It's not POSIX, I agree, but it also serves special use cases. It's harmless in all other cases.

@dsp dsp was assigned
@dsp dsp commented on the diff
ext/posix/posix.c
((29 lines not shown))
+
+ 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);
@dsp Owner
dsp added a note

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@lstrojny

@igmar ping again

@php-pulls
Collaborator

Comment on behalf of stas at php.net:

No response for a year. If this patch is still relevant please update it according to comments and reopen the pull.

@php-pulls php-pulls closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
2  ext/posix/config.m4
@@ -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([
View
3  ext/posix/php_posix.h
@@ -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);
View
85 ext/posix/posix.c
@@ -49,6 +49,9 @@
#include <errno.h>
#include <grp.h>
#include <pwd.h>
+#ifdef HAVE_GETSPNAM
+# include <shadow.h>
+#endif
@derickr Owner
derickr added a note

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

@igmar
igmar added a note

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
#if HAVE_SYS_MKDEV_H
# include <sys/mkdev.h>
#endif
@@ -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()
@@ -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
@@ -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;
+ 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)
@@ -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);
@dsp Owner
dsp added a note

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ 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)
Something went wrong with that request. Please try again.