Пример #1
0
 def test_lone_surrogates(self):
     for fmt in ALL_FORMATS:
         with self.subTest(fmt=fmt):
             with self.assertRaises(UnicodeEncodeError):
                 plistlib.dumps('\ud8ff', fmt=fmt)
             with self.assertRaises(UnicodeEncodeError):
                 plistlib.dumps('\udcff', fmt=fmt)
    def test_int(self):
        for pl in [
            0,
            2 ** 8 - 1,
            2 ** 8,
            2 ** 16 - 1,
            2 ** 16,
            2 ** 32 - 1,
            2 ** 32,
            2 ** 63 - 1,
            2 ** 64 - 1,
            1,
            -2 ** 63,
        ]:
            for fmt in ALL_FORMATS:
                with self.subTest(pl=pl, fmt=fmt):
                    data = plistlib.dumps(pl, fmt=fmt)
                    pl2 = plistlib.loads(data)
                    self.assertIsInstance(pl2, int)
                    self.assertEqual(pl, pl2)
                    data2 = plistlib.dumps(pl2, fmt=fmt)
                    self.assertEqual(data, data2)

        for fmt in ALL_FORMATS:
            for pl in (2 ** 64 + 1, 2 ** 127 - 1, -2 ** 64, -2 ** 127):
                with self.subTest(pl=pl, fmt=fmt):
                    self.assertRaises(OverflowError, plistlib.dumps, pl, fmt=fmt)
Пример #3
0
 def test_bytes(self):
     pl = self._create()
     data = plistlib.dumps(pl)
     pl2 = plistlib.loads(data)
     self.assertEqual(dict(pl), dict(pl2))
     data2 = plistlib.dumps(pl2)
     self.assertEqual(data, data2)
Пример #4
0
 def test_bytes(self):
     pl = self._create()
     data = plistlib.dumps(pl)
     pl2 = plistlib.loads(data)
     self.assertNotIsInstance(pl, plistlib._InternalDict)
     self.assertEqual(dict(pl), dict(pl2))
     data2 = plistlib.dumps(pl2)
     self.assertEqual(data, data2)
Пример #5
0
 def test_nondictroot(self):
     for fmt in ALL_FORMATS:
         with self.subTest(fmt=fmt):
             test1 = "abc"
             test2 = [1, 2, 3, "abc"]
             result1 = plistlib.loads(plistlib.dumps(test1, fmt=fmt))
             result2 = plistlib.loads(plistlib.dumps(test2, fmt=fmt))
             self.assertEqual(test1, result1)
             self.assertEqual(test2, result2)
 def test_controlcharacters(self):
     for i in range(128):
         c = chr(i)
         testString = "string containing %s" % c
         if i >= 32 or c in "\r\n\t":
             # \r, \n and \t are the only legal control chars in XML
             plistlib.dumps(testString, fmt=plistlib.FMT_XML)
         else:
             self.assertRaises(ValueError, plistlib.dumps, testString)
Пример #7
0
 def test_bytearray(self):
     for pl in (b'<binary gunk>', b"<lots of binary gunk>\0\1\2\3" * 10):
         for fmt in ALL_FORMATS:
             with self.subTest(pl=pl, fmt=fmt):
                 data = plistlib.dumps(bytearray(pl), fmt=fmt)
                 pl2 = plistlib.loads(data)
                 self.assertIsInstance(pl2, bytes)
                 self.assertEqual(pl2, pl)
                 data2 = plistlib.dumps(pl2, fmt=fmt)
                 self.assertEqual(data, data2)
Пример #8
0
 def test_controlcharacters(self):
     for i in range(128):
         c = chr(i)
         testString = "string containing %s" % c
         if i >= 32 or c in "\r\n\t":
             # \r, \n and \t are the only legal control chars in XML
             data = plistlib.dumps(testString, fmt=plistlib.FMT_XML)
             if c != "\r":
                 self.assertEqual(plistlib.loads(data), testString)
         else:
             with self.assertRaises(ValueError):
                 plistlib.dumps(testString, fmt=plistlib.FMT_XML)
         plistlib.dumps(testString, fmt=plistlib.FMT_BINARY)
Пример #9
0
def manifest(environ, start_response):
    path = environ.get('PATH_INFO').lstrip('/')
    base, _ = os.path.splitext(path)
    ipa_path = base + '.ipa'
    if not os.path.isfile(ipa_path):
        return not_found(environ, start_response)
    info = _get_ipa_info(ipa_path)
    plist = plistlib.dumps({
        'items': [
            {
                'assets': [
                    {
                        'kind': 'software-package',
                        'url': environ.get('REQUEST_URI', ''),
                    },
                ],
                'metadata': {
                    'bundle-identifier': info.get('CFBundleIdentifier', ''),
                    'bundle-version': info.get('CFBundleVersion', ''),
                    'kind': 'software',
                    'subtitle': _get_modified(ipa_path),
                    'title': info.get('CFBundleName', ''),
                },
            },
        ],
    })
    status = '200 OK'
    start_response(status, [('Content-Type', 'application/x-plist')])
    return [plist]
Пример #10
0
 def sendpacket(self, req, tag, payload={}):
     payload['ClientVersionString'] = 'usbmux.py by marcan'
     if isinstance(req, int):
         req = [self.TYPE_CONNECT, self.TYPE_LISTEN][req-2]
     payload['MessageType'] = req
     payload['ProgName'] = 'tcprelay'
     BinaryProtocol.sendpacket(self, self.TYPE_PLIST, tag, plistlib.dumps(payload))
Пример #11
0
 def test_large_timestamp(self):
     # Issue #26709: 32-bit timestamp out of range
     for ts in -2 ** 31 - 1, 2 ** 31:
         with self.subTest(ts=ts):
             d = datetime.datetime.utcfromtimestamp(0) + datetime.timedelta(seconds=ts)
             data = plistlib.dumps(d, fmt=plistlib.FMT_BINARY)
             self.assertEqual(plistlib.loads(data), d)
