]> granicus.if.org Git - shadow/blobdiff - src/logoutd.c
* src/grpconv.c: Fail if not called correctly.
[shadow] / src / logoutd.c
index 62d15322974fd86e61ca5fc26e507d1cc6993cee..1503a743ac61f49f8e16840256c126227986b40f 100644 (file)
@@ -1,5 +1,7 @@
 /*
- * Copyright 1991 - 1993, Julianne Frances Haugh
+ * Copyright (c) 1991 - 1993, Julianne Frances Haugh
+ * Copyright (c) 1996 - 2000, Marek Michałkiewicz
+ * Copyright (c) 2001 - 2006, Tomasz Kłoczko
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * 2. Redistributions in binary form must reproduce the above copyright
  *    notice, this list of conditions and the following disclaimer in the
  *    documentation and/or other materials provided with the distribution.
- * 3. Neither the name of Julianne F. Haugh nor the names of its contributors
- *    may be used to endorse or promote products derived from this software
- *    without specific prior written permission.
+ * 3. The name of the copyright holders or contributors may not be used to
+ *    endorse or promote products derived from this software without
+ *    specific prior written permission.
  *
- * THIS SOFTWARE IS PROVIDED BY JULIE HAUGH AND CONTRIBUTORS ``AS IS'' AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL JULIE HAUGH OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
+ * PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT
+ * HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
 #include <config.h>
 
-#include "rcsid.h"
-RCSID (PKG_VER "$Id: logoutd.c,v 1.24 2004/10/11 06:26:40 kloczek Exp $")
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <stdio.h>
-#include <signal.h>
+#ident "$Id$"
+
 #include <fcntl.h>
-#include "prototypes.h"
+#include <signal.h>
+#include <stdio.h>
+#include <sys/stat.h>
+#include <sys/types.h>
 #include "defines.h"
-static char *Prog;
+#include "prototypes.h"
+/*
+ * Global variables
+ */
+const char *Prog;
 
 #ifndef DEFAULT_HUP_MESG
-#define DEFAULT_HUP_MESG "login time exceeded\r\n"
+#define DEFAULT_HUP_MESG _("login time exceeded\n\n")
 #endif
 
 #ifndef HUP_MESG_FILE
 #define HUP_MESG_FILE "/etc/logoutd.mesg"
 #endif
 
-#if HAVE_UTMPX_H
-static int check_login (const struct utmpx *);
-#else
-static int check_login (const struct utmp *);
-#endif
+/* local function prototypes */
+#ifdef USE_UTMPX
+static int check_login (const struct utmpx *ut);
+#else                          /* !USE_UTMPX */
+static int check_login (const struct utmp *ut);
+#endif                         /* !USE_UTMPX */
+static void send_mesg_to_tty (int tty_fd);
 
 /*
  * check_login - check if user (struct utmpx/utmp) allowed to stay logged in
  */
-#if HAVE_UTMPX_H
+#ifdef USE_UTMPX
 static int check_login (const struct utmpx *ut)
-#else
+#else                          /* !USE_UTMPX */
 static int check_login (const struct utmp *ut)
-#endif
+#endif                         /* !USE_UTMPX */
 {
        char user[sizeof (ut->ut_user) + 1];
        time_t now;
@@ -72,13 +79,14 @@ static int check_login (const struct utmp *ut)
        strncpy (user, ut->ut_user, sizeof (ut->ut_user));
        user[sizeof (ut->ut_user)] = '\0';
 
-       time (&now);
+       (void) time (&now);
 
        /*
         * Check if they are allowed to be logged in right now.
         */
-       if (!isttytime (user, ut->ut_line, now))
+       if (!isttytime (user, ut->ut_line, now)) {
                return 0;
+       }
        return 1;
 }
 
