Skip to content

Commit

Permalink
Merge pull request #20 from jetty/jna-module-dep
Browse files Browse the repository at this point in the history
Using JPMS and `jna` start module.
  • Loading branch information
joakime committed Mar 28, 2024
2 parents 8a89c13 + c0f8e79 commit 3eb7273
Show file tree
Hide file tree
Showing 10 changed files with 83 additions and 37 deletions.
3 changes: 0 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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



5 changes: 3 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@
<jdk.version.minimum>17</jdk.version.minimum>
<jetty-test-helper.version>6.2</jetty-test-helper.version>
<jetty.git.repo>jetty-setuid-jna</jetty.git.repo>
<jetty.version>12.0.6</jetty.version>
<jetty.version>12.0.7</jetty.version>
<jna.version>5.14.0</jna.version>
</properties>

<dependencies>
<dependency>
<groupId>net.java.dev.jna</groupId>
<artifactId>jna</artifactId>
<artifactId>jna-jpms</artifactId>
<version>${jna.version}</version>
</dependency>
<dependency>
Expand Down Expand Up @@ -57,6 +57,7 @@
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<compilerArgument>-Xlint:all</compilerArgument>
<testCompilerArgument>-nowarn</testCompilerArgument>
</configuration>
</plugin>
</plugins>
Expand Down
4 changes: 1 addition & 3 deletions src/main/config/modules/setuid.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,16 @@ changing to a restricted user (e.g. Jetty).

[depend]
server
jna

[lib]
lib/setuid/jetty-setuid-jna-${jetty-setuid.version}.jar
lib/setuid/jna-${jna.version}.jar

[ini]
jetty-setuid.version?=@project.version@
jna.version?=@jna.version@

[files]
maven://org.eclipse.jetty.toolchain.setuid/jetty-setuid-jna/${jetty-setuid.version}|lib/setuid/jetty-setuid-jna-${jetty-setuid.version}.jar
maven://net.java.dev.jna/jna/${jna.version}|lib/setuid/jna-${jna.version}.jar

[xml]
etc/jetty-setuid.xml
Expand Down
22 changes: 22 additions & 0 deletions src/main/java/module-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//
// ========================================================================
// Copyright (c) 1995 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//

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

exports org.eclipse.jetty.setuid;
}
19 changes: 12 additions & 7 deletions src/main/java/org/eclipse/jetty/setuid/Group.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,16 @@
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
Expand All @@ -34,6 +36,9 @@ public class Group extends Structure
public int _grGid; /* group id */
public Pointer _grMem; /* group members */

public Group()
{
}

public String getGrName()
{
Expand Down
28 changes: 17 additions & 11 deletions src/main/java/org/eclipse/jetty/setuid/Passwd.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,19 @@
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
Expand All @@ -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;
Expand Down
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
Expand Up @@ -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 ()
{
Expand All @@ -46,5 +55,4 @@ public String toString()
{
return "rlimit_nofiles (soft="+_soft+", hard="+_hard+")";
}

}
5 changes: 5 additions & 0 deletions src/main/java/org/eclipse/jetty/setuid/SetUIDListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +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.LibC;
import org.eclipse.jetty.util.component.LifeCycle;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -44,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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,18 @@
// ========================================================================
//

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

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
{
Expand All @@ -46,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
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@
import java.nio.file.attribute.PosixFilePermission;
import java.util.Set;

import org.eclipse.jetty.setuid.internal.LibC;
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
Expand Down Expand Up @@ -55,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());
Expand Down

0 comments on commit 3eb7273

Please sign in to comment.