3 usage: ../fyvzl [<options>] <database-file> <action> <arg>...
5 -l<minrandlength> (for create/choose, minimum randlength)
9 -C (show comments in output)
12 our $usage2 = <<'END';
14 create [<genopts>] [<redirect>] [#<comment>] (default redirect is username)
15 choose [<genopts>] [<redirect>] [#<comment>] (interactively allocate)
16 update <addr> [<redirect>] [#<comment>]
20 empty string for redirect means reject
22 our $usage3 = <<'END';
25 insert-exact <addr> <user> <redirect> <comment>
27 enable-user|disable-user <user>
28 default generation method is alphanum
30 our %usage_genopts = (
32 -l<randlength> (number of letters+digits)
42 our $minrandlength = 6;
44 our $maxrandlength = 50;
46 our $maxperuser = 10000;
53 our $genmethod = 'alphanum';
56 die "too few arguments\n" unless @ARGV;
58 die "option too late on command line\n" if $v =~ m/^-/;
62 sub addr2localpart ($) {
64 return $addr if $addr !~ m/\@/;
65 die "address not in correct domain (\@$dom)\n" unless $' eq $dom; #';
70 return addr2localpart nextarg;
74 die "too many arguments\n" if @ARGV;
80 $dis_q ||= $dbh->prepare("SELECT * FROM disabled_users WHERE user=?");
82 my $row = $dis_q->fetchrow_arrayref();
89 if (!defined $last_u or $last_u ne $u) {
90 print "# user $u ".(isdisabled($u) ? 'disabled' : 'enabled')."\n";
97 my $u = $row->{'user'};
99 my $pa = $row->{'localpart'};
100 $pa .= '@'.$dom if defined $dom;
101 if (length $row->{'redirect'}) {
102 print "$pa: $row->{'redirect'}" or die $!;
104 print "# reject $pa" or die $!;
106 if ($showcomment || !$priv) {
107 print " #$row->{'comment'}" or die $!;
109 print "\n" or die $!;
116 read(R, $ch, 1) == 1 or die $!;
119 next unless $o-$v+$lim < 256;
120 # print STDERR "goodrand($lim)=$v\n";
127 if (defined $$ref && $$ref =~ m/[^\041-\177]/) {
128 die "bad characters in redirection target\n";
130 if (defined $$ref && length $$ref && $$ref !~ m/\@/) {
131 die "unqualified redirection target\n" unless defined $qualdom;
132 $$ref .= '@'.$qualdom;
139 "INSERT INTO addrs (".
140 (join ",", sort keys %$row).
142 (join ",", map { "?" } sort keys %$row).
144 $dbh->do($stmt, {}, map { $row->{$_} } sort keys %$row);
152 my $f = (s/^\#// ? 'comment' : 'redirect');
153 die "$f supplied twice\n" if exists $row->{$f};
156 foreach my $f (keys %$defrow) {
157 next if defined $row->{$f};
158 $row->{$f} = $defrow->{$f};
160 qualify $row->{'redirect'};
164 sub local_part_inuse ($) {
166 our $checkexist_q ||=
167 $dbh->prepare("SELECT localpart FROM addrs WHERE localpart=?");
168 $checkexist_q->execute($s);
169 my $row = $checkexist_q->fetchrow_arrayref();
173 sub gen_local_part_alphanum {
174 my $s = chr(ord('a')+goodrand(26));
175 while (length $s < $randlength) {
176 my $v = goodrand(36);
184 sub generate_local_part () {
187 { no strict qw(refs); $s = &{"gen_local_part_$genmethod"}; }
188 # print STDERR "$s\n";
189 last if !local_part_inuse($s);
194 sub prepare_create () {
195 my $countq = $dbh->prepare("SELECT count(*) FROM addrs WHERE user=?");
196 $countq->execute($user);
197 my ($count) = $countq->fetchrow_array();
198 die unless defined $count;
199 die "too many aliases for this user\n" if $count >= $maxperuser;
200 open R, "/dev/urandom" or die $!;
204 sub genopt_alphanum {
208 die "length out of range $minrandlength..$maxrandlength\n"
209 unless ($minrandlength<=$randlength &&
210 $randlength<=$maxrandlength);
212 die "unknown alphanumeric generation option\n";
216 sub gendefaults_alphanum {
217 $randlength ||= $minrandlength;
221 while (@ARGV && $ARGV[0] =~ m/^-/) {
222 my $arg = shift @ARGV;
223 last if $arg =~ m/^--?$/;
224 { no strict qw(refs); &{"genopt_$genmethod"}($arg); }
226 { no strict qw(refs); &{"gendefaults_$genmethod"}(); }
231 my $newrow = rhsargs({'redirect'=>$user, 'comment'=>''});
233 $newrow->{'user'} = $user;
234 $newrow->{'localpart'} = generate_local_part();
242 my $template = rhsargs({'redirect'=>$user, 'comment'=>''});
243 $template->{'user'} = $user;
247 while (keys %s < 10) {
248 my $s = generate_local_part();
251 print "\@$dom" or die $! if $dom;
252 print "\n" or die $!;
255 print "# ready - enter addrs or local-parts to create,".
256 " then \`.' on a line by itself\n"
264 $s = addr2localpart $_;
265 $s{$s} or die "not an (as-yet-unused) suggestion\n";
267 die "just taken in the meantime (bad luck!)\n"
268 if local_part_inuse $s;
271 my $newrow = { %$template, 'localpart' => $s };
276 print "! error: $@" or die $!;
282 my ($localpart) = @_;
283 our $row_q ||= $dbh->prepare("SELECT * FROM addrs WHERE localpart=?");
284 $row_q->execute($localpart);
285 return $row_q->fetchrow_hashref();
289 my ($localpart) = @_;
290 my $q = $dbh->prepare("SELECT * FROM addrs WHERE localpart=?");
291 my $row = selectrow $localpart;
292 die "unknown localpart\n" unless defined $row;
293 die "not owned by you\n" unless $priv || $row->{user} eq $user;
298 my $localpart = nextarg_addr;
299 my $updrow = rhsargs({});
301 begin_row($localpart);
302 foreach my $f (qw(redirect comment)) {
303 my $v = $updrow->{$f};
304 next unless defined $v;
305 $dbh->do("UPDATE addrs SET $f=? WHERE localpart=?",
308 my $row = selectrow $localpart;
314 my $localpart = nextarg_addr;
316 my $row = begin_row($localpart);
322 while (my $row = $q->fetchrow_hashref()) {
329 my $q = $dbh->prepare("SELECT * FROM addrs WHERE user=?".
330 " ORDER BY localpart");
335 sub action_list_user {
342 sub action_list_all {
345 my $q = $dbh->prepare("SELECT * FROM addrs".
346 " ORDER BY user, localpart");
351 sub action_insert_exact {
354 $row->{'localpart'} = nextarg_addr;
355 $row->{'user'} = $user = nextarg;
356 $row->{'redirect'} = nextarg;
357 $row->{'comment'} = nextarg;
365 my $localpart = nextarg_addr;
366 my $newuser = nextarg;
368 begin_row($localpart);
369 $dbh->do('UPDATE addrs SET user=? WHERE localpart=?',
370 {}, $newuser, $localpart);
374 sub action_enable_user {
378 $dbh->do('DELETE FROM disabled_users WHERE user=?',{},$user);
382 sub action_disable_user {
386 $dbh->do('INSERT INTO disabled_users VALUES user (?)',{},$user);
390 sub action_list_actions {
391 print $usage2 or die $!;
395 last unless $ARGV[0] =~ m/^-/;
402 } elsif (s/^-m(\d+)$//) {
404 } elsif (s/^-d(\S+)$//) {
406 } elsif (s/^-q(\S+)$//) {
411 print $usage1.$usage2.$usage3 or die $!;
412 foreach my $meth (qw(alphanum)) {
413 print "genopts for $meth generation method\n" or die $!;
414 print $usage_genopts{$meth} or die $!;
418 die "unknown option \`$_'\n";
423 my $dbfile = nextarg();
425 if (defined $ENV{'USERV_USER'}) {
427 $user = $ENV{'USERV_USER'};
430 $user = ((getpwuid $<)[0]) or die;
433 $usage2 .= defined $dom
434 ? "addr may be a local part, implicitly qualified with \@$dom\n"
435 : "addr must be a local part (only)\n";
436 $usage2 .= "redirect is implicitly qualified with \@$qualdom".
440 $dbh = DBI->connect("dbi:SQLite:dbname=$dbfile","","",
441 { PrintError => 0, AutoCommit => 0, RaiseError => 1 })
444 my $action = nextarg();
446 { no strict qw(refs); &{"action_$action"}(); }