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