summaryrefslogtreecommitdiffstats
path: root/src/logoutd.c
blob: c8705101ded0bb22a43a68556a56c60ca3ac8f06 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/*
 * SPDX-FileCopyrightText: 1991 - 1993, Julianne Frances Haugh
 * SPDX-FileCopyrightText: 1996 - 2000, Marek Michałkiewicz
 * SPDX-FileCopyrightText: 2001 - 2006, Tomasz Kłoczko
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

#include <config.h>

#ident "$Id$"

#include <fcntl.h>
#include <signal.h>
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <utmpx.h>
#include "defines.h"
#include "prototypes.h"
#include "shadowlog.h"
#include "sizeof.h"
#include "string/zustr2stp.h"
/*
 * Global variables
 */
static const char Prog[] = "logoutd";

#ifndef DEFAULT_HUP_MESG
#define DEFAULT_HUP_MESG _("login time exceeded\n\n")
#endif

#ifndef HUP_MESG_FILE
#define HUP_MESG_FILE "/etc/logoutd.mesg"
#endif


/* local function prototypes */
static int check_login (const struct utmpx *ut);
static void send_mesg_to_tty (int tty_fd);


/*
 * check_login - check if user (struct utmpx) allowed to stay logged in
 */
static int
check_login(const struct utmpx *ut)
{
	char    user[sizeof(ut->ut_user) + 1];
	char    line[sizeof(ut->ut_line) + 1];
	time_t  now;

	ZUSTR2STP(user, ut->ut_user);
	ZUSTR2STP(line, ut->ut_line);

	(void) time (&now);

	/*
	 * Check if they are allowed to be logged in right now.
	 */
	if (!isttytime(user, line, now)) {
		return 0;
	}
	return 1;
}


static void send_mesg_to_tty (int tty_fd)
{
	TERMIO oldt, newt;
	FILE *mesg_file, *tty_file;
	bool is_tty;

	tty_file = fdopen (tty_fd, "w");
	if (NULL == tty_file) {
		return;
	}

	is_tty = (GTTY (tty_fd, &oldt) == 0);
	if (is_tty) {
		/* Suggested by Ivan Nejgebauar <ian@unsux.ns.ac.yu>:
		   set OPOST before writing the message. */
		newt = oldt;
		newt.c_oflag |= OPOST;
		STTY (tty_fd, &newt);
	}

	mesg_file = fopen (HUP_MESG_FILE, "r");
	if (NULL != mesg_file) {
		int c;
		while ((c = getc (mesg_file)) != EOF) {
			if (c == '\n') {
				putc ('\r', tty_file);
			}
			putc (c, tty_file);
		}
		fclose (mesg_file);
	} else {
		fputs (DEFAULT_HUP_MESG, tty_file);
	}
	fflush (tty_file);
	fclose (tty_file);

	if (is_tty) {
		STTY (tty_fd, &oldt);
	}
}


/*
 * logoutd - logout daemon to enforce /etc/porttime file policy
 *
 *	logoutd is started at system boot time and enforces the login
 *	time and port restrictions specified in /etc/porttime. The
 *	utmp file is periodically scanned and offending users are logged
 *	off from the system.
 */
int
main(int argc, char **argv)
{
	int    i;
	int    status;
	pid_t  pid;

	struct utmpx  *ut;
	char          user[sizeof (ut->ut_user) + 1];	/* terminating NUL */
	char          tty_name[sizeof (ut->ut_line) + 6];	/* /dev/ + NUL */
	int           tty_fd;

	if (1 != argc) {
		(void) fputs (_("Usage: logoutd\n"), stderr);
	}

	(void) setlocale (LC_ALL, "");
	(void) bindtextdomain (PACKAGE, LOCALEDIR);
	(void) textdomain (PACKAGE);

#ifndef DEBUG
	for (i = 0; close (i) == 0; i++);

	setpgrp ();

	/*
	 * Put this process in the background.
	 */
	pid = fork ();
	if (pid > 0) {
		/* parent */
		exit (EXIT_SUCCESS);
	} else if (pid < 0) {
		/* error */
		perror ("fork");
		exit (EXIT_FAILURE);
	}
#endif				/* !DEBUG */

	/*
	 * Start syslogging everything
	 */
	log_set_progname(Prog);
	log_set_logfd(stderr);

	OPENLOG (Prog);

	/*
	 * Scan the utmp file once per minute looking for users that
	 * are not supposed to still be logged in.
	 */
	while (true) {

		/*
		 * Attempt to re-open the utmp file. The file is only
		 * open while it is being used.
		 */
		setutxent();

		/*
		 * Read all of the entries in the utmp file. The entries
		 * for login sessions will be checked to see if the user
		 * is permitted to be signed on at this time.
		 */
		while ((ut = getutxent()) != NULL) {
			if (ut->ut_type != USER_PROCESS) {
				continue;
			}
			if (ut->ut_user[0] == '\0') {
				continue;
			}
			if (check_login (ut)) {
				continue;
			}

			/*
			 * Put the rest of this in a child process. This
			 * keeps the scan from waiting on other ports to die.
			 */

			pid = fork ();
			if (pid > 0) {
				/* parent */
				continue;
			} else if (pid < 0) {
				/* failed - give up until the next scan */
				break;
			}
			/* child */

			if (strncmp (ut->ut_line, "/dev/", 5) != 0) {
				strcpy (tty_name, "/dev/");
			} else {
				tty_name[0] = '\0';
			}

			strncat(tty_name, ut->ut_line, NITEMS(ut->ut_line));
#ifndef O_NOCTTY
#define O_NOCTTY 0
#endif
			tty_fd =
			    open (tty_name, O_WRONLY | O_NDELAY | O_NOCTTY);
			if (tty_fd != -1) {
				send_mesg_to_tty (tty_fd);
				close (tty_fd);
				sleep (10);
			}

			if (ut->ut_pid > 1) {
				kill (-ut->ut_pid, SIGHUP);
				sleep (10);
				kill (-ut->ut_pid, SIGKILL);
			}

			ZUSTR2STP(user, ut->ut_user);

			SYSLOG ((LOG_NOTICE,
				 "logged off user '%s' on '%s'", user,
				 tty_name));

			/*
			 * This child has done all it can, drop dead.
			 */
			exit (EXIT_SUCCESS);
		}

		endutxent();

#ifndef DEBUG
		sleep (60);
#endif
		/*
		 * Reap any dead babies ...
		 */
		while (wait (&status) != -1);
	}

	return EXIT_FAILURE;
}