File: aa-repo.pl

package info (click to toggle)
apparmor 2.7.103-4
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 11,920 kB
  • sloc: ansic: 12,022; perl: 10,644; sh: 8,119; cpp: 2,505; yacc: 1,592; python: 1,489; makefile: 1,138; lex: 1,003; pascal: 399; ruby: 374; exp: 250; java: 212; xml: 159
file content (821 lines) | stat: -rw-r--r-- 28,084 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
#!/usr/bin/perl
# ----------------------------------------------------------------------
#    Copyright (c) 2008 Dominic Reynolds. All Rights Reserved.
#
#    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 as published by the Free Software Foundation.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#
# ----------------------------------------------------------------------

# ----------------------------------------------------------------------
#
my $usage =
"aa-repo.pl --command args\n";

my $usage_search =
"    --search [author=XXX] [prog=XXX] [id=XXX]
             Search the repository for profiles matching the search criteria
             and return the results.
             NOTE: One --search switch per option

    --verbose|v
             Verbosity level. Supply either one or two switches. Two switches
             adds full profile text in returned search results.\n";

my $usage_push =

"    --push   [--profile=XXX|all] [--changelog=XXX]
             Push local profiles to repository, uses configured user and upon
             overwrite of an existing profile in the repository then prompt
             user with a diff for confirmation XXX the name of the application
             whose profile should be uploaded or \"all\"  to upload all
             profiles.  Multiple --profile switches may be passed to supply
             multiple profile names

             e.g.  --push --profile /usr/sbin/mdnsd --profile /usr/sbin/ftp
             e.g.  --push --profile all\n";

my $usage_pull =
"    --pull   [--author=XXX] [--profile=XXX] or [--id=XXX] [--mode=complain]
             pull remote profiles and install on local system
             If operation will change local profiles then prompt user with
             diff for confirmation
             NOTE: One --pull switch per option and there are three acceptable
                   combinations

             --pull --author=XXX
               * pull all profiles in the repo for the author

             --pull --author=XXX  --profile=XXXX
               * pull the profile for prog owned by author

             --pull --id=XXXX
               * pull the profile with id

             --pull --mode=complain
               * set the profile(s) to complain mode when installed

             Profiles are checked for conflicts with currently installed
             profiles and presented as a list to the user to confirm and view.\n";


my $usage_sync =
"    --sync   [--up] [--down] [--noconfirm]
             Synchronize local profile set with the repository - showing
             changes and allowing prompting the user with the diffs and
             suggest the newest version to be activated. If the --all option
             is passed then treat profiles not marked as remote as new
             profiles that will be uploaded to the repository.\n";

my $usage_stat =
"    --status
             Show the current status of the local profile set. This operation
             is similar to sync but does not prompt the user to up|down load
             changes\n";

my $usage_getconfig =
"    --getconfig|c
             Print the current configuration for the repsository\n";


my $usage_setconfig =
"    --setconfig [url=xxx] [username=xxxx] [password=xxxx] [enabled=(yes|no)]
                [upload=(yes|no)]
              Set the configuration options for the repository.
              NOTE: One --setconfig switch per option\n";

my $usage_bottom =
"    --quiet|q Don't prompt user - assume that all changes should be made.

    ISSUES:
      o Should changes made to the system be recorded somehow? An audit event?
      o Should the tool allow a repo/distro to be passed for each operation?

";

use strict;
use Getopt::Long;

use Immunix::AppArmor;
use Immunix::Repository;
use Data::Dumper;

use Locale::gettext;
use POSIX;

# force $PATH to be sane
$ENV{PATH} = "/bin:/sbin:/usr/bin:/usr/sbin";

# initialize the local poo
setlocale(LC_MESSAGES, "");
textdomain("apparmor-utils");

# options variables
my $help = '';
my $verbose = '';

my ( $id, $author, $mode, %search, $sync, $getconfig, $push,
     $pull, %setconfig, @profiles, $all, $changelog, $stat );

GetOptions(
    'search=s%'    => \%search,
    'sync=s'       => \$sync,
    'status'       => \$stat,
    'getconfig|c'  => \$getconfig,
    'setconfig=s%' => \%setconfig,
    'push'         => \$push,
    'id=s'         => \$id,
    'author=s'     => \$author,
    'profile=s'    => \@profiles,
    'changelog=s'  => \$changelog,
    'pull'         => \$pull,
    'all|a'        => \$all,
    'help|h'       => \$help,
    'verbose|v+'   => \$verbose
);

