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