internal void SyncReplicaAllHelper(IntPtr handle, SyncReplicaFromAllServersCallback syncAllFunctionPointer, string partition, SyncFromAllServersOptions option, SyncUpdateCallback callback, LoadLibrarySafeHandle libHandle) { IntPtr errorInfo = (IntPtr)0; if (!Partitions.Contains(partition)) { throw new ArgumentException(SR.ServerNotAReplica, "partition"); } // we want to return the dn instead of DNS guid // call DsReplicaSyncAllW IntPtr functionPtr = UnsafeNativeMethods.GetProcAddress(libHandle, "DsReplicaSyncAllW"); if (functionPtr == (IntPtr)0) { throw ExceptionHelper.GetExceptionFromErrorCode(Marshal.GetLastWin32Error()); } UnsafeNativeMethods.DsReplicaSyncAllW dsReplicaSyncAllW = (UnsafeNativeMethods.DsReplicaSyncAllW)Marshal.GetDelegateForFunctionPointer(functionPtr, typeof(UnsafeNativeMethods.DsReplicaSyncAllW)); int result = dsReplicaSyncAllW(handle, partition, (int)option | DS_REPSYNCALL_ID_SERVERS_BY_DN, syncAllFunctionPointer, (IntPtr)0, ref errorInfo); try { // error happens during the synchronization if (errorInfo != (IntPtr)0) { SyncFromAllServersOperationException e = ExceptionHelper.CreateSyncAllException(errorInfo, false); if (e == null) { return; } else { throw e; } } else { // API does not return error infor occurred during synchronization, but result is not success. if (result != 0) { throw new SyncFromAllServersOperationException(ExceptionHelper.GetErrorMessage(result, false)); } } } finally { // release the memory if (errorInfo != (IntPtr)0) { UnsafeNativeMethods.LocalFree(errorInfo); } } }
private void FreeReplicaInfo(DS_REPL_INFO_TYPE type, IntPtr value, LoadLibrarySafeHandle libHandle) { if (value != (IntPtr)0) { // call DsReplicaFreeInfo IntPtr functionPtr = UnsafeNativeMethods.GetProcAddress(libHandle, "DsReplicaFreeInfo"); if (functionPtr == (IntPtr)0) { throw ExceptionHelper.GetExceptionFromErrorCode(Marshal.GetLastWin32Error()); } UnsafeNativeMethods.DsReplicaFreeInfo dsReplicaFreeInfo = (UnsafeNativeMethods.DsReplicaFreeInfo)Marshal.GetDelegateForFunctionPointer(functionPtr, typeof(UnsafeNativeMethods.DsReplicaFreeInfo)); dsReplicaFreeInfo((int)type, value); } }
internal void SyncReplicaAllHelper(IntPtr handle, SyncReplicaFromAllServersCallback syncAllFunctionPointer, string partition, SyncFromAllServersOptions option, SyncUpdateCallback callback, LoadLibrarySafeHandle libHandle) { IntPtr intPtr = (IntPtr)0; if (this.Partitions.Contains(partition)) { IntPtr procAddress = UnsafeNativeMethods.GetProcAddress(libHandle, "DsReplicaSyncAllW"); if (procAddress != (IntPtr)0) { UnsafeNativeMethods.DsReplicaSyncAllW delegateForFunctionPointer = (UnsafeNativeMethods.DsReplicaSyncAllW)Marshal.GetDelegateForFunctionPointer(procAddress, typeof(UnsafeNativeMethods.DsReplicaSyncAllW)); int num = delegateForFunctionPointer(handle, partition, (int)option | 4, syncAllFunctionPointer, (IntPtr)0, ref intPtr); try { if (intPtr == (IntPtr)0) { if (num != 0) { throw new SyncFromAllServersOperationException(ExceptionHelper.GetErrorMessage(num, false)); } } else { SyncFromAllServersOperationException syncFromAllServersOperationException = ExceptionHelper.CreateSyncAllException(intPtr, false); if (syncFromAllServersOperationException != null) { throw syncFromAllServersOperationException; } } } finally { if (intPtr != (IntPtr)0) { UnsafeNativeMethods.LocalFree(intPtr); } } return; } else { throw ExceptionHelper.GetExceptionFromErrorCode(Marshal.GetLastWin32Error()); } } else { throw new ArgumentException(Res.GetString("ServerNotAReplica"), "partition"); } }
internal void CheckConsistencyHelper(IntPtr dsHandle, LoadLibrarySafeHandle libHandle) { // call DsReplicaConsistencyCheck IntPtr functionPtr = UnsafeNativeMethods.GetProcAddress(libHandle, "DsReplicaConsistencyCheck"); if (functionPtr == (IntPtr)0) { throw ExceptionHelper.GetExceptionFromErrorCode(Marshal.GetLastWin32Error()); } UnsafeNativeMethods.DsReplicaConsistencyCheck replicaConsistencyCheck = (UnsafeNativeMethods.DsReplicaConsistencyCheck)Marshal.GetDelegateForFunctionPointer(functionPtr, typeof(UnsafeNativeMethods.DsReplicaConsistencyCheck)); int result = replicaConsistencyCheck(dsHandle, 0, 0); if (result != 0) { throw ExceptionHelper.GetExceptionFromErrorCode(result, Name); } }
internal void CheckConsistencyHelper(IntPtr dsHandle, LoadLibrarySafeHandle libHandle) { IntPtr procAddress = UnsafeNativeMethods.GetProcAddress(libHandle, "DsReplicaConsistencyCheck"); if (procAddress != (IntPtr)0) { UnsafeNativeMethods.DsReplicaConsistencyCheck delegateForFunctionPointer = (UnsafeNativeMethods.DsReplicaConsistencyCheck)Marshal.GetDelegateForFunctionPointer(procAddress, typeof(UnsafeNativeMethods.DsReplicaConsistencyCheck)); int num = delegateForFunctionPointer(dsHandle, 0, 0); if (num == 0) { return; } else { throw ExceptionHelper.GetExceptionFromErrorCode(num, this.Name); } } else { throw ExceptionHelper.GetExceptionFromErrorCode(Marshal.GetLastWin32Error()); } }
internal void SyncReplicaHelper(IntPtr dsHandle, bool isADAM, string partition, string sourceServer, int option, LoadLibrarySafeHandle libHandle) { int structSize = Marshal.SizeOf(typeof(Guid)); IntPtr unmanagedGuid = (IntPtr)0; Guid guid = Guid.Empty; AdamInstance adamServer = null; DomainController dcServer = null; unmanagedGuid = Marshal.AllocHGlobal(structSize); try { if (sourceServer != null) { DirectoryContext newContext = Utils.GetNewDirectoryContext(sourceServer, DirectoryContextType.DirectoryServer, context); if (isADAM) { adamServer = AdamInstance.GetAdamInstance(newContext); guid = adamServer.NtdsaObjectGuid; } else { dcServer = DomainController.GetDomainController(newContext); guid = dcServer.NtdsaObjectGuid; } Marshal.StructureToPtr(guid, unmanagedGuid, false); } // call DsReplicaSyncW IntPtr functionPtr = UnsafeNativeMethods.GetProcAddress(libHandle, "DsReplicaSyncW"); if (functionPtr == (IntPtr)0) { throw ExceptionHelper.GetExceptionFromErrorCode(Marshal.GetLastWin32Error()); } UnsafeNativeMethods.DsReplicaSyncW dsReplicaSyncW = (UnsafeNativeMethods.DsReplicaSyncW)Marshal.GetDelegateForFunctionPointer(functionPtr, typeof(UnsafeNativeMethods.DsReplicaSyncW)); int result = dsReplicaSyncW(dsHandle, partition, unmanagedGuid, (int)option); // check the result if (result != 0) { if (!Partitions.Contains(partition)) { throw new ArgumentException(SR.ServerNotAReplica, "partition"); } string serverDownName = null; // this is the error returned when the server that we want to sync from is down if (result == ExceptionHelper.RPC_S_SERVER_UNAVAILABLE) { serverDownName = sourceServer; } // this is the error returned when the server that we want to get synced is down else if (result == ExceptionHelper.RPC_S_CALL_FAILED) { serverDownName = Name; } throw ExceptionHelper.GetExceptionFromErrorCode(result, serverDownName); } } finally { if (unmanagedGuid != (IntPtr)0) { Marshal.FreeHGlobal(unmanagedGuid); } if (adamServer != null) { adamServer.Dispose(); } if (dcServer != null) { dcServer.Dispose(); } } }
internal IntPtr GetReplicationInfoHelper(IntPtr dsHandle, int type, int secondaryType, string partition, ref bool advanced, int context, LoadLibrarySafeHandle libHandle) { IntPtr info = (IntPtr)0; int result = 0; bool needToTryAgain = true; IntPtr functionPtr; // first try to use the DsReplicaGetInfo2W API which does not exist on win2k machine // call DsReplicaGetInfo2W functionPtr = UnsafeNativeMethods.GetProcAddress(libHandle, "DsReplicaGetInfo2W"); if (functionPtr == (IntPtr)0) { // a win2k machine which does not have it. functionPtr = UnsafeNativeMethods.GetProcAddress(libHandle, "DsReplicaGetInfoW"); if (functionPtr == (IntPtr)0) { throw ExceptionHelper.GetExceptionFromErrorCode(Marshal.GetLastWin32Error()); } UnsafeNativeMethods.DsReplicaGetInfoW dsReplicaGetInfoW = (UnsafeNativeMethods.DsReplicaGetInfoW)Marshal.GetDelegateForFunctionPointer(functionPtr, typeof(UnsafeNativeMethods.DsReplicaGetInfoW)); result = dsReplicaGetInfoW(dsHandle, secondaryType, partition, (IntPtr)0, ref info); advanced = false; needToTryAgain = false; } else { UnsafeNativeMethods.DsReplicaGetInfo2W dsReplicaGetInfo2W = (UnsafeNativeMethods.DsReplicaGetInfo2W)Marshal.GetDelegateForFunctionPointer(functionPtr, typeof(UnsafeNativeMethods.DsReplicaGetInfo2W)); result = dsReplicaGetInfo2W(dsHandle, type, partition, (IntPtr)0, null, null, 0, context, ref info); } // check the result if (needToTryAgain && result == DS_REPL_NOTSUPPORTED) { // this is the case that client is xp/win2k3, dc is win2k functionPtr = UnsafeNativeMethods.GetProcAddress(libHandle, "DsReplicaGetInfoW"); if (functionPtr == (IntPtr)0) { throw ExceptionHelper.GetExceptionFromErrorCode(Marshal.GetLastWin32Error()); } UnsafeNativeMethods.DsReplicaGetInfoW dsReplicaGetInfoW = (UnsafeNativeMethods.DsReplicaGetInfoW)Marshal.GetDelegateForFunctionPointer(functionPtr, typeof(UnsafeNativeMethods.DsReplicaGetInfoW)); result = dsReplicaGetInfoW(dsHandle, secondaryType, partition, (IntPtr)0, ref info); advanced = false; } if (result != 0) { if (partition != null) { // this is the case of meta data if (type == (int)DS_REPL_INFO_TYPE.DS_REPL_INFO_METADATA_2_FOR_OBJ) { if (result == ExceptionHelper.ERROR_DS_DRA_BAD_DN || result == ExceptionHelper.ERROR_DS_NAME_UNPARSEABLE) { throw new ArgumentException(ExceptionHelper.GetErrorMessage(result, false), "objectPath"); } DirectoryEntry verifyEntry = DirectoryEntryManager.GetDirectoryEntry(this.context, partition); try { verifyEntry.RefreshCache(new string[] { "name" }); } catch (COMException e) { if (e.ErrorCode == unchecked ((int)0x80072020) | // dir_error on server side e.ErrorCode == unchecked ((int)0x80072030)) // object not exists { throw new ArgumentException(SR.DSNoObject, "objectPath"); } else if (e.ErrorCode == unchecked ((int)0x80005000) | // bad path name e.ErrorCode == unchecked ((int)0x80072032)) // ERROR_DS_INVALID_DN_SYNTAX { throw new ArgumentException(SR.DSInvalidPath, "objectPath"); } } } else { if (!Partitions.Contains(partition)) { throw new ArgumentException(SR.ServerNotAReplica, "partition"); } } } throw ExceptionHelper.GetExceptionFromErrorCode(result, Name); } return(info); }
internal void SyncReplicaHelper(IntPtr dsHandle, bool isADAM, string partition, string sourceServer, int option, LoadLibrarySafeHandle libHandle) { Guid ntdsaObjectGuid; int num = Marshal.SizeOf(typeof(Guid)); AdamInstance adamInstance = null; DomainController domainController = null; IntPtr intPtr = Marshal.AllocHGlobal(num); try { if (sourceServer != null) { DirectoryContext newDirectoryContext = Utils.GetNewDirectoryContext(sourceServer, DirectoryContextType.DirectoryServer, this.context); if (!isADAM) { domainController = DomainController.GetDomainController(newDirectoryContext); ntdsaObjectGuid = domainController.NtdsaObjectGuid; } else { adamInstance = AdamInstance.GetAdamInstance(newDirectoryContext); ntdsaObjectGuid = adamInstance.NtdsaObjectGuid; } Marshal.StructureToPtr(ntdsaObjectGuid, intPtr, false); } IntPtr procAddress = UnsafeNativeMethods.GetProcAddress(libHandle, "DsReplicaSyncW"); if (procAddress != (IntPtr)0) { UnsafeNativeMethods.DsReplicaSyncW delegateForFunctionPointer = (UnsafeNativeMethods.DsReplicaSyncW)Marshal.GetDelegateForFunctionPointer(procAddress, typeof(UnsafeNativeMethods.DsReplicaSyncW)); int num1 = delegateForFunctionPointer(dsHandle, partition, intPtr, option); if (num1 != 0) { if (this.Partitions.Contains(partition)) { string name = null; if (num1 != ExceptionHelper.RPC_S_SERVER_UNAVAILABLE) { if (num1 == ExceptionHelper.RPC_S_CALL_FAILED) { name = this.Name; } } else { name = sourceServer; } throw ExceptionHelper.GetExceptionFromErrorCode(num1, name); } else { throw new ArgumentException(Res.GetString("ServerNotAReplica"), "partition"); } } } else { throw ExceptionHelper.GetExceptionFromErrorCode(Marshal.GetLastWin32Error()); } } finally { if (intPtr != (IntPtr)0) { Marshal.FreeHGlobal(intPtr); } if (adamInstance != null) { adamInstance.Dispose(); } if (domainController != null) { domainController.Dispose(); } } }
internal IntPtr GetReplicationInfoHelper(IntPtr dsHandle, int type, int secondaryType, string partition, ref bool advanced, int context, LoadLibrarySafeHandle libHandle) { IntPtr intPtr = (IntPtr)0; int num = 0; bool flag = true; IntPtr procAddress = UnsafeNativeMethods.GetProcAddress(libHandle, "DsReplicaGetInfo2W"); if (procAddress != (IntPtr)0) { UnsafeNativeMethods.DsReplicaGetInfo2W delegateForFunctionPointer = (UnsafeNativeMethods.DsReplicaGetInfo2W)Marshal.GetDelegateForFunctionPointer(procAddress, typeof(UnsafeNativeMethods.DsReplicaGetInfo2W)); num = delegateForFunctionPointer(dsHandle, type, partition, (IntPtr)0, null, null, 0, context, ref intPtr); } else { procAddress = UnsafeNativeMethods.GetProcAddress(libHandle, "DsReplicaGetInfoW"); if (procAddress != (IntPtr)0) { UnsafeNativeMethods.DsReplicaGetInfoW dsReplicaGetInfoW = (UnsafeNativeMethods.DsReplicaGetInfoW)Marshal.GetDelegateForFunctionPointer(procAddress, typeof(UnsafeNativeMethods.DsReplicaGetInfoW)); num = dsReplicaGetInfoW(dsHandle, secondaryType, partition, (IntPtr)0, ref intPtr); advanced = false; flag = false; } else { throw ExceptionHelper.GetExceptionFromErrorCode(Marshal.GetLastWin32Error()); } } if (flag && num == 50) { procAddress = UnsafeNativeMethods.GetProcAddress(libHandle, "DsReplicaGetInfoW"); if (procAddress != (IntPtr)0) { UnsafeNativeMethods.DsReplicaGetInfoW delegateForFunctionPointer1 = (UnsafeNativeMethods.DsReplicaGetInfoW)Marshal.GetDelegateForFunctionPointer(procAddress, typeof(UnsafeNativeMethods.DsReplicaGetInfoW)); num = delegateForFunctionPointer1(dsHandle, secondaryType, partition, (IntPtr)0, ref intPtr); advanced = false; } else { throw ExceptionHelper.GetExceptionFromErrorCode(Marshal.GetLastWin32Error()); } } if (num == 0) { return(intPtr); } else { if (partition != null) { if (type != 9) { if (!this.Partitions.Contains(partition)) { throw new ArgumentException(Res.GetString("ServerNotAReplica"), "partition"); } } else { if (num == ExceptionHelper.ERROR_DS_DRA_BAD_DN || num == ExceptionHelper.ERROR_DS_NAME_UNPARSEABLE) { throw new ArgumentException(ExceptionHelper.GetErrorMessage(num, false), "objectPath"); } else { DirectoryEntry directoryEntry = DirectoryEntryManager.GetDirectoryEntry(this.context, partition); try { string[] strArrays = new string[1]; strArrays[0] = "name"; directoryEntry.RefreshCache(strArrays); } catch (COMException cOMException1) { COMException cOMException = cOMException1; if (!(cOMException.ErrorCode == -2147016672 | cOMException.ErrorCode == -2147016656)) { if (cOMException.ErrorCode == -2147463168 | cOMException.ErrorCode == -2147016654) { throw new ArgumentException(Res.GetString("DSInvalidPath"), "objectPath"); } } else { throw new ArgumentException(Res.GetString("DSNoObject"), "objectPath"); } } } } } throw ExceptionHelper.GetExceptionFromErrorCode(num, this.Name); } }