@@ -3065,300 +3065,6 @@ internal byte[] GetBytes(object o, out Format format, out int maxSize)
30653065 }
30663066 } // SqlConnection
30673067
3068- // TODO: This really belongs in it's own source file...
3069- //
3070- // This is a private interface for the SQL Debugger
3071- // You must not change the guid for this coclass
3072- // or the iid for the ISQLDebug interface
3073- //
3074- /// <include file='../../../../../../../doc/snippets/Microsoft.Data.SqlClient/SqlDebugging.xml' path='docs/members[@name="SQLDebugging"]/SQLDebugging/*'/>
3075- [
3076- ComVisible ( true ) ,
3077- ClassInterface ( ClassInterfaceType . None ) ,
3078- Guid ( "afef65ad-4577-447a-a148-83acadd3d4b9" ) ,
3079- ]
3080- [ System . Security . Permissions . PermissionSetAttribute ( System . Security . Permissions . SecurityAction . LinkDemand , Name = "FullTrust" ) ]
3081- public sealed class SQLDebugging : ISQLDebug
3082- {
3083-
3084- // Security stuff
3085- const int STANDARD_RIGHTS_REQUIRED = ( 0x000F0000 ) ;
3086- const int DELETE = ( 0x00010000 ) ;
3087- const int READ_CONTROL = ( 0x00020000 ) ;
3088- const int WRITE_DAC = ( 0x00040000 ) ;
3089- const int WRITE_OWNER = ( 0x00080000 ) ;
3090- const int SYNCHRONIZE = ( 0x00100000 ) ;
3091- const int FILE_ALL_ACCESS = ( STANDARD_RIGHTS_REQUIRED | SYNCHRONIZE | 0x000001FF ) ;
3092- const uint GENERIC_READ = ( 0x80000000 ) ;
3093- const uint GENERIC_WRITE = ( 0x40000000 ) ;
3094- const uint GENERIC_EXECUTE = ( 0x20000000 ) ;
3095- const uint GENERIC_ALL = ( 0x10000000 ) ;
3096-
3097- const int SECURITY_DESCRIPTOR_REVISION = ( 1 ) ;
3098- const int ACL_REVISION = ( 2 ) ;
3099-
3100- const int SECURITY_AUTHENTICATED_USER_RID = ( 0x0000000B ) ;
3101- const int SECURITY_LOCAL_SYSTEM_RID = ( 0x00000012 ) ;
3102- const int SECURITY_BUILTIN_DOMAIN_RID = ( 0x00000020 ) ;
3103- const int SECURITY_WORLD_RID = ( 0x00000000 ) ;
3104- const byte SECURITY_NT_AUTHORITY = 5 ;
3105- const int DOMAIN_GROUP_RID_ADMINS = ( 0x00000200 ) ;
3106- const int DOMAIN_ALIAS_RID_ADMINS = ( 0x00000220 ) ;
3107-
3108- const int sizeofSECURITY_ATTRIBUTES = 12 ; // sizeof(SECURITY_ATTRIBUTES);
3109- const int sizeofSECURITY_DESCRIPTOR = 20 ; // sizeof(SECURITY_DESCRIPTOR);
3110- const int sizeofACCESS_ALLOWED_ACE = 12 ; // sizeof(ACCESS_ALLOWED_ACE);
3111- const int sizeofACCESS_DENIED_ACE = 12 ; // sizeof(ACCESS_DENIED_ACE);
3112- const int sizeofSID_IDENTIFIER_AUTHORITY = 6 ; // sizeof(SID_IDENTIFIER_AUTHORITY)
3113- const int sizeofACL = 8 ; // sizeof(ACL);
3114-
3115- private IntPtr CreateSD ( ref IntPtr pDacl )
3116- {
3117- IntPtr pSecurityDescriptor = IntPtr . Zero ;
3118- IntPtr pUserSid = IntPtr . Zero ;
3119- IntPtr pAdminSid = IntPtr . Zero ;
3120- IntPtr pNtAuthority = IntPtr . Zero ;
3121- int cbAcl = 0 ;
3122- bool status = false ;
3123-
3124- pNtAuthority = Marshal . AllocHGlobal ( sizeofSID_IDENTIFIER_AUTHORITY ) ;
3125- if ( pNtAuthority == IntPtr . Zero )
3126- goto cleanup ;
3127- Marshal . WriteInt32 ( pNtAuthority , 0 , 0 ) ;
3128- Marshal . WriteByte ( pNtAuthority , 4 , 0 ) ;
3129- Marshal . WriteByte ( pNtAuthority , 5 , SECURITY_NT_AUTHORITY ) ;
3130-
3131- status =
3132- NativeMethods . AllocateAndInitializeSid (
3133- pNtAuthority ,
3134- ( byte ) 1 ,
3135- SECURITY_AUTHENTICATED_USER_RID ,
3136- 0 ,
3137- 0 ,
3138- 0 ,
3139- 0 ,
3140- 0 ,
3141- 0 ,
3142- 0 ,
3143- ref pUserSid ) ;
3144-
3145- if ( ! status || pUserSid == IntPtr . Zero )
3146- {
3147- goto cleanup ;
3148- }
3149- status =
3150- NativeMethods . AllocateAndInitializeSid (
3151- pNtAuthority ,
3152- ( byte ) 2 ,
3153- SECURITY_BUILTIN_DOMAIN_RID ,
3154- DOMAIN_ALIAS_RID_ADMINS ,
3155- 0 ,
3156- 0 ,
3157- 0 ,
3158- 0 ,
3159- 0 ,
3160- 0 ,
3161- ref pAdminSid ) ;
3162-
3163- if ( ! status || pAdminSid == IntPtr . Zero )
3164- {
3165- goto cleanup ;
3166- }
3167- status = false ;
3168- pSecurityDescriptor = Marshal . AllocHGlobal ( sizeofSECURITY_DESCRIPTOR ) ;
3169- if ( pSecurityDescriptor == IntPtr . Zero )
3170- {
3171- goto cleanup ;
3172- }
3173- for ( int i = 0 ; i < sizeofSECURITY_DESCRIPTOR ; i ++ )
3174- Marshal . WriteByte ( pSecurityDescriptor , i , ( byte ) 0 ) ;
3175- cbAcl = sizeofACL
3176- + ( 2 * ( sizeofACCESS_ALLOWED_ACE ) )
3177- + sizeofACCESS_DENIED_ACE
3178- + NativeMethods . GetLengthSid ( pUserSid )
3179- + NativeMethods . GetLengthSid ( pAdminSid ) ;
3180-
3181- pDacl = Marshal . AllocHGlobal ( cbAcl ) ;
3182- if ( pDacl == IntPtr . Zero )
3183- {
3184- goto cleanup ;
3185- }
3186- // rights must be added in a certain order. Namely, deny access first, then add access
3187- if ( NativeMethods . InitializeAcl ( pDacl , cbAcl , ACL_REVISION ) )
3188- if ( NativeMethods . AddAccessDeniedAce ( pDacl , ACL_REVISION , WRITE_DAC , pUserSid ) )
3189- if ( NativeMethods . AddAccessAllowedAce ( pDacl , ACL_REVISION , GENERIC_READ , pUserSid ) )
3190- if ( NativeMethods . AddAccessAllowedAce ( pDacl , ACL_REVISION , GENERIC_ALL , pAdminSid ) )
3191- if ( NativeMethods . InitializeSecurityDescriptor ( pSecurityDescriptor , SECURITY_DESCRIPTOR_REVISION ) )
3192- if ( NativeMethods . SetSecurityDescriptorDacl ( pSecurityDescriptor , true , pDacl , false ) )
3193- {
3194- status = true ;
3195- }
3196-
3197- cleanup :
3198- if ( pNtAuthority != IntPtr . Zero )
3199- {
3200- Marshal . FreeHGlobal ( pNtAuthority ) ;
3201- }
3202- if ( pAdminSid != IntPtr . Zero )
3203- NativeMethods . FreeSid ( pAdminSid ) ;
3204- if ( pUserSid != IntPtr . Zero )
3205- NativeMethods . FreeSid ( pUserSid ) ;
3206- if ( status )
3207- return pSecurityDescriptor ;
3208- else
3209- {
3210- if ( pSecurityDescriptor != IntPtr . Zero )
3211- {
3212- Marshal . FreeHGlobal ( pSecurityDescriptor ) ;
3213- }
3214- }
3215- return IntPtr . Zero ;
3216- }
3217-
3218- // SxS: using file mapping API (CreateFileMapping)
3219- // TODO: review this code for SxS issues (VSDD 540765)
3220- [ ResourceExposure ( ResourceScope . None ) ]
3221- [ ResourceConsumption ( ResourceScope . Machine , ResourceScope . Machine ) ]
3222- bool ISQLDebug . SQLDebug( int dwpidDebugger , int dwpidDebuggee , [ MarshalAs ( UnmanagedType . LPStr ) ] string pszMachineName ,
3223- [ MarshalAs ( UnmanagedType . LPStr ) ] string pszSDIDLLName , int dwOption , int cbData , byte [ ] rgbData )
3224- {
3225- bool result = false ;
3226- IntPtr hFileMap = IntPtr . Zero ;
3227- IntPtr pMemMap = IntPtr . Zero ;
3228- IntPtr pSecurityDescriptor = IntPtr . Zero ;
3229- IntPtr pSecurityAttributes = IntPtr . Zero ;
3230- IntPtr pDacl = IntPtr . Zero ;
3231-
3232- // validate the structure
3233- if ( pszMachineName == null || pszSDIDLLName == null )
3234- {
3235- return false ;
3236- }
3237-
3238- if ( pszMachineName . Length > TdsEnums . SDCI_MAX_MACHINENAME ||
3239- pszSDIDLLName . Length > TdsEnums . SDCI_MAX_DLLNAME )
3240- {
3241- return false ;
3242- }
3243-
3244- // note that these are ansi strings
3245- Encoding cp = System . Text . Encoding . GetEncoding ( TdsEnums . DEFAULT_ENGLISH_CODE_PAGE_VALUE ) ;
3246- byte [ ] rgbMachineName = cp . GetBytes ( pszMachineName ) ;
3247- byte [ ] rgbSDIDLLName = cp . GetBytes ( pszSDIDLLName ) ;
3248-
3249- if ( rgbData != null && cbData > TdsEnums . SDCI_MAX_DATA )
3250- {
3251- return false ;
3252- }
3253-
3254- string mapFileName ;
3255-
3256- // If Win2k or later, prepend "Global\\" to enable this to work through TerminalServices.
3257- if ( ADP . s_isPlatformNT5 )
3258- {
3259- mapFileName = "Global\\ " + TdsEnums . SDCI_MAPFILENAME ;
3260- }
3261- else
3262- {
3263- mapFileName = TdsEnums . SDCI_MAPFILENAME ;
3264- }
3265-
3266- mapFileName = mapFileName + dwpidDebuggee . ToString ( CultureInfo . InvariantCulture ) ;
3267-
3268- // Create Security Descriptor
3269- pSecurityDescriptor = CreateSD ( ref pDacl ) ;
3270- pSecurityAttributes = Marshal . AllocHGlobal ( sizeofSECURITY_ATTRIBUTES ) ;
3271- if ( ( pSecurityDescriptor == IntPtr . Zero ) || ( pSecurityAttributes == IntPtr . Zero ) )
3272- return false ;
3273-
3274- Marshal . WriteInt32 ( pSecurityAttributes , 0 , sizeofSECURITY_ATTRIBUTES ) ; // nLength = sizeof(SECURITY_ATTRIBUTES)
3275- Marshal . WriteIntPtr ( pSecurityAttributes , 4 , pSecurityDescriptor ) ; // lpSecurityDescriptor = pSecurityDescriptor
3276- Marshal . WriteInt32 ( pSecurityAttributes , 8 , 0 ) ; // bInheritHandle = FALSE
3277- hFileMap = NativeMethods . CreateFileMappingA (
3278- ADP . s_invalidPtr /*INVALID_HANDLE_VALUE*/ ,
3279- pSecurityAttributes ,
3280- 0x4 /*PAGE_READWRITE*/ ,
3281- 0 ,
3282- Marshal . SizeOf ( typeof ( MEMMAP ) ) ,
3283- mapFileName ) ;
3284-
3285- if ( IntPtr . Zero == hFileMap )
3286- {
3287- goto cleanup ;
3288- }
3289-
3290-
3291- pMemMap = NativeMethods . MapViewOfFile ( hFileMap , 0x6 /*FILE_MAP_READ|FILE_MAP_WRITE*/ , 0 , 0 , IntPtr . Zero ) ;
3292-
3293- if ( IntPtr . Zero == pMemMap )
3294- {
3295- goto cleanup ;
3296- }
3297-
3298- // copy data to memory-mapped file
3299- // layout of MEMMAP structure is:
3300- // uint dbgpid
3301- // uint fOption
3302- // byte[32] machineName
3303- // byte[16] sdiDllName
3304- // uint dbData
3305- // byte[255] vData
3306- int offset = 0 ;
3307- Marshal . WriteInt32 ( pMemMap , offset , ( int ) dwpidDebugger ) ;
3308- offset += 4 ;
3309- Marshal . WriteInt32 ( pMemMap , offset , ( int ) dwOption ) ;
3310- offset += 4 ;
3311- Marshal . Copy ( rgbMachineName , 0 , ADP . IntPtrOffset ( pMemMap , offset ) , rgbMachineName . Length ) ;
3312- offset += TdsEnums . SDCI_MAX_MACHINENAME ;
3313- Marshal . Copy ( rgbSDIDLLName , 0 , ADP . IntPtrOffset ( pMemMap , offset ) , rgbSDIDLLName . Length ) ;
3314- offset += TdsEnums . SDCI_MAX_DLLNAME ;
3315- Marshal . WriteInt32 ( pMemMap , offset , ( int ) cbData ) ;
3316- offset += 4 ;
3317- if ( rgbData != null )
3318- {
3319- Marshal . Copy ( rgbData , 0 , ADP . IntPtrOffset ( pMemMap , offset ) , ( int ) cbData ) ;
3320- }
3321- NativeMethods . UnmapViewOfFile ( pMemMap ) ;
3322- result = true ;
3323- cleanup :
3324- if ( result == false )
3325- {
3326- if ( hFileMap != IntPtr . Zero )
3327- NativeMethods . CloseHandle ( hFileMap ) ;
3328- }
3329- if ( pSecurityAttributes != IntPtr . Zero )
3330- Marshal . FreeHGlobal ( pSecurityAttributes ) ;
3331- if ( pSecurityDescriptor != IntPtr . Zero )
3332- Marshal . FreeHGlobal ( pSecurityDescriptor ) ;
3333- if ( pDacl != IntPtr . Zero )
3334- Marshal . FreeHGlobal ( pDacl ) ;
3335- return result ;
3336- }
3337- }
3338-
3339- // this is a private interface to com+ users
3340- // do not change this guid
3341- [
3342- ComImport ,
3343- ComVisible ( true ) ,
3344- Guid ( "6cb925bf-c3c0-45b3-9f44-5dd67c7b7fe8" ) ,
3345- InterfaceType ( ComInterfaceType . InterfaceIsIUnknown ) ,
3346- BestFitMapping ( false , ThrowOnUnmappableChar = true ) ,
3347- ]
3348- interface ISQLDebug
3349- {
3350-
3351- [ System . Security . Permissions . PermissionSetAttribute ( System . Security . Permissions . SecurityAction . LinkDemand , Name = "FullTrust" ) ]
3352- bool SQLDebug (
3353- int dwpidDebugger ,
3354- int dwpidDebuggee ,
3355- [ MarshalAs ( UnmanagedType . LPStr ) ] string pszMachineName ,
3356- [ MarshalAs ( UnmanagedType . LPStr ) ] string pszSDIDLLName ,
3357- int dwOption ,
3358- int cbData ,
3359- byte [ ] rgbData ) ;
3360- }
3361-
33623068 sealed class SqlDebugContext : IDisposable
33633069 {
33643070 // context data
0 commit comments