]> 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..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++;
 
@@ -305,6 +305,27 @@ 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)