OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright (c) 2011 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 """Registers new hosts in chromoting directory. | 6 """Registers new hosts in chromoting directory. |
7 | 7 |
8 It asks for username/password and then writes these settings to config file. | 8 It asks for username/password and then writes these settings to config file. |
9 """ | 9 """ |
10 | 10 |
| 11 import base64 |
11 import getpass | 12 import getpass |
| 13 import hashlib |
| 14 import hmac |
| 15 import json |
12 import os | 16 import os |
13 import urllib | |
14 import urllib2 | |
15 import random | 17 import random |
16 import socket | 18 import socket |
17 import sys | 19 import sys |
| 20 import urllib |
| 21 import urllib2 |
18 | 22 |
19 import gaia_auth | 23 import gaia_auth |
20 import keygen | 24 import keygen |
21 | 25 |
22 def random_uuid(): | 26 def random_uuid(): |
23 return ("%04x%04x-%04x-%04x-%04x-%04x%04x%04x" % | 27 return ("%04x%04x-%04x-%04x-%04x-%04x%04x%04x" % |
24 tuple(map(lambda x: random.randrange(0,65536), range(8)))) | 28 tuple(map(lambda x: random.randrange(0,65536), range(8)))) |
25 | 29 |
26 | 30 |
27 def main(): | 31 def main(): |
(...skipping 12 matching lines...) Expand all Loading... |
40 | 44 |
41 host_id = random_uuid() | 45 host_id = random_uuid() |
42 print "HostId:", host_id | 46 print "HostId:", host_id |
43 host_name = socket.gethostname() | 47 host_name = socket.gethostname() |
44 print "HostName:", host_name | 48 print "HostName:", host_name |
45 | 49 |
46 print "Generating RSA key pair...", | 50 print "Generating RSA key pair...", |
47 (private_key, public_key) = keygen.generateRSAKeyPair() | 51 (private_key, public_key) = keygen.generateRSAKeyPair() |
48 print "Done" | 52 print "Done" |
49 | 53 |
50 params = ('{"data":{' + | 54 pin = getpass.getpass("Host PIN (can be empty): ") |
51 '"hostId": "%(hostId)s",' + | 55 host_secret_hash = "hmac;" + base64.b64encode( |
52 '"hostName": "%(hostName)s",' + | 56 hmac.new(str(host_id), pin, hashlib.sha256).digest()) |
53 '"publicKey": "%(publicKey)s"}}') % \ | 57 |
54 {'hostId': host_id, 'hostName': host_name, | 58 params = { "data": { |
55 'publicKey': public_key} | 59 "hostId": host_id, |
| 60 "hostName": host_name, |
| 61 "publicKey": public_key, |
| 62 } } |
56 headers = {"Authorization": "GoogleLogin auth=" + auth_token, | 63 headers = {"Authorization": "GoogleLogin auth=" + auth_token, |
57 "Content-Type": "application/json" } | 64 "Content-Type": "application/json" } |
58 request = urllib2.Request(url, params, headers) | 65 request = urllib2.Request(url, json.dumps(params), headers) |
59 | 66 |
60 opener = urllib2.OpenerDirector() | 67 opener = urllib2.OpenerDirector() |
61 opener.add_handler(urllib2.HTTPDefaultErrorHandler()) | 68 opener.add_handler(urllib2.HTTPDefaultErrorHandler()) |
62 | 69 |
63 print | 70 print |
64 print "Registering host with directory service..." | 71 print "Registering host with directory service..." |
65 try: | 72 try: |
66 res = urllib2.urlopen(request) | 73 res = urllib2.urlopen(request) |
67 data = res.read() | 74 data = res.read() |
68 except urllib2.HTTPError, err: | 75 except urllib2.HTTPError, err: |
69 print >> sys.stderr, "Directory returned error:", err | 76 print >> sys.stderr, "Directory returned error:", err |
70 print >> sys.stderr, err.fp.read() | 77 print >> sys.stderr, err.fp.read() |
71 return 1 | 78 return 1 |
72 | 79 |
73 print "Done" | 80 print "Done" |
74 | 81 |
75 # Get token that the host will use to athenticate in talk network. | 82 # Get token that the host will use to athenticate in talk network. |
76 authenticator = gaia_auth.GaiaAuthenticator('chromiumsync'); | 83 authenticator = gaia_auth.GaiaAuthenticator('chromiumsync'); |
77 auth_token = authenticator.authenticate(email, password) | 84 auth_token = authenticator.authenticate(email, password) |
78 | 85 |
79 # Write settings file. | 86 # Write settings file. |
80 os.umask(0066) # Set permission mask for created file. | 87 os.umask(0066) # Set permission mask for created file. |
81 settings_file = open(settings_filepath, 'w') | 88 settings_file = open(settings_filepath, 'w') |
82 settings_file.write('{\n'); | 89 config = { |
83 settings_file.write(' "xmpp_login" : "' + email + '",\n') | 90 "xmpp_login" : email, |
84 settings_file.write(' "xmpp_auth_token" : "' + auth_token + '",\n') | 91 "xmpp_auth_token" : auth_token, |
85 settings_file.write(' "host_id" : "' + host_id + '",\n') | 92 "host_id" : host_id, |
86 settings_file.write(' "host_name" : "' + host_name + '",\n') | 93 "host_name" : host_name, |
87 settings_file.write(' "private_key" : "' + private_key + '",\n') | 94 "host_secret_hash" : host_secret_hash, |
88 settings_file.write('}\n') | 95 "private_key" : private_key, |
| 96 } |
| 97 settings_file.write(json.dumps(config, indent=2)) |
89 settings_file.close() | 98 settings_file.close() |
90 | 99 |
91 print 'Configuration saved in', settings_filepath | 100 print 'Configuration saved in', settings_filepath |
92 return 0 | 101 return 0 |
93 | 102 |
94 | 103 |
95 if __name__ == '__main__': | 104 if __name__ == '__main__': |
96 sys.exit(main()) | 105 sys.exit(main()) |
OLD | NEW |