Skip to content

Commit

Permalink
Some formatting, white space correction and override annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
arjantijms committed Mar 31, 2022
1 parent 3eebb1c commit 11e481f
Show file tree
Hide file tree
Showing 14 changed files with 176 additions and 153 deletions.
92 changes: 43 additions & 49 deletions appserver/libpam4j/src/main/java/org/jvnet/libpam/PAM.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,18 @@
import static org.jvnet.libpam.impl.CLibrary.libc;
import com.sun.jna.Pointer;
import static com.sun.jna.Native.POINTER_SIZE;
import static java.util.logging.Level.FINE;

import com.sun.jna.ptr.PointerByReference;
import java.util.Set;

import java.util.logging.Logger;
import java.util.logging.Level;

/**
* PAM authenticator.
*
* <p>
* Instances are thread unsafe and non reentrant. An instace cannot be reused
* to authenticate multiple users.
* Instances are thread unsafe and non reentrant. An instance cannot be reused to authenticate multiple users.
*
* <p>
* For an overview of PAM programming, refer to the following resources:
Expand All @@ -47,40 +47,40 @@
* @author Kohsuke Kawaguchi
*/
public class PAM {

private static final Logger LOGGER = Logger.getLogger(PAM.class.getName());

private pam_handle_t pht;
private int ret;

/**
* Temporarily stored to pass a value from {@link #authenticate(String, String)}
* to {@link pam_conv}.
* Temporarily stored to pass a value from {@link #authenticate(String, String)} to {@link pam_conv}.
*/
private String password;

/**
* Creates a new authenticator.
*
* @param serviceName
* PAM service name. This corresponds to the service name that shows up
* in the PAM configuration,
* @param serviceName PAM service name. This corresponds to the service name that shows up in the PAM configuration,
*/
public PAM(String serviceName) throws PAMException {
pam_conv conv = new pam_conv(new PamCallback() {
public int callback(int num_msg, Pointer msg, Pointer resp, Pointer pointer) {
if(LOGGER.isLoggable(Level.FINE))
LOGGER.fine("pam_conv num_msg="+num_msg);
if(password==null)
if (LOGGER.isLoggable(FINE))
LOGGER.fine("pam_conv num_msg=" + num_msg);
if (password == null)
return PAM_CONV_ERR;

// allocates pam_response[num_msg]. the caller will free this
Pointer m = libc.calloc(pam_response.SIZE,num_msg);
resp.setPointer(0,m);

for( int i=0; i<num_msg; i++ ) {
pam_message pm = new pam_message(msg.getPointer(POINTER_SIZE*i));
if(LOGGER.isLoggable(Level.FINE))
LOGGER.fine(pm.msg_style+":"+pm.msg);
if(pm.msg_style==PAM_PROMPT_ECHO_OFF) {
pam_response r = new pam_response(m.share(pam_response.SIZE*i));
Pointer m = libc.calloc(pam_response.SIZE, num_msg);
resp.setPointer(0, m);

for (int i = 0; i < num_msg; i++) {
pam_message pm = new pam_message(msg.getPointer(POINTER_SIZE * i));
if (LOGGER.isLoggable(FINE))
LOGGER.fine(pm.msg_style + ":" + pm.msg);
if (pm.msg_style == PAM_PROMPT_ECHO_OFF) {
pam_response r = new pam_response(m.share(pam_response.SIZE * i));
r.setResp(password);
r.write(); // write to (*resp)[i]
}
Expand All @@ -91,15 +91,15 @@ public int callback(int num_msg, Pointer msg, Pointer resp, Pointer pointer) {
});

PointerByReference phtr = new PointerByReference();
check(libpam.pam_start(serviceName,null,conv,phtr), "pam_start failed");
check(libpam.pam_start(serviceName, null, conv, phtr), "pam_start failed");
pht = new pam_handle_t(phtr.getValue());
}

private void check(int ret, String msg) throws PAMException {
this.ret = ret;
if(ret!=0) {
if(pht!=null)
throw new PAMException(msg+" : "+libpam.pam_strerror(pht,ret));
if (ret != 0) {
if (pht != null)
throw new PAMException(msg + " : " + libpam.pam_strerror(pht, ret));
else
throw new PAMException(msg);
}
Expand All @@ -108,72 +108,66 @@ private void check(int ret, String msg) throws PAMException {
/**
* Authenticate the user with a password.
*
* @return
* Upon a successful authentication, return information about the user.
* @throws PAMException
* If the authentication fails.
* @return Upon a successful authentication, return information about the user.
* @throws PAMException If the authentication fails.
*/
public UnixUser authenticate(String username, String password) throws PAMException {
this.password = password;
try {
check(libpam.pam_set_item(pht,PAM_USER,username),"pam_set_item failed");
check(libpam.pam_authenticate(pht,0),"pam_authenticate failed");
check(libpam.pam_setcred(pht,0),"pam_setcred failed");
check(libpam.pam_set_item(pht, PAM_USER, username), "pam_set_item failed");
check(libpam.pam_authenticate(pht, 0), "pam_authenticate failed");
check(libpam.pam_setcred(pht, 0), "pam_setcred failed");
// several different error code seem to be used to represent authentication failures
// check(libpam.pam_acct_mgmt(pht,0),"pam_acct_mgmt failed");

PointerByReference r = new PointerByReference();
check(libpam.pam_get_item(pht,PAM_USER,r),"pam_get_item failed");
check(libpam.pam_get_item(pht, PAM_USER, r), "pam_get_item failed");
String userName = r.getValue().getString(0);
Passwd pwd = libc.getpwnam(userName);
if(pwd==null)
if (pwd == null)
throw new PAMException("Authentication succeeded but no user information is available");
return new UnixUser(userName,pwd);
return new UnixUser(userName, pwd);
} finally {
this.password = null;
}
}

/**
* Returns the groups a user belongs to
*
* @param username
* @return Set of group names
* @throws PAMException
* @deprecated
* Pointless and ugly convenience method.
* @deprecated Pointless and ugly convenience method.
*/
public Set<String> getGroupsOfUser(String username) throws PAMException {
return new UnixUser(username).getGroups();
}

/**
* After a successful authentication, call this method to obtain the effective user name.
* This can be different from the user name that you passed to the {@link #authenticate(String, String)}
* method.
* After a successful authentication, call this method to obtain the effective user name. This can be different from the
* user name that you passed to the {@link #authenticate(String, String)} method.
*/

/**
* Performs an early disposal of the object, instead of letting this GC-ed.
* Since PAM may hold on to native resources that don't put pressure on Java GC,
* doing this is a good idea.
* Performs an early disposal of the object, instead of letting this GC-ed. Since PAM may hold on to native resources
* that don't put pressure on Java GC, doing this is a good idea.
*
* <p>
* This method is called by {@link #finalize()}, too, so it's not required
* to call this method explicitly, however.
* This method is called by {@link #finalize()}, too, so it's not required to call this method explicitly, however.
*/
public void dispose() {
if(pht!=null) {
libpam.pam_end(pht,ret);
pht=null;
if (pht != null) {
libpam.pam_end(pht, ret);
pht = null;
}
}


@Override
protected void finalize() throws Throwable {
super.finalize();
dispose();
}

private static final Logger LOGGER = Logger.getLogger(PAM.class.getName());

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
* @author Kohsuke Kawaguchi
*/
public class PAMException extends Exception {

private static final long serialVersionUID = 1L;

public PAMException() {
}

Expand All @@ -37,5 +40,5 @@ public PAMException(Throwable cause) {
super(cause);
}

private static final long serialVersionUID = 1L;

}
33 changes: 16 additions & 17 deletions appserver/libpam4j/src/main/java/org/jvnet/libpam/UnixUser.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,44 +33,44 @@
*/
public class UnixUser {
private final String userName, gecos, dir, shell;
private final int uid,gid;
private final int uid, gid;
private final Set<String> groups;

/*package*/ UnixUser(String userName, Passwd pwd) throws PAMException {
UnixUser(String userName, Passwd pwd) throws PAMException {
this.userName = userName;
this.gecos = pwd.getPwGecos();
this.dir = pwd.getPwDir();
this.shell = pwd.getPwShell();
this.uid = pwd.getPwUid();
this.gid = pwd.getPwGid();

long sz = 4; /*sizeof(gid_t)*/
long sz = 4; /* sizeof(gid_t) */

int ngroups = 64;
Memory m = new Memory(ngroups*sz);
Memory m = new Memory(ngroups * sz);
IntByReference pngroups = new IntByReference(ngroups);
try {
if(libc.getgrouplist(userName,pwd.getPwGid(),m,pngroups)<0) {
if (libc.getgrouplist(userName, pwd.getPwGid(), m, pngroups) < 0) {
// allocate a bigger memory
m = new Memory(pngroups.getValue()*sz);
if(libc.getgrouplist(userName,pwd.getPwGid(),m,pngroups)<0)
m = new Memory(pngroups.getValue() * sz);
if (libc.getgrouplist(userName, pwd.getPwGid(), m, pngroups) < 0)
// shouldn't happen, but just in case.
throw new PAMException("getgrouplist failed");
}
ngroups = pngroups.getValue();
} catch (LinkageError e) {
// some platform, notably Solaris, doesn't have the getgrouplist function
ngroups = libc._getgroupsbymember(userName,m,ngroups,0);
if (ngroups<0)
ngroups = libc._getgroupsbymember(userName, m, ngroups, 0);
if (ngroups < 0)
throw new PAMException("_getgroupsbymember failed");
}

groups = new HashSet<String>();
for( int i=0; i<ngroups; i++ ) {
for (int i = 0; i < ngroups; i++) {
int gid = m.getInt(i * sz);
Group grp = libc.getgrgid(gid);
if( grp == null ) {
continue;
if (grp == null) {
continue;
}
groups.add(grp.gr_name);
}
Expand All @@ -81,8 +81,8 @@ public UnixUser(String userName) throws PAMException {
}

/**
* Copy constructor for mocking. Not intended for regular use. Only for testing.
* This signature may change in the future.
* Copy constructor for mocking. Not intended for regular use. Only for testing. This signature may change in the
* future.
*/
protected UnixUser(String userName, String gecos, String dir, String shell, int uid, int gid, Set<String> groups) {
this.userName = userName;
Expand Down Expand Up @@ -139,14 +139,13 @@ public String getShell() {
/**
* Gets the groups that this user belongs to.
*
* @return
* never null.
* @return never null.
*/
public Set<String> getGroups() {
return Collections.unmodifiableSet(groups);
}

public static boolean exists(String name) {
return libc.getpwnam(name)!=null;
return libc.getpwnam(name) != null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
*/
public interface BSDCLibrary extends CLibrary {

BSDPasswd getpwnam(String username);
@Override
BSDPasswd getpwnam(String username);

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.jvnet.libpam.impl.CLibrary.Passwd;

/**
Expand Down Expand Up @@ -64,8 +65,7 @@ public String getPwShell() {
@Override
protected List getFieldOrder() {
List fieldOrder = new ArrayList(super.getFieldOrder());
fieldOrder.addAll(Arrays.asList("pw_gecos",
"pw_dir", "pw_shell"));
fieldOrder.addAll(Arrays.asList("pw_gecos", "pw_dir", "pw_shell"));
return fieldOrder;
}

Expand Down

0 comments on commit 11e481f

Please sign in to comment.