#!/usr/bin/perl -w
# This script (sophomorix-quota) is maintained by Rüdiger Beck
# It is Free Software (License GPLv3)
# If you find errors, contact the author
# jeffbeck@web.de  or  jeffbeck@linuxmuster.net

# modules
use strict;
use Getopt::Long;
Getopt::Long::Configure ("bundling");
use Sophomorix::SophomorixConfig;
use List::MoreUtils qw(uniq);
use Data::Dumper;
$Data::Dumper::Indent = 1;
$Data::Dumper::Sortkeys = 1;
$Data::Dumper::Useqq = 1;
$Data::Dumper::Terse = 1; 
use Net::LDAP;
use JSON;
use Sophomorix::SophomorixBase qw(
                                 print_line
                                 print_title
                                 unlock_sophomorix
                                 lock_sophomorix
                                 log_script_start
                                 log_script_end
                                 log_script_exit
                                 backup_auk_file
                                 get_passwd_charlist
                                 get_plain_password
                                 check_options
                                 config_sophomorix_read
                                 result_sophomorix_init
                                 result_sophomorix_add
                                 result_sophomorix_add_summary
                                 result_sophomorix_check_exit
                                 result_sophomorix_print
                                 console_print_quota_user
                                 console_print_mailquota_user
                                 remove_from_list    
                                 json_dump
                                 smb_command
                                 );
use Sophomorix::SophomorixSambaAD qw(
                                 AD_school_create
                                 AD_bind_admin
                                 AD_dns_get
                                 AD_get_user
                                 AD_get_quota
                                 AD_user_kill
                                 AD_unbind_admin
                                 AD_object_search
                                 AD_user_create
                                 AD_user_update
                                 AD_user_setquota
                                 AD_group_create
                                 AD_group_kill
                                 AD_group_addmember
                                 AD_get_schoolname
                                 AD_get_name_tokened
                                 AD_group_update
                                 AD_project_sync_members
                                 AD_dn_fetch_multivalue
                                 AD_get_passwd
                                 AD_smbcquotas_queryuser
                                    );
my @arguments = @ARGV;

# option vars
$Conf::log_level=1;
my $debug_level=0; # for smbcquotas --debuglevel=$debug_level
my $help=0;
my $info=0;
my $json=0;
my $school="";
my $user="";
my $developer_setquota="";
$Conf::log_level=1;

my $show_nondefault=0;

# smbcquotas options
my $list=0;
my $show=0;
my $default_quota="";

# set quota option
my $set_quota=1;
my $set_mailquota=1;

my $set_quota_only=0;
my $set_mailquota_only=0;
my $set=0;

my $smbcquotas=0;
my $smbcquotas_only=0;

# Parsen der Optionen
my $testopt=GetOptions(
           "help|h" => \$help,
           "info|i" => \$info,
           "json|j+" => \$json,
           "verbose|v+" => \$Conf::log_level,
           "school|share|s=s" => \$school,
           "user|u=s" => \$user,
           "developer-setquota=s" => \$developer_setquota,
           "show|F" => \$show,
           "n|nondefault|show-nondefault" => \$show_nondefault,
           "list|L" => \$list,
           "default-quota|FSQLIM=s" => \$default_quota,
           "set-quota-only" => \$set_quota_only,
           "set-mailquota-only" => \$set_mailquota_only,
           "set" => \$set,
           "smbcquotas" => \$smbcquotas,
           "smbcquotas-only" => \$smbcquotas_only,
          );

my %sophomorix_result=&result_sophomorix_init("sophomorix-quota");
# Prüfen, ob Optionen erkannt wurden
&check_options($testopt,\%sophomorix_result,$json);

# Reading Configuration
my ($ldap,$root_dse) = &AD_bind_admin(\@arguments,\%sophomorix_result,$json);
my $root_dns=&AD_dns_get($root_dse);
my %sophomorix_config=&config_sophomorix_read($ldap,$root_dse,\%sophomorix_result);
my ($smb_admin_pass)=&AD_get_passwd($DevelConf::sophomorix_file_admin,
                                     $DevelConf::secret_file_sophomorix_file_admin);


