source: trunk/src/sh_port2proc.c@ 193

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

Report command/user for open ports on FreeBSD.

File size: 22.2 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
62#endif
63
64/****************************************************************************
65 *
66 * >>> LINUX CODE <<<
67 *
68 ****************************************************************************/
69
70#if defined(__linux__)
71
72static size_t sh_minpid = 0x0001;
73static size_t sh_maxpid = 0x8000;
74
75#ifndef HAVE_LSTAT
76#define lstat(x,y) stat(x,y)
77#endif /* HAVE_LSTAT */
78
79#if defined(S_IFLNK) && !defined(S_ISLNK)
80# define S_ISLNK(mode) (((mode) & S_IFMT) == S_IFLNK)
81#else
82# if !defined(S_ISLNK)
83# define S_ISLNK(mode) (0)
84# endif
85#endif
86
87struct sock_store {
88 unsigned long sock;
89 size_t pid;
90 char * path;
91 char * user;
92 struct sock_store * next;
93};
94
95/* /proc:
96 * linux: /proc/pid/exe
97 * freebsd: /proc/pid/file
98 * solaris10: /proc/pid/path/a.out
99 */
100static void get_user_and_path (struct sock_store * add)
101{
102 extern char * sh_unix_getUIDname (int level, uid_t uid, char * out, size_t len);
103
104 char path[128];
105 char * buf;
106 struct stat sbuf;
107 int len;
108 char * tmp;
109
110 sl_snprintf (path, sizeof(path), "/proc/%ld/exe", (unsigned long) add->pid);
111
112 if (0 == retry_lstat(FIL__, __LINE__, path, &sbuf) && S_ISLNK(sbuf.st_mode))
113 {
114 goto linkread;
115 }
116
117 sl_snprintf (path, sizeof(path), "/proc/%ld/file", (unsigned long) add->pid);
118
119 if (0 == retry_lstat(FIL__, __LINE__, path, &sbuf) && S_ISLNK(sbuf.st_mode))
120 {
121 goto linkread;
122 }
123
124 sl_snprintf (path, sizeof(path), "/proc/%ld/path/a.out", (unsigned long) add->pid);
125
126 if (0 == retry_lstat(FIL__, __LINE__, path, &sbuf) && S_ISLNK(sbuf.st_mode))
127 {
128 goto linkread;
129 }
130
131 return;
132
133 linkread:
134
135 buf = SH_ALLOC(PATH_MAX);
136 len = readlink(path, buf, PATH_MAX); /* flawfinder: ignore */
137 len = (len >= PATH_MAX) ? (PATH_MAX-1) : len;
138
139 if (len > 0)
140 {
141 buf[len] = '\0';
142 add->path = buf;
143 }
144 else
145 {
146 SH_FREE(buf);
147 }
148
149 add->user = SH_ALLOC(USER_MAX);
150 tmp = sh_unix_getUIDname (SH_ERR_ALL, sbuf.st_uid, add->user, USER_MAX);
151
152 if (!tmp)
153 sl_snprintf (add->user, USER_MAX, "%ld", (unsigned long) sbuf.st_uid);
154
155 return;
156}
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
307#include <sys/socket.h>
308#include <netinet/in.h>
309#include <arpa/inet.h>
310
311/* returns the command and fills the 'user' array
312 */
313char * sh_port2proc_query(int proto, struct in_addr * saddr, int sport,
314 char * user, size_t userlen)
315{
316 FILE * fd;
317
318 if (proto == IPPROTO_TCP)
319 fd = fopen("/proc/net/tcp", "r");
320 else
321 fd = fopen("/proc/net/udp", "r");
322
323 if (fd)
324 {
325 int n, iface, port, inode;
326 char line[512];
327
328 while (NULL != fgets(line, sizeof(line), fd))
329 {
330
331 if (4 == sscanf(line,
332 "%d: %X:%X %*X:%*X %*X %*X:%*X %*X:%*X %*X %*d %*d %d %*s",
333 &n, &iface, &port, &inode))
334 {
335 struct in_addr haddr;
336 haddr.s_addr = (unsigned long)iface;
337
338 if (haddr.s_addr == saddr->s_addr && port == sport)
339 {
340 struct sock_store * new = socklist;
341
342 while (new)
343 {
344 if ((unsigned int)inode == new->sock)
345 {
346 fclose(fd);
347 if (new->path)
348 {
349 if (new->user)
350 sl_strlcpy(user, new->user, userlen);
351 else
352 sl_strlcpy(user, "-", userlen);
353 return sh_util_strdup(new->path);
354 }
355 goto err_out;
356 }
357 new = new->next;
358 }
359 }
360 }
361 }
362 fclose(fd);
363 }
364 err_out:
365 sl_strlcpy(user, "0", userlen);
366 return sh_util_strdup("-");
367}
368
369
370/****************************************************************************
371 *
372 * >>> FREEBSD CODE <<<
373 *
374 ****************************************************************************/
375
376#elif defined(__FreeBSD__)
377
378/* Uses code from sockstat.c. Error and memory handling modified.
379 * Only required functions from sockstat.c are included.
380 */
381
382/*-
383 * Copyright (c) 2002 Dag-Erling Co<EF>dan Sm<F8>rgrav
384 * All rights reserved.
385 *
386 * Redistribution and use in source and binary forms, with or without
387 * modification, are permitted provided that the following conditions
388 * are met:
389 * 1. Redistributions of source code must retain the above copyright
390 * notice, this list of conditions and the following disclaimer
391 * in this position and unchanged.
392 * 2. Redistributions in binary form must reproduce the above copyright
393 * notice, this list of conditions and the following disclaimer in the
394 * documentation and/or other materials provided with the distribution.
395 * 3. The name of the author may not be used to endorse or promote products
396 * derived from this software without specific prior written permission.
397 *
398 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
399 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
400 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
401 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
402 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
403 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
404 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
405 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
406 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
407 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
408 */
409#include <sys/cdefs.h>
410__FBSDID("$FreeBSD: src/usr.bin/sockstat/sockstat.c,v 1.13.2.1.4.1 2008/10/02 02:57:24 kensmith Exp $");
411
412#include <sys/param.h>
413#include <sys/socket.h>
414#include <sys/socketvar.h>
415#include <sys/sysctl.h>
416#include <sys/file.h>
417#include <sys/user.h>
418
419#include <sys/un.h>
420#include <sys/unpcb.h>
421
422#include <net/route.h>
423
424#include <netinet/in.h>
425#include <netinet/in_pcb.h>
426#include <netinet/tcp.h>
427#include <netinet/tcp_seq.h>
428#include <netinet/tcp_var.h>
429#include <arpa/inet.h>
430
431#include <ctype.h>
432#include <errno.h>
433#include <netdb.h>
434#include <stdio.h>
435#include <stdlib.h>
436
437#include <unistd.h>
438
439static int opt_4 = 1; /* Show IPv4 sockets */
440static int opt_6 = 0; /* Show IPv6 sockets */
441static int opt_c = 0; /* Show connected sockets */
442static int opt_l = 1; /* Show listening sockets */
443static int opt_u = 0; /* Show Unix domain sockets */
444static int opt_v = 0; /* Verbose mode */
445
446struct sock {
447 void *socket;
448 void *pcb;
449 int vflag;
450 int family;
451 int proto;
452
453 struct sockaddr_storage laddr;
454 struct sockaddr_storage faddr;
455 struct sock *next;
456};
457
458#define HASHSIZE 1009
459static struct sock *sockhash[HASHSIZE];
460
461static struct xfile *xfiles;
462static int nxfiles;
463
464
465static void * xrealloc(void * buf, size_t len0, size_t len)
466{
467 if (len > 0)
468 {
469 void xbuf = SH_ALLOC(len);
470 memcpy(xbuf, buf, len0);
471 SH_FREE(buf);
472 return xbuf;
473 }
474 SH_FREE(buf);
475 return NULL;
476}
477
478/* Sets address and port in struct sockaddr_storage *sa
479 */
480static void
481sockaddr(struct sockaddr_storage *sa, int af, void *addr, int port)
482{
483 struct sockaddr_in *sin4;
484 struct sockaddr_in6 *sin6;
485
486 bzero(sa, sizeof *sa);
487 switch (af) {
488 case AF_INET:
489 sin4 = (struct sockaddr_in *)sa;
490 sin4->sin_len = sizeof *sin4;
491 sin4->sin_family = af;
492 sin4->sin_port = port;
493 sin4->sin_addr = *(struct in_addr *)addr;
494 break;
495 case AF_INET6:
496 sin6 = (struct sockaddr_in6 *)sa;
497 sin6->sin6_len = sizeof *sin6;
498 sin6->sin6_family = af;
499 sin6->sin6_port = port;
500 sin6->sin6_addr = *(struct in6_addr *)addr;
501 break;
502 default:
503 return;
504 }
505}
506
507/* Get socket information from the kernel.
508 */
509static void
510gather_inet(int proto)
511{
512 struct xinpgen *xig, *exig;
513 struct xinpcb *xip;
514 struct xtcpcb *xtp;
515 struct inpcb *inp;
516 struct xsocket *so;
517 struct sock *sock;
518 char varname[32];
519 size_t len, bufsize, bufsize0;
520 void *buf;
521 int hash, retry, vflag;
522
523 vflag = 0;
524 if (opt_4)
525 vflag |= INP_IPV4;
526 if (opt_6)
527 vflag |= INP_IPV6;
528
529 switch (proto) {
530 case IPPROTO_TCP:
531 sl_strlcpy(varname, _("net.inet.tcp.pcblist"), sizeof(varname));
532 break;
533 case IPPROTO_UDP:
534 sl_strlcpy(varname, _("net.inet.udp.pcblist"), sizeof(varname));
535 break;
536 case IPPROTO_DIVERT:
537 sl_strlcpy(varname, _("net.inet.divert.pcblist"), sizeof(varname));
538 break;
539 default:
540 return;
541 }
542
543 buf = NULL;
544 bufsize = 8192;
545 bufsize0 = bufsize;
546 retry = 5;
547 do {
548 for (;;) {
549 buf = xrealloc(buf, bufsize0, bufsize);
550 bufsize0 = bufsize;
551 len = bufsize;
552 if (sysctlbyname(varname, buf, &len, NULL, 0) == 0)
553 break;
554 if (errno == ENOENT)
555 goto out;
556 if (errno != ENOMEM)
557 {
558 SH_MUTEX_LOCK(mutex_thread_nolog);
559 sh_error_handle(SH_ERR_ERR, FIL__, __LINE__, 0, MSG_E_SUBGEN,
560 _("sysctlbyname()"),
561 _("gather_inet"));
562 SH_MUTEX_UNLOCK(mutex_thread_nolog);
563 return;
564 }
565 bufsize *= 2;
566 }
567 xig = (struct xinpgen *)buf;
568 exig = (struct xinpgen *)(void *)
569 ((char *)buf + len - sizeof *exig);
570 if (xig->xig_len != sizeof *xig ||
571 exig->xig_len != sizeof *exig)
572 {
573 SH_MUTEX_LOCK(mutex_thread_nolog);
574 sh_error_handle(SH_ERR_ERR, FIL__, __LINE__, 0, MSG_E_SUBGEN,
575 _("struct xinpgen size mismatch"),
576 _("gather_inet"));
577 SH_MUTEX_UNLOCK(mutex_thread_nolog);
578 goto out;
579 }
580
581 } while (xig->xig_gen != exig->xig_gen && retry--);
582
583 if (xig->xig_gen != exig->xig_gen && opt_v)
584 {
585 SH_MUTEX_LOCK(mutex_thread_nolog);
586 sh_error_handle(SH_ERR_WARN, FIL__, __LINE__, 0, MSG_E_SUBGEN,
587 _("data may be inconsistent"),
588 _("gather_inet"));
589 SH_MUTEX_UNLOCK(mutex_thread_nolog);
590 }
591
592 for (;;) {
593 xig = (struct xinpgen *)(void *)((char *)xig + xig->xig_len);
594 if (xig >= exig)
595 break;
596 switch (proto) {
597 case IPPROTO_TCP:
598 xtp = (struct xtcpcb *)xig;
599 if (xtp->xt_len != sizeof *xtp) {
600 SH_MUTEX_LOCK(mutex_thread_nolog);
601 sh_error_handle(SH_ERR_WARN, FIL__, __LINE__, 0, MSG_E_SUBGEN,
602 _("struct xtcpcb size mismatch"),
603 _("gather_inet"));
604 SH_MUTEX_UNLOCK(mutex_thread_nolog);
605 goto out;
606 }
607 inp = &xtp->xt_inp;
608 so = &xtp->xt_socket;
609 break;
610 case IPPROTO_UDP:
611 case IPPROTO_DIVERT:
612 xip = (struct xinpcb *)xig;
613 if (xip->xi_len != sizeof *xip) {
614 SH_MUTEX_LOCK(mutex_thread_nolog);
615 sh_error_handle(SH_ERR_WARN, FIL__, __LINE__, 0, MSG_E_SUBGEN,
616 _("struct xinpcb size mismatch"),
617 _("gather_inet"));
618 SH_MUTEX_UNLOCK(mutex_thread_nolog);
619 goto out;
620 }
621 inp = &xip->xi_inp;
622 so = &xip->xi_socket;
623 break;
624 default:
625 return;
626 }
627 if ((inp->inp_vflag & vflag) == 0)
628 continue;
629 if (inp->inp_vflag & INP_IPV4) {
630 if ((inp->inp_fport == 0 && !opt_l) ||
631 (inp->inp_fport != 0 && !opt_c))
632 continue;
633 } else if (inp->inp_vflag & INP_IPV6) {
634 if ((inp->in6p_fport == 0 && !opt_l) ||
635 (inp->in6p_fport != 0 && !opt_c))
636 continue;
637 } else {
638 if (opt_v) {
639 char errmsg[64];
640 sl_snprintf(errmsg, sizeof(errmsg),
641 _("invalid vflag 0x%x"), inp->inp_vflag);
642 SH_MUTEX_LOCK(mutex_thread_nolog);
643 sh_error_handle(SH_ERR_WARN, FIL__, __LINE__, 0, MSG_E_SUBGEN,
644 errmsg,
645 _("gather_inet"));
646 SH_MUTEX_UNLOCK(mutex_thread_nolog);
647 continue;
648 }
649 }
650
651 sock = SH_ALLOC(sizeof *sock);
652 memset(sock, '\0', sizeof (*sock));
653
654 sock->socket = so->xso_so;
655 sock->proto = proto;
656 if (inp->inp_vflag & INP_IPV4) {
657 sock->family = AF_INET;
658 sockaddr(&sock->laddr, sock->family,
659 &inp->inp_laddr, inp->inp_lport);
660 sockaddr(&sock->faddr, sock->family,
661 &inp->inp_faddr, inp->inp_fport);
662 } else if (inp->inp_vflag & INP_IPV6) {
663 sock->family = AF_INET6;
664 sockaddr(&sock->laddr, sock->family,
665 &inp->in6p_laddr, inp->in6p_lport);
666 sockaddr(&sock->faddr, sock->family,
667 &inp->in6p_faddr, inp->in6p_fport);
668 }
669 sock->vflag = inp->inp_vflag;
670
671 hash = (int)((uintptr_t)sock->socket % HASHSIZE);
672 sock->next = sockhash[hash];
673 sockhash[hash] = sock;
674 }
675out:
676 free(buf);
677}
678
679static void
680getfiles(void)
681{
682 size_t len;
683 size_t len0;
684
685 xfiles = SH_ALLOC(len = sizeof *xfiles);
686 len0 = len;
687
688 while (sysctlbyname(_("kern.file"), xfiles, &len, 0, 0) == -1) {
689 if (errno != ENOMEM)
690 {
691 SH_MUTEX_LOCK(mutex_thread_nolog);
692 sh_error_handle((-1), FIL__, __LINE__, status, MSG_E_SUBGEN,
693 _("sysctlbyname()"),
694 _("getfiles"));
695 SH_MUTEX_UNLOCK(mutex_thread_nolog);
696 }
697 len *= 2;
698 xfiles = xrealloc(xfiles, len0, len);
699 len0 = len;
700 }
701 if (len > 0 && xfiles->xf_size != sizeof *xfiles)
702 if (errno != ENOMEM)
703 {
704 SH_MUTEX_LOCK(mutex_thread_nolog);
705 sh_error_handle((-1), FIL__, __LINE__, status, MSG_E_SUBGEN,
706 _("struct xfile size mismatch"),
707 _("getfiles"));
708 SH_MUTEX_UNLOCK(mutex_thread_nolog);
709 }
710 nxfiles = len / sizeof *xfiles;
711}
712
713static const char *
714getprocname(pid_t pid)
715{
716 static struct kinfo_proc proc;
717 size_t len;
718 int mib[4];
719
720 mib[0] = CTL_KERN;
721 mib[1] = KERN_PROC;
722 mib[2] = KERN_PROC_PID;
723 mib[3] = (int)pid;
724 len = sizeof proc;
725 if (sysctl(mib, 4, &proc, &len, NULL, 0) == -1) {
726 /* Do not warn if the process exits before we get its name. */
727 if (errno != ESRCH)
728 {
729 SH_MUTEX_LOCK(mutex_thread_nolog);
730 sh_error_handle(SH_ERR_WARN, FIL__, __LINE__, status, MSG_E_SUBGEN,
731 _("sysctl()"),
732 _("getfiles"));
733 SH_MUTEX_UNLOCK(mutex_thread_nolog);
734 }
735 return ("-");
736 }
737 return (proc.ki_ocomm);
738}
739
740char * sh_port2proc_query(int proto, struct in_addr * saddr, int sport,
741 char * user, size_t userlen)
742{
743 struct xfile *xf;
744 struct in_addr * haddr;
745
746 for (xf = xfiles, n = 0; n < nxfiles; ++n, ++xf) {
747
748 if (xf->xf_data == NULL)
749 continue;
750
751 /* Find the socket in sockhash[] that corresponds to it
752 */
753 hash = (int)((uintptr_t)xf->xf_data % HASHSIZE);
754 for (s = sockhash[hash]; s != NULL; s = s->next)
755 if ((void *)s->socket == xf->xf_data)
756 break;
757
758 if (s->proto != proto)
759 continue;
760
761 if (s->family != AF_INET /* && s->family != AF_INET6 */)
762 continue;
763
764 if (sport != ntohs(((struct sockaddr_in *)s)->sin_port))
765 continue;
766
767 haddr = &((struct sockaddr_in *)s)->sin_addr;
768 if (haddr.s_addr == saddr->s_addr)
769 {
770 char * tmp = sh_unix_getUIDname (SH_ERR_ALL, xf->xf_uid, user, userlen);
771 if (!tmp)
772 sl_snprintf (user, userlen, "%ld", (unsigned long) xf->xf_uid);
773 return sh_util_strdup(getprocname(xf->xf_pid));
774 }
775 }
776}
777
778static void sockdel(struct sock * sock)
779{
780 if (sock)
781 {
782 if (sock->next)
783 sockdel(sock->next);
784 SH_FREE(sock);
785 }
786 return;
787}
788
789int sh_port2proc_prepare()
790{
791 int i;
792
793 if (xfiles)
794 {
795 SH_FREE(xfiles);
796 xfiles = NULL;
797 }
798
799 for (i = 0; i < HASHSIZE; ++i)
800 sockdel(sockhash[i]);
801
802 /* Inet connections
803 */
804 gather_inet(IPPROTO_TCP);
805 gather_inet(IPPROTO_UDP);
806 gather_inet(IPPROTO_DIVERT);
807
808 getfiles();
809
810 return 0;
811}
812
813#else /* !defined(__linux__) && !defined(__FreeBSD__) */
814
815char * sh_port2proc_query(int proto, struct in_addr * saddr, int sport,
816 char * user, size_t userlen)
817{
818 (void) proto;
819 (void) saddr;
820 (void) sport;
821
822 sl_strlcpy(user, "-", userlen);
823 return sh_util_strdup("-");
824}
825
826int sh_port2proc_prepare()
827{
828 return 0;
829}
830
831#endif
832
833#endif /* defined(SH_USE_PORTCHECK) */
Note: See TracBrowser for help on using the repository browser.