cocoasudo: A graphical Cocoa based alternative to sudo

October 26th, 2009

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.

System Preferences app's use of Authorization Services API

System Preferences app's use of Authorization Services API

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.

10 Responses to “cocoasudo: A graphical Cocoa based alternative to sudo”

  1. Toby S Says:

    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?

  2. aaron Says:

    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

  3. Toby S Says:

    Yup, that did the trick. Both commands work. Thanks again for this nice little app.

  4. Toby S Says:

    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.

  5. aaron Says:

    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

  6. Toby S Says:

    That’s great. Works like a charm!

  7. HaDAk Says:

    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

  8. Mital Vora Says:

    Amazing code that works brilliantly on osx 1.6. good work buddy !!

  9. mystro Says:

    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

  10. Jon Hermansen Says:

    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!

Leave a Reply

Powered by WP Hashcash