-
Notifications
You must be signed in to change notification settings - Fork 6
/
sshKeyDist.py
722 lines (625 loc) · 35.1 KB
/
sshKeyDist.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
import os
import ssh
import wx
import wx.lib.newevent
import re
from StringIO import StringIO
import logging
from threading import *
import threading
import time
import sys
from os.path import expanduser
import subprocess
import traceback
import socket
from utilityFunctions import HelpDialog
import pkgutil
import signal
from logger.Logger import logger
from PassphraseDialog import passphraseDialog
if not sys.platform.startswith('win'):
import pexpect
class KeyDist():
def complete(self):
returnval = self._completed.isSet()
return returnval
class startAgentThread(Thread):
def __init__(self,keydistObject):
Thread.__init__(self)
self.keydistObject = keydistObject
self._stop = Event()
def stop(self):
self._stop.set()
def stopped(self):
return self._stop.isSet()
def run(self):
agentenv = None
self.keydistObject.sshAgentProcess = None
try:
key=self.keydistObject.keyModel.listKey()
logger.debug("KeyDist.startAgentThread keyModel.listKey returned without exception, we assume an agent is running")
except Exception as e:
# If we start the agent, we will stop the agent.
logger.debug("KeyDist.startAgentThread keyModel.listKey returned an error. Presumably ssh-add was unable to contact the agent, starting an agent")
self.keydistObject.stopAgentOnExit.set()
logger.debug(traceback.format_exc())
try:
self.keydistObject.keyModel.startAgent()
except Exception as e:
self.keydistObject.cancel(message="I tried to start an ssh agent, but failed with the error message:\n\n%s" % str(e))
return
newevent = KeyDist.sshKeyDistEvent(KeyDist.EVT_KEYDIST_GETPUBKEY,self.keydistObject)
if (not self.stopped()):
wx.PostEvent(self.keydistObject.notifywindow.GetEventHandler(),newevent)
class loadkeyThread(Thread):
def __init__(self,keydistObject):
Thread.__init__(self)
self.keydistObject = keydistObject
self._stop = Event()
def stop(self):
self._stop.set()
def stopped(self):
return self._stop.isSet()
def run(self):
logger.debug("loadkeyThread: started")
try:
logger.debug("loadkeyThread: Trying to open the key file")
with open(self.keydistObject.keyModel.sshpaths.sshKeyPath,'r'): pass
event = KeyDist.sshKeyDistEvent(KeyDist.EVT_KEYDIST_LOADKEY,self.keydistObject)
except Exception as e:
logger.error("loadkeyThread: Failed to open the key file %s" % str(e))
self.keydistObject.cancel("Failed to open the key file %s" % str(e))
return
if (not self.stopped()):
logger.debug("loadkeyThread: generating LOADKEY event from loadkeyThread")
wx.PostEvent(self.keydistObject.notifywindow.GetEventHandler(),event)
class genkeyThread(Thread):
def __init__(self,keydistObject):
Thread.__init__(self)
self.keydistObject = keydistObject
self._stop = Event()
def stop(self):
self._stop.set()
def stopped(self):
return self._stop.isSet()
def run(self):
logger.debug("genkeyThread: started")
self.nextEvent=None
def success():
self.nextEvent = KeyDist.sshKeyDistEvent(KeyDist.EVT_KEYDIST_LOADKEY,self.keydistObject)
def failure():
self.keydistObject.cancel("Unable to generate a new key pair")
self.keydistObject.keyModel.generateNewKey(self.keydistObject.password,success,failure,failure)
self.keydistObject.keyCreated.set()
if (not self.stopped() and self.nextEvent != None):
logger.debug("genkeyThread: generating LOADKEY event from genkeyThread")
wx.PostEvent(self.keydistObject.notifywindow.GetEventHandler(),self.nextEvent)
class getPubKeyThread(Thread):
def __init__(self,keydistObject):
Thread.__init__(self)
self.keydistObject = keydistObject
self._stop = Event()
def stop(self):
self._stop.set()
def stopped(self):
return self._stop.isSet()
def run(self):
threadid = threading.currentThread().ident
logger.debug("getPubKeyThread %i: started"%threadid)
try:
key = self.keydistObject.keyModel.listKey()
if (key!=None):
self.keydistObject.keyloaded.set()
logger.debug("getPubKeyThread %i: key loaded"%threadid)
logger.debug("getPubKeyThread %i: found a key, creating TESTAUTH event"%threadid)
newevent = KeyDist.sshKeyDistEvent(KeyDist.EVT_KEYDIST_TESTAUTH,self.keydistObject)
else:
logger.debug("getPubKeyThread %i: did not find a key, creating LOADKEY event"%threadid)
newevent = KeyDist.sshKeyDistEvent(KeyDist.EVT_KEYDIST_LOADKEY,self.keydistObject)
except:
logger.debug("getPubKeyThread %i: Unable to contact agent"%threadid)
newevent = KeyDist.sshKeyDistEvent(KeyDist.EVT_KEYDIST_NEEDAGENT,self.keydistObject)
if (not self.stopped()):
logger.debug("getPubKeyThread %i: is posting the next event"%threadid)
wx.PostEvent(self.keydistObject.notifywindow.GetEventHandler(),newevent)
logger.debug("getPubKeyThread %i: stopped"%threadid)
class scanHostKeysThread(Thread):
def __init__(self,keydistObject):
Thread.__init__(self)
self.keydistObject = keydistObject
self.ssh_keygen_cmd = '{sshkeygen} -F {host} -f {known_hosts_file}'.format(sshkeygen=self.keydistObject.keyModel.sshpaths.sshKeyGenBinary,host=self.keydistObject.host,known_hosts_file=self.keydistObject.keyModel.sshpaths.sshKnownHosts)
self.ssh_keyscan_cmd = '{sshscan} -H {host}'.format(sshscan=self.keydistObject.keyModel.sshpaths.sshKeyScanBinary,host=self.keydistObject.host)
self._stop = Event()
def stop(self):
self._stop.set()
def stopped(self):
return self._stop.isSet()
def getKnownHostKeys(self):
keygen = subprocess.Popen(self.ssh_keygen_cmd,stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=True,universal_newlines=True,startupinfo=self.keydistObject.startupinfo,creationflags=self.keydistObject.creationflags)
stdout,stderr = keygen.communicate()
keygen.wait()
hostkeys=[]
for line in stdout.split('\n'):
if (not (line.find('#')==0 or line == '')):
hostkeys.append(line)
return hostkeys
def appendKey(self,key):
with open(self.keydistObject.keyModel.sshpaths.sshKnownHosts,'a+') as known_hosts:
known_hosts.write(key)
known_hosts.write('\n')
def scanHost(self):
scan = subprocess.Popen(self.ssh_keyscan_cmd,stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=True,universal_newlines=True,startupinfo=self.keydistObject.startupinfo,creationflags=self.keydistObject.creationflags)
stdout,stderr = scan.communicate()
scan.wait()
hostkeys=[]
for line in stdout.split('\n'):
if (not (line.find('#')==0 or line == '')):
hostkeys.append(line)
return hostkeys
def run(self):
knownKeys = self.getKnownHostKeys()
if (len(knownKeys)==0):
hostKeys = self.scanHost()
for key in hostKeys:
self.appendKey(key)
newevent = KeyDist.sshKeyDistEvent(KeyDist.EVT_KEYDIST_NEEDAGENT,self.keydistObject)
if (not self.stopped()):
wx.PostEvent(self.keydistObject.notifywindow.GetEventHandler(),newevent)
class testAuthThread(Thread):
def __init__(self,keydistObject):
Thread.__init__(self)
self.keydistObject = keydistObject
self._stop = Event()
def stop(self):
self._stop.set()
def stopped(self):
return self._stop.isSet()
def run(self):
# I have a problem where I have multiple identity files in my ~/.ssh, and I want to use only identities loaded into the agent
# since openssh does not seem to have an option to use only an agent we have a workaround,
# by passing the -o IdentityFile option a path that does not exist, openssh can't use any other identities, and can only use the agent.
# This is a little "racy" in that a tempfile with the same path could conceivably be created between the unlink and openssh attempting to use it
# but since the pub key is extracted from the agent not the identity file I can't see anyway an attacker could use this to trick a user into uploading the attackers key.
threadid = threading.currentThread().ident
logger.debug("testAuthThread %i: started"%threadid)
import tempfile
fd=tempfile.NamedTemporaryFile(delete=True)
path=fd.name
fd.close()
ssh_cmd = '{sshbinary} -o ConnectTimeout=10 -o IdentityFile={nonexistantpath} -o PasswordAuthentication=no -o ChallengeResponseAuthentication=no -o KbdInteractiveAuthentication=no -o PubkeyAuthentication=yes -o StrictHostKeyChecking=no -l {login} {host} echo "success_testauth"'.format(sshbinary=self.keydistObject.keyModel.sshpaths.sshBinary,
login=self.keydistObject.username,
host=self.keydistObject.host,
nonexistantpath=path)
logger.debug('testAuthThread: attempting: ' + ssh_cmd)
ssh = subprocess.Popen(ssh_cmd,stdout=subprocess.PIPE,stderr=subprocess.STDOUT,shell=True,universal_newlines=True, startupinfo=self.keydistObject.startupinfo, creationflags=self.keydistObject.creationflags)
stdout, stderr = ssh.communicate()
ssh.wait()
logger.debug("testAuthThread %i: stdout of ssh command: "%threadid + str(stdout))
logger.debug("testAuthThread %i: stderr of ssh command: "%threadid + str(stderr))
if 'Could not resolve hostname' in stdout:
logger.debug('Network error.')
newevent = KeyDist.sshKeyDistEvent(KeyDist.EVT_KEYDIST_NETWORK_ERROR,self.keydistObject)
elif 'success_testauth' in stdout:
logger.debug("testAuthThread %i: got success_testauth in stdout :)"%threadid)
self.keydistObject.authentication_success = True
newevent = KeyDist.sshKeyDistEvent(KeyDist.EVT_KEYDIST_AUTHSUCCESS,self.keydistObject)
elif 'Agent admitted' in stdout:
logger.debug("testAuthThread %i: the ssh agent has an error. Try rebooting the computer")
self.keydistObject.cancel("Sorry, there is a problem with the SSH agent.\nThis sort of thing usually occurs if you delete your key and create a new one.\nThe easiest solution is to reboot your computer and try again.")
return
else:
logger.debug("testAuthThread %i: did not see success_testauth in stdout, posting EVT_KEYDIST_AUTHFAIL event"%threadid)
newevent = KeyDist.sshKeyDistEvent(KeyDist.EVT_KEYDIST_AUTHFAIL,self.keydistObject)
if (not self.stopped()):
logger.debug("testAuthThread %i: self.stopped() == False, so posting event: "%threadid + str(newevent))
wx.PostEvent(self.keydistObject.notifywindow.GetEventHandler(),newevent)
logger.debug("testAuthThread %i: stopped"%threadid)
class loadKeyThread(Thread):
def __init__(self,keydistObject):
Thread.__init__(self)
self.keydistObject = keydistObject
self._stop = Event()
def stop(self):
self._stop.set()
def stopped(self):
return self._stop.isSet()
def run(self):
self.nextEvent=None
threadid=threading.currentThread().ident
threadname=threading.currentThread().name
km =self.keydistObject.keyModel
if (self.keydistObject.password!=None):
password=self.keydistObject.password
newevent1 = KeyDist.sshKeyDistEvent(KeyDist.EVT_KEYDIST_KEY_WRONGPASS, self.keydistObject)
else:
newevent1 = KeyDist.sshKeyDistEvent(KeyDist.EVT_KEYDIST_KEY_LOCKED, self.keydistObject)
password=""
def incorrectCallback():
self.nextEvent = newevent1
def loadedCallback():
self.nextEvent =KeyDist.sshKeyDistEvent(KeyDist.EVT_KEYDIST_GETPUBKEY, self.keydistObject)
def notFoundCallback():
self.nextEvent=KeyDist.sshKeyDistEvent(KeyDist.EVT_KEYDIST_NEWPASS_REQ,self.keydistObject)
def failedToConnectToAgentCallback():
self.nextEvent=KeyDist.sshKeyDistEvent(KeyDist.EVT_KEYDIST_NEEDAGENT,self.keydistObject)
logger.debug("sshKeyDist.loadKeyThread.run: KeyModel information temporary: %s path: %s exists: %s"%(km.isTemporaryKey(),km.getPrivateKeyFilePath(),km.privateKeyExists()))
km.addKeyToAgent(password,loadedCallback,incorrectCallback,notFoundCallback,failedToConnectToAgentCallback)
if (not self.stopped() and self.nextEvent != None):
wx.PostEvent(self.keydistObject.notifywindow.GetEventHandler(),self.nextEvent)
class CopyIDThread(Thread):
def __init__(self,keydist,obj):
Thread.__init__(self)
self.keydistObject = keydist
self.obj=obj
self._stop = Event()
def stop(self):
self._stop.set()
def stopped(self):
return self._stop.isSet()
def run(self):
try:
pubKeyPath=self.keydistObject.keyModel.getPrivateKeyFilePath()+".pub"
with open(pubKeyPath,'r') as f:
pubkey=f.read()
self.obj.copyID()
logger.debug("KeyDist.CopyIDThread: copyID returned without error")
event = KeyDist.sshKeyDistEvent(KeyDist.EVT_KEYDIST_TESTAUTH,self.keydistObject)
# If the object represented an attempt at logging onto AAF, then we will try to get the IdP and save it for next time.
# If its not an AAF login, we will catch the exception and pass
try:
idp=self.obj.getIdP()
self.keydistObject.updateDict['idp']=idp
except Exception as e:
pass
# This try catch means that if, in the course of authenticing the user, the authentication mechanism was able to tell us the username
# That username will be updated. Example. Authenticing via AAF, I know I'm at Monash, and my Monash username is chines.
# Monash will tell CVL what my email address is. CVL can look up my CVL username based on my email address.
try:
newusername=self.obj.getUsername()
self.keydistObject.updateDict['username']=newusername
except Exception as e:
pass
except Exception as e:
logger.debug('CopyIDThread: threw exception : ' + str(e))
self.keydistObject.cancel(message=str(e))
return
if (not self.stopped()):
wx.PostEvent(self.keydistObject.notifywindow.GetEventHandler(), event)
self.keydistObject.keyModel.copiedID.set()
self.keydistObject.keycopied.set()
self.keydistObject.__dict__.update(self.keydistObject.updateDict)
class sshKeyDistEvent(wx.PyCommandEvent):
def __init__(self,id,keydist,arg=None):
wx.PyCommandEvent.__init__(self,KeyDist.myEVT_CUSTOM_SSHKEYDIST,id)
self.keydist = keydist
self.arg = arg
self.threadid = threading.currentThread().ident
self.threadname = threading.currentThread().name
def newkey(event):
usingOneTimePassphrase = False
if (event.GetId() == KeyDist.EVT_KEYDIST_NEWPASS_REQ):
logger.debug("received NEWPASS_REQ event")
if event.keydist.keyModel.isTemporaryKey():
usingOneTimePassphrase = True
import string
import random
oneTimePassphrase=''.join(random.choice(string.ascii_uppercase + string.ascii_lowercase + string.digits) for x in range(10))
logger.debug("sshKeyDistEvent.newkey: oneTimePassphrase: " + oneTimePassphrase)
event.keydist.password = oneTimePassphrase
else:
wx.CallAfter(event.keydist.getPassphrase,event.arg)
if (event.GetId() == KeyDist.EVT_KEYDIST_NEWPASS_COMPLETE or usingOneTimePassphrase):
if event.GetId() == KeyDist.EVT_KEYDIST_NEWPASS_COMPLETE:
logger.debug("received NEWPASS_COMPLETE event")
if usingOneTimePassphrase:
logger.debug("Using one-time passphrase.")
t = KeyDist.genkeyThread(event.keydist)
t.setDaemon(True)
t.start()
event.keydist.threads.append(t)
event.Skip()
def copyid(event):
import cvlsshutils.cvl_shib_auth
import cvlsshutils.password_copyid
if (event.GetId() == KeyDist.EVT_KEYDIST_COPYID):
pubKeyPath=event.keydist.keyModel.getPrivateKeyFilePath()+".pub"
with open(pubKeyPath,'r') as f:
pubkey=f.read()
# Here we make the decision as to how to copy the public key to the users authorized keys file. This of this as a factory pattern, although I'm sure there are neater ways to implement it.
if event.keydist.keyModel.useAAF():
obj=cvlsshutils.cvl_shib_auth.shibbolethDance(pubkey=pubkey,parent=event.keydist.parentWindow,displayStrings=event.keydist.displayStrings,**event.keydist.jobParams)
else:
obj=cvlsshutils.password_copyid.genericCopyID(pubkey=pubkey,parent=event.keydist.parentWindow,host=event.keydist.host,username=event.keydist.username,displayStrings=event.keydist.displayStrings)
logger.debug("received COPYID event")
t = KeyDist.CopyIDThread(event.keydist,obj=obj)
t.setDaemon(True)
t.start()
event.keydist.threads.append(t)
else:
event.Skip()
def scanhostkeys(event):
if (event.GetId() == KeyDist.EVT_KEYDIST_SCANHOSTKEYS):
logger.debug("received SCANHOSTKEYS event")
t = KeyDist.scanHostKeysThread(event.keydist)
t.setDaemon(True)
t.start()
event.keydist.threads.append(t)
event.Skip()
def shutdownEvent(event):
if (event.GetId() == KeyDist.EVT_KEYDIST_SHUTDOWN):
logger.debug("received EVT_KEYDIST_SHUTDOWN event")
nextevent = KeyDist.sshKeyDistEvent(KeyDist.EVT_KEYDIST_COMPLETE,event.keydist)
try:
nextevent.arg=event.arg
except:
pass
event.keydist.shutdownThread=threading.Thread(target=event.keydist.shutdownReal,args=[nextevent])
event.keydist.shutdownThread.start()
else:
event.Skip()
def completeEvent(event):
if (event.GetId() == KeyDist.EVT_KEYDIST_COMPLETE):
if (event.keydist.canceled()):
if (event.keydist.callback_fail != None):
logger.debug("sshKeyDist.completeEvent: calling-back to indicate completion")
if event.arg!=None:
event.keydist.callback_fail(event.arg)
else:
event.keydist.callback_fail()
event.keydist._completed.set()
def success(event):
if (event.GetId() == KeyDist.EVT_KEYDIST_AUTHSUCCESS):
logger.debug("received AUTHSUCCESS event")
event.keydist._completed.set()
if (event.keydist.callback_success != None):
event.keydist.callback_success()
event.Skip()
def needagent(event):
if (event.GetId() == KeyDist.EVT_KEYDIST_NEEDAGENT and not event.keydist.canceled()):
logger.debug("received NEEDAGENT event")
t = KeyDist.startAgentThread(event.keydist)
t.setDaemon(True)
t.start()
event.keydist.threads.append(t)
else:
event.Skip()
def listpubkeys(event):
if (event.GetId() == KeyDist.EVT_KEYDIST_GETPUBKEY and not event.keydist.canceled()):
t = KeyDist.getPubKeyThread(event.keydist)
t.setDaemon(True)
t.start()
logger.debug("received GETPUBKEY event from thread %i %s, starting thread %i %s in response"%(event.threadid,event.threadname,t.ident,t.name))
event.keydist.threads.append(t)
else:
event.Skip()
def testauth(event):
if (event.GetId() == KeyDist.EVT_KEYDIST_TESTAUTH):
t = KeyDist.testAuthThread(event.keydist)
t.setDaemon(True)
t.start()
logger.debug("received TESTAUTH event from thread %i %s, starting thread %i %s in response"%(event.threadid,event.threadname,t.ident,t.name))
event.keydist.threads.append(t)
else:
event.Skip()
def networkError(event):
if (event.GetId() == KeyDist.EVT_KEYDIST_NETWORK_ERROR):
event.keydist.cancel(message='Network error, could not contact login host.')
return
else:
event.Skip()
def keylocked(event):
if (event.GetId() == KeyDist.EVT_KEYDIST_KEY_LOCKED):
logger.debug("received KEY_LOCKED event")
wx.CallAfter(event.keydist.GetKeyPassphrase)
if (event.GetId() == KeyDist.EVT_KEYDIST_KEY_WRONGPASS):
logger.debug("received KEY_WRONGPASS event")
wx.CallAfter(event.keydist.GetKeyPassphrase,incorrect=True)
event.Skip()
def loadkey(event):
if (event.GetId() == KeyDist.EVT_KEYDIST_LOADKEY and not event.keydist.canceled()):
t = KeyDist.loadKeyThread(event.keydist)
t.setDaemon(True)
t.start()
logger.debug("received LOADKEY event from thread %i %s, starting thread %i %s in response"%(event.threadid,event.threadname,t.ident,t.name))
event.keydist.threads.append(t)
else:
event.Skip()
def authfail(event):
if (event.GetId() == KeyDist.EVT_KEYDIST_AUTHFAIL and not event.keydist.canceled()):
if(not event.keydist.keyloaded.isSet()):
newevent = KeyDist.sshKeyDistEvent(KeyDist.EVT_KEYDIST_LOADKEY,event.keydist)
wx.PostEvent(event.keydist.notifywindow.GetEventHandler(),newevent)
else:
# If the key is loaded into the ssh agent, then authentication failed because the public key isn't on the server.
# *****TODO*****
# Actually this might not be strictly true. GNOME Keychain (and possibly others) will report a key loaded even if its still locked
# we probably need a button that says "I can't remember my old key's passphrase, please generate a new keypair"
if (event.keydist.keycopied.isSet()):
newevent = KeyDist.sshKeyDistEvent(KeyDist.EVT_KEYDIST_TESTAUTH,event.keydist)
logger.debug("received AUTHFAIL event from thread %i %s posting TESTAUTH event in response"%(event.threadid,event.threadname))
wx.PostEvent(event.keydist.notifywindow.GetEventHandler(),newevent)
else:
newevent = KeyDist.sshKeyDistEvent(KeyDist.EVT_KEYDIST_COPYID,event.keydist)
logger.debug("received AUTHFAIL event from thread %i %s posting COPYID event in response"%(event.threadid,event.threadname))
wx.PostEvent(event.keydist.notifywindow.GetEventHandler(),newevent)
else:
event.Skip()
def startevent(event):
if (event.GetId() == KeyDist.EVT_KEYDIST_START):
logger.debug("received KEYDIST_START event")
newevent = KeyDist.sshKeyDistEvent(KeyDist.EVT_KEYDIST_SCANHOSTKEYS,event.keydist)
wx.PostEvent(event.keydist.notifywindow.GetEventHandler(),newevent)
else:
event.Skip()
myEVT_CUSTOM_SSHKEYDIST=None
EVT_CUSTOM_SSHKEYDIST=None
def __init__(self,parentWindow,progressDialog,username,host,configName,notifywindow,keyModel,displayStrings=None,removeKeyOnExit=False,startupinfo=None,creationflags=0,jobParams={}):
logger.debug("KeyDist.__init__")
KeyDist.myEVT_CUSTOM_SSHKEYDIST=wx.NewEventType()
KeyDist.EVT_CUSTOM_SSHKEYDIST=wx.PyEventBinder(self.myEVT_CUSTOM_SSHKEYDIST,1)
KeyDist.EVT_KEYDIST_START = wx.NewId()
KeyDist.EVT_KEYDIST_SHUTDOWN = wx.NewId()
KeyDist.EVT_KEYDIST_SUCCESS = wx.NewId()
KeyDist.EVT_KEYDIST_NEEDAGENT = wx.NewId()
KeyDist.EVT_KEYDIST_NEEDKEYS = wx.NewId()
KeyDist.EVT_KEYDIST_GETPUBKEY = wx.NewId()
KeyDist.EVT_KEYDIST_TESTAUTH = wx.NewId()
KeyDist.EVT_KEYDIST_AUTHSUCCESS = wx.NewId()
KeyDist.EVT_KEYDIST_AUTHFAIL = wx.NewId()
KeyDist.EVT_KEYDIST_NEWPASS_REQ = wx.NewId()
KeyDist.EVT_KEYDIST_NEWPASS_RPT = wx.NewId()
KeyDist.EVT_KEYDIST_NEWPASS_COMPLETE = wx.NewId()
KeyDist.EVT_KEYDIST_COPYID = wx.NewId()
KeyDist.EVT_KEYDIST_KEY_LOCKED = wx.NewId()
KeyDist.EVT_KEYDIST_KEY_WRONGPASS = wx.NewId()
KeyDist.EVT_KEYDIST_SCANHOSTKEYS = wx.NewId()
KeyDist.EVT_KEYDIST_LOADKEY = wx.NewId()
KeyDist.EVT_KEYDIST_NETWORK_ERROR = wx.NewId()
KeyDist.EVT_KEYDIST_COMPLETE = wx.NewId()
notifywindow.Bind(self.EVT_CUSTOM_SSHKEYDIST, KeyDist.sshKeyDistEvent.success)
notifywindow.Bind(self.EVT_CUSTOM_SSHKEYDIST, KeyDist.sshKeyDistEvent.needagent)
notifywindow.Bind(self.EVT_CUSTOM_SSHKEYDIST, KeyDist.sshKeyDistEvent.listpubkeys)
notifywindow.Bind(self.EVT_CUSTOM_SSHKEYDIST, KeyDist.sshKeyDistEvent.testauth)
notifywindow.Bind(self.EVT_CUSTOM_SSHKEYDIST, KeyDist.sshKeyDistEvent.authfail)
notifywindow.Bind(self.EVT_CUSTOM_SSHKEYDIST, KeyDist.sshKeyDistEvent.startevent)
notifywindow.Bind(self.EVT_CUSTOM_SSHKEYDIST, KeyDist.sshKeyDistEvent.newkey)
notifywindow.Bind(self.EVT_CUSTOM_SSHKEYDIST, KeyDist.sshKeyDistEvent.copyid)
notifywindow.Bind(self.EVT_CUSTOM_SSHKEYDIST, KeyDist.sshKeyDistEvent.keylocked)
notifywindow.Bind(self.EVT_CUSTOM_SSHKEYDIST, KeyDist.sshKeyDistEvent.scanhostkeys)
notifywindow.Bind(self.EVT_CUSTOM_SSHKEYDIST, KeyDist.sshKeyDistEvent.loadkey)
notifywindow.Bind(self.EVT_CUSTOM_SSHKEYDIST, KeyDist.sshKeyDistEvent.networkError)
notifywindow.Bind(self.EVT_CUSTOM_SSHKEYDIST, KeyDist.sshKeyDistEvent.completeEvent)
notifywindow.Bind(self.EVT_CUSTOM_SSHKEYDIST, KeyDist.sshKeyDistEvent.shutdownEvent)
self.updateDict={}
self._completed=Event()
self.parentWindow = parentWindow
self.progressDialog = progressDialog
self.username = username
self.host = host
self.configName = configName
self.displayStrings=displayStrings
self.notifywindow = notifywindow
self.sshKeyPath = ""
self.threads=[]
self.pubkeyfp = None
self.keyloaded=Event()
self.password = None
self.pubkeylock = Lock()
self.keycopied=Event()
self.authentication_success = False
self.callback_success=None
self.callback_fail=None
self.callback_error = None
self._canceled=Event()
self.removeKeyOnExit=Event()
self.keyCreated=Event()
if removeKeyOnExit:
self.removeKeyOnExit.set()
self.stopAgentOnExit=Event()
self.keyModel = keyModel
self.startupinfo = startupinfo
self.creationflags = creationflags
self.shuttingDown=Event()
self.jobParams=jobParams
def GetKeyPassphrase(self,incorrect=False):
if (incorrect):
ppd = passphraseDialog(self.parentWindow,self.progressDialog,wx.ID_ANY,'Unlock Key',self.displayStrings.passphrasePromptIncorrect,"OK","Cancel")
else:
ppd = passphraseDialog(self.parentWindow,self.progressDialog,wx.ID_ANY,'Unlock Key',self.displayStrings.passphrasePrompt,"OK","Cancel")
(canceled,passphrase) = ppd.getPassword()
if (canceled):
self.cancel("Sorry, I can't continue without the passphrase for that key. If you've forgotten the passphrase, you could remove the key and generate a new one. The key is probably located in\n\n" + self.keyModel.getPrivateKeyFilePath() + "*")
return
else:
self.password = passphrase
event = KeyDist.sshKeyDistEvent(KeyDist.EVT_KEYDIST_LOADKEY,self)
wx.PostEvent(self.notifywindow.GetEventHandler(),event)
def getLoginPassword(self,incorrect=False):
if (not incorrect):
ppd = passphraseDialog(self.parentWindow,self.progressDialog,wx.ID_ANY,'Login Password',self.displayStrings.passwdPrompt.format(**self.__dict__),"OK","Cancel")
else:
ppd = passphraseDialog(self.parentWindow,self.progressDialog,wx.ID_ANY,'Login Password',self.displayStrings.passwdPromptIncorrect.format(**self.__dict__),"OK","Cancel")
(canceled,password) = ppd.getPassword()
if canceled:
self.cancel()
return
self.password = password
event = KeyDist.sshKeyDistEvent(KeyDist.EVT_KEYDIST_COPYID,self)
wx.PostEvent(self.notifywindow.GetEventHandler(),event)
def getPassphrase(self,reason=None):
from CreateNewKeyDialog import CreateNewKeyDialog
createNewKeyDialog = CreateNewKeyDialog(self.parentWindow, self.progressDialog, wx.ID_ANY, 'MASSIVE/CVL Launcher Private Key', self.keyModel.getPrivateKeyFilePath(),self.displayStrings, displayMessageBoxReportingSuccess=False)
try:
wx.EndBusyCursor()
stoppedBusyCursor = True
except:
stoppedBusyCursor = False
canceled = createNewKeyDialog.ShowModal()==wx.ID_CANCEL
if stoppedBusyCursor:
wx.BeginBusyCursor()
if (not canceled):
logger.debug("User didn't cancel from CreateNewKeyDialog.")
self.password=createNewKeyDialog.getPassphrase()
event = KeyDist.sshKeyDistEvent(KeyDist.EVT_KEYDIST_NEWPASS_COMPLETE,self)
wx.PostEvent(self.notifywindow.GetEventHandler(),event)
else:
logger.debug("KeyDist.getPassphrase: User canceled from CreateNewKeyDialog.")
self.cancel()
def distributeKey(self,callback_success=None, callback_fail=None):
self.callback_fail = callback_fail
self.callback_success = callback_success
logger.debug("KeyDist.distributeKey: posting EVT_KEYDIST_START")
event = KeyDist.sshKeyDistEvent(self.EVT_KEYDIST_START, self)
wx.PostEvent(self.notifywindow.GetEventHandler(), event)
def canceled(self):
return self._canceled.isSet()
def cancel(self,message=""):
if (not self.canceled()):
self._canceled.set()
newevent = KeyDist.sshKeyDistEvent(KeyDist.EVT_KEYDIST_SHUTDOWN, self)
newevent.arg = message
logger.debug('sshKeyDist.cancel: setting canceled, sending EVT_KEYDIST_SHUTDOWN event.')
wx.PostEvent(self.notifywindow.GetEventHandler(), newevent)
def deleteRemoveShutdown(self):
if self.removeKeyOnExit.isSet():
logger.debug("sshKeyDist.deleteRemoveShutdown: self.removeKeyOnExit.isSet() is True.")
if self.keyCreated.isSet():
logger.debug("sshKeyDist.deleteRemoveShutdown: self.keyCreated.isSet() is True.")
logger.debug("sshKeyDist.deleteRemoveShutdown: deleting remote key.")
self.keyModel.deleteRemoteKey(host=self.host,username=self.username)
logger.debug("sshKeyDist.deleteRemoveShutdown: deleting temporary key and removing key from agent.")
self.keyModel.deleteKey()
#logger.debug("sshKeyDist.deleteRemoveShutdown: removing key from agent.")
#self.keyModel.removeKeyFromAgent()
else:
logger.debug("sshKeyDist.deleteRemoveShutdown: self.keyCreated.isSet() is False.")
else:
logger.debug("sshKeyDist.deleteRemoveShutdown: self.removeKeyOnExit.isSet() is False.")
if self.stopAgentOnExit.isSet():
logger.debug("sshKeyDist.deleteRemoveShutdown: self.stopAgentOnExit.isSet() is True.")
self.keyModel.stopAgent()
else:
logger.debug("sshKeyDist.deleteRemoveShutdown: self.stopAgentOnExit.isSet() is False.")
def shutdownReal(self,nextevent=None):
if self.shuttingDown.isSet():
return
self.shuttingDown.set()
logger.debug("sshKeyDist.shutdownReal: calling stop and join on all threads")
for t in self.threads:
try:
t.stop()
t.join()
except:
pass
self.deleteRemoveShutdown()
#t=threading.Thread(target=self.deleteRemoveShutdown)
#t.start()
#t.join()
if nextevent!=None:
wx.PostEvent(self.notifywindow.GetEventHandler(), nextevent)
def shutdown(self):
if (not self.canceled()):
newevent = KeyDist.sshKeyDistEvent(KeyDist.EVT_KEYDIST_SHUTDOWN, self)
logger.debug('Sending EVT_KEYDIST_SHUTDOWN event.')
wx.PostEvent(self.notifywindow.GetEventHandler(), newevent)