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)
44 our $maxperuser = 10000;
51 our $genmethod = 'alphanum';
54 our $minrandlength = 6;
55 our $maxrandlength = 50;
58 die "too few arguments\n" unless @ARGV;
60 die "option too late on command line\n" if $v =~ m/^-/;
64 sub addr2localpart ($) {
66 return $addr if $addr !~ m/\@/;
67 die "address not in correct domain (\@$dom)\n" unless $' eq $dom; #';
72 return addr2localpart nextarg;
76 die "too many arguments\n" if @ARGV;
82 $dis_q ||= $dbh->prepare("SELECT * FROM disabled_users WHERE user=?");
84 my $row = $dis_q->fetchrow_arrayref();
91 if (!defined $last_u or $last_u ne $u) {
92 print "# user $u ".(isdisabled($u) ? 'disabled' : 'enabled')."\n";
99 my $u = $row->{'user'};
101 my $pa = $row->{'localpart'};
102 $pa .= '@'.$dom if defined $dom;
103 if (length $row->{'redirect'}) {
104 print "$pa: $row->{'redirect'}" or die $!;
106 print "# reject $pa" or die $!;
108 if ($showcomment || !$priv) {
109 print " #$row->{'comment'}" or die $!;
111 print "\n" or die $!;
118 read(R, $ch, 1) == 1 or die $!;
121 next unless $o-$v+$lim < 256;
122 # print STDERR "goodrand($lim)=$v\n";
129 if (defined $$ref && $$ref =~ m/[^\041-\177]/) {
130 die "bad characters in redirection target\n";
132 if (defined $$ref && length $$ref && $$ref !~ m/\@/) {
133 die "unqualified redirection target\n" unless defined $qualdom;
134 $$ref .= '@'.$qualdom;
141 "INSERT INTO addrs (".
142 (join ",", sort keys %$row).
144 (join ",", map { "?" } sort keys %$row).
146 $dbh->do($stmt, {}, map { $row->{$_} } sort keys %$row);
154 my $f = (s/^\#// ? 'comment' : 'redirect');
155 die "$f supplied twice\n" if exists $row->{$f};
158 foreach my $f (keys %$defrow) {
159 next if defined $row->{$f};
160 $row->{$f} = $defrow->{$f};
162 qualify $row->{'redirect'};
166 sub local_part_inuse ($) {
168 our $checkexist_q ||=
169 $dbh->prepare("SELECT localpart FROM addrs WHERE localpart=?");
170 $checkexist_q->execute($s);
171 my $row = $checkexist_q->fetchrow_arrayref();
175 sub gen_local_part_alphanum {
176 my $s = chr(ord('a')+goodrand(26));
177 while (length $s < $randlength) {
178 my $v = goodrand(36);
186 sub generate_local_part () {
189 { no strict qw(refs); $s = &{"gen_local_part_$genmethod"}; }
190 # print STDERR "$s\n";
191 last if !local_part_inuse($s);
196 sub prepare_create () {
197 my $countq = $dbh->prepare("SELECT count(*) FROM addrs WHERE user=?");
198 $countq->execute($user);
199 my ($count) = $countq->fetchrow_array();
200 die unless defined $count;
201 die "too many aliases for this user\n" if $count >= $maxperuser;
202 open R, "/dev/urandom" or die $!;
206 sub genopt_alphanum {
210 die "length out of range $minrandlength..$maxrandlength\n"
211 unless ($minrandlength<=$randlength &&
212 $randlength<=$maxrandlength);
214 die "unknown alphanumeric generation option\n";
218 sub gendefaults_alphanum {
219 $randlength ||= $minrandlength;
223 while (@ARGV && $ARGV[0] =~ m/^-/) {
224 my $arg = shift @ARGV;
225 last if $arg =~ m/^--?$/;
226 { no strict qw(refs); &{"genopt_$genmethod"}($arg); }
228 { no strict qw(refs); &{"gendefaults_$genmethod"}(); }
233 my $newrow = rhsargs({'redirect'=>$user, 'comment'=>''});
235 $newrow->{'user'} = $user;
236 $newrow->{'localpart'} = generate_local_part();
244 my $template = rhsargs({'redirect'=>$user, 'comment'=>''});
245 $template->{'user'} = $user;
249 while (keys %s < 10) {
250 my $s = generate_local_part();
253 print "\@$dom" or die $! if $dom;
254 print "\n" or die $!;
257 print "# ready - enter addrs or local-parts to create,".
258 " then \`.' on a line by itself\n"
266 $s = addr2localpart $_;
267 $s{$s} or die "not an (as-yet-unused) suggestion\n";
269 die "just taken in the meantime (bad luck!)\n"
270 if local_part_inuse $s;
273 my $newrow = { %$template, 'localpart' => $s };
278 print "! error: $@" or die $!;
284 my ($localpart) = @_;
285 our $row_q ||= $dbh->prepare("SELECT * FROM addrs WHERE localpart=?");
286 $row_q->execute($localpart);
287 return $row_q->fetchrow_hashref();
291 my ($localpart) = @_;
292 my $q = $dbh->prepare("SELECT * FROM addrs WHERE localpart=?");
293 my $row = selectrow $localpart;
294 die "unknown localpart\n" unless defined $row;
295 die "not owned by you\n" unless $priv || $row->{user} eq $user;
300 my $localpart = nextarg_addr;
301 my $updrow = rhsargs({});
303 begin_row($localpart);
304 foreach my $f (qw(redirect comment)) {
305 my $v = $updrow->{$f};
306 next unless defined $v;
307 $dbh->do("UPDATE addrs SET $f=? WHERE localpart=?",
310 my $row = selectrow $localpart;
316 my $localpart = nextarg_addr;
318 my $row = begin_row($localpart);
324 while (my $row = $q->fetchrow_hashref()) {
331 my $q = $dbh->prepare("SELECT * FROM addrs WHERE user=?".
332 " ORDER BY localpart");
337 sub action_list_user {
344 sub action_list_all {
347 my $q = $dbh->prepare("SELECT * FROM addrs".
348 " ORDER BY user, localpart");
353 sub action_insert_exact {
356 $row->{'localpart'} = nextarg_addr;
357 $row->{'user'} = $user = nextarg;
358 $row->{'redirect'} = nextarg;
359 $row->{'comment'} = nextarg;
367 my $localpart = nextarg_addr;
368 my $newuser = nextarg;
370 begin_row($localpart);
371 $dbh->do('UPDATE addrs SET user=? WHERE localpart=?',
372 {}, $newuser, $localpart);
376 sub action_enable_user {
380 $dbh->do('DELETE FROM disabled_users WHERE user=?',{},$user);
384 sub action_disable_user {
388 $dbh->do('INSERT INTO disabled_users VALUES user (?)',{},$user);
392 sub action_list_actions {
393 print $usage2 or die $!;
394 print "genopts\n" or die $!;
395 print $usage_genopts{$genmethod} or die $!;
399 last unless $ARGV[0] =~ m/^-/;
406 } elsif (s/^-m(\d+)$//) {
408 } elsif (s/^-d(\S+)$//) {
410 } elsif (s/^-q(\S+)$//) {
415 print $usage1.$usage2.$usage3 or die $!;
416 foreach my $meth (qw(alphanum)) {
417 print "genopts for $meth generation method\n" or die $!;
418 print $usage_genopts{$meth} or die $!;
422 die "unknown option \`$_'\n";
427 my $dbfile = nextarg();
429 if (defined $ENV{'USERV_USER'}) {
431 $user = $ENV{'USERV_USER'};
434 $user = ((getpwuid $<)[0]) or die;
437 $usage2 .= defined $dom
438 ? "addr may be a local part, implicitly qualified with \@$dom\n"
439 : "addr must be a local part (only)\n";
440 $usage2 .= "redirect is implicitly qualified with \@$qualdom".
444 $dbh = DBI->connect("dbi:SQLite:dbname=$dbfile","","",
445 { PrintError => 0, AutoCommit => 0, RaiseError => 1 })
448 my $action = nextarg();
450 { no strict qw(refs); &{"action_$action"}(); }