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.15 2000/08/26 18:27:18 marekm Exp $")
35 #include <sys/types.h>
41 #include "prototypes.h"
54 #ifndef DEFAULT_HUP_MESG
55 #define DEFAULT_HUP_MESG "login time exceeded\r\n"
59 #define HUP_MESG_FILE "/etc/logoutd.mesg"
62 /* local function prototypes */
63 static int check_login(const struct utmp *);
67 * check_login - check if user (struct utmp) allowed to stay logged in
70 check_login(const struct utmp *ut)
72 char user[sizeof(ut->ut_user) + 1];
76 * ut_user may not have the terminating NUL.
78 strncpy(user, ut->ut_user, sizeof(ut->ut_user));
79 user[sizeof(ut->ut_user)] = '\0';
84 * Check if they are allowed to be logged in right now.
86 if (!isttytime(user, ut->ut_line, now))
90 * Check for how long they are allowed to stay logged in.
91 * XXX - not implemented yet. Need to add a new field to
92 * /etc/porttime (login time limit in minutes, or no limit,
93 * based on username, tty, and time of login).
95 if (now - ut->ut_time > get_time_limit(user, ut->ut_line, ut->ut_time))
103 send_mesg_to_tty(int tty_fd)
106 FILE *mesg_file, *tty_file;
109 tty_file = fdopen(tty_fd, "w");
113 is_tty = (GTTY(tty_fd, &oldt) == 0);
115 /* Suggested by Ivan Nejgebauar <ian@unsux.ns.ac.yu>:
116 set OPOST before writing the message. */
118 newt.c_oflag |= OPOST;
122 mesg_file = fopen(HUP_MESG_FILE, "r");
124 while ((c = getc(mesg_file)) != EOF) {
126 putc('\r', tty_file);
131 fputs(DEFAULT_HUP_MESG, tty_file);
143 * logoutd - logout daemon to enforce /etc/porttime file policy
145 * logoutd is started at system boot time and enforces the login
146 * time and port restrictions specified in /etc/porttime. The
147 * utmp file is periodically scanned and offending users are logged
148 * off from the system.
152 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 */
181 * Put this process in the background.
188 } else if (pid < 0) {
196 * Start syslogging everything
199 Prog = Basename(argv[0]);
201 openlog(Prog, LOG_PID|LOG_CONS|LOG_NOWAIT, LOG_AUTH);
204 * Scan the UTMP file once per minute looking for users that
205 * are not supposed to still be logged in.
211 * Attempt to re-open the utmp file. The file is only
212 * open while it is being used.
218 * Read all of the entries in the utmp file. The entries
219 * for login sessions will be checked to see if the user
220 * is permitted to be signed on at this time.
223 while ((ut = getutent())) {
225 if (ut->ut_type != USER_PROCESS)
228 if (ut->ut_user[0] == '\0')
234 * Put the rest of this in a child process. This
235 * keeps the scan from waiting on other ports to die.
242 } else if (pid < 0) {
243 /* failed - give up until the next scan */
248 if (strncmp(ut->ut_line, "/dev/", 5) != 0)
249 strcpy(tty_name, "/dev/");
253 strcat(tty_name, ut->ut_line);
257 tty_fd = open(tty_name, O_WRONLY|O_NDELAY|O_NOCTTY);
259 send_mesg_to_tty(tty_fd);
263 #ifdef USER_PROCESS /* USG_UTMP */
264 if (ut->ut_pid > 1) {
265 kill(- ut->ut_pid, SIGHUP);
267 kill(- ut->ut_pid, SIGKILL);
269 #else /* BSD || SUN || SUN4 */
271 * vhangup() the line to kill try and kill
272 * whatever is out there using it.
275 if ((tty_fd = open (tty_name, O_RDONLY|O_NDELAY)) == -1)
280 #endif /* BSD || SUN || SUN4 */
282 strncpy(user, ut->ut_line, sizeof(user) - 1);
283 user[sizeof(user) - 1] = '\0';
285 SYSLOG((LOG_NOTICE, "logged off user `%s' on `%s'\n",
289 * This child has done all it can, drop dead.
301 * Reap any dead babies ...
304 while (wait (&status) != -1)
307 return 1; /* not reached */