X-Git-Url: https://git.cworth.org/git?p=ttt;a=blobdiff_plain;f=src%2Fttt-server.c;h=7a3ed341236658a9f34a7cd0651cff0cf03936ce;hp=5d951fd40d5843430d89937687285c431ff90223;hb=3fb29eb6a3418db7048b27cece3b3fa96fbd69ce;hpb=5e8532a5e248965f2ff3cfc01e9dcc307fe5825a diff --git a/src/ttt-server.c b/src/ttt-server.c index 5d951fd..7a3ed34 100644 --- a/src/ttt-server.c +++ b/src/ttt-server.c @@ -74,7 +74,7 @@ ttt_server_register_client (ttt_server_t *server, ttt_client_t *client) } } - printf ("Client %s has joined.\r\n", username); + fprintf (stderr, "Client %s has joined.\r\n", username); server->num_clients++; @@ -305,11 +305,32 @@ main (int argc, char **argv) if (args.detach) _detach_and_write_child_pid_to (args.pid_file); + /* Now that we've setup logging and the pid file, drop any special + * permissions we might have if we were asked to do that. */ + if (args.user) { + int ret; + struct passwd *pwd; + errno = 0; + pwd = getpwnam (args.user); + if (pwd == NULL) { + fprintf (stderr, "Error: Failed to lookup uid for %s: %s. Aborting.\n", + args.user, + errno == 0 ? "User not found" : strerror (errno)); + exit (1); + } + ret = setuid (pwd->pw_uid); + if (ret == -1) { + fprintf (stderr, "Error: Failed to setuid to %d (%s): %s. Aborting.\n", + pwd->pw_uid, args.user, strerror (errno)); + exit (1); + } + } + socket = ttt_socket_create_server (args.host, args.port); if (args.detach) { - xdup2 (2, 1); printf ("Server started listening on %s:%s\n", args.host, args.port); + fprintf (stderr, "Server started listening on %s:%s\n", args.host, args.port); } else { printf (WELCOME_MESSAGE, args.host, args.port, args.host, args.port); }