-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathteampass-changer
More file actions
executable file
·116 lines (101 loc) · 5.29 KB
/
Copy pathteampass-changer
File metadata and controls
executable file
·116 lines (101 loc) · 5.29 KB
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
#!/usr/bin/python3
#
# Title : teampass-changer
# Date : Wed Jun 6 11:20:44 CEST 2018
# Author : Jose Andres Arias Velichko <a.arias@.paislinux.es>
# Version : See bellow "_version_" line
# Copyright : 2018 Jose Andres Arias Velichko
# License : GNU GPL-2.0
# Description : Uses different plugins to change the current password to a new one
# Usage : teampass-changer [-h|--help]
#
# * This software 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.
#
_version_ = '0.11.dev1'
# Global Libraries
import os
import sys
import pprint
import argparse
# My libraries: First we need to specify where to look
my_dir_path = os.path.dirname(os.path.realpath(__file__))
sys.path.append(my_dir_path + '/lib')
# And now we may import our libraries
import teampass
import pssh
import tpplugins
# Accepted Password Size
def password_size(string):
value = int(string)
if ( (value < 6) or (value > 50) ):
msg = "%r must be in range 6 - 50" % string
raise argparse.ArgumentTypeError(msg)
return [string]
# Parse command line args
parser = argparse.ArgumentParser(description='Allows via plugins to change passwords from teampass.')
parser.add_argument('--url', '-u', nargs=1, action='store', type=str, dest='url', metavar='teampas_url',
default=['https://fuen-pass-1-dev.ftpcomputers.info/teampass'], help='Teampass URL to use')
parser.add_argument('--password-size', '-s', nargs=1, action='store', type=password_size, dest='psize',
metavar='size', default=['12'], help='an integer from 6 to 50')
parser.add_argument('--password-insecure', '-i', action='store_const', const='0', default='1', dest='psecure',
help='Use insecure password (default=yes)')
parser.add_argument('--password--no-capitals', '-c', action='store_const', const='0', default='1', dest='pcapitalize',
help='If should contain capital letters (default=yes)')
parser.add_argument('--password--no-ambiguous', '-a', action='store_const', const='0', default='1', dest='pambiguous',
help='if password can contain ambiguous letters (default=yes)')
parser.add_argument('--key', '-k', required=True, type=str, nargs=1, action='store', dest='key', metavar='api_key', help='Teampass API key to use')
parser.add_argument('--folders', '-f', required=True, type=str, nargs=1, action='store', dest='folders', metavar='api_key', help='A semi-collon separated list of folders')
parser.add_argument('--version', '-v', action='version', version='%(prog)s ' + _version_)
args = parser.parse_args()
# Password rules
password_rules = ':'.join([args.psize[0],args.psecure,args.pcapitalize,args.pambiguous])
# Initialize our TP connection
tp = teampass.teampass(url=args.url[0],key=args.key[0])
# Get a list of entries from specified folders
try:
hosts_dict = tp.get_folders(args.folders[0])
except teampass.BadTeampassResponse:
print ("Unable to connect to Teampass!")
sys.exit(2)
for host in hosts_dict:
print ("{},{},{},".format(host[1],host[0],hosts_dict[host]["port"]), end='')
main_host_data = {}
additional_host_data = {}
main_host_data["host"] = host[0]
main_host_data["user"] = host[1]
main_host_data["newpw"] = tp.get_new_pw(password_rules=password_rules)
main_host_data.update(hosts_dict[host])
if 'Additional_account' in hosts_dict[host]:
if (host[0],hosts_dict[host]['Additional_account']) in hosts_dict:
additional_host_data['host'] = host[0]
additional_host_data['user'] = hosts_dict[host]['Additional_account']
additional_host_data.update(hosts_dict[(host[0],hosts_dict[host]['Additional_account'])])
else:
sys.stderr.write('WARNING: Unable to process additional account {} for {}\n'.format(hosts_dict[host]['Additional_account'],host[0]))
continue
if 'Plugin' not in hosts_dict[host]:
hosts_dict[host]['Plugin'] = 'ssh'
hosts_dict[host]['Plugin_args'] = ''
# Change password with plugin
if tpplugins.plugins[hosts_dict[host]['Plugin']](main_host_data = main_host_data, additional_host_data = additional_host_data):
# Password changed successfully, update TP
if tp.update_pw(main_host_data) :
# Keep our hosts dict up to date
hosts_dict[host]['pw'] = main_host_data["newpw"]
else:
# If update in teampass failed, revert back
temp_pw = main_host_data["newpw"]
main_host_data["newpw"] = main_host_data["pw"]
main_host_data["pw"] = temp_pw
if tpplugins.plugins[hosts_dict[host]['Plugin']](main_host_data = main_host_data, additional_host_data = additional_host_data) :
sys.stderr.write('Warning: Unable to revert password change on {}@{}:\n\tUpdate reverted'.format(host[1],host[0]))
else:
sys.stderr.write('ERROR: Unable to revert password change on {}@{}:\n\tOld Pw: {}\n\tNew Pw: {}\n'.format(host[1],host[0],main_host_data["pw"],main_host_data["newpw"]))
sys.stderr.write('i\tPlease update teampass manually\n')
# Keep our hosts dict up to date
hosts_dict[host]['pw'] = main_host_data["newpw"]
# Keep our data clean
del main_host_data
del additional_host_data