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.

244 lines
9.6KB

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