def thisServiceStatus(service):
    try:
        if service in SERVICE_NAMES:
            output = subprocess.Popen(["service", service, "status"],
                                      stdout=subprocess.PIPE)
            statusRawInit, errorStr = output.communicate()
            if not errorStr is None:
                util.debugPrint("Error String detected (status): " +
                                str(errorStr))
                return {STATUS: NOK, ERROR_MESSAGE: errorStr}
            statusRaw = statusRawInit.split()
            util.debugPrint("statusRaw: " + str(statusRaw))
            if "running" in statusRaw:
                return {STATUS: OK, SERVICE_STATUS: "Running"}
            elif "stopped" in statusRaw:
                return {STATUS: OK, SERVICE_STATUS: "Stopped"}
            else:
                return {STATUS: OK, SERVICE_STATUS: "UNKNOWN"}
        else:
            util.errorPrint(service + " does not match a service")
            return {
                STATUS: NOK,
                ERROR_MESSAGE: service + " does not match a service"
            }

    except:
        print "Unexpected error:", sys.exc_info()[0]
        print sys.exc_info()
        traceback.print_exc()
        util.logStackTrace(sys.exc_info())
        raise
예제 #2
0
def upload():
    """

    Upload sensor data to the database. The format is as follows:

        lengthOfDataMessageHeader<CRLF>DataMessageHeader Data

    Note that the data should immediately follow the DataMessage header (no space or CRLF).

    URL Path:

    - None

    URL Parameters:

    - None.

    Return Codes:

    - 200 OK if the data was successfully put into the MSOD database.
    - 403 Forbidden if the sensor key is not recognized.

    """
    try:
        msg = request.data
        populate_db.put_message(request.data)
        return jsonify({"status": "OK"})
    except:
        util.logStackTrace(sys.exc_info())
        traceback.print_exc()
        raise
def stopThisService(service):
    try:
        if service in SERVICE_NAMES:
            if service == SERVICE_NAMES[0]:
                return False
            else:
                output = subprocess.Popen(["/sbin/service", service, "stop"],
                                          stdout=subprocess.PIPE)
                stopRawInit, errorStr = output.communicate()
                if not errorStr is None:
                    util.debugPrint("Error String detected (stop): " +
                                    str(errorStr))
                    return False
                util.debugPrint("output.communicate() (stop): " +
                                str(stopRawInit))
                return True
        else:
            util.debugPrint(service + " does not match a service")
            return False
    except:
        print "Unexpected error:", sys.exc_info()[0]
        print sys.exc_info()
        traceback.print_exc()
        util.logStackTrace(sys.exc_info())
        raise
def getCaptureEvents(sensorId, startDate, dayCount, sessionId):
    """
    get the capture events for a given sensor within the specified date range.


    """
    try:
        if not authentication.checkSessionId(sessionId, USER):
            util.debugPrint("getCaptureEvents: failed authentication")
            abort(403)
        try:
            sdate = int(startDate)
            dcount = int(dayCount)
        except ValueError:
            abort(400)
        if sdate < 0 or dcount < 0:
            abort(400)
        elif dcount == 0:
            abort(400)
        return jsonify(CaptureDb.getEvents(sensorId, sdate, dcount))
    except:
        print "Unexpected error:", sys.exc_info()[0]
        print sys.exc_info()
        traceback.print_exc()
        util.logStackTrace(sys.exc_info())
        raise
예제 #5
0
    def setSystemConfigWorker(sessionId):
        try:
            util.debugPrint("setSystemConfig: " + sessionId)
            if not authentication.checkSessionId(sessionId, ADMIN):
                abort(403)
            util.debugPrint("passed authentication")
            requestStr = request.data
            systemConfig = json.loads(requestStr)
            (statusCode, message) = Config.verifySystemConfig(systemConfig)
            if not statusCode:
                util.debugPrint("did not verify sys config")
                return jsonify({"status": "NOK", "ErrorMessage": message})

            util.debugPrint("setSystemConfig " + json.dumps(
                systemConfig,
                indent=4,
            ))
            if Config.setSystemConfig(systemConfig):
                return jsonify({"status": "OK"})
            else:
                return jsonify({"status": "NOK", "ErrorMessage": "Unknown"})
        except:
            print "Unexpected error:", sys.exc_info()[0]
            print sys.exc_info()
            traceback.print_exc()
            util.logStackTrace(sys.exc_info())
            raise
