]> granicus.if.org Git - shadow/blob - src/logoutd.c
[svn-upgrade] Integrating new upstream version, shadow (19990709)
[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.13 1999/06/07 16:40:45 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 static char     *mesg_buf = "login time exceeded\r\n";  /* XXX warning: const */
55 static int      mesg_len = 21;
56 static int      mesg_size;
57
58 #ifndef HUP_MESG_FILE
59 #define HUP_MESG_FILE "/etc/logoutd.mesg"
60 #endif
61
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 **));
66
67 /*
68  * reload_mesg - reload the message that is output when killing a process
69  */
70
71 static RETSIGTYPE
72 reload_mesg(int sig)
73 {
74         int     fd;
75         struct  stat    sb;
76
77         signal (sig, reload_mesg);
78
79         if (stat (HUP_MESG_FILE, &sb))
80                 return;
81
82         if ((sb.st_mode & S_IFMT) != S_IFREG)
83                 return;
84
85         if ((fd = open (HUP_MESG_FILE, O_RDONLY)) != -1) {
86                 if (sb.st_size + 1 > mesg_size) {
87                         if (mesg_buf && mesg_size)
88                                 free (mesg_buf);
89
90                         mesg_len = sb.st_size;
91                         mesg_size = mesg_len + 1;
92                         if (! (mesg_buf = (char *) malloc (mesg_len + 1)))
93                                 goto end;
94                 } else
95                         mesg_len = sb.st_size;
96
97                 if (read (fd, mesg_buf, mesg_len) != mesg_len) {
98                         mesg_len = 0;
99                         goto end;
100                 }
101         } else
102                 return;
103
104 end:
105         close (fd);
106 }
107
108 /*
109  * check_login - check if user (struct utmp) allowed to stay logged in
110  */
111 static int
112 check_login(const struct utmp *ut)
113 {
114         char user[sizeof(ut->ut_user) + 1];
115         time_t now;
116
117         /*
118          * ut_user may not have the terminating NUL.
119          */
120         strncpy(user, ut->ut_user, sizeof(ut->ut_user));
121         user[sizeof(ut->ut_user)] = '\0';
122
123         time(&now);
124
125         /*
126          * Check if they are allowed to be logged in right now.
127          */
128         if (!isttytime(user, ut->ut_line, now))
129                 return 0;
130 #if 0
131         /*
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).
136          */
137         if (now - ut->ut_time > get_time_limit(user, ut->ut_line, ut->ut_time))
138                 return 0;
139 #endif
140         return 1;
141 }
142
143 /*
144  * logoutd - logout daemon to enforce /etc/porttime file policy
145  *
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.
150  */
151
152 int
153 main(int argc, char **argv)
154 {
155         int i;
156         int status;
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         reload_mesg (SIGHUP);
181
182         /*
183          * Put this process in the background.
184          */
185
186         if ((i = fork ()))
187                 exit (i < 0 ? 1:0);
188 #endif /* !DEBUG */
189
190         /*
191          * Start syslogging everything
192          */
193
194         Prog = Basename(argv[0]);
195
196         openlog(Prog, LOG_PID|LOG_CONS|LOG_NOWAIT, LOG_AUTH);
197
198         /*
199          * Scan the UTMP file once per minute looking for users that
200          * are not supposed to still be logged in.
201          */
202
203         while (1) {
204 #ifndef DEBUG
205                 sleep(60);
206 #endif
207
208                 /* 
209                  * Attempt to re-open the utmp file.  The file is only
210                  * open while it is being used.
211                  */
212
213                 setutent();
214
215                 /*
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.
219                  */
220
221                 while ((ut = getutent())) {
222 #ifdef USER_PROCESS
223                         if (ut->ut_type != USER_PROCESS)
224                                 continue;
225 #endif
226                         if (ut->ut_user[0] == '\0')
227                                 continue;
228                         if (check_login(ut))
229                                 continue;
230
231                         /*
232                          * Put the rest of this in a child process.  This
233                          * keeps the scan from waiting on other ports to die.
234                          */
235
236                         if (fork() != 0)
237                                 continue;
238
239                         if (strncmp(ut->ut_line, "/dev/", 5) != 0)
240                                 strcpy(tty_name, "/dev/");
241                         else
242                                 tty_name[0] = '\0';
243
244                         strcat(tty_name, ut->ut_line);
245 #ifndef O_NOCTTY
246 #define O_NOCTTY 0
247 #endif
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 */
252                                 TERMIO oldt, newt;
253
254                                 GTTY(tty_fd, &oldt);
255                                 newt = oldt;
256 #ifdef OPOST
257                                 newt.c_oflag |= OPOST;
258 #else  /* XXX - I'm too young to know bsd sgtty, sorry :).  --marekm */
259 #endif
260                                 STTY(tty_fd, &newt);
261                                 write (tty_fd, mesg_buf, mesg_len);
262                                 STTY(tty_fd, &oldt);
263                                 close (tty_fd);
264                                 sleep(10);
265                         }
266 #ifdef USER_PROCESS  /* USG_UTMP */
267                         if (ut->ut_pid > 1) {
268                                 kill(- ut->ut_pid, SIGHUP);
269                                 sleep(10);
270                                 kill(- ut->ut_pid, SIGKILL);
271                         }
272 #else  /* BSD || SUN || SUN4 */
273                         /*
274                          * vhangup() the line to kill try and kill
275                          * whatever is out there using it.
276                          */
277
278                         if ((tty_fd = open (tty_name, O_RDONLY|O_NDELAY)) == -1)
279                                 continue;
280
281                         vhangup (tty_fd);
282                         close (tty_fd);
283 #endif  /* BSD || SUN || SUN4 */
284
285 #if 0
286                         SYSLOG((LOG_NOTICE,
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));
290 #else
291                         /* avoid gcc warnings about %.*s in syslog() */
292                         strncpy(user, ut->ut_line, sizeof(user) - 1);
293                         user[sizeof(user) - 1] = '\0';
294
295                         SYSLOG((LOG_NOTICE, "logged off user `%s' on `%s'\n",
296                                 user, tty_name));
297 #endif
298
299                         /*
300                          * This child has done all it can, drop dead.
301                          */
302
303                         exit (0);
304                 }
305
306                 endutent();
307
308                 /*
309                  * Reap any dead babies ...
310                  */
311
312                 while (wait (&status) != -1)
313                         ;
314         }
315         return 1;  /* not reached */
316 }