xref: /openssh-portable/logintest.c (revision 57ed647e)
12ff7b5d0Sandre /*
22ff7b5d0Sandre  * Copyright (c) 2000 Andre Lucas.  All rights reserved.
32ff7b5d0Sandre  *
42ff7b5d0Sandre  * Redistribution and use in source and binary forms, with or without
52ff7b5d0Sandre  * modification, are permitted provided that the following conditions
62ff7b5d0Sandre  * are met:
72ff7b5d0Sandre  * 1. Redistributions of source code must retain the above copyright
82ff7b5d0Sandre  *    notice, this list of conditions and the following disclaimer.
92ff7b5d0Sandre  * 2. Redistributions in binary form must reproduce the above copyright
102ff7b5d0Sandre  *    notice, this list of conditions and the following disclaimer in the
112ff7b5d0Sandre  *    documentation and/or other materials provided with the distribution.
122ff7b5d0Sandre  *
132ff7b5d0Sandre  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
142ff7b5d0Sandre  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
152ff7b5d0Sandre  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
162ff7b5d0Sandre  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
172ff7b5d0Sandre  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
182ff7b5d0Sandre  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
192ff7b5d0Sandre  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
202ff7b5d0Sandre  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
212ff7b5d0Sandre  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
222ff7b5d0Sandre  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
232ff7b5d0Sandre  */
242ff7b5d0Sandre 
252ff7b5d0Sandre /**
262ff7b5d0Sandre  ** logintest.c:  simple test driver for platform-independent login recording
272ff7b5d0Sandre  **               and lastlog retrieval
282ff7b5d0Sandre  **/
292ff7b5d0Sandre 
30b4db42f7Sandre #include "includes.h"
312ff7b5d0Sandre 
322ff7b5d0Sandre #include <sys/types.h>
332ff7b5d0Sandre #include <sys/wait.h>
348ec8c3e9SDamien Miller #include <sys/socket.h>
358ec8c3e9SDamien Miller 
368ec8c3e9SDamien Miller #include <netinet/in.h>
378ec8c3e9SDamien Miller 
382ff7b5d0Sandre #include <unistd.h>
392ff7b5d0Sandre #include <stdlib.h>
402ff7b5d0Sandre #include <stdio.h>
412ff7b5d0Sandre #include <string.h>
422ff7b5d0Sandre #include <pwd.h>
432ff7b5d0Sandre #include <netdb.h>
442ff7b5d0Sandre #ifdef HAVE_TIME_H
452ff7b5d0Sandre #include <time.h>
462ff7b5d0Sandre #endif
472ff7b5d0Sandre 
482ff7b5d0Sandre #include "loginrec.h"
492ff7b5d0Sandre 
50ff8b4959SKevin Steves extern char *__progname;
512ff7b5d0Sandre 
52b4db42f7Sandre #define PAUSE_BEFORE_LOGOUT 3
53b4db42f7Sandre 
542ff7b5d0Sandre int nologtest = 0;
552ff7b5d0Sandre int compile_opts_only = 0;
562ff7b5d0Sandre int be_verbose = 0;
572ff7b5d0Sandre 
582ff7b5d0Sandre 
592ff7b5d0Sandre /* Dump a logininfo to stdout. Assumes a tab size of 8 chars. */
606e5d3475Sandre void
dump_logininfo(struct logininfo * li,char * descname)616e5d3475Sandre dump_logininfo(struct logininfo *li, char *descname)
6261e67250Sandre {
632ff7b5d0Sandre 	/* yes I know how nasty this is */
642ff7b5d0Sandre 	printf("struct logininfo %s = {\n\t"
652ff7b5d0Sandre 	    "progname\t'%s'\n\ttype\t\t%d\n\t"
662ff7b5d0Sandre 	    "pid\t\t%d\n\tuid\t\t%d\n\t"
672ff7b5d0Sandre 	    "line\t\t'%s'\n\tusername\t'%s'\n\t"
682ff7b5d0Sandre 	    "hostname\t'%s'\n\texit\t\t%d\n\ttermination\t%d\n\t"
692ff7b5d0Sandre 	    "tv_sec\t%d\n\ttv_usec\t%d\n\t"
702ff7b5d0Sandre 	    "struct login_netinfo hostaddr {\n\t\t"
7161e67250Sandre 	    "struct sockaddr sa {\n"
7261e67250Sandre 	    "\t\t\tfamily\t%d\n\t\t}\n"
732ff7b5d0Sandre 	    "\t}\n"
742ff7b5d0Sandre 	    "}\n",
752ff7b5d0Sandre 	    descname, li->progname, li->type,
762ff7b5d0Sandre 	    li->pid, li->uid, li->line,
772ff7b5d0Sandre 	    li->username, li->hostname, li->exit,
782ff7b5d0Sandre 	    li->termination, li->tv_sec, li->tv_usec,
7961e67250Sandre 	    li->hostaddr.sa.sa_family);
802ff7b5d0Sandre }
812ff7b5d0Sandre 
822ff7b5d0Sandre 
836e5d3475Sandre int
testAPI()846e5d3475Sandre testAPI()
8561e67250Sandre {
862ff7b5d0Sandre 	struct logininfo *li1;
872ff7b5d0Sandre 	struct passwd *pw;
882ff7b5d0Sandre 	struct hostent *he;
892ff7b5d0Sandre 	struct sockaddr_in sa_in4;
902ff7b5d0Sandre 	char cmdstring[256], stripline[8];
912ff7b5d0Sandre 	char username[32];
922ff7b5d0Sandre #ifdef HAVE_TIME_H
93b4db42f7Sandre 	time_t t0, t1, t2, logintime, logouttime;
94b4db42f7Sandre 	char s_t0[64],s_t1[64],s_t2[64];
95b4db42f7Sandre 	char s_logintime[64], s_logouttime[64]; /* ctime() strings */
962ff7b5d0Sandre #endif
972ff7b5d0Sandre 
982ff7b5d0Sandre 	printf("**\n** Testing the API...\n**\n");
992ff7b5d0Sandre 
1002ff7b5d0Sandre 	pw = getpwuid(getuid());
1012ff7b5d0Sandre 	strlcpy(username, pw->pw_name, sizeof(username));
1022ff7b5d0Sandre 
1032ff7b5d0Sandre 	/* gethostname(hostname, sizeof(hostname)); */
1042ff7b5d0Sandre 
1052ff7b5d0Sandre 	printf("login_alloc_entry test (no host info):\n");
106b4db42f7Sandre 
107b4db42f7Sandre 	/* FIXME fake tty more effectively - this could upset some platforms */
1082ff7b5d0Sandre 	li1 = login_alloc_entry((int)getpid(), username, NULL, ttyname(0));
10961e67250Sandre 	strlcpy(li1->progname, "OpenSSH-logintest", sizeof(li1->progname));
1102ff7b5d0Sandre 
1112ff7b5d0Sandre 	if (be_verbose)
1122ff7b5d0Sandre 		dump_logininfo(li1, "li1");
1132ff7b5d0Sandre 
11461e67250Sandre 	printf("Setting host address info for 'localhost' (may call out):\n");
1152ff7b5d0Sandre 	if (! (he = gethostbyname("localhost"))) {
1162ff7b5d0Sandre 		printf("Couldn't set hostname(lookup failed)\n");
1172ff7b5d0Sandre 	} else {
1182ff7b5d0Sandre 		/* NOTE: this is messy, but typically a program wouldn't have to set
1192ff7b5d0Sandre 		 *  any of this, a sockaddr_in* would be already prepared */
1202ff7b5d0Sandre 		memcpy((void *)&(sa_in4.sin_addr), (void *)&(he->h_addr_list[0][0]),
1212ff7b5d0Sandre 		    sizeof(struct in_addr));
12261e67250Sandre 		login_set_addr(li1, (struct sockaddr *) &sa_in4, sizeof(sa_in4));
12361e67250Sandre 		strlcpy(li1->hostname, "localhost", sizeof(li1->hostname));
1242ff7b5d0Sandre 	}
1252ff7b5d0Sandre 	if (be_verbose)
1262ff7b5d0Sandre 		dump_logininfo(li1, "li1");
1272ff7b5d0Sandre 
1282ff7b5d0Sandre 	if ((int)geteuid() != 0) {
1292ff7b5d0Sandre 		printf("NOT RUNNING LOGIN TESTS - you are not root!\n");
130b4db42f7Sandre 		return 1;
1312ff7b5d0Sandre 	}
1322ff7b5d0Sandre 
1332ff7b5d0Sandre 	if (nologtest)
1342ff7b5d0Sandre 		return 1;
1352ff7b5d0Sandre 
1362ff7b5d0Sandre 	line_stripname(stripline, li1->line, sizeof(stripline));
1372ff7b5d0Sandre 
1382ff7b5d0Sandre 	printf("Performing an invalid login attempt (no type field)\n--\n");
1392ff7b5d0Sandre 	login_write(li1);
140b4db42f7Sandre 	printf("--\n(Should have written errors to stderr)\n");
1412ff7b5d0Sandre 
1422ff7b5d0Sandre #ifdef HAVE_TIME_H
1432ff7b5d0Sandre 	(void)time(&t0);
1442ff7b5d0Sandre 	strlcpy(s_t0, ctime(&t0), sizeof(s_t0));
14561e67250Sandre 	t1 = login_get_lastlog_time(getuid());
1462ff7b5d0Sandre 	strlcpy(s_t1, ctime(&t1), sizeof(s_t1));
1472ff7b5d0Sandre 	printf("Before logging in:\n\tcurrent time is %d - %s\t"
1482ff7b5d0Sandre 	    "lastlog time is %d - %s\n",
1492ff7b5d0Sandre 	    (int)t0, s_t0, (int)t1, s_t1);
1502ff7b5d0Sandre #endif
1512ff7b5d0Sandre 
152b4db42f7Sandre 	printf("Performing a login on line %s ", stripline);
153b4db42f7Sandre #ifdef HAVE_TIME_H
154b4db42f7Sandre 	(void)time(&logintime);
155b4db42f7Sandre 	strlcpy(s_logintime, ctime(&logintime), sizeof(s_logintime));
156b4db42f7Sandre 	printf("at %d - %s", (int)logintime, s_logintime);
157b4db42f7Sandre #endif
158b4db42f7Sandre 	printf("--\n");
1592ff7b5d0Sandre 	login_login(li1);
1602ff7b5d0Sandre 
1612ff7b5d0Sandre 	snprintf(cmdstring, sizeof(cmdstring), "who | grep '%s '",
1622ff7b5d0Sandre 		 stripline);
1632ff7b5d0Sandre 	system(cmdstring);
1642ff7b5d0Sandre 
165b4db42f7Sandre 	printf("--\nPausing for %d second(s)...\n", PAUSE_BEFORE_LOGOUT);
166b4db42f7Sandre 	sleep(PAUSE_BEFORE_LOGOUT);
1672ff7b5d0Sandre 
1686e5d3475Sandre 	printf("Performing a logout ");
1696e5d3475Sandre #ifdef HAVE_TIME_H
1706e5d3475Sandre 	(void)time(&logouttime);
1716e5d3475Sandre 	strlcpy(s_logouttime, ctime(&logouttime), sizeof(s_logouttime));
1726e5d3475Sandre 	printf("at %d - %s", (int)logouttime, s_logouttime);
1736e5d3475Sandre #endif
174b4db42f7Sandre 	printf("\nThe root login shown above should be gone.\n"
1752ff7b5d0Sandre 	    "If the root login hasn't gone, but another user on the same\n"
1762ff7b5d0Sandre 	    "pty has, this is OK - we're hacking it here, and there\n"
1772ff7b5d0Sandre 	    "shouldn't be two users on one pty in reality...\n"
1782ff7b5d0Sandre 	    "-- ('who' output follows)\n");
1792ff7b5d0Sandre 	login_logout(li1);
1802ff7b5d0Sandre 
1812ff7b5d0Sandre 	system(cmdstring);
1822ff7b5d0Sandre 	printf("-- ('who' output ends)\n");
1832ff7b5d0Sandre 
1842ff7b5d0Sandre #ifdef HAVE_TIME_H
18561e67250Sandre 	t2 = login_get_lastlog_time(getuid());
1862ff7b5d0Sandre 	strlcpy(s_t2, ctime(&t2), sizeof(s_t2));
1872ff7b5d0Sandre 	printf("After logging in, lastlog time is %d - %s\n", (int)t2, s_t2);
1882ff7b5d0Sandre 	if (t1 == t2)
1892ff7b5d0Sandre 		printf("The lastlog times before and after logging in are the "
1902ff7b5d0Sandre 		    "same.\nThis indicates that lastlog is ** NOT WORKING "
1912ff7b5d0Sandre 		    "CORRECTLY **\n");
19255661d99Sandre 	else if (t0 != t2)
193b4db42f7Sandre 		/* We can be off by a second or so, even when recording works fine.
194b4db42f7Sandre 		 * I'm not 100% sure why, but it's true. */
1952ff7b5d0Sandre 		printf("** The login time and the lastlog time differ.\n"
1962ff7b5d0Sandre 		    "** This indicates that lastlog is either recording the "
197b4db42f7Sandre 		    "wrong time,\n** or retrieving the wrong entry.\n"
198b4db42f7Sandre 		    "If it's off by less than %d second(s) "
199b4db42f7Sandre 		    "run the test again.\n", PAUSE_BEFORE_LOGOUT);
2002ff7b5d0Sandre 	else
20155661d99Sandre 		printf("lastlog agrees with the login time. This is a good thing.\n");
2022ff7b5d0Sandre 
2032ff7b5d0Sandre #endif
2042ff7b5d0Sandre 
2052ff7b5d0Sandre 	printf("--\nThe output of 'last' shown next should have "
2062ff7b5d0Sandre 	    "an entry for root \n  on %s for the time shown above:\n--\n",
2072ff7b5d0Sandre 	    stripline);
2082ff7b5d0Sandre 	snprintf(cmdstring, sizeof(cmdstring), "last | grep '%s ' | head -3",
2092ff7b5d0Sandre 		 stripline);
2102ff7b5d0Sandre 	system(cmdstring);
2112ff7b5d0Sandre 
2122ff7b5d0Sandre 	printf("--\nEnd of login test.\n");
2132ff7b5d0Sandre 
2142ff7b5d0Sandre 	login_free_entry(li1);
2152ff7b5d0Sandre 
2162ff7b5d0Sandre 	return 1;
2172ff7b5d0Sandre } /* testAPI() */
2182ff7b5d0Sandre 
2192ff7b5d0Sandre 
2206e5d3475Sandre void
testLineName(char * line)2216e5d3475Sandre testLineName(char *line)
22261e67250Sandre {
2232ff7b5d0Sandre 	/* have to null-terminate - these functions are designed for
2242ff7b5d0Sandre 	 * structures with fixed-length char arrays, and don't null-term.*/
2252ff7b5d0Sandre 	char full[17], strip[9], abbrev[5];
2262ff7b5d0Sandre 
2272ff7b5d0Sandre 	memset(full, '\0', sizeof(full));
2282ff7b5d0Sandre 	memset(strip, '\0', sizeof(strip));
2292ff7b5d0Sandre 	memset(abbrev, '\0', sizeof(abbrev));
2302ff7b5d0Sandre 
2312ff7b5d0Sandre 	line_fullname(full, line, sizeof(full)-1);
2322ff7b5d0Sandre 	line_stripname(strip, full, sizeof(strip)-1);
2332ff7b5d0Sandre 	line_abbrevname(abbrev, full, sizeof(abbrev)-1);
2342ff7b5d0Sandre 	printf("%s: %s, %s, %s\n", line, full, strip, abbrev);
2352ff7b5d0Sandre 
2362ff7b5d0Sandre } /* testLineName() */
2372ff7b5d0Sandre 
2382ff7b5d0Sandre 
2396e5d3475Sandre int
testOutput()2406e5d3475Sandre testOutput()
2416e5d3475Sandre {
2422ff7b5d0Sandre 	printf("**\n** Testing linename functions\n**\n");
2432ff7b5d0Sandre 	testLineName("/dev/pts/1");
2442ff7b5d0Sandre 	testLineName("pts/1");
2452ff7b5d0Sandre 	testLineName("pts/999");
2462ff7b5d0Sandre 	testLineName("/dev/ttyp00");
2472ff7b5d0Sandre 	testLineName("ttyp00");
2482ff7b5d0Sandre 
2492ff7b5d0Sandre 	return 1;
2502ff7b5d0Sandre } /* testOutput() */
2512ff7b5d0Sandre 
2522ff7b5d0Sandre 
2532ff7b5d0Sandre /* show which options got compiled in */
2546e5d3475Sandre void
showOptions(void)2556e5d3475Sandre showOptions(void)
25661e67250Sandre {
2572ff7b5d0Sandre 	printf("**\n** Compile-time options\n**\n");
2582ff7b5d0Sandre 
2592ff7b5d0Sandre 	printf("login recording methods selected:\n");
2602ff7b5d0Sandre #ifdef USE_LOGIN
2612ff7b5d0Sandre 	printf("\tUSE_LOGIN\n");
2622ff7b5d0Sandre #endif
2632ff7b5d0Sandre #ifdef USE_UTMP
2642ff7b5d0Sandre 	printf("\tUSE_UTMP (UTMP_FILE=%s)\n", UTMP_FILE);
2652ff7b5d0Sandre #endif
2662ff7b5d0Sandre #ifdef USE_UTMPX
267*261d93a5SDarren Tucker 	printf("\tUSE_UTMPX\n");
2682ff7b5d0Sandre #endif
2692ff7b5d0Sandre #ifdef USE_WTMP
2702ff7b5d0Sandre 	printf("\tUSE_WTMP (WTMP_FILE=%s)\n", WTMP_FILE);
2712ff7b5d0Sandre #endif
2722ff7b5d0Sandre #ifdef USE_WTMPX
2732ff7b5d0Sandre 	printf("\tUSE_WTMPX (WTMPX_FILE=%s)\n", WTMPX_FILE);
2742ff7b5d0Sandre #endif
2752ff7b5d0Sandre #ifdef USE_LASTLOG
2762ff7b5d0Sandre 	printf("\tUSE_LASTLOG (LASTLOG_FILE=%s)\n", LASTLOG_FILE);
2772ff7b5d0Sandre #endif
2782ff7b5d0Sandre 	printf("\n");
2792ff7b5d0Sandre 
2802ff7b5d0Sandre } /* showOptions() */
2812ff7b5d0Sandre 
2822ff7b5d0Sandre 
2836e5d3475Sandre int
main(int argc,char * argv[])2846e5d3475Sandre main(int argc, char *argv[])
28561e67250Sandre {
28661e67250Sandre 	printf("Platform-independent login recording test driver\n");
2872ff7b5d0Sandre 
28859d3d5b8SDamien Miller 	__progname = ssh_get_progname(argv[0]);
2892ff7b5d0Sandre 	if (argc == 2) {
2902ff7b5d0Sandre 		if (strncmp(argv[1], "-i", 3) == 0)
2912ff7b5d0Sandre 			compile_opts_only = 1;
2922ff7b5d0Sandre 		else if (strncmp(argv[1], "-v", 3) == 0)
2932ff7b5d0Sandre 			be_verbose=1;
2942ff7b5d0Sandre 	}
2952ff7b5d0Sandre 
2962ff7b5d0Sandre 	if (!compile_opts_only) {
2972ff7b5d0Sandre 		if (be_verbose && !testOutput())
2982ff7b5d0Sandre 			return 1;
2992ff7b5d0Sandre 
3002ff7b5d0Sandre 		if (!testAPI())
3012ff7b5d0Sandre 			return 1;
3022ff7b5d0Sandre 	}
3032ff7b5d0Sandre 
3042ff7b5d0Sandre 	showOptions();
3052ff7b5d0Sandre 
3062ff7b5d0Sandre 	return 0;
3072ff7b5d0Sandre } /* main() */
3082ff7b5d0Sandre 
309