예제 #6
0
def verifySessionToken(sessionId):
    """
    Check the session token. Return TRUE if session Token is good and false
    otherwise.

    URL Path:

        - sessionId: the session ID to check.


    HTTP Return code:

       - 200 OK
         returns a document {status:OK} or {status: NOK} depending upon
         whether the session token verified or not.

    """
    try:
        if authentication.checkSessionId(sessionId, ADMIN):
            return jsonify({"status": "OK"})
        else:
            return jsonify({"status": "NOK"})
    except:
        print "Unexpected error:", sys.exc_info()[0]
        print sys.exc_info()
        traceback.print_exc()
        util.logStackTrace(sys.exc_info())
        raise
예제 #7
0
def sendMail(message, receiver, subject, link=False):
    if not Config.isMailServerConfigured():
        util.debugPrint("Cant Send mail. Mail server is not configured")
        return
    try:
        util.debugPrint("sendMail: smtpEmail " + Config.getSmtpEmail())
        util.debugPrint("sendMail: smtpServer " + Config.getSmtpServer())
        server = smtplib.SMTP(Config.getSmtpServer(),
                              Config.getSmtpPort(),
                              timeout=30)
        sender = Config.getSmtpEmail()
        if link:
            message = MIMEText(message, 'html')
        else:
            message = MIMEText(message)
        message["From"] = Config.getSmtpEmail()
        message["To"] = receiver
        message["Subject"] = subject
        #message["Content-Type:"] = "text/html"
        server.sendmail(sender, [receiver], message.as_string())
        server.quit()
    except:
        print "Unexpected error:", sys.exc_info()[0]
        print sys.exc_info()
        traceback.print_exc()
        util.errorPrint("Unexpected error: sendMail")
        util.logStackTrace(sys.exc_info())
def runForensics(sensorId, algorithm, timestamp, sessionId):
    """
    Run forensics at the sensor. This just relays the command to the sensor. The sensor will post back
    after the processing is done.

    timestamp -- the timestamp of the capture.
    algorithm -- the algortithm  to appy (from the toolbox that lives on the sensor)
    sessionId -- the login session id.

    """
    try:
        if not authentication.checkSessionId(sessionId, USER):
            util.debugPrint("runForensics - request body not found")
            abort(403)
        command = {
            "sensorId": sensorId,
            "timestamp": int(timestamp),
            "algorithm": algorithm,
            "command": "analyze"
        }
        sendCommandToSensor(sensorId, json.dumps(command))
        return jsonify({STATUS: OK})
    except:
        print "Unexpected error:", sys.exc_info()[0]
        print sys.exc_info()
        traceback.print_exc()
        util.logStackTrace(sys.exc_info())
        raise
def restartService(service, sessionId):
    """

    Restart specified service
    URL Path:
        sessionId the session Id of the login in session.

    URL Args: None

    Request Body:
        A String of the name of the service

    """
    try:
        util.debugPrint("restartService " + str(service))
        if not authentication.checkSessionId(sessionId, ADMIN):
            abort(403)
        util.debugPrint("passed authentication")
        if restartThisService(service):
            return jsonify({"status": "OK"})
        else:
            return jsonify({
                "status": "NOK",
                "ErrorMessage": "Unknown service"
            })
    except:
        print "Unexpected error:", sys.exc_info()[0]
        print sys.exc_info()
        traceback.print_exc()
        util.logStackTrace(sys.exc_info())
        raise
