]> andersk Git - openssh.git/blobdiff - logintest.c
- dtucker@cvs.openbsd.org 2009/12/06 23:41:15
[openssh.git] / logintest.c
index 1c4df6d5377473ed3b09fa2563454f51abe98aa3..7e9fbbfbbdc1e5a6cac726af7de233410e9438d8 100644 (file)
@@ -9,11 +9,6 @@
  * 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.
  * 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. All advertising materials mentioning features or use of this software
- *    must display the following acknowledgement:
- *      This product includes software developed by Markus Friedl.
- * 4. The name of the author may not be used to endorse or promote products
- *    derived from this software without specific prior written permission.
  *
  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  *
  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-/** 
+/**
  ** logintest.c:  simple test driver for platform-independent login recording
  **               and lastlog retrieval
  **/
 
  ** logintest.c:  simple test driver for platform-independent login recording
  **               and lastlog retrieval
  **/
 
-#include "config.h"
+#include "includes.h"
 
 #include <sys/types.h>
 #include <sys/wait.h>
 
 #include <sys/types.h>
 #include <sys/wait.h>
+#include <sys/socket.h>
+
+#include <netinet/in.h>
+
 #include <unistd.h>
 #include <stdlib.h>
 #include <stdio.h>
 #include <unistd.h>
 #include <stdlib.h>
 #include <stdio.h>
@@ -48,8 +47,9 @@
 
 #include "loginrec.h"
 
 
 #include "loginrec.h"
 
-RCSID("$Id$");
+extern char *__progname;
 
 
+#define PAUSE_BEFORE_LOGOUT 3
 
 int nologtest = 0;
 int compile_opts_only = 0;
 
 int nologtest = 0;
 int compile_opts_only = 0;
@@ -70,13 +70,12 @@ dump_logininfo(struct logininfo *li, char *descname)
               "struct login_netinfo hostaddr {\n\t\t"
               "struct sockaddr sa {\n"
               "\t\t\tfamily\t%d\n\t\t}\n"
               "struct login_netinfo hostaddr {\n\t\t"
               "struct sockaddr sa {\n"
               "\t\t\tfamily\t%d\n\t\t}\n"
-              "\t\t** !!! IP6 stuff not supported yet **\n"
               "\t}\n"
               "}\n",
               "\t}\n"
               "}\n",
-              descname, li->progname, li->type, 
+              descname, li->progname, li->type,
               li->pid, li->uid, li->line,
               li->pid, li->uid, li->line,
-              li->username, li->hostname, li->exit, 
-              li->termination, li->tv_sec, li->tv_usec, 
+              li->username, li->hostname, li->exit,
+              li->termination, li->tv_sec, li->tv_usec,
               li->hostaddr.sa.sa_family);
 }
 
               li->hostaddr.sa.sa_family);
 }
 
@@ -91,8 +90,9 @@ testAPI()
        char cmdstring[256], stripline[8];
        char username[32];
 #ifdef HAVE_TIME_H
        char cmdstring[256], stripline[8];
        char username[32];
 #ifdef HAVE_TIME_H
-       time_t t0, t1, t2, logouttime;
-       char s_t0[64],s_t1[64],s_t2[64], s_logouttime[64]; /* ctime() strings */
+       time_t t0, t1, t2, logintime, logouttime;
+       char s_t0[64],s_t1[64],s_t2[64];
+       char s_logintime[64], s_logouttime[64]; /* ctime() strings */
 #endif
 
        printf("**\n** Testing the API...\n**\n");
 #endif
 
        printf("**\n** Testing the API...\n**\n");
@@ -103,7 +103,8 @@ testAPI()
        /* gethostname(hostname, sizeof(hostname)); */
 
        printf("login_alloc_entry test (no host info):\n");
        /* gethostname(hostname, sizeof(hostname)); */
 
        printf("login_alloc_entry test (no host info):\n");
-       /* !!! fake tty more effectively */
+
+       /* FIXME fake tty more effectively - this could upset some platforms */
        li1 = login_alloc_entry((int)getpid(), username, NULL, ttyname(0));
        strlcpy(li1->progname, "OpenSSH-logintest", sizeof(li1->progname));
 
        li1 = login_alloc_entry((int)getpid(), username, NULL, ttyname(0));
        strlcpy(li1->progname, "OpenSSH-logintest", sizeof(li1->progname));
 
@@ -126,17 +127,17 @@ testAPI()
 
        if ((int)geteuid() != 0) {
                printf("NOT RUNNING LOGIN TESTS - you are not root!\n");
 
        if ((int)geteuid() != 0) {
                printf("NOT RUNNING LOGIN TESTS - you are not root!\n");
-               return 1; /* this isn't necessarily an error */
+               return 1;
        }
 
        if (nologtest)
                return 1;
        }
 
        if (nologtest)
                return 1;
-  
+
        line_stripname(stripline, li1->line, sizeof(stripline));
 
        printf("Performing an invalid login attempt (no type field)\n--\n");
        login_write(li1);
        line_stripname(stripline, li1->line, sizeof(stripline));
 
        printf("Performing an invalid login attempt (no type field)\n--\n");
        login_write(li1);
-       printf("--\n(Should have written an error to stderr)\n");
+       printf("--\n(Should have written errors to stderr)\n");
 
 #ifdef HAVE_TIME_H
        (void)time(&t0);
 
 #ifdef HAVE_TIME_H
        (void)time(&t0);
@@ -148,15 +149,21 @@ testAPI()
               (int)t0, s_t0, (int)t1, s_t1);
 #endif
 
               (int)t0, s_t0, (int)t1, s_t1);
 #endif
 
