1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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
60
61
62
63 public class NetRC {
64 static final Pattern NETRC = Pattern.compile("(\\S+)");
65
66
67
68
69
70
71 static final String DEFAULT_ENTRY = "default";
72
73
74
75
76 public static class NetRCEntry {
77
78
79
80 public String login;
81
82
83
84
85 public char[] password;
86
87
88
89
90 public String machine;
91
92
93
94
95 public String account;
96
97
98
99
100
101
102
103
104
105 public String macdef;
106
107
108
109
110 public String macbody;
111
112
113
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);
133 put("login", State.LOGIN);
134 put("password", State.PASSWORD);
135 put(DEFAULT_ENTRY, State.DEFAULT);
136 put("account", State.ACCOUNT);
137 put("macdef", State.MACDEF);
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
154
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");
164 if (netrc.exists())
165 return netrc;
166
167 netrc = new File(home, "_netrc");
168 if (netrc.exists())
169 return netrc;
170
171 return null;
172 }
173
174
175
176
177
178
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
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 = "";
216
217 Matcher matcher = NETRC.matcher("");
218 while ((line = r.readLine()) != null) {
219
220
221 if (entry.macdef != null && entry.macbody == null) {
222 if (line.length() == 0) {
223 entry.macbody = macbody;
224 macbody = "";
225 continue;
226 }
227 macbody += line + "\n";
228 continue;
229 }
230
231 matcher.reset(line);
232 while (matcher.find()) {
233 String command = matcher.group().toLowerCase();
234 if (command.startsWith("#")) {
235 matcher.reset("");
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
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 }