views:

171

answers:

1

In my Cocoa app how can I get the current user's domain when they're logged in via Active Directory?

I need to determine two things:

  1. If the current user is logged on to an Active Directory domain (only need to handle Active Directory).
  2. If 1, the domain of the user.

I've found references to Directory Services and the Open Directory Programming Guide but the latter is 10.6 only (I must support 10.5+) and I could not find any examples for the former that gave me an idea of what I needed to do.

+1  A: 

dsconfigad -show

It will tell whether you are bound to a directory and details about that directory if bound. Then you will know how to script dscl.

Sable
Thanks. That's a great start, it definitely gets me the name of the domain the computer is bound to. If you use sudo you do not need to specify local credentials (important for using dsadminad programatically). Now is there a way to tell whether the currently logged in user is a domain user or a local account? Poking around so far I haven't been able to figure out a way. If there's a way to do that I will happily accept your answer.
Lawrence Johnston
After poking around more, this question http://stackoverflow.com/questions/1139499/find-active-directory-users-home-folder-from-login-hook-script-in-os-x led me to try fingering the different users, and it appears on my test machine at least that AD users do not have a `Name` (they have a `Login`, but no full name). I'd like a more robust mechanism but if further testing and research shows this to be a fairly reliable method it may suffice.
Lawrence Johnston
I discovered at least one case where a user logged in via Active Directory had a full user name, so that will not work. Still looking.
Lawrence Johnston