Пример #12
0
 def test_cycles(self):
     # recursive list
     a = []
     a.append(a)
     b = plistlib.loads(plistlib.dumps(a, fmt=plistlib.FMT_BINARY))
     self.assertIs(b[0], b)
     # recursive tuple
     a = ([],)
     a[0].append(a)
     b = plistlib.loads(plistlib.dumps(a, fmt=plistlib.FMT_BINARY))
     self.assertIs(b[0][0], b)
     # recursive dict
     a = {}
     a['x'] = a
     b = plistlib.loads(plistlib.dumps(a, fmt=plistlib.FMT_BINARY))
     self.assertIs(b['x'], b)
Пример #13
0
 def test_appleformatting(self):
     for use_builtin_types in (True, False):
         for fmt in ALL_FORMATS:
             with self.subTest(fmt=fmt, use_builtin_types=use_builtin_types):
                 pl = plistlib.loads(TESTDATA[fmt], use_builtin_types=use_builtin_types)
                 data = plistlib.dumps(pl, fmt=fmt)
                 self.assertEqual(data, TESTDATA[fmt], "generated data was not identical to Apple's output")
Пример #14
0
    def generate(self):
        """
        Generate an plist file from a PropertyList object
        """
        recipe = self.to_mongo().to_dict()
        plist = recipe['plist']

        return dumps(plist)
Пример #15
0
    def test_dict_members(self):
        pl = {"first": {"a": 1}, "second": {"a": 1}, "third": {"b": 2}}

        for fmt in ALL_FORMATS:
            with self.subTest(fmt=fmt):
                data = plistlib.dumps(pl, fmt=fmt)
                pl2 = plistlib.loads(data)
                self.assertEqual(pl2, {"first": {"a": 1}, "second": {"a": 1}, "third": {"b": 2}})
                self.assertIsNot(pl2["first"], pl2["second"])
Пример #16
0
    def test_tuple_members(self):
        pl = {"first": (1, 2), "second": (1, 2), "third": (3, 4)}

        for fmt in ALL_FORMATS:
            with self.subTest(fmt=fmt):
                data = plistlib.dumps(pl, fmt=fmt)
                pl2 = plistlib.loads(data)
                self.assertEqual(pl2, {"first": [1, 2], "second": [1, 2], "third": [3, 4]})
                self.assertIsNot(pl2["first"], pl2["second"])
Пример #17
0
    def update(self, view):
        if not self.need_update:
            return
        self.need_update = False

        content = self.color_scheme.content()
        current_colors = set("#%s" % c.upper() for c in re.findall(r'\b%s([a-fA-F0-9]{8})\b' % self.prefix, content))

        bg_col = self.get_background_col(view)

        rules = []
        if not re.search(r'\b%sgutter\b' % self.prefix, content):
            rules.append({
                "scope": "%sgutter" % self.prefix,
                "background": "#000000",
                "foreground": "#ffffff",
            })
        for col, name in self.colors.items():
            if col not in current_colors:
                fg_col = self.get_inv_col(bg_col, col)
                rules.append({
                    "scope": name,
                    "background": col,
                    "foreground": fg_col,
                })

        if rules:
            try:
                # For sublime-color-scheme
                m = re.search(r'([\t ]*)"rules":\s*\[[\r\n]*', content)
                if m:
                    json_rules = json.dumps({"rules": rules}, indent=m.group(1))
                    json_rules = '\n'.join(map(str.rstrip, json_rules.split('\n')[2:-2])) + ',\n'
                    content = content[:m.end()] + json_rules + content[m.end():]
                    write_package(self.color_scheme.path, content)
                    log.debug("Updated sublime-color-scheme")
                    return

                # for tmTheme
                if re.match(r'\s*<(?:\?xml|!DOCTYPE|plist)\b', content):
                    plist_content = plistlib.loads(content.encode('utf-8'))
                    plist_content['settings'].extend({
                        "scope": r['scope'],
                        "settings": {
                            "foreground": r['foreground'],
                            "background": r['background'],
                        }
                    } for r in rules)
                    content = plistlib.dumps(plist_content).decode('utf-8')
                    write_package(self.color_scheme.path, content)
                    log.debug("Updated tmTheme")
                    return

                log.error("Not Updated: Schema format not recognized")
            except Exception as e:
                import traceback; traceback.print_exc();
                log.error("Not Updated: %r" % e)
Пример #18
0
 def test_dump_duplicates(self):
     # Test effectiveness of saving duplicated objects
     for x in (None, False, True, 12345, 123.45, 'abcde', b'abcde',
               datetime.datetime(2004, 10, 26, 10, 33, 33),
               plistlib.Data(b'abcde'), bytearray(b'abcde'),
               [12, 345], (12, 345), {'12': 345}):
         with self.subTest(x=x):
             data = plistlib.dumps([x]*1000, fmt=plistlib.FMT_BINARY)
             self.assertLess(len(data), 1100, repr(data))
Пример #19
0
 def test_plist(self):
     payload = {'id': str(uuid.uuid4())}
     if hasattr(plistlib, 'writePlistToString'):
         body = plistlib.writePlistToString(payload)
     elif hasattr(plistlib, 'dumps'):
         body = plistlib.dumps(payload)
     else:
         raise AssertionError('Missing plistlib method')
     yield self.process_message(body, 'application/x-plist')
     self.assertDictEqual(self.consumer.body, payload)
Пример #20
0
    def render(self):
        """Return the XML result of the view as a character string.
        """
        self.__build_xml_plist_tree()

        if self.__xml is not None:
            return etree.tostring(self.__xml,
                                  xml_declaration=True,
                                  method="xml",
                                  encoding="utf-8",
                                  pretty_print=True)

        elif self.__plist is not None:
            plist_unsigned = dumps(self.__plist, fmt=FMT_XML)

            if "sign_mobileconfig" in self.__model.domain:
                if (self.__model.domain["sign_mobileconfig"] is True and
                        "sign_cert" in self.__model.domain and
                        "sign_key" in self.__model.domain):

                    sign_cert = self.__model.domain["sign_cert"]
                    sign_key = self.__model.domain["sign_key"]
                    if "sign_more_certs" in self.__model.domain:
                        extra = " -certfile " + self.__model.domain[
                            "sign_more_certs"]
                    else:
                        extra = ""

                    import subprocess as s

                    # TODO: Do we need intermediate-CAs?
                    cmd = self.__model.openssl +\
                          " smime -sign -nodetach -outform der -aes-256-cbc"\
                          " -signer " + sign_cert + " -inkey " + sign_key +\
                          extra
                    process = s.Popen(
                        cmd.split(), stdin=s.PIPE, stdout=s.PIPE, stderr=s.PIPE)

                    plist_signed, errors = process.communicate(
                        input=plist_unsigned)
                    if errors is not None:
                        logging.warning("openssl: %s", str(errors))

                    return plist_signed
                else:
                    logging.info("Not signing!")

            return plist_unsigned
        else:
            return ""

