001: /*
002: * Netrc.java
003: *
004: * Copyright (C) 1998-2003 Peter Graves
005: * $Id: Netrc.java,v 1.2 2003/06/29 00:19:34 piso Exp $
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: */
021:
022: package org.armedbear.j;
023:
024: import java.io.InputStream;
025: import java.io.IOException;
026: import java.util.Vector;
027: import java.util.StringTokenizer;
028:
029: public final class Netrc {
030: private static Vector logins;
031: private static long lastModified;
032:
033: public static Login getLogin(String host) {
034: if (host == null)
035: return null;
036: parseNetrc();
037: if (logins == null)
038: return null;
039: final int limit = logins.size();
040: for (int i = 0; i < limit; i++) {
041: Login login = (Login) logins.get(i);
042: if (host.equals(login.host))
043: return login;
044: }
045: return null;
046: }
047:
048: public static String getPassword(String host, String user) {
049: if (host == null)
050: return null;
051: parseNetrc();
052: if (logins == null)
053: return null;
054: final int limit = logins.size();
055: for (int i = 0; i < limit; i++) {
056: Login login = (Login) logins.get(i);
057: if (host.equals(login.host)) {
058: if (user == null || user.equals(login.user))
059: return login.password;
060: }
061: }
062: return null;
063: }
064:
065: // Returns Vector of Login objects.
066: private static void parseNetrc() {
067: File file = File.getInstance(
068: Directories.getUserHomeDirectory(), ".netrc");
069: if (!file.isFile() || !file.canRead()) {
070: logins = null; // Nuke old cache, if any.
071: return;
072: }
073: // File exists and is readable.
074: if (logins != null) {
075: // We have a cache.
076: if (file.lastModified() == lastModified)
077: return; // File hasn't changed.
078: // Cache is stale.
079: logins = null;
080: }
081: try {
082: lastModified = file.lastModified();
083: int length = (int) file.length();
084: byte bytes[] = new byte[length];
085: InputStream in = file.getInputStream();
086: if (in.read(bytes) != length)
087: return;
088: in.close();
089: String s = new String(bytes);
090: StringTokenizer st = new StringTokenizer(s);
091: String host = null;
092: String user = null;
093: String password = null;
094: logins = new Vector();
095: while (st.hasMoreTokens()) {
096: String token = st.nextToken();
097: if (token.equals("machine")) {
098: // Add current entry to vector.
099: if (host != null && user != null
100: && password != null)
101: logins.add(new Login(host, user, password));
102: // Start new entry.
103: host = st.nextToken();
104: user = null;
105: password = null;
106: } else if (token.equals("login")) {
107: user = st.nextToken();
108: } else if (token.equals("password"))
109: password = st.nextToken();
110: }
111: // Add final entry to vector.
112: if (host != null && user != null && password != null)
113: logins.add(new Login(host, user, password));
114: } catch (IOException e) {
115: Log.error(e);
116: }
117: if (logins.size() == 0)
118: logins = null;
119: }
120: }
|