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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
|
/**
* Copyright (C) 2006-2007 International Business Machines Corp.
* Author(s): Mike 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 "config.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <syslog.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include "../include/ecryptfs.h"
#include "../include/decision_graph.h"
static int tf_passwd(struct ecryptfs_ctx *ctx, struct param_node *node,
struct val_node **head, void **foo)
{
int rc;
if (!node->val)
return -EINVAL;
if ((rc = stack_push(head, node->val)))
return rc;
node->val = NULL;
return DEFAULT_TOK;
}
static int tf_pass_file(struct ecryptfs_ctx *ctx, struct param_node *node,
struct val_node **head, void **foo)
{
char *tmp_val = NULL;
int fd;
struct ecryptfs_name_val_pair *file_head;
struct ecryptfs_name_val_pair *walker;
int rc = 0;
file_head = malloc(sizeof(struct ecryptfs_name_val_pair));
if (!file_head) {
rc = -ENOMEM;
goto out;
}
memset(file_head, 0, sizeof(struct ecryptfs_name_val_pair));
if (strcmp(node->mnt_opt_names[0], "passphrase_passwd_file") == 0) {
fd = open(node->val, O_RDONLY);
if (fd == -1) {
rc = -errno;
syslog(LOG_ERR, "%s: Error whilst attempting to open "
"[%s]; errno = [%m]\n", __FUNCTION__, node->val);
goto out;
}
} else if (strcmp(node->mnt_opt_names[0], "passphrase_passwd_fd") == 0) {
fd = strtol(node->val, NULL, 0);
} else {
syslog(LOG_ERR, "%s: Invalid file descriptor qualifier\n",
__FUNCTION__);
rc = MOUNT_ERROR;
goto out;
}
rc = parse_options_file(fd, file_head);
close(fd);
if (rc) {
syslog(LOG_ERR, "%s: Error parsing file for passwd; "
"rc = [%d]\n", __FUNCTION__, rc);
goto out;
}
walker = file_head->next;
while (walker) {
if (strcmp(walker->name, "passphrase_passwd") == 0
|| strcmp(walker->name, "passwd") == 0) {
if (asprintf(&tmp_val, "%s", walker->value) < 0) {
rc = -ENOMEM;
goto out;
}
stack_push(head, tmp_val);
break;
}
walker = walker->next;
}
if (!walker) {
syslog(LOG_ERR, "%s: Cannot find [passwd] directive\n",
__FUNCTION__);
rc = MOUNT_ERROR;
goto out;
}
free_name_val_pairs(file_head);
file_head = NULL;
walker = NULL;
out:
free(node->val);
node->val = NULL;
return rc;
}
static int tf_salt(struct ecryptfs_ctx *ctx, struct param_node *node,
struct val_node **head, void **foo)
{
char *passwd;
char salt[ECRYPTFS_SALT_SIZE];
char *salt_hex;
char *auth_tok_sig;
char *param;
int rc = 0;
if (!node->val)
rc = asprintf(&node->val, "%s", node->default_val);
if (rc == -1)
return -ENOMEM;
stack_push(head, node->val);
node->val = NULL;
stack_pop_val(head, (void *)&salt_hex);
stack_pop_val(head, (void *)&passwd);
auth_tok_sig = malloc(ECRYPTFS_SIG_SIZE_HEX + 1);
if (!auth_tok_sig) {
rc = -ENOMEM;
goto out;
}
from_hex(salt, salt_hex, ECRYPTFS_SIG_SIZE);
rc = ecryptfs_add_passphrase_key_to_keyring(auth_tok_sig, passwd, salt);
if (rc < 0) {
free(auth_tok_sig);
goto out;
}
rc = asprintf(¶m, "ecryptfs_sig=%s", auth_tok_sig);
if (rc == -1) {
free(auth_tok_sig);
rc = -ENOMEM;
goto out;
}
free(auth_tok_sig);
rc = stack_push(head, param);
out:
free(salt_hex);
free(passwd);
if (rc)
return rc;
return DEFAULT_TOK;
}
#define ECRYPTFS_PASSPHRASE_TOK 0
#define ECRYPTFS_PASSWD_TOK 1
#define ECRYPTFS_PASS_FILE_TOK 2
#define ECRYPTFS_PASS_FD_TOK 3
#define ECRYPTFS_SALT_TOK 4
struct param_node passphrase_param_nodes[] = {
/* ECRYPTFS_PASSPHRASE_TOK = 0 */
{.num_mnt_opt_names = 1,
.mnt_opt_names = {"passphrase_type"},
.prompt = "Method for providing the passphrase",
.val_type = VAL_STR,
.val = NULL,
.display_opts = NULL,
.default_val = "passphrase_passwd",
.flags = (ECRYPTFS_PARAM_FLAG_NO_VALUE
| ECRYPTFS_ALLOW_IMPLICIT_TRANSITION
| ECRYPTFS_IMPLICIT_OVERRIDE_DEFAULT),
.num_transitions = 3,
.tl = {{.val = "passphrase_passwd",
.pretty_val = "Provide passphrase directly",
.next_token = &passphrase_param_nodes[ECRYPTFS_PASSWD_TOK],
.trans_func = NULL},
{.val = "passphrase_passwd_file",
.pretty_val = "File containing passphrase (only use secure "
"media)",
.next_token = &passphrase_param_nodes[ECRYPTFS_PASS_FILE_TOK],
.trans_func = NULL},
{.val = "passphrase_passwd_fd",
.pretty_val = "File descriptor for file containing passphrase",
.next_token = &passphrase_param_nodes[ECRYPTFS_PASS_FD_TOK],
.trans_func = NULL}}},
/* ECRYPTFS_PASSWD_TOK = 1 */
{.num_mnt_opt_names = 2,
.mnt_opt_names = {"passphrase_passwd", "passwd"},
.prompt = "Passphrase",
.val_type = VAL_STR,
.val = NULL,
.display_opts = NULL,
.default_val = NULL,
.flags = (ECRYPTFS_PARAM_FLAG_MASK_OUTPUT
| ECRYPTFS_NONEMPTY_VALUE_REQUIRED),
.num_transitions = 2,
.tl = {{.val = "passphrase_salt",
.pretty_val = "salt",
.next_token = &passphrase_param_nodes[ECRYPTFS_SALT_TOK],
.trans_func = tf_passwd},
{.val = "default",
.pretty_val = "default",
.next_token = &passphrase_param_nodes[ECRYPTFS_SALT_TOK],
.trans_func = tf_passwd}}},
/* ECRYPTFS_PASS_FILE_TOK = 2 */
{.num_mnt_opt_names = 2,
.mnt_opt_names = {"passphrase_passwd_file", "passfile"},
.prompt = "Passphrase File",
.val_type = VAL_STR,
.val = NULL,
.display_opts = NULL,
.default_val = NULL,
.flags = ECRYPTFS_PARAM_FLAG_ECHO_INPUT
| ECRYPTFS_NONEMPTY_VALUE_REQUIRED,
.num_transitions = 2,
.tl = {{.val = "passphrase_salt",
.pretty_val = "salt",
.next_token = &passphrase_param_nodes[ECRYPTFS_SALT_TOK],
.trans_func = tf_pass_file},
{.val = "default",
.pretty_val = "default",
.next_token = &passphrase_param_nodes[ECRYPTFS_SALT_TOK],
.trans_func = tf_pass_file}}},
/* ECRYPTFS_PASS_FD_TOK = 3 */
{.num_mnt_opt_names = 2,
.mnt_opt_names = {"passphrase_passwd_fd", "passfd"},
.prompt = "Passphrase File Discriptor",
.val_type = VAL_STR,
.val = NULL,
.display_opts = NULL,
.default_val = NULL,
.flags = ECRYPTFS_PARAM_FLAG_ECHO_INPUT
| ECRYPTFS_NONEMPTY_VALUE_REQUIRED,
.num_transitions = 2,
.tl = {{.val = "salt",
.pretty_val = "salt",
.next_token = &passphrase_param_nodes[ECRYPTFS_SALT_TOK],
.trans_func = tf_pass_file},
{.val = "default",
.pretty_val = "default",
.next_token = &passphrase_param_nodes[ECRYPTFS_SALT_TOK],
.trans_func = tf_pass_file}}},
/* ECRYPTFS_SALT_TOK = 4 */
{.num_mnt_opt_names = 2,
.mnt_opt_names = {"passphrase_salt", "salt"},
.prompt = "Salt (hexadecimal representation)",
.val_type = VAL_HEX,
.val = NULL,
.display_opts = NULL,
.default_val = ECRYPTFS_DEFAULT_SALT_HEX,
.flags = 0,
.num_transitions = 1,
.tl = {{.val = NULL,
.pretty_val = NULL,
.next_token = NULL,
.trans_func = tf_salt}}},
};
struct transition_node passphrase_transition = {
.val = "passphrase",
.pretty_val = "Passphrase",
.next_token = &(passphrase_param_nodes[0]),
.trans_func = NULL
};
static int ecryptfs_passphrase_get_param_subgraph_trans_node(
struct transition_node **trans, uint32_t version)
{
if ((version & ECRYPTFS_VERSIONING_PASSPHRASE) == 0)
return -1;
*trans = &passphrase_transition;
return 0;
}
static int ecryptfs_passphrase_init(char **alias)
{
int rc = 0;
if (asprintf(alias, "passphrase") == -1) {
syslog(LOG_ERR, "Out of memory\n");
rc = -ENOMEM;
goto out;
}
out:
return rc;
}
static int ecryptfs_passphrase_destroy(unsigned char *blob)
{
return 0;
}
static int ecryptfs_passphrase_finalize(void)
{
return 0;
}
struct ecryptfs_key_mod_ops ecryptfs_passphrase_ops = {
&ecryptfs_passphrase_init,
NULL,
NULL,
NULL,
&ecryptfs_passphrase_get_param_subgraph_trans_node,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
&ecryptfs_passphrase_destroy,
&ecryptfs_passphrase_finalize
};
struct ecryptfs_key_mod_ops *get_key_mod_ops(void)
{
return &ecryptfs_passphrase_ops;
}
/**
* Builtin handle
*/
struct ecryptfs_key_mod_ops *passphrase_get_key_mod_ops(void)
{
return get_key_mod_ops();
}
|