def run(self): company_id = self.company_id json_data = {} self.log = Logger('BroadCastMesageThread') #TAG = 'run' # print 'In BroadCastMesageThread\'s POST' # print self.request.request.body data = json.loads(self.request.request.body) broadcast_message = data.get('broadcast') if broadcast_message and len(broadcast_message.strip()) > 0: json_data['broadcast'] = broadcast_message json_data['to'] = 'company' json_data['id'] = company_id json_data['company_id'] = company_id create_command_handler_task.delay(json_data) opJson = json.dumps( { 'pass': True, 'message': 'Message will be delivered to devices \ immediately.'}) else: opJson = json.dumps( {'pass': False, 'message': 'Please send some human readable broadcast \ message'}) self.request.write(opJson) tornado.ioloop.IOLoop.instance().add_callback(self.callback)
def run(self): company_id = self.company_id json_data = {} self.log = Logger('BroadCastMesageThread') TAG = 'run' # print 'In BroadCastMesageThread\'s POST' # print self.request.request.body data = json.loads(self.request.request.body) broadcast_message = data.get('broadcast') if broadcast_message and len(broadcast_message.strip()) > 0: json_data['broadcast'] = broadcast_message json_data['to'] = 'company' json_data['id'] = company_id json_data['company_id'] = company_id create_command_handler_task.delay(json_data) opJson = json.dumps({'pass': True, 'message': 'Message will be delivered to devices immediately.'}) else: opJson = json.dumps({'pass': False, 'message': 'Please send some human readable broadcast message'}) self.request.write(opJson) tornado.ioloop.IOLoop.instance().add_callback(self.callback)
def run(self): log = Logger('TeamPolicyPutHandlerThread') TAG = 'run' # Flag to track status of policy status = False if self.data and self.company_id: # do stuff here team = TeamDBHelper() url_data = self.data.split('/') request_data = json.loads(self.request.request.body) team_id = url_data[0] plugin_name = url_data[1] team_detail = team.get_team(str(team_id), company_id=self.company_id) if team_detail: team_policy_id = team_detail.get('policy_id') command_handler_json = {'to': 'team', 'id': str(team_id), 'company_id': self.company_id} if plugin_name != 'actions': status = put_individual_plugin(team_policy_id, plugin_name, request_data) else: status = True command_handler_json['action'] = request_data.get('action') command_handler_json['passcode'] = request_data.get( 'lock_key') if status: print "\nprinting the json output will be send to command\ handler\n", command_handler_json create_command_handler_task.delay(command_handler_json) request_data['_id'] = team_policy_id request_data['object_type'] = 'Team' request_data['name'] = team_detail.get(c.TEAM_TABLE_NAME) opJson = json.dumps({'data': request_data, 'pass': True, 'message': 'Everything fine', 'count':1}) self.request.write(opJson) tornado.ioloop.IOLoop.instance().add_callback( self.callback) else: opJson = json.dumps({'pass': False, 'count': 0, 'message': 'Update operation at policy table failed'}) self.request.write(opJson) tornado.ioloop.IOLoop.instance().add_callback( self.callback) else: log.e(TAG, 'No valid team id is sent in request') opJson = json.dumps({'pass': False, 'message': 'No valid team id is sent in request'}) self.request.write(opJson) tornado.ioloop.IOLoop.instance().add_callback(self.callback) else: log.e(TAG, 'UnAuthorized Access for team policy ') self.request.set_status(401) tornado.ioloop.IOLoop.instance().add_callback(self.callback)
def run(self): log = Logger('CompanyPutHandlerThread') TAG = 'run' # flag to track policy query status = False if self.data: # do stuff here company = CompanyDBHelper() url_data = self.data.split('/') request_data = json.loads(self.request.request.body) company_id = self.company_id #url_data[0] plugin_name = url_data[1] company_detail = company.get_company(str(company_id)) if company_detail: company_policy_id = company_detail.get('policy_id') command_handler_json = {'to': 'company', 'id': str(company_id), 'company_id': self.company_id} if plugin_name != 'actions': status = put_individual_plugin(company_policy_id, plugin_name, request_data) else: status = True command_handler_json['action'] = request_data.get('action') command_handler_json['passcode'] = request_data.get( 'lock_key') if status: create_command_handler_task.delay(command_handler_json) request_data['_id'] = company_policy_id request_data['object_type'] = 'Company' request_data['name'] = company_detail.get( c.COMPANY_TABLE_NAME) opJson = json.dumps({'data': request_data, 'pass': True, 'count': 1, 'message': 'Everything fine'}) self.request.write(opJson) tornado.ioloop.IOLoop.instance().add_callback( self.callback) else: opJson = json.dumps({'pass': False, 'count': 0, 'message': 'Update operation at policy table failed'}) self.request.write(opJson) tornado.ioloop.IOLoop.instance().add_callback( self.callback) else: log.e(TAG, 'No valid company id is sent in request') opJson = json.dumps({'pass': False, 'message': 'No valid company id is sent in request'}) self.request.write(opJson) tornado.ioloop.IOLoop.instance().add_callback(self.callback) else: log.e(TAG, 'UnAuthorized Access for company policy ') self.request.set_status(401) tornado.ioloop.IOLoop.instance().add_callback(self.callback)
def run(self): log = Logger('CheckInHandler PUT') TAG = 'run' # print ' In deviceCheckin PUT Handler\n\n\n' # parse the body of PUT # First extract the XML arguments = cgi.parse_qsl(self.request.request.body) intermediate = arguments[0] currentxml = intermediate[1] final = currentxml[25:-1] enrollment_id = str(self.data) # print final # Actual Parsing #tree = ET.ElementTree(ET.fromstring(final)) # tree = ET.parse('temp.xml') ### For testing only node = etree.fromstring(final) device_data = [] for text_of_child in node.itertext(): if len(text_of_child.strip()) > 0: device_data.append(text_of_child.strip()) device_data = dict(zip(device_data[::2], device_data[1::2])) if device_data.get('PushMagic'): self.push_magic = str(device_data.get('PushMagic')) if device_data.get('Token'): self.device_token = str(device_data.get('Token')) self.device_token = self.device_token.replace(' ', '+') self.device_token = self.device_token.replace('\n', '') self.device_token = self.device_token.replace('\t', '') # print len(self.device_token) # print self.device_token if device_data.get('MessageType'): message = device_data.get('MessageType') if message == 'TokenUpdate': self.do_entry = True elif message == 'Authenticate': self.do_initial_entry = True if device_data.get('UnlockToken'): self.unlock_token = device_data.get('UnlockToken') self.unlock_token = self.unlock_token.replace(' ', '+') self.unlock_token = self.unlock_token.replace('\n', '') self.unlock_token = self.unlock_token.replace('\t', '') if device_data.get('UDID'): self.udid = device_data.get('UDID') ### Initial Device DB Entries ### if self.do_entry: enrolled_success = False device_id = None # fetch info from enrollment table enrollment = EnrollmentDBHelper() enrollment_dict = enrollment.get_enrollment(enrollment_id) # print 'enrollment_dict = ' + str(enrollment_dict) if enrollment_dict is None: log.e(TAG, 'No user ID in Enrollment table. Enrollment ID = ' + str(enrollment_id)) reply = """ <html> <body>401</body> </html> """ self.request.set_status(401) self.request.write(reply) ioloop.IOLoop.instance().add_callback(self.callback) else: reply = """ <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> </dict> </plist> """ print 'inner write' self.request.write(reply) ioloop.IOLoop.instance().add_callback(self.callback) device = DeviceDBHelper() violation = ViolationsDBHelper() user_id = str(enrollment_dict[c.ENROLLMENT_TABLE_USER]) device_id = enrollment_dict.get(c.ENROLLMENT_TABLE_DEVICE) devices = device.get_device_with_udid( str(self.udid), status=True) # print "\n print devices list if available \n\n",devices device_detail = DeviceDetailsDBHelper() device_details_dict = { 'token': self.device_token, 'push_magic': self.push_magic, 'unlock_token': self.unlock_token } if device_id: enrolled_success = True device.update_device(str(device_id), {c.DEVICE_TABLE_DELETED: False, c.DEVICE_TABLE_UDID: str(self.udid)}) device_detail.update_device_details(str(device_id), device_details_dict) print "Device details table updated." elif devices: device_id = devices[0][c.DEVICE_TABLE_ID] device.update_device(str(device_id), {c.DEVICE_TABLE_DELETED: False}) device_detail.update_device_details(str(device_id), device_details_dict) enrollment.update_enrollment( enrollment_id, { c.ENROLLMENT_TABLE_DEVICE: str(device_id)}) enrollment.set_enrolled(enrollment_id) enrolled_success = True else: # print 'user_id = ' + user_id device_dict = {c.DEVICE_TABLE_USER: user_id, c.DEVICE_TABLE_OS: 'ios', c.DEVICE_TABLE_UDID: str(self.udid), c.DEVICE_TABLE_DELETED: False} device_id = device.add_device(device_dict) if device_id is None: log.e(TAG, 'Not Able to insert in Device table UDID = ' + str(self.udid) + 'userID = ' + str(user_id)) else: device_details_dict_new = {} device_details_dict_new[ c.DEVICE_DETAILS_TABLE_DEVICE] = device_id device_details_dict_new[ c.DEVICE_DETAILS_TABLE_EXTRAS] = device_details_dict device_details_id = device_detail.add_device_detail( device_details_dict_new) # print 'device_details_id = ' + str(device_details_id) if device_details_id is None: log.e(TAG, 'Not Able to insert in Device Details \ table UDID = ' + str(self.udid) + 'userID = ' + str(user_id) + 'DeviceID = ' + str(device_id)) else: success = enrollment.update_enrollment( enrollment_id, {c.ENROLLMENT_TABLE_DEVICE: str(device_id)}) if not success: log.e(TAG, 'enrollment device table not linked') else: success1 = enrollment.set_enrolled( enrollment_id) if success1: enrolled_success = True else: log.e( TAG, 'EnrolledOn time is not updated in \ the Enrollment Table') if device_id and enrolled_success: violation_status = violation.update_violations( str(device_id)) user = UserDBHelper() user_info = user.get_user(user_id) if violation_status: log.i(TAG, "Violation table updated for device_id" + str( device_id)) else: log.e( TAG, "Violation table not updated for device_id" + str(device_id)) ### Add task to Queue for celery Worker. ### json_data = {'to': 'user', 'action': 'device_information', 'id': user_id} json_data['company_id'] = user_info.get('company_id') create_command_handler_task.delay(json_data) ### Now send polling Signal to device ### wrapper = APNSNotificationWrapper( '/opt/toppatch/assets/ios/PushCert.pem', False) message = APNSNotification() message.appendProperty(APNSProperty("mdm", str(self.push_magic))) message.tokenBase64(str(self.device_token)) wrapper.append(message) wrapper.notify() print 'Payload Sent' elif device_data.get('MessageType') == 'CheckOut': reply = """ <html> <body>401</body> </html> """ self.request.set_status(401) self.request.write(reply) ioloop.IOLoop.instance().add_callback(self.callback) violation = ViolationsDBHelper() device = DeviceDBHelper() enrollment = EnrollmentDBHelper() devices = device.get_device_with_udid(self.udid) if devices is None: log.e(TAG, 'No User ID Associated with Device UDID = ' + self.udid) else: device_id = devices[0][c.DEVICE_TABLE_ID] violation_id = violation.add_violation(str(device_id)) if violation_id is None: log.e(TAG, 'Not able to insert in Violation Table.\ DeviceID = ' + str(device_id)) else: device.delete_device(device_id) enrollment.update_enrollment( str(enrollment_id), { 'device_id': "null", 'is_enrolled': False}) log.i(TAG, 'Violation added for device id = ' + str(device_id)) admin_mailer(device_id, violation_id) else: reply = """ <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> </dict> </plist> """ print 'outer write' self.request.write(reply) ioloop.IOLoop.instance().add_callback(self.callback)
def run(self): TAG = 'run' print ' In IOSCommandPerformer\'s PUT' command = IOSCommandDBHelper() invoke_flag = False arguments = cgi.parse_qsl(self.request.request.body) intermediate = arguments[0] currentxml = intermediate[1] final = currentxml[25:-1] # Original Line # final = currentxml[20:] ### For testing only # print '\n\n\n here is final xml \n\n', final # temp Parsing # tree = ET.ElementTree(ET.fromstring(final)) ##Original Line # tree = ET.parse('temp.xml') ### For testing only initial_list = [] initial_dict = {} check_out_requested = False send_command = False store_result = False store_error = False registered = True special_result = False acknowledged = False update_token = False object_root = objectify.fromstring(final) begin = object_root.dict for child in begin.iterchildren(): initial_list.append(child.text) initial_dict = dict(zip(initial_list[::2], initial_list[1::2])) # print '\n\n print here initial dict\n', initial_dict if 'UDID' in initial_dict: udid = initial_dict.get('UDID') device = DeviceDBHelper() registered = device.is_udid_registered(str(udid)) if 'Status' in initial_dict: status = initial_dict.get('Status') if status == 'Idle': send_command = True elif status == 'Acknowledged': acknowledged = True elif status == 'Error': store_error = True uuid = initial_dict.get('CommandUUID') if 'MessageType' in initial_dict: message = initial_dict.get('MessageType') if message == 'TokenUpdate': update_token = True elif message == 'CheckOut': check_out_requested = True push_magic = 'push_magic' token = 'Token' unlock_token = 'UnlockToken' if registered is False: reply = """ """ self.request.set_status(401) self.request.write(reply) ioloop.IOLoop.instance().add_callback(self.callback) else: device_id = str(device.get_device_with_udid(udid)[0]['id']) print 'reached here ' if acknowledged: if 'CommandUUID' in initial_dict: uuid = initial_dict.get('CommandUUID') # special uuid for device information if uuid == '55555555-5555-5555-5555-555555555555' + device_id: info_list = [ el.text if el.text else el.tag for el in object_root.iterdescendants() ] info_list.pop(0) final = dict(zip(info_list[::2], info_list[1::2])) store_result = True special_result = True # special uuid for installed application list elif uuid == ('77777777-7777-7777-7777-777777777777' + device_id): begin = object_root.dict.array apps_list = [] for outer_child in begin.iterchildren(): temp_list = [] for inner_child in outer_child.iterchildren(): temp_list.append(inner_child.text) apps_list.append( dict(zip(temp_list[::2], temp_list[1::2]))) final = apps_list store_result = True invoke_flag = True else: store_result = True if send_command: if udid is None: self.log.e(TAG, 'No UDID is supplied from the device') else: command_list = command.get_not_executed(str(device_id)) if command_list is None or len(command_list) == 0: self.log.e( TAG, 'No command to execute fot the device \ having ID = ' + str(device_id)) reply = " " print 'outer write' self.request.set_status(500) self.request.write(reply) ioloop.IOLoop.instance().add_callback(self.callback) else: device_details = DeviceDetailsDBHelper() violate_device = False special_uuid = ( '2929292929-29292929-292929-292929-292929' + str(device_id)) commands = command_list[0] action = str(commands[c.COMMAND_TABLE_ACTION]) command_attributes = commands[c.COMMAND_TABLE_ATTRIBUTE] command_uuid = str(commands[c.COMMAND_TABLE_COMMAND_UUID]) if command_uuid == special_uuid: violate_device = True if action is not None and not violate_device: command_xml_thread = IOSCommandCreatorThread( action, command_attributes, command_uuid, device_id) command_xml_thread.start() command_xml_thread.join() final_output = command_xml_thread.command_profile if final_output is not None: self.request.write(final_output) ioloop.IOLoop.instance().add_callback(self.callback) # send polling signal to device for next command device_details_list = ( device_details.get_device_details(device_id)) json_extras = device_details_list.get( c.DEVICE_DETAILS_TABLE_EXTRAS) self.push_magic = str(json_extras.get('push_magic')) self.device_token = str(json_extras.get('token')) wrapper = APNSNotificationWrapper( '/opt/toppatch/assets/ios/PushCert.pem', False) message = APNSNotification() message.appendProperty( APNSProperty("mdm", self.push_magic)) message.tokenBase64(str(self.device_token)) wrapper.append(message) wrapper.notify() print 'Payload Sent' elif violate_device: ### Wiping device to Factory Reset ### command_xml_thread = IOSCommandCreatorThread( action, command_attributes, command_uuid, device_id) command_xml_thread.start() command_xml_thread.join() final_output = command_xml_thread.command_profile if final_output is not None: self.request.write(final_output) ioloop.IOLoop.instance().add_callback(self.callback) violation = ViolationsDBHelper() enrollment = EnrollmentDBHelper() violation_id = violation.add_violation(str(device_id)) if violation_id is None: self.log.e( TAG, 'Not able to insert in Violation \ Table. DeviceID = ' + str(device_id)) else: device.delete_device(device_id) enrollment_list = enrollment.get_enrollments( {'device_id': device_id}) for enroll in enrollment_list: enrollment_id = enroll.get('id') enrollment.update_enrollment( str(enrollment_id), { 'device_id': "null", 'is_enrolled': False }) self.log.i( TAG, 'Violation added for device id = ' + str(device_id) + 'due to remote wipe command') else: reply = " " print 'outer write' self.request.set_status(500) self.request.write(reply) ioloop.IOLoop.instance().add_callback(self.callback) elif check_out_requested: violation = ViolationsDBHelper() device = DeviceDBHelper() devices = device.get_device_with_udid(udid) if devices is None: self.log.e(TAG, 'No User ID Associated with Device UDID = ' + udid) else: device_id = str(devices[0][c.DEVICE_TABLE_ID]) violation_id = violation.add_violation(device_id) if violation_id is None: self.log.e( TAG, 'Not able to insert in Violation Table.\ DeviceID = ' + str(device_id)) else: self.log.i( TAG, 'Violation added for device id = ' + str(device_id)) reply = """ """ self.request.set_status(401) self.request.write(reply) ioloop.IOLoop.instance().add_callback(self.callback) elif store_result or store_error: if special_result: is_updated = device.update_device( str(device_id), {c.DEVICE_TABLE_OS_VERSION: str(final.get('OSVersion'))}) if not is_updated: self.log.e( TAG, 'Not able to set the version of the device\ udid = ' + str(udid) + " Device id = " + str(device_id)) else: self.log.i( TAG, 'Version Set for the device udid = ' + str(udid) + " Device id = " + str(device_id)) if store_error: self.log.e( TAG, 'Error in Response for uuid = ' + str(uuid) + ' device_id = ' + str(device_id)) # print '\n\nfinal dict to be stored as json is \n\n', final result_updated = command.update_result(str(uuid), str(device_id), final) reply = """ <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> </dict> </plist> """ self.request.write(reply) ioloop.IOLoop.instance().add_callback(self.callback) if not result_updated: self.log.e( TAG, 'Result Not updated for uuid = ' + str(uuid) + ' udid = ' + str(udid)) else: executed = command.toggle_executed(str(uuid), str(device_id), True) if executed is False: self.log.e( TAG, 'IOSCommand Table executed \ field is not updated CommandUUID = ' + str(uuid) + 'Device id = ' + str(device_id)) self.log.i( TAG, 'Result send in DB for uuid = ' + str(uuid) + ' udid = ' + str(udid)) if invoke_flag: # Queue task to send command to user of app installation user = UserDBHelper() user_id = device.get_device_with_udid( str(udid))[0]['user_id'] user_info = user.get_user(str(user_id)) json_data = {'to': 'user', 'id': user_id} json_data['company_id'] = user_info.get('company_id') create_command_handler_task.delay(json_data) elif update_token: device = DeviceDBHelper() device_list = device.get_device_with_udid(udid) device_id = 'device_id' if device_list is not None and len(device_list) != 0: for devices in device_list: device_id = devices[c.DEVICE_TABLE_ID] if device_id is None: self.log.e( TAG, 'Device id is not found corresponding \ the udid = ' + str(udid)) else: device_details = DeviceDetailsDBHelper() device_details_dict = { c.DEVICE_DETAILS_TABLE_DEVICE_TOKEN: token, c.DEVICE_DETAILS_TABLE_PUSH_MAGIC: push_magic, c.DEVICE_DETAILS_TABLE_UNLOCK_TOKEN: unlock_token } is_updated = device_details.update_device_details( str(device_id), device_details_dict) if not is_updated: self.log.e( TAG, 'Not able to update the device \ details of device_id = ' + str(device_id)) else: self.log.i( TAG, 'Device Details successfully \ updated device id = ' + str(device_id)) reply = """ <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> </dict> </plist> """ self.request.write(reply) ioloop.IOLoop.instance().add_callback(self.callback) else: reply = """ <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> </dict> </plist> """ print 'outer write' self.request.write(reply) ioloop.IOLoop.instance().add_callback(self.callback)
def run(self): log = Logger('CompanyPutHandlerThread') TAG = 'run' # flag to track policy query status = False if self.data: # do stuff here company = CompanyDBHelper() url_data = self.data.split('/') request_data = json.loads(self.request.request.body) company_id = self.company_id # url_data[0] plugin_name = url_data[1] company_detail = company.get_company(str(company_id)) if company_detail: company_policy_id = company_detail.get('policy_id') command_handler_json = {'to': 'company', 'id': str(company_id), 'company_id': self.company_id} if plugin_name != 'actions': status = put_individual_plugin(company_policy_id, plugin_name, request_data) else: status = True command_handler_json['action'] = request_data.get('action') command_handler_json['passcode'] = request_data.get( 'lock_key') if status: create_command_handler_task.delay(command_handler_json) request_data['_id'] = company_policy_id request_data['object_type'] = 'Company' request_data['name'] = company_detail.get( c.COMPANY_TABLE_NAME) opJson = json.dumps( {'data': request_data, 'pass': True, 'count': 1, 'message': 'Everything fine'}) self.request.write(opJson) tornado.ioloop.IOLoop.instance().add_callback( self.callback) else: opJson = json.dumps( {'pass': False, 'count': 0, 'message': 'Update operation at policy table \ failed'}) self.request.write(opJson) tornado.ioloop.IOLoop.instance().add_callback( self.callback) else: log.e(TAG, 'No valid company id is sent in request') opJson = json.dumps( {'pass': False, 'message': 'No valid company id is sent in request'}) self.request.write(opJson) tornado.ioloop.IOLoop.instance().add_callback(self.callback) else: log.e(TAG, 'UnAuthorized Access for company policy ') self.request.set_status(401) tornado.ioloop.IOLoop.instance().add_callback(self.callback)
def run(self): log = Logger('RolePolicyPutHandlerThread') TAG = 'run' # Flag to track status of policy status = False if self.data: # do stuff here role = RoleDBHelper() url_data = self.data.split('/') print self.request.request.body request_data = json.loads(self.request.request.body) role_id = url_data[0] plugin_name = url_data[1] role_detail = role.get_role(str(role_id), company_id=self.company_id) if role_detail: role_policy_id = role_detail.get('policy_id') command_handler_json = {'to': 'role', 'id': str(role_id), 'company_id': self.company_id} if plugin_name != 'actions': status = put_individual_plugin(role_policy_id, plugin_name, request_data) else: status = True command_handler_json['action'] = request_data.get('action') command_handler_json[ 'passcode'] = request_data.get('lock_key') if status: print "\nprinting the json output will be send to command\ handler\n", command_handler_json create_command_handler_task.delay(command_handler_json) request_data['_id'] = role_policy_id request_data['object_type'] = 'Role' request_data['name'] = role_detail.get(c.ROLE_TABLE_NAME) opJson = json.dumps( {'data': request_data, 'pass': True, 'count': 1, 'message': 'Everything fine'}) self.request.write(opJson) tornado.ioloop.IOLoop.instance().add_callback( self.callback) else: opJson = json.dumps( {'pass': False, 'count': 0, 'message': 'Update operation at policy table \ failed'}) self.request.write(opJson) tornado.ioloop.IOLoop.instance().add_callback( self.callback) else: log.e(TAG, 'No valid role id is sent in request') opJson = json.dumps( {'pass': False, 'message': 'No valid role id is sent in request'}) self.request.write(opJson) tornado.ioloop.IOLoop.instance().add_callback(self.callback) else: log.e(TAG, 'UnAuthorized Access for role policy ') self.request.set_status(401) tornado.ioloop.IOLoop.instance().add_callback(self.callback)
def run(self): TAG = 'run' print ' In IOSCommandPerformer\'s PUT' command = IOSCommandDBHelper() invoke_flag = False arguments = cgi.parse_qsl(self.request.request.body) intermediate = arguments[0] currentxml = intermediate[1] final = currentxml[25:-1] # Original Line # final = currentxml[20:] ### For testing only # print '\n\n\n here is final xml \n\n', final # temp Parsing # tree = ET.ElementTree(ET.fromstring(final)) ##Original Line # tree = ET.parse('temp.xml') ### For testing only initial_list = [] initial_dict = {} check_out_requested = False send_command = False store_result = False store_error = False registered = True special_result = False acknowledged = False update_token = False object_root = objectify.fromstring(final) begin = object_root.dict for child in begin.iterchildren(): initial_list.append(child.text) initial_dict = dict(zip(initial_list[::2], initial_list[1::2])) # print '\n\n print here initial dict\n', initial_dict if 'UDID' in initial_dict: udid = initial_dict.get('UDID') device = DeviceDBHelper() registered = device.is_udid_registered(str(udid)) if 'Status' in initial_dict: status = initial_dict.get('Status') if status == 'Idle': send_command = True elif status == 'Acknowledged': acknowledged = True elif status == 'Error': store_error = True uuid = initial_dict.get('CommandUUID') if 'MessageType' in initial_dict: message = initial_dict.get('MessageType') if message == 'TokenUpdate': update_token = True elif message == 'CheckOut': check_out_requested = True push_magic = 'push_magic' token = 'Token' unlock_token = 'UnlockToken' if registered is False: reply = """ """ self.request.set_status(401) self.request.write(reply) ioloop.IOLoop.instance().add_callback(self.callback) else: device_id = str(device.get_device_with_udid(udid)[0]['id']) print 'reached here ' if acknowledged: if 'CommandUUID' in initial_dict: uuid = initial_dict.get('CommandUUID') # special uuid for device information if uuid == '55555555-5555-5555-5555-555555555555' + device_id: info_list = [ el.text if el.text else el.tag for el in object_root.iterdescendants()] info_list.pop(0) final = dict(zip(info_list[::2], info_list[1::2])) store_result = True special_result = True # special uuid for installed application list elif uuid == ( '77777777-7777-7777-7777-777777777777' + device_id): begin = object_root.dict.array apps_list = [] for outer_child in begin.iterchildren(): temp_list = [] for inner_child in outer_child.iterchildren(): temp_list.append(inner_child.text) apps_list.append(dict(zip(temp_list[::2], temp_list[1::2]))) final = apps_list store_result = True invoke_flag = True else: store_result = True if send_command: if udid is None: self.log.e(TAG, 'No UDID is supplied from the device') else: command_list = command.get_not_executed(str(device_id)) if command_list is None or len(command_list) == 0: self.log.e(TAG, 'No command to execute fot the device \ having ID = ' + str(device_id)) reply = " " print 'outer write' self.request.set_status(500) self.request.write(reply) ioloop.IOLoop.instance().add_callback(self.callback) else: device_details = DeviceDetailsDBHelper() violate_device = False special_uuid = ( '2929292929-29292929-292929-292929-292929' + str(device_id)) commands = command_list[0] action = str(commands[c.COMMAND_TABLE_ACTION]) command_attributes = commands[c.COMMAND_TABLE_ATTRIBUTE] command_uuid = str(commands[c.COMMAND_TABLE_COMMAND_UUID]) if command_uuid == special_uuid: violate_device = True if action is not None and not violate_device: command_xml_thread = IOSCommandCreatorThread( action, command_attributes, command_uuid, device_id) command_xml_thread.start() command_xml_thread.join() final_output = command_xml_thread.command_profile if final_output is not None: self.request.write(final_output) ioloop.IOLoop.instance().add_callback(self.callback) # send polling signal to device for next command device_details_list = ( device_details.get_device_details(device_id)) json_extras = device_details_list.get( c.DEVICE_DETAILS_TABLE_EXTRAS) self.push_magic = str(json_extras.get('push_magic')) self.device_token = str(json_extras.get('token')) wrapper = APNSNotificationWrapper( '/opt/toppatch/assets/ios/PushCert.pem', False) message = APNSNotification() message.appendProperty( APNSProperty( "mdm", self.push_magic)) message.tokenBase64(str(self.device_token)) wrapper.append(message) wrapper.notify() print 'Payload Sent' elif violate_device: ### Wiping device to Factory Reset ### command_xml_thread = IOSCommandCreatorThread( action, command_attributes, command_uuid, device_id) command_xml_thread.start() command_xml_thread.join() final_output = command_xml_thread.command_profile if final_output is not None: self.request.write(final_output) ioloop.IOLoop.instance().add_callback(self.callback) violation = ViolationsDBHelper() enrollment = EnrollmentDBHelper() violation_id = violation.add_violation(str(device_id)) if violation_id is None: self.log.e(TAG, 'Not able to insert in Violation \ Table. DeviceID = ' + str(device_id)) else: device.delete_device(device_id) enrollment_list = enrollment.get_enrollments({ 'device_id': device_id}) for enroll in enrollment_list: enrollment_id = enroll.get('id') enrollment.update_enrollment( str(enrollment_id), { 'device_id': "null", 'is_enrolled': False}) self.log.i( TAG, 'Violation added for device id = ' + str(device_id) + 'due to remote wipe command') else: reply = " " print 'outer write' self.request.set_status(500) self.request.write(reply) ioloop.IOLoop.instance().add_callback(self.callback) elif check_out_requested: violation = ViolationsDBHelper() device = DeviceDBHelper() devices = device.get_device_with_udid(udid) if devices is None: self.log.e(TAG, 'No User ID Associated with Device UDID = ' + udid) else: device_id = str(devices[0][c.DEVICE_TABLE_ID]) violation_id = violation.add_violation(device_id) if violation_id is None: self.log.e(TAG, 'Not able to insert in Violation Table.\ DeviceID = ' + str(device_id)) else: self.log.i(TAG, 'Violation added for device id = ' + str(device_id)) reply = """ """ self.request.set_status(401) self.request.write(reply) ioloop.IOLoop.instance().add_callback(self.callback) elif store_result or store_error: if special_result: is_updated = device.update_device( str(device_id), { c.DEVICE_TABLE_OS_VERSION: str( final.get('OSVersion'))}) if not is_updated: self.log.e(TAG, 'Not able to set the version of the device\ udid = ' + str(udid) + " Device id = " + str(device_id)) else: self.log.i(TAG, 'Version Set for the device udid = ' + str(udid) + " Device id = " + str(device_id)) if store_error: self.log.e(TAG, 'Error in Response for uuid = ' + str(uuid) + ' device_id = ' + str(device_id)) # print '\n\nfinal dict to be stored as json is \n\n', final result_updated = command.update_result(str(uuid), str(device_id), final) reply = """ <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> </dict> </plist> """ self.request.write(reply) ioloop.IOLoop.instance().add_callback(self.callback) if not result_updated: self.log.e(TAG, 'Result Not updated for uuid = ' + str(uuid) + ' udid = ' + str(udid)) else: executed = command.toggle_executed(str(uuid), str(device_id), True) if executed is False: self.log.e(TAG, 'IOSCommand Table executed \ field is not updated CommandUUID = ' + str(uuid) + 'Device id = ' + str(device_id)) self.log.i(TAG, 'Result send in DB for uuid = ' + str(uuid) + ' udid = ' + str(udid)) if invoke_flag: # Queue task to send command to user of app installation user = UserDBHelper() user_id = device.get_device_with_udid(str( udid))[0]['user_id'] user_info = user.get_user(str(user_id)) json_data = {'to': 'user', 'id': user_id} json_data['company_id'] = user_info.get('company_id') create_command_handler_task.delay(json_data) elif update_token: device = DeviceDBHelper() device_list = device.get_device_with_udid(udid) device_id = 'device_id' if device_list is not None and len(device_list) != 0: for devices in device_list: device_id = devices[c.DEVICE_TABLE_ID] if device_id is None: self.log.e(TAG, 'Device id is not found corresponding \ the udid = ' + str(udid)) else: device_details = DeviceDetailsDBHelper() device_details_dict = { c.DEVICE_DETAILS_TABLE_DEVICE_TOKEN: token, c.DEVICE_DETAILS_TABLE_PUSH_MAGIC: push_magic, c.DEVICE_DETAILS_TABLE_UNLOCK_TOKEN: unlock_token } is_updated = device_details.update_device_details( str(device_id), device_details_dict) if not is_updated: self.log.e(TAG, 'Not able to update the device \ details of device_id = ' + str(device_id)) else: self.log.i(TAG, 'Device Details successfully \ updated device id = ' + str(device_id)) reply = """ <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> </dict> </plist> """ self.request.write(reply) ioloop.IOLoop.instance().add_callback(self.callback) else: reply = """ <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> </dict> </plist> """ print 'outer write' self.request.write(reply) ioloop.IOLoop.instance().add_callback(self.callback)