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