summaryrefslogtreecommitdiff
path: root/cpio
diff options
context:
space:
mode:
authorMichihiro NAKAJIMA <ggcueroad@gmail.com>2014-09-21 17:45:59 +0900
committerMichihiro NAKAJIMA <ggcueroad@gmail.com>2014-09-21 17:45:59 +0900
commit9769e6f7d5683d79511b40c84fb1c60291e1b72d (patch)
tree36749ad03a5c6c1284ab36a32980051aeca709b4 /cpio
parent96c38181092faa7b76773f00bb3260f87d9ffc0d (diff)
downloadlibarchive-9769e6f7d5683d79511b40c84fb1c60291e1b72d.tar.gz
Implement reading a passphrase from ttys.
Diffstat (limited to 'cpio')
-rw-r--r--cpio/CMakeLists.txt2
-rw-r--r--cpio/cpio.c33
-rw-r--r--cpio/cpio.h1
3 files changed, 36 insertions, 0 deletions
diff --git a/cpio/CMakeLists.txt b/cpio/CMakeLists.txt
index 135d0893..44309971 100644
--- a/cpio/CMakeLists.txt
+++ b/cpio/CMakeLists.txt
@@ -15,6 +15,8 @@ IF(ENABLE_CPIO)
../libarchive_fe/lafe_platform.h
../libarchive_fe/line_reader.c
../libarchive_fe/line_reader.h
+ ../libarchive_fe/passphrase.c
+ ../libarchive_fe/passphrase.h
)
INCLUDE_DIRECTORIES(${PROJECT_SOURCE_DIR}/test_utils)
INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/../libarchive_fe)
diff --git a/cpio/cpio.c b/cpio/cpio.c
index 569e4edd..6dc22378 100644
--- a/cpio/cpio.c
+++ b/cpio/cpio.c
@@ -82,6 +82,7 @@ __FBSDID("$FreeBSD: src/usr.bin/cpio/cpio.c,v 1.15 2008/12/06 07:30:40 kientzle
#include "cpio.h"
#include "err.h"
#include "line_reader.h"
+#include "passphrase.h"
/* Fixed size of uname/gname caches. */
#define name_cache_size 101
@@ -123,6 +124,8 @@ static int restore_time(struct cpio *, struct archive_entry *,
const char *, int fd);
static void usage(void);
static void version(void);
+static const char * passphrase_callback(struct archive *, void *);
+static void passphrase_free(char *);
int
main(int argc, char *argv[])
@@ -422,6 +425,7 @@ main(int argc, char *argv[])
free_cache(cpio->gname_cache);
free_cache(cpio->uname_cache);
free(cpio->destdir);
+ passphrase_free(cpio->ppbuff);
return (cpio->return_value);
}
@@ -580,6 +584,8 @@ mode_out(struct cpio *cpio)
lafe_errc(1, 0, "%s",
archive_error_string(cpio->archive));
}
+ archive_write_set_passphrase_callback(cpio->archive, cpio,
+ &passphrase_callback);
/*
* The main loop: Copy each file into the output archive.
@@ -951,6 +957,7 @@ mode_in(struct cpio *cpio)
cpio->passphrase) != ARCHIVE_OK)
lafe_errc(1, 0, "%s", archive_error_string(a));
}
+ archive_read_set_passphrase_callback(a, cpio, &passphrase_callback);
if (archive_read_open_filename(a, cpio->filename,
cpio->bytes_per_block))
@@ -1059,6 +1066,7 @@ mode_list(struct cpio *cpio)
cpio->passphrase) != ARCHIVE_OK)
lafe_errc(1, 0, "%s", archive_error_string(a));
}
+ archive_read_set_passphrase_callback(a, cpio, &passphrase_callback);
if (archive_read_open_filename(a, cpio->filename,
cpio->bytes_per_block))
@@ -1430,3 +1438,28 @@ cpio_i64toa(int64_t n0)
*--p = '-';
return p;
}
+
+#define PPBUFF_SIZE 1024
+static const char *
+passphrase_callback(struct archive *a, void *_client_data)
+{
+ struct cpio *cpio = (struct cpio *)_client_data;
+ (void)a; /* UNUSED */
+
+ if (cpio->ppbuff == NULL) {
+ cpio->ppbuff = malloc(PPBUFF_SIZE);
+ if (cpio->ppbuff == NULL)
+ lafe_errc(1, errno, "Out of memory");
+ }
+ return lafe_readpassphrase("Enter passphrase:",
+ cpio->ppbuff, PPBUFF_SIZE);
+}
+
+static void
+passphrase_free(char *ppbuff)
+{
+ if (ppbuff != NULL) {
+ memset(ppbuff, 0, PPBUFF_SIZE);
+ free(ppbuff);
+ }
+}
diff --git a/cpio/cpio.h b/cpio/cpio.h
index 227ba53f..1036dece 100644
--- a/cpio/cpio.h
+++ b/cpio/cpio.h
@@ -91,6 +91,7 @@ struct cpio {
struct archive *matching;
char *buff;
size_t buff_size;
+ char *ppbuff;
};
const char *owner_parse(const char *, int *, int *);