blob: 302034da0d9b3e8b68d549da5364c045b6dc895b [file] [log] [blame]
andre2ff7b5d2000-06-03 14:57:40 +00001/*
2 * Copyright (c) 2000 Andre Lucas. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * 3. All advertising materials mentioning features or use of this software
13 * must display the following acknowledgement:
14 * This product includes software developed by Markus Friedl.
15 * 4. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
Kevin Stevesef4eea92001-02-05 12:42:17 +000030/**
andre2ff7b5d2000-06-03 14:57:40 +000031 ** logintest.c: simple test driver for platform-independent login recording
32 ** and lastlog retrieval
33 **/
34
andreb4db42f2000-06-19 08:25:36 +000035#include "includes.h"
andre2ff7b5d2000-06-03 14:57:40 +000036
37#include <sys/types.h>
38#include <sys/wait.h>
39#include <unistd.h>
40#include <stdlib.h>
41#include <stdio.h>
42#include <string.h>
43#include <pwd.h>
44#include <netdb.h>
45#ifdef HAVE_TIME_H
46#include <time.h>
47#endif
48
49#include "loginrec.h"
50
Kevin Stevesef4eea92001-02-05 12:42:17 +000051RCSID("$Id: logintest.c,v 1.7 2001/02/05 12:42:18 stevesk Exp $");
andre2ff7b5d2000-06-03 14:57:40 +000052
53
andreb4db42f2000-06-19 08:25:36 +000054#define PAUSE_BEFORE_LOGOUT 3
55
andre2ff7b5d2000-06-03 14:57:40 +000056int nologtest = 0;
57int compile_opts_only = 0;
58int be_verbose = 0;
59
60
andre2ff7b5d2000-06-03 14:57:40 +000061/* Dump a logininfo to stdout. Assumes a tab size of 8 chars. */
andre6e5d3472000-06-13 00:43:47 +000062void
63dump_logininfo(struct logininfo *li, char *descname)
andre61e67252000-06-04 17:07:49 +000064{
andre6e5d3472000-06-13 00:43:47 +000065 /* yes I know how nasty this is */
66 printf("struct logininfo %s = {\n\t"
67 "progname\t'%s'\n\ttype\t\t%d\n\t"
68 "pid\t\t%d\n\tuid\t\t%d\n\t"
69 "line\t\t'%s'\n\tusername\t'%s'\n\t"
70 "hostname\t'%s'\n\texit\t\t%d\n\ttermination\t%d\n\t"
71 "tv_sec\t%d\n\ttv_usec\t%d\n\t"
72 "struct login_netinfo hostaddr {\n\t\t"
73 "struct sockaddr sa {\n"
74 "\t\t\tfamily\t%d\n\t\t}\n"
andre6e5d3472000-06-13 00:43:47 +000075 "\t}\n"
76 "}\n",
Kevin Stevesef4eea92001-02-05 12:42:17 +000077 descname, li->progname, li->type,
andre6e5d3472000-06-13 00:43:47 +000078 li->pid, li->uid, li->line,
Kevin Stevesef4eea92001-02-05 12:42:17 +000079 li->username, li->hostname, li->exit,
80 li->termination, li->tv_sec, li->tv_usec,
andre6e5d3472000-06-13 00:43:47 +000081 li->hostaddr.sa.sa_family);
andre2ff7b5d2000-06-03 14:57:40 +000082}
83
84
andre6e5d3472000-06-13 00:43:47 +000085int
86testAPI()
andre61e67252000-06-04 17:07:49 +000087{
andre6e5d3472000-06-13 00:43:47 +000088 struct logininfo *li1;
89 struct passwd *pw;
90 struct hostent *he;
91 struct sockaddr_in sa_in4;
92 char cmdstring[256], stripline[8];
93 char username[32];
andre2ff7b5d2000-06-03 14:57:40 +000094#ifdef HAVE_TIME_H
andreb4db42f2000-06-19 08:25:36 +000095 time_t t0, t1, t2, logintime, logouttime;
96 char s_t0[64],s_t1[64],s_t2[64];
97 char s_logintime[64], s_logouttime[64]; /* ctime() strings */
andre2ff7b5d2000-06-03 14:57:40 +000098#endif
99
andre6e5d3472000-06-13 00:43:47 +0000100 printf("**\n** Testing the API...\n**\n");
andre2ff7b5d2000-06-03 14:57:40 +0000101
andre6e5d3472000-06-13 00:43:47 +0000102 pw = getpwuid(getuid());
103 strlcpy(username, pw->pw_name, sizeof(username));
andre2ff7b5d2000-06-03 14:57:40 +0000104
andre6e5d3472000-06-13 00:43:47 +0000105 /* gethostname(hostname, sizeof(hostname)); */
andre2ff7b5d2000-06-03 14:57:40 +0000106
andre6e5d3472000-06-13 00:43:47 +0000107 printf("login_alloc_entry test (no host info):\n");
andreb4db42f2000-06-19 08:25:36 +0000108
109 /* FIXME fake tty more effectively - this could upset some platforms */
andre6e5d3472000-06-13 00:43:47 +0000110 li1 = login_alloc_entry((int)getpid(), username, NULL, ttyname(0));
111 strlcpy(li1->progname, "OpenSSH-logintest", sizeof(li1->progname));
andre2ff7b5d2000-06-03 14:57:40 +0000112
andre6e5d3472000-06-13 00:43:47 +0000113 if (be_verbose)
114 dump_logininfo(li1, "li1");
andre2ff7b5d2000-06-03 14:57:40 +0000115
andre6e5d3472000-06-13 00:43:47 +0000116 printf("Setting host address info for 'localhost' (may call out):\n");
117 if (! (he = gethostbyname("localhost"))) {
118 printf("Couldn't set hostname(lookup failed)\n");
119 } else {
120 /* NOTE: this is messy, but typically a program wouldn't have to set
121 * any of this, a sockaddr_in* would be already prepared */
122 memcpy((void *)&(sa_in4.sin_addr), (void *)&(he->h_addr_list[0][0]),
123 sizeof(struct in_addr));
124 login_set_addr(li1, (struct sockaddr *) &sa_in4, sizeof(sa_in4));
125 strlcpy(li1->hostname, "localhost", sizeof(li1->hostname));
126 }
127 if (be_verbose)
128 dump_logininfo(li1, "li1");
andre2ff7b5d2000-06-03 14:57:40 +0000129
andre6e5d3472000-06-13 00:43:47 +0000130 if ((int)geteuid() != 0) {
131 printf("NOT RUNNING LOGIN TESTS - you are not root!\n");
andreb4db42f2000-06-19 08:25:36 +0000132 return 1;
andre6e5d3472000-06-13 00:43:47 +0000133 }
andre2ff7b5d2000-06-03 14:57:40 +0000134
andre6e5d3472000-06-13 00:43:47 +0000135 if (nologtest)
136 return 1;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000137
andre6e5d3472000-06-13 00:43:47 +0000138 line_stripname(stripline, li1->line, sizeof(stripline));
andre2ff7b5d2000-06-03 14:57:40 +0000139
andre6e5d3472000-06-13 00:43:47 +0000140 printf("Performing an invalid login attempt (no type field)\n--\n");
141 login_write(li1);
andreb4db42f2000-06-19 08:25:36 +0000142 printf("--\n(Should have written errors to stderr)\n");
andre2ff7b5d2000-06-03 14:57:40 +0000143
144#ifdef HAVE_TIME_H
andre6e5d3472000-06-13 00:43:47 +0000145 (void)time(&t0);
146 strlcpy(s_t0, ctime(&t0), sizeof(s_t0));
147 t1 = login_get_lastlog_time(getuid());
148 strlcpy(s_t1, ctime(&t1), sizeof(s_t1));
149 printf("Before logging in:\n\tcurrent time is %d - %s\t"
150 "lastlog time is %d - %s\n",
151 (int)t0, s_t0, (int)t1, s_t1);
andre2ff7b5d2000-06-03 14:57:40 +0000152#endif
153
andreb4db42f2000-06-19 08:25:36 +0000154 printf("Performing a login on line %s ", stripline);
155#ifdef HAVE_TIME_H
156 (void)time(&logintime);
157 strlcpy(s_logintime, ctime(&logintime), sizeof(s_logintime));
158 printf("at %d - %s", (int)logintime, s_logintime);
159#endif
160 printf("--\n");
andre6e5d3472000-06-13 00:43:47 +0000161 login_login(li1);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000162
andre6e5d3472000-06-13 00:43:47 +0000163 snprintf(cmdstring, sizeof(cmdstring), "who | grep '%s '",
164 stripline);
165 system(cmdstring);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000166
andreb4db42f2000-06-19 08:25:36 +0000167 printf("--\nPausing for %d second(s)...\n", PAUSE_BEFORE_LOGOUT);
168 sleep(PAUSE_BEFORE_LOGOUT);
andre2ff7b5d2000-06-03 14:57:40 +0000169
andre6e5d3472000-06-13 00:43:47 +0000170 printf("Performing a logout ");
171#ifdef HAVE_TIME_H
172 (void)time(&logouttime);
173 strlcpy(s_logouttime, ctime(&logouttime), sizeof(s_logouttime));
174 printf("at %d - %s", (int)logouttime, s_logouttime);
175#endif
andreb4db42f2000-06-19 08:25:36 +0000176 printf("\nThe root login shown above should be gone.\n"
andre6e5d3472000-06-13 00:43:47 +0000177 "If the root login hasn't gone, but another user on the same\n"
178 "pty has, this is OK - we're hacking it here, and there\n"
179 "shouldn't be two users on one pty in reality...\n"
180 "-- ('who' output follows)\n");
181 login_logout(li1);
andre2ff7b5d2000-06-03 14:57:40 +0000182
andre6e5d3472000-06-13 00:43:47 +0000183 system(cmdstring);
184 printf("-- ('who' output ends)\n");
andre2ff7b5d2000-06-03 14:57:40 +0000185
186#ifdef HAVE_TIME_H
andre6e5d3472000-06-13 00:43:47 +0000187 t2 = login_get_lastlog_time(getuid());
188 strlcpy(s_t2, ctime(&t2), sizeof(s_t2));
189 printf("After logging in, lastlog time is %d - %s\n", (int)t2, s_t2);
190 if (t1 == t2)
191 printf("The lastlog times before and after logging in are the "
192 "same.\nThis indicates that lastlog is ** NOT WORKING "
193 "CORRECTLY **\n");
194 else if (t0 != t2)
andreb4db42f2000-06-19 08:25:36 +0000195 /* We can be off by a second or so, even when recording works fine.
196 * I'm not 100% sure why, but it's true. */
andre6e5d3472000-06-13 00:43:47 +0000197 printf("** The login time and the lastlog time differ.\n"
198 "** This indicates that lastlog is either recording the "
andreb4db42f2000-06-19 08:25:36 +0000199 "wrong time,\n** or retrieving the wrong entry.\n"
200 "If it's off by less than %d second(s) "
201 "run the test again.\n", PAUSE_BEFORE_LOGOUT);
andre6e5d3472000-06-13 00:43:47 +0000202 else
203 printf("lastlog agrees with the login time. This is a good thing.\n");
andre2ff7b5d2000-06-03 14:57:40 +0000204
205#endif
206
andre6e5d3472000-06-13 00:43:47 +0000207 printf("--\nThe output of 'last' shown next should have "
Kevin Stevesef4eea92001-02-05 12:42:17 +0000208 "an entry for root \n on %s for the time shown above:\n--\n",
andre6e5d3472000-06-13 00:43:47 +0000209 stripline);
210 snprintf(cmdstring, sizeof(cmdstring), "last | grep '%s ' | head -3",
211 stripline);
212 system(cmdstring);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000213
andre6e5d3472000-06-13 00:43:47 +0000214 printf("--\nEnd of login test.\n");
andre2ff7b5d2000-06-03 14:57:40 +0000215
andre6e5d3472000-06-13 00:43:47 +0000216 login_free_entry(li1);
andre2ff7b5d2000-06-03 14:57:40 +0000217
andre6e5d3472000-06-13 00:43:47 +0000218 return 1;
andre2ff7b5d2000-06-03 14:57:40 +0000219} /* testAPI() */
220
221
andre6e5d3472000-06-13 00:43:47 +0000222void
223testLineName(char *line)
andre61e67252000-06-04 17:07:49 +0000224{
andre6e5d3472000-06-13 00:43:47 +0000225 /* have to null-terminate - these functions are designed for
226 * structures with fixed-length char arrays, and don't null-term.*/
227 char full[17], strip[9], abbrev[5];
andre2ff7b5d2000-06-03 14:57:40 +0000228
andre6e5d3472000-06-13 00:43:47 +0000229 memset(full, '\0', sizeof(full));
230 memset(strip, '\0', sizeof(strip));
231 memset(abbrev, '\0', sizeof(abbrev));
andre2ff7b5d2000-06-03 14:57:40 +0000232
andre6e5d3472000-06-13 00:43:47 +0000233 line_fullname(full, line, sizeof(full)-1);
234 line_stripname(strip, full, sizeof(strip)-1);
235 line_abbrevname(abbrev, full, sizeof(abbrev)-1);
236 printf("%s: %s, %s, %s\n", line, full, strip, abbrev);
andre2ff7b5d2000-06-03 14:57:40 +0000237
238} /* testLineName() */
239
240
andre6e5d3472000-06-13 00:43:47 +0000241int
242testOutput()
243{
244 printf("**\n** Testing linename functions\n**\n");
245 testLineName("/dev/pts/1");
246 testLineName("pts/1");
247 testLineName("pts/999");
248 testLineName("/dev/ttyp00");
249 testLineName("ttyp00");
andre2ff7b5d2000-06-03 14:57:40 +0000250
andre6e5d3472000-06-13 00:43:47 +0000251 return 1;
andre2ff7b5d2000-06-03 14:57:40 +0000252} /* testOutput() */
253
254
255/* show which options got compiled in */
andre6e5d3472000-06-13 00:43:47 +0000256void
257showOptions(void)
Kevin Stevesef4eea92001-02-05 12:42:17 +0000258{
andre6e5d3472000-06-13 00:43:47 +0000259 printf("**\n** Compile-time options\n**\n");
Kevin Stevesef4eea92001-02-05 12:42:17 +0000260
andre6e5d3472000-06-13 00:43:47 +0000261 printf("login recording methods selected:\n");
andre2ff7b5d2000-06-03 14:57:40 +0000262#ifdef USE_LOGIN
andre6e5d3472000-06-13 00:43:47 +0000263 printf("\tUSE_LOGIN\n");
andre2ff7b5d2000-06-03 14:57:40 +0000264#endif
265#ifdef USE_UTMP
andre6e5d3472000-06-13 00:43:47 +0000266 printf("\tUSE_UTMP (UTMP_FILE=%s)\n", UTMP_FILE);
andre2ff7b5d2000-06-03 14:57:40 +0000267#endif
268#ifdef USE_UTMPX
andre6e5d3472000-06-13 00:43:47 +0000269 printf("\tUSE_UTMPX (UTMPX_FILE=%s)\n", UTMPX_FILE);
andre2ff7b5d2000-06-03 14:57:40 +0000270#endif
271#ifdef USE_WTMP
andre6e5d3472000-06-13 00:43:47 +0000272 printf("\tUSE_WTMP (WTMP_FILE=%s)\n", WTMP_FILE);
andre2ff7b5d2000-06-03 14:57:40 +0000273#endif
274#ifdef USE_WTMPX
andre6e5d3472000-06-13 00:43:47 +0000275 printf("\tUSE_WTMPX (WTMPX_FILE=%s)\n", WTMPX_FILE);
andre2ff7b5d2000-06-03 14:57:40 +0000276#endif
277#ifdef USE_LASTLOG
andre6e5d3472000-06-13 00:43:47 +0000278 printf("\tUSE_LASTLOG (LASTLOG_FILE=%s)\n", LASTLOG_FILE);
andre2ff7b5d2000-06-03 14:57:40 +0000279#endif
andre6e5d3472000-06-13 00:43:47 +0000280 printf("\n");
andre2ff7b5d2000-06-03 14:57:40 +0000281
andre2ff7b5d2000-06-03 14:57:40 +0000282} /* showOptions() */
283
284
andre6e5d3472000-06-13 00:43:47 +0000285int
286main(int argc, char *argv[])
andre61e67252000-06-04 17:07:49 +0000287{
andre6e5d3472000-06-13 00:43:47 +0000288 printf("Platform-independent login recording test driver\n");
andre2ff7b5d2000-06-03 14:57:40 +0000289
andre6e5d3472000-06-13 00:43:47 +0000290 if (argc == 2) {
291 if (strncmp(argv[1], "-i", 3) == 0)
292 compile_opts_only = 1;
293 else if (strncmp(argv[1], "-v", 3) == 0)
294 be_verbose=1;
295 }
Kevin Stevesef4eea92001-02-05 12:42:17 +0000296
andre6e5d3472000-06-13 00:43:47 +0000297 if (!compile_opts_only) {
298 if (be_verbose && !testOutput())
299 return 1;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000300
andre6e5d3472000-06-13 00:43:47 +0000301 if (!testAPI())
302 return 1;
303 }
andre2ff7b5d2000-06-03 14:57:40 +0000304
andre6e5d3472000-06-13 00:43:47 +0000305 showOptions();
Kevin Stevesef4eea92001-02-05 12:42:17 +0000306
andre6e5d3472000-06-13 00:43:47 +0000307 return 0;
andre2ff7b5d2000-06-03 14:57:40 +0000308} /* main() */
309