Debian Codegraph @

dkim/dkimsign.py

raw symbols SCIP
1 #!/usr/bin/env python
2
3 # This software is provided 'as-is', without any express or implied
4 # warranty. In no event will the author be held liable for any damages
5 # arising from the use of this software.
6 #
7 # Permission is granted to anyone to use this software for any purpose,
8 # including commercial applications, and to alter it and redistribute it
9 # freely, subject to the following restrictions:
10 #
11 # 1. The origin of this software must not be misrepresented; you must not
12 # claim that you wrote the original software. If you use this software
13 # in a product, an acknowledgment in the product documentation would be
14 # appreciated but is not required.
15 # 2. Altered source versions must be plainly marked as such, and must not be
16 # misrepresented as being the original software.
17 # 3. This notice may not be removed or altered from any source distribution.
18 #
19 # Copyright (c) 2008 Greg Hewgill http://hewgill.com
20 #
21 # This has been modified from the original software.
22 # Copyright (c) 2011 William Grant <[email protected]>
23 # Copyright (c) 2017 Scott Kitterman <[email protected]>
24
25 from __future__ import print_function
26
27 import sys
28 import argparse
29
30 import dkim
31
32
33 def main():
34 # Backward compatibility hack because argparse doesn't support optional
35 # positional arguments
36 arguments=['--'+arg if arg[:8] == 'identity' else arg for arg in sys.argv[1:]]
37 parser = argparse.ArgumentParser(
38 description='Produce DKIM signature for email messages.',
39 epilog="message to be signed follows commands on stdin")
40 parser.add_argument('selector', action="store")
41 parser.add_argument('domain', action="store")
42 parser.add_argument('privatekeyfile', action="store")
43 parser.add_argument('--hcanon', choices=['simple', 'relaxed'],
44 default='relaxed',
45 help='Header canonicalization algorithm: default=relaxed')
46 parser.add_argument('--bcanon', choices=['simple', 'relaxed'],
47 default='simple',
48 help='Body canonicalization algorithm: default=simple')
49 parser.add_argument('--signalg', choices=['rsa-sha256', 'ed25519-sha256', 'rsa-sha1'],
50 default='rsa-sha256',
51 help='Signature algorithm: default=rsa-sha256')
52 parser.add_argument('--identity', help='Optional value for i= tag.')
53 args=parser.parse_args(arguments)
54 include_headers = None
55 length = None
56 logger = None
57
58 if sys.version_info[0] >= 3:
59 args.selector = bytes(args.selector, encoding='UTF-8')
60 args.domain = bytes(args.domain, encoding='UTF-8')
61 if args.identity is not None:
62 args.identity = bytes(args.identity, encoding='UTF-8')
63 args.hcanon = bytes(args.hcanon, encoding='UTF-8')
64 args.bcanon = bytes(args.bcanon, encoding='UTF-8')
65 args.signalg = bytes(args.signalg, encoding='UTF-8')
66 # Make sys.stdin and stdout binary streams.
67 sys.stdin = sys.stdin.detach()
68 sys.stdout = sys.stdout.detach()
69 canonicalize = (args.hcanon, args.bcanon)
70
71 message = sys.stdin.read()
72 try:
73 d = dkim.DKIM(message,logger=logger, signature_algorithm=args.signalg,
74 linesep=dkim.util.get_linesep(message))
75 sig = d.sign(args.selector, args.domain, open(
76 args.privatekeyfile, "rb").read(), identity = args.identity,
77 canonicalize=canonicalize, include_headers=include_headers,
78 length=length)
79 sys.stdout.write(sig)
80 sys.stdout.write(message)
81 except Exception as e:
82 print(e, file=sys.stderr)
83 sys.stdout.write(message)
84
85
86 if __name__ == "__main__":
87 main()