Home | History | Annotate | Download | only in server
      1 # Copyright 2012 the V8 project authors. All rights reserved.
      2 # Redistribution and use in source and binary forms, with or without
      3 # modification, are permitted provided that the following conditions are
      4 # met:
      5 #
      6 #     * Redistributions of source code must retain the above copyright
      7 #       notice, this list of conditions and the following disclaimer.
      8 #     * Redistributions in binary form must reproduce the above
      9 #       copyright notice, this list of conditions and the following
     10 #       disclaimer in the documentation and/or other materials provided
     11 #       with the distribution.
     12 #     * Neither the name of Google Inc. nor the names of its
     13 #       contributors may be used to endorse or promote products derived
     14 #       from this software without specific prior written permission.
     15 #
     16 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     17 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     18 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     19 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     20 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     21 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     22 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     23 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     24 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     25 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     26 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     27 
     28 
     29 import socket
     30 import SocketServer
     31 
     32 from . import compression
     33 from . import constants
     34 
     35 
     36 def _StatusQuery(peer, query):
     37   sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
     38   code = sock.connect_ex((peer.address, constants.STATUS_PORT))
     39   if code != 0:
     40     # TODO(jkummerow): disconnect (after 3 failures?)
     41     return
     42   compression.Send(query, sock)
     43   compression.Send(constants.END_OF_STREAM, sock)
     44   rec = compression.Receiver(sock)
     45   data = None
     46   while not rec.IsDone():
     47     data = rec.Current()
     48     assert data[0] == query[0]
     49     data = data[1]
     50     rec.Advance()
     51   sock.close()
     52   return data
     53 
     54 
     55 def RequestTrustedPubkeys(peer, server):
     56   pubkey_list = _StatusQuery(peer, [constants.LIST_TRUSTED_PUBKEYS])
     57   for pubkey in pubkey_list:
     58     if server.IsTrusted(pubkey): continue
     59     result = _StatusQuery(peer, [constants.GET_SIGNED_PUBKEY, pubkey])
     60     server.AcceptNewTrusted(result)
     61 
     62 
     63 def NotifyNewTrusted(peer, data):
     64   _StatusQuery(peer, [constants.NOTIFY_NEW_TRUSTED] + data)
     65 
     66 
     67 def ITrustYouNow(peer):
     68   _StatusQuery(peer, [constants.TRUST_YOU_NOW])
     69 
     70 
     71 def TryTransitiveTrust(peer, pubkey, server):
     72   if _StatusQuery(peer, [constants.DO_YOU_TRUST, pubkey]):
     73     result = _StatusQuery(peer, [constants.GET_SIGNED_PUBKEY, pubkey])
     74     server.AcceptNewTrusted(result)
     75 
     76 
     77 class StatusHandler(SocketServer.BaseRequestHandler):
     78   def handle(self):
     79     rec = compression.Receiver(self.request)
     80     while not rec.IsDone():
     81       data = rec.Current()
     82       action = data[0]
     83 
     84       if action == constants.LIST_TRUSTED_PUBKEYS:
     85         response = self.server.daemon.ListTrusted()
     86         compression.Send([action, response], self.request)
     87 
     88       elif action == constants.GET_SIGNED_PUBKEY:
     89         response = self.server.daemon.SignTrusted(data[1])
     90         compression.Send([action, response], self.request)
     91 
     92       elif action == constants.NOTIFY_NEW_TRUSTED:
     93         self.server.daemon.AcceptNewTrusted(data[1:])
     94         pass  # No response.
     95 
     96       elif action == constants.TRUST_YOU_NOW:
     97         self.server.daemon.MarkPeerAsTrusting(self.client_address[0])
     98         pass  # No response.
     99 
    100       elif action == constants.DO_YOU_TRUST:
    101         response = self.server.daemon.IsTrusted(data[1])
    102         compression.Send([action, response], self.request)
    103 
    104       rec.Advance()
    105     compression.Send(constants.END_OF_STREAM, self.request)
    106 
    107 
    108 class StatusSocketServer(SocketServer.ThreadingMixIn, SocketServer.TCPServer):
    109   def __init__(self, daemon):
    110     address = (daemon.ip, constants.STATUS_PORT)
    111     SocketServer.TCPServer.__init__(self, address, StatusHandler)
    112     self.daemon = daemon
    113