This file is indexed.

/usr/sbin/a2enmod is in apache2 2.4.7-1ubuntu4.20.

This file is owned by root:root, with mode 0o755.

The actual contents of the file can be viewed below.

  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
#!/usr/bin/perl -w
#
# a2enmod by Stefan Fritsch <sf@debian.org>
# Licensed under Apache License 2.0
#
# The coding style is "perltidy -pbp"

use strict;
use Cwd 'realpath';
use File::Spec;
use File::Basename;
use File::Path;
use Getopt::Long;

my $quiet;
my $force;
my $maintmode;
my $purge;

Getopt::Long::Configure('bundling');
GetOptions(
    'quiet|q'     => \$quiet,
    'force|f'     => \$force,
    'maintmode|m' => \$maintmode,
    'purge|p'     => \$purge
) or exit 2;

my $basename = basename($0);
$basename =~ /^a2(en|dis)(mod|site|conf)((?:-.+)?)$/
    or die "$basename call name unknown\n";
my $act        = $1;
my $obj        = $2;
my $dir_suffix = $3;

my $env_file = $ENV{APACHE_ENVVARS}
    || (
    $ENV{APACHE_CONFDIR}
    ? "$ENV{APACHE_CONFDIR}/envvars"
    : "/etc/apache2$dir_suffix/envvars"
    );
$ENV{LANG} = 'C';
read_env_file($env_file);

$act .= 'able';
my ( $name, $dir, $sffx, $reload );
if ( $obj eq 'mod' ) {
    $obj    = 'module';
    $dir    = 'mods';
    $sffx   = '.load';
    $reload = 'restart';
}
elsif ( $obj eq 'conf' ) {
    $obj    = 'conf';
    $dir    = 'conf';
    $sffx   = '.conf';
    $reload = 'reload';
}
else {
    $dir    = 'sites';
    $sffx   = '.conf';
    $reload = 'reload';
}
$name = ucfirst($obj);

my $confdir = $ENV{APACHE_CONFDIR} || "/etc/apache2$dir_suffix";
my $availdir = $ENV{ uc("APACHE_${dir}_AVAILABLE") }
    || "$confdir/$dir-available";
my $enabldir = $ENV{ uc("APACHE_${dir}_ENABLED") } || "$confdir/$dir-enabled";
my $statedir = $ENV{ uc("APACHE_STATE_DIRECTORY") } || "/var/lib/apache2";

$statedir .= "/$obj";

my $choicedir = $act eq 'enable' ? $availdir : $enabldir;
my $linkdir = File::Spec->abs2rel( $availdir, $enabldir );

my $request_reload = 0;

my $rc = 0;

if ( !scalar @ARGV ) {
    my @choices = myglob('*');
    print "Your choices are: @choices\n";
    print "Which ${obj}(s) do you want to $act (wildcards ok)?\n";
    my $input = <>;
    @ARGV = split /\s+/, $input;

}

my @objs;
foreach my $arg (@ARGV) {
    $arg =~ s/${sffx}$//;
    my @glob = myglob($arg);
    if ( !@glob ) {
        error("No $obj found matching $arg!\n");
        $rc = 1;
    }
    else {
        push @objs, @glob;
    }
}

foreach my $acton (@objs) {
    doit($acton) or $rc = 1;
}

info(
    "To activate the new configuration, you need to run:\n  service apache2 $reload\n"
) if $request_reload;

exit($rc);

##############################################################################

sub myglob {
    my $arg = shift;

    my @glob = map {
        s{^$choicedir/}{};
        s{$sffx$}{};
        $_
    } glob("$choicedir/$arg$sffx");

    return @glob;
}

