1994-02-05 00:41:32 +03:00
|
|
|
/*
|
1996-08-30 21:41:29 +04:00
|
|
|
** $NetBSD: netbsd.c,v 1.9 1996/08/30 17:41:37 thorpej Exp $
|
1994-02-05 02:17:50 +03:00
|
|
|
**
|
|
|
|
** netbsd.c Low level kernel access functions for NetBSD
|
1994-02-05 00:41:32 +03:00
|
|
|
**
|
|
|
|
** This program is in the public domain and may be used freely by anyone
|
|
|
|
** who wants to.
|
|
|
|
**
|
|
|
|
** Last update: 17 March 1993
|
|
|
|
**
|
|
|
|
** Please send bug fixes/bug reports to: Peter Eriksson <pen@lysator.liu.se>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <ctype.h>
|
1995-07-09 03:57:06 +04:00
|
|
|
#include <limits.h>
|
1994-02-05 00:41:32 +03:00
|
|
|
#include <nlist.h>
|
|
|
|
#include <pwd.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <syslog.h>
|
|
|
|
|
|
|
|
#include "kvm.h"
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
|
|
|
|
#include <sys/socketvar.h>
|
|
|
|
|
1995-03-28 21:18:59 +04:00
|
|
|
#define _KERNEL
|
1994-02-05 00:41:32 +03:00
|
|
|
|
|
|
|
#include <sys/file.h>
|
|
|
|
|
1995-03-28 21:18:59 +04:00
|
|
|
#undef _KERNEL
|
1994-05-14 23:39:03 +04:00
|
|
|
#include <sys/sysctl.h>
|
1994-02-05 00:41:32 +03:00
|
|
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
|
|
|
#include <sys/user.h>
|
|
|
|
|
|
|
|
#include <sys/wait.h>
|
|
|
|
|
|
|
|
#include <net/if.h>
|
|
|
|
#include <net/route.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
|
|
|
|
#include <netinet/in_systm.h>
|
|
|
|
#include <netinet/ip.h>
|
|
|
|
|
|
|
|
#include <netinet/in_pcb.h>
|
|
|
|
|
|
|
|
#include <netinet/tcp.h>
|
|
|
|
#include <netinet/ip_var.h>
|
|
|
|
#include <netinet/tcp_timer.h>
|
|
|
|
#include <netinet/tcp_var.h>
|
|
|
|
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
|
|
|
|
#include "identd.h"
|
|
|
|
#include "error.h"
|
|
|
|
|
|
|
|
|
|
|
|
extern void *calloc();
|
|
|
|
extern void *malloc();
|
|
|
|
|
|
|
|
|
|
|
|
struct nlist nl[] =
|
|
|
|
{
|
|
|
|
#define N_FILE 0
|
|
|
|
#define N_NFILE 1
|
1995-06-19 02:44:10 +04:00
|
|
|
#define N_TCBTABLE 2
|
1994-02-05 00:41:32 +03:00
|
|
|
|
|
|
|
{ "_filehead" },
|
|
|
|
{ "_nfiles" },
|
1995-06-19 02:44:10 +04:00
|
|
|
{ "_tcbtable" },
|
1994-02-05 00:41:32 +03:00
|
|
|
{ "" }
|
|
|
|
};
|
|
|
|
|
1994-05-14 23:39:03 +04:00
|
|
|
static kvm_t *kd;
|
1994-02-05 00:41:32 +03:00
|
|
|
|
|
|
|
static struct file *xfile;
|
|
|
|
static int nfile;
|
|
|
|
|
1995-06-19 02:44:10 +04:00
|
|
|
static struct inpcbtable tcbtable;
|
1994-05-14 23:39:03 +04:00
|
|
|
|
1994-02-05 00:41:32 +03:00
|
|
|
|
|
|
|
int k_open()
|
|
|
|
{
|
1995-07-09 03:53:46 +04:00
|
|
|
char errbuf[_POSIX2_LINE_MAX];
|
|
|
|
|
1994-02-05 00:41:32 +03:00
|
|
|
/*
|
|
|
|
** Open the kernel memory device
|
|
|
|
*/
|
1995-07-09 03:53:46 +04:00
|
|
|
if ((kd = kvm_openfiles(path_unix, path_kmem, NULL, O_RDONLY, errbuf)) ==
|
1994-05-14 23:39:03 +04:00
|
|
|
NULL)
|
1995-07-09 03:53:46 +04:00
|
|
|
ERROR1("main: kvm_open: %s", errbuf);
|
1994-02-05 00:41:32 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
** Extract offsets to the needed variables in the kernel
|
|
|
|
*/
|
1994-05-14 23:39:03 +04:00
|
|
|
if (kvm_nlist(kd, nl) < 0)
|
1994-02-05 00:41:32 +03:00
|
|
|
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;
|
|
|
|
{
|
1994-05-14 23:39:03 +04:00
|
|
|
if (kvm_read(kd, addr, buf, len) < 0)
|
1994-02-05 00:41:32 +03:00
|
|
|
{
|
|
|
|
if (syslog_flag)
|
|
|
|
syslog(LOG_ERR, "getbuf: kvm_read(%08x, %d) - %s : %m",
|
|
|
|
addr, len, what);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Traverse the inpcb list until a match is found.
|
|
|
|
** Returns NULL if no match.
|
|
|
|
*/
|
|
|
|
static struct socket *
|
1995-06-19 02:44:10 +04:00
|
|
|
getlist(tcbtablep, ktcbtablep, faddr, fport, laddr, lport)
|
|
|
|
struct inpcbtable *tcbtablep, *ktcbtablep;
|
|
|
|
struct in_addr *faddr;
|
|
|
|
int fport;
|
|
|
|
struct in_addr *laddr;
|
|
|
|
int lport;
|
1994-02-05 00:41:32 +03:00
|
|
|
{
|
1995-06-19 02:44:10 +04:00
|
|
|
struct inpcb *kpcbp, pcb;
|
|
|
|
|
|
|
|
if (!tcbtablep)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
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"))
|
|
|
|
break;
|
|
|
|
if (pcb.inp_faddr.s_addr == faddr->s_addr &&
|
|
|
|
pcb.inp_laddr.s_addr == laddr->s_addr &&
|
|
|
|
pcb.inp_fport == fport &&
|
|
|
|
pcb.inp_lport == lport )
|
|
|
|
return pcb.inp_socket;
|
|
|
|
}
|
1994-02-05 00:41:32 +03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
** 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;
|
|
|
|
{
|
|
|
|
long addr;
|
|
|
|
struct socket *sockp;
|
1994-05-14 23:39:03 +04:00
|
|
|
int i, mib[2];
|
1994-02-05 00:41:32 +03:00
|
|
|
struct ucred ucb;
|
|
|
|
|
|
|
|
/* -------------------- FILE DESCRIPTOR TABLE -------------------- */
|
|
|
|
if (!getbuf(nl[N_NFILE].n_value, &nfile, sizeof(nfile), "nfile"))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (!getbuf(nl[N_FILE].n_value, &addr, sizeof(addr), "&file"))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
{
|
1994-12-23 17:29:31 +03:00
|
|
|
size_t siz;
|
|
|
|
int rv;
|
1994-02-05 00:41:32 +03:00
|
|
|
|
1994-05-14 23:39:03 +04:00
|
|
|
mib[0] = CTL_KERN;
|
|
|
|
mib[1] = KERN_FILE;
|
|
|
|
if ((rv = sysctl(mib, 2, NULL, &siz, NULL, 0)) == -1)
|
|
|
|
{
|
|
|
|
ERROR1("k_getuid: sysctl 1 (%d)", rv);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
xfile = malloc(siz);
|
|
|
|
if (!xfile)
|
|
|
|
ERROR1("k_getuid: malloc(%d)", siz);
|
|
|
|
if ((rv = sysctl(mib, 2, xfile, &siz, NULL, 0)) == -1)
|
1994-02-05 00:41:32 +03:00
|
|
|
{
|
1994-05-14 23:39:03 +04:00
|
|
|
ERROR1("k_getuid: sysctl 2 (%d)", rv);
|
1994-02-05 00:41:32 +03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
xfile = (struct file *)((char *)xfile + sizeof(filehead));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* -------------------- TCP PCB LIST -------------------- */
|
1995-06-19 02:44:10 +04:00
|
|
|
if (!getbuf(nl[N_TCBTABLE].n_value, &tcbtable, sizeof(tcbtable), "tcbtable"))
|
1994-02-05 00:41:32 +03:00
|
|
|
return -1;
|
|
|
|
|
1995-06-19 02:44:10 +04:00
|
|
|
sockp = getlist(&tcbtable, nl[N_TCBTABLE].n_value, faddr, fport, laddr,
|
|
|
|
lport);
|
1994-02-05 00:41:32 +03:00
|
|
|
|
|
|
|
if (!sockp)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Locate the file descriptor that has the socket in question
|
|
|
|
** open so that we can get the 'ucred' information
|
|
|
|
*/
|
|
|
|
for (i = 0; i < nfile; i++)
|
|
|
|
{
|
|
|
|
if (xfile[i].f_count == 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (xfile[i].f_type == DTYPE_SOCKET &&
|
|
|
|
(struct socket *) xfile[i].f_data == sockp)
|
|
|
|
{
|
|
|
|
if (!getbuf(xfile[i].f_cred, &ucb, sizeof(ucb), "ucb"))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
*uid = ucb.cr_uid;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|