INSTALL.OnlineUI.txt 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  1. #!/bin/cat
  2. # $Id: INSTALL.OnlineUI.txt,v 1.56 2022/05/19 08:55:50 gilles Exp gilles $
  3. This documentation is also located online at
  4. https://imapsync.lamiral.info/INSTALL.d/
  5. https://imapsync.lamiral.info/INSTALL.d/INSTALL.OnlineUI.txt
  6. =======================================================================
  7. Installing imapsync online
  8. =======================================================================
  9. I'm now confident with /X since the /X service is up and running quite
  10. well since January 2017. Anyway, if you run this service on your own,
  11. online, you take responsibility for it.
  12. =======================================================================
  13. Hardware consideration
  14. RAM used per imapsync process, mean value: 230 MB.
  15. Average_bandwidth_rate: 345 KiB/s ~ 2.8 Mbps.
  16. Load mean: 0.8 on a CPU 4 cores "Intel(R) i5-2320 3.00GHz K8-class"
  17. =======================================================================
  18. Installation
  19. You have to be a little familiar with what a CGI script is and how to
  20. activate a CGI script on the Apache HTTP server, or any other HTTP
  21. server. I have received demands to run it on the Ngnix HTTP server but
  22. I haven't played with it yet. Linux is also a preferred platform (I
  23. run /X service on Linux and FreeBSD).
  24. I have tested this visual interface on Mac. It works. For now, it
  25. demands some skills few Mac users have. Drop me a note in case you
  26. want to do that.
  27. I have tested this visual interface on Windows, it fails on Windows
  28. because of some hardcoded Unix paths. I'm working on it to be Windows
  29. ok but it's not done yet (May 2020).
  30. Some users have successfully installed a /X visual interface on
  31. Windows using a Linux VM machine.
  32. The web visual user interface frontend /X is compounded in four files:
  33. a html5 file, a CSS file, a javascript file, and a logo image:
  34. * https://imapsync.lamiral.info/X/imapsync_form_extra.html
  35. * https://imapsync.lamiral.info/X/imapsync_form.css
  36. * https://imapsync.lamiral.info/X/imapsync_form.js
  37. * https://imapsync.lamiral.info/X/logo_imapsync_Xn.png
  38. You can do a "view source" to see the HTML file as it is written, and
  39. a "save" to get it locally. The three other files can be saved the
  40. same way or with a command named "wget". I strongly suggest using
  41. wget, see below the ready-to-use command lines.
  42. Those four files can be put anywhere on a web server, as long as they
  43. stand in the same directory. If you want to put them in different
  44. directories, just change the content of imapsync_form_extra.html to
  45. reflect the change, ie, change the two lines referencing
  46. imapsync_form.css and imapsync_form.js href="imapsync_form.css" (near
  47. the beginning of imapsync_form_extra.html) src="imapsync_form.js"
  48. (near the end of imapsync_form_extra.html) I let you change the image
  49. logo as an exercise, it's safe if you fail.
  50. The actual imap syncing work is done by imapsync acting as a CGI, the
  51. visual interface is only there to give imapsync the parameters needed
  52. for the sync.
  53. Use at least Perl module CGI.pm release 4.08 (2014-10-18) to avoid the
  54. bug "Undefined subroutine CGI::multi_param". You can use the command
  55. named cpanm to upgrade CGI.pm to its last version, it's the easiest
  56. way.
  57. Print the CGI.pm release with:
  58. perl -MCGI -e 'print "$CGI::VERSION\n"'
  59. If it is under release 4.08 (2014-10-18) then upgrade it with
  60. cpanm CGI
  61. It is a good thing to remove the old one if it was installed by a
  62. distribution package, I let you this part as an exercise too. Ok, here
  63. is a way:
  64. apt remove libcgi-pm-perl # for the Debian family
  65. dnf remove perl-CGI # for the Centos family
  66. To check and fix the Perl modules dependencies, run:
  67. cd
  68. wget -N https://imapsync.lamiral.info/prerequisites_imapsync
  69. sh prerequisites_imapsync
  70. To make imapsync work as a CGI script, there are three conditions.
  71. First, imapsync has to work by itself on the web host. If imapsync
  72. doesn't work by itself, as a command line, then it won't work as a CGI
  73. script.
  74. Second, imapsync has to work by itself on the web host using the Unix
  75. user running the webserver.
  76. Third, the file imapsync has to be considered as a cgi script.
  77. Command lines to provide and verify those three conditions will be
  78. provided further in this document, for the Debian family systems and
  79. for the Centos family systems. You are strongly advised to follow this
  80. commands if need and want help from me because I will first ask you to
  81. run them before searching what you did wrong.
  82. The imapsync_form_extra.html file in action calls the CGI location
  83. /cgi-bin/imapsync
  84. which has to be imapsync itself, the file script (not the directory).
  85. The very latest and relatively stable imapsync is
  86. https://imapsync.lamiral.info/imapsync
  87. This file is the program file used verbatim for the service given at
  88. https://imapsync.lamiral.info/X/
  89. Copy the three files imapsync_form.* on a directory that is exported
  90. by your HTTP server.
  91. Copy the imapsync script on the cgi-bin/ directory
  92. allowing CGIs and you'll have your imapsync visual interface
  93. and service. The cgi-bin/ directory is usually outside the
  94. hierarchy exported to anybody by the HTTP server.
  95. The default Apache 2.4 timeout is 60 seconds, one minute, and 300
  96. secondes for older Apache, 5 minutes. See
  97. https://httpd.apache.org/docs/2.4/mod/core.html#timeout
  98. I use "Timeout 3600", 3600 seconds, an hour. I chose this huge timeout
  99. value because imapsync can spend a long time without talking while
  100. getting the headers of huge folders of 100k messages. If you intend
  101. to offer this service for huge mailboxes or for a long time, I
  102. strongly recommand you to set this "Timeout 3600" in the Apache
  103. configuration right now because you will sure end up with this timeout
  104. issue in a few months. You can search for timeouts in the Apache error
  105. log to see if you have timeout issues.
  106. Now that I have explained the general context for any system, I'll
  107. describe concrete examples on several systems, Debian/Ubuntu and
  108. Centos. Feedbacks show that the Centos process is easier in case you
  109. don't know very much any Linux distribution. But I have to add that if
  110. you don't know very much the Linux distribution you use, then you
  111. shouldn't install this imapsync service at all.
  112. =============================================================================
  113. A) Concrete example on a Debian server with Apache:
  114. First, install Apache on your Debian system:
  115. apt install apache2
  116. Imapsync script place on the server disk will be
  117. /usr/lib/cgi-bin/imapsync
  118. This classical /cgi-bin directory is usually already configured
  119. in the Apache configuration file
  120. /etc/apache2/sites-available/default-ssl
  121. or
  122. /etc/apache2/sites-available/default
  123. This configuration file contains the following section
  124. somewhere, maybe in comments for now, ie, with
  125. some # characters at the beginning to make them ignored.
  126. If you don't find the following section, keep reading,
  127. the solution is below.
  128. ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
  129. <Directory "/usr/lib/cgi-bin">
  130. AllowOverride None
  131. # Next line "no-gzip 1" is to avoid output buffering,
  132. # clients can then see the log during the sync
  133. SetEnv no-gzip 1
  134. Options +ExecCGI -MultiViews
  135. # Choose either one or the other, depending on your Apache version
  136. # Lines beginning with # are ignored
  137. # For Apache 2.2
  138. #Order allow, deny
  139. #Allow from all
  140. # Apache 2.4
  141. Require all granted
  142. </Directory>
  143. In recent Debian distributions you can activate this cgi
  144. stuff with the following commands:
  145. a2enmod cgi
  146. a2enconf serve-cgi-bin
  147. /etc/init.d/apache2 restart
  148. If the cgi mode and the cgi-bin configuration are not activated then
  149. you may encounter a 404 error when, later, you will run the command
  150. wget -nv -S -O- http://localhost/cgi-bin/imapsync?testslive=1
  151. That's all for the Apache Debian family configuration side.
  152. Now get, test, and install the latest imapsync:
  153. cd
  154. wget -N https://imapsync.lamiral.info/imapsync
  155. chmod +x imapsync
  156. # some basic tests
  157. ./imapsync
  158. ./imapsync --testslive
  159. cp imapsync /usr/lib/cgi-bin/
  160. Assuming that the Unix account running Apache is www-data, check that
  161. it will work under Apache with this command:
  162. su -s /bin/sh -c 'SERVER_SOFTWARE=foo /usr/lib/cgi-bin/imapsync' www-data
  163. You should end with something like:
  164. Exiting with return value 0 (EX_OK: successful termination)
  165. Test that imapsync is considered a cgi with:
  166. wget -nv -S -O- http://localhost/cgi-bin/imapsync?testslive=1
  167. The last command should print something like:
  168. Status: 200 OK to sync IMAP boxes. Load on bar is ...
  169. ...
  170. Exiting with return value 0 (EX_OK: successful termination)
  171. If you get a 404 or a 5xx here then review the cgi installation and
  172. configuration part.
  173. You can also verify that the webserver is not buffering its output with the
  174. command:
  175. wget -nv -S -O- 'http://localhost/cgi-bin/imapsync?testslive=1&simulong=10'
  176. You should get the output as time goes on. If you don't get the output
  177. as time goes on, ie you see no output then all output at once, it
  178. means the webserver is buffering. Fix it with the "SetEnv no-gzip 1"
  179. described above.
  180. The UI front-end file place on the server disk in this example is
  181. /var/www/html/X/imapsync_form_extra.html
  182. but it can be placed anywhere on the disk, the important thing is that
  183. it has to be served by the webserver.
  184. mkdir /var/www/html/X/
  185. cd /var/www/html/X/
  186. wget -N \
  187. https://imapsync.lamiral.info/X/imapsync_form_extra.html \
  188. https://imapsync.lamiral.info/X/imapsync_form.css \
  189. https://imapsync.lamiral.info/X/imapsync_form.js \
  190. https://imapsync.lamiral.info/X/logo_imapsync_Xn.png
  191. ln -s imapsync_form_extra.html index.html
  192. The imapsync process working directory in cgi mode is
  193. /var/tmp/imapsync_cgi/
  194. it is not configurable unless changing it in imapsync directly, it is
  195. hard-coded in imapsync. In this directory will go the log files and
  196. the pid files.
  197. Check
  198. http://yourhost/X/imapsync_form_extra.html
  199. or the safer
  200. https://yourhost/X/imapsync_form_extra.html
  201. Let's encrypt your site because crendentials should never travel in
  202. clear form. Go to https://certbot.eff.org/instructions
  203. See the Troubleshooting section to fix the systemd Apache
  204. PrivateTmp=true issue.
  205. That's all for installing a /X service on a Debian family system.
  206. =============================================================================
  207. B) Here is a concrete example on a Centos 7 server with the Apache
  208. webserver httpd:
  209. First, follow and apply the section "Centos 7 and latest imapsync" at
  210. https://imapsync.lamiral.info/INSTALL.d/INSTALL.Centos.txt
  211. Then:
  212. yum install httpd
  213. systemctl restart httpd
  214. cpanm CGI
  215. mkdir /var/www/html/X/
  216. cd /var/www/html/X/
  217. wget -N \
  218. https://imapsync.lamiral.info/X/imapsync_form_extra.html \
  219. https://imapsync.lamiral.info/X/imapsync_form.css \
  220. https://imapsync.lamiral.info/X/imapsync_form.js \
  221. https://imapsync.lamiral.info/X/logo_imapsync_Xn.png
  222. ln -s imapsync_form_extra.html index.html
  223. cd
  224. wget -N https://imapsync.lamiral.info/imapsync
  225. chmod +x imapsync
  226. # some basic tests
  227. ./imapsync
  228. ./imapsync --testslive
  229. cp imapsync /var/www/cgi-bin/
  230. Assuming that the Unix account running Apache is "apache",
  231. which is the default Apache user on Centos system,
  232. check that it will work under Apache with this command:
  233. # a real synchronization but not in cgi context
  234. cd /tmp
  235. su -s /bin/sh -c '/var/www/cgi-bin/imapsync --testslive' apache
  236. # in cgi context but just the imapsync command with no parameter
  237. cd
  238. su -s /bin/sh -c 'SERVER_SOFTWARE=foo /var/www/cgi-bin/imapsync' apache
  239. # a real synchronization in cgi context
  240. wget -nv -S -O- http://localhost/cgi-bin/imapsync?testslive=1
  241. The last command should print something like:
  242. Status: 200 OK to sync IMAP boxes. Load on bar is ...
  243. ...
  244. You can also verify that the webserver is not buffering its output
  245. with the command:
  246. wget -nv -S -O- 'http://localhost/cgi-bin/imapsync?testslive=1&simulong=10'
  247. You should get the output as time goes on. If you don't, no output
  248. then all output at once, it means the webserver is buffering. Fix it
  249. with the "SetEnv no-gzip 1" described above.
  250. Now check
  251. http://yourhost/X/imapsync_form_extra.html
  252. or the safer
  253. https://yourhost/X/imapsync_form_extra.html
  254. That's all for installing a /X service on Centos 7.
  255. B bis) How about Centos 8?
  256. Follow the procedure for Centos 7. While imapsync is ok on the command
  257. line, you will encounter some permission denied in the CGI
  258. context. Something like:
  259. wget -nv -S -O- http://localhost/cgi-bin/imapsync?testslive=1
  260. ...
  261. Host1 failure: can not open imap connection on host1
  262. [test1.lamiral.info] with user [test1]: Unable to connect to
  263. test1.lamiral.info: Permission denied
  264. The issue might come from SELinux. I haven't dig into SELinux enough
  265. to give you the commands that will allow imapsync online and only it
  266. while maintaining SELinux in enforcing mode.
  267. Quick solution:
  268. getsebool httpd_can_network_connect # should show --> off
  269. setsebool -P httpd_can_network_connect=1
  270. getsebool httpd_can_network_connect # should show --> on
  271. wget -nv -S -O- http://localhost/cgi-bin/imapsync?testslive=1 # no more "Permission denied"
  272. The -P option installs the rule permanently, even after a reboot
  273. To go back to the previous state:
  274. getsebool httpd_can_network_connect # should show --> on
  275. setsebool -P httpd_can_network_connect=0
  276. getsebool httpd_can_network_connect # should show --> off
  277. Nota bene
  278. =========
  279. You may also want to avoid being placed by systemd in a directory like
  280. (where xxx are crypto hash characters):
  281. /var/tmp/systemd-private-xxx-httpd.service-xxx/tmp/
  282. In that case, see the Troubleshooting section below.
  283. =======================================================================
  284. =================== Bandwidth statistics ==========================
  285. =======================================================================
  286. If you want the bandwidth statistics like the ones at the bottom of
  287. the page and following the image link, more detailed at
  288. https://imapsync.lamiral.info/vnstat/vnstati.html
  289. Those stats are generated by vnstat
  290. https://humdi.net/vnstat/
  291. Vnstat is already available as a package in most Linux distros.
  292. The images are generated by the following commands, every minute:
  293. vnstati -s -o /var/www/html/vnstat/vnstat_s.png
  294. vnstati -h -o /var/www/html/vnstat/vnstat_h.png
  295. vnstati -hg -o /var/www/html/vnstat/vnstat_hg.png
  296. vnstati -hs -o /var/www/html/vnstat/vnstat_hs.png
  297. vnstati -d -o /var/www/html/vnstat/vnstat_d.png
  298. vnstati -m -o /var/www/html/vnstat/vnstat_m.png
  299. vnstati -y -o /var/www/html/vnstat/vnstat_y.png
  300. vnstati -t -o /var/www/html/vnstat/vnstat_t.png
  301. vnstati -vs -o /var/www/html/vnstat/vnstat_vs.png
  302. vnstati -5 -o /var/www/html/vnstat/vnstat_5.png
  303. =======================================================================
  304. ====================== Troubleshooting ============================
  305. =======================================================================
  306. The log says the temporary directory is
  307. /var/tmp/imapsync_cgi/
  308. but this directory is not in the system. What a mystery!
  309. It may be that the apache or httpd service is run by systemd with a
  310. jailed temporary directory.
  311. Solution:
  312. find /etc/systemd/ /usr/lib/systemd/ | xargs grep -s PrivateTmp
  313. If systemd jails Apache, then you'll find a line like:
  314. /etc/systemd/system/multi-user.target.wants/apache2.service:PrivateTmp=true
  315. (Debian/Ubuntu)
  316. or
  317. /usr/lib/systemd/system/httpd.service:PrivateTmp=true
  318. (Centos)
  319. The goal is to override the line
  320. PrivateTmp=true
  321. found in /etc/systemd/system/multi-user.target.wants/apache2.service
  322. or
  323. /usr/lib/systemd/system/httpd.service
  324. with the line:
  325. PrivateTmp=false
  326. The right way to do it is by using the "systemctl edit ..." command
  327. and then reload the systemd daemon and restart the apache2 service.
  328. You can also edit directly the file override.conf if you know where
  329. to do it. If you don't use the override.conf mechanism then your change will
  330. be canceled the next time the apache package is updated.
  331. Debian:
  332. systemctl edit apache2
  333. cat /etc/systemd/system/apache2.service.d/override.conf
  334. [Service]
  335. PrivateTmp=false
  336. systemctl daemon-reload
  337. systemctl restart apache2
  338. systemctl status apache2
  339. Centos:
  340. systemctl edit httpd
  341. cat /etc/systemd/system/httpd.service.d/override.conf
  342. [Service]
  343. PrivateTmp=false
  344. systemctl daemon-reload
  345. systemctl restart httpd
  346. systemctl status httpd
  347. Then retry
  348. wget -nv -S -O- http://localhost/cgi-bin/imapsync?testslive=1
  349. Look now if /var/tmp/imapsync_cgi/ is there.
  350. =======================================================================
  351. If you encounter this issue:
  352. Failed to find a valid digest in the 'integrity' attribute for resource
  353. 'https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js'
  354. with computed SHA-256 integrity 'kZMXypKF3if9/5v2tP9UHBvS/535tSyH7vjszruyCso='.
  355. The resource has been blocked.
  356. It may be because of AdBlock.
  357. Verification:
  358. wget https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js
  359. cat jquery.min.js | openssl dgst -sha384 -binary | openssl base64 -A
  360. gives exactly what is in imapsync_form_extra.html
  361. more imapsync_form_extra.html
  362. ...
  363. src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"
  364. integrity="sha384-xBuQ/xzmlsLoJpyjoggmTEz8OWUFM0/RC5BsqQBDX2v5cMvDHcMakNTNrHIW2I5f"
  365. So if your https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js
  366. is not what it should be, your access looks compromised.
  367. Thanks to Dominik Ulrich for this insight!
  368. =======================================================================
  369. =======================================================================
  370. This part is for hackers only.
  371. If you want to use the UI but make it more complicated things than
  372. just run imapsync then use the following files:
  373. imapsync_shell_wrapper instead of imapsync itself
  374. imapsync_form_wrapper.js instead of imapsync_form.js
  375. imapsync_form_wrapper.html instead of imapsync_form.html
  376. How to get those files:
  377. wget -N https://imapsync.lamiral.info/X/imapsync_shell_wrapper \
  378. https://imapsync.lamiral.info/X/imapsync_form_wrapper.js \
  379. https://imapsync.lamiral.info/X/imapsync_form_wrapper.html
  380. Centos:
  381. chmod +x imapsync_shell_wrapper
  382. cp imapsync_shell_wrapper /var/www/cgi-bin/
  383. Debian:
  384. chmod +x imapsync_shell_wrapper
  385. cp imapsync_shell_wrapper /usr/lib/cgi-bin/
  386. Normally, you only have to change the script imapsync_shell_wrapper to
  387. suit your needs.
  388. Have in mind that the abort button will kill only one imapsync so it
  389. is not a working button in case of successive imapsync runs.
  390. =======================================================================
  391. =======================================================================
  392. ====== mod_perl failure ======
  393. This part is for mod_perl experts only.
  394. The script imapsync doesn't work under Modperl::Registry nor under
  395. ModPerl::PerlRun. So read on if you think you are better than me.
  396. I tried the standard way, telling how any cgi Perl script can be run
  397. under mod_perl perlrun, but it fails with imapsync. Any hint welcome!
  398. # This is a Debian example
  399. # install mod-perl with
  400. apt-get install libapache2-mod-perl2
  401. # edit the file /etc/apache2/mods-available/perl.conf
  402. # with the following lines
  403. more /etc/apache2/mods-available/perl.conf
  404. <IfModule mod_perl.c>
  405. PerlModule ModPerl::PerlRun
  406. Alias /perl-run/ /usr/lib/cgi-bin/
  407. <Location /perl-run>
  408. SetHandler perl-script
  409. PerlResponseHandler ModPerl::PerlRun
  410. PerlOptions +ParseHeaders
  411. Options +ExecCGI
  412. </Location>
  413. </IfModule>
  414. # Enable the Apache perl module
  415. a2enmod perl
  416. # Verify perl.conf and perl.load are in directory mods-enabled/
  417. ls mods-enabled/perl.*
  418. # Reload Apache
  419. apachectl graceful
  420. # Verify imapsync works under perl-run
  421. curl http://localhost/perl-run/imapsync
  422. =======================================================================
  423. =======================================================================