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