aboutsummaryrefslogtreecommitdiff
path: root/README.markdown
blob: 6fe1ae14bb1e26efaa32b60087fdcbc55c9c3553 (plain) (blame)
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
# NAME

Mail::Run::Crypt - Encrypt and mail output from command runs

# VERSION

Version 0.07

# DESCRIPTION

This module runs a system command with [IPC::Run3](https://metacpan.org/pod/IPC::Run3), and collects any
standard output and standard error it emits. If there is any standard output or
standard error content, it is encrypted and optionally signed with GnuPG, and
then each stream's content is mailed separately to a specified recipient
address.

The idea is to allow you to view the output of automated commands while having
the content encrypted as it passes through to your mailserver, and optionally
to have some assurance that the content was actually generated by the server
concerned. **cron(8)** scripts are the ideal use case, but this would also work
with **at(1)**, or anything else that might non-interactively run jobs for which
output is significant.

You will probably want to call this with the [**runcrypt(1)**](https://metacpan.org/pod/runcrypt) program
provided by this distribution, which includes a means to set the properties for
the module via environment variables or command line options.

# SYNOPSIS

    use Mail::Run::Crypt;
    ...
    my $mrc = Mail::Run::Crypt->new(
        mailto => 'you@example.net',
    );
    $mrc->run($command, @args);
    ...
    my $mrc = Mail::Run::Crypt->new(
        sign       => 1,
        keyid      => '0x1234DEAD5678BEEF',
        passphrase => 'able was i ere i saw elba',
        mailto     => 'you@example.net',
    );
    $mrc->run($command, @args);
    

# SUBROUTINES/METHODS

## `new(%opts)`

Constructor method; accepts the following named parameters:

- `mailto`

    The recipient email address for the content. This is always required.

- `encrypt`

    Whether to encrypt the command output. This defaults to 1.

- `sign`

    Whether to sign the command output. This defaults to 0. A `keyid` and
    `passphrase` will be required for signing.

    It is _strongly_ recommended that a dedicated key and passphrase be used for
    signatures if this is needed. You should carefully consider the consequences of
    a compromised key.

- `keyid`

    The GnuPG key ID that should be used to sign messages. This is required for
    signing, and has no effect without `sign`. It can be any means of identifying
    the key acceptable to GnuPG; the key's 8-byte ("long") hexadecimal ID prefixed
    with `0x` is probably the best way.

- `passphrase`

    The passphrase used to decrypt the key. This is required for signing, and has
    no effect without `sign`.

- `name`

    (Optional) The name of the object. When called from the
    [**runcrypt(1)**](https://metacpan.org/pod/runcrypt) program, this will be the string "runcrypt".
    Otherwise, it will default to this package's name.

## `run(@command)`

Run the specified arguments as a command with [IPC::Run3](https://metacpan.org/pod/IPC::Run3), and email
any output or error content to the email recipient, encrypting and/or signing
as configured. Returns 1 if the command succeeded, 0 otherwise. Use
[`bail()`](#bail) to get the actual exit code if needed.

## `bail()`

Return the exit status of the most recently run command, or 127 if no command
has been successfully run.

# DIAGNOSTICS

- `mailto required`

    The required `mailto` property was not passed in the constructor.

- `keyid required for signing`

    Signing was specified, but no `keyid` attribute was passed in the constructor.

- `passphrase required for signing`

    Signing was specified, but no `passphrase` attribute was passed in the constructor.

- `command failed: %s`

    The command could not be run at all, raising the given error string. This is
    typically due to problems finding the executable.

# CONFIGURATION AND ENVIRONMENT

You will need to have a functioning GnuPG public key setup for this to work,
including stored keys or a key retrieval system for your recipients. You will
also need a secret key if you want to sign the messages.

You should _definitely not_ use your personal key for this; generate one
specifically for mail signing and encryption instead.

I wrote a tutorial on GnuPG key setup, including agent configuration, as part
of this series:

[https://sanctum.geek.nz/arabesque/series/gnu-linux-crypto/](https://sanctum.geek.nz/arabesque/series/gnu-linux-crypto/)

# DEPENDENCIES

- Perl v5.8.1 or newer
- [Carp](https://metacpan.org/pod/Carp)
- [English](https://metacpan.org/pod/English)
- [IPC::Run3](https://metacpan.org/pod/IPC::Run3)
- [Mail::GnuPG](https://metacpan.org/pod/Mail::GnuPG)
- [MIME::Entity](https://metacpan.org/pod/MIME::Entity)

# INCOMPATIBILITIES

This module uses [Mail::GnuPG](https://metacpan.org/pod/Mail::GnuPG) and other GnuPG-specific code, so
it won't work with other OpenPGP implementations.

# BUGS AND LIMITATIONS

Definitely. This code is not production-ready. The test suite coverage is still
not great, but should improve in newer versions.

# AUTHOR

Tom Ryder `<tom@sanctum.geek.nz>`

# LICENSE AND COPYRIGHT

Copyright (C) 2017 Tom Ryder

This program is free software; you can redistribute it and/or modify it under
the terms of the Artistic License (2.0). You may obtain a copy of the full
license at:

[http://www.perlfoundation.org/artistic\_license\_2\_0](http://www.perlfoundation.org/artistic_license_2_0)

Any use, modification, and distribution of the Standard or Modified Versions is
governed by this Artistic License. By using, modifying or distributing the
Package, you accept this license. Do not use, modify, or distribute the
Package, if you do not accept this license.

If your Modified Version has been derived from a Modified Version made by
someone other than you, you are nevertheless required to ensure that your
Modified Version complies with the requirements of this license.

This license does not grant you the right to use any trademark, service mark,
tradename, or logo of the Copyright Holder.

This license includes the non-exclusive, worldwide, free-of-charge patent
license to make, have made, use, offer to sell, sell, import and otherwise
transfer the Package with respect to any patent claims licensable by the
Copyright Holder that are necessarily infringed by the Package. If you
institute patent litigation (including a cross-claim or counterclaim) against
any party alleging that the Package constitutes direct or contributory patent
infringement, then this Artistic License to you shall terminate on the date
that such litigation is filed.

Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER AND
CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES. THE IMPLIED
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY YOUR LOCAL LAW.
UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR CONTRIBUTOR WILL BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING IN ANY WAY
OUT OF THE USE OF THE PACKAGE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
DAMAGE.