From: onoe@sm.sony.co.jp (Atsushi Onoe)
Newsgroups: fj.sys.news
Subject: Re: [Q] Does anybody port pidentd to NEWS-OS 6.x?
Date: 7 Oct 1997 08:00:17 GMT
Organization: ITL, Sony Corporation
Lines: 310
Message-ID: <61cq6h$o8i@csdnews.sm.sony.co.jp>
References: <KJM.97Oct6204542@hyperion.st.ryukoku.ac.jp>
NNTP-Posting-Host: onoe2.sm.sony.co.jp
Path: phoebe.st.ryukoku.ac.jp!rinsgw!rnws.ryukoku.ac.jp!cancer.nca5.ad.jp!news.kpu-m.ac.jp!news.cs.ritsumei.ac.jp!kuis-news!kuee-news!tamaru-news!Q.T.Honey!news01.so-net.or.jp!nr1.scn.co.jp!news.he.net!news-peer.gsl.net!news.gsl.net!gip.net!cpk-news-hub1.bbnplanet.com!news.bbnplanet.com!baron.netcom.net.uk!netcom.net.uk!newsfeed.wizvax.net!news.interq.or.jp!newsfeed.gol.com!nspixp!wnoc-tyo-news!csdnews.sm.sony!onoe

In article <KJM.97Oct6204542@hyperion.st.ryukoku.ac.jp>,
KOJIMA Hajime <kjm@hyperion.st.ryukoku.ac.jp> wrote:
>   $B$I$J$?$+!"(Bpidentd $B$r(B NEWS-OS 6.x $B$K0\?"$5$l$?J}$O$$$i$C$7$c$$$^$9$G$7$g(B
> $B$&$+(B?

$BBg@N$K!"(Bpidentd-2.2 $B$r0\?"$7$?$3$H$,$"$j$^$9!#(B
$B;29M$K$J$k$+$J(B?

$BHx>e(B

*** Makefile.ORG	Thu Nov 18 05:22:41 1993
--- Makefile	Wed Nov 16 18:45:15 1994
***************
*** 35,44 ****
  # GLIBS=-lident
  CFLAGS=-O
  
! all:
  	@echo "Please specify the type of system you wish to build for."
  	@echo "Valid systems are:"
  	@echo ""
  	@echo "	sunos3		SunOS 3.x (tested with 3.5)"
  	@echo "	sunos4		Sun SunOS 4"
  	@echo "	sunos5		Sun SunOS 5 (Solaris 2)"
--- 35,47 ----
  # GLIBS=-lident
  CFLAGS=-O
  
! all:	newsos6
! 
! help:
  	@echo "Please specify the type of system you wish to build for."
  	@echo "Valid systems are:"
  	@echo ""
+ 	@echo "	newsos6		NEWS-OS 6.x"
  	@echo "	sunos3		SunOS 3.x (tested with 3.5)"
  	@echo "	sunos4		Sun SunOS 4"
  	@echo "	sunos5		Sun SunOS 5 (Solaris 2)"
***************
*** 71,76 ****
--- 74,86 ----
  	@echo ""
  	@echo "For example: make sunos4"
  	@echo ""
+ 
+ #
+ # Use this if Sony NEWS-OS 6.*
+ #
+ newsos6:
+ 	@echo "Building for Sony NEWS-OS 6 ..."
+ 	@(cd src ; $(MAKE) CC="$(CC)" LDFLAGS="$(LDFLAGS)" LIBS="-lmld -lsocket -lnsl $(GLIBS)" CFLAGS="$(CFLAGS) -DNEWSOS -DSVR4 $(GDEFS)" KSRC=newsos6 in.identd)
  
  #
  # Use this if Linux 0.99.13q or later
*** src/kvm.c.ORG	Thu Nov 18 05:23:07 1993
--- src/kvm.c	Mon Jan  9 19:05:44 1995
***************
*** 362,366 ****
--- 362,391 ----
  
  #endif
  
+ #ifdef NEWSOS
+ #include <sys/types.h>
+ #include <sys/ksym.h>
+ #include <sys/elf.h>
+ #undef const
+ 
+ nlist(kernel, nl)
+ 	const char *kernel;
+ 	struct nlist *nl;
+ {
+ 	u_long info;
+ 
+ 	while (nl->n_name) {
+ 		info = STT_OBJECT;
+ 		nl->n_value = 0;
+ 		if (getksym(nl->n_name, &nl->n_value, &info) < 0)
+ 			nl->n_type = 0;
+ 		else
+ 			nl->n_type = 1;
+ 		nl++;
+ 	}
+ 	return 0;
+ }
+ #endif /* NEWSOS */
+ 
  #endif
  #endif
*** /tmp/da006z1	Tue Oct  7 16:50:00 1997
--- src/kernel/newsos6.c	Wed Nov 16 20:03:00 1994
***************
*** 0 ****
--- 1,216 ----
+ /*
+ ** kernel/newsos6.c                 NEWS-OS 6 kernel access functions
+ **
+ ** This program is in the public domain and may be used freely by anyone
+ ** who wants to. 
+ **
+ ** Author: Atsushi Onoe <onoe@sm.sony.co.jp>
+ **
+ ** Last update: 16 Nov 1994
+ **
+ ** Please send bug fixes/bug reports to: Peter Eriksson <pen@lysator.liu.se>
+ */
+ 
+ 
+ #define _KMEMUSER
+ #define _KERNEL
+ 
+ /* some definition conflicts. but we must define _KERNEL */
+ 
+ #define exit 		kernel_exit
+ #define strsignal	kernel_strsignal
+ #define	random		kernel_random
+ #define	sleep		kernel_sleep
+ 
+ #include <sys/types.h>
+ #include <sys/socket.h>
+ #include <sys/signal.h>
+ #include <sys/param.h>
+ #include <netinet/in.h>
+ 
+ #include <stdio.h>
+ #include <kvm.h>
+ #include <nlist.h>
+ #include <sys/fcntl.h>
+ #include <sys/cred.h>
+ #include <sys/file.h>
+ #include <sys/stream.h>
+ #include <inet/common.h>
+ #include <inet/ip.h>
+ 
+ #undef exit
+ #undef strsignal
+ #undef random
+ #undef sleep
+ 
+ #include <math.h>
+ 
+ #include <unistd.h>
+ #include <string.h>
+ #include <stddef.h>
+ 
+ #include "identd.h"
+ #include "error.h"
+ 
+ #define N_FANOUT 0
+ #define N_FILE	 1
+ 
+ struct nlist nl[] = {
+ 	{ "ipc_tcp_fanout" },
+ 	{ "file" },
+ 	{ 0 },
+ };
+ 
+ 
+ static kvm_t *kd;
+ 
+ int k_open()
+ {
+   /*
+   ** Open the kernel memory device
+   */
+   if (!(kd = kvm_open(path_unix, path_kmem, NULL, O_RDONLY, NULL)))
+     ERROR("main: kvm_open");
+   
+   /*
+   ** Extract offsets to the needed variables in the kernel
+   */
+   if (kvm_nlist(kd, nl) != 0)
+     ERROR("main: kvm_nlist");
+ 
+   return 0;
+ }
+ 
+ 
+ /*
+ ** Get a piece of kernel memory with error handling.
+ ** Returns 1 if call succeeded, else 0 (zero).
+ */
+ static int getbuf(addr, buf, len, what)
+   long addr;
+   char *buf;
+   int len;
+   char *what;
+ {
+ 
+   if (kvm_read(kd, addr, buf, len) < 0)
+   {
+     if (syslog_flag)
+       syslog(LOG_ERR, "getbuf: kvm_read(%08x, %d) - %s : %m",
+ 	     addr, len, what);
+ 
+     return 0;
+   }
+   
+   return 1;
+ }
+ 
+ 
+ /*
+ ** Return the user number for the connection owner
+ */
+ int k_getuid(faddr, fport, laddr, lport, uid)
+   struct in_addr *faddr;
+   int fport;
+   struct in_addr *laddr;
+   int lport;
+   int *uid;
+ {
+     queue_t *qp, *pq, sqr;
+     ipc_t ic, *icp;
+     unsigned short uslp, usfp;
+     unsigned int offset;
+     unsigned long fp;
+     file_t tf;
+     unsigned long zero = 0;
+     
+     usfp = fport;
+     uslp = lport;
+ 
+     offset = usfp ^ uslp;
+     offset ^= (unsigned) (faddr->s_addr & 0xff) ^ (offset >> 8);
+     offset %= 256;
+     
+     if (!getbuf(nl[N_FANOUT].n_value + sizeof(ipc_t *) * offset,
+ 		(char *) &icp,
+ 		sizeof(ipc_t *),
+ 		"ipc_tcp_fanout[offset]"))
+ 	return -1;
+     
+     if (icp == 0) {
+ 	syslog(LOG_INFO, "k_getuid: Hash miss");
+ 	return -1;
+     }
+     
+     while (icp) {
+ 	if (!getbuf((unsigned long) icp,
+ 		    (char *) &ic,
+ 		    sizeof(ic),
+ 		    "hash entry"))
+ 	    return -1;
+ 
+ 	if (usfp == ic.ipc_tcp_addr[4] && /* remote port */
+ 	    uslp == ic.ipc_tcp_addr[5] && /* local port */
+ #if 0
+ 	    memcmp(&laddr->s_addr, &ic.ipc_tcp_addr[0], 4) == 0 && /* local */
+ #else
+  	    (memcmp(&laddr->s_addr, &ic.ipc_tcp_addr[0], 4) == 0 ||
+  	    /* In SunOS 5.3, the local part can be all zeros */
+  	     memcmp(&zero, &ic.ipc_tcp_addr[0], 4) == 0) /* local */ &&
+ #endif
+ 	    memcmp(&faddr->s_addr, &ic.ipc_tcp_addr[2], 4) == 0)
+ 		break;
+ 	icp = ic.ipc_hash_next;
+     }
+     if (!icp) {
+ 	syslog(LOG_INFO, "k_getuid: Port not found");
+ 	return -1;
+     }
+     
+ #if 0
+     if (!getbuf((unsigned long) ic.ipc_rq+offsetof(queue_t, q_stream),
+ 		(char *) &sqr.q_stream,
+ 		sizeof(sqr.q_stream),
+ 		"queue.q_stream"))
+ 	return -1;
+ #else
+     sqr.q_next = ic.ipc_rq;
+     while (sqr.q_next) {
+ 	if (!getbuf((unsigned long) sqr.q_next, (char *)&sqr, sizeof(sqr),
+ 		    "queue"))
+ 	    return -1;
+     }
+ #endif
+ 
+     /* at this point sqr.q_ptr holds the pointer to the stream we're
+        interested in. Now we're going to find the file pointer
+        that refers to the vnode that refers to this stream stream */
+ 
+     fp = nl[N_FILE].n_value;
+     for (;fp;fp = (unsigned long) tf.f_next) {
+ 	vnode_t vp;
+ 
+ 	if (!getbuf(fp, (char *) &tf, sizeof(file_t),"file pointer"))
+ 	    return -1;
+ 
+ 	if (!tf.f_vnode)
+ 	    continue;
+ 
+ 	if (!getbuf((unsigned long) tf.f_vnode + offsetof(vnode_t,v_stream),
+ 			(char *) &vp.v_stream,
+ 			sizeof(vp.v_stream),"vnode.v_stream"))
+ 	    return -1;
+ 
+ 	if (vp.v_stream == (struct stdata *)sqr.q_ptr) {
+ 	    cred_t cr;
+ 	    if (!getbuf((unsigned long) tf.f_cred + offsetof(cred_t, cr_ruid),
+ 			(char *) &cr.cr_ruid,
+ 			sizeof(cr.cr_ruid),
+ 			"cred.cr_ruid"))
+ 		return -1;
+ 	    *uid = cr.cr_ruid;
+ 	    return 0;
+ 	}
+     }
+     return -1;
+ }