def clearlogs(sessionId):
    if not authentication.checkSessionId(sessionId, ADMIN):
        abort(403)
    try:
        for f in [
                "/var/log/monitoring.log", "/var/log/federation.log",
                "/var/log/streaming.log", "/var/log/occupancy.log",
                "/var/log/flask/federation.log",
                "/var/log/flask/spectrumbrowser.log",
                "/var/log/flask/spectrumdb.log"
        ]:
            if os.path.exists(f):
                os.remove(f)
        serviceNames = [
            "spectrumbrowser", "streaming", "occupancy", "monitoring",
            "federation", "spectrumdb"
        ]
        for service in serviceNames:
            restartThisService(service)
        return jsonify({"status": "OK"})
    except:
        print "Unexpected error:", sys.exc_info()[0]
        print sys.exc_info()
        traceback.print_exc()
        util.logStackTrace(sys.exc_info())
        raise
예제 #11
0
    def addESAgentWorker(sessionId):
        try:
            if not authentication.checkSessionId(sessionId, ADMIN):
                abort(403)
            requestStr = request.data
            agentConfig = json.loads(requestStr)
            agentName = agentConfig["agentName"]

            bad_chars = invalid_chars.intersection(agentName)
            if bad_chars:
                msg = "Invalid character in agentName: {}"
                util.debugPrint(msg.format(bad_chars))
                abort(400)

            key = agentConfig["key"]
            bad_chars = invalid_chars.intersection(key)
            if bad_chars:
                msg = "Invalid character in key: {}"
                util.debugPrint(msg.format(bad_chars))
                abort(400)

            Config.addESAgent(agentName, key)
            agents = Config.getESAgents()
            retval = {"esAgents": agents}
            retval[STATUS] = 'OK'
            return jsonify(retval)
        except:
            print "Unexpected error:", sys.exc_info()[0]
            print sys.exc_info()
            traceback.print_exc()
            util.logStackTrace(sys.exc_info())
            raise
def deleteCaptureEvents(sensorId, startDate, sessionId):
    """
    Delete the events from the capture db.
    Send a message to the sensor to do the same.
    """
    try:
        if not authentication.checkSessionId(sessionId, ADMIN):
            util.debugPrint("deleteCaptureEvents: failed authentication")
            abort(403)
        sdate = int(startDate)
        if sdate < 0:
            util.debugPrint("deleteCaptureEvents: illegal param")
            abort(400)
        else:
            CaptureDb.deleteCaptureDb(sensorId, sdate)
            global memCache
            if memCache is None:
                memCache = MemCache()
            command = json.dumps({
                "sensorId": sensorId,
                "timestamp": sdate,
                "command": "garbage_collect"
            })
            sendCommandToSensor(sensorId, command)
            return jsonify({STATUS: "OK"})
    except:
        print "Unexpected error:", sys.exc_info()[0]
        print sys.exc_info()
        traceback.print_exc()
        util.logStackTrace(sys.exc_info())
        raise
def disconnectSensor(sensorId):
    """
    Send a sensor a command to exit.

    URL Path:
        sensorId -- the session ID of the login session.

    URL Args: None

    Request Body:
    Contains authentication information for the agent that is authorized
    to arm and disarm the sensor:

        - agentName: Name of the agent to arm/disarm sensor.
        - key  : password of the agent to arm/disarm the sensor.

    HTTP Return Codes:

        - 200 OK: invocation was successful.
        - 403 Forbidden: authentication failure
        - 400 Bad request: Sensor is not a streaming sensor.

    Example Invocation:

   ::

       params = {}
       params["agentName"] = "NIST_ESC"
       params["key"] = "ESC_PASS"
       r = requests.post("https://"+ host + ":" + str(443) + "/sensorcontrol/disconnectSensor/" + self.sensorId + "/LTE:70315780:713315880",data=json.dumps(params),verify=False)


    """
    try:
        util.debugPrint("disconnectSensor: sensorId " + sensorId)
        requestStr = request.data
        if requestStr is None:
            abort(400)
        accountData = json.loads(requestStr)
        if not authentication.authenticateSensorAgent(accountData):
            abort(403)
        sensorConfig = SensorDb.getSensorObj(sensorId)
        if sensorConfig is None:
            abort(404)
        if not sensorConfig.isStreamingEnabled():
            abort(400)
        sendCommandToSensor(
            sensorId, json.dumps({
                "sensorId": sensorId,
                "command": "exit"
            }))
        return jsonify({STATUS: OK})
    except:
        print "Unexpected error:", sys.exc_info()[0]
        print sys.exc_info()
        traceback.print_exc()
        util.logStackTrace(sys.exc_info())
        raise
