3 usage: ../fyvzl [<options>] <database-file> <action> <arg>...
8 -C (show comments in output)
10 options for alphanum generation
11 -l<minrandlength> (for create/choose alphanum, minimum randlength)
12 options for wordlist generation
13 -Wf<wordlist> (switches generation method to wordlist)
14 -WF<min-word-list-len> } (for wordlist generation
15 -Wl<min-num-words> } method only)
16 -WL<min-max-mean-word-len> }
17 -Wd<permitted-delimiter-chars> } (first char is default; comma = none)
19 our $usage2 = <<'END';
21 create [<genopts>] [<redirect>] [#<comment>] (default redirect is username)
22 choose [<genopts>] [<redirect>] [#<comment>] (interactively allocate)
23 update <addr> [<redirect>] [#<comment>]
27 empty string for redirect means reject
29 our $usage3 = <<'END';
32 insert-exact <addr> <user> <redirect> <comment>
34 enable-user|disable-user <user>
35 default generation method is alphanum
37 our %usage_genopts = (
39 -l<randlength> (number of letters+digits)
42 -l<num-words> (number of words in output)
43 -d<delim-char> (delimiter character, "," means none)
44 -F<max-dict-size> (pick from up to <dictsize> different words, 0 means all)
45 -m<max-addr-len> (restrict total length of generated addrs, 0 = unlimited)
55 our $maxperuser = 10000;
62 our $genmethod = 'alphanum';
66 our $minrandlength = 6;
67 our $maxrandlength = 100;
74 our $minwordlistlen = 1000;
75 our $minmaxmeanwordlen = 6.2;
77 our $maxnumwords = 10;
78 our $worddelims = '.-_,';
82 our $wordlistlen = 3000;
83 our $maxdomainlen = 40;
86 die "too few arguments\n" unless @ARGV;
88 die "option too late on command line\n" if $v =~ m/^-/;
92 sub addr2localpart ($) {
94 return $addr if $addr !~ m/\@/;
95 die "address not in correct domain (\@$dom)\n" unless $' eq $dom; #';
100 return addr2localpart nextarg;
104 die "too many arguments\n" if @ARGV;
110 $dis_q ||= $dbh->prepare("SELECT * FROM disabled_users WHERE user=?");
112 my $row = $dis_q->fetchrow_arrayref();
119 if (!defined $last_u or $last_u ne $u) {
120 print "# user $u ".(isdisabled($u) ? 'disabled' : 'enabled')."\n";
127 my $u = $row->{'user'};
129 my $pa = $row->{'localpart'};
130 $pa .= '@'.$dom if defined $dom;
131 if (length $row->{'redirect'}) {
132 print "$pa: $row->{'redirect'}" or die $!;
134 print "# reject $pa" or die $!;
136 if ($showcomment || !$priv) {
137 print " #$row->{'comment'}" or die $!;
139 print "\n" or die $!;
146 read(R, $ch, 1) == 1 or die $!;
149 next unless $o-$v+$lim < 256;
150 # print STDERR "goodrand($lim)=$v\n";
157 if (defined $$ref && $$ref =~ m/[^\041-\177]/) {
158 die "bad characters in redirection target\n";
160 if (defined $$ref && length $$ref && $$ref !~ m/\@/) {
161 die "unqualified redirection target\n" unless defined $qualdom;
162 $$ref .= '@'.$qualdom;
169 "INSERT INTO addrs (".
170 (join ",", sort keys %$row).
172 (join ",", map { "?" } sort keys %$row).
174 $dbh->do($stmt, {}, map { $row->{$_} } sort keys %$row);
182 my $f = (s/^\#// ? 'comment' : 'redirect');
183 die "$f supplied twice\n" if exists $row->{$f};
186 foreach my $f (keys %$defrow) {
187 next if defined $row->{$f};
188 $row->{$f} = $defrow->{$f};
190 qualify $row->{'redirect'};
194 sub local_part_inuse ($) {
196 our $checkexist_q ||=
197 $dbh->prepare("SELECT localpart FROM addrs WHERE localpart=?");
198 $checkexist_q->execute($s);
199 my $row = $checkexist_q->fetchrow_arrayref();
203 sub gen_local_part_alphanum {
204 my $s = chr(ord('a')+goodrand(26));
205 while (length $s < $randlength) {
206 my $v = goodrand(36);
214 sub generate_local_part () {
217 { no strict qw(refs); $s = &{"gen_local_part_$genmethod"}; }
218 # print STDERR "$s\n";
219 last if !local_part_inuse($s);
224 sub prepare_create () {
225 my $countq = $dbh->prepare("SELECT count(*) FROM addrs WHERE user=?");
226 $countq->execute($user);
227 my ($count) = $countq->fetchrow_array();
228 die unless defined $count;
229 die "too many aliases for this user\n" if $count >= $maxperuser;
230 open R, "/dev/urandom" or die $!;
234 sub genopt_alphanum {
238 die "length out of range $minrandlength..$maxrandlength\n"
239 unless ($minrandlength<=$randlength &&
240 $randlength<=$maxrandlength);
242 die "unknown alphanumeric generation option\n";
246 sub gendefaults_alphanum {
247 $randlength ||= $minrandlength;
250 sub gen_local_part_wordlist {
251 my @cmd = (qw(random-word), "-f$wordlist","-n$numwords");
252 push @cmd, "-F$wordlistlen" if $wordlistlen < 1e9;
254 open P, "-|", @cmd or die $!;
256 $!=0; $?=0; close P or die "$? $!";
258 $s =~ s/ /$worddelim/g;
259 my $efflen = length $s;
260 $efflen += 1 + length($dom) if defined $dom;
261 return $s if $efflen <= $maxdomainlen;
265 sub genopt_wordlist {
269 die "length out of range $minnumwords..$maxnumwords\n"
270 unless ($minnumwords<=$numwords &&
271 $numwords<=$maxnumwords);
272 } elsif (m/^-d(.)$/) {
274 die "word delimiter must be one of \`$worddelims'\n"
275 unless grep { $worddelim eq $_ } split //, $worddelims;
276 } elsif (m/^-F(\d+)$/) {
277 $wordlistlen = $1 ? 0+$1 : 1e9;
278 die "requested dictionary size too small\n"
279 unless $wordlistlen >= $minwordlistlen;
280 } elsif (m/^-m(\d+)$/) {
281 $maxdomainlen = $1 ? 0+$1 : 1e9;
283 die "unknown wordlist generation option\n";
287 sub gendefaults_wordlist {
288 $numwords ||= $minnumwords;
289 $worddelim = substr($worddelims,0,1) unless defined $worddelim;
290 $worddelim = '' if $worddelim eq ',';
291 my $expectedmindomlen =
292 (defined $dom ? (1 + length $dom) : 0) # @domain.name
293 + $minmaxmeanwordlen * $numwords # some words
294 + (length $worddelim) * ($numwords-1); # delimiters
295 die "assuming lowest reasonable mean word length $minmaxmeanwordlen".
296 " addrs would be $expectedmindomlen long but".
297 " your maximum length specified $maxdomainlen\n"
298 if $expectedmindomlen > $maxdomainlen;
302 while (@ARGV && $ARGV[0] =~ m/^-/) {
303 my $arg = shift @ARGV;
304 last if $arg =~ m/^--?$/;
305 { no strict qw(refs); &{"genopt_$genmethod"}($arg); }
307 { no strict qw(refs); &{"gendefaults_$genmethod"}(); }
312 my $newrow = rhsargs({'redirect'=>$user, 'comment'=>''});
314 $newrow->{'user'} = $user;
315 $newrow->{'localpart'} = generate_local_part();
323 my $template = rhsargs({'redirect'=>$user, 'comment'=>''});
324 $template->{'user'} = $user;
328 while (keys %s < 10) {
329 my $s = generate_local_part();
332 print "\@$dom" or die $! if $dom;
333 print "\n" or die $!;
336 print "# ready - enter addrs or local-parts to create,".
337 " then \`.' on a line by itself\n"
345 $s = addr2localpart $_;
346 $s{$s} or die "not an (as-yet-unused) suggestion\n";
348 die "just taken in the meantime (bad luck!)\n"
349 if local_part_inuse $s;
352 my $newrow = { %$template, 'localpart' => $s };
357 print "! error: $@" or die $!;
363 my ($localpart) = @_;
364 our $row_q ||= $dbh->prepare("SELECT * FROM addrs WHERE localpart=?");
365 $row_q->execute($localpart);
366 return $row_q->fetchrow_hashref();
370 my ($localpart) = @_;
371 my $q = $dbh->prepare("SELECT * FROM addrs WHERE localpart=?");
372 my $row = selectrow $localpart;
373 die "unknown localpart\n" unless defined $row;
374 die "not owned by you\n" unless $priv || $row->{user} eq $user;
379 my $localpart = nextarg_addr;
380 my $updrow = rhsargs({});
382 begin_row($localpart);
383 foreach my $f (qw(redirect comment)) {
384 my $v = $updrow->{$f};
385 next unless defined $v;
386 $dbh->do("UPDATE addrs SET $f=? WHERE localpart=?",
389 my $row = selectrow $localpart;
395 my $localpart = nextarg_addr;
397 my $row = begin_row($localpart);
403 while (my $row = $q->fetchrow_hashref()) {
410 my $q = $dbh->prepare("SELECT * FROM addrs WHERE user=?".
411 " ORDER BY localpart");
416 sub action_list_user {
423 sub action_list_all {
426 my $q = $dbh->prepare("SELECT * FROM addrs".
427 " ORDER BY user, localpart");
432 sub action_insert_exact {
435 $row->{'localpart'} = nextarg_addr;
436 $row->{'user'} = $user = nextarg;
437 $row->{'redirect'} = nextarg;
438 $row->{'comment'} = nextarg;
446 my $localpart = nextarg_addr;
447 my $newuser = nextarg;
449 begin_row($localpart);
450 $dbh->do('UPDATE addrs SET user=? WHERE localpart=?',
451 {}, $newuser, $localpart);
455 sub action_enable_user {
459 $dbh->do('DELETE FROM disabled_users WHERE user=?',{},$user);
463 sub action_disable_user {
467 $dbh->do('INSERT INTO disabled_users VALUES user (?)',{},$user);
471 sub action_list_actions {
472 print $usage2 or die $!;
473 print "genopts\n" or die $!;
474 print $usage_genopts{$genmethod} or die $!;
478 last unless $ARGV[0] =~ m/^-/;
485 } elsif (s/^-m(\d+)$//) {
487 } elsif (s/^-d(\S+)$//) {
489 } elsif (s/^-q(\S+)$//) {
491 } elsif (s/^-Wf(\S+)$//) {
493 $genmethod = 'wordlist';
494 } elsif (s/^-WF(\d+)$//) {
495 $minwordlistlen = $1;
496 } elsif (s/^-Wl(\d+)$//) {
498 } elsif (s/^-WL([0-9.]+)$//) {
499 $minmaxmeanwordlen = $1;
503 print $usage1.$usage2.$usage3 or die $!;
504 foreach my $meth (qw(alphanum wordlist)) {
505 print "genopts for $meth generation method\n" or die $!;
506 print $usage_genopts{$meth} or die $!;
510 die "unknown option \`$_'\n";
515 my $dbfile = nextarg();
517 if (defined $ENV{'USERV_USER'}) {
519 $user = $ENV{'USERV_USER'};
522 $user = ((getpwuid $<)[0]) or die;
525 $usage2 .= defined $dom
526 ? "addr may be a local part, implicitly qualified with \@$dom\n"
527 : "addr must be a local part (only)\n";
528 $usage2 .= "redirect is implicitly qualified with \@$qualdom".
532 $dbh = DBI->connect("dbi:SQLite:dbname=$dbfile","","",
533 { PrintError => 0, AutoCommit => 0, RaiseError => 1 })
536 my $action = nextarg();
538 { no strict qw(refs); &{"action_$action"}(); }