예제 #1
0
END_TEST

START_TEST(test_lib_chown_path)
{
	int fd;
	char filename[] = "/tmp/test_lib_chown_path-XXXXXX";
	uid_t user_id;
	gid_t group_id;
	struct passwd *pswd;
	struct group *grp;

	fd = mkstemp(filename);
	ck_assert(fd != -1);

	user_id = getuid();
	pswd = getpwuid(user_id);
	ck_assert(pswd != NULL);

	group_id = getgid();
	grp = getgrgid(group_id);
	ck_assert(grp != NULL);

	ck_assert(chown_path(filename, pswd->pw_name, grp->gr_name) == 0);

	ck_assert(remove(filename) == 0);
}
예제 #2
0
static void write_files_item(GNode* node, gpointer data) {
    const GNode* content;
    const GNode* path;
    const GNode* permissions;
    const GNode* owner;
    gchar **tokens;
    guint tokens_size;
    mode_t mode;
    const gchar* username = "";
    const gchar* groupname = "";

    CLOUD_CONFIG_KEY(CONTENT, "content");
    CLOUD_CONFIG_KEY(PATH, "path");
    CLOUD_CONFIG_KEY(OWNER, "owner");
    CLOUD_CONFIG_KEY(PERMISSIONS, "permissions");

    content = cloud_config_find(node, CONTENT);
    if (!content) {
        LOG(MOD "Unable to write file without \"content\" value.\n");
        return;
    }

    path = cloud_config_find(node, PATH);
    if (!path) {
        LOG(MOD "Unable to write file without \"path\" value.\n");
        return;
    }

    permissions = cloud_config_find(node, PERMISSIONS);
    owner = cloud_config_find(node, OWNER);

    /* assure the folder exists, and create if nexessary */
    char* dir = strdup((char *)path->data);
    dir = dirname(dir);
    int r = access(dir, W_OK);
    if (r == -1) {
        if (errno & ENOENT) {
            LOG(MOD "Creating part or all of %s\n", dir);
            gchar command[LINE_MAX];
            command[0] = 0;
            g_snprintf(command, LINE_MAX, "mkdir -p %s", dir);
            exec_task(command);
        } else {
            LOG(MOD "Path error: %s", strerror(errno));
            free(dir);
            return;
        }
    }
    free(dir);

    LOG(MOD "Writing to file %s: %s\n", (char*)path->data, (char*)content->data);

    const int fd = open(path->data, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);
    if (fd == -1) {
        LOG(MOD "Cannot open %s.\n", (char*)path->data);
        return;
    }

    write(fd, content->data, strlen(content->data));

    if (permissions) {
        if (cloud_config_int_base(permissions, (int *)&mode, 8)) {
            fchmod(fd, mode);
        }
    }

    close(fd);

    if (owner) {
        tokens = g_strsplit_set(owner->data, ":.", 2);
        tokens_size = g_strv_length(tokens);
        if (tokens_size > 0) {
            username = tokens[0];
            if (tokens_size > 1) {
                groupname = tokens[1];
            }
            chown_path(path->data, username, groupname);
        }
        g_strfreev(tokens);
    }
}
예제 #3
0
static void write_files_item(GNode* node, gpointer data) {
    const GNode* content;
    const GNode* path;
    const GNode* permissions;
    const GNode* owner;
    gchar **tokens;
    guint tokens_size;
    mode_t mode;
    const gchar* username = "";
    const gchar* groupname = "";

    CLOUD_CONFIG_KEY(CONTENT, "content");
    CLOUD_CONFIG_KEY(PATH, "path");
    CLOUD_CONFIG_KEY(OWNER, "owner");
    CLOUD_CONFIG_KEY(PERMISSIONS, "permissions");

    content = cloud_config_find(node, CONTENT);
    if (!content) {
        LOG(MOD "Unable to write file without \"content\" value.\n");
        return;
    }

    path = cloud_config_find(node, PATH);
    if (!path) {
        LOG(MOD "Unable to write file without \"path\" value.\n");
        return;
    }

    permissions = cloud_config_find(node, PERMISSIONS);
    owner = cloud_config_find(node, OWNER);

    LOG(MOD "Writing to file %s: %s\n", (char*)path->data, (char*)content->data);

    const int fd = open(path->data, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);
    if (fd == -1) {
        LOG(MOD "Cannot open %s.\n", (char*)path->data);
        return;
    }

    write(fd, content->data, strlen(content->data));

    if (permissions) {
        if (cloud_config_int_base(permissions, (int *)&mode, 8)) {
            fchmod(fd, mode);
        }
    }

    close(fd);

    if (owner) {
        tokens = g_strsplit_set(owner->data, ":.", 2);
        tokens_size = g_strv_length(tokens);
        if (tokens_size > 0) {
            username = tokens[0];
            if (tokens_size > 1) {
                groupname = tokens[1];
            }
            chown_path(path->data, username, groupname);
        }
        g_strfreev(tokens);
    }
}
예제 #4
0
 virtual int on_reached_special_file() override
 {
     return chown_path() ? Action::FTS_OK : Action::FTS_Fail;
 }
