probe.rb 12 KB

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