| | | 1 | | using System; |
| | | 2 | | using System.Text; |
| | | 3 | | |
| | | 4 | | namespace Renci.SshNet |
| | | 5 | | { |
| | | 6 | | /// <summary> |
| | | 7 | | /// Quotes a path in a way to be suitable to be used with a shell-based server. |
| | | 8 | | /// </summary> |
| | | 9 | | internal sealed class RemotePathShellQuoteTransformation : IRemotePathTransformation |
| | | 10 | | { |
| | | 11 | | /// <summary> |
| | | 12 | | /// Quotes a path in a way to be suitable to be used with a shell-based server. |
| | | 13 | | /// </summary> |
| | | 14 | | /// <param name="path">The path to transform.</param> |
| | | 15 | | /// <returns> |
| | | 16 | | /// A quoted path. |
| | | 17 | | /// </returns> |
| | | 18 | | /// <exception cref="ArgumentNullException"><paramref name="path"/> is <see langword="null"/>.</exception> |
| | | 19 | | /// <remarks> |
| | | 20 | | /// <para> |
| | | 21 | | /// If <paramref name="path"/> contains a single-quote, that character is embedded |
| | | 22 | | /// in quotation marks (eg. "'"). Sequences of single-quotes are grouped in a single |
| | | 23 | | /// pair of quotation marks. |
| | | 24 | | /// </para> |
| | | 25 | | /// <para> |
| | | 26 | | /// An exclamation mark in <paramref name="path"/> is escaped with a backslash. This is |
| | | 27 | | /// necessary because C Shell interprets it as a meta-character for history substitution |
| | | 28 | | /// even when enclosed in single quotes or quotation marks. |
| | | 29 | | /// </para> |
| | | 30 | | /// <para> |
| | | 31 | | /// All other characters are enclosed in single quotes. Sequences of such characters are grouped |
| | | 32 | | /// in a single pair of single quotes. |
| | | 33 | | /// </para> |
| | | 34 | | /// <para> |
| | | 35 | | /// References: |
| | | 36 | | /// <list type="bullet"> |
| | | 37 | | /// <item> |
| | | 38 | | /// <description><a href="http://pubs.opengroup.org/onlinepubs/7908799/xcu/chap2.html">Shell Command Languag |
| | | 39 | | /// </item> |
| | | 40 | | /// <item> |
| | | 41 | | /// <description><a href="https://earthsci.stanford.edu/computing/unix/shell/specialchars.php">Unix C-Shell |
| | | 42 | | /// </item> |
| | | 43 | | /// <item> |
| | | 44 | | /// <description><a href="https://docstore.mik.ua/orelly/unix3/upt/ch27_13.htm">Differences Between Bourne a |
| | | 45 | | /// </item> |
| | | 46 | | /// </list> |
| | | 47 | | /// </para> |
| | | 48 | | /// </remarks> |
| | | 49 | | /// <example> |
| | | 50 | | /// <list type="table"> |
| | | 51 | | /// <listheader> |
| | | 52 | | /// <term>Original</term> |
| | | 53 | | /// <term>Transformed</term> |
| | | 54 | | /// </listheader> |
| | | 55 | | /// <item> |
| | | 56 | | /// <term>/var/log/auth.log</term> |
| | | 57 | | /// <term>'/var/log/auth.log'</term> |
| | | 58 | | /// </item> |
| | | 59 | | /// <item> |
| | | 60 | | /// <term>/var/mp3/Guns N' Roses</term> |
| | | 61 | | /// <term>'/var/mp3/Guns N'"'"' Roses'</term> |
| | | 62 | | /// </item> |
| | | 63 | | /// <item> |
| | | 64 | | /// <term>/var/garbage!/temp</term> |
| | | 65 | | /// <term>'/var/garbage'\!'/temp'</term> |
| | | 66 | | /// </item> |
| | | 67 | | /// <item> |
| | | 68 | | /// <term>/var/would be 'kewl'!, not?</term> |
| | | 69 | | /// <term>'/var/would be '"'"'kewl'"'"\!', not?'</term> |
| | | 70 | | /// </item> |
| | | 71 | | /// <item> |
| | | 72 | | /// <term></term> |
| | | 73 | | /// <term>''</term> |
| | | 74 | | /// </item> |
| | | 75 | | /// <item> |
| | | 76 | | /// <term>Hello "World"</term> |
| | | 77 | | /// <term>'Hello "World"'</term> |
| | | 78 | | /// </item> |
| | | 79 | | /// </list> |
| | | 80 | | /// </example> |
| | | 81 | | public string Transform(string path) |
| | 394 | 82 | | { |
| | 394 | 83 | | if (path is null) |
| | 3 | 84 | | { |
| | 3 | 85 | | throw new ArgumentNullException(nameof(path)); |
| | | 86 | | } |
| | | 87 | | |
| | | 88 | | // result is at least value and (likely) leading/trailing single-quotes |
| | 391 | 89 | | var sb = new StringBuilder(path.Length + 2); |
| | 391 | 90 | | var state = ShellQuoteState.Unquoted; |
| | | 91 | | |
| | 8857 | 92 | | foreach (var c in path) |
| | 3842 | 93 | | { |
| | 3842 | 94 | | switch (c) |
| | | 95 | | { |
| | | 96 | | case '\'': |
| | | 97 | | // embed a single-quote in quotes |
| | 58 | 98 | | switch (state) |
| | | 99 | | { |
| | | 100 | | case ShellQuoteState.Unquoted: |
| | | 101 | | // Start quoted string |
| | 12 | 102 | | _ = sb.Append('"'); |
| | 12 | 103 | | break; |
| | | 104 | | case ShellQuoteState.Quoted: |
| | | 105 | | // Continue quoted string |
| | 3 | 106 | | break; |
| | | 107 | | case ShellQuoteState.SingleQuoted: |
| | | 108 | | // Close single-quoted string |
| | 43 | 109 | | _ = sb.Append('\''); |
| | | 110 | | |
| | | 111 | | // Start quoted string |
| | 43 | 112 | | _ = sb.Append('"'); |
| | 43 | 113 | | break; |
| | | 114 | | default: |
| | 0 | 115 | | break; |
| | | 116 | | } |
| | | 117 | | |
| | 58 | 118 | | state = ShellQuoteState.Quoted; |
| | 58 | 119 | | break; |
| | | 120 | | case '!': |
| | | 121 | | /* |
| | | 122 | | * In C-Shell, an exclamatation point can only be protected from shell interpretation |
| | | 123 | | * when escaped by a backslash. |
| | | 124 | | * |
| | | 125 | | * Source: |
| | | 126 | | * https://earthsci.stanford.edu/computing/unix/shell/specialchars.php |
| | | 127 | | */ |
| | | 128 | | |
| | 42 | 129 | | switch (state) |
| | | 130 | | { |
| | | 131 | | case ShellQuoteState.Unquoted: |
| | 9 | 132 | | _ = sb.Append('\\'); |
| | 9 | 133 | | break; |
| | | 134 | | case ShellQuoteState.Quoted: |
| | | 135 | | // Close quoted string |
| | 18 | 136 | | _ = sb.Append('"'); |
| | 18 | 137 | | _ = sb.Append('\\'); |
| | 18 | 138 | | break; |
| | | 139 | | case ShellQuoteState.SingleQuoted: |
| | | 140 | | // Close single quoted string |
| | 15 | 141 | | _ = sb.Append('\''); |
| | 15 | 142 | | _ = sb.Append('\\'); |
| | 15 | 143 | | break; |
| | | 144 | | default: |
| | 0 | 145 | | break; |
| | | 146 | | } |
| | | 147 | | |
| | 42 | 148 | | state = ShellQuoteState.Unquoted; |
| | 42 | 149 | | break; |
| | | 150 | | default: |
| | 3742 | 151 | | switch (state) |
| | | 152 | | { |
| | | 153 | | case ShellQuoteState.Unquoted: |
| | | 154 | | // Start single-quoted string |
| | 397 | 155 | | _ = sb.Append('\''); |
| | 397 | 156 | | break; |
| | | 157 | | case ShellQuoteState.Quoted: |
| | | 158 | | // Close quoted string |
| | 31 | 159 | | _ = sb.Append('"'); |
| | | 160 | | |
| | | 161 | | // Start single-quoted string |
| | 31 | 162 | | _ = sb.Append('\''); |
| | 31 | 163 | | break; |
| | | 164 | | case ShellQuoteState.SingleQuoted: |
| | | 165 | | // Continue single-quoted string |
| | 3314 | 166 | | break; |
| | | 167 | | default: |
| | 0 | 168 | | break; |
| | | 169 | | } |
| | | 170 | | |
| | 3742 | 171 | | state = ShellQuoteState.SingleQuoted; |
| | 3742 | 172 | | break; |
| | | 173 | | } |
| | | 174 | | |
| | 3842 | 175 | | _ = sb.Append(c); |
| | 3842 | 176 | | } |
| | | 177 | | |
| | 391 | 178 | | switch (state) |
| | | 179 | | { |
| | | 180 | | case ShellQuoteState.Unquoted: |
| | 15 | 181 | | break; |
| | | 182 | | case ShellQuoteState.Quoted: |
| | | 183 | | // Close quoted string |
| | 6 | 184 | | _ = sb.Append('"'); |
| | 6 | 185 | | break; |
| | | 186 | | case ShellQuoteState.SingleQuoted: |
| | | 187 | | // Close single-quoted string |
| | 370 | 188 | | _ = sb.Append('\''); |
| | 370 | 189 | | break; |
| | | 190 | | default: |
| | 0 | 191 | | break; |
| | | 192 | | } |
| | | 193 | | |
| | 391 | 194 | | if (sb.Length == 0) |
| | 3 | 195 | | { |
| | 3 | 196 | | _ = sb.Append("''"); |
| | 3 | 197 | | } |
| | | 198 | | |
| | 391 | 199 | | return sb.ToString(); |
| | 391 | 200 | | } |
| | | 201 | | |
| | | 202 | | private enum ShellQuoteState |
| | | 203 | | { |
| | | 204 | | Unquoted = 1, |
| | | 205 | | SingleQuoted = 2, |
| | | 206 | | Quoted = 3 |
| | | 207 | | } |
| | | 208 | | } |
| | | 209 | | } |