1
0

hook_log.c 974 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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 "clish/shell.h"
  13. #define SYSLOG_IDENT "klish"
  14. /*--------------------------------------------------------- */
  15. CLISH_HOOK_LOG(clish_hook_log)
  16. {
  17. clish_shell_t *this = clish_context__get_shell(clish_context);
  18. struct passwd *user = NULL;
  19. char *uname = NULL;
  20. /* Initialization */
  21. if (!line) {
  22. openlog(SYSLOG_IDENT, LOG_PID,
  23. clish_shell__get_log_facility(this));
  24. return 0;
  25. }
  26. /* Log the given line */
  27. /* Try to get username from environment variables
  28. * USER and LOGNAME and then from /etc/passwd.
  29. */
  30. user = clish_shell__get_user(this);
  31. if (!(uname = getenv("USER"))) {
  32. if (!(uname = getenv("LOGNAME")))
  33. uname = user ? user->pw_name : "unknown";
  34. }
  35. syslog(LOG_INFO, "%u(%s) %s : %d",
  36. user ? user->pw_uid : getuid(), uname, line, retcode);
  37. return 0;
  38. }