blob: 95cce5a3ab864173edb3b32f2490281026790d1b [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.
andre2ff7b5d2000-06-03 14:57:40 +000012 *
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 */
24
Kevin Stevesef4eea92001-02-05 12:42:17 +000025/**
andre2ff7b5d2000-06-03 14:57:40 +000026 ** logintest.c: simple test driver for platform-independent login recording
27 ** and lastlog retrieval
28 **/
29
andreb4db42f2000-06-19 08:25:36 +000030#include "includes.h"
andre2ff7b5d2000-06-03 14:57:40 +000031
32#include <sys/types.h>
33#include <sys/wait.h>
34#include <unistd.h>
35#include <stdlib.h>
36#include <stdio.h>
37#include <string.h>
38#include <pwd.h>
39#include <netdb.h>
40#ifdef HAVE_TIME_H
41#include <time.h>
42#endif
43
44#include "loginrec.h"
45
Darren Tuckerba6de952004-07-17 14:07:42 +100046RCSID("$Id: logintest.c,v 1.11 2004/07/17 04:07:42 dtucker Exp $");
andre2ff7b5d2000-06-03 14:57:40 +000047
Kevin Stevesff8b4952001-04-05 23:05:22 +000048extern char *__progname;
andre2ff7b5d2000-06-03 14:57:40 +000049
andreb4db42f2000-06-19 08:25:36 +000050#define PAUSE_BEFORE_LOGOUT 3
51
andre2ff7b5d2000-06-03 14:57:40 +000052int nologtest = 0;
53int compile_opts_only = 0;
54int be_verbose = 0;
55
56
andre2ff7b5d2000-06-03 14:57:40 +000057/* Dump a logininfo to stdout. Assumes a tab size of 8 chars. */
andre6e5d3472000-06-13 00:43:47 +000058void
59dump_logininfo(struct logininfo *li, char *descname)
andre61e67252000-06-04 17:07:49 +000060{
andre6e5d3472000-06-13 00:43:47 +000061 /* yes I know how nasty this is */
62 printf("struct logininfo %s = {\n\t"
63 "progname\t'%s'\n\ttype\t\t%d\n\t"
64 "pid\t\t%d\n\tuid\t\t%d\n\t"
65 "line\t\t'%s'\n\tusername\t'%s'\n\t"
66 "hostname\t'%s'\n\texit\t\t%d\n\ttermination\t%d\n\t"
67 "tv_sec\t%d\n\ttv_usec\t%d\n\t"
68 "struct login_netinfo hostaddr {\n\t\t"
69 "struct sockaddr sa {\n"
70 "\t\t\tfamily\t%d\n\t\t}\n"
andre6e5d3472000-06-13 00:43:47 +000071 "\t}\n"
72 "}\n",
Kevin Stevesef4eea92001-02-05 12:42:17 +000073 descname, li->progname, li->type,
andre6e5d3472000-06-13 00:43:47 +000074 li->pid, li->uid, li->line,
Kevin Stevesef4eea92001-02-05 12:42:17 +000075 li->username, li->hostname, li->exit,
76 li->termination, li->tv_sec, li->tv_usec,
andre6e5d3472000-06-13 00:43:47 +000077 li->hostaddr.sa.sa_family);
andre2ff7b5d2000-06-03 14:57:40 +000078}
79
80
andre6e5d3472000-06-13 00:43:47 +000081int
82testAPI()
andre61e67252000-06-04 17:07:49 +000083{
andre6e5d3472000-06-13 00:43:47 +000084 struct logininfo *li1;
85 struct passwd *pw;
86 struct hostent *he;
87 struct sockaddr_in sa_in4;
88 char cmdstring[256], stripline[8];
89 char username[32];
andre2ff7b5d2000-06-03 14:57:40 +000090#ifdef HAVE_TIME_H
andreb4db42f2000-06-19 08:25:36 +000091 time_t t0, t1, t2, logintime, logouttime;
92 char s_t0[64],s_t1[64],s_t2[64];
93 char s_logintime[64], s_logouttime[64]; /* ctime() strings */
andre2ff7b5d2000-06-03 14:57:40 +000094#endif
95
andre6e5d3472000-06-13 00:43:47 +000096 printf("**\n** Testing the API...\n**\n");
andre2ff7b5d2000-06-03 14:57:40 +000097
andre6e5d3472000-06-13 00:43:47 +000098 pw = getpwuid(getuid());
99 strlcpy(username, pw->pw_name, sizeof(username));
andre2ff7b5d2000-06-03 14:57:40 +0000100
andre6e5d3472000-06-13 00:43:47 +0000101 /* gethostname(hostname, sizeof(hostname)); */
andre2ff7b5d2000-06-03 14:57:40 +0000102
andre6e5d3472000-06-13 00:43:47 +0000103 printf("login_alloc_entry test (no host info):\n");
andreb4db42f2000-06-19 08:25:36 +0000104
105 /* FIXME fake tty more effectively - this could upset some platforms */
andre6e5d3472000-06-13 00:43:47 +0000106 li1 = login_alloc_entry((int)getpid(), username, NULL, ttyname(0));
107 strlcpy(li1->progname, "OpenSSH-logintest", sizeof(li1->progname));
andre2ff7b5d2000-06-03 14:57:40 +0000108
andre6e5d3472000-06-13 00:43:47 +0000109 if (be_verbose)
110 dump_logininfo(li1, "li1");
andre2ff7b5d2000-06-03 14:57:40 +0000111
andre6e5d3472000-06-13 00:43:47 +0000112 printf("Setting host address info for 'localhost' (may call out):\n");
113 if (! (he = gethostbyname("localhost"))) {
114 printf("Couldn't set hostname(lookup failed)\n");
115 } else {
116 /* NOTE: this is messy, but typically a program wouldn't have to set
117 * any of this, a sockaddr_in* would be already prepared */
118 memcpy((void *)&(sa_in4.sin_addr), (void *)&(he->h_addr_list[0][0]),
119 sizeof(struct in_addr));
120 login_set_addr(li1, (struct sockaddr *) &sa_in4, sizeof(sa_in4));
121 strlcpy(li1->hostname, "localhost", sizeof(li1->hostname));
122 }
123 if (be_verbose)
124 dump_logininfo(li1, "li1");
andre2ff7b5d2000-06-03 14:57:40 +0000125
andre6e5d3472000-06-13 00:43:47 +0000126 if ((int)geteuid() != 0) {
127 printf("NOT RUNNING LOGIN TESTS - you are not root!\n");
andreb4db42f2000-06-19 08:25:36 +0000128 return 1;
andre6e5d3472000-06-13 00:43:47 +0000129 }
andre2ff7b5d2000-06-03 14:57:40 +0000130
andre6e5d3472000-06-13 00:43:47 +0000131 if (nologtest)
132 return 1;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000133
andre6e5d3472000-06-13 00:43:47 +0000134 line_stripname(stripline, li1->line, sizeof(stripline));
andre2ff7b5d2000-06-03 14:57:40 +0000135
andre6e5d3472000-06-13 00:43:47 +0000136 printf("Performing an invalid login attempt (no type field)\n--\n");
137 login_write(li1);
andreb4db42f2000-06-19 08:25:36 +0000138 printf("--\n(Should have written errors to stderr)\n");
andre2ff7b5d2000-06-03 14:57:40 +0000139
140#ifdef HAVE_TIME_H
andre6e5d3472000-06-13 00:43:47 +0000141 (void)time(&t0);
142 strlcpy(s_t0, ctime(&t0), sizeof(s_t0));
143 t1 = login_get_lastlog_time(getuid());
144 strlcpy(s_t1, ctime(&t1), sizeof(s_t1));
145 printf("Before logging in:\n\tcurrent time is %d - %s\t"
146 "lastlog time is %d - %s\n",
147 (int)t0, s_t0, (int)t1, s_t1);
andre2ff7b5d2000-06-03 14:57:40 +0000148#endif
149
andreb4db42f2000-06-19 08:25:36 +0000150 printf("Performing a login on line %s ", stripline);
151#ifdef HAVE_TIME_H
152 (void)time(&logintime);
153 strlcpy(s_logintime, ctime(&logintime), sizeof(s_logintime));
154 printf("at %d - %s", (int)logintime, s_logintime);
155#endif
156 printf("--\n");
andre6e5d3472000-06-13 00:43:47 +0000157 login_login(li1);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000158
andre6e5d3472000-06-13 00:43:47 +0000159 snprintf(cmdstring, sizeof(cmdstring), "who | grep '%s '",
160 stripline);
161 system(cmdstring);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000162
andreb4db42f2000-06-19 08:25:36 +0000163 printf("--\nPausing for %d second(s)...\n", PAUSE_BEFORE_LOGOUT);
164 sleep(PAUSE_BEFORE_LOGOUT);
andre2ff7b5d2000-06-03 14:57:40 +0000165
andre6e5d3472000-06-13 00:43:47 +0000166 printf("Performing a logout ");
167#ifdef HAVE_TIME_H
168 (void)time(&logouttime);
169 strlcpy(s_logouttime, ctime(&logouttime), sizeof(s_logouttime));
170 printf("at %d - %s", (int)logouttime, s_logouttime);
171#endif
andreb4db42f2000-06-19 08:25:36 +0000172 printf("\nThe root login shown above should be gone.\n"
andre6e5d3472000-06-13 00:43:47 +0000173 "If the root login hasn't gone, but another user on the same\n"
174 "pty has, this is OK - we're hacking it here, and there\n"
175 "shouldn't be two users on one pty in reality...\n"
176 "-- ('who' output follows)\n");
177 login_logout(li1);
andre2ff7b5d2000-06-03 14:57:40 +0000178
andre6e5d3472000-06-13 00:43:47 +0000179 system(cmdstring);
180 printf("-- ('who' output ends)\n");
andre2ff7b5d2000-06-03 14:57:40 +0000181
182#ifdef HAVE_TIME_H
andre6e5d3472000-06-13 00:43:47 +0000183 t2 = login_get_lastlog_time(getuid());
184 strlcpy(s_t2, ctime(&t2), sizeof(s_t2));
185 printf("After logging in, lastlog time is %d - %s\n", (int)t2, s_t2);
186 if (t1 == t2)
187 printf("The lastlog times before and after logging in are the "
188 "same.\nThis indicates that lastlog is ** NOT WORKING "
189 "CORRECTLY **\n");
190 else if (t0 != t2)
andreb4db42f2000-06-19 08:25:36 +0000191 /* We can be off by a second or so, even when recording works fine.
192 * I'm not 100% sure why, but it's true. */
andre6e5d3472000-06-13 00:43:47 +0000193 printf("** The login time and the lastlog time differ.\n"
194 "** This indicates that lastlog is either recording the "
andreb4db42f2000-06-19 08:25:36 +0000195 "wrong time,\n** or retrieving the wrong entry.\n"
196 "If it's off by less than %d second(s) "
197 "run the test again.\n", PAUSE_BEFORE_LOGOUT);
andre6e5d3472000-06-13 00:43:47 +0000198 else
199 printf("lastlog agrees with the login time. This is a good thing.\n");
andre2ff7b5d2000-06-03 14:57:40 +0000200
201#endif
202
andre6e5d3472000-06-13 00:43:47 +0000203 printf("--\nThe output of 'last' shown next should have "
Kevin Stevesef4eea92001-02-05 12:42:17 +0000204 "an entry for root \n on %s for the time shown above:\n--\n",
andre6e5d3472000-06-13 00:43:47 +0000205 stripline);
206 snprintf(cmdstring, sizeof(cmdstring), "last | grep '%s ' | head -3",
207 stripline);
208 system(cmdstring);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000209
andre6e5d3472000-06-13 00:43:47 +0000210 printf("--\nEnd of login test.\n");
andre2ff7b5d2000-06-03 14:57:40 +0000211
andre6e5d3472000-06-13 00:43:47 +0000212 login_free_entry(li1);
andre2ff7b5d2000-06-03 14:57:40 +0000213
andre6e5d3472000-06-13 00:43:47 +0000214 return 1;
andre2ff7b5d2000-06-03 14:57:40 +0000215} /* testAPI() */
216
217
andre6e5d3472000-06-13 00:43:47 +0000218void
219testLineName(char *line)
andre61e67252000-06-04 17:07:49 +0000220{
andre6e5d3472000-06-13 00:43:47 +0000221 /* have to null-terminate - these functions are designed for
222 * structures with fixed-length char arrays, and don't null-term.*/
223 char full[17], strip[9], abbrev[5];
andre2ff7b5d2000-06-03 14:57:40 +0000224
andre6e5d3472000-06-13 00:43:47 +0000225 memset(full, '\0', sizeof(full));
226 memset(strip, '\0', sizeof(strip));
227 memset(abbrev, '\0', sizeof(abbrev));
andre2ff7b5d2000-06-03 14:57:40 +0000228
andre6e5d3472000-06-13 00:43:47 +0000229 line_fullname(full, line, sizeof(full)-1);
230 line_stripname(strip, full, sizeof(strip)-1);
231 line_abbrevname(abbrev, full, sizeof(abbrev)-1);
232 printf("%s: %s, %s, %s\n", line, full, strip, abbrev);
andre2ff7b5d2000-06-03 14:57:40 +0000233
234} /* testLineName() */
235
236
andre6e5d3472000-06-13 00:43:47 +0000237int
238testOutput()
239{
240 printf("**\n** Testing linename functions\n**\n");
241 testLineName("/dev/pts/1");
242 testLineName("pts/1");
243 testLineName("pts/999");
244 testLineName("/dev/ttyp00");
245 testLineName("ttyp00");
andre2ff7b5d2000-06-03 14:57:40 +0000246
andre6e5d3472000-06-13 00:43:47 +0000247 return 1;
andre2ff7b5d2000-06-03 14:57:40 +0000248} /* testOutput() */
249
250
251/* show which options got compiled in */
andre6e5d3472000-06-13 00:43:47 +0000252void
253showOptions(void)
Kevin Stevesef4eea92001-02-05 12:42:17 +0000254{
andre6e5d3472000-06-13 00:43:47 +0000255 printf("**\n** Compile-time options\n**\n");
Kevin Stevesef4eea92001-02-05 12:42:17 +0000256
andre6e5d3472000-06-13 00:43:47 +0000257 printf("login recording methods selected:\n");
andre2ff7b5d2000-06-03 14:57:40 +0000258#ifdef USE_LOGIN
andre6e5d3472000-06-13 00:43:47 +0000259 printf("\tUSE_LOGIN\n");
andre2ff7b5d2000-06-03 14:57:40 +0000260#endif
261#ifdef USE_UTMP
andre6e5d3472000-06-13 00:43:47 +0000262 printf("\tUSE_UTMP (UTMP_FILE=%s)\n", UTMP_FILE);
andre2ff7b5d2000-06-03 14:57:40 +0000263#endif
264#ifdef USE_UTMPX
andre6e5d3472000-06-13 00:43:47 +0000265 printf("\tUSE_UTMPX (UTMPX_FILE=%s)\n", UTMPX_FILE);
andre2ff7b5d2000-06-03 14:57:40 +0000266#endif
267#ifdef USE_WTMP
andre6e5d3472000-06-13 00:43:47 +0000268 printf("\tUSE_WTMP (WTMP_FILE=%s)\n", WTMP_FILE);
andre2ff7b5d2000-06-03 14:57:40 +0000269#endif
270#ifdef USE_WTMPX
andre6e5d3472000-06-13 00:43:47 +0000271 printf("\tUSE_WTMPX (WTMPX_FILE=%s)\n", WTMPX_FILE);
andre2ff7b5d2000-06-03 14:57:40 +0000272#endif
273#ifdef USE_LASTLOG
andre6e5d3472000-06-13 00:43:47 +0000274 printf("\tUSE_LASTLOG (LASTLOG_FILE=%s)\n", LASTLOG_FILE);
andre2ff7b5d2000-06-03 14:57:40 +0000275#endif
andre6e5d3472000-06-13 00:43:47 +0000276 printf("\n");
andre2ff7b5d2000-06-03 14:57:40 +0000277
andre2ff7b5d2000-06-03 14:57:40 +0000278} /* showOptions() */
279
280
andre6e5d3472000-06-13 00:43:47 +0000281int
282main(int argc, char *argv[])
andre61e67252000-06-04 17:07:49 +0000283{
andre6e5d3472000-06-13 00:43:47 +0000284 printf("Platform-independent login recording test driver\n");
andre2ff7b5d2000-06-03 14:57:40 +0000285
Damien Miller59d3d5b2003-08-22 09:34:41 +1000286 __progname = ssh_get_progname(argv[0]);
andre6e5d3472000-06-13 00:43:47 +0000287 if (argc == 2) {
288 if (strncmp(argv[1], "-i", 3) == 0)
289 compile_opts_only = 1;
290 else if (strncmp(argv[1], "-v", 3) == 0)
291 be_verbose=1;
292 }
Kevin Stevesef4eea92001-02-05 12:42:17 +0000293
andre6e5d3472000-06-13 00:43:47 +0000294 if (!compile_opts_only) {
295 if (be_verbose && !testOutput())
296 return 1;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000297
andre6e5d3472000-06-13 00:43:47 +0000298 if (!testAPI())
299 return 1;
300 }
andre2ff7b5d2000-06-03 14:57:40 +0000301
andre6e5d3472000-06-13 00:43:47 +0000302 showOptions();
Kevin Stevesef4eea92001-02-05 12:42:17 +0000303
andre6e5d3472000-06-13 00:43:47 +0000304 return 0;
andre2ff7b5d2000-06-03 14:57:40 +0000305} /* main() */
306