@@ -87,11 +95,12 @@ static void send_mesg_to_tty (int tty_fd)
 {
        TERMIO oldt, newt;
        FILE *mesg_file, *tty_file;
-       int c, is_tty;
+       bool is_tty;
 
        tty_file = fdopen (tty_fd, "w");
-       if (!tty_file)
+       if (NULL == tty_file) {
                return;
+       }
 
        is_tty = (GTTY (tty_fd, &oldt) == 0);
        if (is_tty) {
@@ -103,10 +112,12 @@ static void send_mesg_to_tty (int tty_fd)
        }
 
        mesg_file = fopen (HUP_MESG_FILE, "r");
-       if (mesg_file) {
+       if (NULL != mesg_file) {
+               int c;
                while ((c = getc (mesg_file)) != EOF) {
-                       if (c == '\n')
+                       if (c == '\n') {
                                putc ('\r', tty_file);
+                       }
                        putc (c, tty_file);
                }
                fclose (mesg_file);
@@ -130,58 +141,51 @@ static void send_mesg_to_tty (int tty_fd)
  *     utmpx/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;
 
-#if HAVE_UTMPX_H
+#ifdef USE_UTMPX
        struct utmpx *ut;
-#else
+#else                          /* !USE_UTMPX */
        struct utmp *ut;
-#endif
+#endif                         /* !USE_UTMPX */
        char user[sizeof (ut->ut_user) + 1];    /* terminating NUL */
        char tty_name[sizeof (ut->ut_line) + 6];        /* /dev/ + NUL */
        int tty_fd;
 
-       setlocale (LC_ALL, "");
-       bindtextdomain (PACKAGE, LOCALEDIR);
-       textdomain (PACKAGE);
+       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++);
 
-#ifdef HAVE_SETPGRP
-#ifdef SETPGRP_VOID
-       setpgrp ();             /* USG */
-#else
-       setpgrp (getpid (), getpid ());
-#endif
-#else                          /* !HAVE_SETPGRP */
-       setpgid (getpid (), getpid ()); /* BSD || SUN || SUN4 */
-#endif                         /* !HAVE_SETPGRP */
+       setpgrp ();
 
        /*
         * Put this process in the background.
         */
-
        pid = fork ();
        if (pid > 0) {
                /* parent */
-               exit (0);
+               exit (EXIT_SUCCESS);
        } else if (pid < 0) {
                /* error */
                perror ("fork");
-               exit (1);
+               exit (EXIT_FAILURE);
        }
 #endif                         /* !DEBUG */
 
        /*
         * Start syslogging everything
         */
-
        Prog = Basename (argv[0]);
 
        OPENLOG ("logoutd");
@@ -190,39 +194,38 @@ int main (int argc, char **argv)
         * Scan the utmpx/utmp file once per minute looking for users that
         * are not supposed to still be logged in.
         */
-
-       while (1) {
+       while (true) {
 
                /* 
                 * Attempt to re-open the utmpx/utmp file. The file is only
                 * open while it is being used.
                 */
-
-#if HAVE_UTMPX_H
+#ifdef USE_UTMPX
                setutxent ();
-#else
+#else                          /* !USE_UTMPX */
                setutent ();
-#endif
+#endif                         /* !USE_UTMPX */
 
                /*
                 * Read all of the entries in the utmpx/utmp file. The entries
                 * for login sessions will be checked to see if the user
                 * is permitted to be signed on at this time.
                 */
-
-#if HAVE_UTMPX_H
-               while ((ut = getutxent ())) {
-#else
-               while ((ut = getutent ())) {
-#endif
-#ifdef USER_PROCESS
-                       if (ut->ut_type != USER_PROCESS)
+#ifdef USE_UTMPX
+               while ((ut = getutxent ()) != NULL)
+#else                          /* !USE_UTMPX */
+               while ((ut = getutent ()) != NULL)
+#endif                         /* !USE_UTMPX */
+               {
+                       if (ut->ut_type != USER_PROCESS) {
                                continue;
-#endif
-                       if (ut->ut_user[0] == '\0')
+                       }
+                       if (ut->ut_user[0] == '\0') {
                                continue;
-                       if (check_login (ut))
+                       }
+                       if (check_login (ut)) {
                                continue;
+                       }
 
                        /*
                         * Put the rest of this in a child process. This
@@ -239,62 +242,48 @@ int main (int argc, char **argv)
                        }
                        /* child */
 
-                       if (strncmp (ut->ut_line, "/dev/", 5) != 0)
+                       if (strncmp (ut->ut_line, "/dev/", 5) != 0) {
                                strcpy (tty_name, "/dev/");
-                       else
+                       } else {
                                tty_name[0] = '\0';
+                       }
 
                        strcat (tty_name, ut->ut_line);
 #ifndef O_NOCTTY
 #define O_NOCTTY 0
 #endif
                        tty_fd =
-                           open (tty_name,
-                                 O_WRONLY | O_NDELAY | O_NOCTTY);
+                           open (tty_name, O_WRONLY | O_NDELAY | O_NOCTTY);
                        if (tty_fd != -1) {
                                send_mesg_to_tty (tty_fd);
                                close (tty_fd);
                                sleep (10);
                        }
-#ifdef USER_PROCESS            /* USG_UTMP */
+
                        if (ut->ut_pid > 1) {
                                kill (-ut->ut_pid, SIGHUP);
                                sleep (10);
                                kill (-ut->ut_pid, SIGKILL);
                        }
-#else                          /* BSD || SUN || SUN4 */
-                       /*
-                        * vhangup() the line to kill try and kill
-                        * whatever is out there using it.
-                        */
-
-                       if ((tty_fd =
-                            open (tty_name, O_RDONLY | O_NDELAY)) == -1)
-                               continue;
-
-                       vhangup (tty_fd);
-                       close (tty_fd);
-#endif                         /* BSD || SUN || SUN4 */
 
                        strncpy (user, ut->ut_user, sizeof (user) - 1);
                        user[sizeof (user) - 1] = '\0';
 
                        SYSLOG ((LOG_NOTICE,
-                                "logged off user `%s' on `%s'", user,
+                                "logged off user '%s' on '%s'", user,
                                 tty_name));
 
                        /*
                         * This child has done all it can, drop dead.
                         */
-
-                       exit (0);
+                       exit (EXIT_SUCCESS);
                }
 
-#if HAVE_UTMPX_H
+#ifdef USE_UTMPX
                endutxent ();
-#else
+#else                          /* !USE_UTMPX */
                endutent ();
-#endif
+#endif                         /* !USE_UTMPX */
 
 #ifndef DEBUG
                sleep (60);
@@ -302,9 +291,9 @@ int main (int argc, char **argv)
                /*
                 * Reap any dead babies ...
                 */
-
                while (wait (&status) != -1);
        }
-       return 1;
-       /* NOT REACHED */
+
+       return EXIT_FAILURE;
 }
+