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 public NetRC() {
148 netrc = getDefaultFile();
149 if (netrc != null)
150 parse();
151 }
152
153
154
155
156
157 public NetRC(File netrc) {
158 this.netrc = netrc;
159 parse();
160 }
161
162 private static File getDefaultFile() {
163 File home = FS.DETECTED.userHome();
164 File netrc = new File(home, ".netrc");
165 if (netrc.exists())
166 return netrc;
167
168 netrc = new File(home, "_netrc");
169 if (netrc.exists())
170 return netrc;
171
172 return null;
173 }
174
175
176
177
178
179
180
181 public NetRCEntry getEntry(String host) {
182 if (netrc == null)
183 return null;
184
185 if (this.lastModified != this.netrc.lastModified())
186 parse();
187
188 NetRCEntry entry = this.hosts.get(host);
189
190 if (entry == null)
191 entry = this.hosts.get(DEFAULT_ENTRY);
192
193 return entry;
194 }
195
196
197
198
199 public Collection<NetRCEntry> getEntries() {
200 return hosts.values();
201 }
202
203 private void parse() {
204 this.hosts.clear();
205 this.lastModified = this.netrc.lastModified();
206
207 BufferedReader r = null;
208 try {
209 r = new BufferedReader(new FileReader(netrc));
210 String line = null;
211
212 NetRCEntry entry = new NetRCEntry();
213
214 State state = State.COMMAND;
215
216 String macbody = "";
217
218 Matcher matcher = NETRC.matcher("");
219 while ((line = r.readLine()) != null) {
220
221
222 if (entry.macdef != null && entry.macbody == null) {
223 if (line.length() == 0) {
224 entry.macbody = macbody;
225 macbody = "";
226 continue;
227 }
228 macbody += line + "\n";
229 continue;
230 }
231
232 matcher.reset(line);
233 while (matcher.find()) {
234 String command = matcher.group().toLowerCase(Locale.ROOT);
235 if (command.startsWith("#")) {
236 matcher.reset("");
237 continue;
238 }
239 state = STATE.get(command);
240 if (state == null)
241 state = State.COMMAND;
242
243 switch (state) {
244 case COMMAND:
245 break;
246 case ACCOUNT:
247 if (entry.account != null && entry.complete()) {
248 hosts.put(entry.machine, entry);
249 entry = new NetRCEntry();
250 }
251 if (matcher.find())
252 entry.account = matcher.group();
253 state = State.COMMAND;
254 break;
255 case LOGIN:
256 if (entry.login != null && entry.complete()) {
257 hosts.put(entry.machine, entry);
258 entry = new NetRCEntry();
259 }
260 if (matcher.find())
261 entry.login = matcher.group();
262 state = State.COMMAND;
263 break;
264 case PASSWORD:
265 if (entry.password != null && entry.complete()) {
266 hosts.put(entry.machine, entry);
267 entry = new NetRCEntry();
268 }
269 if (matcher.find())
270 entry.password = matcher.group().toCharArray();
271 state = State.COMMAND;
272 break;
273 case DEFAULT:
274 if (entry.machine != null && entry.complete()) {
275 hosts.put(entry.machine, entry);
276 entry = new NetRCEntry();
277 }
278 entry.machine = DEFAULT_ENTRY;
279 state = State.COMMAND;
280 break;
281 case MACDEF:
282 if (entry.macdef != null && entry.complete()) {
283 hosts.put(entry.machine, entry);
284 entry = new NetRCEntry();
285 }
286 if (matcher.find())
287 entry.macdef = matcher.group();
288 state = State.COMMAND;
289 break;
290 case MACHINE:
291 if (entry.machine != null && entry.complete()) {
292 hosts.put(entry.machine, entry);
293 entry = new NetRCEntry();
294 }
295 if (matcher.find())
296 entry.machine = matcher.group();
297 state = State.COMMAND;
298 break;
299 }
300 }
301 }
302
303
304 if (entry.macdef != null && entry.macbody == null)
305 entry.macbody = macbody;
306
307 if (entry.complete())
308 hosts.put(entry.machine, entry);
309 } catch (IOException e) {
310 throw new RuntimeException(e);
311 } finally {
312 try {
313 if (r != null)
314 r.close();
315 } catch (IOException e) {
316 throw new RuntimeException(e);
317 }
318 }
319 }
320 }