#
# Root privs required to run the repo tool
#
if ( geteuid() != 0 ) {
    print STDERR gettext(
"You must be logged in with root user privileges to use this program.\n"
                        );
    exit;
}

# --help
# tell 'em how to use it...
&usage && exit if $help;

my $config = get_repo_config();

#
# --getconfig operation
#
&config && exit if $getconfig;

my $sd_mountpoint = check_for_subdomain();
unless ($sd_mountpoint) {
    fatal_error(gettext(
"AppArmor does not appear to be started.  Please enable AppArmor and try again."
                       )
               );
}

#
# --setconfig operation
#
if ( keys %setconfig ) {
   $config->{url}      = $setconfig{url}      if ( $setconfig{url} );
   $config->{distro}   = $setconfig{distro}   if ( $setconfig{distro} );
   $config->{enabled}  = $setconfig{enabled}  if ( $setconfig{enabled} );
   $config->{email}    = $setconfig{email}    if ( $setconfig{email} );
   $config->{user}     = $setconfig{username} if ( $setconfig{username} );
   $config->{password} = $setconfig{password} if ( $setconfig{password} );
   $config->{upload}   = $setconfig{upload}   if ( $setconfig{upload} );
   set_repo_config( $config );
}

#
# --push operation
#
if ( $push ) {
    my ($conflicts, $repo_profiles, $local_profiles, @overrides);
    if ( ! @profiles ) {
       print STDERR gettext(
"Must supply at least one profile using \"--profile XXX\" to --push\n"
                           );
       exit 1;
    } else {
       print STDERR Data::Dumper->Dump([@profiles], [qw(*profiles)]);
    }
    my $changelog = $changelog?$changelog:"none";
    push_profiles( \@profiles, $changelog, 1 );
}


#
#  --pull operation
#
if ( $pull ) {
    my $type = "";
    if ( $id ) {
        if ( $author || @profiles ) {
            print STDERR gettext(
"Option --id=XX is only allowed by itself and not in combination to
other options for the --pull command.\n"
                                );
            exit 1;
       }
       $type = "id";
    }
    if ( @profiles && ! $author ) {
        print STDERR gettext(
"Option --profile=XX requires that the --author=XX option be supplied
to distinguish a specific profile.\n"
                            );
        exit 1;
    } else {
        $type = "profile";
    }

    my $mode = $mode eq "complain"?1:0;
    pull_profiles( \@profiles, $type, $mode, 1 );
}

#
#  --search operation
#
if ( keys %search ) {
    if ( $search{id} ) {
        my($status,$result) = fetch_profile_by_id( $config->{url},
                                                   $search{id} );
        if ($status) {
            my $title = sprintf(gettext( "Profile ID %s\n"), $search{id});
            console_print_search_results( $title,
                                          "profile",
                                          { $result->{name} => $result }
                                        );

        } else {
            print STDERR "ERROR $result\n";
        }
    } elsif ( $search{author} && $search{prog} ) {
        my($status,$result) =
            fetch_profiles_by_name_and_user( $config->{url},
                                             $config->{distro},
                                             $search{prog},
                                             $search{author}
                                           );
        if ( $status ) {
            my $title =
                sprintf(gettext("Profiles matching user: %s and program: %s\n"),
                        $search{author},
                        $search{prog}
                       );
            console_print_search_results( $title, "profile", $result );
        } else {
            print STDERR "ERROR $result\n";
        }
    } elsif ( $search{author} ) {
        my($status,$result) = fetch_profiles_by_user( $config->{url},
                                                      $config->{distro},
                                                      $search{author}
                                                    );
        if ( $status ) {
            my $title = sprintf(gettext( "Profiles for %s\n"), $search{author});
            console_print_search_results( $title, "profile", $result );
        } else {
            print STDERR "ERROR $result\n";
        }
    } elsif ( $search{prog} ) {
        my($status,$result) = fetch_profiles_by_name( $config->{url},
                                                      $config->{distro},
                                                      $search{prog},
                                                    );
        if ( $status ) {
            my $title = sprintf(gettext("Profiles matching program: %s\n"),
                                $search{prog});
            console_print_search_results( $title, "user", $result );
        } else {
            print STDERR "ERROR $result\n";
        }
    } else  {
        print STDERR
"Unsupported search criteria. Please specify at least one of
author=XXX prog=XXX id=XXX\n";
    }
}

