Debian Codegraph @

lualib/rspamadm/configwizard.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 ansicolors = require "ansicolors"
18 local local_conf = rspamd_paths['LOCAL_CONFDIR']
19 local rspamd_util = require "rspamd_util"
20 local rspamd_logger = require "rspamd_logger"
21 local lua_util = require "lua_util"
22 local lua_stat_tools = require "lua_stat"
23 local lua_redis = require "lua_redis"
24 local ucl = require "ucl"
25 local argparse = require "argparse"
26 local fun = require "fun"
27
28 local plugins_stat = require "plugins_stats"
29
30 local rspamd_logo = [[
31 ____ _
32 | _ \ ___ _ __ __ _ _ __ ___ __| |
33 | |_) |/ __|| '_ \ / _` || '_ ` _ \ / _` |
34 | _ < \__ \| |_) || (_| || | | | | || (_| |
35 |_| \_\|___/| .__/ \__,_||_| |_| |_| \__,_|
36 |_|
37 ]]
38
39 local parser = argparse()
40 :name "rspamadm configwizard"
41 :description "Perform guided configuration for Rspamd daemon"
42 :help_description_margin(32)
43 parser:option "-c --config"
44 :description "Path to config file"
45 :argname("<file>")
46 :default(rspamd_paths["CONFDIR"] .. "/" .. "rspamd.conf")
47 parser:argument "checks"
48 :description "Checks to do (or 'list')"
49 :argname("<checks>")
50 :args "*"
51
52 local redis_params
53
54 local function printf(fmt, ...)
55 if fmt then
56 io.write(string.format(fmt, ...))
57 end
58 io.write('\n')
59 end
60
61 local function highlight(str)
62 return ansicolors.white .. str .. ansicolors.reset
63 end
64
65 local function ask_yes_no(greet, default)
66 local def_str
67 if default then
68 greet = greet .. "[Y/n]: "
69 def_str = "yes"
70 else
71 greet = greet .. "[y/N]: "
72 def_str = "no"
73 end
74
75 local reply = rspamd_util.readline(greet)
76
77 if not reply then
78 os.exit(0)
79 end
80 if #reply == 0 then
81 reply = def_str
82 end
83 reply = reply:lower()
84 if reply == 'y' or reply == 'yes' then
85 return true
86 end
87
88 return false
89 end
90
91 local function readline_default(greet, def_value)
92 local reply = rspamd_util.readline(greet)
93 if not reply then
94 os.exit(0)
95 end
96
97 if #reply == 0 then
98 return def_value
99 end
100
101 return reply
102 end
103
104 local function readline_expire()
105 local expire = '100d'
106 repeat
107 expire = readline_default("Expire time for new tokens [" .. expire .. "]: ",
108 expire)
109 expire = lua_util.parse_time_interval(expire)
110
111 if not expire then
112 expire = '100d'
113 elseif expire > 2147483647 then
114 printf("The maximum possible value is 2147483647 (about 68y)")
115 expire = '68y'
116 elseif expire < -1 then
117 printf("The value must be a non-negative integer or -1")
118 expire = -1
119 elseif expire ~= math.floor(expire) then
120 printf("The value must be an integer")
121 expire = math.floor(expire)
122 else
123 return expire
124 end
125 until false
126 end
127
128 local function print_changes(changes)
129 local function print_change(k, c, where)
130 printf('File: %s, changes list:', highlight(local_conf .. '/'
131 .. where .. '/' .. k))
132
133 for ek, ev in pairs(c) do
134 printf("%s => %s", highlight(ek), rspamd_logger.slog("%s", ev))
135 end
136 end
137 for k, v in pairs(changes.l) do
138 print_change(k, v, 'local.d')
139 if changes.o[k] then
140 v = changes.o[k]
141 print_change(k, v, 'override.d')
142 end
143 print()
144 end
145 end
146
147 local function apply_changes(changes)
148 local function dirname(fname)
149 if fname:match(".-/.-") then
150 return string.gsub(fname, "(.*/)(.*)", "%1")
151 else
152 return nil
153 end
154 end
155
156 local function apply_change(k, c, where)
157 local fname = local_conf .. '/' .. where .. '/' .. k
158
159 if not rspamd_util.file_exists(fname) then
160 printf("Create file %s", highlight(fname))
161
162 local dname = dirname(fname)
163
164 if dname then
165 local ret, err = rspamd_util.mkdir(dname, true)
166
167 if not ret then
168 printf("Cannot make directory %s: %s", dname, highlight(err))
169 os.exit(1)
170 end
171 end
172 end
173
174 local f = io.open(fname, "a+")
175
176 if not f then
177 printf("Cannot open file %s, aborting", highlight(fname))
178 os.exit(1)
179 end
180
181 f:write(ucl.to_config(c))
182
183 f:close()
184 end
185 for k, v in pairs(changes.l) do
186 apply_change(k, v, 'local.d')
187 if changes.o[k] then
188 v = changes.o[k]
189 apply_change(k, v, 'override.d')
190 end
191 end
192 end
193
194 local function setup_controller(controller, changes)
195 printf("Setup %s and controller worker:", highlight("WebUI"))
196
197 if not controller.password or controller.password == 'q1' then
198 if ask_yes_no("Controller password is not set, do you want to set one?", true) then
199 local pw_encrypted = rspamadm.pw_encrypt()
200 if pw_encrypted then
201 printf("Set encrypted password to: %s", highlight(pw_encrypted))
202 changes.l['worker-controller.inc'] = {
203 password = pw_encrypted
204 }
205 end
206 end
207 end
208 end
209
210 local function setup_redis(cfg, changes)
211 local function parse_servers(servers)
212 local ls = lua_util.rspamd_str_split(servers, ",")
213
214 return ls
215 end
216
217 printf("%s servers are not set:", highlight("Redis"))
218 printf("The following modules will be enabled if you add Redis servers:")
219
220 for k, _ in pairs(rspamd_plugins_state.disabled_redis) do
221 printf("\t* %s", highlight(k))
222 end
223
224 if ask_yes_no("Do you wish to set Redis servers?", true) then
225 local read_servers = readline_default("Input read only servers separated by `,` [default: localhost]: ",
226 "localhost")
227
228 local rs = parse_servers(read_servers)
229 if rs and #rs > 0 then
230 changes.l['redis.conf'] = {
231 read_servers = table.concat(rs, ",")
232 }
233 end
234 local write_servers = readline_default("Input write only servers separated by `,` [default: "
235 .. read_servers .. "]: ", read_servers)
236
237 if not write_servers or #write_servers == 0 then
238 printf("Use read servers %s as write servers", highlight(table.concat(rs, ",")))
239 write_servers = read_servers
240 end
241
242 redis_params = {
243 read_servers = rs,
244 }
245
246 local ws = parse_servers(write_servers)
247 if ws and #ws > 0 then
248 changes.l['redis.conf']['write_servers'] = table.concat(ws, ",")
249 redis_params['write_servers'] = ws
250 end
251
252 if ask_yes_no('Do you have any username set for your Redis (ACL SETUSER and Redis 6.0+)') then
253 local username = readline_default("Enter Redis username:", nil)
254
255 if username then
256 changes.l['redis.conf'].username = username
257 redis_params.username = username
258 end
259
260 local passwd = readline_default("Enter Redis password:", nil)
261
262 if passwd then
263 changes.l['redis.conf']['password'] = passwd
264 redis_params['password'] = passwd
265 end
266 elseif ask_yes_no('Do you have any password set for your Redis?') then
267 local passwd = readline_default("Enter Redis password:", nil)
268
269 if passwd then
270 changes.l['redis.conf']['password'] = passwd
271 redis_params['password'] = passwd
272 end
273 end
274
275 if ask_yes_no('Do you have any specific database for your Redis?') then
276 local db = readline_default("Enter Redis database:", nil)
277
278 if db then
279 changes.l['redis.conf']['db'] = db
280 redis_params['db'] = db
281 end
282 end
283 end
284 end
285
286 local function setup_dkim_signing(cfg, changes)
287 -- Remove the trailing slash of a pathname, if present.
288 local function remove_trailing_slash(path)
289 if string.sub(path, -1) ~= "/" then
290 return path
291 end
292 return string.sub(path, 1, string.len(path) - 1)
293 end
294
295 printf('How would you like to set up DKIM signing?')
296 printf('1. Use domain from %s for sign', highlight('mime from header'))
297 printf('2. Use domain from %s for sign', highlight('SMTP envelope from'))
298 printf('3. Use domain from %s for sign', highlight('authenticated user'))
299 printf('4. Sign all mail from %s', highlight('specific networks'))
300 printf()
301
302 local sign_type = readline_default('Enter your choice (1, 2, 3, 4) [default: 1]: ', '1')
303 local sign_networks
304 local allow_mismatch
305 local sign_authenticated
306 local use_esld
307 local sign_domain = 'pet luacheck'
308
309 local defined_auth_types = { 'header', 'envelope', 'auth', 'recipient' }
310
311 if sign_type == '4' then
312 repeat
313 sign_networks = readline_default('Enter list of networks to perform dkim signing: ',
314 '')
315 until #sign_networks ~= 0
316
317 sign_networks = fun.totable(fun.map(lua_util.rspamd_str_trim,
318 lua_util.str_split(sign_networks, ',; ')))
319 printf('What domain would you like to use for signing?')
320 printf('* %s to use mime from domain', highlight('header'))
321 printf('* %s to use SMTP from domain', highlight('envelope'))
322 printf('* %s to use domain from SMTP auth', highlight('auth'))
323 printf('* %s to use domain from SMTP recipient', highlight('recipient'))
324 printf('* anything else to use as a %s domain (e.g. `example.com`)', highlight('static'))
325 printf()
326
327 sign_domain = readline_default('Enter your choice [default: header]: ', 'header')
328 else
329 if sign_type == '1' then
330 sign_domain = 'header'
331 elseif sign_type == '2' then
332 sign_domain = 'envelope'
333 else
334 sign_domain = 'auth'
335 end
336 end
337
338 if sign_type ~= '3' then
339 sign_authenticated = ask_yes_no(
340 string.format('Do you want to sign mail from %s? ',
341 highlight('authenticated users')), true)
342 else
343 sign_authenticated = true
344 end
345
346 if fun.any(function(s)
347 return s == sign_domain
348 end, defined_auth_types) then
349 -- Allow mismatch
350 allow_mismatch = ask_yes_no(
351 string.format('Allow data %s, e.g. if mime from domain is not equal to authenticated user domain? ',
352 highlight('mismatch')), true)
353 -- ESLD check
354 use_esld = ask_yes_no(
355 string.format('Do you want to use %s domain (e.g. example.com instead of foo.example.com)? ',
356 highlight('effective')), true)
357 else
358 allow_mismatch = true
359 end
360
361 local domains = {}
362 local has_domains = false
363
364 local dkim_keys_dir = rspamd_paths["DBDIR"] .. "/dkim/"
365
366 local prompt = string.format("Enter output directory for the keys [default: %s]: ",
367 highlight(dkim_keys_dir))
368 dkim_keys_dir = remove_trailing_slash(readline_default(prompt, dkim_keys_dir))
369
370 local ret, err = rspamd_util.mkdir(dkim_keys_dir, true)
371
372 if not ret then
373 printf("Cannot make directory %s: %s", dkim_keys_dir, highlight(err))
374 os.exit(1)
375 end
376
377 local function print_domains()
378 printf("Domains configured:")
379 for k, v in pairs(domains) do
380 printf("Domain: %s, selector: %s, privkey: %s", highlight(k),
381 v.selector, v.privkey)
382 end
383 printf("--")
384 end
385 local function print_public_key(pk)
386 local base64_pk = tostring(rspamd_util.encode_base64(pk))
387 printf('v=DKIM1; k=rsa; p=%s\n', base64_pk)
388 end
389 repeat
390 if has_domains then
391 print_domains()
392 end
393
394 local domain
395 repeat
396 domain = rspamd_util.readline("Enter domain to sign: ")
397 if not domain then
398 os.exit(1)
399 end
400 until #domain ~= 0
401
402 local selector = readline_default("Enter selector [default: dkim]: ", 'dkim')
403 if not selector then
404 selector = 'dkim'
405 end
406
407 local privkey_file = string.format("%s/%s.%s.key", dkim_keys_dir, domain,
408 selector)
409 if not rspamd_util.file_exists(privkey_file) then
410 if ask_yes_no("Do you want to create privkey " .. highlight(privkey_file),
411 true) then
412 local rsa = require "rspamd_rsa"
413 local sk, pk = rsa.keypair(2048)
414 sk:save(privkey_file, 'pem')
415 print("You need to chown private key file to rspamd user!!")
416 print("To make dkim signing working, to place the following record in your DNS zone:")
417 print_public_key(tostring(pk))
418 end
419 end
420
421 domains[domain] = {
422 selector = selector,
423 path = privkey_file,
424 }
425 until not ask_yes_no("Do you wish to add another DKIM domain?")
426
427 changes.l['dkim_signing.conf'] = { domain = domains }
428 local res_tbl = changes.l['dkim_signing.conf']
429
430 if sign_networks then
431 res_tbl.sign_networks = sign_networks
432 res_tbl.use_domain_sign_networks = sign_domain
433 else
434 res_tbl.use_domain = sign_domain
435 end
436
437 if allow_mismatch then
438 res_tbl.allow_hdrfrom_mismatch = true
439 res_tbl.allow_hdrfrom_mismatch_sign_networks = true
440 res_tbl.allow_username_mismatch = true
441 end
442
443 res_tbl.use_esld = use_esld
444 res_tbl.sign_authenticated = sign_authenticated
445 end
446
447 local function get_postconf_param(param)
448 -- Get Postfix configuration parameter using postconf utility
449 local handle = io.popen('postconf -h ' .. param .. ' 2>/dev/null')
450 if not handle then
451 return nil
452 end
453
454 local result = handle:read('*l')
455 handle:close()
456
457 if result and #result > 0 then
458 -- Trim whitespace
459 result = result:gsub('^%s*(.-)%s*$', '%1')
460 return result
461 end
462
463 return nil
464 end
465
466 local function check_postconf_available()
467 -- Check if postconf is available
468 local handle = io.popen('command -v postconf 2>/dev/null')
469 if not handle then
470 return false
471 end
472
473 local result = handle:read('*l')
474 handle:close()
475
476 return result and #result > 0
477 end
478
479 local function setup_postfix(_cfg, changes)
480 printf('Setup %s integration:', highlight('Postfix'))
481 printf()
482
483 -- Check if postconf is available
484 if not check_postconf_available() then
485 printf('Warning: %s utility not found. Postfix may not be installed.', highlight('postconf'))
486 if not ask_yes_no('Continue anyway?', false) then
487 return
488 end
489 end
490
491 -- Get Postfix paths using postconf
492 local config_directory = get_postconf_param('config_directory') or '/etc/postfix'
493 local postfix_main_cf = config_directory .. '/main.cf'
494 local postfix_master_cf = config_directory .. '/master.cf'
495 local system_aliases = get_postconf_param('alias_maps') or '/etc/aliases'
496 local virtual_aliases = get_postconf_param('virtual_alias_maps') or '/etc/postfix/virtual'
497
498 -- Parse alias_maps which may contain "hash:/path" or similar
499 if system_aliases:match('^%w+:') then
500 system_aliases = system_aliases:gsub('^%w+:', '')
501 end
502 -- Handle comma-separated list - take first entry
503 if system_aliases:match(',') then
504 system_aliases = system_aliases:match('^([^,]+)')
505 end
506
507 -- Parse virtual_alias_maps similarly
508 if virtual_aliases:match('^%w+:') then
509 virtual_aliases = virtual_aliases:gsub('^%w+:', '')
510 end
511 if virtual_aliases:match(',') then
512 virtual_aliases = virtual_aliases:match('^([^,]+)')
513 end
514
515 if not rspamd_util.file_exists(postfix_main_cf) then
516 printf('%s not found. Are you sure Postfix is installed?', highlight(postfix_main_cf))
517 if not ask_yes_no('Continue anyway?', false) then
518 return
519 end
520 else
521 printf('Found Postfix configuration: %s', highlight(postfix_main_cf))
522 end
523
524 -- Get mydestination for local domains using postconf
525 local local_domains = {}
526 local mydest = get_postconf_param('mydestination')
527 if mydest then
528 -- Split by comma/space
529 for domain in mydest:gmatch('[^%s,]+') do
530 -- Skip special values like $myhostname, $mydomain, localhost
531 if not domain:match('^%$') and domain ~= 'localhost' and domain ~= 'localhost.localdomain' then
532 table.insert(local_domains, domain)
533 end
534 end
535 end
536
537 if #local_domains > 0 then
538 printf('Found local domains from mydestination:')
539 for _, domain in ipairs(local_domains) do
540 printf(' - %s', highlight(domain))
541 end
542 printf()
543 end
544
545 -- Ask about enabling aliases module
546 if ask_yes_no('Do you want to enable the ' .. highlight('aliases') .. ' module for Postfix integration?', true) then
547 changes.l['aliases.conf'] = {
548 enabled = true,
549 }
550
551 -- Configure system aliases
552 if rspamd_util.file_exists(system_aliases) then
553 if ask_yes_no('Use system aliases from ' .. highlight(system_aliases) .. '?', true) then
554 changes.l['aliases.conf'].system_aliases = system_aliases
555 end
556 end
557
558 -- Configure virtual aliases
559 if rspamd_util.file_exists(virtual_aliases) then
560 if ask_yes_no('Use virtual aliases from ' .. highlight(virtual_aliases) .. '?', true) then
561 changes.l['aliases.conf'].virtual_aliases = virtual_aliases
562 end
563 end
564
565 -- Configure local domains
566 if #local_domains > 0 then
567 if ask_yes_no('Use local domains from mydestination?', true) then
568 changes.l['aliases.conf'].local_domains = local_domains
569 end
570 else
571 printf('No local domains found in mydestination.')
572 if ask_yes_no('Do you want to manually specify local domains?', true) then
573 local domains_input = readline_default('Enter local domains separated by comma: ', '')
574 if #domains_input > 0 then
575 local domains = {}
576 for domain in domains_input:gmatch('[^,]+') do
577 table.insert(domains, lua_util.rspamd_str_trim(domain))
578 end
579 changes.l['aliases.conf'].local_domains = domains
580 end
581 end
582 end
583 end
584
585 -- Milter integration
586 printf()
587 printf('Checking %s configuration...', highlight('milter'))
588
589 -- Check milter configuration using postconf
590 local has_milter = false
591 local smtpd_milters = get_postconf_param('smtpd_milters')
592 local non_smtpd_milters = get_postconf_param('non_smtpd_milters')
593
594 if (smtpd_milters and smtpd_milters ~= '') or (non_smtpd_milters and non_smtpd_milters ~= '') then
595 has_milter = true
596 end
597
598 if has_milter then
599 printf('Milter configuration detected in %s', highlight(postfix_master_cf))
600 else
601 printf('No milter configuration found.')
602 if ask_yes_no('Do you want to see instructions for milter setup?', true) then
603 printf()
604 printf('To integrate Rspamd with Postfix via milter, add the following to %s:', highlight(postfix_main_cf))
605 printf()
606 printf(ansicolors.cyan .. [[
607 # Rspamd milter
608 smtpd_milters = inet:localhost:11332
609 non_smtpd_milters = inet:localhost:11332
610 milter_protocol = 6
611 milter_mail_macros = i {mail_addr} {client_addr} {client_name} {auth_authen}
612 milter_default_action = accept]] .. ansicolors.reset)
613 printf()
614 printf('Then restart Postfix: %s', highlight('systemctl restart postfix'))
615 printf()
616 end
617 end
618
619 -- Additional features
620 printf()
621 if ask_yes_no('Do you want to configure ' .. highlight('rate limiting') .. ' for outbound mail?', false) then
622 if not changes.l['ratelimit.conf'] then
623 changes.l['ratelimit.conf'] = {}
624 end
625 changes.l['ratelimit.conf'].enabled = true
626
627 -- Suggest reasonable defaults for Postfix
628 local bucket_size = readline_default('Messages per bucket [default: 100]: ', '100')
629 local leak_rate = readline_default('Leak rate (messages per second) [default: 0.01]: ', '0.01')
630
631 changes.l['ratelimit.conf'].rates = {
632 -- Per authenticated user
633 user = {
634 bucket = tonumber(bucket_size),
635 leak = tonumber(leak_rate),
636 }
637 }
638
639 printf('Rate limiting configured. Authenticated users will be limited to %s messages with leak rate %s msg/s',
640 highlight(bucket_size), highlight(leak_rate))
641 end
642
643 if ask_yes_no('Do you want to configure ' .. highlight('ARC signing') .. '?', false) then
644 if not changes.l['arc.conf'] then
645 changes.l['arc.conf'] = {}
646 end
647 changes.l['arc.conf'].enabled = true
648
649 printf('ARC signing enabled. Make sure to configure DKIM keys for ARC to work properly.')
650 end
651
652 printf()
653 printf('Postfix setup complete!')
654 end
655
656 local function check_redis_classifier(cls, changes)
657 local symbol_spam, symbol_ham
658 -- Load symbols from statfiles
659 local statfiles = cls.statfile
660 for _, stf in ipairs(statfiles) do
661 local symbol = stf.symbol or 'undefined'
662
663 local spam
664 if stf.spam then
665 spam = stf.spam
666 else
667 if string.match(symbol:upper(), 'SPAM') then
668 spam = true
669 else
670 spam = false
671 end
672 end
673
674 if spam then
675 symbol_spam = symbol
676 else
677 symbol_ham = symbol
678 end
679 end
680
681 if not symbol_spam or not symbol_ham then
682 printf("Classifier has no symbols defined")
683 return
684 end
685
686 local parsed_redis = lua_redis.try_load_redis_servers(cls, nil)
687
688 if not parsed_redis and redis_params then
689 parsed_redis = lua_redis.try_load_redis_servers(redis_params, nil)
690 if not parsed_redis then
691 printf("Cannot parse Redis params")
692 return
693 end
694 end
695
696 local function try_convert(update_config)
697 if ask_yes_no("Do you wish to convert data to the new schema?", true) then
698 local expire = readline_expire()
699 if not lua_stat_tools.convert_bayes_schema(parsed_redis, symbol_spam,
700 symbol_ham, expire) then
701 printf("Conversion failed")
702 else
703 printf("Conversion succeed")
704 if update_config then
705 changes.l['classifier-bayes.conf'] = {
706 new_schema = true,
707 }
708
709 if expire then
710 changes.l['classifier-bayes.conf'].expire = expire
711 end
712 end
713 end
714 end
715 end
716
717 local function get_version(conn)
718 conn:add_cmd("SMEMBERS", { "RS_keys" })
719
720 local ret, members = conn:exec()
721
722 -- Empty db
723 if not ret or #members == 0 then
724 return false, 0
725 end
726
727 -- We still need to check versions
728 local lua_script = [[
729 local ver = 0
730
731 local tst = redis.call('GET', KEYS[1]..'_version')
732 if tst then
733 ver = tonumber(tst) or 0
734 end
735
736 return ver
737 ]]
738 conn:add_cmd('EVAL', { lua_script, '1', 'RS' })
739 local _, ver = conn:exec()
740
741 return true, tonumber(ver)
742 end
743
744 local function check_expire(conn)
745 -- We still need to check versions
746 local lua_script = [[
747 local ttl = 0
748
749 local sc = redis.call('SCAN', 0, 'MATCH', 'RS*_*', 'COUNT', 1)
750 local _,key = sc[1], sc[2]
751
752 if key and key[1] then
753 ttl = redis.call('TTL', key[1])
754 end
755
756 return ttl
757 ]]
758 conn:add_cmd('EVAL', { lua_script, '0' })
759 local _, ttl = conn:exec()
760
761 return tonumber(ttl)
762 end
763
764 local res, conn = lua_redis.redis_connect_sync(parsed_redis, true)
765 if not res then
766 printf("Cannot connect to Redis server")
767 return false
768 end
769
770 if not cls.new_schema then
771 local r, ver = get_version(conn)
772 if not r then
773 return false
774 end
775 if ver ~= 2 then
776 if not ver then
777 printf('Key "RS_version" has not been found in Redis for %s/%s',
778 symbol_ham, symbol_spam)
779 else
780 printf("You are using an old schema version: %s for %s/%s",
781 ver, symbol_ham, symbol_spam)
782 end
783 try_convert(true)
784 else
785 printf("You have configured an old schema for %s/%s but your data has new layout",
786 symbol_ham, symbol_spam)
787
788 if ask_yes_no("Switch config to the new schema?", true) then
789 changes.l['classifier-bayes.conf'] = {
790 new_schema = true,
791 }
792
793 local expire = check_expire(conn)
794 if expire then
795 changes.l['classifier-bayes.conf'].expire = expire
796 end
797 end
798 end
799 else
800 local r, ver = get_version(conn)
801 if not r then
802 return false
803 end
804 if ver ~= 2 then
805 printf("You have configured new schema for %s/%s but your DB has old version: %s",
806 symbol_spam, symbol_ham, ver)
807 try_convert(false)
808 else
809 printf(
810 'You have configured new schema for %s/%s and your DB already has new layout (v. %s).' ..
811 ' DB conversion is not needed.',
812 symbol_spam, symbol_ham, ver)
813 end
814 end
815 end
816
817 local function setup_statistic(cfg, changes)
818 local sqlite_configs = lua_stat_tools.load_sqlite_config(cfg)
819
820 if #sqlite_configs > 0 then
821
822 if not redis_params then
823 printf('You have %d sqlite classifiers, but you have no Redis servers being set',
824 #sqlite_configs)
825 return false
826 end
827
828 local parsed_redis = lua_redis.try_load_redis_servers(redis_params, nil)
829 if parsed_redis then
830 printf('You have %d sqlite classifiers', #sqlite_configs)
831 local expire = readline_expire()
832
833 local reset_previous = ask_yes_no("Reset previous data?")
834 if ask_yes_no('Do you wish to convert them to Redis?', true) then
835
836 for _, cls in ipairs(sqlite_configs) do
837 if rspamd_util.file_exists(cls.db_spam) and rspamd_util.file_exists(cls.db_ham) then
838 if not lua_stat_tools.convert_sqlite_to_redis(parsed_redis, cls.db_spam,
839 cls.db_ham, cls.symbol_spam, cls.symbol_ham, cls.learn_cache, expire,
840 reset_previous) then
841 rspamd_logger.errx('conversion failed')
842
843 return false
844 end
845 else
846 rspamd_logger.messagex('cannot find %s and %s, skip conversion',
847 cls.db_spam, cls.db_ham)
848 end
849
850 rspamd_logger.messagex('Converted classifier to the from sqlite to redis')
851 changes.l['classifier-bayes.conf'] = {
852 backend = 'redis',
853 new_schema = true,
854 }
855
856 if expire then
857 changes.l['classifier-bayes.conf'].expire = expire
858 end
859
860 if cls.learn_cache then
861 changes.l['classifier-bayes.conf'].cache = {
862 backend = 'redis'
863 }
864 end
865 end
866 end
867 end
868 else
869 -- Check sanity for the existing Redis classifiers
870 local classifier = cfg.classifier
871
872 if classifier then
873 if classifier[1] then
874 for _, cls in ipairs(classifier) do
875 if cls.bayes then
876 cls = cls.bayes
877 end
878 if cls.backend and cls.backend == 'redis' then
879 check_redis_classifier(cls, changes)
880 end
881 end
882 else
883 if classifier.bayes then
884
885 classifier = classifier.bayes
886 if classifier[1] then
887 for _, cls in ipairs(classifier) do
888 if cls.backend and cls.backend == 'redis' then
889 check_redis_classifier(cls, changes)
890 end
891 end
892 else
893 if classifier.backend and classifier.backend == 'redis' then
894 check_redis_classifier(classifier, changes)
895 end
896 end
897 end
898 end
899 end
900 end
901 end
902
903 local function find_worker(cfg, wtype)
904 if cfg.worker then
905 for k, s in pairs(cfg.worker) do
906 if type(k) == 'number' and type(s) == 'table' then
907 if s[wtype] then
908 return s[wtype]
909 end
910 end
911 if type(s) == 'table' and s.type and s.type == wtype then
912 return s
913 end
914 if type(k) == 'string' and k == wtype then
915 return s
916 end
917 end
918 end
919
920 return nil
921 end
922
923 return {
924 handler = function(cmd_args)
925 local changes = {
926 l = {}, -- local changes
927 o = {}, -- override changes
928 }
929
930 local interactive_start = true
931 local checks = {}
932 local all_checks = {
933 'controller',
934 'redis',
935 'postfix',
936 'dkim',
937 'statistic',
938 }
939
940 local opts = parser:parse(cmd_args)
941 local args = opts['checks'] or {}
942
943 local _r, err = rspamd_config:load_ucl(opts['config'])
944
945 if not _r then
946 rspamd_logger.errx('cannot parse %s: %s', opts['config'], err)
947 os.exit(1)
948 end
949
950 _r, err = rspamd_config:parse_rcl({ 'logging', 'worker' })
951 if not _r then
952 rspamd_logger.errx('cannot process %s: %s', opts['config'], err)
953 os.exit(1)
954 end
955
956 local cfg = rspamd_config:get_ucl()
957
958 if not rspamd_config:init_modules() then
959 rspamd_logger.errx('cannot init modules when parsing %s', opts['config'])
960 os.exit(1)
961 end
962
963 if #args > 0 then
964 interactive_start = false
965
966 for _, arg in ipairs(args) do
967 if arg == 'all' then
968 checks = all_checks
969 elseif arg == 'list' then
970 printf(highlight(rspamd_logo))
971 printf('Available modules')
972 for _, c in ipairs(all_checks) do
973 printf('- %s', c)
974 end
975 return
976 else
977 table.insert(checks, arg)
978 end
979 end
980 else
981 checks = all_checks
982 end
983
984 local function has_check(check)
985 for _, c in ipairs(checks) do
986 if c == check then
987 return true
988 end
989 end
990
991 return false
992 end
993
994 rspamd_util.umask('022')
995 if interactive_start then
996 printf(highlight(rspamd_logo))
997 printf("Welcome to the configuration tool")
998 printf("We use %s configuration file, writing results to %s",
999 highlight(opts['config']), highlight(local_conf))
1000 plugins_stat(nil, nil)
1001 end
1002
1003 if not interactive_start or
1004 ask_yes_no("Do you wish to continue?", true) then
1005
1006 if has_check('controller') then
1007 local controller = find_worker(cfg, 'controller')
1008 if controller then
1009 setup_controller(controller, changes)
1010 end
1011 end
1012
1013 if has_check('redis') then
1014 if not cfg.redis or (not cfg.redis.servers and not cfg.redis.read_servers) then
1015 setup_redis(cfg, changes)
1016 else
1017 redis_params = cfg.redis
1018 end
1019 else
1020 redis_params = cfg.redis
1021 end
1022
1023 if has_check('postfix') then
1024 setup_postfix(cfg, changes)
1025 end
1026
1027 if has_check('dkim') then
1028 if cfg.dkim_signing and not cfg.dkim_signing.domain then
1029 if ask_yes_no('Do you want to setup dkim signing feature?') then
1030 setup_dkim_signing(cfg, changes)
1031 end
1032 end
1033 end
1034
1035 if has_check('statistic') or has_check('statistics') then
1036 setup_statistic(cfg, changes)
1037 end
1038
1039 local nchanges = 0
1040 for _, _ in pairs(changes.l) do
1041 nchanges = nchanges + 1
1042 end
1043 for _, _ in pairs(changes.o) do
1044 nchanges = nchanges + 1
1045 end
1046
1047 if nchanges > 0 then
1048 print_changes(changes)
1049 if ask_yes_no("Apply changes?", true) then
1050 apply_changes(changes)
1051 printf("%d changes applied, the wizard is finished now", nchanges)
1052 printf("*** Please reload the Rspamd configuration ***")
1053 else
1054 printf("No changes applied, the wizard is finished now")
1055 end
1056 else
1057 printf("No changes found, the wizard is finished now")
1058 end
1059 end
1060 end,
1061 name = 'configwizard',
1062 description = parser._description,
1063 }