]> git.saurik.com Git - apple/mdnsresponder.git/blame - mDNSWindows/ControlPanel/SharedSecret.cpp
mDNSResponder-170.tar.gz
[apple/mdnsresponder.git] / mDNSWindows / ControlPanel / SharedSecret.cpp
CommitLineData
67c8f8a1
A
1/* -*- Mode: C; tab-width: 4 -*-
2 *
05292456
A
3 * Copyright (c) 2002-2004 Apple Computer, Inc. All rights reserved.
4 *
67c8f8a1
A
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
05292456 8 *
67c8f8a1 9 * http://www.apache.org/licenses/LICENSE-2.0
05292456 10 *
67c8f8a1
A
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
05292456 15 * limitations under the License.
05292456
A
16
17 Change History (most recent first):
f5e6e86c 18
05292456 19$Log: SharedSecret.cpp,v $
67c8f8a1
A
20Revision 1.6 2007/06/12 20:06:06 herscher
21<rdar://problem/5263387> ControlPanel was inadvertently adding a trailing dot to all key names.
22
23Revision 1.5 2006/08/14 23:25:28 cheshire
24Re-licensed mDNSResponder daemon source code under Apache License, Version 2.0
25
4aea607d
A
26Revision 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
29Revision 1.3 2005/04/06 02:04:49 shersche
30<rdar://problem/4066485> Registering with shared secret doesn't work
31
7cb34e5c
A
32Revision 1.2 2005/03/03 19:55:22 shersche
33<rdar://problem/4034481> ControlPanel source code isn't saving CVS log info
05292456
A
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
52static BOOL
53InitLsaString
54 (
55 PLSA_UNICODE_STRING pLsaString,
56 LPCWSTR pwszString
57 );
58
59// SharedSecret dialog
60
61IMPLEMENT_DYNAMIC(CSharedSecret, CDialog)
62
63
64//---------------------------------------------------------------------------------------------------------------------------
65// CSharedSecret::CSharedSecret
66//---------------------------------------------------------------------------------------------------------------------------
67
68CSharedSecret::CSharedSecret(CWnd* pParent /*=NULL*/)
69 : CDialog(CSharedSecret::IDD, pParent)
4aea607d 70 , m_key(_T(""))
05292456 71 , m_secret(_T(""))
05292456
A
72{
73}
74
75
76//---------------------------------------------------------------------------------------------------------------------------
77// CSharedSecret::~CSharedSecret
78//---------------------------------------------------------------------------------------------------------------------------
79
80CSharedSecret::~CSharedSecret()
81{
82}
83
84
85//---------------------------------------------------------------------------------------------------------------------------
86// CSharedSecret::DoDataExchange
87//---------------------------------------------------------------------------------------------------------------------------
88
89void CSharedSecret::DoDataExchange(CDataExchange* pDX)
90{
91 CDialog::DoDataExchange(pDX);
4aea607d
A
92 DDX_Text(pDX, IDC_KEY, m_key );
93 DDX_Text(pDX, IDC_SECRET, m_secret );
05292456
A
94}
95
96
97BEGIN_MESSAGE_MAP(CSharedSecret, CDialog)
98END_MESSAGE_MAP()
99
100
101
102//---------------------------------------------------------------------------------------------------------------------------
103// CSharedSecret::Commit
104//---------------------------------------------------------------------------------------------------------------------------
105
106void
4aea607d 107CSharedSecret::Commit( CString zone )
05292456
A
108{
109 LSA_OBJECT_ATTRIBUTES attrs;
110 LSA_HANDLE handle = NULL;
111 NTSTATUS res;
4aea607d 112 LSA_UNICODE_STRING lucZoneName;
05292456 113 LSA_UNICODE_STRING lucKeyName;
4aea607d 114 LSA_UNICODE_STRING lucSecretName;
05292456
A
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
67c8f8a1 121 if ( zone.ReverseFind( '.' ) != ( zone.GetLength() - 1 ) )
4aea607d
A
122 {
123 zone += '.';
124 }
125
67c8f8a1 126 if ( m_key.ReverseFind( '.' ) != ( m_key.GetLength() - 1 ) )
05292456 127 {
4aea607d 128 m_key += '.';
05292456
A
129 }
130
7df24c4d 131 // <rdar://problem/4192119>
4aea607d
A
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
05292456
A
139 // attrs are reserved, so initialize to zeroes.
140
4aea607d 141 ZeroMemory( &attrs, sizeof( attrs ) );
05292456
A
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
4aea607d
A
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 );
05292456
A
156 err = translate_errno( ok, errno_compat(), kUnknownErr );
157 require_noerr( err, exit );
158
4aea607d 159 ok = InitLsaString( &lucSecretName, m_secret );
05292456
A
160 err = translate_errno( ok, errno_compat(), kUnknownErr );
161 require_noerr( err, exit );
162
163 // Store the private data.
164
4aea607d
A
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 );
05292456
A
170 err = translate_errno( res == 0, LsaNtStatusToWinError( res ), kUnknownErr );
171 require_noerr( err, exit );
172
173exit:
174
175 if ( handle )
176 {
177 LsaClose( handle );
178 handle = NULL;
179 }
180
181 return;
182}
183
184
185//---------------------------------------------------------------------------------------------------------------------------
186// InitLsaString
187//---------------------------------------------------------------------------------------------------------------------------
188
189static BOOL
190InitLsaString
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
223exit:
224
225 return ret;
226}