def _readPlist(filepath): """ Read a .plist file from filepath. Return the unpacked root object (which is usually a dictionary). If the file doesn't exist, this returns None """ if not os.path.isfile(filepath): log.debug( 'Tried to read non-existent property list at path: {0}'.format( filepath)) return None plistData = NSData.dataWithContentsOfFile_(filepath) dataObject, plistFormat, error = \ NSPropertyListSerialization.propertyListFromData_mutabilityOption_format_errorDescription_( plistData, NSPropertyListMutableContainers, None, None) if error: error = error.encode('ascii', 'ignore') errmsg = "%s in file %s" % (error, filepath) import traceback log.debug(errmsg) log.debug(traceback.format_exc()) raise NSPropertyListSerializationException(errmsg) else: return dataObject
def _readPlist(filepath): """ Read a .plist file from filepath. Return the unpacked root object (which is usually a dictionary). If the file doesn't exist, this returns None """ if not os.path.isfile(filepath): log.debug('Tried to read non-existent property list at path: {0}'.format(filepath)) return None plistData = NSData.dataWithContentsOfFile_(filepath) dataObject, plistFormat, error = \ NSPropertyListSerialization.propertyListFromData_mutabilityOption_format_errorDescription_( plistData, NSPropertyListMutableContainers, None, None) if error: error = error.encode('ascii', 'ignore') errmsg = "%s in file %s" % (error, filepath) import traceback log.debug(errmsg) log.debug(traceback.format_exc()) raise NSPropertyListSerializationException(errmsg) else: return dataObject
def _dataToPlist(data): """low-level function that parses a data object into a propertyList object""" darwin_vers = int(os.uname()[2].split(".")[0]) if darwin_vers > 10: ( plistObject, plistFormat, error, ) = NSPropertyListSerialization.propertyListWithData_options_format_error_( data, NSPropertyListMutableContainersAndLeaves, None, None ) else: # 10.5 doesn't support propertyListWithData:options:format:error: # 10.6's PyObjC wrapper for propertyListWithData:options:format:error: # is broken # so use the older NSPropertyListSerialization function ( plistObject, plistFormat, error, ) = NSPropertyListSerialization.propertyListFromData_mutabilityOption_format_errorDescription_( data, NSPropertyListMutableContainersAndLeaves, None, None ) if plistObject is None: if error is None: error = "Plist data is invalid and could not be deserialized." raise NSPropertyListSerializationException(error) else: return plistObject
def main(argv): p = optparse.OptionParser() p.set_usage("""Usage: %prog [options] userdata.plist [password]""") p.add_option("-v", "--verbose", action="store_true", help="Verbose output.") options, argv = p.parse_args(argv) if len(argv) not in (2, 3): print >>sys.stderr, p.get_usage() return 1 # Read the userdata.plist. plist = argv[1] plist_data = NSData.dataWithContentsOfFile_(plist) if not plist_data: print >>sys.stderr, "Couldn't read %s" % plist return 1 user_plist, plist_format, error = \ NSPropertyListSerialization.propertyListFromData_mutabilityOption_format_errorDescription_( plist_data, NSPropertyListMutableContainers, None, None) if error: print >>sys.stderr, "Can't read %s: %s" % (plist, error) return 1 # Use password on commandline, or ask if one isn't provided. try: password = argv[2] except IndexError: while True: password = getpass.getpass() verify_password = getpass.getpass("Password again: ") if password == verify_password: break else: print "Passwords don't match!" # Hash password with all available methods. hashed_passwords = dict() for k, m in hash_methods.items(): hashed_pwd = m(password) pwd_data = NSData.alloc().initWithBytes_length_(hashed_pwd, len(hashed_pwd)) hashed_passwords[k] = pwd_data # Serialize hashed passwords to a binary plist. serialized_passwords = serialize_hash_dict(hashed_passwords) if not serialized_passwords: return 2 # Write back userdata.plist with ShadowHashData. user_plist["ShadowHashData"] = list() user_plist["ShadowHashData"].append(serialized_passwords) plist_data, error = \ NSPropertyListSerialization.dataFromPropertyList_format_errorDescription_( user_plist, plist_format, None) plist_data.writeToFile_atomically_(argv[1], True) return 0
def _read_plist(filepath): """ Read a .plist file from filepath. Return the unpacked root object (which is usually a dictionary). If the file doesn't exist, this returns None """ if not os.path.isfile(filepath): log.debug( 'Tried to read non-existent property list at path: {0}'.format( filepath)) return None plistData = NSData.dataWithContentsOfFile_(filepath) dataObject, plistFormat, error = \ NSPropertyListSerialization.propertyListFromData_mutabilityOption_format_errorDescription_( plistData, NSPropertyListMutableContainers, None, None) if error: error = error.encode('ascii', 'ignore') raise salt.exceptions.SaltInvocationError( 'Error decoding Property List : {}'.format(error)) else: return dataObject
def readPlistFromString(data): '''Read a plist data from a string. Return the root object.''' plistData = buffer(data) dataObject, plistFormat, error = \ NSPropertyListSerialization.propertyListFromData_mutabilityOption_format_errorDescription_( plistData, NSPropertyListMutableContainers, None, None) if error: raise NSPropertyListSerializationException(error) else: return dataObject
def readPlist(path): plistData = NSData.dataWithContentsOfFile_(path) dataObject, plistFormat, error = \ NSPropertyListSerialization.propertyListFromData_mutabilityOption_format_errorDescription_( plistData, NSPropertyListMutableContainers, None, None) if error: errmsg = "%s in file %s" % (repr(error), repr(path)) raise NSPropertyListSerializationException(errmsg) else: return dataObject
def readPlist(path): plistData = NSData.dataWithContentsOfFile_(path) dataObject, plistFormat, error = \ NSPropertyListSerialization.propertyListFromData_mutabilityOption_format_errorDescription_( plistData, NSPropertyListMutableContainers, None, None) if error: errmsg = "%s in file %s" % (repr(error), repr(path)) raise NSPropertyListSerializationException(errmsg) else: return dataObject
def readPlistFromString(data): '''Read a plist data from a string. Return the root object.''' plistData = buffer(data) dataObject, plistFormat, error = \ NSPropertyListSerialization.propertyListFromData_mutabilityOption_format_errorDescription_( plistData, NSPropertyListMutableContainers, None, None) if error: raise NSPropertyListSerializationException(error) else: return dataObject
def decode_bdisk(key): """Return the relative path of file referenced by key """ decodedBytes = base64.b64decode(key) nsData = NSData.dataWithBytes_length_(decodedBytes, len(decodedBytes)) plist, fmt, error = NSPropertyListSerialization.propertyListFromData_mutabilityOption_format_errorDescription_( nsData, 0, None, None) if plist is None: return "Missed" else: return plist['relativePath']
def readPlistFromString(data): """Read a plist data from a string. Return the root object.""" plistData = buffer(data) dataObject, plistFormat, error = NSPropertyListSerialization.propertyListFromData_mutabilityOption_format_errorDescription_( plistData, NSPropertyListMutableContainers, None, None ) if error: error = error.encode("ascii", "ignore") raise NSPropertyListSerializationException(error) else: return dataObject
def readPlist(filepath): """ Read a .plist file from filepath. Return the unpacked root object (which is usually a dictionary). """ plistData = NSData.dataWithContentsOfFile_(filepath) dataObject, plistFormat, error = \ NSPropertyListSerialization.propertyListFromData_mutabilityOption_format_errorDescription_( plistData, NSPropertyListMutableContainers, None, None) if error: errmsg = "%s in file %s" % (error, filepath) raise NSPropertyListSerializationException(errmsg) else: return dataObject
def readPlist(self, filepath): """ Read a .plist file from filepath. Return the unpacked root object (which is usually a dictionary). """ plistData = NSData.dataWithContentsOfFile_(filepath) dataObject, plistFormat, error = \ NSPropertyListSerialization.propertyListFromData_mutabilityOption_format_errorDescription_( plistData, NSPropertyListMutableContainers, None, None) if error: errmsg = "%s in file %s" % (error, filepath) raise ProcessorError(errmsg) else: return dataObject
def read_bundle_info(self, path): """Read Contents/Info.plist inside a bundle.""" plistpath = os.path.join(path, "Contents", "Info.plist") info, format, error = \ NSPropertyListSerialization.propertyListFromData_mutabilityOption_format_errorDescription_( NSData.dataWithContentsOfFile_(plistpath), NSPropertyListMutableContainers, None, None ) if error: raise ProcessorError("Can't read %s: %s" % (plistpath, error)) return info
def read_bundle_info(self, path): """Read Contents/Info.plist inside a bundle.""" plistpath = os.path.join(path, "Contents", "Info.plist") info, format, error = \ NSPropertyListSerialization.propertyListFromData_mutabilityOption_format_errorDescription_( NSData.dataWithContentsOfFile_(plistpath), NSPropertyListMutableContainers, None, None ) if error: raise ProcessorError("Can't read %s: %s" % (plistpath, error)) return info
def read_bundle_info(self, path): """Read Contents/Info.plist inside a bundle.""" # pylint: disable=no-self-use plistpath = os.path.join(path, "Contents", "Info.plist") # pylint: disable=line-too-long info, _, error = NSPropertyListSerialization.propertyListFromData_mutabilityOption_format_errorDescription_( NSData.dataWithContentsOfFile_(plistpath), NSPropertyListMutableContainers, None, None, ) # pylint: enable=line-too-long if error: raise ProcessorError("Can't read %s: %s" % (plistpath, error)) return info
def readPlist(filepath): """ Read a .plist file from filepath. Return the unpacked root object (which is usually a dictionary). """ plistData = NSData.dataWithContentsOfFile_(filepath) dataObject, dummy_plistFormat, error = NSPropertyListSerialization.propertyListFromData_mutabilityOption_format_errorDescription_( plistData, NSPropertyListMutableContainers, None, None ) if dataObject is None: if error: error = error.encode("ascii", "ignore") else: error = "Unknown error" errmsg = "%s in file %s" % (error, filepath) raise NSPropertyListSerializationException(errmsg) else: return dataObject
def read_bundle_info(self, path): """Read Contents/Info.plist inside a bundle.""" # pylint: disable=no-self-use plistpath = os.path.join(path, "Contents", "Info.plist") # pylint: disable=line-too-long info, _, error = NSPropertyListSerialization.propertyListFromData_mutabilityOption_format_errorDescription_( NSData.dataWithContentsOfFile_(plistpath), NSPropertyListMutableContainers, None, None, ) # pylint: enable=line-too-long if error: raise ProcessorError("Can't read %s: %s" % (plistpath, error)) return info
def read_plist(filepath): ''' Read a .plist file from filepath. Return the unpacked root object (which is usually a dictionary). ''' plist_data = NSData.dataWithContentsOfFile_(filepath) data_object, dummy_plist_format, error = ( NSPropertyListSerialization.propertyListFromData_mutabilityOption_format_errorDescription_( plist_data, NSPropertyListMutableContainers, None, None)) if data_object is None: if error: error = error.encode('ascii', 'ignore') else: error = "Unknown error" errmsg = "%s in file %s" % (error, filepath) raise NSPropertyListSerializationException(errmsg) # pylint: disable=E0602 else: return data_object
def readPlistFromString(self, data): """Read a plist data from a string. Return the root object.""" try: plistData = memoryview(data) except TypeError as err: raise ProcessorError(err) ( dataObject, dummy_plistFormat, error, ) = NSPropertyListSerialization.propertyListFromData_mutabilityOption_format_errorDescription_( plistData, NSPropertyListMutableContainers, None, None) if dataObject is None: if error: error = error.encode("ascii", "ignore") else: error = "Unknown error" raise ProcessorError(error) else: return dataObject
def parse_string(data): ''' Take a property list as a string and return a python native representation. Used by other modules in salt-osx ''' plistData = buffer(data) dataObject, plistFormat, error = \ NSPropertyListSerialization.propertyListFromData_mutabilityOption_format_errorDescription_( plistData, NSPropertyListMutableContainers, None, None) if error: error = error.encode('ascii', 'ignore') import traceback log.debug('Error parsing plist from string') log.debug(error) raise NSPropertyListSerializationException(error) else: return dataObject
def parse_string(data): ''' Take a property list as a string and return a python native representation. Used by other modules in salt-osx ''' plistData = buffer(data) dataObject, plistFormat, error = \ NSPropertyListSerialization.propertyListFromData_mutabilityOption_format_errorDescription_( plistData, NSPropertyListMutableContainers, None, None) if error: error = error.encode('ascii', 'ignore') import traceback log.debug('Error parsing plist from string') log.debug(error) raise NSPropertyListSerializationException(error) else: return dataObject
def readPlist(self, filepath): """ Read a .plist file from filepath. Return the unpacked root object (which is usually a dictionary). """ plistData = NSData.dataWithContentsOfFile_(filepath) ( dataObject, dummy_plistFormat, error, ) = NSPropertyListSerialization.propertyListFromData_mutabilityOption_format_errorDescription_( plistData, NSPropertyListMutableContainers, None, None) if dataObject is None: if error: error = error.encode("ascii", "ignore") else: error = "Unknown error" errmsg = "%s in file %s" % (error, filepath) raise ProcessorError(errmsg) else: return dataObject
def _dataToPlist(data): '''low-level function that parses a data object into a propertyList object''' darwin_vers = int(os.uname()[2].split('.')[0]) if darwin_vers > 10: (plistObject, plistFormat, error) = ( NSPropertyListSerialization.propertyListWithData_options_format_error_( data, NSPropertyListMutableContainersAndLeaves, None, None)) else: # 10.5 doesn't support propertyListWithData:options:format:error: # 10.6's PyObjC wrapper for propertyListWithData:options:format:error: # is broken # so use the older NSPropertyListSerialization function (plistObject, plistFormat, error) = ( NSPropertyListSerialization.propertyListFromData_mutabilityOption_format_errorDescription_( data, NSPropertyListMutableContainersAndLeaves, None, None ) ) if plistObject is None: if error is None: error = "Plist data is invalid and could not be deserialized." raise NSPropertyListSerializationException(error) else: return plistObject
def _read_plist(filepath): """ Read a .plist file from filepath. Return the unpacked root object (which is usually a dictionary). If the file doesn't exist, this returns None """ if not os.path.isfile(filepath): log.debug('Tried to read non-existent property list at path: {0}'.format(filepath)) return None plistData = NSData.dataWithContentsOfFile_(filepath) dataObject, plistFormat, error = \ NSPropertyListSerialization.propertyListFromData_mutabilityOption_format_errorDescription_( plistData, NSPropertyListMutableContainers, None, None) if error: error = error.encode('ascii', 'ignore') raise salt.exceptions.SaltInvocationError( 'Error decoding Property List : {}'.format(error) ) else: return dataObject
def main(argv): p = optparse.OptionParser() p.set_usage("""Usage: %prog [options] userdata.plist [password]""") p.add_option("-v", "--verbose", action="store_true", help="Verbose output.") options, argv = p.parse_args(argv) if len(argv) not in (2, 3): print >> sys.stderr, p.get_usage() return 1 # Read the userdata.plist. plist = argv[1] plist_data = NSData.dataWithContentsOfFile_(plist) if not plist_data: print >> sys.stderr, "Couldn't read %s" % plist return 1 user_plist, plist_format, error = \ NSPropertyListSerialization.propertyListFromData_mutabilityOption_format_errorDescription_( plist_data, NSPropertyListMutableContainers, None, None) if error: print >> sys.stderr, "Can't read %s: %s" % (plist, error) return 1 # Use password on commandline, or ask if one isn't provided. try: password = argv[2] except IndexError: while True: password = getpass.getpass() verify_password = getpass.getpass("Password again: ") if password == verify_password: break else: print "Passwords don't match!" # Hash password with all available methods. hashed_passwords = dict() for k, m in hash_methods.items(): hashed_pwd = m(password) pwd_data = NSData.alloc().initWithBytes_length_( hashed_pwd, len(hashed_pwd)) hashed_passwords[k] = pwd_data # Serialize hashed passwords to a binary plist. serialized_passwords = serialize_hash_dict(hashed_passwords) if not serialized_passwords: return 2 # Write back userdata.plist with ShadowHashData. user_plist["ShadowHashData"] = list() user_plist["ShadowHashData"].append(serialized_passwords) plist_data, error = \ NSPropertyListSerialization.dataFromPropertyList_format_errorDescription_( user_plist, plist_format, None) plist_data.writeToFile_atomically_(argv[1], True) return 0
else: error = "Unknown error" errmsg = "%s in file %s" % (error, filepath) raise NSPropertyListSerializationException(errmsg) else: return dataObject def readPlistFromString(data): """Read a plist data from a string. Return the root object.""" try: plistData = buffer(data) except TypeError, err: raise NSPropertyListSerializationException(err) dataObject, dummy_plistFormat, error = NSPropertyListSerialization.propertyListFromData_mutabilityOption_format_errorDescription_( plistData, NSPropertyListMutableContainers, None, None ) if dataObject is None: if error: error = error.encode("ascii", "ignore") else: error = "Unknown error" raise NSPropertyListSerializationException(error) else: return dataObject def writePlist(dataObject, filepath): """ Write 'rootObject' as a plist to filepath. """
def readPlistFromString(data): """Read a plist data from a string. Return the root object. """ try: plistData = buffer(data) except TypeError, err: raise NSPropertyListSerializationException(err) ( dataObject, dummy_plistFormat, error, ) = NSPropertyListSerialization.propertyListFromData_mutabilityOption_format_errorDescription_( plistData, NSPropertyListMutableContainers, None, None) if dataObject is None: if error: error = error.encode("ascii", "ignore") else: error = "Unknown error" raise NSPropertyListSerializationException(error) else: return dataObject def writePlist(dataObject, filepath): """Write 'rootObject' as a plist to filepath.""" ( plistData, error,