]> git.saurik.com Git - apple/mdnsresponder.git/blob - mDNSWindows/ControlPanel/SharedSecret.cpp
mDNSResponder-107.5.tar.gz
[apple/mdnsresponder.git] / mDNSWindows / ControlPanel / SharedSecret.cpp
1 /*
2 * Copyright (c) 2002-2004 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22
23 Change History (most recent first):
24
25 $Log: SharedSecret.cpp,v $
26 Revision 1.4 2005/10/18 06:13:41 herscher
27 <rdar://problem/4192119> Prepend "$" to key name to ensure that secure updates work if the domain name and key name are the same
28
29 Revision 1.3 2005/04/06 02:04:49 shersche
30 <rdar://problem/4066485> Registering with shared secret doesn't work
31
32 Revision 1.2 2005/03/03 19:55:22 shersche
33 <rdar://problem/4034481> ControlPanel source code isn't saving CVS log info
34
35
36 */
37
38
39 // SharedSecret.cpp : implementation file
40 //
41
42 #include "stdafx.h"
43 #include "SharedSecret.h"
44
45 #include <DebugServices.h>
46 #include <ntsecapi.h>
47
48 //---------------------------------------------------------------------------------------------------------------------------
49 // Private declarations
50 //---------------------------------------------------------------------------------------------------------------------------
51
52 static BOOL
53 InitLsaString
54 (
55 PLSA_UNICODE_STRING pLsaString,
56 LPCWSTR pwszString
57 );
58
59 // SharedSecret dialog
60
61 IMPLEMENT_DYNAMIC(CSharedSecret, CDialog)
62
63
64 //---------------------------------------------------------------------------------------------------------------------------
65 // CSharedSecret::CSharedSecret
66 //---------------------------------------------------------------------------------------------------------------------------
67
68 CSharedSecret::CSharedSecret(CWnd* pParent /*=NULL*/)
69 : CDialog(CSharedSecret::IDD, pParent)
70 , m_key(_T(""))
71 , m_secret(_T(""))
72 {
73 }
74
75
76 //---------------------------------------------------------------------------------------------------------------------------
77 // CSharedSecret::~CSharedSecret
78 //---------------------------------------------------------------------------------------------------------------------------
79
80 CSharedSecret::~CSharedSecret()
81 {
82 }
83
84
85 //---------------------------------------------------------------------------------------------------------------------------
86 // CSharedSecret::DoDataExchange
87 //---------------------------------------------------------------------------------------------------------------------------
88
89 void CSharedSecret::DoDataExchange(CDataExchange* pDX)
90 {
91 CDialog::DoDataExchange(pDX);
92 DDX_Text(pDX, IDC_KEY, m_key );
93 DDX_Text(pDX, IDC_SECRET, m_secret );
94 }
95
96
97 BEGIN_MESSAGE_MAP(CSharedSecret, CDialog)
98 END_MESSAGE_MAP()
99
100
101
102 //---------------------------------------------------------------------------------------------------------------------------
103 // CSharedSecret::Commit
104 //---------------------------------------------------------------------------------------------------------------------------
105
106 void
107 CSharedSecret::Commit( CString zone )
108 {
109 LSA_OBJECT_ATTRIBUTES attrs;
110 LSA_HANDLE handle = NULL;
111 NTSTATUS res;
112 LSA_UNICODE_STRING lucZoneName;
113 LSA_UNICODE_STRING lucKeyName;
114 LSA_UNICODE_STRING lucSecretName;
115 BOOL ok;
116 OSStatus err;
117
118 // If there isn't a trailing dot, add one because the mDNSResponder
119 // presents names with the trailing dot.
120
121 if ( zone.ReverseFind( '.' ) != zone.GetLength() )
122 {
123 zone += '.';
124 }
125
126 if ( m_key.ReverseFind( '.' ) != m_key.GetLength() )
127 {
128 m_key += '.';
129 }
130
131 // <rdar://problem/4192119>
132 //
133 // Prepend "$" to the key name, so that there will
134 // be no conflict between the zone name and the key
135 // name
136
137 m_key.Insert( 0, L"$" );
138
139 // attrs are reserved, so initialize to zeroes.
140
141 ZeroMemory( &attrs, sizeof( attrs ) );
142
143 // Get a handle to the Policy object on the local system
144
145 res = LsaOpenPolicy( NULL, &attrs, POLICY_ALL_ACCESS, &handle );
146 err = translate_errno( res == 0, LsaNtStatusToWinError( res ), kUnknownErr );
147 require_noerr( err, exit );
148
149 // Intializing PLSA_UNICODE_STRING structures
150
151 ok = InitLsaString( &lucZoneName, zone );
152 err = translate_errno( ok, errno_compat(), kUnknownErr );
153 require_noerr( err, exit );
154
155 ok = InitLsaString( &lucKeyName, m_key );
156 err = translate_errno( ok, errno_compat(), kUnknownErr );
157 require_noerr( err, exit );
158
159 ok = InitLsaString( &lucSecretName, m_secret );
160 err = translate_errno( ok, errno_compat(), kUnknownErr );
161 require_noerr( err, exit );
162
163 // Store the private data.
164
165 res = LsaStorePrivateData( handle, &lucZoneName, &lucKeyName );
166 err = translate_errno( res == 0, LsaNtStatusToWinError( res ), kUnknownErr );
167 require_noerr( err, exit );
168
169 res = LsaStorePrivateData( handle, &lucKeyName, &lucSecretName );
170 err = translate_errno( res == 0, LsaNtStatusToWinError( res ), kUnknownErr );
171 require_noerr( err, exit );
172
173 exit:
174
175 if ( handle )
176 {
177 LsaClose( handle );
178 handle = NULL;
179 }
180
181 return;
182 }
183
184
185 //---------------------------------------------------------------------------------------------------------------------------
186 // InitLsaString
187 //---------------------------------------------------------------------------------------------------------------------------
188
189 static BOOL
190 InitLsaString
191 (
192 PLSA_UNICODE_STRING pLsaString,
193 LPCWSTR pwszString
194 )
195 {
196 size_t dwLen = 0;
197 BOOL ret = FALSE;
198
199 if ( pLsaString == NULL )
200 {
201 goto exit;
202 }
203
204 if ( pwszString != NULL )
205 {
206 dwLen = wcslen(pwszString);
207
208 // String is too large
209 if (dwLen > 0x7ffe)
210 {
211 goto exit;
212 }
213 }
214
215 // Store the string.
216
217 pLsaString->Buffer = (WCHAR *) pwszString;
218 pLsaString->Length = (USHORT) dwLen * sizeof(WCHAR);
219 pLsaString->MaximumLength = (USHORT)(dwLen+1) * sizeof(WCHAR);
220
221 ret = TRUE;
222
223 exit:
224
225 return ret;
226 }