~zulcss/ubuntu/lucid/likewise-open/likewise-open-sru

« back to all changes in this revision

Viewing changes to domainjoin/domainjoin-gui/carbon/DomainJoin/main.cp

  • Committer: Bazaar Package Importer
  • Author(s): Rick Clark
  • Date: 2008-08-27 08:56:20 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20080827085620-5q0f58b9qtog9myq
Tags: 4.1.0.2956-0ubuntu1
* missing-likewise-logo.diff: removed
* fixed copyright notice
* updated Standards-Version to 3.8.0
* removed path from command in prerm
* removed stop in S runlevel

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
//  main.cpp
 
3
//  DomainJoin
 
4
//
 
5
//  Created by Chuck Mount on 8/7/07.
 
6
//  Copyright Centeris Corporation 2007. All rights reserved.
 
7
//
 
8
 
 
9
#include "main.h"
 
10
 
 
11
#include "DomainJoinStatus.h"
 
12
#include "DomainJoinInterface.h"
 
13
 
 
14
#include <string>
 
15
#include <vector>
 
16
#include <sstream>
 
17
#include <algorithm>
 
18
 
 
19
const int DomainJoinApp::ApplicationSignature = 'CnTs';
 
20
 
 
21
DomainJoinApp::DomainJoinApp()
 
22
: _joinWindow(0),
 
23
  _leaveWindow(0),
 
24
  _mainWindow(0),
 
25
  _envPath(NULL)
 
26
{
 
27
    // Create a new window. A full-fledged application would do this from an AppleEvent handler
 
28
    // for kAEOpenApplication.
 
29
        _mainWindow = new MainWindow(ApplicationSignature);
 
30
        JoinOrLeaveDomain();
 
31
}
 
32
 
 
33
DomainJoinApp::~DomainJoinApp()
 
34
{
 
35
        if (_joinWindow)
 
36
        {
 
37
           _joinWindow->Close();
 
38
           delete _joinWindow;
 
39
        }
 
40
        if (_leaveWindow)
 
41
        {
 
42
           _leaveWindow->Close();
 
43
           delete _leaveWindow;
 
44
        }
 
45
        if (_mainWindow)
 
46
        {
 
47
           _mainWindow->Close();
 
48
           delete _mainWindow;
 
49
        }
 
50
        if (_envPath)
 
51
           delete _envPath;
 
52
}
 
53
 
 
54
DomainJoinWindow&
 
55
DomainJoinApp::GetJoinWindow()
 
56
{
 
57
    if (!_joinWindow)
 
58
        {
 
59
           _joinWindow = new DomainJoinWindow(ApplicationSignature);
 
60
        }
 
61
        
 
62
        return *_joinWindow;
 
63
}
 
64
 
 
65
DomainLeaveWindow&
 
66
DomainJoinApp::GetLeaveWindow()
 
67
{
 
68
    if (!_leaveWindow)
 
69
        {
 
70
           _leaveWindow = new DomainLeaveWindow(ApplicationSignature);
 
71
        }
 
72
        
 
73
        return *_leaveWindow;
 
74
}
 
75
 
 
76
void
 
77
DomainJoinApp::JoinOrLeaveDomain()
 
78
{
 
79
    try
 
80
    {
 
81
            DomainJoinStatus joinStatus;
 
82
                
 
83
        DomainJoinInterface::GetDomainJoinStatus(joinStatus);
 
84
                
 
85
                DomainJoinWindow& joinWindow = GetJoinWindow();
 
86
                DomainLeaveWindow& leaveWindow = GetLeaveWindow();
 
87
                
 
88
                if (joinStatus.DomainName.length() > 0)
 
89
                {
 
90
                   joinWindow.Hide();
 
91
                   leaveWindow.SetComputerName(joinStatus.Name);
 
92
                   leaveWindow.SetDomainName(joinStatus.DomainName);
 
93
                   leaveWindow.Show();
 
94
                }
 
95
                else
 
96
                {
 
97
                   leaveWindow.Hide();
 
98
                   joinWindow.SetComputerName(joinStatus.Name);
 
99
                   joinWindow.SetDomainName(joinStatus.DomainName);
 
100
                   joinWindow.Show();
 
101
                }
 
102
 
 
103
    }
 
104
    catch(DomainJoinException& dje)
 
105
    {                                     
 
106
                SInt16 outItemHit;
 
107
                const char* err = dje.what();   
 
108
                const char* message = dje.GetLongErrorMessage();
 
109
                DialogRef dialog;       
 
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);
 
116
    }
 
117
    catch(...)
 
118
    {
 
119
        SInt16 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",
 
123
                      NULL,
 
124
                      &outItemHit);
 
125
    }
 
126
}
 
127
 
 
128
//--------------------------------------------------------------------------------------------
 
129
Boolean
 
130
DomainJoinApp::HandleCommand( const HICommandExtended& inCommand )
 
131
{
 
132
    switch ( inCommand.commandID )
 
133
    {
 
134
        
 
135
                case MAIN_MENU_JOIN_OR_LEAVE_ID:
 
136
                    JoinOrLeaveDomain();
 
137
                        return true;
 
138
            
 
139
        // Add your own command-handling cases here
 
140
        
 
141
        default:
 
142
            return false;
 
143
    }
 
144
}
 
145
 
 
146
//
 
147
// AuthorizationExecuteWithPrivileges strips the environment
 
