chiark / gitweb /
break out addr2localpart
[d.git] / service
1 #!/usr/bin/perl -w
2 our $usage1 = <<'END';
3 usage: ../fyvzl [OPTIONS DATABASE-FILE ACTION ARG
4 options
5   -lLENGTH   (for create)
6   -mMAXPERUSER
7   -dDOM
8   -qQUALDOM
9   -C     (show comments in output)
10   -h     (display help)
11 END
12 our $usage2 = <<'END';
13 actions
14   create [REDIRECT] [#COMMENT]    (default for REDIRECT is your username)
15   update ADDR [REDIRECT] [#COMMENT]
16   show ADDR
17   list
18   list-actions
19 empty string for REDIRECT means reject
20 END
21 our $usage3 = <<'END';
22 privileged actions
23   list-user USER
24   insert-exact ADDR USER REDIRECT COMMENT
25   donate ADDR USER
26   enable-user|disable-user USER
27 END
28 #/
29
30 use strict;
31
32 use DBI;
33 use POSIX;
34
35 our $randlength = 6;
36 our $maxperuser = 10000;
37 our $qualdom;
38 our $dbh;
39 our $dom;
40 our $user;
41 our $priv;
42 our $showcomment;
43
44 sub nextarg () {
45     die "too few arguments\n" unless @ARGV;
46     my $v = shift @ARGV;
47     die "option too late on command line\n" if $v =~ m/^-/;
48     return $v;
49 }
50
51 sub addr2localpart ($) {
52     my ($addr) = @_;
53     return $addr if $addr !~ m/\@/;
54     die "address not in correct domain (\@$dom)\n" unless $' eq $dom; #';
55     return $`; #`;
56 }
57
58 sub nextarg_addr () {
59     return addr2localpart nextarg;
60 }
61
62 sub nomoreargs () {
63     die "too many arguments\n" if @ARGV;
64 }
65
66 sub isdisabled ($) {
67     my ($u) = @_;
68     our $dis_q;
69     $dis_q ||= $dbh->prepare("SELECT * FROM disabled_users WHERE user=?");
70     $dis_q->execute($u);
71     my $row = $dis_q->fetchrow_arrayref();
72     return !!$row;
73 }
74
75 sub prow ($) {
76     my ($row) = @_;
77     my $u = $row->{'user'};
78     our $last_u;
79     if (!defined $last_u or $last_u ne $u) {
80         print "# user $u ".(isdisabled($u) ? 'disabled' : 'enabled')."\n";
81         $last_u = $u;
82     }
83     my $pa = $row->{'localpart'};
84     $pa .= '@'.$dom if defined $dom;
85     if (length $row->{'redirect'}) {
86         print "$pa: $row->{'redirect'}" or die $!;
87     } else {
88         print "# reject $pa" or die $!;
89     }
90     if ($showcomment || !$priv) {
91         print " #$row->{'comment'}" or die $!;
92     }
93     print "\n" or die $!;
94 }
95
96 sub goodrand ($) {
97     my ($lim) = @_;
98     for (;;) {
99         my $ch;
100         read(R, $ch, 1) == 1 or die $!;
101         my $o = ord $ch;
102         my $v = $o % $lim;
103         next unless $o-$v+$lim < 256;
104 #       print STDERR "goodrand($lim)=$v\n";
105         return $v;
106     }
107 }
108
109 sub qualify (\$) {
110     my ($ref) = @_;
111     if (defined $$ref && $$ref =~ m/[^\041-\177]/) {
112         die "bad characters in redirection target\n";
113     }
114     if (defined $$ref && length $$ref && $$ref !~ m/\@/) {
115         die "unqualified redirection target\n" unless defined $qualdom;
116         $$ref .= '@'.$qualdom;
117     }
118 }
119
120 sub insertrow ($) {
121     my ($row) = @_;
122     my $stmt =
123         "INSERT INTO addrs (".
124         (join ",", sort keys %$row).
125         ") VALUES (".
126         (join ",", map { "?" } sort keys %$row).
127         ") ";
128     $dbh->do($stmt, {}, map { $row->{$_} } sort keys %$row);
129 }
130
131 sub rhsargs ($) {
132     my ($defrow) = @_;
133     my $row = { };
134     while (@ARGV) {
135         $_ = shift @ARGV;
136         my $f = (s/^\#// ? 'comment' : 'redirect');
137         die "$f supplied twice\n" if exists $row->{$f};
138         $row->{$f} = $_;
139     }
140     foreach my $f (keys %$defrow) {
141         next if defined $row->{$f};
142         $row->{$f} = $defrow->{$f};
143     }
144     qualify $row->{'redirect'};
145     return $row;
146 }
147
148 sub generate_local_part () {
149     our $checkexist_q ||=
150         $dbh->prepare("SELECT localpart FROM addrs WHERE localpart=?");
151     my $s;
152     for (;;) {
153         $s = chr(ord('a')+goodrand(26));
154         while (length $s < $randlength) {
155             my $v = goodrand(36);
156             $s .= chr($v < 26
157                       ? ord('a')+($v)
158                       : ord('0')+($v-26));
159         }
160 #       print STDERR "$s\n";
161         $checkexist_q->execute($s);
162         my $row = $checkexist_q->fetchrow_arrayref();
163         last if !$row;
164     }
165     return $s;
166 }
167
168 sub prepare_create () {
169     my $countq = $dbh->prepare("SELECT count(*) FROM addrs WHERE user=?");
170     $countq->execute($user);
171     my ($count) = $countq->fetchrow_array();
172     die unless defined $count;
173     die "too many aliases for this user\n" if $count >= $maxperuser;
174     open R, "/dev/urandom" or die $!;
175     binmode R;
176 }
177
178 sub action_create {
179     my $newrow = rhsargs({'redirect'=>$user, 'comment'=>''});
180     prepare_create();
181     $newrow->{'user'} = $user;
182     $newrow->{'localpart'} = generate_local_part();
183     insertrow($newrow);
184     $dbh->commit();
185     prow($newrow);
186 }
187
188 sub selectrow ($) {
189     my ($localpart) = @_;
190     our $row_q ||= $dbh->prepare("SELECT * FROM addrs WHERE localpart=?");
191     $row_q->execute($localpart);
192     return $row_q->fetchrow_hashref();
193 }
194
195 sub begin_row ($) {
196     my ($localpart) = @_;
197     my $q = $dbh->prepare("SELECT * FROM addrs WHERE localpart=?");
198     my $row = selectrow $localpart;
199     die "unknown localpart\n" unless defined $row;
200     die "not owned by you\n" unless $priv || $row->{user} eq $user;
201     return $row;
202 }
203
204 sub action_update {
205     my $localpart = nextarg_addr;
206     my $updrow = rhsargs({});
207     nomoreargs;
208     begin_row($localpart);
209     foreach my $f (qw(redirect comment)) {
210         my $v = $updrow->{$f};
211         next unless defined $v;
212         $dbh->do("UPDATE addrs SET $f=? WHERE localpart=?",
213                  {}, $v, $localpart);
214     }
215     my $row = selectrow $localpart;
216     $dbh->commit;
217     prow($row);
218 }
219
220 sub action_show {
221     my $localpart = nextarg_addr;
222     nomoreargs;
223     my $row = begin_row($localpart);
224     prow($row);
225 }
226
227 sub listq ($) {
228     my ($q) = @_;
229     while (my $row = $q->fetchrow_hashref()) {
230         prow($row);
231     }
232 }
233
234 sub action_list {
235     nomoreargs;
236     my $q = $dbh->prepare("SELECT * FROM addrs WHERE user=?".
237                           " ORDER BY localpart");
238     $q->execute($user);
239     listq($q);
240 }
241
242 sub action_list_user {
243     die unless $priv;
244     $user = nextarg;
245     nomoreargs;
246     action_list;
247 }
248
249 sub action_list_all {
250     die unless $priv;
251     nomoreargs;
252     my $q = $dbh->prepare("SELECT * FROM addrs".
253                           " ORDER BY user, localpart");
254     $q->execute();
255     listq($q)
256 }
257
258 sub action_insert_exact {
259     die unless $priv;
260     my $row = { };
261     $row->{'localpart'} = nextarg_addr;
262     $row->{'user'} = $user = nextarg;
263     $row->{'redirect'} = nextarg;
264     $row->{'comment'} = nextarg;
265     nomoreargs;
266     insertrow($row);
267     $dbh->commit;
268 }
269
270 sub action_donate {
271     die unless $priv;
272     my $localpart = nextarg_addr;
273     my $newuser = nextarg;
274     nomoreargs;
275     begin_row($localpart);
276     $dbh->do('UPDATE addrs SET user=? WHERE localpart=?',
277              {}, $newuser, $localpart);
278     $dbh->commit;
279 }
280
281 sub action_enable_user {
282     die unless $priv;
283     $user = nextarg;
284     nomoreargs;
285     $dbh->do('DELETE FROM disabled_users WHERE user=?',{},$user);
286     $dbh->commit;
287 }
288
289 sub action_disable_user {
290     die unless $priv;
291     $user = nextarg;
292     nomoreargs;
293     $dbh->do('INSERT INTO disabled_users VALUES user (?)',{},$user);
294     $dbh->commit;
295 }
296
297 sub action_list_actions {
298     print $usage2 or die $!;
299 }
300
301 while (@ARGV) {
302     last unless $ARGV[0] =~ m/^-/;
303     $_ = shift @ARGV;
304     last if m/^--?$/;
305     for (;;) {
306         last unless m/^-./;
307         if (s/^-l(\d+)$//) {
308             $randlength = $1;
309         } elsif (s/^-m(\d+)$//) {
310             $maxperuser = $1;
311         } elsif (s/^-d(\S+)$//) {
312             $dom = $1;
313         } elsif (s/^-q(\S+)$//) {
314             $qualdom = $1;
315         } elsif (s/^-C/-/) {
316             $showcomment = 1;
317         } elsif (s/^-h/-/) {
318             print $usage1.$usage2.$usage3 or die $!;
319             exit 0;
320         } else {
321             die "unknown option \`$_'\n";
322         }
323     }
324 }
325
326 my $dbfile = nextarg();
327
328 if (defined $ENV{'USERV_USER'}) {
329     $priv=0;
330     $user = $ENV{'USERV_USER'};
331 } else {
332     $priv=1;
333     $user = ((getpwuid $<)[0]) or die;
334 }
335
336 $usage2 .= defined $dom
337     ? "ADDR may be a local part, implicitly qualified with \@$dom\n"
338     : "ADDR must be a local part (only)\n";
339 $usage2 .= "REDIRECT is implicitly qualified with \@$qualdom if it has no \@\n"
340     if defined $qualdom;
341
342 $dbh = DBI->connect("dbi:SQLite:dbname=$dbfile","","",
343                     { PrintError => 0, AutoCommit => 0, RaiseError => 1 }) 
344     or die "$dbfile $!";
345
346 my $action = nextarg();
347 $action =~ y/-/_/;
348 { no strict qw(refs); &{"action_$action"}(); }