]>
git.saurik.com Git - apple/network_cmds.git/blob - identd.tproj/netbsd.c
83626a43e955a835eb057c516009ed164658fbd3
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * "Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.0 (the 'License'). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
14 * The Original Code and all software distributed under the License are
15 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
22 * @APPLE_LICENSE_HEADER_END@
25 ** $Id: netbsd.c,v 1.1.1.2 2000/01/11 01:48:48 wsanchez Exp $
27 ** netbsd.c Low level kernel access functions for NetBSD
29 ** This program is in the public domain and may be used freely by anyone
32 ** Last update: 17 March 1993
34 ** Please send bug fixes/bug reports to: Peter Eriksson <pen@lysator.liu.se>
47 #include <sys/types.h>
49 #include <sys/param.h>
50 #include <sys/ioctl.h>
51 #include <sys/socket.h>
53 #include <sys/socketvar.h>
60 #include <sys/sysctl.h>
69 #include <net/route.h>
70 #include <netinet/in.h>
72 #include <netinet/in_systm.h>
73 #include <netinet/ip.h>
75 #include <netinet/in_pcb.h>
77 #include <netinet/tcp.h>
78 #include <netinet/ip_var.h>
79 #include <netinet/tcp_timer.h>
80 #include <netinet/tcp_var.h>
82 #include <arpa/inet.h>
88 extern void *calloc();
89 extern void *malloc();
106 static struct file
*xfile
;
109 static struct inpcbhead tcb
;
115 ** Open the kernel memory device
117 if ((kd
= kvm_openfiles(path_unix
, path_kmem
, NULL
, O_RDONLY
, "identd")) ==
119 ERROR("main: kvm_open");
122 ** Extract offsets to the needed variables in the kernel
124 if (kvm_nlist(kd
, nl
) < 0)
125 ERROR("main: kvm_nlist");
132 ** Get a piece of kernel memory with error handling.
133 ** Returns 1 if call succeeded, else 0 (zero).
135 static int getbuf(addr
, buf
, len
, what
)
141 if (kvm_read(kd
, addr
, buf
, len
) < 0)
144 syslog(LOG_ERR
, "getbuf: kvm_read(%08x, %d) - %s : %m",
156 ** Traverse the inpcb list until a match is found.
157 ** Returns NULL if no match.
159 static struct socket
*
160 getlist(pcbp_head
, faddr
, fport
, laddr
, lport
)
161 struct inpcbhead
*pcbp_head
;
162 struct in_addr
*faddr
;
164 struct in_addr
*laddr
;
170 pcbp
= pcbp_head
->lh_first
;
174 sizeof(struct inpcb
),
177 if ( pcb
.inp_faddr
.s_addr
== faddr
->s_addr
&&
178 pcb
.inp_laddr
.s_addr
== laddr
->s_addr
&&
179 pcb
.inp_fport
== fport
&&
180 pcb
.inp_lport
== lport
)
181 return pcb
.inp_socket
;
183 pcbp
= pcb
.inp_list
.le_next
;
192 ** Return the user number for the connection owner
194 int k_getuid(faddr
, fport
, laddr
, lport
, uid
)
195 struct in_addr
*faddr
;
197 struct in_addr
*laddr
;
202 struct socket
*sockp
;
206 /* -------------------- FILE DESCRIPTOR TABLE -------------------- */
207 if (!getbuf(nl
[N_NFILE
].n_value
, &nfile
, sizeof(nfile
), "nfile"))
210 if (!getbuf(nl
[N_FILE
].n_value
, &addr
, sizeof(addr
), "&file"))
218 if ((rv
= sysctl(mib
, 2, NULL
, &siz
, NULL
, 0)) == -1)
220 ERROR1("k_getuid: sysctl 1 (%d)", rv
);
225 ERROR1("k_getuid: malloc(%d)", siz
);
226 if ((rv
= sysctl(mib
, 2, xfile
, &siz
, NULL
, 0)) == -1)
228 ERROR1("k_getuid: sysctl 2 (%d)", rv
);
231 xfile
= (struct file
*)((char *)xfile
+ sizeof(filehead
));
234 /* -------------------- TCP PCB LIST -------------------- */
235 if (!getbuf(nl
[N_TCB
].n_value
, &tcb
, sizeof(tcb
), "tcb"))
238 sockp
= getlist(&tcb
, faddr
, fport
, laddr
, lport
);
244 ** Locate the file descriptor that has the socket in question
245 ** open so that we can get the 'ucred' information
247 for (i
= 0; i
< nfile
; i
++)
249 if (xfile
[i
].f_count
== 0)
252 if (xfile
[i
].f_type
== DTYPE_SOCKET
&&
253 (struct socket
*) xfile
[i
].f_data
== sockp
)
255 if (!getbuf(xfile
[i
].f_cred
, &ucb
, sizeof(ucb
), "ucb"))