Debian Codegraph @

lualib/rspamadm/cookie.lua

raw
1 --[[
2 Copyright (c) 2022, Vsevolod Stakhov <[email protected]>
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 ]]--
16
17 local argparse = require "argparse"
18
19
20 -- Define command line options
21 local parser = argparse()
22 :name "rspamadm cookie"
23 :description "Produces cookies or message ids"
24 :help_description_margin(30)
25
26 parser:mutex(
27 parser:option "-k --key"
28 :description('Key to load')
29 :argname "<32hex>",
30 parser:flag "-K --new-key"
31 :description('Generates a new key')
32 )
33
34 parser:option "-d --domain"
35 :description('Use specified domain and generate full message id')
36 :argname "<domain>"
37 parser:flag "-D --decrypt"
38 :description('Decrypt cookie instead of encrypting one')
39 parser:flag "-t --timestamp"
40 :description('Show cookie timestamp (valid for decrypting only)')
41 parser:argument "cookie":args "?"
42 :description('Use specified cookie')
43
44 local function gen_cookie(args, key)
45 local cr = require "rspamd_cryptobox"
46
47 if not args.cookie then
48 return
49 end
50
51 local function encrypt()
52 if #args.cookie > 31 then
53 print('cookie too long (>31 characters), cannot encrypt')
54 os.exit(1)
55 end
56
57 local enc_cookie = cr.encrypt_cookie(key, args.cookie)
58 if args.domain then
59 print(string.format('<%s@%s>', enc_cookie, args.domain))
60 else
61 print(enc_cookie)
62 end
63 end
64
65 local function decrypt()
66 local extracted_cookie = args.cookie:match('^%<?([^@]+)@.*$')
67 if not extracted_cookie then
68 -- Assume full message id as a cookie
69 extracted_cookie = args.cookie
70 end
71
72 local dec_cookie, ts = cr.decrypt_cookie(key, extracted_cookie)
73
74 if dec_cookie then
75 if args.timestamp then
76 print(string.format('%s %s', dec_cookie, ts))
77 else
78 print(dec_cookie)
79 end
80 else
81 print('cannot decrypt cookie')
82 os.exit(1)
83 end
84 end
85
86 if args.decrypt then
87 decrypt()
88 else
89 encrypt()
90 end
91 end
92
93 local function handler(args)
94 local res = parser:parse(args)
95
96 if not (res.key or res['new_key']) then
97 parser:error('--key or --new-key must be specified')
98 end
99
100 if res.key then
101 local pattern = { '^' }
102 for i = 1, 32 do
103 pattern[i + 1] = '[a-zA-Z0-9]'
104 end
105 pattern[34] = '$'
106
107 if not res.key:match(table.concat(pattern, '')) then
108 parser:error('invalid key: ' .. res.key)
109 end
110
111 gen_cookie(res, res.key)
112 else
113 local util = require "rspamd_util"
114 local key = util.random_hex(32)
115
116 print(key)
117 gen_cookie(res, res.key)
118 end
119 end
120
121 return {
122 handler = handler,
123 description = parser._description,
124 name = 'cookie'
125 }