Beispiel #1
0
def write_random_uuid_file(fname):
    f = None
    try:
        f = open(fname, 'wb')
        f.write('%s\n' % randutil.random_uuid())
    except:
        timestamp = None

    if f is not None:
        f.close()
Beispiel #2
0
def write_random_uuid_file(fname):
    f = None
    try:
        f = open(fname, 'wb')
        f.write('%s\n' % randutil.random_uuid())
    except:
        timestamp = None

    if f is not None:
        f.close()
    def _handle_identify(self, kw, identify_version):
        """Handle Identify, Identify2, Identify3, Identify4"""
        arg_primary = kw['isPrimary']
        arg_licenseKey = kw['licenseKey']
        arg_bootUuid = kw['bootUuid']
        arg_installationUuid = kw['installationUuid']
        arg_cookieUuid = None
        arg_address = kw['address']
        arg_port = kw['port']
        arg_softwareVersion = kw['softwareVersion']
        arg_softwareBuildInfo = kw['softwareBuildInfo']
        arg_hardwareType = kw['hardwareType']
        arg_hardwareInfo = kw['hardwareInfo']
        arg_automaticUpdates = kw['automaticUpdates']
        arg_isLiveCd = kw['isLiveCd']

        # cookieUuid only present in Identify2
        request_had_cookie = False
        if kw.has_key('cookieUuid'):
            request_had_cookie = True
            arg_cookieUuid = kw['cookieUuid']
        
        res = {}

        # Fill in fixed server info
        res['softwareBuildInfo'] = msconstants.SERVER_IDENTIFY_SOFTWARE_BUILD_INFO
        res['serverInfo'] = msconstants.SERVER_IDENTIFY_SERVER_INFO

        # Check license; either anonymous (no license) or non-anonymous
        self.licensemanager.license_lookup(res, arg_licenseKey)

        # Determine proper aptsource - stable or unstable currently
        aptsource = None
        repokeys = None
        if str(self.transport.getPeer().host) in self.master.beta_servers:
            _log.info('beta server detected, using unstable source')
            aptsource = self.master.unstable_aptsource
        else:
            aptsource = self.master.stable_aptsource

        # Repokeys are currently version independent
        f = open(msconstants.REPOSITORY_KEYS_FILE, 'rb')
        repokeys = f.read()
        f.close()

        # Update check
        self.updatemanager.update_check(res, arg_softwareVersion, arg_automaticUpdates, aptsource)

        # Always send up-to-date sources.list; client must only use if update_needed = True
        res['aptSourcesList'] = aptsource

        # Always send repo keys
        res['repositoryKeys'] = repokeys

        # Always send current changelog
        current_version, current_changelog = self.aptcache.get_apt_info(aptsource)
        if current_changelog is not None:
            res['changeLog'] = current_changelog
        else:
            _log.error('changelog information not available, sending back empty string')
            res['changeLog'] = ''

        # Store basic information to own state
        self.identify_successful = True
        self.license_key = arg_licenseKey
        self.license_status = res['licenseStatus']
        self.is_demo_license = res['isDemoLicense']
        self.client_software_version = arg_softwareVersion
        self.client_installation_uuid = arg_installationUuid
        self.client_boot_uuid = arg_bootUuid
        self.client_cookie_uuid = None

        # Note: self.client_cookie_uuid is set to None initially to ensure that the
        # concurrent cookie check below does not mistakenly match this connection

        # Fill in cookie for Identify2
        if request_had_cookie:
            # FIXME - what to do we actually want to do with the cookie?
            #
            # This is the current heuristic for cookies which doesn't actually do
            # anything useful except that it tries to keep the cookies unique.

            if arg_cookieUuid == '':
                res_cookie = randutil.random_uuid()
            else:
                if self.master.cookie_used_by_a_management_connection(arg_cookieUuid):
                    res_cookie = randutil.random_uuid()
                    _log.warning('cookie %s already in use, generated new cookie %s for client' % (arg_cookieUuid, res_cookie))
                else:
                    res_cookie = arg_cookieUuid
            res['cookieUuid'] = unicode(res_cookie)

            # Update cookie in state
            self.client_cookie_uuid = res_cookie

        # Address processing for Identify3
        if identify_version >= 3:
            local_addr = self.transport.getHost()
            remote_addr = self.transport.getPeer()
            behind_nat = False
            if (arg_address != unicode(remote_addr.host)):
                behind_nat = True
            
            res['clientAddressSeenByServer'] = unicode(remote_addr.host)
            res['clientPortSeenByServer'] = int(remote_addr.port)
            res['behindNat'] = behind_nat

        # Currently no v4 specific stuff
        if identify_version >= 4:
            pass
        
        # Fill in current time (last to minimize time diff)
        res['currentUtcTime'] = datetime.datetime.utcnow()

        return res
    def renderHTTP(self, ctx):
        """Determine appropriate redirection URI and redirect.

        Special care must be taken to avoid caching of any content that is
        served here; when the user reissues the same URI to his browser,
        the page should work unless a new forwarding happens.
        """

        try:
            request = inevow.IRequest(ctx)

            host_hdr = request.getHeader('Host')        # may be None
            use_ssl = request.isSecure()
            our_ppp_ip = openl2tp.get_server_ppp_address()  # may except in several cases
            our_ip = str(request.getHost().host)
            our_port = int(request.getHost().port)
            peer_ip = str(request.getClientIP())        # XXX: validate?

            # XXX: may need later, but currently unused
            peer_port = None
            
            # XXX: we should also test if runner is active; not done now, as it is not critical
            # (our_ppp_ip may be invalid but we don't mind)

            # construct a base uri, and append query parameters afterwards
            if use_ssl:
                tmp = 'https://'
            else:
                tmp = 'http://'
            random_uuid = randutil.random_uuid()  # used to make every forwarding URI unique; prevents caching
            tmp += '%s/%s/%s' % (our_ppp_ip.toString(), 'web-forced-redirection', random_uuid)
            redir = url.URL.fromString(tmp)

            # figure out various parameters for redirect
            original_uri = str(request.URLPath())

            client_ppp_address = peer_ip

            if not use_ssl and our_port == constants.WEBUI_FORWARD_PORT_UIFORCED_HTTP:
                forwarding_reason = 'webui-forced'
            elif not use_ssl and our_port == constants.WEBUI_FORWARD_PORT_LICENSE_HTTP:
                forwarding_reason = 'license'
            elif not use_ssl and our_port == constants.WEBUI_FORWARD_PORT_OLDPSK_HTTP:
                forwarding_reason = 'old-psk'
            elif use_ssl and our_port == constants.WEBUI_FORWARD_PORT_UIFORCED_HTTPS:
                forwarding_reason = 'webui-forced'
            elif use_ssl and our_port == constants.WEBUI_FORWARD_PORT_LICENSE_HTTPS:
                forwarding_reason = 'license'
            elif use_ssl and our_port == constants.WEBUI_FORWARD_PORT_OLDPSK_HTTPS:
                forwarding_reason = 'old-psk'
            else:
                raise Exception('unknown local port for forwarding: %s, cannot determine reason' % our_port)

            try:
                ppp_dev = helpers.find_ppp_device_status(address=datatypes.IPv4Address.fromString(peer_ip), username=None)
                client_username = ppp_dev.getS(ns.username, rdf.String)
            except:
                client_username = ''  # XXX: web UI should default to something useful, empty is good here

            try:
                ppp_user = helpers.find_ppp_user(client_username)
                client_user_node = str(ppp_user.getUri())
            except:
                client_user_node = '' # XXX: web UI should do something useful with this
            
            # add resulting args
            redir = redir.add('original_uri', original_uri)
            redir = redir.add('client_username', client_username)
            redir = redir.add('client_ppp_address', client_ppp_address)
            redir = redir.add('client_user_node', client_user_node)
            redir = redir.add('forwarding_reason', forwarding_reason)
            
            # logging
            _log.info('redirecting incoming request (%s:%s -> %s:%s) to uri %s' % (peer_ip, peer_port, our_ip, our_port, str(redir)))
            
            # return a redirect
            request.redirect(redir)
            request.finish()
            return ''
        except:
            _log.exception('cannot determine forwarding uri')
            raise
Beispiel #5
0
 def _get_guid():
     return randutil.random_uuid().upper()