5
// Created by Chuck Mount on 8/7/07.
6
// Copyright Centeris Corporation 2007. All rights reserved.
11
#include "DomainJoinStatus.h"
12
#include "DomainJoinInterface.h"
19
const int DomainJoinApp::ApplicationSignature = 'CnTs';
21
DomainJoinApp::DomainJoinApp()
27
// Create a new window. A full-fledged application would do this from an AppleEvent handler
28
// for kAEOpenApplication.
29
_mainWindow = new MainWindow(ApplicationSignature);
33
DomainJoinApp::~DomainJoinApp()
42
_leaveWindow->Close();
55
DomainJoinApp::GetJoinWindow()
59
_joinWindow = new DomainJoinWindow(ApplicationSignature);
66
DomainJoinApp::GetLeaveWindow()
70
_leaveWindow = new DomainLeaveWindow(ApplicationSignature);
77
DomainJoinApp::JoinOrLeaveDomain()
81
DomainJoinStatus joinStatus;
83
DomainJoinInterface::GetDomainJoinStatus(joinStatus);
85
DomainJoinWindow& joinWindow = GetJoinWindow();
86
DomainLeaveWindow& leaveWindow = GetLeaveWindow();
88
if (joinStatus.DomainName.length() > 0)
91
leaveWindow.SetComputerName(joinStatus.Name);
92
leaveWindow.SetDomainName(joinStatus.DomainName);
98
joinWindow.SetComputerName(joinStatus.Name);
99
joinWindow.SetDomainName(joinStatus.DomainName);
104
catch(DomainJoinException& dje)
107
const char* err = dje.what();
108
const char* message = dje.GetLongErrorMessage();
110
CFStringRef msgStrRef = CFStringCreateWithCString(NULL, message, kCFStringEncodingASCII);
111
CFStringGetPascalString(msgStrRef, (StringPtr)message, strlen(message), kCFStringEncodingASCII);
112
CFStringRef errStrRef = CFStringCreateWithCString(NULL, err, kCFStringEncodingASCII);
113
CFStringGetPascalString(errStrRef, (StringPtr)err, strlen(err), kCFStringEncodingASCII);
114
CreateStandardAlert(kAlertStopAlert, errStrRef, msgStrRef, NULL, &dialog);
115
RunStandardAlert(dialog, NULL, &outItemHit);
120
StandardAlert(kAlertStopAlert,
121
"\pUnexpected error",
122
"\pAn unexpected error occurred when joining the Active Directory domain. Please report this to Likewise Technical Support at support@likewisesoftware.com",
128
//--------------------------------------------------------------------------------------------
130
DomainJoinApp::HandleCommand( const HICommandExtended& inCommand )
132
switch ( inCommand.commandID )
135
case MAIN_MENU_JOIN_OR_LEAVE_ID:
139
// Add your own command-handling cases here
147
// AuthorizationExecuteWithPrivileges strips the environment
148
// We need to put back some of the environment
150
DomainJoinApp::FixProcessEnvironment()
152
std::string delim = ":";
153
std::vector<std::string> subPaths;
154
std::vector<std::string> essentialPaths;
155
std::vector<std::string>::iterator iter;
156
bool bNeedNewPath = false;
158
std::string curPath = getenv("PATH");
159
size_t idx_first = 0;
160
size_t idx_next = std::string::npos;
161
while (idx_first != std::string::npos)
163
idx_next = curPath.find_first_of(delim, idx_first);
164
if (idx_next != idx_first)
166
std::string token = curPath.substr(idx_first, idx_next - idx_first);
167
subPaths.push_back(token);
169
idx_first = curPath.find_first_not_of(delim, idx_next);
171
essentialPaths.push_back("/sbin");
172
essentialPaths.push_back("/bin");
173
essentialPaths.push_back("/usr/bin");
174
// search these paths in reverse, because we are going to add them in front (if they don't exist in the new path)
175
for (iter = essentialPaths.begin(); iter != essentialPaths.end(); iter++)
177
std::vector<std::string>::iterator pos = std::find(subPaths.begin(), subPaths.end(), *iter);
178
if (pos == subPaths.end())
180
subPaths.push_back(*iter);
187
std::ostringstream newPath;
189
for (iPath = 0, iter = subPaths.begin(); iter != subPaths.end(); iter++, iPath++)
198
_envPath = strdup(newPath.str().c_str());
207
FixProcessEnvironment();
211
//--------------------------------------------------------------------------------------------
212
int main(int argc, char* argv[])
221
AuthorizationRef authRef;
222
FILE* commPipe = NULL;
226
OSStatus status = noErr;
227
AuthorizationFlags authFlags = kAuthorizationFlagDefaults;
229
status = AuthorizationCreate(NULL, kAuthorizationEmptyEnvironment, authFlags, &authRef);
230
if (status != errAuthorizationSuccess)
232
throw FailedAdminPrivilegeException("Failed to create authorization");
237
AuthorizationItem authItems = {kAuthorizationRightExecute, 0, NULL, 0};
238
AuthorizationRights authRights = {1, &authItems};
240
authFlags = (kAuthorizationFlagDefaults |
241
kAuthorizationFlagInteractionAllowed |
242
kAuthorizationFlagPreAuthorize |
243
kAuthorizationFlagExtendRights);
244
status = AuthorizationCopyRights(authRef, &authRights, NULL, authFlags, NULL);
246
if (status != errAuthorizationSuccess)
248
throw FailedAdminPrivilegeException("Failed to acquire admin rights");
255
authFlags = kAuthorizationFlagDefaults;
256
status = AuthorizationExecuteWithPrivileges(authRef, argv[0], authFlags, argv, &commPipe);
257
if (status != errAuthorizationSuccess)
259
throw FailedAdminPrivilegeException("Failed to launch with privileges");
262
for(bytesRead = 0; bytesRead > 0; bytesRead = read(fileno(commPipe), msgBuf, sizeof(msgBuf)));
264
catch(FailedAdminPrivilegeException& fape)
268
CFStringRef msgStrRef = CFStringCreateWithCString(NULL, fape.what(), kCFStringEncodingASCII);
269
CFStringGetPascalString(msgStrRef, (StringPtr)msgStr, 255, kCFStringEncodingASCII);
270
StandardAlert(kAlertStopAlert,
271
"\pFailed to acquire admin privileges",
279
StandardAlert(kAlertStopAlert,
280
"\pUnexpected error",
281
"\pUnexpected error when launching Active Directory Join Application",
288
AuthorizationFree(authRef, kAuthorizationFlagDefaults);