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