if ( $stat ) {
    my ( $local_profiles, $remote_profiles );
    my $msg =
" The following profiles are stored in the repository but
 are not synchronized with the copy in the repository\n";

    my ($status, $result) = fetch_profiles_by_user( $config->{url},
                                                    $config->{distro},
                                                    $config->{user}
                                                  );
    if ( $status ) {
        $remote_profiles = $result;
    } else {
        print STDERR sprintf(gettext("ERROR connecting to repository: %s\n"),
                             $result);
        exit;
    }

    readprofiles();
    $local_profiles = serialize_local_profiles( \%sd );
    my ($local_only,$unsynched,$synched,$conflicts) = ({}, {}, {});
    $unsynched = find_profile_conflicts($remote_profiles, $local_profiles);
    for my $p ( keys %$local_profiles ) {
        if ( ! $remote_profiles->{$p} ) {
            $local_only->{$p} = $local_profiles->{$p};
        }
    }

    for my $p ( keys %$remote_profiles ) {
        $synched->{$p} =
            $remote_profiles->{$p}->{profile} if ( ! %$unsynched->{$p} );
    }
    UI_status($synched, $unsynched, $local_only);
}

######################
# Helper functions
######################

#
# Compare the local profile set with the remote profile set.
# Return a list of the conflicting profiles as a list
#  { PROFILE_NAME =>  [LOCAL_PROFILE, REMOTE_PROFILE] ]
#
#
# remote_profiles = repository profiles as returned by one of the
#                   Immunix::Repository::fetch... functions
# local_profiles  = hash ref containing
#                    {  name => serialized local profile }
#
#

sub find_profile_conflicts ($$) {
    my ($remote_profiles,$local_profiles) = @_;
    my $conflicts = {};
    for my $p ( keys(%$local_profiles) ) {
        if ( $local_profiles->{$p} and $remote_profiles->{$p} ) {
            my $p_local  = $local_profiles->{$p};
            my $p_remote = $remote_profiles->{$p}->{profile};
            chomp($p_local);
            chomp($p_remote);
            if ( $p_remote ne $p_local ) {
                $conflicts->{$p} = [ $p_local, $p_remote ];
            }
        }
    }
   return( $conflicts );
}

sub serialize_local_profiles ($) {
   my $profiles = shift;
   my $local_profiles = {};
   for my $p ( keys %$profiles ) {
       my $serialize_opts = {};
       $serialize_opts->{NO_FLAGS} = 1;
       my $p_local = serialize_profile( $profiles->{$p},
                                        $p,
                                        $serialize_opts );
       $local_profiles->{$p} = $p_local;
   }
   return $local_profiles;
}


sub console_print_search_results ($$$) {
    my ($title, $type,$result) = @_;
    open(PAGER, "| less") or die "Can't open pager";
    print PAGER $title;
    print PAGER "Found " . values(%$result) . " profiles \n";
    for my $p ( values(%$result) ) {
        if ( $verbose ) {
            if ( $type eq "user" ) {
                print PAGER " Author    [ " . $p->{username} . " ]\n";
            } elsif ( $type eq "profile" ) {
                print PAGER " Name      [ " . $p->{name} . " ]\n";
            }
            print PAGER " Created   [ " . $p->{created_at} . " ]\n";
            print PAGER " Downloads [ " . $p->{downloaded_count} . " ]\n";
            print PAGER " ID        [ " . $p->{id} . " ]\n";
            if ( $verbose > 1 ) {
                print PAGER " Profile   [ \n" . $p->{profile} . " ]\n\n";
            } else {
                print PAGER "\n";
            }
        } else {
            my $data =  $type eq "user"?$p->{username}:$p->{name};
            print PAGER " " . $data . "\n";
        }
    }
    close PAGER;
}

