#!/usr/bin/perl

# ------------------------------------------------------------------
#
#    Copyright (C) 2002-2005 Novell/SUSE
#
#    This program is free software; you can redistribute it and/or
#    modify it under the terms of version 2 of the GNU General Public 
#    License published by the Free Software Foundation.
#
# ------------------------------------------------------------------

################################################################################
# ag_aa_config
################################################################################

use strict;
use ycp;
use Data::Dumper;

# Subroutines
################################################################################
sub setAppArmor {

  my $action = shift;
        my $errmsg = ""; 
        my $lines = 0; 
  if ($action eq "enable") {
     if (-e "/sbin/rcapparmor") {
             open(RUN, "/sbin/rcapparmor start 2>&1 |");
     } else {
             open(RUN, "/sbin/rcapparmor start 2>&1 |");
     }
           while (<RUN>) {  
             if (/FATAL:(.*)/) {    
               $errmsg = $1;  
             }  
     }
           close(RUN);
     system("systemctl enable apparmor");
  } else {
     if (-e "/sbin/rcapparmor") {
             open(RUN, "/sbin/rcapparmor stop 2>&1 |");
     } else {
             open(RUN, "/sbin/rcapparmor stop 2>&1 |");
     }
           while (<RUN>) {  
             if (/FATAL:(.*)/) {    
               $errmsg = $1;  
             }  
           }
           close(RUN);
     system("systemctl disable apparmor");
  }
  return $errmsg;
}

sub setLearningMode {

  my $action = shift;
  my $rcscript = -f "/sbin/rcapparmor" ? "/sbin/rcapparmor"
                 : "/sbin/rcapparmor";

  if ($action eq "enable") {
    system("$rcscript", "stop");
    system("$rcscript", "complain");
  } else {
    system("$rcscript". "stop");
    system("$rcscript", "start");
  }

    return 0;
}

# Main 
################################################################################


while ( <STDIN> ) {

  my ($command, $path, $argument) = ycp::ParseCommand ($_);

  my $result = undef;
  my $action = undef;

  if ( $command && $path && $argument ) {

    ($action) = (split(/:/, $argument))[1];

    if ( $argument =~ /apparmor/ ) {
      $result = setAppArmor($action);
    } elsif ( $argument =~ /learning/ ) {
      setLearningMode($action);
    }

    if ( $result ) {
      ycp::Return( $result );
    } else {
      ycp::Return("true");
    }
  }
}

exit 0;



