Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using JPMS and jna start module. #20

Merged
merged 2 commits into from
Mar 28, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Code cleanups.
Improved javadocs.
Fixed module-info.java.

Signed-off-by: Simone Bordet <[email protected]>
sbordet committed Mar 28, 2024
commit c0f8e791556d57b6732f828b4c9ec4101d6901cf
3 changes: 0 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -10,6 +10,3 @@ $ java -jar /opt/jetty-home/start.jar --add-module=setuid
```

Then configure the userid you want in the `${jetty.base}/start.d/setuid.ini` file



1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -57,6 +57,7 @@
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<compilerArgument>-Xlint:all</compilerArgument>
<testCompilerArgument>-nowarn</testCompilerArgument>
</configuration>
</plugin>
</plugins>
9 changes: 3 additions & 6 deletions src/main/java/module-info.java
Original file line number Diff line number Diff line change
@@ -13,13 +13,10 @@

module org.eclipse.jetty.setuid.jna
{
requires com.sun.jna;
requires transitive com.sun.jna;
requires org.eclipse.jetty.server;
requires org.eclipse.jetty.util;
requires transitive org.eclipse.jetty.util;
requires org.slf4j;

// needed to allow internal classes to use com.sun.jna
opens org.eclipse.jetty.setuid.internal;

exports org.eclipse.jetty.setuid;
}
}
Original file line number Diff line number Diff line change
@@ -11,20 +11,22 @@
// ========================================================================
//

package org.eclipse.jetty.setuid.internal;
package org.eclipse.jetty.setuid;

import com.sun.jna.Pointer;
import com.sun.jna.Structure;

/**
* Class is the equivalent java class used for holding values from native c code structure group. for more information please see man pages for getgrnam and getgrgid
* <p>Class is the equivalent java class used for holding values from native c code structure group.</p>
* <p>For more information please see man pages for {@code getgrnam()} and {@code getgrgid()}.</p>
* <pre>{@code
* struct group {
* char *gr_name; // group name
* char *gr_passwd; // group password
* gid_t gr_gid; // group ID
* char **gr_mem; // group members
* };
*
* char *gr_name; // group name
* char *gr_passwd; // group password
* gid_t gr_gid; // group ID
* char **gr_mem; // group members
* };
* }</pre>
*/
@Structure.FieldOrder({"_grName", "_grPasswd", "_grGid", "_grMem"})
public class Group extends Structure
@@ -34,6 +36,9 @@ public class Group extends Structure
public int _grGid; /* group id */
public Pointer _grMem; /* group members */

public Group()
{
}

public String getGrName()
{
Original file line number Diff line number Diff line change
@@ -11,22 +11,24 @@
// ========================================================================
//

package org.eclipse.jetty.setuid.internal;
package org.eclipse.jetty.setuid;

import com.sun.jna.Structure;

/**
* Class is the equivalent java class used for holding values from native c code structure passwd. for more information please see man pages for getpwuid and getpwnam
* struct passwd {
* char *pw_name; // user name
* char *pw_passwd; // user password
* uid_t pw_uid; // user id
* gid_t pw_gid; // group id
* char *pw_gecos; // real name
* char *pw_dir; // home directory
* char *pw_shell; // shell program
* };
*
* <p>Class is the equivalent java class used for holding values from native c code structure passwd.</p>
* <p>For more information please see man pages for {@code getpwuid()} and {@code getpwnam()}</p>
* <pre>{@code
* struct passwd {
* char *pw_name; // user name
* char *pw_passwd; // user password
* uid_t pw_uid; // user id
* gid_t pw_gid; // group id
* char *pw_gecos; // real name
* char *pw_dir; // home directory
* char *pw_shell; // shell program
* };
* }</pre>
*/
@Structure.FieldOrder({"_pwName", "_pwPasswd", "_pwUid", "_pwGid", "_pwGecos", "_pwDir", "_pwShell"})
public class Passwd extends Structure
@@ -39,6 +41,10 @@ public class Passwd extends Structure
public String _pwDir; /* home directory */
public String _pwShell; /* shell program */

public Passwd()
{
}

public String getPwName()
{
return _pwName;
10 changes: 9 additions & 1 deletion src/main/java/org/eclipse/jetty/setuid/RLimit.java
Original file line number Diff line number Diff line change
@@ -21,6 +21,15 @@ public class RLimit extends Structure
public long _soft;
public long _hard;

public RLimit()
{
}

public RLimit(long _soft, long _hard)
{
this._soft = _soft;
this._hard = _hard;
}

public long getSoft ()
{
@@ -46,5 +55,4 @@ public String toString()
{
return "rlimit_nofiles (soft="+_soft+", hard="+_hard+")";
}

}
6 changes: 4 additions & 2 deletions src/main/java/org/eclipse/jetty/setuid/SetUIDListener.java
Original file line number Diff line number Diff line change
@@ -16,9 +16,7 @@
import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.NetworkConnector;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.setuid.internal.Group;
import org.eclipse.jetty.setuid.internal.LibC;
import org.eclipse.jetty.setuid.internal.Passwd;
import org.eclipse.jetty.util.component.LifeCycle;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -47,6 +45,10 @@ public class SetUIDListener implements LifeCycle.Listener
private boolean _clearSupplementalGroups;
private RLimit _rlimitNoFiles = null;

public SetUIDListener()
{
}

public void setUsername(String username)
{
Passwd passwd = LibC.INSTANCE.getpwnam(username);
16 changes: 9 additions & 7 deletions src/main/java/org/eclipse/jetty/setuid/internal/LibC.java
Original file line number Diff line number Diff line change
@@ -16,11 +16,13 @@
import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.Platform;
import org.eclipse.jetty.setuid.Group;
import org.eclipse.jetty.setuid.Passwd;
import org.eclipse.jetty.setuid.RLimit;

/**
* 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
* <p>Class is for changing user and groupId, it can also be used to retrieve user information
* by using {@code getpwuid(uid)} or {@code getpwnam(username)} of both linux and unix systems.</p>
*/
public interface LibC extends Library
{
@@ -47,17 +49,17 @@ public interface LibC extends Library
int setrlimit(int resource, RLimit rlimit);

/**
* Compile and run the following C program to get the <code>RLIMIT_NOFILE</code> value of you OS of choice.
* <pre>
* #include &lt;stdio.h&gt;
* #include &lt;sys/resource.h&gt;
* <p>Compile and run the following C program to get the {@code RLIMIT_NOFILE} value of you OS of choice.</p>
* <pre>{@code
* #include <stdio.h>
* #include <sys/resource.h>
*
* int main()
* {
* printf("RLIMIT_NOFILE = %d\n", RLIMIT_NOFILE);
* return 0;
* }
* </pre>
* }</pre>
*/
class Constants
{
Original file line number Diff line number Diff line change
@@ -18,16 +18,14 @@
import java.nio.file.attribute.PosixFilePermission;
import java.util.Set;

import org.eclipse.jetty.setuid.internal.Group;
import org.eclipse.jetty.setuid.internal.LibC;
import org.eclipse.jetty.setuid.internal.Passwd;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class TestLibC
public class LibCTest
{
@Test
public void testSetuid() throws Exception
@@ -58,7 +56,7 @@ public void testSetuid() throws Exception

// get the group using the roots groupid
Group gr1 = LibC.INSTANCE.getgrgid(passwd1.getPwGid());
// get the group name using the aquired name
// get the group name using the acquired name
Group gr2 = LibC.INSTANCE.getgrnam(gr1.getGrName());

assertEquals(gr1.getGrName(), gr2.getGrName());