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.

NetRC.java 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /*
  2. * Copyright (C) 2014, Alexey Kuznetsov <axet@me.com>
  3. *
  4. * This program and the accompanying materials are made available
  5. * under the terms of the Eclipse Distribution License v1.0 which
  6. * accompanies this distribution, is reproduced below, and is
  7. * available at http://www.eclipse.org/org/documents/edl-v10.php
  8. *
  9. * All rights reserved.
  10. *
  11. * Redistribution and use in source and binary forms, with or
  12. * without modification, are permitted provided that the following
  13. * conditions are met:
  14. *
  15. * - Redistributions of source code must retain the above copyright
  16. * notice, this list of conditions and the following disclaimer.
  17. *
  18. * - Redistributions in binary form must reproduce the above
  19. * copyright notice, this list of conditions and the following
  20. * disclaimer in the documentation and/or other materials provided
  21. * with the distribution.
  22. *
  23. * - Neither the name of the Eclipse Foundation, Inc. nor the
  24. * names of its contributors may be used to endorse or promote
  25. * products derived from this software without specific prior
  26. * written permission.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  29. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  30. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  31. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  32. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  33. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  34. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  35. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  36. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  37. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  38. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  39. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  40. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  41. */
  42. package org.eclipse.jgit.transport;
  43. import static java.nio.charset.StandardCharsets.UTF_8;
  44. import java.io.BufferedReader;
  45. import java.io.File;
  46. import java.io.FileInputStream;
  47. import java.io.IOException;
  48. import java.io.InputStreamReader;
  49. import java.util.Collection;
  50. import java.util.HashMap;
  51. import java.util.Locale;
  52. import java.util.Map;
  53. import java.util.TreeMap;
  54. import java.util.regex.Matcher;
  55. import java.util.regex.Pattern;
  56. import org.eclipse.jgit.util.FS;
  57. /**
  58. * NetRC file parser.
  59. *
  60. * @since 3.5
  61. */
  62. public class NetRC {
  63. static final Pattern NETRC = Pattern.compile("(\\S+)"); //$NON-NLS-1$
  64. /**
  65. * 'default' netrc entry. This is the same as machine name except that
  66. * default matches any name. There can be only one default token, and it
  67. * must be after all machine tokens.
  68. */
  69. static final String DEFAULT_ENTRY = "default"; //$NON-NLS-1$
  70. /**
  71. * .netrc file entry
  72. */
  73. public static class NetRCEntry {
  74. /**
  75. * login netrc entry
  76. */
  77. public String login;
  78. /**
  79. * password netrc entry
  80. */
  81. public char[] password;
  82. /**
  83. * machine netrc entry
  84. */
  85. public String machine;
  86. /**
  87. * account netrc entry
  88. */
  89. public String account;
  90. /**
  91. * macdef netrc entry. Defines a macro. This token functions like the
  92. * ftp macdef command functions. A macro is defined with the specified
  93. * name; its contents begins with the next .netrc line and continues
  94. * until a null line (consecutive new-line characters) is encountered.
  95. * If a macro named init is defined, it is automatically executed as the
  96. * last step in the auto-login process.
  97. */
  98. public String macdef;
  99. /**
  100. * macro script body of macdef entry.
  101. */
  102. public String macbody;
  103. /**
  104. * Default constructor
  105. */
  106. public NetRCEntry() {
  107. }
  108. boolean complete() {
  109. return login != null && password != null && machine != null;
  110. }
  111. }
  112. private File netrc;
  113. private long lastModified;
  114. private Map<String, NetRCEntry> hosts = new HashMap<>();
  115. private static final TreeMap<String, State> STATE = new TreeMap<String, NetRC.State>() {
  116. private static final long serialVersionUID = -4285910831814853334L;
  117. {
  118. put("machine", State.MACHINE); //$NON-NLS-1$
  119. put("login", State.LOGIN); //$NON-NLS-1$
  120. put("password", State.PASSWORD); //$NON-NLS-1$
  121. put(DEFAULT_ENTRY, State.DEFAULT);
  122. put("account", State.ACCOUNT); //$NON-NLS-1$
  123. put("macdef", State.MACDEF); //$NON-NLS-1$
  124. }
  125. };
  126. enum State {
  127. COMMAND, MACHINE, LOGIN, PASSWORD, DEFAULT, ACCOUNT, MACDEF
  128. }
  129. /**
  130. * <p>Constructor for NetRC.</p>
  131. */
  132. public NetRC() {
  133. netrc = getDefaultFile();
  134. if (netrc != null)
  135. parse();
  136. }
  137. /**
  138. * <p>Constructor for NetRC.</p>
  139. *
  140. * @param netrc
  141. * the .netrc file
  142. */
  143. public NetRC(File netrc) {
  144. this.netrc = netrc;
  145. parse();
  146. }
  147. private static File getDefaultFile() {
  148. File home = FS.DETECTED.userHome();
  149. File netrc = new File(home, ".netrc"); //$NON-NLS-1$
  150. if (netrc.exists())
  151. return netrc;
  152. netrc = new File(home, "_netrc"); //$NON-NLS-1$
  153. if (netrc.exists())
  154. return netrc;
  155. return null;
  156. }
  157. /**
  158. * Get entry by host name
  159. *
  160. * @param host
  161. * the host name
  162. * @return entry associated with host name or null
  163. */
  164. public NetRCEntry getEntry(String host) {
  165. if (netrc == null)
  166. return null;
  167. if (this.lastModified != this.netrc.lastModified())
  168. parse();
  169. NetRCEntry entry = this.hosts.get(host);
  170. if (entry == null)
  171. entry = this.hosts.get(DEFAULT_ENTRY);
  172. return entry;
  173. }
  174. /**
  175. * Get all entries collected from .netrc file
  176. *
  177. * @return all entries collected from .netrc file
  178. */
  179. public Collection<NetRCEntry> getEntries() {
  180. return hosts.values();
  181. }
  182. private void parse() {
  183. this.hosts.clear();
  184. this.lastModified = this.netrc.lastModified();
  185. try (BufferedReader r = new BufferedReader(
  186. new InputStreamReader(new FileInputStream(netrc), UTF_8))) {
  187. String line = null;
  188. NetRCEntry entry = new NetRCEntry();
  189. State state = State.COMMAND;
  190. String macbody = ""; //$NON-NLS-1$
  191. Matcher matcher = NETRC.matcher(""); //$NON-NLS-1$
  192. while ((line = r.readLine()) != null) {
  193. // reading macbody
  194. if (entry.macdef != null && entry.macbody == null) {
  195. if (line.length() == 0) {
  196. entry.macbody = macbody;
  197. macbody = ""; //$NON-NLS-1$
  198. continue;
  199. }
  200. macbody += line + "\n"; //$NON-NLS-1$;
  201. continue;
  202. }
  203. matcher.reset(line);
  204. while (matcher.find()) {
  205. String command = matcher.group().toLowerCase(Locale.ROOT);
  206. if (command.startsWith("#")) { //$NON-NLS-1$
  207. matcher.reset(""); //$NON-NLS-1$
  208. continue;
  209. }
  210. state = STATE.get(command);
  211. if (state == null)
  212. state = State.COMMAND;
  213. switch (state) {
  214. case COMMAND:
  215. break;
  216. case ACCOUNT:
  217. if (entry.account != null && entry.complete()) {
  218. hosts.put(entry.machine, entry);
  219. entry = new NetRCEntry();
  220. }
  221. if (matcher.find())
  222. entry.account = matcher.group();
  223. state = State.COMMAND;
  224. break;
  225. case LOGIN:
  226. if (entry.login != null && entry.complete()) {
  227. hosts.put(entry.machine, entry);
  228. entry = new NetRCEntry();
  229. }
  230. if (matcher.find())
  231. entry.login = matcher.group();
  232. state = State.COMMAND;
  233. break;
  234. case PASSWORD:
  235. if (entry.password != null && entry.complete()) {
  236. hosts.put(entry.machine, entry);
  237. entry = new NetRCEntry();
  238. }
  239. if (matcher.find())
  240. entry.password = matcher.group().toCharArray();
  241. state = State.COMMAND;
  242. break;
  243. case DEFAULT:
  244. if (entry.machine != null && entry.complete()) {
  245. hosts.put(entry.machine, entry);
  246. entry = new NetRCEntry();
  247. }
  248. entry.machine = DEFAULT_ENTRY;
  249. state = State.COMMAND;
  250. break;
  251. case MACDEF:
  252. if (entry.macdef != null && entry.complete()) {
  253. hosts.put(entry.machine, entry);
  254. entry = new NetRCEntry();
  255. }
  256. if (matcher.find())
  257. entry.macdef = matcher.group();
  258. state = State.COMMAND;
  259. break;
  260. case MACHINE:
  261. if (entry.machine != null && entry.complete()) {
  262. hosts.put(entry.machine, entry);
  263. entry = new NetRCEntry();
  264. }
  265. if (matcher.find())
  266. entry.machine = matcher.group();
  267. state = State.COMMAND;
  268. break;
  269. }
  270. }
  271. }
  272. // reading macbody on EOF
  273. if (entry.macdef != null && entry.macbody == null)
  274. entry.macbody = macbody;
  275. if (entry.complete())
  276. hosts.put(entry.machine, entry);
  277. } catch (IOException e) {
  278. throw new RuntimeException(e);
  279. }
  280. }
  281. }