From 6f50959ebd4fa56977201a90f70f0352db66b5d5 Mon Sep 17 00:00:00 2001 From: Ricardo Sateler Date: Fri, 12 Aug 2016 15:49:57 -0400 Subject: [PATCH] Added flag to run in foreground, no daemon (--no-daemon switch) --- man/logkeys.8 | 7 +++++++ src/args.cc | 2 ++ src/logkeys.cc | 18 +++++++++++------- src/usage.cc | 1 + 4 files changed, 21 insertions(+), 7 deletions(-) diff --git a/man/logkeys.8 b/man/logkeys.8 index c398ee1..e2710e5 100644 --- a/man/logkeys.8 +++ b/man/logkeys.8 @@ -7,6 +7,8 @@ logkeys \- a GNU/Linux keylogger that works! [\fB--no-func-keys\fR] [\fB--no-timestamps\fR] .br [\fB--post-http=\fIURL\fR] [\fB--post-size=\fISIZE\fR] +.br + [\fB--no-daemon\fR] .br .B logkeys \fB-k\fR .br @@ -133,6 +135,11 @@ by a (PHP) script. The file is sent with header \fIContent-Type: multipart/form-data\fR as file, so it is accessible in PHP via $_FILES['file'] variable. +.TP +\fB-\-no-daemon\fR +When this option is set, logkeys runs in the foreground. +Useful when printing output to stdout. + .SH FILES .TP \fB/var/log/logkeys.log\fR diff --git a/src/args.cc b/src/args.cc index fc1a316..2ec14fd 100644 --- a/src/args.cc +++ b/src/args.cc @@ -33,6 +33,7 @@ struct arguments #define FLAG_POST_HTTP 0x8 // post log to remote HTTP server, --post-http switch #define FLAG_POST_IRC 0x10 // post log to remote IRC server, --post-irc switch #define FLAG_POST_SIZE 0x20 // post log to remote HTTP or IRC server when log of size optarg, --post-size +#define FLAG_NO_DAEMON 0x40 // don't daemonize process, stay in foreground, --no-daemon switch } args = {0}; // default all args to 0x0 or "" @@ -54,6 +55,7 @@ void process_command_line_arguments(int argc, char **argv) {"post-http", required_argument, &flags, FLAG_POST_HTTP}, {"post-irc", required_argument, &flags, FLAG_POST_IRC}, {"post-size", required_argument, &flags, FLAG_POST_SIZE}, + {"no-daemon", no_argument, &flags, FLAG_NO_DAEMON}, {0} }; diff --git a/src/logkeys.cc b/src/logkeys.cc index c94b4e4..4cb490b 100644 --- a/src/logkeys.cc +++ b/src/logkeys.cc @@ -422,12 +422,14 @@ int main(int argc, char **argv) set_signal_handling(); - int nochdir = 0; - if (args.logfile[0] != '/') - nochdir = 1; // don't chdir (logfile specified with relative path) - int noclose = 1; // don't close streams (stderr used) - if (daemon(nochdir, noclose) == -1) // become daemon - error(EXIT_FAILURE, errno, "Failed to become daemon"); + if (!(args.flags & FLAG_NO_DAEMON)) { + int nochdir = 0; + if (args.logfile[0] != '/') + nochdir = 1; // don't chdir (logfile specified with relative path) + int noclose = 1; // don't close streams (stderr used) + if (daemon(nochdir, noclose) == -1) // become daemon + error(EXIT_FAILURE, errno, "Failed to become daemon"); + } close(STDIN_FILENO); // leave stderr open if (args.logfile != "-") { @@ -460,7 +462,9 @@ int main(int argc, char **argv) // now we need those privileges back in order to create system-wide PID_FILE seteuid(0); setegid(0); - create_PID_file(); + if (!(args.flags & FLAG_NO_DAEMON)) { + create_PID_file(); + } // now we've got everything we need, finally drop privileges by becoming 'nobody' //setegid(65534); seteuid(65534); // commented-out, I forgot why xD diff --git a/src/usage.cc b/src/usage.cc index dde8bd5..89a10ea 100644 --- a/src/usage.cc +++ b/src/usage.cc @@ -30,6 +30,7 @@ void usage() " --post-http=URL POST log to URL as multipart/form-data file\n" //" --post-irc=FORMAT FORMAT is nick_or_channel@server:port\n" " --post-size=SIZE post log file when size equals SIZE [500k]\n" +" --no-daemon run in foreground\n" "\n" "Examples: logkeys -s -m mylang.map -o ~/.secret-keys.log\n" " logkeys -s -d event6\n"