#!/usr/local/bin/perl
#
#	$NetBSD: pidentd2netbsd,v 1.1 1998/07/15 07:37:26 msaitoh Exp $
#
#  Perl script to convert a standard distribution directory for pidentd into
#	a NetBSD source tree.
#
#  This is done as a script so that as each distribution is released,
#	only changes from the previous one need to be dealt with as
#	modifications to this script and related files.  This should
#	reduce the cost of updating from a new release of pidentd by an
#	order of magnitude (or more?)
#
#  This script requires two environment variables set:
#	SRCDIR - pidentd source directory
#	TARGETDIR - name of the high level directory to make
#
#  Written by SAITOH Masanobu July 1st, 1998 for pidentd-2.8.3
#

$version = "2.8.3";

# definitions ...

@subdirs = ("libexec/identd");



@identdf = ("identd.c", "parse.c", "config.c", "proxy.c", "version.c",
	  "netbsd.c");

@identdhf = ("identd.h", "error.h", "paths.h");

@identdmf = ("identd.8");
@identddf = ("CREDITS", "README");


# sed edit list: file, sed-program
%sedlist = ();

#
# Utility Subroutines
#

sub makedir {
    system("mkdir -p @_");
}

# &fixrcs (fromfile, tofile);
sub fixrcs
{
    my ($f, $t) = @_;
    my @keywords = ("Author", "Date", "Header", "Id", "Locker", "Log",
		    "Name", "RCSfile", "Revision", "Source", "State");
    my $state = 0;
    my $hdr = 0;

    open(IFILE, "<$f") || die "Cannot open $f";
    open(OFILE, ">$t") || die "Cannot create $t";

    if ($t =~ /.*\.[0-9]/) {
	print OFILE '.\\" $', 'NetBSD',  '$', "\n.\\", "\n";
    }
    elsif ($t =~ /.*\.[ch]/) {
	print OFILE "/*\t", '$', 'NetBSD', '$', "\t*/\n\n";
    }
    elsif ($t =~ /.*\.[yl]/) {
	$hdr = 1;
    }
    else {
	print OFILE '$', 'NetBSD', '$', "\n";
    }
    while (<IFILE>) {
	if ($hdr == 1) {
	    if (/%{/) {
		print OFILE "%{\n/*\t", '$', 'NetBSD', '$', "\t*/\n\n";
		$hdr = 0;
		next;
	    }
	}
	if ($state == 2) {
	    if (/#endif/) {
		print OFILE "#else\n__RCSID(", '"$', 'NetBSD', '$"',
		    ");\n#endif\n";
		$state = 0;
	    }
	}
	if ($state == 1) {
	    print OFILE "#if 0\n";
	    $state = 2;
	}
	if (/#ifndef lint/) {
	    print OFILE "#include <sys/cdefs.h>\n";
	    $state = 1;
	}
	foreach $key (@keywords) {
	    s/\$$key\$/$key/g;
	    s/\$$key:(.*)\$/$key:$1/g;
	}
	print OFILE $_;
    }
    close(IFILE) || die "closing input file";
    close(OFILE) || die "closing output file";
}

# &copyfiles (fromdir, todir, list of files);
sub copyfiles {
    local ($fdir, $tdir, @list) = @_;
    local ($f);

    foreach $f (@list) {
	print "  $fdir/$f --> $tdir/$f\n";
	&fixrcs("$fdir/$f", "$tdir/$f");
    }
}

# &copyfile (fromfile, tofile);
sub copyfile {
    local ($f, $t) = @_;

    print "  $f --> $t\n";
    system ("cp $f $t");
}

sub uniq {
    local (@inlist) = @_;
    local (@outlist);

    @outlist = ($inlist[0]);
    for ( $i=1; $i < @inlist; $i++ ) {
	if ($inlist[$i] ne $inlist[$i-1]) {
	    push (@outlist, $inlist[$i]);
	}
    }

    @outlist;
}

sub dumpsrcs {
    local (@names) = @_;
    local ($count);

    $count = 0;
    while ($f = pop(@names)) {
        print ODATA "$f ";
	if ($count == 5 && @names > 0) {
	    print ODATA "\\\n";
	    $count = 0;
	} else {
	    $count += 1;
	}
    }
    if ($count != 0) {
	print ODATA "\n";
    }
}

#
# Main progarm.
# 

$srcdir = $ENV{'SRCDIR'};
$targetdir = $ENV{'TARGETDIR'};
$incdirs = "-I.";

if (!$srcdir | !targetdir) {
    die "You must define the environment variables SRCDIR and TARGETDIR.\n"
} 
print "Making the NetBSD directory tree.\n";
foreach $f (@subdirs) { 
    print "   -->$f\n";
    makedir ("$targetdir/$f");
}

print "Populating the libexec/identd directory.\n";
system("cp $srcdir/identd.man $targetdir/libexec/identd/identd.8");
&copyfiles ("$srcdir/src", "$targetdir/libexec/identd", @identdf, @identdhf);
&copyfiles ("$srcdir", "$targetdir/libexec/identd", @identddf);

#
# Build makefiles
#

$first = "True";
while ($line = <DATA>) {
    chop ($line);
    if (substr($line,0,2) eq "%%") {
	@cmd = split (/ /,$line);
	if ($cmd[1] eq "file") {
	    print "Building $targetdir/$cmd[2]\n";
	    if ($first eq "") {
		close (ODATA);
	    } else {
		$first = "";
	    }
	    open (ODATA, ">$targetdir/$cmd[2]") ||
		die "Could not create $targetdir/$cmd[2]";
	} elsif ($cmd[1] eq "awks") {
	    print "  Defining AWKS\n";
	    if ($first) {
		die "Data file must start with a %% file!";
	    }
	    print ODATA "AWKS=\t";
	    &dumpsrcs (@identddaf);
	} elsif ($cmd[1] eq "srcs") {
	    print "  Defining SRCS\n";
	    if ($first) {
		die "Data file must start with a %% file!";
	    }
	    print ODATA "SRCS=\t";
	    &dumpsrcs (@identdf);
	} elsif ($cmd[1] eq "man") {
	    print "  Defining MAN\n";
	    if ($first) {
		die "Data file must start with a %% file!";
	    }
	    print ODATA "MAN=\t";
	    &dumpsrcs (@identdmf);
	} elsif ($cmd[1] eq "version") {
	    print "  Defining VERSION\n";
	    print ODATA "char version[] = \"$version\";";
	} elsif ($cmd[1] eq "NetBSD") {
	    if ($first) {
		die "Data section must start with a %% file!";
	    }
	    print ODATA "$cmd[2]	\$"."NetBSD".": \$	$cmd[3]\n";
	} elsif ($cmd[1] eq "patch") {
	    print "Patching in directory $cmd[2]\n";
	    $cwd = `pwd`;
	    chop($cwd);
	    chdir ("$targetdir/$cmd[2]")
		|| die "Can't connect to $targetdir/$cmd[2] in patch.\n";
	    if (!$first) {
		close (ODATA);
		$first = "True";
	    }
	    open (ODATA, ">PATCH") ||
		die "Could not create PATCH file";
	    $more = "True";
	    while ($more) {
		$line = <DATA>;
		if (substr($line,0,2) eq "%%") {
		    $more = "";
		    close (ODATA);
		} else {
		    print ODATA $line;
		}
	    }
	    system ("patch < PATCH");
	    system ("rm -f *.orig PATCH");
	    chdir($cwd);
	}
    } else {
	if ($first) {
	    die "Data file must start with a %% file!";
	}
	print ODATA "$line\n";
    }
}    
close (ODATA);

#
# Sed transformations of files
#
foreach $n (keys(%sedlist)) {
    print "Modifying $n\n";
    system ("cd $targetdir; sed $sedlist{$n} $n > tmp; mv -f tmp $n");
}

#
# end of the script
#

# what follows is the data for makefiles and other special files
# that need to be created.

__END__
%% file libexec/identd/Makefile
%% NetBSD #

PROG=	identd
%% srcs
%% man

CPPFLAGS+= -DINCLUDE_EXTENSIONS -DSTRONG_LOG -DALLOW_FORMAT
LDADD+=	-lutil -lkvm
DPADD+=	${LIBUTIL} ${LIBKVM}

.include <bsd.prog.mk>
%% patch libexec/identd
--- pidentd-2.8.3/src/config.c	Tue Jul 29 06:01:26 1997
+++ identd/config.c	Wed Jul  1 21:54:11 1998
@@ -10,6 +10,10 @@
 */
 
 #include <sys/types.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+
 #include <stdio.h>
 #include <errno.h>
 #include <string.h>
@@ -44,7 +48,7 @@
   }
 
   netcnt = 0;
