Ejemplo n.º 1
0
 def getResponse(self,buff,srcaddr):
     from helper import fingerPrintPacket,getTag
     srcip,srcport = srcaddr
     uaname = 'unknown'
     if buff.startswith('OPTIONS ') \
         or buff.startswith('INVITE ') \
         or buff.startswith('REGISTER '): 
         if self.externalip == srcip:
             self.log.debug("We received our own packet from %s:%s" % srcaddr)
         else: 
             self.log.info("Looks like we received a SIP request from %s:%s"% srcaddr)
             self.log.debug(repr(buff))            
         return
     self.log.debug("running fingerPrintPacket()")
     res = fingerPrintPacket(buff)
     if res is not None:
         if res.has_key('name'):
             uaname = res['name'][0]
         else:
             uaname = 'unknown'
             self.log.debug(`buff`)
         if self.fpworks:
             try:
                 fp = self.sipfingerprint(buff)
             except:
                 self.log.error("fingerprinting gave errors - will be disabled")
                 self.fpworks = False
         if not self.fpworks:
             fp = None
         if fp is None:
             if self.fpworks:
                 fpname = 'unknown'
             else:
                 fpname = 'disabled'
         else:
             fpname = ' / '.join(fp)
         self.log.debug('Fingerprint: %s' % fpname)
         self.log.debug("Uaname: %s" % uaname)
         #print buff
         originaldst = getTag(buff)
         try:
             dstip = socket.inet_ntoa(pack('!L',int(originaldst[:8],16)))
             dstport = int(originaldst[8:12],16)
         except (ValueError,TypeError,socket.error):
             self.log.debug("original destination could not be decoded: %s" % (originaldst))
             dstip,dstport = 'unknown','unknown'            
         resultstr = '%s:%s\t->\t%s:%s\t->\t%s\t->\t%s' % (dstip,dstport,srcip,srcport,uaname,fpname)
         self.log.info( resultstr )
         self.resultip['%s:%s' % (srcip,srcport)] = '%s:%s' % (dstip,dstport)
         self.resultua['%s:%s' % (srcip,srcport)] = uaname
         self.resultfp['%s:%s' % (srcip,srcport)] = fpname
         if self.sessionpath is not None and self.dbsyncs:
                 self.resultip.sync()
                 self.resultua.sync()
                 self.resultfp.sync()
     else:
         self.log.info('Packet from %s:%s did not contain a SIP msg'%srcaddr)
         self.log.debug('Packet: %s' % `buff`)
Ejemplo n.º 2
0
    def getResponse(self):
        from helper import getNonce,getCredentials,getRealm,getCID,getTag        
        from base64 import b64decode
        from helper import parseHeader
        from helper import mysendto
        import re
        # we got stuff to read off the socket
        from socket import error as socketerror
        buff,srcaddr = self.sock.recvfrom(8192)
        try:
            extension = getTag(buff)
        except TypeError:
            
            extension = None
        if extension is None:
            self.nomore = True
            return
        try:
            firstline = buff.splitlines()[0]
        except (ValueError,IndexError,AttributeError):
            self.log.error("could not get the 1st line")
            return

        # send an ack to any responses which match
        _tmp = parseHeader(buff)
        if _tmp['code'] >= 200:
            self.log.debug('will try to send an ACK response')
            if _tmp['code'] >= 300:
                # handle differently
                pass
            if not _tmp.has_key('headers'):
                self.log.debug('no headers?')
                return
            if not _tmp['headers'].has_key('from'):
                self.log.debug('no from?')
                return
            if not _tmp['headers'].has_key('cseq'):
                self.log.debug('no cseq')
                return
            if not _tmp['headers'].has_key('call-id'):
                self.log.debug('no caller id')
                return
            username = _tmp['headers']['from'][0].split('"')[1]
            cseq = _tmp['headers']['cseq'][0]
            cid = _tmp['headers']['call-id'][0]
            ackreq = self.createRequest('ACK',
                                   username=username,
                                   cid=cid,
                                   cseq=cseq,
                                   )
            self.log.debug('here is your ack request: %s' % ackreq)
            mysendto(self.sock,ackreq,(self.dsthost,self.dstport))
            #self.sock.sendto(ackreq,(self.dsthost,self.dstport))

        if firstline != self.BADUSER:
            if buff.startswith(self.PROXYAUTHREQ) \
            or buff.startswith(self.INVALIDPASS) \
            or buff.startswith(self.AUTHREQ):
                if self.realm is None:
                    self.realm = getRealm(buff)
                self.log.info("extension '%s' exists - requires authentication" % extension)
                self.resultauth[extension] = 'reqauth'
                
                if self.sessionpath is not None and self.dbsyncs:
                    self.resultauth.sync()
            elif buff.startswith(self.TRYING):
                pass
            elif buff.startswith(self.RINGING):
                pass
            elif buff.startswith(self.OKEY):
                self.log.info("extension '%s' exists - authentication not required" % extension)
                self.resultauth[extension] = 'noauth'
                if self.sessionpath is not None and self.dbsyncs:
                    self.resultauth.sync()
            else:
                sys.exit(1)
                self.log.warn("extension '%s' probably exists but the response is unexpected" % extension)
                self.log.debug("response: %s" % firstline)
                self.resultauth[extension] = 'weird'
                
                if self.sessionpath is not None and self.dbsyncs:
                    self.resultauth.sync()
        elif buff.startswith(self.NOTFOUND):
            self.log.debug("User '%s' not found" % extension)
        
        # Prefix not found, lets go to the next one. Should we add a warning here???
        elif buff.startswith(self.SERVICEUN):
            pass
        elif buff.startswith(self.TRYING):
            pass
        elif buff.startswith(self.RINGING):
            pass
        elif buff.startswith(self.OKEY):
            pass
        elif buff.startswith(self.DECLINED):
            pass
        elif buff.startswith(self.NOTALLOWED):
            self.log.warn("method not allowed")
            self.nomore = True
            return
        elif buff.startswith(self.BADREQUEST):
            self.log.error("Protocol / interopability error! The remote side most probably has problems with parsing your SIP messages!")
            self.nomore = True
            return
        else:
            self.log.warn("We got an unknown response")
            #self.log.error("Response: %s" % `buff`)
            self.log.debug("1st line: %s" % `firstline`)
            self.log.debug("Bad user: %s" % `self.BADUSER`)
            self.nomore = True