sub UI_resolve_profile_conflicts {

    my ($explanation, $conflict_hash) = @_;
    my $url = $config->{url};
    my @conflicts = map { [ $_,
                            $conflict_hash->{$_}->[0],
                            $conflict_hash->{$_}->[1]
                          ] }
                          keys %$conflict_hash;
    my @commits = [];
    my $title = "Profile conflicts";
    my %resolution = ();
    my $q = {};
    $q->{title} = $title;
    $q->{headers} = [ "Repository", $url, ];

    $q->{explanation} = $explanation;

    $q->{functions} = [ "CMD_OVERWRITE",
                        "CMD_KEEP",
                        "CMD_VIEW_CHANGES",
                        "CMD_ABORT",
                        "CMD_CONTINUE", ];

    $q->{default} = "CMD_OVERWRITE";
    $q->{options} = [ map { $_->[0] } @conflicts ];
    $q->{selected} = 0;

    my ($ans, $arg);
    do {
        ($ans, $arg) = UI_PromptUser($q);

        if ($ans eq "CMD_VIEW_CHANGES") {
            display_changes($conflicts[$arg]->[2], $conflicts[$arg]->[1]);
        }
        if ( $ans eq "CMD_OVERWRITE") {
           $q->{options} =
             [ map { $_ =~ /$conflicts[$arg]->[0](  K|  O)?$/?
                           $conflicts[$arg]->[0] . "  O":
                           $_ }
                   @{$q->{options}}
             ];
             $resolution{$conflicts[$arg]->[0]} = "O";
        }
        if ( $ans eq "CMD_KEEP") {
           $q->{options} =
             [ map { $_ =~ /$conflicts[$arg]->[0](  K|  O)?$/?
                           $conflicts[$arg]->[0] . "  K":
                           $_ }
                   @{$q->{options}}
             ];
             $resolution{$conflicts[$arg]->[0]} = "K";
        }
        $q->{selected} = ($arg+1) % @conflicts;
    } until $ans =~ /^CMD_CONTINUE/;
    if ($ans eq "CMD_CONTINUE") {
        my @results = ();
        for my $p ( keys %resolution ) {
            if ( $resolution{$p} eq "O" ) {
                 push @results, $p;
            }
        }
        return @results;
    }
}

sub UI_display_profiles {
    my ($explanation, $profile_hash) = @_;
    my $url = $config->{url};
    my @profiles = map { [ $_, $profile_hash->{$_} ] } keys %$profile_hash;
    my $title = gettext("Profiles");
    my $q = {};
    $q->{title} = $title;
    $q->{headers} = [ "Repository", $url, ];

    $q->{explanation} = $explanation;

    $q->{functions} = [ "CMD_VIEW",
                        "CMD_CONTINUE", ];

    $q->{default} = "CMD_CONTINUE";
    $q->{options} = [ map { $_->[0] } @profiles ];
    $q->{selected} = 0;

    my ($ans, $arg);
    do {
        ($ans, $arg) = UI_PromptUser($q);

        if ($ans eq "CMD_VIEW") {
            my $pager = get_pager();
            open ( PAGER, "| $pager" ) or die "Can't open $pager";
            print PAGER gettext("Profile: ") . $profiles[$arg]->[0] . "\n";
            print PAGER $profiles[$arg]->[1];
            close PAGER;
        }
        $q->{selected} = ($arg+1) % @profiles;
    } until $ans =~ /^CMD_CONTINUE/;
    return;
}

sub UI_display_profile_conflicts {
    my ($explanation, $conflict_hash) = @_;
    my $url = $config->{url};
    my @conflicts = map { [ $_,
                            $conflict_hash->{$_}->[0],
                            $conflict_hash->{$_}->[1]
                          ] }
                          keys %$conflict_hash;
    my @commits = [];
    my $title = gettext("Profile conflicts");
    my $q = {};
    $q->{title} = $title;
    $q->{headers} = [ "Repository", $url, ];

    $q->{explanation} = $explanation;

    $q->{functions} = [ "CMD_VIEW_CHANGES",
                        "CMD_CONTINUE", ];

    $q->{default} = "CMD_CONTINUE";
    $q->{options} = [ map { $_->[0] } @conflicts ];
    $q->{selected} = 0;

    my ($ans, $arg);
    do {
        ($ans, $arg) = UI_PromptUser($q);

        if ($ans eq "CMD_VIEW_CHANGES") {
            display_changes($conflicts[$arg]->[2], $conflicts[$arg]->[1]);
        }
        $q->{selected} = ($arg+1) % @conflicts;
    } until $ans =~ /^CMD_CONTINUE/;
    return;
}

