]> git.cworth.org Git - ttt/commitdiff
2005-12-09 Carl Worth <cworth@cworth.org>
authorCarl Worth <carl@theworths.org>
Fri, 9 Dec 2005 17:10:08 +0000 (17:10 +0000)
committerCarl Worth <carl@theworths.org>
Fri, 9 Dec 2005 17:10:08 +0000 (17:10 +0000)
        * src/ttt-args.h:
        * src/ttt-args.c: (ttt_args_help), (ttt_args_parse): Add a -u,
        --user option for controlling the uid under which the server
        process runs.

        * src/ttt.h:
        * src/ttt-server.c: (ttt_server_register_client), (main): Recert
        the dup of stdout which wasn't working. Instead just print client
        join information on stderr so that it goes into the log. Implement
        support for the user option by calling setuid to drop root
        privileges the process might start out with.

ChangeLog
src/ttt-args.c
src/ttt-args.h
src/ttt-server.c
src/ttt.h

index 30093c47409f2b8c5e3e75cf76148e729708d886..372a3206c5d6c546ed5b46c4bd88a0eadb6c1aea 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,17 @@
+2005-12-09  Carl Worth  <cworth@cworth.org>
+
+       * src/ttt-args.h:
+       * src/ttt-args.c: (ttt_args_help), (ttt_args_parse): Add a -u,
+       --user option for controlling the uid under which the server
+       process runs.
+       
+       * src/ttt.h:
+       * src/ttt-server.c: (ttt_server_register_client), (main): Recert
+       the dup of stdout which wasn't working. Instead just print client
+       join information on stderr so that it goes into the log. Implement
+       support for the user option by calling setuid to drop root
+       privileges the process might start out with.
+
 2005-12-09  Bryan Worth  <bryan@theworths.org>
        * src/ttt-curses-client.c: added error checking to ttt_create_client
        call. Added ability to escape server commands by prepending with "/".
 2005-12-09  Bryan Worth  <bryan@theworths.org>
        * src/ttt-curses-client.c: added error checking to ttt_create_client
        call. Added ability to escape server commands by prepending with "/".
index f367716c6b70e8e28326d1223c3d1e914424a54e..0f3e8fed47839a1ef7dbc042c81dfff2eb684e9c 100644 (file)
@@ -53,11 +53,12 @@ enum {
     TTT_ARGS_VAL_VERSION,
 };
 
     TTT_ARGS_VAL_VERSION,
 };
 
-static char ttt_args_optstring[] = "dh:p:";
+static char ttt_args_optstring[] = "dh:p:u:";
 static struct option ttt_args_options[] = {
     /* name,           has_arg,        flag,   val */
     {"host",           1,              0,      'h'},
     {"port",           1,              0,      'p'},
 static struct option ttt_args_options[] = {
     /* name,           has_arg,        flag,   val */
     {"host",           1,              0,      'h'},
     {"port",           1,              0,      'p'},
+    {"user",           1,              0,      'u'},
     {"detach",         0,              0,      'd'},
     {"log-file",       1,              0,      TTT_ARGS_VAL_LOG_FILE},
     {"pid-file",       1,              0,      TTT_ARGS_VAL_PID_FILE},
     {"detach",         0,              0,      'd'},
     {"log-file",       1,              0,      TTT_ARGS_VAL_LOG_FILE},
     {"pid-file",       1,              0,      TTT_ARGS_VAL_PID_FILE},
@@ -78,6 +79,7 @@ ttt_args_help (const char *argv0)
            TTT_ARGS_HOST_DEFAULT);
     printf ("  -p PORT, --port=PORT\tPort to connect/bind to [%s]\n",
            TTT_ARGS_PORT_DEFAULT);
            TTT_ARGS_HOST_DEFAULT);
     printf ("  -p PORT, --port=PORT\tPort to connect/bind to [%s]\n",
            TTT_ARGS_PORT_DEFAULT);
+    printf ("  -u USER, --user=USER\tUser the server should run as.\n");
     printf ("           --help\tGive this help list\n");
     printf ("           --version\tPrint program version\n");
     puts ("");
     printf ("           --help\tGive this help list\n");
     printf ("           --version\tPrint program version\n");
     puts ("");
@@ -109,6 +111,7 @@ ttt_args_parse(ttt_args_t *args, int argc, char *argv[])
 
     args->host = TTT_ARGS_HOST_DEFAULT;
     args->port = TTT_ARGS_PORT_DEFAULT;
 
     args->host = TTT_ARGS_HOST_DEFAULT;
     args->port = TTT_ARGS_PORT_DEFAULT;
+    args->user = NULL;
     args->log_file = TTT_ARGS_LOG_FILE_DEFAULT;
     args->detach = FALSE;
     args->pid_file = TTT_ARGS_PID_FILE_DEFAULT;
     args->log_file = TTT_ARGS_LOG_FILE_DEFAULT;
     args->detach = FALSE;
     args->pid_file = TTT_ARGS_PID_FILE_DEFAULT;
@@ -125,6 +128,9 @@ ttt_args_parse(ttt_args_t *args, int argc, char *argv[])
        case 'p':
            args->port = optarg;
            break;
        case 'p':
            args->port = optarg;
            break;
+       case 'u':
+           args->user = optarg;
+           break;
        case TTT_ARGS_VAL_LOG_FILE:
            args->log_file = optarg;
            break;
        case TTT_ARGS_VAL_LOG_FILE:
            args->log_file = optarg;
            break;
index 5a1b3cfe67f822657e51b054b71749989a58cd8e..f35bee8eb7185e3906a53619d312c4ef0e52f0c6 100644 (file)
@@ -34,6 +34,7 @@ typedef struct ttt_args
 {
     char *host;
     char *port;
 {
     char *host;
     char *port;
+    char *user;
     char *log_file;
     ttt_bool_t detach;
     char *pid_file;
     char *log_file;
     ttt_bool_t detach;
     char *pid_file;
index 5d951fd40d5843430d89937687285c431ff90223..4f647ee04f69c8b6e0044c325109cedd4b3b4ceb 100644 (file)
@@ -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++;
 
 
     server->num_clients++;
 
@@ -305,14 +305,33 @@ main (int argc, char **argv)
     if (args.detach)
        _detach_and_write_child_pid_to (args.pid_file);
 
     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);
 
     socket = ttt_socket_create_server (args.host, args.port);
 
-    if (args.detach) {
-       xdup2 (2, 1);
+    if (args.detach)
        printf ("Server started listening on %s:%s\n", args.host, args.port);
        printf ("Server started listening on %s:%s\n", args.host, args.port);
-    } else {
+    else
        printf (WELCOME_MESSAGE, args.host, args.port, args.host, args.port);
        printf (WELCOME_MESSAGE, args.host, args.port, args.host, args.port);
-    }
 
     fclose (stdout);
     fclose (stdin);
 
     fclose (stdout);
     fclose (stdin);
index c1662afd8aaf6875ff38e961c6fa822a2d5dc1b4..5c141695b87c6bcc9598b2c12a171ef8ece0cb00 100644 (file)
--- a/src/ttt.h
+++ b/src/ttt.h
@@ -41,6 +41,7 @@
 #include <sys/types.h>
 #include <sys/socket.h>
 #include <pthread.h>
 #include <sys/types.h>
 #include <sys/socket.h>
 #include <pthread.h>
+#include <pwd.h>
 
 #define ASSERT_NOT_REACHED             \
 do {                                   \
 
 #define ASSERT_NOT_REACHED             \
 do {                                   \