| career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

/* $Id:,v 2002/10/02 18:41:44 wastl Exp $ */
import net.wastl.webmail.server.*;
import net.wastl.webmail.exceptions.*;
import java.util.*;

import net.wastl.webmail.misc.*;
import net.wastl.webmail.config.ConfigScheme;

 * Created: Mon Apr 19 13:43:48 1999
 * @author Sebastian Schaffert
 * @version

public class UnixAuthenticator extends Authenticator {
    public final String VERSION="1.2";

    public static final String passwd="/etc/passwd";
    public static final String shadow="/etc/shadow";

    public UnixAuthenticator() {

    public String getVersion() {
	return VERSION;

    public void init(Storage store) {

    public void register(ConfigScheme store) {
	store.configAddChoice("AUTH",key,"Authenticate against the local Unix server's passwd/shadow files. Password change not possible.");

    public void authenticatePreUserData(String user, String domain,
     String given_passwd) throws InvalidPasswordException 
	String login=user;
	try {
	    File f_passwd=new File(passwd);
	    File f_shadow=new File(shadow);
	    BufferedReader in;
	    if(f_shadow.exists()) {		
		in=new BufferedReader(new InputStreamReader(new FileInputStream(f_shadow)));
	    } else {
		in=new BufferedReader(new InputStreamReader(new FileInputStream(f_passwd)));
	    String line;
	    while(line != null) {
		if(line.startsWith(login)) break;
	    if(line == null) throw new InvalidPasswordException("Invalid user: "+login);
	    StringTokenizer tok=new StringTokenizer(line,":");
	    String my_login=tok.nextToken();
	    String password=tok.nextToken();

	    if(!password.equals(Helper.crypt(password,given_passwd))) {
		WebMailServer.getStorage().log(Storage.LOG_WARN,"UnixAuthentication: user "+login+
					       " authentication failed.");
		throw new InvalidPasswordException("Unix authentication failed");
	    WebMailServer.getStorage().log(Storage.LOG_INFO,"UnixAuthentication: user "+login+
					   " authenticated successfully.");
	} catch(IOException ex) {
	    System.err.println("*** Cannot use UnixAuthentication and shadow passwords if WebMail is not executed as user 'root'! ***");
	    throw new InvalidPasswordException("User login denied due to configuration error (contact system administrator)");

     * Don't allow to change Unix-Passwords as this could mess things up.
    public boolean canChangePassword() {
	return false;
} // UnixAuthenticator
... this post is sponsored by my books ...

#1 New Release!

FP Best Seller


new blog posts


Copyright 1998-2021 Alvin Alexander,
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.