~ecryptfs/ecryptfs/trunk

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/**
 * Copyright (C) 2007 International Business Machines
 * Author(s): Michael Halcrow <mhalcrow@us.ibm.com>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.
 */

#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <pwd.h>
#include <stdio.h>
#include <stdlib.h>
#include <termios.h>
#include <string.h>
#include <errno.h>
#include "config.h"
#include "ecryptfs.h"
#include "io.h"

/**
 * TODO: Use decision graph here
 */
int ecryptfs_generate_key(void)
{
	return -EINVAL;
/*	struct ecryptfs_ctx ctx;
	struct ecryptfs_key_mod *key_mod = NULL;
	char *home;
	char *directory;
	char *file;
	uid_t id;
	struct passwd *pw;
	int rc = 0;

	id = getuid();
	pw = getpwuid(id);
	home = pw->pw_dir;
	printf("\n");
	printf("This is the eCryptfs key generation utility. At any time \n"
	       "you may hit enter to selected a default option appearing in \n"
	       "brackets.\n");
	printf("\n");
	if ((rc = ecryptfs_get_key_mod_list(&ctx))) {
		fprintf(stderr, "Error: eCryptfs was unable to initialize the "
				"PKI modules.\n");
		return 0;
	}
	if (ecryptfs_select_key_mod(&key_mod, &ctx)) {
		fprintf(stderr, "Error: Problem loading the selected PKI.\n");
		return 0;
	}
	file = malloc(MAX_PATH_SIZE);
	if (!file) {
		fprintf(stderr, "Out of memory\n");
		return 0;
	}
	printf("\nEnter the filename where the key should be written.\n"
	       "[%s%s%s/key.pem]:", home, "/.ecryptfs/pki/",
	       key_mod->alias);
	get_string(file, MAX_PATH_SIZE, ECHO);
	if (*file == '\0')
		memcpy(file, "key.pem", 8);
	if (*file == '/') {
		rc = key_mod->ops->generate_key(file);
		if (rc) {
			fprintf(stderr, "Error: unable to write key to file\n");
			return 0;
		}
	} else {
		rc = create_default_dir(home, selected_pki);
		if (rc) {
			fprintf(stderr, "Error: unable to create default pki directory\n");
			goto out;
		}
		rc = create_subdirectory(file, home, selected_pki);
		if (rc) {
			fprintf(stderr, "Error: unable to create the desired subdirectories\n");
			goto out;
		}
		rc = asprintf(&directory, "%s/.ecryptfs/pki/%s/%s", home,
			      selected_pki->pki_name, file);
		if (rc == -1) {
			fprintf(stderr, "Out of memory\n");
			rc = 0;
			goto out;
		}
		rc = selected_pki->ops.generate_key(directory);
		if (rc)
			fprintf(stderr, "Error: unable to write key to file\n");
	}
out:
return rc; */
}

int
create_subdirectory(char *file, char *home, struct ecryptfs_key_mod *key_mod)
{
	char *substring;
	char *directory;
	int rc = 0;

	substring = file;
	while((substring = strstr(substring, "/")) != NULL) {
		char temp = *(substring + 1);
		*(substring + 1) = '\0';
		if (asprintf(&directory, "%s/.ecryptfs/pki/%s/%s",
			     home, key_mod->alias, file) < 0) {
			rc = errno;
			fprintf(stderr, "Error: %s", strerror(errno));
			goto out;
		}
		printf("%s\n",directory);
		if (mkdir(directory,0700) != 0 && errno != EEXIST) {
			rc = errno;
			fprintf(stderr, "Error: %s\n", strerror(errno));
			goto out;
		}
               	free(directory);
		*(substring + 1) = temp;
		substring = substring + 1;
	}
out:
	return rc;
}

int create_default_dir(char *home, struct ecryptfs_key_mod *key_mod)
{
	char *directory;
	int rc = 0;

	if (asprintf(&directory, "%s/.ecryptfs/", home) < 0) {
		rc = errno;
		fprintf(stderr, "Error: %s", strerror(errno));
		goto out;
	}
	if (mkdir(directory,0700) != 0 && errno != EEXIST) {
		rc = errno;
		fprintf(stderr, "Error: %s", strerror(errno));
		goto out;
	}
	free(directory);
	if (asprintf(&directory, "%s/.ecryptfs/pki/", home) < 0) {
		rc = errno;
		fprintf(stderr, "Error: %s", strerror(errno));
		goto out;
	}
	if (mkdir(directory,0700) != 0 && errno != EEXIST) {
		rc = errno;
		fprintf(stderr, "Error: %s", strerror(errno));
		goto out;
	}
	free(directory);
	if (asprintf(&directory, "%s/.ecryptfs/pki/%s/", home,
		     key_mod->alias) < 0) {
		rc = errno;
		fprintf(stderr, "Error: %s", strerror(errno));
		goto out;
	}
	if (mkdir(directory,0700) != 0 && errno != EEXIST) {
		rc = errno;
		fprintf(stderr, "Error: %s", strerror(errno));
		goto out;
	}
	free(directory);
out:
	return rc;
}