blob: d974f6e49a9f29e169657c7c6d8312f7ed824c4e [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
30/**
31 ** logintest.c: simple test driver for platform-independent login recording
32 ** and lastlog retrieval
33 **/
34
35#include "config.h"
36
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
andre6e5d3472000-06-13 00:43:47 +000051RCSID("$Id: logintest.c,v 1.5 2000/06/13 00:43:47 andre Exp $");
andre2ff7b5d2000-06-03 14:57:40 +000052
53
54int nologtest = 0;
55int compile_opts_only = 0;
56int be_verbose = 0;
57
58
andre2ff7b5d2000-06-03 14:57:40 +000059/* Dump a logininfo to stdout. Assumes a tab size of 8 chars. */
andre6e5d3472000-06-13 00:43:47 +000060void
61dump_logininfo(struct logininfo *li, char *descname)
andre61e67252000-06-04 17:07:49 +000062{
andre6e5d3472000-06-13 00:43:47 +000063 /* yes I know how nasty this is */
64 printf("struct logininfo %s = {\n\t"
65 "progname\t'%s'\n\ttype\t\t%d\n\t"
66 "pid\t\t%d\n\tuid\t\t%d\n\t"
67 "line\t\t'%s'\n\tusername\t'%s'\n\t"
68 "hostname\t'%s'\n\texit\t\t%d\n\ttermination\t%d\n\t"
69 "tv_sec\t%d\n\ttv_usec\t%d\n\t"
70 "struct login_netinfo hostaddr {\n\t\t"
71 "struct sockaddr sa {\n"
72 "\t\t\tfamily\t%d\n\t\t}\n"
73 "\t\t** !!! IP6 stuff not supported yet **\n"
74 "\t}\n"
75 "}\n",
76 descname, li->progname, li->type,
77 li->pid, li->uid, li->line,
78 li->username, li->hostname, li->exit,
79 li->termination, li->tv_sec, li->tv_usec,
80 li->hostaddr.sa.sa_family);
andre2ff7b5d2000-06-03 14:57:40 +000081}
82
83
andre6e5d3472000-06-13 00:43:47 +000084int
85testAPI()
andre61e67252000-06-04 17:07:49 +000086{
andre6e5d3472000-06-13 00:43:47 +000087 struct logininfo *li1;
88 struct passwd *pw;
89 struct hostent *he;
90 struct sockaddr_in sa_in4;
91 char cmdstring[256], stripline[8];
92 char username[32];
andre2ff7b5d2000-06-03 14:57:40 +000093#ifdef HAVE_TIME_H
andre6e5d3472000-06-13 00:43:47 +000094 time_t t0, t1, t2, logouttime;
95 char s_t0[64],s_t1[64],s_t2[64], s_logouttime[64]; /* ctime() strings */
andre2ff7b5d2000-06-03 14:57:40 +000096#endif
97
andre6e5d3472000-06-13 00:43:47 +000098 printf("**\n** Testing the API...\n**\n");
andre2ff7b5d2000-06-03 14:57:40 +000099
andre6e5d3472000-06-13 00:43:47 +0000100 pw = getpwuid(getuid());
101 strlcpy(username, pw->pw_name, sizeof(username));
andre2ff7b5d2000-06-03 14:57:40 +0000102
andre6e5d3472000-06-13 00:43:47 +0000103 /* gethostname(hostname, sizeof(hostname)); */
andre2ff7b5d2000-06-03 14:57:40 +0000104
andre6e5d3472000-06-13 00:43:47 +0000105 printf("login_alloc_entry test (no host info):\n");
106 /* !!! fake tty more effectively */
107 li1 = login_alloc_entry((int)getpid(), username, NULL, ttyname(0));
108 strlcpy(li1->progname, "OpenSSH-logintest", sizeof(li1->progname));
andre2ff7b5d2000-06-03 14:57:40 +0000109
andre6e5d3472000-06-13 00:43:47 +0000110 if (be_verbose)
111 dump_logininfo(li1, "li1");
andre2ff7b5d2000-06-03 14:57:40 +0000112
andre6e5d3472000-06-13 00:43:47 +0000113 printf("Setting host address info for 'localhost' (may call out):\n");
114 if (! (he = gethostbyname("localhost"))) {
115 printf("Couldn't set hostname(lookup failed)\n");
116 } else {
117 /* NOTE: this is messy, but typically a program wouldn't have to set
118 * any of this, a sockaddr_in* would be already prepared */
119 memcpy((void *)&(sa_in4.sin_addr), (void *)&(he->h_addr_list[0][0]),
120 sizeof(struct in_addr));
121 login_set_addr(li1, (struct sockaddr *) &sa_in4, sizeof(sa_in4));
122 strlcpy(li1->hostname, "localhost", sizeof(li1->hostname));
123 }
124 if (be_verbose)
125 dump_logininfo(li1, "li1");
andre2ff7b5d2000-06-03 14:57:40 +0000126
andre6e5d3472000-06-13 00:43:47 +0000127 if ((int)geteuid() != 0) {
128 printf("NOT RUNNING LOGIN TESTS - you are not root!\n");
129 return 1; /* this isn't necessarily an error */
130 }
andre2ff7b5d2000-06-03 14:57:40 +0000131
andre6e5d3472000-06-13 00:43:47 +0000132 if (nologtest)
133 return 1;
andre2ff7b5d2000-06-03 14:57:40 +0000134
andre6e5d3472000-06-13 00:43:47 +0000135 line_stripname(stripline, li1->line, sizeof(stripline));
andre2ff7b5d2000-06-03 14:57:40 +0000136
andre6e5d3472000-06-13 00:43:47 +0000137 printf("Performing an invalid login attempt (no type field)\n--\n");
138 login_write(li1);
139 printf("--\n(Should have written an error to stderr)\n");
andre2ff7b5d2000-06-03 14:57:40 +0000140
141#ifdef HAVE_TIME_H
andre6e5d3472000-06-13 00:43:47 +0000142 (void)time(&t0);
143 strlcpy(s_t0, ctime(&t0), sizeof(s_t0));
144 t1 = login_get_lastlog_time(getuid());
145 strlcpy(s_t1, ctime(&t1), sizeof(s_t1));
146 printf("Before logging in:\n\tcurrent time is %d - %s\t"
147 "lastlog time is %d - %s\n",
148 (int)t0, s_t0, (int)t1, s_t1);
andre2ff7b5d2000-06-03 14:57:40 +0000149#endif
150
andre6e5d3472000-06-13 00:43:47 +0000151 printf("Performing a login on line %s...\n--\n", stripline);
152 login_login(li1);
andre2ff7b5d2000-06-03 14:57:40 +0000153
andre6e5d3472000-06-13 00:43:47 +0000154 snprintf(cmdstring, sizeof(cmdstring), "who | grep '%s '",
155 stripline);
156 system(cmdstring);
andre2ff7b5d2000-06-03 14:57:40 +0000157
andre6e5d3472000-06-13 00:43:47 +0000158 printf("--\nWaiting for a few seconds...\n");
159 sleep(2);
andre2ff7b5d2000-06-03 14:57:40 +0000160
andre6e5d3472000-06-13 00:43:47 +0000161 printf("Performing a logout ");
162#ifdef HAVE_TIME_H
163 (void)time(&logouttime);
164 strlcpy(s_logouttime, ctime(&logouttime), sizeof(s_logouttime));
165 printf("at %d - %s", (int)logouttime, s_logouttime);
166#endif
167 printf("(the root login shown above should be gone)\n"
168 "If the root login hasn't gone, but another user on the same\n"
169 "pty has, this is OK - we're hacking it here, and there\n"
170 "shouldn't be two users on one pty in reality...\n"
171 "-- ('who' output follows)\n");
172 login_logout(li1);
andre2ff7b5d2000-06-03 14:57:40 +0000173
andre6e5d3472000-06-13 00:43:47 +0000174 system(cmdstring);
175 printf("-- ('who' output ends)\n");
andre2ff7b5d2000-06-03 14:57:40 +0000176
177#ifdef HAVE_TIME_H
andre6e5d3472000-06-13 00:43:47 +0000178 t2 = login_get_lastlog_time(getuid());
179 strlcpy(s_t2, ctime(&t2), sizeof(s_t2));
180 printf("After logging in, lastlog time is %d - %s\n", (int)t2, s_t2);
181 if (t1 == t2)
182 printf("The lastlog times before and after logging in are the "
183 "same.\nThis indicates that lastlog is ** NOT WORKING "
184 "CORRECTLY **\n");
185 else if (t0 != t2)
186 printf("** The login time and the lastlog time differ.\n"
187 "** This indicates that lastlog is either recording the "
188 "wrong time,\n** or retrieving the wrong entry.\n");
189 else
190 printf("lastlog agrees with the login time. This is a good thing.\n");
andre2ff7b5d2000-06-03 14:57:40 +0000191
192#endif
193
andre6e5d3472000-06-13 00:43:47 +0000194 printf("--\nThe output of 'last' shown next should have "
195 "an entry for root \n on %s for the time shown above:\n--\n",
196 stripline);
197 snprintf(cmdstring, sizeof(cmdstring), "last | grep '%s ' | head -3",
198 stripline);
199 system(cmdstring);
andre2ff7b5d2000-06-03 14:57:40 +0000200
andre6e5d3472000-06-13 00:43:47 +0000201 printf("--\nEnd of login test.\n");
andre2ff7b5d2000-06-03 14:57:40 +0000202
andre6e5d3472000-06-13 00:43:47 +0000203 login_free_entry(li1);
andre2ff7b5d2000-06-03 14:57:40 +0000204
andre6e5d3472000-06-13 00:43:47 +0000205 return 1;
andre2ff7b5d2000-06-03 14:57:40 +0000206} /* testAPI() */
207
208
andre6e5d3472000-06-13 00:43:47 +0000209void
210testLineName(char *line)
andre61e67252000-06-04 17:07:49 +0000211{
andre6e5d3472000-06-13 00:43:47 +0000212 /* have to null-terminate - these functions are designed for
213 * structures with fixed-length char arrays, and don't null-term.*/
214 char full[17], strip[9], abbrev[5];
andre2ff7b5d2000-06-03 14:57:40 +0000215
andre6e5d3472000-06-13 00:43:47 +0000216 memset(full, '\0', sizeof(full));
217 memset(strip, '\0', sizeof(strip));
218 memset(abbrev, '\0', sizeof(abbrev));
andre2ff7b5d2000-06-03 14:57:40 +0000219
andre6e5d3472000-06-13 00:43:47 +0000220 line_fullname(full, line, sizeof(full)-1);
221 line_stripname(strip, full, sizeof(strip)-1);
222 line_abbrevname(abbrev, full, sizeof(abbrev)-1);
223 printf("%s: %s, %s, %s\n", line, full, strip, abbrev);
andre2ff7b5d2000-06-03 14:57:40 +0000224
225} /* testLineName() */
226
227
andre6e5d3472000-06-13 00:43:47 +0000228int
229testOutput()
230{
231 printf("**\n** Testing linename functions\n**\n");
232 testLineName("/dev/pts/1");
233 testLineName("pts/1");
234 testLineName("pts/999");
235 testLineName("/dev/ttyp00");
236 testLineName("ttyp00");
andre2ff7b5d2000-06-03 14:57:40 +0000237
andre6e5d3472000-06-13 00:43:47 +0000238 return 1;
andre2ff7b5d2000-06-03 14:57:40 +0000239} /* testOutput() */
240
241
242/* show which options got compiled in */
andre6e5d3472000-06-13 00:43:47 +0000243void
244showOptions(void)
andre61e67252000-06-04 17:07:49 +0000245{
andre6e5d3472000-06-13 00:43:47 +0000246 printf("**\n** Compile-time options\n**\n");
andre2ff7b5d2000-06-03 14:57:40 +0000247
andre6e5d3472000-06-13 00:43:47 +0000248 printf("login recording methods selected:\n");
andre2ff7b5d2000-06-03 14:57:40 +0000249#ifdef USE_LOGIN
andre6e5d3472000-06-13 00:43:47 +0000250 printf("\tUSE_LOGIN\n");
andre2ff7b5d2000-06-03 14:57:40 +0000251#endif
252#ifdef USE_UTMP
andre6e5d3472000-06-13 00:43:47 +0000253 printf("\tUSE_UTMP (UTMP_FILE=%s)\n", UTMP_FILE);
andre2ff7b5d2000-06-03 14:57:40 +0000254#endif
255#ifdef USE_UTMPX
andre6e5d3472000-06-13 00:43:47 +0000256 printf("\tUSE_UTMPX (UTMPX_FILE=%s)\n", UTMPX_FILE);
andre2ff7b5d2000-06-03 14:57:40 +0000257#endif
258#ifdef USE_WTMP
andre6e5d3472000-06-13 00:43:47 +0000259 printf("\tUSE_WTMP (WTMP_FILE=%s)\n", WTMP_FILE);
andre2ff7b5d2000-06-03 14:57:40 +0000260#endif
261#ifdef USE_WTMPX
andre6e5d3472000-06-13 00:43:47 +0000262 printf("\tUSE_WTMPX (WTMPX_FILE=%s)\n", WTMPX_FILE);
andre2ff7b5d2000-06-03 14:57:40 +0000263#endif
264#ifdef USE_LASTLOG
andre6e5d3472000-06-13 00:43:47 +0000265 printf("\tUSE_LASTLOG (LASTLOG_FILE=%s)\n", LASTLOG_FILE);
andre2ff7b5d2000-06-03 14:57:40 +0000266#endif
andre6e5d3472000-06-13 00:43:47 +0000267 printf("\n");
andre2ff7b5d2000-06-03 14:57:40 +0000268
andre2ff7b5d2000-06-03 14:57:40 +0000269} /* showOptions() */
270
271
andre6e5d3472000-06-13 00:43:47 +0000272int
273main(int argc, char *argv[])
andre61e67252000-06-04 17:07:49 +0000274{
andre6e5d3472000-06-13 00:43:47 +0000275 printf("Platform-independent login recording test driver\n");
andre2ff7b5d2000-06-03 14:57:40 +0000276
andre6e5d3472000-06-13 00:43:47 +0000277 if (argc == 2) {
278 if (strncmp(argv[1], "-i", 3) == 0)
279 compile_opts_only = 1;
280 else if (strncmp(argv[1], "-v", 3) == 0)
281 be_verbose=1;
282 }
andre2ff7b5d2000-06-03 14:57:40 +0000283
andre6e5d3472000-06-13 00:43:47 +0000284 if (!compile_opts_only) {
285 if (be_verbose && !testOutput())
286 return 1;
andre2ff7b5d2000-06-03 14:57:40 +0000287
andre6e5d3472000-06-13 00:43:47 +0000288 if (!testAPI())
289 return 1;
290 }
andre2ff7b5d2000-06-03 14:57:40 +0000291
andre6e5d3472000-06-13 00:43:47 +0000292 showOptions();
andre2ff7b5d2000-06-03 14:57:40 +0000293
andre6e5d3472000-06-13 00:43:47 +0000294 return 0;
andre2ff7b5d2000-06-03 14:57:40 +0000295} /* main() */
296