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