source: trunk/src/sh_port2proc.c@ 250

Last change on this file since 250 was 250, checked in by katerina, 16 years ago

Proper reporting for udp6 sockets (ticket #168).

File size: 24.3 KB
Line 
1/* SAMHAIN file system integrity testing */
2/* Copyright (C) 2008 Rainer Wichmann */
3/* */
4/* This program is free software; you can redistribute it */
5/* and/or modify */
6/* it under the terms of the GNU General Public License as */
7/* published by */
8/* the Free Software Foundation; either version 2 of the License, or */
9/* (at your option) any later version. */
10/* */
11/* This program is distributed in the hope that it will be useful, */
12/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
13/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
14/* GNU General Public License for more details. */
15/* */
16/* You should have received a copy of the GNU General Public License */
17/* along with this program; if not, write to the Free Software */
18/* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20#include "config_xor.h"
21
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25#include <sys/types.h>
26#include <unistd.h>
27
28#ifdef HAVE_DIRENT_H
29#include <dirent.h>
30#define NAMLEN(dirent) sl_strlen((dirent)->d_name)
31#else
32#define dirent direct
33#define NAMLEN(dirent) (dirent)->d_namlen
34#ifdef HAVE_SYS_NDIR_H
35#include <sys/ndir.h>
36#endif
37#ifdef HAVE_SYS_DIR_H
38#include <sys/dir.h>
39#endif
40#ifdef HAVE_NDIR_H
41#include <ndir.h>
42#endif
43#endif
44#define NEED_ADD_DIRENT
45
46#if defined(SH_USE_PORTCHECK) && (defined (SH_WITH_CLIENT) || defined (SH_STANDALONE))
47
48/****************************************************************************
49 *
50 * >>> COMMON CODE <<<
51 *
52 ****************************************************************************/
53#if defined(__linux__) || defined(__FreeBSD__)
54
55#include "samhain.h"
56#include "sh_error_min.h"
57#include "sh_utils.h"
58#include "sh_pthread.h"
59
60#define FIL__ _("sh_port2proc.c")
61
62struct sock_store {
63 unsigned long sock;
64 size_t pid;
65 char * path;
66 char * user;
67 struct sock_store * next;
68};
69
70/* /proc:
71 * linux: /proc/pid/exe
72 * freebsd: /proc/pid/file
73 * solaris10: /proc/pid/path/a.out
74 */
75static void get_user_and_path (struct sock_store * add)
76{
77 extern char * sh_unix_getUIDname (int level, uid_t uid, char * out, size_t len);
78
79 char path[128];
80 char * buf;
81 struct stat sbuf;
82 int len;
83 char * tmp;
84
85 sl_snprintf (path, sizeof(path), "/proc/%ld/exe", (unsigned long) add->pid);
86
87 if (0 == retry_lstat(FIL__, __LINE__, path, &sbuf) && S_ISLNK(sbuf.st_mode))
88 {
89 goto linkread;
90 }
91
92 sl_snprintf (path, sizeof(path), "/proc/%ld/file", (unsigned long) add->pid);
93
94 if (0 == retry_lstat(FIL__, __LINE__, path, &sbuf) && S_ISLNK(sbuf.st_mode))
95 {
96 goto linkread;
97 }
98
99 sl_snprintf (path, sizeof(path), "/proc/%ld/path/a.out", (unsigned long) add->pid);
100
101 if (0 == retry_lstat(FIL__, __LINE__, path, &sbuf) && S_ISLNK(sbuf.st_mode))
102 {
103 goto linkread;
104 }
105
106 return;
107
108 linkread:
109
110 buf = SH_ALLOC(PATH_MAX);
111 len = readlink(path, buf, PATH_MAX); /* flawfinder: ignore */
112 len = (len >= PATH_MAX) ? (PATH_MAX-1) : len;
113
114 if (len > 0)
115 {
116 buf[len] = '\0';
117 add->path = buf;
118 }
119 else
120 {
121 SH_FREE(buf);
122 }
123
124 add->user = SH_ALLOC(USER_MAX);
125 tmp = sh_unix_getUIDname (SH_ERR_ALL, sbuf.st_uid, add->user, USER_MAX);
126
127 if (!tmp)
128 sl_snprintf (add->user, USER_MAX, "%ld", (unsigned long) sbuf.st_uid);
129
130 return;
131}
132
133#endif
134
135/****************************************************************************
136 *
137 * >>> LINUX CODE <<<
138 *
139 ****************************************************************************/
140
141#if defined(__linux__)
142
143static size_t sh_minpid = 0x0001;
144static size_t sh_maxpid = 0x8000;
145
146#ifndef HAVE_LSTAT
147#define lstat(x,y) stat(x,y)
148#endif /* HAVE_LSTAT */
149
150#if defined(S_IFLNK) && !defined(S_ISLNK)
151# define S_ISLNK(mode) (((mode) & S_IFMT) == S_IFLNK)
152#else
153# if !defined(S_ISLNK)
154# define S_ISLNK(mode) (0)
155# endif
156#endif
157
158#if defined(__linux__)
159#define PROC_PID_MAX _("/proc/sys/kernel/pid_max")
160
161static int proc_max_pid (size_t * procpid)
162{
163 char * ret;
164 unsigned long pid;
165 FILE * fd;
166 char str[128];
167 char * ptr;
168
169 SL_ENTER(_("proc_max_pid"));
170
171 if (0 == access(PROC_PID_MAX, R_OK)) /* flawfinder: ignore */
172 {
173 if (NULL != (fd = fopen(PROC_PID_MAX, "r")))
174 {
175 str[0] = '\0';
176 ret = fgets(str, 128, fd);
177 if (ret && *str != '\0')
178 {
179 pid = strtoul(str, &ptr, 0);
180 if (*ptr == '\0' || *ptr == '\n')
181 {
182 fclose(fd);
183 *procpid = (size_t) pid;
184 SL_RETURN(0, _("proc_max_pid"));
185 }
186 }
187 fclose(fd);
188 }
189 }
190 SL_RETURN((-1), _("proc_max_pid"));
191}
192#else
193static int proc_max_pid(size_t * procpid)
194{
195 *procpid = sh_maxpid;
196 return 0;
197}
198#endif
199
200static struct sock_store * socklist = NULL;
201
202static void del_sock_all()
203{
204 struct sock_store * del = socklist;
205
206 while (del)
207 {
208 socklist = del->next;
209 if (del->path)
210 SH_FREE(del->path);
211 if (del->user)
212 SH_FREE(del->user);
213 SH_FREE(del);
214 del = socklist;
215 }
216 socklist = NULL;
217 return;
218}
219
220static void add_sock(unsigned long sock, size_t pid)
221{
222 struct sock_store * add = SH_ALLOC(sizeof(struct sock_store));
223
224 add->sock = sock;
225 add->pid = pid;
226 add->path = NULL;
227 add->user = NULL;
228 SH_MUTEX_LOCK(mutex_thread_nolog);
229 get_user_and_path(add);
230 SH_MUTEX_UNLOCK(mutex_thread_nolog);
231 add->next = socklist;
232 socklist = add;
233 return;
234}
235
236static void check_and_add_sock(char * fbuf, size_t pid)
237{
238 if (0 == strncmp(_("socket:["), fbuf, 8))
239 {
240 char * end;
241 unsigned long sock;
242 size_t len = strlen(fbuf);
243 if (fbuf[len-1] == ']')
244 fbuf[len-1] = '\0';
245 sock = strtoul(&fbuf[8], &end, 0);
246 if (*end == '\0' && fbuf[8] != '\0')
247 {
248 add_sock(sock, pid);
249 }
250 }
251}
252
253static void fetch_socks(size_t pid)
254{
255 char path[128];
256 DIR * dir;
257 sl_snprintf(path, sizeof(path), _("/proc/%lu/fd"), (unsigned long) pid);
258
259 dir = opendir(path);
260 if (dir)
261 {
262 struct dirent *entry;
263 while (NULL != (entry = readdir(dir)))
264 {
265 char fpath[384];
266 char fbuf[64];
267 int ret;
268 /* /proc/PID/fd/N-> socket:[15713] */
269 sl_snprintf(fpath, sizeof(fpath), _("%s/%s"), path, entry->d_name);
270 ret = readlink(fpath, fbuf, sizeof(fbuf)-1); /* flawfinder: ignore */
271 if (ret > 0)
272 {
273 fbuf[ret] = '\0';
274 check_and_add_sock(fbuf, pid);
275 }
276 }
277 closedir(dir);
278 }
279}
280
281int sh_port2proc_prepare()
282{
283 size_t i;
284
285 if (0 != proc_max_pid(&sh_maxpid))
286 {
287 SH_MUTEX_LOCK(mutex_thread_nolog);
288 sh_error_handle(SH_ERR_ALL, FIL__, __LINE__, 0, MSG_E_SUBGEN,
289 _("Failed to detect max_pid"),
290 _("sh_port2proc"));
291 SH_MUTEX_UNLOCK(mutex_thread_nolog);
292 SL_RETURN ((-1), _("sh_port2proc"));
293 }
294
295 /* Delete old socket list and re-create it
296 */
297 del_sock_all();
298
299 for (i = sh_minpid; i < sh_maxpid; ++i)
300 {
301 fetch_socks(i);
302 }
303
304 return 0;
305}
306
307void sh_port2proc_finish()
308{
309 /* Delete old socket list
310 */
311 del_sock_all();
312 return;
313}
314
315
316#include <sys/socket.h>
317#include <netinet/in.h>
318#include <arpa/inet.h>
319
320/* returns the command and fills the 'user' array
321 */
322static char * port2proc_query(char * file, int proto, struct in_addr * saddr, int sport,
323 unsigned long * pid, char * user, size_t userlen)
324{
325 FILE * fd;
326
327 fd = fopen(file, "r");
328
329 *pid = 0;
330
331 if (fd)
332 {
333 int n, iface, port, inode;
334 char line[512];
335
336 while (NULL != fgets(line, sizeof(line), fd))
337 {
338
339 if (4 == sscanf(line,
340 "%d: %X:%X %*X:%*X %*X %*X:%*X %*X:%*X %*X %*d %*d %d %*s",
341 &n, &iface, &port, &inode))
342 {
343 struct in_addr haddr;
344 haddr.s_addr = (unsigned long)iface;
345
346 if ((proto == IPPROTO_UDP || haddr.s_addr == saddr->s_addr) && port == sport)
347 {
348 struct sock_store * new = socklist;
349
350 while (new)
351 {
352 if ((unsigned int)inode == new->sock)
353 {
354 fclose(fd);
355 *pid = (unsigned long) new->pid;
356 if (new->path)
357 {
358 if (new->user)
359 sl_strlcpy(user, new->user, userlen);
360 else
361 sl_strlcpy(user, "-", userlen);
362 return sh_util_strdup(new->path);
363 }
364 goto err_out;
365 }
366 new = new->next;
367 }
368 }
369 }
370 }
371 fclose(fd);
372 }
373 err_out:
374 sl_strlcpy(user, "-", userlen);
375 return sh_util_strdup("-");
376}
377
378/* returns the command and fills the 'user' array
379 */
380char * sh_port2proc_query(int proto, struct in_addr * saddr, int sport,
381 unsigned long * pid, char * user, size_t userlen)
382{
383 char file[32];
384
385 if (proto == IPPROTO_TCP)
386 {
387 sl_strlcpy(file, _("/proc/net/tcp"), sizeof(file));
388 return port2proc_query(file, proto, saddr, sport, pid, user, userlen);
389 }
390 else
391 {
392 char * ret;
393 sl_strlcpy(file, _("/proc/net/udp"), sizeof(file));
394 ret = port2proc_query(file, proto, saddr, sport, pid, user, userlen);
395 if (ret[0] == '-' && ret[1] == '\0')
396 {
397 SH_FREE(ret);
398 sl_strlcpy(file, _("/proc/net/udp6"), sizeof(file));
399 ret = port2proc_query(file, proto, saddr, sport, pid, user, userlen);
400 }
401 return ret;
402 }
403}
404
405
406/****************************************************************************
407 *
408 * >>> FREEBSD CODE <<<
409 *
410 ****************************************************************************/
411
412#elif defined(__FreeBSD__)
413
414/* Uses code from sockstat.c. Error and memory handling modified.
415 * Only required functions from sockstat.c are included.
416 */
417
418/*-
419 * Copyright (c) 2002 Dag-Erling Co<EF>dan Sm<F8>rgrav
420 * All rights reserved.
421 *
422 * Redistribution and use in source and binary forms, with or without
423 * modification, are permitted provided that the following conditions
424 * are met:
425 * 1. Redistributions of source code must retain the above copyright
426 * notice, this list of conditions and the following disclaimer
427 * in this position and unchanged.
428 * 2. Redistributions in binary form must reproduce the above copyright
429 * notice, this list of conditions and the following disclaimer in the
430 * documentation and/or other materials provided with the distribution.
431 * 3. The name of the author may not be used to endorse or promote products
432 * derived from this software without specific prior written permission.
433 *
434 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
435 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
436 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
437 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
438 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
439 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
440 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
441 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
442 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
443 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
444 */
445#include <sys/cdefs.h>
446__FBSDID("$FreeBSD: src/usr.bin/sockstat/sockstat.c,v 1.13.2.1.4.1 2008/10/02 02:57:24 kensmith Exp $");
447
448#include <sys/param.h>
449#include <sys/socket.h>
450#include <sys/socketvar.h>
451#include <sys/sysctl.h>
452#include <sys/file.h>
453#include <sys/user.h>
454
455#include <sys/un.h>
456#include <sys/unpcb.h>
457
458#include <net/route.h>
459
460#include <netinet/in.h>
461#include <netinet/in_pcb.h>
462#include <netinet/tcp.h>
463#include <netinet/tcp_seq.h>
464#include <netinet/tcp_var.h>
465#include <arpa/inet.h>
466
467#include <ctype.h>
468#include <errno.h>
469#include <netdb.h>
470#include <stdio.h>
471#include <stdlib.h>
472
473#include <unistd.h>
474
475static int opt_4 = 1; /* Show IPv4 sockets */
476static int opt_6 = 0; /* Show IPv6 sockets */
477static int opt_c = 0; /* Show connected sockets */
478static int opt_l = 1; /* Show listening sockets */
479static int opt_v = 0; /* Verbose mode */
480
481struct sock {
482 void *socket;
483 void *pcb;
484 int vflag;
485 int family;
486 int proto;
487
488 struct sockaddr_storage laddr;
489 struct sockaddr_storage faddr;
490 struct sock *next;
491};
492
493#define HASHSIZE 1009
494static struct sock *sockhash[HASHSIZE];
495
496static struct xfile *xfiles;
497static int nxfiles;
498
499
500static void * xrealloc(void * buf, size_t len0, size_t len)
501{
502 if (len > 0)
503 {
504 void * xbuf = SH_ALLOC(len);
505 if (buf)
506 {
507 if (len0 <= len)
508 memcpy(xbuf, buf, len0);
509 else
510 memset(xbuf, '\0', len);
511 SH_FREE(buf);
512 }
513 return xbuf;
514 }
515 SH_FREE(buf);
516 return NULL;
517}
518
519/* Sets address and port in struct sockaddr_storage *sa
520 */
521static void
522sockaddr(struct sockaddr_storage *sa, int af, void *addr, int port)
523{
524 struct sockaddr_in *sin4;
525 struct sockaddr_in6 *sin6;
526
527 bzero(sa, sizeof *sa);
528 switch (af) {
529 case AF_INET:
530 sin4 = (struct sockaddr_in *)sa;
531 sin4->sin_len = sizeof *sin4;
532 sin4->sin_family = af;
533 sin4->sin_port = port;
534 sin4->sin_addr = *(struct in_addr *)addr;
535 break;
536 case AF_INET6:
537 sin6 = (struct sockaddr_in6 *)sa;
538 sin6->sin6_len = sizeof *sin6;
539 sin6->sin6_family = af;
540 sin6->sin6_port = port;
541 sin6->sin6_addr = *(struct in6_addr *)addr;
542 break;
543 default:
544 return;
545 }
546}
547
548/* Get socket information from the kernel.
549 */
550static void
551gather_inet(int proto)
552{
553 struct xinpgen *xig, *exig;
554 struct xinpcb *xip;
555 struct xtcpcb *xtp;
556 struct inpcb *inp;
557 struct xsocket *so;
558 struct sock *sock;
559 char varname[32];
560 size_t len, bufsize, bufsize0;
561 void *buf;
562 int hash, retry, vflag;
563
564 vflag = 0;
565 if (opt_4)
566 vflag |= INP_IPV4;
567 if (opt_6)
568 vflag |= INP_IPV6;
569
570 switch (proto) {
571 case IPPROTO_TCP:
572 sl_strlcpy(varname, _("net.inet.tcp.pcblist"), sizeof(varname));
573 break;
574 case IPPROTO_UDP:
575 sl_strlcpy(varname, _("net.inet.udp.pcblist"), sizeof(varname));
576 break;
577 case IPPROTO_DIVERT:
578 sl_strlcpy(varname, _("net.inet.divert.pcblist"), sizeof(varname));
579 break;
580 default:
581 return;
582 }
583
584 buf = NULL;
585 bufsize = 8192;
586 bufsize0 = bufsize;
587 retry = 5;
588 do {
589 for (;;) {
590 buf = xrealloc(buf, bufsize0, bufsize);
591 bufsize0 = bufsize;
592 len = bufsize;
593 if (sysctlbyname(varname, buf, &len, NULL, 0) == 0)
594 break;
595 if (errno == ENOENT)
596 goto out;
597 if (errno != ENOMEM)
598 {
599 SH_MUTEX_LOCK(mutex_thread_nolog);
600 sh_error_handle(SH_ERR_ERR, FIL__, __LINE__,
601 0, MSG_E_SUBGEN,
602 _("sysctlbyname()"),
603 _("gather_inet"));
604 SH_MUTEX_UNLOCK(mutex_thread_nolog);
605 SH_FREE(buf);
606 return;
607 }
608 bufsize *= 2;
609 }
610 xig = (struct xinpgen *)buf;
611 exig = (struct xinpgen *)(void *)
612 ((char *)buf + len - sizeof *exig);
613 if (xig->xig_len != sizeof *xig ||
614 exig->xig_len != sizeof *exig)
615 {
616 SH_MUTEX_LOCK(mutex_thread_nolog);
617 sh_error_handle(SH_ERR_ERR, FIL__, __LINE__, 0, MSG_E_SUBGEN,
618 _("struct xinpgen size mismatch"),
619 _("gather_inet"));
620 SH_MUTEX_UNLOCK(mutex_thread_nolog);
621 goto out;
622 }
623
624 } while (xig->xig_gen != exig->xig_gen && retry--);
625
626 if (xig->xig_gen != exig->xig_gen && opt_v)
627 {
628 SH_MUTEX_LOCK(mutex_thread_nolog);
629 sh_error_handle(SH_ERR_WARN, FIL__, __LINE__, 0, MSG_E_SUBGEN,
630 _("data may be inconsistent"),
631 _("gather_inet"));
632 SH_MUTEX_UNLOCK(mutex_thread_nolog);
633 }
634
635 for (;;) {
636 xig = (struct xinpgen *)(void *)((char *)xig + xig->xig_len);
637 if (xig >= exig)
638 break;
639 switch (proto) {
640 case IPPROTO_TCP:
641 xtp = (struct xtcpcb *)xig;
642 if (xtp->xt_len != sizeof *xtp) {
643 SH_MUTEX_LOCK(mutex_thread_nolog);
644 sh_error_handle(SH_ERR_WARN, FIL__, __LINE__, 0, MSG_E_SUBGEN,
645 _("struct xtcpcb size mismatch"),
646 _("gather_inet"));
647 SH_MUTEX_UNLOCK(mutex_thread_nolog);
648 goto out;
649 }
650 inp = &xtp->xt_inp;
651 so = &xtp->xt_socket;
652 break;
653 case IPPROTO_UDP:
654 case IPPROTO_DIVERT:
655 xip = (struct xinpcb *)xig;
656 if (xip->xi_len != sizeof *xip) {
657 SH_MUTEX_LOCK(mutex_thread_nolog);
658 sh_error_handle(SH_ERR_WARN, FIL__, __LINE__, 0, MSG_E_SUBGEN,
659 _("struct xinpcb size mismatch"),
660 _("gather_inet"));
661 SH_MUTEX_UNLOCK(mutex_thread_nolog);
662 goto out;
663 }
664 inp = &xip->xi_inp;
665 so = &xip->xi_socket;
666 break;
667 default:
668 return;
669 }
670 if ((inp->inp_vflag & vflag) == 0)
671 continue;
672 if (inp->inp_vflag & INP_IPV4) {
673 if ((inp->inp_fport == 0 && !opt_l) ||
674 (inp->inp_fport != 0 && !opt_c))
675 continue;
676 } else if (inp->inp_vflag & INP_IPV6) {
677 if ((inp->in6p_fport == 0 && !opt_l) ||
678 (inp->in6p_fport != 0 && !opt_c))
679 continue;
680 } else {
681 if (opt_v) {
682 char errmsg[64];
683 sl_snprintf(errmsg, sizeof(errmsg),
684 _("invalid vflag 0x%x"), inp->inp_vflag);
685 SH_MUTEX_LOCK(mutex_thread_nolog);
686 sh_error_handle(SH_ERR_WARN, FIL__, __LINE__, 0, MSG_E_SUBGEN,
687 errmsg,
688 _("gather_inet"));
689 SH_MUTEX_UNLOCK(mutex_thread_nolog);
690 continue;
691 }
692 }
693
694 sock = SH_ALLOC(sizeof *sock);
695 memset(sock, '\0', sizeof (*sock));
696
697 sock->socket = so->xso_so;
698 sock->proto = proto;
699 if (inp->inp_vflag & INP_IPV4) {
700 sock->family = AF_INET;
701 sockaddr(&sock->laddr, sock->family,
702 &inp->inp_laddr, inp->inp_lport);
703 sockaddr(&sock->faddr, sock->family,
704 &inp->inp_faddr, inp->inp_fport);
705 } else if (inp->inp_vflag & INP_IPV6) {
706 sock->family = AF_INET6;
707 sockaddr(&sock->laddr, sock->family,
708 &inp->in6p_laddr, inp->in6p_lport);
709 sockaddr(&sock->faddr, sock->family,
710 &inp->in6p_faddr, inp->in6p_fport);
711 }
712 sock->vflag = inp->inp_vflag;
713
714 hash = (int)((uintptr_t)sock->socket % HASHSIZE);
715 sock->next = sockhash[hash];
716 sockhash[hash] = sock;
717 }
718out:
719 if (buf)
720 SH_FREE(buf);
721}
722
723static void
724getfiles(void)
725{
726 size_t len;
727 size_t len0;
728
729 xfiles = SH_ALLOC(len = sizeof *xfiles);
730 len0 = len;
731
732 while (sysctlbyname(_("kern.file"), xfiles, &len, 0, 0) == -1) {
733 if (errno != ENOMEM)
734 {
735 volatile int status = errno;
736 SH_MUTEX_LOCK(mutex_thread_nolog);
737 sh_error_handle((-1), FIL__, __LINE__, status, MSG_E_SUBGEN,
738 _("sysctlbyname()"),
739 _("getfiles"));
740 SH_MUTEX_UNLOCK(mutex_thread_nolog);
741 }
742 len *= 2;
743 xfiles = xrealloc(xfiles, len0, len);
744 len0 = len;
745 }
746 if (len > 0 && xfiles->xf_size != sizeof *xfiles)
747 if (errno != ENOMEM)
748 {
749 volatile int status = errno;
750 SH_MUTEX_LOCK(mutex_thread_nolog);
751 sh_error_handle((-1), FIL__, __LINE__, status, MSG_E_SUBGEN,
752 _("struct xfile size mismatch"),
753 _("getfiles"));
754 SH_MUTEX_UNLOCK(mutex_thread_nolog);
755 }
756 nxfiles = len / sizeof *xfiles;
757}
758
759static const char *
760getprocname(pid_t pid)
761{
762 static struct kinfo_proc proc;
763 size_t len;
764 int mib[4];
765
766 mib[0] = CTL_KERN;
767 mib[1] = KERN_PROC;
768 mib[2] = KERN_PROC_PID;
769 mib[3] = (int)pid;
770 len = sizeof proc;
771 if (sysctl(mib, 4, &proc, &len, NULL, 0) == -1) {
772 /* Do not warn if the process exits before we get its name. */
773 if (errno != ESRCH)
774 {
775 volatile int status = errno;
776 SH_MUTEX_LOCK(mutex_thread_nolog);
777 sh_error_handle(SH_ERR_WARN, FIL__, __LINE__, status, MSG_E_SUBGEN,
778 _("sysctl()"),
779 _("getfiles"));
780 SH_MUTEX_UNLOCK(mutex_thread_nolog);
781 }
782 return ("-");
783 }
784 return (proc.ki_ocomm);
785}
786
787char * sh_port2proc_query(int proto, struct in_addr * saddr, int sport,
788 unsigned long * pid, char * user, size_t userlen)
789{
790 int n, hash;
791 struct xfile *xf;
792 struct in_addr * haddr;
793 struct sock * s;
794
795 *pid = 0;
796
797 for (xf = xfiles, n = 0; n < nxfiles; ++n, ++xf) {
798
799 if (xf->xf_data == NULL)
800 continue;
801
802 /* Find the socket in sockhash[] that corresponds to it
803 */
804 hash = (int)((uintptr_t)xf->xf_data % HASHSIZE);
805 for (s = sockhash[hash]; s != NULL; s = s->next)
806 if ((void *)s->socket == xf->xf_data)
807 break;
808
809 if (!s)
810 continue;
811
812 /* fprintf(stderr, "FIXME: %d %d, %d %d, %d %d, %d, %d\n", s->proto, proto,
813 s->family, AF_INET,
814 sport, ntohs(((struct sockaddr_in *)(&s->laddr))->sin_port),
815 (int) xf->xf_uid, (int)xf->xf_pid);
816 */
817
818 if (s->proto != proto)
819 continue;
820
821 if (s->family != AF_INET /* && s->family != AF_INET6 */)
822 continue;
823
824 if (sport != ntohs(((struct sockaddr_in *)(&s->laddr))->sin_port))
825 continue;
826
827 haddr = &((struct sockaddr_in *)(&s->laddr))->sin_addr;
828
829 /* fprintf(stderr, "FIXME: %s\n", inet_ntoa(*haddr)); */
830 /* fprintf(stderr, "FIXME: %s\n", inet_ntoa(*saddr)); */
831
832 if (haddr->s_addr == saddr->s_addr || inet_lnaof(*saddr) == INADDR_ANY || inet_lnaof(*haddr) == INADDR_ANY)
833 {
834 struct sock_store try;
835
836 *pid = xf->xf_pid;
837
838 try.pid = xf->xf_pid;
839 try.path = NULL;
840 try.user = NULL;
841 get_user_and_path (&try); /* Try to get info from /proc */
842
843 if (try.path == NULL)
844 {
845 extern char * sh_unix_getUIDname (int level, uid_t uid, char * out, size_t len);
846 char * tmp = sh_unix_getUIDname (SH_ERR_ALL, xf->xf_uid, user, userlen);
847 if (!tmp)
848 sl_snprintf (user, userlen, "%ld", (unsigned long) xf->xf_uid);
849 return sh_util_strdup(getprocname(xf->xf_pid));
850 }
851 else
852 {
853 sl_strlcpy(user, try.user, userlen);
854 SH_FREE(try.user);
855 return try.path;
856 }
857 }
858 }
859 sl_strlcpy(user, "-", userlen);
860 return sh_util_strdup("-");
861}
862
863static void sockdel(struct sock * sock)
864{
865 if (sock)
866 {
867 if (sock->next)
868 sockdel(sock->next);
869 SH_FREE(sock);
870 }
871 return;
872}
873
874int sh_port2proc_prepare()
875{
876 int i;
877
878 if (xfiles)
879 {
880 SH_FREE(xfiles);
881 xfiles = NULL;
882 }
883
884 for (i = 0; i < HASHSIZE; ++i)
885 {
886 sockdel(sockhash[i]);
887 sockhash[i] = NULL;
888 }
889
890 /* Inet connections
891 */
892 gather_inet(IPPROTO_TCP);
893 gather_inet(IPPROTO_UDP);
894 gather_inet(IPPROTO_DIVERT);
895
896 getfiles();
897
898 return 0;
899}
900
901void sh_port2proc_finish()
902{
903 return;
904}
905
906#else /* !defined(__linux__) && !defined(__FreeBSD__) */
907
908char * sh_port2proc_query(int proto, struct in_addr * saddr, int sport,
909 unsigned long * pid, char * user, size_t userlen)
910{
911 (void) proto;
912 (void) saddr;
913 (void) sport;
914
915 *pid = 0;
916
917 sl_strlcpy(user, "-", userlen);
918 return sh_util_strdup("-");
919}
920
921int sh_port2proc_prepare()
922{
923 return 0;
924}
925
926void sh_port2proc_finish()
927{
928 return;
929}
930#endif
931
932#endif /* defined(SH_USE_PORTCHECK) */
Note: See TracBrowser for help on using the repository browser.