148
// We need to put back some of the environment
 
149
void
 
150
DomainJoinApp::FixProcessEnvironment()
 
151
{
 
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;
 
157
        
 
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)
 
162
        {
 
163
            idx_next = curPath.find_first_of(delim, idx_first);
 
164
                if (idx_next != idx_first)
 
165
                {
 
166
                   std::string token = curPath.substr(idx_first, idx_next - idx_first);
 
167
                   subPaths.push_back(token);
 
168
                }
 
169
                idx_first = curPath.find_first_not_of(delim, idx_next);
 
170
        }
 
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++)
 
176
        {
 
177
            std::vector<std::string>::iterator pos = std::find(subPaths.begin(), subPaths.end(), *iter);
 
178
                if (pos == subPaths.end())
 
179
                {
 
180
                   subPaths.push_back(*iter);
 
181
                   bNeedNewPath = true;
 
182
                }
 
183
        }
 
184
        if (bNeedNewPath)
 
185
        {
 
186
           int iPath = 0;
 
187
           std::ostringstream newPath;
 
188
           newPath << "PATH=";
 
189
           for (iPath = 0, iter = subPaths.begin(); iter != subPaths.end(); iter++, iPath++)
 
190
           {
 
191
               if (iPath)
 
192
                   {
 
193
                      newPath << delim;
 
194
                   }
 
195
               newPath << *iter;
 
196
           }
 
197
           
 
198
           _envPath = strdup(newPath.str().c_str());
 
199
           
 
200
           putenv(_envPath);
 
201
        }
 
202
}
 
203
 
 
204
void
 
205
DomainJoinApp::Run()
 
206
{
 
207
    FixProcessEnvironment();
 
208
        TApplication::Run();
 
209
}
 
210
 
 
211
//--------------------------------------------------------------------------------------------
 
212
int main(int argc, char* argv[])
 
213
{
 
214
    if (geteuid() == 0)
 
215
    {
 
216
        DomainJoinApp app;
 
217
        app.Run();
 
218
    }
 
219
    else
 
220
    {
 
221
       AuthorizationRef   authRef;
 
222
       FILE* commPipe = NULL;
 
223
 
 
224
       try
 
225
       {
 
226
           OSStatus status = noErr;
 
227
           AuthorizationFlags authFlags = kAuthorizationFlagDefaults;
 
228
       
 
229
           status = AuthorizationCreate(NULL, kAuthorizationEmptyEnvironment, authFlags, &authRef);
 
230
           if (status != errAuthorizationSuccess)
 
231
           {
 
232
              throw FailedAdminPrivilegeException("Failed to create authorization");
 
233
           }
 
234
       
 
235
           do
 
236
           {
 
237
               AuthorizationItem authItems = {kAuthorizationRightExecute, 0, NULL, 0};
 
238
               AuthorizationRights authRights = {1, &authItems};
 
239
           
 
240
               authFlags = (kAuthorizationFlagDefaults |
 
241
                            kAuthorizationFlagInteractionAllowed |
 
242
                            kAuthorizationFlagPreAuthorize |
 
243
                            kAuthorizationFlagExtendRights);
 
244
               status = AuthorizationCopyRights(authRef, &authRights, NULL, authFlags, NULL);
 
245
        
 
246
               if (status != errAuthorizationSuccess)
 
247
               {
 
248
                  throw FailedAdminPrivilegeException("Failed to acquire admin rights");
 
249
               }
 
250
           } while (0);
 
251
           
 
252
           char msgBuf[128];
 
253
           int bytesRead = 0;
 
254
           
 
255
           authFlags = kAuthorizationFlagDefaults;
 
256
           status = AuthorizationExecuteWithPrivileges(authRef, argv[0], authFlags, argv, &commPipe);
 
257
           if (status != errAuthorizationSuccess)
 
258
           {
 
259
              throw FailedAdminPrivilegeException("Failed to launch with privileges");
 
260
           }
 
261
           
 
262
           for(bytesRead = 0; bytesRead > 0; bytesRead = read(fileno(commPipe), msgBuf, sizeof(msgBuf)));
 
263
       }
 
264
       catch(FailedAdminPrivilegeException& fape)
 
265
       {
 
266
           SInt16 outItemHit;
 
267
           char msgStr[256];
 
268
                   CFStringRef msgStrRef = CFStringCreateWithCString(NULL, fape.what(), kCFStringEncodingASCII);
 
269
                   CFStringGetPascalString(msgStrRef, (StringPtr)msgStr, 255, kCFStringEncodingASCII);
 
270
                   StandardAlert(kAlertStopAlert,
 
271
                                 "\pFailed to acquire admin privileges",
 
272
                                             (StringPtr)msgStr,
 
273
                                             NULL,
 
274
                                             &outItemHit);
 
275
       }
 
276
       catch(...)
 
277
       {
 
278
           SInt16 outItemHit;
 
279
                   StandardAlert(kAlertStopAlert,
 
280
                                 "\pUnexpected error",
 
281
                                             "\pUnexpected error when launching Active Directory Join Application",
 
282
                                             NULL,
 
283
                                             &outItemHit);
 
284
       }
 
285
       
 
286
       if (authRef)
 
287
       {
 
288
           AuthorizationFree(authRef, kAuthorizationFlagDefaults);
 
289
       }
 
290
    }
 
291
 
 
292
    return 0;
 
293
}
 
294
 
 
295