예제 #14
0
 def logOutWorker(sessionId):
     try:
         authentication.logOut(sessionId)
         return jsonify({"status": "OK"})
     except:
         print "Unexpected error:", sys.exc_info()[0]
         print sys.exc_info()
         traceback.print_exc()
         util.logStackTrace(sys.exc_info())
         raise
예제 #15
0
def testArmSensor(sensorId, sessionId):
    """
    URL Path:
        sessionId -- the session ID of the login session.
        sensorId -- the sensorId

    URL Args: None

    Request Body:

        - agentName: Name of the agent to arm/disarm sensor.
        - key      : Key (password) of the agent to arm/disarm the sensor.

    HTTP Return Codes:

        - 200 OK: invocation was successful.
        - 403 Forbidden: authentication failure
        - 400 Bad request: Sensor is not a streaming sensor.

    Example Invocation:

   ::

       params = {}
       params["agentName"] = "NIST_ESC"
       params["key"] = "ESC_PASS"
       url = "https://{host}:8443/admin/armSensor/{self.sensorId}"
       r = requests.post(url, data=json.dumps(params), verify=False)

    """
    try:
        if not authentication.checkSessionId(sessionId, ADMIN):
            abort(403)
        sensorConfig = SensorDb.getSensorObj(sensorId)
        if sensorConfig is None:
            abort(404)
        if not sensorConfig.isStreamingEnabled():
            abort(400)
        persistent = request.args.get("persistent")
        if persistent is None:
            persistent = "false"
        DataStreamSharedState.sendCommandToSensor(
            sensorId,
            json.dumps({
                "sensorId": sensorId,
                "command": "arm",
                "persistent": persistent
            }))
        return jsonify({STATUS: OK})
    except:
        print "Unexpected error:", sys.exc_info()[0]
        print sys.exc_info()
        traceback.print_exc()
        util.logStackTrace(sys.exc_info())
        raise
예제 #16
0
 def recomputeOccupanciesWorker(sensorId, sessionId):
     try:
         if not authentication.checkSessionId(sessionId, ADMIN):
             return make_response("Session not found", 403)
         return jsonify(RecomputeOccupancies.recomputeOccupancies(sensorId))
     except:
         print "Unexpected error:", sys.exc_info()[0]
         print sys.exc_info()
         traceback.print_exc()
         util.logStackTrace(sys.exc_info())
         raise
예제 #17
0
 def unfreezeRequestWorker(sessionId):
     try:
         if not authentication.checkSessionId(sessionId, ADMIN):
             return make_response("Session not found", 403)
         return jsonify(SessionLock.freezeRelease(sessionId))
     except:
         print "Unexpected error:", sys.exc_info()[0]
         print sys.exc_info()
         traceback.print_exc()
         util.logStackTrace(sys.exc_info())
         raise
예제 #18
0
def getResourceData(ws):
    """
    Web-browser websocket connection handler.
    """
    util.debugPrint("getResourceData")
    try:
        ResourceDataStreaming.getResourceData(ws)
    except:
        util.logStackTrace(sys.exc_info())
        traceback.print_exc()
        raise