# vim: expandtab ts=4 sw=4
Пример #21
0
 def test_identity(self):
     for x in (None, False, True, 12345, 123.45, 'abcde', b'abcde',
               datetime.datetime(2004, 10, 26, 10, 33, 33),
               plistlib.Data(b'abcde'), bytearray(b'abcde'),
               [12, 345], (12, 345), {'12': 345}):
         with self.subTest(x=x):
             data = plistlib.dumps([x]*2, fmt=plistlib.FMT_BINARY)
             a, b = plistlib.loads(data)
             if isinstance(x, tuple):
                 x = list(x)
             self.assertEqual(a, x)
             self.assertEqual(b, x)
             self.assertIs(a, b)
Пример #22
0
    def _dump_plist_value(value):
        """Create a plist value from a dictionary

        :param dict value: The value to make the plist from
        :rtype: dict

        """
        if hasattr(plistlib, 'dumps'):
            return plistlib.dumps(value)
        try:
            return plistlib.writePlistToString(value)
        except AttributeError:
            return plistlib.writePlistToBytes(value)
Пример #23
0
    def dumps(self, serializable_object):

        import plistlib

        try:
            if sys.version_info < (3,):
                plist_str = plistlib.writePlistToString(serializable_object)
            else:
                plist_str = plistlib.dumps(serializable_object)
        except Exception as exp:
            raise exception.SerializationFailedError("XMLPlist: %s" % exp)

        return plist_str
Пример #24
0
def writePlistToString(rootObject, binary=True):
    if not binary:
        rootObject = wrapDataObject(rootObject, binary)
        if hasattr(plistlib, "dumps"):
            return plistlib.dumps(rootObject)
        elif hasattr(plistlib, "writePlistToBytes"):
            return plistlib.writePlistToBytes(rootObject)
        else:
            return plistlib.writePlistToString(rootObject)
    else:
        ioObject = io.BytesIO()
        writer = PlistWriter(ioObject)
        writer.writeRoot(rootObject)
        return ioObject.getvalue()
Пример #25
0
def api_v1(request):
    if request.method != 'POST':
        return not_allowed('This endpoint only accepts POST', ['POST'])

    try:
        query = json.loads(request.body.decode())
    except json.decoder.JSONDecodeError:
        return bad_request('Could not parse JSON')

    try:
        ident_raw = query['app_id']
    except KeyError:
        return bad_request('Key app_id was not present')

    ident = Identifier.objects.filter(value=ident_raw)
    if not ident:
        return bad_request('No app found with identifier "{}"'.format(ident_raw))

    app = ident[0].app
    keys = app.typed_keys()

    try:
        fmt = query['format']
    except KeyError:
        return jsonify(keys)

    if fmt == 'json':
        return jsonify(keys)

    elif fmt == 'json_annotated':
        # Store key-value pairs in "values"
        typed = {'values': keys}

        # Add type annotations and store in "types"
        annotations = {}
        for pair in app.key_set.all():
            key = pair.key
            ktype_raw = pair.datatype
            ktype = DATATYPE_SLUGS[ktype_raw]
            annotations[key] = ktype
        typed['types'] = annotations

        return jsonify(typed)

    elif fmt == 'plist':
        return HttpResponse(content=plistlib.dumps(keys), content_type='application/x-plist')

    else:
        return bad_request('Unsupported format "{}"'.format(fmt))
Пример #26
0
    def test_skipkeys(self):
        pl = {42: "aNumber", "snake": "aWord"}

        for fmt in ALL_FORMATS:
            with self.subTest(fmt=fmt):
                data = plistlib.dumps(pl, fmt=fmt, skipkeys=True, sort_keys=False)

                pl2 = plistlib.loads(data)
                self.assertEqual(pl2, {"snake": "aWord"})

                fp = BytesIO()
                plistlib.dump(pl, fp, fmt=fmt, skipkeys=True, sort_keys=False)
                data = fp.getvalue()
                pl2 = plistlib.loads(fp.getvalue())
                self.assertEqual(pl2, {"snake": "aWord"})
Пример #27
0
    def test_keysort(self):
        pl = collections.OrderedDict()
        pl['b'] = 1
        pl['a'] = 2
        pl['c'] = 3

        for fmt in ALL_FORMATS:
            for sort_keys in (False, True):
                with self.subTest(fmt=fmt, sort_keys=sort_keys):
                    data = plistlib.dumps(pl, fmt=fmt, sort_keys=sort_keys)
                    pl2 = plistlib.loads(data, dict_type=collections.OrderedDict)

                    self.assertEqual(dict(pl), dict(pl2))
                    if sort_keys:
                        self.assertEqual(list(pl2.keys()), ['a', 'b', 'c'])
                    else:
                        self.assertEqual(list(pl2.keys()), ['b', 'a', 'c'])
Пример #28
0
 def RemoveProfile(self, ident):
     profiles = self.GetProfileList()
     if not profiles:
         return
     if ident not in profiles["ProfileMetadata"]:
         print("Trying to remove not installed profile %s" % ident)
         return
     meta = profiles["ProfileMetadata"][ident]
     pprint(meta)
     data = plistlib.dumps({"PayloadType": "Configuration",
                            "PayloadIdentifier": ident,
                            "PayloadUUID": meta["PayloadUUID"],
                            "PayloadVersion": meta["PayloadVersion"]
                            })
     self.service.sendPlist(
         {"RequestType": "RemoveProfile", "ProfileIdentifier": plistlib.Data(data)})
     return self.service.recvPlist()
