2 * Copyright 1991 - 1993, Julianne Frances Haugh
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of Julianne F. Haugh nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY JULIE HAUGH AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL JULIE HAUGH OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 RCSID(PKG_VER "$Id: logoutd.c,v 1.13 1999/06/07 16:40:45 marekm Exp $")
35 #include <sys/types.h>
41 #include "prototypes.h"
54 static char *mesg_buf = "login time exceeded\r\n"; /* XXX warning: const */
55 static int mesg_len = 21;
59 #define HUP_MESG_FILE "/etc/logoutd.mesg"
62 /* local function prototypes */
63 static RETSIGTYPE reload_mesg P_((int));
64 static int check_login P_((const struct utmp *));
65 int main P_((int, char **));
68 * reload_mesg - reload the message that is output when killing a process
77 signal (sig, reload_mesg);
79 if (stat (HUP_MESG_FILE, &sb))
82 if ((sb.st_mode & S_IFMT) != S_IFREG)
85 if ((fd = open (HUP_MESG_FILE, O_RDONLY)) != -1) {
86 if (sb.st_size + 1 > mesg_size) {
87 if (mesg_buf && mesg_size)
90 mesg_len = sb.st_size;
91 mesg_size = mesg_len + 1;
92 if (! (mesg_buf = (char *) malloc (mesg_len + 1)))
95 mesg_len = sb.st_size;
97 if (read (fd, mesg_buf, mesg_len) != mesg_len) {
109 * check_login - check if user (struct utmp) allowed to stay logged in
112 check_login(const struct utmp *ut)
114 char user[sizeof(ut->ut_user) + 1];
118 * ut_user may not have the terminating NUL.
120 strncpy(user, ut->ut_user, sizeof(ut->ut_user));
121 user[sizeof(ut->ut_user)] = '\0';
126 * Check if they are allowed to be logged in right now.
128 if (!isttytime(user, ut->ut_line, now))
132 * Check for how long they are allowed to stay logged in.
133 * XXX - not implemented yet. Need to add a new field to
134 * /etc/porttime (login time limit in minutes, or no limit,
135 * based on username, tty, and time of login).
137 if (now - ut->ut_time > get_time_limit(user, ut->ut_line, ut->ut_time))
144 * logoutd - logout daemon to enforce /etc/porttime file policy
146 * logoutd is started at system boot time and enforces the login
147 * time and port restrictions specified in /etc/porttime. The
148 * utmp file is periodically scanned and offending users are logged
149 * off from the system.
153 main(int argc, char **argv)
158 char user[sizeof(ut->ut_user) + 1]; /* terminating NUL */
159 char tty_name[sizeof(ut->ut_line) + 6]; /* /dev/ + NUL */
162 setlocale(LC_ALL, "");
163 bindtextdomain(PACKAGE, LOCALEDIR);
167 for (i = 0;close (i) == 0;i++)
174 setpgrp(getpid(), getpid());
176 #else /* !HAVE_SETPGRP */
177 setpgid(getpid(), getpid()); /* BSD || SUN || SUN4 */
178 #endif /* !HAVE_SETPGRP */
180 reload_mesg (SIGHUP);
183 * Put this process in the background.
191 * Start syslogging everything
194 Prog = Basename(argv[0]);
196 openlog(Prog, LOG_PID|LOG_CONS|LOG_NOWAIT, LOG_AUTH);
199 * Scan the UTMP file once per minute looking for users that
200 * are not supposed to still be logged in.
209 * Attempt to re-open the utmp file. The file is only
210 * open while it is being used.
216 * Read all of the entries in the utmp file. The entries
217 * for login sessions will be checked to see if the user
218 * is permitted to be signed on at this time.
221 while ((ut = getutent())) {
223 if (ut->ut_type != USER_PROCESS)
226 if (ut->ut_user[0] == '\0')
232 * Put the rest of this in a child process. This
233 * keeps the scan from waiting on other ports to die.
239 if (strncmp(ut->ut_line, "/dev/", 5) != 0)
240 strcpy(tty_name, "/dev/");
244 strcat(tty_name, ut->ut_line);
248 if ((tty_fd = open (tty_name,
249 O_WRONLY|O_NDELAY|O_NOCTTY)) != -1) {
250 /* Suggested by Ivan Nejgebauar <ian@unsux.ns.ac.yu>: set OPOST
251 before writing the message. --marekm */
257 newt.c_oflag |= OPOST;
258 #else /* XXX - I'm too young to know bsd sgtty, sorry :). --marekm */
261 write (tty_fd, mesg_buf, mesg_len);
266 #ifdef USER_PROCESS /* USG_UTMP */
267 if (ut->ut_pid > 1) {
268 kill(- ut->ut_pid, SIGHUP);
270 kill(- ut->ut_pid, SIGKILL);
272 #else /* BSD || SUN || SUN4 */
274 * vhangup() the line to kill try and kill
275 * whatever is out there using it.
278 if ((tty_fd = open (tty_name, O_RDONLY|O_NDELAY)) == -1)
283 #endif /* BSD || SUN || SUN4 */
287 "logged off user `%.*s' on `%.*s'\n",
288 (int) sizeof(ut->ut_user), ut->ut_user,
289 (int) sizeof(ut->ut_line), ut->ut_line));
291 /* avoid gcc warnings about %.*s in syslog() */
292 strncpy(user, ut->ut_line, sizeof(user) - 1);
293 user[sizeof(user) - 1] = '\0';
295 SYSLOG((LOG_NOTICE, "logged off user `%s' on `%s'\n",
300 * This child has done all it can, drop dead.
309 * Reap any dead babies ...
312 while (wait (&status) != -1)
315 return 1; /* not reached */