#!/bin/bash
source /OSM/script/os_load_base.sh

function PATCH_START()
{
    return 
}

function PATCH_END()
{
    #aa
    echo "" >> /etc/sudoers
    echo "%UTOPUSERS ALL=(ALL) NOPASSWD:/OSM/script/os_debug_mode_process_guest.sh" >> /etc/sudoers
    sqlite3 /OSM/conf/product.dat ".timeout 10000" "UPDATE RoleBuiltIn set sv_permit='user:ldap_readonly,safe_strategy_readonly;mapping_view:initiator_readonly,target_readonly,isns_readonly,mapping_view_readonly,lun_group_readonly,host_group_readonly,host_readonly,port_group_readonly;pool:disk_domain_readonly,storage_pool_readonly,disk_readonly,enclosure_readonly;file_system:file_system_readonly;quota:quota_tree_readonly,quota_readonly;share:share_readonly;file_storage_service:nfs_service_readonly,cifs_service_readonly,http_service_readonly,ftp_service_readonly;resource_user:domain_readonly,resource_user_readonly;network:port_readonly,logical_port_readonly,vlan_readonly,failover_group_readonly,controller_readonly,interface_module_readonly,dns_zone_readonly;local_data_protection:remote_device_readonly,remote_resource_readonly,mirror_lun_readonly,lun_snapshot_readonly,lun_clone_readonly,lun_clone_vstore_readonly,fs_snapshot_readonly,lun_copy_readonly;remote_data_protection:remote_device_readonly,remote_resource_readonly,hyper_vault_readonly,remote_replication_readonly,ndmp_service_readonly,lun_group_readonly,consistency_group_readonly,remote_replication_vstore_pair_readonly;hyper_metro:remote_device_readonly,remote_resource_readonly,hyper_metro_consistency_group_readonly,hyper_metro_domain_readonly,hyper_metro_pair_readonly,hyper_metro_vstore_pair_readonly,quorum_server_readonly;resource_performance_tuning:smart_qos_readonly,smart_tier_readonly,smart_tier_fs_readonly,smart_partition_readonly,disk_readonly,enclosure_readonly,smart_cache_readonly,smart_migration_readonly;smart_virtualization:remote_resource_readonly,remote_device_readonly,port_readonly;performance:performance_readonly,cifs_service_readonly,nfs_service_readonly,lun_copy_readonly,share_readonly,controller_readonly,smart_qos_readonly,disk_domain_readonly,storage_pool_readonly,smart_partition_readonly,host_readonly,remote_device_readonly,remote_replication_readonly,file_system_readonly,lun_readonly,port_readonly,lun_snapshot_readonly,disk_readonly,enclosure_readonly;device:port_readonly,disk_readonly,controller_readonly,bbu_readonly,interface_module_readonly,fan_readonly,expansion_module_readonly,enclosure_readonly,power_supply_readonly,storage_engine_readonly,cabinet_readonly;virtual_machine:virtual_machine_readonly;alarm:alarm_readonly;system:dns_server_readonly,system_readonly,upgrade_readonly,version_readonly,license_readonly,logfile,logfile_readonly,configuration_data,configuration_data_readonly,running_data,running_data_readonly,snmp_readonly,system_reporter_readonly,svp_readonly,smis_readonly,mirror_domain_readonly,call_home_readonly;security:security_rule_readonly,certificate_readonly,safe_strategy_readonly,audit_strategy_readonly,lun_destroy_data_readonly,disk_destroy_data_readonly,kmc_readonly,antivirus_readonly,secure_compliance_clock_readonly;dr_star_data_protection:dr_star_readonly;vstore:vstore_readonly;' where sv_permit='Maintenance administrator'"

    sqlite3 /OSM/conf/product.dat ".timeout 10000" "UPDATE opcodeconfig set PermitObject='support,safe_strategy_readonly' where OpCode='55847747695'"
    sqlite3 /OSM/conf/product.dat ".timeout 10000" "UPDATE opcodeconfig set PermitObject='support,safe_strategy_readonly' where OpCode='57176752850'"
    sqlite3 /OSM/conf/product.dat ".timeout 10000" "UPDATE opcodeconfig set Level=2,PermitObject='support,safe_strategy_readonly' where OpCode='57176752890'"
    sqlite3 /OSM/conf/product.dat ".timeout 10000" "UPDATE opcodeconfig set Level=2,PermitObject='support,safe_strategy_readonly' where OpCode='57176752532'"
    sqlite3 /OSM/conf/product.dat ".timeout 10000" "UPDATE opcodeconfig set Level=2,PermitObject='support,safe_strategy_readonly' where OpCode='57176752563'"
    
    #cli
    #back
    mkdir -p /ISM/cli_patch_back_sph009/cmd_autogen
    cp -rf /ISM/cli/cmd_autogen/debug /ISM/cli_patch_back_sph009/cmd_autogen/
    cp -rf /ISM/cli/cmd_autogen/delete.file /ISM/cli_patch_back_sph009/cmd_autogen/
    cp -rf /ISM/cli/cmd_autogen/minisystem /ISM/cli_patch_back_sph009/cmd_autogen/
    cp -rf /ISM/cli/cmd_autogen/show.cache.blocked_page /ISM/cli_patch_back_sph009/cmd_autogen/
    cp -rf /ISM/cli/cmd_autogen/show.cifs.server_info /ISM/cli_patch_back_sph009/cmd_autogen/
    cp -rf /ISM/cli/cmd_autogen/show.file.export_path /ISM/cli_patch_back_sph009/cmd_autogen/
    cp -rf /ISM/cli/cmd_autogen/show.nfs.server_info /ISM/cli_patch_back_sph009/cmd_autogen/
    cp -rf /ISM/cli/cmd_autogen/show.system.force_write_back /ISM/cli_patch_back_sph009/cmd_autogen/
    cp -rf /ISM/cli/cmd_autogen/change.user_mode.current_mode /ISM/cli_patch_back_sph009/cmd_autogen/
    cp /OSM/script/os_debug_mode_process.sh /ISM/cli_patch_back_sph009/
    
    #install
    mkdir -p /ISM/cli_patch_sph009
    tar -zxvf /ISM/v500r007c61sph009-cli-DTS2020101605JS2OP0G00.tgz -C /ISM/cli_patch_sph009/
    cp -rf /ISM/cli_patch_sph009/cmd_autogen/* /ISM/cli/cmd_autogen/
    cp -f /ISM/cli_patch_sph009/os_debug_mode_process.sh /OSM/script/
    cp -f /ISM/cli_patch_sph009/os_debug_mode_process_guest.sh /OSM/script/
    sed -i "s/chmod 550 \/OSM\/script\/os_debug_mode_process.sh/chmod 555 \/OSM\/script\/os_debug_mode_process.sh/" /OSM/script/file_auth_control.sh
    chmod 555 /OSM/script/os_debug_mode_process.sh
    chmod 555 /OSM/script/os_debug_mode_process_guest.sh
    killall -s 9 CLI_Server
    rm -rf /ISM/cli_patch_sph009

    return
}

function PATCH_ROLLSTART()
{
    #aa
    sed -i '/^%UTOPUSERS ALL=(ALL) NOPASSWD:\/OSM\/script\/os_debug_mode_process_guest.sh/d' /etc/sudoers
    sqlite3 /OSM/conf/product.dat ".timeout 10000" "UPDATE RoleBuiltIn set sv_permit='user:ldap_readonly,safe_strategy_readonly;mapping_view:initiator_readonly,target_readonly,isns_readonly,mapping_view_readonly,lun_group_readonly,host_group_readonly,host_readonly,port_group_readonly;pool:disk_domain_readonly,storage_pool_readonly,disk_readonly,enclosure_readonly;file_system:file_system_readonly;quota:quota_tree_readonly,quota_readonly;share:share_readonly;file_storage_service:nfs_service_readonly,cifs_service_readonly,http_service_readonly,ftp_service_readonly;resource_user:domain_readonly,resource_user_readonly;network:port_readonly,logical_port_readonly,vlan_readonly,failover_group_readonly,controller_readonly,interface_module_readonly,dns_zone_readonly;local_data_protection:remote_device_readonly,remote_resource_readonly,mirror_lun_readonly,lun_snapshot_readonly,lun_clone_readonly,lun_clone_vstore_readonly,fs_snapshot_readonly,lun_copy_readonly;remote_data_protection:remote_device_readonly,remote_resource_readonly,hyper_vault_readonly,remote_replication_readonly,ndmp_service_readonly,lun_group_readonly,consistency_group_readonly,remote_replication_vstore_pair_readonly;hyper_metro:remote_device_readonly,remote_resource_readonly,hyper_metro_consistency_group_readonly,hyper_metro_domain_readonly,hyper_metro_pair_readonly,hyper_metro_vstore_pair_readonly,quorum_server_readonly;resource_performance_tuning:smart_qos_readonly,smart_tier_readonly,smart_tier_fs_readonly,smart_partition_readonly,disk_readonly,enclosure_readonly,smart_cache_readonly,smart_migration_readonly;smart_virtualization:remote_resource_readonly,remote_device_readonly,port_readonly;performance:performance_readonly,cifs_service_readonly,nfs_service_readonly,lun_copy_readonly,share_readonly,controller_readonly,smart_qos_readonly,disk_domain_readonly,storage_pool_readonly,smart_partition_readonly,host_readonly,remote_device_readonly,remote_replication_readonly,file_system_readonly,lun_readonly,port_readonly,lun_snapshot_readonly,disk_readonly,enclosure_readonly;device:port_readonly,disk_readonly,controller_readonly,bbu_readonly,interface_module_readonly,fan_readonly,expansion_module_readonly,enclosure_readonly,power_supply_readonly,storage_engine_readonly,cabinet_readonly;virtual_machine:virtual_machine_readonly;alarm:alarm_readonly;system:dns_server_readonly,system_readonly,upgrade_readonly,version_readonly,license_readonly,logfile,logfile_readonly,configuration_data,configuration_data_readonly,running_data,running_data_readonly,snmp_readonly,system_reporter_readonly,svp_readonly,smis_readonly,mirror_domain_readonly,call_home_readonly;security:security_rule_readonly,certificate_readonly,safe_strategy_readonly,audit_strategy_readonly,lun_destroy_data_readonly,disk_destroy_data_readonly,kmc_readonly,antivirus_readonly,secure_compliance_clock_readonly;dr_star_data_protection:dr_star_readonly;' where sv_permit='Maintenance administrator'"
    
    sqlite3 /OSM/conf/product.dat ".timeout 10000" "UPDATE opcodeconfig set PermitObject='support' where OpCode='55847747695'"
    sqlite3 /OSM/conf/product.dat ".timeout 10000" "UPDATE opcodeconfig set PermitObject='support' where OpCode='57176752850'"
    sqlite3 /OSM/conf/product.dat ".timeout 10000" "UPDATE opcodeconfig set Level=1,PermitObject='support' where OpCode='57176752890'"
    sqlite3 /OSM/conf/product.dat ".timeout 10000" "UPDATE opcodeconfig set Level=1,PermitObject='support' where OpCode='57176752532'"
    sqlite3 /OSM/conf/product.dat ".timeout 10000" "UPDATE opcodeconfig set Level=1,PermitObject='support' where OpCode='57176752563'"
    
    #cli
    cp -rf /ISM/cli_patch_back_sph009/os_debug_mode_process.sh /OSM/script/
    sed -i "s/chmod 555 \/OSM\/script\/os_debug_mode_process.sh/chmod 550 \/OSM\/script\/os_debug_mode_process.sh/" /OSM/script/file_auth_control.sh
    chmod 550 /OSM/script/os_debug_mode_process.sh
    rm -rf /OSM/script/os_debug_mode_process_guest.sh
    cp -rf /ISM/cli_patch_back_sph009/cmd_autogen/* /ISM/cli/cmd_autogen/
    killall -s 9 CLI_Server
    rm -rf /ISM/cli_patch_back_sph009
    
    return
}

function PATCH_ROLLEND()
{
    return
}

#start：打补丁前需要执行的操作，脚本的执行时间点在替换文件前
#end：打补丁后需要执行的操作，脚本的执行时间点在替换文件后
#rollstart：回滚补丁时需要执行的操作，脚本的执行时间点在文件被恢复之前。
#rollend：回滚补丁时需要执行的操作，脚本的执行时间点在文件被恢复之后。

L_CMD=$1
OS_echo "command is $L_CMD"
case $L_CMD in
        "start") 
                PATCH_START
                ;;
        "end") 
                PATCH_END
                ;;
        "rollstart") 
                PATCH_ROLLSTART
                ;;
        "rollend") 
                PATCH_ROLLEND
                ;;
        *)
                OS_echo  "unsupported option."
esac
exit 0

