2007-10-07 17:14:02 +05:30
|
|
|
/*
|
2008-04-27 06:10:09 +05:30
|
|
|
* Copyright (c) 1991 - 1993, Julianne Frances Haugh
|
|
|
|
* Copyright (c) 1996 - 2000, Marek Michałkiewicz
|
|
|
|
* Copyright (c) 2001 - 2006, Tomasz Kłoczko
|
2007-10-07 17:14:02 +05:30
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
2008-04-27 06:10:09 +05:30
|
|
|
* 3. The name of the copyright holders or contributors may not be used to
|
|
|
|
* endorse or promote products derived from this software without
|
|
|
|
* specific prior written permission.
|
2007-10-07 17:14:02 +05:30
|
|
|
*
|
2008-04-27 06:10:09 +05:30
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
|
|
|
|
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
* HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
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"
|
|
|
|
/*
|
|
|
|
* 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/groupadd.c, src/chage.c, src/login.c, src/grpconv.c,
src/groups.c, src/grpunconv.c, src/chsh.c: Prog is now global (not
static to the file) so that it can be used by the helper functions
of libmisc.
* lib/prototypes.h: Added extern char *Prog.
* libmisc/find_new_gid.c, libmisc/find_new_uid.c: Indicate the
program name with the warning.
2008-09-06 18:21:53 +05:30
|
|
|
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 */
|
2009-04-29 01:33:23 +05:30
|
|
|
#ifdef USE_UTMPX
|
2008-08-31 22:58:30 +05:30
|
|
|
static int check_login (const struct utmpx *ut);
|
2009-04-29 01:33:23 +05:30
|
|
|
#else /* !USE_UTMPX */
|
2008-08-31 22:58:30 +05:30
|
|
|
static int check_login (const struct utmp *ut);
|
2009-04-29 01:33:23 +05:30
|
|
|
#endif /* !USE_UTMPX */
|
2008-08-31 22:58:30 +05:30
|
|
|
static void send_mesg_to_tty (int tty_fd);
|
2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
/*
|
2007-10-07 17:15:23 +05:30
|
|
|
* check_login - check if user (struct utmpx/utmp) allowed to stay logged in
|
2007-10-07 17:14:02 +05:30
|
|
|
*/
|
2009-04-29 01:33:23 +05:30
|
|
|
#ifdef USE_UTMPX
|
2007-10-07 17:15:23 +05:30
|
|
|
static int check_login (const struct utmpx *ut)
|
2009-04-29 01:33:23 +05:30
|
|
|
#else /* !USE_UTMPX */
|
2007-10-07 17:14:59 +05:30
|
|
|
static int check_login (const struct utmp *ut)
|
2009-04-29 01:33:23 +05:30
|
|
|
#endif /* !USE_UTMPX */
|
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
|
|
|
int c;
|
|
|
|
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) {
|
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
|
2007-10-07 17:15:23 +05:30
|
|
|
* utmpx/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
|
|
|
|
2009-04-29 01:33:23 +05:30
|
|
|
#ifdef USE_UTMPX
|
2007-10-07 17:15:23 +05:30
|
|
|
struct utmpx *ut;
|
2009-04-29 01:33:23 +05:30
|
|
|
#else /* !USE_UTMPX */
|
2007-10-07 17:14:02 +05:30
|
|
|
struct utmp *ut;
|
2009-04-29 01:33:23 +05:30
|
|
|
#endif /* !USE_UTMPX */
|
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 */
|
2007-10-07 17:14:59 +05:30
|
|
|
exit (0);
|
2007-10-07 17:14:08 +05:30
|
|
|
} else if (pid < 0) {
|
|
|
|
/* error */
|
2007-10-07 17:14:59 +05:30
|
|
|
perror ("fork");
|
|
|
|
exit (1);
|
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]);
|
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
|
|
|
|
|
|
|
/*
|
2007-10-07 17:15:23 +05:30
|
|
|
* Scan the utmpx/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
|
|
|
|
|
|
|
/*
|
2007-10-07 17:15:23 +05:30
|
|
|
* Attempt to re-open the utmpx/utmp file. The file is only
|
2007-10-07 17:14:02 +05:30
|
|
|
* open while it is being used.
|
|
|
|
*/
|
2009-04-29 01:33:23 +05:30
|
|
|
#ifdef USE_UTMPX
|
2007-10-07 17:15:23 +05:30
|
|
|
setutxent ();
|
2009-04-29 01:33:23 +05:30
|
|
|
#else /* !USE_UTMPX */
|
2007-10-07 17:14:59 +05:30
|
|
|
setutent ();
|
2009-04-29 01:33:23 +05:30
|
|
|
#endif /* !USE_UTMPX */
|
2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
/*
|
2007-10-07 17:15:23 +05:30
|
|
|
* Read all of the entries in the utmpx/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.
|
|
|
|
*/
|
2009-04-29 01:33:23 +05:30
|
|
|
#ifdef USE_UTMPX
|
2008-09-20 20:26:10 +05:30
|
|
|
while ((ut = getutxent ()) != NULL)
|
2009-04-29 01:33:23 +05:30
|
|
|
#else /* !USE_UTMPX */
|
2008-09-20 20:26:10 +05:30
|
|
|
while ((ut = getutent ()) != NULL)
|
2009-04-29 01:33:23 +05:30
|
|
|
#endif /* !USE_UTMPX */
|
2008-09-20 20:26:10 +05:30
|
|
|
{
|
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
|
|
|
|
2007-10-07 17:14:59 +05:30
|
|
|
strcat (tty_name, ut->ut_line);
|
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.
|
|
|
|
*/
|
|
|
|
exit (0);
|
|
|
|
}
|
|
|
|
|
2009-04-29 01:33:23 +05:30
|
|
|
#ifdef USE_UTMPX
|
2007-10-07 17:15:23 +05:30
|
|
|
endutxent ();
|
2009-04-29 01:33:23 +05:30
|
|
|
#else /* !USE_UTMPX */
|
2007-10-07 17:14:59 +05:30
|
|
|
endutent ();
|
2009-04-29 01:33:23 +05:30
|
|
|
#endif /* !USE_UTMPX */
|
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
|
|
|
}
|
2007-10-07 17:15:23 +05:30
|
|
|
return 1;
|
|
|
|
/* NOT REACHED */
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
2008-06-10 01:32:46 +05:30
|
|
|
|