Skip to content

Commit

Permalink
Merge pull request #18 from inobulles/copy-files
Browse files Browse the repository at this point in the history
aquarium: `-y` option for copying files from outside of aquariums
  • Loading branch information
obiwac authored Jul 26, 2022
2 parents e9479a8 + 30a8580 commit 04cdccb
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 4 deletions.
1 change: 1 addition & 0 deletions aquariums/aquabsd-installer/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ $prefix -c $pointer -t amd64.aquabsd.$version -k amd64.aquabsd.$version

# TODO copy files in 'files' directory

$prefix -y $pointer files/* /tmp
$prefix -ve $pointer < custom.sh
$prefix -i $pointer -o final.img

Expand Down
83 changes: 79 additions & 4 deletions src/aquarium.c
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,9 @@

static gid_t stoners_gid = 0;

static char** copy_args = NULL;
static size_t copy_args_len = 0;

static char* template = "amd64.aquabsd.0622a";
static char* kernel_template = NULL;

Expand All @@ -146,11 +149,12 @@ static void __dead2 usage(void) {
"usage: %1$s [-r base]\n"
" %1$s [-r base] -c path [-t template] [-k kernel_template]\n"
" %1$s [-r base] -f\n"
" %1$s [-r base] -T path -o template\n"
" %1$s [-r base] -i path -o image\n"
" %1$s [-r base] [-p] [-v] -e path\n"
" %1$s [-r base] [-pv] -e path\n"
" %1$s [-r base] -l\n"
" %1$s [-r base] -s\n",
" %1$s [-r base] -s\n"
" %1$s [-r base] -T path -o template\n"
" %1$s [-r base] -y path source_file ... target_directory\n",
getprogname());

exit(EXIT_FAILURE);
Expand Down Expand Up @@ -1815,6 +1819,63 @@ static int do_img_out(void) {
return EXIT_SUCCESS;
}

// copy files from outside of the aquarium

static int do_copy(void) {
if (!copy_args || copy_args_len < 2) {
usage();
}

char* aquarium_path = __read_pointer_file();

// setuid root

uid_t uid = getuid();

if (setuid(0) < 0) {
errx(EXIT_FAILURE, "setuid(0): %s", strerror(errno));
}

// iterate through all files

char* target = copy_args[--copy_args_len];

while (copy_args_len --> 0) {
char* source = copy_args[copy_args_len];

// load target

char* target_path;
asprintf(&target_path, "%s/%s/%s", aquarium_path, target, strrchr(source, '/'));

int target_fd = creat(target_path, 0660);
free(target_path);

if (target_fd < 0) {
errx(EXIT_FAILURE, "creat(\"%s\"): %s", target_path, strerror(errno));
}

// load source

int fd = open(source, O_RDONLY);

if (fd < 0) {
errx(EXIT_FAILURE, "open(\"%s\"): %s", source, strerror(errno));
}

// copy & close

if (fcopyfile(fd, target_fd, 0, COPYFILE_ALL) < 0) {
errx(EXIT_FAILURE, "fcopyfile(\"%s\", \"%s\"): %s", source, target_path, strerror(errno));
}

close(fd);
close(target_fd);
}

return EXIT_SUCCESS;
}

// main function

typedef int (*action_t) (void);
Expand All @@ -1826,7 +1887,7 @@ int main(int argc, char* argv[]) {

int c;

while ((c = getopt(argc, argv, "c:e:fi:k:lo:pr:st:T:v")) != -1) {
while ((c = getopt(argc, argv, "c:e:f:k:lo:pr:st:T:vy:")) != -1) {
// general options

if (c == 'p') {
Expand Down Expand Up @@ -1875,6 +1936,11 @@ int main(int argc, char* argv[]) {
path = optarg;
}

else if (c == 'y') {
action = do_copy;
path = optarg;
}

// name-passing options

else if (c == 'k') {
Expand All @@ -1897,6 +1963,15 @@ int main(int argc, char* argv[]) {
argc -= optind;
argv += optind;

if (action == do_copy) {
copy_args = argv;
copy_args_len = argc;
}

else if (argc) {
usage();
}

// generate various paths relative to the base path
// we don't really care about freeing these

Expand Down

0 comments on commit 04cdccb

Please sign in to comment.