From 4aac31c788be61de0c1ee5d2d5bf0df723336838 Mon Sep 17 00:00:00 2001 From: Aaron Ball Date: Sun, 11 Jul 2021 15:34:29 -0600 Subject: Improve main function efficiency There already existed a string buffer for the original ssh command string, but this still made repeated calls to get that environment variable. Now we only use the `cmd` buffer. This also prints "No soup for you!" to stderr, rather than stdout. --- src/main.c | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/src/main.c b/src/main.c index 9fb7f31..4ba1d4d 100644 --- a/src/main.c +++ b/src/main.c @@ -228,14 +228,14 @@ int main(int argc, char* argv[]) { return 1; } user = argv[1]; - // Read the USERNAME variable + // Set the USERNAME environment variable setenv("USERNAME", user, 1); // Ensure a command was specified if(! getenv("SSH_ORIGINAL_COMMAND")) { sprintf(msg, "[%s] logged in without specifying a command", user); logmsg(msg); - printf("No soup for you!\n"); + fprintf(stderr, "No soup for you!\n"); return 1; } @@ -245,22 +245,20 @@ int main(int argc, char* argv[]) { if(is_git_cmd(cmd)) { // Read the repo path (command argument) if(!validate_git(user)) { - sprintf(msg, "[%s] attempted invalid git command \"%s\"", - user, getenv("SSH_ORIGINAL_COMMAND")); + sprintf(msg, "[%s] attempted invalid git command \"%s\"", user, cmd); logmsg(msg); return 1; } } else if(! is_allowed_cmd(cmd)) { - sprintf(msg, "[%s] attempted disallowed command \"%s\"", - user, getenv("SSH_ORIGINAL_COMMAND")); + sprintf(msg, "[%s] attempted disallowed command \"%s\"", user, cmd); logmsg(msg); fprintf(stderr, "Command '%s' is not allowed\n", cmd); return 1; } - sprintf(msg, "[%s] executed \"%s\"", user, getenv("SSH_ORIGINAL_COMMAND")); + sprintf(msg, "[%s] executed \"%s\"", user, cmd); logmsg(msg); - sprintf(gitsh, "/usr/bin/env git-shell -c \"%s\"", getenv("SSH_ORIGINAL_COMMAND")); + sprintf(gitsh, "/usr/bin/env git-shell -c \"%s\"", cmd); system(gitsh); return 0; -- cgit v1.2.3