Ejemplo n.º 3
0
    def getResponse(self):
        from helper import getNonce,getCredentials,getRealm,getCID,getTag        
        from base64 import b64decode
        from helper import parseHeader
        from helper import mysendto
        import re
        # we got stuff to read off the socket
        from socket import error as socketerror
        buff,srcaddr = self.sock.recvfrom(8192)
        try:
            extension = getTag(buff)
        except TypeError:
            self.log.error('could not decode to tag')
            extension = None
        if extension is None:
            self.nomore = True
            return
        try:
            firstline = buff.splitlines()[0]
        except (ValueError,IndexError,AttributeError):
            self.log.error("could not get the 1st line")
            return
        if not self.disableack:
            # send an ack to any responses which match
            _tmp = parseHeader(buff)
            if 300 > _tmp['code'] >= 200:
                self.log.debug('will try to send an ACK response')                
                if not _tmp.has_key('headers'):
                    self.log.debug('no headers?')
                    return
                if not _tmp['headers'].has_key('from'):
                    self.log.debug('no from?')
                    return
                if not _tmp['headers'].has_key('cseq'):
                    self.log.debug('no cseq')
                    return
                if not _tmp['headers'].has_key('call-id'):
                    self.log.debug('no caller id')
                    return
                try:
                    username = getTag(buff)#_tmp['headers']['from'][0].split('"')[1]
                except IndexError:
                    self.log.warn('could not parse the from address %s' % _tmp['headers']['from'])
                    username = '******'
                cseq = _tmp['headers']['cseq'][0]
                cid = _tmp['headers']['call-id'][0]
                ackreq = self.createRequest('ACK',
                                       username=username,
                                       cid=cid,
                                       cseq=cseq,
                                       )
                self.log.debug('here is your ack request: %s' % ackreq)
                mysendto(self.sock,ackreq,(self.dsthost,self.dstport))
                #self.sock.sendto(ackreq,(self.dsthost,self.dstport))

        if firstline != self.BADUSER:
            if buff.startswith(self.PROXYAUTHREQ) \
            or buff.startswith(self.INVALIDPASS) \
            or buff.startswith(self.AUTHREQ):
                if self.realm is None:
                    self.realm = getRealm(buff)
                self.log.info("extension '%s' exists - requires authentication" % extension)
                self.resultauth[extension] = 'reqauth'
                if self.sessionpath is not None and self.dbsyncs:
                    self.resultauth.sync()
            elif buff.startswith(self.TRYING):
                pass
            elif buff.startswith(self.RINGING):
                pass
            elif buff.startswith(self.OKEY):
                self.log.info("extension '%s' exists - authentication not required" % extension)
                self.resultauth[extension] = 'noauth'
                if self.sessionpath is not None and self.dbsyncs:
                    self.resultauth.sync()
            else:
                self.log.warn("extension '%s' probably exists but the response is unexpected" % extension)
                self.log.debug("response: %s" % firstline)
                self.resultauth[extension] = 'weird'
                if self.sessionpath is not None and self.dbsyncs:
                    self.resultauth.sync()
        elif buff.startswith(self.NOTFOUND):
            self.log.debug("User '%s' not found" % extension)
        elif buff.startswith(self.INEXISTENTTRANSACTION):
            pass
        
        # Prefix not found, lets go to the next one. Should we add a warning here???
        elif buff.startswith(self.SERVICEUN):
            pass
        elif buff.startswith(self.TRYING):
            pass
        elif buff.startswith(self.RINGING):
            pass
        elif buff.startswith(self.OKEY):
            pass
        elif buff.startswith(self.DECLINED):
            pass
        elif buff.startswith(self.NOTALLOWED):
            self.log.warn("method not allowed")
            self.nomore = True
            return
        elif buff.startswith(self.BADREQUEST):
            self.log.error("Protocol / interopability error! The remote side most probably has problems with parsing your SIP messages!")
            self.nomore = True
            return
        else:
            self.log.warn("We got an unknown response")
            self.log.error("Response: %s" % `buff`)
            self.log.debug("1st line: %s" % `firstline`)
            self.log.debug("Bad user: %s" % `self.BADUSER`)
            self.nomore = True