]> andersk Git - openssh.git/blame - logintest.c
Backed out previous changes - 'tolerance' setting may just hide a bug in
[openssh.git] / logintest.c
CommitLineData
1d7b9b20 1/*
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
51RCSID("$Id$");
52
53
54int nologtest = 0;
55int compile_opts_only = 0;
56int be_verbose = 0;
57
58
1d7b9b20 59/* Dump a logininfo to stdout. Assumes a tab size of 8 chars. */
564dd50a 60void dump_logininfo(struct logininfo *li, char *descname)
61{
1d7b9b20 62 /* yes I know how nasty this is */
63 printf("struct logininfo %s = {\n\t"
64 "progname\t'%s'\n\ttype\t\t%d\n\t"
65 "pid\t\t%d\n\tuid\t\t%d\n\t"
66 "line\t\t'%s'\n\tusername\t'%s'\n\t"
67 "hostname\t'%s'\n\texit\t\t%d\n\ttermination\t%d\n\t"
68 "tv_sec\t%d\n\ttv_usec\t%d\n\t"
69 "struct login_netinfo hostaddr {\n\t\t"
564dd50a 70 "struct sockaddr sa {\n"
71 "\t\t\tfamily\t%d\n\t\t}\n"
1d7b9b20 72 "\t\t** !!! IP6 stuff not supported yet **\n"
73 "\t}\n"
74 "}\n",
75 descname, li->progname, li->type,
76 li->pid, li->uid, li->line,
77 li->username, li->hostname, li->exit,
78 li->termination, li->tv_sec, li->tv_usec,
564dd50a 79 li->hostaddr.sa.sa_family);
1d7b9b20 80}
81
82
564dd50a 83int testAPI()
84{
1d7b9b20 85 struct logininfo *li1;
86 struct passwd *pw;
87 struct hostent *he;
88 struct sockaddr_in sa_in4;
89 char cmdstring[256], stripline[8];
90 char username[32];
91#ifdef HAVE_TIME_H
92 time_t t0, t1, t2;
93 char s_t0[64],s_t1[64],s_t2[64]; /* ctime() strings */
94#endif
95
96 printf("**\n** Testing the API...\n**\n");
97
98 pw = getpwuid(getuid());
99 strlcpy(username, pw->pw_name, sizeof(username));
100
101 /* gethostname(hostname, sizeof(hostname)); */
102
103 printf("login_alloc_entry test (no host info):\n");
104 /* !!! fake tty more effectively */
105 li1 = login_alloc_entry((int)getpid(), username, NULL, ttyname(0));
564dd50a 106 strlcpy(li1->progname, "OpenSSH-logintest", sizeof(li1->progname));
1d7b9b20 107
108 if (be_verbose)
109 dump_logininfo(li1, "li1");
110
564dd50a 111 printf("Setting host address info for 'localhost' (may call out):\n");
1d7b9b20 112 if (! (he = gethostbyname("localhost"))) {
113 printf("Couldn't set hostname(lookup failed)\n");
114 } else {
115 /* NOTE: this is messy, but typically a program wouldn't have to set
116 * any of this, a sockaddr_in* would be already prepared */
117 memcpy((void *)&(sa_in4.sin_addr), (void *)&(he->h_addr_list[0][0]),
118 sizeof(struct in_addr));
564dd50a 119 login_set_addr(li1, (struct sockaddr *) &sa_in4, sizeof(sa_in4));
120 strlcpy(li1->hostname, "localhost", sizeof(li1->hostname));
1d7b9b20 121 }
122 if (be_verbose)
123 dump_logininfo(li1, "li1");
124
125 if ((int)geteuid() != 0) {
126 printf("NOT RUNNING LOGIN TESTS - you are not root!\n");
127 return 1; /* this isn't necessarily an error */
128 }
129
130 if (nologtest)
131 return 1;
132
133 line_stripname(stripline, li1->line, sizeof(stripline));
134
135 printf("Performing an invalid login attempt (no type field)\n--\n");
136 login_write(li1);
137 printf("--\n(Should have written an error to stderr)\n");
138
139#ifdef HAVE_TIME_H
140 (void)time(&t0);
141 strlcpy(s_t0, ctime(&t0), sizeof(s_t0));
564dd50a 142 t1 = login_get_lastlog_time(getuid());
1d7b9b20 143 strlcpy(s_t1, ctime(&t1), sizeof(s_t1));
144 printf("Before logging in:\n\tcurrent time is %d - %s\t"
145 "lastlog time is %d - %s\n",
146 (int)t0, s_t0, (int)t1, s_t1);
147#endif
148
149 printf("Performing a login on line %s...\n--\n", stripline);
150 login_login(li1);
151
152 snprintf(cmdstring, sizeof(cmdstring), "who | grep '%s '",
153 stripline);
154 system(cmdstring);
155
156 printf("--\nWaiting for a few seconds...\n");
157 sleep(2);
158
159 printf("Performing a logout (the root login "
160 "shown above should be gone)\n"
161 "If the root login hasn't gone, but another user on the same\n"
162 "pty has, this is OK - we're hacking it here, and there\n"
163 "shouldn't be two users on one pty in reality...\n"
164 "-- ('who' output follows)\n");
165 login_logout(li1);
166
167 system(cmdstring);
168 printf("-- ('who' output ends)\n");
169
170#ifdef HAVE_TIME_H
564dd50a 171 t2 = login_get_lastlog_time(getuid());
1d7b9b20 172 strlcpy(s_t2, ctime(&t2), sizeof(s_t2));
173 printf("After logging in, lastlog time is %d - %s\n", (int)t2, s_t2);
174 if (t1 == t2)
175 printf("The lastlog times before and after logging in are the "
176 "same.\nThis indicates that lastlog is ** NOT WORKING "
177 "CORRECTLY **\n");
866c0b0c 178 else if (t0 != t2)
1d7b9b20 179 printf("** The login time and the lastlog time differ.\n"
180 "** This indicates that lastlog is either recording the "
181 "wrong time,\n** or retrieving the wrong entry.\n");
182 else
866c0b0c 183 printf("lastlog agrees with the login time. This is a good thing.\n");
1d7b9b20 184
185#endif
186
187 printf("--\nThe output of 'last' shown next should have "
188 "an entry for root \n on %s for the time shown above:\n--\n",
189 stripline);
190 snprintf(cmdstring, sizeof(cmdstring), "last | grep '%s ' | head -3",
191 stripline);
192 system(cmdstring);
193
194 printf("--\nEnd of login test.\n");
195
196 login_free_entry(li1);
197
198 return 1;
199} /* testAPI() */
200
201
564dd50a 202void testLineName(char *line)
203{
1d7b9b20 204 /* have to null-terminate - these functions are designed for
205 * structures with fixed-length char arrays, and don't null-term.*/
206 char full[17], strip[9], abbrev[5];
207
208 memset(full, '\0', sizeof(full));
209 memset(strip, '\0', sizeof(strip));
210 memset(abbrev, '\0', sizeof(abbrev));
211
212 line_fullname(full, line, sizeof(full)-1);
213 line_stripname(strip, full, sizeof(strip)-1);
214 line_abbrevname(abbrev, full, sizeof(abbrev)-1);
215 printf("%s: %s, %s, %s\n", line, full, strip, abbrev);
216
217} /* testLineName() */
218
219
220int testOutput() {
221 printf("**\n** Testing linename functions\n**\n");
222 testLineName("/dev/pts/1");
223 testLineName("pts/1");
224 testLineName("pts/999");
225 testLineName("/dev/ttyp00");
226 testLineName("ttyp00");
227
228 return 1;
229} /* testOutput() */
230
231
232/* show which options got compiled in */
564dd50a 233void showOptions(void)
234{
1d7b9b20 235 printf("**\n** Compile-time options\n**\n");
236
237 printf("login recording methods selected:\n");
238#ifdef USE_LOGIN
239 printf("\tUSE_LOGIN\n");
240#endif
241#ifdef USE_UTMP
242 printf("\tUSE_UTMP (UTMP_FILE=%s)\n", UTMP_FILE);
243#endif
244#ifdef USE_UTMPX
245 printf("\tUSE_UTMPX (UTMPX_FILE=%s)\n", UTMPX_FILE);
246#endif
247#ifdef USE_WTMP
248 printf("\tUSE_WTMP (WTMP_FILE=%s)\n", WTMP_FILE);
249#endif
250#ifdef USE_WTMPX
251 printf("\tUSE_WTMPX (WTMPX_FILE=%s)\n", WTMPX_FILE);
252#endif
253#ifdef USE_LASTLOG
254 printf("\tUSE_LASTLOG (LASTLOG_FILE=%s)\n", LASTLOG_FILE);
255#endif
256 printf("\n");
257
1d7b9b20 258} /* showOptions() */
259
260
564dd50a 261int main(int argc, char *argv[])
262{
263 printf("Platform-independent login recording test driver\n");
1d7b9b20 264
265 if (argc == 2) {
266 if (strncmp(argv[1], "-i", 3) == 0)
267 compile_opts_only = 1;
268 else if (strncmp(argv[1], "-v", 3) == 0)
269 be_verbose=1;
270 }
271
272 if (!compile_opts_only) {
273 if (be_verbose && !testOutput())
274 return 1;
275
276 if (!testAPI())
277 return 1;
278 }
279
280 showOptions();
281
282 return 0;
283} /* main() */
284
This page took 0.19814 seconds and 5 git commands to generate.