~ecryptfs/ecryptfs/trunk

« back to all changes in this revision

Viewing changes to src/utils/gen_key.c

  • Committer: mhalcrow@us.ibm.com
  • Date: 2007-11-06 22:56:01 UTC
  • Revision ID: git-v1:f8357de9d554b274497b5cce9db4347254b7e7eb
Initial import of eCryptfs filesystem userspace utilities (mount helper, daemon component,
etc.)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
 * Copyright (C) 2007 International Business Machines
 
3
 * Author(s): Michael Halcrow <mhalcrow@us.ibm.com>
 
4
 *
 
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.
 
9
 *
 
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.
 
14
 *
 
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
 
18
 * 02111-1307, USA.
 
19
 */
 
20
 
 
21
#include <unistd.h>
 
22
#include <sys/stat.h>
 
23
#include <sys/types.h>
 
24
#include <pwd.h>
 
25
#include <stdio.h>
 
26
#include <stdlib.h>
 
27
#include <termios.h>
 
28
#include <string.h>
 
29
#include <errno.h>
 
30
#include "config.h"
 
31
#include "ecryptfs.h"
 
32
#include "io.h"
 
33
 
 
34
/**
 
35
 * TODO: Use decision graph here
 
36
 */
 
37
int ecryptfs_generate_key(void)
 
38
{
 
39
        return -EINVAL;
 
40
/*      struct ecryptfs_ctx ctx;
 
41
        struct ecryptfs_key_mod *key_mod = NULL;
 
42
        char *home;
 
43
        char *directory;
 
44
        char *file;
 
45
        uid_t id;
 
46
        struct passwd *pw;
 
47
        int rc = 0;
 
48
 
 
49
        id = getuid();
 
50
        pw = getpwuid(id);
 
51
        home = pw->pw_dir;
 
52
        printf("\n");
 
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"
 
55
               "brackets.\n");
 
56
        printf("\n");
 
57
        if ((rc = ecryptfs_get_key_mod_list(&ctx))) {
 
58
                fprintf(stderr, "Error: eCryptfs was unable to initialize the "
 
59
                                "PKI modules.\n");
 
60
                return 0;
 
61
        }
 
62
        if (ecryptfs_select_key_mod(&key_mod, &ctx)) {
 
63
                fprintf(stderr, "Error: Problem loading the selected PKI.\n");
 
64
                return 0;
 
65
        }
 
66
        file = malloc(MAX_PATH_SIZE);
 
67
        if (!file) {
 
68
                fprintf(stderr, "Out of memory\n");
 
69
                return 0;
 
70
        }
 
71
        printf("\nEnter the filename where the key should be written.\n"
 
72
               "[%s%s%s/key.pem]:", home, "/.ecryptfs/pki/",
 
73
               key_mod->alias);
 
74
        get_string(file, MAX_PATH_SIZE, ECHO);
 
75
        if (*file == '\0')
 
76
                memcpy(file, "key.pem", 8);
 
77
        if (*file == '/') {
 
78
                rc = key_mod->ops->generate_key(file);
 
79
                if (rc) {
 
80
                        fprintf(stderr, "Error: unable to write key to file\n");
 
81
                        return 0;
 
82
                }
 
83
        } else {
 
84
                rc = create_default_dir(home, selected_pki);
 
85
                if (rc) {
 
86
                        fprintf(stderr, "Error: unable to create default pki directory\n");
 
87
                        goto out;
 
88
                }
 
89
                rc = create_subdirectory(file, home, selected_pki);
 
90
                if (rc) {
 
91
                        fprintf(stderr, "Error: unable to create the desired subdirectories\n");
 
92
                        goto out;
 
93
                }
 
94
                rc = asprintf(&directory, "%s/.ecryptfs/pki/%s/%s", home,
 
95
                              selected_pki->pki_name, file);
 
96
                if (rc == -1) {
 
97
                        fprintf(stderr, "Out of memory\n");
 
98
                        rc = 0;
 
99
                        goto out;
 
100
                }
 
101
                rc = selected_pki->ops.generate_key(directory);
 
102
                if (rc)
 
103
                        fprintf(stderr, "Error: unable to write key to file\n");
 
104
        }
 
105
out:
 
106
return rc; */
 
107
}
 
108
 
 
109
int
 
110
create_subdirectory(char *file, char *home, struct ecryptfs_key_mod *key_mod)
 
111
{
 
112
        char *substring;
 
113
        char *directory;
 
114
        int rc = 0;
 
115
 
 
116
        substring = file;
 
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) {
 
122
                        rc = errno;
 
123
                        fprintf(stderr, "Error: %s", strerror(errno));
 
124
                        goto out;
 
125
                }
 
126
                printf("%s\n",directory);
 
127
                if (mkdir(directory,0700) != 0 && errno != EEXIST) {
 
128
                        rc = errno;
 
129
                        fprintf(stderr, "Error: %s\n", strerror(errno));
 
130
                        goto out;
 
131
                }
 
132
                free(directory);
 
133
                *(substring + 1) = temp;
 
134
                substring = substring + 1;
 
135
        }
 
136
out:
 
137
        return rc;
 
138
}
 
139
 
 
140
int create_default_dir(char *home, struct ecryptfs_key_mod *key_mod)
 
141
{
 
142
        char *directory;
 
143
        int rc = 0;
 
144
 
 
145
        if (asprintf(&directory, "%s/.ecryptfs/", home) < 0) {
 
146
                rc = errno;
 
147
                fprintf(stderr, "Error: %s", strerror(errno));
 
148
                goto out;
 
149
        }
 
150
        if (mkdir(directory,0700) != 0 && errno != EEXIST) {
 
151
                rc = errno;
 
152
                fprintf(stderr, "Error: %s", strerror(errno));
 
153
                goto out;
 
154
        }
 
155
        free(directory);
 
156
        if (asprintf(&directory, "%s/.ecryptfs/pki/", home) < 0) {
 
157
                rc = errno;
 
158
                fprintf(stderr, "Error: %s", strerror(errno));
 
159
                goto out;
 
160
        }
 
161
        if (mkdir(directory,0700) != 0 && errno != EEXIST) {
 
162
                rc = errno;
 
163
                fprintf(stderr, "Error: %s", strerror(errno));
 
164
                goto out;
 
165
        }
 
166
        free(directory);
 
167
        if (asprintf(&directory, "%s/.ecryptfs/pki/%s/", home,
 
168
                     key_mod->alias) < 0) {
 
169
                rc = errno;
 
170
                fprintf(stderr, "Error: %s", strerror(errno));
 
171
                goto out;
 
172
        }
 
173
        if (mkdir(directory,0700) != 0 && errno != EEXIST) {
 
174
                rc = errno;
 
175
                fprintf(stderr, "Error: %s", strerror(errno));
 
176
                goto out;
 
177
        }
 
178
        free(directory);
 
179
out:
 
180
        return rc;
 
181
}