Пример #29
0
    def test_dict_members(self):
        pl = {
            'first': {'a': 1},
            'second': {'a': 1},
            'third': {'b': 2 },
        }

        for fmt in ALL_FORMATS:
            with self.subTest(fmt=fmt):
                data = plistlib.dumps(pl, fmt=fmt)
                pl2 = plistlib.loads(data)
                self.assertEqual(pl2, {
                    'first': {'a': 1},
                    'second': {'a': 1},
                    'third': {'b': 2 },
                })
                self.assertIsNot(pl2['first'], pl2['second'])
Пример #30
0
    def test_tuple_members(self):
        pl = {
            'first': (1, 2),
            'second': (1, 2),
            'third': (3, 4),
        }

        for fmt in ALL_FORMATS:
            with self.subTest(fmt=fmt):
                data = plistlib.dumps(pl, fmt=fmt)
                pl2 = plistlib.loads(data)
                self.assertEqual(pl2, {
                    'first': [1, 2],
                    'second': [1, 2],
                    'third': [3, 4],
                })
                self.assertIsNot(pl2['first'], pl2['second'])
Пример #31
0
    def test_dataobject_deprecated(self):
        in_data = { 'key': plistlib.Data(b'hello') }
        out_data = { 'key': b'hello' }

        buf = plistlib.dumps(in_data)

        cur = plistlib.loads(buf)
        self.assertEqual(cur, out_data)
        self.assertEqual(cur, in_data)

        cur = plistlib.loads(buf, use_builtin_types=False)
        self.assertEqual(cur, out_data)
        self.assertEqual(cur, in_data)

        with self.assertWarns(DeprecationWarning):
            cur = plistlib.readPlistFromBytes(buf)
        self.assertEqual(cur, out_data)
        self.assertEqual(cur, in_data)
Пример #32
0
    def test_tuple_members(self):
        pl = {
            'first': (1, 2),
            'second': (1, 2),
            'third': (3, 4),
        }

        for fmt in ALL_FORMATS:
            with self.subTest(fmt=fmt):
                data = plistlib.dumps(pl, fmt=fmt)
                pl2 = plistlib.loads(data)
                self.assertEqual(pl2, {
                    'first': [1, 2],
                    'second': [1, 2],
                    'third': [3, 4],
                })
                if fmt != plistlib.FMT_BINARY:
                    self.assertIsNot(pl2['first'], pl2['second'])
Пример #33
0
    def test_keysort(self):
        pl = collections.OrderedDict()
        pl['b'] = 1
        pl['a'] = 2
        pl['c'] = 3

        for fmt in ALL_FORMATS:
            for sort_keys in (False, True):
                with self.subTest(fmt=fmt, sort_keys=sort_keys):
                    data = plistlib.dumps(pl, fmt=fmt, sort_keys=sort_keys)
                    pl2 = plistlib.loads(data,
                                         dict_type=collections.OrderedDict)

                    self.assertEqual(dict(pl), dict(pl2))
                    if sort_keys:
                        self.assertEqual(list(pl2.keys()), ['a', 'b', 'c'])
                    else:
                        self.assertEqual(list(pl2.keys()), ['b', 'a', 'c'])
Пример #34
0
    def handle_ask(self):
        content_length = int(self.headers['Content-Length'])
        post_data = self.rfile.read(content_length)

        AirDropUtil.write_debug(self.config, post_data,
                                'receive_ask_request.plist')

        ask_response = {
            'ReceiverModelName': self.config.computer_model,
            'ReceiverComputerName': self.config.computer_name
        }
        ask_resp_binary = plistlib.dumps(ask_response, fmt=plistlib.FMT_BINARY)

        AirDropUtil.write_debug(self.config, ask_resp_binary,
                                'receive_ask_response.plist')

        self._set_response(len(ask_resp_binary))
        self.wfile.write(ask_resp_binary)
Пример #35
0
    def dumps(self) -> str:
        data = {
            'WFWorkflowActions':
            self.shortcut._get_actions(),
            'WFWorkflowImportQuestions':
            self.shortcut._get_import_questions(),
            'WFWorkflowClientRelease':
            self.shortcut.client_release,
            'WFWorkflowClientVersion':
            self.shortcut.client_version,
            'WFWorkflowTypes': ['NCWidget', 'WatchKit'],  # todo: change me
            'WFWorkflowIcon':
            self.shortcut._get_icon(),
            'WFWorkflowInputContentItemClasses':
            self.shortcut._get_input_content_item_classes(),
        }

        return plistlib.dumps(data).decode('utf-8')
Пример #36
0
    def send_packet(self, payload: dict, reqtype: int = 8):
        """
        Args:
            payload: required

            # The following args only used in the first request
            reqtype: request type, always 8 
            tag: int
        """
        body_data = plistlib.dumps(payload)
        if self._first:  # first package
            length = 16 + len(body_data)
            header = struct.pack(
                "IIII", length, 1, reqtype,
                self._tag)  # version: 1, request: 8(?), tag: 1(?)
        else:
            header = struct.pack(">I", len(body_data))
        self.sendall(header + body_data)
Пример #37
0
    def pair(self):
        self.DevicePublicKey = self.getValue("", "DevicePublicKey")
        if self.DevicePublicKey == '':
            print("Unable to retreive DevicePublicKey")
            return False

        print("Creating host key & certificate")
        certPem, privateKeyPem, DeviceCertificate = ca_do_everything(
            self.DevicePublicKey)

        pair_record = {
            "DevicePublicKey": plistlib.Data(self.DevicePublicKey),
            "DeviceCertificate": plistlib.Data(DeviceCertificate),
            "HostCertificate": plistlib.Data(certPem),
            "HostID": self.hostID,
            "RootCertificate": plistlib.Data(certPem),
            "SystemBUID": "30142955-444094379208051516"
        }

        pair = {
            "Label": self.label,
            "Request": "Pair",
            "PairRecord": pair_record
        }
        self.c = PlistService(62078, self.udid)
        self.c.sendPlist(pair)
        pair = self.c.recvPlist()

        if pair and pair.get("Result") == "Success" or "EscrowBag" in pair:
            pair_record["HostPrivateKey"] = plistlib.Data(privateKeyPem)
            pair_record["EscrowBag"] = pair.get("EscrowBag")
            writeHomeFile(HOMEFOLDER, "%s.plist" % self.identifier,
                          plistlib.dumps(pair_record))
            self.paired = True
            return True

        elif pair and pair.get("Error") == "PasswordProtected":
            self.c.close()
            raise NotTrustedError

        else:
            print(pair.get("Error"))
            self.c.close()
            raise PairingError
