Initial commit.
[crypto-install.git] / crypto-install.py
1 #!/usr/bin/env python
2 # -*- mode: python; coding: utf-8-unix; -*-
3
4
5 import argparse, ConfigParser, os, sys, textwrap
6
7
8 def print_version ():
9     print ("crypto-install.py GIT-TAG (GIT-REVISION)")
10
11
12 def input_string (prompt=""):
13     if sys.version_info[0] == 2:
14         return raw_input(prompt)
15     return input(prompt)
16
17
18 def parse_arguments ():
19     parser = argparse.ArgumentParser ()
20     parser.add_argument (
21         "-v", "--version",
22         dest = "version",
23         action = "store_true",
24         help = "Display version.")
25     parser.add_argument (
26         "--no-gpg",
27         dest = "gnupg",
28         action = "store_false",
29         help = "Disable GnuPG setup.")
30     parser.add_argument (
31         "--no-ssh",
32         dest = "openssh",
33         action = "store_false",
34         help = "Disable OpenSSH setup.")
35     parser.add_argument (
36         "--ssh-config",
37         dest = "openssh_config",
38         default = "~/.ssh/config",
39         help = "Set path for OpenSSH configuration file.")
40     return parser.parse_args ()
41
42
43 def gnupg_setup ():
44     if False:
45         print("Default GnuPG key already exists.")
46         return
47
48     print (textwrap.fill (textwrap.dedent("""\
49     No default GnuPG key available.  Please enter your information to
50     create a new key."""), width = 80))
51
52     name = input_string("What is your name?  (Max Mustermann) ")
53     email = input_string("What is your email address?  (max@example.de) ")
54     motto = input_string("What is your motto phrase, if any?  (Schlüssel für 2014) ")
55
56
57 def openssh_setup (arguments):
58     if not os.path.exists(arguments.openssh_config):
59         with open(arguments.openssh_config, "w") as ssh_config:
60             ssh_config.write(textwrap.dedent("""\
61             ForwardAgent yes
62             ForwardX11 yes
63             """))
64
65     if os.path.exists (os.path.expanduser ("~/.ssh/id_rsa")) \
66        or os.path.exists (os.path.expanduser ("~/.ssh/id_dsa")):
67         print("OpenSSH key already exists.")
68         return
69
70     print (textwrap.fill (textwrap.dedent("""\
71     No OpenSSH key available.  Generating new key."""), width = 80))
72
73     os.system ("ssh-keygen")
74
75
76 def main ():
77     args = parse_arguments ()
78
79     if args.version:
80         print_version ()
81         sys.exit ()
82
83     if args.gnupg:
84         gnupg_setup ()
85
86     if args.openssh:
87         openssh_setup (args)
88
89
90 if __name__ == "__main__":
91     main ()