Sudo is “a program for some Unix and Unix-like computer operating systems that allows users to run programs with the security privileges of another user (normally the superuser, a.k.a. root).” (Wikipedia reference). It works great for command line applications – especially for running them from a terminal prompt, or as part of a shell utility.
For Mac OS X Cocoa-based apps, there is analagous ability to sudo provided via the Authorization Services API. Use of the API allows you to prompt the user for their username and password requesting the ability to escalate privileges.
The Authorization Services API is a bit complicated. All the more so if you’re not writing a Cocoa app in Objective C. For example, maybe you’re using a scripting language, or you’re writing your GUI in Java / Swing for cross-platform use.
For that case, I’ve written a small utility that I’ve dubbed cocoasudo. Use cocoasudo in much the same way you’d use sudo. However, instead of users being prompted for their password in a Terminal window, they’ll get a dialog prompt via the Authorization Services API.
Usage:
cocoasudo [--icon=/path/to/icon.icns] ["--prompt=This prompt will be displayed to users."] command-to-launch [command-arguments...]
Both the –icon and –prompt parameters are optional and may be omitted.
Download the source on Github, or via your git client:
git clone git://github.com/performantdesign/cocoasudo.git
Or, if you don’t have Xcode, or don’t want to compile from source, you can download the cocoasudo binary directly here.
cocoasudo is Apache-licensed, so you can mostly use it as you see fit.
October 27th, 2009 at 1:10 pm
Dude, I’ve been looking for a program just like this for the past week. I searched today and bam, here it is. Just like gksudo on Linux. Just one problem. I am having issues getting the command and arguments to run. Not sure if this is because it is not recieving the correct authentication or if it is just a syntax error on my part.
ex.
./cocoasudo installer -pkg /Users/distrotubux/Desktop/Setup.mpkg -target / -verbose
I get the prompt for pw but the installer does not run. I’ve tested this command using sudo and it works just fine. Ideas?
October 27th, 2009 at 1:39 pm
Glad this can hopefully meet a need.
Could you give these alternates a try? It could be that the path of what you are running is required. If this works, I’ll work on updating the utility to search the path as well.
Alternate #1:
./cocoasudo `which installer` -pkg /Users/distrotubux/Desktop/Setup.mpkg -target / -verbose
Alternate #2:
./cocoasudo /usr/sbin/installer -pkg /Users/distrotubux/Desktop/Setup.mpkg -target / -verbose
October 29th, 2009 at 9:27 am
Yup, that did the trick. Both commands work. Thanks again for this nice little app.
October 29th, 2009 at 10:21 am
Another problem. My script is unable to catch the output of a command that is run with cocoasudo. Seems that cocoasudo is catching all the stdout from the run command and it is never getting to stdout.
October 29th, 2009 at 11:07 pm
A few updates were just pushed to git:
- stdout works now
- path is now searched for executable
- OS X 10.4 support
- improved error handling & messages
October 30th, 2009 at 4:02 pm
That’s great. Works like a charm!
May 8th, 2010 at 2:41 pm
For some reason, it won’t let me specify my own icon. It overlays the standard lock icon with an exec icon. OS X 10.6.3
May 31st, 2010 at 3:30 am
Amazing code that works brilliantly on osx 1.6. good work buddy !!
June 28th, 2010 at 3:55 pm
I love your tool! It’s fantastic and is great exactly formy situation (Java app ported to mac). I’m wondering if you could also write a quick tool that lets you add items to the com.apple.loginitems.plist file? I think it’s accessible using Apple’s LSSharedFileListItemRef, but I’m not an OSX developer :S
December 3rd, 2010 at 2:36 am
I came here to tell you that this was an awesome tool, but I had a feature request (until I figured it out on my own). I was hoping this tool could provide a “-u” option much like sudo does, but after looking at the Apple docs, it looks like they only allow privilege escalation via this API.
Anyways, you can have cocoasudo call ’sudo -u user command’ and it will DROP root privileges and run as a different user than your current login, which is exactly what I needed.
Thought I might share, thanks for the awesome software!