sub usage {
    if ( $help eq "push" ) {
        print STDERR $usage . $usage_push ."\n";
    } elsif ( $help eq "pull" ) {
        print STDERR $usage . $usage_pull ."\n";
    } elsif ( $help eq "sync" ) {
        print STDERR $usage . $usage_sync ."\n";
    } elsif ( $help eq "getconfig" ) {
        print STDERR $usage . $usage_getconfig ."\n";
    } elsif ( $help eq "setconfig" ) {
        print STDERR $usage . $usage_setconfig ."\n";
    } elsif ( $help eq "status" ) {
        print STDERR $usage . $usage_stat ."\n";
    } elsif ( $help eq "search" ) {
        print STDERR $usage . $usage_search ."\n";
    } else {
     open(PAGER, "| less") or die "Can't open pager";
     print PAGER $usage .
                 $usage_search .
                 $usage_push .
                 $usage_pull .
                 $usage_sync .
                 $usage_stat .
                 $usage_setconfig .
                 $usage_getconfig .
                 $usage_bottom . "\n";
     close PAGER;
   }
}

#
# --getconfig helper function
#
sub config {
    my $configstr = gettext("Current config\n");
    my $config = get_repo_config();
    $configstr .= "\turl:\t\t$config->{url}\n";
    $configstr .= "\tdistro:\t\t$config->{distro}\n";
    $configstr .= "\tenabled:\t$config->{enabled}\n";
    $configstr .= "\temail:\t\t$config->{email}\n";
    $configstr .= "\tusername:\t$config->{user}\n";
    $configstr .= "\tpassword:\t$config->{password}\n";
    $configstr .= "\tupload:\t\t$config->{upload}\n";
    print STDERR $configstr . "\n";
}

#
#  helper function to push profiles to the repository
#  used by --push and --sync options
#
sub push_profiles($$$) {
    my ( $p_ref, $changelog, $confirm ) = @_;
    my ( $conflicts, $remote_profiles, $local_profiles, @overrides );
    my @profiles = @$p_ref;

    my $conflict_msg =
" The following profile(s) selected for upload conflicts with a profile already
 stored in the repository for your account. Please choose whether to keep the
 current version or overwrite it.\n";
    $all = 0;

    readprofiles();
    my ($status, $result) = fetch_profiles_by_user( $config->{url},
                                                    $config->{distro},
                                                    $config->{user}
                                                  );
    if ( $status ) {
        $remote_profiles = $result;
    } else {
        print STDERR sprintf(gettext("ERROR connecting to repository: %s\n"),
                             $result);
        exit;
    }

    $all = 1 if ( grep(/^all$/, @profiles) );

    if ( $all ) {
        $local_profiles = serialize_local_profiles( \%sd );
    } else {
        my $local_sd = {};
        for my $p ( @profiles ) {
            if ( !$sd{$p} ) {
                print STDERR
                    sprintf(gettext("Profile for [%s] does not exist\n"), $p);
                exit;
            }
            $local_sd->{$p} = $sd{$p};
        }
        $local_profiles = serialize_local_profiles( $local_sd );
    }

    $conflicts = find_profile_conflicts($remote_profiles, $local_profiles);

    if ( keys %$conflicts ) {
        @overrides = UI_resolve_profile_conflicts( $conflict_msg, $conflicts );
    }

    if ( $local_profiles ) {
        my @uploads;
        for my $p ( keys %$local_profiles ) {
            unless ( $conflicts->{$p} and !grep(/^$p$/, @overrides) ) {
                print STDERR gettext("Uploading ") .  $p . "... ";
                my ($status,$result) = upload_profile( $config->{url},
                                                       $config->{user},
                                                       $config->{password},
                                                       $config->{distro},
                                                       $p,
                                                       $local_profiles->{$p},
                                                       $changelog
                                                     );
                print STDERR gettext("done") . "\n";
            }
            if ( $status ) {
               push @uploads, $p;
            } else {
              print STDERR gettext("Error uploading") . "$p: $result\n";
            }
        }
        if ( @uploads ) {
            #
            # Currently the upload API with the repository returns the
            # the current users profile set before the update so we have
            # to refetch to obtain the metadata to update the local profiles
            #
            my $repo_p = [];
            print STDERR gettext("Updating local profile metedata....\n");
            my ($status,$result) = fetch_profiles_by_user( $config->{url},
                                                           $config->{distro},
                                                           $config->{user} );
            if ( $status ) {
                for my $p ( @uploads ) {
                   push( @$repo_p, [$p, $result->{$p}] ) if ( $result->{$p} );
                }
                activate_repo_profiles( $config->{url}, $repo_p, 0 );
                print STDERR gettext(" done\n");
            } else {
                print STDERR gettext(
"Failed to retrieve updated profiles from the repository. Error: "
                                    ) . $result . "\n";
            }
        }
    }
}

