/* Nessus
 * Copyright (C) 1998 Renaud Deraison
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 * This is the Authentification Manager
 *
 * This authentification scheme is BADLY written, and will NOT
 * be used in the future
 *
 */


#include <includes.h>
#include <stdarg.h>

#include "rules.h"
#include "comm.h"
#include "auth.h"
#include "log.h"
#include "sighand.h"



/*
 * auth_check_user() :
 *
 * Checks if a user has the right to use nessusd,
 * and return its permissions
 */
char * 
auth_check_user(globals, from)
   struct arglist * globals;
   char * from;
{
  char * buf_user, * buf_password;
  char * permissions;
  struct users * usersdb = arg_get_value(globals, "users");
   
  buf_user = emalloc(255);
  buf_password = emalloc(255);
  
  auth_printf(globals,"User : ");
  auth_gets(globals, buf_user, 254);
  
  auth_printf(globals, "Password : ");
  auth_gets(globals, buf_password, 254);
  
  buf_user[strlen(buf_user)-1]=0;
  buf_password[strlen(buf_password)-1]=0;
  
  if((permissions = check_user(usersdb, buf_user, buf_password)))
  {
	char* user = emalloc(strlen(buf_user)+1);
	strncpy(user, buf_user, strlen(buf_user));

    log_write("successful login of %s from %s\n", buf_user, from);
	if(arg_get_value(globals, "user"))
	 arg_set_value(globals, "user", strlen(user), user);
	else
	 arg_add_value(globals, "user", ARG_STRING, strlen(user), user);
  }
  efree(&buf_user);
  efree(&buf_password);
  return(permissions);
}


