aboutsummaryrefslogtreecommitdiffstats
path: root/mail/qmail/files/mkaliasdir
blob: bb1343ec7b6c5c34e15727a46d588221268746b0 (plain) (blame)
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
#!/usr/bin/env perl
#
#  Script to populate a Qmail ~alias directory with entries from
#  an [SZ]mail{er} or sendmail-like aliases file
#
# $FreeBSD$
#
#  Dan Cross <tenser@spitfire.ecsel.psu.edu>
#

require 'getopts.pl';

$ALIASES = "/etc/aliases";

die "$0 must be run as root!\n" if ($< != 0);

do Getopts('vf:');

$verbose = 0 unless ($verbose = $opt_v);
$aliases = $ALIASES unless ($aliases = $opt_f);

if (($aliasdir = (getpwnam("alias"))[7]) eq "")
{
    die "User alias does not exist, bailing!\n";
}

if (($qmaildir = (getpwnam("qmaild"))[7]) eq "")
{
    die "User qmaild does not exist, bailing!\n";
}

$ME = "$qmaildir/control/me";

die "Error opening $ME: $!\n" unless open(ME, "$ME");
chop($me = <ME>);
close (ME);

die "Error opening $aliases: $!\n" unless open(ALIASES, "$aliases");

while (<ALIASES>)
{
    chop;

    $mode = ">";

    s/#.*$//;

    ($target, $data) = split(/:/, $_, 2);

    $data =~ s/^\s+//;
    $data =~ s/\s+$//;

    next if (/^$/);

    if ($data =~ /^$/)
    {
        while (chop($data = <ALIASES>))
        {
            $data =~ s/^\s+//;
            $data =~ s/\s+$//;

            last if ($data !~ /^$/);
        }
    }

    $f = "$aliasdir/.qmail-$target";

    @data = split(/\,/, $data);
    $datum = $data[0];

    if ($datum =~ /^:include:/i)
    {
        $datum =~ s/^:include://i;
        $verbose && print "Symlinking $f to $datum...\n";
        symlink($datum, $f);
        shift(@data);
        $f = $datum;
        $mode = ">>";
    }

    open(ALIAS, "$mode$f") || die "Error creating $f: $!\n";

    $verbose && print "Creating $f...\n";

    foreach $datum (@data)
    {
        $datum =~ s/"$// if ($datum =~ s/^"// ||
                    $datum =~ s/^\|[ \t]*"/|/);

        if ($datum =~ /^[|\/]/)
        {
            print ALIAS $datum, "\n";
        }
        else
        {
            $datum = "&" . $datum;
            $datum .= "\@$me" unless ($datum =~ /\@/);
            print ALIAS "$datum\n";
        }

        $verbose && print "\tPopulating $f with $datum...\n";
    }

    close(ALIAS);
}

close(ALIASES);

exit 0;