1 /*
2  * Copyright 2012, 2014 Andrew Ayer
3  *
4  * This file is part of git-crypt.
5  *
6  * git-crypt is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * git-crypt is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with git-crypt.  If not, see <http://www.gnu.org/licenses/>.
18  *
19  * Additional permission under GNU GPL version 3 section 7:
20  *
21  * If you modify the Program, or any covered work, by linking or
22  * combining it with the OpenSSL project's OpenSSL library (or a
23  * modified version of that library), containing parts covered by the
24  * terms of the OpenSSL or SSLeay licenses, the licensors of the Program
25  * grant you additional permission to convey the resulting work.
26  * Corresponding Source for a non-source form of such a combination
27  * shall include the source code for the parts of OpenSSL used as well
28  * as that of the covered work.
29  */
30 
31 #include "git-crypt.hpp"
32 #include "commands.hpp"
33 #include "util.hpp"
34 #include "crypto.hpp"
35 #include "key.hpp"
36 #include "gpg.hpp"
37 #include "parse_options.hpp"
38 #include <cstring>
39 #include <unistd.h>
40 #include <iostream>
41 #include <string.h>
42 
43 const char*	argv0;
44 
print_usage(std::ostream & out)45 static void print_usage (std::ostream& out)
46 {
47 	out << "Usage: " << argv0 << " COMMAND [ARGS ...]" << std::endl;
48 	out << std::endl;
49 	//     |--------------------------------------------------------------------------------| 80 characters
50 	out << "Common commands:" << std::endl;
51 	out << "  init                 generate a key and prepare repo to use git-crypt" << std::endl;
52 	out << "  status               display which files are encrypted" << std::endl;
53 	//out << "  refresh              ensure all files in the repo are properly decrypted" << std::endl;
54 	out << "  lock                 de-configure git-crypt and re-encrypt files in work tree" << std::endl;
55 	out << std::endl;
56 	out << "GPG commands:" << std::endl;
57 	out << "  add-gpg-user USERID  add the user with the given GPG user ID as a collaborator" << std::endl;
58 	//out << "  rm-gpg-user USERID   revoke collaborator status from the given GPG user ID" << std::endl;
59 	//out << "  ls-gpg-users         list the GPG key IDs of collaborators" << std::endl;
60 	out << "  unlock               decrypt this repo using the in-repo GPG-encrypted key" << std::endl;
61 	out << std::endl;
62 	out << "Symmetric key commands:" << std::endl;
63 	out << "  export-key FILE      export this repo's symmetric key to the given file" << std::endl;
64 	out << "  unlock KEYFILE       decrypt this repo using the given symmetric key" << std::endl;
65 	out << std::endl;
66 	out << "Legacy commands:" << std::endl;
67 	out << "  init KEYFILE         alias for 'unlock KEYFILE'" << std::endl;
68 	out << "  keygen KEYFILE       generate a git-crypt key in the given file" << std::endl;
69 	out << "  migrate-key OLD NEW  migrate the legacy key file OLD to the new format in NEW" << std::endl;
70 	/*
71 	out << std::endl;
72 	out << "Plumbing commands (not to be used directly):" << std::endl;
73 	out << "   clean [LEGACY-KEYFILE]" << std::endl;
74 	out << "   smudge [LEGACY-KEYFILE]" << std::endl;
75 	out << "   diff [LEGACY-KEYFILE] FILE" << std::endl;
76 	*/
77 	out << std::endl;
78 	out << "See 'git-crypt help COMMAND' for more information on a specific command." << std::endl;
79 }
80 
print_version(std::ostream & out)81 static void print_version (std::ostream& out)
82 {
83 	out << "git-crypt " << VERSION << std::endl;
84 }
85 
help_for_command(const char * command,std::ostream & out)86 static bool help_for_command (const char* command, std::ostream& out)
87 {
88 	if (std::strcmp(command, "init") == 0) {
89 		help_init(out);
90 	} else if (std::strcmp(command, "unlock") == 0) {
91 		help_unlock(out);
92 	} else if (std::strcmp(command, "lock") == 0) {
93 		help_lock(out);
94 	} else if (std::strcmp(command, "add-gpg-user") == 0) {
95 		help_add_gpg_user(out);
96 	} else if (std::strcmp(command, "rm-gpg-user") == 0) {
97 		help_rm_gpg_user(out);
98 	} else if (std::strcmp(command, "ls-gpg-users") == 0) {
99 		help_ls_gpg_users(out);
100 	} else if (std::strcmp(command, "export-key") == 0) {
101 		help_export_key(out);
102 	} else if (std::strcmp(command, "keygen") == 0) {
103 		help_keygen(out);
104 	} else if (std::strcmp(command, "migrate-key") == 0) {
105 		help_migrate_key(out);
106 	} else if (std::strcmp(command, "refresh") == 0) {
107 		help_refresh(out);
108 	} else if (std::strcmp(command, "status") == 0) {
109 		help_status(out);
110 	} else {
111 		return false;
112 	}
113 	return true;
114 }
115 
help(int argc,const char ** argv)116 static int help (int argc, const char** argv)
117 {
118 	if (argc == 0) {
119 		print_usage(std::cout);
120 	} else {
121 		if (!help_for_command(argv[0], std::cout)) {
122 			std::clog << "Error: '" << argv[0] << "' is not a git-crypt command. See 'git-crypt help'." << std::endl;
123 			return 1;
124 		}
125 	}
126 	return 0;
127 }
128 
version(int argc,const char ** argv)129 static int version (int argc, const char** argv)
130 {
131 	print_version(std::cout);
132 	return 0;
133 }
134 
135 
main(int argc,const char ** argv)136 int main (int argc, const char** argv)
137 try {
138 	argv0 = argv[0];
139 
140 	/*
141 	 * General initialization
142 	 */
143 
144 	init_std_streams();
145 	init_crypto();
146 
147 	/*
148 	 * Parse command line arguments
149 	 */
150 	int			arg_index = 1;
151 	while (arg_index < argc && argv[arg_index][0] == '-') {
152 		if (std::strcmp(argv[arg_index], "--help") == 0) {
153 			print_usage(std::clog);
154 			return 0;
155 		} else if (std::strcmp(argv[arg_index], "--version") == 0) {
156 			print_version(std::clog);
157 			return 0;
158 		} else if (std::strcmp(argv[arg_index], "--") == 0) {
159 			++arg_index;
160 			break;
161 		} else {
162 			std::clog << argv0 << ": " << argv[arg_index] << ": Unknown option" << std::endl;
163 			print_usage(std::clog);
164 			return 2;
165 		}
166 	}
167 
168 	argc -= arg_index;
169 	argv += arg_index;
170 
171 	if (argc == 0) {
172 		print_usage(std::clog);
173 		return 2;
174 	}
175 
176 	/*
177 	 * Pass off to command handler
178 	 */
179 	const char*		command = argv[0];
180 	--argc;
181 	++argv;
182 
183 	try {
184 		// Public commands:
185 		if (std::strcmp(command, "help") == 0) {
186 			return help(argc, argv);
187 		}
188 		if (std::strcmp(command, "version") == 0) {
189 			return version(argc, argv);
190 		}
191 		if (std::strcmp(command, "init") == 0) {
192 			return init(argc, argv);
193 		}
194 		if (std::strcmp(command, "unlock") == 0) {
195 			return unlock(argc, argv);
196 		}
197 		if (std::strcmp(command, "lock") == 0) {
198 			return lock(argc, argv);
199 		}
200 		if (std::strcmp(command, "add-gpg-user") == 0) {
201 			return add_gpg_user(argc, argv);
202 		}
203 		if (std::strcmp(command, "rm-gpg-user") == 0) {
204 			return rm_gpg_user(argc, argv);
205 		}
206 		if (std::strcmp(command, "ls-gpg-users") == 0) {
207 			return ls_gpg_users(argc, argv);
208 		}
209 		if (std::strcmp(command, "export-key") == 0) {
210 			return export_key(argc, argv);
211 		}
212 		if (std::strcmp(command, "keygen") == 0) {
213 			return keygen(argc, argv);
214 		}
215 		if (std::strcmp(command, "migrate-key") == 0) {
216 			return migrate_key(argc, argv);
217 		}
218 		if (std::strcmp(command, "refresh") == 0) {
219 			return refresh(argc, argv);
220 		}
221 		if (std::strcmp(command, "status") == 0) {
222 			return status(argc, argv);
223 		}
224 		// Plumbing commands (executed by git, not by user):
225 		if (std::strcmp(command, "clean") == 0) {
226 			return clean(argc, argv);
227 		}
228 		if (std::strcmp(command, "smudge") == 0) {
229 			return smudge(argc, argv);
230 		}
231 		if (std::strcmp(command, "diff") == 0) {
232 			return diff(argc, argv);
233 		}
234 	} catch (const Option_error& e) {
235 		std::clog << "git-crypt: Error: " << e.option_name << ": " << e.message << std::endl;
236 		help_for_command(command, std::clog);
237 		return 2;
238 	}
239 
240 	std::clog << "Error: '" << command << "' is not a git-crypt command. See 'git-crypt help'." << std::endl;
241 	return 2;
242 
243 } catch (const Error& e) {
244 	std::cerr << "git-crypt: Error: " << e.message << std::endl;
245 	return 1;
246 } catch (const Gpg_error& e) {
247 	std::cerr << "git-crypt: GPG error: " << e.message << std::endl;
248 	return 1;
249 } catch (const System_error& e) {
250 	std::cerr << "git-crypt: System error: " << e.message() << std::endl;
251 	return 1;
252 } catch (const Crypto_error& e) {
253 	std::cerr << "git-crypt: Crypto error: " << e.where << ": " << e.message << std::endl;
254 	return 1;
255 } catch (Key_file::Incompatible) {
256 	std::cerr << "git-crypt: This repository contains a incompatible key file.  Please upgrade git-crypt." << std::endl;
257 	return 1;
258 } catch (Key_file::Malformed) {
259 	std::cerr << "git-crypt: This repository contains a malformed key file.  It may be corrupted." << std::endl;
260 	return 1;
261 } catch (const std::ios_base::failure& e) {
262 	std::cerr << "git-crypt: I/O error: " << e.what() << std::endl;
263 	return 1;
264 }
265 
266 
267