From 43146158aaeffb4eab629dcd9982b146636cd660 Mon Sep 17 00:00:00 2001 From: gfleury Date: Sun, 21 Dec 2014 21:03:47 -0200 Subject: [PATCH 1/5] Fix environment vars after setuid --- main.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/main.c b/main.c index 9a4a098..ce9fd6b 100644 --- a/main.c +++ b/main.c @@ -566,7 +566,11 @@ int change_user_group () { if (setuid (pw->pw_uid) < 0) { fprintf (stderr, "change_user_group: failed to assume identity of user %s\n", username); return -1; - } + } else { + pw = getpwuid(getuid()); + setenv("USER", pw->pw_name, 1); + setenv("HOME", pw->pw_dir, 1); + setenv("SHELL", pw->pw_shell, 1); } return 0; } From 2409907c6b22c725c12b689d27e04ea49736a947 Mon Sep 17 00:00:00 2001 From: gfleury Date: Sun, 21 Dec 2014 21:12:00 -0200 Subject: [PATCH 2/5] Fix typo --- main.c | 1 + 1 file changed, 1 insertion(+) diff --git a/main.c b/main.c index ce9fd6b..f5ba848 100644 --- a/main.c +++ b/main.c @@ -571,6 +571,7 @@ int change_user_group () { setenv("USER", pw->pw_name, 1); setenv("HOME", pw->pw_dir, 1); setenv("SHELL", pw->pw_shell, 1); + } } return 0; } From fde3182b40a4335c17db584171560cc6c1baa35f Mon Sep 17 00:00:00 2001 From: George Fleury Date: Mon, 22 Dec 2014 00:50:28 +0000 Subject: [PATCH 3/5] Avoid loop break when daemonize option is on, now daemonize runs forever until sigkill or sigterm --- interface.c | 10 ++++++++++ loop.c | 5 +++-- main.c | 7 +++++-- 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/interface.c b/interface.c index ac0b8bc..b1a908d 100644 --- a/interface.c +++ b/interface.c @@ -104,8 +104,12 @@ extern int usfd; extern int sfd; extern int use_ids; +extern int daemonize; + extern struct tgl_state *TLS; +void event_incoming (struct bufferevent *bev, short what, void *_arg); + int is_same_word (const char *s, size_t l, const char *word) { return s && word && strlen (word) == l && !memcmp (s, word, l); } @@ -909,10 +913,16 @@ void do_status_offline (int arg_num, struct arg args[], struct in_ev *ev) { } void do_quit (int arg_num, struct arg args[], struct in_ev *ev) { + if (daemonize) + event_incoming (ev->bev, BEV_EVENT_EOF, ev); + //bufferevent_free(ev->bev); do_halt (0); } void do_safe_quit (int arg_num, struct arg args[], struct in_ev *ev) { + if (daemonize) + event_incoming (ev->bev, BEV_EVENT_EOF, ev); + //bufferevent_free(ev->bev); safe_quit = 1; } diff --git a/loop.c b/loop.c index c2b6348..116b314 100644 --- a/loop.c +++ b/loop.c @@ -192,6 +192,7 @@ void net_loop (int flags, int (*is_end)(void)) { if (safe_quit && !TLS->active_queries) { printf ("All done. Exit\n"); do_halt (0); + safe_quit = 0; } if (sigterm_cnt > 0) { do_halt (0); @@ -682,7 +683,7 @@ static void read_incoming (struct bufferevent *bev, void *_arg) { } } -static void event_incoming (struct bufferevent *bev, short what, void *_arg) { +void event_incoming (struct bufferevent *bev, short what, void *_arg) { struct in_ev *ev = _arg; if (what & (BEV_EVENT_EOF | BEV_EVENT_ERROR)) { vlogprintf (E_WARNING, "Closing incoming connection\n"); @@ -701,7 +702,7 @@ static void accept_incoming (evutil_socket_t efd, short what, void *arg) { int fd = accept (efd, (struct sockaddr *)&cli_addr, &clilen); assert (fd >= 0); - struct bufferevent *bev = bufferevent_socket_new (TLS->ev_base, fd, 0); + struct bufferevent *bev = bufferevent_socket_new (TLS->ev_base, fd, BEV_OPT_CLOSE_ON_FREE); struct in_ev *e = malloc (sizeof (*e)); e->bev = bev; e->refcnt = 1; diff --git a/main.c b/main.c index f5ba848..ce08a29 100644 --- a/main.c +++ b/main.c @@ -491,7 +491,7 @@ int disable_auto_accept; int wait_dialog_list; char *logname; -int daemonize; +int daemonize=0; void reopen_logs (void) { @@ -741,11 +741,14 @@ void sig_term_handler (int signum __attribute__ ((unused))) { } void do_halt (int error) { + if (daemonize) + return; + if (!readline_disabled) { rl_free_line_state (); rl_cleanup_after_signal (); } - + if (write (1, "halt\n", 5) < 0) { // Sad thing } From 5f81eb4a3680408ab6774cd509b581b940fde8ac Mon Sep 17 00:00:00 2001 From: George Fleury Date: Mon, 22 Dec 2014 01:44:32 +0000 Subject: [PATCH 4/5] Fix wrong parameters on start daemon script --- start-telegram-daemon | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/start-telegram-daemon b/start-telegram-daemon index 85c8584..7484781 100755 --- a/start-telegram-daemon +++ b/start-telegram-daemon @@ -112,7 +112,7 @@ if(open $etchandle, $etcfile) $have_l = 1 if $line =~ /^-L\s/; $have_c = 1 if $line =~ /^-c\s/; - $have_u = 1 if $line =~ /^-u\s/; + $have_u = 1 if $line =~ /^-U\s/; push @$params, $line; } @@ -120,7 +120,7 @@ if(open $etchandle, $etcfile) $params = []; } -push @$params, "-u telegramd" unless($have_u); +push @$params, "-U telegramd" unless($have_u); push @$params, "-L $fd_reopened" unless($have_l); push @$params, "-c config$idl" unless($have_c); $params = join " ", @$params; From e3dce286e5422de70fe298727f0bf0e46fe1f4d4 Mon Sep 17 00:00:00 2001 From: George Fleury Date: Mon, 22 Dec 2014 03:17:43 +0000 Subject: [PATCH 5/5] start-telegram-daemon permit -d flag --- start-telegram-daemon | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/start-telegram-daemon b/start-telegram-daemon index 7484781..a498166 100755 --- a/start-telegram-daemon +++ b/start-telegram-daemon @@ -96,7 +96,7 @@ if(open $etchandle, $etcfile) $line =~ s/\s+$//g; $line =~ s/^\s+//g; next unless $line; - next if $line =~ /^\-[dh]/; + next if $line =~ /^\-[h]/; if($line =~ /^[^\-]/) {