Re: [Pyobjc-dev] Calling SMJobBless with valid AuthorizationRef
Brought to you by:
ronaldoussoren
|
From: Kunal P. <kun...@gm...> - 2012-11-07 22:23:32
|
Thanks Mike. I was able to get it working on my end as well. For reference,
here's my code -
# CoreFoundation framework.
_CoreFoundation =
ctypes.cdll.LoadLibrary(ctypes.util.find_library('CoreFoundation'))
_CFStringCreateWithCString = _CoreFoundation.CFStringCreateWithCString
_CFStringCreateWithCString.restype = ctypes.c_void_p
_CFStringCreateWithCString.argtypes = [
ctypes.c_void_p,
ctypes.c_void_p,
ctypes.c_uint32,
]
_CFRelease = _CoreFoundation.CFRelease
_CFRelease.restype = None
_CFRelease.argtypes = [ctypes.c_void_p]
_kCFStringEncodingUTF8 = 0x08000100
# Security framework.
_Security = ctypes.cdll.LoadLibrary(ctypes.util.find_library('Security'))
_AuthorizationCreate = _Security.AuthorizationCreate
_AuthorizationFree = _Security.AuthorizationFree
_kAuthorizationFlagDefaults = 0
_kAuthorizationFlagInteractionAllowed = (1 << 0)
_kAuthorizationFlagExtendRights = (1 << 1)
_kAuthorizationFlagPartialRights = (1 << 2)
_kAuthorizationFlagDestroyRights = (1 << 3)
_kAuthorizationFlagPreAuthorize = (1 << 4)
_kAuthorizationFlagNoData = (1 << 20)
# ServiceManagement framework.
_ServiceManagement =
ctypes.cdll.LoadLibrary(ctypes.util.find_library('ServiceManagement'))
_SMJobBless = _ServiceManagement.SMJobBless
_SMJobBless.restype = ctypes.c_bool
_SMJobBless.argtypes = [
ctypes.c_void_p,
ctypes.c_void_p,
ctypes.c_void_p,
ctypes.POINTER(ctypes.c_void_p),
]
_kSMDomainSystemLaunchd = ctypes.c_void_p.in_dll(_ServiceManagement,
'kSMDomainSystemLaunchd')
_kSMRightBlessPrivilegedHelper = 'com.apple.ServiceManagement.blesshelper'
class _AuthorizationItem(ctypes.Structure):
_fields_ = [
('name', ctypes.c_char_p),
('valueLength', ctypes.c_uint32),
('value', ctypes.c_void_p),
('flags', ctypes.c_uint32),
]
class _AuthorizationRights(ctypes.Structure):
_fields_ = [
('count', ctypes.c_uint32),
('items', ctypes.POINTER(_AuthorizationItem)),
]
authorization_item = _AuthorizationItem(
name=_kSMRightBlessPrivilegedHelper,
valueLength=0,
value=None,
flags=0,
)
authorization_rights = _AuthorizationRights(
count=1,
items=ctypes.pointer(authorization_item),
)
authorization_flags = (
_kAuthorizationFlagDefaults
| _kAuthorizationFlagInteractionAllowed
| _kAuthorizationFlagPreAuthorize
| _kAuthorizationFlagExtendRights
)
authorization_ref = ctypes.c_void_p()
status = _AuthorizationCreate(
ctypes.byref(authorization_rights),
None,
authorization_flags,
ctypes.byref(authorization_ref),
)
if status:
print('Failed to get rights to install helper, status %d', status)
return
try:
executable_label = _CFStringCreateWithCString(
None,
b'com.kunalparmar.helper'.encode('utf-8'),
_kCFStringEncodingUTF8,
)
try:
if _SMJobBless(
_kSMDomainSystemLaunchd,
executable_label,
authorization_ref,
None,
):
print('Helper installed')
else:
print('Failed to install helper')
finally:
_CFRelease(executable_label)
finally:
_AuthorizationFree(authorization_ref, _kAuthorizationFlagDefaults)
On Mon, Nov 5, 2012 at 8:15 AM, Michael McCracken <
mic...@gm...> wrote:
> Hi Kunal, I recently wrote some code to call SMJobBless and SMJobRemove
> using only ctypes.
> This was lots of boilerplate but works reliably.
> It's GPL, so you can see the code here:
> http://bazaar.launchpad.net/~ubuntuone-control-tower/ubuntuone-control-panel/trunk/view/head:/ubuntuone/controlpanel/utils/darwin.py
>
> Please feel free to ask me if you have any questions.
>
> -mike
>
>
> On Mon, Nov 5, 2012 at 1:56 AM, Ronald Oussoren <ron...@ma...>wrote:
>
>>
>> On 5 Nov, 2012, at 2:59, Kunal Parmar <kun...@gm...> wrote:
>>
>> > I'm getting an error when calling SMJobBless - "ValueError:
>> depythonifying 'pointer', got 'LP_c_void_p'". I've tried a few things
>> without luck. Any help will be appreciated.
>>
>> It's currently not easily possible to use ctypes with PyObjC, I've filed
>> an issue in my bitbucket about adding ctypes support to pyobjc <
>> https://bitbucket.org/ronaldoussoren/pyobjc/issue/23/add-ctypes-support>.
>>
>> Anoyingly the Security framework is not easily wrapped using the normal
>> PyObjC mechanism's either, in particular support for C structs that contain
>> pointers is lacking right now. I'll have to experiment a little to see if I
>> can get your example to work properly using some other hacks.
>>
>> Ronald
>>
>>
>> >
>> > Here's my code -
>> >
>> > from ServiceManagement import (
>> > kSMDomainSystemLaunchd,
>> > SMJobBless,
>> > )
>> >
>> > security = ctypes.cdll.LoadLibrary(ctypes.util.find_library('Security'))
>> >
>> > class AuthorizationItem(ctypes.Structure):
>> > _fields_ = [
>> > ('name', ctypes.c_char_p),
>> > ('valueLength', ctypes.c_uint32),
>> > ('value', ctypes.c_void_p),
>> > ('flags', ctypes.c_uint32),
>> > ]
>> >
>> > class AuthorizationRights(ctypes.Structure):
>> > _fields_ = [
>> > ('count', ctypes.c_uint32),
>> > ('items', ctypes.POINTER(AuthorizationItem)),
>> > ]
>> >
>> > authorization_item = AuthorizationItem(
>> > name='com.apple.ServiceManagement.blesshelper',
>> > valueLength=0,
>> > value=None,
>> > flags=0,
>> > )
>> > authorization_rights = AuthorizationRights(
>> > count=1,
>> > items=ctypes.pointer(authorization_item),
>> > )
>> > authorization_flags = 19
>> > authorization_ref = ctypes.POINTER(ctypes.c_void_p)()
>> > status = security.AuthorizationCreate(
>> > ctypes.pointer(authorization_rights),
>> > None,
>> > authorization_flags,
>> > ctypes.pointer(authorization_ref),
>> > )
>> > SMJobBless(
>> > kSMDomainSystemLaunchd,
>> > 'app.helper',
>> > authorization_ref,
>> > None,
>> > )
>> >
>> ------------------------------------------------------------------------------
>> > LogMeIn Central: Instant, anywhere, Remote PC access and management.
>> > Stay in control, update software, and manage PCs from one command center
>> > Diagnose problems and improve visibility into emerging IT issues
>> > Automate, monitor and manage. Do more in less time with Central
>> >
>> http://p.sf.net/sfu/logmein12331_d2d_______________________________________________
>> > Pyobjc-dev mailing list
>> > Pyo...@li...
>> > https://lists.sourceforge.net/lists/listinfo/pyobjc-dev
>>
>>
>>
>> ------------------------------------------------------------------------------
>> LogMeIn Central: Instant, anywhere, Remote PC access and management.
>> Stay in control, update software, and manage PCs from one command center
>> Diagnose problems and improve visibility into emerging IT issues
>> Automate, monitor and manage. Do more in less time with Central
>> http://p.sf.net/sfu/logmein12331_d2d
>> _______________________________________________
>> Pyobjc-dev mailing list
>> Pyo...@li...
>> https://lists.sourceforge.net/lists/listinfo/pyobjc-dev
>>
>
>
|