summaryrefslogtreecommitdiff
path: root/src/utils/ecryptfs_add_passphrase.c
blob: a2b3345fc38c16bc474ead1b17f0c8ee203479a7 (plain)
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
/**
 * Copyright (C) 2007 International Business Machines
 * Author(s): Michael Halcrow <mhalcrow@us.ibm.com>
 *            Dustin Kirkland <kirkland@ubuntu.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 <stdio.h>
#include <string.h>
#include <ecryptfs.h>

void usage(void)
{
	printf("Usage:\n"
	       "ecryptfs-add-passphrase [--fnek]\n"
	       "or\n"
	       "printf \"%%s\" \"passphrase\" | ecryptfs-add-passphrase"
	       " [--fnek] -\n"
	       "\n");
}

int main(int argc, char *argv[])
{
	char *passphrase;
	char auth_tok_sig_hex[ECRYPTFS_SIG_SIZE_HEX + 1];
	char salt[ECRYPTFS_SALT_SIZE];
	char salt_hex[ECRYPTFS_SALT_SIZE_HEX];
	int rc = 0;
	int fnek = 0;
	uint32_t version;

	if (argc == 1) {
		/* interactive mode */
		passphrase = ecryptfs_get_passphrase("Passphrase");
	} else if (argc == 2 &&
		   strlen(argv[1]) == 6 && strncmp(argv[1], "--fnek", 6) == 0) {
		/* interactive mode, plus fnek */
		passphrase = ecryptfs_get_passphrase("Passphrase");
		fnek = 1;
	} else if (argc == 2 &&
		   strlen(argv[1]) == 1 && strncmp(argv[1], "-", 1) == 0) {
		/* stdin mode */
		passphrase = ecryptfs_get_passphrase(NULL);
	} else if (argc == 3 &&
		/* stdin mode, plus fnek */
		   (strlen(argv[1])==6 && strncmp(argv[1], "--fnek", 6)==0) &&
		   (strlen(argv[2])==1 && strncmp(argv[2], "-", 1)==0)) {
		passphrase = ecryptfs_get_passphrase(NULL);
		fnek = 1;
	} else {
		usage();
		goto out;
	}
	if (passphrase == NULL ||
	    strlen(passphrase) > ECRYPTFS_MAX_PASSWORD_LENGTH) {
		usage();
		rc = 1;
		goto out;
	}
	if (fnek == 1) {
		rc = ecryptfs_get_version(&version);
		if (rc!=0 || !ecryptfs_supports_filename_encryption(version)) { 
			fprintf(stderr, "%s\n", ECRYPTFS_ERROR_FNEK_SUPPORT);
			rc = 1;
			goto out;
		}
	}

	rc = ecryptfs_read_salt_hex_from_rc(salt_hex);
	if (rc) {
		from_hex(salt, ECRYPTFS_DEFAULT_SALT_HEX, ECRYPTFS_SALT_SIZE);
	} else
		from_hex(salt, salt_hex, ECRYPTFS_SALT_SIZE);
	if ((rc = ecryptfs_add_passphrase_key_to_keyring(auth_tok_sig_hex,
							 passphrase,
							 salt)) < 0) {
		fprintf(stderr, "%s [%d]\n", ECRYPTFS_ERROR_INSERT_KEY, rc);
		fprintf(stderr, "%s\n", ECRYPTFS_INFO_CHECK_LOG);
		rc = 1;
		goto out;
	} else
		rc = 0;
	auth_tok_sig_hex[ECRYPTFS_SIG_SIZE_HEX] = '\0';
	printf("Inserted auth tok with sig [%s] into the user session "
	       "keyring\n", auth_tok_sig_hex);

	if (fnek == 0) {
		goto out;
	}

	/* If we make it here, filename encryption is enabled, and it has
	 * been requested that we add the fnek to the keyring too
	 */
	if ((rc = ecryptfs_add_passphrase_key_to_keyring(auth_tok_sig_hex,
				 passphrase,
				 ECRYPTFS_DEFAULT_SALT_FNEK_HEX)) < 0) {
		fprintf(stderr, "%s [%d]\n", ECRYPTFS_ERROR_INSERT_KEY, rc);
		fprintf(stderr, "%s\n", ECRYPTFS_INFO_CHECK_LOG);
		rc = 1;
		goto out;
	} else
		rc = 0;
	auth_tok_sig_hex[ECRYPTFS_SIG_SIZE_HEX] = '\0';
	printf("Inserted auth tok with sig [%s] into the user session "
	       "keyring\n", auth_tok_sig_hex);

out:
	return rc;
}