Bug 236944

Summary: security/strongswan: startup script improvements
Product: Ports & Packages Reporter: Jose Luis Duran <jlduran>
Component: Individual Port(s)Assignee: Mateusz Piotrowski <0mp>
Status: Closed Overcome By Events    
Severity: Affects Only Me CC: 0mp, driesm, sc.gear, strongswan
Priority: --- Keywords: needs-qa
Version: LatestFlags: strongswan: maintainer-feedback+
koobs: merge-quarterly?
Hardware: Any   
OS: Any   
See Also: https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=234648
Attachments:
Description Flags
Patch for strongswan.in
none
Patch strongswan.in
none
Improve strongswan.in
none
Improve strongswan.in
none
Improve strongswan.in
none
Improve strongswan.in
none
Improve strongswan.in
none
Improve strongswan.in none

Description Jose Luis Duran 2019-04-01 17:56:00 UTC
Created attachment 203306 [details]
Patch for strongswan.in

This is a follow-up of bug #234648.

There are still a few missing details:

If the service is stopped, and "reload", "status" or "statusall" is issued, the message displayed is rather cryptic.

Also, if there are established connections, and "restart" is issued, there is a possibility for charon not having enough time to teardown.  In _ipsec.in they hardcode a sleep of 2 seconds (https://github.com/strongswan/strongswan/blob/5c38a5ea832accec3a8d3942d878ac5be5bb2a4b/src/ipsec/_ipsec.in#L183), here I've decided to reuse the waiting stanza.

This is a proposed patch, again, far from elegant, but we're getting there.

Thank you!
Comment 1 Francois ten Krooden 2019-04-04 05:13:41 UTC
(In reply to Jose Luis Duran from comment #0)

Thanks will have a look at the patch.  Just have a deadline, so will try and get to it asap.
But it looks good with the quick glance I gave it.
Comment 2 Jose Luis Duran 2019-04-05 04:17:11 UTC
Created attachment 203395 [details]
Patch strongswan.in

This patch includes the PORTREVISION bump and a change to https in the package description.
Comment 3 Jose Luis Duran 2019-04-08 15:14:07 UTC
Created attachment 203489 [details]
Improve strongswan.in

Make the message more consistent with rc.subr.
Comment 4 Jose Luis Duran 2019-04-22 04:17:06 UTC
Created attachment 203887 [details]
Improve strongswan.in

- Use https
- Fix a few typos
Comment 5 Sam Chen 2019-04-22 15:02:17 UTC
Hi Jose, thanks for making the code better.  Small observations and suggestions, if you will:
1) check_charon() - Consider rc.subr err() to throw messages.  Why exit code 7 ?  Since "swanctl --stats" sets errno 61 on VICI socket failure.
2) wait_charon() - Consider hard-code the wait interval (e.g. "sleep 5").  Else while loop runs command "sleep 1" five times when $charon_pidfile exists.
Comment 6 Jose Luis Duran 2019-04-22 15:28:29 UTC
(In reply to Sam Chen from comment #5)

Thank you for taking the time to review the patch:

1) I tried to keep it similar to ipsec.in, currently the failing exit code for reload is 7:

https://github.com/strongswan/strongswan/blob/5c38a5ea832accec3a8d3942d878ac5be5bb2a4b/src/ipsec/_ipsec.in#L171

2) The idea is not to use a hard-coded sleep, else I would prefer to use 2 seconds, which is what ipsec.in is currently using (see comment #0). The idea is that if $charon_pidfile still exists after stopping the service, wait another second.

What I would like to do is to rely more on rc.subr to clean up the code a bit more. By using $pidfile and perhaps $procname.
Comment 7 Sam Chen 2019-04-22 16:19:42 UTC
(In reply to Jose Luis Duran from comment #6)

Thanks, using code 7 for missing charon.pid now makes perfect sense to me.  My mistake in #2, I misread the code and was about to ask you disregard that comment.

Another observation:
$strongswan_interface declaration changed from ":=" to ":-", which IIRC, means the parameter disappears after use.  Since strongswan.in is the first (only?) place $strongswan_interface is defined (vs others preset in /etc/defaults/rc.conf).  I'm not familiar with FreeBSD init.  How does ":-" expansion change the scope of the variable outside of strongswan.in?  Any problem if $strongswan_interface is not set in /etc/rc.conf?

Thanks!
Comment 8 Sam Chen 2019-04-22 16:59:07 UTC
(In reply to Sam Chen from comment #7)

Please disregard observation #3 => Just a different coding style that give the same result.
Comment 9 Sam Chen 2019-04-23 18:53:46 UTC
My interest is in the updated restart/stop_postcmd section, and I can confirm that works fine.  Thank you.

Changes to error messages, I'm indifferent.  Just two things I noticed while testing:
1. strongswan.in check_charon() echos to STDOUT.  /usr/local/sbin/ipsec and rc.subr warn() err() dups to STDERR.
2. "ipsec reload" has more verbose message of "Reloading strongSwan IPsec failed: starter is not running".
Comment 10 Jose Luis Duran 2019-04-24 00:57:45 UTC
(In reply to Sam Chen from comment #9)

1. I can add the missing `>&2`.
2. Something like "Reloading strongSwan IPsec failed: charon is not running"?

Thanks.
Comment 11 Francois ten Krooden 2019-04-24 11:04:36 UTC
I'm happy with the updates.
Seems like a good improvement on the previous script.
Comment 12 Jose Luis Duran 2019-05-04 07:36:42 UTC
Created attachment 204210 [details]
Improve strongswan.in

Redirect to stderr.
Comment 13 Jose Luis Duran 2019-05-29 15:43:39 UTC
Created attachment 204703 [details]
Improve strongswan.in

Rebase 5.8.0
Comment 14 Jose Luis Duran 2019-10-04 18:28:00 UTC
Created attachment 208097 [details]
Improve strongswan.in

Rebasing 5.8.1

Improving the script to rely more on rc.subr.

I have setup an ATF (Kyua-based) with some tests for the rc script (https://github.com/jlduran/strongswan-tests/blob/master/tests/freebsd/rc_script/rc_script), the strongswan.in currently distributed fails some of these tests.

The submitted version has a few differences, for instance I am keeping "stroke" as the default strongswan_interface, while upstream (starting 5.8.0) and in my GitHub repo "swanctl" (or "vici") is the default.
Comment 15 Francois ten Krooden 2019-10-11 08:44:05 UTC
Comment on attachment 208097 [details]
Improve strongswan.in

Happy with the updates on the script that improves to be more robust.
Comment 16 Jose Luis Duran 2019-10-11 10:52:43 UTC
(In reply to strongswan from comment #15)
Thank you for reviewing!

I will upgrade the script a little bit so that the sleep waits 5 seconds (as it is right now), just to make sure no one is affected by this new version. Hopefully before next week.

I was reluctant to do it because I wanted to use something kqueue-based. I have not found anything in base that allows me to do so.  In the meantime, I'll keep the sleep loop.

Thank you all!
Comment 17 Jose Luis Duran 2019-10-11 12:54:27 UTC
Created attachment 208247 [details]
Improve strongswan.in

Put back the sleep-wait block, I could not find a kqueue(2)-based tool to use that detects when the charon_pidfile has been created.  Use the current polling method.
Comment 18 Dries Michiels freebsd_committer freebsd_triage 2020-01-31 16:44:21 UTC
Hi! Based on the current rc script, most of this functionality seems to be already included upstream. Could be closed with "overcome by events".