/* 
   Copyright Andrew Tridgell 1997


   */
   

#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <time.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>

#include "pserver.h"

#ifndef __Usockaddr__

typedef union {
    struct sockaddr_in v4;
#ifdef INET6
    struct sockaddr_in6 v6;
#endif
} Usockaddr;

#define __Usockaddr__
#endif

/*******************************************************************
 return the IP addr of the client as a string 
 ******************************************************************/
char *client_addr(int fd)
{
  Usockaddr sa;
  Usockaddr *sockin = &sa;
  int    length = sizeof(sa);
  static char addr_buf[1000];

  if (getpeername(fd, (struct sockaddr *)&sa, &length)) {
    exit(1);
  }

  if (sa.v4.sin_family == AF_INET) {
    strncpy(addr_buf,(char *)inet_ntoa(sa.v4.sin_addr),sizeof(addr_buf));
  }
#ifdef INET6
  else if (sa.v6.sin6_family == AF_INET6) {
    inet_ntop(AF_INET6, (const void*)&sa.v6.sin6_addr,
	      addr_buf, sizeof(addr_buf));
  }
#endif
  else {
    unsigned char *p = (void*)&sa;
    char *s = addr_buf;
    for ( ;length>0 && s < (addr_buf + sizeof(addr_buf)-4); --length,s+=3,++p)
      sprintf(s,"%02X-", (*p & 0xFF));
    if (s > addr_buf)
      s[-1] = 0; /* The last hyphen */
  }

  addr_buf[sizeof(addr_buf)-1] = 0;

  return addr_buf;
}


/*******************************************************************
 return the DNS name of the client 
 ******************************************************************/
char *client_name(int fd)
{
  Usockaddr sa;
  int    length = sizeof(sa);
  static char name_buf[1000];
  struct hostent *hp;

  strcpy(name_buf,"UNKNOWN");

  if (getpeername(fd, (struct sockaddr*)&sa, &length)) {
    exit(1);
  }

  /* Look up the remote host name. */
  if (sa.v4.sin_family == AF_INET) {
    hp = gethostbyaddr((char *) &sa.v4.sin_addr,
		       sizeof(sa.v4.sin_addr),
		       AF_INET);
    if (hp)
      strncpy(name_buf, (char *)hp->h_name, sizeof(name_buf));
  }
#ifdef INET6
  else if (sa.v6.sin6_family == AF_INET6) {
    struct in6_addr *ip6 = &sa.v6.sin6_addr;
    if (IN6_IS_ADDR_V4MAPPED(ip6)) {
      hp = gethostbyaddr(((char *) ip6) + 12, 4, AF_INET);
      if (!hp)
	inet_ntop(AF_INET, ((char*) ip6) + 12, name_buf, sizeof(name_buf));
    } else
      hp = gethostbyaddr((char *) ip6, 16, AF_INET6);
    if (hp)
      strncpy(name_buf, (char *)hp->h_name, sizeof(name_buf));
  }
#endif
  else
    sprintf(name_buf, "addr_family=%d", sa.v4.sin_family);

  name_buf[sizeof(name_buf) - 1] = 0;

  return name_buf;
}



/****************************************************************************
  return the date and time as a string
  ****************************************************************************/
char *timestring(void )
{
  static char TimeBuf[1000];
  time_t t = time(NULL);
  struct tm *tm = localtime(&t);

  strftime(TimeBuf,100,"%D %T",tm);

  return(TimeBuf);
}


int process_exists(int pid)
{
  return(kill(pid,0) == 0 || errno != ESRCH);
}


int sync_trees(void)
{
	struct stat st1, st2;
	int child, pid, fd, status;

again:
	if (stat(REAL_REPOSITORY MARKER_FILE, &st1))
		exit(1);

	memset(&st2, 0, sizeof(st2));
	stat(FAKE_REPOSITORY MARKER_FILE, &st2);

	if (st1.st_size == st2.st_size && 
	    st1.st_mtime == st2.st_mtime) {
		return 0;
	}

	while ((fd = open(PID_FILE, O_CREAT|O_EXCL|O_WRONLY, 0444)) == -1) {
	  fd = open(PID_FILE,O_RDONLY);
	  if (read(fd, &pid, sizeof(pid)) != sizeof(pid) ||
	      !process_exists(pid)) {
	    close(fd);
	    unlink(PID_FILE);
	    continue;
	  }
	  close(fd);
	  sleep(5);
	  goto again;
	}

	pid = getpid();
	write(fd, &pid, sizeof(pid));
	close(fd);

	if ((child = fork())) {
	  waitpid(child, &status, 0);
	} else {
	  execl(RSYNC_PATH, "rsync", 
		"-a", "--delete", "--ignore-errors", "--delete-excluded", 
		"--exclude=*.lock",
		REAL_REPOSITORY, FAKE_REPOSITORY, NULL);
	}

	unlink(PID_FILE);
	return 0;
}