sub doit {
    my $acton = shift;

    my ( $conftgt, $conflink );
    if ( $obj eq 'module' ) {
        if ( $acton eq 'cgi' && threaded() ) {
            print
                "Your MPM seems to be threaded. Selecting cgid instead of cgi.\n";
            $acton = 'cgid';
        }

        $conftgt = "$availdir/$acton.conf";
        if ( -e $conftgt ) {
            $conflink = "$enabldir/$acton.conf";
        }
    }

    my $tgt  = "$availdir/$acton$sffx";
    my $link = "$enabldir/$acton$sffx";

    if ( !-e $tgt ) {
        if ( -l $link && !-e $link ) {
            if ( $act eq 'disable' ) {
                info("removing dangling symlink $link\n");
                unlink($link);

                # force a .conf path. It may exist as dangling link, too
                $conflink = "$enabldir/$acton.conf";

                if ( -l $conflink && !-e $conflink ) {
                    info("removing dangling symlink $conflink\n");
                    unlink($conflink);
                }

                return 1;
            }
            else {
                error("$link is a dangling symlink!\n");
            }
        }

        if ( $purge ) {
            switch_marker( $obj, $act, $acton );
            # exit silently, we are purging anyway
            return 1;
        }

        error("$name $acton does not exist!\n");
        return 0;
    }

    # handle module dependencies
    if ( $obj eq 'module' ) {
        if ( $act eq 'enable' ) {
            my @depends = get_deps("$availdir/$acton.load");
            do_deps( $acton, @depends ) or return 0;

            my @conflicts = get_deps( "$availdir/$acton.load", "Conflicts" );
            check_conflicts( $acton, @conflicts ) or return 0;
        }
        else {
            my @depending;
            foreach my $d ( glob("$enabldir/*.load") ) {
                my @deps = get_deps($d);
                if ( is_in( $acton, @deps ) ) {
                    $d =~ m,/([^/]+).load$,;
                    push @depending, $1;
                }
            }
            if ( scalar @depending ) {
                if ($force) {
                    do_deps( $acton, @depending ) or return 0;
                }
                else {
                    error(
                        "The following modules depend on $acton ",
                        "and need to be disabled first: @depending\n"
                    );
                    return 0;
                }
            }
        }
    }
    elsif ( $act eq 'enable' ) {
        my @depends = get_deps("$availdir/$acton$sffx");
        warn_deps( $acton, @depends ) or return 0;
    }

    if ( $act eq 'enable' ) {
        my $check = check_link( $tgt, $link );
        if ( $check eq 'ok' ) {
            if ($conflink) {

                # handle .conf file
                my $confcheck = check_link( $conftgt, $conflink );
                if ( $confcheck eq 'ok' ) {
                    info("$name $acton already enabled\n");
                    return 1;
                }
                elsif ( $confcheck eq 'missing' ) {
                    print "Enabling config file $acton.conf.\n";
                    add_link( $conftgt, $conflink ) or return 0;
                }
                else {
                    error(
                        "Config file $acton.conf not properly enabled: $confcheck\n"
                    );
                    return 0;
                }
            }
            else {
                info("$name $acton already enabled\n");
                return 1;
            }
        }
        elsif ( $check eq 'missing' ) {
            if ($conflink) {

                # handle .conf file
                my $confcheck = check_link( $conftgt, $conflink );
                if ( $confcheck eq 'missing' ) {
                    add_link( $conftgt, $conflink ) or return 0;
                }
                elsif ( $confcheck ne 'ok' ) {
                    error(
                        "Config file $acton.conf not properly enabled: $confcheck\n"
                    );
                    return 0;
                }
            }

            print "Enabling $obj $acton.\n";
            if ( $acton eq 'ssl' ) {
                info(     "See /usr/share/doc/apache2/README.Debian.gz on "
                        . "how to configure SSL and create self-signed certificates.\n"
                );
            }
            return add_link( $tgt, $link )
                && switch_marker( $obj, $act, $acton );
        }
        else {
            error("$name $acton not properly enabled: $check\n");
            return 0;
        }
    }
    else {
        if ( -e $link || -l $link ) {
            remove_link($link);
            if ( $conflink && -e $conflink ) {
                remove_link($conflink);
            }
            switch_marker( $obj, $act, $acton );
            print "$name $acton disabled.\n";
        }
        elsif ( $conflink && -e $conflink ) {
            print "Disabling stale config file $acton.conf.\n";
            remove_link($conflink);
        }
        else {
            info("$name $acton already disabled\n");
            if ( $purge ) {
                switch_marker( $obj, $act, $acton );
            }
            return 1;
        }
    }

    return 1;
}

