2007-10-07 17:14:02 +05:30
|
|
|
/*
|
2021-12-05 21:05:27 +05:30
|
|
|
* SPDX-FileCopyrightText: 1991 - 1993, Julianne Frances Haugh
|
|
|
|
* SPDX-FileCopyrightText: 1996 - 2000, Marek Michałkiewicz
|
|
|
|
* SPDX-FileCopyrightText: 2001 - 2006, Tomasz Kłoczko
|
2007-10-07 17:14:02 +05:30
|
|
|
*
|
2021-12-05 21:05:27 +05:30
|
|
|
* SPDX-License-Identifier: BSD-3-Clause
|
2007-10-07 17:14:02 +05:30
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
2007-11-11 05:16:11 +05:30
|
|
|
#ident "$Id$"
|
2007-10-07 17:17:01 +05:30
|
|
|
|
2007-10-07 17:14:02 +05:30
|
|
|
#include <fcntl.h>
|
2007-10-07 17:17:01 +05:30
|
|
|
#include <signal.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
2007-10-07 17:14:02 +05:30
|
|
|
#include "defines.h"
|
2007-10-07 17:17:01 +05:30
|
|
|
#include "prototypes.h"
|
2021-11-29 05:07:53 +05:30
|
|
|
#include "shadowlog.h"
|
2007-10-07 17:17:01 +05:30
|
|
|
/*
|
|
|
|
* Global variables
|
|
|
|
*/
|
* src/newgrp.c, src/userdel.c, src/grpck.c, src/gpasswd.c,
src/newusers.c, src/pwconv.c, src/chpasswd.c, src/logoutd.c,
src/chfn.c, src/groupmems.c, src/usermod.c, src/pwunconv.c,
src/expiry.c, src/groupdel.c, src/chgpasswd.c, src/useradd.c,
src/su.c, src/groupmod.c, src/passwd.c, src/pwck.c, src/chage.c,
src/groupadd.c, src/login.c, src/grpconv.c, src/groups.c,
src/grpunconv.c, src/chsh.c: Prog changed to a constant string.
2010-08-23 01:06:09 +05:30
|
|
|
const char *Prog;
|
2007-10-07 17:14:02 +05:30
|
|
|
|
2007-10-07 17:14:08 +05:30
|
|
|
#ifndef DEFAULT_HUP_MESG
|
2007-10-07 17:17:01 +05:30
|
|
|
#define DEFAULT_HUP_MESG _("login time exceeded\n\n")
|
2007-10-07 17:14:08 +05:30
|
|
|
#endif
|
2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
#ifndef HUP_MESG_FILE
|
|
|
|
#define HUP_MESG_FILE "/etc/logoutd.mesg"
|
|
|
|
#endif
|
|
|
|
|
2008-08-31 22:58:30 +05:30
|
|
|
/* local function prototypes */
|
|
|
|
static int check_login (const struct utmp *ut);
|
|
|
|
static void send_mesg_to_tty (int tty_fd);
|
2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
/*
|
2022-12-21 23:03:40 +05:30
|
|
|
* check_login - check if user (struct utmp) allowed to stay logged in
|
2007-10-07 17:14:02 +05:30
|
|
|
*/
|
2007-10-07 17:14:59 +05:30
|
|
|
static int check_login (const struct utmp *ut)
|
2007-10-07 17:14:02 +05:30
|
|
|
{
|
2007-10-07 17:14:59 +05:30
|
|
|
char user[sizeof (ut->ut_user) + 1];
|
2007-10-07 17:14:02 +05:30
|
|
|
time_t now;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* ut_user may not have the terminating NUL.
|
|
|
|
*/
|
2007-10-07 17:14:59 +05:30
|
|
|
strncpy (user, ut->ut_user, sizeof (ut->ut_user));
|
|
|
|
user[sizeof (ut->ut_user)] = '\0';
|
2007-10-07 17:14:02 +05:30
|
|
|
|
2008-06-14 02:04:46 +05:30
|
|
|
(void) time (&now);
|
2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
/*
|
|
|
|
* Check if they are allowed to be logged in right now.
|
|
|
|
*/
|
2008-06-10 01:32:46 +05:30
|
|
|
if (!isttytime (user, ut->ut_line, now)) {
|
2007-10-07 17:14:02 +05:30
|
|
|
return 0;
|
2008-06-10 01:32:46 +05:30
|
|
|
}
|
2007-10-07 17:14:02 +05:30
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2007-10-07 17:14:08 +05:30
|
|
|
|
2007-10-07 17:14:59 +05:30
|
|
|
static void send_mesg_to_tty (int tty_fd)
|
2007-10-07 17:14:08 +05:30
|
|
|
{
|
|
|
|
TERMIO oldt, newt;
|
|
|
|
FILE *mesg_file, *tty_file;
|
2008-06-10 01:32:46 +05:30
|
|
|
bool is_tty;
|
2007-10-07 17:14:08 +05:30
|
|
|
|
2007-10-07 17:14:59 +05:30
|
|
|
tty_file = fdopen (tty_fd, "w");
|
2008-06-10 01:32:46 +05:30
|
|
|
if (NULL == tty_file) {
|
2007-10-07 17:14:08 +05:30
|
|
|
return;
|
2008-06-10 01:32:46 +05:30
|
|
|
}
|
2007-10-07 17:14:08 +05:30
|
|
|
|
2007-10-07 17:14:59 +05:30
|
|
|
is_tty = (GTTY (tty_fd, &oldt) == 0);
|
2007-10-07 17:14:08 +05:30
|
|
|
if (is_tty) {
|
|
|
|
/* Suggested by Ivan Nejgebauar <ian@unsux.ns.ac.yu>:
|
2007-10-07 17:14:59 +05:30
|
|
|
set OPOST before writing the message. */
|
2007-10-07 17:14:08 +05:30
|
|
|
newt = oldt;
|
|
|
|
newt.c_oflag |= OPOST;
|
2007-10-07 17:14:59 +05:30
|
|
|
STTY (tty_fd, &newt);
|
2007-10-07 17:14:08 +05:30
|
|
|
}
|
|
|
|
|
2007-10-07 17:14:59 +05:30
|
|
|
mesg_file = fopen (HUP_MESG_FILE, "r");
|
2008-06-10 01:32:46 +05:30
|
|
|
if (NULL != mesg_file) {
|
* src/newgrp.c: Limit the scope of variable pid.
* src/login_nopam.c: Limit the scope of variables end, lineno, i,
str_len.
* src/logoutd.c: Limit the scope of variable c.
* src/vipw.c: Re-indent.
* src/vipw.c: Close the file after the creation of the backup.
* src/useradd.c (set_default): Close input file on failure.
* src/useradd.c: Limit the scope of variables spool, file, fd, gr,
gid, mode.
* src/passwd.c: Limit the scope of variables last and ok.
* src/chage.c: Fix typo (non breaking space).
* src/login.c: Limit the scope of variables erasechar killchar, c,
failed.
* src/groups.c: Limit the scope of variable ngroups, pri_grp, i.
* src/id.c: Limit the scope of variable i.
2010-03-23 16:56:34 +05:30
|
|
|
int c;
|
2007-10-07 17:14:59 +05:30
|
|
|
while ((c = getc (mesg_file)) != EOF) {
|
2008-06-10 01:32:46 +05:30
|
|
|
if (c == '\n') {
|
2007-10-07 17:14:59 +05:30
|
|
|
putc ('\r', tty_file);
|
2008-06-10 01:32:46 +05:30
|
|
|
}
|
2007-10-07 17:14:59 +05:30
|
|
|
putc (c, tty_file);
|
2007-10-07 17:14:08 +05:30
|
|
|
}
|
2007-10-07 17:14:59 +05:30
|
|
|
fclose (mesg_file);
|
2007-10-07 17:14:08 +05:30
|
|
|
} else {
|
2007-10-07 17:14:59 +05:30
|
|
|
fputs (DEFAULT_HUP_MESG, tty_file);
|
2007-10-07 17:14:08 +05:30
|
|
|
}
|
2007-10-07 17:14:59 +05:30
|
|
|
fflush (tty_file);
|
|
|
|
fclose (tty_file);
|
2007-10-07 17:14:08 +05:30
|
|
|
|
|
|
|
if (is_tty) {
|
2007-10-07 17:14:59 +05:30
|
|
|
STTY (tty_fd, &oldt);
|
2007-10-07 17:14:08 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-10-07 17:14:02 +05:30
|
|
|
/*
|
|
|
|
* logoutd - logout daemon to enforce /etc/porttime file policy
|
|
|
|
*
|
|
|
|
* logoutd is started at system boot time and enforces the login
|
2007-10-07 17:14:59 +05:30
|
|
|
* time and port restrictions specified in /etc/porttime. The
|
2022-12-21 23:03:40 +05:30
|
|
|
* utmp file is periodically scanned and offending users are logged
|
2007-10-07 17:14:02 +05:30
|
|
|
* off from the system.
|
|
|
|
*/
|
2007-10-07 17:14:59 +05:30
|
|
|
int main (int argc, char **argv)
|
2007-10-07 17:14:02 +05:30
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int status;
|
2007-10-07 17:14:08 +05:30
|
|
|
pid_t pid;
|
2007-10-07 17:15:23 +05:30
|
|
|
|
2007-10-07 17:14:02 +05:30
|
|
|
struct utmp *ut;
|
2007-10-07 17:14:59 +05:30
|
|
|
char user[sizeof (ut->ut_user) + 1]; /* terminating NUL */
|
|
|
|
char tty_name[sizeof (ut->ut_line) + 6]; /* /dev/ + NUL */
|
2007-10-07 17:14:02 +05:30
|
|
|
int tty_fd;
|
|
|
|
|
2008-08-31 22:58:30 +05:30
|
|
|
if (1 != argc) {
|
|
|
|
(void) fputs (_("Usage: logoutd\n"), stderr);
|
|
|
|
}
|
|
|
|
|
2008-06-10 01:32:46 +05:30
|
|
|
(void) setlocale (LC_ALL, "");
|
|
|
|
(void) bindtextdomain (PACKAGE, LOCALEDIR);
|
|
|
|
(void) textdomain (PACKAGE);
|
2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
#ifndef DEBUG
|
2007-10-07 17:14:59 +05:30
|
|
|
for (i = 0; close (i) == 0; i++);
|
2007-10-07 17:14:02 +05:30
|
|
|
|
2007-10-07 17:16:34 +05:30
|
|
|
setpgrp ();
|
2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
/*
|
|
|
|
* Put this process in the background.
|
|
|
|
*/
|
2007-10-07 17:14:59 +05:30
|
|
|
pid = fork ();
|
2007-10-07 17:14:08 +05:30
|
|
|
if (pid > 0) {
|
|
|
|
/* parent */
|
* lib/exitcodes.h: Define E_SUCCESS as EXIT_SUCCESS. Added FIXMEs.
* libmisc/chowntty.c, libmisc/rlogin.c, libmisc/sub.c,
src/newusers.c, libmisc/sulog.c, libmisc/system.c, src/logoutd.c,
src/groups.c, src/id.c, lib/encrypt.c, libmisc/audit_help.c,
libmisc/limits.c: Return EXIT_FAILURE instead of 1, and
EXIT_SUCCESS instead of 0.
* libmisc/audit_help.c: Replace an fprintf() by fputs().
* libmisc/audit_help.c: Remove documentation of the audit_logger
returned values. The function returns void.
* libmisc/system.c: Only return status if waitpid succeeded.
Return -1 otherwise.
2009-05-01 02:38:49 +05:30
|
|
|
exit (EXIT_SUCCESS);
|
2007-10-07 17:14:08 +05:30
|
|
|
} else if (pid < 0) {
|
|
|
|
/* error */
|
2007-10-07 17:14:59 +05:30
|
|
|
perror ("fork");
|
* lib/exitcodes.h: Define E_SUCCESS as EXIT_SUCCESS. Added FIXMEs.
* libmisc/chowntty.c, libmisc/rlogin.c, libmisc/sub.c,
src/newusers.c, libmisc/sulog.c, libmisc/system.c, src/logoutd.c,
src/groups.c, src/id.c, lib/encrypt.c, libmisc/audit_help.c,
libmisc/limits.c: Return EXIT_FAILURE instead of 1, and
EXIT_SUCCESS instead of 0.
* libmisc/audit_help.c: Replace an fprintf() by fputs().
* libmisc/audit_help.c: Remove documentation of the audit_logger
returned values. The function returns void.
* libmisc/system.c: Only return status if waitpid succeeded.
Return -1 otherwise.
2009-05-01 02:38:49 +05:30
|
|
|
exit (EXIT_FAILURE);
|
2007-10-07 17:14:08 +05:30
|
|
|
}
|
2007-10-07 17:14:59 +05:30
|
|
|
#endif /* !DEBUG */
|
2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
/*
|
|
|
|
* Start syslogging everything
|
|
|
|
*/
|
2007-10-07 17:14:59 +05:30
|
|
|
Prog = Basename (argv[0]);
|
2021-11-29 05:07:53 +05:30
|
|
|
log_set_progname(Prog);
|
|
|
|
log_set_logfd(stderr);
|
2007-10-07 17:14:02 +05:30
|
|
|
|
2007-10-07 17:15:40 +05:30
|
|
|
OPENLOG ("logoutd");
|
2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
/*
|
2022-12-21 23:03:40 +05:30
|
|
|
* Scan the utmp file once per minute looking for users that
|
2007-10-07 17:14:02 +05:30
|
|
|
* are not supposed to still be logged in.
|
|
|
|
*/
|
2008-06-10 01:32:46 +05:30
|
|
|
while (true) {
|
2007-10-07 17:14:02 +05:30
|
|
|
|
2021-09-13 21:53:17 +05:30
|
|
|
/*
|
2022-12-21 23:03:40 +05:30
|
|
|
* Attempt to re-open the utmp file. The file is only
|
2007-10-07 17:14:02 +05:30
|
|
|
* open while it is being used.
|
|
|
|
*/
|
2007-10-07 17:14:59 +05:30
|
|
|
setutent ();
|
2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
/*
|
2022-12-21 23:03:40 +05:30
|
|
|
* Read all of the entries in the utmp file. The entries
|
2007-10-07 17:14:02 +05:30
|
|
|
* for login sessions will be checked to see if the user
|
|
|
|
* is permitted to be signed on at this time.
|
|
|
|
*/
|
2022-12-21 23:14:06 +05:30
|
|
|
while ((ut = getutent ()) != NULL) {
|
2008-06-10 01:32:46 +05:30
|
|
|
if (ut->ut_type != USER_PROCESS) {
|
2007-10-07 17:14:02 +05:30
|
|
|
continue;
|
2008-06-10 01:32:46 +05:30
|
|
|
}
|
|
|
|
if (ut->ut_user[0] == '\0') {
|
2007-10-07 17:14:02 +05:30
|
|
|
continue;
|
2008-06-10 01:32:46 +05:30
|
|
|
}
|
|
|
|
if (check_login (ut)) {
|
2007-10-07 17:14:02 +05:30
|
|
|
continue;
|
2008-06-10 01:32:46 +05:30
|
|
|
}
|
2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
/*
|
2007-10-07 17:14:59 +05:30
|
|
|
* Put the rest of this in a child process. This
|
2007-10-07 17:14:02 +05:30
|
|
|
* keeps the scan from waiting on other ports to die.
|
|
|
|
*/
|
|
|
|
|
2007-10-07 17:14:59 +05:30
|
|
|
pid = fork ();
|
2007-10-07 17:14:08 +05:30
|
|
|
if (pid > 0) {
|
|
|
|
/* parent */
|
2007-10-07 17:14:02 +05:30
|
|
|
continue;
|
2007-10-07 17:14:08 +05:30
|
|
|
} else if (pid < 0) {
|
|
|
|
/* failed - give up until the next scan */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* child */
|
2007-10-07 17:14:02 +05:30
|
|
|
|
2008-06-10 01:32:46 +05:30
|
|
|
if (strncmp (ut->ut_line, "/dev/", 5) != 0) {
|
2007-10-07 17:14:59 +05:30
|
|
|
strcpy (tty_name, "/dev/");
|
2008-06-10 01:32:46 +05:30
|
|
|
} else {
|
2007-10-07 17:14:02 +05:30
|
|
|
tty_name[0] = '\0';
|
2008-06-10 01:32:46 +05:30
|
|
|
}
|
2007-10-07 17:14:02 +05:30
|
|
|
|
2022-03-19 01:05:02 +05:30
|
|
|
strncat (tty_name, ut->ut_line, UT_LINESIZE);
|
2007-10-07 17:14:02 +05:30
|
|
|
#ifndef O_NOCTTY
|
|
|
|
#define O_NOCTTY 0
|
|
|
|
#endif
|
2007-10-07 17:14:59 +05:30
|
|
|
tty_fd =
|
2007-10-07 17:16:07 +05:30
|
|
|
open (tty_name, O_WRONLY | O_NDELAY | O_NOCTTY);
|
2007-10-07 17:14:08 +05:30
|
|
|
if (tty_fd != -1) {
|
2007-10-07 17:14:59 +05:30
|
|
|
send_mesg_to_tty (tty_fd);
|
|
|
|
close (tty_fd);
|
|
|
|
sleep (10);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
2007-10-07 17:17:45 +05:30
|
|
|
|
2007-10-07 17:14:02 +05:30
|
|
|
if (ut->ut_pid > 1) {
|
2007-10-07 17:14:59 +05:30
|
|
|
kill (-ut->ut_pid, SIGHUP);
|
|
|
|
sleep (10);
|
|
|
|
kill (-ut->ut_pid, SIGKILL);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
|
|
|
|
2007-10-07 17:14:59 +05:30
|
|
|
strncpy (user, ut->ut_user, sizeof (user) - 1);
|
|
|
|
user[sizeof (user) - 1] = '\0';
|
2007-10-07 17:14:02 +05:30
|
|
|
|
2007-10-07 17:14:59 +05:30
|
|
|
SYSLOG ((LOG_NOTICE,
|
* src/chfn.c, src/chsh.c, src/groupdel.c, src/groupmems.c,
src/groupmod.c, src/grpck.c, src/login.c, src/logoutd.c,
src/newgrp.c, src/newusers.c, src/passwd.c, src/pwck.c,
src/suauth.c, src/useradd.c, src/userdel.c, src/usermod.c,
src/vipw.c: Complete the switch from the `' quotation style to ''.
Do it also in SYSLOG messages. Quote some parameters. All this
permits to merge some messages.
2008-08-06 21:21:52 +05:30
|
|
|
"logged off user '%s' on '%s'", user,
|
2007-10-07 17:14:59 +05:30
|
|
|
tty_name));
|
2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
/*
|
|
|
|
* This child has done all it can, drop dead.
|
|
|
|
*/
|
* lib/exitcodes.h: Define E_SUCCESS as EXIT_SUCCESS. Added FIXMEs.
* libmisc/chowntty.c, libmisc/rlogin.c, libmisc/sub.c,
src/newusers.c, libmisc/sulog.c, libmisc/system.c, src/logoutd.c,
src/groups.c, src/id.c, lib/encrypt.c, libmisc/audit_help.c,
libmisc/limits.c: Return EXIT_FAILURE instead of 1, and
EXIT_SUCCESS instead of 0.
* libmisc/audit_help.c: Replace an fprintf() by fputs().
* libmisc/audit_help.c: Remove documentation of the audit_logger
returned values. The function returns void.
* libmisc/system.c: Only return status if waitpid succeeded.
Return -1 otherwise.
2009-05-01 02:38:49 +05:30
|
|
|
exit (EXIT_SUCCESS);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
|
|
|
|
2007-10-07 17:14:59 +05:30
|
|
|
endutent ();
|
2007-10-07 17:14:02 +05:30
|
|
|
|
2007-10-07 17:14:08 +05:30
|
|
|
#ifndef DEBUG
|
2007-10-07 17:14:59 +05:30
|
|
|
sleep (60);
|
2007-10-07 17:14:08 +05:30
|
|
|
#endif
|
2007-10-07 17:14:02 +05:30
|
|
|
/*
|
|
|
|
* Reap any dead babies ...
|
|
|
|
*/
|
2007-10-07 17:14:59 +05:30
|
|
|
while (wait (&status) != -1);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
* lib/exitcodes.h: Define E_SUCCESS as EXIT_SUCCESS. Added FIXMEs.
* libmisc/chowntty.c, libmisc/rlogin.c, libmisc/sub.c,
src/newusers.c, libmisc/sulog.c, libmisc/system.c, src/logoutd.c,
src/groups.c, src/id.c, lib/encrypt.c, libmisc/audit_help.c,
libmisc/limits.c: Return EXIT_FAILURE instead of 1, and
EXIT_SUCCESS instead of 0.
* libmisc/audit_help.c: Replace an fprintf() by fputs().
* libmisc/audit_help.c: Remove documentation of the audit_logger
returned values. The function returns void.
* libmisc/system.c: Only return status if waitpid succeeded.
Return -1 otherwise.
2009-05-01 02:38:49 +05:30
|
|
|
|
|
|
|
return EXIT_FAILURE;
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
2008-06-10 01:32:46 +05:30
|
|
|
|