]> git.saurik.com Git - apple/mdnsresponder.git/blob - mDNSWindows/ControlPanel/SharedSecret.cpp
6c42aca11a0ae1f44d4b1d3e3f4530f67628cad7
[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.2 2005/03/03 19:55:22 shersche
27 <rdar://problem/4034481> ControlPanel source code isn't saving CVS log info
28
29
30 */
31
32
33 // SharedSecret.cpp : implementation file
34 //
35
36 #include "stdafx.h"
37 #include "SharedSecret.h"
38
39 #include <DebugServices.h>
40 #include <ntsecapi.h>
41
42 //---------------------------------------------------------------------------------------------------------------------------
43 // Private declarations
44 //---------------------------------------------------------------------------------------------------------------------------
45
46 static BOOL
47 InitLsaString
48 (
49 PLSA_UNICODE_STRING pLsaString,
50 LPCWSTR pwszString
51 );
52
53 // SharedSecret dialog
54
55 IMPLEMENT_DYNAMIC(CSharedSecret, CDialog)
56
57
58 //---------------------------------------------------------------------------------------------------------------------------
59 // CSharedSecret::CSharedSecret
60 //---------------------------------------------------------------------------------------------------------------------------
61
62 CSharedSecret::CSharedSecret(CWnd* pParent /*=NULL*/)
63 : CDialog(CSharedSecret::IDD, pParent)
64 , m_secret(_T(""))
65 , m_secretName(_T(""))
66 {
67 }
68
69
70 //---------------------------------------------------------------------------------------------------------------------------
71 // CSharedSecret::~CSharedSecret
72 //---------------------------------------------------------------------------------------------------------------------------
73
74 CSharedSecret::~CSharedSecret()
75 {
76 }
77
78
79 //---------------------------------------------------------------------------------------------------------------------------
80 // CSharedSecret::DoDataExchange
81 //---------------------------------------------------------------------------------------------------------------------------
82
83 void CSharedSecret::DoDataExchange(CDataExchange* pDX)
84 {
85 CDialog::DoDataExchange(pDX);
86 DDX_Text(pDX, IDC_SECRET, m_secret);
87 DDX_Text(pDX, IDC_SECRET_NAME, m_secretName);
88 }
89
90
91 BEGIN_MESSAGE_MAP(CSharedSecret, CDialog)
92 END_MESSAGE_MAP()
93
94
95
96 //---------------------------------------------------------------------------------------------------------------------------
97 // CSharedSecret::Commit
98 //---------------------------------------------------------------------------------------------------------------------------
99
100 void
101 CSharedSecret::Commit()
102 {
103 LSA_OBJECT_ATTRIBUTES attrs;
104 LSA_HANDLE handle = NULL;
105 NTSTATUS res;
106 LSA_UNICODE_STRING lucKeyName;
107 LSA_UNICODE_STRING lucPrivateData;
108 BOOL ok;
109 OSStatus err;
110
111 // If there isn't a trailing dot, add one because the mDNSResponder
112 // presents names with the trailing dot.
113
114 if ( m_secretName.ReverseFind( '.' ) != m_secretName.GetLength() )
115 {
116 m_secretName += '.';
117 }
118
119 // attrs are reserved, so initialize to zeroes.
120
121 ZeroMemory(&attrs, sizeof( attrs ) );
122
123 // Get a handle to the Policy object on the local system
124
125 res = LsaOpenPolicy( NULL, &attrs, POLICY_ALL_ACCESS, &handle );
126 err = translate_errno( res == 0, LsaNtStatusToWinError( res ), kUnknownErr );
127 require_noerr( err, exit );
128
129 // Intializing PLSA_UNICODE_STRING structures
130
131 ok = InitLsaString( &lucKeyName, m_secretName );
132 err = translate_errno( ok, errno_compat(), kUnknownErr );
133 require_noerr( err, exit );
134
135 ok = InitLsaString( &lucPrivateData, m_secret );
136 err = translate_errno( ok, errno_compat(), kUnknownErr );
137 require_noerr( err, exit );
138
139 // Store the private data.
140
141 res = LsaStorePrivateData( handle, &lucKeyName, &lucPrivateData );
142 err = translate_errno( res == 0, LsaNtStatusToWinError( res ), kUnknownErr );
143 require_noerr( err, exit );
144
145 exit:
146
147 if ( handle )
148 {
149 LsaClose( handle );
150 handle = NULL;
151 }
152
153 return;
154 }
155
156
157 //---------------------------------------------------------------------------------------------------------------------------
158 // InitLsaString
159 //---------------------------------------------------------------------------------------------------------------------------
160
161 static BOOL
162 InitLsaString
163 (
164 PLSA_UNICODE_STRING pLsaString,
165 LPCWSTR pwszString
166 )
167 {
168 size_t dwLen = 0;
169 BOOL ret = FALSE;
170
171 if ( pLsaString == NULL )
172 {
173 goto exit;
174 }
175
176 if ( pwszString != NULL )
177 {
178 dwLen = wcslen(pwszString);
179
180 // String is too large
181 if (dwLen > 0x7ffe)
182 {
183 goto exit;
184 }
185 }
186
187 // Store the string.
188
189 pLsaString->Buffer = (WCHAR *) pwszString;
190 pLsaString->Length = (USHORT) dwLen * sizeof(WCHAR);
191 pLsaString->MaximumLength = (USHORT)(dwLen+1) * sizeof(WCHAR);
192
193 ret = TRUE;
194
195 exit:
196
197 return ret;
198 }