callback_log.c 992 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. /*
  2. * callback_log.c
  3. *
  4. * Callback hook to log users's commands
  5. */
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <syslog.h>
  9. #include <unistd.h>
  10. #include <sys/types.h>
  11. #include <pwd.h>
  12. #include "internal.h"
  13. #define SYSLOG_IDENT "klish"
  14. /*--------------------------------------------------------- */
  15. int clish_log_callback(clish_context_t *context, const char *line,
  16. int retcode)
  17. {
  18. clish_shell_t *this = context->shell;
  19. struct passwd *user = NULL;
  20. char *uname = NULL;
  21. /* Initialization */
  22. if (!line) {
  23. openlog(SYSLOG_IDENT, LOG_PID,
  24. clish_shell__get_facility(this));
  25. return 0;
  26. }
  27. /* Log the given line */
  28. /* Try to get username from environment variables
  29. * USER and LOGNAME and then from /etc/passwd.
  30. */
  31. user = clish_shell__get_user(this);
  32. if (!(uname = getenv("USER"))) {
  33. if (!(uname = getenv("LOGNAME")))
  34. uname = user ? user->pw_name : "unknown";
  35. }
  36. syslog(LOG_INFO, "%u(%s) %s : %d",
  37. user ? user->pw_uid : getuid(), uname, line, retcode);
  38. return 0;
  39. }