2
* Copyright (C) 2007 International Business Machines
3
* Author(s): Michael Halcrow <mhalcrow@us.ibm.com>
5
* This program is free software; you can redistribute it and/or
6
* modify it under the terms of the GNU General Public License as
7
* published by the Free Software Foundation; either version 2 of the
8
* License, or (at your option) any later version.
10
* This program is distributed in the hope that it will be useful, but
11
* WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
* General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
23
#include <sys/types.h>
35
* TODO: Use decision graph here
37
int ecryptfs_generate_key(void)
40
/* struct ecryptfs_ctx ctx;
41
struct ecryptfs_key_mod *key_mod = NULL;
53
printf("This is the eCryptfs key generation utility. At any time \n"
54
"you may hit enter to selected a default option appearing in \n"
57
if ((rc = ecryptfs_get_key_mod_list(&ctx))) {
58
fprintf(stderr, "Error: eCryptfs was unable to initialize the "
62
if (ecryptfs_select_key_mod(&key_mod, &ctx)) {
63
fprintf(stderr, "Error: Problem loading the selected PKI.\n");
66
file = malloc(MAX_PATH_SIZE);
68
fprintf(stderr, "Out of memory\n");
71
printf("\nEnter the filename where the key should be written.\n"
72
"[%s%s%s/key.pem]:", home, "/.ecryptfs/pki/",
74
get_string(file, MAX_PATH_SIZE, ECHO);
76
memcpy(file, "key.pem", 8);
78
rc = key_mod->ops->generate_key(file);
80
fprintf(stderr, "Error: unable to write key to file\n");
84
rc = create_default_dir(home, selected_pki);
86
fprintf(stderr, "Error: unable to create default pki directory\n");
89
rc = create_subdirectory(file, home, selected_pki);
91
fprintf(stderr, "Error: unable to create the desired subdirectories\n");
94
rc = asprintf(&directory, "%s/.ecryptfs/pki/%s/%s", home,
95
selected_pki->pki_name, file);
97
fprintf(stderr, "Out of memory\n");
101
rc = selected_pki->ops.generate_key(directory);
103
fprintf(stderr, "Error: unable to write key to file\n");
110
create_subdirectory(char *file, char *home, struct ecryptfs_key_mod *key_mod)
117
while((substring = strstr(substring, "/")) != NULL) {
118
char temp = *(substring + 1);
119
*(substring + 1) = '\0';
120
if (asprintf(&directory, "%s/.ecryptfs/pki/%s/%s",
121
home, key_mod->alias, file) < 0) {
123
fprintf(stderr, "Error: %s", strerror(errno));
126
printf("%s\n",directory);
127
if (mkdir(directory,0700) != 0 && errno != EEXIST) {
129
fprintf(stderr, "Error: %s\n", strerror(errno));
133
*(substring + 1) = temp;
134
substring = substring + 1;
140
int create_default_dir(char *home, struct ecryptfs_key_mod *key_mod)
145
if (asprintf(&directory, "%s/.ecryptfs/", home) < 0) {
147
fprintf(stderr, "Error: %s", strerror(errno));
150
if (mkdir(directory,0700) != 0 && errno != EEXIST) {
152
fprintf(stderr, "Error: %s", strerror(errno));
156
if (asprintf(&directory, "%s/.ecryptfs/pki/", home) < 0) {
158
fprintf(stderr, "Error: %s", strerror(errno));
161
if (mkdir(directory,0700) != 0 && errno != EEXIST) {
163
fprintf(stderr, "Error: %s", strerror(errno));
167
if (asprintf(&directory, "%s/.ecryptfs/pki/%s/", home,
168
key_mod->alias) < 0) {
170
fprintf(stderr, "Error: %s", strerror(errno));
173
if (mkdir(directory,0700) != 0 && errno != EEXIST) {
175
fprintf(stderr, "Error: %s", strerror(errno));