]> git.cworth.org Git - ttt/blobdiff - src/ttt-server.c
2005-12-09 Carl Worth <cworth@cworth.org>
[ttt] / src / ttt-server.c
index 34159b547ddb0b0946ef97d5a655646259223c07..cb28741885721763fd54abd4a8a83b270e07f442 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++;
 
@@ -110,7 +110,7 @@ ttt_server_unregister_client (ttt_server_t *server, ttt_client_t *client)
 
     assert (i < server->num_clients);
 
-    printf ("Client %s has left.\r\n", ttt_client_get_username (client));
+    fprintf (stderr, "Client %s has left.\r\n", ttt_client_get_username (client));
 
     memmove (&server->clients[i], &server->clients[i+1],
             (server->num_clients - i - 1) * sizeof (ttt_client_t *));
@@ -305,12 +305,35 @@ 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)
+    if (args.detach) {
        printf ("Server started listening on %s:%s\n", args.host, args.port);
-    else
+       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);
+    }
 
     fclose (stdout);
     fclose (stdin);