@@ -523,29 +523,31 @@ def set_value(
523523 msg = f"{ v_type } data must be an integer"
524524 raise SaltInvocationError (msg )
525525
526- pol_data = read_reg_pol (policy_class = policy_class )
527-
528- found_key , found_name = _find_value (pol_data , key , v_name )
529-
530- if found_key :
531- if found_name :
532- if "**del." in found_name :
533- log .debug ("LGPO_REG Mod: Found disabled name: %s" , found_name )
534- pol_data [found_key ][v_name ] = pol_data [found_key ].pop (found_name )
535- found_name = v_name
536- log .debug ("LGPO_REG Mod: Updating value: %s" , found_name )
537- pol_data [found_key ][found_name ] = {"data" : v_data , "type" : v_type }
526+ machine = policy_class == "Machine"
527+ with salt .utils .win_lgpo_reg ._policy_lock (machine = machine ):
528+ pol_data = read_reg_pol (policy_class = policy_class )
529+
530+ found_key , found_name = _find_value (pol_data , key , v_name )
531+
532+ if found_key :
533+ if found_name :
534+ if "**del." in found_name :
535+ log .debug ("LGPO_REG Mod: Found disabled name: %s" , found_name )
536+ pol_data [found_key ][v_name ] = pol_data [found_key ].pop (found_name )
537+ found_name = v_name
538+ log .debug ("LGPO_REG Mod: Updating value: %s" , found_name )
539+ pol_data [found_key ][found_name ] = {"data" : v_data , "type" : v_type }
540+ else :
541+ log .debug ("LGPO_REG Mod: Setting new value: %s" , found_name )
542+ pol_data [found_key ][v_name ] = {"data" : v_data , "type" : v_type }
538543 else :
539- log .debug ("LGPO_REG Mod: Setting new value: %s" , found_name )
540- pol_data [found_key ][v_name ] = {"data" : v_data , "type" : v_type }
541- else :
542- log .debug ("LGPO_REG Mod: Adding new key and value: %s" , found_name )
543- pol_data [key ] = {v_name : {"data" : v_data , "type" : v_type }}
544+ log .debug ("LGPO_REG Mod: Adding new key and value: %s" , found_name )
545+ pol_data [key ] = {v_name : {"data" : v_data , "type" : v_type }}
544546
545- success = True
546- if not write_reg_pol (pol_data , policy_class = policy_class ):
547- log .error ("LGPO_REG Mod: Failed to write registry.pol file" )
548- success = False
547+ success = True
548+ if not write_reg_pol (pol_data , policy_class = policy_class ):
549+ log .error ("LGPO_REG Mod: Failed to write registry.pol file" )
550+ success = False
549551
550552 # Resolve auto-detect: skip registry write on Domain Controllers where
551553 # HKLM\SOFTWARE\Policies\ is write-protected by AD security hardening.
@@ -661,40 +663,42 @@ def disable_value(
661663 else :
662664 raise SaltInvocationError ("An invalid policy class was specified" )
663665
664- pol_data = read_reg_pol (policy_class = policy_class )
666+ machine = policy_class == "Machine"
667+ with salt .utils .win_lgpo_reg ._policy_lock (machine = machine ):
668+ pol_data = read_reg_pol (policy_class = policy_class )
665669
666- found_key , found_name = _find_value (pol_data , key , v_name )
670+ found_key , found_name = _find_value (pol_data , key , v_name )
667671
668- pol_modified = False
669- if found_key :
670- if found_name :
671- if "**del." in found_name :
672- log .debug ("LGPO_REG Mod: Already disabled: %s" , v_name )
672+ pol_modified = False
673+ if found_key :
674+ if found_name :
675+ if "**del." in found_name :
676+ log .debug ("LGPO_REG Mod: Already disabled: %s" , v_name )
677+ else :
678+ log .debug ("LGPO_REG Mod: Disabling value name: %s" , v_name )
679+ pol_data [found_key ].pop (found_name )
680+ found_name = f"**del.{ found_name } "
681+ pol_data [found_key ][found_name ] = {"data" : " " , "type" : "REG_SZ" }
682+ pol_modified = True
673683 else :
674- log .debug ("LGPO_REG Mod: Disabling value name: %s" , v_name )
675- pol_data [found_key ].pop (found_name )
676- found_name = f"**del.{ found_name } "
677- pol_data [found_key ][found_name ] = {"data" : " " , "type" : "REG_SZ" }
684+ log .debug ("LGPO_REG Mod: Setting new disabled value name: %s" , v_name )
685+ pol_data [found_key ][f"**del.{ v_name } " ] = {
686+ "data" : " " ,
687+ "type" : "REG_SZ" ,
688+ }
678689 pol_modified = True
679690 else :
680- log .debug ("LGPO_REG Mod: Setting new disabled value name: %s" , v_name )
681- pol_data [found_key ][f"**del.{ v_name } " ] = {
682- "data" : " " ,
683- "type" : "REG_SZ" ,
684- }
691+ log .debug (
692+ "LGPO_REG Mod: Adding new key and disabled value name: %s" , found_name
693+ )
694+ pol_data [key ] = {f"**del.{ v_name } " : {"data" : " " , "type" : "REG_SZ" }}
685695 pol_modified = True
686- else :
687- log .debug (
688- "LGPO_REG Mod: Adding new key and disabled value name: %s" , found_name
689- )
690- pol_data [key ] = {f"**del.{ v_name } " : {"data" : " " , "type" : "REG_SZ" }}
691- pol_modified = True
692-
693- success = True
694- if pol_modified :
695- if not write_reg_pol (pol_data , policy_class = policy_class ):
696- log .error ("LGPO_REG Mod: Failed to write registry.pol file" )
697- success = False
696+
697+ success = True
698+ if pol_modified :
699+ if not write_reg_pol (pol_data , policy_class = policy_class ):
700+ log .error ("LGPO_REG Mod: Failed to write registry.pol file" )
701+ success = False
698702
699703 # Resolve auto-detect: skip registry delete on Domain Controllers.
700704 if write_registry is None :
@@ -813,29 +817,31 @@ def delete_value(
813817 else :
814818 raise SaltInvocationError ("An invalid policy class was specified" )
815819
816- pol_data = read_reg_pol (policy_class = policy_class )
820+ machine = policy_class == "Machine"
821+ with salt .utils .win_lgpo_reg ._policy_lock (machine = machine ):
822+ pol_data = read_reg_pol (policy_class = policy_class )
817823
818- found_key , found_name = _find_value (pol_data , key , v_name )
824+ found_key , found_name = _find_value (pol_data , key , v_name )
819825
820- pol_modified = False
821- if found_key :
822- if found_name :
823- log .debug ("LGPO_REG Mod: Removing value name: %s" , found_name )
824- pol_data [found_key ].pop (found_name )
825- pol_modified = True
826- if len (pol_data [found_key ]) == 0 :
827- log .debug ("LGPO_REG Mod: Removing empty key: %s" , found_key )
828- pol_data .pop (found_key )
826+ pol_modified = False
827+ if found_key :
828+ if found_name :
829+ log .debug ("LGPO_REG Mod: Removing value name: %s" , found_name )
830+ pol_data [found_key ].pop (found_name )
831+ pol_modified = True
832+ if len (pol_data [found_key ]) == 0 :
833+ log .debug ("LGPO_REG Mod: Removing empty key: %s" , found_key )
834+ pol_data .pop (found_key )
835+ else :
836+ log .debug ("LGPO_REG Mod: Value name not found: %s" , v_name )
829837 else :
830- log .debug ("LGPO_REG Mod: Value name not found: %s" , v_name )
831- else :
832- log .debug ("LGPO_REG Mod: Key not found: %s" , key )
838+ log .debug ("LGPO_REG Mod: Key not found: %s" , key )
833839
834- success = True
835- if pol_modified :
836- if not write_reg_pol (pol_data , policy_class = policy_class ):
837- log .error ("LGPO_REG Mod: Failed to write registry.pol file" )
838- success = False
840+ success = True
841+ if pol_modified :
842+ if not write_reg_pol (pol_data , policy_class = policy_class ):
843+ log .error ("LGPO_REG Mod: Failed to write registry.pol file" )
844+ success = False
839845
840846 # Resolve auto-detect: skip registry delete on Domain Controllers.
841847 if write_registry is None :
0 commit comments