Imported Upstream version 1.12.0
[platform/upstream/augeas.git] / lenses / ssh.aug
1 (*
2 Module: Ssh
3   Parses ssh client configuration
4
5 Author: Jiri Suchomel <jsuchome@suse.cz>
6
7 About: Reference
8     ssh_config man page
9
10 About: License
11     This file is licensed under the GPL.
12
13 About: Lens Usage
14   Sample usage of this lens in augtool
15
16 (start code)
17 augtool> set /files/etc/ssh/ssh_config/Host example.com
18 augtool> set /files/etc/ssh/ssh_config/Host[.='example.com']/RemoteForward/machine1:1234 machine2:5678
19 augtool> set /files/etc/ssh/ssh_config/Host[.='example.com']/Ciphers/1 aes128-ctr
20 augtool> set /files/etc/ssh/ssh_config/Host[.='example.com']/Ciphers/2 aes192-ctr
21 (end code)
22
23 *)
24
25 module Ssh =
26     autoload xfm
27
28 (************************************************************************
29  * Group:                 USEFUL PRIMITIVES
30  *************************************************************************)
31
32     let eol = Util.doseol
33     let spc = Util.del_ws_spc
34     let spc_eq = del /[ \t]+|[ \t]*=[ \t]*/ " "
35     let comment = Util.comment
36     let empty = Util.empty
37     let comma = Util.del_str ","
38     let indent = Util.indent
39     let value_to_eol = store Rx.space_in
40     let value_to_spc = store /[^ \t\r\n=][^ \t\r\n]*/
41     let value_to_comma = store /[^, \t\r\n=][^, \t\r\n]*/
42
43
44 (************************************************************************
45  * Group:                 ENTRIES
46  *************************************************************************)
47
48     let array_entry (k:regexp) =
49         [ indent . key k . counter "array_entry"
50          . [ spc . seq "array_entry" . value_to_spc]* . eol ]
51
52     let commas_entry (k:regexp) =
53          let value = [ seq "commas_entry" . value_to_comma]
54       in [ indent . key k . counter "commas_entry" . spc_eq .
55            Build.opt_list value comma . eol ]
56
57     let spaces_entry (k:regexp) =
58          let value = [ seq "spaces_entry" . value_to_spc ]
59       in [ indent . key k . counter "spaces_entry" . spc_eq .
60            Build.opt_list value spc . eol ]
61
62     let fw_entry (k:regexp) = [ indent . key k . spc_eq .
63             [ key /[^ \t\r\n\/=][^ \t\r\n\/]*/ . spc . value_to_eol . eol ]]
64
65     let send_env = array_entry /SendEnv/i
66
67     let proxy_command = [ indent . key /ProxyCommand/i . spc . value_to_eol . eol ]
68
69     let remote_fw = fw_entry /RemoteForward/i
70     let local_fw = fw_entry /LocalForward/i
71
72     let ciphers = commas_entry /Ciphers/i
73     let macs    = commas_entry /MACs/i
74     let algorithms = commas_entry /(HostKey|Kex)Algorithms/i
75     let pubkey_accepted_key_types = commas_entry /PubkeyAcceptedKeyTypes/i
76
77     let global_knownhosts_file = spaces_entry /GlobalKnownHostsFile/i
78
79     let rekey_limit = [ indent . key /RekeyLimit/i . spc_eq .
80                         [ label "amount" . value_to_spc ] .
81                         [ spc . label "duration" . value_to_spc ]? . eol ]
82
83     let special_entry = send_env
84                             | proxy_command
85                             | remote_fw
86                             | local_fw
87                             | macs
88                             | ciphers
89                             | algorithms
90                             | pubkey_accepted_key_types
91                         | global_knownhosts_file
92                         | rekey_limit
93
94     let key_re = /[A-Za-z0-9]+/
95                - /SendEnv|Host|ProxyCommand|RemoteForward|LocalForward|MACs|Ciphers|(HostKey|Kex)Algorithms|PubkeyAcceptedKeyTypes|GlobalKnownHostsFile|RekeyLimit/i
96
97
98     let other_entry = [ indent . key key_re
99                     . spc_eq . value_to_spc . eol ]
100
101     let entry = comment | empty
102               | special_entry
103                     | other_entry
104
105     let host = [ key /Host/i . spc . value_to_eol . eol . entry* ]
106
107
108 (************************************************************************
109  * Group:                 LENS
110  *************************************************************************)
111
112     let lns = entry* . host*
113
114     let xfm = transform lns (incl "/etc/ssh/ssh_config" .
115                              incl (Sys.getenv("HOME") . "/.ssh/config") .
116                              incl "/etc/ssh/ssh_config.d/*.conf")