/*
 * ----------------------------------------------------------------
 * Night Light IRC Proxy - Connection Logging Functions
 * ----------------------------------------------------------------
 * Copyright (C) 1997-2007 Jonas Kvinge <jonas@night-light.net>
 * All rights reserved.
 *
 * 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.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Last modified by:
 * Jonas Kvinge (25.11.2007)
 *
 */

#define CONN_LOG_C

#define NEED_SYS_TYPES_H 1		/* Extra types */
#define NEED_SYS_PARAM_H 1		/* Some systems need this */
#define NEED_LIMITS_H 0			/* Kernel limits */
#define NEED_STDARG_H 1			/* va_list, etc */
#define NEED_ERRNO_H 1			/* errno */
#define NEED_CTYPE_H 1			/* isdigit(), etc */
#define NEED_NETINET_IN_H 1		/* in_addr, sockaddr_in, etc */
#define NEED_ARPA_INET_H 0		/* inet_ntoa(), inet_aton(), etc */
#define NEED_STDIO_H 1			/* Standard C UNIX functions */
#define NEED_STDLIB_H 1			/* malloc(), exit(), atoi(), etc */
#define NEED_TIME_H 1			/* time(), etc */
#define NEED_SYSCTL_H 0			/* sysctl(), etc */
#define NEED_SYS_STAT_H 1		/* chmod(), mkdir(), etc */
#define NEED_SYS_UIO_H 0		/* iovec, etc */
#define NEED_FCNTL_H 1			/* open(), creat(), fcntl(), etc */
#define NEED_SYS_IOCTL_H 0		/* ioctl(), etc */
#define NEED_SYS_FILIO_H 0		/* Solaris need this for ioctl(), etc */
#define NEED_UNISTD_H 1			/* Unix standard functions */
#define NEED_STRING_H 1			/* C string functions */
#define NEED_SIGNAL_H 0			/* Signal functions */
#define NEED_SYS_SOCKET_H 0		/* Socket functions */
#define NEED_NETDB_H 0			/* Network database functions */
#define NEED_ARPA_NAMESER_H 0		/* Nameserver definitions */
#define NEED_GETUSERPW_HEADERS 0 	/* Functions to retrive system passwords */
#define NEED_ARES 0			/* Functions needed for ares */
#define NEED_SSL 1			/* Needed for SSL support */

#include "includes.h"
#include "irc.h"

#include "conf.h"

#include "conn.h"
#include "conn_log.h"

extern struct Conf_Struct ConfS;
extern unsigned short int Root;

/* CONN_LOG FUNCTION - JONAS (18.07.2001) */

unsigned short int conn_log(struct Conn_Struct *ConnS, const char *const NUHPT, const char *const CommandPT, const char *const MessagePT) {

  signed long int Result = 0;
#ifndef WIN32
  char *HomeDirPT = NULL;
#endif
  char FileName[FILELEN+1] = "";
  char LogPath[FILELEN+1] = "";
  FILE *FilePT = NULL;

  assert(ConnS != NULL);
  assert(NUHPT != NULL);
  assert(CommandPT != NULL);
  assert(MessagePT != NULL);

  Conn_ClearLogHomeDir(ConnS);
if ((ConfS.UnixPasswd == TRUE) && (Root == TRUE)) {
  HomeDirPT = sysgethomedirfromuser(ConnS->User);
  if (HomeDirPT == NULL) {
    time_t Duration = NOW - ConnS->LogError;
    if (Duration > 3600) { sysprint(BITMASK_ERROR, "Unable to log messages for connection %s: Unable to retrive home directory for user \"%s\"", ConnS->Name, ConnS->User); }
    Conn_SetLogError(ConnS);
    ConnS->LogError = NOW;
    return(ERROR);
  }
  if (sysseteuidbyuser(ConnS->User) != SUCCESS) {
    time_t Duration = NOW - ConnS->LogError;
    if (Duration > 3600) { sysprint(BITMASK_ERROR, "Unable to log messages for connection %s: Unable to set effective user ID to \"%s\"", ConnS->Name, ConnS->User); }
    Conn_SetLogError(ConnS);
    ConnS->LogError = NOW;
    return(ERROR);
  }
  if (access(HomeDirPT, X_OK) != SUCCESS) {
    time_t Duration = NOW - ConnS->LogError;
    if (Duration > 3600) { sysprint(BITMASK_ERROR, "Unable to log messages for connection %s: Unable to access home directory for user \"%s\"", ConnS->Name, ConnS->User); }
    Conn_SetLogError(ConnS);
    ConnS->LogError = NOW;
    sysseteuidnormal();
    return(ERROR);
  }
  Conn_SetLogHomeDir(ConnS);
  snprintf(LogPath, FILELEN+1, "%s/ircproxy-logs", HomeDirPT);
}
else { snprintf(LogPath, FILELEN+1, "%s", ConfS.LogPath); }

  if (access(LogPath, X_OK) == ERROR) {
    Result = mkdir(LogPath, (S_IRWXU|S_IXGRP|S_IXOTH));
    if ((Result <= ERROR) && (errno != EEXIST)) {
      sysprint(BITMASK_ERROR, "Unable to create directory %s: [%d] %s", LogPath, errno, strerror(errno));
      if (Conn_IsLogHomeDir(ConnS)) { sysseteuidnormal(); }
      return(ERROR);
    }
  }

  snprintf(FileName, FILELEN+1, "%s/%s.log", LogPath, ConnS->Name);
  ConnS->FileName = strrealloc(ConnS->FileName, FileName);

  FilePT = fopen(FileName, "a");
  if (FilePT == NULL) {
    sysprint(BITMASK_ERROR, "Unable to open %s for writing: [%d] %s", FileName, errno, strerror(errno));
    if (Conn_IsLogHomeDir(ConnS)) { sysseteuidnormal(); }
    return(ERROR);
  }
  Result = chmod(FileName, S_IRUSR|S_IWUSR);
  if (Result <= ERROR) {
    sysprint(BITMASK_ERROR, "Unable to set file permission for %s: [%d] %s", FileName, errno, strerror(errno));
    fclose(FilePT);
    if (Conn_IsLogHomeDir(ConnS)) { sysseteuidnormal(); }
    return(ERROR);
  }

  if (strcasecmp(CommandPT, "PRIVMSG") == FALSE) { fprintf(FilePT, "%s <%s> %s%s", dtstamp(), NUHPT, MessagePT, LINEFEED); }
  else if (strcasecmp(CommandPT, "NOTICE") == FALSE) { fprintf(FilePT, "%s -%s- %s%s", dtstamp(), NUHPT, MessagePT, LINEFEED); }

  fclose(FilePT);

  if (Conn_IsLogHomeDir(ConnS)) { sysseteuidnormal(); }

  return(SUCCESS);

}



syntax highlighted by Code2HTML, v. 0.9.1