Line | Hits | Source |
---|---|---|
1 | package umich.cac.util; | |
2 | ||
3 | import java.io.BufferedReader; | |
4 | import java.io.File; | |
5 | import java.io.FileNotFoundException; | |
6 | import java.io.FileReader; | |
7 | import java.io.IOException; | |
8 | import java.util.Vector; | |
9 | import java.util.logging.Logger; | |
10 | ||
11 | /** | |
12 | * A class that reads a UNIX style password file so we can obtain the UNIX uid | |
13 | * for a given user for PBS accounting | |
14 | * | |
15 | * @author rmach@umich.edu | |
16 | * @Version $Header: /cvsroot/pbsaccounting/pbsaccounting/src/umich/cac/util/Passwd.java,v 1.2 2003/10/21 19:08:00 rodmach Exp $ | |
17 | */ | |
18 | public class Passwd { | |
19 | ||
20 | /** If UID not found, the default value is NOT_FOUND */ | |
21 | 3 | public long NOT_FOUND = -1; |
22 | ||
23 | /** The default system password file */ | |
24 | 3 | private String passwdFile = "/etc/passwd"; |
25 | /** The Login ID field position */ | |
26 | 3 | private int LOGIN_ID = 0; |
27 | /** The Passwd field posistion */ | |
28 | 3 | private int PASSWORD = 1; |
29 | /** The UID field position */ | |
30 | 3 | private int UID = 2; |
31 | /** The GID field position */ | |
32 | 3 | private int GID = 3; |
33 | /** The GECOS field position */ | |
34 | 3 | private int GECOS = 4; |
35 | /** The HOME DIR field position */ | |
36 | 3 | private int HOME_DIR = 5; |
37 | /** The shell field position */ | |
38 | 3 | private int SHELL = 6; |
39 | ||
40 | /** Used for logging */ | |
41 | 6 | private Logger logger = Logger.getLogger(Passwd.class.getName()); |
42 | ||
43 | /** | |
44 | * A vector of String[] of the tokens in a unix passwd entries | |
45 | * [0] -> [LOGIN ID] [ PASSWORD] [UID] [GID] [GECOS] [HOME_DIR] [SHELL] | |
46 | * [1] -> [LOGIN ID] [ PASSWORD] [UID] [GID] [GECOS] [HOME_DIR] [SHELL] etc. etc. | |
47 | */ | |
48 | 3 | private Vector passwdVector = new Vector(); |
49 | ||
50 | /** | |
51 | * Constructor for unit testing | |
52 | * | |
53 | * @param passwdFile | |
54 | * an optional password file to use for lookups | |
55 | * @throws IOException | |
56 | * if problem reading password file | |
57 | * @throws FileNotFoundException | |
58 | * if doesn't exist | |
59 | */ | |
60 | public Passwd(String passwdFile) | |
61 | 3 | throws FileNotFoundException, IOException { |
62 | 3 | this.passwdFile = passwdFile; |
63 | ||
64 | 3 | if (!isExist(passwdFile)) { |
65 | 0 | throw new FileNotFoundException("File was not found " + passwdFile); |
66 | } | |
67 | ||
68 | 3 | readFile(); |
69 | 3 | } |
70 | ||
71 | /** | |
72 | * Constructor. By default reads "/etc/passwd" file | |
73 | * | |
74 | * @throws IOException | |
75 | * if problem reading password file | |
76 | * @throws FileNotFoundException | |
77 | * if doesn't exist | |
78 | * | |
79 | */ | |
80 | 0 | public Passwd() throws FileNotFoundException, IOException { |
81 | ||
82 | 0 | if (!isExist(passwdFile)) { |
83 | 0 | throw new FileNotFoundException("File was not found " + passwdFile); |
84 | } | |
85 | ||
86 | 0 | readFile(); |
87 | ||
88 | 0 | } |
89 | ||
90 | /** | |
91 | * Checks if passwd file exists | |
92 | * | |
93 | * @param passwd | |
94 | * file to check for | |
95 | * @return true if exists, false if not | |
96 | */ | |
97 | ||
98 | public boolean isExist(String filename) { | |
99 | 3 | File passFile = new File(filename); |
100 | ||
101 | 3 | if (passFile.exists()) { |
102 | 3 | return true; |
103 | } else { | |
104 | 0 | return false; |
105 | } | |
106 | ||
107 | } | |
108 | ||
109 | /** | |
110 | * Reads the passwd file and creates the passwdVector. If the passwd file | |
111 | * can not be read, then passwordVector is set to null | |
112 | * | |
113 | */ | |
114 | private void readFile() throws IOException { | |
115 | ||
116 | try { | |
117 | 3 | BufferedReader in = new BufferedReader(new FileReader(passwdFile)); |
118 | String str; | |
119 | ||
120 | 9 | while ((str = in.readLine()) != null) { |
121 | 6 | String[] entries = str.split(":"); |
122 | 6 | passwdVector.add(entries); |
123 | } | |
124 | 3 | in.close(); |
125 | 0 | } catch (IOException e) { |
126 | 0 | logger.throwing("readFile", "readFile", e); |
127 | 0 | throw e; |
128 | 3 | } |
129 | ||
130 | 3 | } |
131 | ||
132 | /** | |
133 | * Lookup the UID for a user given the loginId | |
134 | * | |
135 | * @param loginId | |
136 | * The login ID (entry 0 in the passwd file) | |
137 | * @return The UID for the corresponding loginId, or NOT_FOUND if not found | |
138 | */ | |
139 | public long lookupUid(String loginId) { | |
140 | ||
141 | 6 | for (int i = 0; i < passwdVector.size(); i++) { |
142 | 6 | String[] entry = (String[]) passwdVector.get(i); |
143 | 6 | if (entry[LOGIN_ID].equals(loginId)) { |
144 | 3 | Long uid = new Long(entry[UID]); |
145 | 3 | return uid.longValue(); |
146 | } | |
147 | } | |
148 | ||
149 | 0 | return NOT_FOUND; |
150 | } | |
151 | ||
152 | } |
this report was generated by version 1.0.5 of jcoverage. |
copyright © 2003, jcoverage ltd. all rights reserved. |