Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

NetRC.java 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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<String, NetRCEntry>();
  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. public NetRC() {
  129. netrc = getDefaultFile();
  130. if (netrc != null)
  131. parse();
  132. }
  133. /**
  134. * @param netrc
  135. * the .netrc file
  136. */
  137. public NetRC(File netrc) {
  138. this.netrc = netrc;
  139. parse();
  140. }
  141. private static File getDefaultFile() {
  142. File home = FS.DETECTED.userHome();
  143. File netrc = new File(home, ".netrc"); //$NON-NLS-1$
  144. if (netrc.exists())
  145. return netrc;
  146. netrc = new File(home, "_netrc"); //$NON-NLS-1$
  147. if (netrc.exists())
  148. return netrc;
  149. return null;
  150. }
  151. /**
  152. * Get entry by host name
  153. *
  154. * @param host
  155. * @return entry associated with host name or null
  156. */
  157. public NetRCEntry getEntry(String host) {
  158. if (netrc == null)
  159. return null;
  160. if (this.lastModified != this.netrc.lastModified())
  161. parse();
  162. NetRCEntry entry = this.hosts.get(host);
  163. if (entry == null)
  164. entry = this.hosts.get(DEFAULT_ENTRY);
  165. return entry;
  166. }
  167. /**
  168. * @return all entries collected from .netrc file
  169. */
  170. public Collection<NetRCEntry> getEntries() {
  171. return hosts.values();
  172. }
  173. private void parse() {
  174. this.hosts.clear();
  175. this.lastModified = this.netrc.lastModified();
  176. BufferedReader r = null;
  177. try {
  178. r = new BufferedReader(new FileReader(netrc));
  179. String line = null;
  180. NetRCEntry entry = new NetRCEntry();
  181. State state = State.COMMAND;
  182. String macbody = ""; //$NON-NLS-1$
  183. Matcher matcher = NETRC.matcher(""); //$NON-NLS-1$
  184. while ((line = r.readLine()) != null) {
  185. // reading macbody
  186. if (entry.macdef != null && entry.macbody == null) {
  187. if (line.length() == 0) {
  188. entry.macbody = macbody;
  189. macbody = ""; //$NON-NLS-1$
  190. continue;
  191. }
  192. macbody += line + "\n"; //$NON-NLS-1$;
  193. continue;
  194. }
  195. matcher.reset(line);
  196. while (matcher.find()) {
  197. String command = matcher.group().toLowerCase(Locale.ROOT);
  198. if (command.startsWith("#")) { //$NON-NLS-1$
  199. matcher.reset(""); //$NON-NLS-1$
  200. continue;
  201. }
  202. state = STATE.get(command);
  203. if (state == null)
  204. state = State.COMMAND;
  205. switch (state) {
  206. case COMMAND:
  207. break;
  208. case ACCOUNT:
  209. if (entry.account != null && entry.complete()) {
  210. hosts.put(entry.machine, entry);
  211. entry = new NetRCEntry();
  212. }
  213. if (matcher.find())
  214. entry.account = matcher.group();
  215. state = State.COMMAND;
  216. break;
  217. case LOGIN:
  218. if (entry.login != null && entry.complete()) {
  219. hosts.put(entry.machine, entry);
  220. entry = new NetRCEntry();
  221. }
  222. if (matcher.find())
  223. entry.login = matcher.group();
  224. state = State.COMMAND;
  225. break;
  226. case PASSWORD:
  227. if (entry.password != null && entry.complete()) {
  228. hosts.put(entry.machine, entry);
  229. entry = new NetRCEntry();
  230. }
  231. if (matcher.find())
  232. entry.password = matcher.group().toCharArray();
  233. state = State.COMMAND;
  234. break;
  235. case DEFAULT:
  236. if (entry.machine != null && entry.complete()) {
  237. hosts.put(entry.machine, entry);
  238. entry = new NetRCEntry();
  239. }
  240. entry.machine = DEFAULT_ENTRY;
  241. state = State.COMMAND;
  242. break;
  243. case MACDEF:
  244. if (entry.macdef != null && entry.complete()) {
  245. hosts.put(entry.machine, entry);
  246. entry = new NetRCEntry();
  247. }
  248. if (matcher.find())
  249. entry.macdef = matcher.group();
  250. state = State.COMMAND;
  251. break;
  252. case MACHINE:
  253. if (entry.machine != null && entry.complete()) {
  254. hosts.put(entry.machine, entry);
  255. entry = new NetRCEntry();
  256. }
  257. if (matcher.find())
  258. entry.machine = matcher.group();
  259. state = State.COMMAND;
  260. break;
  261. }
  262. }
  263. }
  264. // reading macbody on EOF
  265. if (entry.macdef != null && entry.macbody == null)
  266. entry.macbody = macbody;
  267. if (entry.complete())
  268. hosts.put(entry.machine, entry);
  269. } catch (IOException e) {
  270. throw new RuntimeException(e);
  271. } finally {
  272. try {
  273. if (r != null)
  274. r.close();
  275. } catch (IOException e) {
  276. throw new RuntimeException(e);
  277. }
  278. }
  279. }
  280. }