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 try (BufferedReader r = new BufferedReader(new FileReader(netrc))) {
215 String line = null;
216
217 NetRCEntry entry = new NetRCEntry();
218
219 State state = State.COMMAND;
220
221 String macbody = "";
222
223 Matcher matcher = NETRC.matcher("");
224 while ((line = r.readLine()) != null) {
225
226
227 if (entry.macdef != null && entry.macbody == null) {
228 if (line.length() == 0) {
229 entry.macbody = macbody;
230 macbody = "";
231 continue;
232 }
233 macbody += line + "\n";
234 continue;
235 }
236
237 matcher.reset(line);
238 while (matcher.find()) {
239 String command = matcher.group().toLowerCase(Locale.ROOT);
240 if (command.startsWith("#")) {
241 matcher.reset("");
242 continue;
243 }
244 state = STATE.get(command);
245 if (state == null)
246 state = State.COMMAND;
247
248 switch (state) {
249 case COMMAND:
250 break;
251 case ACCOUNT:
252 if (entry.account != null && entry.complete()) {
253 hosts.put(entry.machine, entry);
254 entry = new NetRCEntry();
255 }
256 if (matcher.find())
257 entry.account = matcher.group();
258 state = State.COMMAND;
259 break;
260 case LOGIN:
261 if (entry.login != null && entry.complete()) {
262 hosts.put(entry.machine, entry);
263 entry = new NetRCEntry();
264 }
265 if (matcher.find())
266 entry.login = matcher.group();
267 state = State.COMMAND;
268 break;
269 case PASSWORD:
270 if (entry.password != null && entry.complete()) {
271 hosts.put(entry.machine, entry);
272 entry = new NetRCEntry();
273 }
274 if (matcher.find())
275 entry.password = matcher.group().toCharArray();
276 state = State.COMMAND;
277 break;
278 case DEFAULT:
279 if (entry.machine != null && entry.complete()) {
280 hosts.put(entry.machine, entry);
281 entry = new NetRCEntry();
282 }
283 entry.machine = DEFAULT_ENTRY;
284 state = State.COMMAND;
285 break;
286 case MACDEF:
287 if (entry.macdef != null && entry.complete()) {
288 hosts.put(entry.machine, entry);
289 entry = new NetRCEntry();
290 }
291 if (matcher.find())
292 entry.macdef = matcher.group();
293 state = State.COMMAND;
294 break;
295 case MACHINE:
296 if (entry.machine != null && entry.complete()) {
297 hosts.put(entry.machine, entry);
298 entry = new NetRCEntry();
299 }
300 if (matcher.find())
301 entry.machine = matcher.group();
302 state = State.COMMAND;
303 break;
304 }
305 }
306 }
307
308
309 if (entry.macdef != null && entry.macbody == null)
310 entry.macbody = macbody;
311
312 if (entry.complete())
313 hosts.put(entry.machine, entry);
314 } catch (IOException e) {
315 throw new RuntimeException(e);
316 }
317 }
318 }