/* 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 should NOT
 * be used in the future
 */


#include <includes.h>
#include <stdarg.h>
#include "comm.h"
#include "auth.h"
#include "sighand.h"
#include "globals.h"


/* 
 * auth_login
 *
 * sends the login and password to the Nessus 
 * daemon
 *
 * Params :
 *  user : login
 *  password : password
 *
 * Returns :
 *  0 if the login informations were sent successfully
 * -1 if a problem occured
 * 
 * Note : this function does NOT check if the login/password are
 * valid.
 */
int auth_login(user,password)
	char * user;
	char * password;
{
  char * buf = emalloc(255);
 
  network_gets(buf, 8);
  if(strncmp(buf, "User : ", strlen(buf)))return(-1);
  network_printf("%s\n", user);
  
  bzero(buf, 255);
  network_gets(buf,12);
  if(strncmp(buf, "Password : ", strlen(buf)))return(-1);
  network_printf("%s\n", password);
  efree(&buf);
  return(0);
}

/*
 * network_printf(
 * 
 * This function sends a string to the server.
 * In the future, it will have to encrypt the string
 * but I have not implemented this feature right now
 */
void network_printf(char * data, ...)
{
  va_list param;
  char * buffer;
  signal(SIGPIPE, sighand_pipe);
  va_start(param, data);
  buffer = emalloc(4096);
#ifdef HAVE_VNSPRINTF
  vsnprintf(buffer, 4095, data, param);
#else
  vsprintf(buffer, data,param);
#endif
  send(GlobalSocket, buffer, strlen(buffer), 0);
  efree(&buffer);
  signal(SIGPIPE, SIG_IGN);
  va_end(param);
}                    

/*
 * network_gets(
 * 
 * Reads data sent by the server, and will
 * have to decrypt it in the future
 */
char * network_gets(s, size)
     char * s;
     size_t size;
{
  recv_line(GlobalSocket, s, size-1); 
  if(!strlen(s))return(NULL);
  /* we will have to decrypt some stuff here !*/
  return(s);
}

