Saltstack Official OpenSSH Formula
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

241 lines
8.5KB

  1. {% from "openssh/map.jinja" import sshd_config with context %}
  2. {#- present in sshd_config and known in actual file options -#}
  3. {%- set processed_options = [] -%}
  4. {#- generic renderer used for sshd matches, known options, -#}
  5. {#- and unknown options -#}
  6. {%- macro render_option(keyword, config_dict=sshd_config) -%}
  7. {%- set value = config_dict.get(keyword) -%}
  8. {%- if value is sameas true -%}
  9. {{ keyword }} yes
  10. {% elif value is sameas false -%}
  11. {{ keyword }} no
  12. {% elif value is string or value is number -%}
  13. {{ keyword }} {{ value }}
  14. {% else -%}
  15. {%- for single_value in value -%}
  16. {{ keyword }} {{ single_value }}
  17. {% endfor -%}
  18. {%- endif -%}
  19. {%- endmacro -%}
  20. {#- macros for render option if present -#}
  21. {%- macro option(keyword, present) -%}
  22. {%- if keyword in sshd_config -%}
  23. {%- do processed_options.append(keyword) -%}
  24. {{ render_option(keyword) }}
  25. {%- endif -%}
  26. {%- endmacro -%}
  27. {#- macro for collapsing a list into a string -#}
  28. {%- macro option_collapselist(keyword, sep, config_dict=None) -%}
  29. {%- if config_dict is sameas None -%}
  30. {%- do processed_options.append(keyword) -%}
  31. {%- set config_dict = sshd_config -%}
  32. {%- endif -%}
  33. {{ keyword }} {{ config_dict.get(keyword) | join(sep) }}
  34. {% endmacro -%}
  35. {#- macro for handling an option that can be specified as a list or a string -#}
  36. {%- macro option_string_or_list(keyword, sep=',') -%}
  37. {%- if sshd_config.get(keyword, '') is string -%}
  38. {{ option(keyword) }}
  39. {%- else -%}
  40. {{ option_collapselist(keyword, sep) }}
  41. {%- endif -%}
  42. {%- endmacro -%}
  43. {#- macro for conditionally joining a string, list or dict(keys) to just a string -#}
  44. {%- macro join_to_string(src, keyword, sep=',') -%}
  45. {%- set srcval = src.get(keyword, '') -%}
  46. {%- if srcval is string -%}
  47. {{ srcval }}
  48. {%- elif srcval is mapping -%}
  49. {{ srcval.keys() | sort | join(sep) }}
  50. {%- else -%}
  51. {{ srcval | join(sep) }}
  52. {%- endif -%}
  53. {%- endmacro -%}
  54. {%- if sshd_config.get('ConfigBanner', False) -%}
  55. {%- do processed_options.append('ConfigBanner') -%}
  56. {{ sshd_config['ConfigBanner'] }}
  57. {%- else -%}
  58. # This file is managed by salt. Manual changes risk being overwritten.
  59. {%- endif %}
  60. {%- set global_src_url = salt ['pillar.get']('__formulas:print_template_url', None) %}
  61. {%- set local_src_url = salt ['pillar.get']('openssh-formula:print_template_url', None) %}
  62. {%- if (global_src_url and local_src_url is none) or local_src_url %}
  63. #
  64. # Template used to generate this file:
  65. # {{ source }}
  66. #
  67. {%- endif %}
  68. # The contents of the original sshd_config are kept on the bottom for
  69. # quick reference.
  70. # See the sshd_config(5) manpage for details
  71. {# Specifies which address family should be used by sshd(8). -#}
  72. {#- Valid arguments are any, inet (use IPv4 only), or inet6 (use IPv6 only) -#}
  73. {{- option('AddressFamily') -}}
  74. {#- What ports, IPs and protocols we listen for -#}
  75. {{- option('Port') -}}
  76. {#- Use these options to restrict which interfaces/protocols sshd will bind to -#}
  77. {{- option('ListenAddress') -}}
  78. {{- option('Protocol') -}}
  79. {#- HostKeys for protocol version 2 -#}
  80. {{- option('HostKey') -}}
  81. {#- Privilege Separation is turned on for security -#}
  82. {{- option('UsePrivilegeSeparation') -}}
  83. {#- Lifetime and size of ephemeral version 1 server key -#}
  84. {{- option('KeyRegenerationInterval') -}}
  85. {{- option('ServerKeyBits') -}}
  86. {#- Logging -#}
  87. {{- option('SyslogFacility') -}}
  88. {{- option('LogLevel') -}}
  89. {#- Session idle time out -#}
  90. {{- option('ClientAliveInterval') -}}
  91. {{- option('ClientAliveCountMax') -}}
  92. {#- Authentication: -#}
  93. {{- option('LoginGraceTime') -}}
  94. {{- option('PermitRootLogin') -}}
  95. {{- option('StrictModes') -}}
  96. {{- option('MaxAuthTries') -}}
  97. {{- option('MaxSessions') -}}
  98. {{- option('DSAAuthentication') -}}
  99. {{- option('RSAAuthentication') -}}
  100. {{- option('PubkeyAuthentication') -}}
  101. {{- option('AuthorizedKeysFile') -}}
  102. {{- option('AuthorizedKeysCommand') -}}
  103. {{- option('AuthorizedKeysCommandUser') -}}
  104. {#- Don't read the user's ~/.rhosts and ~/.shosts files -#}
  105. {{- option('IgnoreRhosts') -}}
  106. {#- For this to work you will also need host keys in /etc/ssh_known_hosts -#}
  107. {{- option('RhostsRSAAuthentication') -}}
  108. {#- similar for protocol version 2 -#}
  109. {{- option('HostbasedAuthentication') -}}
  110. {#- Uncomment if you don't trust ~/.ssh/known_hosts for RhostsRSAAuthentication -#}
  111. {{- option('IgnoreUserKnownHosts') -}}
  112. {#- To enable empty passwords, change to yes (NOT RECOMMENDED) -#}
  113. {{- option('PermitEmptyPasswords') -}}
  114. {#- Change to yes to enable challenge-response passwords (beware issues with -#}
  115. {#- some PAM modules and threads) -#}
  116. {{- option('ChallengeResponseAuthentication') -}}
  117. {{- option('AuthenticationMethods') -}}
  118. {#- Change to no to disable tunnelled clear text passwords -#}
  119. {{- option('PasswordAuthentication') -}}
  120. {#- Kerberos options -#}
  121. {{- option('KerberosAuthentication') -}}
  122. {{- option('KerberosGetAFSToken') -}}
  123. {{- option('KerberosOrLocalPasswd') -}}
  124. {{- option('KerberosTicketCleanup') -}}
  125. {#- GSSAPI options -#}
  126. {{- option('GSSAPIAuthentication') -}}
  127. {{- option('GSSAPICleanupCredentials') -}}
  128. {{- option('X11Forwarding') -}}
  129. {{- option('AllowTcpForwarding') -}}
  130. {{- option('X11DisplayOffset') -}}
  131. {{- option('PrintMotd') -}}
  132. {#- Bug in FreeBSD 10.3 (?) See https://lists.freebsd.org/pipermail/freebsd-stable/2016-April/084501.html -#}
  133. {% if not (salt['grains.get']('os') == 'FreeBSD' and salt['grains.get']('osrelease')|float >= 10.3) -%}
  134. {{- option('PrintLastLog') -}}
  135. {% endif -%}
  136. {{- option('TCPKeepAlive') -}}
  137. {{- option('UseLogin') -}}
  138. {{- option('MaxStartups') -}}
  139. {{- option('Banner') -}}
  140. {#- Allow client to pass locale environment variables -#}
  141. {{- option('AcceptEnv') -}}
  142. {{- option('Subsystem') -}}
  143. {% if not salt['grains.get']('os') == 'OpenBSD' -%}
  144. {#- Set this to 'yes' to enable PAM authentication, account processing, -#}
  145. {#- and session processing. If this is enabled, PAM authentication will -#}
  146. {#- be allowed through the ChallengeResponseAuthentication and -#}
  147. {#- PasswordAuthentication. Depending on your PAM configuration, -#}
  148. {#- PAM authentication via ChallengeResponseAuthentication may bypass -#}
  149. {#- the setting of "PermitRootLogin without-password". -#}
  150. {#- If you just want the PAM account and session checks to run without -#}
  151. {#- PAM authentication, then enable this but set PasswordAuthentication -#}
  152. {#- and ChallengeResponseAuthentication to 'no'. -#}
  153. {{- option('UsePAM') -}}
  154. {%- endif %}
  155. {#- DNS resolve and map remote IP addresses -#}
  156. {{- option('UseDNS') -}}
  157. {#- Restricting Users and Hosts -#}
  158. {#- example: -#}
  159. {#- AllowUsers vader@10.0.0.1 maul@sproing.evil.com luke -#}
  160. {#- AllowGroups wheel staff -#}
  161. {#- Keep in mind that using AllowUsers or AllowGroups means that anyone -#}
  162. {#- not Matching one of the supplied patterns will be denied access by default. -#}
  163. {#- Also, in order for sshd to allow access based on full or partial hostnames it -#}
  164. {#- needs to to a DNS lookup -#}
  165. {# DenyUsers -#}
  166. {{- option_string_or_list('DenyUsers', sep=' ') }}
  167. {# AllowUsers -#}
  168. {{- option_string_or_list('AllowUsers', sep=' ') }}
  169. {# DenyGroups -#}
  170. {{- option_string_or_list('DenyGroups', sep=' ') }}
  171. {# AllowGroups -#}
  172. {{- option_string_or_list('AllowGroups', sep=' ') }}{{ "\n" -}}
  173. {#- Specifies the available KEX (Key Exchange) algorithms. -#}
  174. {{- option_string_or_list('KexAlgorithms') -}}
  175. {#- Specifies the ciphers allowed for protocol version 2. -#}
  176. {{- option_string_or_list('Ciphers') -}}
  177. {#- Specifies the available MAC (message authentication code) algorithms. -#}
  178. {{- option_string_or_list('MACs') -}}
  179. {#- Handling unknown in salt template options -#}
  180. {%- for keyword in sshd_config.keys() %}
  181. {#- Matches have to be at the bottom and should be handled differently -#}
  182. {%- if not keyword in processed_options and keyword != 'matches' -%}
  183. {{- render_option(keyword) -}}
  184. {% endif -%}
  185. {%- endfor %}
  186. {#- Handle matches last as they need to go at the bottom -#}
  187. {%- if 'matches' in sshd_config %}
  188. {%- for name, match in sshd_config['matches']|dictsort(true) %}
  189. Match
  190. {#- Set up the match criteria -#}
  191. {%- for criteria in match['type'].keys()|sort() -%}
  192. {{ ' ' -}}{{criteria }} {{ join_to_string(match['type'], criteria) }}
  193. {%- endfor %} #{{- name }}
  194. {# Set up the applied options -#}
  195. {%- for keyword in match['options'].keys()|sort() -%}
  196. {%- if keyword in ['AllowUsers', 'DenyUsers', 'AllowGroups', 'DenyGroups'] -%}
  197. {{ option_collapselist(keyword, ' ', config_dict=match['options']) | indent(4, true) }}
  198. {% else -%}
  199. {{ render_option(keyword, config_dict=match['options']) | indent(4, true) }}
  200. {% endif -%}
  201. {%- endfor %}
  202. {%- endfor %}
  203. {%- endif %}
  204. {#- vim: set ft=jinja : -#}