예제 #19
0
 def resetPasswordWorker():
     try:
         requestStr = request.data
         accountData = json.loads(requestStr)
         return authentication.resetPassword(accountData)
     except:
         print "Unexpected error:", sys.exc_info()[0]
         print sys.exc_info()
         traceback.print_exc()
         util.logStackTrace(sys.exc_info())
         raise
예제 #20
0
 def garbageCollectWorker(sensorId, sessionId):
     try:
         if not authentication.checkSessionId(sessionId, ADMIN):
             return make_response("Session not found", 403)
         return jsonify(GarbageCollect.runGarbageCollector(sensorId))
     except:
         print "Unexpected error:", sys.exc_info()[0]
         print sys.exc_info()
         traceback.print_exc()
         util.logStackTrace(sys.exc_info())
         raise
예제 #21
0
 def getSessionsWorker(sessionId):
     try:
         if not authentication.checkSessionId(
                 sessionId, ADMIN, updateSessionTimer=False):
             return make_response("Session not found", 403)
         return jsonify(SessionLock.getSessions())
     except:
         print "Unexpected error:", sys.exc_info()[0]
         print sys.exc_info()
         traceback.print_exc()
         util.logStackTrace(sys.exc_info())
         raise
예제 #22
0
 def unlockAccountWorker(emailAddress, sessionId):
     try:
         if not authentication.checkSessionId(sessionId, ADMIN):
             abort(403)
         util.debugPrint("unlockAccount")
         return jsonify(AccountsManagement.unlockAccount(emailAddress))
     except:
         print "Unexpected error:", sys.exc_info()[0]
         print sys.exc_info()
         traceback.print_exc()
         util.logStackTrace(sys.exc_info())
         raise
def getResourceData(ws):
    """

    Handle resource data streaming requests from the web browser.

    Token is of the form <sessionID> => len(parts)==1

    """
    try:
        util.debugPrint("ResourceDataStreaming:getResourceData")
        token = ws.receive()

        if token is None:  #or len(parts) < 2:
            ws.close()
            return
        sessionId = token
        if not authentication.checkSessionId(sessionId, "admin"):
            ws.close()
            util.debugPrint(
                "ResourceDataStreamng:failed to authenticate: user != " +
                sessionId)
            return

        memCache = memcache.Client(['127.0.0.1:11211'], debug=0)
        keys = MemCacheKeys.RESOURCEKEYS
        resourceData = {}
        secondsPerFrame = 1
        while True:
            for resource in keys:
                key = str(resource).encode("UTF-8")
                value = memCache.get(key)
                if value is not None:
                    resourceData[str(key)] = float(value)
                else:
                    util.errorPrint("Unrecognized resource key " + key)

            client = MongoClient(getDbHost(), 27017)
            collection = client.systemResources.dbResources
            dbResources = collection.find_one({})
            if dbResources is not None and dbResources["Disk"] is not None:
                resourceData["Disk"] = float(dbResources["Disk"])

            util.debugPrint("resource Data = " +
                            str(json.dumps(resourceData, indent=4)))

            ws.send(json.dumps(resourceData))
            sleepTime = secondsPerFrame
            gevent.sleep(sleepTime)
    except:
        traceback.print_exc()
        util.debugPrint("Error writing to resource websocket")
        util.logStackTrace(traceback)
        ws.close()
