| 1 | # Copyright (C) 2007, 2008 One Laptop Per Child |
|---|
| 2 | # |
|---|
| 3 | # This program is free software; you can redistribute it and/or modify |
|---|
| 4 | # it under the terms of the GNU General Public License as published by |
|---|
| 5 | # the Free Software Foundation; either version 2 of the License, or |
|---|
| 6 | # (at your option) any later version. |
|---|
| 7 | # |
|---|
| 8 | # This program is distributed in the hope that it will be useful, |
|---|
| 9 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 10 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 11 | # GNU General Public License for more details. |
|---|
| 12 | # |
|---|
| 13 | # You should have received a copy of the GNU General Public License |
|---|
| 14 | # along with this program; if not, write to the Free Software |
|---|
| 15 | # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
|---|
| 16 | |
|---|
| 17 | import logging |
|---|
| 18 | from gettext import gettext as _ |
|---|
| 19 | from xmlrpclib import ServerProxy, Error |
|---|
| 20 | import socket |
|---|
| 21 | import os |
|---|
| 22 | import string |
|---|
| 23 | import random |
|---|
| 24 | import time |
|---|
| 25 | import uuid |
|---|
| 26 | |
|---|
| 27 | import gconf |
|---|
| 28 | |
|---|
| 29 | from sugar import env |
|---|
| 30 | from sugar.profile import get_profile |
|---|
| 31 | |
|---|
| 32 | REGISTER_URL = 'http://schoolserver:8080/' |
|---|
| 33 | |
|---|
| 34 | def generate_serial_number(): |
|---|
| 35 | """ Generates a serial number based on 3 random uppercase letters |
|---|
| 36 | and the last 8 digits of the current unix seconds. """ |
|---|
| 37 | |
|---|
| 38 | serial_part1 = [] |
|---|
| 39 | |
|---|
| 40 | for y in range(3) : |
|---|
| 41 | serial_part1.append(random.choice(string.ascii_uppercase)) |
|---|
| 42 | |
|---|
| 43 | serial_part1 = ''.join(serial_part1) |
|---|
| 44 | serial_part2 = str(int(time.time()))[-8:] |
|---|
| 45 | serial = serial_part1 + serial_part2 |
|---|
| 46 | |
|---|
| 47 | return serial |
|---|
| 48 | |
|---|
| 49 | def store_identifiers(serial_number, uuid, backup_url): |
|---|
| 50 | """ Stores the serial number, uuid and backup_url |
|---|
| 51 | in the identifier folder inside the profile directory |
|---|
| 52 | so that these identifiers can be used for backup. """ |
|---|
| 53 | |
|---|
| 54 | identifier_path = os.path.join(env.get_profile_path(), 'identifiers') |
|---|
| 55 | if not os.path.exists(identifier_path): |
|---|
| 56 | os.mkdir(identifier_path) |
|---|
| 57 | |
|---|
| 58 | if os.path.exists(os.path.join(identifier_path, 'sn')): |
|---|
| 59 | os.remove(os.path.join(identifier_path, 'sn')) |
|---|
| 60 | serial_file = open(os.path.join(identifier_path, 'sn'), 'w') |
|---|
| 61 | serial_file.write(serial_number) |
|---|
| 62 | serial_file.close() |
|---|
| 63 | |
|---|
| 64 | if os.path.exists(os.path.join(identifier_path, 'uuid')): |
|---|
| 65 | os.remove(os.path.join(identifier_path, 'uuid')) |
|---|
| 66 | uuid_file = open(os.path.join(identifier_path, 'uuid'), 'w') |
|---|
| 67 | uuid_file.write(uuid) |
|---|
| 68 | uuid_file.close() |
|---|
| 69 | |
|---|
| 70 | if os.path.exists(os.path.join(identifier_path, 'backup_url')): |
|---|
| 71 | os.remove(os.path.join(identifier_path, 'backup_url')) |
|---|
| 72 | backup_url_file = open(os.path.join(identifier_path, 'backup_url'), 'w') |
|---|
| 73 | backup_url_file.write(backup_url) |
|---|
| 74 | backup_url_file.close() |
|---|
| 75 | |
|---|
| 76 | class RegisterError(Exception): |
|---|
| 77 | pass |
|---|
| 78 | |
|---|
| 79 | def register_laptop(url=REGISTER_URL): |
|---|
| 80 | |
|---|
| 81 | profile = get_profile() |
|---|
| 82 | client = gconf.client_get_default() |
|---|
| 83 | |
|---|
| 84 | if have_ofw_tree(): |
|---|
| 85 | sn = read_ofw('mfg-data/SN') |
|---|
| 86 | uuid_ = read_ofw('mfg-data/U#') |
|---|
| 87 | sn = sn or 'SHF00000000' |
|---|
| 88 | uuid_ = uuid_ or '00000000-0000-0000-0000-000000000000' |
|---|
| 89 | else: |
|---|
| 90 | sn = generate_serial_number() |
|---|
| 91 | uuid_ = str(uuid.uuid1()) |
|---|
| 92 | jabber_server = client.get_string('/desktop/sugar/collaboration/jabber_server') |
|---|
| 93 | store_identifiers(sn, uuid_, jabber_server) |
|---|
| 94 | url = 'http://' + jabber_server + ':8080/' |
|---|
| 95 | |
|---|
| 96 | nick = client.get_string('/desktop/sugar/user/nick') |
|---|
| 97 | |
|---|
| 98 | server = ServerProxy(url) |
|---|
| 99 | try: |
|---|
| 100 | data = server.register(sn, nick, uuid_, profile.pubkey) |
|---|
| 101 | except (Error, socket.error): |
|---|
| 102 | logging.exception('Registration: cannot connect to server') |
|---|
| 103 | raise RegisterError(_('Cannot connect to the server.')) |
|---|
| 104 | |
|---|
| 105 | if data['success'] != 'OK': |
|---|
| 106 | logging.error('Registration: server could not complete request: %s', |
|---|
| 107 | data['error']) |
|---|
| 108 | raise RegisterError(_('The server could not complete the request.')) |
|---|
| 109 | |
|---|
| 110 | client.set_string('/desktop/sugar/collaboration/jabber_server', |
|---|
| 111 | data['jabberserver']) |
|---|
| 112 | client.set_string('/desktop/sugar/backup_url', data['backupurl']) |
|---|
| 113 | |
|---|
| 114 | return True |
|---|
| 115 | |
|---|
| 116 | def have_ofw_tree(): |
|---|
| 117 | return os.path.exists('/ofw') |
|---|
| 118 | |
|---|
| 119 | def read_ofw(path): |
|---|
| 120 | path = os.path.join('/ofw', path) |
|---|
| 121 | if not os.path.exists(path): |
|---|
| 122 | return None |
|---|
| 123 | fh = open(path, 'r') |
|---|
| 124 | data = fh.read().rstrip('\0\n') |
|---|
| 125 | fh.close() |
|---|
| 126 | return data |
|---|