]> granicus.if.org Git - shadow/blob - src/logoutd.c
[svn-upgrade] Integrating new upstream version, shadow (20000902)
[shadow] / src / logoutd.c
1 /*
2  * Copyright 1991 - 1993, Julianne Frances Haugh
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
16  *
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
27  * SUCH DAMAGE.
28  */
29
30 #include <config.h>
31
32 #include "rcsid.h"
33 RCSID(PKG_VER "$Id: logoutd.c,v 1.16 2000/09/02 18:40:44 marekm Exp $")
34
35 #include <sys/types.h>
36 #include <sys/stat.h>
37 #include <stdio.h>
38 #include <signal.h>
39 #include <utmp.h>
40 #include <fcntl.h>
41 #include "prototypes.h"
42 #include "defines.h"
43
44 #ifdef  SVR4
45 #include <libgen.h>
46 #endif
47
48 #ifdef  SVR4
49 #define signal  sigset
50 #endif
51
52 static char     *Prog;
53
54 #ifndef DEFAULT_HUP_MESG
55 #define DEFAULT_HUP_MESG "login time exceeded\r\n"
56 #endif
57
58 #ifndef HUP_MESG_FILE
59 #define HUP_MESG_FILE "/etc/logoutd.mesg"
60 #endif
61
62 /* local function prototypes */
63 static int check_login(const struct utmp *);
64
65
66 /*
67  * check_login - check if user (struct utmp) allowed to stay logged in
68  */
69 static int
70 check_login(const struct utmp *ut)
71 {
72         char user[sizeof(ut->ut_user) + 1];
73         time_t now;
74
75         /*
76          * ut_user may not have the terminating NUL.
77          */
78         strncpy(user, ut->ut_user, sizeof(ut->ut_user));
79         user[sizeof(ut->ut_user)] = '\0';
80
81         time(&now);
82
83         /*
84          * Check if they are allowed to be logged in right now.
85          */
86         if (!isttytime(user, ut->ut_line, now))
87                 return 0;
88 #if 0
89         /*
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).
94          */
95         if (now - ut->ut_time > get_time_limit(user, ut->ut_line, ut->ut_time))
96                 return 0;
97 #endif
98         return 1;
99 }
100
101
102 static void
103 send_mesg_to_tty(int tty_fd)
104 {
105         TERMIO oldt, newt;
106         FILE *mesg_file, *tty_file;
107         int c, is_tty;
108
109         tty_file = fdopen(tty_fd, "w");
110         if (!tty_file)
111                 return;
112
113         is_tty = (GTTY(tty_fd, &oldt) == 0);
114         if (is_tty) {
115                 /* Suggested by Ivan Nejgebauar <ian@unsux.ns.ac.yu>:
116                    set OPOST before writing the message.  */
117                 newt = oldt;
118                 newt.c_oflag |= OPOST;
119                 STTY(tty_fd, &newt);
120         }
121
122         mesg_file = fopen(HUP_MESG_FILE, "r");
123         if (mesg_file) {
124                 while ((c = getc(mesg_file)) != EOF) {
125                         if (c == '\n')
126                                 putc('\r', tty_file);
127                         putc(c, tty_file);
128                 }
129                 fclose(mesg_file);
130         } else {
131                 fputs(DEFAULT_HUP_MESG, tty_file);
132         }
133         fflush(tty_file);
134         fclose(tty_file);
135
136         if (is_tty) {
137                 STTY(tty_fd, &oldt);
138         }
139 }
140
141
142 /*
143  * logoutd - logout daemon to enforce /etc/porttime file policy
144  *
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.
149  */
150
151 int
152 main(int argc, char **argv)
153 {
154         int i;
155         int status;
156         pid_t pid;
157         struct utmp *ut;
158         char user[sizeof(ut->ut_user) + 1];  /* terminating NUL */
159         char tty_name[sizeof(ut->ut_line) + 6];  /* /dev/ + NUL */
160         int tty_fd;
161
162         setlocale(LC_ALL, "");
163         bindtextdomain(PACKAGE, LOCALEDIR);
164         textdomain(PACKAGE);
165
166 #ifndef DEBUG
167         for (i = 0;close (i) == 0;i++)
168                 ;
169
170 #ifdef HAVE_SETPGRP
171 #ifdef SETPGRP_VOID
172         setpgrp();  /* USG */
173 #else
174         setpgrp(getpid(), getpid());
175 #endif
176 #else /* !HAVE_SETPGRP */
177         setpgid(getpid(), getpid());  /* BSD || SUN || SUN4 */
178 #endif /* !HAVE_SETPGRP */
179
180         /*
181          * Put this process in the background.
182          */
183
184         pid = fork();
185         if (pid > 0) {
186                 /* parent */
187                 exit(0);
188         } else if (pid < 0) {
189                 /* error */
190                 perror("fork");
191                 exit(1);
192         }
193 #endif /* !DEBUG */
194
195         /*
196          * Start syslogging everything
197          */
198
199         Prog = Basename(argv[0]);
200
201         OPENLOG(Prog);
202
203         /*
204          * Scan the UTMP file once per minute looking for users that
205          * are not supposed to still be logged in.
206          */
207
208         while (1) {
209
210                 /* 
211                  * Attempt to re-open the utmp file.  The file is only
212                  * open while it is being used.
213                  */
214
215                 setutent();
216
217                 /*
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.
221                  */
222
223                 while ((ut = getutent())) {
224 #ifdef USER_PROCESS
225                         if (ut->ut_type != USER_PROCESS)
226                                 continue;
227 #endif
228                         if (ut->ut_user[0] == '\0')
229                                 continue;
230                         if (check_login(ut))
231                                 continue;
232
233                         /*
234                          * Put the rest of this in a child process.  This
235                          * keeps the scan from waiting on other ports to die.
236                          */
237
238                         pid = fork();
239                         if (pid > 0) {
240                                 /* parent */
241                                 continue;
242                         } else if (pid < 0) {
243                                 /* failed - give up until the next scan */
244                                 break;
245                         }
246                         /* child */
247
248                         if (strncmp(ut->ut_line, "/dev/", 5) != 0)
249                                 strcpy(tty_name, "/dev/");
250                         else
251                                 tty_name[0] = '\0';
252
253                         strcat(tty_name, ut->ut_line);
254 #ifndef O_NOCTTY
255 #define O_NOCTTY 0
256 #endif
257                         tty_fd = open(tty_name, O_WRONLY|O_NDELAY|O_NOCTTY);
258                         if (tty_fd != -1) {
259                                 send_mesg_to_tty(tty_fd);
260                                 close(tty_fd);
261                                 sleep(10);
262                         }
263 #ifdef USER_PROCESS  /* USG_UTMP */
264                         if (ut->ut_pid > 1) {
265                                 kill(- ut->ut_pid, SIGHUP);
266                                 sleep(10);
267                                 kill(- ut->ut_pid, SIGKILL);
268                         }
269 #else  /* BSD || SUN || SUN4 */
270                         /*
271                          * vhangup() the line to kill try and kill
272                          * whatever is out there using it.
273                          */
274
275                         if ((tty_fd = open (tty_name, O_RDONLY|O_NDELAY)) == -1)
276                                 continue;
277
278                         vhangup (tty_fd);
279                         close (tty_fd);
280 #endif  /* BSD || SUN || SUN4 */
281
282                         strncpy(user, ut->ut_line, sizeof(user) - 1);
283                         user[sizeof(user) - 1] = '\0';
284
285                         SYSLOG((LOG_NOTICE, "logged off user `%s' on `%s'\n",
286                                 user, tty_name));
287
288                         /*
289                          * This child has done all it can, drop dead.
290                          */
291
292                         exit (0);
293                 }
294
295                 endutent();
296
297 #ifndef DEBUG
298                 sleep(60);
299 #endif
300                 /*
301                  * Reap any dead babies ...
302                  */
303
304                 while (wait (&status) != -1)
305                         ;
306         }
307         return 1;  /* not reached */
308 }