001: // Unix.java
002: // //$Id: Unix.java,v 2.0 1998/08/13 16:27:03 bmahe Exp $
003: // (c) COPYRIGHT MIT and INRIA, 1996.
004: // Please first read the full copyright statement in file COPYRIGHT.html
005:
006: // modified by gisburn (Roland.Mainz@informatik.med.uni-giessen.de)
007: // to work under java 2 (implementing JNI 1.1)
008: //
009: // TODO: - Selection of shared library name based on OS/CPU type
010: // (using System.getProperties)
011: // to allow jigsaw to choose libUnix.linux386.so or
012: // libUnix.solarissparc.so without any changes of library names.
013: // - support for error:
014: // - getErrNo (returns the static field errno, which is set by native
015: // methods)
016: // - getErrNoString returns a String describing the problem in words...
017: // - What about "wrapper"-classes for UID, GID codes, e.g.
018: // java-euiv of uid_t and gid_t ?
019:
020: package org.w3c.util;
021:
022: /**
023: * Native methods to do some UNIX specific system calls.
024: * This class can be used on UNIX variants to access some specific system
025: * calls.
026: */
027:
028: public class Unix {
029: private static final String NATIVE_LIBRARY = "Unix";
030: /**
031: * Are the calls we support really availables ?
032: */
033: private static boolean haslibrary = false;
034: private static Unix that = null;
035:
036: private native int libunix_getUID(String user);
037:
038: private native int libunix_getGID(String group);
039:
040: private native boolean libunix_setUID(int uid);
041:
042: private native boolean libunix_setGID(int gid);
043:
044: private native boolean libunix_chRoot(String root);
045:
046: /**
047: * Get the UNIX system call manger.
048: * @return An instance of this class, suitable to call UNIX system
049: * calls.
050: */
051:
052: public static synchronized Unix getUnix() {
053: if (that == null) {
054: // Load the library:
055: try {
056: System.loadLibrary(NATIVE_LIBRARY);
057: haslibrary = true;
058: } catch (UnsatisfiedLinkError ex) {
059: haslibrary = false;
060: } catch (Exception ex) {
061: haslibrary = false;
062: }
063:
064: // Create the only instance:
065: that = new Unix();
066: }
067:
068: return (that);
069: }
070:
071: /**
072: * Can I perform UNIX system calls through that instance ?
073: * @return A boolean, <strong>true</strong> if these system calls are
074: * allowed, <strong>false</strong> otherwise.
075: */
076:
077: public boolean isResolved() {
078: return (haslibrary);
079: }
080:
081: /**
082: * Get the user identifier for that user.
083: * @return The user's identifier, or <strong>-1</strong> if user was not
084: * found.
085: */
086:
087: public int getUID(String uname) {
088: // FIXME: Security check needed here
089: if (uname == null)
090: return (-1);
091:
092: return (libunix_getUID(uname));
093: }
094:
095: /**
096: * Get the group identifier for that group.
097: * @return The group identifier, or <strong>-1</strong> if not found.
098: */
099:
100: public int getGID(String gname) {
101: // FIXME: Security check needed
102: if (gname == null)
103: return (-1);
104:
105: return (libunix_getGID(gname));
106: }
107:
108: /**
109: * Set the user id for the running process.
110: * @param uid The new user identifier for the process.
111: * @exception UnixException If failed.
112: */
113:
114: public void setUID(int uid) throws UnixException {
115: // FIXME: Security check needed
116: if (!libunix_setUID(uid))
117: throw new UnixException("setuid failed");
118: }
119:
120: /**
121: * Set the group id for the running process.
122: * @param gid The new user identifier for the process.
123: * @exception UnixException If failed.
124: */
125:
126: public void setGID(int gid) throws UnixException {
127: // FIXME: Security check needed
128: if (!libunix_setGID(gid))
129: throw new UnixException("setgid failed");
130: }
131:
132: /**
133: * Change the process root, using <code>chroot</code> system call.
134: * @param root The new root for the process.
135: * @exception UnixException If failed.
136: */
137:
138: public void chroot(String root) throws UnixException {
139: if (root == null)
140: throw new NullPointerException("chroot: root == null");
141:
142: if (!libunix_chRoot(root))
143: throw new UnixException("chroot failed");
144: }
145: }
|