I am trying to understand an sshd configuration that I believe should not work but does. The premise comes from a production system I’m working on; however, I simplified it for my own testing. Since I am unable to explain why this simple example works, I am also unable to explain why the more complex iteration works.
I have two servers, both with users Auser, Buser, and Cuser.
My client machine has an IP address of 192.168.10.1
My server has an sshd configuration that looks like this:
AllowGroups Cuser
Match Address 192.168.10.1
AllowGroups Cuser Buser
Match Address 192.168.10.1
AllowGroups Cuser Auser
According to the man pages for sshd_config(5)
Match Introduces a conditional block. If all of the criteria on the Match line are satisfied, the keywords on the following lines override those set in the global section of the config file, un- til either another Match line or the end of the file. If a key- word appears in multiple Match blocks that are satisfied, only the first instance of the keyword is applied.
My interpretation is that from the client (192.168.10.1), only Cuser and Buser should be allowed to ssh to the server.
However when I test this, all three users: Auser, Buser, and Cuser have access. If I look in the sshd logs for the server, I see where each match block is applied:
Jul 25 10:48:59 server1 sshd[3525]: debug3: Trying to reverse map address 192.168.10.1.
Jul 25 10:48:59 server1 sshd[3525]: debug2: parse_server_config: config reprocess config len 854
Jul 25 10:48:59 server1 sshd[3525]: debug3: checking match for 'Address 192.168.10.1' user buser host client addr 192.168.10.1 laddr 192.168.10.4 lport 22
Jul 25 10:48:59 server1 sshd[3525]: debug1: connection from 192.168.10.1 matched 'Address 192.168.10.1' at line 138
Jul 25 10:48:59 server1 sshd[3525]: debug3: match found
Jul 25 10:48:59 server1 sshd[3525]: debug3: reprocess config:139 setting AllowGroups cuser buser
Jul 25 10:48:59 server1 sshd[3525]: debug3: checking match for 'Address 192.168.10.1' user buser host fedora addr 192.168.10.1 laddr 192.168.10.4 lport 22
Jul 25 10:48:59 server1 sshd[3525]: debug1: connection from 192.168.10.1 matched 'Address 192.168.10.1' at line 140
Jul 25 10:48:59 server1 sshd[3525]: debug3: match found
Jul 25 10:48:59 server1 sshd[3525]: debug3: reprocess config:141 setting AllowGroups cuser auser
So, interestingly, from my interpretation of the man pages, I would have expected only the first “reprocess config:139” line to be applied as it is the first instance of the AllowGroups keyword. However, looking at the logs, since I see “reprocess config:141 setting AllowGroups cuser auser”, I might only expect the second instance to be applied.
However, neither of these interpretations seem correct since all three users are able to connect.
So, with some additional testing I changed my sshd_config to look like this:
AllowGroups Cuser
Match Address 192.168.10.1
AllowGroups Cuser Buser
Match Address 192.168.10.1
AllowGroups Auser
and
AllowGroups Cuser
Match Address 192.168.10.1
AllowGroups Auser
Match Address 192.168.10.1
AllowGroups Cuser Buser
All three users were still able to login.
And one final test
AllowGroups Cuser
Match Address 192.168.10.1
AllowGroups Buser
Match Address 192.168.10.1
AllowGroups Auser
Finally, only Auser and Buser have access.
It's almost as if the first Match block will override any default settings, but subsequent match blocks append to any previous match blocks.