/*## (c) SECURITY EXPLORATIONS    2018 poland                                #*/
/*##     http://www.security-explorations.com                                #*/

/* THIS SOFTWARE IS PROTECTED BY DOMESTIC AND INTERNATIONAL COPYRIGHT LAWS    */
/* UNAUTHORISED COPYING OF THIS SOFTWARE IN EITHER SOURCE OR BINARY FORM IS   */
/* EXPRESSLY FORBIDDEN. ANY USE, INCLUDING THE REPRODUCTION, MODIFICATION,    */
/* DISTRIBUTION, TRANSMISSION, RE-PUBLICATION, STORAGE OR DISPLAY OF ANY      */
/* PART OF THE SOFTWARE, FOR COMMERCIAL OR ANY OTHER PURPOSES REQUIRES A      */
/* VALID LICENSE FROM THE COPYRIGHT HOLDER.                                   */

/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS    */
/* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,*/
/* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL    */
/* SECURITY EXPLORATIONS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, */
/* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF  */
/* OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE     */
/* SOFTWARE.                                                                  */

import java.lang.*;
import java.util.*;
import java.io.*;

public class EtcPasswd {
 public static final String ETC_PASSWD = "/etc/passwd";
 public static final String ETC_GROUP  = "/etc/group";

 static Hashtable users;
 static Hashtable groups;

 private static void parse_file(String filename,Hashtable tab) {
  try {
   int fd=Syscall.open(filename,0);

   if (fd>0) {
    int size=FS.sys_filesize(filename);
    
    if (size>0) {
     NativeCode nc=NativeCode.getInstance();
     int buf=nc.malloc(size);

     FS.sys_read_fully(fd,buf,size);

     byte data[]=Utils.buf2barray(buf,size);

     nc.free(buf);

     ByteArrayInputStream in=new ByteArrayInputStream(data);
     BufferedReader b=new BufferedReader(new InputStreamReader(in,"utf-8"));

     String line=b.readLine();

     while(line!=null) {
      int pos=line.indexOf(':');

      if (pos>0) {
       String name=line.substring(0,pos);

       line=line.substring(pos+1,line.length());

       pos=line.indexOf(':');

       if (pos>=0) {
        line=line.substring(pos+1,line.length());

        pos=line.indexOf(':');

        if (pos>0) {
         String val=line.substring(0,pos);
         int v=Integer.parseInt(val);

         tab.put(new Integer(val),name);
        }
       }
      }

      line=b.readLine();
     }
    
     b.close();
     in.close();
    }
    
    Syscall.close(fd);
   }
  } catch(Throwable t) {}
 }

 static {
  users=new Hashtable();
  groups=new Hashtable();

  parse_file(ETC_PASSWD,users);
  parse_file(ETC_GROUP,groups);
 }

 public static String username(int uid) {
  String name=(String)users.get(new Integer(uid));

  if (name!=null) return name;
   else return ""+uid;
 }

 public static String groupname(int gid) {
  String name=(String)groups.get(new Integer(gid));

  if (name!=null) return name;
   else return ""+gid;
 }

 private static int reverse_lookup(Hashtable h,String name) {
  for(Enumeration e=h.keys();e.hasMoreElements();) {
   Integer id=(Integer)e.nextElement();
   String val=(String)h.get(id);

   if (val.equals(name)) return id.intValue();
  }

  return -1;
 }

 public static int userid(String username) {
  return reverse_lookup(users,username);
 }

 public static int groupid(String groupname) {
  return reverse_lookup(groups,groupname);
 }

}