#
# Helper function for pulling profiles from the repository
# used by --pull and --sync options
#
sub pull_profiles($$$$) {
    my ( $p_ref, $mode, $confirm, $opts ) = @_;
    my @profiles = @$p_ref;
    my ( $conflicts, $commit_list, $remote_profiles,
         $local_profiles, @overrides );

    my $conflict_msg =
"  The following profiles selected for download conflict with profiles
  already deployed on the system. Please choose whether to keep the local
  version or overwrite with the version from the repository\n";

    readprofiles();

    if ( $opts->{id} ) {
        my ($status,$newp) = fetch_profile_by_id( $config->{url}, $opts->{id} );
        if ( ! $status ) {
            print STDERR gettext(
                             sprintf("Error occured during operation\n\t[%s]\n",
                                     $newp
                                    )
                                );
            exit 1;
        } else {
            $remote_profiles = { $newp->{name} => $newp->{profile} };
        }
    } elsif ( @profiles && $opts->{author} ) {
        $remote_profiles = {};
        for my $p ( @profiles ) {
            my ($status,$profiles) =
                fetch_profiles_by_name_and_user( $config->{url},
                                                 $config->{distro},
                                                 $p,
                                                 $opts->{author} );
            if ( ! $status ) {
                print STDERR gettext(sprintf(
                                     "Error occured during operation\n\t[%s]\n",
                                             $profiles
                                            )
                                    );
                exit 1;
            } else {
                $remote_profiles->{$p} = $profiles->{$p};
            }
        }
    } elsif ( $opts->{author} ) {
        my ($status,$profiles) = fetch_profiles_by_user( $config->{url},
                                                         $config->{distro},
                                                         $opts->{author} );
        if ( ! $status ) {
            print STDERR gettext(sprintf(
                                     "Error occured during operation\n\t[%s]\n",
                                         $profiles
                                        )
                                );
            exit 1;
        } else {
            $remote_profiles = $profiles;
        }
    }
    $local_profiles = serialize_local_profiles( \%sd );
    $conflicts = find_profile_conflicts( $remote_profiles, $local_profiles );
    if ( keys %$conflicts ) {
        @overrides = UI_resolve_profile_conflicts( $conflict_msg, $conflicts );
    }
    for my $p ( keys %$remote_profiles ) {
        unless ( $conflicts->{$p} and !grep(/^$p$/, @overrides) ) {
            $remote_profiles->{$p}->{username} = $opts->{author};
            push @$commit_list, [$p, $remote_profiles->{$p}];
        }
    }

    if ( $commit_list and @$commit_list ) {
        activate_repo_profiles( $config->{url}, $commit_list, $mode );
        system("rcapparmor reload");
    } else {
        UI_Info(gettext("No changes to make"));
    }
}

sub UI_status {

    my ($synched, $unsynched, $local) = @_;
    my $url = $config->{url};
    my $synched_text   = gettext("Synchronized repository profiles:\t\t") .
                                 keys %$synched;
    my $unsynched_text = gettext("Unsynchronized repository profiles:\t") .
                                 keys %$unsynched;
    my $local_text     = gettext("Local only profiles :\t\t\t") . keys %$local;
    my $options        = [ $synched_text, $unsynched_text, $local_text ];
    my $title          = gettext("Profile Status");
    my $explanation    = gettext(
" This is the current status of active profiles on the system.
 To view the profiles or unsyncronized changes select VIEW\n"
    );
    my $q = {};
    $q->{title} = $title;
    $q->{headers} = [ "Repository", $url, ];
    $q->{explanation} = $explanation;
    $q->{functions} = [ "CMD_VIEW", "CMD_FINISHED", ];
    $q->{default} = "CMD_FINISHED";
    $q->{options} = $options;
    $q->{selected} = 0;

    my ($ans, $arg);
    do {
        ($ans, $arg) = UI_PromptUser($q);

        if ($ans eq "CMD_VIEW") {
            if ( $arg == 0 ) {
                UI_display_profiles(
                    gettext("Profiles stored in the repository"),
                    $synched
                );
            } elsif ( $arg == 1 ) {
                UI_display_profile_conflicts(
                                      gettext("Unsyncronised profile changes"),
                                      $unsynched
                                            );
            } elsif ( $arg == 2 ) {
                UI_display_profiles(
                    gettext("Profiles stored in the repository"),
                    $local
                );
            }
        }
    } until $ans =~ /^CMD_FINSHED/;
}