probe.rb 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class EmailHelper
  3. class Probe
  4. #
  5. # Try to guess email channel configuration from basic user data.
  6. #
  7. def self.full(params)
  8. user, domain = EmailHelper.parse_email(params[:email])
  9. if !user || !domain
  10. return {
  11. result: 'invalid',
  12. messages: {
  13. email: "Invalid email '#{params[:email]}'."
  14. },
  15. }
  16. end
  17. # probe provider based settings
  18. provider_map = EmailHelper.provider(params[:email], params[:password])
  19. domains = [domain]
  20. # get mx records, try to find provider based on mx records
  21. mx_records = EmailHelper.mx_records(domain)
  22. domains.concat(mx_records)
  23. provider_map.each_value do |settings|
  24. domains.each do |domain_to_check|
  25. next if !domain_to_check.match?(%r{#{settings[:domain]}}i)
  26. # add folder to config if needed
  27. if params[:folder].present? && settings[:inbound] && settings[:inbound][:options]
  28. settings[:inbound][:options][:folder] = params[:folder]
  29. end
  30. config_set_verify_ssl(settings[:inbound], params)
  31. config_set_verify_ssl(settings[:outbound], params)
  32. # probe inbound
  33. Rails.logger.debug { "INBOUND PROBE PROVIDER: #{settings[:inbound].inspect}" }
  34. result_inbound = EmailHelper::Probe.inbound(settings[:inbound])
  35. Rails.logger.debug { "INBOUND RESULT PROVIDER: #{result_inbound.inspect}" }
  36. next if result_inbound[:result] != 'ok'
  37. # probe outbound
  38. Rails.logger.debug { "OUTBOUND PROBE PROVIDER: #{settings[:outbound].inspect}" }
  39. result_outbound = EmailHelper::Probe.outbound(settings[:outbound], params[:email])
  40. Rails.logger.debug { "OUTBOUND RESULT PROVIDER: #{result_outbound.inspect}" }
  41. next if result_outbound[:result] != 'ok'
  42. return {
  43. result: 'ok',
  44. content_messages: result_inbound[:content_messages],
  45. archive_possible: result_inbound[:archive_possible],
  46. archive_possible_is_fallback: result_inbound[:archive_possible_is_fallback],
  47. archive_week_range: result_inbound[:archive_week_range],
  48. setting: settings,
  49. }
  50. end
  51. end
  52. # probe guess settings
  53. # probe inbound
  54. inbound_mx = EmailHelper.provider_inbound_mx(user, params[:email], params[:password], mx_records)
  55. inbound_guess = EmailHelper.provider_inbound_guess(user, params[:email], params[:password], domain)
  56. inbound_map = inbound_mx + inbound_guess
  57. result = {
  58. result: 'ok',
  59. setting: {}
  60. }
  61. success = false
  62. inbound_map.each do |config|
  63. # add folder to config if needed
  64. if params[:folder].present? && config[:options]
  65. config[:options][:folder] = params[:folder]
  66. end
  67. # Add SSL verification flag to configuration, if needed.
  68. config_set_verify_ssl(config, params)
  69. Rails.logger.debug { "INBOUND PROBE GUESS: #{config.inspect}" }
  70. result_inbound = EmailHelper::Probe.inbound(config)
  71. Rails.logger.debug { "INBOUND RESULT GUESS: #{result_inbound.inspect}" }
  72. next if result_inbound[:result] != 'ok'
  73. success = true
  74. result[:setting][:inbound] = config
  75. result[:content_messages] = result_inbound[:content_messages]
  76. result[:archive_possible] = result_inbound[:archive_possible]
  77. result[:archive_possible_is_fallback] = result_inbound[:archive_possible_is_fallback]
  78. result[:archive_week_range] = result_inbound[:archive_week_range]
  79. break
  80. end
  81. # give up, no possible inbound found
  82. if !success
  83. return {
  84. result: 'failed',
  85. reason: 'inbound failed',
  86. }
  87. end
  88. # probe outbound
  89. outbound_mx = EmailHelper.provider_outbound_mx(user, params[:email], params[:password], mx_records)
  90. outbound_guess = EmailHelper.provider_outbound_guess(user, params[:email], params[:password], domain)
  91. outbound_map = outbound_mx + outbound_guess
  92. success = false
  93. outbound_map.each do |config|
  94. # Add SSL verification flag to configuration, if needed.
  95. config_set_verify_ssl(config, params)
  96. Rails.logger.debug { "OUTBOUND PROBE GUESS: #{config.inspect}" }
  97. result_outbound = EmailHelper::Probe.outbound(config, params[:email])
  98. Rails.logger.debug { "OUTBOUND RESULT GUESS: #{result_outbound.inspect}" }
  99. next if result_outbound[:result] != 'ok'
  100. success = true
  101. result[:setting][:outbound] = config
  102. break
  103. end
  104. # give up, no possible outbound found
  105. if !success
  106. return {
  107. result: 'failed',
  108. reason: 'outbound failed',
  109. }
  110. end
  111. Rails.logger.debug { "PROBE FULL SUCCESS: #{result.inspect}" }
  112. result
  113. end
  114. #
  115. # Validate an inbound email channel configuration.
  116. #
  117. def self.inbound(params)
  118. adapter = params[:adapter].downcase
  119. # validate adapter
  120. if !EmailHelper.available_driver[:inbound][adapter.to_sym]
  121. return {
  122. result: 'failed',
  123. message: "Unknown adapter '#{adapter}'",
  124. }
  125. end
  126. # connection test
  127. result_inbound = {}
  128. begin
  129. driver_class = "Channel::Driver::#{adapter.to_classname}".constantize
  130. driver_instance = driver_class.new
  131. result_inbound = driver_instance.fetch(params[:options], nil, 'check')
  132. rescue => e
  133. Rails.logger.debug { e }
  134. return {
  135. result: 'invalid',
  136. settings: params,
  137. message: e.message,
  138. message_human: translation(e.message),
  139. invalid_field: invalid_field(e.message),
  140. }
  141. end
  142. result_inbound
  143. end
  144. #
  145. # Validate an outbound email channel configuration.
  146. #
  147. def self.outbound(params, email, subject = nil)
  148. adapter = params[:adapter].downcase
  149. # validate adapter
  150. if !EmailHelper.available_driver[:outbound][adapter.to_sym]
  151. return {
  152. result: 'failed',
  153. message: "Unknown adapter '#{adapter}'",
  154. }
  155. end
  156. # prepare test email
  157. # rubocop:disable Zammad/DetectTranslatableString
  158. mail = if subject
  159. {
  160. from: email,
  161. to: email,
  162. subject: "Zammad Getting started Test Email #{subject}",
  163. body: "This is a test email from Zammad to check if email sending and receiving work correctly.\n\nYou can ignore or delete this email.",
  164. }
  165. else
  166. {
  167. from: email,
  168. to: 'verify-external-smtp-sending@discard.zammad.org',
  169. subject: 'This is a Test Email',
  170. body: "This is a test email from Zammad to verify if Zammad can send emails to an external address.\n\nIf you see this email, you can ignore or delete it.",
  171. }
  172. end
  173. # rubocop:enable Zammad/DetectTranslatableString
  174. if subject.present?
  175. mail['X-Zammad-Test-Message'] = subject
  176. end
  177. mail['X-Zammad-Ignore'] = 'true'
  178. mail['X-Zammad-Fqdn'] = Setting.get('fqdn')
  179. mail['X-Zammad-Verify'] = 'true'
  180. mail['X-Zammad-Verify-Time'] = Time.zone.now.iso8601
  181. mail['X-Loop'] = 'yes'
  182. mail['Precedence'] = 'bulk'
  183. mail['Auto-Submitted'] = 'auto-generated'
  184. mail['X-Auto-Response-Suppress'] = 'All'
  185. # test connection
  186. begin
  187. driver_class = "Channel::Driver::#{adapter.to_classname}".constantize
  188. driver_instance = driver_class.new
  189. driver_instance.deliver(
  190. params[:options],
  191. mail,
  192. )
  193. rescue => e
  194. Rails.logger.debug { e }
  195. # check if sending email was ok, but mailserver rejected
  196. if !subject
  197. white_map = {
  198. 'Recipient address rejected' => true,
  199. 'Sender address rejected: Domain not found' => true,
  200. }
  201. white_map.each_key do |key|
  202. next if !e.message.match?(%r{#{Regexp.escape(key)}}i)
  203. return {
  204. result: 'ok',
  205. settings: params,
  206. notice: e.message,
  207. }
  208. end
  209. end
  210. return {
  211. result: 'invalid',
  212. settings: params,
  213. message: e.message,
  214. message_human: translation(e.message),
  215. invalid_field: invalid_field(e.message),
  216. }
  217. end
  218. {
  219. result: 'ok',
  220. }
  221. end
  222. def self.invalid_field(message_backend)
  223. invalid_fields.each do |key, fields|
  224. return fields if message_backend.match?(%r{#{Regexp.escape(key)}}i)
  225. end
  226. {}
  227. end
  228. def self.invalid_fields
  229. {
  230. 'authentication failed' => { user: true, password: true },
  231. 'Username and Password not accepted' => { user: true, password: true },
  232. 'Incorrect username' => { user: true, password: true },
  233. 'Lookup failed' => { user: true },
  234. 'Invalid credentials' => { user: true, password: true },
  235. 'getaddrinfo: nodename nor servname provided, or not known' => { host: true },
  236. 'getaddrinfo: Name or service not known' => { host: true },
  237. 'No route to host' => { host: true },
  238. 'execution expired' => { host: true },
  239. 'Connection refused' => { host: true },
  240. 'Mailbox doesn\'t exist' => { folder: true },
  241. 'Folder doesn\'t exist' => { folder: true },
  242. 'Unknown Mailbox' => { folder: true },
  243. }
  244. end
  245. def self.translation(message_backend)
  246. translations.each do |key, message_human|
  247. return message_human if message_backend.match?(%r{#{Regexp.escape(key)}}i)
  248. end
  249. nil
  250. end
  251. def self.translations
  252. {
  253. 'authentication failed' => __('Authentication failed.'),
  254. 'Username and Password not accepted' => __('Authentication failed.'),
  255. 'Incorrect username' => __('Authentication failed due to incorrect username.'),
  256. 'Lookup failed' => __('Authentication failed due to incorrect username.'),
  257. 'Invalid credentials' => __('Authentication failed due to incorrect credentials.'),
  258. 'authentication not enabled' => __('Authentication not possible (not offered by the service)'),
  259. 'getaddrinfo: nodename nor servname provided, or not known' => __('The hostname could not be found.'),
  260. 'getaddrinfo: Name or service not known' => __('The hostname could not be found.'),
  261. 'No route to host' => __('There is no route to this host.'),
  262. 'execution expired' => __('This host cannot be reached.'),
  263. 'Connection refused' => __('The connection was refused.'),
  264. }
  265. end
  266. def self.config_set_verify_ssl(config, params)
  267. return if !config[:options]
  268. if params.key?(:ssl_verify)
  269. config[:options][:ssl_verify] = params[:ssl_verify]
  270. elsif config[:options][:ssl] || config[:options][:start_tls]
  271. config[:options][:ssl_verify] = true
  272. else
  273. config[:options][:ssl_verify] ||= false
  274. end
  275. end
  276. end
  277. end