-       printf("Performing a login on line %s...\n--\n", stripline);
+       printf("Performing a login on line %s ", stripline);
+#ifdef HAVE_TIME_H
+       (void)time(&logintime);
+       strlcpy(s_logintime, ctime(&logintime), sizeof(s_logintime));
+       printf("at %d - %s", (int)logintime, s_logintime);
+#endif
+       printf("--\n");
        login_login(li1);
        login_login(li1);
-  
+
        snprintf(cmdstring, sizeof(cmdstring), "who | grep '%s '",
                 stripline);
        system(cmdstring);
        snprintf(cmdstring, sizeof(cmdstring), "who | grep '%s '",
                 stripline);
        system(cmdstring);
-  
-       printf("--\nWaiting for a few seconds...\n");
-       sleep(2);
+
+       printf("--\nPausing for %d second(s)...\n", PAUSE_BEFORE_LOGOUT);
+       sleep(PAUSE_BEFORE_LOGOUT);
 
        printf("Performing a logout ");
 #ifdef HAVE_TIME_H
 
        printf("Performing a logout ");
 #ifdef HAVE_TIME_H
@@ -164,7 +171,7 @@ testAPI()
        strlcpy(s_logouttime, ctime(&logouttime), sizeof(s_logouttime));
        printf("at %d - %s", (int)logouttime, s_logouttime);
 #endif
        strlcpy(s_logouttime, ctime(&logouttime), sizeof(s_logouttime));
        printf("at %d - %s", (int)logouttime, s_logouttime);
 #endif
-       printf("(the root login shown above should be gone)\n"
+       printf("\nThe root login shown above should be gone.\n"
               "If the root login hasn't gone, but another user on the same\n"
               "pty has, this is OK - we're hacking it here, and there\n"
               "shouldn't be two users on one pty in reality...\n"
               "If the root login hasn't gone, but another user on the same\n"
               "pty has, this is OK - we're hacking it here, and there\n"
               "shouldn't be two users on one pty in reality...\n"
@@ -183,21 +190,25 @@ testAPI()
                       "same.\nThis indicates that lastlog is ** NOT WORKING "
                       "CORRECTLY **\n");
        else if (t0 != t2)
                       "same.\nThis indicates that lastlog is ** NOT WORKING "
                       "CORRECTLY **\n");
        else if (t0 != t2)
+               /* We can be off by a second or so, even when recording works fine.
+                * I'm not 100% sure why, but it's true. */
                printf("** The login time and the lastlog time differ.\n"
                       "** This indicates that lastlog is either recording the "
                printf("** The login time and the lastlog time differ.\n"
                       "** This indicates that lastlog is either recording the "
-                      "wrong time,\n** or retrieving the wrong entry.\n");
+                      "wrong time,\n** or retrieving the wrong entry.\n"
+                      "If it's off by less than %d second(s) "
+                      "run the test again.\n", PAUSE_BEFORE_LOGOUT);
        else
                printf("lastlog agrees with the login time. This is a good thing.\n");
 
 #endif
 
        printf("--\nThe output of 'last' shown next should have "
        else
                printf("lastlog agrees with the login time. This is a good thing.\n");
 
 #endif
 
        printf("--\nThe output of 'last' shown next should have "
-              "an entry for root \n  on %s for the time shown above:\n--\n", 
+              "an entry for root \n  on %s for the time shown above:\n--\n",
               stripline);
        snprintf(cmdstring, sizeof(cmdstring), "last | grep '%s ' | head -3",
                 stripline);
        system(cmdstring);
               stripline);
        snprintf(cmdstring, sizeof(cmdstring), "last | grep '%s ' | head -3",
                 stripline);
        system(cmdstring);
-            
+
        printf("--\nEnd of login test.\n");
 
        login_free_entry(li1);
        printf("--\nEnd of login test.\n");
 
        login_free_entry(li1);
@@ -242,9 +253,9 @@ testOutput()
 /* show which options got compiled in */
 void
 showOptions(void)
 /* show which options got compiled in */
 void
 showOptions(void)
-{  
+{
        printf("**\n** Compile-time options\n**\n");
        printf("**\n** Compile-time options\n**\n");
-  
+
        printf("login recording methods selected:\n");
 #ifdef USE_LOGIN
        printf("\tUSE_LOGIN\n");
        printf("login recording methods selected:\n");
 #ifdef USE_LOGIN
        printf("\tUSE_LOGIN\n");
@@ -274,23 +285,24 @@ main(int argc, char *argv[])
 {
        printf("Platform-independent login recording test driver\n");
 
 {
        printf("Platform-independent login recording test driver\n");
 
+       __progname = ssh_get_progname(argv[0]);
        if (argc == 2) {
                if (strncmp(argv[1], "-i", 3) == 0)
                        compile_opts_only = 1;
                else if (strncmp(argv[1], "-v", 3) == 0)
                        be_verbose=1;
        }
        if (argc == 2) {
                if (strncmp(argv[1], "-i", 3) == 0)
                        compile_opts_only = 1;
                else if (strncmp(argv[1], "-v", 3) == 0)
                        be_verbose=1;
        }
-      
+
        if (!compile_opts_only) {
                if (be_verbose && !testOutput())
                        return 1;
        if (!compile_opts_only) {
                if (be_verbose && !testOutput())
                        return 1;
-    
+
                if (!testAPI())
                        return 1;
        }
 
        showOptions();
                if (!testAPI())
                        return 1;
        }
 
        showOptions();
-  
+
        return 0;
 } /* main() */
 
        return 0;
 } /* main() */
 
This page took 0.047941 seconds and 4 git commands to generate.