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.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
61
62
63
64 public class NetRC {
65 static final Pattern NETRC = Pattern.compile("(\\S+)");
66
67
68
69
70
71
72 static final String DEFAULT_ENTRY = "default";
73
74
75
76
77 public static class NetRCEntry {
78
79
80
81 public String login;
82
83
84
85
86 public char[] password;
87
88
89
90
91 public String machine;
92
93
94
95
96 public String account;
97
98
99
100
101
102
103
104
105
106 public String macdef;
107
108
109
110
111 public String macbody;
112
113
114
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);
134 put("login", State.LOGIN);
135 put("password", State.PASSWORD);
136 put(DEFAULT_ENTRY, State.DEFAULT);
137 put("account", State.ACCOUNT);
138 put("macdef", State.MACDEF);
139 }
140 };
141
142 enum State {
143 COMMAND, MACHINE, LOGIN, PASSWORD, DEFAULT, ACCOUNT, MACDEF
144 }
145
146
147
148
149 public NetRC() {
150 netrc = getDefaultFile();
151 if (netrc != null)
152 parse();
153 }
154
155
156
157
158
159
160
161 public NetRC(File netrc) {
162 this.netrc = netrc;
163 parse();
164 }
165
166 private static File getDefaultFile() {
167 File home = FS.DETECTED.userHome();
168 File netrc = new File(home, ".netrc");
169 if (netrc.exists())
170 return netrc;
171
172 netrc = new File(home, "_netrc");
173 if (netrc.exists())
174 return netrc;
175
176 return null;
177 }
178
179
180
181
182
183
184
185
186 public NetRCEntry getEntry(String host) {
187 if (netrc == null)
188 return null;
189
190 if (this.lastModified != this.netrc.lastModified())
191 parse();
192
193 NetRCEntry entry = this.hosts.get(host);
194
195 if (entry == null)
196 entry = this.hosts.get(DEFAULT_ENTRY);
197
198 return entry;
199 }
200
201
202
203
204
205
206 public Collection<NetRCEntry> getEntries() {
207 return hosts.values();
208 }
209
210 private void parse() {
211 this.hosts.clear();
212 this.lastModified = this.netrc.lastModified();
213
214 BufferedReader r = null;
215 try {
216 r = new BufferedReader(new FileReader(netrc));
217 String line = null;
218
219 NetRCEntry entry = new NetRCEntry();
220
221 State state = State.COMMAND;
222
223 String macbody = "";
224
225 Matcher matcher = NETRC.matcher("");
226 while ((line = r.readLine()) != null) {
227
228
229 if (entry.macdef != null && entry.macbody == null) {
230 if (line.length() == 0) {
231 entry.macbody = macbody;
232 macbody = "";
233 continue;
234 }
235 macbody += line + "\n";
236 continue;
237 }
238
239 matcher.reset(line);
240 while (matcher.find()) {
241 String command = matcher.group().toLowerCase(Locale.ROOT);
242 if (command.startsWith("#")) {
243 matcher.reset("");
244 continue;
245 }
246 state = STATE.get(command);
247 if (state == null)
248 state = State.COMMAND;
249
250 switch (state) {
251 case COMMAND:
252 break;
253 case ACCOUNT:
254 if (entry.account != null && entry.complete()) {
255 hosts.put(entry.machine, entry);
256 entry = new NetRCEntry();
257 }
258 if (matcher.find())
259 entry.account = matcher.group();
260 state = State.COMMAND;
261 break;
262 case LOGIN:
263 if (entry.login != null && entry.complete()) {
264 hosts.put(entry.machine, entry);
265 entry = new NetRCEntry();
266 }
267 if (matcher.find())
268 entry.login = matcher.group();
269 state = State.COMMAND;
270 break;
271 case PASSWORD:
272 if (entry.password != null && entry.complete()) {
273 hosts.put(entry.machine, entry);
274 entry = new NetRCEntry();
275 }
276 if (matcher.find())
277 entry.password = matcher.group().toCharArray();
278 state = State.COMMAND;
279 break;
280 case DEFAULT:
281 if (entry.machine != null && entry.complete()) {
282 hosts.put(entry.machine, entry);
283 entry = new NetRCEntry();
284 }
285 entry.machine = DEFAULT_ENTRY;
286 state = State.COMMAND;
287 break;
288 case MACDEF:
289 if (entry.macdef != null && entry.complete()) {
290 hosts.put(entry.machine, entry);
291 entry = new NetRCEntry();
292 }
293 if (matcher.find())
294 entry.macdef = matcher.group();
295 state = State.COMMAND;
296 break;
297 case MACHINE:
298 if (entry.machine != null && entry.complete()) {
299 hosts.put(entry.machine, entry);
300 entry = new NetRCEntry();
301 }
302 if (matcher.find())
303 entry.machine = matcher.group();
304 state = State.COMMAND;
305 break;
306 }
307 }
308 }
309
310
311 if (entry.macdef != null && entry.macbody == null)
312 entry.macbody = macbody;
313
314 if (entry.complete())
315 hosts.put(entry.machine, entry);
316 } catch (IOException e) {
317 throw new RuntimeException(e);
318 } finally {
319 try {
320 if (r != null)
321 r.close();
322 } catch (IOException e) {
323 throw new RuntimeException(e);
324 }
325 }
326 }
327 }