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.1KB

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