Пример #38
0
def _generate_shortcut_macos(name: str, working_dir: str, path: str,
                             executable: Union[str, bool], icon: str) -> str:
    """The steps to generate an icon for windows

    Parameters
    ----------
    name : str;
        The name of the shortcut

    working_dir : str
        The path to the directory to execute the executable from, or create folder shortcut to

    path : str
        The path where you want to put the shortcut

    executable : str or bool,
        The path to the executable you want to create a shortcut for, set to False if you want folder shortcut

    icon : str
        The path to a custom icon to use for the shortcut

    References
    ----------
    - Introduction to Property List files: https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/PropertyLists/Introduction/Introduction.html
    - Quick Start for property list files: https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/PropertyLists/QuickStartPlist/QuickStartPlist.html#//apple_ref/doc/uid/10000048i-CH4-SW5
    """
    if executable:
        data = {
            "CFBundleGetInfoString": "An icon generated by ezshortcut",
            "CFBundleName": "Name",
            "CFBundleExecutable": executable,
            "CFBundleIconFile": icon,
            "CFBundlePackageType": "APPL"
        }
    else:  # TODO: Figure out folder shortcut .plist files
        data = {
            "CFBundleGetInfoString": "An icon generated by ezshortcut",
            "CFBundleName": "Name",
            "CFBundleIconFile": icon,
            "CFBundlePackageType": "APPL"
        }
    with open("yeet.plist", "wb") as output:
        plistlib.dump(data, output, skipkeys=True)
    return str(plistlib.dumps(data))
Пример #39
0
    def send_ask(self, file_path, is_url=False, icon=None):
        ask_body = {
            "SenderComputerName": self.config.computer_name,
            "BundleID": "com.apple.finder",
            "SenderModelName": self.config.computer_model,
            "SenderID": self.config.service_id,
            "ConvertMediaFormats": False,
        }
        if self.config.record_data:
            ask_body["SenderRecordData"] = self.config.record_data

        def file_entries(files):
            for file in files:
                file_name = os.path.basename(file)
                file_entry = {
                    "FileName": file_name,
                    "FileType": AirDropUtil.get_uti_type(flp),
                    "FileBomPath": os.path.join(".", file_name),
                    "FileIsDirectory": os.path.isdir(file_name),
                    "ConvertMediaFormats": 0,
                }
                yield file_entry

        if isinstance(file_path, str):
            file_path = [file_path]
        if is_url:
            ask_body["Items"] = file_path
        else:
            # generate icon for first file
            with open(file_path[0], "rb") as f:
                file_header = f.read(128)
                flp = fleep.get(file_header)
                if not icon and len(flp.mime) > 0 and "image" in flp.mime[0]:
                    icon = AirDropUtil.generate_file_icon(f.name)
            ask_body["Files"] = [e for e in file_entries(file_path)]
        if icon:
            ask_body["FileIcon"] = icon

        ask_binary = plistlib.dumps(
            ask_body, fmt=plistlib.FMT_BINARY  # pylint: disable=no-member
        )
        success, _ = self.send_POST("/Ask", ask_binary)

        return success
Пример #40
0
    def send_ask(self, file_path, icon=None):
        ask_body = {
            'SenderComputerName': self.config.computer_name,
            'BundleID': 'com.apple.finder',
            'SenderModelName': self.config.computer_model,
            'SenderID': self.config.service_id,
            'ConvertMediaFormats': False,
        }
        if self.config.legacy:
            ask_body['SenderEmailHash'] = AirDropUtil.doubleSHA1Hash(self.config.email)
            ask_body['SenderPhoneHash'] = AirDropUtil.doubleSHA1Hash(self.config.phone)
        if self.config.record_data:
            ask_body['SenderRecordData'] = self.config.record_data

        if isinstance(file_path, str):
            file_path = [file_path]

        # generate icon for first file
        with open(file_path[0], 'rb') as f:
            file_header = f.read(128)
            flp = fleep.get(file_header)
            if not icon and len(flp.mime) > 0 and 'image' in flp.mime[0]:
                icon = AirDropUtil.generate_file_icon(f.name)
        if icon:
            ask_body['FileIcon'] = icon

        def file_entries(files):
            for file in files:
                file_name = os.path.basename(file)
                file_entry = {
                    'FileName': file_name,
                    'FileType': AirDropUtil.get_uti_type(flp),
                    'FileBomPath': os.path.join('.', file_name),
                    'FileIsDirectory': os.path.isdir(file_name),
                    'ConvertMediaFormats': 0
                }
                yield file_entry
        ask_body['Files'] = [e for e in file_entries(file_path)]
        ask_body['Items'] = []

        ask_binary = plistlib.dumps(ask_body, fmt=plistlib.FMT_BINARY)
        success, _ = self.send_POST('/Ask', ask_binary)

        return success
Пример #41
0
    def encode(self, data: dict, **options) -> BinaryFileStream:
        """
        Encodes the data into a Property List file-like stream.

        Args:
            data: The data to encode
            **options: The encoding options

        Returns:
            A Property List file-like stream

        Raises:
            geodatabr.encoders.EncodeError: If data fails to encode
        """
        try:
            return BinaryFileStream(
                plistlib.dumps(data, **dict(self.options, **options)))
        except Exception:
            raise EncodeError
