-
Notifications
You must be signed in to change notification settings - Fork 0
/
sshfetchkey.py
executable file
·100 lines (92 loc) · 3.39 KB
/
sshfetchkey.py
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
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Copyright (C) 2011 Michael Dierks (michael dot dierks at gmail dot com)
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License version 2
# as published by the Free Software Foundation
#
# 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., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
#
import sys
import os
import hashlib
import sshdb
import argparse
global myresults
def main():
configPresent = sshdb.configCheck()
if configPresent != 0:
sys.exit("No configfile found, please use \
sshdbconfig.py to create a default config")
# parse config und connect
connectingString = sshdb.parseConfig()
if connectingString[0] == 1:
sys.exit("Please edit your configfile")
if connectingString[0] == 'postgresql':
conn = sshdb.connectpsql(connectingString[1])
if connectingString[0] == 'mysql':
import MySQLdb
conn = MySQLdb.connect(read_default_file="~/sshkeydb.conf")
try:
parser = argparse.ArgumentParser()
parser.add_argument('--role', '-r', dest='role',
help='Role of the user (default: admin)', default='admin')
parser.add_argument('--realname', '-R', dest='realname',
help='Name of the key owner', default='')
args = parser.parse_args()
theRole = args.role
theName = args.realname
except:
sys.exit("Wrong commandline arguments")
try:
keyList = sshdb.genList()
cursor = conn.cursor()
if theName == '':
getByRole = \
"select role, realname, keyfile, keypath, checksum from \
users where role = '%s';" % theRole
cursor.execute(getByRole)
myresults = cursor.fetchall()
else:
getByName = \
"select role, realname, keyfile, keypath, checksum from \
users where realname = '%s';" % theName
cursor.execute(getByName)
myresults = cursor.fetchall()
"""
Dirty hack with the exception ValueError need a better solution
"""
for key in myresults:
getKey = key[2]
""" checking if the key exists in the authorized_keys """
try:
keyList.index(key[2])
print "Key exists in authorized_keys: %s" % key[3]
except ValueError:
keychecksum = hashlib.sha256(getKey).hexdigest()
if key[4] == keychecksum:
print "Checksum ok for %s" % key[3]
else:
print "Checksum is different"
sys.exit("Aborting")
auth = open(os.path.expanduser('~/.ssh/authorized_keys'), 'a')
auth.write(key[2])
auth.close()
except:
print "please check if the file ~/.ssh/authorized_keys \
is present"
except:
print "Database Error"
conn.close()
sys.exit()
if __name__ == '__main__':
main()