def postCaptureEvent():
    """
    Handle post of a capture event from a sensor

    URL Path:

        - None

    URL Parameters:

        - None

    Request Body:

        - CaptureEvent JSON structure which includes the sensor ID and sensor key.
          These are used for verifying the request. See MSOD specification for definition of
          CaptureEvent structure.


    HTTP Return Codes:

       - 200 OK. A JSON Document containing {"status":"OK"} is returned.

    """
    try:
        requestStr = request.data
        if requestStr is None or requestStr == "":
            util.debugPrint("postCaptureEvent - request body not found")
            abort(400)

        util.debugPrint("postCaptureEvent " + requestStr)
        captureEvent = json.loads(requestStr)

        if SENSOR_ID not in captureEvent or SENSOR_KEY not in captureEvent:
            util.debugPrint("postCaptureEvent - missing a required field")
            abort(400)

        sensorId = captureEvent[SENSOR_ID]
        sensorConfig = SensorDb.getSensorObj(sensorId)
        if sensorConfig is None:
            util.debugPrint("postCaptureEvent - sensor not found")
            abort(404)
        sensorKey = captureEvent[SENSOR_KEY]
        if not authentication.authenticateSensor(sensorId, sensorKey):
            abort(403)
        return jsonify(CaptureDb.insertEvent(sensorId, captureEvent))
    except:
        print "Unexpected error:", sys.exc_info()[0]
        print sys.exc_info()
        traceback.print_exc()
        util.logStackTrace(sys.exc_info())
        raise
예제 #25
0
 def removePeerWorker(host, port, sessionId):
     try:
         if not authentication.checkSessionId(sessionId, ADMIN):
             abort(403)
         Config.removePeer(host, int(port))
         peers = Config.getPeers()
         retval = {"peers": peers}
         return jsonify(retval)
     except:
         print "Unexpected error:", sys.exc_info()[0]
         print sys.exc_info()
         traceback.print_exc()
         util.logStackTrace(sys.exc_info())
         raise
예제 #26
0
 def getScreenConfigWorker(sessionId):
     try:
         screenConfig = Config.getScreenConfig()
         if screenConfig is None:
             config = Config.getDefaultScreenConfig()
             return jsonify(config)
         else:
             return jsonify(screenConfig)
     except:
         print "Unexpected error:", sys.exc_info()[0]
         print sys.exc_info()
         traceback.print_exc()
         util.logStackTrace(sys.exc_info())
         raise
예제 #27
0
 def createAccountWorker(sessionId):
     try:
         if not authentication.checkSessionId(sessionId, ADMIN):
             abort(403)
         util.debugPrint("createAccount")
         requestStr = request.data
         accountData = json.loads(requestStr)
         return jsonify(AccountsManagement.createAccount(accountData))
     except:
         print "Unexpected error:", sys.exc_info()[0]
         print sys.exc_info()
         traceback.print_exc()
         util.logStackTrace(sys.exc_info())
         raise
예제 #28
0
 def getESAgentsWorker(sessionId):
     try:
         if not authentication.checkSessionId(sessionId, ADMIN):
             abort(403)
         agents = Config.getESAgents()
         retval = {"esAgents": agents}
         retval[STATUS] = 'OK'
         return jsonify(retval)
     except:
         print "Unexpected error:", sys.exc_info()[0]
         print sys.exc_info()
         traceback.print_exc()
         util.logStackTrace(sys.exc_info())
         raise
예제 #29
0
 def getInboundPeersWorker(sessionId):
     try:
         if not authentication.checkSessionId(sessionId, ADMIN):
             abort(403)
         peerKeys = Config.getInboundPeers()
         retval = {"inboundPeers": peerKeys}
         retval[STATUS] = OK
         return jsonify(retval)
     except:
         print "Unexpected error:", sys.exc_info()[0]
         print sys.exc_info()
         traceback.print_exc()
         util.logStackTrace(sys.exc_info())
         raise
예제 #30
0
 def getFreqBandsWorker(sessionId):
     try:
         if not Config.isConfigured():
             util.debugPrint("Please configure system")
             return make_response("Please configure system", 500)
         if not authentication.checkSessionId(sessionId, ADMIN):
             return make_response("Session not found.", 403)
         return jsonify(SensorDb.getFreqBands())
     except:
         print "Unexpected error:", sys.exc_info()[0]
         print sys.exc_info()
         traceback.print_exc()
         util.logStackTrace(sys.exc_info())
         raise