Skip to content

Commit 2b35431

Browse files
committed
ext/posix: proposing posix_eaccess. unlike access, it is not standard but available in enough platforms ; on linux it's euidaccess in reality eaccess being 'just' an alias. key difference is eaccess checks the effective user id instead.
Close GH-10917
1 parent f1333bc commit 2b35431

File tree

6 files changed

+78
-2
lines changed

6 files changed

+78
-2
lines changed

NEWS

+1
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ PHP NEWS
112112
. Added posix_pathconf. (David Carlier)
113113
. Added posix_fpathconf. (David Carlier)
114114
. Fixed zend_parse_arg_long's bool pointer argument assignment. (Cristian Rodriguez)
115+
. Added posix_eaccess. (David Carlier)
115116

116117
- Random:
117118
. Added Randomizer::getBytesFromString(). (Joshua Rüsweg)

ext/posix/config.m4

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ if test "$PHP_POSIX" = "yes"; then
1010

1111
AC_CHECK_HEADERS([sys/mkdev.h sys/sysmacros.h])
1212

13-
AC_CHECK_FUNCS(seteuid setegid setsid getsid getpgid ctermid mkfifo mknod setrlimit getrlimit getgroups makedev initgroups getgrgid_r posix_pathconf)
13+
AC_CHECK_FUNCS(seteuid setegid setsid getsid getpgid ctermid mkfifo mknod setrlimit getrlimit getgroups makedev initgroups getgrgid_r posix_pathconf eaccess)
1414

1515
AC_MSG_CHECKING([for working ttyname_r() implementation])
1616
AC_RUN_IFELSE([AC_LANG_SOURCE([[

ext/posix/posix.c

+38
Original file line numberDiff line numberDiff line change
@@ -713,6 +713,44 @@ PHP_FUNCTION(posix_access)
713713

714714
RETURN_TRUE;
715715
}
716+
717+
#ifdef HAVE_EACCESS
718+
PHP_FUNCTION(posix_eaccess)
719+
{
720+
zend_long mode = 0;
721+
size_t filename_len, ret;
722+
char *filename, *path;
723+
724+
ZEND_PARSE_PARAMETERS_START(1, 2)
725+
Z_PARAM_PATH(filename, filename_len)
726+
Z_PARAM_OPTIONAL
727+
Z_PARAM_LONG(mode)
728+
ZEND_PARSE_PARAMETERS_END();
729+
730+
path = expand_filepath(filename, NULL);
731+
if (!path) {
732+
zend_argument_value_error(1, "must not be empty");
733+
RETURN_THROWS();
734+
}
735+
736+
if (php_check_open_basedir_ex(path, 0)) {
737+
efree(path);
738+
POSIX_G(last_error) = EPERM;
739+
RETURN_FALSE;
740+
}
741+
742+
ret = eaccess(path, mode);
743+
efree(path);
744+
745+
if (ret) {
746+
POSIX_G(last_error) = errno;
747+
RETURN_FALSE;
748+
}
749+
750+
RETURN_TRUE;
751+
}
752+
#endif
753+
716754
/* }}} */
717755

718756
/*

ext/posix/posix.stub.php

+4
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,10 @@ function posix_mknod(string $filename, int $flags, int $major = 0, int $minor =
379379

380380
function posix_access(string $filename, int $flags = 0): bool {}
381381

382+
#ifdef HAVE_EACCESS
383+
function posix_eaccess(string $filename, int $flags = 0): bool {}
384+
#endif
385+
382386
/**
383387
* @return array<string, int|string|array|null>|false
384388
* @refcount 1

ext/posix/posix_arginfo.h

+14-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ext/posix/tests/posix_eaccess.phpt

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
--TEST--
2+
posix_eaccess() with bogus paths
3+
--EXTENSIONS--
4+
posix
5+
--SKIPIF--
6+
<?php
7+
if (!function_exists("posix_eaccess")) die("skip only platforms with posix_eaccess");
8+
?>
9+
--FILE--
10+
<?php
11+
12+
try {
13+
posix_eaccess(str_repeat('bogus path', 1042));
14+
} catch (ValueError $e) {
15+
echo $e->getMessage() . PHP_EOL;
16+
}
17+
18+
?>
19+
--EXPECT--
20+
posix_eaccess(): Argument #1 ($filename) must not be empty

0 commit comments

Comments
 (0)