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.

sshd_config 9.9KB

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