]> git.saurik.com Git - apple/mdnsresponder.git/blob - mDNSWindows/ControlPanel/SharedSecret.cpp
29636df0b5f6d1b786a35f9d7f5daccf14e2df61
[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.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 // attrs are reserved, so initialize to zeroes.
129
130 ZeroMemory( &attrs, sizeof( attrs ) );
131
132 // Get a handle to the Policy object on the local system
133
134 res = LsaOpenPolicy( NULL, &attrs, POLICY_ALL_ACCESS, &handle );
135 err = translate_errno( res == 0, LsaNtStatusToWinError( res ), kUnknownErr );
136 require_noerr( err, exit );
137
138 // Intializing PLSA_UNICODE_STRING structures
139
140 ok = InitLsaString( &lucZoneName, zone );
141 err = translate_errno( ok, errno_compat(), kUnknownErr );
142 require_noerr( err, exit );
143
144 ok = InitLsaString( &lucKeyName, m_key );
145 err = translate_errno( ok, errno_compat(), kUnknownErr );
146 require_noerr( err, exit );
147
148 ok = InitLsaString( &lucSecretName, m_secret );
149 err = translate_errno( ok, errno_compat(), kUnknownErr );
150 require_noerr( err, exit );
151
152 // Store the private data.
153
154 res = LsaStorePrivateData( handle, &lucZoneName, &lucKeyName );
155 err = translate_errno( res == 0, LsaNtStatusToWinError( res ), kUnknownErr );
156 require_noerr( err, exit );
157
158 res = LsaStorePrivateData( handle, &lucKeyName, &lucSecretName );
159 err = translate_errno( res == 0, LsaNtStatusToWinError( res ), kUnknownErr );
160 require_noerr( err, exit );
161
162 exit:
163
164 if ( handle )
165 {
166 LsaClose( handle );
167 handle = NULL;
168 }
169
170 return;
171 }
172
173
174 //---------------------------------------------------------------------------------------------------------------------------
175 // InitLsaString
176 //---------------------------------------------------------------------------------------------------------------------------
177
178 static BOOL
179 InitLsaString
180 (
181 PLSA_UNICODE_STRING pLsaString,
182 LPCWSTR pwszString
183 )
184 {
185 size_t dwLen = 0;
186 BOOL ret = FALSE;
187
188 if ( pLsaString == NULL )
189 {
190 goto exit;
191 }
192
193 if ( pwszString != NULL )
194 {
195 dwLen = wcslen(pwszString);
196
197 // String is too large
198 if (dwLen > 0x7ffe)
199 {
200 goto exit;
201 }
202 }
203
204 // Store the string.
205
206 pLsaString->Buffer = (WCHAR *) pwszString;
207 pLsaString->Length = (USHORT) dwLen * sizeof(WCHAR);
208 pLsaString->MaximumLength = (USHORT)(dwLen+1) * sizeof(WCHAR);
209
210 ret = TRUE;
211
212 exit:
213
214 return ret;
215 }