예제 #5
0
bool write_ssh_keys(const GString* data, const gchar* username) {
	int i;
	gchar auth_keys_file[PATH_MAX];
	gchar* auth_keys_content = NULL;
	gchar** vector_ssh_keys = NULL;
	GString* ssh_keys = NULL;
	struct passwd pwd;
	struct passwd* pwd_result;
	char* pwd_buf = NULL;
	long int pwd_bufsize;
	struct stat st;

	pwd_bufsize = sysconf(_SC_GETPW_R_SIZE_MAX);
	if (pwd_bufsize == -1) {
		pwd_bufsize = 1<<14;
	}

	pwd_buf = malloc((size_t)pwd_bufsize);
	if (pwd_buf == NULL) {
		LOG(MOD "Unable to allocate memory for passwd buffer\n");
		return false;
	}

	getpwnam_r(username, &pwd, pwd_buf, (size_t)pwd_bufsize, &pwd_result);
	if (pwd_result == NULL) {
		LOG(MOD "User not found '%s'\n", username);
		free(pwd_buf);
		return false;
	}

	if (pwd.pw_dir) {
		g_snprintf(auth_keys_file, PATH_MAX, "%s/.ssh/", pwd.pw_dir);
		free(pwd_buf);

		if (make_dir(auth_keys_file, S_IRWXU) != 0) {
			LOG(MOD "Cannot create %s.\n", auth_keys_file);
			return false;
		}

		if (chown_path(auth_keys_file, username, username) != 0) {
			LOG(MOD "Cannot change the owner and group of %s.\n", auth_keys_file);
			return false;
		}

		g_strlcat(auth_keys_file, "authorized_keys", PATH_MAX);

		if (stat(auth_keys_file, &st) != 0) {
			if (!write_file(data->str, data->len, auth_keys_file, O_CREAT|O_WRONLY, S_IRUSR|S_IWUSR)) {
				return false;
			}
		} else {
			if (!g_file_get_contents(auth_keys_file, &auth_keys_content, NULL, NULL)) {
				return false;
			}

			ssh_keys = g_string_new("");

			vector_ssh_keys = g_strsplit(data->str, "\n", -1);

			for (i=0; vector_ssh_keys[i]; ++i) {
				if (!g_strstr_len(auth_keys_content, -1, vector_ssh_keys[i])) {
					g_string_append_printf(ssh_keys, "%s\n", vector_ssh_keys[i]);
				}
			}

			g_free(auth_keys_content);
			g_strfreev(vector_ssh_keys);

			if (!write_file(ssh_keys->str, ssh_keys->len, auth_keys_file, O_APPEND|O_WRONLY, S_IRUSR|S_IWUSR)) {
				g_string_free(ssh_keys, true);
				return false;
			}

			g_string_free(ssh_keys, true);
		}

		if (chown_path(auth_keys_file, username, username) != 0) {
			LOG(MOD "Cannot change the owner and group of %s.\n", auth_keys_file);
			return false;
		}
	} else {
		free(pwd_buf);
	}

	return true;
}
예제 #6
0
 virtual int on_reached_symlink() override
 {
     return chown_path() ? Action::FTS_OK : Action::FTS_Fail;
 }
예제 #7
0
 virtual int on_reached_directory_post() override
 {
     return chown_path() ? Action::FTS_OK : Action::FTS_Fail;
 }
예제 #8
0
 Actions on_reached_special_file() override
 {
     return chown_path() ? Action::Ok : Action::Fail;
 }
예제 #9
0
 Actions on_reached_symlink() override
 {
     return chown_path() ? Action::Ok : Action::Fail;
 }
예제 #10
0
 Actions on_reached_directory_post() override
 {
     return chown_path() ? Action::Ok : Action::Fail;
 }