View Javadoc

    1   // ========================================================================
    2   // Copyright 2002-2005 Mort Bay Consulting Pty. Ltd.
    3   // ------------------------------------------------------------------------
    4   // Licensed under the Apache License, Version 2.0 (the "License");
    5   // you may not use this file except in compliance with the License.
    6   // You may obtain a copy of the License at
    7   // http://www.apache.org/licenses/LICENSE-2.0
    8   // Unless required by applicable law or agreed to in writing, software
    9   // distributed under the License is distributed on an "AS IS" BASIS,
    10  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  // ========================================================================
    14
    15  package org.mortbay.setuid;
    16
    17  import java.io.File;
    18
    19
    20  /**
    21   * Class is for changing user and groupId, it can also be use to retrieve user information by using getpwuid(uid) or getpwnam(username) of both linux and unix systems
    22   * @author Greg Wilkins
    23   * @author Leopoldo Lee Agdeppa III
    24   */
    25
    26  public class SetUID
    27  {
    28      public static final int OK = 0;
    29      public static final int ERROR = -1;
    30
    31      public static native int setumask(int mask);
    32      public static native int setuid(int uid);
    33      public static native int setgid(int gid);
    34
    35      public static native Passwd getpwnam(String name) throws SecurityException;
    36      public static native Passwd getpwuid(int uid) throws SecurityException;
    37
    38      public static native Group getgrnam(String name) throws SecurityException;
    39      public static native Group getgrgid(int gid) throws SecurityException;
    40
    41      public static native RLimit getrlimitnofiles();
    42      public static native int setrlimitnofiles(RLimit rlimit);
    43
    44      private static void loadLibrary()
    45      {
    46          // load libjettysetuid.so ${jetty.libsetuid.path}
    47          try
    48          {
    49              if(System.getProperty("jetty.libsetuid.path") != null)
    50              {
    51                  File lib = new File(System.getProperty("jetty.libsetuid.path"));
    52                  if(lib.exists())
    53                  {
    54                      System.load(lib.getCanonicalPath());
    55                  }
    56                  return;
    57              }
    58
    59          }
    60          catch (Throwable e)
    61          {
    62             //Ignorable if there is another way to find the lib
    63  	   if (Boolean.valueOf(System.getProperty("DEBUG")).booleanValue())
    64  	       e.printStackTrace();
    65          }
    66
    67          try
    68          {
    69              System.loadLibrary("setuid");
    70              return;
    71          }
    72          catch (Throwable e)
    73          {
    74             //Ignorable if ther eis another way to find the lib
    75  	   if (Boolean.valueOf(System.getProperty("DEBUG")).booleanValue())
    76  	       e.printStackTrace();
    77          }
    78
    79          // try to load from usual path @ jetty.home/lib/ext
    80          try
    81          {
    82              if(System.getProperty("jetty.home") != null)
    83              {
    84                  File lib = new File(System.getProperty("jetty.home") + "/lib/ext/libsetuid.so");
    85                  if(lib.exists())
    86                  {
    87                      System.load(lib.getCanonicalPath());
    88                  }
    89                  return;
    90              }
    91
    92          }
    93          catch (Throwable e)
    94          {
    95  	   if (Boolean.valueOf(System.getProperty("DEBUG")).booleanValue())
    96  	       e.printStackTrace();
    97          }
    98
    99          // try to load from jetty.lib where rpm puts this file
    100         try
    101         {
    102             if(System.getProperty("jetty.lib") != null)
    103             {
    104                 File lib = new File(System.getProperty("jetty.lib") + "/libsetuid.so");
    105                 if(lib.exists())
    106                 {
    107                     System.load(lib.getCanonicalPath());
    108                 }
    109                 return;
    110             }
    111
    112         }
    113         catch (Throwable e)
    114         {
    115 	   if (Boolean.valueOf(System.getProperty("DEBUG")).booleanValue())
    116 	       e.printStackTrace();
    117         }
    118
    119         System.err.println("Error: libsetuid.so could not be found");
    120     }
    121
    122
    123     static
    124     {
    125     	loadLibrary();
    126     }
    127
    128 }