--- tethereal.c Thu Sep 8 10:14:25 2005 +++ tethereal.c Thu Sep 8 10:16:25 2005 @@ -34,6 +34,7 @@ #include #include #include +#include #ifdef HAVE_UNISTD_H #include @@ -105,6 +106,11 @@ #endif /* HAVE_LIBPCAP */ #include "epan/emem.h" +uid_t nuid = 0; +char *nusr = NULL; +struct passwd *pwent; + + /* * This is the template for the decode as option; it is shared between the * various functions that output the usage for this parameter. @@ -776,7 +782,7 @@ print_format = PR_FMT_TEXT; /* Now get our args */ - while ((opt = getopt(argc, argv, "a:b:c:d:Df:F:hi:lLnN:o:pqr:R:s:St:T:vw:Vxy:z:")) != -1) { + while ((opt = getopt(argc, argv, "a:b:c:d:Df:F:hi:lLnN:o:pqr:R:s:St:T:u:vw:Vxy:z:")) != -1) { switch (opt) { case 'a': /* autostop criteria */ case 'b': /* Ringbuffer option */ @@ -1002,6 +1008,29 @@ exit(1); } break; + case 'u': /* Change the UID that tethereal runs as */ + if ((nuid = atoi (optarg)) == 0) { + nusr = strdup (optarg); + pwent = getpwnam (nusr); + if (pwent == (struct passwd *) NULL) { + fprintf (stderr, "Error user unknown\n"); + exit (8); + } + + nuid = pwent->pw_uid; + + } else { + + pwent = getpwuid (nuid); + if (pwent == (struct passwd *) NULL) { + fprintf (stderr, + "Error cannot resolve user name for uid.\n"); + exit (8); + } + nusr = strdup (pwent->pw_name); + } + break; + case 'v': /* Show version and exit */ printf("t" PACKAGE " " VERSION #ifdef SVNVERSION @@ -1722,6 +1751,11 @@ } else ld.go = FALSE; while (ld.go) { + + /* XXX only good for capture. Is there a better place for this? */ + if ((nusr != NULL) && (setuid (nuid) < 0)) + fprintf (stderr, "Failed to change to user id %d\n", nuid); + /* We need to be careful with automatic variables defined in the outer scope which are changed inside the loop. Most compilers don't try to roll them back to their original values after the