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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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 java.io.BufferedReader;
  44. import java.io.File;
  45. import java.io.FileReader;
  46. import java.io.IOException;
  47. import java.util.Collection;
  48. import java.util.HashMap;
  49. import java.util.Locale;
  50. import java.util.Map;
  51. import java.util.TreeMap;
  52. import java.util.regex.Matcher;
  53. import java.util.regex.Pattern;
  54. import org.eclipse.jgit.util.FS;
  55. /**
  56. * NetRC file parser.
  57. *
  58. * @since 3.5
  59. */
  60. public class NetRC {
  61. static final Pattern NETRC = Pattern.compile("(\\S+)"); //$NON-NLS-1$
  62. /**
  63. * 'default' netrc entry. This is the same as machine name except that
  64. * default matches any name. There can be only one default token, and it
  65. * must be after all machine tokens.
  66. */
  67. static final String DEFAULT_ENTRY = "default"; //$NON-NLS-1$
  68. /**
  69. * .netrc file entry
  70. */
  71. public static class NetRCEntry {
  72. /**
  73. * login netrc entry
  74. */
  75. public String login;
  76. /**
  77. * password netrc entry
  78. */
  79. public char[] password;
  80. /**
  81. * machine netrc entry
  82. */
  83. public String machine;
  84. /**
  85. * account netrc entry
  86. */
  87. public String account;
  88. /**
  89. * macdef netrc entry. Defines a macro. This token functions like the
  90. * ftp macdef command functions. A macro is defined with the specified
  91. * name; its contents begins with the next .netrc line and continues
  92. * until a null line (consecutive new-line characters) is encountered.
  93. * If a macro named init is defined, it is automatically executed as the
  94. * last step in the auto-login process.
  95. */
  96. public String macdef;
  97. /**
  98. * macro script body of macdef entry.
  99. */
  100. public String macbody;
  101. /**
  102. * Default constructor
  103. */
  104. public NetRCEntry() {
  105. }
  106. boolean complete() {
  107. return login != null && password != null && machine != null;
  108. }
  109. }
  110. private File netrc;
  111. private long lastModified;
  112. private Map<String, NetRCEntry> hosts = new HashMap<>();
  113. private static final TreeMap<String, State> STATE = new TreeMap<String, NetRC.State>() {
  114. private static final long serialVersionUID = -4285910831814853334L;
  115. {
  116. put("machine", State.MACHINE); //$NON-NLS-1$
  117. put("login", State.LOGIN); //$NON-NLS-1$
  118. put("password", State.PASSWORD); //$NON-NLS-1$
  119. put(DEFAULT_ENTRY, State.DEFAULT);
  120. put("account", State.ACCOUNT); //$NON-NLS-1$
  121. put("macdef", State.MACDEF); //$NON-NLS-1$
  122. }
  123. };
  124. enum State {
  125. COMMAND, MACHINE, LOGIN, PASSWORD, DEFAULT, ACCOUNT, MACDEF
  126. }
  127. /**
  128. * <p>Constructor for NetRC.</p>
  129. */
  130. public NetRC() {
  131. netrc = getDefaultFile();
  132. if (netrc != null)
  133. parse();
  134. }
  135. /**
  136. * <p>Constructor for NetRC.</p>
  137. *
  138. * @param netrc
  139. * the .netrc file
  140. */
  141. public NetRC(File netrc) {
  142. this.netrc = netrc;
  143. parse();
  144. }
  145. private static File getDefaultFile() {
  146. File home = FS.DETECTED.userHome();
  147. File netrc = new File(home, ".netrc"); //$NON-NLS-1$
  148. if (netrc.exists())
  149. return netrc;
  150. netrc = new File(home, "_netrc"); //$NON-NLS-1$
  151. if (netrc.exists())
  152. return netrc;
  153. return null;
  154. }
  155. /**
  156. * Get entry by host name
  157. *
  158. * @param host
  159. * the host name
  160. * @return entry associated with host name or null
  161. */
  162. public NetRCEntry getEntry(String host) {
  163. if (netrc == null)
  164. return null;
  165. if (this.lastModified != this.netrc.lastModified())
  166. parse();
  167. NetRCEntry entry = this.hosts.get(host);
  168. if (entry == null)
  169. entry = this.hosts.get(DEFAULT_ENTRY);
  170. return entry;
  171. }
  172. /**
  173. * Get all entries collected from .netrc file
  174. *
  175. * @return all entries collected from .netrc file
  176. */
  177. public Collection<NetRCEntry> getEntries() {
  178. return hosts.values();
  179. }
  180. private void parse() {
  181. this.hosts.clear();
  182. this.lastModified = this.netrc.lastModified();
  183. BufferedReader r = null;
  184. try {
  185. r = new BufferedReader(new FileReader(netrc));
  186. String line = null;
  187. NetRCEntry entry = new NetRCEntry();
  188. State state = State.COMMAND;
  189. String macbody = ""; //$NON-NLS-1$
  190. Matcher matcher = NETRC.matcher(""); //$NON-NLS-1$
  191. while ((line = r.readLine()) != null) {
  192. // reading macbody
  193. if (entry.macdef != null && entry.macbody == null) {
  194. if (line.length() == 0) {
  195. entry.macbody = macbody;
  196. macbody = ""; //$NON-NLS-1$
  197. continue;
  198. }
  199. macbody += line + "\n"; //$NON-NLS-1$;
  200. continue;
  201. }
  202. matcher.reset(line);
  203. while (matcher.find()) {
  204. String command = matcher.group().toLowerCase(Locale.ROOT);
  205. if (command.startsWith("#")) { //$NON-NLS-1$
  206. matcher.reset(""); //$NON-NLS-1$
  207. continue;
  208. }
  209. state = STATE.get(command);
  210. if (state == null)
  211. state = State.COMMAND;
  212. switch (state) {
  213. case COMMAND:
  214. break;
  215. case ACCOUNT:
  216. if (entry.account != null && entry.complete()) {
  217. hosts.put(entry.machine, entry);
  218. entry = new NetRCEntry();
  219. }
  220. if (matcher.find())
  221. entry.account = matcher.group();
  222. state = State.COMMAND;
  223. break;
  224. case LOGIN:
  225. if (entry.login != null && entry.complete()) {
  226. hosts.put(entry.machine, entry);
  227. entry = new NetRCEntry();
  228. }
  229. if (matcher.find())
  230. entry.login = matcher.group();
  231. state = State.COMMAND;
  232. break;
  233. case PASSWORD:
  234. if (entry.password != null && entry.complete()) {
  235. hosts.put(entry.machine, entry);
  236. entry = new NetRCEntry();
  237. }
  238. if (matcher.find())
  239. entry.password = matcher.group().toCharArray();
  240. state = State.COMMAND;
  241. break;
  242. case DEFAULT:
  243. if (entry.machine != null && entry.complete()) {
  244. hosts.put(entry.machine, entry);
  245. entry = new NetRCEntry();
  246. }
  247. entry.machine = DEFAULT_ENTRY;
  248. state = State.COMMAND;
  249. break;
  250. case MACDEF:
  251. if (entry.macdef != null && entry.complete()) {
  252. hosts.put(entry.machine, entry);
  253. entry = new NetRCEntry();
  254. }
  255. if (matcher.find())
  256. entry.macdef = matcher.group();
  257. state = State.COMMAND;
  258. break;
  259. case MACHINE:
  260. if (entry.machine != null && entry.complete()) {
  261. hosts.put(entry.machine, entry);
  262. entry = new NetRCEntry();
  263. }
  264. if (matcher.find())
  265. entry.machine = matcher.group();
  266. state = State.COMMAND;
  267. break;
  268. }
  269. }
  270. }
  271. // reading macbody on EOF
  272. if (entry.macdef != null && entry.macbody == null)
  273. entry.macbody = macbody;
  274. if (entry.complete())
  275. hosts.put(entry.machine, entry);
  276. } catch (IOException e) {
  277. throw new RuntimeException(e);
  278. } finally {
  279. try {
  280. if (r != null)
  281. r.close();
  282. } catch (IOException e) {
  283. throw new RuntimeException(e);
  284. }
  285. }
  286. }
  287. }