# --help
if ($help==1) {
   # Scriptname ermitteln
   my @list = split(/\//,$0);
   my $scriptname = pop @list;
   # Befehlbeschreibung
   print('
sophomorix-quota sets SMB-Share-Quota(=Quota) CloudQuota and MailQuota

  * Quota is set with the smbcquotas command from samba
  * CloudQuota sets the attribute sophomorixCloudQuotaCalculated
    (i.e. to be used by NextCloud) with the quota of the school share
    multipied by CLOUDQUOTA_PERCENTAGE (in school.ini) of the users role
  * MailQuota sets the attribute sophomorixMailQuotaCalculated
    (i.e. to be used by dovecot)

Options
  -h  / --help
  -v  / --verbose
  -vv / --verbose --verbose

Show sophomorix info about quota calculation
  Show info about all school-shares:
    -i  / --info
  Show info about quota that differ from default:
    -n / --nondefault / --show-nondefault
  Show info about a school or another share:
    -s <school>/<sharename> / --school <school>/<sharename> / --share <school>/<sharename>
  Limit the output to some users:
    -u / --user <user1>,<user2>,... 

Query used space:
  Query the smbshare with smbcquotas for every user for 
  used space/softlimit/hardlimit (this is slow):
    --smbcquotas -i
  Query used space for some users (but still calculate all quota):
    --smbcquotas -i --user <user1>,<user2>,...
  Query used space with smbcquotas command for some users (do nothing else):
    --smbcquotas-only -i --user <user1>,<user2>,...

  
Setting the user Quota/MailQuota
  set Quota/CloudQuota AND MailQuota
    sophomorix-quota
  set Quota/CloudQuota only:
    sophomorix-quota --set-quota-only
  set MailQuota only:
    sophomorix-quota --set-mailquota-only
  set quota even if nothing has changed:
    ... --set ...
  
Set a users quota manually (in AD AND on smbshare):
  sophomorix-quota --developer-setquota <share>:<num(MiB)> -u <user>

smbcquotas wrappers:
  List quota of the users on a school/share:
    sophomorix-quota --share <sharename> -L
    sophomorix-quota --share <sharename> --list
  Show quota quota settings of a school/share:
    sophomorix-quota --share <sharename> -F
    sophomorix-quota --share <sharename> --show
  Set default quota on smb-share:
     sophomorix-quota --share <sharename> --default-quota <softlimit>/<hardlimit>

Please see the sophomorix-quota(8) man pages for full documentation
');
   print "\n";
   exit;
}




# create sharelist from options
# --school <school>/--share <share>
my @sharelist=();
if ($school ne ""){
    if (exists $sophomorix_config{'samba'}{'net_conf_list'}{$school}){
	push @sharelist,$school;
    } else {
        print "\nERROR: $school is not a SMB-share!\n\n";
	exit 88;
    }
} else {
    # without option use only school shares
    @sharelist=@{ $sophomorix_config{'LISTS'}{'SCHOOLS'} };
}


# options that need only a sharename
# --list/-L
if ($list==1){
    foreach my $share (@sharelist){
        my $smbcquotas_command=$sophomorix_config{'INI'}{'EXECUTABLES'}{'SMBCQUOTAS'}.
            " ".$sophomorix_config{'INI'}{'EXECUTABLES'}{'SMBCQUOTAS_PROTOCOL_OPT'}.
#            " -mNT1".
            " --debuglevel=$debug_level -U ".$DevelConf::sophomorix_file_admin.
            "%'******'".
            " -L //$root_dns/$share";
        &smb_command($smbcquotas_command,$smb_admin_pass);
    }
    exit;
}



# --show/-F
if ($show==1){
    foreach my $share (@sharelist){
        my $smbcquotas_command=$sophomorix_config{'INI'}{'EXECUTABLES'}{'SMBCQUOTAS'}.
            " ".$sophomorix_config{'INI'}{'EXECUTABLES'}{'SMBCQUOTAS_PROTOCOL_OPT'}.
#            " -mNT1".
            " --debuglevel=$debug_level -U ".$DevelConf::sophomorix_file_admin.
            "%'******'".
            " -F //$root_dns/$share";
        &smb_command($smbcquotas_command,$smb_admin_pass);
    }
    exit;
}



# --share <share> --default-quota <soft>/<hard>
if ($school ne "" and  $default_quota ne ""){
    foreach my $share (@sharelist){
        my $smbcquotas_command=$sophomorix_config{'INI'}{'EXECUTABLES'}{'SMBCQUOTAS'}.
            " ".$sophomorix_config{'INI'}{'EXECUTABLES'}{'SMBCQUOTAS_PROTOCOL_OPT'}.
#            " -mNT1".
            " --debuglevel=$debug_level -U ".$DevelConf::sophomorix_file_admin.
            "%'******'".
            " -S FSQLIM:".$default_quota." //$root_dns/$share";
        &smb_command($smbcquotas_command,$smb_admin_pass);
    }
    exit;
}



# --user user1,user2
my @userlist=();
my %userlist=();
if ($user ne ""){
    @userlist=split(/,/,$user);
    $userlist{$user}="seen";
}



# --smbcquotas-only --user user1,user2 --info
if ($info==1 and $smbcquotas_only==1 and $user ne ""){
    my %quota=();
    foreach my $user (@userlist){
        foreach my $share ( @{ $sophomorix_config{'LISTS'}{'SHARES'} } ) {
            ($quota{'QUOTA'}{'USERS'}{$user}{'SHARES'}{$share}{'smbcquotas'}{'USED'},
             $quota{'QUOTA'}{'USERS'}{$user}{'SHARES'}{$share}{'smbcquotas'}{'SOFTLIMIT'},
             $quota{'QUOTA'}{'USERS'}{$user}{'SHARES'}{$share}{'smbcquotas'}{'HARDLIMIT'},
             $quota{'QUOTA'}{'USERS'}{$user}{'SHARES'}{$share}{'smbcquotas'}{'USED_KiB'},
             $quota{'QUOTA'}{'USERS'}{$user}{'SHARES'}{$share}{'smbcquotas'}{'SOFTLIMIT_KiB'},
             $quota{'QUOTA'}{'USERS'}{$user}{'SHARES'}{$share}{'smbcquotas'}{'HARDLIMIT_KiB'},
             $quota{'QUOTA'}{'USERS'}{$user}{'SHARES'}{$share}{'smbcquotas'}{'USED_MiB'},
             $quota{'QUOTA'}{'USERS'}{$user}{'SHARES'}{$share}{'smbcquotas'}{'SOFTLIMIT_MiB'},
             $quota{'QUOTA'}{'USERS'}{$user}{'SHARES'}{$share}{'smbcquotas'}{'HARDLIMIT_MiB'},
             $quota{'QUOTA'}{'USERS'}{$user}{'SHARES'}{$share}{'smbcquotas'}{'SMBCQUOTAS_RETURN_STRING'},
            )=&AD_smbcquotas_queryuser(
                $root_dns,
                $smb_admin_pass,
                $user,
                $share,
	        \%sophomorix_config
            );
	}
    }
    &json_dump({json => $json,
                jsoninfo => "QUOTA",
                jsoncomment => "sophomorix quota",
                log_level => $Conf::log_level,
                hash_ref=>\%quota,
                sophomorix_config=>\%sophomorix_config,
              });
    #print Dumper(\%quota);
    exit;
}



my $ref_quota=&AD_get_quota({ldap=>$ldap,
                             root_dse=>$root_dse,
                             root_dns=>$root_dns,
                             smbcquotas=>$smbcquotas,
                             user=>$user,
                             smb_admin_pass=>$smb_admin_pass,
                             sophomorix_config=>\%sophomorix_config,
                           });



#print Dumper($ref_quota);
# print Dumper($ref_quota->{'QUOTA'}{'USERS'}{'blackmri42'});
# print Dumper($ref_quota->{'QUOTA'}{'USERS'}{'lordjo42'});

############################################################
# --nondefault / --show-nondefault
if ($show_nondefault==1){
    foreach my $school (@sharelist){
        &Sophomorix::SophomorixBase::print_title("Nondefault quota of school-share $school:");
        foreach my $class ( @{ $ref_quota->{'LISTS'}{'CLASS_by_SCHOOL'}{$school} }){
            #print "CLASS: $class\n";
            foreach my $quota ( @{ $ref_quota->{'NONDEFAULT_QUOTA'}{$school}{'CLASS'}{$class}{'sophomorixQuota'} }){
	        print "  CLASS: $class --> $quota\n";
	    }            
            foreach my $mailquota ( @{ $ref_quota->{'NONDEFAULT_QUOTA'}{$school}{'CLASS'}{$class}{'sophomorixMailQuota'} }){
	        print "  CLASS: $class --> $mailquota (MAILQUOTA)\n";
	    }            
        }
        foreach my $project ( @{ $ref_quota->{'LISTS'}{'GROUPS_by_SCHOOL'}{$school} }){
            #print "PROJECT: $project\n";
            foreach my $quota ( @{ $ref_quota->{'NONDEFAULT_QUOTA'}{$school}{'GROUPS'}{$project}{'sophomorixAddQuota'} }){
	        print "  PROJECT: $project --> $quota\n";
	    }
            foreach my $mailquota ( @{ $ref_quota->{'NONDEFAULT_QUOTA'}{$school}{'GROUPS'}{$project}{'sophomorixAddMailQuota'} }){
	        print "  PROJECT: $project --> $mailquota (MAILQUOTA)\n";
	    }
        }
        foreach my $user ( @{ $ref_quota->{'LISTS'}{'USER_by_SCHOOL'}{$school} }){
            #print "USER: $user\n";
            foreach my $quota ( @{ $ref_quota->{'NONDEFAULT_QUOTA'}{$school}{'USER'}{$user}{'sophomorixQuota'} }){
	        print "  USER: $user --> $quota\n";
	    }
            foreach my $mailquota ( @{ $ref_quota->{'NONDEFAULT_QUOTA'}{$school}{'USER'}{$user}{'sophomorixMailQuota'} }){
	        print "  USER: $user --> $mailquota (MAILQUOTA)\n";
	    }
        }
    }
    #print Dumper($ref_quota->{'NONDEFAULT_QUOTA'});
    exit;
}



############################################################
# --info --json
if ($info==1 and $json>0){
    &json_dump({json => $json,
                jsoninfo => "QUOTA",
                jsoncomment => "sophomorix quota",
                log_level => $Conf::log_level,
                hash_ref=>$ref_quota,
                sophomorix_config=>\%sophomorix_config,
              });
    exit;
############################################################
# --info 
} elsif ($info==1 and $json==0){
    my $line="+--------------------------+--------+------+".
             "------+------+-------------------+\n";
    my $line2="+------------------------------------------".
              "------------------------------------+\n";

    # if users are given but no schoollist: limit schoollist to the users schools
    if ($user ne "" and $school eq ""){
        my %schools_to_show=();
        my @schools_to_show=();
        foreach my $user (@userlist){
            my $school;
            if (exists $ref_quota->{'QUOTA'}{'LOOKUP'}{'USER'}{'sophomorixSchoolname_by_sAMAccountName'}
                {$user}{'sophomorixSchoolname'}
               ){
                $school=$ref_quota->{'QUOTA'}{'LOOKUP'}{'USER'}{'sophomorixSchoolname_by_sAMAccountName'}
                    {$user}{'sophomorixSchoolname'};
                $schools_to_show{$school}="show";
            }
        }
        # create the school list
        foreach my $sch (keys %schools_to_show) {
            push @schools_to_show, $sch; # its unique already
        }
        @schools_to_show = sort @schools_to_show;
        # replace schoollist
        @sharelist=@schools_to_show;
    }
    

    foreach my $school_share (@sharelist){
        if ($user eq ""){
            if (not exists $ref_quota->{'LISTS'}{'USER_by_SCHOOL'}{$school_share}){
                # use no users, when option is not a school share
                @userlist=();
	    } else {
	        # all users of school
	        @userlist = @{ $ref_quota->{'LISTS'}{'USER_by_SCHOOL'}{$school_share} };
	    }
        }

        ############################################################
        # HEADER for SCHOOL
        print "\n";
        &Sophomorix::SophomorixBase::print_title("User quota of school $school_share:");
        # there are 0 users
        if($#userlist==-1){
            print "     0 sophomorix users in school $school_share\n";
            next;
        }

        # school header
        if($Conf::log_level==1){
            print $line;
            printf "| %-25s| %-7s|%5s |%5s |%5s | %-18s|\n",
                   "user(role:DEFAULT)",
                   "share",
                   "CALC",
                   "USER",
                   "CLASS",
                   "PROJECTS";
            print $line;
        } else {
            # no such header for higher loglevels
        }

        ############################################################
        # Walk through users
        foreach my $user (@userlist){
            # skip if user (given by option) is not in this school 
            if (not exists $ref_quota->{'QUOTA'}{'LOOKUP'}{'USER'}{'sAMAccountName_by_sophomorixSchoolname'}{$school_share}{$user}){
                next;
            }
            &console_print_quota_user({user => $user,
                                       log_level => $Conf::log_level,
                                       ref_quota=>$ref_quota,
                                       ref_sophomorix_config=>\%sophomorix_config,
                                     });
            &console_print_mailquota_user({user => $user,
                                           log_level => $Conf::log_level,
                                           ref_quota=>$ref_quota,
                                           ref_sophomorix_config=>\%sophomorix_config,
                                         });
            if($Conf::log_level==1){
                # print line after user
                print $line;
            }
        } # end of user walk
        print "* at end of CALC: quota must be updated  **MQ**: MailQuota  **CQ**: CloudQuota\n";
    } # end of school
    exit;
}



############################################################
# --developer-setquota <share>:<value> -user u
if($developer_setquota ne "" and $user ne ""){
    my ($share,$quota)=split(":",$developer_setquota);
    foreach my $user (@userlist){
        &AD_user_setquota({ldap=>$ldap,
                           root_dse=>$root_dse,
                           root_dns=>$root_dns,
                           user=>$user,
                           share_count=>"1",
                           max_share_count=>"1",
                           share=>$share,
                           quota=>$quota,
                           smb_admin_pass=>$smb_admin_pass, 
                           debug_level=>$debug_level,
                           json=>$json,
                           sophomorix_result=>\%sophomorix_result,
                           sophomorix_config=>\%sophomorix_config,
                         });
    }
    exit;
}



#print Dumper($ref_quota->{'QUOTA'}{'USERS'}{'blackmri23'});
#exit;

################################################################################
# continue with the calculated quota from $ref_quota
################################################################################



# --set-quota-only
if ($set_quota_only==1){
    # do not set other stuff
    $set_mailquota=0;
}



# --set-mailquota-only
if ($set_mailquota_only==1){
    # do not set other stuff
    $set_quota=0;
}



my %user_updated=(); # make sure user is not updated twice
my $user_count=0; # count the users for progression output 
my $mail_user_count=0; # count the users for progression output

&Sophomorix::SophomorixBase::print_title("Setting Quota/MailQuota of the following users:");
foreach my $school_share (@sharelist){
    &Sophomorix::SophomorixBase::print_title("Setting Quota/MailQuota for school $school_share:");
    if ($user eq ""){
        # use ALL sophomorix users
        if (not exists $ref_quota->{'LISTS'}{'USER_by_SCHOOL'}{$school_share}){
            @userlist=();
	} else {
	    # all users of school
	    @userlist = @{ $ref_quota->{'LISTS'}{'USER_by_SCHOOL'}{$school_share} };
	}
    }
    if($#userlist==-1){
        print "     0 sophomorix users in school $school_share\n";
        next;
    }


    if ($set_mailquota==1){
        ############################################################
        # Walk through users for MailQuota
        foreach my $user (@userlist){
            if (not exists $ref_quota->{'QUOTA'}{'USERS'}{$user}){
                print "  WARNING: User $user not found!\n";
                next;
            }
            print "   * Mailquota of user $user:\n";
            ############################################################
            # set user MailQuota
            if ($ref_quota->{'QUOTA'}{'USERS'}{$user}{'MAILQUOTA'}{'ACTION'}{'UPDATE'} eq "TRUE" or
                $set==1
               ){
                $mail_user_count++;
                my $mailquota_calc=$ref_quota->{'QUOTA'}{'USERS'}{$user}{'MAILQUOTA'}{'CALC'};
                print "      * SET: sophomorixMailQuotaCalculated to $mailquota_calc MiB:\n";
                my $dn=$ref_quota->{'QUOTA'}{'LOOKUP'}{'USER'}{'DN_by_sAMAccountName'}{$user};
                &AD_user_update({ldap=>$ldap,
                                 root_dse=>$root_dse,
                                 dn=>$dn,
                                 user=>$user,
                                 mailquota_calc=>$mailquota_calc,
                                 user_count=>$mail_user_count,
                                 max_user_count=>$ref_quota->{'QUOTA'}{'UPDATE_COUNTER'}{'MAILQUOTA'},
                                 time_stamp_AD=> $sophomorix_config{'DATE'}{'LOCAL'}{'TIMESTAMP_AD'},
                                 json=>$json,
                                 sophomorix_config=>\%sophomorix_config,
                                 sophomorix_result=>\%sophomorix_result,
                               });
            } else {
                print "      * OK:  sophomorixMailQuotaCalculated\n";
	    }
        }
    } # set mailquota end


    if ($set_quota==1){
        ############################################################
        # Walk through users for Quota
        foreach my $user (@userlist){
            if (not exists $ref_quota->{'QUOTA'}{'USERS'}{$user}){
                print "  WARNING: User $user not found!\n";
                next;
            }
            print "   * Share-/Cloudquota of user $user:\n";
            ############################################################
            # Walk through all shares
            my $share_count=0;
            foreach my $share ( @{ $ref_quota->{'QUOTA'}{'USERS'}{$user}{'SHARELIST'} }){
                $share_count++;
                if ($ref_quota->{'QUOTA'}{'USERS'}{$user}{'SHARES'}{$share}{'ACTION'}{'UPDATE'} eq "TRUE" or
                    $set==1
                   ){
                    my $quota=$ref_quota->{'QUOTA'}{'USERS'}{$user}{'SHARES'}{$share}{'CALC'};
                    print "      * SET: $share to $quota MiB:\n";
                    if (not exists $user_updated{$user}){
                        $user_updated{$user}="seen";
                        $user_count++;
                    }
                    &AD_user_setquota({ldap=>$ldap,
                                       root_dse=>$root_dse,
                                       root_dns=>$root_dns,
                                       user=>$user,
                                       user_count=>$user_count,
                                       max_user_count=>$ref_quota->{'QUOTA'}{'UPDATE_COUNTER'}{'USERS'},
                                       share_count=>$share_count,
                                       max_share_count=>$ref_quota->{'QUOTA'}{'UPDATE_COUNTER'}{'SHARES'},
                                       share=>$share,
                                       quota=>$quota,
                                       smb_admin_pass=>$smb_admin_pass, 
                                       debug_level=>$debug_level,
                                       json=>$json,
                                       sophomorix_result=>\%sophomorix_result,
                                       sophomorix_config=>\%sophomorix_config,
                                     });
                    print "\n";
                } else {
                    print "      * OK:  $share\n";
                }
            } # end Walk through all shares
            ############################################################
            # update sophomorixCloudQuotaCalculated 
            my $cloud_user_count=0; # count the users for progression output
            if ($ref_quota->{'QUOTA'}{'USERS'}{$user}{'CLOUDQUOTA'}{'ACTION'}{'UPDATE'} eq "TRUE" or
                $set==1
               ){
                $cloud_user_count++;
                my $cloudquota_calc=$ref_quota->{'QUOTA'}{'USERS'}{$user}{'CLOUDQUOTA'}{'CALC'};
                print "      * SET: sophomorixCloudQuotaCalculated to $cloudquota_calc MiB:\n";
                my $dn=$ref_quota->{'QUOTA'}{'LOOKUP'}{'USER'}{'DN_by_sAMAccountName'}{$user};
                &AD_user_update({ldap=>$ldap,
                                 root_dse=>$root_dse,
                                 dn=>$dn,
                                 user=>$user,
                                 cloudquota_calc=>$cloudquota_calc,
                                 user_count=>$cloud_user_count,
                                 max_user_count=>$ref_quota->{'QUOTA'}{'UPDATE_COUNTER'}{'CLOUDQUOTA'},
                                 time_stamp_AD=> $sophomorix_config{'DATE'}{'LOCAL'}{'TIMESTAMP_AD'},
                                 json=>$json,
                                 sophomorix_config=>\%sophomorix_config,
                                 sophomorix_result=>\%sophomorix_result,
                               });
            } else {
                print "      * OK:  sophomorixCloudQuotaCalculated\n";
	    }
        }
    } # set quota end
}


#    print Dumper($ref_quota);


# add result mailquota
if ($set_mailquota==1){
    &result_sophomorix_add_summary({
        NAME=>"UPDATED MAILQUOTA", 
        RESULT=>$mail_user_count, 
        RESULT_TYPE => "integer",
        DESCRIPTION_POST => "users mailquota updated", 
        DESCRIPTION_PRE => "users with updated mailquota", 
        FORMAT_TYPE => 1,
        sophomorix_result=>\%sophomorix_result,
      });
}

# add result quota
if ($set_quota==1){
    &result_sophomorix_add_summary({
        NAME=>"UPDATED QUOTA", 
        RESULT=>$user_count, 
        RESULT_TYPE => "integer",
        DESCRIPTION_POST => "users smbcquota updated", 
        DESCRIPTION_PRE => "users with updated smbcquota", 
        FORMAT_TYPE => 1,
        sophomorix_result=>\%sophomorix_result,
      });
}


&result_sophomorix_check_exit(\%sophomorix_result,\%sophomorix_config,$json);
# ===========================================================================
# Start
# ===========================================================================
&log_script_start(\@arguments,\%sophomorix_result,\%sophomorix_config);


&AD_unbind_admin($ldap);
&log_script_end(\@arguments,\%sophomorix_result,\%sophomorix_config,$json);
############################################################
# End
############################################################



############################################################
# subs
############################################################