sub get_deps {
    my $file = shift;
    my $type = shift || "Depends";

    my $fd;
    if ( !open( $fd, '<', $file ) ) {
        error("Can't open $file: $!");
        return;
    }
    my $line;
    while ( defined( $line = <$fd> ) ) {
        chomp $line;
        if ( $line =~ /^# $type:\s+(.*?)\s*$/ ) {
            my $deps = $1;
            return split( /[\n\s]+/, $deps );
        }

        # only check until the first non-empty non-comment line
        last if ( $line !~ /^\s*(?:#.*)?$/ );
    }
    return;
}

sub do_deps {
    my $acton = shift;
    foreach my $d (@_) {
        info("Considering dependency $d for $acton:\n");
        if ( !doit($d) ) {
            error("Could not $act dependency $d for $acton, aborting\n");
            return 0;
        }
    }
    return 1;
}

sub warn_deps {
    my $acton = shift;
    my $modsenabldir = $ENV{APACHE_MODS_ENABLED} || "$confdir/mods-enabled";
    foreach my $d (@_) {
        info("Checking dependency $d for $acton:\n");
        if ( !-e "$modsenabldir/$d.load" ) {
            warning(
                "Module $d is not enabled, but $acton depends on it, aborting\n"
            );
            return 0;
        }
    }
    return 1;
}

sub check_conflicts {
    my $acton    = shift;
    my $haderror = 0;
    foreach my $d (@_) {
        info("Considering conflict $d for $acton:\n");

        my $tgt  = "$availdir/$d$sffx";
        my $link = "$enabldir/$d$sffx";

        my $confcheck = check_link( $tgt, $link );
        if ( $confcheck eq 'ok' ) {
            error(
                "Module $d is enabled - cannot proceed due to conflicts. It needs to be disabled first!\n"
            );

            # Don't return immediately, there could be several conflicts
            $haderror++;
        }
    }

    if ($haderror) {
        return 0;
    }

    return 1;
}

sub add_link {
    my ( $tgt, $link ) = @_;

    # create relative link
    if ( !symlink( File::Spec->abs2rel( $tgt, dirname($link) ), $link ) ) {
        die("Could not create $link: $!\n");
    }
    $request_reload = 1;
    return 1;
}

sub check_link {
    my ( $tgt, $link ) = @_;

    if ( !-e $link ) {
        if ( -l $link ) {

            # points to nowhere
            info("Removing dangling link $link");
            unlink($link) or die "Could not remove $link\n";
        }
        return 'missing';
    }

    if ( -e $link && !-l $link ) {
        return "$link is a real file, not touching it";
    }
    if ( realpath($link) ne realpath($tgt) ) {
        return "$link exists but does not point to $tgt, not touching it";
    }
    return 'ok';
}

sub remove_link {
    my ($link) = @_;

    if ( -l $link ) {
        unlink($link) or die "Could not remove $link: $!\n";
    }
    elsif ( -e $link ) {
        error("$link is not a symbolic link, not deleting\n");
        return 0;
    }
    $request_reload = 1;
    return 1;
}

sub threaded {
    my $result = "";
    $result = qx{/usr/sbin/apache2ctl -V | grep 'threaded'}
        if -x '/usr/sbin/apache2ctl';
    if ( $? != 0 ) {

        # config doesn't work
        if ( -e "$enabldir/mpm_prefork.load" || -e "$enabldir/mpm_itk.load" )
        {
            return 0;
        }
        elsif (-e "$enabldir/mpm_worker.load"
            || -e "$enabldir/mpm_event.load" )
        {
            return 1;
        }
        else {
            error("Can't determine enabled MPM");

            # do what user requested
            return 0;
        }
    }
    if ( $result =~ / no/ ) {
        return 0;
    }
    elsif ( $result =~ / yes/ ) {
        return 1;
    }
    else {
        die("Can't parse output from apache2ctl -V:\n$result\n");
    }
}

sub info {
    print @_ if !$quiet;
}

sub error {
    print STDERR 'ERROR: ', @_;
}

sub warning {
    print STDERR 'WARNING: ', @_;
}

sub is_in {
    my $needle = shift;
    foreach my $e (@_) {
        return 1 if $needle eq $e;
    }
    return 0;
}

sub read_env_file {
    my $file = shift;

    -r $file or return;
    my @lines = qx{env - sh -c '. $file && env'};
    if ($?) {
        die "Could not read $file\n";
    }

    foreach my $l (@lines) {
        chomp $l;
        $l =~ /^(.*)?=(.*)$/ or die "Could not parse $file\n";
        $ENV{$1} = $2;
    }
}

sub switch_marker {
    die('usage: switch_marker([module|site|conf], [enable|disable], $name)')
        if @_ != 3;
    my $which = shift;
    my $what  = shift;
    my $name  = shift;

    my $mode = "admin";
    $mode = "maint" if $maintmode;

    #print("switch_marker $which $what $name\n");
    # TODO: get rid of the magic string(s)
    my $state_marker_dir = "$statedir/$what" . "d" . "_by_$mode";
    my $state_marker     = "$state_marker_dir/$name";
    if ( !-d $state_marker_dir ) {
        File::Path::mkpath("$state_marker_dir")
            || error(
            "Failed to create marker directory: '$state_marker_dir'\n");
    }

    # XXX: swap find with perl alternative
    my @markers = qx{find "$statedir" -type f -a -name "$name"};
    chomp(@markers);
    foreach (@markers) {
        unless ( unlink $_ ) {
            error("Failed to remove old marker '$_'!\n") && return 0;
        }
    }
    unless ($purge) {
        qx{touch "$state_marker"};
        if ( $? != 0 ) {
            error("Failed to create marker '$state_marker'!\n") && return 0;
        }
        return 1;
    }
}

# vim: syntax=perl sw=4 sts=4 sr et