/*
 * Copyright Alexander O. Yuriev, 1996.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, and the entire permission notice in its entirety,
 *    including the disclaimer of warranties.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. The name of the author may not be used to endorse or promote
 *    products derived from this software without specific prior
 *    written permission.
 * 
 * ALTERNATIVELY, this product may be distributed under the terms of
 * the GNU Public License, in which case the provisions of the GPL are
 * required INSTEAD OF the above restrictions.  (This clause is
 * necessary due to a potential bad interaction between the GPL and
 * the restrictions contained in a BSD-style copyright.)
 * 
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/* 
   This code has been changed heavily for smb authentication by

   pam_smb_auth -- David Airlie 1998 v1.0 ( David.Airlie@ul.ie ) 
   http://www.csn.ul.ie/~airlied

   all changes are Copyright David Airlie 1998.

   Hacked by John Newbigin jn@it.swin.edu.au

   gcc -Wall -o smb_test smb_test.c pam_read_conf.c smbval/smbvalid.a
*/

#include "config.h"

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <pwd.h>

#include "valid.h"

#include <syslog.h>

/* Put LOG_AUTHPRIV == LOG_AUTH if it doesn't exist */
#ifdef USE_LOGAUTH
#define LOG_AUTHPRIV (4<<3)
#endif

#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif

// prototype for function in pam_read_conf.c
int smb_readpamconf(char *smb_server, char *smb_backup, char *smb_domain);

int pam_auth_smb(char *user, char *passwd);

int smb_debug=0;

#define USER_FILE "/etc/ppp/smb-users"

int user_allowed(char *user)
{
	FILE *users;
	char buffer[80];
	int result = 0; // default fail

	users = fopen(USER_FILE, "r");
	if(users)
	{
		while(fgets(buffer, sizeof(buffer), users))
		{
			// remove the \n
			int len = strlen(buffer);
			if(len > 0)
			{
				if(buffer[len - 1] == '\n')
				{
					buffer[len - 1] = 0;
					len--;
				}
			}

			// and we will be nice to Andrew and remove ^M's
			if(len > 0)
			{
				if(buffer[len - 1] == '\r')
				{
					buffer[len - 1] = 0;
				}
			}

			// see if we have a match
			if(strcasecmp(buffer, user) == 0)
			{
				result = 1;
				break;
			}
		}
		fclose(users);
		if(smb_debug)
		{
			if(!result)
			{
				printf("%s not in %s\n", user, USER_FILE);
			}
		}
	}
	else
	{
		printf("Could not open %s\n", USER_FILE);
	}
	return result;
}

// remove the domain (if any) and check
// if the user is allowed in
// if so then check the password with the NT server

int check_user(char *user, char *passwd)
{
	char *slash;

	slash = strrchr(user, '/');
	if(slash)
	{
		user = slash + 1;
	}

	slash = strchr(user, '\\');
	if(slash)
	{
		user = slash + 1; // we check for both slashes anyway
	}

	// user should now not have any domain attachment...

	// look the user up in the allowed people file...
	//if(user_allowed(user))
	{
		return pam_auth_smb(user, passwd);
	}
	return -1;
}

int pam_auth_smb(char *user, char *passwd)
{
	char server[80],server2[80],domain[80];
	int w;

	w=smb_readpamconf(server,server2,domain);
	if (w!=0) 
	{
	    printf("pam_smb: Missing Configuration file : /etc/pam_smb.conf\n");
	    return -1;
	}
	
	if (smb_debug) {
	  printf("pam_smb: Configuration Data, Primary %s, Backup %s, Domain %s.\n", server, server2, domain);
	}

	//w=Valid_User(ntname, p, server, server2,  domain);
	//w=Valid_User(user, passwd, "enterprise", "enterprise",  "AIPC");
	w=Valid_User(user, passwd, server, server2,  domain);
		  
	/* Users valid user for return value 0 is success
	   1 and 2 indicate Network and protocol failures and
	   3 is not logged on 
	   */

	switch (w)
	  {
	  case 0 : 
		if(smb_debug)
		{
	   		printf("pam_smb: Correct NT username/password pair\n");
		}
	    return 0; 
	  case 1 :
	  case 2 :
	    return -1;
	  case 3 :
		if(smb_debug)
		{
	    	printf("pam_smb: NT user not logged on : %s\n", user);
		}
	    return -1;
	  default:
		if(smb_debug)
		{
		    printf("pam_smb: Incorrect NT password for username : %s\n", user);
		}
	    return -1;
	  }

  	return -1;

}

#ifndef LIBRARY

int main(int argc, char **argv)
{
	char user[256];
	char *passwd;
	int len;

	printf("User name: ");
	fflush(stdout);
	fgets(user, sizeof(user), stdin);
	len = strlen(user) - 1;
	if(user[len] == '\n')
	{
		user[len] = 0;
	}
	passwd=getpass("Password: ");

	if(check_user(user, passwd) == 0)
	{
		printf("Success\n");
	}
	else
	{
		printf("Authentication Failed\n");
	}
	return 0;
}

#endif