Пример #42
0
    def pair(self):
        self.device_public_key = self.get_value('', 'DevicePublicKey')
        if self.device_public_key == '':
            self.logger.error('Unable to retrieve DevicePublicKey')
            return False

        self.logger.info('Creating host key & certificate')
        cert_pem, private_key_pem, device_certificate = ca_do_everything(
            self.device_public_key)

        pair_record = {
            'DevicePublicKey': self.device_public_key,
            'DeviceCertificate': device_certificate,
            'HostCertificate': cert_pem,
            'HostID': self.host_id,
            'RootCertificate': cert_pem,
            'SystemBUID': '30142955-444094379208051516'
        }

        pair = {
            'Label': self.label,
            'Request': 'Pair',
            'PairRecord': pair_record
        }
        self.service.send_plist(pair)
        pair = self.service.recv_plist()

        if pair and (pair.get('Result') == 'Success') or ('EscrowBag' in pair):
            pair_record['HostPrivateKey'] = private_key_pem
            pair_record['EscrowBag'] = pair.get('EscrowBag')
            write_home_file(HOMEFOLDER, '%s.plist' % self.identifier,
                            plistlib.dumps(pair_record))
            self.paired = True
            return True

        elif pair and pair.get('Error') == 'PasswordProtected':
            self.service.close()
            raise NotTrustedError()
        else:
            self.logger.error(pair.get('Error'))
            self.service.close()
            raise PairingError()
Пример #43
0
    def serialize(self, tags):
        data = {
            'catalogs': [c.get_signed_name() for c in self.catalogs(tags)],
            'included_manifests': []
        }

        # include the sub manifests
        for sm in self.sub_manifests(tags):
            data['included_manifests'].append(sm.get_signed_name())
            if sm.has_attachments():
                # add the sub manifest catalog to make the attachments available.
                # include the catalog even if the attachments are all trashed
                # so that autoremove works.
                data['catalogs'].append(sm.get_catalog_signed_name())

        # add the special catalog for the zentral enrollment packages
        data['catalogs'].append(self.get_enrollment_catalog_signed_name())

        # loop on the configured enrollment package builders
        enrollment_packages = self.enrollment_packages(tags)
        for builder, builder_config in monolith_conf.enrollment_package_builders.items(
        ):
            if builder in enrollment_packages:
                key = "managed_installs"
            else:
                # TODO: do not remove munki deps
                key = "managed_uninstalls"
            data.setdefault(key, []).append(builder_config["update_for"])

        # printers

        # include the catalog with all the printers for autoremove
        all_printers = self.printer_set.all()
        if all_printers.count():
            data['catalogs'].append(self.get_printer_catalog_signed_name())
        # include only the matching active printers as managed installs
        for printer in self.printers(tags):
            print("MANAGED INSTALL", printer.get_pkg_info_name())
            data.setdefault("managed_installs",
                            []).append(printer.get_pkg_info_name())

        return plistlib.dumps(data)
Пример #44
0
def macos_info_plist() -> bytes:
    import plistlib
    VERSION = '.'.join(map(str, version))
    pl = dict(
        CFBundleDevelopmentRegion='English',
        CFBundleDisplayName=appname,
        CFBundleName=appname,
        CFBundleIdentifier='net.kovidgoyal.' + appname,
        CFBundleVersion=VERSION,
        CFBundleShortVersionString=VERSION,
        CFBundlePackageType='APPL',
        CFBundleSignature='????',
        CFBundleExecutable=appname,
        LSMinimumSystemVersion='10.12.0',
        LSRequiresNativeExecution=True,
        NSAppleScriptEnabled=False,
        # Needed for dark mode in Mojave when linking against older SDKs
        NSRequiresAquaSystemAppearance='NO',
        NSHumanReadableCopyright=time.strftime(
            'Copyright %Y, Kovid Goyal'),
        CFBundleGetInfoString='kitty, an OpenGL based terminal emulator https://sw.kovidgoyal.net/kitty/',
        CFBundleIconFile=appname + '.icns',
        NSHighResolutionCapable=True,
        NSSupportsAutomaticGraphicsSwitching=True,
        LSApplicationCategoryType='public.app-category.utilities',
        LSEnvironment={'KITTY_LAUNCHED_BY_LAUNCH_SERVICES': '1'},
        NSServices=[
            {
                'NSMenuItem': {'default': 'New ' + appname + ' Tab Here'},
                'NSMessage': 'openTab',
                'NSRequiredContext': {'NSTextContent': 'FilePath'},
                'NSSendTypes': ['NSFilenamesPboardType', 'public.plain-text'],
            },
            {
                'NSMenuItem': {'default': 'New ' + appname + ' Window Here'},
                'NSMessage': 'openOSWindow',
                'NSRequiredContext': {'NSTextContent': 'FilePath'},
                'NSSendTypes': ['NSFilenamesPboardType', 'public.plain-text'],
            },
        ],
    )
    return plistlib.dumps(pl)
Пример #45
0
 def test_failure(self, run_command_output):
     plist_output = plistlib.dumps({
         'notarization-info': {
             'Date': '2019-05-20T13:18:35Z',
             'LogFileURL': 'https://example.com/log.json',
             'RequestUUID': 'cca0aec2-7c64-4ea4-b895-051ea3a17311',
             'Status': 'invalid',
             'Status Code': 2,
             'Status Message': 'Package Invalid',
         }
     })
     run_command_output.return_value = plist_output
     uuid = 'cca0aec2-7c64-4ea4-b895-051ea3a17311'
     config = test_config.TestConfig(
         notarization_tool=NotarizationTool.ALTOOL)
     result = notarize._get_result_altool(uuid, config)
     self.assertEqual(notarize.Status.ERROR, result.status)
     self.assertEqual('invalid', result.status_string)
     self.assertEqual(plist_output, result.output)
     self.assertEqual('https://example.com/log.json', result.log_file)
