]> git.saurik.com Git - apple/mdnsresponder.git/blobdiff - mDNSWindows/ControlPanel/SharedSecret.cpp
mDNSResponder-107.tar.gz
[apple/mdnsresponder.git] / mDNSWindows / ControlPanel / SharedSecret.cpp
diff --git a/mDNSWindows/ControlPanel/SharedSecret.cpp b/mDNSWindows/ControlPanel/SharedSecret.cpp
new file mode 100755 (executable)
index 0000000..6c42aca
--- /dev/null
@@ -0,0 +1,198 @@
+/*\r
+ * Copyright (c) 2002-2004 Apple Computer, Inc. All rights reserved.\r
+ *\r
+ * @APPLE_LICENSE_HEADER_START@\r
+ * \r
+ * This file contains Original Code and/or Modifications of Original Code\r
+ * as defined in and that are subject to the Apple Public Source License\r
+ * Version 2.0 (the 'License'). You may not use this file except in\r
+ * compliance with the License. Please obtain a copy of the License at\r
+ * http://www.opensource.apple.com/apsl/ and read it before using this\r
+ * file.\r
+ * \r
+ * The Original Code and all software distributed under the License are\r
+ * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER\r
+ * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,\r
+ * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,\r
+ * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.\r
+ * Please see the License for the specific language governing rights and\r
+ * limitations under the License.\r
+ * \r
+ * @APPLE_LICENSE_HEADER_END@\r
+\r
+    Change History (most recent first):\r
+\r
+$Log: SharedSecret.cpp,v $
+Revision 1.2  2005/03/03 19:55:22  shersche
+<rdar://problem/4034481> ControlPanel source code isn't saving CVS log info
+\r
+\r
+*/\r
+\r
+    \r
+// SharedSecret.cpp : implementation file\r
+//\r
+\r
+#include "stdafx.h"\r
+#include "SharedSecret.h"\r
+\r
+#include <DebugServices.h>\r
+#include <ntsecapi.h>\r
+\r
+//---------------------------------------------------------------------------------------------------------------------------\r
+//     Private declarations\r
+//---------------------------------------------------------------------------------------------------------------------------\r
+\r
+static BOOL\r
+InitLsaString\r
+                       (\r
+                       PLSA_UNICODE_STRING     pLsaString,\r
+                       LPCWSTR                         pwszString\r
+                       );\r
+\r
+// SharedSecret dialog\r
+\r
+IMPLEMENT_DYNAMIC(CSharedSecret, CDialog)\r
+\r
+\r
+//---------------------------------------------------------------------------------------------------------------------------\r
+//     CSharedSecret::CSharedSecret\r
+//---------------------------------------------------------------------------------------------------------------------------\r
+\r
+CSharedSecret::CSharedSecret(CWnd* pParent /*=NULL*/)\r
+       : CDialog(CSharedSecret::IDD, pParent)\r
+       , m_secret(_T(""))\r
+       , m_secretName(_T(""))\r
+{\r
+}\r
+\r
+\r
+//---------------------------------------------------------------------------------------------------------------------------\r
+//     CSharedSecret::~CSharedSecret\r
+//---------------------------------------------------------------------------------------------------------------------------\r
+\r
+CSharedSecret::~CSharedSecret()\r
+{\r
+}\r
+\r
+\r
+//---------------------------------------------------------------------------------------------------------------------------\r
+//     CSharedSecret::DoDataExchange\r
+//---------------------------------------------------------------------------------------------------------------------------\r
+\r
+void CSharedSecret::DoDataExchange(CDataExchange* pDX)\r
+{\r
+       CDialog::DoDataExchange(pDX);\r
+       DDX_Text(pDX, IDC_SECRET, m_secret);\r
+       DDX_Text(pDX, IDC_SECRET_NAME, m_secretName);\r
+}\r
+\r
+\r
+BEGIN_MESSAGE_MAP(CSharedSecret, CDialog)\r
+END_MESSAGE_MAP()\r
+\r
+\r
+\r
+//---------------------------------------------------------------------------------------------------------------------------\r
+//     CSharedSecret::Commit\r
+//---------------------------------------------------------------------------------------------------------------------------\r
+\r
+void\r
+CSharedSecret::Commit()\r
+{\r
+       LSA_OBJECT_ATTRIBUTES   attrs;\r
+       LSA_HANDLE                              handle = NULL;\r
+       NTSTATUS                                res;\r
+       LSA_UNICODE_STRING              lucKeyName;\r
+       LSA_UNICODE_STRING              lucPrivateData;\r
+       BOOL                                    ok;\r
+       OSStatus                                err;\r
+\r
+       // If there isn't a trailing dot, add one because the mDNSResponder\r
+       // presents names with the trailing dot.\r
+\r
+       if ( m_secretName.ReverseFind( '.' ) != m_secretName.GetLength() )\r
+       {\r
+               m_secretName += '.';\r
+       }\r
+\r
+       // attrs are reserved, so initialize to zeroes.\r
+\r
+       ZeroMemory(&attrs, sizeof( attrs ) );\r
+\r
+       // Get a handle to the Policy object on the local system\r
+\r
+       res = LsaOpenPolicy( NULL, &attrs, POLICY_ALL_ACCESS, &handle );\r
+       err = translate_errno( res == 0, LsaNtStatusToWinError( res ), kUnknownErr );\r
+       require_noerr( err, exit );\r
+\r
+       // Intializing PLSA_UNICODE_STRING structures\r
+\r
+       ok = InitLsaString( &lucKeyName, m_secretName );\r
+       err = translate_errno( ok, errno_compat(), kUnknownErr );\r
+       require_noerr( err, exit );\r
+\r
+       ok = InitLsaString( &lucPrivateData, m_secret );\r
+       err = translate_errno( ok, errno_compat(), kUnknownErr );\r
+       require_noerr( err, exit );\r
+\r
+       // Store the private data.\r
+\r
+       res = LsaStorePrivateData( handle, &lucKeyName, &lucPrivateData );\r
+       err = translate_errno( res == 0, LsaNtStatusToWinError( res ), kUnknownErr );\r
+       require_noerr( err, exit );\r
+\r
+exit:\r
+\r
+       if ( handle )\r
+       {\r
+               LsaClose( handle );\r
+               handle = NULL;\r
+       }\r
+\r
+       return;\r
+}\r
+\r
+\r
+//---------------------------------------------------------------------------------------------------------------------------\r
+//     InitLsaString\r
+//---------------------------------------------------------------------------------------------------------------------------\r
+\r
+static BOOL\r
+InitLsaString\r
+               (\r
+               PLSA_UNICODE_STRING     pLsaString,\r
+               LPCWSTR                         pwszString\r
+               )\r
+{\r
+       size_t  dwLen   = 0;\r
+       BOOL    ret             = FALSE;\r
+       \r
+       if ( pLsaString == NULL )\r
+       {\r
+               goto exit;\r
+       }\r
+\r
+       if ( pwszString != NULL ) \r
+       {\r
+               dwLen = wcslen(pwszString);\r
+\r
+               // String is too large\r
+               if (dwLen > 0x7ffe)\r
+               {\r
+                       goto exit;\r
+               }\r
+       }\r
+\r
+       // Store the string.\r
+  \r
+       pLsaString->Buffer                      = (WCHAR *) pwszString;\r
+       pLsaString->Length                      = (USHORT) dwLen * sizeof(WCHAR);\r
+       pLsaString->MaximumLength       = (USHORT)(dwLen+1) * sizeof(WCHAR);\r
+\r
+       ret = TRUE;\r
+\r
+exit:\r
+\r
+       return ret;\r
+}\r