-  while (start = fgets(buf, sizeof buf, fp)) {
+  while ((start = fgets(buf, sizeof buf, fp)) != NULL) {
     char *net, *mask;
     char *cmd = strtok(buf, " \t");
     if (cmd) {
%% end patch
%% patch libexec/identd
--- pidentd-2.8.3/src/identd.c	Mon Jun  1 08:19:51 1998
+++ identd/identd.c	Wed Jul  1 21:54:11 1998
@@ -13,7 +13,7 @@
 #define void int
 #endif
 
-#if defined(IRIX) || defined(SVR4) || defined(NeXT) || (defined(sco) && sco >= 42) || defined(_AIX4) || defined(__FreeBSD__) || defined(ultrix)
+#if defined(IRIX) || defined(SVR4) || defined(NeXT) || (defined(sco) && sco >= 42) || defined(_AIX4) || defined(__NetBSD__) || defined(__FreeBSD__) || defined(ultrix)
 #  define SIGRETURN_TYPE void
 #  define SIGRETURN_TYPE_IS_VOID
 #else
@@ -66,7 +66,7 @@
 extern int errno;
 #endif
 
-#if defined(SOLARIS) || defined(__FreeBSD__) || defined(__linux__) || defined(_AIX)
+#if defined(SOLARIS) || defined(__NetBSD__) || defined(__FreeBSD__) || defined(__linux__) || defined(_AIX)
 #  include <unistd.h>
 #  include <stdlib.h>
 #  include <string.h>
@@ -75,7 +75,7 @@
 #include "identd.h"
 #include "error.h"
 #include "paths.h"
-#include "crypto.h"
+
 
 /* Antique unixes do not have these things defined... */
 #ifndef FD_SETSIZE
@@ -94,10 +94,6 @@
 #endif
 
 
-extern void *calloc();
-extern void *malloc();
-
-
 char *path_unix = (char *) NULL;
 char *path_kmem = (char *) NULL;
 
@@ -128,6 +124,11 @@
 static int syslog_facility = LOG_DAEMON;
 #endif
 
+static int comparemem __P((void *, void *, int));
+char *clearmem __P((void *, int));
+static SIGRETURN_TYPE child_handler __P((int));
+int main __P((int, char *[]));
+
 /*
 ** The structure passing convention for GCC is incompatible with
 ** Suns own C compiler, so we define our own inet_ntoa() function.
@@ -236,7 +237,8 @@
 ** if the -w or -b options are used.
 */
 static SIGRETURN_TYPE
-child_handler()
+child_handler(dummy)
+	int dummy;
 {
 #if defined(NeXT) || (defined(__sgi) && defined(__SVR3))
     union wait status;
@@ -650,10 +652,11 @@
 	** handshake.
 	*/
 #if (defined(SVR4) || defined(hpux) || defined(__hpux) || defined(IRIX) || \
-     defined(_CRAY) || defined(_AUX_SOURCE)) || defined(sco) || defined(LINUX)
+     defined(_CRAY) || defined(_AUX_SOURCE) || defined(sco) || \
+	 defined(LINUX))
 	signal(SIGCHLD, SIG_IGN);
 #else
-	signal(SIGCHLD, (SIGRETURN_TYPE (*)()) child_handler);
+	signal(SIGCHLD, child_handler);
 #endif
     
 	/*
%% end patch
%% patch libexec/identd
diff -u pidentd-2.8.3/src/identd.h identd/identd.h
--- pidentd-2.8.3/src/identd.h	Tue Jul 29 06:01:26 1997
+++ identd/identd.h	Wed Jul  1 23:32:04 1998
@@ -38,11 +40,17 @@
 extern int lport;
 extern int fport;
 
-extern char *gethost();
+extern char *gethost __P((struct in_addr *));
 
-extern int k_open();
-extern int k_getuid();
-extern int parse();
-extern int parse_config();
+extern int k_open __P((void));
+extern int k_getuid __P((struct in_addr *, int, struct in_addr *, int, int *));
+extern int parse __P((FILE *, struct in_addr *, struct in_addr *));
+extern int parse_config __P((char *, int));
+
+#ifdef INCLUDE_PROXY
+int proxy __P((struct in_addr *, struct in_addr *, int, int, struct timeval *));
+#else
+int proxy __P((void *, void *, int, int, void *));
+#endif
 
 #endif
%% end patch
%% patch libexec/identd
--- pidentd-2.8.3/src/netbsd.c	Tue Jul 29 06:01:27 1997
+++ identd/netbsd.c	Thu Jul  2 00:11:01 1998
@@ -12,6 +12,7 @@
 */
 
 #include <stdio.h>
+#include <stdlib.h>
 #include <errno.h>
 #include <ctype.h>
 #include <limits.h>
@@ -63,10 +64,6 @@
 #include "error.h"
 
 
-extern void *calloc();
-extern void *malloc();
-
-
 struct nlist nl[] =
 {
 #define N_FILE 0
@@ -86,6 +83,9 @@
 
 static struct inpcbtable tcbtable;
   
+static int getbuf __P((long, char *, int, char *));
+static struct socket *getlist __P((struct inpcbtable *, struct inpcbtable *,
+    struct in_addr *, int, struct in_addr *, int));
 
 int k_open()
 {
@@ -121,7 +121,7 @@
   if (kvm_read(kd, addr, buf, len) != len)
   {
     if (syslog_flag)
-      syslog(LOG_ERR, "getbuf: kvm_read(%08x, %d) - %s : %m",
+      syslog(LOG_ERR, "getbuf: kvm_read(%#lx, %d) - %s : %m",
 	     addr, len, what);
 
     return 0;
@@ -152,7 +152,7 @@
 	for (kpcbp = tcbtablep->inpt_queue.cqh_first;
 	     kpcbp != (struct inpcb *)ktcbtablep;
 	     kpcbp = pcb.inp_queue.cqe_next) {
-		if (!getbuf((long) kpcbp, &pcb, sizeof(struct inpcb), "tcb"))
+		if (!getbuf((long) kpcbp, (char *)&pcb, sizeof(struct inpcb), "tcb"))
 			break;
 		if (pcb.inp_faddr.s_addr == faddr->s_addr &&
 		    pcb.inp_laddr.s_addr == laddr->s_addr &&
@@ -181,10 +181,10 @@
   struct ucred ucb;
   
   /* -------------------- FILE DESCRIPTOR TABLE -------------------- */
-  if (!getbuf(nl[N_NFILE].n_value, &nfile, sizeof(nfile), "nfile"))
+  if (!getbuf(nl[N_NFILE].n_value, (char *)&nfile, sizeof(nfile), "nfile"))
     return -1;
   
-  if (!getbuf(nl[N_FILE].n_value, &addr, sizeof(addr), "&file"))
+  if (!getbuf(nl[N_FILE].n_value, (char *)&addr, sizeof(addr), "&file"))
     return -1;
 
   {
@@ -200,7 +200,7 @@
     }
     xfile = malloc(siz);
     if (!xfile)
-      ERROR1("k_getuid: malloc(%d)", siz);
+      ERROR1("k_getuid: malloc(%ld)", (u_long)siz);
     if ((rv = sysctl(mib, 2, xfile, &siz, NULL, 0)) == -1)
     {
       ERROR1("k_getuid: sysctl 2 (%d)", rv);
@@ -210,11 +210,12 @@
   }
   
   /* -------------------- TCP PCB LIST -------------------- */
-  if (!getbuf(nl[N_TCBTABLE].n_value, &tcbtable, sizeof(tcbtable), "tcbtable"))
+  if (!getbuf(nl[N_TCBTABLE].n_value, (char *)&tcbtable, sizeof(tcbtable),
+			  "tcbtable"))
     return -1;
   
-  sockp = getlist(&tcbtable, nl[N_TCBTABLE].n_value, faddr, fport, laddr,
-      lport);
+  sockp = getlist(&tcbtable, (struct inpcbtable *)nl[N_TCBTABLE].n_value,
+				  faddr, fport, laddr, lport);
   
   if (!sockp)
     return -1;
@@ -231,7 +232,7 @@
     if (xfile[i].f_type == DTYPE_SOCKET &&
 	(struct socket *) xfile[i].f_data == sockp)
     {
-      if (!getbuf(xfile[i].f_cred, &ucb, sizeof(ucb), "ucb"))
+      if (!getbuf((long)xfile[i].f_cred, (char *)&ucb, sizeof(ucb), "ucb"))
 	return -1;
 
       *uid = ucb.cr_uid;
%% end patch
%% patch libexec/identd
diff -u pidentd-2.8.3/src/parse.c identd/parse.c
--- pidentd-2.8.3/src/parse.c	Fri Mar 20 08:49:10 1998
+++ identd/parse.c	Wed Jul  1 23:32:04 1998
@@ -14,6 +16,8 @@
 #endif
 
 #include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
 #include <errno.h>
 #include <ctype.h>
 #include <pwd.h>
@@ -28,11 +32,7 @@
 #  include <arpa/inet.h>
 #endif
 
-#ifdef HAVE_KVM
-#  include <kvm.h>
-#else
-#  include "kvm.h"
-#endif
+#include <kvm.h>
 
 #include <sys/types.h>
 #include <sys/stat.h>
@@ -48,9 +48,10 @@
 
 #include "identd.h"
 #include "error.h"
-#include "crypto.h"
 
-extern void *malloc();
+static int eat_whitespace __P((void));
+static int check_noident __P((char *));
+static int valid_fhost(struct in_addr *, char *);
 
 /*
 ** This function will eat whitespace characters until
@@ -193,7 +194,6 @@
   char arg[33];
 #endif
 #ifdef INCLUDE_EXTENSIONS
-  extern int proxy();
   int c;
 #endif
   struct in_addr laddr2;
@@ -631,7 +631,7 @@
     {
       char* cp;
       char** gmp;
-      long bp;
+      int bp;
       char buff[512];
       for (cp = format, bp = 0; *cp != 0; cp++)
       {
@@ -649,7 +649,7 @@
 	    sprintf (&buff[bp], "%.*s", 490-bp, grname);
 	    bp += strlen(&buff[bp]); if (bp >= 490) break;
 	    setgrent();
-	    while (grp = getgrent()) {
+	    while ((grp = getgrent()) != NULL) {
 	      if (grp->gr_gid == pwp->pw_gid) continue;
 	      for (gmp = grp->gr_mem; *gmp && **gmp; gmp++) {
 		if (! strcmp(*gmp, pwp->pw_name)) {
@@ -666,7 +666,7 @@
 	    sprintf (&buff[bp], "%d", pwp->pw_gid);
 	    bp += strlen(&buff[bp]); if (bp >= 490) break;
 	    setgrent();
-	    while (grp = getgrent()) {
+	    while ((grp = getgrent()) != NULL) {
 	      if (grp->gr_gid == pwp->pw_gid) continue;
 	      for (gmp = grp->gr_mem; *gmp && **gmp; gmp++) {
 		if (! strcmp(*gmp, pwp->pw_name)) {
%% end patch
%% patch libexec/identd
--- pidentd-2.8.3/src/proxy.c	Tue Jul 29 06:01:28 1997
+++ identd/proxy.c	Wed Jul  1 21:54:10 1998
@@ -12,19 +12,16 @@
 #include <stdio.h>
 #include <errno.h>
 
-#include "identd.h"
-
-
-#ifdef INCLUDE_PROXY
 #include <sys/types.h>
 #include <sys/time.h>
 #include <netinet/in.h>
 
-#include <ident.h>
-#endif
+#include "identd.h"
 
-#ifndef __STDC__
-#define void int
+#ifdef INCLUDE_PROXY
+int proxy __P((struct in_addr *, struct in_addr *, int, int, struct timeval *));
+#else
+int proxy __P((void *, void *, int, int, void *));
 #endif
 
 /*
%% end patch
%% patch libexec/identd
--- pidentd-2.8.3/identd.man	Tue Jul 29 06:01:22 1997
+++ identd/identd.8	Wed Jul  1 23:32:04 1998
@@ -4,9 +4,9 @@
 .\"
 .TH IDENTD 8 "27 May 1992"
 .SH NAME
-identd, in.identd \- TCP/IP IDENT protocol server
+identd \- TCP/IP IDENT protocol server
 .SH SYNOPSIS
-.B xDESTDIRx/[in.]identd
+.B identd
 .RB [ \-i | \-w | \-b ]
 .RB [ \-t<seconds> ]
 .RB [ \-u<uid> ]
@@ -322,14 +322,14 @@
 mode of operation.
 .SH EXAMPLES
 Assuming the server is located in
-.B /usr/etc/in.identd
+.B /usr/libexec/identd
 one can put either:
 .PP
-ident stream tcp wait sys /usr/etc/in.identd in.identd -w -t120
+ident stream tcp wait sys /usr/libexec/identd identd -w -t120
 .PP
 or:
 .PP
-ident stream tcp nowait sys /usr/etc/in.identd in.identd -i
+ident stream tcp nowait sys /usr/libexec/identd identd -i
 .PP
 into the
 .B /etc/inetd.conf
@@ -342,7 +342,7 @@
 .B /etc/rc.local
 file:
 .PP
-/usr/etc/in.identd -b -u2 -g2
+/usr/libexec/identd -b -u2 -g2
 .PP
 This will make it run in the background as user 2, group 2 (user "sys",
 group "kmem" on SunOS 4.1.1).
@@ -376,12 +376,12 @@
 one or the other or both.
 .SH FILES
 .TP
-.B xCONFDIRx/identd.conf
+.B /etc/identd.conf
 This file is as yet un-used, but will eventually contain configuration
 options for
 .B identd
 .TP
-.B xCONFDIRx/identd.key
+.B /etc/identd.key
 If compiled with
 .I \-ldes
 this file can be used to specify a secret key for encrypting replies.
%% end patch
