mirror of
https://git.libssh.org/projects/libssh.git
synced 2026-02-09 18:04:25 +09:00
misc: Introduce internal function ssh_dir_writeable()
The introduced internal function checks if the provided path is for an existing directory which is accessible for writing. Signed-off-by: Anderson Toshiyuki Sasaki <ansasaki@redhat.com> Reviewed-by: Andreas Schneider <asn@cryptomilk.org>
This commit is contained in:
committed by
Andreas Schneider
parent
3737e5f0e7
commit
7857cd1aa5
@@ -26,6 +26,7 @@
|
|||||||
char *ssh_get_user_home_dir(void);
|
char *ssh_get_user_home_dir(void);
|
||||||
char *ssh_get_local_username(void);
|
char *ssh_get_local_username(void);
|
||||||
int ssh_file_readaccess_ok(const char *file);
|
int ssh_file_readaccess_ok(const char *file);
|
||||||
|
int ssh_dir_writeable(const char *path);
|
||||||
|
|
||||||
char *ssh_path_expand_tilde(const char *d);
|
char *ssh_path_expand_tilde(const char *d);
|
||||||
char *ssh_path_expand_escape(ssh_session session, const char *s);
|
char *ssh_path_expand_escape(ssh_session session, const char *s);
|
||||||
|
|||||||
50
src/misc.c
50
src/misc.c
@@ -130,6 +130,31 @@ int ssh_file_readaccess_ok(const char *file) {
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Check if the given path is an existing directory and that is
|
||||||
|
* accessible for writing.
|
||||||
|
*
|
||||||
|
* @param[in] path Path to the directory to be checked
|
||||||
|
*
|
||||||
|
* @return Return 1 if the directory exists and is accessible; 0 otherwise
|
||||||
|
* */
|
||||||
|
int ssh_dir_writeable(const char *path)
|
||||||
|
{
|
||||||
|
struct _stat buffer;
|
||||||
|
int rc;
|
||||||
|
|
||||||
|
rc = _stat(path, &buffer);
|
||||||
|
if (rc < 0) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ((buffer.st_mode & _S_IFDIR) && (buffer.st_mode & _S_IWRITE)) {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
#define SSH_USEC_IN_SEC 1000000LL
|
#define SSH_USEC_IN_SEC 1000000LL
|
||||||
#define SSH_SECONDS_SINCE_1601 11644473600LL
|
#define SSH_SECONDS_SINCE_1601 11644473600LL
|
||||||
|
|
||||||
@@ -247,6 +272,31 @@ int ssh_file_readaccess_ok(const char *file)
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Check if the given path is an existing directory and that is
|
||||||
|
* accessible for writing.
|
||||||
|
*
|
||||||
|
* @param[in] path Path to the directory to be checked
|
||||||
|
*
|
||||||
|
* @return Return 1 if the directory exists and is accessible; 0 otherwise
|
||||||
|
* */
|
||||||
|
int ssh_dir_writeable(const char *path)
|
||||||
|
{
|
||||||
|
struct stat buffer;
|
||||||
|
int rc;
|
||||||
|
|
||||||
|
rc = stat(path, &buffer);
|
||||||
|
if (rc < 0) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (S_ISDIR(buffer.st_mode) && (buffer.st_mode & S_IWRITE)) {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
char *ssh_get_local_username(void)
|
char *ssh_get_local_username(void)
|
||||||
{
|
{
|
||||||
struct passwd pwd;
|
struct passwd pwd;
|
||||||
|
|||||||
@@ -19,6 +19,7 @@
|
|||||||
|
|
||||||
#define TORTURE_TEST_DIR "/usr/local/bin/truc/much/.."
|
#define TORTURE_TEST_DIR "/usr/local/bin/truc/much/.."
|
||||||
|
|
||||||
|
const char template[] = "temp_dir_XXXXXX";
|
||||||
|
|
||||||
static int setup(void **state)
|
static int setup(void **state)
|
||||||
{
|
{
|
||||||
@@ -363,6 +364,51 @@ static void torture_ssh_analyze_banner(void **state) {
|
|||||||
ssh_free(session);
|
ssh_free(session);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void torture_ssh_dir_writeable(UNUSED_PARAM(void **state))
|
||||||
|
{
|
||||||
|
char *tmp_dir = NULL;
|
||||||
|
int rc = 0;
|
||||||
|
FILE *file = NULL;
|
||||||
|
char buffer[256];
|
||||||
|
|
||||||
|
tmp_dir = torture_make_temp_dir(template);
|
||||||
|
assert_non_null(tmp_dir);
|
||||||
|
|
||||||
|
rc = ssh_dir_writeable(tmp_dir);
|
||||||
|
assert_int_equal(rc, 1);
|
||||||
|
|
||||||
|
/* Create a file */
|
||||||
|
snprintf(buffer, sizeof(buffer), "%s/a", tmp_dir);
|
||||||
|
|
||||||
|
file = fopen(buffer, "w");
|
||||||
|
assert_non_null(file);
|
||||||
|
|
||||||
|
fprintf(file, "Hello world!\n");
|
||||||
|
fclose(file);
|
||||||
|
|
||||||
|
/* Negative test for checking a normal file */
|
||||||
|
rc = ssh_dir_writeable(buffer);
|
||||||
|
assert_int_equal(rc, 0);
|
||||||
|
|
||||||
|
/* Negative test for non existent file */
|
||||||
|
snprintf(buffer, sizeof(buffer), "%s/b", tmp_dir);
|
||||||
|
rc = ssh_dir_writeable(buffer);
|
||||||
|
assert_int_equal(rc, 0);
|
||||||
|
|
||||||
|
#ifndef _WIN32
|
||||||
|
/* Negative test for directory without write permission */
|
||||||
|
rc = ssh_mkdir(buffer, 0400);
|
||||||
|
assert_return_code(rc, errno);
|
||||||
|
|
||||||
|
rc = ssh_dir_writeable(buffer);
|
||||||
|
assert_int_equal(rc, 0);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
torture_rmdirs(tmp_dir);
|
||||||
|
|
||||||
|
SAFE_FREE(tmp_dir);
|
||||||
|
}
|
||||||
|
|
||||||
int torture_run_tests(void) {
|
int torture_run_tests(void) {
|
||||||
int rc;
|
int rc;
|
||||||
struct CMUnitTest tests[] = {
|
struct CMUnitTest tests[] = {
|
||||||
@@ -381,6 +427,7 @@ int torture_run_tests(void) {
|
|||||||
cmocka_unit_test(torture_timeout_elapsed),
|
cmocka_unit_test(torture_timeout_elapsed),
|
||||||
cmocka_unit_test(torture_timeout_update),
|
cmocka_unit_test(torture_timeout_update),
|
||||||
cmocka_unit_test(torture_ssh_analyze_banner),
|
cmocka_unit_test(torture_ssh_analyze_banner),
|
||||||
|
cmocka_unit_test(torture_ssh_dir_writeable),
|
||||||
};
|
};
|
||||||
|
|
||||||
ssh_init();
|
ssh_init();
|
||||||
|
|||||||
Reference in New Issue
Block a user