Пример #46
0
 def RemoveProfile(self, ident):
     profiles = self.GetProfileList()
     if not profiles:
         return
     if ident not in profiles["ProfileMetadata"]:
         print("Trying to remove not installed profile %s" % ident)
         return
     meta = profiles["ProfileMetadata"][ident]
     pprint(meta)
     data = plistlib.dumps({
         "PayloadType": "Configuration",
         "PayloadIdentifier": ident,
         "PayloadUUID": meta["PayloadUUID"],
         "PayloadVersion": meta["PayloadVersion"]
     })
     self.service.sendPlist({
         "RequestType": "RemoveProfile",
         "ProfileIdentifier": plistlib.Data(data)
     })
     return self.service.recvPlist()
Пример #47
0
    def send_protobuf(self, message: protobuf.ProtocolMessage) -> None:
        """Serialize a protobuf message and send it to receiver."""
        serialized_message = message.SerializeToString()
        serialized_length = write_variant(len(serialized_message))

        payload = plistlib.dumps(
            {"params": {"data": serialized_length + serialized_message}},
            fmt=plistlib.FMT_BINARY,  # pylint: disable=no-member
        )

        self.send(
            DataHeader.encode(
                DataHeader.length + len(payload),
                b"sync" + 8 * b"\x00",
                b"comm",
                self.send_seqno,
                DATA_HEADER_PADDING,
            )
            + payload
        )
Пример #48
0
    def send_request(self, request):
        """Send an install request to autopkginstalld"""
        self.socket.send(plistlib.dumps(request))
        with os.fdopen(self.socket.fileno()) as fileref:
            while True:
                data = fileref.readline()
                if data:
                    if data.startswith("OK:"):
                        return data.replace("OK:", "").rstrip()
                    elif data.startswith("ERROR:"):
                        break
                    else:
                        self.output(data.rstrip())
                else:
                    break

        errors = data.rstrip().split("\n")
        if not errors:
            errors = ["ERROR:No reply from autopkginstalld (crash?), check system logs"]
        raise ProcessorError(", ".join([s.replace("ERROR:", "") for s in errors]))
Пример #49
0
 def remove_profile(self, ident):
     profiles = self.get_profile_list()
     if not profiles:
         return
     if ident not in profiles['ProfileMetadata']:
         self.logger.info('Trying to remove not installed profile %s',
                          ident)
         return
     meta = profiles['ProfileMetadata'][ident]
     data = plistlib.dumps({
         'PayloadType': 'Configuration',
         'PayloadIdentifier': ident,
         'PayloadUUID': meta['PayloadUUID'],
         'PayloadVersion': meta['PayloadVersion']
     })
     self.service.send_plist({
         'RequestType': 'RemoveProfile',
         'ProfileIdentifier': data
     })
     return self.service.recv_plist()
Пример #50
0
    def test_skipkeys(self):
        pl = {
            42: 'aNumber',
            'snake': 'aWord',
        }

        for fmt in ALL_FORMATS:
            with self.subTest(fmt=fmt):
                data = plistlib.dumps(
                    pl, fmt=fmt, skipkeys=True, sort_keys=False)

                pl2 = plistlib.loads(data)
                self.assertEqual(pl2, {'snake': 'aWord'})

                fp = BytesIO()
                plistlib.dump(
                    pl, fp, fmt=fmt, skipkeys=True, sort_keys=False)
                data = fp.getvalue()
                pl2 = plistlib.loads(fp.getvalue())
                self.assertEqual(pl2, {'snake': 'aWord'})
Пример #51
0
def _content_to_uuid(payload):
    '''
    Generate a UUID based upon the payload content

    :param payload:
    :return:
    '''
    log.debug('Attempting to Hash {}'.format(payload))

    if six.PY3:
        str_payload = plistlib.dumps(payload)
    else:
        str_payload = plistlib.writePlistToString(payload)
    hashobj = hashlib.md5(str_payload)

    identifier = re.sub(
        b'([0-9a-f]{8})([0-9a-f]{4})([0-9a-f]{4})([0-9a-f]{4})([0-9a-f]{12})',
        b'\\1-\\2-\\3-\\4-\\5', binascii.hexlify(hashobj.digest()))

    return identifier.decode()
Пример #52
0
    def handle_ask(self):
        content_length = int(self.headers["Content-Length"])
        post_data = self.rfile.read(content_length)

        AirDropUtil.write_debug(self.config, post_data,
                                "receive_ask_request.plist")

        ask_response = {
            "ReceiverModelName": self.config.computer_model,
            "ReceiverComputerName": self.config.computer_name,
        }
        ask_resp_binary = plistlib.dumps(
            ask_response,
            fmt=plistlib.FMT_BINARY  # pylint: disable=no-member
        )

        AirDropUtil.write_debug(self.config, ask_resp_binary,
                                "receive_ask_response.plist")

        self._set_response(len(ask_resp_binary))
        self.wfile.write(ask_resp_binary)
Пример #53
0
def build_configuration_profile(enrollment):
    payload_content = {"PayloadContent": {"ManagedInstalls": {"Forced": [
                           {"mcx_preference_settings": build_configuration(enrollment)}
                       ]}},
                       "PayloadEnabled": True,
                       "PayloadIdentifier": get_payload_identifier("monolith.settings.0"),
                       "PayloadUUID": generate_payload_uuid(),
                       "PayloadType": "com.apple.ManagedClient.preferences",
                       "PayloadVersion": 1}
    configuration_profile_data = {"PayloadContent": [payload_content],
                                  "PayloadDescription": "Munki settings for Zentral/Monolith",
                                  "PayloadDisplayName": "Zentral - Munki settings",
                                  "PayloadIdentifier": get_payload_identifier("monolith.settings"),
                                  "PayloadOrganization": "Zentral",
                                  "PayloadRemovalDisallowed": True,
                                  "PayloadScope": "System",
                                  "PayloadType": "Configuration",
                                  "PayloadUUID": generate_payload_uuid(),
                                  "PayloadVersion": 1}
    content = plistlib.dumps(configuration_profile_data)
    return get_payload_identifier("monolith.settings.mobileconfig"), content
