source: trunk/src/sh_port2proc.c@ 196

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

New option SetDropCache ([false]/true) to drop checksummed files from file cache.

File size: 23.4 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
158struct sock_store {
159 unsigned long sock;
160 size_t pid;
161 char * path;
162 char * user;
163 struct sock_store * next;
164};
165
166#if defined(__linux__)
167#define PROC_PID_MAX _("/proc/sys/kernel/pid_max")
168
169static int proc_max_pid (size_t * procpid)
170{
171 char * ret;
172 unsigned long pid;
173 FILE * fd;
174 char str[128];
175 char * ptr;
176
177 SL_ENTER(_("proc_max_pid"));
178
179 if (0 == access(PROC_PID_MAX, R_OK)) /* flawfinder: ignore */
180 {
181 if (NULL != (fd = fopen(PROC_PID_MAX, "r")))
182 {
183 str[0] = '\0';
184 ret = fgets(str, 128, fd);
185 if (ret && *str != '\0')
186 {
187 pid = strtoul(str, &ptr, 0);
188 if (*ptr == '\0' || *ptr == '\n')
189 {
190 fclose(fd);
191 *procpid = (size_t) pid;
192 SL_RETURN(0, _("proc_max_pid"));
193 }
194 }
195 fclose(fd);
196 }
197 }
198 SL_RETURN((-1), _("proc_max_pid"));
199}
200#else
201static int proc_max_pid(size_t * procpid)
202{
203 *procpid = sh_maxpid;
204 return 0;
205}
206#endif
207
208static struct sock_store * socklist = NULL;
209
210static void del_sock_all()
211{
212 struct sock_store * del = socklist;
213
214 while (del)
215 {
216 socklist = del->next;
217 if (del->path)
218 SH_FREE(del->path);
219 if (del->user)
220 SH_FREE(del->user);
221 SH_FREE(del);
222 del = socklist;
223 }
224 socklist = NULL;
225 return;
226}
227
228static void add_sock(unsigned long sock, size_t pid)
229{
230 struct sock_store * add = SH_ALLOC(sizeof(struct sock_store));
231
232 add->sock = sock;
233 add->pid = pid;
234 add->path = NULL;
235 add->user = NULL;
236 SH_MUTEX_LOCK(mutex_thread_nolog);
237 get_user_and_path(add);
238 SH_MUTEX_UNLOCK(mutex_thread_nolog);
239 add->next = socklist;
240 socklist = add;
241 return;
242}
243
244static void check_and_add_sock(char * fbuf, size_t pid)
245{
246 if (0 == strncmp(_("socket:["), fbuf, 8))
247 {
248 char * end;
249 unsigned long sock;
250 size_t len = strlen(fbuf);
251 if (fbuf[len-1] == ']')
252 fbuf[len-1] = '\0';
253 sock = strtoul(&fbuf[8], &end, 0);
254 if (*end == '\0' && fbuf[8] != '\0')
255 {
256 add_sock(sock, pid);
257 }
258 }
259}
260
261static void fetch_socks(size_t pid)
262{
263 char path[128];
264 DIR * dir;
265 sl_snprintf(path, sizeof(path), _("/proc/%lu/fd"), (unsigned long) pid);
266
267 dir = opendir(path);
268 if (dir)
269 {
270 struct dirent *entry;
271 while (NULL != (entry = readdir(dir)))
272 {
273 char fpath[384];
274 char fbuf[64];
275 int ret;
276 /* /proc/PID/fd/N-> socket:[15713] */
277 sl_snprintf(fpath, sizeof(fpath), _("%s/%s"), path, entry->d_name);
278 ret = readlink(fpath, fbuf, sizeof(fbuf)-1); /* flawfinder: ignore */
279 if (ret > 0)
280 {
281 fbuf[ret] = '\0';
282 check_and_add_sock(fbuf, pid);
283 }
284 }
285 closedir(dir);
286 }
287}
288
289int sh_port2proc_prepare()
290{
291 size_t i;
292
293 if (0 != proc_max_pid(&sh_maxpid))
294 {
295 SH_MUTEX_LOCK(mutex_thread_nolog);
296 sh_error_handle(SH_ERR_ALL, FIL__, __LINE__, 0, MSG_E_SUBGEN,
297 _("Failed to detect max_pid"),
298 _("sh_port2proc"));
299 SH_MUTEX_UNLOCK(mutex_thread_nolog);
300 SL_RETURN ((-1), _("sh_port2proc"));
301 }
302
303 /* Delete old socket list and re-create it
304 */
305 del_sock_all();
306
307 for (i = sh_minpid; i < sh_maxpid; ++i)
308 {
309 fetch_socks(i);
310 }
311
312 return 0;
313}
314
315#include <sys/socket.h>
316#include <netinet/in.h>
317#include <arpa/inet.h>
318
319/* returns the command and fills the 'user' array
320 */
321char * sh_port2proc_query(int proto, struct in_addr * saddr, int sport,
322 char * user, size_t userlen)
323{
324 FILE * fd;
325
326 if (proto == IPPROTO_TCP)
327 fd = fopen("/proc/net/tcp", "r");
328 else
329 fd = fopen("/proc/net/udp", "r");
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 (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 if (new->path)
356 {
357 if (new->user)
358 sl_strlcpy(user, new->user, userlen);
359 else
360 sl_strlcpy(user, "-", userlen);
361 return sh_util_strdup(new->path);
362 }
363 goto err_out;
364 }
365 new = new->next;
366 }
367 }
368 }
369 }
370 fclose(fd);
371 }
372 err_out:
373 sl_strlcpy(user, "0", userlen);
374 return sh_util_strdup("-");
375}
376
377
378/****************************************************************************
379 *
380 * >>> FREEBSD CODE <<<
381 *
382 ****************************************************************************/
383
384#elif defined(__FreeBSD__)
385
386/* Uses code from sockstat.c. Error and memory handling modified.
387 * Only required functions from sockstat.c are included.
388 */
389
390/*-
391 * Copyright (c) 2002 Dag-Erling Co<EF>dan Sm<F8>rgrav
392 * All rights reserved.
393 *
394 * Redistribution and use in source and binary forms, with or without
395 * modification, are permitted provided that the following conditions
396 * are met:
397 * 1. Redistributions of source code must retain the above copyright
398 * notice, this list of conditions and the following disclaimer
399 * in this position and unchanged.
400 * 2. Redistributions in binary form must reproduce the above copyright
401 * notice, this list of conditions and the following disclaimer in the
402 * documentation and/or other materials provided with the distribution.
403 * 3. The name of the author may not be used to endorse or promote products
404 * derived from this software without specific prior written permission.
405 *
406 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
407 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
408 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
409 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
410 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
411 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
412 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
413 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
414 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
415 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
416 */
417#include <sys/cdefs.h>
418__FBSDID("$FreeBSD: src/usr.bin/sockstat/sockstat.c,v 1.13.2.1.4.1 2008/10/02 02:57:24 kensmith Exp $");
419
420#include <sys/param.h>
421#include <sys/socket.h>
422#include <sys/socketvar.h>
423#include <sys/sysctl.h>
424#include <sys/file.h>
425#include <sys/user.h>
426
427#include <sys/un.h>
428#include <sys/unpcb.h>
429
430#include <net/route.h>
431
432#include <netinet/in.h>
433#include <netinet/in_pcb.h>
434#include <netinet/tcp.h>
435#include <netinet/tcp_seq.h>
436#include <netinet/tcp_var.h>
437#include <arpa/inet.h>
438
439#include <ctype.h>
440#include <errno.h>
441#include <netdb.h>
442#include <stdio.h>
443#include <stdlib.h>
444
445#include <unistd.h>
446
447static int opt_4 = 1; /* Show IPv4 sockets */
448static int opt_6 = 0; /* Show IPv6 sockets */
449static int opt_c = 0; /* Show connected sockets */
450static int opt_l = 1; /* Show listening sockets */
451static int opt_v = 0; /* Verbose mode */
452
453struct sock {
454 void *socket;
455 void *pcb;
456 int vflag;
457 int family;
458 int proto;
459
460 struct sockaddr_storage laddr;
461 struct sockaddr_storage faddr;
462 struct sock *next;
463};
464
465#define HASHSIZE 1009
466static struct sock *sockhash[HASHSIZE];
467
468static struct xfile *xfiles;
469static int nxfiles;
470
471
472static void * xrealloc(void * buf, size_t len0, size_t len)
473{
474 if (len > 0)
475 {
476 void * xbuf = SH_ALLOC(len);
477 if (buf)
478 {
479 if (len0 <= len)
480 memcpy(xbuf, buf, len0);
481 else
482 memset(xbuf, '\0', len);
483 SH_FREE(buf);
484 }
485 return xbuf;
486 }
487 SH_FREE(buf);
488 return NULL;
489}
490
491/* Sets address and port in struct sockaddr_storage *sa
492 */
493static void
494sockaddr(struct sockaddr_storage *sa, int af, void *addr, int port)
495{
496 struct sockaddr_in *sin4;
497 struct sockaddr_in6 *sin6;
498
499 bzero(sa, sizeof *sa);
500 switch (af) {
501 case AF_INET:
502 sin4 = (struct sockaddr_in *)sa;
503 sin4->sin_len = sizeof *sin4;
504 sin4->sin_family = af;
505 sin4->sin_port = port;
506 sin4->sin_addr = *(struct in_addr *)addr;
507 break;
508 case AF_INET6:
509 sin6 = (struct sockaddr_in6 *)sa;
510 sin6->sin6_len = sizeof *sin6;
511 sin6->sin6_family = af;
512 sin6->sin6_port = port;
513 sin6->sin6_addr = *(struct in6_addr *)addr;
514 break;
515 default:
516 return;
517 }
518}
519
520/* Get socket information from the kernel.
521 */
522static void
523gather_inet(int proto)
524{
525 struct xinpgen *xig, *exig;
526 struct xinpcb *xip;
527 struct xtcpcb *xtp;
528 struct inpcb *inp;
529 struct xsocket *so;
530 struct sock *sock;
531 char varname[32];
532 size_t len, bufsize, bufsize0;
533 void *buf;
534 int hash, retry, vflag;
535
536 vflag = 0;
537 if (opt_4)
538 vflag |= INP_IPV4;
539 if (opt_6)
540 vflag |= INP_IPV6;
541
542 switch (proto) {
543 case IPPROTO_TCP:
544 sl_strlcpy(varname, _("net.inet.tcp.pcblist"), sizeof(varname));
545 break;
546 case IPPROTO_UDP:
547 sl_strlcpy(varname, _("net.inet.udp.pcblist"), sizeof(varname));
548 break;
549 case IPPROTO_DIVERT:
550 sl_strlcpy(varname, _("net.inet.divert.pcblist"), sizeof(varname));
551 break;
552 default:
553 return;
554 }
555
556 buf = NULL;
557 bufsize = 8192;
558 bufsize0 = bufsize;
559 retry = 5;
560 do {
561 for (;;) {
562 buf = xrealloc(buf, bufsize0, bufsize);
563 bufsize0 = bufsize;
564 len = bufsize;
565 if (sysctlbyname(varname, buf, &len, NULL, 0) == 0)
566 break;
567 if (errno == ENOENT)
568 goto out;
569 if (errno != ENOMEM)
570 {
571 SH_MUTEX_LOCK(mutex_thread_nolog);
572 sh_error_handle(SH_ERR_ERR, FIL__, __LINE__,
573 0, MSG_E_SUBGEN,
574 _("sysctlbyname()"),
575 _("gather_inet"));
576 SH_MUTEX_UNLOCK(mutex_thread_nolog);
577 SH_FREE(buf);
578 return;
579 }
580 bufsize *= 2;
581 }
582 xig = (struct xinpgen *)buf;
583 exig = (struct xinpgen *)(void *)
584 ((char *)buf + len - sizeof *exig);
585 if (xig->xig_len != sizeof *xig ||
586 exig->xig_len != sizeof *exig)
587 {
588 SH_MUTEX_LOCK(mutex_thread_nolog);
589 sh_error_handle(SH_ERR_ERR, FIL__, __LINE__, 0, MSG_E_SUBGEN,
590 _("struct xinpgen size mismatch"),
591 _("gather_inet"));
592 SH_MUTEX_UNLOCK(mutex_thread_nolog);
593 goto out;
594 }
595
596 } while (xig->xig_gen != exig->xig_gen && retry--);
597
598 if (xig->xig_gen != exig->xig_gen && opt_v)
599 {
600 SH_MUTEX_LOCK(mutex_thread_nolog);
601 sh_error_handle(SH_ERR_WARN, FIL__, __LINE__, 0, MSG_E_SUBGEN,
602 _("data may be inconsistent"),
603 _("gather_inet"));
604 SH_MUTEX_UNLOCK(mutex_thread_nolog);
605 }
606
607 for (;;) {
608 xig = (struct xinpgen *)(void *)((char *)xig + xig->xig_len);
609 if (xig >= exig)
610 break;
611 switch (proto) {
612 case IPPROTO_TCP:
613 xtp = (struct xtcpcb *)xig;
614 if (xtp->xt_len != sizeof *xtp) {
615 SH_MUTEX_LOCK(mutex_thread_nolog);
616 sh_error_handle(SH_ERR_WARN, FIL__, __LINE__, 0, MSG_E_SUBGEN,
617 _("struct xtcpcb size mismatch"),
618 _("gather_inet"));
619 SH_MUTEX_UNLOCK(mutex_thread_nolog);
620 goto out;
621 }
622 inp = &xtp->xt_inp;
623 so = &xtp->xt_socket;
624 break;
625 case IPPROTO_UDP:
626 case IPPROTO_DIVERT:
627 xip = (struct xinpcb *)xig;
628 if (xip->xi_len != sizeof *xip) {
629 SH_MUTEX_LOCK(mutex_thread_nolog);
630 sh_error_handle(SH_ERR_WARN, FIL__, __LINE__, 0, MSG_E_SUBGEN,
631 _("struct xinpcb size mismatch"),
632 _("gather_inet"));
633 SH_MUTEX_UNLOCK(mutex_thread_nolog);
634 goto out;
635 }
636 inp = &xip->xi_inp;
637 so = &xip->xi_socket;
638 break;
639 default:
640 return;
641 }
642 if ((inp->inp_vflag & vflag) == 0)
643 continue;
644 if (inp->inp_vflag & INP_IPV4) {
645 if ((inp->inp_fport == 0 && !opt_l) ||
646 (inp->inp_fport != 0 && !opt_c))
647 continue;
648 } else if (inp->inp_vflag & INP_IPV6) {
649 if ((inp->in6p_fport == 0 && !opt_l) ||
650 (inp->in6p_fport != 0 && !opt_c))
651 continue;
652 } else {
653 if (opt_v) {
654 char errmsg[64];
655 sl_snprintf(errmsg, sizeof(errmsg),
656 _("invalid vflag 0x%x"), inp->inp_vflag);
657 SH_MUTEX_LOCK(mutex_thread_nolog);
658 sh_error_handle(SH_ERR_WARN, FIL__, __LINE__, 0, MSG_E_SUBGEN,
659 errmsg,
660 _("gather_inet"));
661 SH_MUTEX_UNLOCK(mutex_thread_nolog);
662 continue;
663 }
664 }
665
666 sock = SH_ALLOC(sizeof *sock);
667 memset(sock, '\0', sizeof (*sock));
668
669 sock->socket = so->xso_so;
670 sock->proto = proto;
671 if (inp->inp_vflag & INP_IPV4) {
672 sock->family = AF_INET;
673 sockaddr(&sock->laddr, sock->family,
674 &inp->inp_laddr, inp->inp_lport);
675 sockaddr(&sock->faddr, sock->family,
676 &inp->inp_faddr, inp->inp_fport);
677 } else if (inp->inp_vflag & INP_IPV6) {
678 sock->family = AF_INET6;
679 sockaddr(&sock->laddr, sock->family,
680 &inp->in6p_laddr, inp->in6p_lport);
681 sockaddr(&sock->faddr, sock->family,
682 &inp->in6p_faddr, inp->in6p_fport);
683 }
684 sock->vflag = inp->inp_vflag;
685
686 hash = (int)((uintptr_t)sock->socket % HASHSIZE);
687 sock->next = sockhash[hash];
688 sockhash[hash] = sock;
689 }
690out:
691 if (buf)
692 SH_FREE(buf);
693}
694
695static void
696getfiles(void)
697{
698 size_t len;
699 size_t len0;
700
701 xfiles = SH_ALLOC(len = sizeof *xfiles);
702 len0 = len;
703
704 while (sysctlbyname(_("kern.file"), xfiles, &len, 0, 0) == -1) {
705 if (errno != ENOMEM)
706 {
707 volatile int status = errno;
708 SH_MUTEX_LOCK(mutex_thread_nolog);
709 sh_error_handle((-1), FIL__, __LINE__, status, MSG_E_SUBGEN,
710 _("sysctlbyname()"),
711 _("getfiles"));
712 SH_MUTEX_UNLOCK(mutex_thread_nolog);
713 }
714 len *= 2;
715 xfiles = xrealloc(xfiles, len0, len);
716 len0 = len;
717 }
718 if (len > 0 && xfiles->xf_size != sizeof *xfiles)
719 if (errno != ENOMEM)
720 {
721 volatile int status = errno;
722 SH_MUTEX_LOCK(mutex_thread_nolog);
723 sh_error_handle((-1), FIL__, __LINE__, status, MSG_E_SUBGEN,
724 _("struct xfile size mismatch"),
725 _("getfiles"));
726 SH_MUTEX_UNLOCK(mutex_thread_nolog);
727 }
728 nxfiles = len / sizeof *xfiles;
729}
730
731static const char *
732getprocname(pid_t pid)
733{
734 static struct kinfo_proc proc;
735 size_t len;
736 int mib[4];
737
738 mib[0] = CTL_KERN;
739 mib[1] = KERN_PROC;
740 mib[2] = KERN_PROC_PID;
741 mib[3] = (int)pid;
742 len = sizeof proc;
743 if (sysctl(mib, 4, &proc, &len, NULL, 0) == -1) {
744 /* Do not warn if the process exits before we get its name. */
745 if (errno != ESRCH)
746 {
747 volatile int status = errno;
748 SH_MUTEX_LOCK(mutex_thread_nolog);
749 sh_error_handle(SH_ERR_WARN, FIL__, __LINE__, status, MSG_E_SUBGEN,
750 _("sysctl()"),
751 _("getfiles"));
752 SH_MUTEX_UNLOCK(mutex_thread_nolog);
753 }
754 return ("-");
755 }
756 return (proc.ki_ocomm);
757}
758
759char * sh_port2proc_query(int proto, struct in_addr * saddr, int sport,
760 char * user, size_t userlen)
761{
762 int n, hash;
763 struct xfile *xf;
764 struct in_addr * haddr;
765 struct sock * s;
766
767 for (xf = xfiles, n = 0; n < nxfiles; ++n, ++xf) {
768
769 if (xf->xf_data == NULL)
770 continue;
771
772 /* Find the socket in sockhash[] that corresponds to it
773 */
774 hash = (int)((uintptr_t)xf->xf_data % HASHSIZE);
775 for (s = sockhash[hash]; s != NULL; s = s->next)
776 if ((void *)s->socket == xf->xf_data)
777 break;
778
779 if (!s)
780 continue;
781
782 /* fprintf(stderr, "FIXME: %d %d, %d %d, %d %d, %d, %d\n", s->proto, proto,
783 s->family, AF_INET,
784 sport, ntohs(((struct sockaddr_in *)(&s->laddr))->sin_port),
785 (int) xf->xf_uid, (int)xf->xf_pid);
786 */
787
788 if (s->proto != proto)
789 continue;
790
791 if (s->family != AF_INET /* && s->family != AF_INET6 */)
792 continue;
793
794 if (sport != ntohs(((struct sockaddr_in *)(&s->laddr))->sin_port))
795 continue;
796
797 haddr = &((struct sockaddr_in *)(&s->laddr))->sin_addr;
798
799 /* fprintf(stderr, "FIXME: %s\n", inet_ntoa(*haddr)); */
800 /* fprintf(stderr, "FIXME: %s\n", inet_ntoa(*saddr)); */
801
802 if (haddr->s_addr == saddr->s_addr || inet_lnaof(*saddr) == INADDR_ANY || inet_lnaof(*haddr) == INADDR_ANY)
803 {
804 struct sock_store try;
805
806 try.pid = xf->xf_pid;
807 try.path = NULL;
808 try.user = NULL;
809 get_user_and_path (&try); /* Try to get info from /proc */
810
811 if (try.path == NULL)
812 {
813 extern char * sh_unix_getUIDname (int level, uid_t uid, char * out, size_t len);
814 char * tmp = sh_unix_getUIDname (SH_ERR_ALL, xf->xf_uid, user, userlen);
815 if (!tmp)
816 sl_snprintf (user, userlen, "%ld", (unsigned long) xf->xf_uid);
817 return sh_util_strdup(getprocname(xf->xf_pid));
818 }
819 else
820 {
821 sl_strlcpy(user, try.user, userlen);
822 SH_FREE(try.user);
823 return try.path;
824 }
825 }
826 }
827 sl_strlcpy(user, "-", userlen);
828 return sh_util_strdup("-");
829}
830
831static void sockdel(struct sock * sock)
832{
833 if (sock)
834 {
835 if (sock->next)
836 sockdel(sock->next);
837 SH_FREE(sock);
838 }
839 return;
840}
841
842int sh_port2proc_prepare()
843{
844 int i;
845
846 if (xfiles)
847 {
848 SH_FREE(xfiles);
849 xfiles = NULL;
850 }
851
852 for (i = 0; i < HASHSIZE; ++i)
853 {
854 sockdel(sockhash[i]);
855 sockhash[i] = NULL;
856 }
857
858 /* Inet connections
859 */
860 gather_inet(IPPROTO_TCP);
861 gather_inet(IPPROTO_UDP);
862 gather_inet(IPPROTO_DIVERT);
863
864 getfiles();
865
866 return 0;
867}
868
869#else /* !defined(__linux__) && !defined(__FreeBSD__) */
870
871char * sh_port2proc_query(int proto, struct in_addr * saddr, int sport,
872 char * user, size_t userlen)
873{
874 (void) proto;
875 (void) saddr;
876 (void) sport;
877
878 sl_strlcpy(user, "-", userlen);
879 return sh_util_strdup("-");
880}
881
882int sh_port2proc_prepare()
883{
884 return 0;
885}
886
887#endif
888
889#endif /* defined(SH_USE_PORTCHECK) */
Note: See TracBrowser for help on using the repository browser.