Example #1
0
def main():

    # Command line examples
    #
    # -s is always a valid option
    #
    # Display all sites:
    #   gpgpass
    #
    # Show login info for a certain site:
    #   gpgpass somesite.com
    #
    # Add/edit a site's information:
    #   gpgpass -a -r [email protected] site.com

    usage = "usage: %prog [options] site"
    parser = optparse.OptionParser(usage=usage)
    parser.add_option("-s", "--store", dest="password_store",
                      help="Path to the password store", metavar="DIRECTORY")
    parser.add_option("-r", "--recipient", dest="recipient", action="append",
                      help="Recipient to encrypt the site to. May be used multiple times", metavar="NAME")

    (options, args) = parser.parse_args()
    global_config = gpgpass_config.get_config()

    if options.password_store:
        global_config.set('store', options.password_store)

    if global_config.get('store') is None:
        parser.print_help()
        sys.exit(1)


    if len(args) == 0:
        get_password.print_all_sites(global_config.get('store'))
        sys.exit(0)

    if len(args) > 1:
        parser.print_help()
        sys.exit(1)


    global_config.set('site', args[0])

    if options.recipient:
        global_config.set('recipient', options.recipient)
        set_password.add()
    else:
        get_password.get_site()
Example #2
0
def get_site():

    global_config = gpgpass_config.get_config()
    site = global_config.get('site')

    scrambled_site = hashlib.sha256(site).hexdigest()

    encrypted_file = os.path.join(global_config.get('store'), scrambled_site)

    if not os.path.isfile(encrypted_file):
        print "No entry for %s found\n" % site
        sys.exit(1)

    try:
        output = decrypt_file(encrypted_file)
    except gpgme.GpgmeError as e:
        print "Error: %s" % e.strerror
        sys.exit(1)

    for i in output.readlines():
        print i.strip()

    sys.exit(0)
Example #3
0
def add():
    "Add or update a site in the store"

    global_config = gpgpass_config.get_config()

    site = global_config.get('site')
    password_store = global_config.get('store')
    recipients = global_config.get('recipient')

    new_entry = True

    site_data = {
                    'site'      : site,
                    'username'  : None,
                    'password'  : None
                }

    scrambled_site = hashlib.sha256(site).hexdigest()

    encrypted_file = os.path.join(password_store, scrambled_site)

    # If this file exists, let's update it
    if os.path.isfile(encrypted_file):
        new_entry = False

    gpg_ctx = gpgme.Context()

    if new_entry == False:
        fh = open(encrypted_file, 'r')
        output = StringIO.StringIO()
        signatures = gpg_ctx.decrypt_verify(fh, output)

        output.seek(0)
        for i in output.readlines():
            data = i.strip().split(': ')
            site_data[data[0]] = data[1]

        fh.close()

        print("Updating %s" % site)
    else:
        print("Adding %s" % site)

    if site_data['username'] is None:
        print("Username: ", end="")
    else:
        print("Username: [%s] " % site_data['username'], end="")

    username = sys.stdin.readline().strip()
    if username == '':
        username = site_data['username']

    if site_data['password'] is None:
        print("Password: ", end="")
    else:
        print("Password: [return for no change] ", end="")

    # We want a blank password prompt
    password = getpass.getpass("")
    if password == '':
        password = site_data['password']

    site_data['site'] = site
    site_data['username'] = username
    site_data['password'] = password

    plaintext_data = "%s: %s\n%s: %s\n%s: %s" % ('site', site_data['site'],
                                                 'username', site_data['username'],
                                                 'password', site_data['password'])

    keys = []
    for i in recipients:
        keys.append(gpg_ctx.get_key(i))

    plaintext = StringIO.StringIO(plaintext_data)
    ciphertext = StringIO.StringIO()
    gpg_ctx.armor = True
    gpg_ctx.encrypt(keys, gpgme.ENCRYPT_ALWAYS_TRUST, plaintext, ciphertext)

    ciphertext.seek(0)

    fh = open(encrypted_file, 'w')
    fh.writelines(ciphertext.readlines())
    fh.close