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.

227 lines
9.0KB

  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. {%- if sshd_config.get('ConfigBanner', False) -%}
  58. {{ sshd_config['ConfigBanner'] }}
  59. {%- else -%}
  60. # This file is managed by salt. Manual changes risk being overwritten.
  61. {%- endif %}
  62. # The contents of the original sshd_config are kept on the bottom for
  63. # quick reference.
  64. # See the sshd_config(5) manpage for details
  65. # Specifies which address family should be used by sshd(8).
  66. # Valid arguments are any, inet (use IPv4 only), or inet6 (use IPv6 only)
  67. {{ option('AddressFamily', 'any') }}
  68. # What ports, IPs and protocols we listen for
  69. {{ option('Port', 22) }}
  70. # Use these options to restrict which interfaces/protocols sshd will bind to
  71. {{ option('ListenAddress', ['::', '0.0.0.0']) }}
  72. {{ option_default_uncommented('Protocol', 2) }}
  73. # HostKeys for protocol version 2
  74. {{ 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']) -}}
  75. #Privilege Separation is turned on for security
  76. {{ option_default_uncommented('UsePrivilegeSeparation', 'yes') }}
  77. # Lifetime and size of ephemeral version 1 server key
  78. {{ option_default_uncommented('KeyRegenerationInterval', 3600) }}
  79. {{ option_default_uncommented('ServerKeyBits', 1024) }}
  80. # Logging
  81. {{ option_default_uncommented('SyslogFacility', 'AUTH') }}
  82. {{ option_default_uncommented('LogLevel', 'INFO') }}
  83. # Session idle time out
  84. {{ option_default_uncommented('ClientAliveInterval', 0) }}
  85. {{ option_default_uncommented('ClientAliveCountMax', 3) }}
  86. # Authentication:
  87. {{ option_default_uncommented('LoginGraceTime', 120) }}
  88. {{ option_default_uncommented('PermitRootLogin', 'yes') }}
  89. {{ option_default_uncommented('StrictModes', 'yes') }}
  90. {{ option_default_uncommented('MaxAuthTries', '6') }}
  91. {{ option_default_uncommented('MaxSessions', '10') }}
  92. {{ option('DSAAuthentication', 'yes') }}
  93. {{ option_default_uncommented('RSAAuthentication', 'yes') }}
  94. {{ option_default_uncommented('PubkeyAuthentication', 'yes') }}
  95. {{ option('AuthorizedKeysFile', '%h/.ssh/authorized_keys') }}
  96. {{ option('AuthorizedKeysCommand', 'none') }}
  97. {{ option('AuthorizedKeysCommandUser', 'nobody') }}
  98. # Don't read the user's ~/.rhosts and ~/.shosts files
  99. {{ option_default_uncommented('IgnoreRhosts', 'yes') }}
  100. # For this to work you will also need host keys in /etc/ssh_known_hosts
  101. {{ option_default_uncommented('RhostsRSAAuthentication', 'no') }}
  102. # similar for protocol version 2
  103. {{ option_default_uncommented('HostbasedAuthentication', 'no') }}
  104. # Uncomment if you don't trust ~/.ssh/known_hosts for RhostsRSAAuthentication
  105. {{ option('IgnoreUserKnownHosts', 'yes') }}
  106. # To enable empty passwords, change to yes (NOT RECOMMENDED)
  107. {{ option_default_uncommented('PermitEmptyPasswords', 'no') }}
  108. # Change to yes to enable challenge-response passwords (beware issues with
  109. # some PAM modules and threads)
  110. {{ option_default_uncommented('ChallengeResponseAuthentication', 'no') }}
  111. {{ option('AuthenticationMethods', 'publickey,keyboard-interactive') }}
  112. # Change to no to disable tunnelled clear text passwords
  113. {{ option('PasswordAuthentication', 'yes') }}
  114. # Kerberos options
  115. {{ option('KerberosAuthentication', 'no') }}
  116. {{ option('KerberosGetAFSToken', 'no') }}
  117. {{ option('KerberosOrLocalPasswd', 'yes') }}
  118. {{ option('KerberosTicketCleanup', 'yes') }}
  119. # GSSAPI options
  120. {{ option('GSSAPIAuthentication', 'no') }}
  121. {{ option('GSSAPICleanupCredentials', 'yes') }}
  122. {{ option_default_uncommented('X11Forwarding', 'yes') }}
  123. {{ option('AllowTcpForwarding', 'yes') }}
  124. {{ option_default_uncommented('X11DisplayOffset', '10') }}
  125. {{ option_default_uncommented('PrintMotd', 'no') }}
  126. {# Bug in FreeBSD 10.3 (?) See https://lists.freebsd.org/pipermail/freebsd-stable/2016-April/084501.html #}
  127. {% if not (salt['grains.get']('os') == 'FreeBSD' and salt['grains.get']('osrelease') == '10.3') -%}
  128. {{ option_default_uncommented('PrintLastLog', 'yes') }}
  129. {% endif -%}
  130. {{ option_default_uncommented('TCPKeepAlive', 'yes') }}
  131. {{ option('UseLogin', 'no') }}
  132. {{ option('MaxStartups', '10:30:60') }}
  133. {{ option('Banner', '/etc/issue.net') }}
  134. # Allow client to pass locale environment variables
  135. {{ option_default_uncommented('AcceptEnv', 'LANG LC_*') }}
  136. {{ option_default_uncommented('Subsystem', 'sftp /usr/lib/openssh/sftp-server') }}
  137. {% if not salt['grains.get']('os') == 'OpenBSD' -%}
  138. # Set this to 'yes' to enable PAM authentication, account processing,
  139. # and session processing. If this is enabled, PAM authentication will
  140. # be allowed through the ChallengeResponseAuthentication and
  141. # PasswordAuthentication. Depending on your PAM configuration,
  142. # PAM authentication via ChallengeResponseAuthentication may bypass
  143. # the setting of "PermitRootLogin without-password".
  144. # If you just want the PAM account and session checks to run without
  145. # PAM authentication, then enable this but set PasswordAuthentication
  146. # and ChallengeResponseAuthentication to 'no'.
  147. {{ option_default_uncommented('UsePAM', 'yes') }}
  148. {%- endif %}
  149. # DNS resolve and map remote IP addresses
  150. {{ option('UseDNS', 'yes') }}
  151. # Restricting Users and Hosts
  152. # example:
  153. # AllowUsers vader@10.0.0.1 maul@sproing.evil.com luke
  154. # AllowGroups wheel staff
  155. #
  156. # Keep in mind that using AllowUsers or AllowGroups means that anyone
  157. # not Matching one of the supplied patterns will be denied access by default.
  158. # Also, in order for sshd to allow access based on full or partial hostnames it
  159. # needs to to a DNS lookup
  160. #
  161. # DenyUsers
  162. {{ option('DenyUsers', '') }}
  163. # AllowUsers
  164. {{ option('AllowUsers', '') }}
  165. # DenyGroups
  166. {{ option('DenyGroups', '') }}
  167. # AllowGroups
  168. {{ option('AllowGroups', '') }}
  169. # Specifies the available KEX (Key Exchange) algorithms.
  170. {{ 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) }}
  171. # Specifies the ciphers allowed for protocol version 2.
  172. {{ 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) }}
  173. # Specifies the available MAC (message authentication code) algorithms.
  174. {{ 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) }}
  175. {# Handling unknown in salt template options #}
  176. {%- for keyword in sshd_config.keys() %}
  177. {#- Matches have to be at the bottom and should be handled differently -#}
  178. {%- if not keyword in processed_options and keyword != 'matches' -%}
  179. {#- send a blank default as it doesn't matter #}
  180. {{ render_option(keyword, '') }}
  181. {%- endif -%}
  182. {%- endfor %}
  183. {# Handle matches last as they need to go at the bottom #}
  184. {%- if 'matches' in sshd_config %}
  185. {%- for match in sshd_config['matches'].values() %}
  186. Match {{ match['type'].keys()[0] }} {{ match['type'].values()[0] }}
  187. {%- for keyword in match['options'].keys() %}
  188. {{ render_option(keyword, '', config_dict=match['options']) }}
  189. {%- endfor %}
  190. {%- endfor %}
  191. {%- endif %}
  192. {#- vim: set ft=jinja : #}