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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
|
#!/usr/bin/perl
use 5.010_000;
use strict;
use warnings;
use utf8;
our $VERSION = '0.01';
=head1 NAME
fripost - Fripost.org handling utility for virtual hosting
=head1 SYNOPSIS
B<fripost> [I<options>] { add-alias | add-domain | add-user | mkpass |
search-alias | search-domain | search-user | user-passwd }
B<fripost> B<--man>
=head1 COMMANDS
=over 4
=item B<fripost> add-alias [B<--force>] [I<goto> [I<from>...]]
Add a new virtual alias.
=item B<fripost> add-domain [B<--force>] [I<domain> [I<username>]]
Add a new virtual domain.
=item B<fripost> add-user [I<username>] [B<--password=>I<password>]
Add a new virtual mailbox.
=item B<fripost> mkpass [I<password>]
Create a random new password, and returns its hash.
=item B<fripost> search-domain [I<domain> [I<username>]]
List matching virtual domains.
=item B<fripost> search-user [I<username>]
List matching virtual users.
=item B<fripost> search-alias [B<-f>|B<--from>] [B<-g>|B<--goto>] [I<address>]
List matching virtual aliases.
=item B<fripost> user-passwd [I<username>] [B<--password=>I<password>]
Change user password.
=back
=head1 DESCRIPTION
Unless one of the B<-h>, B<--help>, or B<--man> option is given, one of
the following commands is required.
=over 4
=item B<fripost> add-alias [B<--force>] [I<goto> [I<from>...]]
C<add-alias> is used to add a new virtual alias to the system, unless
B<--pretend> is set.
If I<goto> or I<from> are not given, the user is prompted for their
value.
If I<goto> is not fully qualified, the domain C<fripost.org> is appended.
An error is raised if I<from> is already an existing username, or if
I<from> is an existing alias and B<--force> is not set.
Inserted aliases conform to Postfix's B<virtual>(5) alias table format;
I<from> has to be of one of the following forms:
=over 4
=item .
I<user>@I<domain>, to redirect emails for I<user>@I<domain> to I<goto>, or
=item .
@I<domain>, to catch all emails for users in I<domain> and redirect them
to I<goto>.
This form has the lowest precedence: If there is an alias from
I<user>@I<domain> to I<goto2>, emails to I<user>@I<domain> will be
redirected to I<goto2> only.
See B<virtual>(5) for details and warnings.
=back
If serveral entries are matching, for instance if there are an alias from
I<user>@I<domain> to I<goto> and another for I<user>@I<domain> to
I<goto2>, emails to I<user>@I<domain> will be redirected to BOTH I<goto>
and I<goto2>. Note that C<add-alias> forbids the creation of such
multi-recipient aliases, unless B<--force> is set.
=item B<fripost> add-domain [B<--force>] [I<domain> [I<username>]]
C<add-domain> is used add a new virtual domain to the system, unless
B<--pretend> is set.
If I<domain> is not given, the user is prompted for its value.
By default, C<add-domain> prompts for the owner(s) of the new
domain; Use the empty string I<''> in the command line to create a
"global" domain, only managed by Fripost's administrators.
An error is raised if I<domain> is an existing virtual domain unless
B<--force> is set, in which case
I<username> is simply added to the list of managers.
=item B<fripost> add-user [I<username>] [B<--password=>I<password>]
C<add-user> is used to add a new virtual mailbox to the system, unless
B<--pretend> is set.
If I<username> or I<password> are not given, the user is prompted for
their value.
If I<username> is not fully qualified, the domain C<fripost.org> is
appended.
An error is raised if I<username> is already an existing virtual user or
alias.
If I<password> is given, is it used RAW (not hashed).
This can be useful if the user does not want to give the clear copy but
only a hash, for example.
Using this option disables the sending of credentials.
=item B<fripost> mkpass [I<password>]
C<mkpass> is used to generate a salted SHA-1 hash of the given
I<password>. If no argument is given, the password is randomly
generated, respecting Fripost's password policy.
=item B<fripost> search-alias [B<-f>|B<--from>] [B<-g>|B<--goto>] [I<address>]
C<search-alias> is used to list virtual aliases whose value or target
matches exactly I<address>.
As of the current version, wilcards are not allowed in I<address>; This
will be fixed soon.
To list matching aliases (resp., targets) only, use the flag B<-f>
(resp., B<-g>).
If no I<address> is given, C<search-alias> lists all existing virtual
aliases.
=item B<fripost> search-domain [I<domain> [I<username>]]
C<search-domain> is used to list virtual domains matching exactly I<domain>,
and whose owner is I<username>.
Wildcards I<*> can appear in I<domain>, to match zero or more characters.
If no I<username> is given, list all domains matching I<domain>, regardless
of the owner; If I<owner> is the empty string I<''>, list only the non
self-managed domains.
If neither I<domain> nor I<owner> are given, C<search-domain> lists
all existing virtual domains.
=item B<fripost> search-user [I<username>]
C<search-user> is used to
list virtual mailboxes whose username matches exactly I<username>.
Wildcards I<*> can appear in I<username>, to match zero or more characters.
If no I<username> is given, lists all existing mailboxes.
If I<username> has no domain part, C<search-user> lists matching users for any
domains.
Otherwise, C<search-user> looks up the matching user parts for each matching
domain.
Because of these multiple searches, the use of wildcards on the domain
part of I<username> may be inefficient.
=item B<fripost> user-passwd [I<username>] [B<--password=>I<password>]
C<user-passwd> is used to change the password of I<username>, unless
B<--pretend> is set.
If I<username> or I<password> are not given, the user is prompted for
their value.
If I<username> is not fully qualified, the domain C<fripost.org> is appended.
An error is raised if I<username> is not an existing virtual user.
If I<password> is given, is it used RAW (not hashed).
This can be useful if the user does not want to give the clear copy but
only a hash, for example.
Using this option disables the sending of credentials.
=back
=head1 OPTIONS
=over 8
=item B<--base_dn=>I<basedn>
The root DN for every communication to the LDAP server.
Overrides the value read from the configuration file (see
B<CONFIGURATION>) if present.
=item B<--bind_dn=>I<binddn>
The Distinguished Name (DN) to bind to the LDAP directory.
Overrides the value read from the configuration file (see
B<CONFIGURATION>) if present.
If not set (the default), B<fripost> binds anonymously.
=item B<--bind_pw=>I<password>
The password to to bind with.
Overrides the value read from the configuration file (see
B<CONFIGURATION>) if present.
=item B<-d>, B<--debug>
Debug mode.
=item B<--encrypt=>{I<never>|I<may>|I<secure>}
Tells whether non-empty emails should be encrypted.
No email will be encrypted if I<never> is chosen (the default).
I<may> turns on opportunistic encryption that is, emails will be
encrypted as soon as the recipient is a usable user ID in the public
keyring.
I<secure> will disallow the sending of all non-empty clear emails.
Overrides the value read from the configuration file (see
B<CONFIGURATION>) if present.
=item B<--encrypt-to=>I<name>
If one of the I<may> or I<secure> encryption level is chosen, encrypt
for the user ID I<name>.
=item B<--pretend>
Dry-run all operations that is, do not modify the virtual lookup tables.
But still queries the LDAP server to ensure that the modification would
be safe. (For instance, ensure that a new user is not already existing.)
=item B<--server_host=>I<host>
The LDAP URI to connect to.
Overrides the value read from the configuration file (see
B<CONFIGURATION>) if present.
=item B<--sign>[B<=>I<name>]
Use I<name> as the key to sign all non-empty emails. If I<name> is empty
or not given, use the first key found in the secret keyring, see
B<gpg>(1). A running gpg-agent is required if the private key is
protected by a passphrase.
Overrides the value read from the configuration file (see
B<CONFIGURATION>) if present.
=item B<-v>, B<--verbose>
Verbose mode.
=back
=head1 CONFIGURATION
The configuration is read from the file C<$HOME/.fripost.yml>, and has a
lower precedence than the I<OPTIONS> above.
Valid keys include:
=over 4
=item I<admin_email>
The I<From:> e-mail address to use. Defaults to C<admin@fripost.org>.
=item I<base_dn>
The root DN for every communication to the LDAP server.
=item I<bind_dn>
The Distinguished Name (DN) to bind to the LDAP directory.
(If not set, B<fripost> binds anonymously.)
=item I<bind_pw>
The password to to bind with.
=item I<encrypt>
Tells whether non-empty emails should be encrypted.
No email will be encrypted if I<never> is chosen (the default).
I<may> turns on opportunistic encryption that is, emails will be
encrypted as soon as the recipient is a usable user ID in the public
keyring.
I<secure> will disallow the sending of all non-empty clear emails (not
recommended).
=item I<server_host>
The LDAP URI to connect to. Defaults to C<ldap://127.0.0.1:389>.
=item I<sign>
The key used to sign all non-empty emails. If no key is given,
use the first one found in the secret keyring, see B<gpg>(1).
A running gpg-agent is required if the private key is
protected by a passphrase.
=back
=cut
use FindBin qw($Bin);
use lib "$Bin/lib";
use Env qw /HOME/;
use File::Spec::Functions;
use Getopt::Long qw /:config noauto_abbrev no_ignore_case
gnu_compat bundling permute nogetopt_compat
auto_version/;
use Pod::Usage;
use YAML::Syck;
## Get global command line options
our $conf = LoadFile( catfile ($HOME, '.fripost.yml') );
GetOptions(
'server_host=s' => \$conf->{server_host},
'base_dn=s' => \$conf->{base_dn},
'bind_dn=s' => \$conf->{bind_dn},
'bind_pw=s' => \$conf->{bind_pw},
'pretend' => \$conf->{pretend},
'sign:s' => \$conf->{sign},
'encrypt=s' => \$conf->{encrypt},
'encrypt-to=s' => \$conf->{encrypt_to},
'd|debug' => \$conf->{debug},
'v|verbose' => \$conf->{verbose},
'h|help' => sub { pod2usage(-exitstatus => 0,
-sections => [ qw/SYNOPSIS COMMANDS/ ],
-verbose => 99) },
'man' => sub { pod2usage(-exitstatus => 0,
-verbose => 2) },
'password=s' => \$conf->{password},
'force' => \$conf->{force},
'f|from' => \$conf->{from},
'g|goto' => \$conf->{goto},
) or pod2usage(2);
require Fripost::Schema;
require Fripost::Commands::mkpass;
require Fripost::Commands::add_user;
require Fripost::Commands::search_user;
require Fripost::Commands::user_passwd;
require Fripost::Commands::add_domain;
require Fripost::Commands::search_domain;
require Fripost::Commands::add_alias;
require Fripost::Commands::search_alias;
## Set the default values
$conf->{server_host} //= 'ldap://127.0.0.1:389';
$conf->{bind_dn} //= '';
$conf->{base_dn} //= '';
$conf->{admin_email} //= 'admin@fripost.org';
$conf->{encrypt} //= 'never';
die "Illegal encrypt level: `$conf->{encrypt}'.\n"
unless grep {$_ eq $conf->{encrypt}} qw /never may secure/;
## Get the command
my $cmd = shift;
my $main;
$cmd //= '';
if ($cmd eq 'mkpass') {
&Fripost::Commands::mkpass::main (@ARGV);
exit 0;
}
elsif ($cmd eq 'add-user') {
$main = "Fripost::Commands::add_user::main";
}
elsif ($cmd eq 'search-user') {
$main = "Fripost::Commands::search_user::main";
}
elsif ($cmd eq 'user-passwd') {
$main = "Fripost::Commands::user_passwd::main";
}
elsif ($cmd eq 'add-domain') {
$main = "Fripost::Commands::add_domain::main";
}
elsif ($cmd eq 'search-domain') {
$main = "Fripost::Commands::search_domain::main";
}
elsif ($cmd eq 'add-alias') {
$main = "Fripost::Commands::add_alias::main";
}
elsif ($cmd eq 'search-alias') {
$main = "Fripost::Commands::search_alias::main";
}
else {
pod2usage( -exitstatus => 1,
-verbose => 0,
-msg => length $cmd > 0 ? "Unknown command: `$cmd'." : '' );
}
## Connect to the LDAP server
my $ldap = Fripost::Schema->new( $conf );
{
no strict "refs";
&$main ($ldap, $conf, @ARGV);
}
$ldap->unbind();
=head1 AUTHOR
Stefan Kangas C<< <skangas at skangas.se> >>
Guilhem Moulin C<< <guilhem at fripost.org> >>
=head1 COPYRIGHT
Copyright 2010-2012 Stefan Kangas.
Copyright 2012 Guilhem Moulin.
=head1 LICENSE
This program is free software; you can redistribute it and/or modify it
under the same terms as perl itself.
=cut
|