=== modified file 'Makefile' --- Makefile 2012-11-03 05:44:25 +0000 +++ Makefile 2012-11-03 16:23:27 +0000 @@ -12,4 +12,4 @@ install -o man -g root -m 444 timeouts.5 /usr/share/man/man5 clean: - rm timeoutd timeoutd.o TAGS + rm timeoutd timeoutd.o TAGS *~ === modified file 'debug.c' --- debug.c 2012-11-03 05:44:25 +0000 +++ debug.c 2012-11-03 16:17:57 +0000 @@ -23,11 +23,11 @@ closelog (); } -void -dumps (char *s, char *varname, char *filename, int line) +void dumps(const char *var, const char *varname, const char *filename, + int line) { openlog ("timeoutd", OPENLOG_FLAGS, LOG_DAEMON); - syslog (LOG_DEBUG, "%s:%d: %s %s", filename, line, varname, s); + syslog (LOG_DEBUG, "%s:%d: %s %s", filename, line, varname, var); closelog (); } === modified file 'debug.h' --- debug.h 2012-11-03 07:29:02 +0000 +++ debug.h 2012-11-03 16:17:41 +0000 @@ -5,7 +5,8 @@ #define DUMP(num) dump(num, #num, __FILE__, __LINE__) void dump(long long n, char *varname, char *filename, int line); #define DUMPS(str) dumps(str, #str, __FILE__, __LINE__) -void dumps(char *str, char *strname, char *filename, int line); +void dumps(const char *var, const char *varname, const char *filename, + int line); #else /* not DEBUG */ === modified file 'timeoutd.c' --- timeoutd.c 2012-11-03 05:44:25 +0000 +++ timeoutd.c 2012-11-03 16:14:09 +0000 @@ -42,12 +42,13 @@ #include #include #include +#include +#include "debug.h" #ifdef TIMEOUTDX11 #include #include #include -#include "debug.h" #define TIMEOUTD_XSESSION_NONE 0 #define TIMEOUTD_XSESSION_LOCAL 1 @@ -305,7 +306,7 @@ closelog (); exit (1); } -#endif /* !DEBUG */ +#endif /* !DEBUG */ /* Handle the "timeoutd user tty" invocation */ /* This is a bit of a shameless hack, but, well, it works. */ @@ -376,7 +377,7 @@ } } -#ifndef DEBUG /* Debugger stays in foreground */ +#ifndef DEBUG /* Debugger stays in foreground */ /* If running in daemon mode (no parameters) */ if (fork ()) /* the parent process */ exit (0); /* exits */ @@ -385,7 +386,7 @@ close (1); close (2); setsid (); -#endif /* DEBUG */ +#endif /* DEBUG */ openlog ("timeoutd", OPENLOG_FLAGS, LOG_DAEMON); syslog (LOG_NOTICE, "Daemon started."); @@ -906,63 +907,89 @@ return; } -void -warnpending (tty, time_remaining, user, host) - char *tty; - int time_remaining; - char *user; - char *host; +void xwarn (char *tty, int time_remaining, char *user, char *host) { - int fd; - FILE *ttyf; char cmdbuf[1024]; + TRACE(xwarn); + DUMPS(tty); + DUMP(time_remaining); + DUMPS(user); + DUMPS(host); -#ifdef DEBUG openlog ("timeoutd", OPENLOG_FLAGS, LOG_DAEMON); syslog (SYSLOG_DEBUG, - "Warning %s@%s on %s of pending logoff in %d minutes.", user, host, - tty, time_remaining); - closelog (); -#endif - + "Warning %s running X on %s for pending logout! (%d min%s left)", + user, tty, time_remaining, time_remaining == 1 ? "" : "s"); + closelog (); + sprintf (cmdbuf, + "su %s -c \"xmessage -display %s -center 'WARNING: You will be logged out in %d minute%s when your %s limit expires.'&\"", + user, host, time_remaining, time_remaining == 1 ? "" : "s", + limit_names[limit_type]); + system (cmdbuf); + openlog ("timeoutd", OPENLOG_FLAGS, LOG_DAEMON); + syslog (LOG_DEBUG, "cmdbuf=%s", cmdbuf); + closelog (); + sleep (KWAIT); /* and give the user some time to read the message ;) */ + return; +} + +char *build_devname (const char *tty) +{ + TRACE(build_devname); + DUMPS(tty); + + static char ret[0x100]; + if (strstr(dev, "/dev/") == NULL) + sprintf(ret, "/dev/%s", dev); + else + sprintf(ret, "%s", dev); + return ret; +} + +void ttywarn (char *tty, int time_remaining, char *user, char *host) +{ + int fd = -1; + FILE *ttyf = NULL; + char *devname = NULL; + TRACE(ttywarn); + DUMPS(tty); + DUMP(time_remaining); + DUMPS(user); + DUMPS(host); + + devname = build_devname(tty); + if ((fd = open (devname, O_WRONLY | O_NOCTTY | O_NONBLOCK)) < 0 || + (ttyf = fdopen (fd, "w")) == NULL) + { + int err = errno; + openlog ("timeoutd", OPENLOG_FLAGS, LOG_DAEMON); + syslog (LOG_ERR, "ttywarn(%s, %s): %s", user, + devname, sys_errlist[err]); + closelog (); + } + else + { + fprintf (ttyf, + "\r\nWARNING:\r\nYou will be logged out in %d minute%s when your %s limit expires.\r\n", + time_remaining, time_remaining == 1 ? "" : "s", + limit_names[limit_type]); + } + if (ttyf != NULL) fclose (ttyf); + if (fd != -1) close (fd); +} + +void warnpending (char *tty, int time_remaining, char *user, char *host) +{ + TRACE(warnpending); + DUMPS(tty); + DUMP(time_remaining); + DUMPS(user); + DUMPS(host); + if (chk_xsession (tty, host)) - { - openlog ("timeoutd", OPENLOG_FLAGS, LOG_DAEMON); - syslog (SYSLOG_DEBUG, - "Warning %s running X on %s for pending logout! (%d min%s left)", - user, tty, time_remaining, time_remaining == 1 ? "" : "s"); - closelog (); - - /* then send the message using xmessage */ - /* well, this is not really clean: */ - sprintf (cmdbuf, - "su %s -c \"xmessage -display %s -center 'WARNING: You will be logged out in %d minute%s when your %s limit expires.'&\"", - user, host, time_remaining, time_remaining == 1 ? "" : "s", - limit_names[limit_type]); - system (cmdbuf); - /*#ifdef DEBUG */ - openlog ("timeoutd", OPENLOG_FLAGS, LOG_DAEMON); - syslog (LOG_DEBUG, "cmdbuf=%s", cmdbuf); - closelog (); - /*#endif */ - sleep (KWAIT); /* and give the user some time to read the message ;) */ - return; - } - - if ((fd = open (tty, O_WRONLY | O_NOCTTY | O_NONBLOCK)) < 0 || - (ttyf = fdopen (fd, "w")) == NULL) - { - openlog ("timeoutd", OPENLOG_FLAGS, LOG_DAEMON); - syslog (LOG_ERR, "Could not open %s to warn of impending logoff.\n", - tty); - closelog (); - return; - } - fprintf (ttyf, - "\r\nWARNING:\r\nYou will be logged out in %d minute%s when your %s limit expires.\r\n", - time_remaining, time_remaining == 1 ? "" : "s", - limit_names[limit_type]); - fclose (ttyf); + xwarn (tty, time_remaining, user, host); + else + ttywarn (tty, time_remaining, user, host); } char @@ -1298,7 +1325,6 @@ } } - /* terminate process using SIGHUP, then SIGKILL */ void killit (pid, user, dev, host) @@ -1312,6 +1338,8 @@ #ifdef SUNOS struct passwd *pw; #endif + char *devname = NULL; + TRACE (killit); DUMP (pid); DUMPS (user); @@ -1323,21 +1351,22 @@ killit_xsession (utmpp->ut_pid, user, host); return; } - /* Tell user which limit they have exceeded and that they will be logged off */ - if ((tty = open (dev, O_WRONLY | O_NOCTTY | O_NONBLOCK)) < 0) + + /* Tell user which limit they have exceeded and that they will be + logged off. Trim the tty name first. */ + devname = build_devname(dev); + if ((tty = open (devname, O_WRONLY | O_NOCTTY | O_NONBLOCK)) < 0) { + int err = errno; openlog ("timeoutd", OPENLOG_FLAGS, LOG_DAEMON); - syslog (LOG_ERR, "Could not write logoff message to %s.", dev); + syslog (LOG_ERR, "killit(%s, %s): %s", user, devname, sys_errlist[err]); closelog (); -#ifndef DEBUG /* Continue to catch the SIGSEGV */ - return; -#endif /* DEBUG */ } - - /* check if the pid is sshd. If so, get PID of the child process (another ssh, owned by the user). - Test reverse if this child process is also ssh and owned by the user we want to log out. - (because we don't want to slay another user ;) */ + /* check if the pid is sshd. If so, get PID of the child process + (another ssh, owned by the user). Test reverse if this child + process is also ssh and owned by the user we want to log + out. (because we don't want to slay another user ;) */ cpid = getcpid (pid); #ifdef DEBUG openlog ("timeoutd", OPENLOG_FLAGS, LOG_DAEMON); @@ -1464,17 +1493,18 @@ { int fd; int disc; - + char *devname = NULL; + #ifdef linux if (chk_xsession (d, host) || chk_xterm (d, host)) return N_TTY; - if ((fd = open (d, O_RDONLY | O_NONBLOCK | O_NOCTTY)) < 0) + devname = build_devname(d); + if ((fd = open (devname, O_WRONLY | O_NOCTTY | O_NONBLOCK)) < 0) { + int err = errno; openlog ("timeoutd", OPENLOG_FLAGS, LOG_DAEMON); - syslog (LOG_WARNING, - "Could not open %s for checking line discipline - idle limits will be enforced.", - d); + syslog (LOG_ERR, "getdisc(%s, %s): %s", d, host, sys_errlist[err]); closelog (); return N_TTY; } @@ -1810,11 +1840,12 @@ { /* check only PIDs */ sprintf (path, "/proc/%s/status", cont->d_name); proc_file = fopen (path, "r"); - if (!proc_file) { - printf ("error opening proc status file %s\n", path); - continue; - } - + if (!proc_file) + { + printf ("error opening proc status file %s\n", path); + continue; + } + while (!fscanf (proc_file, "PPid: %s", akt_pid)) fgets (akt_pid, 10, proc_file);