Пример #54
0
 def setUp(self):
     self.good_env = {
         "re_pattern": "http://.*.dmg",
         "result_output_var_name": "match",
         "url": "foobar",
     }
     self.bad_env = {}
     self.match = {
         "protocol": "http",
         "server": "someserver.url",
         "path": "first.dmg",
     }
     self.first_match = (
         f"{self.match['protocol']}://{self.match['server']}/{self.match['path']}"
     )
     self.case_sensitive_pattern = (
         f"{self.match['protocol']}://{self.match['server']}/FiRsT.dmg"
     )
     self.input_plist = plistlib.dumps(self.good_env)
     self.processor = URLTextSearcher(infile=self.input_plist)
     self.processor.env = self.good_env
Пример #55
0
def build_profile(payload_display_name=None,
                  payload_description=None,
                  payload_identifier=None,
                  payload_uuid=None,
                  channel=Channel.Device):
    if payload_uuid is None:
        payload_uuid = str(uuid.uuid4()).upper()
    if payload_identifier is None:
        payload_identifier = f"io.zentral.test.{payload_uuid}"
    profile = copy.deepcopy(PROFILE_TEMPLATE)
    profile["PayloadIdentifier"] = payload_identifier
    profile["PayloadUUID"] = payload_uuid
    profile["PayloadDisplayName"] = payload_display_name or get_random_string(
        16)
    profile["PayloadDescription"] = payload_description or get_random_string(
        32)
    profile["PayloadScope"] = "System" if channel == Channel.Device else "User"
    payload = profile["PayloadContent"][0]
    payload["PayloadIdentifier"] = f"{payload_identifier}.0"
    payload["PayloadUUID"] = str(uuid.uuid4()).upper()
    return plistlib.dumps(profile)
Пример #56
0
def build_profile(display_name,
                  suffix,
                  content,
                  payload_type="Configuration",
                  payload_description=None,
                  sign=True,
                  encrypt=False):
    profile = {
        "PayloadUUID": generate_payload_uuid(),
        "PayloadIdentifier": get_payload_identifier(suffix),
        "PayloadVersion": 1,
        "PayloadDisplayName": display_name,
        "PayloadType": payload_type,  # Only known exception: "Profile Service"
        "PayloadContent": content
    }
    if payload_description:
        profile["PayloadDescription"] = payload_description
    data = plistlib.dumps(profile)
    if sign:
        data = sign_payload(data)
    return data
Пример #57
0
 def test_indentation_dict(self):
     data = {
         '1': {
             '2': {
                 '3': {
                     '4': {
                         '5': {
                             '6': {
                                 '7': {
                                     '8': {
                                         '9': b'aaaaaa'
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
         }
     }
     self.assertEqual(plistlib.loads(plistlib.dumps(data)), data)
Пример #58
0
 def test_success_with_asc_provider(self, run_command_output):
     run_command_output.return_value = plistlib.dumps({
         'notarization-info': {
             'Date': '2019-07-08T20:11:24Z',
             'LogFileURL': 'https://example.com/log.json',
             'RequestUUID': '0a88b2d8-4098-4d3a-8461-5b543b479d15',
             'Status': 'success',
             'Status Code': 0
         }
     })
     uuid = '0a88b2d8-4098-4d3a-8461-5b543b479d15'
     config = test_config.TestConfig(
         notarization_tool=NotarizationTool.ALTOOL,
         notary_asc_provider='[NOTARY-ASC-PROVIDER]')
     self.assertEqual(notarize.Status.SUCCESS,
                      notarize._get_result_altool(uuid, config).status)
     run_command_output.assert_called_once_with([
         'xcrun', 'altool', '--notarization-info', uuid, '--username',
         '[NOTARY-USER]', '--password', '[NOTARY-PASSWORD]',
         '--output-format', 'xml', '--asc-provider', '[NOTARY-ASC-PROVIDER]'
     ])
Пример #59
0
    def serialize(self, out):
        plist = OrderedDict()

        for u in self.units:
            loc = u.outerkey
            subkey = u.innerkey

            if loc not in plist:
                plist[loc] = OrderedDict()

            if subkey is not None:
                plurals = OrderedDict()
                plurals["NSStringFormatSpecTypeKey"] = "NSStringPluralRuleType"
                plurals["NSStringFormatValueTypeKey"] = u.format_value_type

                plural_tags = self.target_plural_tags

                if isinstance(u.target, multistring):
                    plural_strings = u.target.strings
                elif isinstance(u.target, list):
                    plural_strings = u.target
                else:
                    plural_strings = [u.target]

                # Sync plural_strings elements to plural_tags count.
                if len(plural_strings) < len(plural_tags):
                    plural_strings += [""] * (len(plural_tags) -
                                              len(plural_strings))
                plural_strings = plural_strings[:len(plural_tags)]

                for plural_tag, plural_string in zip(plural_tags,
                                                     plural_strings):
                    if plural_string:
                        plurals[plural_tag] = plural_string

                plist[loc][subkey] = plurals
            else:
                plist[loc]["NSStringLocalizedFormatKey"] = u.target or u.source

        out.write(plistlib.dumps(plist, sort_keys=False))
Пример #60
0
def remove_report_from_plist(plist_file_obj, skip_handler):
    """
    Parse the original plist content provided by the analyzer
    and return a new plist content where reports were removed
    if they should be skipped. If the remove failed for some reason None
    will be returned.

    WARN !!!!
    If the 'files' array in the plist is modified all of the
    diagnostic section (control, event ...) nodes should be
    re indexed to use the proper file array indexes!!!
    """
    report_data = None
    try:
        report_data = parse_plist(plist_file_obj)
        if not report_data:
            return
    except Exception as ex:
        LOG.error("Plist parsing error")
        LOG.error(ex)
        return

    file_ids_to_remove = []

    try:
        for i, f in enumerate(report_data['files']):
            if skip_handler.should_skip(f):
                file_ids_to_remove.append(i)

        kept_diagnostics, kept_files = get_kept_report_data(report_data,
                                                            file_ids_to_remove)
        report_data['diagnostics'] = kept_diagnostics
        report_data['files'] = kept_files if kept_diagnostics else []

        return plistlib.dumps(report_data)

    except KeyError:
        LOG.error("Failed to modify plist content, "
                  "keeping the original version")
        return