]> git.saurik.com Git - apple/mdnsresponder.git/blobdiff - mDNSWindows/ControlPanel/SharedSecret.cpp
mDNSResponder-544.tar.gz
[apple/mdnsresponder.git] / mDNSWindows / ControlPanel / SharedSecret.cpp
old mode 100755 (executable)
new mode 100644 (file)
index 29636df..3d19295
-/*\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.3  2005/04/06 02:04:49  shersche
-<rdar://problem/4066485> Registering with shared secret doesn't work
-
-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_key(_T(""))\r
-       , m_secret(_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_KEY, m_key );\r
-       DDX_Text(pDX, IDC_SECRET, m_secret );\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( CString zone )\r
-{\r
-       LSA_OBJECT_ATTRIBUTES   attrs;\r
-       LSA_HANDLE                              handle = NULL;\r
-       NTSTATUS                                res;\r
-       LSA_UNICODE_STRING              lucZoneName;\r
-       LSA_UNICODE_STRING              lucKeyName;\r
-       LSA_UNICODE_STRING              lucSecretName;\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 ( zone.ReverseFind( '.' ) != zone.GetLength() )\r
-       {\r
-               zone += '.';\r
-       }\r
-\r
-       if ( m_key.ReverseFind( '.' ) != m_key.GetLength() )\r
-       {\r
-               m_key += '.';\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( &lucZoneName, zone );\r
-       err = translate_errno( ok, errno_compat(), kUnknownErr );\r
-       require_noerr( err, exit );\r
\r
-       ok = InitLsaString( &lucKeyName, m_key );\r
-       err = translate_errno( ok, errno_compat(), kUnknownErr );\r
-       require_noerr( err, exit );\r
-\r
-       ok = InitLsaString( &lucSecretName, 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, &lucZoneName, &lucKeyName );\r
-       err = translate_errno( res == 0, LsaNtStatusToWinError( res ), kUnknownErr );\r
-       require_noerr( err, exit );\r
-\r
-       res = LsaStorePrivateData( handle, &lucKeyName, &lucSecretName );\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
+/* -*- Mode: C; tab-width: 4 -*-
+ *
+ * Copyright (c) 2002-2004 Apple Computer, Inc. All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+    
+// SharedSecret.cpp : implementation file
+//
+
+
+#include <Secret.h>
+#include "stdafx.h"
+#include "SharedSecret.h"
+#include <WinServices.h>
+
+#include <DebugServices.h>
+
+
+// SharedSecret dialog
+
+IMPLEMENT_DYNAMIC(CSharedSecret, CDialog)
+
+
+//---------------------------------------------------------------------------------------------------------------------------
+//     CSharedSecret::CSharedSecret
+//---------------------------------------------------------------------------------------------------------------------------
+
+CSharedSecret::CSharedSecret(CWnd* pParent /*=NULL*/)
+       : CDialog(CSharedSecret::IDD, pParent)
+       , m_key(_T(""))
+       , m_secret(_T(""))
+{
+}
+
+
+//---------------------------------------------------------------------------------------------------------------------------
+//     CSharedSecret::~CSharedSecret
+//---------------------------------------------------------------------------------------------------------------------------
+
+CSharedSecret::~CSharedSecret()
+{
+}
+
+
+//---------------------------------------------------------------------------------------------------------------------------
+//     CSharedSecret::DoDataExchange
+//---------------------------------------------------------------------------------------------------------------------------
+
+void CSharedSecret::DoDataExchange(CDataExchange* pDX)
+{
+       CDialog::DoDataExchange(pDX);
+       DDX_Text(pDX, IDC_KEY, m_key );
+       DDX_Text(pDX, IDC_SECRET, m_secret );
+}
+
+
+BEGIN_MESSAGE_MAP(CSharedSecret, CDialog)
+END_MESSAGE_MAP()
+
+
+//---------------------------------------------------------------------------------------------------------------------------
+//     CSharedSecret::Load
+//---------------------------------------------------------------------------------------------------------------------------
+
+void
+CSharedSecret::Load( CString zone )
+{
+       char    zoneUTF8[ 256 ];
+       char    outDomain[ 256 ];
+       char    outKey[ 256 ];
+       char    outSecret[ 256 ];
+
+       StringObjectToUTF8String( zone, zoneUTF8, sizeof( zoneUTF8 ) );
+
+       if ( LsaGetSecret( zoneUTF8, outDomain, sizeof( outDomain ) / sizeof( TCHAR ), outKey, sizeof( outKey ) / sizeof( TCHAR ), outSecret, sizeof( outSecret ) / sizeof( TCHAR ) ) )
+       {
+               m_key           = outKey;
+               m_secret        = outSecret;
+       }
+       else
+       {
+               m_key = zone;
+       }
+}
+
+
+//---------------------------------------------------------------------------------------------------------------------------
+//     CSharedSecret::Commit
+//---------------------------------------------------------------------------------------------------------------------------
+
+void
+CSharedSecret::Commit( CString zone )
+{
+       char    zoneUTF8[ 256 ];
+       char    keyUTF8[ 256 ];
+       char    secretUTF8[ 256 ];
+
+       StringObjectToUTF8String( zone, zoneUTF8, sizeof( zoneUTF8 ) );
+       StringObjectToUTF8String( m_key, keyUTF8, sizeof( keyUTF8 ) );
+       StringObjectToUTF8String( m_secret, secretUTF8, sizeof( secretUTF8 ) );
+
+       LsaSetSecret( zoneUTF8, keyUTF8, secretUTF8 );
+}