1 /* -*- Mode: C; tab-width: 4 -*-
3 * Copyright (c) 2002-2004 Apple Computer, Inc. All rights reserved.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
19 #include "RegistrationPage.h"
22 #include "ConfigPropertySheet.h"
25 #include <ClientCommon.h>
27 #include <WinServices.h>
29 #define MAX_KEY_LENGTH 255
32 IMPLEMENT_DYNCREATE(CRegistrationPage
, CPropertyPage
)
34 //---------------------------------------------------------------------------------------------------------------------------
35 // CRegistrationPage::CRegistrationPage
36 //---------------------------------------------------------------------------------------------------------------------------
38 CRegistrationPage::CRegistrationPage()
40 CPropertyPage(CRegistrationPage::IDD
),
41 m_ignoreChanges( false ),
42 m_hostnameSetupKey( NULL
),
43 m_registrationSetupKey( NULL
),
46 //{{AFX_DATA_INIT(CRegistrationPage)
51 err
= RegCreateKeyEx( HKEY_LOCAL_MACHINE
, kServiceParametersNode L
"\\DynDNS\\Setup\\Hostnames", 0,
52 NULL
, REG_OPTION_NON_VOLATILE
, KEY_READ
|KEY_WRITE
|KEY_WOW64_32KEY
, NULL
, &m_hostnameSetupKey
, NULL
);
55 err
= RegCreateKeyEx( HKEY_LOCAL_MACHINE
, kServiceParametersNode L
"\\DynDNS\\Setup\\" kServiceDynDNSRegistrationDomains
, 0,
56 NULL
, REG_OPTION_NON_VOLATILE
, KEY_READ
|KEY_WRITE
|KEY_WOW64_32KEY
, NULL
, &m_registrationSetupKey
, NULL
);
59 err
= RegCreateKeyEx( HKEY_LOCAL_MACHINE
, kServiceParametersNode L
"\\DynDNS\\State\\Hostnames", 0,
60 NULL
, REG_OPTION_NON_VOLATILE
, KEY_READ
|KEY_WRITE
|KEY_WOW64_32KEY
, NULL
, &m_statusKey
, NULL
);
66 CRegistrationPage::~CRegistrationPage()
68 if ( m_hostnameSetupKey
)
70 RegCloseKey( m_hostnameSetupKey
);
71 m_hostnameSetupKey
= NULL
;
74 if ( m_registrationSetupKey
)
76 RegCloseKey( m_registrationSetupKey
);
77 m_registrationSetupKey
= NULL
;
82 RegCloseKey( m_statusKey
);
88 //---------------------------------------------------------------------------------------------------------------------------
89 // CRegistrationPage::DoDataExchange
90 //---------------------------------------------------------------------------------------------------------------------------
92 void CRegistrationPage::DoDataExchange(CDataExchange
* pDX
)
94 CPropertyPage::DoDataExchange(pDX
);
95 //{{AFX_DATA_MAP(CRegistrationPage)
97 DDX_Control(pDX
, IDC_HOSTNAME
, m_hostnameControl
);
98 DDX_Control(pDX
, IDC_USERNAME
, m_usernameControl
);
99 DDX_Control(pDX
, IDC_PASSWORD
, m_passwordControl
);
100 DDX_Control(pDX
, IDC_ADVERTISE_SERVICES
, m_advertiseServices
);
103 BEGIN_MESSAGE_MAP(CRegistrationPage
, CPropertyPage
)
104 //{{AFX_MSG_MAP(CRegistrationPage)
106 ON_EN_CHANGE(IDC_HOSTNAME
, OnEnChangeHostname
)
107 ON_EN_CHANGE(IDC_USERNAME
, OnEnChangeUsername
)
108 ON_EN_CHANGE(IDC_PASSWORD
, OnEnChangePassword
)
109 ON_BN_CLICKED(IDC_ADVERTISE_SERVICES
, OnBnClickedAdvertiseServices
)
113 //---------------------------------------------------------------------------------------------------------------------------
114 // CRegistrationPage::OnEnChangedHostname
115 //---------------------------------------------------------------------------------------------------------------------------
117 void CRegistrationPage::OnEnChangeHostname()
119 if ( !m_ignoreChanges
)
126 //---------------------------------------------------------------------------------------------------------------------------
127 // CRegistrationPage::OnEnChangedUsername
128 //---------------------------------------------------------------------------------------------------------------------------
130 void CRegistrationPage::OnEnChangeUsername()
132 if ( !m_ignoreChanges
)
139 //---------------------------------------------------------------------------------------------------------------------------
140 // CRegistrationPage::OnEnChangedPassword
141 //---------------------------------------------------------------------------------------------------------------------------
143 void CRegistrationPage::OnEnChangePassword()
145 if ( !m_ignoreChanges
)
152 //---------------------------------------------------------------------------------------------------------------------------
153 // CRegistrationPage::OnBnClickedAdvertiseServices
154 //---------------------------------------------------------------------------------------------------------------------------
156 void CRegistrationPage::OnBnClickedAdvertiseServices()
158 if ( !m_ignoreChanges
)
165 //---------------------------------------------------------------------------------------------------------------------------
166 // CRegistrationPage::SetModified
167 //---------------------------------------------------------------------------------------------------------------------------
169 void CRegistrationPage::SetModified( BOOL bChanged
)
171 m_modified
= bChanged
? true : false;
173 CPropertyPage::SetModified( bChanged
);
177 //---------------------------------------------------------------------------------------------------------------------------
178 // CRegistrationPage::OnSetActive
179 //---------------------------------------------------------------------------------------------------------------------------
182 CRegistrationPage::OnSetActive()
184 TCHAR name
[kDNSServiceMaxDomainName
+ 1];
185 DWORD nameLen
= ( kDNSServiceMaxDomainName
+ 1 ) * sizeof( TCHAR
);
188 BOOL b
= CPropertyPage::OnSetActive();
190 m_ignoreChanges
= true;
193 if ( m_hostnameSetupKey
)
195 err
= RegQueryValueEx( m_hostnameSetupKey
, L
"", NULL
, NULL
, (LPBYTE
) name
, &nameLen
);
199 char hostnameUTF8
[ 256 ];
200 char outDomain
[ 256 ];
201 char outUsername
[ 256 ];
202 char outPassword
[ 256 ];
203 CString hostname
= name
;
207 m_hostnameControl
.SetWindowText( hostname
);
209 StringObjectToUTF8String( hostname
, hostnameUTF8
, sizeof( hostnameUTF8
) );
211 if ( LsaGetSecret( hostnameUTF8
, outDomain
, sizeof( outDomain
) / sizeof( TCHAR
), outUsername
, sizeof( outUsername
) / sizeof( TCHAR
), outPassword
, sizeof( outPassword
) / sizeof( TCHAR
) ) )
213 username
= outUsername
;
214 m_usernameControl
.SetWindowText( username
);
216 password
= outPassword
;
217 m_passwordControl
.SetWindowText( password
);
222 m_advertiseServices
.SetCheck( 0 );
224 if ( m_registrationSetupKey
)
229 TCHAR subKeyName
[MAX_KEY_LENGTH
];
235 err
= RegQueryInfoKey( m_registrationSetupKey
, NULL
, NULL
, NULL
, &cSubKeys
, &cbMaxSubKey
, &cchMaxClass
, NULL
, NULL
, NULL
, NULL
, NULL
);
240 dwSize
= MAX_KEY_LENGTH
;
242 err
= RegEnumKeyEx( m_registrationSetupKey
, 0, subKeyName
, &dwSize
, NULL
, NULL
, NULL
, NULL
);
245 err
= RegOpenKey( m_registrationSetupKey
, subKeyName
, &subKey
);
248 dwSize
= sizeof( DWORD
);
249 err
= RegQueryValueEx( subKey
, L
"Enabled", NULL
, NULL
, (LPBYTE
) &enabled
, &dwSize
);
250 if ( !err
&& enabled
)
252 m_advertiseServices
.SetCheck( enabled
);
255 RegCloseKey( subKey
);
263 m_ignoreChanges
= false;
269 //---------------------------------------------------------------------------------------------------------------------------
270 // CRegistrationPage::OnOK
271 //---------------------------------------------------------------------------------------------------------------------------
274 CRegistrationPage::OnOK()
283 //---------------------------------------------------------------------------------------------------------------------------
284 // CRegistrationPage::Commit
285 //---------------------------------------------------------------------------------------------------------------------------
288 CRegistrationPage::Commit()
291 char hostnameUTF8
[ 256 ];
293 char usernameUTF8
[ 256 ];
295 char passwordUTF8
[ 256 ];
300 m_hostnameControl
.GetWindowText( hostname
);
301 hostname
.MakeLower();
302 hostname
.TrimRight( '.' );
303 StringObjectToUTF8String( hostname
, hostnameUTF8
, sizeof( hostnameUTF8
) );
305 m_usernameControl
.GetWindowText( username
);
306 m_passwordControl
.GetWindowText( password
);
308 if ( username
.GetLength() && password
.GetLength() )
310 StringObjectToUTF8String( username
, usernameUTF8
, sizeof( usernameUTF8
) );
311 StringObjectToUTF8String( password
, passwordUTF8
, sizeof( passwordUTF8
) );
315 if ( m_hostnameSetupKey
!= NULL
)
317 err
= RegSetValueEx( m_hostnameSetupKey
, L
"", 0, REG_SZ
, (LPBYTE
) (LPCTSTR
) hostname
, ( hostname
.GetLength() + 1 ) * sizeof( TCHAR
) );
318 require_noerr( err
, exit
);
320 err
= RegSetValueEx( m_hostnameSetupKey
, L
"Enabled", 0, REG_DWORD
, (LPBYTE
) &enabled
, sizeof( DWORD
) );
321 require_noerr( err
, exit
);
325 LsaSetSecret( hostnameUTF8
, usernameUTF8
, passwordUTF8
);
329 if ( m_registrationSetupKey
!= NULL
)
331 TCHAR subKeyName
[MAX_KEY_LENGTH
];
337 OSStatus err
= kNoErr
;
339 // First, remove all the entries that are there
341 err
= RegQueryInfoKey( m_registrationSetupKey
, NULL
, NULL
, NULL
, &cSubKeys
, &cbMaxSubKey
, &cchMaxClass
, NULL
, NULL
, NULL
, NULL
, NULL
);
344 for ( i
= 0; i
< (int) cSubKeys
; i
++ )
346 dwSize
= MAX_KEY_LENGTH
;
348 err
= RegEnumKeyEx( m_registrationSetupKey
, 0, subKeyName
, &dwSize
, NULL
, NULL
, NULL
, NULL
);
349 require_noerr( err
, exit
);
351 err
= RegDeleteKey( m_registrationSetupKey
, subKeyName
);
352 require_noerr( err
, exit
);
356 if ( m_advertiseServices
.GetCheck() )
358 const char * domainUTF8
;
363 domainUTF8
= GetNextLabel( hostnameUTF8
, label
);
366 err
= RegCreateKeyEx( m_registrationSetupKey
, domain
, 0,
367 NULL
, REG_OPTION_NON_VOLATILE
, KEY_READ
|KEY_WRITE
|KEY_WOW64_32KEY
, NULL
, &subKey
, NULL
);
370 err
= RegSetValueEx( subKey
, L
"Enabled", 0, REG_DWORD
, (LPBYTE
) &enabled
, sizeof( DWORD
) );
373 RegCloseKey( subKey
);
379 LsaSetSecret( domainUTF8
, usernameUTF8
, passwordUTF8
);