File: samplepipe.pl

package info (click to toggle)
courier-authlib 0.72.4-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 14,824 kB
  • sloc: ansic: 15,828; sh: 5,047; cpp: 4,205; makefile: 853; perl: 761
file content (91 lines) | stat: -rw-r--r-- 2,368 bytes parent folder | download | duplicates (5)
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
#!/usr/bin/perl -w

# This is a sample authentication module for authpipe. It uses the same
# protocol that pop3d/imapd/webmail use to communicate with authdaemon.
#
# If you want to indicate a temporary failure (e.g. database unavailable)
# then simply exit without sending any response. This will be indicated as
# a tempfail response, and a new child process will be started for the
# next authentication request, therefore ensuring that it is reinitialized.
#
# You can run this from the command line to test it. Example requests:
#
# PRE . imap fred@example.com	-- display data for this account
#
# AUTH 35			-- 35 is length of
# imap				-- "imap\nlogin\nfred@example.com\nfoobar\n"
# login
# fred@example.com
# foobar
#
# PASSWD imap<tab>fred@example.com<tab>foobar<tab>newpass
#
# ENUMERATE

my %authdata = (
  'fred@example.com' => ['foobar', 81, 81, '/home/fred', 'Maildir', 'disablepop3=0', '10000000S'],
  'wilma@example.com' => ['bazbaz', 81, 81, '/home/wilma'],
);

$|=1;

sub sendres($$)
{
  my $uid = shift;
  my $ref = shift;
  # see authdamond.c for full list of possible fields
  print "ADDRESS=$uid\n";
  print "PASSWD2=$ref->[0]\n";
  print "UID=$ref->[1]\n";
  print "GID=$ref->[2]\n";
  print "HOME=$ref->[3]\n";
  print "MAILDIR=$ref->[4]\n" if $ref->[4];
  print "OPTIONS=$ref->[5]\n" if $ref->[5];
  print "QUOTA=$ref->[6]\n" if $ref->[6];
  print ".\n";
}

print STDERR "Sample module starting\n";

while (<>)
{
  if (/^PRE (\S+) (\S+) (.*)$/)
  {
    # $1=. $2=service $3=uid
    if ($authdata{$3})
    {
      sendres($3, $authdata{$3});
      next;
    }
  }
  elsif (/^AUTH (\d+)$/ && read(STDIN,$buf,$1) == $1 &&
         $buf =~ /^(.*)\n(login)\n(.*)\n(.*)$/)
  {
    # $1=service, $2=authtype, $3=user, $4=password
    if ($authdata{$3} && $authdata{$3}->[0] eq $4)
    {
      sendres($3, $authdata{$3});
      next; 
    }
  }
  elsif (/^PASSWD (.*?)\t(.*?)\t(.*?)\t(.*?)$/)
  {
    # $1=service, $2=user, $3=oldpasswd, $4=newpasswd
    if ($authdata{$2} && $authdata{$2}->[0] eq $3)
    {
      # not a useful example unless you have set MAXDAEMONS=1
      $authdata{$2}->[0] = $4;
      print "OK\n";
      next;
    }
  }
  elsif (/^ENUMERATE$/) {
    foreach $k (keys %authdata) {
      $d = $authdata{$k};
      print "$k\t$d->[1]\t$d->[2]\t$d->[3]\t$d->[4]\t$d->[5]\n";
    }
    print ".\n";
    next;
  }
  print "FAIL\n";
}